File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.29: download - view: text, annotated - select for diffs
Mon Dec 23 14:34:35 2013 UTC (10 years, 5 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  - Backport 1.214, 1.215.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.29 2013/12/23 14:34:35 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affiliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas, 
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course, and to display/store
   98: default quota sizes for authoring spaces.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial and community).  In each case the radio buttons allow the 
  107: selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use LONCAPA qw(:DEFAULT :match);
  168: use LONCAPA::Enrollment;
  169: use LONCAPA::lonauthcgi();
  170: use File::Copy;
  171: use Locale::Language;
  172: use DateTime::TimeZone;
  173: use DateTime::Locale;
  174: 
  175: my $registered_cleanup;
  176: my $modified_urls;
  177: 
  178: sub handler {
  179:     my $r=shift;
  180:     if ($r->header_only) {
  181:         &Apache::loncommon::content_type($r,'text/html');
  182:         $r->send_http_header;
  183:         return OK;
  184:     }
  185: 
  186:     my $context = 'domain';
  187:     my $dom = $env{'request.role.domain'};
  188:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  189:     if (&Apache::lonnet::allowed('mau',$dom)) {
  190:         &Apache::loncommon::content_type($r,'text/html');
  191:         $r->send_http_header;
  192:     } else {
  193:         $env{'user.error.msg'}=
  194:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  195:         return HTTP_NOT_ACCEPTABLE;
  196:     }
  197: 
  198:     $registered_cleanup=0;
  199:     @{$modified_urls}=();
  200: 
  201:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  202:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  203:                                             ['phase','actions']);
  204:     my $phase = 'pickactions';
  205:     if ( exists($env{'form.phase'}) ) {
  206:         $phase = $env{'form.phase'};
  207:     }
  208:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  209:     my %domconfig =
  210:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  211:                 'quotas','autoenroll','autoupdate','autocreate',
  212:                 'directorysrch','usercreation','usermodification',
  213:                 'contacts','defaults','scantron','coursecategories',
  214:                 'serverstatuses','requestcourses','coursedefaults',
  215:                 'usersessions','loadbalancing','requestauthor'],$dom);
  216:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  217:                        'autoupdate','autocreate','directorysrch','contacts',
  218:                        'usercreation','usermodification','scantron',
  219:                        'requestcourses','requestauthor','coursecategories',
  220:                        'serverstatuses','coursedefaults','usersessions');
  221:     my %existing;
  222:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  223:         %existing = %{$domconfig{'loadbalancing'}};
  224:     }
  225:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  226:         push(@prefs_order,'loadbalancing');
  227:     }
  228:     my %prefs = (
  229:         'rolecolors' =>
  230:                    { text => 'Default color schemes',
  231:                      help => 'Domain_Configuration_Color_Schemes',
  232:                      header => [{col1 => 'Student Settings',
  233:                                  col2 => '',},
  234:                                 {col1 => 'Coordinator Settings',
  235:                                  col2 => '',},
  236:                                 {col1 => 'Author Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Administrator Settings',
  239:                                  col2 => '',}],
  240:                     },
  241:         'login' =>
  242:                     { text => 'Log-in page options',
  243:                       help => 'Domain_Configuration_Login_Page',
  244:                       header => [{col1 => 'Log-in Page Items',
  245:                                   col2 => '',},
  246:                                  {col1 => 'Log-in Help',
  247:                                   col2 => 'Value'}],
  248:                     },
  249:         'defaults' => 
  250:                     { text => 'Default authentication/language/timezone/portal',
  251:                       help => 'Domain_Configuration_LangTZAuth',
  252:                       header => [{col1 => 'Setting',
  253:                                   col2 => 'Value'}],
  254:                     },
  255:         'quotas' => 
  256:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  257:                       help => 'Domain_Configuration_Quotas',
  258:                       header => [{col1 => 'User affiliation',
  259:                                   col2 => 'Available tools',
  260:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  261:                     },
  262:         'autoenroll' =>
  263:                    { text => 'Auto-enrollment settings',
  264:                      help => 'Domain_Configuration_Auto_Enrollment',
  265:                      header => [{col1 => 'Configuration setting',
  266:                                  col2 => 'Value(s)'}],
  267:                    },
  268:         'autoupdate' => 
  269:                    { text => 'Auto-update settings',
  270:                      help => 'Domain_Configuration_Auto_Updates',
  271:                      header => [{col1 => 'Setting',
  272:                                  col2 => 'Value',},
  273:                                 {col1 => 'Setting',
  274:                                  col2 => 'Affiliation'},
  275:                                 {col1 => 'User population',
  276:                                  col2 => 'Updateable user data'}],
  277:                   },
  278:         'autocreate' => 
  279:                   { text => 'Auto-course creation settings',
  280:                      help => 'Domain_Configuration_Auto_Creation',
  281:                      header => [{col1 => 'Configuration Setting',
  282:                                  col2 => 'Value',}],
  283:                   },
  284:         'directorysrch' => 
  285:                   { text => 'Institutional directory searches',
  286:                     help => 'Domain_Configuration_InstDirectory_Search',
  287:                     header => [{col1 => 'Setting',
  288:                                 col2 => 'Value',}],
  289:                   },
  290:         'contacts' =>
  291:                   { text => 'Contact Information',
  292:                     help => 'Domain_Configuration_Contact_Info',
  293:                     header => [{col1 => 'Setting',
  294:                                 col2 => 'Value',}],
  295:                   },
  296: 
  297:         'usercreation' => 
  298:                   { text => 'User creation',
  299:                     help => 'Domain_Configuration_User_Creation',
  300:                     header => [{col1 => 'Format rule type',
  301:                                 col2 => 'Format rules in force'},
  302:                                {col1 => 'User account creation',
  303:                                 col2 => 'Usernames which may be created',},
  304:                                {col1 => 'Context',
  305:                                 col2 => 'Assignable authentication types'}],
  306:                   },
  307:         'usermodification' =>
  308:                   { text => 'User modification',
  309:                     help => 'Domain_Configuration_User_Modification',
  310:                     header => [{col1 => 'Target user has role',
  311:                                 col2 => 'User information updateable in author context'},
  312:                                {col1 => 'Target user has role',
  313:                                 col2 => 'User information updateable in course context'},
  314:                                {col1 => "Status of user",
  315:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  316:                   },
  317:         'scantron' =>
  318:                   { text => 'Bubblesheet format file',
  319:                     help => 'Domain_Configuration_Scantron_Format',
  320:                     header => [ {col1 => 'Item',
  321:                                  col2 => '',
  322:                               }],
  323:                   },
  324:         'requestcourses' => 
  325:                  {text => 'Request creation of courses',
  326:                   help => 'Domain_Configuration_Request_Courses',
  327:                   header => [{col1 => 'User affiliation',
  328:                               col2 => 'Availability/Processing of requests',},
  329:                              {col1 => 'Setting',
  330:                               col2 => 'Value'}],
  331:                  },
  332:         'requestauthor' =>
  333:                  {text => 'Request authoring space',
  334:                   help => 'Domain_Configuration_Request_Author',
  335:                   header => [{col1 => 'User affiliation',
  336:                               col2 => 'Availability/Processing of requests',},
  337:                              {col1 => 'Setting',
  338:                               col2 => 'Value'}],
  339:                  },
  340:         'coursecategories' =>
  341:                   { text => 'Cataloging of courses/communities',
  342:                     help => 'Domain_Configuration_Cataloging_Courses',
  343:                     header => [{col1 => 'Category settings',
  344:                                 col2 => '',},
  345:                                {col1 => 'Categories',
  346:                                 col2 => '',
  347:                                }],
  348:                   },
  349:         'serverstatuses' =>
  350:                  {text   => 'Access to server status pages',
  351:                   help   => 'Domain_Configuration_Server_Status',
  352:                   header => [{col1 => 'Status Page',
  353:                               col2 => 'Other named users',
  354:                               col3 => 'Specific IPs',
  355:                             }],
  356:                  },
  357:         'coursedefaults' =>
  358:                  {text => 'Course/Community defaults',
  359:                   help => 'Domain_Configuration_Course_Defaults',
  360:                   header => [{col1 => 'Defaults which can be overridden for each course by a DC',
  361:                               col2 => 'Value',},],
  362:                  },
  363:         'usersessions' =>
  364:                  {text  => 'User session hosting/offloading',
  365:                   help  => 'Domain_Configuration_User_Sessions',
  366:                   header => [{col1 => 'Domain server',
  367:                               col2 => 'Servers to offload sessions to when busy'},
  368:                              {col1 => 'Hosting of users from other domains',
  369:                               col2 => 'Rules'},
  370:                              {col1 => "Hosting domain's own users elsewhere",
  371:                               col2 => 'Rules'}],
  372:                  },
  373:          'loadbalancing' =>
  374:                  {text  => 'Dedicated Load Balancer(s)',
  375:                   help  => 'Domain_Configuration_Load_Balancing',
  376:                   header => [{col1 => 'Balancers',
  377:                               col2 => 'Default destinations',
  378:                               col3 => 'User affiliation',
  379:                               col4 => 'Overrides'},
  380:                             ],
  381:                  },
  382:     );
  383:     if (keys(%servers) > 1) {
  384:         $prefs{'login'}  = { text   => 'Log-in page options',
  385:                              help   => 'Domain_Configuration_Login_Page',
  386:                             header => [{col1 => 'Log-in Service',
  387:                                         col2 => 'Server Setting',},
  388:                                        {col1 => 'Log-in Page Items',
  389:                                         col2 => ''},
  390:                                        {col1 => 'Log-in Help',
  391:                                         col2 => 'Value'}],
  392:                            };
  393:     }
  394: 
  395:     my @roles = ('student','coordinator','author','admin');
  396:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  397:     &Apache::lonhtmlcommon::add_breadcrumb
  398:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  399:       text=>"Settings to display/modify"});
  400:     my $confname = $dom.'-domainconfig';
  401: 
  402:     if ($phase eq 'process') {
  403:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  404:                                                               \%prefs,\%domconfig,$confname,\@roles);
  405:         if (ref($result) eq 'HASH') {
  406:             $r->rflush();
  407:             &devalidate_remote_domconfs($dom,$result);
  408:         }
  409:     } elsif ($phase eq 'display') {
  410:         my $js = &recaptcha_js().
  411:                  &credits_js();
  412:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  413:             my ($othertitle,$usertypes,$types) =
  414:                 &Apache::loncommon::sorted_inst_types($dom);
  415:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  416:                                           $domconfig{'loadbalancing'}).
  417:                    &new_spares_js().
  418:                    &common_domprefs_js().
  419:                    &Apache::loncommon::javascript_array_indexof();
  420:         }
  421:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  422:     } else {
  423: # check if domconfig user exists for the domain.
  424:         my $servadm = $r->dir_config('lonAdmEMail');
  425:         my ($configuserok,$author_ok,$switchserver) =
  426:             &config_check($dom,$confname,$servadm);
  427:         unless ($configuserok eq 'ok') {
  428:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  429:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  430:                           $confname).
  431:                       '<br />'
  432:             );
  433:             if ($switchserver) {
  434:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  435:                           '<br />'.
  436:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  437:                           '<br />'.
  438:                           &mt('The "[_1]" user can be created automatically when a Domain Coordinator visits the web-based "Set domain configuration" screen, in a session hosted on the primary library server.',$confname).
  439:                           '<br />'.
  440:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  441:                 );
  442:             } else {
  443:                 $r->print(&mt('To create that user from the command line run the ./UPDATE script found in the top level directory of the extracted LON-CAPA tarball.').
  444:                           '<br />'.
  445:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  446:                 );
  447:             }
  448:             $r->print(&Apache::loncommon::end_page());
  449:             return OK;
  450:         }
  451:         if (keys(%domconfig) == 0) {
  452:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  453:             my @ids=&Apache::lonnet::current_machine_ids();
  454:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  455:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  456:                 my @loginimages = ('img','logo','domlogo','login');
  457:                 my $custom_img_count = 0;
  458:                 foreach my $img (@loginimages) {
  459:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  460:                         $custom_img_count ++;
  461:                     }
  462:                 }
  463:                 foreach my $role (@roles) {
  464:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  465:                         $custom_img_count ++;
  466:                     }
  467:                 }
  468:                 if ($custom_img_count > 0) {
  469:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  470:                     my $switch_server = &check_switchserver($dom,$confname);
  471:                     $r->print(
  472:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  473:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  474:     &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 />'.
  475:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  476:                     if ($switch_server) {
  477:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  478:                     }
  479:                     $r->print(&Apache::loncommon::end_page());
  480:                     return OK;
  481:                 }
  482:             }
  483:         }
  484:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  485:     }
  486:     return OK;
  487: }
  488: 
  489: sub process_changes {
  490:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  491:     my %domconfig;
  492:     if (ref($values) eq 'HASH') {
  493:         %domconfig = %{$values};
  494:     }
  495:     my $output;
  496:     if ($action eq 'login') {
  497:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  498:     } elsif ($action eq 'rolecolors') {
  499:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  500:                                      $lastactref,%domconfig);
  501:     } elsif ($action eq 'quotas') {
  502:         $output = &modify_quotas($dom,$action,$lastactref,%domconfig);
  503:     } elsif ($action eq 'autoenroll') {
  504:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  505:     } elsif ($action eq 'autoupdate') {
  506:         $output = &modify_autoupdate($dom,%domconfig);
  507:     } elsif ($action eq 'autocreate') {
  508:         $output = &modify_autocreate($dom,%domconfig);
  509:     } elsif ($action eq 'directorysrch') {
  510:         $output = &modify_directorysrch($dom,%domconfig);
  511:     } elsif ($action eq 'usercreation') {
  512:         $output = &modify_usercreation($dom,%domconfig);
  513:     } elsif ($action eq 'usermodification') {
  514:         $output = &modify_usermodification($dom,%domconfig);
  515:     } elsif ($action eq 'contacts') {
  516:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  517:     } elsif ($action eq 'defaults') {
  518:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  519:     } elsif ($action eq 'scantron') {
  520:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  521:     } elsif ($action eq 'coursecategories') {
  522:         $output = &modify_coursecategories($dom,%domconfig);
  523:     } elsif ($action eq 'serverstatuses') {
  524:         $output = &modify_serverstatuses($dom,%domconfig);
  525:     } elsif ($action eq 'requestcourses') {
  526:         $output = &modify_quotas($dom,$action,$lastactref,%domconfig);
  527:     } elsif ($action eq 'requestauthor') {
  528:         $output = &modify_quotas($dom,$action,$lastactref,%domconfig);
  529:     } elsif ($action eq 'coursedefaults') {
  530:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  531:     } elsif ($action eq 'usersessions') {
  532:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  533:     } elsif ($action eq 'loadbalancing') {
  534:         $output = &modify_loadbalancing($dom,%domconfig);
  535:     }
  536:     return $output;
  537: }
  538: 
  539: sub print_config_box {
  540:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  541:     my $rowtotal = 0;
  542:     my $output;
  543:     if ($action eq 'coursecategories') {
  544:         $output = &coursecategories_javascript($settings);
  545:     }
  546:     $output .= 
  547:          '<table class="LC_nested_outer">
  548:           <tr>
  549:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  550:            &mt($item->{text}).'&nbsp;'.
  551:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  552:           '</tr>';
  553:     $rowtotal ++;
  554:     my $numheaders = 1;
  555:     if (ref($item->{'header'}) eq 'ARRAY') {
  556:         $numheaders = scalar(@{$item->{'header'}});
  557:     }
  558:     if ($numheaders > 1) {
  559:         my $colspan = '';
  560:         my $rightcolspan = '';
  561:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  562:             (($action eq 'login') && ($numheaders < 3))) {
  563:             $colspan = ' colspan="2"';
  564:         }
  565:         if ($action eq 'usersessions') {
  566:             $rightcolspan = ' colspan="3"'; 
  567:         }
  568:         $output .= '
  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'}->[0]->{'col1'}).'</td>
  574:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  575:              </tr>';
  576:         $rowtotal ++;
  577:         if ($action eq 'autoupdate') {
  578:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  579:         } elsif ($action eq 'usercreation') {
  580:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  581:         } elsif ($action eq 'usermodification') {
  582:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  583:         } elsif ($action eq 'coursecategories') {
  584:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  585:         } elsif ($action eq 'login') {
  586:             if ($numheaders == 3) {
  587:                 $colspan = ' colspan="2"';
  588:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  589:             } else {
  590:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  591:             }
  592:         } elsif ($action eq 'requestcourses') {
  593:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  594:         } elsif ($action eq 'requestauthor') {
  595:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  596:         } elsif ($action eq 'usersessions') {
  597:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  598:         } elsif ($action eq 'rolecolors') {
  599:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  600:         }
  601:         $output .= '
  602:            </table>
  603:           </td>
  604:          </tr>
  605:          <tr>
  606:            <td>
  607:             <table class="LC_nested">
  608:              <tr class="LC_info_row">
  609:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  610:         $output .= '
  611:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  612:              </tr>';
  613:             $rowtotal ++;
  614:         if ($action eq 'autoupdate') {
  615:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  616:            </table>
  617:           </td>
  618:          </tr>
  619:          <tr>
  620:            <td>
  621:             <table class="LC_nested">
  622:              <tr class="LC_info_row">
  623:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  624:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  625:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  626:             $rowtotal ++;
  627:         } elsif ($action eq 'usercreation') {
  628:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  629:            </table>
  630:           </td>
  631:          </tr>
  632:          <tr>
  633:            <td>
  634:             <table class="LC_nested">
  635:              <tr class="LC_info_row">
  636:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  637:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  638:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  639:             $rowtotal ++;
  640:         } elsif ($action eq 'usermodification') {
  641:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  642:            </table>
  643:           </td>
  644:          </tr>
  645:          <tr>
  646:            <td>
  647:             <table class="LC_nested">
  648:              <tr class="LC_info_row">
  649:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  650:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  651:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  652:             $rowtotal ++;
  653:         } elsif ($action eq 'coursecategories') {
  654:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  655:         } elsif ($action eq 'login') {
  656:             if ($numheaders == 3) {
  657:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  658:            </table>
  659:           </td>
  660:          </tr>
  661:          <tr>
  662:            <td>
  663:             <table class="LC_nested">
  664:              <tr class="LC_info_row">
  665:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  666:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  667:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  668:                 $rowtotal ++;
  669:             } else {
  670:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  671:             }
  672:         } elsif ($action eq 'requestcourses') {
  673:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  674:         } elsif ($action eq 'requestauthor') {
  675:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  676:         } elsif ($action eq 'usersessions') {
  677:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  678:            </table>
  679:           </td>
  680:          </tr>
  681:          <tr>
  682:            <td>
  683:             <table class="LC_nested">
  684:              <tr class="LC_info_row">
  685:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  686:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  687:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  688:             $rowtotal ++;
  689:         } elsif ($action eq 'rolecolors') {
  690:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  691:            </table>
  692:           </td>
  693:          </tr>
  694:          <tr>
  695:            <td>
  696:             <table class="LC_nested">
  697:              <tr class="LC_info_row">
  698:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  699:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  700:               <td class="LC_right_item" valign="top">'.
  701:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  702:              </tr>'.
  703:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  704:            </table>
  705:           </td>
  706:          </tr>
  707:          <tr>
  708:            <td>
  709:             <table class="LC_nested">
  710:              <tr class="LC_info_row">
  711:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  712:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  713:              </tr>'.
  714:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  715:             $rowtotal += 2;
  716:         }
  717:     } else {
  718:         $output .= '
  719:           <tr>
  720:            <td>
  721:             <table class="LC_nested">
  722:              <tr class="LC_info_row">';
  723:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  724:             $output .= '  
  725:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  726:         } elsif ($action eq 'serverstatuses') {
  727:             $output .= '
  728:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  729:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  730: 
  731:         } else {
  732:             $output .= '
  733:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  734:         }
  735:         if (defined($item->{'header'}->[0]->{'col3'})) {
  736:             $output .= '<td class="LC_left_item" valign="top">'.
  737:                        &mt($item->{'header'}->[0]->{'col2'});
  738:             if ($action eq 'serverstatuses') {
  739:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  740:             } 
  741:         } else {
  742:             $output .= '<td class="LC_right_item" valign="top">'.
  743:                        &mt($item->{'header'}->[0]->{'col2'});
  744:         }
  745:         $output .= '</td>';
  746:         if ($item->{'header'}->[0]->{'col3'}) {
  747:             if (defined($item->{'header'}->[0]->{'col4'})) {
  748:                 $output .= '<td class="LC_left_item" valign="top">'.
  749:                             &mt($item->{'header'}->[0]->{'col3'});
  750:             } else {
  751:                 $output .= '<td class="LC_right_item" valign="top">'.
  752:                            &mt($item->{'header'}->[0]->{'col3'});
  753:             }
  754:             if ($action eq 'serverstatuses') {
  755:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  756:             }
  757:             $output .= '</td>';
  758:         }
  759:         if ($item->{'header'}->[0]->{'col4'}) {
  760:             $output .= '<td class="LC_right_item" valign="top">'.
  761:                        &mt($item->{'header'}->[0]->{'col4'});
  762:         }
  763:         $output .= '</tr>';
  764:         $rowtotal ++;
  765:         if ($action eq 'quotas') {
  766:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  767:         } elsif ($action eq 'autoenroll') {
  768:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  769:         } elsif ($action eq 'autocreate') {
  770:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  771:         } elsif ($action eq 'directorysrch') {
  772:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  773:         } elsif ($action eq 'contacts') {
  774:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  775:         } elsif ($action eq 'defaults') {
  776:             $output .= &print_defaults($dom,$settings,\$rowtotal);
  777:         } elsif ($action eq 'scantron') {
  778:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  779:         } elsif ($action eq 'serverstatuses') {
  780:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  781:         } elsif ($action eq 'helpsettings') {
  782:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  783:         } elsif ($action eq 'loadbalancing') {
  784:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  785:         } elsif ($action eq 'coursedefaults') {
  786:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  787:         }
  788:     }
  789:     $output .= '
  790:    </table>
  791:   </td>
  792:  </tr>
  793: </table><br />';
  794:     return ($output,$rowtotal);
  795: }
  796: 
  797: sub print_login {
  798:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  799:     my ($css_class,$datatable);
  800:     my %choices = &login_choices();
  801: 
  802:     if ($caller eq 'service') {
  803:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  804:         my $choice = $choices{'disallowlogin'};
  805:         $css_class = ' class="LC_odd_row"';
  806:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  807:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  808:                       '<th>'.$choices{'server'}.'</th>'.
  809:                       '<th>'.$choices{'serverpath'}.'</th>'.
  810:                       '<th>'.$choices{'custompath'}.'</th>'.
  811:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  812:         my %disallowed;
  813:         if (ref($settings) eq 'HASH') {
  814:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  815:                %disallowed = %{$settings->{'loginvia'}};
  816:             }
  817:         }
  818:         foreach my $lonhost (sort(keys(%servers))) {
  819:             my $direct = 'selected="selected"';
  820:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  821:                 if ($disallowed{$lonhost}{'server'} ne '') {
  822:                     $direct = '';
  823:                 }
  824:             }
  825:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  826:                           '<td><select name="'.$lonhost.'_server">'.
  827:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  828:                           '</option>';
  829:             foreach my $hostid (sort(keys(%servers))) {
  830:                 next if ($servers{$hostid} eq $servers{$lonhost});
  831:                 my $selected = '';
  832:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  833:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  834:                         $selected = 'selected="selected"';
  835:                     }
  836:                 }
  837:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  838:                               $servers{$hostid}.'</option>';
  839:             }
  840:             $datatable .= '</select></td>'.
  841:                           '<td><select name="'.$lonhost.'_serverpath">';
  842:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  843:                 my $pathname = $path;
  844:                 if ($path eq 'custom') {
  845:                     $pathname = &mt('Custom Path').' ->';
  846:                 }
  847:                 my $selected = '';
  848:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  849:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  850:                         $selected = 'selected="selected"';
  851:                     }
  852:                 } elsif ($path eq '') {
  853:                     $selected = 'selected="selected"';
  854:                 }
  855:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  856:             }
  857:             $datatable .= '</select></td>';
  858:             my ($custom,$exempt);
  859:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  860:                 $custom = $disallowed{$lonhost}{'custompath'};
  861:                 $exempt = $disallowed{$lonhost}{'exempt'};
  862:             }
  863:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  864:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  865:                           '</tr>';
  866:         }
  867:         $datatable .= '</table></td></tr>';
  868:         return $datatable;
  869:     } elsif ($caller eq 'page') {
  870:         my %defaultchecked = ( 
  871:                                'coursecatalog' => 'on',
  872:                                'helpdesk'      => 'on',
  873:                                'adminmail'     => 'off',
  874:                                'newuser'       => 'off',
  875:                              );
  876:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  877:         my (%checkedon,%checkedoff);
  878:         foreach my $item (@toggles) {
  879:             if ($defaultchecked{$item} eq 'on') { 
  880:                 $checkedon{$item} = ' checked="checked" ';
  881:                 $checkedoff{$item} = ' ';
  882:             } elsif ($defaultchecked{$item} eq 'off') {
  883:                 $checkedoff{$item} = ' checked="checked" ';
  884:                 $checkedon{$item} = ' ';
  885:             }
  886:         }
  887:         my @images = ('img','logo','domlogo','login');
  888:         my @logintext = ('textcol','bgcol');
  889:         my @bgs = ('pgbg','mainbg','sidebg');
  890:         my @links = ('link','alink','vlink');
  891:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  892:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  893:         my (%is_custom,%designs);
  894:         my %defaults = (
  895:                        font => $defaultdesign{'login.font'},
  896:                        );
  897:         foreach my $item (@images) {
  898:             $defaults{$item} = $defaultdesign{'login.'.$item};
  899:             $defaults{'showlogo'}{$item} = 1;
  900:         }
  901:         foreach my $item (@bgs) {
  902:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  903:         }
  904:         foreach my $item (@logintext) {
  905:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  906:         }
  907:         foreach my $item (@links) {
  908:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  909:         }
  910:         if (ref($settings) eq 'HASH') {
  911:             foreach my $item (@toggles) {
  912:                 if ($settings->{$item} eq '1') {
  913:                     $checkedon{$item} =  ' checked="checked" ';
  914:                     $checkedoff{$item} = ' ';
  915:                 } elsif ($settings->{$item} eq '0') {
  916:                     $checkedoff{$item} =  ' checked="checked" ';
  917:                     $checkedon{$item} = ' ';
  918:                 }
  919:             }
  920:             foreach my $item (@images) {
  921:                 if (defined($settings->{$item})) {
  922:                     $designs{$item} = $settings->{$item};
  923:                     $is_custom{$item} = 1;
  924:                 }
  925:                 if (defined($settings->{'showlogo'}{$item})) {
  926:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  927:                 }
  928:             }
  929:             foreach my $item (@logintext) {
  930:                 if ($settings->{$item} ne '') {
  931:                     $designs{'logintext'}{$item} = $settings->{$item};
  932:                     $is_custom{$item} = 1;
  933:                 }
  934:             }
  935:             if ($settings->{'font'} ne '') {
  936:                 $designs{'font'} = $settings->{'font'};
  937:                 $is_custom{'font'} = 1;
  938:             }
  939:             foreach my $item (@bgs) {
  940:                 if ($settings->{$item} ne '') {
  941:                     $designs{'bgs'}{$item} = $settings->{$item};
  942:                     $is_custom{$item} = 1;
  943:                 }
  944:             }
  945:             foreach my $item (@links) {
  946:                 if ($settings->{$item} ne '') {
  947:                     $designs{'links'}{$item} = $settings->{$item};
  948:                     $is_custom{$item} = 1;
  949:                 }
  950:             }
  951:         } else {
  952:             if ($designhash{$dom.'.login.font'} ne '') {
  953:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  954:                 $is_custom{'font'} = 1;
  955:             }
  956:             foreach my $item (@images) {
  957:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  958:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  959:                     $is_custom{$item} = 1;
  960:                 }
  961:             }
  962:             foreach my $item (@bgs) {
  963:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  964:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  965:                     $is_custom{$item} = 1;
  966:                 }
  967:             }
  968:             foreach my $item (@links) {
  969:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  970:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  971:                     $is_custom{$item} = 1;
  972:                 }
  973:             }
  974:         }
  975:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  976:                                                       logo => 'Institution Logo',
  977:                                                       domlogo => 'Domain Logo',
  978:                                                       login => 'Login box');
  979:         my $itemcount = 1;
  980:         foreach my $item (@toggles) {
  981:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
  982:             $datatable .=  
  983:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  984:                 '</td><td>'.
  985:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
  986:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
  987:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
  988:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
  989:                 '</tr>';
  990:             $itemcount ++;
  991:         }
  992:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
  993:         $datatable .= '</tr></table></td></tr>';
  994:     } elsif ($caller eq 'help') {
  995:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
  996:         my $switchserver = &check_switchserver($dom,$confname);
  997:         my $itemcount = 1;
  998:         $defaulturl = '/adm/loginproblems.html';
  999:         $defaulttype = 'default';
 1000:         %lt = &Apache::lonlocal::texthash (
 1001:                      del     => 'Delete?',
 1002:                      rep     => 'Replace:',
 1003:                      upl     => 'Upload:',
 1004:                      default => 'Default',
 1005:                      custom  => 'Custom',
 1006:                                              );
 1007:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1008:         my @currlangs;
 1009:         if (ref($settings) eq 'HASH') {
 1010:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1011:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1012:                     next if ($settings->{'helpurl'}{$key} eq '');
 1013:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1014:                     $type{$key} = 'custom';
 1015:                     unless ($key eq 'nolang') {
 1016:                         push(@currlangs,$key);
 1017:                     }
 1018:                 }
 1019:             } elsif ($settings->{'helpurl'} ne '') {
 1020:                 $type{'nolang'} = 'custom';
 1021:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1022:             }
 1023:         }
 1024:         foreach my $lang ('nolang',sort(@currlangs)) {
 1025:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1026:             $datatable .= '<tr'.$css_class.'>';
 1027:             if ($url{$lang} eq '') {
 1028:                 $url{$lang} = $defaulturl;
 1029:             }
 1030:             if ($type{$lang} eq '') {
 1031:                 $type{$lang} = $defaulttype;
 1032:             }
 1033:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1034:             if ($lang eq 'nolang') {
 1035:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1036:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1037:             } else {
 1038:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1039:                                   $langchoices{$lang},
 1040:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1041:             }
 1042:             $datatable .= '</span></td>'."\n".
 1043:                           '<td class="LC_left_item">';
 1044:             if ($type{$lang} eq 'custom') {
 1045:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1046:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1047:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1048:             } else {
 1049:                 $datatable .= $lt{'upl'};
 1050:             }
 1051:             $datatable .='<br />';
 1052:             if ($switchserver) {
 1053:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1054:             } else {
 1055:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1056:             }
 1057:             $datatable .= '</td></tr>';
 1058:             $itemcount ++;
 1059:         }
 1060:         my @addlangs;
 1061:         foreach my $lang (sort(keys(%langchoices))) {
 1062:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1063:             push(@addlangs,$lang);
 1064:         }
 1065:         if (@addlangs > 0) {
 1066:             my %toadd;
 1067:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1068:             $toadd{''} = &mt('Select');
 1069:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1070:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1071:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1072:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1073:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1074:             if ($switchserver) {
 1075:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1076:             } else {
 1077:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1078:             }
 1079:             $datatable .= '</td></tr>';
 1080:             $itemcount ++;
 1081:         }
 1082:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1083:     }
 1084:     return $datatable;
 1085: }
 1086: 
 1087: sub login_choices {
 1088:     my %choices =
 1089:         &Apache::lonlocal::texthash (
 1090:             coursecatalog => 'Display Course/Community Catalog link?',
 1091:             adminmail     => "Display Administrator's E-mail Address?",
 1092:             helpdesk      => 'Display "Contact Helpdesk" link',
 1093:             disallowlogin => "Login page requests redirected",
 1094:             hostid        => "Server",
 1095:             server        => "Redirect to:",
 1096:             serverpath    => "Path",
 1097:             custompath    => "Custom", 
 1098:             exempt        => "Exempt IP(s)",
 1099:             directlogin   => "No redirect",
 1100:             newuser       => "Link to create a user account",
 1101:             img           => "Header",
 1102:             logo          => "Main Logo",
 1103:             domlogo       => "Domain Logo",
 1104:             login         => "Log-in Header", 
 1105:             textcol       => "Text color",
 1106:             bgcol         => "Box color",
 1107:             bgs           => "Background colors",
 1108:             links         => "Link colors",
 1109:             font          => "Font color",
 1110:             pgbg          => "Header",
 1111:             mainbg        => "Page",
 1112:             sidebg        => "Login box",
 1113:             link          => "Link",
 1114:             alink         => "Active link",
 1115:             vlink         => "Visited link",
 1116:         );
 1117:     return %choices;
 1118: }
 1119: 
 1120: sub print_rolecolors {
 1121:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1122:     my %choices = &color_font_choices();
 1123:     my @bgs = ('pgbg','tabbg','sidebg');
 1124:     my @links = ('link','alink','vlink');
 1125:     my @images = ('img');
 1126:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1127:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1128:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1129:     my (%is_custom,%designs);
 1130:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1131:     if (ref($settings) eq 'HASH') {
 1132:         if (ref($settings->{$role}) eq 'HASH') {
 1133:             if ($settings->{$role}->{'img'} ne '') {
 1134:                 $designs{'img'} = $settings->{$role}->{'img'};
 1135:                 $is_custom{'img'} = 1;
 1136:             }
 1137:             if ($settings->{$role}->{'font'} ne '') {
 1138:                 $designs{'font'} = $settings->{$role}->{'font'};
 1139:                 $is_custom{'font'} = 1;
 1140:             }
 1141:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1142:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1143:                 $is_custom{'fontmenu'} = 1;
 1144:             }
 1145:             foreach my $item (@bgs) {
 1146:                 if ($settings->{$role}->{$item} ne '') {
 1147:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1148:                     $is_custom{$item} = 1;
 1149:                 }
 1150:             }
 1151:             foreach my $item (@links) {
 1152:                 if ($settings->{$role}->{$item} ne '') {
 1153:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1154:                     $is_custom{$item} = 1;
 1155:                 }
 1156:             }
 1157:         }
 1158:     } else {
 1159:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1160:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1161:             $is_custom{'img'} = 1;
 1162:         }
 1163:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1164:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1165:             $is_custom{'fontmenu'} = 1; 
 1166:         }
 1167:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1168:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1169:             $is_custom{'font'} = 1;
 1170:         }
 1171:         foreach my $item (@bgs) {
 1172:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1173:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1174:                 $is_custom{$item} = 1;
 1175:             
 1176:             }
 1177:         }
 1178:         foreach my $item (@links) {
 1179:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1180:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1181:                 $is_custom{$item} = 1;
 1182:             }
 1183:         }
 1184:     }
 1185:     my $itemcount = 1;
 1186:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1187:     $datatable .= '</tr></table></td></tr>';
 1188:     return $datatable;
 1189: }
 1190: 
 1191: sub role_defaults {
 1192:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1193:     my %defaults;
 1194:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1195:         return %defaults;
 1196:     }
 1197:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1198:     if ($role eq 'login') {
 1199:         %defaults = (
 1200:                        font => $defaultdesign{$role.'.font'},
 1201:                     );
 1202:         if (ref($logintext) eq 'ARRAY') {
 1203:             foreach my $item (@{$logintext}) {
 1204:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1205:             }
 1206:         }
 1207:         foreach my $item (@{$images}) {
 1208:             $defaults{'showlogo'}{$item} = 1;
 1209:         }
 1210:     } else {
 1211:         %defaults = (
 1212:                        img => $defaultdesign{$role.'.img'},
 1213:                        font => $defaultdesign{$role.'.font'},
 1214:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1215:                     );
 1216:     }
 1217:     foreach my $item (@{$bgs}) {
 1218:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1219:     }
 1220:     foreach my $item (@{$links}) {
 1221:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1222:     }
 1223:     foreach my $item (@{$images}) {
 1224:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1225:     }
 1226:     return %defaults;
 1227: }
 1228: 
 1229: sub display_color_options {
 1230:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1231:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1232:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1233:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1234:     my $datatable = '<tr'.$css_class.'>'.
 1235:         '<td>'.$choices->{'font'}.'</td>';
 1236:     if (!$is_custom->{'font'}) {
 1237:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1238:     } else {
 1239:         $datatable .= '<td>&nbsp;</td>';
 1240:     }
 1241:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1242: 
 1243:     $datatable .= '<td><span class="LC_nobreak">'.
 1244:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1245:                   ' value="'.$current_color.'" />&nbsp;'.
 1246:                   '&nbsp;</td></tr>';
 1247:     unless ($role eq 'login') { 
 1248:         $datatable .= '<tr'.$css_class.'>'.
 1249:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1250:         if (!$is_custom->{'fontmenu'}) {
 1251:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1252:         } else {
 1253:             $datatable .= '<td>&nbsp;</td>';
 1254:         }
 1255: 	$current_color = $designs->{'fontmenu'} ?
 1256: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1257:         $datatable .= '<td><span class="LC_nobreak">'.
 1258:                       '<input class="colorchooser" type="text" size="10" name="'
 1259: 		      .$role.'_fontmenu"'.
 1260:                       ' value="'.$current_color.'" />&nbsp;'.
 1261:                       '&nbsp;</td></tr>';
 1262:     }
 1263:     my $switchserver = &check_switchserver($dom,$confname);
 1264:     foreach my $img (@{$images}) {
 1265: 	$itemcount ++;
 1266:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1267:         $datatable .= '<tr'.$css_class.'>'.
 1268:                       '<td>'.$choices->{$img};
 1269:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1270:         if ($role eq 'login') {
 1271:             if ($img eq 'login') {
 1272:                 $login_hdr_pick =
 1273:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1274:                 $logincolors =
 1275:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1276:                                        $designs,$defaults);
 1277:             } elsif ($img ne 'domlogo') {
 1278:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1279:             }
 1280:         }
 1281:         $datatable .= '</td>';
 1282:         if ($designs->{$img} ne '') {
 1283:             $imgfile = $designs->{$img};
 1284: 	    $img_import = ($imgfile =~ m{^/adm/});
 1285:         } else {
 1286:             $imgfile = $defaults->{$img};
 1287:         }
 1288:         if ($imgfile) {
 1289:             my ($showfile,$fullsize);
 1290:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1291:                 my $urldir = $1;
 1292:                 my $filename = $2;
 1293:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1294:                 if (@info) {
 1295:                     my $thumbfile = 'tn-'.$filename;
 1296:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1297:                     if (@thumb) {
 1298:                         $showfile = $urldir.'/'.$thumbfile;
 1299:                     } else {
 1300:                         $showfile = $imgfile;
 1301:                     }
 1302:                 } else {
 1303:                     $showfile = '';
 1304:                 }
 1305:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1306:                 $showfile = $imgfile;
 1307:                 my $imgdir = $1;
 1308:                 my $filename = $2;
 1309:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1310:                     $showfile = "/$imgdir/tn-".$filename;
 1311:                 } else {
 1312:                     my $input = $londocroot.$imgfile;
 1313:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1314:                     if (!-e $output) {
 1315:                         my ($width,$height) = &thumb_dimensions();
 1316:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1317:                         if ($fullwidth ne '' && $fullheight ne '') {
 1318:                             if ($fullwidth > $width && $fullheight > $height) { 
 1319:                                 my $size = $width.'x'.$height;
 1320:                                 system("convert -sample $size $input $output");
 1321:                                 $showfile = "/$imgdir/tn-".$filename;
 1322:                             }
 1323:                         }
 1324:                     }
 1325:                 }
 1326:             }
 1327:             if ($showfile) {
 1328:                 if ($showfile =~ m{^/(adm|res)/}) {
 1329:                     if ($showfile =~ m{^/res/}) {
 1330:                         my $local_showfile =
 1331:                             &Apache::lonnet::filelocation('',$showfile);
 1332:                         &Apache::lonnet::repcopy($local_showfile);
 1333:                     }
 1334:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1335:                 }
 1336:                 if ($imgfile) {
 1337:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1338:                         if ($imgfile =~ m{^/res/}) {
 1339:                             my $local_imgfile =
 1340:                                 &Apache::lonnet::filelocation('',$imgfile);
 1341:                             &Apache::lonnet::repcopy($local_imgfile);
 1342:                         }
 1343:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1344:                     } else {
 1345:                         $fullsize = $imgfile;
 1346:                     }
 1347:                 }
 1348:                 $datatable .= '<td>';
 1349:                 if ($img eq 'login') {
 1350:                     $datatable .= $login_hdr_pick;
 1351:                 } 
 1352:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1353:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1354:             } else {
 1355:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1356:                               &mt('Upload:').'<br />';
 1357:             }
 1358:         } else {
 1359:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1360:                           &mt('Upload:').'<br />';
 1361:         }
 1362:         if ($switchserver) {
 1363:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1364:         } else {
 1365:             if ($img ne 'login') { # suppress file selection for Log-in header
 1366:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1367:             }
 1368:         }
 1369:         $datatable .= '</td></tr>';
 1370:     }
 1371:     $itemcount ++;
 1372:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1373:     $datatable .= '<tr'.$css_class.'>'.
 1374:                   '<td>'.$choices->{'bgs'}.'</td>';
 1375:     my $bgs_def;
 1376:     foreach my $item (@{$bgs}) {
 1377:         if (!$is_custom->{$item}) {
 1378:             $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>';
 1379:         }
 1380:     }
 1381:     if ($bgs_def) {
 1382:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1383:     } else {
 1384:         $datatable .= '<td>&nbsp;</td>';
 1385:     }
 1386:     $datatable .= '<td class="LC_right_item">'.
 1387:                   '<table border="0"><tr>';
 1388: 
 1389:     foreach my $item (@{$bgs}) {
 1390:         $datatable .= '<td align="center">'.$choices->{$item};
 1391: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1392:         if ($designs->{'bgs'}{$item}) {
 1393:             $datatable .= '&nbsp;';
 1394:         }
 1395:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1396:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1397:     }
 1398:     $datatable .= '</tr></table></td></tr>';
 1399:     $itemcount ++;
 1400:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1401:     $datatable .= '<tr'.$css_class.'>'.
 1402:                   '<td>'.$choices->{'links'}.'</td>';
 1403:     my $links_def;
 1404:     foreach my $item (@{$links}) {
 1405:         if (!$is_custom->{$item}) {
 1406:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1407:         }
 1408:     }
 1409:     if ($links_def) {
 1410:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1411:     } else {
 1412:         $datatable .= '<td>&nbsp;</td>';
 1413:     }
 1414:     $datatable .= '<td class="LC_right_item">'.
 1415:                   '<table border="0"><tr>';
 1416:     foreach my $item (@{$links}) {
 1417: 	my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1418:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1419:         if ($designs->{'links'}{$item}) {
 1420:             $datatable.='&nbsp;';
 1421:         }
 1422:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1423:                       '" /></td>';
 1424:     }
 1425:     $$rowtotal += $itemcount;
 1426:     return $datatable;
 1427: }
 1428: 
 1429: sub logo_display_options {
 1430:     my ($img,$defaults,$designs) = @_;
 1431:     my $checkedon;
 1432:     if (ref($defaults) eq 'HASH') {
 1433:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1434:             if ($defaults->{'showlogo'}{$img}) {
 1435:                 $checkedon = 'checked="checked" ';     
 1436:             }
 1437:         } 
 1438:     }
 1439:     if (ref($designs) eq 'HASH') {
 1440:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1441:             if (defined($designs->{'showlogo'}{$img})) {
 1442:                 if ($designs->{'showlogo'}{$img} == 0) {
 1443:                     $checkedon = '';
 1444:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1445:                     $checkedon = 'checked="checked" ';
 1446:                 }
 1447:             }
 1448:         }
 1449:     }
 1450:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1451:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1452:            &mt('show').'</label>'."\n";
 1453: }
 1454: 
 1455: sub login_header_options  {
 1456:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1457:     my $output = '';
 1458:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1459:         $output .= &mt('Text default(s):').'<br />';
 1460:         if (!$is_custom->{'textcol'}) {
 1461:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1462:                        '&nbsp;&nbsp;&nbsp;';
 1463:         }
 1464:         if (!$is_custom->{'bgcol'}) {
 1465:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1466:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1467:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1468:         }
 1469:         $output .= '<br />';
 1470:     }
 1471:     $output .='<br />';
 1472:     return $output;
 1473: }
 1474: 
 1475: sub login_text_colors {
 1476:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1477:     my $color_menu = '<table border="0"><tr>';
 1478:     foreach my $item (@{$logintext}) {
 1479:         $color_menu .= '<td align="center">'.$choices->{$item};
 1480:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1481:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1482:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1483:     }
 1484:     $color_menu .= '</tr></table><br />';
 1485:     return $color_menu;
 1486: }
 1487: 
 1488: sub image_changes {
 1489:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1490:     my $output;
 1491:     if ($img eq 'login') {
 1492:             # suppress image for Log-in header
 1493:     } elsif (!$is_custom) {
 1494:         if ($img ne 'domlogo') {
 1495:             $output .= &mt('Default image:').'<br />';
 1496:         } else {
 1497:             $output .= &mt('Default in use:').'<br />';
 1498:         }
 1499:     }
 1500:     if ($img eq 'login') { # suppress image for Log-in header
 1501:         $output .= '<td>'.$logincolors;
 1502:     } else {
 1503:         if ($img_import) {
 1504:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1505:         }
 1506:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1507:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1508:         if ($is_custom) {
 1509:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1510:                        '<input type="checkbox" name="'.
 1511:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1512:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1513:         } else {
 1514:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1515:         }
 1516:     }
 1517:     return $output;
 1518: }
 1519: 
 1520: sub print_quotas {
 1521:     my ($dom,$settings,$rowtotal,$action) = @_;
 1522:     my $context;
 1523:     if ($action eq 'quotas') {
 1524:         $context = 'tools';
 1525:     } else {
 1526:         $context = $action;
 1527:     }
 1528:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1529:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1530:     my $typecount = 0;
 1531:     my ($css_class,%titles);
 1532:     if ($context eq 'requestcourses') {
 1533:         @usertools = ('official','unofficial','community');
 1534:         @options =('norequest','approval','validate','autolimit');
 1535:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1536:         %titles = &courserequest_titles();
 1537:     } elsif ($context eq 'requestauthor') {
 1538:         @usertools = ('author');
 1539:         @options = ('norequest','approval','automatic');
 1540:         %titles = &authorrequest_titles();
 1541:     } else {
 1542:         @usertools = ('aboutme','blog','webdav','portfolio');
 1543:         %titles = &tool_titles();
 1544:     }
 1545:     if (ref($types) eq 'ARRAY') {
 1546:         foreach my $type (@{$types}) {
 1547:             my ($currdefquota,$currauthorquota);
 1548:             unless (($context eq 'requestcourses') ||
 1549:                     ($context eq 'requestauthor')) {
 1550:                 if (ref($settings) eq 'HASH') {
 1551:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1552:                         $currdefquota = $settings->{defaultquota}->{$type};
 1553:                     } else {
 1554:                         $currdefquota = $settings->{$type};
 1555:                     }
 1556:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1557:                         $currauthorquota = $settings->{authorquota}->{$type};
 1558:                     }
 1559:                 }
 1560:             }
 1561:             if (defined($usertypes->{$type})) {
 1562:                 $typecount ++;
 1563:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1564:                 $datatable .= '<tr'.$css_class.'>'.
 1565:                               '<td>'.$usertypes->{$type}.'</td>'.
 1566:                               '<td class="LC_left_item">';
 1567:                 if ($context eq 'requestcourses') {
 1568:                     $datatable .= '<table><tr>';
 1569:                 }
 1570:                 my %cell;  
 1571:                 foreach my $item (@usertools) {
 1572:                     if ($context eq 'requestcourses') {
 1573:                         my ($curroption,$currlimit);
 1574:                         if (ref($settings) eq 'HASH') {
 1575:                             if (ref($settings->{$item}) eq 'HASH') {
 1576:                                 $curroption = $settings->{$item}->{$type};
 1577:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1578:                                     $currlimit = $1; 
 1579:                                 }
 1580:                             }
 1581:                         }
 1582:                         if (!$curroption) {
 1583:                             $curroption = 'norequest';
 1584:                         }
 1585:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1586:                         foreach my $option (@options) {
 1587:                             my $val = $option;
 1588:                             if ($option eq 'norequest') {
 1589:                                 $val = 0;  
 1590:                             }
 1591:                             if ($option eq 'validate') {
 1592:                                 my $canvalidate = 0;
 1593:                                 if (ref($validations{$item}) eq 'HASH') { 
 1594:                                     if ($validations{$item}{$type}) {
 1595:                                         $canvalidate = 1;
 1596:                                     }
 1597:                                 }
 1598:                                 next if (!$canvalidate);
 1599:                             }
 1600:                             my $checked = '';
 1601:                             if ($option eq $curroption) {
 1602:                                 $checked = ' checked="checked"';
 1603:                             } elsif ($option eq 'autolimit') {
 1604:                                 if ($curroption =~ /^autolimit/) {
 1605:                                     $checked = ' checked="checked"';
 1606:                                 }                       
 1607:                             } 
 1608:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1609:                                   '<input type="radio" name="crsreq_'.$item.
 1610:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1611:                                   $titles{$option}.'</label>';
 1612:                             if ($option eq 'autolimit') {
 1613:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1614:                                                 $item.'_limit_'.$type.'" size="1" '.
 1615:                                                 'value="'.$currlimit.'" />';
 1616:                             }
 1617:                             $cell{$item} .= '</span> ';
 1618:                             if ($option eq 'autolimit') {
 1619:                                 $cell{$item} .= $titles{'unlimited'};
 1620:                             }
 1621:                         }
 1622:                     } elsif ($context eq 'requestauthor') {
 1623:                         my $curroption;
 1624:                         if (ref($settings) eq 'HASH') {
 1625:                             $curroption = $settings->{$type};
 1626:                         }
 1627:                         if (!$curroption) {
 1628:                             $curroption = 'norequest';
 1629:                         }
 1630:                         foreach my $option (@options) {
 1631:                             my $val = $option;
 1632:                             if ($option eq 'norequest') {
 1633:                                 $val = 0;
 1634:                             }
 1635:                             my $checked = '';
 1636:                             if ($option eq $curroption) {
 1637:                                 $checked = ' checked="checked"';
 1638:                             }
 1639:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1640:                                   '<input type="radio" name="authorreq_'.$type.
 1641:                                   '" value="'.$val.'"'.$checked.' />'.
 1642:                                   $titles{$option}.'</label></span>&nbsp; ';
 1643:                         }
 1644:                     } else {
 1645:                         my $checked = 'checked="checked" ';
 1646:                         if (ref($settings) eq 'HASH') {
 1647:                             if (ref($settings->{$item}) eq 'HASH') {
 1648:                                 if ($settings->{$item}->{$type} == 0) {
 1649:                                     $checked = '';
 1650:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1651:                                     $checked =  'checked="checked" ';
 1652:                                 }
 1653:                             }
 1654:                         }
 1655:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1656:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1657:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1658:                                       '</label></span>&nbsp; ';
 1659:                     }
 1660:                 }
 1661:                 if ($context eq 'requestcourses') {
 1662:                     $datatable .= '</tr><tr>';
 1663:                     foreach my $item (@usertools) {
 1664:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1665:                     }
 1666:                     $datatable .= '</tr></table>';
 1667:                 }
 1668:                 $datatable .= '</td>';
 1669:                 unless (($context eq 'requestcourses') ||
 1670:                         ($context eq 'requestauthor')) {
 1671:                     $datatable .= 
 1672:                               '<td class="LC_right_item">'.
 1673:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1674:                               '<input type="text" name="quota_'.$type.
 1675:                               '" value="'.$currdefquota.
 1676:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1677:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1678:                               '<input type="text" name="authorquota_'.$type.
 1679:                               '" value="'.$currauthorquota.
 1680:                               '" size="5" /></span></td>';
 1681:                 }
 1682:                 $datatable .= '</tr>';
 1683:             }
 1684:         }
 1685:     }
 1686:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1687:         $defaultquota = '20';
 1688:         $authorquota = '500';
 1689:         if (ref($settings) eq 'HASH') {
 1690:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1691:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1692:             } elsif (defined($settings->{'default'})) {
 1693:                 $defaultquota = $settings->{'default'};
 1694:             }
 1695:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1696:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1697:             }
 1698:         }
 1699:     }
 1700:     $typecount ++;
 1701:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1702:     $datatable .= '<tr'.$css_class.'>'.
 1703:                   '<td>'.$othertitle.'</td>'.
 1704:                   '<td class="LC_left_item">';
 1705:     if ($context eq 'requestcourses') {
 1706:         $datatable .= '<table><tr>';
 1707:     }
 1708:     my %defcell;
 1709:     foreach my $item (@usertools) {
 1710:         if ($context eq 'requestcourses') {
 1711:             my ($curroption,$currlimit);
 1712:             if (ref($settings) eq 'HASH') {
 1713:                 if (ref($settings->{$item}) eq 'HASH') {
 1714:                     $curroption = $settings->{$item}->{'default'};
 1715:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1716:                         $currlimit = $1;
 1717:                     }
 1718:                 }
 1719:             }
 1720:             if (!$curroption) {
 1721:                 $curroption = 'norequest';
 1722:             }
 1723:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1724:             foreach my $option (@options) {
 1725:                 my $val = $option;
 1726:                 if ($option eq 'norequest') {
 1727:                     $val = 0;
 1728:                 }
 1729:                 if ($option eq 'validate') {
 1730:                     my $canvalidate = 0;
 1731:                     if (ref($validations{$item}) eq 'HASH') {
 1732:                         if ($validations{$item}{'default'}) {
 1733:                             $canvalidate = 1;
 1734:                         }
 1735:                     }
 1736:                     next if (!$canvalidate);
 1737:                 }
 1738:                 my $checked = '';
 1739:                 if ($option eq $curroption) {
 1740:                     $checked = ' checked="checked"';
 1741:                 } elsif ($option eq 'autolimit') {
 1742:                     if ($curroption =~ /^autolimit/) {
 1743:                         $checked = ' checked="checked"';
 1744:                     }
 1745:                 }
 1746:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1747:                                   '<input type="radio" name="crsreq_'.$item.
 1748:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1749:                                   $titles{$option}.'</label>';
 1750:                 if ($option eq 'autolimit') {
 1751:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1752:                                        $item.'_limit_default" size="1" '.
 1753:                                        'value="'.$currlimit.'" />';
 1754:                 }
 1755:                 $defcell{$item} .= '</span> ';
 1756:                 if ($option eq 'autolimit') {
 1757:                     $defcell{$item} .= $titles{'unlimited'};
 1758:                 }
 1759:             }
 1760:         } elsif ($context eq 'requestauthor') {
 1761:             my $curroption;
 1762:             if (ref($settings) eq 'HASH') {
 1763:                 $curroption = $settings->{'default'};
 1764:             }
 1765:             if (!$curroption) {
 1766:                 $curroption = 'norequest';
 1767:             }
 1768:             foreach my $option (@options) {
 1769:                 my $val = $option;
 1770:                 if ($option eq 'norequest') {
 1771:                     $val = 0;
 1772:                 }
 1773:                 my $checked = '';
 1774:                 if ($option eq $curroption) {
 1775:                     $checked = ' checked="checked"';
 1776:                 }
 1777:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1778:                               '<input type="radio" name="authorreq_default"'.
 1779:                               ' value="'.$val.'"'.$checked.' />'.
 1780:                               $titles{$option}.'</label></span>&nbsp; ';
 1781:             }
 1782:         } else {
 1783:             my $checked = 'checked="checked" ';
 1784:             if (ref($settings) eq 'HASH') {
 1785:                 if (ref($settings->{$item}) eq 'HASH') {
 1786:                     if ($settings->{$item}->{'default'} == 0) {
 1787:                         $checked = '';
 1788:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1789:                         $checked = 'checked="checked" ';
 1790:                     }
 1791:                 }
 1792:             }
 1793:             $datatable .= '<span class="LC_nobreak"><label>'.
 1794:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1795:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1796:                           '</label></span>&nbsp; ';
 1797:         }
 1798:     }
 1799:     if ($context eq 'requestcourses') {
 1800:         $datatable .= '</tr><tr>';
 1801:         foreach my $item (@usertools) {
 1802:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1803:         }
 1804:         $datatable .= '</tr></table>';
 1805:     }
 1806:     $datatable .= '</td>';
 1807:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1808:         $datatable .= '<td class="LC_right_item">'.
 1809:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1810:                       '<input type="text" name="defaultquota" value="'.
 1811:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1812:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1813:                       '<input type="text" name="authorquota" value="'.
 1814:                       $authorquota.'" size="5" /></span></td>';
 1815:     }
 1816:     $datatable .= '</tr>';
 1817:     $typecount ++;
 1818:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1819:     $datatable .= '<tr'.$css_class.'>'.
 1820:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1821:     if ($context eq 'requestcourses') {
 1822:         $datatable .= &mt('(overrides affiliation, if set)').
 1823:                       '</td>'.
 1824:                       '<td class="LC_left_item">'.
 1825:                       '<table><tr>';
 1826:     } else {
 1827:         $datatable .= &mt('(overrides affiliation, if checked)').
 1828:                       '</td>'.
 1829:                       '<td class="LC_left_item" colspan="2">'.
 1830:                       '<br />';
 1831:     }
 1832:     my %advcell;
 1833:     foreach my $item (@usertools) {
 1834:         if ($context eq 'requestcourses') {
 1835:             my ($curroption,$currlimit);
 1836:             if (ref($settings) eq 'HASH') {
 1837:                 if (ref($settings->{$item}) eq 'HASH') {
 1838:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1839:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1840:                         $currlimit = $1;
 1841:                     }
 1842:                 }
 1843:             }
 1844:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1845:             my $checked = '';
 1846:             if ($curroption eq '') {
 1847:                 $checked = ' checked="checked"';
 1848:             }
 1849:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1850:                                '<input type="radio" name="crsreq_'.$item.
 1851:                                '__LC_adv" value=""'.$checked.' />'.
 1852:                                &mt('No override set').'</label></span>&nbsp; ';
 1853:             foreach my $option (@options) {
 1854:                 my $val = $option;
 1855:                 if ($option eq 'norequest') {
 1856:                     $val = 0;
 1857:                 }
 1858:                 if ($option eq 'validate') {
 1859:                     my $canvalidate = 0;
 1860:                     if (ref($validations{$item}) eq 'HASH') {
 1861:                         if ($validations{$item}{'_LC_adv'}) {
 1862:                             $canvalidate = 1;
 1863:                         }
 1864:                     }
 1865:                     next if (!$canvalidate);
 1866:                 }
 1867:                 my $checked = '';
 1868:                 if ($val eq $curroption) {
 1869:                     $checked = ' checked="checked"';
 1870:                 } elsif ($option eq 'autolimit') {
 1871:                     if ($curroption =~ /^autolimit/) {
 1872:                         $checked = ' checked="checked"';
 1873:                     }
 1874:                 }
 1875:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1876:                                   '<input type="radio" name="crsreq_'.$item.
 1877:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1878:                                   $titles{$option}.'</label>';
 1879:                 if ($option eq 'autolimit') {
 1880:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1881:                                        $item.'_limit__LC_adv" size="1" '.
 1882:                                        'value="'.$currlimit.'" />';
 1883:                 }
 1884:                 $advcell{$item} .= '</span> ';
 1885:                 if ($option eq 'autolimit') {
 1886:                     $advcell{$item} .= $titles{'unlimited'};
 1887:                 }
 1888:             }
 1889:         } elsif ($context eq 'requestauthor') {
 1890:             my $curroption;
 1891:             if (ref($settings) eq 'HASH') {
 1892:                 $curroption = $settings->{'_LC_adv'};
 1893:             }
 1894:             my $checked = '';
 1895:             if ($curroption eq '') {
 1896:                 $checked = ' checked="checked"';
 1897:             }
 1898:             $datatable .= '<span class="LC_nobreak"><label>'.
 1899:                           '<input type="radio" name="authorreq__LC_adv"'.
 1900:                           ' value=""'.$checked.' />'.
 1901:                           &mt('No override set').'</label></span>&nbsp; ';
 1902:             foreach my $option (@options) {
 1903:                 my $val = $option;
 1904:                 if ($option eq 'norequest') {
 1905:                     $val = 0;
 1906:                 }
 1907:                 my $checked = '';
 1908:                 if ($val eq $curroption) {
 1909:                     $checked = ' checked="checked"';
 1910:                 }
 1911:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1912:                               '<input type="radio" name="authorreq__LC_adv"'.
 1913:                               ' value="'.$val.'"'.$checked.' />'.
 1914:                               $titles{$option}.'</label></span>&nbsp; ';
 1915:             }
 1916:         } else {
 1917:             my $checked = 'checked="checked" ';
 1918:             if (ref($settings) eq 'HASH') {
 1919:                 if (ref($settings->{$item}) eq 'HASH') {
 1920:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1921:                         $checked = '';
 1922:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1923:                         $checked = 'checked="checked" ';
 1924:                     }
 1925:                 }
 1926:             }
 1927:             $datatable .= '<span class="LC_nobreak"><label>'.
 1928:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1929:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1930:                           '</label></span>&nbsp; ';
 1931:         }
 1932:     }
 1933:     if ($context eq 'requestcourses') {
 1934:         $datatable .= '</tr><tr>';
 1935:         foreach my $item (@usertools) {
 1936:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1937:         }
 1938:         $datatable .= '</tr></table>';
 1939:     }
 1940:     $datatable .= '</td></tr>';
 1941:     $$rowtotal += $typecount;
 1942:     return $datatable;
 1943: }
 1944: 
 1945: sub print_requestmail {
 1946:     my ($dom,$action,$settings,$rowtotal) = @_;
 1947:     my ($now,$datatable,%currapp);
 1948:     $now = time;
 1949:     if (ref($settings) eq 'HASH') {
 1950:         if (ref($settings->{'notify'}) eq 'HASH') {
 1951:             if ($settings->{'notify'}{'approval'} ne '') {
 1952:                map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 1953:             }
 1954:         }
 1955:     }
 1956:     my $numinrow = 2;
 1957:     my $css_class = 'class="LC_odd_row"';
 1958:     my $text;
 1959:     if ($action eq 'requestcourses') {
 1960:         $text = &mt('Receive notification of course requests requiring approval');
 1961:     } else {
 1962:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1963:     }
 1964:     $datatable = '<tr '.$css_class.'>'.
 1965:                  ' <td>'.$text.'</td>'.
 1966:                  ' <td class="LC_left_item">';
 1967:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 1968:                                                  'reqapprovalnotify',%currapp);
 1969:     if ($numdc > 0) {
 1970:         $datatable .= $table;
 1971:     } else {
 1972:         $datatable .= &mt('There are no active Domain Coordinators');
 1973:     }
 1974:     $datatable .='</td></tr>';
 1975:     $$rowtotal += $rows;
 1976:     return $datatable;
 1977: }
 1978: 
 1979: sub print_autoenroll {
 1980:     my ($dom,$settings,$rowtotal) = @_;
 1981:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1982:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1983:     if (ref($settings) eq 'HASH') {
 1984:         if (exists($settings->{'run'})) {
 1985:             if ($settings->{'run'} eq '0') {
 1986:                 $runoff = ' checked="checked" ';
 1987:                 $runon = ' ';
 1988:             } else {
 1989:                 $runon = ' checked="checked" ';
 1990:                 $runoff = ' ';
 1991:             }
 1992:         } else {
 1993:             if ($autorun) {
 1994:                 $runon = ' checked="checked" ';
 1995:                 $runoff = ' ';
 1996:             } else {
 1997:                 $runoff = ' checked="checked" ';
 1998:                 $runon = ' ';
 1999:             }
 2000:         }
 2001:         if (exists($settings->{'co-owners'})) {
 2002:             if ($settings->{'co-owners'} eq '0') {
 2003:                 $coownersoff = ' checked="checked" ';
 2004:                 $coownerson = ' ';
 2005:             } else {
 2006:                 $coownerson = ' checked="checked" ';
 2007:                 $coownersoff = ' ';
 2008:             }
 2009:         } else {
 2010:             $coownersoff = ' checked="checked" ';
 2011:             $coownerson = ' ';
 2012:         }
 2013:         if (exists($settings->{'sender_domain'})) {
 2014:             $defdom = $settings->{'sender_domain'};
 2015:         }
 2016:     } else {
 2017:         if ($autorun) {
 2018:             $runon = ' checked="checked" ';
 2019:             $runoff = ' ';
 2020:         } else {
 2021:             $runoff = ' checked="checked" ';
 2022:             $runon = ' ';
 2023:         }
 2024:     }
 2025:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2026:     my $notif_sender;
 2027:     if (ref($settings) eq 'HASH') {
 2028:         $notif_sender = $settings->{'sender_uname'};
 2029:     }
 2030:     my $datatable='<tr class="LC_odd_row">'.
 2031:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2032:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2033:                   '<input type="radio" name="autoenroll_run"'.
 2034:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2035:                   '<label><input type="radio" name="autoenroll_run"'.
 2036:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2037:                   '</tr><tr>'.
 2038:                   '<td>'.&mt('Notification messages - sender').
 2039:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2040:                   &mt('username').':&nbsp;'.
 2041:                   '<input type="text" name="sender_uname" value="'.
 2042:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2043:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2044:                   '<tr class="LC_odd_row">'.
 2045:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2046:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2047:                   '<input type="radio" name="autoassign_coowners"'.
 2048:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2049:                   '<label><input type="radio" name="autoassign_coowners"'.
 2050:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2051:                   '</tr>';
 2052:     $$rowtotal += 3;
 2053:     return $datatable;
 2054: }
 2055: 
 2056: sub print_autoupdate {
 2057:     my ($position,$dom,$settings,$rowtotal) = @_;
 2058:     my $datatable;
 2059:     if ($position eq 'top') {
 2060:         my $updateon = ' ';
 2061:         my $updateoff = ' checked="checked" ';
 2062:         my $classlistson = ' ';
 2063:         my $classlistsoff = ' checked="checked" ';
 2064:         if (ref($settings) eq 'HASH') {
 2065:             if ($settings->{'run'} eq '1') {
 2066:                 $updateon = $updateoff;
 2067:                 $updateoff = ' ';
 2068:             }
 2069:             if ($settings->{'classlists'} eq '1') {
 2070:                 $classlistson = $classlistsoff;
 2071:                 $classlistsoff = ' ';
 2072:             }
 2073:         }
 2074:         my %title = (
 2075:                    run => 'Auto-update active?',
 2076:                    classlists => 'Update information in classlists?',
 2077:                     );
 2078:         $datatable = '<tr class="LC_odd_row">'. 
 2079:                   '<td>'.&mt($title{'run'}).'</td>'.
 2080:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2081:                   '<input type="radio" name="autoupdate_run"'.
 2082:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2083:                   '<label><input type="radio" name="autoupdate_run"'.
 2084:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2085:                   '</tr><tr>'.
 2086:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2087:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2088:                   '<label><input type="radio" name="classlists"'.
 2089:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2090:                   '<label><input type="radio" name="classlists"'.
 2091:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2092:                   '</tr>';
 2093:         $$rowtotal += 2;
 2094:     } elsif ($position eq 'middle') {
 2095:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2096:         my $numinrow = 3;
 2097:         my $locknamesettings;
 2098:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2099:                                      $dom,$numinrow,$othertitle,
 2100:                                     'lockablenames');
 2101:         $$rowtotal ++;
 2102:     } else {
 2103:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2104:         my @fields = ('lastname','firstname','middlename','generation',
 2105:                       'permanentemail','id');
 2106:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2107:         my $numrows = 0;
 2108:         if (ref($types) eq 'ARRAY') {
 2109:             if (@{$types} > 0) {
 2110:                 $datatable = 
 2111:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2112:                                          \@fields,$types,\$numrows);
 2113:                     $$rowtotal += @{$types}; 
 2114:             }
 2115:         }
 2116:         $datatable .= 
 2117:             &usertype_update_row($settings,{'default' => $othertitle},
 2118:                                  \%fieldtitles,\@fields,['default'],
 2119:                                  \$numrows);
 2120:         $$rowtotal ++;     
 2121:     }
 2122:     return $datatable;
 2123: }
 2124: 
 2125: sub print_autocreate {
 2126:     my ($dom,$settings,$rowtotal) = @_;
 2127:     my (%createon,%createoff,%currhash);
 2128:     my @types = ('xml','req');
 2129:     if (ref($settings) eq 'HASH') {
 2130:         foreach my $item (@types) {
 2131:             $createoff{$item} = ' checked="checked" ';
 2132:             $createon{$item} = ' ';
 2133:             if (exists($settings->{$item})) {
 2134:                 if ($settings->{$item}) {
 2135:                     $createon{$item} = ' checked="checked" ';
 2136:                     $createoff{$item} = ' ';
 2137:                 }
 2138:             }
 2139:         }
 2140:         if ($settings->{'xmldc'} ne '') {
 2141:             $currhash{$settings->{'xmldc'}} = 1;
 2142:         }
 2143:     } else {
 2144:         foreach my $item (@types) {
 2145:             $createoff{$item} = ' checked="checked" ';
 2146:             $createon{$item} = ' ';
 2147:         }
 2148:     }
 2149:     $$rowtotal += 2;
 2150:     my $numinrow = 2;
 2151:     my $datatable='<tr class="LC_odd_row">'.
 2152:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2153:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2154:                   '<input type="radio" name="autocreate_xml"'.
 2155:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2156:                   '<label><input type="radio" name="autocreate_xml"'.
 2157:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2158:                   '</td></tr><tr>'.
 2159:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2160:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2161:                   '<input type="radio" name="autocreate_req"'.
 2162:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2163:                   '<label><input type="radio" name="autocreate_req"'.
 2164:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2165:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2166:                                                    'autocreate_xmldc',%currhash);
 2167:     if ($numdc > 1) {
 2168:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2169:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2170:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2171:     } else {
 2172:         $datatable .= $dctable.'</td></tr>';
 2173:     }
 2174:     $$rowtotal += $rows;
 2175:     return $datatable;
 2176: }
 2177: 
 2178: sub print_directorysrch {
 2179:     my ($dom,$settings,$rowtotal) = @_;
 2180:     my $srchon = ' ';
 2181:     my $srchoff = ' checked="checked" ';
 2182:     my ($exacton,$containson,$beginson);
 2183:     my $localon = ' ';
 2184:     my $localoff = ' checked="checked" ';
 2185:     if (ref($settings) eq 'HASH') {
 2186:         if ($settings->{'available'} eq '1') {
 2187:             $srchon = $srchoff;
 2188:             $srchoff = ' ';
 2189:         }
 2190:         if ($settings->{'localonly'} eq '1') {
 2191:             $localon = $localoff;
 2192:             $localoff = ' ';
 2193:         }
 2194:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2195:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2196:                 if ($type eq 'exact') {
 2197:                     $exacton = ' checked="checked" ';
 2198:                 } elsif ($type eq 'contains') {
 2199:                     $containson = ' checked="checked" ';
 2200:                 } elsif ($type eq 'begins') {
 2201:                     $beginson = ' checked="checked" ';
 2202:                 }
 2203:             }
 2204:         } else {
 2205:             if ($settings->{'searchtypes'} eq 'exact') {
 2206:                 $exacton = ' checked="checked" ';
 2207:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2208:                 $containson = ' checked="checked" ';
 2209:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2210:                 $exacton = ' checked="checked" ';
 2211:                 $containson = ' checked="checked" ';
 2212:             }
 2213:         }
 2214:     }
 2215:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2216:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2217: 
 2218:     my $numinrow = 4;
 2219:     my $cansrchrow = 0;
 2220:     my $datatable='<tr class="LC_odd_row">'.
 2221:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2222:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2223:                   '<input type="radio" name="dirsrch_available"'.
 2224:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2225:                   '<label><input type="radio" name="dirsrch_available"'.
 2226:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2227:                   '</tr><tr>'.
 2228:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2229:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2230:                   '<input type="radio" name="dirsrch_localonly"'.
 2231:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2232:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2233:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2234:                   '</tr>';
 2235:     $$rowtotal += 2;
 2236:     if (ref($usertypes) eq 'HASH') {
 2237:         if (keys(%{$usertypes}) > 0) {
 2238:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2239:                                          $numinrow,$othertitle,'cansearch');
 2240:             $cansrchrow = 1;
 2241:         }
 2242:     }
 2243:     if ($cansrchrow) {
 2244:         $$rowtotal ++;
 2245:         $datatable .= '<tr>';
 2246:     } else {
 2247:         $datatable .= '<tr class="LC_odd_row">';
 2248:     }
 2249:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2250:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2251:     foreach my $title (@{$titleorder}) {
 2252:         if (defined($searchtitles->{$title})) {
 2253:             my $check = ' ';
 2254:             if (ref($settings) eq 'HASH') {
 2255:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2256:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2257:                         $check = ' checked="checked" ';
 2258:                     }
 2259:                 }
 2260:             }
 2261:             $datatable .= '<td class="LC_left_item">'.
 2262:                           '<span class="LC_nobreak"><label>'.
 2263:                           '<input type="checkbox" name="searchby" '.
 2264:                           'value="'.$title.'"'.$check.'/>'.
 2265:                           $searchtitles->{$title}.'</label></span></td>';
 2266:         }
 2267:     }
 2268:     $datatable .= '</tr></table></td></tr>';
 2269:     $$rowtotal ++;
 2270:     if ($cansrchrow) {
 2271:         $datatable .= '<tr class="LC_odd_row">';
 2272:     } else {
 2273:         $datatable .= '<tr>';
 2274:     }
 2275:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2276:                   '<td class="LC_left_item" colspan="2">'.
 2277:                   '<span class="LC_nobreak"><label>'.
 2278:                   '<input type="checkbox" name="searchtypes" '.
 2279:                   $exacton.' value="exact" />'.&mt('Exact match').
 2280:                   '</label>&nbsp;'.
 2281:                   '<label><input type="checkbox" name="searchtypes" '.
 2282:                   $beginson.' value="begins" />'.&mt('Begins with').
 2283:                   '</label>&nbsp;'.
 2284:                   '<label><input type="checkbox" name="searchtypes" '.
 2285:                   $containson.' value="contains" />'.&mt('Contains').
 2286:                   '</label></span></td></tr>';
 2287:     $$rowtotal ++;
 2288:     return $datatable;
 2289: }
 2290: 
 2291: sub print_contacts {
 2292:     my ($dom,$settings,$rowtotal) = @_;
 2293:     my $datatable;
 2294:     my @contacts = ('adminemail','supportemail');
 2295:     my (%checked,%to,%otheremails,%bccemails);
 2296:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2297:                     'requestsmail','updatesmail','idconflictsmail');
 2298:     foreach my $type (@mailings) {
 2299:         $otheremails{$type} = '';
 2300:     }
 2301:     $bccemails{'helpdeskmail'} = '';
 2302:     if (ref($settings) eq 'HASH') {
 2303:         foreach my $item (@contacts) {
 2304:             if (exists($settings->{$item})) {
 2305:                 $to{$item} = $settings->{$item};
 2306:             }
 2307:         }
 2308:         foreach my $type (@mailings) {
 2309:             if (exists($settings->{$type})) {
 2310:                 if (ref($settings->{$type}) eq 'HASH') {
 2311:                     foreach my $item (@contacts) {
 2312:                         if ($settings->{$type}{$item}) {
 2313:                             $checked{$type}{$item} = ' checked="checked" ';
 2314:                         }
 2315:                     }
 2316:                     $otheremails{$type} = $settings->{$type}{'others'};
 2317:                     if ($type eq 'helpdeskmail') {
 2318:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2319:                     }
 2320:                 }
 2321:             } elsif ($type eq 'lonstatusmail') {
 2322:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2323:             }
 2324:         }
 2325:     } else {
 2326:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2327:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2328:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2329:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2330:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2331:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2332:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2333:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2334:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2335:     }
 2336:     my ($titles,$short_titles) = &contact_titles();
 2337:     my $rownum = 0;
 2338:     my $css_class;
 2339:     foreach my $item (@contacts) {
 2340:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2341:         $datatable .= '<tr'.$css_class.'>'. 
 2342:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2343:                   '</span></td><td class="LC_right_item">'.
 2344:                   '<input type="text" name="'.$item.'" value="'.
 2345:                   $to{$item}.'" /></td></tr>';
 2346:         $rownum ++;
 2347:     }
 2348:     foreach my $type (@mailings) {
 2349:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2350:         $datatable .= '<tr'.$css_class.'>'.
 2351:                       '<td><span class="LC_nobreak">'.
 2352:                       $titles->{$type}.': </span></td>'.
 2353:                       '<td class="LC_left_item">'.
 2354:                       '<span class="LC_nobreak">';
 2355:         foreach my $item (@contacts) {
 2356:             $datatable .= '<label>'.
 2357:                           '<input type="checkbox" name="'.$type.'"'.
 2358:                           $checked{$type}{$item}.
 2359:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2360:                           '</label>&nbsp;';
 2361:         }
 2362:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2363:                       '<input type="text" name="'.$type.'_others" '.
 2364:                       'value="'.$otheremails{$type}.'"  />';
 2365:         if ($type eq 'helpdeskmail') {
 2366:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2367:                           '<input type="text" name="'.$type.'_bcc" '.
 2368:                           'value="'.$bccemails{$type}.'"  />';
 2369:         }
 2370:         $datatable .= '</td></tr>'."\n";
 2371:         $rownum ++;
 2372:     }
 2373:     my %choices;
 2374:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2375:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2376:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2377:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2378:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2379:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2380:     my @toggles = ('reporterrors','reportupdates');
 2381:     my %defaultchecked = ('reporterrors'  => 'on',
 2382:                           'reportupdates' => 'on');
 2383:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2384:                                                \%choices,$rownum);
 2385:     $datatable .= $reports;
 2386:     $$rowtotal += $rownum;
 2387:     return $datatable;
 2388: }
 2389: 
 2390: sub print_helpsettings {
 2391:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2392:     my ($datatable,$itemcount);
 2393:     $itemcount = 1;
 2394:     my (%choices,%defaultchecked,@toggles);
 2395:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2396:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2397:                                  &mt('LON-CAPA bug tracker'),600,500));
 2398:     %defaultchecked = ('submitbugs' => 'on');
 2399:     @toggles = ('submitbugs',);
 2400: 
 2401:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2402:                                                  \%choices,$itemcount);
 2403:     return $datatable;
 2404: }
 2405: 
 2406: sub radiobutton_prefs {
 2407:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2408:         $additional) = @_;
 2409:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2410:                    (ref($choices) eq 'HASH'));
 2411: 
 2412:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2413: 
 2414:     foreach my $item (@{$toggles}) {
 2415:         if ($defaultchecked->{$item} eq 'on') {
 2416:             $checkedon{$item} = ' checked="checked" ';
 2417:             $checkedoff{$item} = ' ';
 2418:         } elsif ($defaultchecked->{$item} eq 'off') {
 2419:             $checkedoff{$item} = ' checked="checked" ';
 2420:             $checkedon{$item} = ' ';
 2421:         }
 2422:     }
 2423:     if (ref($settings) eq 'HASH') {
 2424:         foreach my $item (@{$toggles}) {
 2425:             if ($settings->{$item} eq '1') {
 2426:                 $checkedon{$item} =  ' checked="checked" ';
 2427:                 $checkedoff{$item} = ' ';
 2428:             } elsif ($settings->{$item} eq '0') {
 2429:                 $checkedoff{$item} =  ' checked="checked" ';
 2430:                 $checkedon{$item} = ' ';
 2431:             }
 2432:         }
 2433:     }
 2434:     if ($onclick) {
 2435:         $onclick = ' onclick="'.$onclick.'"';
 2436:     }
 2437:     foreach my $item (@{$toggles}) {
 2438:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2439:         $datatable .=
 2440:             '<tr'.$css_class.'><td valign="top">'.
 2441:             '<span class="LC_nobreak">'.$choices->{$item}.
 2442:             '</span></td>'.
 2443:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2444:             '<label><input type="radio" name="'.
 2445:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2446:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2447:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2448:             '</span>'.$additional.
 2449:             '</td>'.
 2450:             '</tr>';
 2451:         $itemcount ++;
 2452:     }
 2453:     return ($datatable,$itemcount);
 2454: }
 2455: 
 2456: sub print_coursedefaults {
 2457:     my ($position,$dom,$settings,$rowtotal) = @_;
 2458:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2459:     my $itemcount = 1;
 2460:     my %choices =  &Apache::lonlocal::texthash (
 2461:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2462:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2463:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2464:         coursecredits        => 'Credits can be specified for courses',
 2465:     );
 2466:     my %staticdefaults = (
 2467:                            anonsurvey_threshold => 10,
 2468:                            uploadquota          => 500,
 2469:                          );
 2470:     if ($position eq 'top') {
 2471:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2472:         @toggles = ('canuse_pdfforms');
 2473:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2474:                                                  \%choices,$itemcount);
 2475:     } else {
 2476:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2477:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,%curruploadquota);
 2478:         my $currusecredits = 0;
 2479:         my @types = ('official','unofficial','community');
 2480:         if (ref($settings) eq 'HASH') {
 2481:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2482:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2483:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2484:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2485:                 }
 2486:             }
 2487:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2488:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2489:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2490:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '')) {
 2491:                     $currusecredits = 1;
 2492:                 }
 2493:             }
 2494:         }
 2495:         if (!$currdefresponder) {
 2496:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2497:         } elsif ($currdefresponder < 1) {
 2498:             $currdefresponder = 1;
 2499:         }
 2500:         foreach my $type (@types) {
 2501:             if ($curruploadquota{$type} eq '') {
 2502:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2503:             }
 2504:         }
 2505:         $datatable .=
 2506:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2507:                 $choices{'anonsurvey_threshold'}.
 2508:                 '</span></td>'.
 2509:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2510:                 '<input type="text" name="anonsurvey_threshold"'.
 2511:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2512:                 '</td></tr>'."\n".
 2513:                 '<tr><td><span class="LC_nobreak">'.
 2514:                 $choices{'uploadquota'}.
 2515:                 '</span></td>'.
 2516:                 '<td align="right" class="LC_right_item">'.
 2517:                 '<table><tr>';
 2518:         foreach my $type (@types) {
 2519:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2520:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2521:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2522:         }
 2523:         $datatable .= '</tr></table></td></tr>'."\n";
 2524:         $itemcount += 2;
 2525:         my $onclick = 'toggleCredits(this.form);';
 2526:         my $display = 'none';
 2527:         if ($currusecredits) {
 2528:             $display = 'block';
 2529:         }
 2530:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2531:                          '<span class="LC_nobreak">'.
 2532:                          &mt('Default credits for official courses [_1]',
 2533:                          '<input type="text" name="official_credits" value="'.
 2534:                          $def_official_credits.'" size="3" />').
 2535:                          '</span><br />'.
 2536:                          '<span class="LC_nobreak">'.
 2537:                          &mt('Default credits for unofficial courses [_1]',
 2538:                          '<input type="text" name="unofficial_credits" value="'.
 2539:                          $def_unofficial_credits.'" size="3" />').
 2540:                          '</span></div>'."\n";
 2541:         %defaultchecked = ('coursecredits' => 'off');
 2542:         @toggles = ('coursecredits');
 2543:         my $current = {
 2544:                         'coursecredits' => $currusecredits,
 2545:                       };
 2546:         (my $table,$itemcount) =
 2547:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2548:                                \%choices,$itemcount,$onclick,$additional);
 2549:         $datatable .= $table;
 2550:     }
 2551:     $$rowtotal += $itemcount;
 2552:     return $datatable;
 2553: }
 2554: 
 2555: sub print_usersessions {
 2556:     my ($position,$dom,$settings,$rowtotal) = @_;
 2557:     my ($css_class,$datatable,%checked,%choices);
 2558:     my (%by_ip,%by_location,@intdoms);
 2559:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2560: 
 2561:     my @alldoms = &Apache::lonnet::all_domains();
 2562:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2563:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2564:     my %altids = &id_for_thisdom(%servers);
 2565:     my $itemcount = 1;
 2566:     if ($position eq 'top') {
 2567:         if (keys(%serverhomes) > 1) {
 2568:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2569:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2570:         } else {
 2571:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2572:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2573:         }
 2574:     } else {
 2575:         if (keys(%by_location) == 0) {
 2576:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2577:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2578:         } else {
 2579:             my %lt = &usersession_titles();
 2580:             my $numinrow = 5;
 2581:             my $prefix;
 2582:             my @types;
 2583:             if ($position eq 'bottom') {
 2584:                 $prefix = 'remote';
 2585:                 @types = ('version','excludedomain','includedomain');
 2586:             } else {
 2587:                 $prefix = 'hosted';
 2588:                 @types = ('excludedomain','includedomain');
 2589:             }
 2590:             my (%current,%checkedon,%checkedoff);
 2591:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2592:             my @locations = sort(keys(%by_location));
 2593:             foreach my $type (@types) {
 2594:                 $checkedon{$type} = '';
 2595:                 $checkedoff{$type} = ' checked="checked"';
 2596:             }
 2597:             if (ref($settings) eq 'HASH') {
 2598:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2599:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2600:                         $current{$key} = $settings->{$prefix}{$key};
 2601:                         if ($key eq 'version') {
 2602:                             if ($current{$key} ne '') {
 2603:                                 $checkedon{$key} = ' checked="checked"';
 2604:                                 $checkedoff{$key} = '';
 2605:                             }
 2606:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2607:                             $checkedon{$key} = ' checked="checked"';
 2608:                             $checkedoff{$key} = '';
 2609:                         }
 2610:                     }
 2611:                 }
 2612:             }
 2613:             foreach my $type (@types) {
 2614:                 next if ($type ne 'version' && !@locations);
 2615:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2616:                 $datatable .= '<tr'.$css_class.'>
 2617:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2618:                                <span class="LC_nobreak">&nbsp;
 2619:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2620:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2621:                 if ($type eq 'version') {
 2622:                     my $selector = '<select name="'.$prefix.'_version">';
 2623:                     foreach my $version (@lcversions) {
 2624:                         my $selected = '';
 2625:                         if ($current{'version'} eq $version) {
 2626:                             $selected = ' selected="selected"';
 2627:                         }
 2628:                         $selector .= ' <option value="'.$version.'"'.
 2629:                                      $selected.'>'.$version.'</option>';
 2630:                     }
 2631:                     $selector .= '</select> ';
 2632:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2633:                 } else {
 2634:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2635:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2636:                                  ' />'.('&nbsp;'x2).
 2637:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2638:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2639:                                  "\n".
 2640:                                  '</div><div><table>';
 2641:                     my $rem;
 2642:                     for (my $i=0; $i<@locations; $i++) {
 2643:                         my ($showloc,$value,$checkedtype);
 2644:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2645:                             my $ip = $by_location{$locations[$i]}->[0];
 2646:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2647:                                  $value = join(':',@{$by_ip{$ip}});
 2648:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2649:                                 if (ref($current{$type}) eq 'ARRAY') {
 2650:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2651:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2652:                                             $checkedtype = ' checked="checked"';
 2653:                                             last;
 2654:                                         }
 2655:                                     }
 2656:                                 }
 2657:                             }
 2658:                         }
 2659:                         $rem = $i%($numinrow);
 2660:                         if ($rem == 0) {
 2661:                             if ($i > 0) {
 2662:                                 $datatable .= '</tr>';
 2663:                             }
 2664:                             $datatable .= '<tr>';
 2665:                         }
 2666:                         $datatable .= '<td class="LC_left_item">'.
 2667:                                       '<span class="LC_nobreak"><label>'.
 2668:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2669:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2670:                                       '</label></span></td>';
 2671:                     }
 2672:                     $rem = @locations%($numinrow);
 2673:                     my $colsleft = $numinrow - $rem;
 2674:                     if ($colsleft > 1 ) {
 2675:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2676:                                       '&nbsp;</td>';
 2677:                     } elsif ($colsleft == 1) {
 2678:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2679:                     }
 2680:                     $datatable .= '</tr></table>';
 2681:                 }
 2682:                 $datatable .= '</td></tr>';
 2683:                 $itemcount ++;
 2684:             }
 2685:         }
 2686:     }
 2687:     $$rowtotal += $itemcount;
 2688:     return $datatable;
 2689: }
 2690: 
 2691: sub build_location_hashes {
 2692:     my ($intdoms,$by_ip,$by_location) = @_;
 2693:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2694:                   (ref($by_location) eq 'HASH')); 
 2695:     my %iphost = &Apache::lonnet::get_iphost();
 2696:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2697:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2698:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2699:         foreach my $id (@{$iphost{$primary_ip}}) {
 2700:             my $intdom = &Apache::lonnet::internet_dom($id);
 2701:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2702:                 push(@{$intdoms},$intdom);
 2703:             }
 2704:         }
 2705:     }
 2706:     foreach my $ip (keys(%iphost)) {
 2707:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2708:             foreach my $id (@{$iphost{$ip}}) {
 2709:                 my $location = &Apache::lonnet::internet_dom($id);
 2710:                 if ($location) {
 2711:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2712:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2713:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2714:                             push(@{$by_ip->{$ip}},$location);
 2715:                         }
 2716:                     } else {
 2717:                         $by_ip->{$ip} = [$location];
 2718:                     }
 2719:                 }
 2720:             }
 2721:         }
 2722:     }
 2723:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2724:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2725:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2726:             my $first = $by_ip->{$ip}->[0];
 2727:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2728:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2729:                     push(@{$by_location->{$first}},$ip);
 2730:                 }
 2731:             } else {
 2732:                 $by_location->{$first} = [$ip];
 2733:             }
 2734:         }
 2735:     }
 2736:     return;
 2737: }
 2738: 
 2739: sub current_offloads_to {
 2740:     my ($dom,$settings,$servers) = @_;
 2741:     my (%spareid,%otherdomconfigs);
 2742:     if (ref($servers) eq 'HASH') {
 2743:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2744:             my $gotspares;
 2745:             if (ref($settings) eq 'HASH') {
 2746:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2747:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2748:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2749:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2750:                         $gotspares = 1;
 2751:                     }
 2752:                 }
 2753:             }
 2754:             unless ($gotspares) {
 2755:                 my $gotspares;
 2756:                 my $serverhomeID =
 2757:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2758:                 my $serverhomedom =
 2759:                     &Apache::lonnet::host_domain($serverhomeID);
 2760:                 if ($serverhomedom ne $dom) {
 2761:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2762:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2763:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2764:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2765:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2766:                                 $gotspares = 1;
 2767:                             }
 2768:                         }
 2769:                     } else {
 2770:                         $otherdomconfigs{$serverhomedom} =
 2771:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2772:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2773:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2774:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2775:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2776:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2777:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2778:                                         $gotspares = 1;
 2779:                                     }
 2780:                                 }
 2781:                             }
 2782:                         }
 2783:                     }
 2784:                 }
 2785:             }
 2786:             unless ($gotspares) {
 2787:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2788:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2789:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2790:                } else {
 2791:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2792:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2793:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2794:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2795:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2796:                     } else {
 2797:                         my %what = (
 2798:                              spareid => 1,
 2799:                         );
 2800:                         my ($result,$returnhash) = 
 2801:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2802:                         if ($result eq 'ok') { 
 2803:                             if (ref($returnhash) eq 'HASH') {
 2804:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2805:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2806:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2807:                                 }
 2808:                             }
 2809:                         }
 2810:                     }
 2811:                 }
 2812:             }
 2813:         }
 2814:     }
 2815:     return %spareid;
 2816: }
 2817: 
 2818: sub spares_row {
 2819:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2820:     my $css_class;
 2821:     my $numinrow = 4;
 2822:     my $itemcount = 1;
 2823:     my $datatable;
 2824:     my %typetitles = &sparestype_titles();
 2825:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2826:         foreach my $server (sort(keys(%{$servers}))) {
 2827:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2828:             my ($othercontrol,$serverdom);
 2829:             if ($serverhome ne $server) {
 2830:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2831:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2832:             } else {
 2833:                 $serverdom = &Apache::lonnet::host_domain($server);
 2834:                 if ($serverdom ne $dom) {
 2835:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2836:                 }
 2837:             }
 2838:             next unless (ref($spareid->{$server}) eq 'HASH');
 2839:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2840:             $datatable .= '<tr'.$css_class.'>
 2841:                            <td rowspan="2">
 2842:                             <span class="LC_nobreak">'.
 2843:                           &mt('[_1] when busy, offloads to:'
 2844:                               ,'<b>'.$server.'</b>').
 2845:                           "\n";
 2846:             my (%current,%canselect);
 2847:             my @choices = 
 2848:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2849:             foreach my $type ('primary','default') {
 2850:                 if (ref($spareid->{$server}) eq 'HASH') {
 2851:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2852:                         my @spares = @{$spareid->{$server}{$type}};
 2853:                         if (@spares > 0) {
 2854:                             if ($othercontrol) {
 2855:                                 $current{$type} = join(', ',@spares);
 2856:                             } else {
 2857:                                 $current{$type} .= '<table>';
 2858:                                 my $numspares = scalar(@spares);
 2859:                                 for (my $i=0;  $i<@spares; $i++) {
 2860:                                     my $rem = $i%($numinrow);
 2861:                                     if ($rem == 0) {
 2862:                                         if ($i > 0) {
 2863:                                             $current{$type} .= '</tr>';
 2864:                                         }
 2865:                                         $current{$type} .= '<tr>';
 2866:                                     }
 2867:                                     $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;'.
 2868:                                                        $spareid->{$server}{$type}[$i].
 2869:                                                        '</label></td>'."\n";
 2870:                                 }
 2871:                                 my $rem = @spares%($numinrow);
 2872:                                 my $colsleft = $numinrow - $rem;
 2873:                                 if ($colsleft > 1 ) {
 2874:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2875:                                                        '" class="LC_left_item">'.
 2876:                                                        '&nbsp;</td>';
 2877:                                 } elsif ($colsleft == 1) {
 2878:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2879:                                 }
 2880:                                 $current{$type} .= '</tr></table>';
 2881:                             }
 2882:                         }
 2883:                     }
 2884:                     if ($current{$type} eq '') {
 2885:                         $current{$type} = &mt('None specified');
 2886:                     }
 2887:                     if ($othercontrol) {
 2888:                         if ($type eq 'primary') {
 2889:                             $canselect{$type} = $othercontrol;
 2890:                         }
 2891:                     } else {
 2892:                         $canselect{$type} = 
 2893:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2894:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2895:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2896:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2897:                         if (@choices > 0) {
 2898:                             foreach my $lonhost (@choices) {
 2899:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2900:                             }
 2901:                         }
 2902:                         $canselect{$type} .= '</select>'."\n";
 2903:                     }
 2904:                 } else {
 2905:                     $current{$type} = &mt('Could not be determined');
 2906:                     if ($type eq 'primary') {
 2907:                         $canselect{$type} =  $othercontrol;
 2908:                     }
 2909:                 }
 2910:                 if ($type eq 'default') {
 2911:                     $datatable .= '<tr'.$css_class.'>';
 2912:                 }
 2913:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2914:                               '<td>'.$current{$type}.'</td>'."\n".
 2915:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2916:             }
 2917:             $itemcount ++;
 2918:         }
 2919:     }
 2920:     $$rowtotal += $itemcount;
 2921:     return $datatable;
 2922: }
 2923: 
 2924: sub possible_newspares {
 2925:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2926:     my $serverhostname = &Apache::lonnet::hostname($server);
 2927:     my %excluded;
 2928:     if ($serverhostname ne '') {
 2929:         %excluded = (
 2930:                        $serverhostname => 1,
 2931:                     );
 2932:     }
 2933:     if (ref($currspares) eq 'HASH') {
 2934:         foreach my $type (keys(%{$currspares})) {
 2935:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2936:                 if (@{$currspares->{$type}} > 0) {
 2937:                     foreach my $curr (@{$currspares->{$type}}) {
 2938:                         my $hostname = &Apache::lonnet::hostname($curr);
 2939:                         $excluded{$hostname} = 1;
 2940:                     }
 2941:                 }
 2942:             }
 2943:         }
 2944:     }
 2945:     my @choices;
 2946:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2947:         if (keys(%{$serverhomes}) > 1) {
 2948:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2949:                 unless ($excluded{$name}) {
 2950:                     if (exists($altids->{$serverhomes->{$name}})) {
 2951:                         push(@choices,$altids->{$serverhomes->{$name}});
 2952:                     } else {
 2953:                         push(@choices,$serverhomes->{$name});
 2954:                     }
 2955:                 }
 2956:             }
 2957:         }
 2958:     }
 2959:     return sort(@choices);
 2960: }
 2961: 
 2962: sub print_loadbalancing {
 2963:     my ($dom,$settings,$rowtotal) = @_;
 2964:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2965:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2966:     my $numinrow = 1;
 2967:     my $datatable;
 2968:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2969:     my (%currbalancer,%currtargets,%currrules,%existing);
 2970:     if (ref($settings) eq 'HASH') {
 2971:         %existing = %{$settings};
 2972:     }
 2973:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 2974:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 2975:                                   \%currtargets,\%currrules);
 2976:     } else {
 2977:         return;
 2978:     }
 2979:     my ($othertitle,$usertypes,$types) =
 2980:         &Apache::loncommon::sorted_inst_types($dom);
 2981:     my $rownum = 8;
 2982:     if (ref($types) eq 'ARRAY') {
 2983:         $rownum += scalar(@{$types});
 2984:     }
 2985:     my @css_class = ('LC_odd_row','LC_even_row');
 2986:     my $balnum = 0;
 2987:     my $islast;
 2988:     my (@toshow,$disabledtext);
 2989:     if (keys(%currbalancer) > 0) {
 2990:         @toshow = sort(keys(%currbalancer));
 2991:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 2992:             push(@toshow,'');
 2993:         }
 2994:     } else {
 2995:         @toshow = ('');
 2996:         $disabledtext = &mt('No existing load balancer');
 2997:     }
 2998:     foreach my $lonhost (@toshow) {
 2999:         if ($balnum == scalar(@toshow)-1) {
 3000:             $islast = 1;
 3001:         } else {
 3002:             $islast = 0;
 3003:         }
 3004:         my $cssidx = $balnum%2;
 3005:         my $targets_div_style = 'display: none';
 3006:         my $disabled_div_style = 'display: block';
 3007:         my $homedom_div_style = 'display: none';
 3008:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3009:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3010:                       '<p>';
 3011:         if ($lonhost eq '') {
 3012:             $datatable .= '<span class="LC_nobreak">';
 3013:             if (keys(%currbalancer) > 0) {
 3014:                 $datatable .= &mt('Add balancer:');
 3015:             } else {
 3016:                 $datatable .= &mt('Enable balancer:');
 3017:             }
 3018:             $datatable .= '&nbsp;'.
 3019:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3020:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3021:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3022:                           '<option value="" selected="selected">'.&mt('None').
 3023:                           '</option>'."\n";
 3024:             foreach my $server (sort(keys(%servers))) {
 3025:                 next if ($currbalancer{$server});
 3026:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3027:             }
 3028:             $datatable .=
 3029:                 '</select>'."\n".
 3030:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3031:         } else {
 3032:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3033:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3034:                            &mt('Stop balancing').'</label>'.
 3035:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3036:             $targets_div_style = 'display: block';
 3037:             $disabled_div_style = 'display: none';
 3038:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3039:                 $homedom_div_style = 'display: block';
 3040:             }
 3041:         }
 3042:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3043:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3044:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3045:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3046:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3047:         my @sparestypes = ('primary','default');
 3048:         my %typetitles = &sparestype_titles();
 3049:         foreach my $sparetype (@sparestypes) {
 3050:             my $targettable;
 3051:             for (my $i=0; $i<$numspares; $i++) {
 3052:                 my $checked;
 3053:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3054:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3055:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3056:                             $checked = ' checked="checked"';
 3057:                         }
 3058:                     }
 3059:                 }
 3060:                 my ($chkboxval,$disabled);
 3061:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3062:                     $chkboxval = $spares[$i];
 3063:                 }
 3064:                 if (exists($currbalancer{$spares[$i]})) {
 3065:                     $disabled = ' disabled="disabled"';
 3066:                 }
 3067:                 $targettable .=
 3068:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3069:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3070:                     '</span></label></td>';
 3071:                 my $rem = $i%($numinrow);
 3072:                 if ($rem == 0) {
 3073:                     if (($i > 0) && ($i < $numspares-1)) {
 3074:                         $targettable .= '</tr>';
 3075:                     }
 3076:                     if ($i < $numspares-1) {
 3077:                         $targettable .= '<tr>';
 3078:                     }
 3079:                 }
 3080:             }
 3081:             if ($targettable ne '') {
 3082:                 my $rem = $numspares%($numinrow);
 3083:                 my $colsleft = $numinrow - $rem;
 3084:                 if ($colsleft > 1 ) {
 3085:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3086:                                     '&nbsp;</td>';
 3087:                 } elsif ($colsleft == 1) {
 3088:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3089:                 }
 3090:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3091:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3092:             }
 3093:         }
 3094:         $datatable .= '</div></td></tr>'.
 3095:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3096:                                            $othertitle,$usertypes,$types,\%servers,
 3097:                                            \%currbalancer,$lonhost,
 3098:                                            $targets_div_style,$homedom_div_style,
 3099:                                            $css_class[$cssidx],$balnum,$islast);
 3100:         $$rowtotal += $rownum;
 3101:         $balnum ++;
 3102:     }
 3103:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3104:     return $datatable;
 3105: }
 3106: 
 3107: sub get_loadbalancers_config {
 3108:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3109:     return unless ((ref($servers) eq 'HASH') &&
 3110:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3111:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3112:     if (keys(%{$existing}) > 0) {
 3113:         my $oldlonhost;
 3114:         foreach my $key (sort(keys(%{$existing}))) {
 3115:             if ($key eq 'lonhost') {
 3116:                 $oldlonhost = $existing->{'lonhost'};
 3117:                 $currbalancer->{$oldlonhost} = 1;
 3118:             } elsif ($key eq 'targets') {
 3119:                 if ($oldlonhost) {
 3120:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3121:                 }
 3122:             } elsif ($key eq 'rules') {
 3123:                 if ($oldlonhost) {
 3124:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3125:                 }
 3126:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3127:                 $currbalancer->{$key} = 1;
 3128:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3129:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3130:             }
 3131:         }
 3132:     } else {
 3133:         my ($balancerref,$targetsref) =
 3134:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3135:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3136:             foreach my $server (sort(keys(%{$balancerref}))) {
 3137:                 $currbalancer->{$server} = 1;
 3138:                 $currtargets->{$server} = $targetsref->{$server};
 3139:             }
 3140:         }
 3141:     }
 3142:     return;
 3143: }
 3144: 
 3145: sub loadbalancing_rules {
 3146:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3147:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3148:         $css_class,$balnum,$islast) = @_;
 3149:     my $output;
 3150:     my $num = 0;
 3151:     my ($alltypes,$othertypes,$titles) =
 3152:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3153:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3154:         foreach my $type (@{$alltypes}) {
 3155:             $num ++;
 3156:             my $current;
 3157:             if (ref($currrules) eq 'HASH') {
 3158:                 $current = $currrules->{$type};
 3159:             }
 3160:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3161:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3162:                     $current = '';
 3163:                 }
 3164:             }
 3165:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3166:                                              $servers,$currbalancer,$lonhost,$dom,
 3167:                                              $targets_div_style,$homedom_div_style,
 3168:                                              $css_class,$balnum,$num,$islast);
 3169:         }
 3170:     }
 3171:     return $output;
 3172: }
 3173: 
 3174: sub loadbalancing_titles {
 3175:     my ($dom,$intdom,$usertypes,$types) = @_;
 3176:     my %othertypes = (
 3177:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3178:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3179:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3180:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3181:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3182:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3183:                      );
 3184:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3185:     if (ref($types) eq 'ARRAY') {
 3186:         unshift(@alltypes,@{$types},'default');
 3187:     }
 3188:     my %titles;
 3189:     foreach my $type (@alltypes) {
 3190:         if ($type =~ /^_LC_/) {
 3191:             $titles{$type} = $othertypes{$type};
 3192:         } elsif ($type eq 'default') {
 3193:             $titles{$type} = &mt('All users from [_1]',$dom);
 3194:             if (ref($types) eq 'ARRAY') {
 3195:                 if (@{$types} > 0) {
 3196:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3197:                 }
 3198:             }
 3199:         } elsif (ref($usertypes) eq 'HASH') {
 3200:             $titles{$type} = $usertypes->{$type};
 3201:         }
 3202:     }
 3203:     return (\@alltypes,\%othertypes,\%titles);
 3204: }
 3205: 
 3206: sub loadbalance_rule_row {
 3207:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3208:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3209:     my @rulenames;
 3210:     my %ruletitles = &offloadtype_text();
 3211:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3212:         @rulenames = ('balancer','offloadedto');
 3213:     } else {
 3214:         @rulenames = ('default','homeserver');
 3215:         if ($type eq '_LC_external') {
 3216:             push(@rulenames,'externalbalancer');
 3217:         } else {
 3218:             push(@rulenames,'specific');
 3219:         }
 3220:         push(@rulenames,'none');
 3221:     }
 3222:     my $style = $targets_div_style;
 3223:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3224:         $style = $homedom_div_style;
 3225:     }
 3226:     my $space;
 3227:     if ($islast && $num == 1) {
 3228:         $space = '<div display="inline-block">&nbsp;</div>';
 3229:     }
 3230:     my $output =
 3231:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3232:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3233:         '<td valaign="top">'.$space.
 3234:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3235:     for (my $i=0; $i<@rulenames; $i++) {
 3236:         my $rule = $rulenames[$i];
 3237:         my ($checked,$extra);
 3238:         if ($rulenames[$i] eq 'default') {
 3239:             $rule = '';
 3240:         }
 3241:         if ($rulenames[$i] eq 'specific') {
 3242:             if (ref($servers) eq 'HASH') {
 3243:                 my $default;
 3244:                 if (($current ne '') && (exists($servers->{$current}))) {
 3245:                     $checked = ' checked="checked"';
 3246:                 }
 3247:                 unless ($checked) {
 3248:                     $default = ' selected="selected"';
 3249:                 }
 3250:                 $extra =
 3251:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3252:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3253:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3254:                     '<option value=""'.$default.'></option>'."\n";
 3255:                 foreach my $server (sort(keys(%{$servers}))) {
 3256:                     if (ref($currbalancer) eq 'HASH') {
 3257:                         next if (exists($currbalancer->{$server}));
 3258:                     }
 3259:                     my $selected;
 3260:                     if ($server eq $current) {
 3261:                         $selected = ' selected="selected"';
 3262:                     }
 3263:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3264:                 }
 3265:                 $extra .= '</select>';
 3266:             }
 3267:         } elsif ($rule eq $current) {
 3268:             $checked = ' checked="checked"';
 3269:         }
 3270:         $output .= '<span class="LC_nobreak"><label>'.
 3271:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3272:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3273:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3274:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3275:                    '</label>'.$extra.'</span><br />'."\n";
 3276:     }
 3277:     $output .= '</div></td></tr>'."\n";
 3278:     return $output;
 3279: }
 3280: 
 3281: sub offloadtype_text {
 3282:     my %ruletitles = &Apache::lonlocal::texthash (
 3283:            'default'          => 'Offloads to default destinations',
 3284:            'homeserver'       => "Offloads to user's home server",
 3285:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3286:            'specific'         => 'Offloads to specific server',
 3287:            'none'             => 'No offload',
 3288:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3289:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3290:     );
 3291:     return %ruletitles;
 3292: }
 3293: 
 3294: sub sparestype_titles {
 3295:     my %typestitles = &Apache::lonlocal::texthash (
 3296:                           'primary' => 'primary',
 3297:                           'default' => 'default',
 3298:                       );
 3299:     return %typestitles;
 3300: }
 3301: 
 3302: sub contact_titles {
 3303:     my %titles = &Apache::lonlocal::texthash (
 3304:                    'supportemail' => 'Support E-mail address',
 3305:                    'adminemail'   => 'Default Server Admin E-mail address',
 3306:                    'errormail'    => 'Error reports to be e-mailed to',
 3307:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3308:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3309:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3310:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3311:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3312:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3313:                  );
 3314:     my %short_titles = &Apache::lonlocal::texthash (
 3315:                            adminemail   => 'Admin E-mail address',
 3316:                            supportemail => 'Support E-mail',
 3317:                        );   
 3318:     return (\%titles,\%short_titles);
 3319: }
 3320: 
 3321: sub tool_titles {
 3322:     my %titles = &Apache::lonlocal::texthash (
 3323:                      aboutme    => 'Personal web page',
 3324:                      blog       => 'Blog',
 3325:                      webdav     => 'WebDAV',
 3326:                      portfolio  => 'Portfolio',
 3327:                      official   => 'Official courses (with institutional codes)',
 3328:                      unofficial => 'Unofficial courses',
 3329:                      community  => 'Communities',
 3330:                  );
 3331:     return %titles;
 3332: }
 3333: 
 3334: sub courserequest_titles {
 3335:     my %titles = &Apache::lonlocal::texthash (
 3336:                                    official   => 'Official',
 3337:                                    unofficial => 'Unofficial',
 3338:                                    community  => 'Communities',
 3339:                                    norequest  => 'Not allowed',
 3340:                                    approval   => 'Approval by Dom. Coord.',
 3341:                                    validate   => 'With validation',
 3342:                                    autolimit  => 'Numerical limit',
 3343:                                    unlimited  => '(blank for unlimited)',
 3344:                  );
 3345:     return %titles;
 3346: }
 3347: 
 3348: sub authorrequest_titles {
 3349:     my %titles = &Apache::lonlocal::texthash (
 3350:                                    norequest  => 'Not allowed',
 3351:                                    approval   => 'Approval by Dom. Coord.',
 3352:                                    automatic  => 'Automatic approval',
 3353:                  );
 3354:     return %titles;
 3355: }
 3356: 
 3357: sub courserequest_conditions {
 3358:     my %conditions = &Apache::lonlocal::texthash (
 3359:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3360:        validate   => '(Processing of request subject to institutional validation).',
 3361:                  );
 3362:     return %conditions;
 3363: }
 3364: 
 3365: 
 3366: sub print_usercreation {
 3367:     my ($position,$dom,$settings,$rowtotal) = @_;
 3368:     my $numinrow = 4;
 3369:     my $datatable;
 3370:     if ($position eq 'top') {
 3371:         $$rowtotal ++;
 3372:         my $rowcount = 0;
 3373:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3374:         if (ref($rules) eq 'HASH') {
 3375:             if (keys(%{$rules}) > 0) {
 3376:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3377:                                                 $ruleorder,$numinrow,$rowcount);
 3378:                 $$rowtotal ++;
 3379:                 $rowcount ++;
 3380:             }
 3381:         }
 3382:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3383:         if (ref($idrules) eq 'HASH') {
 3384:             if (keys(%{$idrules}) > 0) {
 3385:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3386:                                                 $idruleorder,$numinrow,$rowcount);
 3387:                 $$rowtotal ++;
 3388:                 $rowcount ++;
 3389:             }
 3390:         }
 3391:         my ($emailrules,$emailruleorder) = 
 3392:             &Apache::lonnet::inst_userrules($dom,'email');
 3393:         if (ref($emailrules) eq 'HASH') {
 3394:             if (keys(%{$emailrules}) > 0) {
 3395:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3396:                                                 $emailruleorder,$numinrow,$rowcount);
 3397:                 $$rowtotal ++;
 3398:                 $rowcount ++;
 3399:             }
 3400:         }
 3401:         if ($rowcount == 0) {
 3402:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3403:             $$rowtotal ++;
 3404:             $rowcount ++;
 3405:         }
 3406:     } elsif ($position eq 'middle') {
 3407:         my @creators = ('author','course','requestcrs','selfcreate');
 3408:         my ($rules,$ruleorder) =
 3409:             &Apache::lonnet::inst_userrules($dom,'username');
 3410:         my %lt = &usercreation_types();
 3411:         my %checked;
 3412:         my @selfcreate; 
 3413:         if (ref($settings) eq 'HASH') {
 3414:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3415:                 foreach my $item (@creators) {
 3416:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3417:                 }
 3418:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3419:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3420:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3421:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3422:                         @selfcreate = ('email','login','sso');
 3423:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3424:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3425:                     }
 3426:                 }
 3427:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3428:                 foreach my $item (@creators) {
 3429:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3430:                         $checked{$item} = 'none';
 3431:                     }
 3432:                 }
 3433:             }
 3434:         }
 3435:         my $rownum = 0;
 3436:         foreach my $item (@creators) {
 3437:             $rownum ++;
 3438:             if ($item ne 'selfcreate') {  
 3439:                 if ($checked{$item} eq '') {
 3440:                     $checked{$item} = 'any';
 3441:                 }
 3442:             }
 3443:             my $css_class;
 3444:             if ($rownum%2) {
 3445:                 $css_class = '';
 3446:             } else {
 3447:                 $css_class = ' class="LC_odd_row" ';
 3448:             }
 3449:             $datatable .= '<tr'.$css_class.'>'.
 3450:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3451:                          '</span></td><td align="right">';
 3452:             my @options;
 3453:             if ($item eq 'selfcreate') {
 3454:                 push(@options,('email','login','sso'));
 3455:             } else {
 3456:                 @options = ('any');
 3457:                 if (ref($rules) eq 'HASH') {
 3458:                     if (keys(%{$rules}) > 0) {
 3459:                         push(@options,('official','unofficial'));
 3460:                     }
 3461:                 }
 3462:                 push(@options,'none');
 3463:             }
 3464:             foreach my $option (@options) {
 3465:                 my $type = 'radio';
 3466:                 my $check = ' ';
 3467:                 if ($item eq 'selfcreate') {
 3468:                     $type = 'checkbox';
 3469:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3470:                         $check = ' checked="checked" ';
 3471:                     }
 3472:                 } else {
 3473:                     if ($checked{$item} eq $option) {
 3474:                         $check = ' checked="checked" ';
 3475:                     }
 3476:                 } 
 3477:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3478:                               '<input type="'.$type.'" name="can_createuser_'.
 3479:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3480:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3481:             }
 3482:             $datatable .= '</td></tr>';
 3483:         }
 3484:         my ($othertitle,$usertypes,$types) =
 3485:             &Apache::loncommon::sorted_inst_types($dom);
 3486:         my $createsettings;
 3487:         if (ref($settings) eq 'HASH') {
 3488:             $createsettings = $settings->{cancreate};
 3489:         }
 3490:         if (ref($usertypes) eq 'HASH') {
 3491:             if (keys(%{$usertypes}) > 0) {
 3492:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3493:                                              $dom,$numinrow,$othertitle,
 3494:                                              'statustocreate');
 3495:                 $$rowtotal ++;
 3496:                 $rownum ++;
 3497:             }
 3498:         }
 3499:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3500:     } else {
 3501:         my @contexts = ('author','course','domain');
 3502:         my @authtypes = ('int','krb4','krb5','loc');
 3503:         my %checked;
 3504:         if (ref($settings) eq 'HASH') {
 3505:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3506:                 foreach my $item (@contexts) {
 3507:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3508:                         foreach my $auth (@authtypes) {
 3509:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3510:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3511:                             }
 3512:                         }
 3513:                     }
 3514:                 }
 3515:             }
 3516:         } else {
 3517:             foreach my $item (@contexts) {
 3518:                 foreach my $auth (@authtypes) {
 3519:                     $checked{$item}{$auth} = ' checked="checked" ';
 3520:                 }
 3521:             }
 3522:         }
 3523:         my %title = &context_names();
 3524:         my %authname = &authtype_names();
 3525:         my $rownum = 0;
 3526:         my $css_class; 
 3527:         foreach my $item (@contexts) {
 3528:             if ($rownum%2) {
 3529:                 $css_class = '';
 3530:             } else {
 3531:                 $css_class = ' class="LC_odd_row" ';
 3532:             }
 3533:             $datatable .=   '<tr'.$css_class.'>'.
 3534:                             '<td>'.$title{$item}.
 3535:                             '</td><td class="LC_left_item">'.
 3536:                             '<span class="LC_nobreak">';
 3537:             foreach my $auth (@authtypes) {
 3538:                 $datatable .= '<label>'. 
 3539:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3540:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3541:                               $authname{$auth}.'</label>&nbsp;';
 3542:             }
 3543:             $datatable .= '</span></td></tr>';
 3544:             $rownum ++;
 3545:         }
 3546:         $$rowtotal += $rownum;
 3547:     }
 3548:     return $datatable;
 3549: }
 3550: 
 3551: sub captcha_choice {
 3552:     my ($context,$settings,$itemcount) = @_;
 3553:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3554:     my %lt = &captcha_phrases();
 3555:     $keyentry = 'hidden';
 3556:     if ($context eq 'cancreate') {
 3557:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3558:     } elsif ($context eq 'login') {
 3559:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3560:     }
 3561:     if (ref($settings) eq 'HASH') {
 3562:         if ($settings->{'captcha'}) {
 3563:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3564:         } else {
 3565:             $checked{'original'} = ' checked="checked"';
 3566:         }
 3567:         if ($settings->{'captcha'} eq 'recaptcha') {
 3568:             $pubtext = $lt{'pub'};
 3569:             $privtext = $lt{'priv'};
 3570:             $keyentry = 'text';
 3571:         }
 3572:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3573:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3574:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3575:         }
 3576:     } else {
 3577:         $checked{'original'} = ' checked="checked"';
 3578:     }
 3579:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3580:     my $output = '<tr'.$css_class.'>'.
 3581:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3582:                  '<table><tr><td>'."\n";
 3583:     foreach my $option ('original','recaptcha','notused') {
 3584:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3585:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3586:                    $lt{$option}.'</label></span>';
 3587:         unless ($option eq 'notused') {
 3588:             $output .= ('&nbsp;'x2)."\n";
 3589:         }
 3590:     }
 3591: #
 3592: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3593: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3594: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 3595: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3596: #
 3597:     $output .= '</td></tr>'."\n".
 3598:                '<tr><td>'."\n".
 3599:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3600:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3601:                $currpub.'" size="40" /></span><br />'."\n".
 3602:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3603:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3604:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3605:                '</td></tr>';
 3606:     return $output;
 3607: }
 3608: 
 3609: sub user_formats_row {
 3610:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3611:     my $output;
 3612:     my %text = (
 3613:                    'username' => 'new usernames',
 3614:                    'id'       => 'IDs',
 3615:                    'email'    => 'self-created accounts (e-mail)',
 3616:                );
 3617:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3618:     $output = '<tr '.$css_class.'>'.
 3619:               '<td><span class="LC_nobreak">';
 3620:     if ($type eq 'email') {
 3621:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3622:     } else {
 3623:         $output .= &mt("Format rules to check for $text{$type}: ");
 3624:     }
 3625:     $output .= '</span></td>'.
 3626:                '<td class="LC_left_item" colspan="2"><table>';
 3627:     my $rem;
 3628:     if (ref($ruleorder) eq 'ARRAY') {
 3629:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3630:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3631:                 my $rem = $i%($numinrow);
 3632:                 if ($rem == 0) {
 3633:                     if ($i > 0) {
 3634:                         $output .= '</tr>';
 3635:                     }
 3636:                     $output .= '<tr>';
 3637:                 }
 3638:                 my $check = ' ';
 3639:                 if (ref($settings) eq 'HASH') {
 3640:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3641:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3642:                             $check = ' checked="checked" ';
 3643:                         }
 3644:                     }
 3645:                 }
 3646:                 $output .= '<td class="LC_left_item">'.
 3647:                            '<span class="LC_nobreak"><label>'.
 3648:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3649:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3650:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3651:             }
 3652:         }
 3653:         $rem = @{$ruleorder}%($numinrow);
 3654:     }
 3655:     my $colsleft = $numinrow - $rem;
 3656:     if ($colsleft > 1 ) {
 3657:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3658:                    '&nbsp;</td>';
 3659:     } elsif ($colsleft == 1) {
 3660:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3661:     }
 3662:     $output .= '</tr></table></td></tr>';
 3663:     return $output;
 3664: }
 3665: 
 3666: sub usercreation_types {
 3667:     my %lt = &Apache::lonlocal::texthash (
 3668:                     author     => 'When adding a co-author',
 3669:                     course     => 'When adding a user to a course',
 3670:                     requestcrs => 'When requesting a course',
 3671:                     selfcreate => 'User creates own account', 
 3672:                     any        => 'Any',
 3673:                     official   => 'Institutional only ',
 3674:                     unofficial => 'Non-institutional only',
 3675:                     email      => 'E-mail address',
 3676:                     login      => 'Institutional Login',
 3677:                     sso        => 'SSO', 
 3678:                     none       => 'None',
 3679:     );
 3680:     return %lt;
 3681: }
 3682: 
 3683: sub authtype_names {
 3684:     my %lt = &Apache::lonlocal::texthash(
 3685:                       int    => 'Internal',
 3686:                       krb4   => 'Kerberos 4',
 3687:                       krb5   => 'Kerberos 5',
 3688:                       loc    => 'Local',
 3689:                   );
 3690:     return %lt;
 3691: }
 3692: 
 3693: sub context_names {
 3694:     my %context_title = &Apache::lonlocal::texthash(
 3695:        author => 'Creating users when an Author',
 3696:        course => 'Creating users when in a course',
 3697:        domain => 'Creating users when a Domain Coordinator',
 3698:     );
 3699:     return %context_title;
 3700: }
 3701: 
 3702: sub print_usermodification {
 3703:     my ($position,$dom,$settings,$rowtotal) = @_;
 3704:     my $numinrow = 4;
 3705:     my ($context,$datatable,$rowcount);
 3706:     if ($position eq 'top') {
 3707:         $rowcount = 0;
 3708:         $context = 'author'; 
 3709:         foreach my $role ('ca','aa') {
 3710:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3711:                                                    $numinrow,$rowcount);
 3712:             $$rowtotal ++;
 3713:             $rowcount ++;
 3714:         }
 3715:     } elsif ($position eq 'middle') {
 3716:         $context = 'course';
 3717:         $rowcount = 0;
 3718:         foreach my $role ('st','ep','ta','in','cr') {
 3719:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3720:                                                    $numinrow,$rowcount);
 3721:             $$rowtotal ++;
 3722:             $rowcount ++;
 3723:         }
 3724:     } elsif ($position eq 'bottom') {
 3725:         $context = 'selfcreate';
 3726:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3727:         $usertypes->{'default'} = $othertitle;
 3728:         if (ref($types) eq 'ARRAY') {
 3729:             push(@{$types},'default');
 3730:             $usertypes->{'default'} = $othertitle;
 3731:             foreach my $status (@{$types}) {
 3732:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3733:                                                        $numinrow,$rowcount,$usertypes);
 3734:                 $$rowtotal ++;
 3735:                 $rowcount ++;
 3736:             }
 3737:         }
 3738:     }
 3739:     return $datatable;
 3740: }
 3741: 
 3742: sub print_defaults {
 3743:     my ($dom,$settings,$rowtotal) = @_;
 3744:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3745:                  'datelocale_def','portal_def');
 3746:     my %defaults;
 3747:     if (ref($settings) eq 'HASH') {
 3748:         %defaults = %{$settings};
 3749:     } else {
 3750:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 3751:         foreach my $item (@items) {
 3752:             $defaults{$item} = $domdefaults{$item};
 3753:         }
 3754:     }
 3755:     my $titles = &defaults_titles($dom);
 3756:     my $rownum = 0;
 3757:     my ($datatable,$css_class);
 3758:     foreach my $item (@items) {
 3759:         if ($rownum%2) {
 3760:             $css_class = '';
 3761:         } else {
 3762:             $css_class = ' class="LC_odd_row" ';
 3763:         }
 3764:         $datatable .= '<tr'.$css_class.'>'.
 3765:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3766:                   '</span></td><td class="LC_right_item">';
 3767:         if ($item eq 'auth_def') {
 3768:             my @authtypes = ('internal','krb4','krb5','localauth');
 3769:             my %shortauth = (
 3770:                              internal => 'int',
 3771:                              krb4 => 'krb4',
 3772:                              krb5 => 'krb5',
 3773:                              localauth  => 'loc'
 3774:                            );
 3775:             my %authnames = &authtype_names();
 3776:             foreach my $auth (@authtypes) {
 3777:                 my $checked = ' ';
 3778:                 if ($defaults{$item} eq $auth) {
 3779:                     $checked = ' checked="checked" ';
 3780:                 }
 3781:                 $datatable .= '<label><input type="radio" name="'.$item.
 3782:                               '" value="'.$auth.'"'.$checked.'/>'.
 3783:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3784:             }
 3785:         } elsif ($item eq 'timezone_def') {
 3786:             my $includeempty = 1;
 3787:             $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 3788:         } elsif ($item eq 'datelocale_def') {
 3789:             my $includeempty = 1;
 3790:             $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 3791:         } elsif ($item eq 'lang_def') {
 3792:             my %langchoices = &get_languages_hash();
 3793:             $langchoices{''} = 'No language preference';
 3794:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 3795:             $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 3796:                                                           \%langchoices);
 3797:         } else {
 3798:             my $size;
 3799:             if ($item eq 'portal_def') {
 3800:                 $size = ' size="25"';
 3801:             }
 3802:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3803:                           $defaults{$item}.'"'.$size.' />';
 3804:         }
 3805:         $datatable .= '</td></tr>';
 3806:         $rownum ++;
 3807:     }
 3808:     $$rowtotal += $rownum;
 3809:     return $datatable;
 3810: }
 3811: 
 3812: sub get_languages_hash {
 3813:     my %langchoices;
 3814:     foreach my $id (&Apache::loncommon::languageids()) {
 3815:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 3816:         if ($code ne '') {
 3817:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 3818:         }
 3819:     }
 3820:     return %langchoices;
 3821: }
 3822: 
 3823: sub defaults_titles {
 3824:     my ($dom) = @_;
 3825:     my %titles = &Apache::lonlocal::texthash (
 3826:                    'auth_def'      => 'Default authentication type',
 3827:                    'auth_arg_def'  => 'Default authentication argument',
 3828:                    'lang_def'      => 'Default language',
 3829:                    'timezone_def'  => 'Default timezone',
 3830:                    'datelocale_def' => 'Default locale for dates',
 3831:                    'portal_def'     => 'Portal/Default URL',
 3832:                  );
 3833:     if ($dom) {
 3834:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3835:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3836:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3837:         $protocol = 'http' if ($protocol ne 'https');
 3838:         if ($uint_dom) {
 3839:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3840:                                          $uint_dom);
 3841:         }
 3842:     }
 3843:     return (\%titles);
 3844: }
 3845: 
 3846: sub print_scantronformat {
 3847:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3848:     my $itemcount = 1;
 3849:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3850:         %confhash);
 3851:     my $switchserver = &check_switchserver($dom,$confname);
 3852:     my %lt = &Apache::lonlocal::texthash (
 3853:                 default => 'Default bubblesheet format file error',
 3854:                 custom  => 'Custom bubblesheet format file error',
 3855:              );
 3856:     my %scantronfiles = (
 3857:         default => 'default.tab',
 3858:         custom => 'custom.tab',
 3859:     );
 3860:     foreach my $key (keys(%scantronfiles)) {
 3861:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3862:                               .$scantronfiles{$key};
 3863:     }
 3864:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3865:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3866:         if (!$switchserver) {
 3867:             my $servadm = $r->dir_config('lonAdmEMail');
 3868:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3869:             if ($configuserok eq 'ok') {
 3870:                 if ($author_ok eq 'ok') {
 3871:                     my %legacyfile = (
 3872:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3873:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3874:                     );
 3875:                     my %md5chk;
 3876:                     foreach my $type (keys(%legacyfile)) {
 3877:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3878:                         chomp($md5chk{$type});
 3879:                     }
 3880:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3881:                         foreach my $type (keys(%legacyfile)) {
 3882:                             ($scantronurls{$type},my $error) = 
 3883:                                 &legacy_scantronformat($r,$dom,$confname,
 3884:                                                  $type,$legacyfile{$type},
 3885:                                                  $scantronurls{$type},
 3886:                                                  $scantronfiles{$type});
 3887:                             if ($error ne '') {
 3888:                                 $error{$type} = $error;
 3889:                             }
 3890:                         }
 3891:                         if (keys(%error) == 0) {
 3892:                             $is_custom = 1;
 3893:                             $confhash{'scantron'}{'scantronformat'} = 
 3894:                                 $scantronurls{'custom'};
 3895:                             my $putresult = 
 3896:                                 &Apache::lonnet::put_dom('configuration',
 3897:                                                          \%confhash,$dom);
 3898:                             if ($putresult ne 'ok') {
 3899:                                 $error{'custom'} = 
 3900:                                     '<span class="LC_error">'.
 3901:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3902:                             }
 3903:                         }
 3904:                     } else {
 3905:                         ($scantronurls{'default'},my $error) =
 3906:                             &legacy_scantronformat($r,$dom,$confname,
 3907:                                           'default',$legacyfile{'default'},
 3908:                                           $scantronurls{'default'},
 3909:                                           $scantronfiles{'default'});
 3910:                         if ($error eq '') {
 3911:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3912:                             my $putresult =
 3913:                                 &Apache::lonnet::put_dom('configuration',
 3914:                                                          \%confhash,$dom);
 3915:                             if ($putresult ne 'ok') {
 3916:                                 $error{'default'} =
 3917:                                     '<span class="LC_error">'.
 3918:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3919:                             }
 3920:                         } else {
 3921:                             $error{'default'} = $error;
 3922:                         }
 3923:                     }
 3924:                 }
 3925:             }
 3926:         } else {
 3927:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3928:         }
 3929:     }
 3930:     if (ref($settings) eq 'HASH') {
 3931:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3932:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3933:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3934:                 $scantronurl = '';
 3935:             } else {
 3936:                 $scantronurl = $settings->{'scantronformat'};
 3937:             }
 3938:             $is_custom = 1;
 3939:         } else {
 3940:             $scantronurl = $scantronurls{'default'};
 3941:         }
 3942:     } else {
 3943:         if ($is_custom) {
 3944:             $scantronurl = $scantronurls{'custom'};
 3945:         } else {
 3946:             $scantronurl = $scantronurls{'default'};
 3947:         }
 3948:     }
 3949:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3950:     $datatable .= '<tr'.$css_class.'>';
 3951:     if (!$is_custom) {
 3952:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3953:                       '<span class="LC_nobreak">';
 3954:         if ($scantronurl) {
 3955:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 3956:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 3957:         } else {
 3958:             $datatable = &mt('File unavailable for display');
 3959:         }
 3960:         $datatable .= '</span></td>';
 3961:         if (keys(%error) == 0) { 
 3962:             $datatable .= '<td valign="bottom">';
 3963:             if (!$switchserver) {
 3964:                 $datatable .= &mt('Upload:').'<br />';
 3965:             }
 3966:         } else {
 3967:             my $errorstr;
 3968:             foreach my $key (sort(keys(%error))) {
 3969:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3970:             }
 3971:             $datatable .= '<td>'.$errorstr;
 3972:         }
 3973:     } else {
 3974:         if (keys(%error) > 0) {
 3975:             my $errorstr;
 3976:             foreach my $key (sort(keys(%error))) {
 3977:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3978:             } 
 3979:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3980:         } elsif ($scantronurl) {
 3981:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 3982:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 3983:             $datatable .= '<td><span class="LC_nobreak">'.
 3984:                           $link.
 3985:                           '<label><input type="checkbox" name="scantronformat_del"'.
 3986:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 3987:                           '<td><span class="LC_nobreak">&nbsp;'.
 3988:                           &mt('Replace:').'</span><br />';
 3989:         }
 3990:     }
 3991:     if (keys(%error) == 0) {
 3992:         if ($switchserver) {
 3993:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3994:         } else {
 3995:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3996:                          '<input type="file" name="scantronformat" /></span>';
 3997:         }
 3998:     }
 3999:     $datatable .= '</td></tr>';
 4000:     $$rowtotal ++;
 4001:     return $datatable;
 4002: }
 4003: 
 4004: sub legacy_scantronformat {
 4005:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4006:     my ($url,$error);
 4007:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4008:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4009:         (my $result,$url) =
 4010:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4011:                          '','',$newfile);
 4012:         if ($result ne 'ok') {
 4013:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4014:         }
 4015:     }
 4016:     return ($url,$error);
 4017: }
 4018: 
 4019: sub print_coursecategories {
 4020:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4021:     my $datatable;
 4022:     if ($position eq 'top') {
 4023:         my $toggle_cats_crs = ' ';
 4024:         my $toggle_cats_dom = ' checked="checked" ';
 4025:         my $can_cat_crs = ' ';
 4026:         my $can_cat_dom = ' checked="checked" ';
 4027:         my $toggle_catscomm_comm = ' ';
 4028:         my $toggle_catscomm_dom = ' checked="checked" ';
 4029:         my $can_catcomm_comm = ' ';
 4030:         my $can_catcomm_dom = ' checked="checked" ';
 4031: 
 4032:         if (ref($settings) eq 'HASH') {
 4033:             if ($settings->{'togglecats'} eq 'crs') {
 4034:                 $toggle_cats_crs = $toggle_cats_dom;
 4035:                 $toggle_cats_dom = ' ';
 4036:             }
 4037:             if ($settings->{'categorize'} eq 'crs') {
 4038:                 $can_cat_crs = $can_cat_dom;
 4039:                 $can_cat_dom = ' ';
 4040:             }
 4041:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4042:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4043:                 $toggle_catscomm_dom = ' ';
 4044:             }
 4045:             if ($settings->{'categorizecomm'} eq 'comm') {
 4046:                 $can_catcomm_comm = $can_catcomm_dom;
 4047:                 $can_catcomm_dom = ' ';
 4048:             }
 4049:         }
 4050:         my %title = &Apache::lonlocal::texthash (
 4051:                      togglecats     => 'Show/Hide a course in catalog',
 4052:                      togglecatscomm => 'Show/Hide a community in catalog',
 4053:                      categorize     => 'Assign a category to a course',
 4054:                      categorizecomm => 'Assign a category to a community',
 4055:                     );
 4056:         my %level = &Apache::lonlocal::texthash (
 4057:                      dom  => 'Set in Domain',
 4058:                      crs  => 'Set in Course',
 4059:                      comm => 'Set in Community',
 4060:                     );
 4061:         $datatable = '<tr class="LC_odd_row">'.
 4062:                   '<td>'.$title{'togglecats'}.'</td>'.
 4063:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4064:                   '<input type="radio" name="togglecats"'.
 4065:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4066:                   '<label><input type="radio" name="togglecats"'.
 4067:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4068:                   '</tr><tr>'.
 4069:                   '<td>'.$title{'categorize'}.'</td>'.
 4070:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4071:                   '<label><input type="radio" name="categorize"'.
 4072:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4073:                   '<label><input type="radio" name="categorize"'.
 4074:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4075:                   '</tr><tr class="LC_odd_row">'.
 4076:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4077:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4078:                   '<input type="radio" name="togglecatscomm"'.
 4079:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4080:                   '<label><input type="radio" name="togglecatscomm"'.
 4081:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4082:                   '</tr><tr>'.
 4083:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4084:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4085:                   '<label><input type="radio" name="categorizecomm"'.
 4086:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4087:                   '<label><input type="radio" name="categorizecomm"'.
 4088:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4089:                   '</tr>';
 4090:         $$rowtotal += 4;
 4091:     } else {
 4092:         my $css_class;
 4093:         my $itemcount = 1;
 4094:         my $cathash; 
 4095:         if (ref($settings) eq 'HASH') {
 4096:             $cathash = $settings->{'cats'};
 4097:         }
 4098:         if (ref($cathash) eq 'HASH') {
 4099:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4100:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4101:                                                    \%allitems,\%idx,\@jsarray);
 4102:             my $maxdepth = scalar(@cats);
 4103:             my $colattrib = '';
 4104:             if ($maxdepth > 2) {
 4105:                 $colattrib = ' colspan="2" ';
 4106:             }
 4107:             my @path;
 4108:             if (@cats > 0) {
 4109:                 if (ref($cats[0]) eq 'ARRAY') {
 4110:                     my $numtop = @{$cats[0]};
 4111:                     my $maxnum = $numtop;
 4112:                     my %default_names = (
 4113:                           instcode    => &mt('Official courses'),
 4114:                           communities => &mt('Communities'),
 4115:                     );
 4116: 
 4117:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4118:                         ($cathash->{'instcode::0'} eq '') ||
 4119:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4120:                         ($cathash->{'communities::0'} eq '')) {
 4121:                         $maxnum ++;
 4122:                     }
 4123:                     my $lastidx;
 4124:                     for (my $i=0; $i<$numtop; $i++) {
 4125:                         my $parent = $cats[0][$i];
 4126:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4127:                         my $item = &escape($parent).'::0';
 4128:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4129:                         $lastidx = $idx{$item};
 4130:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4131:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4132:                         for (my $k=0; $k<=$maxnum; $k++) {
 4133:                             my $vpos = $k+1;
 4134:                             my $selstr;
 4135:                             if ($k == $i) {
 4136:                                 $selstr = ' selected="selected" ';
 4137:                             }
 4138:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4139:                         }
 4140:                         $datatable .= '</select></span></td><td>';
 4141:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4142:                             $datatable .=  '<span class="LC_nobreak">'
 4143:                                            .$default_names{$parent}.'</span>';
 4144:                             if ($parent eq 'instcode') {
 4145:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4146:                                               .&mt('with institutional codes')
 4147:                                               .')</span></td><td'.$colattrib.'>';
 4148:                             } else {
 4149:                                 $datatable .= '<table><tr><td>';
 4150:                             }
 4151:                             $datatable .= '<span class="LC_nobreak">'
 4152:                                           .'<label><input type="radio" name="'
 4153:                                           .$parent.'" value="1" checked="checked" />'
 4154:                                           .&mt('Display').'</label>';
 4155:                             if ($parent eq 'instcode') {
 4156:                                 $datatable .= '&nbsp;';
 4157:                             } else {
 4158:                                 $datatable .= '</span></td></tr><tr><td>'
 4159:                                               .'<span class="LC_nobreak">';
 4160:                             }
 4161:                             $datatable .= '<label><input type="radio" name="'
 4162:                                           .$parent.'" value="0" />'
 4163:                                           .&mt('Do not display').'</label></span>';
 4164:                             if ($parent eq 'communities') {
 4165:                                 $datatable .= '</td></tr></table>';
 4166:                             }
 4167:                             $datatable .= '</td>';
 4168:                         } else {
 4169:                             $datatable .= $parent
 4170:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 4171:                                           .'<input type="checkbox" name="deletecategory" '
 4172:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4173:                         }
 4174:                         my $depth = 1;
 4175:                         push(@path,$parent);
 4176:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4177:                         pop(@path);
 4178:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4179:                         $itemcount ++;
 4180:                     }
 4181:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4182:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4183:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4184:                     for (my $k=0; $k<=$maxnum; $k++) {
 4185:                         my $vpos = $k+1;
 4186:                         my $selstr;
 4187:                         if ($k == $numtop) {
 4188:                             $selstr = ' selected="selected" ';
 4189:                         }
 4190:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4191:                     }
 4192:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4193:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4194:                                   .'</tr>'."\n";
 4195:                     $itemcount ++;
 4196:                     foreach my $default ('instcode','communities') {
 4197:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4198:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4199:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4200:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4201:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4202:                             for (my $k=0; $k<=$maxnum; $k++) {
 4203:                                 my $vpos = $k+1;
 4204:                                 my $selstr;
 4205:                                 if ($k == $maxnum) {
 4206:                                     $selstr = ' selected="selected" ';
 4207:                                 }
 4208:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4209:                             }
 4210:                             $datatable .= '</select></span></td>'.
 4211:                                           '<td><span class="LC_nobreak">'.
 4212:                                           $default_names{$default}.'</span>';
 4213:                             if ($default eq 'instcode') {
 4214:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4215:                                               .&mt('with institutional codes').')</span>';
 4216:                             }
 4217:                             $datatable .= '</td>'
 4218:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4219:                                           .&mt('Display').'</label>&nbsp;'
 4220:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4221:                                           .&mt('Do not display').'</label></span></td></tr>';
 4222:                         }
 4223:                     }
 4224:                 }
 4225:             } else {
 4226:                 $datatable .= &initialize_categories($itemcount);
 4227:             }
 4228:         } else {
 4229:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4230:                           .&initialize_categories($itemcount);
 4231:         }
 4232:         $$rowtotal += $itemcount;
 4233:     }
 4234:     return $datatable;
 4235: }
 4236: 
 4237: sub print_serverstatuses {
 4238:     my ($dom,$settings,$rowtotal) = @_;
 4239:     my $datatable;
 4240:     my @pages = &serverstatus_pages();
 4241:     my (%namedaccess,%machineaccess);
 4242:     foreach my $type (@pages) {
 4243:         $namedaccess{$type} = '';
 4244:         $machineaccess{$type}= '';
 4245:     }
 4246:     if (ref($settings) eq 'HASH') {
 4247:         foreach my $type (@pages) {
 4248:             if (exists($settings->{$type})) {
 4249:                 if (ref($settings->{$type}) eq 'HASH') {
 4250:                     foreach my $key (keys(%{$settings->{$type}})) {
 4251:                         if ($key eq 'namedusers') {
 4252:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4253:                         } elsif ($key eq 'machines') {
 4254:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4255:                         }
 4256:                     }
 4257:                 }
 4258:             }
 4259:         }
 4260:     }
 4261:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4262:     my $rownum = 0;
 4263:     my $css_class;
 4264:     foreach my $type (@pages) {
 4265:         $rownum ++;
 4266:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4267:         $datatable .= '<tr'.$css_class.'>'.
 4268:                       '<td><span class="LC_nobreak">'.
 4269:                       $titles->{$type}.'</span></td>'.
 4270:                       '<td class="LC_left_item">'.
 4271:                       '<input type="text" name="'.$type.'_namedusers" '.
 4272:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4273:                       '<td class="LC_right_item">'.
 4274:                       '<span class="LC_nobreak">'.
 4275:                       '<input type="text" name="'.$type.'_machines" '.
 4276:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4277:                       '</td></tr>'."\n";
 4278:     }
 4279:     $$rowtotal += $rownum;
 4280:     return $datatable;
 4281: }
 4282: 
 4283: sub serverstatus_pages {
 4284:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4285:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4286:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4287: }
 4288: 
 4289: sub coursecategories_javascript {
 4290:     my ($settings) = @_;
 4291:     my ($output,$jstext,$cathash);
 4292:     if (ref($settings) eq 'HASH') {
 4293:         $cathash = $settings->{'cats'};
 4294:     }
 4295:     if (ref($cathash) eq 'HASH') {
 4296:         my (@cats,@jsarray,%idx);
 4297:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4298:         if (@jsarray > 0) {
 4299:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4300:             for (my $i=0; $i<@jsarray; $i++) {
 4301:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4302:                     my $catstr = join('","',@{$jsarray[$i]});
 4303:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4304:                 }
 4305:             }
 4306:         }
 4307:     } else {
 4308:         $jstext  = '    var categories = Array(1);'."\n".
 4309:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4310:     }
 4311:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4312:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4313:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4314:     $output = <<"ENDSCRIPT";
 4315: <script type="text/javascript">
 4316: // <![CDATA[
 4317: function reorderCats(form,parent,item,idx) {
 4318:     var changedVal;
 4319: $jstext
 4320:     var newpos = 'addcategory_pos';
 4321:     var current = new Array;
 4322:     if (parent == '') {
 4323:         var has_instcode = 0;
 4324:         var maxtop = categories[idx].length;
 4325:         for (var j=0; j<maxtop; j++) {
 4326:             if (categories[idx][j] == 'instcode::0') {
 4327:                 has_instcode == 1;
 4328:             }
 4329:         }
 4330:         if (has_instcode == 0) {
 4331:             categories[idx][maxtop] = 'instcode_pos';
 4332:         }
 4333:     } else {
 4334:         newpos += '_'+parent;
 4335:     }
 4336:     var maxh = 1 + categories[idx].length;
 4337:     var current = new Array;
 4338:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4339:     if (item == newpos) {
 4340:         changedVal = newitemVal;
 4341:     } else {
 4342:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4343:         current[newitemVal] = newpos;
 4344:     }
 4345:     for (var i=0; i<categories[idx].length; i++) {
 4346:         var elementName = categories[idx][i];
 4347:         if (elementName != item) {
 4348:             if (form.elements[elementName]) {
 4349:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4350:                 current[currVal] = elementName;
 4351:             }
 4352:         }
 4353:     }
 4354:     var oldVal;
 4355:     for (var j=0; j<maxh; j++) {
 4356:         if (current[j] == undefined) {
 4357:             oldVal = j;
 4358:         }
 4359:     }
 4360:     if (oldVal < changedVal) {
 4361:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4362:            var elementName = current[k];
 4363:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4364:         }
 4365:     } else {
 4366:         for (var k=changedVal; k<oldVal; k++) {
 4367:             var elementName = current[k];
 4368:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4369:         }
 4370:     }
 4371:     return;
 4372: }
 4373: 
 4374: function categoryCheck(form) {
 4375:     if (form.elements['addcategory_name'].value == 'instcode') {
 4376:         alert('$instcode_reserved\\n$choose_again');
 4377:         return false;
 4378:     }
 4379:     if (form.elements['addcategory_name'].value == 'communities') {
 4380:         alert('$communities_reserved\\n$choose_again');
 4381:         return false;
 4382:     }
 4383:     return true;
 4384: }
 4385: 
 4386: // ]]>
 4387: </script>
 4388: 
 4389: ENDSCRIPT
 4390:     return $output;
 4391: }
 4392: 
 4393: sub initialize_categories {
 4394:     my ($itemcount) = @_;
 4395:     my ($datatable,$css_class,$chgstr);
 4396:     my %default_names = (
 4397:                       instcode    => 'Official courses (with institutional codes)',
 4398:                       communities => 'Communities',
 4399:                         );
 4400:     my $select0 = ' selected="selected"';
 4401:     my $select1 = '';
 4402:     foreach my $default ('instcode','communities') {
 4403:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4404:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4405:         if ($default eq 'communities') {
 4406:             $select1 = $select0;
 4407:             $select0 = '';
 4408:         }
 4409:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4410:                      .'<select name="'.$default.'_pos">'
 4411:                      .'<option value="0"'.$select0.'>1</option>'
 4412:                      .'<option value="1"'.$select1.'>2</option>'
 4413:                      .'<option value="2">3</option></select>&nbsp;'
 4414:                      .$default_names{$default}
 4415:                      .'</span></td><td><span class="LC_nobreak">'
 4416:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4417:                      .&mt('Display').'</label>&nbsp;<label>'
 4418:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4419:                  .'</label></span></td></tr>';
 4420:         $itemcount ++;
 4421:     }
 4422:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4423:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4424:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4425:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4426:                   .'<option value="0">1</option>'
 4427:                   .'<option value="1">2</option>'
 4428:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4429:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4430:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4431:     return $datatable;
 4432: }
 4433: 
 4434: sub build_category_rows {
 4435:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4436:     my ($text,$name,$item,$chgstr);
 4437:     if (ref($cats) eq 'ARRAY') {
 4438:         my $maxdepth = scalar(@{$cats});
 4439:         if (ref($cats->[$depth]) eq 'HASH') {
 4440:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4441:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4442:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4443:                 $text .= '<td><table class="LC_data_table">';
 4444:                 my ($idxnum,$parent_name,$parent_item);
 4445:                 my $higher = $depth - 1;
 4446:                 if ($higher == 0) {
 4447:                     $parent_name = &escape($parent).'::'.$higher;
 4448:                 } else {
 4449:                     if (ref($path) eq 'ARRAY') {
 4450:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4451:                     }
 4452:                 }
 4453:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4454:                 for (my $j=0; $j<=$numchildren; $j++) {
 4455:                     if ($j < $numchildren) {
 4456:                         $name = $cats->[$depth]{$parent}[$j];
 4457:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4458:                         $idxnum = $idx->{$item};
 4459:                     } else {
 4460:                         $name = $parent_name;
 4461:                         $item = $parent_item;
 4462:                     }
 4463:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4464:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4465:                     for (my $i=0; $i<=$numchildren; $i++) {
 4466:                         my $vpos = $i+1;
 4467:                         my $selstr;
 4468:                         if ($j == $i) {
 4469:                             $selstr = ' selected="selected" ';
 4470:                         }
 4471:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4472:                     }
 4473:                     $text .= '</select>&nbsp;';
 4474:                     if ($j < $numchildren) {
 4475:                         my $deeper = $depth+1;
 4476:                         $text .= $name.'&nbsp;'
 4477:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4478:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4479:                         if(ref($path) eq 'ARRAY') {
 4480:                             push(@{$path},$name);
 4481:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4482:                             pop(@{$path});
 4483:                         }
 4484:                     } else {
 4485:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4486:                         if ($j == $numchildren) {
 4487:                             $text .= $name;
 4488:                         } else {
 4489:                             $text .= $item;
 4490:                         }
 4491:                         $text .= '" value="" />';
 4492:                     }
 4493:                     $text .= '</td></tr>';
 4494:                 }
 4495:                 $text .= '</table></td>';
 4496:             } else {
 4497:                 my $higher = $depth-1;
 4498:                 if ($higher == 0) {
 4499:                     $name = &escape($parent).'::'.$higher;
 4500:                 } else {
 4501:                     if (ref($path) eq 'ARRAY') {
 4502:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4503:                     }
 4504:                 }
 4505:                 my $colspan;
 4506:                 if ($parent ne 'instcode') {
 4507:                     $colspan = $maxdepth - $depth - 1;
 4508:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4509:                 }
 4510:             }
 4511:         }
 4512:     }
 4513:     return $text;
 4514: }
 4515: 
 4516: sub modifiable_userdata_row {
 4517:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4518:     my $rolename;
 4519:     if ($context eq 'selfcreate') {
 4520:         if (ref($usertypes) eq 'HASH') {
 4521:             $rolename = $usertypes->{$role};
 4522:         } else {
 4523:             $rolename = $role;
 4524:         }
 4525:     } else {
 4526:         if ($role eq 'cr') {
 4527:             $rolename = &mt('Custom role');
 4528:         } else {
 4529:             $rolename = &Apache::lonnet::plaintext($role);
 4530:         }
 4531:     }
 4532:     my @fields = ('lastname','firstname','middlename','generation',
 4533:                   'permanentemail','id');
 4534:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4535:     my $output;
 4536:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4537:     $output = '<tr '.$css_class.'>'.
 4538:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4539:               '<td class="LC_left_item" colspan="2"><table>';
 4540:     my $rem;
 4541:     my %checks;
 4542:     if (ref($settings) eq 'HASH') {
 4543:         if (ref($settings->{$context}) eq 'HASH') {
 4544:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4545:                 foreach my $field (@fields) {
 4546:                     if ($settings->{$context}->{$role}->{$field}) {
 4547:                         $checks{$field} = ' checked="checked" ';
 4548:                     }
 4549:                 }
 4550:             }
 4551:         }
 4552:     }
 4553:     for (my $i=0; $i<@fields; $i++) {
 4554:         my $rem = $i%($numinrow);
 4555:         if ($rem == 0) {
 4556:             if ($i > 0) {
 4557:                 $output .= '</tr>';
 4558:             }
 4559:             $output .= '<tr>';
 4560:         }
 4561:         my $check = ' ';
 4562:         if (exists($checks{$fields[$i]})) {
 4563:             $check = $checks{$fields[$i]}
 4564:         } else {
 4565:             if ($role eq 'st') {
 4566:                 if (ref($settings) ne 'HASH') {
 4567:                     $check = ' checked="checked" '; 
 4568:                 }
 4569:             }
 4570:         }
 4571:         $output .= '<td class="LC_left_item">'.
 4572:                    '<span class="LC_nobreak"><label>'.
 4573:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4574:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4575:                    '</label></span></td>';
 4576:         $rem = @fields%($numinrow);
 4577:     }
 4578:     my $colsleft = $numinrow - $rem;
 4579:     if ($colsleft > 1 ) {
 4580:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4581:                    '&nbsp;</td>';
 4582:     } elsif ($colsleft == 1) {
 4583:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4584:     }
 4585:     $output .= '</tr></table></td></tr>';
 4586:     return $output;
 4587: }
 4588: 
 4589: sub insttypes_row {
 4590:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4591:     my %lt = &Apache::lonlocal::texthash (
 4592:                       cansearch => 'Users allowed to search',
 4593:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4594:                       lockablenames => 'User preference to lock name',
 4595:              );
 4596:     my $showdom;
 4597:     if ($context eq 'cansearch') {
 4598:         $showdom = ' ('.$dom.')';
 4599:     }
 4600:     my $class = 'LC_left_item';
 4601:     if ($context eq 'statustocreate') {
 4602:         $class = 'LC_right_item';
 4603:     }
 4604:     my $output =  '<tr class="LC_odd_row">'.
 4605:                   '<td>'.$lt{$context}.$showdom.
 4606:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4607:     my $rem;
 4608:     if (ref($types) eq 'ARRAY') {
 4609:         for (my $i=0; $i<@{$types}; $i++) {
 4610:             if (defined($usertypes->{$types->[$i]})) {
 4611:                 my $rem = $i%($numinrow);
 4612:                 if ($rem == 0) {
 4613:                     if ($i > 0) {
 4614:                         $output .= '</tr>';
 4615:                     }
 4616:                     $output .= '<tr>';
 4617:                 }
 4618:                 my $check = ' ';
 4619:                 if (ref($settings) eq 'HASH') {
 4620:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4621:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4622:                             $check = ' checked="checked" ';
 4623:                         }
 4624:                     } elsif ($context eq 'statustocreate') {
 4625:                         $check = ' checked="checked" ';
 4626:                     }
 4627:                 }
 4628:                 $output .= '<td class="LC_left_item">'.
 4629:                            '<span class="LC_nobreak"><label>'.
 4630:                            '<input type="checkbox" name="'.$context.'" '.
 4631:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4632:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4633:             }
 4634:         }
 4635:         $rem = @{$types}%($numinrow);
 4636:     }
 4637:     my $colsleft = $numinrow - $rem;
 4638:     if (($rem == 0) && (@{$types} > 0)) {
 4639:         $output .= '<tr>';
 4640:     }
 4641:     if ($colsleft > 1) {
 4642:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4643:     } else {
 4644:         $output .= '<td class="LC_left_item">';
 4645:     }
 4646:     my $defcheck = ' ';
 4647:     if (ref($settings) eq 'HASH') {  
 4648:         if (ref($settings->{$context}) eq 'ARRAY') {
 4649:             if (grep(/^default$/,@{$settings->{$context}})) {
 4650:                 $defcheck = ' checked="checked" ';
 4651:             }
 4652:         } elsif ($context eq 'statustocreate') {
 4653:             $defcheck = ' checked="checked" ';
 4654:         }
 4655:     }
 4656:     $output .= '<span class="LC_nobreak"><label>'.
 4657:                '<input type="checkbox" name="'.$context.'" '.
 4658:                'value="default"'.$defcheck.'/>'.
 4659:                $othertitle.'</label></span></td>'.
 4660:                '</tr></table></td></tr>';
 4661:     return $output;
 4662: }
 4663: 
 4664: sub sorted_searchtitles {
 4665:     my %searchtitles = &Apache::lonlocal::texthash(
 4666:                          'uname' => 'username',
 4667:                          'lastname' => 'last name',
 4668:                          'lastfirst' => 'last name, first name',
 4669:                      );
 4670:     my @titleorder = ('uname','lastname','lastfirst');
 4671:     return (\%searchtitles,\@titleorder);
 4672: }
 4673: 
 4674: sub sorted_searchtypes {
 4675:     my %srchtypes_desc = (
 4676:                            exact    => 'is exact match',
 4677:                            contains => 'contains ..',
 4678:                            begins   => 'begins with ..',
 4679:                          );
 4680:     my @srchtypeorder = ('exact','begins','contains');
 4681:     return (\%srchtypes_desc,\@srchtypeorder);
 4682: }
 4683: 
 4684: sub usertype_update_row {
 4685:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4686:     my $datatable;
 4687:     my $numinrow = 4;
 4688:     foreach my $type (@{$types}) {
 4689:         if (defined($usertypes->{$type})) {
 4690:             $$rownums ++;
 4691:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4692:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4693:                           '</td><td class="LC_left_item"><table>';
 4694:             for (my $i=0; $i<@{$fields}; $i++) {
 4695:                 my $rem = $i%($numinrow);
 4696:                 if ($rem == 0) {
 4697:                     if ($i > 0) {
 4698:                         $datatable .= '</tr>';
 4699:                     }
 4700:                     $datatable .= '<tr>';
 4701:                 }
 4702:                 my $check = ' ';
 4703:                 if (ref($settings) eq 'HASH') {
 4704:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4705:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4706:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4707:                                 $check = ' checked="checked" ';
 4708:                             }
 4709:                         }
 4710:                     }
 4711:                 }
 4712: 
 4713:                 if ($i == @{$fields}-1) {
 4714:                     my $colsleft = $numinrow - $rem;
 4715:                     if ($colsleft > 1) {
 4716:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4717:                     } else {
 4718:                         $datatable .= '<td>';
 4719:                     }
 4720:                 } else {
 4721:                     $datatable .= '<td>';
 4722:                 }
 4723:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4724:                               '<input type="checkbox" name="updateable_'.$type.
 4725:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4726:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4727:             }
 4728:             $datatable .= '</tr></table></td></tr>';
 4729:         }
 4730:     }
 4731:     return $datatable;
 4732: }
 4733: 
 4734: sub modify_login {
 4735:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 4736:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4737:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4738:     %title = ( coursecatalog => 'Display course catalog',
 4739:                adminmail => 'Display administrator E-mail address',
 4740:                helpdesk  => 'Display "Contact Helpdesk" link',
 4741:                newuser => 'Link for visitors to create a user account',
 4742:                loginheader => 'Log-in box header');
 4743:     @offon = ('off','on');
 4744:     if (ref($domconfig{login}) eq 'HASH') {
 4745:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4746:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4747:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4748:             }
 4749:         }
 4750:     }
 4751:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4752:                                            \%domconfig,\%loginhash);
 4753:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 4754:     foreach my $item (@toggles) {
 4755:         $loginhash{login}{$item} = $env{'form.'.$item};
 4756:     }
 4757:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4758:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4759:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4760:                                          \%loginhash);
 4761:     }
 4762: 
 4763:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4764:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4765:     if (keys(%servers) > 1) {
 4766:         foreach my $lonhost (keys(%servers)) {
 4767:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4768:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4769:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4770:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4771:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4772:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4773:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4774:                         $changes{'loginvia'}{$lonhost} = 1;
 4775:                     } else {
 4776:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4777:                         $changes{'loginvia'}{$lonhost} = 1;
 4778:                     }
 4779:                 } else {
 4780:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4781:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4782:                         $changes{'loginvia'}{$lonhost} = 1;
 4783:                     }
 4784:                 }
 4785:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4786:                     foreach my $item (@loginvia_attribs) {
 4787:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4788:                     }
 4789:                 } else {
 4790:                     foreach my $item (@loginvia_attribs) {
 4791:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4792:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4793:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4794:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4795:                                 $new = '/';
 4796:                             }
 4797:                         }
 4798:                         if (($item eq 'custompath') && 
 4799:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4800:                             $new = '';
 4801:                         }
 4802:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4803:                             $changes{'loginvia'}{$lonhost} = 1;
 4804:                         }
 4805:                         if ($item eq 'exempt') {
 4806:                             $new =~ s/^\s+//;
 4807:                             $new =~ s/\s+$//;
 4808:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4809:                             my @okips;
 4810:                             foreach my $ip (@poss_ips) {
 4811:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4812:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4813:                                         push(@okips,$ip); 
 4814:                                     }
 4815:                                 }
 4816:                             }
 4817:                             if (@okips > 0) {
 4818:                                 $new = join(',',@okips); 
 4819:                             } else {
 4820:                                 $new = ''; 
 4821:                             }
 4822:                         }
 4823:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4824:                     }
 4825:                 }
 4826:             } else {
 4827:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4828:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4829:                     $changes{'loginvia'}{$lonhost} = 1;
 4830:                     foreach my $item (@loginvia_attribs) {
 4831:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4832:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4833:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4834:                                 $new = '/';
 4835:                             }
 4836:                         }
 4837:                         if (($item eq 'custompath') && 
 4838:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4839:                             $new = '';
 4840:                         }
 4841:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4842:                     }
 4843:                 }
 4844:             }
 4845:         }
 4846:     }
 4847: 
 4848:     my $servadm = $r->dir_config('lonAdmEMail');
 4849:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 4850:     if (ref($domconfig{'login'}) eq 'HASH') {
 4851:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 4852:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 4853:                 if ($lang eq 'nolang') {
 4854:                     push(@currlangs,$lang);
 4855:                 } elsif (defined($langchoices{$lang})) {
 4856:                     push(@currlangs,$lang);
 4857:                 } else {
 4858:                     next;
 4859:                 }
 4860:             }
 4861:         }
 4862:     }
 4863:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 4864:     if (@currlangs > 0) {
 4865:         foreach my $lang (@currlangs) {
 4866:             if (grep(/^\Q$lang\E$/,@delurls)) {
 4867:                 $changes{'helpurl'}{$lang} = 1;
 4868:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 4869:                 $changes{'helpurl'}{$lang} = 1;
 4870:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 4871:                 push(@newlangs,$lang);
 4872:             } else {
 4873:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4874:             }
 4875:         }
 4876:     }
 4877:     unless (grep(/^nolang$/,@currlangs)) {
 4878:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 4879:             $changes{'helpurl'}{'nolang'} = 1;
 4880:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 4881:             push(@newlangs,'nolang');
 4882:         }
 4883:     }
 4884:     if ($env{'form.loginhelpurl_add_lang'}) {
 4885:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 4886:             ($env{'form.loginhelpurl_add_file.filename'})) {
 4887:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 4888:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 4889:         }
 4890:     }
 4891:     if ((@newlangs > 0) || ($addedfile)) {
 4892:         my $error;
 4893:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 4894:         if ($configuserok eq 'ok') {
 4895:             if ($switchserver) {
 4896:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 4897:             } elsif ($author_ok eq 'ok') {
 4898:                 my @allnew = @newlangs;
 4899:                 if ($addedfile ne '') {
 4900:                     push(@allnew,$addedfile);
 4901:                 }
 4902:                 foreach my $lang (@allnew) {
 4903:                     my $formelem = 'loginhelpurl_'.$lang;
 4904:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 4905:                         $formelem = 'loginhelpurl_add_file';
 4906:                     }
 4907:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 4908:                                                                "help/$lang",'','',$newfile{$lang});
 4909:                     if ($result eq 'ok') {
 4910:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 4911:                         $changes{'helpurl'}{$lang} = 1;
 4912:                     } else {
 4913:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 4914:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 4915:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 4916:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 4917: 
 4918:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4919:                         }
 4920:                     }
 4921:                 }
 4922:             } else {
 4923:                 $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);
 4924:             }
 4925:         } else {
 4926:             $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);
 4927:         }
 4928:         if ($error) {
 4929:             &Apache::lonnet::logthis($error);
 4930:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4931:         }
 4932:     }
 4933:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 4934: 
 4935:     my $defaulthelpfile = '/adm/loginproblems.html';
 4936:     my $defaulttext = &mt('Default in use');
 4937: 
 4938:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4939:                                              $dom);
 4940:     if ($putresult eq 'ok') {
 4941:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 4942:         my %defaultchecked = (
 4943:                     'coursecatalog' => 'on',
 4944:                     'helpdesk'      => 'on',
 4945:                     'adminmail'     => 'off',
 4946:                     'newuser'       => 'off',
 4947:         );
 4948:         if (ref($domconfig{'login'}) eq 'HASH') {
 4949:             foreach my $item (@toggles) {
 4950:                 if ($defaultchecked{$item} eq 'on') { 
 4951:                     if (($domconfig{'login'}{$item} eq '0') &&
 4952:                         ($env{'form.'.$item} eq '1')) {
 4953:                         $changes{$item} = 1;
 4954:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4955:                               $domconfig{'login'}{$item} eq '1') &&
 4956:                              ($env{'form.'.$item} eq '0')) {
 4957:                         $changes{$item} = 1;
 4958:                     }
 4959:                 } elsif ($defaultchecked{$item} eq 'off') {
 4960:                     if (($domconfig{'login'}{$item} eq '1') &&
 4961:                         ($env{'form.'.$item} eq '0')) {
 4962:                         $changes{$item} = 1;
 4963:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4964:                               $domconfig{'login'}{$item} eq '0') &&
 4965:                              ($env{'form.'.$item} eq '1')) {
 4966:                         $changes{$item} = 1;
 4967:                     }
 4968:                 }
 4969:             }
 4970:         }
 4971:         if (keys(%changes) > 0 || $colchgtext) {
 4972:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4973:             if (ref($lastactref) eq 'HASH') {
 4974:                 $lastactref->{'domainconfig'} = 1;
 4975:             }
 4976:             $resulttext = &mt('Changes made:').'<ul>';
 4977:             foreach my $item (sort(keys(%changes))) {
 4978:                 if ($item eq 'loginvia') {
 4979:                     if (ref($changes{$item}) eq 'HASH') {
 4980:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4981:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4982:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4983:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4984:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4985:                                     $protocol = 'http' if ($protocol ne 'https');
 4986:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4987: 
 4988:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4989:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4990:                                     } else {
 4991:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4992:                                     }
 4993:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4994:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4995:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4996:                                     }
 4997:                                     $resulttext .= '</li>';
 4998:                                 } else {
 4999:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5000:                                 }
 5001:                             } else {
 5002:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5003:                             }
 5004:                         }
 5005:                         $resulttext .= '</ul></li>';
 5006:                     }
 5007:                 } elsif ($item eq 'helpurl') {
 5008:                     if (ref($changes{$item}) eq 'HASH') {
 5009:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5010:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5011:                                 my ($chg,$link);
 5012:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5013:                                 if ($lang eq 'nolang') {
 5014:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5015:                                 } else {
 5016:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5017:                                 }
 5018:                                 $resulttext .= '<li>'.$chg.'</li>';
 5019:                             } else {
 5020:                                 my $chg;
 5021:                                 if ($lang eq 'nolang') {
 5022:                                     $chg = &mt('custom log-in help file for no preferred language');
 5023:                                 } else {
 5024:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 5025:                                 }
 5026:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 5027:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 5028:                                                       '?inhibitmenu=yes',$chg,600,500).
 5029:                                                '</li>';
 5030:                             }
 5031:                         }
 5032:                     }
 5033:                 } elsif ($item eq 'captcha') {
 5034:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5035:                         my $chgtxt;
 5036:                         if ($loginhash{'login'}{$item} eq 'notused') {
 5037:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 5038:                         } else {
 5039:                             my %captchas = &captcha_phrases();
 5040:                             if ($captchas{$loginhash{'login'}{$item}}) {
 5041:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 5042:                             } else {
 5043:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 5044:                             }
 5045:                         }
 5046:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5047:                     }
 5048:                 } elsif ($item eq 'recaptchakeys') {
 5049:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5050:                         my ($privkey,$pubkey);
 5051:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 5052:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 5053:                             $privkey = $loginhash{'login'}{$item}{'private'};
 5054:                         }
 5055:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 5056:                         if (!$pubkey) {
 5057:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 5058:                         } else {
 5059:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 5060:                         }
 5061:                         if (!$privkey) {
 5062:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 5063:                         } else {
 5064:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 5065:                         }
 5066:                         $chgtxt .= '</ul>';
 5067:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5068:                     }
 5069:                 } else {
 5070:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 5071:                 }
 5072:             }
 5073:             $resulttext .= $colchgtext.'</ul>';
 5074:         } else {
 5075:             $resulttext = &mt('No changes made to log-in page settings');
 5076:         }
 5077:     } else {
 5078:         $resulttext = '<span class="LC_error">'.
 5079: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5080:     }
 5081:     if ($errors) {
 5082:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 5083:                        $errors.'</ul>';
 5084:     }
 5085:     return $resulttext;
 5086: }
 5087: 
 5088: sub color_font_choices {
 5089:     my %choices =
 5090:         &Apache::lonlocal::texthash (
 5091:             img => "Header",
 5092:             bgs => "Background colors",
 5093:             links => "Link colors",
 5094:             images => "Images",
 5095:             font => "Font color",
 5096:             fontmenu => "Font menu",
 5097:             pgbg => "Page",
 5098:             tabbg => "Header",
 5099:             sidebg => "Border",
 5100:             link => "Link",
 5101:             alink => "Active link",
 5102:             vlink => "Visited link",
 5103:         );
 5104:     return %choices;
 5105: }
 5106: 
 5107: sub modify_rolecolors {
 5108:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 5109:     my ($resulttext,%rolehash);
 5110:     $rolehash{'rolecolors'} = {};
 5111:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5112:         if ($domconfig{'rolecolors'} eq '') {
 5113:             $domconfig{'rolecolors'} = {};
 5114:         }
 5115:     }
 5116:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5117:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5118:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5119:                                              $dom);
 5120:     if ($putresult eq 'ok') {
 5121:         if (keys(%changes) > 0) {
 5122:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5123:             if (ref($lastactref) eq 'HASH') {
 5124:                 $lastactref->{'domainconfig'} = 1;
 5125:             }
 5126:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5127:                                              $rolehash{'rolecolors'});
 5128:         } else {
 5129:             $resulttext = &mt('No changes made to default color schemes');
 5130:         }
 5131:     } else {
 5132:         $resulttext = '<span class="LC_error">'.
 5133: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5134:     }
 5135:     if ($errors) {
 5136:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5137:                        $errors.'</ul>';
 5138:     }
 5139:     return $resulttext;
 5140: }
 5141: 
 5142: sub modify_colors {
 5143:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5144:     my (%changes,%choices);
 5145:     my @bgs;
 5146:     my @links = ('link','alink','vlink');
 5147:     my @logintext;
 5148:     my @images;
 5149:     my $servadm = $r->dir_config('lonAdmEMail');
 5150:     my $errors;
 5151:     my %defaults;
 5152:     foreach my $role (@{$roles}) {
 5153:         if ($role eq 'login') {
 5154:             %choices = &login_choices();
 5155:             @logintext = ('textcol','bgcol');
 5156:         } else {
 5157:             %choices = &color_font_choices();
 5158:         }
 5159:         if ($role eq 'login') {
 5160:             @images = ('img','logo','domlogo','login');
 5161:             @bgs = ('pgbg','mainbg','sidebg');
 5162:         } else {
 5163:             @images = ('img');
 5164:             @bgs = ('pgbg','tabbg','sidebg');
 5165:         }
 5166:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 5167:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 5168:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5169:         }
 5170:         if ($role eq 'login') {
 5171:             foreach my $item (@logintext) {
 5172:                 unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'logintext'}{$item}) {
 5173:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5174:                 }
 5175:             }
 5176:         } else {
 5177:             unless($env{'form.'.$role.'_fontmenu'} eq $defaults{'fontmenu'}) {
 5178:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5179:             }
 5180:         }
 5181:         foreach my $item (@bgs) {
 5182:             unless ($env{'form.'.$role.'_'.$item} eq $defaults{'bgs'}{$item} ) {
 5183:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5184:             }
 5185:         }
 5186:         foreach my $item (@links) {
 5187:             unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'links'}{$item}) {
 5188:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5189:             }
 5190:         }
 5191:         my ($configuserok,$author_ok,$switchserver) = 
 5192:             &config_check($dom,$confname,$servadm);
 5193:         my ($width,$height) = &thumb_dimensions();
 5194:         if (ref($domconfig->{$role}) ne 'HASH') {
 5195:             $domconfig->{$role} = {};
 5196:         }
 5197:         foreach my $img (@images) {
 5198:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5199:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5200:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5201:                 } else { 
 5202:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5203:                 }
 5204:             } 
 5205: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5206: 		 && !defined($domconfig->{$role}{$img})
 5207: 		 && !$env{'form.'.$role.'_del_'.$img}
 5208: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5209: 		# import the old configured image from the .tab setting
 5210: 		# if they haven't provided a new one 
 5211: 		$domconfig->{$role}{$img} = 
 5212: 		    $env{'form.'.$role.'_import_'.$img};
 5213: 	    }
 5214:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5215:                 my $error;
 5216:                 if ($configuserok eq 'ok') {
 5217:                     if ($switchserver) {
 5218:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5219:                     } else {
 5220:                         if ($author_ok eq 'ok') {
 5221:                             my ($result,$logourl) = 
 5222:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5223:                                            $dom,$confname,$img,$width,$height);
 5224:                             if ($result eq 'ok') {
 5225:                                 $confhash->{$role}{$img} = $logourl;
 5226:                                 $changes{$role}{'images'}{$img} = 1;
 5227:                             } else {
 5228:                                 $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);
 5229:                             }
 5230:                         } else {
 5231:                             $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);
 5232:                         }
 5233:                     }
 5234:                 } else {
 5235:                     $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);
 5236:                 }
 5237:                 if ($error) {
 5238:                     &Apache::lonnet::logthis($error);
 5239:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5240:                 }
 5241:             } elsif ($domconfig->{$role}{$img} ne '') {
 5242:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5243:                     my $error;
 5244:                     if ($configuserok eq 'ok') {
 5245: # is confname an author?
 5246:                         if ($switchserver eq '') {
 5247:                             if ($author_ok eq 'ok') {
 5248:                                 my ($result,$logourl) = 
 5249:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5250:                                             $dom,$confname,$img,$width,$height);
 5251:                                 if ($result eq 'ok') {
 5252:                                     $confhash->{$role}{$img} = $logourl;
 5253: 				    $changes{$role}{'images'}{$img} = 1;
 5254:                                 }
 5255:                             }
 5256:                         }
 5257:                     }
 5258:                 }
 5259:             }
 5260:         }
 5261:         if (ref($domconfig) eq 'HASH') {
 5262:             if (ref($domconfig->{$role}) eq 'HASH') {
 5263:                 foreach my $img (@images) {
 5264:                     if ($domconfig->{$role}{$img} ne '') {
 5265:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5266:                             $confhash->{$role}{$img} = '';
 5267:                             $changes{$role}{'images'}{$img} = 1;
 5268:                         } else {
 5269:                             if ($confhash->{$role}{$img} eq '') {
 5270:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5271:                             }
 5272:                         }
 5273:                     } else {
 5274:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5275:                             $confhash->{$role}{$img} = '';
 5276:                             $changes{$role}{'images'}{$img} = 1;
 5277:                         } 
 5278:                     }
 5279:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5280:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5281:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5282:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5283:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5284:                             }
 5285:                         } else {
 5286:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5287:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5288:                             }
 5289:                         }
 5290:                     }
 5291:                 }
 5292:                 if ($domconfig->{$role}{'font'} ne '') {
 5293:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5294:                         $changes{$role}{'font'} = 1;
 5295:                     }
 5296:                 } else {
 5297:                     if ($confhash->{$role}{'font'}) {
 5298:                         $changes{$role}{'font'} = 1;
 5299:                     }
 5300:                 }
 5301:                 if ($role ne 'login') {
 5302:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5303:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5304:                             $changes{$role}{'fontmenu'} = 1;
 5305:                         }
 5306:                     } else {
 5307:                         if ($confhash->{$role}{'fontmenu'}) {
 5308:                             $changes{$role}{'fontmenu'} = 1;
 5309:                         }
 5310:                     }
 5311:                 }
 5312:                 foreach my $item (@bgs) {
 5313:                     if ($domconfig->{$role}{$item} ne '') {
 5314:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5315:                             $changes{$role}{'bgs'}{$item} = 1;
 5316:                         } 
 5317:                     } else {
 5318:                         if ($confhash->{$role}{$item}) {
 5319:                             $changes{$role}{'bgs'}{$item} = 1;
 5320:                         }
 5321:                     }
 5322:                 }
 5323:                 foreach my $item (@links) {
 5324:                     if ($domconfig->{$role}{$item} ne '') {
 5325:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5326:                             $changes{$role}{'links'}{$item} = 1;
 5327:                         }
 5328:                     } else {
 5329:                         if ($confhash->{$role}{$item}) {
 5330:                             $changes{$role}{'links'}{$item} = 1;
 5331:                         }
 5332:                     }
 5333:                 }
 5334:                 foreach my $item (@logintext) {
 5335:                     if ($domconfig->{$role}{$item} ne '') {
 5336:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5337:                             $changes{$role}{'logintext'}{$item} = 1;
 5338:                         }
 5339:                     } else {
 5340:                         if ($confhash->{$role}{$item}) {
 5341:                             $changes{$role}{'logintext'}{$item} = 1;
 5342:                         }
 5343:                     }
 5344:                 }
 5345:             } else {
 5346:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5347:                                         \@logintext,$confhash,\%changes); 
 5348:             }
 5349:         } else {
 5350:             &default_change_checker($role,\@images,\@links,\@bgs,
 5351:                                     \@logintext,$confhash,\%changes); 
 5352:         }
 5353:     }
 5354:     return ($errors,%changes);
 5355: }
 5356: 
 5357: sub config_check {
 5358:     my ($dom,$confname,$servadm) = @_;
 5359:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5360:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5361:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5362:                                                    $confname,$servadm);
 5363:     if ($configuserok eq 'ok') {
 5364:         $switchserver = &check_switchserver($dom,$confname);
 5365:         if ($switchserver eq '') {
 5366:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5367:         }
 5368:     }
 5369:     return ($configuserok,$author_ok,$switchserver);
 5370: }
 5371: 
 5372: sub default_change_checker {
 5373:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5374:     foreach my $item (@{$links}) {
 5375:         if ($confhash->{$role}{$item}) {
 5376:             $changes->{$role}{'links'}{$item} = 1;
 5377:         }
 5378:     }
 5379:     foreach my $item (@{$bgs}) {
 5380:         if ($confhash->{$role}{$item}) {
 5381:             $changes->{$role}{'bgs'}{$item} = 1;
 5382:         }
 5383:     }
 5384:     foreach my $item (@{$logintext}) {
 5385:         if ($confhash->{$role}{$item}) {
 5386:             $changes->{$role}{'logintext'}{$item} = 1;
 5387:         }
 5388:     }
 5389:     foreach my $img (@{$images}) {
 5390:         if ($env{'form.'.$role.'_del_'.$img}) {
 5391:             $confhash->{$role}{$img} = '';
 5392:             $changes->{$role}{'images'}{$img} = 1;
 5393:         }
 5394:         if ($role eq 'login') {
 5395:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5396:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5397:             }
 5398:         }
 5399:     }
 5400:     if ($confhash->{$role}{'font'}) {
 5401:         $changes->{$role}{'font'} = 1;
 5402:     }
 5403: }
 5404: 
 5405: sub display_colorchgs {
 5406:     my ($dom,$changes,$roles,$confhash) = @_;
 5407:     my (%choices,$resulttext);
 5408:     if (!grep(/^login$/,@{$roles})) {
 5409:         $resulttext = &mt('Changes made:').'<br />';
 5410:     }
 5411:     foreach my $role (@{$roles}) {
 5412:         if ($role eq 'login') {
 5413:             %choices = &login_choices();
 5414:         } else {
 5415:             %choices = &color_font_choices();
 5416:         }
 5417:         if (ref($changes->{$role}) eq 'HASH') {
 5418:             if ($role ne 'login') {
 5419:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5420:             }
 5421:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5422:                 if ($role ne 'login') {
 5423:                     $resulttext .= '<ul>';
 5424:                 }
 5425:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5426:                     if ($role ne 'login') {
 5427:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5428:                     }
 5429:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5430:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5431:                             if ($confhash->{$role}{$key}{$item}) {
 5432:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5433:                             } else {
 5434:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5435:                             }
 5436:                         } elsif ($confhash->{$role}{$item} eq '') {
 5437:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5438:                         } else {
 5439:                             my $newitem = $confhash->{$role}{$item};
 5440:                             if ($key eq 'images') {
 5441:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5442:                             }
 5443:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5444:                         }
 5445:                     }
 5446:                     if ($role ne 'login') {
 5447:                         $resulttext .= '</ul></li>';
 5448:                     }
 5449:                 } else {
 5450:                     if ($confhash->{$role}{$key} eq '') {
 5451:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5452:                     } else {
 5453:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5454:                     }
 5455:                 }
 5456:                 if ($role ne 'login') {
 5457:                     $resulttext .= '</ul>';
 5458:                 }
 5459:             }
 5460:         }
 5461:     }
 5462:     return $resulttext;
 5463: }
 5464: 
 5465: sub thumb_dimensions {
 5466:     return ('200','50');
 5467: }
 5468: 
 5469: sub check_dimensions {
 5470:     my ($inputfile) = @_;
 5471:     my ($fullwidth,$fullheight);
 5472:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5473:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5474:             my $imageinfo = <PIPE>;
 5475:             if (!close(PIPE)) {
 5476:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5477:             }
 5478:             chomp($imageinfo);
 5479:             my ($fullsize) = 
 5480:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5481:             if ($fullsize) {
 5482:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5483:             }
 5484:         }
 5485:     }
 5486:     return ($fullwidth,$fullheight);
 5487: }
 5488: 
 5489: sub check_configuser {
 5490:     my ($uhome,$dom,$confname,$servadm) = @_;
 5491:     my ($configuserok,%currroles);
 5492:     if ($uhome eq 'no_host') {
 5493:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5494:         my $configpass = &LONCAPA::Enrollment::create_password();
 5495:         $configuserok = 
 5496:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5497:                              $configpass,'','','','','',undef,$servadm);
 5498:     } else {
 5499:         $configuserok = 'ok';
 5500:         %currroles = 
 5501:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5502:     }
 5503:     return ($configuserok,%currroles);
 5504: }
 5505: 
 5506: sub check_authorstatus {
 5507:     my ($dom,$confname,%currroles) = @_;
 5508:     my $author_ok;
 5509:     if (!$currroles{':'.$dom.':au'}) {
 5510:         my $start = time;
 5511:         my $end = 0;
 5512:         $author_ok = 
 5513:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5514:                                         'au',$end,$start,'','','domconfig');
 5515:     } else {
 5516:         $author_ok = 'ok';
 5517:     }
 5518:     return $author_ok;
 5519: }
 5520: 
 5521: sub publishlogo {
 5522:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5523:     my ($output,$fname,$logourl);
 5524:     if ($action eq 'upload') {
 5525:         $fname=$env{'form.'.$formname.'.filename'};
 5526:         chop($env{'form.'.$formname});
 5527:     } else {
 5528:         ($fname) = ($formname =~ /([^\/]+)$/);
 5529:     }
 5530:     if ($savefileas ne '') {
 5531:         $fname = $savefileas;
 5532:     }
 5533:     $fname=&Apache::lonnet::clean_filename($fname);
 5534: # See if there is anything left
 5535:     unless ($fname) { return ('error: no uploaded file'); }
 5536:     $fname="$subdir/$fname";
 5537:     my $docroot=$r->dir_config('lonDocRoot');
 5538:     my $filepath="$docroot/priv";
 5539:     my $relpath = "$dom/$confname";
 5540:     my ($fnamepath,$file,$fetchthumb);
 5541:     $file=$fname;
 5542:     if ($fname=~m|/|) {
 5543:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5544:     }
 5545:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5546:     my $count;
 5547:     for ($count=5;$count<=$#parts;$count++) {
 5548:         $filepath.="/$parts[$count]";
 5549:         if ((-e $filepath)!=1) {
 5550:             mkdir($filepath,02770);
 5551:         }
 5552:     }
 5553:     # Check for bad extension and disallow upload
 5554:     if ($file=~/\.(\w+)$/ &&
 5555:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5556:         $output = 
 5557:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 5558:     } elsif ($file=~/\.(\w+)$/ &&
 5559:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5560:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5561:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5562:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5563:     } elsif (-d "$filepath/$file") {
 5564:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 5565:     } else {
 5566:         my $source = $filepath.'/'.$file;
 5567:         my $logfile;
 5568:         if (!open($logfile,">>$source".'.log')) {
 5569:             return (&mt('No write permission to Authoring Space'));
 5570:         }
 5571:         print $logfile
 5572: "\n================= Publish ".localtime()." ================\n".
 5573: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5574: # Save the file
 5575:         if (!open(FH,'>'.$source)) {
 5576:             &Apache::lonnet::logthis('Failed to create '.$source);
 5577:             return (&mt('Failed to create file'));
 5578:         }
 5579:         if ($action eq 'upload') {
 5580:             if (!print FH ($env{'form.'.$formname})) {
 5581:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5582:                 return (&mt('Failed to write file'));
 5583:             }
 5584:         } else {
 5585:             my $original = &Apache::lonnet::filelocation('',$formname);
 5586:             if(!copy($original,$source)) {
 5587:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5588:                 return (&mt('Failed to write file'));
 5589:             }
 5590:         }
 5591:         close(FH);
 5592:         chmod(0660, $source); # Permissions to rw-rw---.
 5593: 
 5594:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5595:         my $copyfile=$targetdir.'/'.$file;
 5596: 
 5597:         my @parts=split(/\//,$targetdir);
 5598:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5599:         for (my $count=5;$count<=$#parts;$count++) {
 5600:             $path.="/$parts[$count]";
 5601:             if (!-e $path) {
 5602:                 print $logfile "\nCreating directory ".$path;
 5603:                 mkdir($path,02770);
 5604:             }
 5605:         }
 5606:         my $versionresult;
 5607:         if (-e $copyfile) {
 5608:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5609:         } else {
 5610:             $versionresult = 'ok';
 5611:         }
 5612:         if ($versionresult eq 'ok') {
 5613:             if (copy($source,$copyfile)) {
 5614:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5615:                 $output = 'ok';
 5616:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5617:                 push(@{$modified_urls},[$copyfile,$source]);
 5618:                 my $metaoutput = 
 5619:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5620:                 unless ($registered_cleanup) {
 5621:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5622:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5623:                     $registered_cleanup=1;
 5624:                 }
 5625:             } else {
 5626:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5627:                 $output = &mt('Failed to copy file to RES space').", $!";
 5628:             }
 5629:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5630:                 my $inputfile = $filepath.'/'.$file;
 5631:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5632:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5633:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5634:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5635:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5636:                         system("convert -sample $thumbsize $inputfile $outfile");
 5637:                         chmod(0660, $filepath.'/tn-'.$file);
 5638:                         if (-e $outfile) {
 5639:                             my $copyfile=$targetdir.'/tn-'.$file;
 5640:                             if (copy($outfile,$copyfile)) {
 5641:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5642:                                 my $thumb_metaoutput = 
 5643:                                     &write_metadata($dom,$confname,$formname,
 5644:                                                     $targetdir,'tn-'.$file,$logfile);
 5645:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5646:                                 unless ($registered_cleanup) {
 5647:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5648:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5649:                                     $registered_cleanup=1;
 5650:                                 }
 5651:                             } else {
 5652:                                 print $logfile "\nUnable to write ".$copyfile.
 5653:                                                ':'.$!."\n";
 5654:                             }
 5655:                         }
 5656:                     }
 5657:                 }
 5658:             }
 5659:         } else {
 5660:             $output = $versionresult;
 5661:         }
 5662:     }
 5663:     return ($output,$logourl);
 5664: }
 5665: 
 5666: sub logo_versioning {
 5667:     my ($targetdir,$file,$logfile) = @_;
 5668:     my $target = $targetdir.'/'.$file;
 5669:     my ($maxversion,$fn,$extn,$output);
 5670:     $maxversion = 0;
 5671:     if ($file =~ /^(.+)\.(\w+)$/) {
 5672:         $fn=$1;
 5673:         $extn=$2;
 5674:     }
 5675:     opendir(DIR,$targetdir);
 5676:     while (my $filename=readdir(DIR)) {
 5677:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5678:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5679:         }
 5680:     }
 5681:     $maxversion++;
 5682:     print $logfile "\nCreating old version ".$maxversion."\n";
 5683:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5684:     if (copy($target,$copyfile)) {
 5685:         print $logfile "Copied old target to ".$copyfile."\n";
 5686:         $copyfile=$copyfile.'.meta';
 5687:         if (copy($target.'.meta',$copyfile)) {
 5688:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5689:             $output = 'ok';
 5690:         } else {
 5691:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5692:             $output = &mt('Failed to copy old meta').", $!, ";
 5693:         }
 5694:     } else {
 5695:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5696:         $output = &mt('Failed to copy old target').", $!, ";
 5697:     }
 5698:     return $output;
 5699: }
 5700: 
 5701: sub write_metadata {
 5702:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5703:     my (%metadatafields,%metadatakeys,$output);
 5704:     $metadatafields{'title'}=$formname;
 5705:     $metadatafields{'creationdate'}=time;
 5706:     $metadatafields{'lastrevisiondate'}=time;
 5707:     $metadatafields{'copyright'}='public';
 5708:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5709:                                          $env{'user.domain'};
 5710:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5711:     $metadatafields{'domain'}=$dom;
 5712:     {
 5713:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5714:         my $mfh;
 5715:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5716:             foreach (sort(keys(%metadatafields))) {
 5717:                 unless ($_=~/\./) {
 5718:                     my $unikey=$_;
 5719:                     $unikey=~/^([A-Za-z]+)/;
 5720:                     my $tag=$1;
 5721:                     $tag=~tr/A-Z/a-z/;
 5722:                     print $mfh "\n\<$tag";
 5723:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5724:                         my $value=$metadatafields{$unikey.'.'.$_};
 5725:                         $value=~s/\"/\'\'/g;
 5726:                         print $mfh ' '.$_.'="'.$value.'"';
 5727:                     }
 5728:                     print $mfh '>'.
 5729:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5730:                             .'</'.$tag.'>';
 5731:                 }
 5732:             }
 5733:             $output = 'ok';
 5734:             print $logfile "\nWrote metadata";
 5735:             close($mfh);
 5736:         } else {
 5737:             print $logfile "\nFailed to open metadata file";
 5738:             $output = &mt('Could not write metadata');
 5739:         }
 5740:     }
 5741:     return $output;
 5742: }
 5743: 
 5744: sub notifysubscribed {
 5745:     foreach my $targetsource (@{$modified_urls}){
 5746:         next unless (ref($targetsource) eq 'ARRAY');
 5747:         my ($target,$source)=@{$targetsource};
 5748:         if ($source ne '') {
 5749:             if (open(my $logfh,'>>'.$source.'.log')) {
 5750:                 print $logfh "\nCleanup phase: Notifications\n";
 5751:                 my @subscribed=&subscribed_hosts($target);
 5752:                 foreach my $subhost (@subscribed) {
 5753:                     print $logfh "\nNotifying host ".$subhost.':';
 5754:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5755:                     print $logfh $reply;
 5756:                 }
 5757:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5758:                 foreach my $subhost (@subscribedmeta) {
 5759:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5760:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5761:                                                         $subhost);
 5762:                     print $logfh $reply;
 5763:                 }
 5764:                 print $logfh "\n============ Done ============\n";
 5765:                 close($logfh);
 5766:             }
 5767:         }
 5768:     }
 5769:     return OK;
 5770: }
 5771: 
 5772: sub subscribed_hosts {
 5773:     my ($target) = @_;
 5774:     my @subscribed;
 5775:     if (open(my $fh,"<$target.subscription")) {
 5776:         while (my $subline=<$fh>) {
 5777:             if ($subline =~ /^($match_lonid):/) {
 5778:                 my $host = $1;
 5779:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5780:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5781:                         push(@subscribed,$host);
 5782:                     }
 5783:                 }
 5784:             }
 5785:         }
 5786:     }
 5787:     return @subscribed;
 5788: }
 5789: 
 5790: sub check_switchserver {
 5791:     my ($dom,$confname) = @_;
 5792:     my ($allowed,$switchserver);
 5793:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5794:     if ($home eq 'no_host') {
 5795:         $home = &Apache::lonnet::domain($dom,'primary');
 5796:     }
 5797:     my @ids=&Apache::lonnet::current_machine_ids();
 5798:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5799:     if (!$allowed) {
 5800: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5801:     }
 5802:     return $switchserver;
 5803: }
 5804: 
 5805: sub modify_quotas {
 5806:     my ($dom,$action,$lastactref,%domconfig) = @_;
 5807:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5808:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5809:     if ($action eq 'quotas') {
 5810:         $context = 'tools'; 
 5811:     } else {
 5812:         $context = $action;
 5813:     }
 5814:     if ($context eq 'requestcourses') {
 5815:         @usertools = ('official','unofficial','community');
 5816:         @options =('norequest','approval','validate','autolimit');
 5817:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5818:         %titles = &courserequest_titles();
 5819:         $toolregexp = join('|',@usertools);
 5820:         %conditions = &courserequest_conditions();
 5821:     } elsif ($context eq 'requestauthor') {
 5822:         @usertools = ('author');
 5823:         %titles = &authorrequest_titles();
 5824:     } else {
 5825:         @usertools = ('aboutme','blog','webdav','portfolio');
 5826:         %titles = &tool_titles();
 5827:     }
 5828:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 5829:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5830:     foreach my $key (keys(%env)) {
 5831:         if ($context eq 'requestcourses') {
 5832:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5833:                 my $item = $1;
 5834:                 my $type = $2;
 5835:                 if ($type =~ /^limit_(.+)/) {
 5836:                     $limithash{$item}{$1} = $env{$key};
 5837:                 } else {
 5838:                     $confhash{$item}{$type} = $env{$key};
 5839:                 }
 5840:             }
 5841:         } elsif ($context eq 'requestauthor') {
 5842:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5843:                 $confhash{$1} = $env{$key};
 5844:             }
 5845:         } else {
 5846:             if ($key =~ /^form\.quota_(.+)$/) {
 5847:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5848:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 5849:                 $confhash{'authorquota'}{$1} = $env{$key};
 5850:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5851:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5852:             }
 5853:         }
 5854:     }
 5855:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5856:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5857:         @approvalnotify = sort(@approvalnotify);
 5858:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5859:         if (ref($domconfig{$action}) eq 'HASH') {
 5860:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5861:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5862:                     $changes{'notify'}{'approval'} = 1;
 5863:                 }
 5864:             } else {
 5865:                 if ($confhash{'notify'}{'approval'}) {
 5866:                     $changes{'notify'}{'approval'} = 1;
 5867:                 }
 5868:             }
 5869:         } else {
 5870:             if ($confhash{'notify'}{'approval'}) {
 5871:                 $changes{'notify'}{'approval'} = 1;
 5872:             }
 5873:         }
 5874:     } else {
 5875:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5876:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 5877:     }
 5878:     foreach my $item (@usertools) {
 5879:         foreach my $type (@{$types},'default','_LC_adv') {
 5880:             my $unset; 
 5881:             if ($context eq 'requestcourses') {
 5882:                 $unset = '0';
 5883:                 if ($type eq '_LC_adv') {
 5884:                     $unset = '';
 5885:                 }
 5886:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5887:                     $confhash{$item}{$type} .= '=';
 5888:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5889:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5890:                     }
 5891:                 }
 5892:             } elsif ($context eq 'requestauthor') {
 5893:                 $unset = '0';
 5894:                 if ($type eq '_LC_adv') {
 5895:                     $unset = '';
 5896:                 }
 5897:             } else {
 5898:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5899:                     $confhash{$item}{$type} = 1;
 5900:                 } else {
 5901:                     $confhash{$item}{$type} = 0;
 5902:                 }
 5903:             }
 5904:             if (ref($domconfig{$action}) eq 'HASH') {
 5905:                 if ($action eq 'requestauthor') {
 5906:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5907:                         $changes{$type} = 1;
 5908:                     }
 5909:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5910:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5911:                         $changes{$item}{$type} = 1;
 5912:                     }
 5913:                 } else {
 5914:                     if ($context eq 'requestcourses') {
 5915:                         if ($confhash{$item}{$type} ne $unset) {
 5916:                             $changes{$item}{$type} = 1;
 5917:                         }
 5918:                     } else {
 5919:                         if (!$confhash{$item}{$type}) {
 5920:                             $changes{$item}{$type} = 1;
 5921:                         }
 5922:                     }
 5923:                 }
 5924:             } else {
 5925:                 if ($context eq 'requestcourses') {
 5926:                     if ($confhash{$item}{$type} ne $unset) {
 5927:                         $changes{$item}{$type} = 1;
 5928:                     }
 5929:                 } elsif ($context eq 'requestauthor') {
 5930:                     if ($confhash{$type} ne $unset) {
 5931:                         $changes{$type} = 1;
 5932:                     }
 5933:                 } else {
 5934:                     if (!$confhash{$item}{$type}) {
 5935:                         $changes{$item}{$type} = 1;
 5936:                     }
 5937:                 }
 5938:             }
 5939:         }
 5940:     }
 5941:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5942:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5943:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5944:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5945:                     if (exists($confhash{'defaultquota'}{$key})) {
 5946:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5947:                             $changes{'defaultquota'}{$key} = 1;
 5948:                         }
 5949:                     } else {
 5950:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5951:                     }
 5952:                 }
 5953:             } else {
 5954:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5955:                     if (exists($confhash{'defaultquota'}{$key})) {
 5956:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5957:                             $changes{'defaultquota'}{$key} = 1;
 5958:                         }
 5959:                     } else {
 5960:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5961:                     }
 5962:                 }
 5963:             }
 5964:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 5965:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 5966:                     if (exists($confhash{'authorquota'}{$key})) {
 5967:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 5968:                             $changes{'authorquota'}{$key} = 1;
 5969:                         }
 5970:                     } else {
 5971:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 5972:                     }
 5973:                 }
 5974:             }
 5975:         }
 5976:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5977:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5978:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5979:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5980:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5981:                             $changes{'defaultquota'}{$key} = 1;
 5982:                         }
 5983:                     } else {
 5984:                         if (!exists($domconfig{'quotas'}{$key})) {
 5985:                             $changes{'defaultquota'}{$key} = 1;
 5986:                         }
 5987:                     }
 5988:                 } else {
 5989:                     $changes{'defaultquota'}{$key} = 1;
 5990:                 }
 5991:             }
 5992:         }
 5993:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 5994:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 5995:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5996:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 5997:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 5998:                             $changes{'authorquota'}{$key} = 1;
 5999:                         }
 6000:                     } else {
 6001:                         $changes{'authorquota'}{$key} = 1;
 6002:                     }
 6003:                 } else {
 6004:                     $changes{'authorquota'}{$key} = 1;
 6005:                 }
 6006:             }
 6007:         }
 6008:     }
 6009: 
 6010:     if ($context eq 'requestauthor') {
 6011:         $domdefaults{'requestauthor'} = \%confhash;
 6012:     } else {
 6013:         foreach my $key (keys(%confhash)) {
 6014:             $domdefaults{$key} = $confhash{$key};
 6015:         }
 6016:     }
 6017: 
 6018:     my %quotahash = (
 6019:                       $action => { %confhash }
 6020:                     );
 6021:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 6022:                                              $dom);
 6023:     if ($putresult eq 'ok') {
 6024:         if (keys(%changes) > 0) {
 6025:             my $cachetime = 24*60*60;
 6026:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6027:             if (ref($lastactref) eq 'HASH') {
 6028:                 $lastactref->{'domdefaults'} = 1;
 6029:             }
 6030:             $resulttext = &mt('Changes made:').'<ul>';
 6031:             unless (($context eq 'requestcourses') ||
 6032:                     ($context eq 'requestauthor')) {
 6033:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 6034:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 6035:                     foreach my $type (@{$types},'default') {
 6036:                         if (defined($changes{'defaultquota'}{$type})) {
 6037:                             my $typetitle = $usertypes->{$type};
 6038:                             if ($type eq 'default') {
 6039:                                 $typetitle = $othertitle;
 6040:                             }
 6041:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 6042:                         }
 6043:                     }
 6044:                     $resulttext .= '</ul></li>';
 6045:                 }
 6046:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 6047:                     $resulttext .= '<li>'.&mt('Authoring space default quotas').'<ul>';
 6048:                     foreach my $type (@{$types},'default') {
 6049:                         if (defined($changes{'authorquota'}{$type})) {
 6050:                             my $typetitle = $usertypes->{$type};
 6051:                             if ($type eq 'default') {
 6052:                                 $typetitle = $othertitle;
 6053:                             }
 6054:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 6055:                         }
 6056:                     }
 6057:                     $resulttext .= '</ul></li>';
 6058:                 }
 6059:             }
 6060:             my %newenv;
 6061:             foreach my $item (@usertools) {
 6062:                 my (%haschgs,%inconf);
 6063:                 if ($context eq 'requestauthor') {
 6064:                     %haschgs = %changes;
 6065:                     %inconf = %confhash;
 6066:                 } else {
 6067:                     if (ref($changes{$item}) eq 'HASH') {
 6068:                         %haschgs = %{$changes{$item}};
 6069:                     }
 6070:                     if (ref($confhash{$item}) eq 'HASH') {
 6071:                         %inconf = %{$confhash{$item}};
 6072:                     }
 6073:                 }
 6074:                 if (keys(%haschgs) > 0) {
 6075:                     my $newacc = 
 6076:                         &Apache::lonnet::usertools_access($env{'user.name'},
 6077:                                                           $env{'user.domain'},
 6078:                                                           $item,'reload',$context);
 6079:                     if (($context eq 'requestcourses') ||
 6080:                         ($context eq 'requestauthor')) {
 6081:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 6082:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 6083:                         }
 6084:                     } else {
 6085:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 6086:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 6087:                         }
 6088:                     }
 6089:                     unless ($context eq 'requestauthor') {
 6090:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 6091:                     }
 6092:                     foreach my $type (@{$types},'default','_LC_adv') {
 6093:                         if ($haschgs{$type}) {
 6094:                             my $typetitle = $usertypes->{$type};
 6095:                             if ($type eq 'default') {
 6096:                                 $typetitle = $othertitle;
 6097:                             } elsif ($type eq '_LC_adv') {
 6098:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 6099:                             }
 6100:                             if ($inconf{$type}) {
 6101:                                 if ($context eq 'requestcourses') {
 6102:                                     my $cond;
 6103:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 6104:                                         if ($1 eq '') {
 6105:                                             $cond = &mt('(Automatic processing of any request).');
 6106:                                         } else {
 6107:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 6108:                                         }
 6109:                                     } else { 
 6110:                                         $cond = $conditions{$inconf{$type}};
 6111:                                     }
 6112:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 6113:                                 } elsif ($context eq 'requestauthor') {
 6114:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 6115:                                                              $titles{$inconf{$type}},$typetitle);
 6116: 
 6117:                                 } else {
 6118:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 6119:                                 }
 6120:                             } else {
 6121:                                 if ($type eq '_LC_adv') {
 6122:                                     if ($inconf{$type} eq '0') {
 6123:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6124:                                     } else { 
 6125:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 6126:                                     }
 6127:                                 } else {
 6128:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6129:                                 }
 6130:                             }
 6131:                         }
 6132:                     }
 6133:                     unless ($context eq 'requestauthor') {
 6134:                         $resulttext .= '</ul></li>';
 6135:                     }
 6136:                 }
 6137:             }
 6138:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 6139:                 if (ref($changes{'notify'}) eq 'HASH') {
 6140:                     if ($changes{'notify'}{'approval'}) {
 6141:                         if (ref($confhash{'notify'}) eq 'HASH') {
 6142:                             if ($confhash{'notify'}{'approval'}) {
 6143:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 6144:                             } else {
 6145:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 6146:                             }
 6147:                         }
 6148:                     }
 6149:                 }
 6150:             }
 6151:             $resulttext .= '</ul>';
 6152:             if (keys(%newenv)) {
 6153:                 &Apache::lonnet::appenv(\%newenv);
 6154:             }
 6155:         } else {
 6156:             if ($context eq 'requestcourses') {
 6157:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 6158:             } elsif ($context eq 'requestauthor') {
 6159:                 $resulttext = &mt('No changes made to rights to request author space.');
 6160:             } else {
 6161:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 6162:             }
 6163:         }
 6164:     } else {
 6165:         $resulttext = '<span class="LC_error">'.
 6166: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6167:     }
 6168:     return $resulttext;
 6169: }
 6170: 
 6171: sub modify_autoenroll {
 6172:     my ($dom,$lastactref,%domconfig) = @_;
 6173:     my ($resulttext,%changes);
 6174:     my %currautoenroll;
 6175:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 6176:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 6177:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 6178:         }
 6179:     }
 6180:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6181:     my %title = ( run => 'Auto-enrollment active',
 6182:                   sender => 'Sender for notification messages',
 6183:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6184:     my @offon = ('off','on');
 6185:     my $sender_uname = $env{'form.sender_uname'};
 6186:     my $sender_domain = $env{'form.sender_domain'};
 6187:     if ($sender_domain eq '') {
 6188:         $sender_uname = '';
 6189:     } elsif ($sender_uname eq '') {
 6190:         $sender_domain = '';
 6191:     }
 6192:     my $coowners = $env{'form.autoassign_coowners'};
 6193:     my %autoenrollhash =  (
 6194:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6195:                                        'sender_uname' => $sender_uname,
 6196:                                        'sender_domain' => $sender_domain,
 6197:                                        'co-owners' => $coowners,
 6198:                                 }
 6199:                      );
 6200:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6201:                                              $dom);
 6202:     if ($putresult eq 'ok') {
 6203:         if (exists($currautoenroll{'run'})) {
 6204:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6205:                  $changes{'run'} = 1;
 6206:              }
 6207:         } elsif ($autorun) {
 6208:             if ($env{'form.autoenroll_run'} ne '1') {
 6209:                  $changes{'run'} = 1;
 6210:             }
 6211:         }
 6212:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6213:             $changes{'sender'} = 1;
 6214:         }
 6215:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6216:             $changes{'sender'} = 1;
 6217:         }
 6218:         if ($currautoenroll{'co-owners'} ne '') {
 6219:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6220:                 $changes{'coowners'} = 1;
 6221:             }
 6222:         } elsif ($coowners) {
 6223:             $changes{'coowners'} = 1;
 6224:         }      
 6225:         if (keys(%changes) > 0) {
 6226:             $resulttext = &mt('Changes made:').'<ul>';
 6227:             if ($changes{'run'}) {
 6228:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6229:             }
 6230:             if ($changes{'sender'}) {
 6231:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6232:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6233:                 } else {
 6234:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6235:                 }
 6236:             }
 6237:             if ($changes{'coowners'}) {
 6238:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6239:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6240:                 if (ref($lastactref) eq 'HASH') {
 6241:                     $lastactref->{'domainconfig'} = 1;
 6242:                 }
 6243:             }
 6244:             $resulttext .= '</ul>';
 6245:         } else {
 6246:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6247:         }
 6248:     } else {
 6249:         $resulttext = '<span class="LC_error">'.
 6250: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6251:     }
 6252:     return $resulttext;
 6253: }
 6254: 
 6255: sub modify_autoupdate {
 6256:     my ($dom,%domconfig) = @_;
 6257:     my ($resulttext,%currautoupdate,%fields,%changes);
 6258:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6259:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6260:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6261:         }
 6262:     }
 6263:     my @offon = ('off','on');
 6264:     my %title = &Apache::lonlocal::texthash (
 6265:                    run => 'Auto-update:',
 6266:                    classlists => 'Updates to user information in classlists?'
 6267:                 );
 6268:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6269:     my %fieldtitles = &Apache::lonlocal::texthash (
 6270:                         id => 'Student/Employee ID',
 6271:                         permanentemail => 'E-mail address',
 6272:                         lastname => 'Last Name',
 6273:                         firstname => 'First Name',
 6274:                         middlename => 'Middle Name',
 6275:                         generation => 'Generation',
 6276:                       );
 6277:     $othertitle = &mt('All users');
 6278:     if (keys(%{$usertypes}) >  0) {
 6279:         $othertitle = &mt('Other users');
 6280:     }
 6281:     foreach my $key (keys(%env)) {
 6282:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6283:             my ($usertype,$item) = ($1,$2);
 6284:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6285:                 if ($usertype eq 'default') {   
 6286:                     push(@{$fields{$1}},$2);
 6287:                 } elsif (ref($types) eq 'ARRAY') {
 6288:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6289:                         push(@{$fields{$1}},$2);
 6290:                     }
 6291:                 }
 6292:             }
 6293:         }
 6294:     }
 6295:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6296:     @lockablenames = sort(@lockablenames);
 6297:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6298:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6299:         if (@changed) {
 6300:             $changes{'lockablenames'} = 1;
 6301:         }
 6302:     } else {
 6303:         if (@lockablenames) {
 6304:             $changes{'lockablenames'} = 1;
 6305:         }
 6306:     }
 6307:     my %updatehash = (
 6308:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6309:                                       classlists => $env{'form.classlists'},
 6310:                                       fields => {%fields},
 6311:                                       lockablenames => \@lockablenames,
 6312:                                     }
 6313:                      );
 6314:     foreach my $key (keys(%currautoupdate)) {
 6315:         if (($key eq 'run') || ($key eq 'classlists')) {
 6316:             if (exists($updatehash{autoupdate}{$key})) {
 6317:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6318:                     $changes{$key} = 1;
 6319:                 }
 6320:             }
 6321:         } elsif ($key eq 'fields') {
 6322:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6323:                 foreach my $item (@{$types},'default') {
 6324:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6325:                         my $change = 0;
 6326:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6327:                             if (!exists($fields{$item})) {
 6328:                                 $change = 1;
 6329:                                 last;
 6330:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6331:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6332:                                     $change = 1;
 6333:                                     last;
 6334:                                 }
 6335:                             }
 6336:                         }
 6337:                         if ($change) {
 6338:                             push(@{$changes{$key}},$item);
 6339:                         }
 6340:                     } 
 6341:                 }
 6342:             }
 6343:         } elsif ($key eq 'lockablenames') {
 6344:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6345:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6346:                 if (@changed) {
 6347:                     $changes{'lockablenames'} = 1;
 6348:                 }
 6349:             } else {
 6350:                 if (@lockablenames) {
 6351:                     $changes{'lockablenames'} = 1;
 6352:                 }
 6353:             }
 6354:         }
 6355:     }
 6356:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6357:         if (@lockablenames) {
 6358:             $changes{'lockablenames'} = 1;
 6359:         }
 6360:     }
 6361:     foreach my $item (@{$types},'default') {
 6362:         if (defined($fields{$item})) {
 6363:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6364:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6365:                     my $change = 0;
 6366:                     if (ref($fields{$item}) eq 'ARRAY') {
 6367:                         foreach my $type (@{$fields{$item}}) {
 6368:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6369:                                 $change = 1;
 6370:                                 last;
 6371:                             }
 6372:                         }
 6373:                     }
 6374:                     if ($change) {
 6375:                         push(@{$changes{'fields'}},$item);
 6376:                     }
 6377:                 } else {
 6378:                     push(@{$changes{'fields'}},$item);
 6379:                 }
 6380:             } else {
 6381:                 push(@{$changes{'fields'}},$item);
 6382:             }
 6383:         }
 6384:     }
 6385:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6386:                                              $dom);
 6387:     if ($putresult eq 'ok') {
 6388:         if (keys(%changes) > 0) {
 6389:             $resulttext = &mt('Changes made:').'<ul>';
 6390:             foreach my $key (sort(keys(%changes))) {
 6391:                 if ($key eq 'lockablenames') {
 6392:                     $resulttext .= '<li>';
 6393:                     if (@lockablenames) {
 6394:                         $usertypes->{'default'} = $othertitle;
 6395:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6396:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6397:                     } else {
 6398:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6399:                     }
 6400:                     $resulttext .= '</li>';
 6401:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6402:                     foreach my $item (@{$changes{$key}}) {
 6403:                         my @newvalues;
 6404:                         foreach my $type (@{$fields{$item}}) {
 6405:                             push(@newvalues,$fieldtitles{$type});
 6406:                         }
 6407:                         my $newvaluestr;
 6408:                         if (@newvalues > 0) {
 6409:                             $newvaluestr = join(', ',@newvalues);
 6410:                         } else {
 6411:                             $newvaluestr = &mt('none');
 6412:                         }
 6413:                         if ($item eq 'default') {
 6414:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6415:                         } else {
 6416:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6417:                         }
 6418:                     }
 6419:                 } else {
 6420:                     my $newvalue;
 6421:                     if ($key eq 'run') {
 6422:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6423:                     } else {
 6424:                         $newvalue = $offon[$env{'form.'.$key}];
 6425:                     }
 6426:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6427:                 }
 6428:             }
 6429:             $resulttext .= '</ul>';
 6430:         } else {
 6431:             $resulttext = &mt('No changes made to autoupdates');
 6432:         }
 6433:     } else {
 6434:         $resulttext = '<span class="LC_error">'.
 6435: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6436:     }
 6437:     return $resulttext;
 6438: }
 6439: 
 6440: sub modify_autocreate {
 6441:     my ($dom,%domconfig) = @_;
 6442:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6443:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6444:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6445:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6446:         }
 6447:     }
 6448:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6449:                  req => 'Auto-creation of validated requests for official courses',
 6450:                  xmldc => 'Identity of course creator of courses from XML files',
 6451:                );
 6452:     my @types = ('xml','req');
 6453:     foreach my $item (@types) {
 6454:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6455:         $newvals{$item} =~ s/\D//g;
 6456:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6457:     }
 6458:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6459:     my %domcoords = &get_active_dcs($dom);
 6460:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6461:         $newvals{'xmldc'} = '';
 6462:     } 
 6463:     %autocreatehash =  (
 6464:                         autocreate => { xml => $newvals{'xml'},
 6465:                                         req => $newvals{'req'},
 6466:                                       }
 6467:                        );
 6468:     if ($newvals{'xmldc'} ne '') {
 6469:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6470:     }
 6471:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6472:                                              $dom);
 6473:     if ($putresult eq 'ok') {
 6474:         my @items = @types;
 6475:         if ($newvals{'xml'}) {
 6476:             push(@items,'xmldc');
 6477:         }
 6478:         foreach my $item (@items) {
 6479:             if (exists($currautocreate{$item})) {
 6480:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6481:                     $changes{$item} = 1;
 6482:                 }
 6483:             } elsif ($newvals{$item}) {
 6484:                 $changes{$item} = 1;
 6485:             }
 6486:         }
 6487:         if (keys(%changes) > 0) {
 6488:             my @offon = ('off','on'); 
 6489:             $resulttext = &mt('Changes made:').'<ul>';
 6490:             foreach my $item (@types) {
 6491:                 if ($changes{$item}) {
 6492:                     my $newtxt = $offon[$newvals{$item}];
 6493:                     $resulttext .= '<li>'.
 6494:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 6495:                                        '<b>','</b>').
 6496:                                    '</li>';
 6497:                 }
 6498:             }
 6499:             if ($changes{'xmldc'}) {
 6500:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6501:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6502:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 6503:             }
 6504:             $resulttext .= '</ul>';
 6505:         } else {
 6506:             $resulttext = &mt('No changes made to auto-creation settings');
 6507:         }
 6508:     } else {
 6509:         $resulttext = '<span class="LC_error">'.
 6510:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6511:     }
 6512:     return $resulttext;
 6513: }
 6514: 
 6515: sub modify_directorysrch {
 6516:     my ($dom,%domconfig) = @_;
 6517:     my ($resulttext,%changes);
 6518:     my %currdirsrch;
 6519:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6520:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6521:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6522:         }
 6523:     }
 6524:     my %title = ( available => 'Directory search available',
 6525:                   localonly => 'Other domains can search',
 6526:                   searchby => 'Search types',
 6527:                   searchtypes => 'Search latitude');
 6528:     my @offon = ('off','on');
 6529:     my @otherdoms = ('Yes','No');
 6530: 
 6531:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6532:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6533:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6534: 
 6535:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6536:     if (keys(%{$usertypes}) == 0) {
 6537:         @cansearch = ('default');
 6538:     } else {
 6539:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6540:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6541:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6542:                     push(@{$changes{'cansearch'}},$type);
 6543:                 }
 6544:             }
 6545:             foreach my $type (@cansearch) {
 6546:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6547:                     push(@{$changes{'cansearch'}},$type);
 6548:                 }
 6549:             }
 6550:         } else {
 6551:             push(@{$changes{'cansearch'}},@cansearch);
 6552:         }
 6553:     }
 6554: 
 6555:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6556:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6557:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6558:                 push(@{$changes{'searchby'}},$by);
 6559:             }
 6560:         }
 6561:         foreach my $by (@searchby) {
 6562:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6563:                 push(@{$changes{'searchby'}},$by);
 6564:             }
 6565:         }
 6566:     } else {
 6567:         push(@{$changes{'searchby'}},@searchby);
 6568:     }
 6569: 
 6570:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6571:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6572:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6573:                 push(@{$changes{'searchtypes'}},$type);
 6574:             }
 6575:         }
 6576:         foreach my $type (@searchtypes) {
 6577:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6578:                 push(@{$changes{'searchtypes'}},$type);
 6579:             }
 6580:         }
 6581:     } else {
 6582:         if (exists($currdirsrch{'searchtypes'})) {
 6583:             foreach my $type (@searchtypes) {  
 6584:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6585:                     push(@{$changes{'searchtypes'}},$type);
 6586:                 }
 6587:             }
 6588:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6589:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6590:             }   
 6591:         } else {
 6592:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6593:         }
 6594:     }
 6595: 
 6596:     my %dirsrch_hash =  (
 6597:             directorysrch => { available => $env{'form.dirsrch_available'},
 6598:                                cansearch => \@cansearch,
 6599:                                localonly => $env{'form.dirsrch_localonly'},
 6600:                                searchby => \@searchby,
 6601:                                searchtypes => \@searchtypes,
 6602:                              }
 6603:             );
 6604:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6605:                                              $dom);
 6606:     if ($putresult eq 'ok') {
 6607:         if (exists($currdirsrch{'available'})) {
 6608:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6609:                  $changes{'available'} = 1;
 6610:              }
 6611:         } else {
 6612:             if ($env{'form.dirsrch_available'} eq '1') {
 6613:                 $changes{'available'} = 1;
 6614:             }
 6615:         }
 6616:         if (exists($currdirsrch{'localonly'})) {
 6617:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6618:                  $changes{'localonly'} = 1;
 6619:              }
 6620:         } else {
 6621:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6622:                 $changes{'localonly'} = 1;
 6623:             }
 6624:         }
 6625:         if (keys(%changes) > 0) {
 6626:             $resulttext = &mt('Changes made:').'<ul>';
 6627:             if ($changes{'available'}) {
 6628:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6629:             }
 6630:             if ($changes{'localonly'}) {
 6631:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6632:             }
 6633: 
 6634:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6635:                 my $chgtext;
 6636:                 if (ref($usertypes) eq 'HASH') {
 6637:                     if (keys(%{$usertypes}) > 0) {
 6638:                         foreach my $type (@{$types}) {
 6639:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6640:                                 $chgtext .= $usertypes->{$type}.'; ';
 6641:                             }
 6642:                         }
 6643:                         if (grep(/^default$/,@cansearch)) {
 6644:                             $chgtext .= $othertitle;
 6645:                         } else {
 6646:                             $chgtext =~ s/\; $//;
 6647:                         }
 6648:                         $resulttext .=
 6649:                             '<li>'.
 6650:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 6651:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 6652:                             '</li>';
 6653:                     }
 6654:                 }
 6655:             }
 6656:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6657:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6658:                 my $chgtext;
 6659:                 foreach my $type (@{$titleorder}) {
 6660:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6661:                         if (defined($searchtitles->{$type})) {
 6662:                             $chgtext .= $searchtitles->{$type}.'; ';
 6663:                         }
 6664:                     }
 6665:                 }
 6666:                 $chgtext =~ s/\; $//;
 6667:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6668:             }
 6669:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6670:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6671:                 my $chgtext;
 6672:                 foreach my $type (@{$srchtypeorder}) {
 6673:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6674:                         if (defined($srchtypes_desc->{$type})) {
 6675:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6676:                         }
 6677:                     }
 6678:                 }
 6679:                 $chgtext =~ s/\; $//;
 6680:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 6681:             }
 6682:             $resulttext .= '</ul>';
 6683:         } else {
 6684:             $resulttext = &mt('No changes made to institution directory search settings');
 6685:         }
 6686:     } else {
 6687:         $resulttext = '<span class="LC_error">'.
 6688:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6689:     }
 6690:     return $resulttext;
 6691: }
 6692: 
 6693: sub modify_contacts {
 6694:     my ($dom,$lastactref,%domconfig) = @_;
 6695:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6696:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6697:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6698:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6699:         }
 6700:     }
 6701:     my (%others,%to,%bcc);
 6702:     my @contacts = ('supportemail','adminemail');
 6703:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6704:                     'requestsmail','updatesmail','idconflictsmail');
 6705:     my @toggles = ('reporterrors','reportupdates');
 6706:     foreach my $type (@mailings) {
 6707:         @{$newsetting{$type}} = 
 6708:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6709:         foreach my $item (@contacts) {
 6710:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6711:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6712:             } else {
 6713:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6714:             }
 6715:         }  
 6716:         $others{$type} = $env{'form.'.$type.'_others'};
 6717:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6718:         if ($type eq 'helpdeskmail') {
 6719:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6720:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6721:         }
 6722:     }
 6723:     foreach my $item (@contacts) {
 6724:         $to{$item} = $env{'form.'.$item};
 6725:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6726:     }
 6727:     foreach my $item (@toggles) {
 6728:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 6729:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 6730:         }
 6731:     }
 6732:     if (keys(%currsetting) > 0) {
 6733:         foreach my $item (@contacts) {
 6734:             if ($to{$item} ne $currsetting{$item}) {
 6735:                 $changes{$item} = 1;
 6736:             }
 6737:         }
 6738:         foreach my $type (@mailings) {
 6739:             foreach my $item (@contacts) {
 6740:                 if (ref($currsetting{$type}) eq 'HASH') {
 6741:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6742:                         push(@{$changes{$type}},$item);
 6743:                     }
 6744:                 } else {
 6745:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6746:                 }
 6747:             }
 6748:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6749:                 push(@{$changes{$type}},'others');
 6750:             }
 6751:             if ($type eq 'helpdeskmail') {   
 6752:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6753:                     push(@{$changes{$type}},'bcc'); 
 6754:                 }
 6755:             }
 6756:         }
 6757:     } else {
 6758:         my %default;
 6759:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6760:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6761:         $default{'errormail'} = 'adminemail';
 6762:         $default{'packagesmail'} = 'adminemail';
 6763:         $default{'helpdeskmail'} = 'supportemail';
 6764:         $default{'lonstatusmail'} = 'adminemail';
 6765:         $default{'requestsmail'} = 'adminemail';
 6766:         $default{'updatesmail'} = 'adminemail';
 6767:         foreach my $item (@contacts) {
 6768:            if ($to{$item} ne $default{$item}) {
 6769:               $changes{$item} = 1;
 6770:            }
 6771:         }
 6772:         foreach my $type (@mailings) {
 6773:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6774:                
 6775:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6776:             }
 6777:             if ($others{$type} ne '') {
 6778:                 push(@{$changes{$type}},'others');
 6779:             }
 6780:             if ($type eq 'helpdeskmail') {
 6781:                 if ($bcc{$type} ne '') {
 6782:                     push(@{$changes{$type}},'bcc');
 6783:                 }
 6784:             }
 6785:         }
 6786:     }
 6787:     foreach my $item (@toggles) {
 6788:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 6789:             $changes{$item} = 1;
 6790:         } elsif ((!$env{'form.'.$item}) &&
 6791:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 6792:             $changes{$item} = 1;
 6793:         }
 6794:     }
 6795:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6796:                                              $dom);
 6797:     if ($putresult eq 'ok') {
 6798:         if (keys(%changes) > 0) {
 6799:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6800:             if (ref($lastactref) eq 'HASH') {
 6801:                 $lastactref->{'domainconfig'} = 1;
 6802:             }
 6803:             my ($titles,$short_titles)  = &contact_titles();
 6804:             $resulttext = &mt('Changes made:').'<ul>';
 6805:             foreach my $item (@contacts) {
 6806:                 if ($changes{$item}) {
 6807:                     $resulttext .= '<li>'.$titles->{$item}.
 6808:                                     &mt(' set to: ').
 6809:                                     '<span class="LC_cusr_emph">'.
 6810:                                     $to{$item}.'</span></li>';
 6811:                 }
 6812:             }
 6813:             foreach my $type (@mailings) {
 6814:                 if (ref($changes{$type}) eq 'ARRAY') {
 6815:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6816:                     my @text;
 6817:                     foreach my $item (@{$newsetting{$type}}) {
 6818:                         push(@text,$short_titles->{$item});
 6819:                     }
 6820:                     if ($others{$type} ne '') {
 6821:                         push(@text,$others{$type});
 6822:                     }
 6823:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6824:                                    join(', ',@text).'</span>';
 6825:                     if ($type eq 'helpdeskmail') {
 6826:                         if ($bcc{$type} ne '') {
 6827:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6828:                         }
 6829:                     }
 6830:                     $resulttext .= '</li>';
 6831:                 }
 6832:             }
 6833:             my @offon = ('off','on');
 6834:             if ($changes{'reporterrors'}) {
 6835:                 $resulttext .= '<li>'.
 6836:                                &mt('E-mail error reports to [_1] set to "'.
 6837:                                    $offon[$env{'form.reporterrors'}].'".',
 6838:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 6839:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 6840:                                '</li>';
 6841:             }
 6842:             if ($changes{'reportupdates'}) {
 6843:                 $resulttext .= '<li>'.
 6844:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 6845:                                     $offon[$env{'form.reportupdates'}].'".',
 6846:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 6847:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 6848:                                 '</li>';
 6849:             }
 6850:             $resulttext .= '</ul>';
 6851:         } else {
 6852:             $resulttext = &mt('No changes made to contact information');
 6853:         }
 6854:     } else {
 6855:         $resulttext = '<span class="LC_error">'.
 6856:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6857:     }
 6858:     return $resulttext;
 6859: }
 6860: 
 6861: sub modify_usercreation {
 6862:     my ($dom,%domconfig) = @_;
 6863:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6864:     my $warningmsg;
 6865:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6866:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6867:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6868:         }
 6869:     }
 6870:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6871:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6872:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6873:     my @contexts = ('author','course','requestcrs','selfcreate');
 6874:     foreach my $item(@contexts) {
 6875:         if ($item eq 'selfcreate') {
 6876:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6877:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6878:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6879:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6880:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6881:                         $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.');   
 6882:                     }
 6883:                 }
 6884:             }
 6885:         } else {
 6886:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6887:         }
 6888:     }
 6889:     my ($othertitle,$usertypes,$types) = 
 6890:         &Apache::loncommon::sorted_inst_types($dom);
 6891:     if (ref($types) eq 'ARRAY') {
 6892:         if (@{$types} > 0) {
 6893:             @{$cancreate{'statustocreate'}} = 
 6894:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6895:         } else {
 6896:             @{$cancreate{'statustocreate'}} = ();
 6897:         }
 6898:         push(@contexts,'statustocreate');
 6899:     }
 6900:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6901:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6902:         foreach my $item (@contexts) {
 6903:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6904:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6905:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6906:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6907:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6908:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6909:                                     push(@{$changes{'cancreate'}},$item);
 6910:                                 }
 6911:                             }
 6912:                         }
 6913:                     }
 6914:                 } else {
 6915:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6916:                         if (@{$cancreate{$item}} > 0) {
 6917:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6918:                                 push(@{$changes{'cancreate'}},$item);
 6919:                             }
 6920:                         }
 6921:                     } else {
 6922:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6923:                             if (@{$cancreate{$item}} < 3) {
 6924:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6925:                                     push(@{$changes{'cancreate'}},$item);
 6926:                                 }
 6927:                             }
 6928:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6929:                             if (@{$cancreate{$item}} > 0) {
 6930:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6931:                                     push(@{$changes{'cancreate'}},$item);
 6932:                                 }
 6933:                             }
 6934:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6935:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6936:                                 push(@{$changes{'cancreate'}},$item);
 6937:                             }
 6938:                         }
 6939:                     }
 6940:                 }
 6941:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6942:                     foreach my $type (@{$cancreate{$item}}) {
 6943:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6944:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6945:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6946:                                     push(@{$changes{'cancreate'}},$item);
 6947:                                 }
 6948:                             }
 6949:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6950:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6951:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6952:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6953:                                     push(@{$changes{'cancreate'}},$item);
 6954:                                 }
 6955:                             }
 6956:                         }
 6957:                     }
 6958:                 }
 6959:             } else {
 6960:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6961:                     push(@{$changes{'cancreate'}},$item);
 6962:                 }
 6963:             }
 6964:         }
 6965:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6966:         foreach my $item (@contexts) {
 6967:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6968:                 if ($cancreate{$item} ne 'any') {
 6969:                     push(@{$changes{'cancreate'}},$item);
 6970:                 }
 6971:             } else {
 6972:                 if ($cancreate{$item} ne 'none') {
 6973:                     push(@{$changes{'cancreate'}},$item);
 6974:                 }
 6975:             }
 6976:         }
 6977:     } else {
 6978:         foreach my $item (@contexts)  {
 6979:             push(@{$changes{'cancreate'}},$item);
 6980:         }
 6981:     }
 6982: 
 6983:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6984:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6985:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6986:                 push(@{$changes{'username_rule'}},$type);
 6987:             }
 6988:         }
 6989:         foreach my $type (@username_rule) {
 6990:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6991:                 push(@{$changes{'username_rule'}},$type);
 6992:             }
 6993:         }
 6994:     } else {
 6995:         push(@{$changes{'username_rule'}},@username_rule);
 6996:     }
 6997: 
 6998:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6999:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 7000:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 7001:                 push(@{$changes{'id_rule'}},$type);
 7002:             }
 7003:         }
 7004:         foreach my $type (@id_rule) {
 7005:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 7006:                 push(@{$changes{'id_rule'}},$type);
 7007:             }
 7008:         }
 7009:     } else {
 7010:         push(@{$changes{'id_rule'}},@id_rule);
 7011:     }
 7012: 
 7013:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 7014:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 7015:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 7016:                 push(@{$changes{'email_rule'}},$type);
 7017:             }
 7018:         }
 7019:         foreach my $type (@email_rule) {
 7020:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 7021:                 push(@{$changes{'email_rule'}},$type);
 7022:             }
 7023:         }
 7024:     } else {
 7025:         push(@{$changes{'email_rule'}},@email_rule);
 7026:     }
 7027: 
 7028:     my @authen_contexts = ('author','course','domain');
 7029:     my @authtypes = ('int','krb4','krb5','loc');
 7030:     my %authhash;
 7031:     foreach my $item (@authen_contexts) {
 7032:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 7033:         foreach my $auth (@authtypes) {
 7034:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 7035:                 $authhash{$item}{$auth} = 1;
 7036:             } else {
 7037:                 $authhash{$item}{$auth} = 0;
 7038:             }
 7039:         }
 7040:     }
 7041:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 7042:         foreach my $item (@authen_contexts) {
 7043:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 7044:                 foreach my $auth (@authtypes) {
 7045:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 7046:                         push(@{$changes{'authtypes'}},$item);
 7047:                         last;
 7048:                     }
 7049:                 }
 7050:             }
 7051:         }
 7052:     } else {
 7053:         foreach my $item (@authen_contexts) {
 7054:             push(@{$changes{'authtypes'}},$item);
 7055:         }
 7056:     }
 7057: 
 7058:     my %usercreation_hash =  (
 7059:             usercreation => {
 7060:                               cancreate     => \%cancreate,
 7061:                               username_rule => \@username_rule,
 7062:                               id_rule       => \@id_rule,
 7063:                               email_rule    => \@email_rule,
 7064:                               authtypes     => \%authhash,
 7065:                             }
 7066:             );
 7067: 
 7068:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 7069:                                              $dom);
 7070: 
 7071:     my %selfcreatetypes = (
 7072:                              sso   => 'users authenticated by institutional single sign on',
 7073:                              login => 'users authenticated by institutional log-in',
 7074:                              email => 'users who provide a valid e-mail address for use as the username',
 7075:                           );
 7076:     if ($putresult eq 'ok') {
 7077:         if (keys(%changes) > 0) {
 7078:             $resulttext = &mt('Changes made:').'<ul>';
 7079:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 7080:                 my %lt = &usercreation_types();
 7081:                 foreach my $type (@{$changes{'cancreate'}}) {
 7082:                     my $chgtext;
 7083:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 7084:                         $chgtext = $lt{$type}.', ';
 7085:                     }
 7086:                     if ($type eq 'selfcreate') {
 7087:                         if (@{$cancreate{$type}} == 0) {
 7088:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 7089:                         } else {
 7090:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 7091:                             foreach my $case (@{$cancreate{$type}}) {
 7092:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 7093:                             }
 7094:                             $chgtext .= '</ul>';
 7095:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 7096:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 7097:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 7098:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 7099:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 7100:                                         }
 7101:                                     }
 7102:                                 }
 7103:                             }
 7104:                         }
 7105:                     } elsif ($type eq 'statustocreate') {
 7106:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 7107:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 7108:                             if (@{$cancreate{'selfcreate'}} > 0) {
 7109:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 7110: 
 7111:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 7112:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 7113:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 7114:                                     } 
 7115:                                 } elsif (ref($usertypes) eq 'HASH') {
 7116:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 7117:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 7118:                                     } else {
 7119:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 7120:                                     }
 7121:                                     $chgtext .= '<ul>';
 7122:                                     foreach my $case (@{$cancreate{$type}}) {
 7123:                                         if ($case eq 'default') {
 7124:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 7125:                                         } else {
 7126:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 7127:                                         }
 7128:                                     }
 7129:                                     $chgtext .= '</ul>';
 7130:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 7131:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 7132:                                     }
 7133:                                 }
 7134:                             } else {
 7135:                                 if (@{$cancreate{$type}} == 0) {
 7136:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 7137:                                 } else {
 7138:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 7139:                                 }
 7140:                             }
 7141:                         }
 7142:                     } elsif ($type eq 'captcha') {
 7143:                         if ($cancreate{$type} eq 'notused') {
 7144:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 7145:                         } else {
 7146:                             my %captchas = &captcha_phrases();
 7147:                             if ($captchas{$cancreate{$type}}) {
 7148:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 7149:                             } else {
 7150:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 7151:                             }
 7152:                         }
 7153:                     } elsif ($type eq 'recaptchakeys') {
 7154:                         my ($privkey,$pubkey);
 7155:                         if (ref($cancreate{$type}) eq 'HASH') {
 7156:                             $pubkey = $cancreate{$type}{'public'};
 7157:                             $privkey = $cancreate{$type}{'private'};
 7158:                         }
 7159:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 7160:                         if (!$pubkey) {
 7161:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 7162:                         } else {
 7163:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7164:                         }
 7165:                         if (!$privkey) {
 7166:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 7167:                         } else {
 7168:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 7169:                         }
 7170:                         $chgtext .= '</ul>';
 7171:                     } else {
 7172:                         if ($cancreate{$type} eq 'none') {
 7173:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 7174:                         } elsif ($cancreate{$type} eq 'any') {
 7175:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 7176:                         } elsif ($cancreate{$type} eq 'official') {
 7177:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 7178:                         } elsif ($cancreate{$type} eq 'unofficial') {
 7179:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 7180:                         }
 7181:                     }
 7182:                     $resulttext .= '<li>'.$chgtext.'</li>';
 7183:                 }
 7184:             }
 7185:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 7186:                 my ($rules,$ruleorder) = 
 7187:                     &Apache::lonnet::inst_userrules($dom,'username');
 7188:                 my $chgtext = '<ul>';
 7189:                 foreach my $type (@username_rule) {
 7190:                     if (ref($rules->{$type}) eq 'HASH') {
 7191:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 7192:                     }
 7193:                 }
 7194:                 $chgtext .= '</ul>';
 7195:                 if (@username_rule > 0) {
 7196:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 7197:                 } else {
 7198:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 7199:                 }
 7200:             }
 7201:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 7202:                 my ($idrules,$idruleorder) = 
 7203:                     &Apache::lonnet::inst_userrules($dom,'id');
 7204:                 my $chgtext = '<ul>';
 7205:                 foreach my $type (@id_rule) {
 7206:                     if (ref($idrules->{$type}) eq 'HASH') {
 7207:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 7208:                     }
 7209:                 }
 7210:                 $chgtext .= '</ul>';
 7211:                 if (@id_rule > 0) {
 7212:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 7213:                 } else {
 7214:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 7215:                 }
 7216:             }
 7217:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 7218:                 my ($emailrules,$emailruleorder) =
 7219:                     &Apache::lonnet::inst_userrules($dom,'email');
 7220:                 my $chgtext = '<ul>';
 7221:                 foreach my $type (@email_rule) {
 7222:                     if (ref($emailrules->{$type}) eq 'HASH') {
 7223:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 7224:                     }
 7225:                 }
 7226:                 $chgtext .= '</ul>';
 7227:                 if (@email_rule > 0) {
 7228:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 7229:                 } else {
 7230:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 7231:                 }
 7232:             }
 7233: 
 7234:             my %authname = &authtype_names();
 7235:             my %context_title = &context_names();
 7236:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7237:                 my $chgtext = '<ul>';
 7238:                 foreach my $type (@{$changes{'authtypes'}}) {
 7239:                     my @allowed;
 7240:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7241:                     foreach my $auth (@authtypes) {
 7242:                         if ($authhash{$type}{$auth}) {
 7243:                             push(@allowed,$authname{$auth});
 7244:                         }
 7245:                     }
 7246:                     if (@allowed > 0) {
 7247:                         $chgtext .= join(', ',@allowed).'</li>';
 7248:                     } else {
 7249:                         $chgtext .= &mt('none').'</li>';
 7250:                     }
 7251:                 }
 7252:                 $chgtext .= '</ul>';
 7253:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7254:                 $resulttext .= '</li>';
 7255:             }
 7256:             $resulttext .= '</ul>';
 7257:         } else {
 7258:             $resulttext = &mt('No changes made to user creation settings');
 7259:         }
 7260:     } else {
 7261:         $resulttext = '<span class="LC_error">'.
 7262:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7263:     }
 7264:     if ($warningmsg ne '') {
 7265:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7266:     }
 7267:     return $resulttext;
 7268: }
 7269: 
 7270: sub process_captcha {
 7271:     my ($container,$changes,$newsettings,$current) = @_;
 7272:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 7273:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 7274:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 7275:         $newsettings->{'captcha'} = 'original';
 7276:     }
 7277:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 7278:         if ($container eq 'cancreate') {
 7279:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7280:                 push(@{$changes->{'cancreate'}},'captcha');
 7281:             } elsif (!defined($changes->{'cancreate'})) {
 7282:                 $changes->{'cancreate'} = ['captcha'];
 7283:             }
 7284:         } else {
 7285:             $changes->{'captcha'} = 1;
 7286:         }
 7287:     }
 7288:     my ($newpub,$newpriv,$currpub,$currpriv);
 7289:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 7290:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 7291:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 7292:         $newpub =~ s/\W//g;
 7293:         $newpriv =~ s/\W//g;
 7294:         $newsettings->{'recaptchakeys'} = {
 7295:                                              public  => $newpub,
 7296:                                              private => $newpriv,
 7297:                                           };
 7298:     }
 7299:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 7300:         $currpub = $current->{'recaptchakeys'}{'public'};
 7301:         $currpriv = $current->{'recaptchakeys'}{'private'};
 7302:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 7303:             $newsettings->{'recaptchakeys'} = {
 7304:                                                  public  => '',
 7305:                                                  private => '',
 7306:                                               }
 7307:         }
 7308:     }
 7309:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 7310:         if ($container eq 'cancreate') {
 7311:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7312:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 7313:             } elsif (!defined($changes->{'cancreate'})) {
 7314:                 $changes->{'cancreate'} = ['recaptchakeys'];
 7315:             }
 7316:         } else {
 7317:             $changes->{'recaptchakeys'} = 1;
 7318:         }
 7319:     }
 7320:     return;
 7321: }
 7322: 
 7323: sub modify_usermodification {
 7324:     my ($dom,%domconfig) = @_;
 7325:     my ($resulttext,%curr_usermodification,%changes);
 7326:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7327:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7328:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 7329:         }
 7330:     }
 7331:     my @contexts = ('author','course','selfcreate');
 7332:     my %context_title = (
 7333:                            author => 'In author context',
 7334:                            course => 'In course context',
 7335:                            selfcreate => 'When self creating account', 
 7336:                         );
 7337:     my @fields = ('lastname','firstname','middlename','generation',
 7338:                   'permanentemail','id');
 7339:     my %roles = (
 7340:                   author => ['ca','aa'],
 7341:                   course => ['st','ep','ta','in','cr'],
 7342:                 );
 7343:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7344:     if (ref($types) eq 'ARRAY') {
 7345:         push(@{$types},'default');
 7346:         $usertypes->{'default'} = $othertitle;
 7347:     }
 7348:     $roles{'selfcreate'} = $types;  
 7349:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7350:     my %modifyhash;
 7351:     foreach my $context (@contexts) {
 7352:         foreach my $role (@{$roles{$context}}) {
 7353:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7354:             foreach my $item (@fields) {
 7355:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7356:                     $modifyhash{$context}{$role}{$item} = 1;
 7357:                 } else {
 7358:                     $modifyhash{$context}{$role}{$item} = 0;
 7359:                 }
 7360:             }
 7361:         }
 7362:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7363:             foreach my $role (@{$roles{$context}}) {
 7364:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7365:                     foreach my $field (@fields) {
 7366:                         if ($modifyhash{$context}{$role}{$field} ne 
 7367:                                 $curr_usermodification{$context}{$role}{$field}) {
 7368:                             push(@{$changes{$context}},$role);
 7369:                             last;
 7370:                         }
 7371:                     }
 7372:                 }
 7373:             }
 7374:         } else {
 7375:             foreach my $context (@contexts) {
 7376:                 foreach my $role (@{$roles{$context}}) {
 7377:                     push(@{$changes{$context}},$role);
 7378:                 }
 7379:             }
 7380:         }
 7381:     }
 7382:     my %usermodification_hash =  (
 7383:                                    usermodification => \%modifyhash,
 7384:                                  );
 7385:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7386:                                              \%usermodification_hash,$dom);
 7387:     if ($putresult eq 'ok') {
 7388:         if (keys(%changes) > 0) {
 7389:             $resulttext = &mt('Changes made: ').'<ul>';
 7390:             foreach my $context (@contexts) {
 7391:                 if (ref($changes{$context}) eq 'ARRAY') {
 7392:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7393:                     if (ref($changes{$context}) eq 'ARRAY') {
 7394:                         foreach my $role (@{$changes{$context}}) {
 7395:                             my $rolename;
 7396:                             if ($context eq 'selfcreate') {
 7397:                                 $rolename = $role;
 7398:                                 if (ref($usertypes) eq 'HASH') {
 7399:                                     if ($usertypes->{$role} ne '') {
 7400:                                         $rolename = $usertypes->{$role};
 7401:                                     }
 7402:                                 }
 7403:                             } else {
 7404:                                 if ($role eq 'cr') {
 7405:                                     $rolename = &mt('Custom');
 7406:                                 } else {
 7407:                                     $rolename = &Apache::lonnet::plaintext($role);
 7408:                                 }
 7409:                             }
 7410:                             my @modifiable;
 7411:                             if ($context eq 'selfcreate') {
 7412:                                 $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): ');
 7413:                             } else {
 7414:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7415:                             }
 7416:                             foreach my $field (@fields) {
 7417:                                 if ($modifyhash{$context}{$role}{$field}) {
 7418:                                     push(@modifiable,$fieldtitles{$field});
 7419:                                 }
 7420:                             }
 7421:                             if (@modifiable > 0) {
 7422:                                 $resulttext .= join(', ',@modifiable);
 7423:                             } else {
 7424:                                 $resulttext .= &mt('none'); 
 7425:                             }
 7426:                             $resulttext .= '</li>';
 7427:                         }
 7428:                         $resulttext .= '</ul></li>';
 7429:                     }
 7430:                 }
 7431:             }
 7432:             $resulttext .= '</ul>';
 7433:         } else {
 7434:             $resulttext = &mt('No changes made to user modification settings');
 7435:         }
 7436:     } else {
 7437:         $resulttext = '<span class="LC_error">'.
 7438:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7439:     }
 7440:     return $resulttext;
 7441: }
 7442: 
 7443: sub modify_defaults {
 7444:     my ($dom,$lastactref,%domconfig) = @_;
 7445:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7446:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7447:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7448:     my @authtypes = ('internal','krb4','krb5','localauth');
 7449:     foreach my $item (@items) {
 7450:         $newvalues{$item} = $env{'form.'.$item};
 7451:         if ($item eq 'auth_def') {
 7452:             if ($newvalues{$item} ne '') {
 7453:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7454:                     push(@errors,$item);
 7455:                 }
 7456:             }
 7457:         } elsif ($item eq 'lang_def') {
 7458:             if ($newvalues{$item} ne '') {
 7459:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7460:                     my $langcode = $1;
 7461:                     if ($langcode ne 'x_chef') {
 7462:                         if (code2language($langcode) eq '') {
 7463:                             push(@errors,$item);
 7464:                         }
 7465:                     }
 7466:                 } else {
 7467:                     push(@errors,$item);
 7468:                 }
 7469:             }
 7470:         } elsif ($item eq 'timezone_def') {
 7471:             if ($newvalues{$item} ne '') {
 7472:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7473:                     push(@errors,$item);   
 7474:                 }
 7475:             }
 7476:         } elsif ($item eq 'datelocale_def') {
 7477:             if ($newvalues{$item} ne '') {
 7478:                 my @datelocale_ids = DateTime::Locale->ids();
 7479:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7480:                     push(@errors,$item);
 7481:                 }
 7482:             }
 7483:         } elsif ($item eq 'portal_def') {
 7484:             if ($newvalues{$item} ne '') {
 7485:                 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])\/?$/) {
 7486:                     push(@errors,$item);
 7487:                 }
 7488:             }
 7489:         }
 7490:         if (grep(/^\Q$item\E$/,@errors)) {
 7491:             $newvalues{$item} = $domdefaults{$item};
 7492:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7493:             $changes{$item} = 1;
 7494:         }
 7495:         $domdefaults{$item} = $newvalues{$item};
 7496:     }
 7497:     my %defaults_hash = (
 7498:                          defaults => \%newvalues,
 7499:                         );
 7500:     my $title = &defaults_titles();
 7501:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7502:                                              $dom);
 7503:     if ($putresult eq 'ok') {
 7504:         if (keys(%changes) > 0) {
 7505:             $resulttext = &mt('Changes made:').'<ul>';
 7506:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 7507:             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";
 7508:             foreach my $item (sort(keys(%changes))) {
 7509:                 my $value = $env{'form.'.$item};
 7510:                 if ($value eq '') {
 7511:                     $value = &mt('none');
 7512:                 } elsif ($item eq 'auth_def') {
 7513:                     my %authnames = &authtype_names();
 7514:                     my %shortauth = (
 7515:                              internal => 'int',
 7516:                              krb4 => 'krb4',
 7517:                              krb5 => 'krb5',
 7518:                              localauth  => 'loc',
 7519:                     );
 7520:                     $value = $authnames{$shortauth{$value}};
 7521:                 }
 7522:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7523:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7524:             }
 7525:             $resulttext .= '</ul>';
 7526:             $mailmsgtext .= "\n";
 7527:             my $cachetime = 24*60*60;
 7528:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7529:             if (ref($lastactref) eq 'HASH') {
 7530:                 $lastactref->{'domdefaults'} = 1;
 7531:             }
 7532:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7533:                 my $notify = 1;
 7534:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 7535:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 7536:                         $notify = 0;
 7537:                     }
 7538:                 }
 7539:                 if ($notify) {
 7540:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 7541:                                                "LON-CAPA Domain Settings Change - $dom",
 7542:                                                $mailmsgtext);
 7543:                 }
 7544:             }
 7545:         } else {
 7546:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 7547:         }
 7548:     } else {
 7549:         $resulttext = '<span class="LC_error">'.
 7550:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7551:     }
 7552:     if (@errors > 0) {
 7553:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7554:         foreach my $item (@errors) {
 7555:             $resulttext .= ' "'.$title->{$item}.'",';
 7556:         }
 7557:         $resulttext =~ s/,$//;
 7558:     }
 7559:     return $resulttext;
 7560: }
 7561: 
 7562: sub modify_scantron {
 7563:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7564:     my ($resulttext,%confhash,%changes,$errors);
 7565:     my $custom = 'custom.tab';
 7566:     my $default = 'default.tab';
 7567:     my $servadm = $r->dir_config('lonAdmEMail');
 7568:     my ($configuserok,$author_ok,$switchserver) = 
 7569:         &config_check($dom,$confname,$servadm);
 7570:     if ($env{'form.scantronformat.filename'} ne '') {
 7571:         my $error;
 7572:         if ($configuserok eq 'ok') {
 7573:             if ($switchserver) {
 7574:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7575:             } else {
 7576:                 if ($author_ok eq 'ok') {
 7577:                     my ($result,$scantronurl) =
 7578:                         &publishlogo($r,'upload','scantronformat',$dom,
 7579:                                      $confname,'scantron','','',$custom);
 7580:                     if ($result eq 'ok') {
 7581:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7582:                         $changes{'scantronformat'} = 1;
 7583:                     } else {
 7584:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7585:                     }
 7586:                 } else {
 7587:                     $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);
 7588:                 }
 7589:             }
 7590:         } else {
 7591:             $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);
 7592:         }
 7593:         if ($error) {
 7594:             &Apache::lonnet::logthis($error);
 7595:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7596:         }
 7597:     }
 7598:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7599:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7600:             if ($env{'form.scantronformat_del'}) {
 7601:                 $confhash{'scantron'}{'scantronformat'} = '';
 7602:                 $changes{'scantronformat'} = 1;
 7603:             }
 7604:         }
 7605:     }
 7606:     if (keys(%confhash) > 0) {
 7607:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7608:                                                  $dom);
 7609:         if ($putresult eq 'ok') {
 7610:             if (keys(%changes) > 0) {
 7611:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7612:                     $resulttext = &mt('Changes made:').'<ul>';
 7613:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7614:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7615:                     } else {
 7616:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7617:                     }
 7618:                     $resulttext .= '</ul>';
 7619:                 } else {
 7620:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7621:                 }
 7622:                 $resulttext .= '</ul>';
 7623:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7624:                 if (ref($lastactref) eq 'HASH') {
 7625:                     $lastactref->{'domainconfig'} = 1;
 7626:                 }
 7627:             } else {
 7628:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7629:             }
 7630:         } else {
 7631:             $resulttext = '<span class="LC_error">'.
 7632:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7633:         }
 7634:     } else {
 7635:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7636:     }
 7637:     if ($errors) {
 7638:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7639:                        $errors.'</ul>';
 7640:     }
 7641:     return $resulttext;
 7642: }
 7643: 
 7644: sub modify_coursecategories {
 7645:     my ($dom,%domconfig) = @_;
 7646:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7647:         $cathash);
 7648:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7649:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7650:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7651:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7652:             $changes{'togglecats'} = 1;
 7653:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7654:         }
 7655:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7656:             $changes{'categorize'} = 1;
 7657:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7658:         }
 7659:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7660:             $changes{'togglecatscomm'} = 1;
 7661:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7662:         }
 7663:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7664:             $changes{'categorizecomm'} = 1;
 7665:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7666:         }
 7667:     } else {
 7668:         $changes{'togglecats'} = 1;
 7669:         $changes{'categorize'} = 1;
 7670:         $changes{'togglecatscomm'} = 1;
 7671:         $changes{'categorizecomm'} = 1;
 7672:         $domconfig{'coursecategories'} = {
 7673:                                              togglecats => $env{'form.togglecats'},
 7674:                                              categorize => $env{'form.categorize'},
 7675:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7676:                                              categorizecomm => $env{'form.categorizecomm'},
 7677:                                          };
 7678:     }
 7679:     if (ref($cathash) eq 'HASH') {
 7680:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7681:             push (@deletecategory,'instcode::0');
 7682:         }
 7683:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7684:             push(@deletecategory,'communities::0');
 7685:         }
 7686:     }
 7687:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7688:     if (ref($cathash) eq 'HASH') {
 7689:         if (@deletecategory > 0) {
 7690:             #FIXME Need to remove category from all courses using a deleted category 
 7691:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7692:             foreach my $item (@deletecategory) {
 7693:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7694:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7695:                     $deletions{$item} = 1;
 7696:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7697:                 }
 7698:             }
 7699:         }
 7700:         foreach my $item (keys(%{$cathash})) {
 7701:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7702:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7703:                 $reorderings{$item} = 1;
 7704:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7705:             }
 7706:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7707:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7708:                 my $newdepth = $depth+1;
 7709:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7710:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7711:                 $adds{$newitem} = 1; 
 7712:             }
 7713:             if ($env{'form.subcat_'.$item} ne '') {
 7714:                 my $newcat = $env{'form.subcat_'.$item};
 7715:                 my $newdepth = $depth+1;
 7716:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7717:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7718:                 $adds{$newitem} = 1;
 7719:             }
 7720:         }
 7721:     }
 7722:     if ($env{'form.instcode'} eq '1') {
 7723:         if (ref($cathash) eq 'HASH') {
 7724:             my $newitem = 'instcode::0';
 7725:             if ($cathash->{$newitem} eq '') {  
 7726:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7727:                 $adds{$newitem} = 1;
 7728:             }
 7729:         } else {
 7730:             my $newitem = 'instcode::0';
 7731:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7732:             $adds{$newitem} = 1;
 7733:         }
 7734:     }
 7735:     if ($env{'form.communities'} eq '1') {
 7736:         if (ref($cathash) eq 'HASH') {
 7737:             my $newitem = 'communities::0';
 7738:             if ($cathash->{$newitem} eq '') {
 7739:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7740:                 $adds{$newitem} = 1;
 7741:             }
 7742:         } else {
 7743:             my $newitem = 'communities::0';
 7744:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7745:             $adds{$newitem} = 1;
 7746:         }
 7747:     }
 7748:     if ($env{'form.addcategory_name'} ne '') {
 7749:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7750:             ($env{'form.addcategory_name'} ne 'communities')) {
 7751:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7752:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7753:             $adds{$newitem} = 1;
 7754:         }
 7755:     }
 7756:     my $putresult;
 7757:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7758:         if (keys(%deletions) > 0) {
 7759:             foreach my $key (keys(%deletions)) {
 7760:                 if ($predelallitems{$key} ne '') {
 7761:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7762:                 }
 7763:             }
 7764:         }
 7765:         my (@chkcats,@chktrails,%chkallitems);
 7766:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7767:         if (ref($chkcats[0]) eq 'ARRAY') {
 7768:             my $depth = 0;
 7769:             my $chg = 0;
 7770:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7771:                 my $name = $chkcats[0][$i];
 7772:                 my $item;
 7773:                 if ($name eq '') {
 7774:                     $chg ++;
 7775:                 } else {
 7776:                     $item = &escape($name).'::0';
 7777:                     if ($chg) {
 7778:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7779:                     }
 7780:                     $depth ++; 
 7781:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7782:                     $depth --;
 7783:                 }
 7784:             }
 7785:         }
 7786:     }
 7787:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7788:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7789:         if ($putresult eq 'ok') {
 7790:             my %title = (
 7791:                          togglecats     => 'Show/Hide a course in catalog',
 7792:                          categorize     => 'Assign a category to a course',
 7793:                          togglecatscomm => 'Show/Hide a community in catalog',
 7794:                          categorizecomm => 'Assign a category to a community',
 7795:                         );
 7796:             my %level = (
 7797:                          dom  => 'set in Domain ("Modify Course/Community")',
 7798:                          crs  => 'set in Course ("Course Configuration")',
 7799:                          comm => 'set in Community ("Community Configuration")',
 7800:                         );
 7801:             $resulttext = &mt('Changes made:').'<ul>';
 7802:             if ($changes{'togglecats'}) {
 7803:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7804:             }
 7805:             if ($changes{'categorize'}) {
 7806:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7807:             }
 7808:             if ($changes{'togglecatscomm'}) {
 7809:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7810:             }
 7811:             if ($changes{'categorizecomm'}) {
 7812:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7813:             }
 7814:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7815:                 my $cathash;
 7816:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7817:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7818:                 } else {
 7819:                     $cathash = {};
 7820:                 } 
 7821:                 my (@cats,@trails,%allitems);
 7822:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7823:                 if (keys(%deletions) > 0) {
 7824:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7825:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7826:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7827:                     }
 7828:                     $resulttext .= '</ul></li>';
 7829:                 }
 7830:                 if (keys(%reorderings) > 0) {
 7831:                     my %sort_by_trail;
 7832:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7833:                     foreach my $key (keys(%reorderings)) {
 7834:                         if ($allitems{$key} ne '') {
 7835:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7836:                         }
 7837:                     }
 7838:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7839:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7840:                     }
 7841:                     $resulttext .= '</ul></li>';
 7842:                 }
 7843:                 if (keys(%adds) > 0) {
 7844:                     my %sort_by_trail;
 7845:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7846:                     foreach my $key (keys(%adds)) {
 7847:                         if ($allitems{$key} ne '') {
 7848:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7849:                         }
 7850:                     }
 7851:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7852:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7853:                     }
 7854:                     $resulttext .= '</ul></li>';
 7855:                 }
 7856:             }
 7857:             $resulttext .= '</ul>';
 7858:         } else {
 7859:             $resulttext = '<span class="LC_error">'.
 7860:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7861:         }
 7862:     } else {
 7863:         $resulttext = &mt('No changes made to course and community categories');
 7864:     }
 7865:     return $resulttext;
 7866: }
 7867: 
 7868: sub modify_serverstatuses {
 7869:     my ($dom,%domconfig) = @_;
 7870:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7871:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7872:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7873:     }
 7874:     my @pages = &serverstatus_pages();
 7875:     foreach my $type (@pages) {
 7876:         $newserverstatus{$type}{'namedusers'} = '';
 7877:         $newserverstatus{$type}{'machines'} = '';
 7878:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7879:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7880:             my @okusers;
 7881:             foreach my $user (@users) {
 7882:                 my ($uname,$udom) = split(/:/,$user);
 7883:                 if (($udom =~ /^$match_domain$/) &&   
 7884:                     (&Apache::lonnet::domain($udom)) &&
 7885:                     ($uname =~ /^$match_username$/)) {
 7886:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7887:                         push(@okusers,$user);
 7888:                     }
 7889:                 }
 7890:             }
 7891:             if (@okusers > 0) {
 7892:                  @okusers = sort(@okusers);
 7893:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7894:             }
 7895:         }
 7896:         if (defined($env{'form.'.$type.'_machines'})) {
 7897:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7898:             my @okmachines;
 7899:             foreach my $ip (@machines) {
 7900:                 my @parts = split(/\./,$ip);
 7901:                 next if (@parts < 4);
 7902:                 my $badip = 0;
 7903:                 for (my $i=0; $i<4; $i++) {
 7904:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7905:                         $badip = 1;
 7906:                         last;
 7907:                     }
 7908:                 }
 7909:                 if (!$badip) {
 7910:                     push(@okmachines,$ip);     
 7911:                 }
 7912:             }
 7913:             @okmachines = sort(@okmachines);
 7914:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7915:         }
 7916:     }
 7917:     my %serverstatushash =  (
 7918:                                 serverstatuses => \%newserverstatus,
 7919:                             );
 7920:     foreach my $type (@pages) {
 7921:         foreach my $setting ('namedusers','machines') {
 7922:             my (@current,@new);
 7923:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7924:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7925:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7926:                 }
 7927:             }
 7928:             if ($newserverstatus{$type}{$setting} ne '') {
 7929:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7930:             }
 7931:             if (@current > 0) {
 7932:                 if (@new > 0) {
 7933:                     foreach my $item (@current) {
 7934:                         if (!grep(/^\Q$item\E$/,@new)) {
 7935:                             $changes{$type}{$setting} = 1;
 7936:                             last;
 7937:                         }
 7938:                     }
 7939:                     foreach my $item (@new) {
 7940:                         if (!grep(/^\Q$item\E$/,@current)) {
 7941:                             $changes{$type}{$setting} = 1;
 7942:                             last;
 7943:                         }
 7944:                     }
 7945:                 } else {
 7946:                     $changes{$type}{$setting} = 1;
 7947:                 }
 7948:             } elsif (@new > 0) {
 7949:                 $changes{$type}{$setting} = 1;
 7950:             }
 7951:         }
 7952:     }
 7953:     if (keys(%changes) > 0) {
 7954:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7955:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7956:                                                  \%serverstatushash,$dom);
 7957:         if ($putresult eq 'ok') {
 7958:             $resulttext .= &mt('Changes made:').'<ul>';
 7959:             foreach my $type (@pages) {
 7960:                 if (ref($changes{$type}) eq 'HASH') {
 7961:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7962:                     if ($changes{$type}{'namedusers'}) {
 7963:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7964:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7965:                         } else {
 7966:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7967:                         }
 7968:                     }
 7969:                     if ($changes{$type}{'machines'}) {
 7970:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7971:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7972:                         } else {
 7973:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7974:                         }
 7975: 
 7976:                     }
 7977:                     $resulttext .= '</ul></li>';
 7978:                 }
 7979:             }
 7980:             $resulttext .= '</ul>';
 7981:         } else {
 7982:             $resulttext = '<span class="LC_error">'.
 7983:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7984: 
 7985:         }
 7986:     } else {
 7987:         $resulttext = &mt('No changes made to access to server status pages');
 7988:     }
 7989:     return $resulttext;
 7990: }
 7991: 
 7992: sub modify_helpsettings {
 7993:     my ($r,$dom,$confname,%domconfig) = @_;
 7994:     my ($resulttext,$errors,%changes,%helphash);
 7995:     my %defaultchecked = ('submitbugs' => 'on');
 7996:     my @offon = ('off','on');
 7997:     my @toggles = ('submitbugs');
 7998:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7999:         foreach my $item (@toggles) {
 8000:             if ($defaultchecked{$item} eq 'on') { 
 8001:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 8002:                     if ($env{'form.'.$item} eq '0') {
 8003:                         $changes{$item} = 1;
 8004:                     }
 8005:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8006:                     $changes{$item} = 1;
 8007:                 }
 8008:             } elsif ($defaultchecked{$item} eq 'off') {
 8009:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 8010:                     if ($env{'form.'.$item} eq '1') {
 8011:                         $changes{$item} = 1;
 8012:                     }
 8013:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8014:                     $changes{$item} = 1;
 8015:                 }
 8016:             }
 8017:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 8018:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 8019:             }
 8020:         }
 8021:     }
 8022:     my $putresult;
 8023:     if (keys(%changes) > 0) {
 8024:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 8025:         if ($putresult eq 'ok') {
 8026:             $resulttext = &mt('Changes made:').'<ul>';
 8027:             foreach my $item (sort(keys(%changes))) {
 8028:                 if ($item eq 'submitbugs') {
 8029:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 8030:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 8031:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 8032:                 }
 8033:             }
 8034:             $resulttext .= '</ul>';
 8035:         } else {
 8036:             $resulttext = &mt('No changes made to help settings');
 8037:             $errors .= '<li><span class="LC_error">'.
 8038:                        &mt('An error occurred storing the settings: [_1]',
 8039:                            $putresult).'</span></li>';
 8040:         }
 8041:     }
 8042:     if ($errors) {
 8043:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8044:                        $errors.'</ul>';
 8045:     }
 8046:     return $resulttext;
 8047: }
 8048: 
 8049: sub modify_coursedefaults {
 8050:     my ($dom,$lastactref,%domconfig) = @_;
 8051:     my ($resulttext,$errors,%changes,%defaultshash);
 8052:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 8053:     my @toggles = ('canuse_pdfforms');
 8054:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 8055:                    'uploadquota_community');
 8056:     my @types = ('official','unofficial','community');
 8057:     my %staticdefaults = (
 8058:                            anonsurvey_threshold => 10,
 8059:                            uploadquota          => 500,
 8060:                          );
 8061: 
 8062:     $defaultshash{'coursedefaults'} = {};
 8063: 
 8064:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 8065:         if ($domconfig{'coursedefaults'} eq '') {
 8066:             $domconfig{'coursedefaults'} = {};
 8067:         }
 8068:     }
 8069: 
 8070:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 8071:         foreach my $item (@toggles) {
 8072:             if ($defaultchecked{$item} eq 'on') {
 8073:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 8074:                     ($env{'form.'.$item} eq '0')) {
 8075:                     $changes{$item} = 1;
 8076:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 8077:                     $changes{$item} = 1;
 8078:                 }
 8079:             } elsif ($defaultchecked{$item} eq 'off') {
 8080:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 8081:                     ($env{'form.'.$item} eq '1')) {
 8082:                     $changes{$item} = 1;
 8083:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 8084:                     $changes{$item} = 1;
 8085:                 }
 8086:             }
 8087:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 8088:         }
 8089:         foreach my $item (@numbers) {
 8090:             my ($currdef,$newdef);
 8091:             $newdef = $env{'form.'.$item};
 8092:             if ($item eq 'anonsurvey_threshold') {
 8093:                 $currdef = $domconfig{'coursedefaults'}{$item};
 8094:                 $newdef =~ s/\D//g;
 8095:                 if ($newdef eq '' || $newdef < 1) {
 8096:                     $newdef = 1;
 8097:                 }
 8098:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 8099:             } else {
 8100:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 8101:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8102:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 8103:                 }
 8104:                 $newdef =~ s/[^\w.\-]//g;
 8105:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 8106:             }
 8107:             if ($currdef ne $newdef) {
 8108:                 my $staticdef;
 8109:                 if ($item eq 'anonsurvey_threshold') {
 8110:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
 8111:                         $changes{$item} = 1;
 8112:                     }
 8113:                 } else {
 8114:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
 8115:                         $changes{'uploadquota'} = 1;
 8116:                     }
 8117:                 }
 8118:             }
 8119:         }
 8120:         my $officialcreds = $env{'form.official_credits'};
 8121:         $officialcreds =~ s/^[^\d\.]//g;
 8122:         my $unofficialcreds = $env{'form.unofficial_credits'};
 8123:         $unofficialcreds =~ s/^[^\d\.]//g;
 8124:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 8125:                 ($env{'form.coursecredits'} eq '1')) {
 8126:                 $changes{'coursecredits'} = 1;
 8127:         } else {
 8128:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 8129:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds)) {
 8130:                 $changes{'coursecredits'} = 1;
 8131:             }
 8132:         }
 8133:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 8134:             official   => $officialcreds,
 8135:             unofficial => $unofficialcreds,
 8136:         }
 8137:     }
 8138:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8139:                                              $dom);
 8140:     if ($putresult eq 'ok') {
 8141:         if (keys(%changes) > 0) {
 8142:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8143:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || ($changes{'uploadquota'})) {
 8144:                 if ($changes{'canuse_pdfforms'}) {
 8145:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 8146:                 }
 8147:                 if ($changes{'coursecredits'}) {
 8148:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 8149:                         $domdefaults{'officialcredits'} =
 8150:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 8151:                         $domdefaults{'unofficialcredits'} =
 8152:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 8153:                     }
 8154:                 }
 8155:                 if ($changes{'uploadquota'}) {
 8156:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8157:                         foreach my $type (@types) {
 8158:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
 8159:                         }
 8160:                     }
 8161:                 }
 8162:                 my $cachetime = 24*60*60;
 8163:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8164:                 if (ref($lastactref) eq 'HASH') {
 8165:                     $lastactref->{'domdefaults'} = 1;
 8166:                 }
 8167:             }
 8168:             $resulttext = &mt('Changes made:').'<ul>';
 8169:             foreach my $item (sort(keys(%changes))) {
 8170:                 if ($item eq 'canuse_pdfforms') {
 8171:                     if ($env{'form.'.$item} eq '1') {
 8172:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 8173:                     } else {
 8174:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 8175:                     }
 8176:                 } elsif ($item eq 'anonsurvey_threshold') {
 8177:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 8178:                 } elsif ($item eq 'uploadquota') {
 8179:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8180:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
 8181:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
 8182:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
 8183:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
 8184:                                        '</ul>'.
 8185:                                        '</li>';
 8186:                     } else {
 8187:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
 8188:                     }
 8189:                 } elsif ($item eq 'coursecredits') {
 8190:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 8191:                         if (($domdefaults{'officialcredits'} eq '') &&
 8192:                             ($domdefaults{'unofficialcredits'} eq '')) {
 8193:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 8194:                         } else {
 8195:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
 8196:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
 8197:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
 8198:                                            '</ul>'.
 8199:                                            '</li>';
 8200:                         }
 8201:                     } else {
 8202:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 8203:                     }
 8204:                 }
 8205:             }
 8206:             $resulttext .= '</ul>';
 8207:         } else {
 8208:             $resulttext = &mt('No changes made to course defaults');
 8209:         }
 8210:     } else {
 8211:         $resulttext = '<span class="LC_error">'.
 8212:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8213:     }
 8214:     return $resulttext;
 8215: }
 8216: 
 8217: sub modify_usersessions {
 8218:     my ($dom,$lastactref,%domconfig) = @_;
 8219:     my @hostingtypes = ('version','excludedomain','includedomain');
 8220:     my @offloadtypes = ('primary','default');
 8221:     my %types = (
 8222:                   remote => \@hostingtypes,
 8223:                   hosted => \@hostingtypes,
 8224:                   spares => \@offloadtypes,
 8225:                 );
 8226:     my @prefixes = ('remote','hosted','spares');
 8227:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 8228:     my (%by_ip,%by_location,@intdoms);
 8229:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 8230:     my @locations = sort(keys(%by_location));
 8231:     my (%defaultshash,%changes);
 8232:     foreach my $prefix (@prefixes) {
 8233:         $defaultshash{'usersessions'}{$prefix} = {};
 8234:     }
 8235:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8236:     my $resulttext;
 8237:     my %iphost = &Apache::lonnet::get_iphost();
 8238:     foreach my $prefix (@prefixes) {
 8239:         next if ($prefix eq 'spares');
 8240:         foreach my $type (@{$types{$prefix}}) {
 8241:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 8242:             if ($type eq 'version') {
 8243:                 my $value = $env{'form.'.$prefix.'_'.$type};
 8244:                 my $okvalue;
 8245:                 if ($value ne '') {
 8246:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 8247:                         $okvalue = $value;
 8248:                     }
 8249:                 }
 8250:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8251:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8252:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 8253:                             if ($inuse == 0) {
 8254:                                 $changes{$prefix}{$type} = 1;
 8255:                             } else {
 8256:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 8257:                                     $changes{$prefix}{$type} = 1;
 8258:                                 }
 8259:                                 if ($okvalue ne '') {
 8260:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8261:                                 } 
 8262:                             }
 8263:                         } else {
 8264:                             if (($inuse == 1) && ($okvalue ne '')) {
 8265:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8266:                                 $changes{$prefix}{$type} = 1;
 8267:                             }
 8268:                         }
 8269:                     } else {
 8270:                         if (($inuse == 1) && ($okvalue ne '')) {
 8271:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8272:                             $changes{$prefix}{$type} = 1;
 8273:                         }
 8274:                     }
 8275:                 } else {
 8276:                     if (($inuse == 1) && ($okvalue ne '')) {
 8277:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8278:                         $changes{$prefix}{$type} = 1;
 8279:                     }
 8280:                 }
 8281:             } else {
 8282:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 8283:                 my @okvals;
 8284:                 foreach my $val (@vals) {
 8285:                     if ($val =~ /:/) {
 8286:                         my @items = split(/:/,$val);
 8287:                         foreach my $item (@items) {
 8288:                             if (ref($by_location{$item}) eq 'ARRAY') {
 8289:                                 push(@okvals,$item);
 8290:                             }
 8291:                         }
 8292:                     } else {
 8293:                         if (ref($by_location{$val}) eq 'ARRAY') {
 8294:                             push(@okvals,$val);
 8295:                         }
 8296:                     }
 8297:                 }
 8298:                 @okvals = sort(@okvals);
 8299:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8300:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8301:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8302:                             if ($inuse == 0) {
 8303:                                 $changes{$prefix}{$type} = 1; 
 8304:                             } else {
 8305:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8306:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 8307:                                 if (@changed > 0) {
 8308:                                     $changes{$prefix}{$type} = 1;
 8309:                                 }
 8310:                             }
 8311:                         } else {
 8312:                             if ($inuse == 1) {
 8313:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8314:                                 $changes{$prefix}{$type} = 1;
 8315:                             }
 8316:                         } 
 8317:                     } else {
 8318:                         if ($inuse == 1) {
 8319:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8320:                             $changes{$prefix}{$type} = 1;
 8321:                         }
 8322:                     }
 8323:                 } else {
 8324:                     if ($inuse == 1) {
 8325:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8326:                         $changes{$prefix}{$type} = 1;
 8327:                     }
 8328:                 }
 8329:             }
 8330:         }
 8331:     }
 8332: 
 8333:     my @alldoms = &Apache::lonnet::all_domains();
 8334:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8335:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 8336:     my $savespares;
 8337: 
 8338:     foreach my $lonhost (sort(keys(%servers))) {
 8339:         my $serverhomeID =
 8340:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 8341:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 8342:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 8343:         my %spareschg;
 8344:         foreach my $type (@{$types{'spares'}}) {
 8345:             my @okspares;
 8346:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 8347:             foreach my $server (@checked) {
 8348:                 if (&Apache::lonnet::hostname($server) ne '') {
 8349:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 8350:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 8351:                             push(@okspares,$server);
 8352:                         }
 8353:                     }
 8354:                 }
 8355:             }
 8356:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 8357:             my $newspare;
 8358:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 8359:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 8360:                     $newspare = $new;
 8361:                 }
 8362:             }
 8363:             my @spares;
 8364:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 8365:                 @spares = sort(@okspares,$newspare);
 8366:             } else {
 8367:                 @spares = sort(@okspares);
 8368:             }
 8369:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 8370:             if (ref($spareid{$lonhost}) eq 'HASH') {
 8371:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 8372:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 8373:                     if (@diffs > 0) {
 8374:                         $spareschg{$type} = 1;
 8375:                     }
 8376:                 }
 8377:             }
 8378:         }
 8379:         if (keys(%spareschg) > 0) {
 8380:             $changes{'spares'}{$lonhost} = \%spareschg;
 8381:         }
 8382:     }
 8383: 
 8384:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8385:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 8386:             if (ref($changes{'spares'}) eq 'HASH') {
 8387:                 if (keys(%{$changes{'spares'}}) > 0) {
 8388:                     $savespares = 1;
 8389:                 }
 8390:             }
 8391:         } else {
 8392:             $savespares = 1;
 8393:         }
 8394:     }
 8395: 
 8396:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 8397:     if ((keys(%changes) > 0) || ($savespares)) {
 8398:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8399:                                                  $dom);
 8400:         if ($putresult eq 'ok') {
 8401:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8402:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 8403:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 8404:                 }
 8405:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 8406:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 8407:                 }
 8408:             }
 8409:             my $cachetime = 24*60*60;
 8410:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8411:             if (ref($lastactref) eq 'HASH') {
 8412:                 $lastactref->{'domdefaults'} = 1;
 8413:             }
 8414:             if (keys(%changes) > 0) {
 8415:                 my %lt = &usersession_titles();
 8416:                 $resulttext = &mt('Changes made:').'<ul>';
 8417:                 foreach my $prefix (@prefixes) {
 8418:                     if (ref($changes{$prefix}) eq 'HASH') {
 8419:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 8420:                         if ($prefix eq 'spares') {
 8421:                             if (ref($changes{$prefix}) eq 'HASH') {
 8422:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 8423:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 8424:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 8425:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
 8426:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
 8427:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 8428:                                         foreach my $type (@{$types{$prefix}}) {
 8429:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 8430:                                                 my $offloadto = &mt('None');
 8431:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 8432:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 8433:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 8434:                                                     }
 8435:                                                 }
 8436:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 8437:                                             }
 8438:                                         }
 8439:                                     }
 8440:                                     $resulttext .= '</li>';
 8441:                                 }
 8442:                             }
 8443:                         } else {
 8444:                             foreach my $type (@{$types{$prefix}}) {
 8445:                                 if (defined($changes{$prefix}{$type})) {
 8446:                                     my $newvalue;
 8447:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8448:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 8449:                                             if ($type eq 'version') {
 8450:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 8451:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8452:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 8453:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 8454:                                                 }
 8455:                                             }
 8456:                                         }
 8457:                                     }
 8458:                                     if ($newvalue eq '') {
 8459:                                         if ($type eq 'version') {
 8460:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8461:                                         } else {
 8462:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8463:                                         }
 8464:                                     } else {
 8465:                                         if ($type eq 'version') {
 8466:                                             $newvalue .= ' '.&mt('(or later)'); 
 8467:                                         }
 8468:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8469:                                     }
 8470:                                 }
 8471:                             }
 8472:                         }
 8473:                         $resulttext .= '</ul>';
 8474:                     }
 8475:                 }
 8476:                 $resulttext .= '</ul>';
 8477:             } else {
 8478:                 $resulttext = $nochgmsg;
 8479:             }
 8480:         } else {
 8481:             $resulttext = '<span class="LC_error">'.
 8482:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8483:         }
 8484:     } else {
 8485:         $resulttext = $nochgmsg;
 8486:     }
 8487:     return $resulttext;
 8488: }
 8489: 
 8490: sub modify_loadbalancing {
 8491:     my ($dom,%domconfig) = @_;
 8492:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8493:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8494:     my ($othertitle,$usertypes,$types) =
 8495:         &Apache::loncommon::sorted_inst_types($dom);
 8496:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8497:     my @sparestypes = ('primary','default');
 8498:     my %typetitles = &sparestype_titles();
 8499:     my $resulttext;
 8500:     my (%currbalancer,%currtargets,%currrules,%existing);
 8501:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8502:         %existing = %{$domconfig{'loadbalancing'}};
 8503:     }
 8504:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8505:                               \%currtargets,\%currrules);
 8506:     my ($saveloadbalancing,%defaultshash,%changes);
 8507:     my ($alltypes,$othertypes,$titles) =
 8508:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8509:     my %ruletitles = &offloadtype_text();
 8510:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 8511:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 8512:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 8513:         if ($balancer eq '') {
 8514:             next;
 8515:         }
 8516:         if (!exists($servers{$balancer})) {
 8517:             if (exists($currbalancer{$balancer})) {
 8518:                 push(@{$changes{'delete'}},$balancer);
 8519:             }
 8520:             next;
 8521:         }
 8522:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 8523:             push(@{$changes{'delete'}},$balancer);
 8524:             next;
 8525:         }
 8526:         if (!exists($currbalancer{$balancer})) {
 8527:             push(@{$changes{'add'}},$balancer);
 8528:         }
 8529:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 8530:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 8531:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 8532:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8533:             $saveloadbalancing = 1;
 8534:         }
 8535:         foreach my $sparetype (@sparestypes) {
 8536:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 8537:             my @offloadto;
 8538:             foreach my $target (@targets) {
 8539:                 if (($servers{$target}) && ($target ne $balancer)) {
 8540:                     if ($sparetype eq 'default') {
 8541:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 8542:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 8543:                         }
 8544:                     }
 8545:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 8546:                         push(@offloadto,$target);
 8547:                     }
 8548:                 }
 8549:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 8550:             }
 8551:         }
 8552:         if (ref($currtargets{$balancer}) eq 'HASH') {
 8553:             foreach my $sparetype (@sparestypes) {
 8554:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 8555:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 8556:                     if (@targetdiffs > 0) {
 8557:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8558:                     }
 8559:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8560:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8561:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8562:                     }
 8563:                 }
 8564:             }
 8565:         } else {
 8566:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 8567:                 foreach my $sparetype (@sparestypes) {
 8568:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8569:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8570:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 8571:                         }
 8572:                     }
 8573:                 }
 8574:             }
 8575:         }
 8576:         my $ishomedom;
 8577:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 8578:             $ishomedom = 1;
 8579:         }
 8580:         if (ref($alltypes) eq 'ARRAY') {
 8581:             foreach my $type (@{$alltypes}) {
 8582:                 my $rule;
 8583:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
 8584:                          (!$ishomedom)) {
 8585:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 8586:                 }
 8587:                 if ($rule eq 'specific') {
 8588:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 8589:                 }
 8590:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 8591:                 if (ref($currrules{$balancer}) eq 'HASH') {
 8592:                     if ($rule ne $currrules{$balancer}{$type}) {
 8593:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8594:                     }
 8595:                 } elsif ($rule ne '') {
 8596:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8597:                 }
 8598:             }
 8599:         }
 8600:     }
 8601:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8602:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8603:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 8604:             $defaultshash{'loadbalancing'} = {};
 8605:         }
 8606:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8607:                                                  \%defaultshash,$dom);
 8608:         if ($putresult eq 'ok') {
 8609:             if (keys(%changes) > 0) {
 8610:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 8611:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 8612:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 8613:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
 8614:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
 8615:                     }
 8616:                 }
 8617:                 if (ref($changes{'add'}) eq 'ARRAY') {
 8618:                     foreach my $balancer (sort(@{$changes{'add'}})) {
 8619:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 8620:                     }
 8621:                 }
 8622:                 if (ref($changes{'curr'}) eq 'HASH') {
 8623:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 8624:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 8625:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 8626:                                 my %offloadstr;
 8627:                                 foreach my $sparetype (@sparestypes) {
 8628:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8629:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8630:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 8631:                                         }
 8632:                                     }
 8633:                                 }
 8634:                                 if (keys(%offloadstr) == 0) {
 8635:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8636:                                 } else {
 8637:                                     my $showoffload;
 8638:                                     foreach my $sparetype (@sparestypes) {
 8639:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8640:                                         if (defined($offloadstr{$sparetype})) {
 8641:                                             $showoffload .= $offloadstr{$sparetype};
 8642:                                         } else {
 8643:                                             $showoffload .= &mt('None');
 8644:                                         }
 8645:                                         $showoffload .= ('&nbsp;'x3);
 8646:                                     }
 8647:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 8648:                                 }
 8649:                             }
 8650:                         }
 8651:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 8652:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8653:                                 foreach my $type (@{$alltypes}) {
 8654:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 8655:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 8656:                                         my $balancetext;
 8657:                                         if ($rule eq '') {
 8658:                                             $balancetext =  $ruletitles{'default'};
 8659:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
 8660:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
 8661:                                             $balancetext =  $ruletitles{$rule};
 8662:                                         } else {
 8663:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 8664:                                         }
 8665:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
 8666:                                     }
 8667:                                 }
 8668:                             }
 8669:                         }
 8670:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
 8671:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
 8672:                     }
 8673:                 }
 8674:                 if ($resulttext ne '') {
 8675:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8676:                 } else {
 8677:                     $resulttext = $nochgmsg;
 8678:                 }
 8679:             } else {
 8680:                 $resulttext = $nochgmsg;
 8681:             }
 8682:         } else {
 8683:             $resulttext = '<span class="LC_error">'.
 8684:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8685:         }
 8686:     } else {
 8687:         $resulttext = $nochgmsg;
 8688:     }
 8689:     return $resulttext;
 8690: }
 8691: 
 8692: sub recurse_check {
 8693:     my ($chkcats,$categories,$depth,$name) = @_;
 8694:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8695:         my $chg = 0;
 8696:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8697:             my $category = $chkcats->[$depth]{$name}[$j];
 8698:             my $item;
 8699:             if ($category eq '') {
 8700:                 $chg ++;
 8701:             } else {
 8702:                 my $deeper = $depth + 1;
 8703:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8704:                 if ($chg) {
 8705:                     $categories->{$item} -= $chg;
 8706:                 }
 8707:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8708:                 $deeper --;
 8709:             }
 8710:         }
 8711:     }
 8712:     return;
 8713: }
 8714: 
 8715: sub recurse_cat_deletes {
 8716:     my ($item,$coursecategories,$deletions) = @_;
 8717:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8718:     my $subdepth = $depth + 1;
 8719:     if (ref($coursecategories) eq 'HASH') {
 8720:         foreach my $subitem (keys(%{$coursecategories})) {
 8721:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8722:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8723:                 delete($coursecategories->{$subitem});
 8724:                 $deletions->{$subitem} = 1;
 8725:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8726:             }
 8727:         }
 8728:     }
 8729:     return;
 8730: }
 8731: 
 8732: sub get_active_dcs {
 8733:     my ($dom) = @_;
 8734:     my $now = time;
 8735:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 8736:     my %domcoords;
 8737:     my $numdcs = 0;
 8738:     foreach my $server (keys(%dompersonnel)) {
 8739:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8740:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8741:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8742:         }
 8743:     }
 8744:     return %domcoords;
 8745: }
 8746: 
 8747: sub active_dc_picker {
 8748:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
 8749:     my %domcoords = &get_active_dcs($dom); 
 8750:     my @domcoord = keys(%domcoords);
 8751:     if (keys(%currhash)) {
 8752:         foreach my $dc (keys(%currhash)) {
 8753:             unless (exists($domcoords{$dc})) {
 8754:                 push(@domcoord,$dc);
 8755:             }
 8756:         }
 8757:     }
 8758:     @domcoord = sort(@domcoord);
 8759:     my $numdcs = scalar(@domcoord);
 8760:     my $rows = 0;
 8761:     my $table;
 8762:     if ($numdcs > 1) {
 8763:         $table = '<table>';
 8764:         for (my $i=0; $i<@domcoord; $i++) {
 8765:             my $rem = $i%($numinrow);
 8766:             if ($rem == 0) {
 8767:                 if ($i > 0) {
 8768:                     $table .= '</tr>';
 8769:                 }
 8770:                 $table .= '<tr>';
 8771:                 $rows ++;
 8772:             }
 8773:             my $check = '';
 8774:             if ($inputtype eq 'radio') {
 8775:                 if (keys(%currhash) == 0) {
 8776:                     if (!$i) {
 8777:                         $check = ' checked="checked"';
 8778:                     }
 8779:                 } elsif (exists($currhash{$domcoord[$i]})) {
 8780:                     $check = ' checked="checked"';
 8781:                 }
 8782:             } else {
 8783:                 if (exists($currhash{$domcoord[$i]})) {
 8784:                     $check = ' checked="checked"';
 8785:                 }
 8786:             }
 8787:             if ($i == @domcoord - 1) {
 8788:                 my $colsleft = $numinrow - $rem;
 8789:                 if ($colsleft > 1) {
 8790:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
 8791:                 } else {
 8792:                     $table .= '<td class="LC_left_item">';
 8793:                 }
 8794:             } else {
 8795:                 $table .= '<td class="LC_left_item">';
 8796:             }
 8797:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
 8798:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
 8799:             $table .= '<span class="LC_nobreak"><label>'.
 8800:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
 8801:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
 8802:             if ($user ne $dcname.':'.$dcdom) {
 8803:                 $table .=  ' ('.$dcname.':'.$dcdom.')'.
 8804:                            '</label></span></td>';
 8805:             }
 8806:         }
 8807:         $table .= '</tr></table>';
 8808:     } elsif ($numdcs == 1) {
 8809:         if ($inputtype eq 'radio') {
 8810:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />';
 8811:         } else {
 8812:             my $check;
 8813:             if (exists($currhash{$domcoord[0]})) {
 8814:                 $check = ' checked="checked"';
 8815:             }
 8816:             $table .= '<input type="checkbox" name="'.$name.'" '.
 8817:                       'value="'.$domcoord[0].'"'.$check.' />';
 8818:             $rows ++;
 8819:         }
 8820:     }
 8821:     return ($numdcs,$table,$rows);
 8822: }
 8823: 
 8824: sub usersession_titles {
 8825:     return &Apache::lonlocal::texthash(
 8826:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8827:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8828:                spares => 'Servers offloaded to, when busy',
 8829:                version => 'LON-CAPA version requirement',
 8830:                excludedomain => 'Allow all, but exclude specific domains',
 8831:                includedomain => 'Deny all, but include specific domains',
 8832:                primary => 'Primary (checked first)',
 8833:                default => 'Default',
 8834:            );
 8835: }
 8836: 
 8837: sub id_for_thisdom {
 8838:     my (%servers) = @_;
 8839:     my %altids;
 8840:     foreach my $server (keys(%servers)) {
 8841:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8842:         if ($serverhome ne $server) {
 8843:             $altids{$serverhome} = $server;
 8844:         }
 8845:     }
 8846:     return %altids;
 8847: }
 8848: 
 8849: sub count_servers {
 8850:     my ($currbalancer,%servers) = @_;
 8851:     my (@spares,$numspares);
 8852:     foreach my $lonhost (sort(keys(%servers))) {
 8853:         next if ($currbalancer eq $lonhost);
 8854:         push(@spares,$lonhost);
 8855:     }
 8856:     if ($currbalancer) {
 8857:         $numspares = scalar(@spares);
 8858:     } else {
 8859:         $numspares = scalar(@spares) - 1;
 8860:     }
 8861:     return ($numspares,@spares);
 8862: }
 8863: 
 8864: sub lonbalance_targets_js {
 8865:     my ($dom,$types,$servers,$settings) = @_;
 8866:     my $select = &mt('Select');
 8867:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8868:     if (ref($servers) eq 'HASH') {
 8869:         $alltargets = join("','",sort(keys(%{$servers})));
 8870:         my @homedoms;
 8871:         foreach my $server (sort(keys(%{$servers}))) {
 8872:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8873:                 push(@homedoms,'1');
 8874:             } else {
 8875:                 push(@homedoms,'0');
 8876:             }
 8877:         }
 8878:         $allishome = join("','",@homedoms);
 8879:     }
 8880:     if (ref($types) eq 'ARRAY') {
 8881:         if (@{$types} > 0) {
 8882:             @alltypes = @{$types};
 8883:         }
 8884:     }
 8885:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8886:     $allinsttypes = join("','",@alltypes);
 8887:     my (%currbalancer,%currtargets,%currrules,%existing);
 8888:     if (ref($settings) eq 'HASH') {
 8889:         %existing = %{$settings};
 8890:     }
 8891:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 8892:                               \%currtargets,\%currrules);
 8893:     my $balancers = join("','",sort(keys(%currbalancer)));
 8894:     return <<"END";
 8895: 
 8896: <script type="text/javascript">
 8897: // <![CDATA[
 8898: 
 8899: currBalancers = new Array('$balancers');
 8900: 
 8901: function toggleTargets(balnum) {
 8902:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8903:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 8904:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 8905:     var prevbalancer = prevhostitem.value;
 8906:     var baltotal = document.getElementById('loadbalancing_total').value;
 8907:     prevhostitem.value = balancer;
 8908:     if (prevbalancer != '') {
 8909:         var prevIdx = currBalancers.indexOf(prevbalancer);
 8910:         if (prevIdx != -1) {
 8911:             currBalancers.splice(prevIdx,1);
 8912:         }
 8913:     }
 8914:     if (balancer == '') {
 8915:         hideSpares(balnum);
 8916:     } else {
 8917:         var currIdx = currBalancers.indexOf(balancer);
 8918:         if (currIdx == -1) {
 8919:             currBalancers.push(balancer);
 8920:         }
 8921:         var homedoms = new Array('$allishome');
 8922:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 8923:         showSpares(balancer,ishomedom,balnum);
 8924:     }
 8925:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 8926:     return;
 8927: }
 8928: 
 8929: function showSpares(balancer,ishomedom,balnum) {
 8930:     var alltargets = new Array('$alltargets');
 8931:     var insttypes = new Array('$allinsttypes');
 8932:     var offloadtypes = new Array('primary','default');
 8933: 
 8934:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 8935:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 8936:  
 8937:     for (var i=0; i<offloadtypes.length; i++) {
 8938:         var count = 0;
 8939:         for (var j=0; j<alltargets.length; j++) {
 8940:             if (alltargets[j] != balancer) {
 8941:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 8942:                 item.value = alltargets[j];
 8943:                 item.style.textAlign='left';
 8944:                 item.style.textFace='normal';
 8945:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8946:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 8947:                     item.disabled = '';
 8948:                 } else {
 8949:                     item.disabled = 'disabled';
 8950:                     item.checked = false;
 8951:                 }
 8952:                 count ++;
 8953:             }
 8954:         }
 8955:     }
 8956:     for (var k=0; k<insttypes.length; k++) {
 8957:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8958:             if (ishomedom == 1) {
 8959:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8960:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8961:             } else {
 8962:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8963:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8964:             }
 8965:         } else {
 8966:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8967:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8968:         }
 8969:         if ((insttypes[k] != '_LC_external') && 
 8970:             ((insttypes[k] != '_LC_internetdom') ||
 8971:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8972:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 8973:             item.options.length = 0;
 8974:             item.options[0] = new Option("","",true,true);
 8975:             var idx = 0;
 8976:             for (var m=0; m<alltargets.length; m++) {
 8977:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 8978:                     idx ++;
 8979:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8980:                 }
 8981:             }
 8982:         }
 8983:     }
 8984:     return;
 8985: }
 8986: 
 8987: function hideSpares(balnum) {
 8988:     var alltargets = new Array('$alltargets');
 8989:     var insttypes = new Array('$allinsttypes');
 8990:     var offloadtypes = new Array('primary','default');
 8991: 
 8992:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 8993:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 8994: 
 8995:     var total = alltargets.length - 1;
 8996:     for (var i=0; i<offloadtypes; i++) {
 8997:         for (var j=0; j<total; j++) {
 8998:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 8999:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 9000:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 9001:         }
 9002:     }
 9003:     for (var k=0; k<insttypes.length; k++) {
 9004:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 9005:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 9006:         if (insttypes[k] != '_LC_external') {
 9007:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 9008:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 9009:         }
 9010:     }
 9011:     return;
 9012: }
 9013: 
 9014: function checkOffloads(item,balnum,type) {
 9015:     var alltargets = new Array('$alltargets');
 9016:     var offloadtypes = new Array('primary','default');
 9017:     if (item.checked) {
 9018:         var total = alltargets.length - 1;
 9019:         var other;
 9020:         if (type == offloadtypes[0]) {
 9021:             other = offloadtypes[1];
 9022:         } else {
 9023:             other = offloadtypes[0];
 9024:         }
 9025:         for (var i=0; i<total; i++) {
 9026:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 9027:             if (server == item.value) {
 9028:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 9029:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 9030:                 }
 9031:             }
 9032:         }
 9033:     }
 9034:     return;
 9035: }
 9036: 
 9037: function singleServerToggle(balnum,type) {
 9038:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 9039:     if (offloadtoSelIdx == 0) {
 9040:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 9041:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9042: 
 9043:     } else {
 9044:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 9045:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 9046:     }
 9047:     return;
 9048: }
 9049: 
 9050: function balanceruleChange(formname,balnum,type) {
 9051:     if (type == '_LC_external') {
 9052:         return;
 9053:     }
 9054:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 9055:     for (var i=0; i<typesRules.length; i++) {
 9056:         if (formname.elements[typesRules[i]].checked) {
 9057:             if (formname.elements[typesRules[i]].value != 'specific') {
 9058:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 9059:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9060:             } else {
 9061:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 9062:             }
 9063:         }
 9064:     }
 9065:     return;
 9066: }
 9067: 
 9068: function balancerDeleteChange(balnum) {
 9069:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 9070:     var baltotal = document.getElementById('loadbalancing_total').value;
 9071:     var addtarget;
 9072:     var removetarget;
 9073:     var action = 'delete';
 9074:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 9075:         var lonhost = hostitem.value;
 9076:         var currIdx = currBalancers.indexOf(lonhost);
 9077:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 9078:             if (currIdx != -1) {
 9079:                 currBalancers.splice(currIdx,1);
 9080:             }
 9081:             addtarget = lonhost;
 9082:         } else {
 9083:             if (currIdx == -1) {
 9084:                 currBalancers.push(lonhost);
 9085:             }
 9086:             removetarget = lonhost;
 9087:             action = 'undelete';
 9088:         }
 9089:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 9090:     }
 9091:     return;
 9092: }
 9093: 
 9094: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 9095:     if (baltotal > 1) {
 9096:         var offloadtypes = new Array('primary','default');
 9097:         var alltargets = new Array('$alltargets');
 9098:         var insttypes = new Array('$allinsttypes');
 9099:         for (var i=0; i<baltotal; i++) {
 9100:             if (i != balnum) {
 9101:                 for (var j=0; j<offloadtypes.length; j++) {
 9102:                     var total = alltargets.length - 1;
 9103:                     for (var k=0; k<total; k++) {
 9104:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 9105:                         var server = serveritem.value;
 9106:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 9107:                             if (server == addtarget) {
 9108:                                 serveritem.disabled = '';
 9109:                             }
 9110:                         }
 9111:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 9112:                             if (server == removetarget) {
 9113:                                 serveritem.disabled = 'disabled';
 9114:                                 serveritem.checked = false;
 9115:                             }
 9116:                         }
 9117:                     }
 9118:                 }
 9119:                 for (var j=0; j<insttypes.length; j++) {
 9120:                     if (insttypes[j] != '_LC_external') {
 9121:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 9122:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 9123:                             var currSel = singleserver.selectedIndex;
 9124:                             var currVal = singleserver.options[currSel].value;
 9125:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 9126:                                 var numoptions = singleserver.options.length;
 9127:                                 var needsnew = 1;
 9128:                                 for (var k=0; k<numoptions; k++) {
 9129:                                     if (singleserver.options[k] == addtarget) {
 9130:                                         needsnew = 0;
 9131:                                         break;
 9132:                                     }
 9133:                                 }
 9134:                                 if (needsnew == 1) {
 9135:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 9136:                                 }
 9137:                             }
 9138:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 9139:                                 singleserver.options.length = 0;
 9140:                                 if ((currVal) && (currVal != removetarget)) {
 9141:                                     singleserver.options[0] = new Option("","",false,false);
 9142:                                 } else {
 9143:                                     singleserver.options[0] = new Option("","",true,true);
 9144:                                 }
 9145:                                 var idx = 0;
 9146:                                 for (var m=0; m<alltargets.length; m++) {
 9147:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
 9148:                                         idx ++;
 9149:                                         if (currVal == alltargets[m]) {
 9150:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
 9151:                                         } else {
 9152:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 9153:                                         }
 9154:                                     }
 9155:                                 }
 9156:                             }
 9157:                         }
 9158:                     }
 9159:                 }
 9160:             }
 9161:         }
 9162:     }
 9163:     return;
 9164: }
 9165: 
 9166: // ]]>
 9167: </script>
 9168: 
 9169: END
 9170: }
 9171: 
 9172: sub new_spares_js {
 9173:     my @sparestypes = ('primary','default');
 9174:     my $types = join("','",@sparestypes);
 9175:     my $select = &mt('Select');
 9176:     return <<"END";
 9177: 
 9178: <script type="text/javascript">
 9179: // <![CDATA[
 9180: 
 9181: function updateNewSpares(formname,lonhost) {
 9182:     var types = new Array('$types');
 9183:     var include = new Array();
 9184:     var exclude = new Array();
 9185:     for (var i=0; i<types.length; i++) {
 9186:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 9187:         for (var j=0; j<spareboxes.length; j++) {
 9188:             if (formname.elements[spareboxes[j]].checked) {
 9189:                 exclude.push(formname.elements[spareboxes[j]].value);
 9190:             } else {
 9191:                 include.push(formname.elements[spareboxes[j]].value);
 9192:             }
 9193:         }
 9194:     }
 9195:     for (var i=0; i<types.length; i++) {
 9196:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 9197:         var selIdx = newSpare.selectedIndex;
 9198:         var currnew = newSpare.options[selIdx].value;
 9199:         var okSpares = new Array();
 9200:         for (var j=0; j<newSpare.options.length; j++) {
 9201:             var possible = newSpare.options[j].value;
 9202:             if (possible != '') {
 9203:                 if (exclude.indexOf(possible) == -1) {
 9204:                     okSpares.push(possible);
 9205:                 } else {
 9206:                     if (currnew == possible) {
 9207:                         selIdx = 0;
 9208:                     }
 9209:                 }
 9210:             }
 9211:         }
 9212:         for (var k=0; k<include.length; k++) {
 9213:             if (okSpares.indexOf(include[k]) == -1) {
 9214:                 okSpares.push(include[k]);
 9215:             }
 9216:         }
 9217:         okSpares.sort();
 9218:         newSpare.options.length = 0;
 9219:         if (selIdx == 0) {
 9220:             newSpare.options[0] = new Option("$select","",true,true);
 9221:         } else {
 9222:             newSpare.options[0] = new Option("$select","",false,false);
 9223:         }
 9224:         for (var m=0; m<okSpares.length; m++) {
 9225:             var idx = m+1;
 9226:             var selThis = 0;
 9227:             if (selIdx != 0) {
 9228:                 if (okSpares[m] == currnew) {
 9229:                     selThis = 1;
 9230:                 }
 9231:             }
 9232:             if (selThis == 1) {
 9233:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 9234:             } else {
 9235:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 9236:             }
 9237:         }
 9238:     }
 9239:     return;
 9240: }
 9241: 
 9242: function checkNewSpares(lonhost,type) {
 9243:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 9244:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 9245:     if (chosen != '') { 
 9246:         var othertype;
 9247:         var othernewSpare;
 9248:         if (type == 'primary') {
 9249:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 9250:         }
 9251:         if (type == 'default') {
 9252:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 9253:         }
 9254:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 9255:             othernewSpare.selectedIndex = 0;
 9256:         }
 9257:     }
 9258:     return;
 9259: }
 9260: 
 9261: // ]]>
 9262: </script>
 9263: 
 9264: END
 9265: 
 9266: }
 9267: 
 9268: sub common_domprefs_js {
 9269:     return <<"END";
 9270: 
 9271: <script type="text/javascript">
 9272: // <![CDATA[
 9273: 
 9274: function getIndicesByName(formname,item) {
 9275:     var group = new Array();
 9276:     for (var i=0;i<formname.elements.length;i++) {
 9277:         if (formname.elements[i].name == item) {
 9278:             group.push(formname.elements[i].id);
 9279:         }
 9280:     }
 9281:     return group;
 9282: }
 9283: 
 9284: // ]]>
 9285: </script>
 9286: 
 9287: END
 9288: 
 9289: }
 9290: 
 9291: sub recaptcha_js {
 9292:     my %lt = &captcha_phrases();
 9293:     return <<"END";
 9294: 
 9295: <script type="text/javascript">
 9296: // <![CDATA[
 9297: 
 9298: function updateCaptcha(caller,context) {
 9299:     var privitem;
 9300:     var pubitem;
 9301:     var privtext;
 9302:     var pubtext;
 9303:     if (document.getElementById(context+'_recaptchapub')) {
 9304:         pubitem = document.getElementById(context+'_recaptchapub');
 9305:     } else {
 9306:         return;
 9307:     }
 9308:     if (document.getElementById(context+'_recaptchapriv')) {
 9309:         privitem = document.getElementById(context+'_recaptchapriv');
 9310:     } else {
 9311:         return;
 9312:     }
 9313:     if (document.getElementById(context+'_recaptchapubtxt')) {
 9314:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 9315:     } else {
 9316:         return;
 9317:     }
 9318:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 9319:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 9320:     } else {
 9321:         return;
 9322:     }
 9323:     if (caller.checked) {
 9324:         if (caller.value == 'recaptcha') {
 9325:             pubitem.type = 'text';
 9326:             privitem.type = 'text';
 9327:             pubitem.size = '40';
 9328:             privitem.size = '40';
 9329:             pubtext.innerHTML = "$lt{'pub'}";
 9330:             privtext.innerHTML = "$lt{'priv'}";
 9331:         } else {
 9332:             pubitem.type = 'hidden';
 9333:             privitem.type = 'hidden';
 9334:             pubtext.innerHTML = '';
 9335:             privtext.innerHTML = '';
 9336:         }
 9337:     }
 9338:     return;
 9339: }
 9340: 
 9341: // ]]>
 9342: </script>
 9343: 
 9344: END
 9345: 
 9346: }
 9347: 
 9348: sub credits_js {
 9349:     return <<"END";
 9350: 
 9351: <script type="text/javascript">
 9352: // <![CDATA[
 9353: 
 9354: function toggleCredits(domForm) {
 9355:     if (document.getElementById('credits')) {
 9356:         creditsitem = document.getElementById('credits');
 9357:         var creditsLength = domForm.coursecredits.length;
 9358:         if (creditsLength) {
 9359:             var currval;
 9360:             for (var i=0; i<creditsLength; i++) {
 9361:                 if (domForm.coursecredits[i].checked) {
 9362:                    currval = domForm.coursecredits[i].value;
 9363:                 }
 9364:             }
 9365:             if (currval == 1) {
 9366:                 creditsitem.style.display = 'block';
 9367:             } else {
 9368:                 creditsitem.style.display = 'none';
 9369:             }
 9370:         }
 9371:     }
 9372:     return;
 9373: }
 9374: 
 9375: // ]]>
 9376: </script>
 9377: 
 9378: END
 9379: 
 9380: }
 9381: 
 9382: sub captcha_phrases {
 9383:     return &Apache::lonlocal::texthash (
 9384:                  priv => 'Private key',
 9385:                  pub  => 'Public key',
 9386:                  original  => 'original (CAPTCHA)',
 9387:                  recaptcha => 'successor (ReCAPTCHA)',
 9388:                  notused   => 'unused',
 9389:     );
 9390: }
 9391: 
 9392: sub devalidate_remote_domconfs {
 9393:     my ($dom,$cachekeys) = @_;
 9394:     return unless (ref($cachekeys) eq 'HASH');
 9395:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9396:     my %thismachine;
 9397:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
 9398:     my @posscached = ('domainconfig','domdefaults');
 9399:     if (keys(%servers) > 1) {
 9400:         foreach my $server (keys(%servers)) {
 9401:             next if ($thismachine{$server});
 9402:             my @cached;
 9403:             foreach my $name (@posscached) {
 9404:                 if ($cachekeys->{$name}) {
 9405:                     push(@cached,&escape($name).':'.&escape($dom));
 9406:                 }
 9407:             }
 9408:             if (@cached) {
 9409:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
 9410:             }
 9411:         }
 9412:     }
 9413:     return;
 9414: }
 9415: 
 9416: 1;

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