File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.186: download - view: text, annotated - select for diffs
Wed Jan 9 21:30:27 2013 UTC (11 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Modification to changes in rev 1.174.
  - Eliminate blank line.
  - Remove commented out line (no longer used).

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.186 2013/01/09 21:30:27 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affiliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas,  
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course.
   98: 
   99: Outputs: 1
  100: 
  101: $datatable  - HTML containing form elements which allow settings to be changed. 
  102: 
  103: In the case of course requests, radio buttons are displayed for each institutional
  104: affiliate type (and also default, and _LC_adv) for each of the course types 
  105: (official, unofficial and community).  In each case the radio buttons allow the 
  106: selection of one of four values:
  107: 
  108: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  109: which have the following effects:
  110: 
  111: 0
  112: 
  113: =over
  114: 
  115: - course requests are not allowed for this course types/affiliation
  116: 
  117: =back
  118: 
  119: approval 
  120: 
  121: =over 
  122: 
  123: - course requests must be approved by a Doman Coordinator in the 
  124: course's domain
  125: 
  126: =back
  127: 
  128: validate 
  129: 
  130: =over
  131: 
  132: - an institutional validation (e.g., check requestor is instructor
  133: of record) needs to be passed before the course will be created.  The required
  134: validation is in localenroll.pm on the primary library server for the course 
  135: domain.
  136: 
  137: =back
  138: 
  139: autolimit 
  140: 
  141: =over
  142:  
  143: - course requests will be processed automatically up to a limit of
  144: N requests for the course type for the particular requestor.
  145: If N is undefined, there is no limit to the number of course requests
  146: which a course owner may submit and have processed automatically. 
  147: 
  148: =back
  149: 
  150: =item modify_quotas() 
  151: 
  152: =back
  153: 
  154: =cut
  155: 
  156: package Apache::domainprefs;
  157: 
  158: use strict;
  159: use Apache::Constants qw(:common :http);
  160: use Apache::lonnet;
  161: use Apache::loncommon();
  162: use Apache::lonhtmlcommon();
  163: use Apache::lonlocal;
  164: use Apache::lonmsg();
  165: use Apache::lonconfigsettings;
  166: use LONCAPA qw(:DEFAULT :match);
  167: use LONCAPA::Enrollment;
  168: use LONCAPA::lonauthcgi();
  169: use File::Copy;
  170: use Locale::Language;
  171: use DateTime::TimeZone;
  172: use DateTime::Locale;
  173: 
  174: my $registered_cleanup;
  175: my $modified_urls;
  176: 
  177: sub handler {
  178:     my $r=shift;
  179:     if ($r->header_only) {
  180:         &Apache::loncommon::content_type($r,'text/html');
  181:         $r->send_http_header;
  182:         return OK;
  183:     }
  184: 
  185:     my $context = 'domain';
  186:     my $dom = $env{'request.role.domain'};
  187:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  188:     if (&Apache::lonnet::allowed('mau',$dom)) {
  189:         &Apache::loncommon::content_type($r,'text/html');
  190:         $r->send_http_header;
  191:     } else {
  192:         $env{'user.error.msg'}=
  193:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  194:         return HTTP_NOT_ACCEPTABLE;
  195:     }
  196: 
  197:     $registered_cleanup=0;
  198:     @{$modified_urls}=();
  199: 
  200:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  201:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  202:                                             ['phase','actions']);
  203:     my $phase = 'pickactions';
  204:     if ( exists($env{'form.phase'}) ) {
  205:         $phase = $env{'form.phase'};
  206:     }
  207:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  208:     my %domconfig =
  209:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  210:                 'quotas','autoenroll','autoupdate','autocreate',
  211:                 'directorysrch','usercreation','usermodification',
  212:                 'contacts','defaults','scantron','coursecategories',
  213:                 'serverstatuses','requestcourses','helpsettings',
  214:                 'coursedefaults','usersessions','loadbalancing',
  215:                 'requestauthor'],$dom);
  216:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  217:                        'autoupdate','autocreate','directorysrch','contacts',
  218:                        'usercreation','usermodification','scantron',
  219:                        'requestcourses','requestauthor','coursecategories',
  220:                        'serverstatuses','helpsettings',
  221:                        'coursedefaults','usersessions');
  222:     my %existing;
  223:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  224:         %existing = %{$domconfig{'loadbalancing'}};
  225:     }
  226:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  227:         push(@prefs_order,'loadbalancing');
  228:     }
  229:     my %prefs = (
  230:         'rolecolors' =>
  231:                    { text => 'Default color schemes',
  232:                      help => 'Domain_Configuration_Color_Schemes',
  233:                      header => [{col1 => 'Student Settings',
  234:                                  col2 => '',},
  235:                                 {col1 => 'Coordinator Settings',
  236:                                  col2 => '',},
  237:                                 {col1 => 'Author Settings',
  238:                                  col2 => '',},
  239:                                 {col1 => 'Administrator Settings',
  240:                                  col2 => '',}],
  241:                     },
  242:         'login' =>
  243:                     { text => 'Log-in page options',
  244:                       help => 'Domain_Configuration_Login_Page',
  245:                       header => [{col1 => 'Log-in Page Items',
  246:                                   col2 => '',},
  247:                                  {col1 => 'Log-in Help',
  248:                                   col2 => 'Value'}],
  249:                     },
  250:         'defaults' => 
  251:                     { text => 'Default authentication/language/timezone/portal',
  252:                       help => 'Domain_Configuration_LangTZAuth',
  253:                       header => [{col1 => 'Setting',
  254:                                   col2 => 'Value'}],
  255:                     },
  256:         'quotas' => 
  257:                     { text => 'Blogs, personal web pages, webDAV, portfolios',
  258:                       help => 'Domain_Configuration_Quotas',
  259:                       header => [{col1 => 'User affiliation',
  260:                                   col2 => 'Available tools',
  261:                                   col3 => 'Portfolio quota',}],
  262:                     },
  263:         'autoenroll' =>
  264:                    { text => 'Auto-enrollment settings',
  265:                      help => 'Domain_Configuration_Auto_Enrollment',
  266:                      header => [{col1 => 'Configuration setting',
  267:                                  col2 => 'Value(s)'}],
  268:                    },
  269:         'autoupdate' => 
  270:                    { text => 'Auto-update settings',
  271:                      help => 'Domain_Configuration_Auto_Updates',
  272:                      header => [{col1 => 'Setting',
  273:                                  col2 => 'Value',},
  274:                                 {col1 => 'Setting',
  275:                                  col2 => 'Affiliation'},
  276:                                 {col1 => 'User population',
  277:                                  col2 => 'Updateable user data'}],
  278:                   },
  279:         'autocreate' => 
  280:                   { text => 'Auto-course creation settings',
  281:                      help => 'Domain_Configuration_Auto_Creation',
  282:                      header => [{col1 => 'Configuration Setting',
  283:                                  col2 => 'Value',}],
  284:                   },
  285:         'directorysrch' => 
  286:                   { text => 'Institutional directory searches',
  287:                     help => 'Domain_Configuration_InstDirectory_Search',
  288:                     header => [{col1 => 'Setting',
  289:                                 col2 => 'Value',}],
  290:                   },
  291:         'contacts' =>
  292:                   { text => 'Contact Information',
  293:                     help => 'Domain_Configuration_Contact_Info',
  294:                     header => [{col1 => 'Setting',
  295:                                 col2 => 'Value',}],
  296:                   },
  297: 
  298:         'usercreation' => 
  299:                   { text => 'User creation',
  300:                     help => 'Domain_Configuration_User_Creation',
  301:                     header => [{col1 => 'Format rule type',
  302:                                 col2 => 'Format rules in force'},
  303:                                {col1 => 'User account creation',
  304:                                 col2 => 'Usernames which may be created',},
  305:                                {col1 => 'Context',
  306:                                 col2 => 'Assignable authentication types'}],
  307:                   },
  308:         'usermodification' =>
  309:                   { text => 'User modification',
  310:                     help => 'Domain_Configuration_User_Modification',
  311:                     header => [{col1 => 'Target user has role',
  312:                                 col2 => 'User information updateable in author context'},
  313:                                {col1 => 'Target user has role',
  314:                                 col2 => 'User information updateable in course context'},
  315:                                {col1 => "Status of user",
  316:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  317:                   },
  318:         'scantron' =>
  319:                   { text => 'Bubblesheet format file',
  320:                     help => 'Domain_Configuration_Scantron_Format',
  321:                     header => [ {col1 => 'Item',
  322:                                  col2 => '',
  323:                               }],
  324:                   },
  325:         'requestcourses' => 
  326:                  {text => 'Request creation of courses',
  327:                   help => 'Domain_Configuration_Request_Courses',
  328:                   header => [{col1 => 'User affiliation',
  329:                               col2 => 'Availability/Processing of requests',},
  330:                              {col1 => 'Setting',
  331:                               col2 => 'Value'}],
  332:                  },
  333:         'requestauthor' =>
  334:                  {text => 'Request authoring space',
  335:                   help => 'Domain_Configuration_Request_Author',
  336:                   header => [{col1 => 'User affiliation',
  337:                               col2 => 'Availability/Processing of requests',},
  338:                              {col1 => 'Setting',
  339:                               col2 => 'Value'}],
  340:                  },
  341:         'coursecategories' =>
  342:                   { text => 'Cataloging of courses/communities',
  343:                     help => 'Domain_Configuration_Cataloging_Courses',
  344:                     header => [{col1 => 'Category settings',
  345:                                 col2 => '',},
  346:                                {col1 => 'Categories',
  347:                                 col2 => '',
  348:                                }],
  349:                   },
  350:         'serverstatuses' =>
  351:                  {text   => 'Access to server status pages',
  352:                   help   => 'Domain_Configuration_Server_Status',
  353:                   header => [{col1 => 'Status Page',
  354:                               col2 => 'Other named users',
  355:                               col3 => 'Specific IPs',
  356:                             }],
  357:                  },
  358:         'helpsettings' =>
  359:                  {text   => 'Help page settings',
  360:                   help   => 'Domain_Configuration_Help_Settings',
  361:                   header => [{col1 => 'Help Settings (logged-in users)',
  362:                               col2 => 'Value'}],
  363:                  },
  364:         'coursedefaults' => 
  365:                  {text => 'Course/Community defaults',
  366:                   help => 'Domain_Configuration_Course_Defaults',
  367:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  368:                               col2 => 'Value',},
  369:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  370:                               col2 => 'Value',},],
  371:                  },
  372:         'privacy' => 
  373:                  {text   => 'User Privacy',
  374:                   help   => 'Domain_Configuration_User_Privacy',
  375:                   header => [{col1 => 'Setting',
  376:                               col2 => 'Value',}],
  377:                  },
  378:         'usersessions' =>
  379:                  {text  => 'User session hosting/offloading',
  380:                   help  => 'Domain_Configuration_User_Sessions',
  381:                   header => [{col1 => 'Domain server',
  382:                               col2 => 'Servers to offload sessions to when busy'},
  383:                              {col1 => 'Hosting of users from other domains',
  384:                               col2 => 'Rules'},
  385:                              {col1 => "Hosting domain's own users elsewhere",
  386:                               col2 => 'Rules'}],
  387:                  },
  388:          'loadbalancing' =>
  389:                  {text  => 'Dedicated Load Balancer(s)',
  390:                   help  => 'Domain_Configuration_Load_Balancing',
  391:                   header => [{col1 => 'Balancers',
  392:                               col2 => 'Default destinations',
  393:                               col3 => 'User affiliation',
  394:                               col4 => 'Overrides'},
  395:                             ],
  396:                  },
  397:     );
  398:     if (keys(%servers) > 1) {
  399:         $prefs{'login'}  = { text   => 'Log-in page options',
  400:                              help   => 'Domain_Configuration_Login_Page',
  401:                             header => [{col1 => 'Log-in Service',
  402:                                         col2 => 'Server Setting',},
  403:                                        {col1 => 'Log-in Page Items',
  404:                                         col2 => ''},
  405:                                        {col1 => 'Log-in Help',
  406:                                         col2 => 'Value'}],
  407:                            };
  408:     }
  409: 
  410: 
  411: 
  412:     my @roles = ('student','coordinator','author','admin');
  413:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  414:     &Apache::lonhtmlcommon::add_breadcrumb
  415:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  416:       text=>"Settings to display/modify"});
  417:     my $confname = $dom.'-domainconfig';
  418: 
  419:     if ($phase eq 'process') {
  420:         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
  421:     } elsif ($phase eq 'display') {
  422:         my $js = &recaptcha_js();
  423:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  424:             my ($othertitle,$usertypes,$types) =
  425:                 &Apache::loncommon::sorted_inst_types($dom);
  426:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  427:                                           $domconfig{'loadbalancing'}).
  428:                    &new_spares_js().
  429:                    &common_domprefs_js().
  430:                    &Apache::loncommon::javascript_array_indexof();
  431:         }
  432:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  433:     } else {
  434: # check if domconfig user exists for the domain.
  435:         my $servadm = $r->dir_config('lonAdmEMail');
  436:         my ($configuserok,$author_ok,$switchserver) =
  437:             &config_check($dom,$confname,$servadm);
  438:         unless ($configuserok eq 'ok') {
  439:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  440:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  441:                            $confname).
  442:                       '<br />'
  443:             );
  444:             if ($switchserver) {
  445:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  446:                           '<br />'.
  447:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  448:                           '<br />'.
  449:                           &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).
  450:                           '<br />'.
  451:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  452:                 );
  453:             } else {
  454:                 $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.').
  455:                           '<br />'.
  456:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  457:                 );
  458:             }
  459:             $r->print(&Apache::loncommon::end_page());
  460:             return OK;
  461:         }
  462:         if (keys(%domconfig) == 0) {
  463:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  464:             my @ids=&Apache::lonnet::current_machine_ids();
  465:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  466:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  467:                 my @loginimages = ('img','logo','domlogo','login');
  468:                 my $custom_img_count = 0;
  469:                 foreach my $img (@loginimages) {
  470:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  471:                         $custom_img_count ++;
  472:                     }
  473:                 }
  474:                 foreach my $role (@roles) {
  475:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  476:                         $custom_img_count ++;
  477:                     }
  478:                 }
  479:                 if ($custom_img_count > 0) {
  480:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  481:                     my $switch_server = &check_switchserver($dom,$confname);
  482:                     $r->print(
  483:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  484:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  485:     &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 />'.
  486:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  487:                     if ($switch_server) {
  488:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  489:                     }
  490:                     $r->print(&Apache::loncommon::end_page());
  491:                     return OK;
  492:                 }
  493:             }
  494:         }
  495: 
  496:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  497:     }
  498:     return OK;
  499: }
  500: 
  501: sub process_changes {
  502:     my ($r,$dom,$confname,$action,$roles,$values) = @_;
  503:     my %domconfig;
  504:     if (ref($values) eq 'HASH') {
  505:         %domconfig = %{$values};
  506:     }
  507:     my $output;
  508:     if ($action eq 'login') {
  509:         $output = &modify_login($r,$dom,$confname,%domconfig);
  510:     } elsif ($action eq 'rolecolors') {
  511:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  512:                                      %domconfig);
  513:     } elsif ($action eq 'quotas') {
  514:         $output = &modify_quotas($dom,$action,%domconfig);
  515:     } elsif ($action eq 'autoenroll') {
  516:         $output = &modify_autoenroll($dom,%domconfig);
  517:     } elsif ($action eq 'autoupdate') {
  518:         $output = &modify_autoupdate($dom,%domconfig);
  519:     } elsif ($action eq 'autocreate') {
  520:         $output = &modify_autocreate($dom,%domconfig);
  521:     } elsif ($action eq 'directorysrch') {
  522:         $output = &modify_directorysrch($dom,%domconfig);
  523:     } elsif ($action eq 'usercreation') {
  524:         $output = &modify_usercreation($dom,%domconfig);
  525:     } elsif ($action eq 'usermodification') {
  526:         $output = &modify_usermodification($dom,%domconfig);
  527:     } elsif ($action eq 'contacts') {
  528:         $output = &modify_contacts($dom,%domconfig);
  529:     } elsif ($action eq 'defaults') {
  530:         $output = &modify_defaults($dom,$r);
  531:     } elsif ($action eq 'scantron') {
  532:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  533:     } elsif ($action eq 'coursecategories') {
  534:         $output = &modify_coursecategories($dom,%domconfig);
  535:     } elsif ($action eq 'serverstatuses') {
  536:         $output = &modify_serverstatuses($dom,%domconfig);
  537:     } elsif ($action eq 'requestcourses') {
  538:         $output = &modify_quotas($dom,$action,%domconfig);
  539:     } elsif ($action eq 'requestauthor') {
  540:         $output = &modify_quotas($dom,$action,%domconfig);
  541:     } elsif ($action eq 'helpsettings') {
  542:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  543:     } elsif ($action eq 'coursedefaults') {
  544:         $output = &modify_coursedefaults($dom,%domconfig);
  545:     } elsif ($action eq 'usersessions') {
  546:         $output = &modify_usersessions($dom,%domconfig);
  547:     } elsif ($action eq 'loadbalancing') {
  548:         $output = &modify_loadbalancing($dom,%domconfig);
  549:     }
  550:     return $output;
  551: }
  552: 
  553: sub print_config_box {
  554:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  555:     my $rowtotal = 0;
  556:     my $output;
  557: 
  558:     if ($action eq 'coursecategories') {
  559:         $output = &coursecategories_javascript($settings);
  560:     }
  561:     $output .= 
  562:          '<table class="LC_nested_outer">
  563:           <tr>
  564:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  565:            &mt($item->{text}).'&nbsp;'.
  566:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  567:           '</tr>';
  568:     $rowtotal ++;
  569:     my $numheaders = 1;
  570:     if (ref($item->{'header'}) eq 'ARRAY') {
  571:         $numheaders = scalar(@{$item->{'header'}});
  572:     }
  573:     if ($numheaders > 1) {
  574:         my $colspan = '';
  575:         my $rightcolspan = '';
  576:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') || 
  577:             (($action eq 'login') && ($numheaders < 3))) {
  578:             $colspan = ' colspan="2"';
  579:         }
  580:         if ($action eq 'usersessions') {
  581:             $rightcolspan = ' colspan="3"'; 
  582:         }
  583:         $output .= '
  584:           <tr>
  585:            <td>
  586:             <table class="LC_nested">
  587:              <tr class="LC_info_row">
  588:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  589:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  590:              </tr>';
  591:         $rowtotal ++;
  592:         if ($action eq 'autoupdate') {
  593:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  594:         } elsif ($action eq 'usercreation') {
  595:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  596:         } elsif ($action eq 'usermodification') {
  597:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  598:         } elsif ($action eq 'coursecategories') {
  599:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  600:         } elsif ($action eq 'login') {
  601:             if ($numheaders == 3) {
  602:                 $colspan = ' colspan="2"';
  603:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  604:             } else {
  605:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  606:             }
  607:         } elsif ($action eq 'requestcourses') {
  608:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  609:         } elsif ($action eq 'requestauthor') {
  610:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  611:         } elsif ($action eq 'usersessions') {
  612:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  613:         } elsif ($action eq 'rolecolors') {
  614:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  615:         } elsif ($action eq 'coursedefaults') {
  616:             $output .= &print_coursedefaults('top',$dom,$settings,\$rowtotal);
  617:         }
  618:         $output .= '
  619:            </table>
  620:           </td>
  621:          </tr>
  622:          <tr>
  623:            <td>
  624:             <table class="LC_nested">
  625:              <tr class="LC_info_row">
  626:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  627:         $output .= '
  628:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  629:              </tr>';
  630:             $rowtotal ++;
  631:         if ($action eq 'autoupdate') {
  632:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  633:            </table>
  634:           </td>
  635:          </tr>
  636:          <tr>
  637:            <td>
  638:             <table class="LC_nested">
  639:              <tr class="LC_info_row">
  640:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  641:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  642:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  643:             $rowtotal ++;
  644:         } elsif ($action eq 'usercreation') {
  645:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  646:            </table>
  647:           </td>
  648:          </tr>
  649:          <tr>
  650:            <td>
  651:             <table class="LC_nested">
  652:              <tr class="LC_info_row">
  653:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  654:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  655:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  656:             $rowtotal ++;
  657:         } elsif ($action eq 'usermodification') {
  658:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  659:            </table>
  660:           </td>
  661:          </tr>
  662:          <tr>
  663:            <td>
  664:             <table class="LC_nested">
  665:              <tr class="LC_info_row">
  666:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  667:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  668:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  669:             $rowtotal ++;
  670:         } elsif ($action eq 'coursecategories') {
  671:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  672:         } elsif ($action eq 'login') {
  673:             if ($numheaders == 3) {
  674:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  675:            </table>
  676:           </td>
  677:          </tr>
  678:          <tr>
  679:            <td>
  680:             <table class="LC_nested">
  681:              <tr class="LC_info_row">
  682:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  683:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  684:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  685:                 $rowtotal ++;
  686:             } else {
  687:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  688:             }
  689:         } elsif ($action eq 'requestcourses') {
  690:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  691:         } elsif ($action eq 'requestauthor') {
  692:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  693:         } elsif ($action eq 'usersessions') {
  694:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  695:            </table>
  696:           </td>
  697:          </tr>
  698:          <tr>
  699:            <td>
  700:             <table class="LC_nested">
  701:              <tr class="LC_info_row">
  702:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  703:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  704:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  705:             $rowtotal ++;
  706:         } elsif ($action eq 'coursedefaults') {
  707:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  708:         } elsif ($action eq 'rolecolors') {
  709:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  710:            </table>
  711:           </td>
  712:          </tr>
  713:          <tr>
  714:            <td>
  715:             <table class="LC_nested">
  716:              <tr class="LC_info_row">
  717:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  718:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  719:               <td class="LC_right_item" valign="top">'.
  720:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  721:              </tr>'.
  722:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  723:            </table>
  724:           </td>
  725:          </tr>
  726:          <tr>
  727:            <td>
  728:             <table class="LC_nested">
  729:              <tr class="LC_info_row">
  730:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  731:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  732:              </tr>'.
  733:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  734:             $rowtotal += 2;
  735:         }
  736:     } else {
  737:         $output .= '
  738:           <tr>
  739:            <td>
  740:             <table class="LC_nested">
  741:              <tr class="LC_info_row">';
  742:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  743:             $output .= '  
  744:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  745:         } elsif ($action eq 'serverstatuses') {
  746:             $output .= '
  747:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  748:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  749: 
  750:         } else {
  751:             $output .= '
  752:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  753:         }
  754:         if (defined($item->{'header'}->[0]->{'col3'})) {
  755:             $output .= '<td class="LC_left_item" valign="top">'.
  756:                        &mt($item->{'header'}->[0]->{'col2'});
  757:             if ($action eq 'serverstatuses') {
  758:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  759:             } 
  760:         } else {
  761:             $output .= '<td class="LC_right_item" valign="top">'.
  762:                        &mt($item->{'header'}->[0]->{'col2'});
  763:         }
  764:         $output .= '</td>';
  765:         if ($item->{'header'}->[0]->{'col3'}) {
  766:             if (defined($item->{'header'}->[0]->{'col4'})) {
  767:                 $output .= '<td class="LC_left_item" valign="top">'.
  768:                             &mt($item->{'header'}->[0]->{'col3'});
  769:             } else {
  770:                 $output .= '<td class="LC_right_item" valign="top">'.
  771:                            &mt($item->{'header'}->[0]->{'col3'});
  772:             }
  773:             if ($action eq 'serverstatuses') {
  774:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  775:             }
  776:             $output .= '</td>';
  777:         }
  778:         if ($item->{'header'}->[0]->{'col4'}) {
  779:             $output .= '<td class="LC_right_item" valign="top">'.
  780:                        &mt($item->{'header'}->[0]->{'col4'});
  781:         }
  782:         $output .= '</tr>';
  783:         $rowtotal ++;
  784:         if ($action eq 'quotas') {
  785:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  786:         } elsif ($action eq 'autoenroll') {
  787:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  788:         } elsif ($action eq 'autocreate') {
  789:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  790:         } elsif ($action eq 'directorysrch') {
  791:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  792:         } elsif ($action eq 'contacts') {
  793:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  794:         } elsif ($action eq 'defaults') {
  795:             $output .= &print_defaults($dom,\$rowtotal);
  796:         } elsif ($action eq 'scantron') {
  797:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  798:         } elsif ($action eq 'serverstatuses') {
  799:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  800:         } elsif ($action eq 'helpsettings') {
  801:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  802:         } elsif ($action eq 'loadbalancing') {
  803:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  804:         }
  805:     }
  806:     $output .= '
  807:    </table>
  808:   </td>
  809:  </tr>
  810: </table><br />';
  811:     return ($output,$rowtotal);
  812: }
  813: 
  814: sub print_login {
  815:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  816:     my ($css_class,$datatable);
  817:     my %choices = &login_choices();
  818: 
  819:     if ($caller eq 'service') {
  820:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  821:         my $choice = $choices{'disallowlogin'};
  822:         $css_class = ' class="LC_odd_row"';
  823:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  824:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  825:                       '<th>'.$choices{'server'}.'</th>'.
  826:                       '<th>'.$choices{'serverpath'}.'</th>'.
  827:                       '<th>'.$choices{'custompath'}.'</th>'.
  828:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  829:         my %disallowed;
  830:         if (ref($settings) eq 'HASH') {
  831:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  832:                %disallowed = %{$settings->{'loginvia'}};
  833:             }
  834:         }
  835:         foreach my $lonhost (sort(keys(%servers))) {
  836:             my $direct = 'selected="selected"';
  837:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  838:                 if ($disallowed{$lonhost}{'server'} ne '') {
  839:                     $direct = '';
  840:                 }
  841:             }
  842:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  843:                           '<td><select name="'.$lonhost.'_server">'.
  844:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  845:                           '</option>';
  846:             foreach my $hostid (sort(keys(%servers))) {
  847:                 next if ($servers{$hostid} eq $servers{$lonhost});
  848:                 my $selected = '';
  849:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  850:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  851:                         $selected = 'selected="selected"';
  852:                     }
  853:                 }
  854:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  855:                               $servers{$hostid}.'</option>';
  856:             }
  857:             $datatable .= '</select></td>'.
  858:                           '<td><select name="'.$lonhost.'_serverpath">';
  859:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  860:                 my $pathname = $path;
  861:                 if ($path eq 'custom') {
  862:                     $pathname = &mt('Custom Path').' ->';
  863:                 }
  864:                 my $selected = '';
  865:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  866:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  867:                         $selected = 'selected="selected"';
  868:                     }
  869:                 } elsif ($path eq '') {
  870:                     $selected = 'selected="selected"';
  871:                 }
  872:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  873:             }
  874:             $datatable .= '</select></td>';
  875:             my ($custom,$exempt);
  876:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  877:                 $custom = $disallowed{$lonhost}{'custompath'};
  878:                 $exempt = $disallowed{$lonhost}{'exempt'};
  879:             }
  880:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  881:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  882:                           '</tr>';
  883:         }
  884:         $datatable .= '</table></td></tr>';
  885:         return $datatable;
  886:     } elsif ($caller eq 'page') {
  887:         my %defaultchecked = ( 
  888:                                'coursecatalog' => 'on',
  889:                                'adminmail'     => 'off',
  890:                                'newuser'       => 'off',
  891:                              );
  892:         my @toggles = ('coursecatalog','adminmail','newuser');
  893:         my (%checkedon,%checkedoff);
  894:         foreach my $item (@toggles) {
  895:             if ($defaultchecked{$item} eq 'on') { 
  896:                 $checkedon{$item} = ' checked="checked" ';
  897:                 $checkedoff{$item} = ' ';
  898:             } elsif ($defaultchecked{$item} eq 'off') {
  899:                 $checkedoff{$item} = ' checked="checked" ';
  900:                 $checkedon{$item} = ' ';
  901:             }
  902:         }
  903:         my @images = ('img','logo','domlogo','login');
  904:         my @logintext = ('textcol','bgcol');
  905:         my @bgs = ('pgbg','mainbg','sidebg');
  906:         my @links = ('link','alink','vlink');
  907:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  908:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  909:         my (%is_custom,%designs);
  910:         my %defaults = (
  911:                        font => $defaultdesign{'login.font'},
  912:                        );
  913:         foreach my $item (@images) {
  914:             $defaults{$item} = $defaultdesign{'login.'.$item};
  915:             $defaults{'showlogo'}{$item} = 1;
  916:         }
  917:         foreach my $item (@bgs) {
  918:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  919:         }
  920:         foreach my $item (@logintext) {
  921:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  922:         }
  923:         foreach my $item (@links) {
  924:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  925:         }
  926:         if (ref($settings) eq 'HASH') {
  927:             foreach my $item (@toggles) {
  928:                 if ($settings->{$item} eq '1') {
  929:                     $checkedon{$item} =  ' checked="checked" ';
  930:                     $checkedoff{$item} = ' ';
  931:                 } elsif ($settings->{$item} eq '0') {
  932:                     $checkedoff{$item} =  ' checked="checked" ';
  933:                     $checkedon{$item} = ' ';
  934:                 }
  935:             }
  936:             foreach my $item (@images) {
  937:                 if (defined($settings->{$item})) {
  938:                     $designs{$item} = $settings->{$item};
  939:                     $is_custom{$item} = 1;
  940:                 }
  941:                 if (defined($settings->{'showlogo'}{$item})) {
  942:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  943:                 }
  944:             }
  945:             foreach my $item (@logintext) {
  946:                 if ($settings->{$item} ne '') {
  947:                     $designs{'logintext'}{$item} = $settings->{$item};
  948:                     $is_custom{$item} = 1;
  949:                 }
  950:             }
  951:             if ($settings->{'font'} ne '') {
  952:                 $designs{'font'} = $settings->{'font'};
  953:                 $is_custom{'font'} = 1;
  954:             }
  955:             foreach my $item (@bgs) {
  956:                 if ($settings->{$item} ne '') {
  957:                     $designs{'bgs'}{$item} = $settings->{$item};
  958:                     $is_custom{$item} = 1;
  959:                 }
  960:             }
  961:             foreach my $item (@links) {
  962:                 if ($settings->{$item} ne '') {
  963:                     $designs{'links'}{$item} = $settings->{$item};
  964:                     $is_custom{$item} = 1;
  965:                 }
  966:             }
  967:         } else {
  968:             if ($designhash{$dom.'.login.font'} ne '') {
  969:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  970:                 $is_custom{'font'} = 1;
  971:             }
  972:             foreach my $item (@images) {
  973:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  974:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  975:                     $is_custom{$item} = 1;
  976:                 }
  977:             }
  978:             foreach my $item (@bgs) {
  979:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  980:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  981:                     $is_custom{$item} = 1;
  982:                 }
  983:             }
  984:             foreach my $item (@links) {
  985:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  986:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  987:                     $is_custom{$item} = 1;
  988:                 }
  989:             }
  990:         }
  991:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  992:                                                       logo => 'Institution Logo',
  993:                                                       domlogo => 'Domain Logo',
  994:                                                       login => 'Login box');
  995:         my $itemcount = 1;
  996:         foreach my $item (@toggles) {
  997:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
  998:             $datatable .=  
  999:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1000:                 '</td><td>'.
 1001:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1002:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1003:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1004:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1005:                 '</tr>';
 1006:             $itemcount ++;
 1007:         }
 1008:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1009:         $datatable .= '</tr></table></td></tr>';
 1010:     } elsif ($caller eq 'help') {
 1011:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1012:         my $switchserver = &check_switchserver($dom,$confname);
 1013:         my $itemcount = 1;
 1014:         $defaulturl = '/adm/loginproblems.html';
 1015:         $defaulttype = 'default';
 1016:         %lt = &Apache::lonlocal::texthash (
 1017:                      del     => 'Delete?',
 1018:                      rep     => 'Replace:',
 1019:                      upl     => 'Upload:',
 1020:                      default => 'Default',
 1021:                      custom  => 'Custom',
 1022:                                              );
 1023:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1024:         my @currlangs;
 1025:         if (ref($settings) eq 'HASH') {
 1026:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1027:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1028:                     next if ($settings->{'helpurl'}{$key} eq '');
 1029:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1030:                     $type{$key} = 'custom';
 1031:                     unless ($key eq 'nolang') {
 1032:                         push(@currlangs,$key);
 1033:                     }
 1034:                 }
 1035:             } elsif ($settings->{'helpurl'} ne '') {
 1036:                 $type{'nolang'} = 'custom';
 1037:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1038:             }
 1039:         }
 1040:         foreach my $lang ('nolang',sort(@currlangs)) {
 1041:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1042:             $datatable .= '<tr'.$css_class.'>';
 1043:             if ($url{$lang} eq '') {
 1044:                 $url{$lang} = $defaulturl;
 1045:             }
 1046:             if ($type{$lang} eq '') {
 1047:                 $type{$lang} = $defaulttype;
 1048:             }
 1049:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1050:             if ($lang eq 'nolang') {
 1051:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1052:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1053:             } else {
 1054:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1055:                                   $langchoices{$lang},
 1056:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1057:             }
 1058:             $datatable .= '</span></td>'."\n".
 1059:                           '<td class="LC_left_item">';
 1060:             if ($type{$lang} eq 'custom') {
 1061:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1062:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1063:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1064:             } else {
 1065:                 $datatable .= $lt{'upl'};
 1066:             }
 1067:             $datatable .='<br />';
 1068:             if ($switchserver) {
 1069:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1070:             } else {
 1071:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1072:             }
 1073:             $datatable .= '</td></tr>';
 1074:             $itemcount ++;
 1075:         }
 1076:         my @addlangs;
 1077:         foreach my $lang (sort(keys(%langchoices))) {
 1078:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1079:             push(@addlangs,$lang);
 1080:         }
 1081:         if (@addlangs > 0) {
 1082:             my %toadd;
 1083:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1084:             $toadd{''} = &mt('Select');
 1085:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1086:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1087:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1088:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1089:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1090:             if ($switchserver) {
 1091:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1092:             } else {
 1093:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1094:             }
 1095:             $datatable .= '</td></tr>';
 1096:             $itemcount ++;
 1097:         }
 1098:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1099:     }
 1100:     return $datatable;
 1101: }
 1102: 
 1103: sub login_choices {
 1104:     my %choices =
 1105:         &Apache::lonlocal::texthash (
 1106:             coursecatalog => 'Display Course/Community Catalog link?',
 1107:             adminmail     => "Display Administrator's E-mail Address?",
 1108:             disallowlogin => "Login page requests redirected",
 1109:             hostid        => "Server",
 1110:             server        => "Redirect to:",
 1111:             serverpath    => "Path",
 1112:             custompath    => "Custom", 
 1113:             exempt        => "Exempt IP(s)",
 1114:             directlogin   => "No redirect",
 1115:             newuser       => "Link to create a user account",
 1116:             img           => "Header",
 1117:             logo          => "Main Logo",
 1118:             domlogo       => "Domain Logo",
 1119:             login         => "Log-in Header", 
 1120:             textcol       => "Text color",
 1121:             bgcol         => "Box color",
 1122:             bgs           => "Background colors",
 1123:             links         => "Link colors",
 1124:             font          => "Font color",
 1125:             pgbg          => "Header",
 1126:             mainbg        => "Page",
 1127:             sidebg        => "Login box",
 1128:             link          => "Link",
 1129:             alink         => "Active link",
 1130:             vlink         => "Visited link",
 1131:         );
 1132:     return %choices;
 1133: }
 1134: 
 1135: sub print_rolecolors {
 1136:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1137:     my %choices = &color_font_choices();
 1138:     my @bgs = ('pgbg','tabbg','sidebg');
 1139:     my @links = ('link','alink','vlink');
 1140:     my @images = ('img');
 1141:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1142:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1143:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1144:     my (%is_custom,%designs);
 1145:     my %defaults = (
 1146:                    img => $defaultdesign{$role.'.img'},
 1147:                    font => $defaultdesign{$role.'.font'},
 1148: 		   fontmenu => $defaultdesign{$role.'.fontmenu'},
 1149:                    );
 1150:     foreach my $item (@bgs) {
 1151:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1152:     }
 1153:     foreach my $item (@links) {
 1154:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1155:     }
 1156:     if (ref($settings) eq 'HASH') {
 1157:         if (ref($settings->{$role}) eq 'HASH') {
 1158:             if ($settings->{$role}->{'img'} ne '') {
 1159:                 $designs{'img'} = $settings->{$role}->{'img'};
 1160:                 $is_custom{'img'} = 1;
 1161:             }
 1162:             if ($settings->{$role}->{'font'} ne '') {
 1163:                 $designs{'font'} = $settings->{$role}->{'font'};
 1164:                 $is_custom{'font'} = 1;
 1165:             }
 1166:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1167:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1168:                 $is_custom{'fontmenu'} = 1;
 1169:             }
 1170:             foreach my $item (@bgs) {
 1171:                 if ($settings->{$role}->{$item} ne '') {
 1172:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1173:                     $is_custom{$item} = 1;
 1174:                 }
 1175:             }
 1176:             foreach my $item (@links) {
 1177:                 if ($settings->{$role}->{$item} ne '') {
 1178:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1179:                     $is_custom{$item} = 1;
 1180:                 }
 1181:             }
 1182:         }
 1183:     } else {
 1184:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1185:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1186:             $is_custom{'img'} = 1;
 1187:         }
 1188:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1189:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1190:             $is_custom{'fontmenu'} = 1; 
 1191:         }
 1192:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1193:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1194:             $is_custom{'font'} = 1;
 1195:         }
 1196:         foreach my $item (@bgs) {
 1197:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1198:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1199:                 $is_custom{$item} = 1;
 1200:             
 1201:             }
 1202:         }
 1203:         foreach my $item (@links) {
 1204:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1205:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1206:                 $is_custom{$item} = 1;
 1207:             }
 1208:         }
 1209:     }
 1210:     my $itemcount = 1;
 1211:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1212:     $datatable .= '</tr></table></td></tr>';
 1213:     return $datatable;
 1214: }
 1215: 
 1216: sub display_color_options {
 1217:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1218:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1219:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1220:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1221:     my $datatable = '<tr'.$css_class.'>'.
 1222:         '<td>'.$choices->{'font'}.'</td>';
 1223:     if (!$is_custom->{'font'}) {
 1224:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1225:     } else {
 1226:         $datatable .= '<td>&nbsp;</td>';
 1227:     }
 1228:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1229: 
 1230:     $datatable .= '<td><span class="LC_nobreak">'.
 1231:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1232:                   ' value="'.$current_color.'" />&nbsp;'. 
 1233:                   '&nbsp;</td></tr>';
 1234:     unless ($role eq 'login') { 
 1235:         $datatable .= '<tr'.$css_class.'>'.
 1236:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1237:         if (!$is_custom->{'fontmenu'}) {
 1238:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1239:         } else {
 1240:             $datatable .= '<td>&nbsp;</td>';
 1241:         }
 1242: 	$current_color = $designs->{'fontmenu'} ? 
 1243: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1244:         $datatable .= '<td><span class="LC_nobreak">'.
 1245:                       '<input class="colorchooser" type="text" size="10" name="'
 1246: 		      .$role.'_fontmenu"'.
 1247:                       ' value="'.$current_color.'" />&nbsp;'.
 1248:                       '&nbsp;</td></tr>';
 1249:     }
 1250:     my $switchserver = &check_switchserver($dom,$confname);
 1251:     foreach my $img (@{$images}) {
 1252: 	$itemcount ++;
 1253:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1254:         $datatable .= '<tr'.$css_class.'>'.
 1255:                       '<td>'.$choices->{$img};
 1256:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1257:         if ($role eq 'login') {
 1258:             if ($img eq 'login') {
 1259:                 $login_hdr_pick =
 1260:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1261:                 $logincolors =
 1262:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1263:                                             $designs);
 1264:             } elsif ($img ne 'domlogo') {
 1265:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1266:             }
 1267:         }
 1268:         $datatable .= '</td>';
 1269:         if ($designs->{$img} ne '') {
 1270:             $imgfile = $designs->{$img};
 1271: 	    $img_import = ($imgfile =~ m{^/adm/});
 1272:         } else {
 1273:             $imgfile = $defaults->{$img};
 1274:         }
 1275:         if ($imgfile) {
 1276:             my ($showfile,$fullsize);
 1277:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1278:                 my $urldir = $1;
 1279:                 my $filename = $2;
 1280:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1281:                 if (@info) {
 1282:                     my $thumbfile = 'tn-'.$filename;
 1283:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1284:                     if (@thumb) {
 1285:                         $showfile = $urldir.'/'.$thumbfile;
 1286:                     } else {
 1287:                         $showfile = $imgfile;
 1288:                     }
 1289:                 } else {
 1290:                     $showfile = '';
 1291:                 }
 1292:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1293:                 $showfile = $imgfile;
 1294:                 my $imgdir = $1;
 1295:                 my $filename = $2;
 1296:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1297:                     $showfile = "/$imgdir/tn-".$filename;
 1298:                 } else {
 1299:                     my $input = $londocroot.$imgfile;
 1300:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1301:                     if (!-e $output) {
 1302:                         my ($width,$height) = &thumb_dimensions();
 1303:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1304:                         if ($fullwidth ne '' && $fullheight ne '') {
 1305:                             if ($fullwidth > $width && $fullheight > $height) { 
 1306:                                 my $size = $width.'x'.$height;
 1307:                                 system("convert -sample $size $input $output");
 1308:                                 $showfile = "/$imgdir/tn-".$filename;
 1309:                             }
 1310:                         }
 1311:                     }
 1312:                 }
 1313:             }
 1314:             if ($showfile) {
 1315:                 if ($showfile =~ m{^/(adm|res)/}) {
 1316:                     if ($showfile =~ m{^/res/}) {
 1317:                         my $local_showfile =
 1318:                             &Apache::lonnet::filelocation('',$showfile);
 1319:                         &Apache::lonnet::repcopy($local_showfile);
 1320:                     }
 1321:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1322:                 }
 1323:                 if ($imgfile) {
 1324:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1325:                         if ($imgfile =~ m{^/res/}) {
 1326:                             my $local_imgfile =
 1327:                                 &Apache::lonnet::filelocation('',$imgfile);
 1328:                             &Apache::lonnet::repcopy($local_imgfile);
 1329:                         }
 1330:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1331:                     } else {
 1332:                         $fullsize = $imgfile;
 1333:                     }
 1334:                 }
 1335:                 $datatable .= '<td>';
 1336:                 if ($img eq 'login') {
 1337:                     $datatable .= $login_hdr_pick;
 1338:                 } 
 1339:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1340:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1341:             } else {
 1342:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1343:                               &mt('Upload:');
 1344:             }
 1345:         } else {
 1346:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1347:                           &mt('Upload:');
 1348:         }
 1349:         if ($switchserver) {
 1350:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1351:         } else {
 1352:             if ($img ne 'login') { # suppress file selection for Log-in header
 1353:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1354:             }
 1355:         }
 1356:         $datatable .= '</td></tr>';
 1357:     }
 1358:     $itemcount ++;
 1359:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1360:     $datatable .= '<tr'.$css_class.'>'.
 1361:                   '<td>'.$choices->{'bgs'}.'</td>';
 1362:     my $bgs_def;
 1363:     foreach my $item (@{$bgs}) {
 1364:         if (!$is_custom->{$item}) {
 1365:             $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>';
 1366:         }
 1367:     }
 1368:     if ($bgs_def) {
 1369:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1370:     } else {
 1371:         $datatable .= '<td>&nbsp;</td>';
 1372:     }
 1373:     $datatable .= '<td class="LC_right_item">'.
 1374:                   '<table border="0"><tr>';
 1375: 
 1376:     foreach my $item (@{$bgs}) {
 1377:         $datatable .= '<td align="center">';
 1378: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1379:         if ($designs->{'bgs'}{$item}) {
 1380:             $datatable .= '&nbsp;';
 1381:         }
 1382:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1383:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1384:     }
 1385:     $datatable .= '</tr></table></td></tr>';
 1386:     $itemcount ++;
 1387:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1388:     $datatable .= '<tr'.$css_class.'>'.
 1389:                   '<td>'.$choices->{'links'}.'</td>';
 1390:     my $links_def;
 1391:     foreach my $item (@{$links}) {
 1392:         if (!$is_custom->{$item}) {
 1393:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1394:         }
 1395:     }
 1396:     if ($links_def) {
 1397:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1398:     } else {
 1399:         $datatable .= '<td>&nbsp;</td>';
 1400:     }
 1401:     $datatable .= '<td class="LC_right_item">'.
 1402:                   '<table border="0"><tr>';
 1403:     foreach my $item (@{$links}) {
 1404: 	my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1405:         $datatable .= '<td align="center">'."\n";
 1406: 
 1407:         if ($designs->{'links'}{$item}) {
 1408:             $datatable.='&nbsp;';
 1409:         }
 1410:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1411:                       '" /></td>';
 1412:     }
 1413:     $$rowtotal += $itemcount;
 1414:     return $datatable;
 1415: }
 1416: 
 1417: sub logo_display_options {
 1418:     my ($img,$defaults,$designs) = @_;
 1419:     my $checkedon;
 1420:     if (ref($defaults) eq 'HASH') {
 1421:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1422:             if ($defaults->{'showlogo'}{$img}) {
 1423:                 $checkedon = 'checked="checked" ';     
 1424:             }
 1425:         } 
 1426:     }
 1427:     if (ref($designs) eq 'HASH') {
 1428:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1429:             if (defined($designs->{'showlogo'}{$img})) {
 1430:                 if ($designs->{'showlogo'}{$img} == 0) {
 1431:                     $checkedon = '';
 1432:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1433:                     $checkedon = 'checked="checked" ';
 1434:                 }
 1435:             }
 1436:         }
 1437:     }
 1438:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1439:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1440:            &mt('show').'</label>'."\n";
 1441: }
 1442: 
 1443: sub login_header_options  {
 1444:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1445:     my $output = '';
 1446:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1447:         $output .= &mt('Text default(s):').'<br />';
 1448:         if (!$is_custom->{'textcol'}) {
 1449:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1450:                        '&nbsp;&nbsp;&nbsp;';
 1451:         }
 1452:         if (!$is_custom->{'bgcol'}) {
 1453:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1454:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1455:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1456:         }
 1457:         $output .= '<br />';
 1458:     }
 1459:     $output .='<br />';
 1460:     return $output;
 1461: }
 1462: 
 1463: sub login_text_colors {
 1464:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1465:     my $color_menu = '<table border="0"><tr>';
 1466:     foreach my $item (@{$logintext}) {
 1467:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1468:         $color_menu .= '<td align="center">'.$link;
 1469:         if ($designs->{'logintext'}{$item}) {
 1470:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1471:         }
 1472:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1473:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1474:                        '<td>&nbsp;</td>';
 1475:     }
 1476:     $color_menu .= '</tr></table><br />';
 1477:     return $color_menu;
 1478: }
 1479: 
 1480: sub image_changes {
 1481:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1482:     my $output;
 1483:     if ($img eq 'login') {
 1484:             # suppress image for Log-in header
 1485:     } elsif (!$is_custom) {
 1486:         if ($img ne 'domlogo') {
 1487:             $output .= &mt('Default image:').'<br />';
 1488:         } else {
 1489:             $output .= &mt('Default in use:').'<br />';
 1490:         }
 1491:     }
 1492:     if ($img eq 'login') { # suppress image for Log-in header
 1493:         $output .= '<td>'.$logincolors;
 1494:     } else {
 1495:         if ($img_import) {
 1496:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1497:         }
 1498:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1499:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1500:         if ($is_custom) {
 1501:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1502:                        '<input type="checkbox" name="'.
 1503:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1504:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1505:         } else {
 1506:             $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1507:         }
 1508:     }
 1509:     return $output;
 1510: }
 1511: 
 1512: sub color_pick {
 1513:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1514:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1515:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1516:                ');">'.$desc.'</a>';
 1517:     return $link;
 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,@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;
 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:                 }
 1557:             }
 1558:             if (defined($usertypes->{$type})) {
 1559:                 $typecount ++;
 1560:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1561:                 $datatable .= '<tr'.$css_class.'>'.
 1562:                               '<td>'.$usertypes->{$type}.'</td>'.
 1563:                               '<td class="LC_left_item">';
 1564:                 if ($context eq 'requestcourses') {
 1565:                     $datatable .= '<table><tr>';
 1566:                 }
 1567:                 my %cell;  
 1568:                 foreach my $item (@usertools) {
 1569:                     if ($context eq 'requestcourses') {
 1570:                         my ($curroption,$currlimit);
 1571:                         if (ref($settings) eq 'HASH') {
 1572:                             if (ref($settings->{$item}) eq 'HASH') {
 1573:                                 $curroption = $settings->{$item}->{$type};
 1574:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1575:                                     $currlimit = $1; 
 1576:                                 }
 1577:                             }
 1578:                         }
 1579:                         if (!$curroption) {
 1580:                             $curroption = 'norequest';
 1581:                         }
 1582:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1583:                         foreach my $option (@options) {
 1584:                             my $val = $option;
 1585:                             if ($option eq 'norequest') {
 1586:                                 $val = 0;  
 1587:                             }
 1588:                             if ($option eq 'validate') {
 1589:                                 my $canvalidate = 0;
 1590:                                 if (ref($validations{$item}) eq 'HASH') { 
 1591:                                     if ($validations{$item}{$type}) {
 1592:                                         $canvalidate = 1;
 1593:                                     }
 1594:                                 }
 1595:                                 next if (!$canvalidate);
 1596:                             }
 1597:                             my $checked = '';
 1598:                             if ($option eq $curroption) {
 1599:                                 $checked = ' checked="checked"';
 1600:                             } elsif ($option eq 'autolimit') {
 1601:                                 if ($curroption =~ /^autolimit/) {
 1602:                                     $checked = ' checked="checked"';
 1603:                                 }                       
 1604:                             } 
 1605:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1606:                                   '<input type="radio" name="crsreq_'.$item.
 1607:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1608:                                   $titles{$option}.'</label>';
 1609:                             if ($option eq 'autolimit') {
 1610:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1611:                                                 $item.'_limit_'.$type.'" size="1" '.
 1612:                                                 'value="'.$currlimit.'" />';
 1613:                             }
 1614:                             $cell{$item} .= '</span> ';
 1615:                             if ($option eq 'autolimit') {
 1616:                                 $cell{$item} .= $titles{'unlimited'};
 1617:                             }
 1618:                         }
 1619:                     } elsif ($context eq 'requestauthor') {
 1620:                         my $curroption;
 1621:                         if (ref($settings) eq 'HASH') {
 1622:                             $curroption = $settings->{$type};
 1623:                         }
 1624:                         if (!$curroption) {
 1625:                             $curroption = 'norequest';
 1626:                         }
 1627:                         foreach my $option (@options) {
 1628:                             my $val = $option;
 1629:                             if ($option eq 'norequest') {
 1630:                                 $val = 0;
 1631:                             }
 1632:                             my $checked = '';
 1633:                             if ($option eq $curroption) {
 1634:                                 $checked = ' checked="checked"';
 1635:                             }
 1636:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1637:                                   '<input type="radio" name="authorreq_'.$type.
 1638:                                   '" value="'.$val.'"'.$checked.' />'.
 1639:                                   $titles{$option}.'</label></span>&nbsp; ';
 1640:                         }
 1641:                     } else {
 1642:                         my $checked = 'checked="checked" ';
 1643:                         if (ref($settings) eq 'HASH') {
 1644:                             if (ref($settings->{$item}) eq 'HASH') {
 1645:                                 if ($settings->{$item}->{$type} == 0) {
 1646:                                     $checked = '';
 1647:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1648:                                     $checked =  'checked="checked" ';
 1649:                                 }
 1650:                             }
 1651:                         }
 1652:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1653:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1654:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1655:                                       '</label></span>&nbsp; ';
 1656:                     }
 1657:                 }
 1658:                 if ($context eq 'requestcourses') {
 1659:                     $datatable .= '</tr><tr>';
 1660:                     foreach my $item (@usertools) {
 1661:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1662:                     }
 1663:                     $datatable .= '</tr></table>';
 1664:                 }
 1665:                 $datatable .= '</td>';
 1666:                 unless (($context eq 'requestcourses') ||
 1667:                         ($context eq 'requestauthor')) {
 1668:                     $datatable .= 
 1669:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1670:                               '<input type="text" name="quota_'.$type.
 1671:                               '" value="'.$currdefquota.
 1672:                               '" size="5" /> Mb</span></td>';
 1673:                 }
 1674:                 $datatable .= '</tr>';
 1675:             }
 1676:         }
 1677:     }
 1678:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1679:         $defaultquota = '20';
 1680:         if (ref($settings) eq 'HASH') {
 1681:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1682:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1683:             } elsif (defined($settings->{'default'})) {
 1684:                 $defaultquota = $settings->{'default'};
 1685:             }
 1686:         }
 1687:     }
 1688:     $typecount ++;
 1689:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1690:     $datatable .= '<tr'.$css_class.'>'.
 1691:                   '<td>'.$othertitle.'</td>'.
 1692:                   '<td class="LC_left_item">';
 1693:     if ($context eq 'requestcourses') {
 1694:         $datatable .= '<table><tr>';
 1695:     }
 1696:     my %defcell;
 1697:     foreach my $item (@usertools) {
 1698:         if ($context eq 'requestcourses') {
 1699:             my ($curroption,$currlimit);
 1700:             if (ref($settings) eq 'HASH') {
 1701:                 if (ref($settings->{$item}) eq 'HASH') {
 1702:                     $curroption = $settings->{$item}->{'default'};
 1703:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1704:                         $currlimit = $1;
 1705:                     }
 1706:                 }
 1707:             }
 1708:             if (!$curroption) {
 1709:                 $curroption = 'norequest';
 1710:             }
 1711:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1712:             foreach my $option (@options) {
 1713:                 my $val = $option;
 1714:                 if ($option eq 'norequest') {
 1715:                     $val = 0;
 1716:                 }
 1717:                 if ($option eq 'validate') {
 1718:                     my $canvalidate = 0;
 1719:                     if (ref($validations{$item}) eq 'HASH') {
 1720:                         if ($validations{$item}{'default'}) {
 1721:                             $canvalidate = 1;
 1722:                         }
 1723:                     }
 1724:                     next if (!$canvalidate);
 1725:                 }
 1726:                 my $checked = '';
 1727:                 if ($option eq $curroption) {
 1728:                     $checked = ' checked="checked"';
 1729:                 } elsif ($option eq 'autolimit') {
 1730:                     if ($curroption =~ /^autolimit/) {
 1731:                         $checked = ' checked="checked"';
 1732:                     }
 1733:                 }
 1734:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1735:                                   '<input type="radio" name="crsreq_'.$item.
 1736:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1737:                                   $titles{$option}.'</label>';
 1738:                 if ($option eq 'autolimit') {
 1739:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1740:                                        $item.'_limit_default" size="1" '.
 1741:                                        'value="'.$currlimit.'" />';
 1742:                 }
 1743:                 $defcell{$item} .= '</span> ';
 1744:                 if ($option eq 'autolimit') {
 1745:                     $defcell{$item} .= $titles{'unlimited'};
 1746:                 }
 1747:             }
 1748:         } elsif ($context eq 'requestauthor') {
 1749:             my $curroption;
 1750:             if (ref($settings) eq 'HASH') {
 1751:                 $curroption = $settings->{'default'};
 1752:             }
 1753:             if (!$curroption) {
 1754:                 $curroption = 'norequest';
 1755:             }
 1756:             foreach my $option (@options) {
 1757:                 my $val = $option;
 1758:                 if ($option eq 'norequest') {
 1759:                     $val = 0;
 1760:                 }
 1761:                 my $checked = '';
 1762:                 if ($option eq $curroption) {
 1763:                     $checked = ' checked="checked"';
 1764:                 }
 1765:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1766:                               '<input type="radio" name="authorreq_default"'.
 1767:                               ' value="'.$val.'"'.$checked.' />'.
 1768:                               $titles{$option}.'</label></span>&nbsp; ';
 1769:             }
 1770:         } else {
 1771:             my $checked = 'checked="checked" ';
 1772:             if (ref($settings) eq 'HASH') {
 1773:                 if (ref($settings->{$item}) eq 'HASH') {
 1774:                     if ($settings->{$item}->{'default'} == 0) {
 1775:                         $checked = '';
 1776:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1777:                         $checked = 'checked="checked" ';
 1778:                     }
 1779:                 }
 1780:             }
 1781:             $datatable .= '<span class="LC_nobreak"><label>'.
 1782:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1783:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1784:                           '</label></span>&nbsp; ';
 1785:         }
 1786:     }
 1787:     if ($context eq 'requestcourses') {
 1788:         $datatable .= '</tr><tr>';
 1789:         foreach my $item (@usertools) {
 1790:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1791:         }
 1792:         $datatable .= '</tr></table>';
 1793:     }
 1794:     $datatable .= '</td>';
 1795:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1796:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1797:                       '<input type="text" name="defaultquota" value="'.
 1798:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1799:     }
 1800:     $datatable .= '</tr>';
 1801:     $typecount ++;
 1802:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1803:     $datatable .= '<tr'.$css_class.'>'.
 1804:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1805:     if ($context eq 'requestcourses') {
 1806:         $datatable .= &mt('(overrides affiliation, if set)').
 1807:                       '</td>'.
 1808:                       '<td class="LC_left_item">'.
 1809:                       '<table><tr>';
 1810:     } else {
 1811:         $datatable .= &mt('(overrides affiliation, if checked)').
 1812:                       '</td>'.
 1813:                       '<td class="LC_left_item" colspan="2">'.
 1814:                       '<br />';
 1815:     }
 1816:     my %advcell;
 1817:     foreach my $item (@usertools) {
 1818:         if ($context eq 'requestcourses') {
 1819:             my ($curroption,$currlimit);
 1820:             if (ref($settings) eq 'HASH') {
 1821:                 if (ref($settings->{$item}) eq 'HASH') {
 1822:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1823:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1824:                         $currlimit = $1;
 1825:                     }
 1826:                 }
 1827:             }
 1828:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1829:             my $checked = '';
 1830:             if ($curroption eq '') {
 1831:                 $checked = ' checked="checked"';
 1832:             }
 1833:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1834:                                '<input type="radio" name="crsreq_'.$item.
 1835:                                '__LC_adv" value=""'.$checked.' />'.
 1836:                                &mt('No override set').'</label></span>&nbsp; ';
 1837:             foreach my $option (@options) {
 1838:                 my $val = $option;
 1839:                 if ($option eq 'norequest') {
 1840:                     $val = 0;
 1841:                 }
 1842:                 if ($option eq 'validate') {
 1843:                     my $canvalidate = 0;
 1844:                     if (ref($validations{$item}) eq 'HASH') {
 1845:                         if ($validations{$item}{'_LC_adv'}) {
 1846:                             $canvalidate = 1;
 1847:                         }
 1848:                     }
 1849:                     next if (!$canvalidate);
 1850:                 }
 1851:                 my $checked = '';
 1852:                 if ($val eq $curroption) {
 1853:                     $checked = ' checked="checked"';
 1854:                 } elsif ($option eq 'autolimit') {
 1855:                     if ($curroption =~ /^autolimit/) {
 1856:                         $checked = ' checked="checked"';
 1857:                     }
 1858:                 }
 1859:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1860:                                   '<input type="radio" name="crsreq_'.$item.
 1861:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1862:                                   $titles{$option}.'</label>';
 1863:                 if ($option eq 'autolimit') {
 1864:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1865:                                        $item.'_limit__LC_adv" size="1" '.
 1866:                                        'value="'.$currlimit.'" />';
 1867:                 }
 1868:                 $advcell{$item} .= '</span> ';
 1869:                 if ($option eq 'autolimit') {
 1870:                     $advcell{$item} .= $titles{'unlimited'};
 1871:                 }
 1872:             }
 1873:         } elsif ($context eq 'requestauthor') {
 1874:             my $curroption;
 1875:             if (ref($settings) eq 'HASH') {
 1876:                 $curroption = $settings->{'_LC_adv'};
 1877:             }
 1878:             my $checked = '';
 1879:             if ($curroption eq '') {
 1880:                 $checked = ' checked="checked"';
 1881:             }
 1882:             $datatable .= '<span class="LC_nobreak"><label>'.
 1883:                           '<input type="radio" name="authorreq__LC_adv"'.
 1884:                           ' value=""'.$checked.' />'.
 1885:                           &mt('No override set').'</label></span>&nbsp; ';
 1886:             foreach my $option (@options) {
 1887:                 my $val = $option;
 1888:                 if ($option eq 'norequest') {
 1889:                     $val = 0;
 1890:                 }
 1891:                 my $checked = '';
 1892:                 if ($val eq $curroption) {
 1893:                     $checked = ' checked="checked"';
 1894:                 }
 1895:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1896:                               '<input type="radio" name="authorreq__LC_adv"'.
 1897:                               ' value="'.$val.'"'.$checked.' />'.
 1898:                               $titles{$option}.'</label></span>&nbsp; ';
 1899:             }
 1900:         } else {
 1901:             my $checked = 'checked="checked" ';
 1902:             if (ref($settings) eq 'HASH') {
 1903:                 if (ref($settings->{$item}) eq 'HASH') {
 1904:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1905:                         $checked = '';
 1906:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1907:                         $checked = 'checked="checked" ';
 1908:                     }
 1909:                 }
 1910:             }
 1911:             $datatable .= '<span class="LC_nobreak"><label>'.
 1912:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1913:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1914:                           '</label></span>&nbsp; ';
 1915:         }
 1916:     }
 1917:     if ($context eq 'requestcourses') {
 1918:         $datatable .= '</tr><tr>';
 1919:         foreach my $item (@usertools) {
 1920:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1921:         }
 1922:         $datatable .= '</tr></table>';
 1923:     }
 1924:     $datatable .= '</td></tr>';
 1925:     $$rowtotal += $typecount;
 1926:     return $datatable;
 1927: }
 1928: 
 1929: sub print_requestmail {
 1930:     my ($dom,$action,$settings,$rowtotal) = @_;
 1931:     my ($now,$datatable,%dompersonnel,@domcoord,@currapproval,$rows);
 1932:     $now = time;
 1933:     $rows = 0;
 1934:     %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 1935:     foreach my $server (keys(%dompersonnel)) {
 1936:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 1937:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 1938:             if (!grep(/^$uname:$udom$/,@domcoord)) {
 1939:                 push(@domcoord,$uname.':'.$udom);
 1940:             }
 1941:         }
 1942:     }
 1943:     if (ref($settings) eq 'HASH') {
 1944:         if (ref($settings->{'notify'}) eq 'HASH') {
 1945:             if ($settings->{'notify'}{'approval'} ne '') {
 1946:                @currapproval = split(',',$settings->{'notify'}{'approval'});
 1947:             }
 1948:         }
 1949:     }
 1950:     if (@currapproval) {
 1951:         foreach my $dc (@currapproval) {
 1952:             unless (grep(/^\Q$dc\E$/,@domcoord)) {
 1953:                 push(@domcoord,$dc);
 1954:             }
 1955:         }
 1956:     }
 1957:     @domcoord = sort(@domcoord);
 1958:     my $numinrow = 4;
 1959:     my $numdc = @domcoord;
 1960:     my $css_class = 'class="LC_odd_row"';
 1961:     my $text;
 1962:     if ($action eq 'requestcourses') {
 1963:         $text = &mt('Receive notification of course requests requiring approval');
 1964:     } else {
 1965:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1966:     }
 1967:     $datatable = '<tr '.$css_class.'>'.
 1968:                  ' <td>'.$text.'</td>'.
 1969:                  ' <td class="LC_left_item">';
 1970:     if (@domcoord > 0) {
 1971:         $datatable .= '<table>';
 1972:         for (my $i=0; $i<$numdc; $i++) {
 1973:             my $rem = $i%($numinrow);
 1974:             if ($rem == 0) {
 1975:                 if ($i > 0) {
 1976:                     $datatable .= '</tr>';
 1977:                 }
 1978:                 $datatable .= '<tr>';
 1979:                 $rows ++;
 1980:             }
 1981:             my $check = ' ';
 1982:             if (grep(/^\Q$domcoord[$i]\E$/,@currapproval)) {
 1983:                 $check = ' checked="checked" ';
 1984:             }
 1985:             my ($uname,$udom) = split(':',$domcoord[$i]);
 1986:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
 1987:             if ($i == $numdc-1) {
 1988:                 my $colsleft = $numinrow-$rem;
 1989:                 if ($colsleft > 1) {
 1990:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 1991:                 } else {
 1992:                     $datatable .= '<td class="LC_left_item">';
 1993:                 }
 1994:             } else {
 1995:                 $datatable .= '<td class="LC_left_item">';
 1996:             }
 1997:             $datatable .= '<span class="LC_nobreak"><label>'.
 1998:                           '<input type="checkbox" name="reqapprovalnotify" '.
 1999:                           'value="'.$domcoord[$i].'"'.$check.'/>'.
 2000:                           $fullname.' ('.$uname.':'.$udom.')</label></span></td>';
 2001:         }
 2002:         $datatable .= '</tr></table>';
 2003:     } else {
 2004:         $datatable .= &mt('There are no active Domain Coordinators');
 2005:         $rows ++;
 2006:     }
 2007:     $datatable .='</td></tr>';
 2008:     $$rowtotal += $rows;
 2009:     return $datatable;
 2010: }
 2011: 
 2012: sub print_autoenroll {
 2013:     my ($dom,$settings,$rowtotal) = @_;
 2014:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2015:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2016:     if (ref($settings) eq 'HASH') {
 2017:         if (exists($settings->{'run'})) {
 2018:             if ($settings->{'run'} eq '0') {
 2019:                 $runoff = ' checked="checked" ';
 2020:                 $runon = ' ';
 2021:             } else {
 2022:                 $runon = ' checked="checked" ';
 2023:                 $runoff = ' ';
 2024:             }
 2025:         } else {
 2026:             if ($autorun) {
 2027:                 $runon = ' checked="checked" ';
 2028:                 $runoff = ' ';
 2029:             } else {
 2030:                 $runoff = ' checked="checked" ';
 2031:                 $runon = ' ';
 2032:             }
 2033:         }
 2034:         if (exists($settings->{'co-owners'})) {
 2035:             if ($settings->{'co-owners'} eq '0') {
 2036:                 $coownersoff = ' checked="checked" ';
 2037:                 $coownerson = ' ';
 2038:             } else {
 2039:                 $coownerson = ' checked="checked" ';
 2040:                 $coownersoff = ' ';
 2041:             }
 2042:         } else {
 2043:             $coownersoff = ' checked="checked" ';
 2044:             $coownerson = ' ';
 2045:         }
 2046:         if (exists($settings->{'sender_domain'})) {
 2047:             $defdom = $settings->{'sender_domain'};
 2048:         }
 2049:     } else {
 2050:         if ($autorun) {
 2051:             $runon = ' checked="checked" ';
 2052:             $runoff = ' ';
 2053:         } else {
 2054:             $runoff = ' checked="checked" ';
 2055:             $runon = ' ';
 2056:         }
 2057:     }
 2058:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2059:     my $notif_sender;
 2060:     if (ref($settings) eq 'HASH') {
 2061:         $notif_sender = $settings->{'sender_uname'};
 2062:     }
 2063:     my $datatable='<tr class="LC_odd_row">'.
 2064:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2065:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2066:                   '<input type="radio" name="autoenroll_run"'.
 2067:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2068:                   '<label><input type="radio" name="autoenroll_run"'.
 2069:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2070:                   '</tr><tr>'.
 2071:                   '<td>'.&mt('Notification messages - sender').
 2072:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2073:                   &mt('username').':&nbsp;'.
 2074:                   '<input type="text" name="sender_uname" value="'.
 2075:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2076:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2077:                   '<tr class="LC_odd_row">'.
 2078:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2079:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2080:                   '<input type="radio" name="autoassign_coowners"'.
 2081:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2082:                   '<label><input type="radio" name="autoassign_coowners"'.
 2083:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2084:                   '</tr>';
 2085:     $$rowtotal += 3;
 2086:     return $datatable;
 2087: }
 2088: 
 2089: sub print_autoupdate {
 2090:     my ($position,$dom,$settings,$rowtotal) = @_;
 2091:     my $datatable;
 2092:     if ($position eq 'top') {
 2093:         my $updateon = ' ';
 2094:         my $updateoff = ' checked="checked" ';
 2095:         my $classlistson = ' ';
 2096:         my $classlistsoff = ' checked="checked" ';
 2097:         if (ref($settings) eq 'HASH') {
 2098:             if ($settings->{'run'} eq '1') {
 2099:                 $updateon = $updateoff;
 2100:                 $updateoff = ' ';
 2101:             }
 2102:             if ($settings->{'classlists'} eq '1') {
 2103:                 $classlistson = $classlistsoff;
 2104:                 $classlistsoff = ' ';
 2105:             }
 2106:         }
 2107:         my %title = (
 2108:                    run => 'Auto-update active?',
 2109:                    classlists => 'Update information in classlists?',
 2110:                     );
 2111:         $datatable = '<tr class="LC_odd_row">'. 
 2112:                   '<td>'.&mt($title{'run'}).'</td>'.
 2113:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2114:                   '<input type="radio" name="autoupdate_run"'.
 2115:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2116:                   '<label><input type="radio" name="autoupdate_run"'.
 2117:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2118:                   '</tr><tr>'.
 2119:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2120:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2121:                   '<label><input type="radio" name="classlists"'.
 2122:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2123:                   '<label><input type="radio" name="classlists"'.
 2124:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2125:                   '</tr>';
 2126:         $$rowtotal += 2;
 2127:     } elsif ($position eq 'middle') {
 2128:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2129:         my $numinrow = 3;
 2130:         my $locknamesettings;
 2131:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2132:                                      $dom,$numinrow,$othertitle,
 2133:                                     'lockablenames');
 2134:         $$rowtotal ++;
 2135:     } else {
 2136:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2137:         my @fields = ('lastname','firstname','middlename','generation',
 2138:                       'permanentemail','id');
 2139:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2140:         my $numrows = 0;
 2141:         if (ref($types) eq 'ARRAY') {
 2142:             if (@{$types} > 0) {
 2143:                 $datatable = 
 2144:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2145:                                          \@fields,$types,\$numrows);
 2146:                     $$rowtotal += @{$types}; 
 2147:             }
 2148:         }
 2149:         $datatable .= 
 2150:             &usertype_update_row($settings,{'default' => $othertitle},
 2151:                                  \%fieldtitles,\@fields,['default'],
 2152:                                  \$numrows);
 2153:         $$rowtotal ++;     
 2154:     }
 2155:     return $datatable;
 2156: }
 2157: 
 2158: sub print_autocreate {
 2159:     my ($dom,$settings,$rowtotal) = @_;
 2160:     my (%createon,%createoff);
 2161:     my $curr_dc;
 2162:     my @types = ('xml','req');
 2163:     if (ref($settings) eq 'HASH') {
 2164:         foreach my $item (@types) {
 2165:             $createoff{$item} = ' checked="checked" ';
 2166:             $createon{$item} = ' ';
 2167:             if (exists($settings->{$item})) {
 2168:                 if ($settings->{$item}) {
 2169:                     $createon{$item} = ' checked="checked" ';
 2170:                     $createoff{$item} = ' ';
 2171:                 }
 2172:             }
 2173:         }
 2174:         $curr_dc = $settings->{'xmldc'};
 2175:     } else {
 2176:         foreach my $item (@types) {
 2177:             $createoff{$item} = ' checked="checked" ';
 2178:             $createon{$item} = ' ';
 2179:         }
 2180:     }
 2181:     $$rowtotal += 2;
 2182:     my $datatable='<tr class="LC_odd_row">'.
 2183:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2184:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2185:                   '<input type="radio" name="autocreate_xml"'.
 2186:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2187:                   '<label><input type="radio" name="autocreate_xml"'.
 2188:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2189:                   '</td></tr><tr>'.
 2190:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2191:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2192:                   '<input type="radio" name="autocreate_req"'.
 2193:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2194:                   '<label><input type="radio" name="autocreate_req"'.
 2195:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2196:     my ($numdc,$dctable) = &active_dc_picker($dom,$curr_dc);
 2197:     if ($numdc > 1) {
 2198:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2199:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2200:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2201:         $$rowtotal ++ ;
 2202:     } else {
 2203:         $datatable .= $dctable.'</td></tr>';
 2204:     }
 2205:     return $datatable;
 2206: }
 2207: 
 2208: sub print_directorysrch {
 2209:     my ($dom,$settings,$rowtotal) = @_;
 2210:     my $srchon = ' ';
 2211:     my $srchoff = ' checked="checked" ';
 2212:     my ($exacton,$containson,$beginson);
 2213:     my $localon = ' ';
 2214:     my $localoff = ' checked="checked" ';
 2215:     if (ref($settings) eq 'HASH') {
 2216:         if ($settings->{'available'} eq '1') {
 2217:             $srchon = $srchoff;
 2218:             $srchoff = ' ';
 2219:         }
 2220:         if ($settings->{'localonly'} eq '1') {
 2221:             $localon = $localoff;
 2222:             $localoff = ' ';
 2223:         }
 2224:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2225:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2226:                 if ($type eq 'exact') {
 2227:                     $exacton = ' checked="checked" ';
 2228:                 } elsif ($type eq 'contains') {
 2229:                     $containson = ' checked="checked" ';
 2230:                 } elsif ($type eq 'begins') {
 2231:                     $beginson = ' checked="checked" ';
 2232:                 }
 2233:             }
 2234:         } else {
 2235:             if ($settings->{'searchtypes'} eq 'exact') {
 2236:                 $exacton = ' checked="checked" ';
 2237:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2238:                 $containson = ' checked="checked" ';
 2239:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2240:                 $exacton = ' checked="checked" ';
 2241:                 $containson = ' checked="checked" ';
 2242:             }
 2243:         }
 2244:     }
 2245:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2246:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2247: 
 2248:     my $numinrow = 4;
 2249:     my $cansrchrow = 0;
 2250:     my $datatable='<tr class="LC_odd_row">'.
 2251:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2252:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2253:                   '<input type="radio" name="dirsrch_available"'.
 2254:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2255:                   '<label><input type="radio" name="dirsrch_available"'.
 2256:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2257:                   '</tr><tr>'.
 2258:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2259:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2260:                   '<input type="radio" name="dirsrch_localonly"'.
 2261:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2262:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2263:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2264:                   '</tr>';
 2265:     $$rowtotal += 2;
 2266:     if (ref($usertypes) eq 'HASH') {
 2267:         if (keys(%{$usertypes}) > 0) {
 2268:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2269:                                          $numinrow,$othertitle,'cansearch');
 2270:             $cansrchrow = 1;
 2271:         }
 2272:     }
 2273:     if ($cansrchrow) {
 2274:         $$rowtotal ++;
 2275:         $datatable .= '<tr>';
 2276:     } else {
 2277:         $datatable .= '<tr class="LC_odd_row">';
 2278:     }
 2279:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2280:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2281:     foreach my $title (@{$titleorder}) {
 2282:         if (defined($searchtitles->{$title})) {
 2283:             my $check = ' ';
 2284:             if (ref($settings) eq 'HASH') {
 2285:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2286:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2287:                         $check = ' checked="checked" ';
 2288:                     }
 2289:                 }
 2290:             }
 2291:             $datatable .= '<td class="LC_left_item">'.
 2292:                           '<span class="LC_nobreak"><label>'.
 2293:                           '<input type="checkbox" name="searchby" '.
 2294:                           'value="'.$title.'"'.$check.'/>'.
 2295:                           $searchtitles->{$title}.'</label></span></td>';
 2296:         }
 2297:     }
 2298:     $datatable .= '</tr></table></td></tr>';
 2299:     $$rowtotal ++;
 2300:     if ($cansrchrow) {
 2301:         $datatable .= '<tr class="LC_odd_row">';
 2302:     } else {
 2303:         $datatable .= '<tr>';
 2304:     }
 2305:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2306:                   '<td class="LC_left_item" colspan="2">'.
 2307:                   '<span class="LC_nobreak"><label>'.
 2308:                   '<input type="checkbox" name="searchtypes" '.
 2309:                   $exacton.' value="exact" />'.&mt('Exact match').
 2310:                   '</label>&nbsp;'.
 2311:                   '<label><input type="checkbox" name="searchtypes" '.
 2312:                   $beginson.' value="begins" />'.&mt('Begins with').
 2313:                   '</label>&nbsp;'.
 2314:                   '<label><input type="checkbox" name="searchtypes" '.
 2315:                   $containson.' value="contains" />'.&mt('Contains').
 2316:                   '</label></span></td></tr>';
 2317:     $$rowtotal ++;
 2318:     return $datatable;
 2319: }
 2320: 
 2321: sub print_contacts {
 2322:     my ($dom,$settings,$rowtotal) = @_;
 2323:     my $datatable;
 2324:     my @contacts = ('adminemail','supportemail');
 2325:     my (%checked,%to,%otheremails,%bccemails);
 2326:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2327:                     'requestsmail');
 2328:     foreach my $type (@mailings) {
 2329:         $otheremails{$type} = '';
 2330:     }
 2331:     $bccemails{'helpdeskmail'} = '';
 2332:     if (ref($settings) eq 'HASH') {
 2333:         foreach my $item (@contacts) {
 2334:             if (exists($settings->{$item})) {
 2335:                 $to{$item} = $settings->{$item};
 2336:             }
 2337:         }
 2338:         foreach my $type (@mailings) {
 2339:             if (exists($settings->{$type})) {
 2340:                 if (ref($settings->{$type}) eq 'HASH') {
 2341:                     foreach my $item (@contacts) {
 2342:                         if ($settings->{$type}{$item}) {
 2343:                             $checked{$type}{$item} = ' checked="checked" ';
 2344:                         }
 2345:                     }
 2346:                     $otheremails{$type} = $settings->{$type}{'others'};
 2347:                     if ($type eq 'helpdeskmail') {
 2348:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2349:                     }
 2350:                 }
 2351:             } elsif ($type eq 'lonstatusmail') {
 2352:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2353:             }
 2354:         }
 2355:     } else {
 2356:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2357:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2358:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2359:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2360:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2361:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2362:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2363:     }
 2364:     my ($titles,$short_titles) = &contact_titles();
 2365:     my $rownum = 0;
 2366:     my $css_class;
 2367:     foreach my $item (@contacts) {
 2368:         $rownum ++;
 2369:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2370:         $datatable .= '<tr'.$css_class.'>'. 
 2371:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2372:                   '</span></td><td class="LC_right_item">'.
 2373:                   '<input type="text" name="'.$item.'" value="'.
 2374:                   $to{$item}.'" /></td></tr>';
 2375:     }
 2376:     foreach my $type (@mailings) {
 2377:         $rownum ++;
 2378:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2379:         $datatable .= '<tr'.$css_class.'>'.
 2380:                       '<td><span class="LC_nobreak">'.
 2381:                       $titles->{$type}.': </span></td>'.
 2382:                       '<td class="LC_left_item">'.
 2383:                       '<span class="LC_nobreak">';
 2384:         foreach my $item (@contacts) {
 2385:             $datatable .= '<label>'.
 2386:                           '<input type="checkbox" name="'.$type.'"'.
 2387:                           $checked{$type}{$item}.
 2388:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2389:                           '</label>&nbsp;';
 2390:         }
 2391:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2392:                       '<input type="text" name="'.$type.'_others" '.
 2393:                       'value="'.$otheremails{$type}.'"  />';
 2394:         if ($type eq 'helpdeskmail') {
 2395:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2396:                           '<input type="text" name="'.$type.'_bcc" '.
 2397:                           'value="'.$bccemails{$type}.'"  />';
 2398:         }
 2399:         $datatable .= '</td></tr>'."\n";
 2400:     }
 2401:     $$rowtotal += $rownum;
 2402:     return $datatable;
 2403: }
 2404: 
 2405: sub print_helpsettings {
 2406:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2407:     my ($datatable,$itemcount);
 2408:     $itemcount = 1;
 2409:     my (%choices,%defaultchecked,@toggles);
 2410:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2411:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2412:                                  &mt('LON-CAPA bug tracker'),600,500));
 2413:     %defaultchecked = ('submitbugs' => 'on');
 2414:     @toggles = ('submitbugs',);
 2415: 
 2416:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2417:                                                  \%choices,$itemcount);
 2418:     return $datatable;
 2419: }
 2420: 
 2421: sub radiobutton_prefs {
 2422:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount) = @_;
 2423:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2424:                    (ref($choices) eq 'HASH'));
 2425: 
 2426:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2427: 
 2428:     foreach my $item (@{$toggles}) {
 2429:         if ($defaultchecked->{$item} eq 'on') {
 2430:             $checkedon{$item} = ' checked="checked" ';
 2431:             $checkedoff{$item} = ' ';
 2432:         } elsif ($defaultchecked->{$item} eq 'off') {
 2433:             $checkedoff{$item} = ' checked="checked" ';
 2434:             $checkedon{$item} = ' ';
 2435:         }
 2436:     }
 2437:     if (ref($settings) eq 'HASH') {
 2438:         foreach my $item (@{$toggles}) {
 2439:             if ($settings->{$item} eq '1') {
 2440:                 $checkedon{$item} =  ' checked="checked" ';
 2441:                 $checkedoff{$item} = ' ';
 2442:             } elsif ($settings->{$item} eq '0') {
 2443:                 $checkedoff{$item} =  ' checked="checked" ';
 2444:                 $checkedon{$item} = ' ';
 2445:             }
 2446:         }
 2447:     }
 2448:     foreach my $item (@{$toggles}) {
 2449:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2450:         $datatable .=
 2451:             '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices->{$item}.
 2452:             '</span></td>'.
 2453:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2454:             '<label><input type="radio" name="'.
 2455:             $item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').
 2456:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2457:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2458:             '</span></td>'.
 2459:             '</tr>';
 2460:         $itemcount ++;
 2461:     }
 2462:     return ($datatable,$itemcount);
 2463: }
 2464: 
 2465: sub print_coursedefaults {
 2466:     my ($position,$dom,$settings,$rowtotal) = @_;
 2467:     my ($css_class,$datatable);
 2468:     my $itemcount = 1;
 2469:     if ($position eq 'top') {
 2470:         my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2471:         %choices =
 2472:             &Apache::lonlocal::texthash (
 2473:                 canuse_pdfforms => 'Course/Community users can create/upload PDF forms',
 2474:         );
 2475:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2476:         @toggles = ('canuse_pdfforms',);
 2477:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2478:                                                  \%choices,$itemcount);
 2479:         $$rowtotal += $itemcount;
 2480:     } else {
 2481:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2482:         my %choices =
 2483:             &Apache::lonlocal::texthash (
 2484:                 anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2485:         );
 2486:         my $currdefresponder;
 2487:         if (ref($settings) eq 'HASH') {
 2488:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2489:         }
 2490:         if (!$currdefresponder) {
 2491:             $currdefresponder = 10;
 2492:         } elsif ($currdefresponder < 1) {
 2493:             $currdefresponder = 1;
 2494:         }
 2495:         $datatable .=
 2496:                '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices{'anonsurvey_threshold'}.
 2497:                 '</span></td>'.
 2498:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2499:                 '<input type="text" name="anonsurvey_threshold"'.
 2500:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2501:                 '</td></tr>';
 2502:     }
 2503:     return $datatable;
 2504: }
 2505: 
 2506: sub print_usersessions {
 2507:     my ($position,$dom,$settings,$rowtotal) = @_;
 2508:     my ($css_class,$datatable,%checked,%choices);
 2509:     my (%by_ip,%by_location,@intdoms);
 2510:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2511: 
 2512:     my @alldoms = &Apache::lonnet::all_domains();
 2513:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2514:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2515:     my %altids = &id_for_thisdom(%servers);
 2516:     my $itemcount = 1;
 2517:     if ($position eq 'top') {
 2518:         if (keys(%serverhomes) > 1) {
 2519:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2520:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2521:         } else {
 2522:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2523:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2524:         }
 2525:     } else {
 2526:         if (keys(%by_location) == 0) {
 2527:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2528:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2529:         } else {
 2530:             my %lt = &usersession_titles();
 2531:             my $numinrow = 5;
 2532:             my $prefix;
 2533:             my @types;
 2534:             if ($position eq 'bottom') {
 2535:                 $prefix = 'remote';
 2536:                 @types = ('version','excludedomain','includedomain');
 2537:             } else {
 2538:                 $prefix = 'hosted';
 2539:                 @types = ('excludedomain','includedomain');
 2540:             }
 2541:             my (%current,%checkedon,%checkedoff);
 2542:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2543:             my @locations = sort(keys(%by_location));
 2544:             foreach my $type (@types) {
 2545:                 $checkedon{$type} = '';
 2546:                 $checkedoff{$type} = ' checked="checked"';
 2547:             }
 2548:             if (ref($settings) eq 'HASH') {
 2549:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2550:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2551:                         $current{$key} = $settings->{$prefix}{$key};
 2552:                         if ($key eq 'version') {
 2553:                             if ($current{$key} ne '') {
 2554:                                 $checkedon{$key} = ' checked="checked"';
 2555:                                 $checkedoff{$key} = '';
 2556:                             }
 2557:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2558:                             $checkedon{$key} = ' checked="checked"';
 2559:                             $checkedoff{$key} = '';
 2560:                         }
 2561:                     }
 2562:                 }
 2563:             }
 2564:             foreach my $type (@types) {
 2565:                 next if ($type ne 'version' && !@locations);
 2566:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2567:                 $datatable .= '<tr'.$css_class.'>
 2568:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2569:                                <span class="LC_nobreak">&nbsp;
 2570:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2571:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2572:                 if ($type eq 'version') {
 2573:                     my $selector = '<select name="'.$prefix.'_version">';
 2574:                     foreach my $version (@lcversions) {
 2575:                         my $selected = '';
 2576:                         if ($current{'version'} eq $version) {
 2577:                             $selected = ' selected="selected"';
 2578:                         }
 2579:                         $selector .= ' <option value="'.$version.'"'.
 2580:                                      $selected.'>'.$version.'</option>';
 2581:                     }
 2582:                     $selector .= '</select> ';
 2583:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2584:                 } else {
 2585:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2586:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2587:                                  ' />'.('&nbsp;'x2).
 2588:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2589:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2590:                                  "\n".
 2591:                                  '</div><div><table>';
 2592:                     my $rem;
 2593:                     for (my $i=0; $i<@locations; $i++) {
 2594:                         my ($showloc,$value,$checkedtype);
 2595:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2596:                             my $ip = $by_location{$locations[$i]}->[0];
 2597:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2598:                                  $value = join(':',@{$by_ip{$ip}});
 2599:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2600:                                 if (ref($current{$type}) eq 'ARRAY') {
 2601:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2602:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2603:                                             $checkedtype = ' checked="checked"';
 2604:                                             last;
 2605:                                         }
 2606:                                     }
 2607:                                 }
 2608:                             }
 2609:                         }
 2610:                         $rem = $i%($numinrow);
 2611:                         if ($rem == 0) {
 2612:                             if ($i > 0) {
 2613:                                 $datatable .= '</tr>';
 2614:                             }
 2615:                             $datatable .= '<tr>';
 2616:                         }
 2617:                         $datatable .= '<td class="LC_left_item">'.
 2618:                                       '<span class="LC_nobreak"><label>'.
 2619:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2620:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2621:                                       '</label></span></td>';
 2622:                     }
 2623:                     $rem = @locations%($numinrow);
 2624:                     my $colsleft = $numinrow - $rem;
 2625:                     if ($colsleft > 1 ) {
 2626:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2627:                                       '&nbsp;</td>';
 2628:                     } elsif ($colsleft == 1) {
 2629:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2630:                     }
 2631:                     $datatable .= '</tr></table>';
 2632:                 }
 2633:                 $datatable .= '</td></tr>';
 2634:                 $itemcount ++;
 2635:             }
 2636:         }
 2637:     }
 2638:     $$rowtotal += $itemcount;
 2639:     return $datatable;
 2640: }
 2641: 
 2642: sub build_location_hashes {
 2643:     my ($intdoms,$by_ip,$by_location) = @_;
 2644:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2645:                   (ref($by_location) eq 'HASH')); 
 2646:     my %iphost = &Apache::lonnet::get_iphost();
 2647:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2648:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2649:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2650:         foreach my $id (@{$iphost{$primary_ip}}) {
 2651:             my $intdom = &Apache::lonnet::internet_dom($id);
 2652:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2653:                 push(@{$intdoms},$intdom);
 2654:             }
 2655:         }
 2656:     }
 2657:     foreach my $ip (keys(%iphost)) {
 2658:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2659:             foreach my $id (@{$iphost{$ip}}) {
 2660:                 my $location = &Apache::lonnet::internet_dom($id);
 2661:                 if ($location) {
 2662:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2663:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2664:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2665:                             push(@{$by_ip->{$ip}},$location);
 2666:                         }
 2667:                     } else {
 2668:                         $by_ip->{$ip} = [$location];
 2669:                     }
 2670:                 }
 2671:             }
 2672:         }
 2673:     }
 2674:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2675:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2676:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2677:             my $first = $by_ip->{$ip}->[0];
 2678:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2679:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2680:                     push(@{$by_location->{$first}},$ip);
 2681:                 }
 2682:             } else {
 2683:                 $by_location->{$first} = [$ip];
 2684:             }
 2685:         }
 2686:     }
 2687:     return;
 2688: }
 2689: 
 2690: sub current_offloads_to {
 2691:     my ($dom,$settings,$servers) = @_;
 2692:     my (%spareid,%otherdomconfigs);
 2693:     if (ref($servers) eq 'HASH') {
 2694:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2695:             my $gotspares;
 2696:             if (ref($settings) eq 'HASH') {
 2697:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2698:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2699:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2700:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2701:                         $gotspares = 1;
 2702:                     }
 2703:                 }
 2704:             }
 2705:             unless ($gotspares) {
 2706:                 my $gotspares;
 2707:                 my $serverhomeID =
 2708:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2709:                 my $serverhomedom =
 2710:                     &Apache::lonnet::host_domain($serverhomeID);
 2711:                 if ($serverhomedom ne $dom) {
 2712:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2713:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2714:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2715:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2716:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2717:                                 $gotspares = 1;
 2718:                             }
 2719:                         }
 2720:                     } else {
 2721:                         $otherdomconfigs{$serverhomedom} =
 2722:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2723:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2724:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2725:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2726:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2727:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2728:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2729:                                         $gotspares = 1;
 2730:                                     }
 2731:                                 }
 2732:                             }
 2733:                         }
 2734:                     }
 2735:                 }
 2736:             }
 2737:             unless ($gotspares) {
 2738:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2739:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2740:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2741:                } else {
 2742:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2743:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2744:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2745:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2746:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2747:                     } else {
 2748:                         my %what = (
 2749:                              spareid => 1,
 2750:                         );
 2751:                         my ($result,$returnhash) = 
 2752:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2753:                         if ($result eq 'ok') { 
 2754:                             if (ref($returnhash) eq 'HASH') {
 2755:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2756:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2757:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2758:                                 }
 2759:                             }
 2760:                         }
 2761:                     }
 2762:                 }
 2763:             }
 2764:         }
 2765:     }
 2766:     return %spareid;
 2767: }
 2768: 
 2769: sub spares_row {
 2770:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2771:     my $css_class;
 2772:     my $numinrow = 4;
 2773:     my $itemcount = 1;
 2774:     my $datatable;
 2775:     my %typetitles = &sparestype_titles();
 2776:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2777:         foreach my $server (sort(keys(%{$servers}))) {
 2778:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2779:             my ($othercontrol,$serverdom);
 2780:             if ($serverhome ne $server) {
 2781:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2782:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2783:             } else {
 2784:                 $serverdom = &Apache::lonnet::host_domain($server);
 2785:                 if ($serverdom ne $dom) {
 2786:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2787:                 }
 2788:             }
 2789:             next unless (ref($spareid->{$server}) eq 'HASH');
 2790:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2791:             $datatable .= '<tr'.$css_class.'>
 2792:                            <td rowspan="2">
 2793:                             <span class="LC_nobreak">'.
 2794:                           &mt('[_1] when busy, offloads to:'
 2795:                               ,'<b>'.$server.'</b>').
 2796:                           "\n";
 2797:             my (%current,%canselect);
 2798:             my @choices = 
 2799:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2800:             foreach my $type ('primary','default') {
 2801:                 if (ref($spareid->{$server}) eq 'HASH') {
 2802:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2803:                         my @spares = @{$spareid->{$server}{$type}};
 2804:                         if (@spares > 0) {
 2805:                             if ($othercontrol) {
 2806:                                 $current{$type} = join(', ',@spares);
 2807:                             } else {
 2808:                                 $current{$type} .= '<table>';
 2809:                                 my $numspares = scalar(@spares);
 2810:                                 for (my $i=0;  $i<@spares; $i++) {
 2811:                                     my $rem = $i%($numinrow);
 2812:                                     if ($rem == 0) {
 2813:                                         if ($i > 0) {
 2814:                                             $current{$type} .= '</tr>';
 2815:                                         }
 2816:                                         $current{$type} .= '<tr>';
 2817:                                     }
 2818:                                     $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;'.
 2819:                                                        $spareid->{$server}{$type}[$i].
 2820:                                                        '</label></td>'."\n";
 2821:                                 }
 2822:                                 my $rem = @spares%($numinrow);
 2823:                                 my $colsleft = $numinrow - $rem;
 2824:                                 if ($colsleft > 1 ) {
 2825:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2826:                                                        '" class="LC_left_item">'.
 2827:                                                        '&nbsp;</td>';
 2828:                                 } elsif ($colsleft == 1) {
 2829:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2830:                                 }
 2831:                                 $current{$type} .= '</tr></table>';
 2832:                             }
 2833:                         }
 2834:                     }
 2835:                     if ($current{$type} eq '') {
 2836:                         $current{$type} = &mt('None specified');
 2837:                     }
 2838:                     if ($othercontrol) {
 2839:                         if ($type eq 'primary') {
 2840:                             $canselect{$type} = $othercontrol;
 2841:                         }
 2842:                     } else {
 2843:                         $canselect{$type} = 
 2844:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2845:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2846:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2847:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2848:                         if (@choices > 0) {
 2849:                             foreach my $lonhost (@choices) {
 2850:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2851:                             }
 2852:                         }
 2853:                         $canselect{$type} .= '</select>'."\n";
 2854:                     }
 2855:                 } else {
 2856:                     $current{$type} = &mt('Could not be determined');
 2857:                     if ($type eq 'primary') {
 2858:                         $canselect{$type} =  $othercontrol;
 2859:                     }
 2860:                 }
 2861:                 if ($type eq 'default') {
 2862:                     $datatable .= '<tr'.$css_class.'>';
 2863:                 }
 2864:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2865:                               '<td>'.$current{$type}.'</td>'."\n".
 2866:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2867:             }
 2868:             $itemcount ++;
 2869:         }
 2870:     }
 2871:     $$rowtotal += $itemcount;
 2872:     return $datatable;
 2873: }
 2874: 
 2875: sub possible_newspares {
 2876:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2877:     my $serverhostname = &Apache::lonnet::hostname($server);
 2878:     my %excluded;
 2879:     if ($serverhostname ne '') {
 2880:         %excluded = (
 2881:                        $serverhostname => 1,
 2882:                     );
 2883:     }
 2884:     if (ref($currspares) eq 'HASH') {
 2885:         foreach my $type (keys(%{$currspares})) {
 2886:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2887:                 if (@{$currspares->{$type}} > 0) {
 2888:                     foreach my $curr (@{$currspares->{$type}}) {
 2889:                         my $hostname = &Apache::lonnet::hostname($curr);
 2890:                         $excluded{$hostname} = 1;
 2891:                     }
 2892:                 }
 2893:             }
 2894:         }
 2895:     }
 2896:     my @choices;
 2897:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2898:         if (keys(%{$serverhomes}) > 1) {
 2899:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2900:                 unless ($excluded{$name}) {
 2901:                     if (exists($altids->{$serverhomes->{$name}})) {
 2902:                         push(@choices,$altids->{$serverhomes->{$name}});
 2903:                     } else {
 2904:                         push(@choices,$serverhomes->{$name});
 2905:                     }
 2906:                 }
 2907:             }
 2908:         }
 2909:     }
 2910:     return sort(@choices);
 2911: }
 2912: 
 2913: sub print_loadbalancing {
 2914:     my ($dom,$settings,$rowtotal) = @_;
 2915:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2916:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2917:     my $numinrow = 1;
 2918:     my $datatable;
 2919:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2920:     my (%currbalancer,%currtargets,%currrules,%existing);
 2921:     if (ref($settings) eq 'HASH') {
 2922:         %existing = %{$settings};
 2923:     }
 2924:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 2925:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 2926:                                   \%currtargets,\%currrules);
 2927:     } else {
 2928:         return;
 2929:     }
 2930:     my ($othertitle,$usertypes,$types) =
 2931:         &Apache::loncommon::sorted_inst_types($dom);
 2932:     my $rownum = 6;
 2933:     if (ref($types) eq 'ARRAY') {
 2934:         $rownum += scalar(@{$types});
 2935:     }
 2936:     my @css_class = ('LC_odd_row','LC_even_row');
 2937:     my $balnum = 0;
 2938:     my $islast;
 2939:     my (@toshow,$disabledtext);
 2940:     if (keys(%currbalancer) > 0) {
 2941:         @toshow = sort(keys(%currbalancer));
 2942:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 2943:             push(@toshow,'');
 2944:         }
 2945:     } else {
 2946:         @toshow = ('');
 2947:         $disabledtext = &mt('No existing load balancer');
 2948:     }
 2949:     foreach my $lonhost (@toshow) {
 2950:         if ($balnum == scalar(@toshow)-1) {
 2951:             $islast = 1;
 2952:         } else {
 2953:             $islast = 0;
 2954:         }
 2955:         my $cssidx = $balnum%2;
 2956:         my $targets_div_style = 'display: none';
 2957:         my $disabled_div_style = 'display: block';
 2958:         my $homedom_div_style = 'display: none';
 2959:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 2960:                       '<td rowspan="'.$rownum.'" valign="top">'.
 2961:                       '<p>';
 2962:         if ($lonhost eq '') {
 2963:             $datatable .= '<span class="LC_nobreak">'; 
 2964:             if (keys(%currbalancer) > 0) {
 2965:                 $datatable .= &mt('Add balancer:');
 2966:             } else {
 2967:                 $datatable .= &mt('Enable balancer:');
 2968:             }
 2969:             $datatable .= '&nbsp;'.
 2970:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 2971:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 2972:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 2973:                           '<option value="" selected="selected">'.&mt('None').
 2974:                           '</option>'."\n";
 2975:             foreach my $server (sort(keys(%servers))) {
 2976:                 next if ($currbalancer{$server});
 2977:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 2978:             }
 2979:             $datatable .= 
 2980:                 '</select>'."\n".
 2981:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 2982:         } else {
 2983:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 2984:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 2985:                            &mt('Stop balancing').'</label>'.
 2986:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 2987:             $targets_div_style = 'display: block';
 2988:             $disabled_div_style = 'display: none';
 2989:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 2990:                 $homedom_div_style = 'display: block';
 2991:             }
 2992:         }
 2993:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 2994:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 2995:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 2996:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2997:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 2998:         my @sparestypes = ('primary','default');
 2999:         my %typetitles = &sparestype_titles();
 3000:         foreach my $sparetype (@sparestypes) {
 3001:             my $targettable;
 3002:             for (my $i=0; $i<$numspares; $i++) {
 3003:                 my $checked;
 3004:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3005:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3006:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3007:                             $checked = ' checked="checked"';
 3008:                         }
 3009:                     }
 3010:                 }
 3011:                 my ($chkboxval,$disabled);
 3012:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3013:                     $chkboxval = $spares[$i];
 3014:                 }
 3015:                 if (exists($currbalancer{$spares[$i]})) {
 3016:                     $disabled = ' disabled="disabled"';
 3017:                 }
 3018:                 $targettable .= 
 3019:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3020:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3021:                     '</span></label></td>';
 3022:                 my $rem = $i%($numinrow);
 3023:                 if ($rem == 0) {
 3024:                     if (($i > 0) && ($i < $numspares-1)) {
 3025:                         $targettable .= '</tr>';
 3026:                     }
 3027:                     if ($i < $numspares-1) {
 3028:                         $targettable .= '<tr>';
 3029:                     }
 3030:                 }
 3031:             }
 3032:             if ($targettable ne '') {
 3033:                 my $rem = $numspares%($numinrow);
 3034:                 my $colsleft = $numinrow - $rem;
 3035:                 if ($colsleft > 1 ) {
 3036:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3037:                                     '&nbsp;</td>';
 3038:                 } elsif ($colsleft == 1) {
 3039:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3040:                 }
 3041:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3042:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3043:             }
 3044:         }
 3045:         $datatable .= '</div></td></tr>'.
 3046:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3047:                                            $othertitle,$usertypes,$types,\%servers,
 3048:                                            \%currbalancer,$lonhost,
 3049:                                            $targets_div_style,$homedom_div_style,
 3050:                                            $css_class[$cssidx],$balnum,$islast);
 3051:         $$rowtotal += $rownum;
 3052:         $balnum ++;
 3053:     }
 3054:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3055:     return $datatable;
 3056: }
 3057: 
 3058: sub get_loadbalancers_config {
 3059:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3060:     return unless ((ref($servers) eq 'HASH') &&
 3061:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3062:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3063:     if (keys(%{$existing}) > 0) {
 3064:         my $oldlonhost;
 3065:         foreach my $key (sort(keys(%{$existing}))) {
 3066:             if ($key eq 'lonhost') {
 3067:                 $oldlonhost = $existing->{'lonhost'};
 3068:                 $currbalancer->{$oldlonhost} = 1;
 3069:             } elsif ($key eq 'targets') {
 3070:                 if ($oldlonhost) {
 3071:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3072:                 }
 3073:             } elsif ($key eq 'rules') {
 3074:                 if ($oldlonhost) {
 3075:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3076:                 }
 3077:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3078:                 $currbalancer->{$key} = 1;
 3079:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3080:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3081:             }
 3082:         }
 3083:     } else {
 3084:         my ($balancerref,$targetsref) =
 3085:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3086:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3087:             foreach my $server (sort(keys(%{$balancerref}))) {
 3088:                 $currbalancer->{$server} = 1;
 3089:                 $currtargets->{$server} = $targetsref->{$server};
 3090:             }
 3091:         }
 3092:     }
 3093:     return;
 3094: }
 3095: 
 3096: sub loadbalancing_rules {
 3097:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3098:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3099:         $css_class,$balnum,$islast) = @_;
 3100:     my $output;
 3101:     my $num = 0;
 3102:     my ($alltypes,$othertypes,$titles) = 
 3103:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3104:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3105:         foreach my $type (@{$alltypes}) {
 3106:             $num ++;
 3107:             my $current;
 3108:             if (ref($currrules) eq 'HASH') {
 3109:                 $current = $currrules->{$type};
 3110:             }
 3111:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3112:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3113:                     $current = '';
 3114:                 }
 3115:             }
 3116:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3117:                                              $servers,$currbalancer,$lonhost,$dom,
 3118:                                              $targets_div_style,$homedom_div_style,
 3119:                                              $css_class,$balnum,$num,$islast);
 3120:         }
 3121:     }
 3122:     return $output;
 3123: }
 3124: 
 3125: sub loadbalancing_titles {
 3126:     my ($dom,$intdom,$usertypes,$types) = @_;
 3127:     my %othertypes = (
 3128:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3129:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3130:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3131:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3132:                      );
 3133:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 3134:     if (ref($types) eq 'ARRAY') {
 3135:         unshift(@alltypes,@{$types},'default');
 3136:     }
 3137:     my %titles;
 3138:     foreach my $type (@alltypes) {
 3139:         if ($type =~ /^_LC_/) {
 3140:             $titles{$type} = $othertypes{$type};
 3141:         } elsif ($type eq 'default') {
 3142:             $titles{$type} = &mt('All users from [_1]',$dom);
 3143:             if (ref($types) eq 'ARRAY') {
 3144:                 if (@{$types} > 0) {
 3145:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3146:                 }
 3147:             }
 3148:         } elsif (ref($usertypes) eq 'HASH') {
 3149:             $titles{$type} = $usertypes->{$type};
 3150:         }
 3151:     }
 3152:     return (\@alltypes,\%othertypes,\%titles);
 3153: }
 3154: 
 3155: sub loadbalance_rule_row {
 3156:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3157:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3158:     my @rulenames = ('default','homeserver');
 3159:     my %ruletitles = &offloadtype_text();
 3160:     if ($type eq '_LC_external') {
 3161:         push(@rulenames,'externalbalancer');
 3162:     } else {
 3163:         push(@rulenames,'specific');
 3164:     }
 3165:     push(@rulenames,'none');
 3166:     my $style = $targets_div_style;
 3167:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3168:         $style = $homedom_div_style;
 3169:     }
 3170:     my $space;
 3171:     if ($islast && $num == 1) {
 3172:         $space = '<div display="inline-block">&nbsp;</div>';
 3173:     }
 3174:     my $output = 
 3175:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3176:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3177:         '<td valaign="top">'.$space.
 3178:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3179:     for (my $i=0; $i<@rulenames; $i++) {
 3180:         my $rule = $rulenames[$i];
 3181:         my ($checked,$extra);
 3182:         if ($rulenames[$i] eq 'default') {
 3183:             $rule = '';
 3184:         }
 3185:         if ($rulenames[$i] eq 'specific') {
 3186:             if (ref($servers) eq 'HASH') {
 3187:                 my $default;
 3188:                 if (($current ne '') && (exists($servers->{$current}))) {
 3189:                     $checked = ' checked="checked"';
 3190:                 }
 3191:                 unless ($checked) {
 3192:                     $default = ' selected="selected"';
 3193:                 }
 3194:                 $extra = 
 3195:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3196:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3197:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3198:                     '<option value=""'.$default.'></option>'."\n";
 3199:                 foreach my $server (sort(keys(%{$servers}))) {
 3200:                     if (ref($currbalancer) eq 'HASH') {
 3201:                         next if (exists($currbalancer->{$server}));
 3202:                     }
 3203:                     my $selected;
 3204:                     if ($server eq $current) {
 3205:                         $selected = ' selected="selected"';
 3206:                     }
 3207:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3208:                 }
 3209:                 $extra .= '</select>';
 3210:             }
 3211:         } elsif ($rule eq $current) {
 3212:             $checked = ' checked="checked"';
 3213:         }
 3214:         $output .= '<span class="LC_nobreak"><label>'.
 3215:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3216:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3217:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3218:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3219:                    '</label>'.$extra.'</span><br />'."\n";
 3220:     }
 3221:     $output .= '</div></td></tr>'."\n";
 3222:     return $output;
 3223: }
 3224: 
 3225: sub offloadtype_text {
 3226:     my %ruletitles = &Apache::lonlocal::texthash (
 3227:            'default'          => 'Offloads to default destinations',
 3228:            'homeserver'       => "Offloads to user's home server",
 3229:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3230:            'specific'         => 'Offloads to specific server',
 3231:            'none'             => 'No offload',
 3232:     );
 3233:     return %ruletitles;
 3234: }
 3235: 
 3236: sub sparestype_titles {
 3237:     my %typestitles = &Apache::lonlocal::texthash (
 3238:                           'primary' => 'primary',
 3239:                           'default' => 'default',
 3240:                       );
 3241:     return %typestitles;
 3242: }
 3243: 
 3244: sub contact_titles {
 3245:     my %titles = &Apache::lonlocal::texthash (
 3246:                    'supportemail' => 'Support E-mail address',
 3247:                    'adminemail'   => 'Default Server Admin E-mail address',
 3248:                    'errormail'    => 'Error reports to be e-mailed to',
 3249:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3250:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3251:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3252:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3253:                  );
 3254:     my %short_titles = &Apache::lonlocal::texthash (
 3255:                            adminemail   => 'Admin E-mail address',
 3256:                            supportemail => 'Support E-mail',
 3257:                        );   
 3258:     return (\%titles,\%short_titles);
 3259: }
 3260: 
 3261: sub tool_titles {
 3262:     my %titles = &Apache::lonlocal::texthash (
 3263:                      aboutme    => 'Personal web page',
 3264:                      blog       => 'Blog',
 3265:                      webdav     => 'WebDAV',
 3266:                      portfolio  => 'Portfolio',
 3267:                      official   => 'Official courses (with institutional codes)',
 3268:                      unofficial => 'Unofficial courses',
 3269:                      community  => 'Communities',
 3270:                  );
 3271:     return %titles;
 3272: }
 3273: 
 3274: sub courserequest_titles {
 3275:     my %titles = &Apache::lonlocal::texthash (
 3276:                                    official   => 'Official',
 3277:                                    unofficial => 'Unofficial',
 3278:                                    community  => 'Communities',
 3279:                                    norequest  => 'Not allowed',
 3280:                                    approval   => 'Approval by Dom. Coord.',
 3281:                                    validate   => 'With validation',
 3282:                                    autolimit  => 'Numerical limit',
 3283:                                    unlimited  => '(blank for unlimited)',
 3284:                  );
 3285:     return %titles;
 3286: }
 3287: 
 3288: sub authorrequest_titles {
 3289:     my %titles = &Apache::lonlocal::texthash (
 3290:                                    norequest  => 'Not allowed',
 3291:                                    approval   => 'Approval by Dom. Coord.',
 3292:                                    automatic  => 'Automatic approval',
 3293:                  );
 3294:     return %titles;
 3295: } 
 3296: 
 3297: sub courserequest_conditions {
 3298:     my %conditions = &Apache::lonlocal::texthash (
 3299:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3300:        validate   => '(Processing of request subject to instittutional validation).',
 3301:                  );
 3302:     return %conditions;
 3303: }
 3304: 
 3305: 
 3306: sub print_usercreation {
 3307:     my ($position,$dom,$settings,$rowtotal) = @_;
 3308:     my $numinrow = 4;
 3309:     my $datatable;
 3310:     if ($position eq 'top') {
 3311:         $$rowtotal ++;
 3312:         my $rowcount = 0;
 3313:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3314:         if (ref($rules) eq 'HASH') {
 3315:             if (keys(%{$rules}) > 0) {
 3316:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3317:                                                 $ruleorder,$numinrow,$rowcount);
 3318:                 $$rowtotal ++;
 3319:                 $rowcount ++;
 3320:             }
 3321:         }
 3322:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3323:         if (ref($idrules) eq 'HASH') {
 3324:             if (keys(%{$idrules}) > 0) {
 3325:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3326:                                                 $idruleorder,$numinrow,$rowcount);
 3327:                 $$rowtotal ++;
 3328:                 $rowcount ++;
 3329:             }
 3330:         }
 3331:         my ($emailrules,$emailruleorder) = 
 3332:             &Apache::lonnet::inst_userrules($dom,'email');
 3333:         if (ref($emailrules) eq 'HASH') {
 3334:             if (keys(%{$emailrules}) > 0) {
 3335:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3336:                                                 $emailruleorder,$numinrow,$rowcount);
 3337:                 $$rowtotal ++;
 3338:                 $rowcount ++;
 3339:             }
 3340:         }
 3341:         if ($rowcount == 0) {
 3342:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3343:             $$rowtotal ++;
 3344:             $rowcount ++;
 3345:         }
 3346:     } elsif ($position eq 'middle') {
 3347:         my @creators = ('author','course','requestcrs','selfcreate');
 3348:         my ($rules,$ruleorder) =
 3349:             &Apache::lonnet::inst_userrules($dom,'username');
 3350:         my %lt = &usercreation_types();
 3351:         my %checked;
 3352:         my @selfcreate; 
 3353:         if (ref($settings) eq 'HASH') {
 3354:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3355:                 foreach my $item (@creators) {
 3356:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3357:                 }
 3358:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3359:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3360:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3361:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3362:                         @selfcreate = ('email','login','sso');
 3363:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3364:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3365:                     }
 3366:                 }
 3367:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3368:                 foreach my $item (@creators) {
 3369:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3370:                         $checked{$item} = 'none';
 3371:                     }
 3372:                 }
 3373:             }
 3374:         }
 3375:         my $rownum = 0;
 3376:         foreach my $item (@creators) {
 3377:             $rownum ++;
 3378:             if ($item ne 'selfcreate') {  
 3379:                 if ($checked{$item} eq '') {
 3380:                     $checked{$item} = 'any';
 3381:                 }
 3382:             }
 3383:             my $css_class;
 3384:             if ($rownum%2) {
 3385:                 $css_class = '';
 3386:             } else {
 3387:                 $css_class = ' class="LC_odd_row" ';
 3388:             }
 3389:             $datatable .= '<tr'.$css_class.'>'.
 3390:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3391:                          '</span></td><td align="right">';
 3392:             my @options;
 3393:             if ($item eq 'selfcreate') {
 3394:                 push(@options,('email','login','sso'));
 3395:             } else {
 3396:                 @options = ('any');
 3397:                 if (ref($rules) eq 'HASH') {
 3398:                     if (keys(%{$rules}) > 0) {
 3399:                         push(@options,('official','unofficial'));
 3400:                     }
 3401:                 }
 3402:                 push(@options,'none');
 3403:             }
 3404:             foreach my $option (@options) {
 3405:                 my $type = 'radio';
 3406:                 my $check = ' ';
 3407:                 if ($item eq 'selfcreate') {
 3408:                     $type = 'checkbox';
 3409:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3410:                         $check = ' checked="checked" ';
 3411:                     }
 3412:                 } else {
 3413:                     if ($checked{$item} eq $option) {
 3414:                         $check = ' checked="checked" ';
 3415:                     }
 3416:                 } 
 3417:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3418:                               '<input type="'.$type.'" name="can_createuser_'.
 3419:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3420:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3421:             }
 3422:             $datatable .= '</td></tr>';
 3423:         }
 3424:         my ($othertitle,$usertypes,$types) =
 3425:             &Apache::loncommon::sorted_inst_types($dom);
 3426:         my $createsettings;
 3427:         if (ref($settings) eq 'HASH') {
 3428:             $createsettings = $settings->{cancreate};
 3429:         }
 3430:         if (ref($usertypes) eq 'HASH') {
 3431:             if (keys(%{$usertypes}) > 0) {
 3432:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3433:                                              $dom,$numinrow,$othertitle,
 3434:                                              'statustocreate');
 3435:                 $$rowtotal ++;
 3436:                 $rownum ++;
 3437:             }
 3438:         }
 3439:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3440:     } else {
 3441:         my @contexts = ('author','course','domain');
 3442:         my @authtypes = ('int','krb4','krb5','loc');
 3443:         my %checked;
 3444:         if (ref($settings) eq 'HASH') {
 3445:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3446:                 foreach my $item (@contexts) {
 3447:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3448:                         foreach my $auth (@authtypes) {
 3449:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3450:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3451:                             }
 3452:                         }
 3453:                     }
 3454:                 }
 3455:             }
 3456:         } else {
 3457:             foreach my $item (@contexts) {
 3458:                 foreach my $auth (@authtypes) {
 3459:                     $checked{$item}{$auth} = ' checked="checked" ';
 3460:                 }
 3461:             }
 3462:         }
 3463:         my %title = &context_names();
 3464:         my %authname = &authtype_names();
 3465:         my $rownum = 0;
 3466:         my $css_class; 
 3467:         foreach my $item (@contexts) {
 3468:             if ($rownum%2) {
 3469:                 $css_class = '';
 3470:             } else {
 3471:                 $css_class = ' class="LC_odd_row" ';
 3472:             }
 3473:             $datatable .=   '<tr'.$css_class.'>'.
 3474:                             '<td>'.$title{$item}.
 3475:                             '</td><td class="LC_left_item">'.
 3476:                             '<span class="LC_nobreak">';
 3477:             foreach my $auth (@authtypes) {
 3478:                 $datatable .= '<label>'. 
 3479:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3480:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3481:                               $authname{$auth}.'</label>&nbsp;';
 3482:             }
 3483:             $datatable .= '</span></td></tr>';
 3484:             $rownum ++;
 3485:         }
 3486:         $$rowtotal += $rownum;
 3487:     }
 3488:     return $datatable;
 3489: }
 3490: 
 3491: sub captcha_choice {
 3492:     my ($context,$settings,$itemcount) = @_;
 3493:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3494:     my %lt = &captcha_phrases();
 3495:     $keyentry = 'hidden';
 3496:     if ($context eq 'cancreate') {
 3497:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3498:     } elsif ($context eq 'login') {
 3499:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3500:     }
 3501:     if (ref($settings) eq 'HASH') {
 3502:         if ($settings->{'captcha'}) {
 3503:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3504:         } else {
 3505:             $checked{'original'} = ' checked="checked"';
 3506:         }
 3507:         if ($settings->{'captcha'} eq 'recaptcha') {
 3508:             $pubtext = $lt{'pub'};
 3509:             $privtext = $lt{'priv'};
 3510:             $keyentry = 'text';
 3511:         }
 3512:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3513:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3514:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3515:         }
 3516:     } else {
 3517:         $checked{'original'} = ' checked="checked"';
 3518:     }
 3519:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3520:     my $output = '<tr'.$css_class.'>'.
 3521:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3522:                  '<table><tr><td>'."\n";
 3523:     foreach my $option ('original','recaptcha','notused') {
 3524:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3525:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3526:                    $lt{$option}.'</label></span>';
 3527:         unless ($option eq 'notused') {
 3528:             $output .= ('&nbsp;'x2)."\n";
 3529:         }
 3530:     }
 3531: #
 3532: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3533: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3534: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu) 
 3535: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3536: #  
 3537:     $output .= '</td></tr>'."\n".
 3538:                '<tr><td>'."\n".
 3539:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3540:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3541:                $currpub.'" size="40" /></span><br />'."\n".
 3542:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3543:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3544:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3545:                '</td></tr>';
 3546:     return $output;
 3547: }
 3548: 
 3549: sub user_formats_row {
 3550:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3551:     my $output;
 3552:     my %text = (
 3553:                    'username' => 'new usernames',
 3554:                    'id'       => 'IDs',
 3555:                    'email'    => 'self-created accounts (e-mail)',
 3556:                );
 3557:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3558:     $output = '<tr '.$css_class.'>'.
 3559:               '<td><span class="LC_nobreak">';
 3560:     if ($type eq 'email') {
 3561:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3562:     } else {
 3563:         $output .= &mt("Format rules to check for $text{$type}: ");
 3564:     }
 3565:     $output .= '</span></td>'.
 3566:                '<td class="LC_left_item" colspan="2"><table>';
 3567:     my $rem;
 3568:     if (ref($ruleorder) eq 'ARRAY') {
 3569:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3570:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3571:                 my $rem = $i%($numinrow);
 3572:                 if ($rem == 0) {
 3573:                     if ($i > 0) {
 3574:                         $output .= '</tr>';
 3575:                     }
 3576:                     $output .= '<tr>';
 3577:                 }
 3578:                 my $check = ' ';
 3579:                 if (ref($settings) eq 'HASH') {
 3580:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3581:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3582:                             $check = ' checked="checked" ';
 3583:                         }
 3584:                     }
 3585:                 }
 3586:                 $output .= '<td class="LC_left_item">'.
 3587:                            '<span class="LC_nobreak"><label>'.
 3588:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3589:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3590:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3591:             }
 3592:         }
 3593:         $rem = @{$ruleorder}%($numinrow);
 3594:     }
 3595:     my $colsleft = $numinrow - $rem;
 3596:     if ($colsleft > 1 ) {
 3597:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3598:                    '&nbsp;</td>';
 3599:     } elsif ($colsleft == 1) {
 3600:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3601:     }
 3602:     $output .= '</tr></table></td></tr>';
 3603:     return $output;
 3604: }
 3605: 
 3606: sub usercreation_types {
 3607:     my %lt = &Apache::lonlocal::texthash (
 3608:                     author     => 'When adding a co-author',
 3609:                     course     => 'When adding a user to a course',
 3610:                     requestcrs => 'When requesting a course',
 3611:                     selfcreate => 'User creates own account', 
 3612:                     any        => 'Any',
 3613:                     official   => 'Institutional only ',
 3614:                     unofficial => 'Non-institutional only',
 3615:                     email      => 'E-mail address',
 3616:                     login      => 'Institutional Login',
 3617:                     sso        => 'SSO', 
 3618:                     none       => 'None',
 3619:     );
 3620:     return %lt;
 3621: }
 3622: 
 3623: sub authtype_names {
 3624:     my %lt = &Apache::lonlocal::texthash(
 3625:                       int    => 'Internal',
 3626:                       krb4   => 'Kerberos 4',
 3627:                       krb5   => 'Kerberos 5',
 3628:                       loc    => 'Local',
 3629:                   );
 3630:     return %lt;
 3631: }
 3632: 
 3633: sub context_names {
 3634:     my %context_title = &Apache::lonlocal::texthash(
 3635:        author => 'Creating users when an Author',
 3636:        course => 'Creating users when in a course',
 3637:        domain => 'Creating users when a Domain Coordinator',
 3638:     );
 3639:     return %context_title;
 3640: }
 3641: 
 3642: sub print_usermodification {
 3643:     my ($position,$dom,$settings,$rowtotal) = @_;
 3644:     my $numinrow = 4;
 3645:     my ($context,$datatable,$rowcount);
 3646:     if ($position eq 'top') {
 3647:         $rowcount = 0;
 3648:         $context = 'author'; 
 3649:         foreach my $role ('ca','aa') {
 3650:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3651:                                                    $numinrow,$rowcount);
 3652:             $$rowtotal ++;
 3653:             $rowcount ++;
 3654:         }
 3655:     } elsif ($position eq 'middle') {
 3656:         $context = 'course';
 3657:         $rowcount = 0;
 3658:         foreach my $role ('st','ep','ta','in','cr') {
 3659:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3660:                                                    $numinrow,$rowcount);
 3661:             $$rowtotal ++;
 3662:             $rowcount ++;
 3663:         }
 3664:     } elsif ($position eq 'bottom') {
 3665:         $context = 'selfcreate';
 3666:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3667:         $usertypes->{'default'} = $othertitle;
 3668:         if (ref($types) eq 'ARRAY') {
 3669:             push(@{$types},'default');
 3670:             $usertypes->{'default'} = $othertitle;
 3671:             foreach my $status (@{$types}) {
 3672:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3673:                                                        $numinrow,$rowcount,$usertypes);
 3674:                 $$rowtotal ++;
 3675:                 $rowcount ++;
 3676:             }
 3677:         }
 3678:     }
 3679:     return $datatable;
 3680: }
 3681: 
 3682: sub print_defaults {
 3683:     my ($dom,$rowtotal) = @_;
 3684:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3685:                  'datelocale_def','portal_def');
 3686:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3687:     my $titles = &defaults_titles($dom);
 3688:     my $rownum = 0;
 3689:     my ($datatable,$css_class);
 3690:     foreach my $item (@items) {
 3691:         if ($rownum%2) {
 3692:             $css_class = '';
 3693:         } else {
 3694:             $css_class = ' class="LC_odd_row" ';
 3695:         }
 3696:         $datatable .= '<tr'.$css_class.'>'.
 3697:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3698:                   '</span></td><td class="LC_right_item">';
 3699:         if ($item eq 'auth_def') {
 3700:             my @authtypes = ('internal','krb4','krb5','localauth');
 3701:             my %shortauth = (
 3702:                              internal => 'int',
 3703:                              krb4 => 'krb4',
 3704:                              krb5 => 'krb5',
 3705:                              localauth  => 'loc'
 3706:                            );
 3707:             my %authnames = &authtype_names();
 3708:             foreach my $auth (@authtypes) {
 3709:                 my $checked = ' ';
 3710:                 if ($domdefaults{$item} eq $auth) {
 3711:                     $checked = ' checked="checked" ';
 3712:                 }
 3713:                 $datatable .= '<label><input type="radio" name="'.$item.
 3714:                               '" value="'.$auth.'"'.$checked.'/>'.
 3715:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3716:             }
 3717:         } elsif ($item eq 'timezone_def') {
 3718:             my $includeempty = 1;
 3719:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3720:         } elsif ($item eq 'datelocale_def') {
 3721:             my $includeempty = 1;
 3722:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3723:         } elsif ($item eq 'lang_def') {
 3724:             my %langchoices = &get_languages_hash();
 3725:             $langchoices{''} = 'No language preference';
 3726:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 3727:             $datatable .= &Apache::loncommon::select_form($domdefaults{$item},$item,
 3728:                                                           \%langchoices);
 3729:         } else {
 3730:             my $size;
 3731:             if ($item eq 'portal_def') {
 3732:                 $size = ' size="25"';
 3733:             }
 3734:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3735:                           $domdefaults{$item}.'"'.$size.' />';
 3736:         }
 3737:         $datatable .= '</td></tr>';
 3738:         $rownum ++;
 3739:     }
 3740:     $$rowtotal += $rownum;
 3741:     return $datatable;
 3742: }
 3743: 
 3744: sub get_languages_hash {
 3745:     my %langchoices;
 3746:     foreach my $id (&Apache::loncommon::languageids()) {
 3747:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 3748:         if ($code ne '') {
 3749:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 3750:         }
 3751:     }
 3752:     return %langchoices;
 3753: }
 3754: 
 3755: sub defaults_titles {
 3756:     my ($dom) = @_;
 3757:     my %titles = &Apache::lonlocal::texthash (
 3758:                    'auth_def'      => 'Default authentication type',
 3759:                    'auth_arg_def'  => 'Default authentication argument',
 3760:                    'lang_def'      => 'Default language',
 3761:                    'timezone_def'  => 'Default timezone',
 3762:                    'datelocale_def' => 'Default locale for dates',
 3763:                    'portal_def'     => 'Portal/Default URL',
 3764:                  );
 3765:     if ($dom) {
 3766:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3767:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3768:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3769:         $protocol = 'http' if ($protocol ne 'https');
 3770:         if ($uint_dom) {
 3771:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3772:                                          $uint_dom);
 3773:         }
 3774:     }
 3775:     return (\%titles);
 3776: }
 3777: 
 3778: sub print_scantronformat {
 3779:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3780:     my $itemcount = 1;
 3781:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3782:         %confhash);
 3783:     my $switchserver = &check_switchserver($dom,$confname);
 3784:     my %lt = &Apache::lonlocal::texthash (
 3785:                 default => 'Default bubblesheet format file error',
 3786:                 custom  => 'Custom bubblesheet format file error',
 3787:              );
 3788:     my %scantronfiles = (
 3789:         default => 'default.tab',
 3790:         custom => 'custom.tab',
 3791:     );
 3792:     foreach my $key (keys(%scantronfiles)) {
 3793:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3794:                               .$scantronfiles{$key};
 3795:     }
 3796:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3797:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3798:         if (!$switchserver) {
 3799:             my $servadm = $r->dir_config('lonAdmEMail');
 3800:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3801:             if ($configuserok eq 'ok') {
 3802:                 if ($author_ok eq 'ok') {
 3803:                     my %legacyfile = (
 3804:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3805:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3806:                     );
 3807:                     my %md5chk;
 3808:                     foreach my $type (keys(%legacyfile)) {
 3809:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3810:                         chomp($md5chk{$type});
 3811:                     }
 3812:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3813:                         foreach my $type (keys(%legacyfile)) {
 3814:                             ($scantronurls{$type},my $error) = 
 3815:                                 &legacy_scantronformat($r,$dom,$confname,
 3816:                                                  $type,$legacyfile{$type},
 3817:                                                  $scantronurls{$type},
 3818:                                                  $scantronfiles{$type});
 3819:                             if ($error ne '') {
 3820:                                 $error{$type} = $error;
 3821:                             }
 3822:                         }
 3823:                         if (keys(%error) == 0) {
 3824:                             $is_custom = 1;
 3825:                             $confhash{'scantron'}{'scantronformat'} = 
 3826:                                 $scantronurls{'custom'};
 3827:                             my $putresult = 
 3828:                                 &Apache::lonnet::put_dom('configuration',
 3829:                                                          \%confhash,$dom);
 3830:                             if ($putresult ne 'ok') {
 3831:                                 $error{'custom'} = 
 3832:                                     '<span class="LC_error">'.
 3833:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3834:                             }
 3835:                         }
 3836:                     } else {
 3837:                         ($scantronurls{'default'},my $error) =
 3838:                             &legacy_scantronformat($r,$dom,$confname,
 3839:                                           'default',$legacyfile{'default'},
 3840:                                           $scantronurls{'default'},
 3841:                                           $scantronfiles{'default'});
 3842:                         if ($error eq '') {
 3843:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3844:                             my $putresult =
 3845:                                 &Apache::lonnet::put_dom('configuration',
 3846:                                                          \%confhash,$dom);
 3847:                             if ($putresult ne 'ok') {
 3848:                                 $error{'default'} =
 3849:                                     '<span class="LC_error">'.
 3850:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3851:                             }
 3852:                         } else {
 3853:                             $error{'default'} = $error;
 3854:                         }
 3855:                     }
 3856:                 }
 3857:             }
 3858:         } else {
 3859:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3860:         }
 3861:     }
 3862:     if (ref($settings) eq 'HASH') {
 3863:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3864:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3865:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3866:                 $scantronurl = '';
 3867:             } else {
 3868:                 $scantronurl = $settings->{'scantronformat'};
 3869:             }
 3870:             $is_custom = 1;
 3871:         } else {
 3872:             $scantronurl = $scantronurls{'default'};
 3873:         }
 3874:     } else {
 3875:         if ($is_custom) {
 3876:             $scantronurl = $scantronurls{'custom'};
 3877:         } else {
 3878:             $scantronurl = $scantronurls{'default'};
 3879:         }
 3880:     }
 3881:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3882:     $datatable .= '<tr'.$css_class.'>';
 3883:     if (!$is_custom) {
 3884:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3885:                       '<span class="LC_nobreak">';
 3886:         if ($scantronurl) {
 3887:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3888:                           &mt('Default bubblesheet format file').'</a>';
 3889:         } else {
 3890:             $datatable = &mt('File unavailable for display');
 3891:         }
 3892:         $datatable .= '</span></td>';
 3893:         if (keys(%error) == 0) { 
 3894:             $datatable .= '<td valign="bottom">';
 3895:             if (!$switchserver) {
 3896:                 $datatable .= &mt('Upload:').'<br />';
 3897:             }
 3898:         } else {
 3899:             my $errorstr;
 3900:             foreach my $key (sort(keys(%error))) {
 3901:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3902:             }
 3903:             $datatable .= '<td>'.$errorstr;
 3904:         }
 3905:     } else {
 3906:         if (keys(%error) > 0) {
 3907:             my $errorstr;
 3908:             foreach my $key (sort(keys(%error))) {
 3909:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3910:             } 
 3911:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3912:         } elsif ($scantronurl) {
 3913:             $datatable .= '<td><span class="LC_nobreak">'.
 3914:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3915:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3916:                           '<input type="checkbox" name="scantronformat_del"'.
 3917:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3918:                           '<td><span class="LC_nobreak">&nbsp;'.
 3919:                           &mt('Replace:').'</span><br />';
 3920:         }
 3921:     }
 3922:     if (keys(%error) == 0) {
 3923:         if ($switchserver) {
 3924:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3925:         } else {
 3926:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3927:                          '<input type="file" name="scantronformat" /></span>';
 3928:         }
 3929:     }
 3930:     $datatable .= '</td></tr>';
 3931:     $$rowtotal ++;
 3932:     return $datatable;
 3933: }
 3934: 
 3935: sub legacy_scantronformat {
 3936:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3937:     my ($url,$error);
 3938:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3939:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3940:         (my $result,$url) =
 3941:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3942:                          '','',$newfile);
 3943:         if ($result ne 'ok') {
 3944:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3945:         }
 3946:     }
 3947:     return ($url,$error);
 3948: }
 3949: 
 3950: sub print_coursecategories {
 3951:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3952:     my $datatable;
 3953:     if ($position eq 'top') {
 3954:         my $toggle_cats_crs = ' ';
 3955:         my $toggle_cats_dom = ' checked="checked" ';
 3956:         my $can_cat_crs = ' ';
 3957:         my $can_cat_dom = ' checked="checked" ';
 3958:         my $toggle_catscomm_comm = ' ';
 3959:         my $toggle_catscomm_dom = ' checked="checked" ';
 3960:         my $can_catcomm_comm = ' ';
 3961:         my $can_catcomm_dom = ' checked="checked" ';
 3962: 
 3963:         if (ref($settings) eq 'HASH') {
 3964:             if ($settings->{'togglecats'} eq 'crs') {
 3965:                 $toggle_cats_crs = $toggle_cats_dom;
 3966:                 $toggle_cats_dom = ' ';
 3967:             }
 3968:             if ($settings->{'categorize'} eq 'crs') {
 3969:                 $can_cat_crs = $can_cat_dom;
 3970:                 $can_cat_dom = ' ';
 3971:             }
 3972:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3973:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3974:                 $toggle_catscomm_dom = ' ';
 3975:             }
 3976:             if ($settings->{'categorizecomm'} eq 'comm') {
 3977:                 $can_catcomm_comm = $can_catcomm_dom;
 3978:                 $can_catcomm_dom = ' ';
 3979:             }
 3980:         }
 3981:         my %title = &Apache::lonlocal::texthash (
 3982:                      togglecats     => 'Show/Hide a course in catalog',
 3983:                      togglecatscomm => 'Show/Hide a community in catalog',
 3984:                      categorize     => 'Assign a category to a course',
 3985:                      categorizecomm => 'Assign a category to a community',
 3986:                     );
 3987:         my %level = &Apache::lonlocal::texthash (
 3988:                      dom  => 'Set in Domain',
 3989:                      crs  => 'Set in Course',
 3990:                      comm => 'Set in Community',
 3991:                     );
 3992:         $datatable = '<tr class="LC_odd_row">'.
 3993:                   '<td>'.$title{'togglecats'}.'</td>'.
 3994:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3995:                   '<input type="radio" name="togglecats"'.
 3996:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3997:                   '<label><input type="radio" name="togglecats"'.
 3998:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3999:                   '</tr><tr>'.
 4000:                   '<td>'.$title{'categorize'}.'</td>'.
 4001:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4002:                   '<label><input type="radio" name="categorize"'.
 4003:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4004:                   '<label><input type="radio" name="categorize"'.
 4005:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4006:                   '</tr><tr class="LC_odd_row">'.
 4007:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4008:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4009:                   '<input type="radio" name="togglecatscomm"'.
 4010:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4011:                   '<label><input type="radio" name="togglecatscomm"'.
 4012:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4013:                   '</tr><tr>'.
 4014:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4015:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4016:                   '<label><input type="radio" name="categorizecomm"'.
 4017:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4018:                   '<label><input type="radio" name="categorizecomm"'.
 4019:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4020:                   '</tr>';
 4021:         $$rowtotal += 4;
 4022:     } else {
 4023:         my $css_class;
 4024:         my $itemcount = 1;
 4025:         my $cathash; 
 4026:         if (ref($settings) eq 'HASH') {
 4027:             $cathash = $settings->{'cats'};
 4028:         }
 4029:         if (ref($cathash) eq 'HASH') {
 4030:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4031:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4032:                                                    \%allitems,\%idx,\@jsarray);
 4033:             my $maxdepth = scalar(@cats);
 4034:             my $colattrib = '';
 4035:             if ($maxdepth > 2) {
 4036:                 $colattrib = ' colspan="2" ';
 4037:             }
 4038:             my @path;
 4039:             if (@cats > 0) {
 4040:                 if (ref($cats[0]) eq 'ARRAY') {
 4041:                     my $numtop = @{$cats[0]};
 4042:                     my $maxnum = $numtop;
 4043:                     my %default_names = (
 4044:                           instcode    => &mt('Official courses'),
 4045:                           communities => &mt('Communities'),
 4046:                     );
 4047: 
 4048:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4049:                         ($cathash->{'instcode::0'} eq '') ||
 4050:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4051:                         ($cathash->{'communities::0'} eq '')) {
 4052:                         $maxnum ++;
 4053:                     }
 4054:                     my $lastidx;
 4055:                     for (my $i=0; $i<$numtop; $i++) {
 4056:                         my $parent = $cats[0][$i];
 4057:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4058:                         my $item = &escape($parent).'::0';
 4059:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4060:                         $lastidx = $idx{$item};
 4061:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4062:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4063:                         for (my $k=0; $k<=$maxnum; $k++) {
 4064:                             my $vpos = $k+1;
 4065:                             my $selstr;
 4066:                             if ($k == $i) {
 4067:                                 $selstr = ' selected="selected" ';
 4068:                             }
 4069:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4070:                         }
 4071:                         $datatable .= '</select></td><td>';
 4072:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4073:                             $datatable .=  '<span class="LC_nobreak">'
 4074:                                            .$default_names{$parent}.'</span>';
 4075:                             if ($parent eq 'instcode') {
 4076:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4077:                                               .&mt('with institutional codes')
 4078:                                               .')</span></td><td'.$colattrib.'>';
 4079:                             } else {
 4080:                                 $datatable .= '<table><tr><td>';
 4081:                             }
 4082:                             $datatable .= '<span class="LC_nobreak">'
 4083:                                           .'<label><input type="radio" name="'
 4084:                                           .$parent.'" value="1" checked="checked" />'
 4085:                                           .&mt('Display').'</label>';
 4086:                             if ($parent eq 'instcode') {
 4087:                                 $datatable .= '&nbsp;';
 4088:                             } else {
 4089:                                 $datatable .= '</span></td></tr><tr><td>'
 4090:                                               .'<span class="LC_nobreak">';
 4091:                             }
 4092:                             $datatable .= '<label><input type="radio" name="'
 4093:                                           .$parent.'" value="0" />'
 4094:                                           .&mt('Do not display').'</label></span>';
 4095:                             if ($parent eq 'communities') {
 4096:                                 $datatable .= '</td></tr></table>';
 4097:                             }
 4098:                             $datatable .= '</td>';
 4099:                         } else {
 4100:                             $datatable .= $parent
 4101:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 4102:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4103:                         }
 4104:                         my $depth = 1;
 4105:                         push(@path,$parent);
 4106:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4107:                         pop(@path);
 4108:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4109:                         $itemcount ++;
 4110:                     }
 4111:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4112:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4113:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4114:                     for (my $k=0; $k<=$maxnum; $k++) {
 4115:                         my $vpos = $k+1;
 4116:                         my $selstr;
 4117:                         if ($k == $numtop) {
 4118:                             $selstr = ' selected="selected" ';
 4119:                         }
 4120:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4121:                     }
 4122:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4123:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4124:                                   .'</tr>'."\n";
 4125:                     $itemcount ++;
 4126:                     foreach my $default ('instcode','communities') {
 4127:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4128:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4129:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4130:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4131:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4132:                             for (my $k=0; $k<=$maxnum; $k++) {
 4133:                                 my $vpos = $k+1;
 4134:                                 my $selstr;
 4135:                                 if ($k == $maxnum) {
 4136:                                     $selstr = ' selected="selected" ';
 4137:                                 }
 4138:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4139:                             }
 4140:                             $datatable .= '</select></span></td>'.
 4141:                                           '<td><span class="LC_nobreak">'.
 4142:                                           $default_names{$default}.'</span>';
 4143:                             if ($default eq 'instcode') {
 4144:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4145:                                               .&mt('with institutional codes').')</span>';
 4146:                             }
 4147:                             $datatable .= '</td>'
 4148:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4149:                                           .&mt('Display').'</label>&nbsp;'
 4150:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4151:                                           .&mt('Do not display').'</label></span></td></tr>';
 4152:                         }
 4153:                     }
 4154:                 }
 4155:             } else {
 4156:                 $datatable .= &initialize_categories($itemcount);
 4157:             }
 4158:         } else {
 4159:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4160:                           .&initialize_categories($itemcount);
 4161:         }
 4162:         $$rowtotal += $itemcount;
 4163:     }
 4164:     return $datatable;
 4165: }
 4166: 
 4167: sub print_serverstatuses {
 4168:     my ($dom,$settings,$rowtotal) = @_;
 4169:     my $datatable;
 4170:     my @pages = &serverstatus_pages();
 4171:     my (%namedaccess,%machineaccess);
 4172:     foreach my $type (@pages) {
 4173:         $namedaccess{$type} = '';
 4174:         $machineaccess{$type}= '';
 4175:     }
 4176:     if (ref($settings) eq 'HASH') {
 4177:         foreach my $type (@pages) {
 4178:             if (exists($settings->{$type})) {
 4179:                 if (ref($settings->{$type}) eq 'HASH') {
 4180:                     foreach my $key (keys(%{$settings->{$type}})) {
 4181:                         if ($key eq 'namedusers') {
 4182:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4183:                         } elsif ($key eq 'machines') {
 4184:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4185:                         }
 4186:                     }
 4187:                 }
 4188:             }
 4189:         }
 4190:     }
 4191:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4192:     my $rownum = 0;
 4193:     my $css_class;
 4194:     foreach my $type (@pages) {
 4195:         $rownum ++;
 4196:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4197:         $datatable .= '<tr'.$css_class.'>'.
 4198:                       '<td><span class="LC_nobreak">'.
 4199:                       $titles->{$type}.'</span></td>'.
 4200:                       '<td class="LC_left_item">'.
 4201:                       '<input type="text" name="'.$type.'_namedusers" '.
 4202:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4203:                       '<td class="LC_right_item">'.
 4204:                       '<span class="LC_nobreak">'.
 4205:                       '<input type="text" name="'.$type.'_machines" '.
 4206:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4207:                       '</td></tr>'."\n";
 4208:     }
 4209:     $$rowtotal += $rownum;
 4210:     return $datatable;
 4211: }
 4212: 
 4213: sub serverstatus_pages {
 4214:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4215:             'clusterstatus','metadata_keywords','metadata_harvest',
 4216:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4217: }
 4218: 
 4219: sub coursecategories_javascript {
 4220:     my ($settings) = @_;
 4221:     my ($output,$jstext,$cathash);
 4222:     if (ref($settings) eq 'HASH') {
 4223:         $cathash = $settings->{'cats'};
 4224:     }
 4225:     if (ref($cathash) eq 'HASH') {
 4226:         my (@cats,@jsarray,%idx);
 4227:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4228:         if (@jsarray > 0) {
 4229:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4230:             for (my $i=0; $i<@jsarray; $i++) {
 4231:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4232:                     my $catstr = join('","',@{$jsarray[$i]});
 4233:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4234:                 }
 4235:             }
 4236:         }
 4237:     } else {
 4238:         $jstext  = '    var categories = Array(1);'."\n".
 4239:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4240:     }
 4241:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4242:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4243:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4244:     $output = <<"ENDSCRIPT";
 4245: <script type="text/javascript">
 4246: // <![CDATA[
 4247: function reorderCats(form,parent,item,idx) {
 4248:     var changedVal;
 4249: $jstext
 4250:     var newpos = 'addcategory_pos';
 4251:     var current = new Array;
 4252:     if (parent == '') {
 4253:         var has_instcode = 0;
 4254:         var maxtop = categories[idx].length;
 4255:         for (var j=0; j<maxtop; j++) {
 4256:             if (categories[idx][j] == 'instcode::0') {
 4257:                 has_instcode == 1;
 4258:             }
 4259:         }
 4260:         if (has_instcode == 0) {
 4261:             categories[idx][maxtop] = 'instcode_pos';
 4262:         }
 4263:     } else {
 4264:         newpos += '_'+parent;
 4265:     }
 4266:     var maxh = 1 + categories[idx].length;
 4267:     var current = new Array;
 4268:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4269:     if (item == newpos) {
 4270:         changedVal = newitemVal;
 4271:     } else {
 4272:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4273:         current[newitemVal] = newpos;
 4274:     }
 4275:     for (var i=0; i<categories[idx].length; i++) {
 4276:         var elementName = categories[idx][i];
 4277:         if (elementName != item) {
 4278:             if (form.elements[elementName]) {
 4279:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4280:                 current[currVal] = elementName;
 4281:             }
 4282:         }
 4283:     }
 4284:     var oldVal;
 4285:     for (var j=0; j<maxh; j++) {
 4286:         if (current[j] == undefined) {
 4287:             oldVal = j;
 4288:         }
 4289:     }
 4290:     if (oldVal < changedVal) {
 4291:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4292:            var elementName = current[k];
 4293:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4294:         }
 4295:     } else {
 4296:         for (var k=changedVal; k<oldVal; k++) {
 4297:             var elementName = current[k];
 4298:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4299:         }
 4300:     }
 4301:     return;
 4302: }
 4303: 
 4304: function categoryCheck(form) {
 4305:     if (form.elements['addcategory_name'].value == 'instcode') {
 4306:         alert('$instcode_reserved\\n$choose_again');
 4307:         return false;
 4308:     }
 4309:     if (form.elements['addcategory_name'].value == 'communities') {
 4310:         alert('$communities_reserved\\n$choose_again');
 4311:         return false;
 4312:     }
 4313:     return true;
 4314: }
 4315: 
 4316: // ]]>
 4317: </script>
 4318: 
 4319: ENDSCRIPT
 4320:     return $output;
 4321: }
 4322: 
 4323: sub initialize_categories {
 4324:     my ($itemcount) = @_;
 4325:     my ($datatable,$css_class,$chgstr);
 4326:     my %default_names = (
 4327:                       instcode    => 'Official courses (with institutional codes)',
 4328:                       communities => 'Communities',
 4329:                         );
 4330:     my $select0 = ' selected="selected"';
 4331:     my $select1 = '';
 4332:     foreach my $default ('instcode','communities') {
 4333:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4334:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4335:         if ($default eq 'communities') {
 4336:             $select1 = $select0;
 4337:             $select0 = '';
 4338:         }
 4339:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4340:                      .'<select name="'.$default.'_pos">'
 4341:                      .'<option value="0"'.$select0.'>1</option>'
 4342:                      .'<option value="1"'.$select1.'>2</option>'
 4343:                      .'<option value="2">3</option></select>&nbsp;'
 4344:                      .$default_names{$default}
 4345:                      .'</span></td><td><span class="LC_nobreak">'
 4346:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4347:                      .&mt('Display').'</label>&nbsp;<label>'
 4348:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4349:                  .'</label></span></td></tr>';
 4350:         $itemcount ++;
 4351:     }
 4352:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4353:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4354:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4355:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4356:                   .'<option value="0">1</option>'
 4357:                   .'<option value="1">2</option>'
 4358:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4359:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4360:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4361:     return $datatable;
 4362: }
 4363: 
 4364: sub build_category_rows {
 4365:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4366:     my ($text,$name,$item,$chgstr);
 4367:     if (ref($cats) eq 'ARRAY') {
 4368:         my $maxdepth = scalar(@{$cats});
 4369:         if (ref($cats->[$depth]) eq 'HASH') {
 4370:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4371:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4372:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4373:                 $text .= '<td><table class="LC_datatable">';
 4374:                 my ($idxnum,$parent_name,$parent_item);
 4375:                 my $higher = $depth - 1;
 4376:                 if ($higher == 0) {
 4377:                     $parent_name = &escape($parent).'::'.$higher;
 4378:                 } else {
 4379:                     if (ref($path) eq 'ARRAY') {
 4380:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4381:                     }
 4382:                 }
 4383:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4384:                 for (my $j=0; $j<=$numchildren; $j++) {
 4385:                     if ($j < $numchildren) {
 4386:                         $name = $cats->[$depth]{$parent}[$j];
 4387:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4388:                         $idxnum = $idx->{$item};
 4389:                     } else {
 4390:                         $name = $parent_name;
 4391:                         $item = $parent_item;
 4392:                     }
 4393:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4394:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4395:                     for (my $i=0; $i<=$numchildren; $i++) {
 4396:                         my $vpos = $i+1;
 4397:                         my $selstr;
 4398:                         if ($j == $i) {
 4399:                             $selstr = ' selected="selected" ';
 4400:                         }
 4401:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4402:                     }
 4403:                     $text .= '</select>&nbsp;';
 4404:                     if ($j < $numchildren) {
 4405:                         my $deeper = $depth+1;
 4406:                         $text .= $name.'&nbsp;'
 4407:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4408:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4409:                         if(ref($path) eq 'ARRAY') {
 4410:                             push(@{$path},$name);
 4411:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4412:                             pop(@{$path});
 4413:                         }
 4414:                     } else {
 4415:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4416:                         if ($j == $numchildren) {
 4417:                             $text .= $name;
 4418:                         } else {
 4419:                             $text .= $item;
 4420:                         }
 4421:                         $text .= '" value="" />';
 4422:                     }
 4423:                     $text .= '</td></tr>';
 4424:                 }
 4425:                 $text .= '</table></td>';
 4426:             } else {
 4427:                 my $higher = $depth-1;
 4428:                 if ($higher == 0) {
 4429:                     $name = &escape($parent).'::'.$higher;
 4430:                 } else {
 4431:                     if (ref($path) eq 'ARRAY') {
 4432:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4433:                     }
 4434:                 }
 4435:                 my $colspan;
 4436:                 if ($parent ne 'instcode') {
 4437:                     $colspan = $maxdepth - $depth - 1;
 4438:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4439:                 }
 4440:             }
 4441:         }
 4442:     }
 4443:     return $text;
 4444: }
 4445: 
 4446: sub modifiable_userdata_row {
 4447:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4448:     my $rolename;
 4449:     if ($context eq 'selfcreate') {
 4450:         if (ref($usertypes) eq 'HASH') {
 4451:             $rolename = $usertypes->{$role};
 4452:         } else {
 4453:             $rolename = $role;
 4454:         }
 4455:     } else {
 4456:         if ($role eq 'cr') {
 4457:             $rolename = &mt('Custom role');
 4458:         } else {
 4459:             $rolename = &Apache::lonnet::plaintext($role);
 4460:         }
 4461:     }
 4462:     my @fields = ('lastname','firstname','middlename','generation',
 4463:                   'permanentemail','id');
 4464:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4465:     my $output;
 4466:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4467:     $output = '<tr '.$css_class.'>'.
 4468:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4469:               '<td class="LC_left_item" colspan="2"><table>';
 4470:     my $rem;
 4471:     my %checks;
 4472:     if (ref($settings) eq 'HASH') {
 4473:         if (ref($settings->{$context}) eq 'HASH') {
 4474:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4475:                 foreach my $field (@fields) {
 4476:                     if ($settings->{$context}->{$role}->{$field}) {
 4477:                         $checks{$field} = ' checked="checked" ';
 4478:                     }
 4479:                 }
 4480:             }
 4481:         }
 4482:     }
 4483:     for (my $i=0; $i<@fields; $i++) {
 4484:         my $rem = $i%($numinrow);
 4485:         if ($rem == 0) {
 4486:             if ($i > 0) {
 4487:                 $output .= '</tr>';
 4488:             }
 4489:             $output .= '<tr>';
 4490:         }
 4491:         my $check = ' ';
 4492:         if (exists($checks{$fields[$i]})) {
 4493:             $check = $checks{$fields[$i]}
 4494:         } else {
 4495:             if ($role eq 'st') {
 4496:                 if (ref($settings) ne 'HASH') {
 4497:                     $check = ' checked="checked" '; 
 4498:                 }
 4499:             }
 4500:         }
 4501:         $output .= '<td class="LC_left_item">'.
 4502:                    '<span class="LC_nobreak"><label>'.
 4503:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4504:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4505:                    '</label></span></td>';
 4506:         $rem = @fields%($numinrow);
 4507:     }
 4508:     my $colsleft = $numinrow - $rem;
 4509:     if ($colsleft > 1 ) {
 4510:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4511:                    '&nbsp;</td>';
 4512:     } elsif ($colsleft == 1) {
 4513:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4514:     }
 4515:     $output .= '</tr></table></td></tr>';
 4516:     return $output;
 4517: }
 4518: 
 4519: sub insttypes_row {
 4520:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4521:     my %lt = &Apache::lonlocal::texthash (
 4522:                       cansearch => 'Users allowed to search',
 4523:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4524:                       lockablenames => 'User preference to lock name',
 4525:              );
 4526:     my $showdom;
 4527:     if ($context eq 'cansearch') {
 4528:         $showdom = ' ('.$dom.')';
 4529:     }
 4530:     my $class = 'LC_left_item';
 4531:     if ($context eq 'statustocreate') {
 4532:         $class = 'LC_right_item';
 4533:     }
 4534:     my $output =  '<tr class="LC_odd_row">'.
 4535:                   '<td>'.$lt{$context}.$showdom.
 4536:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4537:     my $rem;
 4538:     if (ref($types) eq 'ARRAY') {
 4539:         for (my $i=0; $i<@{$types}; $i++) {
 4540:             if (defined($usertypes->{$types->[$i]})) {
 4541:                 my $rem = $i%($numinrow);
 4542:                 if ($rem == 0) {
 4543:                     if ($i > 0) {
 4544:                         $output .= '</tr>';
 4545:                     }
 4546:                     $output .= '<tr>';
 4547:                 }
 4548:                 my $check = ' ';
 4549:                 if (ref($settings) eq 'HASH') {
 4550:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4551:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4552:                             $check = ' checked="checked" ';
 4553:                         }
 4554:                     } elsif ($context eq 'statustocreate') {
 4555:                         $check = ' checked="checked" ';
 4556:                     }
 4557:                 }
 4558:                 $output .= '<td class="LC_left_item">'.
 4559:                            '<span class="LC_nobreak"><label>'.
 4560:                            '<input type="checkbox" name="'.$context.'" '.
 4561:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4562:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4563:             }
 4564:         }
 4565:         $rem = @{$types}%($numinrow);
 4566:     }
 4567:     my $colsleft = $numinrow - $rem;
 4568:     if (($rem == 0) && (@{$types} > 0)) {
 4569:         $output .= '<tr>';
 4570:     }
 4571:     if ($colsleft > 1) {
 4572:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4573:     } else {
 4574:         $output .= '<td class="LC_left_item">';
 4575:     }
 4576:     my $defcheck = ' ';
 4577:     if (ref($settings) eq 'HASH') {  
 4578:         if (ref($settings->{$context}) eq 'ARRAY') {
 4579:             if (grep(/^default$/,@{$settings->{$context}})) {
 4580:                 $defcheck = ' checked="checked" ';
 4581:             }
 4582:         } elsif ($context eq 'statustocreate') {
 4583:             $defcheck = ' checked="checked" ';
 4584:         }
 4585:     }
 4586:     $output .= '<span class="LC_nobreak"><label>'.
 4587:                '<input type="checkbox" name="'.$context.'" '.
 4588:                'value="default"'.$defcheck.'/>'.
 4589:                $othertitle.'</label></span></td>'.
 4590:                '</tr></table></td></tr>';
 4591:     return $output;
 4592: }
 4593: 
 4594: sub sorted_searchtitles {
 4595:     my %searchtitles = &Apache::lonlocal::texthash(
 4596:                          'uname' => 'username',
 4597:                          'lastname' => 'last name',
 4598:                          'lastfirst' => 'last name, first name',
 4599:                      );
 4600:     my @titleorder = ('uname','lastname','lastfirst');
 4601:     return (\%searchtitles,\@titleorder);
 4602: }
 4603: 
 4604: sub sorted_searchtypes {
 4605:     my %srchtypes_desc = (
 4606:                            exact    => 'is exact match',
 4607:                            contains => 'contains ..',
 4608:                            begins   => 'begins with ..',
 4609:                          );
 4610:     my @srchtypeorder = ('exact','begins','contains');
 4611:     return (\%srchtypes_desc,\@srchtypeorder);
 4612: }
 4613: 
 4614: sub usertype_update_row {
 4615:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4616:     my $datatable;
 4617:     my $numinrow = 4;
 4618:     foreach my $type (@{$types}) {
 4619:         if (defined($usertypes->{$type})) {
 4620:             $$rownums ++;
 4621:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4622:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4623:                           '</td><td class="LC_left_item"><table>';
 4624:             for (my $i=0; $i<@{$fields}; $i++) {
 4625:                 my $rem = $i%($numinrow);
 4626:                 if ($rem == 0) {
 4627:                     if ($i > 0) {
 4628:                         $datatable .= '</tr>';
 4629:                     }
 4630:                     $datatable .= '<tr>';
 4631:                 }
 4632:                 my $check = ' ';
 4633:                 if (ref($settings) eq 'HASH') {
 4634:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4635:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4636:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4637:                                 $check = ' checked="checked" ';
 4638:                             }
 4639:                         }
 4640:                     }
 4641:                 }
 4642: 
 4643:                 if ($i == @{$fields}-1) {
 4644:                     my $colsleft = $numinrow - $rem;
 4645:                     if ($colsleft > 1) {
 4646:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4647:                     } else {
 4648:                         $datatable .= '<td>';
 4649:                     }
 4650:                 } else {
 4651:                     $datatable .= '<td>';
 4652:                 }
 4653:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4654:                               '<input type="checkbox" name="updateable_'.$type.
 4655:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4656:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4657:             }
 4658:             $datatable .= '</tr></table></td></tr>';
 4659:         }
 4660:     }
 4661:     return $datatable;
 4662: }
 4663: 
 4664: sub modify_login {
 4665:     my ($r,$dom,$confname,%domconfig) = @_;
 4666:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4667:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4668:     %title = ( coursecatalog => 'Display course catalog',
 4669:                adminmail => 'Display administrator E-mail address',
 4670:                newuser => 'Link for visitors to create a user account',
 4671:                loginheader => 'Log-in box header');
 4672:     @offon = ('off','on');
 4673:     if (ref($domconfig{login}) eq 'HASH') {
 4674:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4675:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4676:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4677:             }
 4678:         }
 4679:     }
 4680:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4681:                                            \%domconfig,\%loginhash);
 4682:     my @toggles = ('coursecatalog','adminmail','newuser');
 4683:     foreach my $item (@toggles) {
 4684:         $loginhash{login}{$item} = $env{'form.'.$item};
 4685:     }
 4686:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4687:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4688:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4689:                                          \%loginhash);
 4690:     }
 4691: 
 4692:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4693:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4694:     if (keys(%servers) > 1) {
 4695:         foreach my $lonhost (keys(%servers)) {
 4696:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4697:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4698:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4699:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4700:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4701:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4702:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4703:                         $changes{'loginvia'}{$lonhost} = 1;
 4704:                     } else {
 4705:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4706:                         $changes{'loginvia'}{$lonhost} = 1;
 4707:                     }
 4708:                 } else {
 4709:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4710:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4711:                         $changes{'loginvia'}{$lonhost} = 1;
 4712:                     }
 4713:                 }
 4714:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4715:                     foreach my $item (@loginvia_attribs) {
 4716:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4717:                     }
 4718:                 } else {
 4719:                     foreach my $item (@loginvia_attribs) {
 4720:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4721:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4722:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4723:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4724:                                 $new = '/';
 4725:                             }
 4726:                         }
 4727:                         if (($item eq 'custompath') && 
 4728:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4729:                             $new = '';
 4730:                         }
 4731:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4732:                             $changes{'loginvia'}{$lonhost} = 1;
 4733:                         }
 4734:                         if ($item eq 'exempt') {
 4735:                             $new =~ s/^\s+//;
 4736:                             $new =~ s/\s+$//;
 4737:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4738:                             my @okips;
 4739:                             foreach my $ip (@poss_ips) {
 4740:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4741:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4742:                                         push(@okips,$ip); 
 4743:                                     }
 4744:                                 }
 4745:                             }
 4746:                             if (@okips > 0) {
 4747:                                 $new = join(',',@okips); 
 4748:                             } else {
 4749:                                 $new = ''; 
 4750:                             }
 4751:                         }
 4752:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4753:                     }
 4754:                 }
 4755:             } else {
 4756:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4757:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4758:                     $changes{'loginvia'}{$lonhost} = 1;
 4759:                     foreach my $item (@loginvia_attribs) {
 4760:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4761:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4762:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4763:                                 $new = '/';
 4764:                             }
 4765:                         }
 4766:                         if (($item eq 'custompath') && 
 4767:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4768:                             $new = '';
 4769:                         }
 4770:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4771:                     }
 4772:                 }
 4773:             }
 4774:         }
 4775:     }
 4776: 
 4777:     my $servadm = $r->dir_config('lonAdmEMail');
 4778:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 4779:     if (ref($domconfig{'login'}) eq 'HASH') {
 4780:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 4781:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 4782:                 if ($lang eq 'nolang') {
 4783:                     push(@currlangs,$lang);
 4784:                 } elsif (defined($langchoices{$lang})) {
 4785:                     push(@currlangs,$lang);
 4786:                 } else {
 4787:                     next;
 4788:                 }
 4789:             }
 4790:         }
 4791:     }
 4792:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 4793:     if (@currlangs > 0) {
 4794:         foreach my $lang (@currlangs) {
 4795:             if (grep(/^\Q$lang\E$/,@delurls)) {
 4796:                 $changes{'helpurl'}{$lang} = 1;
 4797:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 4798:                 $changes{'helpurl'}{$lang} = 1;
 4799:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 4800:                 push(@newlangs,$lang);
 4801:             } else {
 4802:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4803:             }
 4804:         }
 4805:     }
 4806:     unless (grep(/^nolang$/,@currlangs)) {
 4807:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 4808:             $changes{'helpurl'}{'nolang'} = 1;
 4809:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 4810:             push(@newlangs,'nolang');
 4811:         }
 4812:     }
 4813:     if ($env{'form.loginhelpurl_add_lang'}) {
 4814:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 4815:             ($env{'form.loginhelpurl_add_file.filename'})) {
 4816:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 4817:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 4818:         }
 4819:     }
 4820:     if ((@newlangs > 0) || ($addedfile)) {
 4821:         my $error;
 4822:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 4823:         if ($configuserok eq 'ok') {
 4824:             if ($switchserver) {
 4825:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 4826:             } elsif ($author_ok eq 'ok') {
 4827:                 my @allnew = @newlangs;
 4828:                 if ($addedfile ne '') {
 4829:                     push(@allnew,$addedfile);
 4830:                 }
 4831:                 foreach my $lang (@allnew) {
 4832:                     my $formelem = 'loginhelpurl_'.$lang;
 4833:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 4834:                         $formelem = 'loginhelpurl_add_file';
 4835:                     }
 4836:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 4837:                                                                "help/$lang",'','',$newfile{$lang});
 4838:                     if ($result eq 'ok') {
 4839:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 4840:                         $changes{'helpurl'}{$lang} = 1;
 4841:                     } else {
 4842:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 4843:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 4844:                         if ((grep(/^\Q$lang\E$/,@currlangs)) && 
 4845:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 4846: 
 4847:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4848:                         }
 4849:                     }
 4850:                 }
 4851:             } else {
 4852:                 $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);
 4853:             }
 4854:         } else {
 4855:             $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);
 4856:         }
 4857:         if ($error) {
 4858:             &Apache::lonnet::logthis($error);
 4859:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4860:         }
 4861:     }
 4862:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 4863: 
 4864:     my $defaulthelpfile = '/adm/loginproblems.html';
 4865:     my $defaulttext = &mt('Default in use');
 4866: 
 4867:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4868:                                              $dom);
 4869:     if ($putresult eq 'ok') {
 4870:         my @toggles = ('coursecatalog','adminmail','newuser');
 4871:         my %defaultchecked = (
 4872:                     'coursecatalog' => 'on',
 4873:                     'adminmail'     => 'off',
 4874:                     'newuser'       => 'off',
 4875:         );
 4876:         if (ref($domconfig{'login'}) eq 'HASH') {
 4877:             foreach my $item (@toggles) {
 4878:                 if ($defaultchecked{$item} eq 'on') { 
 4879:                     if (($domconfig{'login'}{$item} eq '0') &&
 4880:                         ($env{'form.'.$item} eq '1')) {
 4881:                         $changes{$item} = 1;
 4882:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4883:                               $domconfig{'login'}{$item} eq '1') &&
 4884:                              ($env{'form.'.$item} eq '0')) {
 4885:                         $changes{$item} = 1;
 4886:                     }
 4887:                 } elsif ($defaultchecked{$item} eq 'off') {
 4888:                     if (($domconfig{'login'}{$item} eq '1') &&
 4889:                         ($env{'form.'.$item} eq '0')) {
 4890:                         $changes{$item} = 1;
 4891:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4892:                               $domconfig{'login'}{$item} eq '0') &&
 4893:                              ($env{'form.'.$item} eq '1')) {
 4894:                         $changes{$item} = 1;
 4895:                     }
 4896:                 }
 4897:             }
 4898:         }
 4899:         if (keys(%changes) > 0 || $colchgtext) {
 4900:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4901:             $resulttext = &mt('Changes made:').'<ul>';
 4902:             foreach my $item (sort(keys(%changes))) {
 4903:                 if ($item eq 'loginvia') {
 4904:                     if (ref($changes{$item}) eq 'HASH') {
 4905:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4906:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4907:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4908:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4909:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4910:                                     $protocol = 'http' if ($protocol ne 'https');
 4911:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4912: 
 4913:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4914:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4915:                                     } else {
 4916:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4917:                                     }
 4918:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4919:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4920:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4921:                                     }
 4922:                                     $resulttext .= '</li>';
 4923:                                 } else {
 4924:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4925:                                 }
 4926:                             } else {
 4927:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4928:                             }
 4929:                         }
 4930:                         $resulttext .= '</ul></li>';
 4931:                     }
 4932:                 } elsif ($item eq 'helpurl') {
 4933:                     if (ref($changes{$item}) eq 'HASH') {
 4934:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 4935:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 4936:                                 my ($chg,$link);
 4937:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 4938:                                 if ($lang eq 'nolang') {
 4939:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 4940:                                 } else {
 4941:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 4942:                                 }
 4943:                                 $resulttext .= '<li>'.$chg.'</li>';
 4944:                             } else {
 4945:                                 my $chg;
 4946:                                 if ($lang eq 'nolang') {
 4947:                                     $chg = &mt('custom log-in help file for no preferred language');
 4948:                                 } else {
 4949:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 4950:                                 }
 4951:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 4952:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 4953:                                                       '?inhibitmenu=yes',$chg,600,500).
 4954:                                                '</li>';
 4955:                             }
 4956:                         }
 4957:                     }
 4958:                 } elsif ($item eq 'captcha') {
 4959:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4960:                         my $chgtxt; 
 4961:                         if ($loginhash{'login'}{$item} eq 'notused') {
 4962:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 4963:                         } else {
 4964:                             my %captchas = &captcha_phrases();
 4965:                             if ($captchas{$loginhash{'login'}{$item}}) {
 4966:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 4967:                             } else {
 4968:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 4969:                             }
 4970:                         }
 4971:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4972:                     }
 4973:                 } elsif ($item eq 'recaptchakeys') {
 4974:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4975:                         my ($privkey,$pubkey);
 4976:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 4977:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 4978:                             $privkey = $loginhash{'login'}{$item}{'private'};
 4979:                         }
 4980:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 4981:                         if (!$pubkey) {
 4982:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 4983:                         } else {
 4984:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 4985:                         }
 4986:                         if (!$privkey) {
 4987:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 4988:                         } else {
 4989:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 4990:                         }
 4991:                         $chgtxt .= '</ul>';
 4992:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4993:                     }
 4994:                 } else {
 4995:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4996:                 }
 4997:             }
 4998:             $resulttext .= $colchgtext.'</ul>';
 4999:         } else {
 5000:             $resulttext = &mt('No changes made to log-in page settings');
 5001:         }
 5002:     } else {
 5003:         $resulttext = '<span class="LC_error">'.
 5004: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5005:     }
 5006:     if ($errors) {
 5007:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 5008:                        $errors.'</ul>';
 5009:     }
 5010:     return $resulttext;
 5011: }
 5012: 
 5013: sub color_font_choices {
 5014:     my %choices =
 5015:         &Apache::lonlocal::texthash (
 5016:             img => "Header",
 5017:             bgs => "Background colors",
 5018:             links => "Link colors",
 5019:             images => "Images",
 5020:             font => "Font color",
 5021:             fontmenu => "Font Menu",
 5022:             pgbg => "Page",
 5023:             tabbg => "Header",
 5024:             sidebg => "Border",
 5025:             link => "Link",
 5026:             alink => "Active link",
 5027:             vlink => "Visited link",
 5028:         );
 5029:     return %choices;
 5030: }
 5031: 
 5032: sub modify_rolecolors {
 5033:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 5034:     my ($resulttext,%rolehash);
 5035:     $rolehash{'rolecolors'} = {};
 5036:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5037:         if ($domconfig{'rolecolors'} eq '') {
 5038:             $domconfig{'rolecolors'} = {};
 5039:         }
 5040:     }
 5041:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5042:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5043:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5044:                                              $dom);
 5045:     if ($putresult eq 'ok') {
 5046:         if (keys(%changes) > 0) {
 5047:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5048:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5049:                                              $rolehash{'rolecolors'});
 5050:         } else {
 5051:             $resulttext = &mt('No changes made to default color schemes');
 5052:         }
 5053:     } else {
 5054:         $resulttext = '<span class="LC_error">'.
 5055: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5056:     }
 5057:     if ($errors) {
 5058:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5059:                        $errors.'</ul>';
 5060:     }
 5061:     return $resulttext;
 5062: }
 5063: 
 5064: sub modify_colors {
 5065:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5066:     my (%changes,%choices);
 5067:     my @bgs;
 5068:     my @links = ('link','alink','vlink');
 5069:     my @logintext;
 5070:     my @images;
 5071:     my $servadm = $r->dir_config('lonAdmEMail');
 5072:     my $errors;
 5073:     foreach my $role (@{$roles}) {
 5074:         if ($role eq 'login') {
 5075:             %choices = &login_choices();
 5076:             @logintext = ('textcol','bgcol');
 5077:         } else {
 5078:             %choices = &color_font_choices();
 5079:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5080:         }
 5081:         if ($role eq 'login') {
 5082:             @images = ('img','logo','domlogo','login');
 5083:             @bgs = ('pgbg','mainbg','sidebg');
 5084:         } else {
 5085:             @images = ('img');
 5086:             @bgs = ('pgbg','tabbg','sidebg'); 
 5087:         }
 5088:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5089:         foreach my $item (@bgs,@links,@logintext) {
 5090:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5091:         }
 5092:         my ($configuserok,$author_ok,$switchserver) = 
 5093:             &config_check($dom,$confname,$servadm);
 5094:         my ($width,$height) = &thumb_dimensions();
 5095:         if (ref($domconfig->{$role}) ne 'HASH') {
 5096:             $domconfig->{$role} = {};
 5097:         }
 5098:         foreach my $img (@images) {
 5099:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5100:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5101:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5102:                 } else { 
 5103:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5104:                 }
 5105:             } 
 5106: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5107: 		 && !defined($domconfig->{$role}{$img})
 5108: 		 && !$env{'form.'.$role.'_del_'.$img}
 5109: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5110: 		# import the old configured image from the .tab setting
 5111: 		# if they haven't provided a new one 
 5112: 		$domconfig->{$role}{$img} = 
 5113: 		    $env{'form.'.$role.'_import_'.$img};
 5114: 	    }
 5115:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5116:                 my $error;
 5117:                 if ($configuserok eq 'ok') {
 5118:                     if ($switchserver) {
 5119:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5120:                     } else {
 5121:                         if ($author_ok eq 'ok') {
 5122:                             my ($result,$logourl) = 
 5123:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5124:                                            $dom,$confname,$img,$width,$height);
 5125:                             if ($result eq 'ok') {
 5126:                                 $confhash->{$role}{$img} = $logourl;
 5127:                                 $changes{$role}{'images'}{$img} = 1;
 5128:                             } else {
 5129:                                 $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);
 5130:                             }
 5131:                         } else {
 5132:                             $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);
 5133:                         }
 5134:                     }
 5135:                 } else {
 5136:                     $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);
 5137:                 }
 5138:                 if ($error) {
 5139:                     &Apache::lonnet::logthis($error);
 5140:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5141:                 }
 5142:             } elsif ($domconfig->{$role}{$img} ne '') {
 5143:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5144:                     my $error;
 5145:                     if ($configuserok eq 'ok') {
 5146: # is confname an author?
 5147:                         if ($switchserver eq '') {
 5148:                             if ($author_ok eq 'ok') {
 5149:                                 my ($result,$logourl) = 
 5150:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5151:                                             $dom,$confname,$img,$width,$height);
 5152:                                 if ($result eq 'ok') {
 5153:                                     $confhash->{$role}{$img} = $logourl;
 5154: 				    $changes{$role}{'images'}{$img} = 1;
 5155:                                 }
 5156:                             }
 5157:                         }
 5158:                     }
 5159:                 }
 5160:             }
 5161:         }
 5162:         if (ref($domconfig) eq 'HASH') {
 5163:             if (ref($domconfig->{$role}) eq 'HASH') {
 5164:                 foreach my $img (@images) {
 5165:                     if ($domconfig->{$role}{$img} ne '') {
 5166:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5167:                             $confhash->{$role}{$img} = '';
 5168:                             $changes{$role}{'images'}{$img} = 1;
 5169:                         } else {
 5170:                             if ($confhash->{$role}{$img} eq '') {
 5171:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5172:                             }
 5173:                         }
 5174:                     } else {
 5175:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5176:                             $confhash->{$role}{$img} = '';
 5177:                             $changes{$role}{'images'}{$img} = 1;
 5178:                         } 
 5179:                     }
 5180:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5181:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5182:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5183:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5184:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5185:                             }
 5186:                         } else {
 5187:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5188:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5189:                             }
 5190:                         }
 5191:                     }
 5192:                 }
 5193:                 if ($domconfig->{$role}{'font'} ne '') {
 5194:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5195:                         $changes{$role}{'font'} = 1;
 5196:                     }
 5197:                 } else {
 5198:                     if ($confhash->{$role}{'font'}) {
 5199:                         $changes{$role}{'font'} = 1;
 5200:                     }
 5201:                 }
 5202:                 if ($role ne 'login') {
 5203:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5204:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5205:                             $changes{$role}{'fontmenu'} = 1;
 5206:                         }
 5207:                     } else {
 5208:                         if ($confhash->{$role}{'fontmenu'}) {
 5209:                             $changes{$role}{'fontmenu'} = 1;
 5210:                         }
 5211:                     }
 5212:                 }
 5213:                 foreach my $item (@bgs) {
 5214:                     if ($domconfig->{$role}{$item} ne '') {
 5215:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5216:                             $changes{$role}{'bgs'}{$item} = 1;
 5217:                         } 
 5218:                     } else {
 5219:                         if ($confhash->{$role}{$item}) {
 5220:                             $changes{$role}{'bgs'}{$item} = 1;
 5221:                         }
 5222:                     }
 5223:                 }
 5224:                 foreach my $item (@links) {
 5225:                     if ($domconfig->{$role}{$item} ne '') {
 5226:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5227:                             $changes{$role}{'links'}{$item} = 1;
 5228:                         }
 5229:                     } else {
 5230:                         if ($confhash->{$role}{$item}) {
 5231:                             $changes{$role}{'links'}{$item} = 1;
 5232:                         }
 5233:                     }
 5234:                 }
 5235:                 foreach my $item (@logintext) {
 5236:                     if ($domconfig->{$role}{$item} ne '') {
 5237:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5238:                             $changes{$role}{'logintext'}{$item} = 1;
 5239:                         }
 5240:                     } else {
 5241:                         if ($confhash->{$role}{$item}) {
 5242:                             $changes{$role}{'logintext'}{$item} = 1;
 5243:                         }
 5244:                     }
 5245:                 }
 5246:             } else {
 5247:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5248:                                         \@logintext,$confhash,\%changes); 
 5249:             }
 5250:         } else {
 5251:             &default_change_checker($role,\@images,\@links,\@bgs,
 5252:                                     \@logintext,$confhash,\%changes); 
 5253:         }
 5254:     }
 5255:     return ($errors,%changes);
 5256: }
 5257: 
 5258: sub config_check {
 5259:     my ($dom,$confname,$servadm) = @_;
 5260:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5261:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5262:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5263:                                                    $confname,$servadm);
 5264:     if ($configuserok eq 'ok') {
 5265:         $switchserver = &check_switchserver($dom,$confname);
 5266:         if ($switchserver eq '') {
 5267:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5268:         }
 5269:     }
 5270:     return ($configuserok,$author_ok,$switchserver);
 5271: }
 5272: 
 5273: sub default_change_checker {
 5274:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5275:     foreach my $item (@{$links}) {
 5276:         if ($confhash->{$role}{$item}) {
 5277:             $changes->{$role}{'links'}{$item} = 1;
 5278:         }
 5279:     }
 5280:     foreach my $item (@{$bgs}) {
 5281:         if ($confhash->{$role}{$item}) {
 5282:             $changes->{$role}{'bgs'}{$item} = 1;
 5283:         }
 5284:     }
 5285:     foreach my $item (@{$logintext}) {
 5286:         if ($confhash->{$role}{$item}) {
 5287:             $changes->{$role}{'logintext'}{$item} = 1;
 5288:         }
 5289:     }
 5290:     foreach my $img (@{$images}) {
 5291:         if ($env{'form.'.$role.'_del_'.$img}) {
 5292:             $confhash->{$role}{$img} = '';
 5293:             $changes->{$role}{'images'}{$img} = 1;
 5294:         }
 5295:         if ($role eq 'login') {
 5296:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5297:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5298:             }
 5299:         }
 5300:     }
 5301:     if ($confhash->{$role}{'font'}) {
 5302:         $changes->{$role}{'font'} = 1;
 5303:     }
 5304: }
 5305: 
 5306: sub display_colorchgs {
 5307:     my ($dom,$changes,$roles,$confhash) = @_;
 5308:     my (%choices,$resulttext);
 5309:     if (!grep(/^login$/,@{$roles})) {
 5310:         $resulttext = &mt('Changes made:').'<br />';
 5311:     }
 5312:     foreach my $role (@{$roles}) {
 5313:         if ($role eq 'login') {
 5314:             %choices = &login_choices();
 5315:         } else {
 5316:             %choices = &color_font_choices();
 5317:         }
 5318:         if (ref($changes->{$role}) eq 'HASH') {
 5319:             if ($role ne 'login') {
 5320:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5321:             }
 5322:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5323:                 if ($role ne 'login') {
 5324:                     $resulttext .= '<ul>';
 5325:                 }
 5326:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5327:                     if ($role ne 'login') {
 5328:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5329:                     }
 5330:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5331:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5332:                             if ($confhash->{$role}{$key}{$item}) {
 5333:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5334:                             } else {
 5335:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5336:                             }
 5337:                         } elsif ($confhash->{$role}{$item} eq '') {
 5338:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5339:                         } else {
 5340:                             my $newitem = $confhash->{$role}{$item};
 5341:                             if ($key eq 'images') {
 5342:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5343:                             }
 5344:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5345:                         }
 5346:                     }
 5347:                     if ($role ne 'login') {
 5348:                         $resulttext .= '</ul></li>';
 5349:                     }
 5350:                 } else {
 5351:                     if ($confhash->{$role}{$key} eq '') {
 5352:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5353:                     } else {
 5354:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5355:                     }
 5356:                 }
 5357:                 if ($role ne 'login') {
 5358:                     $resulttext .= '</ul>';
 5359:                 }
 5360:             }
 5361:         }
 5362:     }
 5363:     return $resulttext;
 5364: }
 5365: 
 5366: sub thumb_dimensions {
 5367:     return ('200','50');
 5368: }
 5369: 
 5370: sub check_dimensions {
 5371:     my ($inputfile) = @_;
 5372:     my ($fullwidth,$fullheight);
 5373:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5374:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5375:             my $imageinfo = <PIPE>;
 5376:             if (!close(PIPE)) {
 5377:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5378:             }
 5379:             chomp($imageinfo);
 5380:             my ($fullsize) = 
 5381:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5382:             if ($fullsize) {
 5383:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5384:             }
 5385:         }
 5386:     }
 5387:     return ($fullwidth,$fullheight);
 5388: }
 5389: 
 5390: sub check_configuser {
 5391:     my ($uhome,$dom,$confname,$servadm) = @_;
 5392:     my ($configuserok,%currroles);
 5393:     if ($uhome eq 'no_host') {
 5394:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5395:         my $configpass = &LONCAPA::Enrollment::create_password();
 5396:         $configuserok = 
 5397:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5398:                              $configpass,'','','','','',undef,$servadm);
 5399:     } else {
 5400:         $configuserok = 'ok';
 5401:         %currroles = 
 5402:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5403:     }
 5404:     return ($configuserok,%currroles);
 5405: }
 5406: 
 5407: sub check_authorstatus {
 5408:     my ($dom,$confname,%currroles) = @_;
 5409:     my $author_ok;
 5410:     if (!$currroles{':'.$dom.':au'}) {
 5411:         my $start = time;
 5412:         my $end = 0;
 5413:         $author_ok = 
 5414:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5415:                                         'au',$end,$start,'','','domconfig');
 5416:     } else {
 5417:         $author_ok = 'ok';
 5418:     }
 5419:     return $author_ok;
 5420: }
 5421: 
 5422: sub publishlogo {
 5423:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5424:     my ($output,$fname,$logourl);
 5425:     if ($action eq 'upload') {
 5426:         $fname=$env{'form.'.$formname.'.filename'};
 5427:         chop($env{'form.'.$formname});
 5428:     } else {
 5429:         ($fname) = ($formname =~ /([^\/]+)$/);
 5430:     }
 5431:     if ($savefileas ne '') {
 5432:         $fname = $savefileas;
 5433:     }
 5434:     $fname=&Apache::lonnet::clean_filename($fname);
 5435: # See if there is anything left
 5436:     unless ($fname) { return ('error: no uploaded file'); }
 5437:     $fname="$subdir/$fname";
 5438:     my $docroot=$r->dir_config('lonDocRoot'); 
 5439:     my $filepath="$docroot/priv";
 5440:     my $relpath = "$dom/$confname";
 5441:     my ($fnamepath,$file,$fetchthumb);
 5442:     $file=$fname;
 5443:     if ($fname=~m|/|) {
 5444:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5445:     }
 5446:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5447:     my $count;
 5448:     for ($count=5;$count<=$#parts;$count++) {
 5449:         $filepath.="/$parts[$count]";
 5450:         if ((-e $filepath)!=1) {
 5451:             mkdir($filepath,02770);
 5452:         }
 5453:     }
 5454:     # Check for bad extension and disallow upload
 5455:     if ($file=~/\.(\w+)$/ &&
 5456:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5457:         $output = 
 5458:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 5459:     } elsif ($file=~/\.(\w+)$/ &&
 5460:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5461:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5462:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5463:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5464:     } elsif (-d "$filepath/$file") {
 5465:         $output = &mt('File name is a directory name - rename the file and re-upload');
 5466:     } else {
 5467:         my $source = $filepath.'/'.$file;
 5468:         my $logfile;
 5469:         if (!open($logfile,">>$source".'.log')) {
 5470:             return (&mt('No write permission to Construction Space'));
 5471:         }
 5472:         print $logfile
 5473: "\n================= Publish ".localtime()." ================\n".
 5474: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5475: # Save the file
 5476:         if (!open(FH,'>'.$source)) {
 5477:             &Apache::lonnet::logthis('Failed to create '.$source);
 5478:             return (&mt('Failed to create file'));
 5479:         }
 5480:         if ($action eq 'upload') {
 5481:             if (!print FH ($env{'form.'.$formname})) {
 5482:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5483:                 return (&mt('Failed to write file'));
 5484:             }
 5485:         } else {
 5486:             my $original = &Apache::lonnet::filelocation('',$formname);
 5487:             if(!copy($original,$source)) {
 5488:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5489:                 return (&mt('Failed to write file'));
 5490:             }
 5491:         }
 5492:         close(FH);
 5493:         chmod(0660, $source); # Permissions to rw-rw---.
 5494: 
 5495:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5496:         my $copyfile=$targetdir.'/'.$file;
 5497: 
 5498:         my @parts=split(/\//,$targetdir);
 5499:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5500:         for (my $count=5;$count<=$#parts;$count++) {
 5501:             $path.="/$parts[$count]";
 5502:             if (!-e $path) {
 5503:                 print $logfile "\nCreating directory ".$path;
 5504:                 mkdir($path,02770);
 5505:             }
 5506:         }
 5507:         my $versionresult;
 5508:         if (-e $copyfile) {
 5509:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5510:         } else {
 5511:             $versionresult = 'ok';
 5512:         }
 5513:         if ($versionresult eq 'ok') {
 5514:             if (copy($source,$copyfile)) {
 5515:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5516:                 $output = 'ok';
 5517:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5518:                 push(@{$modified_urls},[$copyfile,$source]);
 5519:                 my $metaoutput = 
 5520:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5521:                 unless ($registered_cleanup) {
 5522:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5523:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5524:                     $registered_cleanup=1;
 5525:                 }
 5526:             } else {
 5527:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5528:                 $output = &mt('Failed to copy file to RES space').", $!";
 5529:             }
 5530:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5531:                 my $inputfile = $filepath.'/'.$file;
 5532:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5533:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5534:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5535:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5536:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5537:                         system("convert -sample $thumbsize $inputfile $outfile");
 5538:                         chmod(0660, $filepath.'/tn-'.$file);
 5539:                         if (-e $outfile) {
 5540:                             my $copyfile=$targetdir.'/tn-'.$file;
 5541:                             if (copy($outfile,$copyfile)) {
 5542:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5543:                                 my $thumb_metaoutput = 
 5544:                                     &write_metadata($dom,$confname,$formname,
 5545:                                                     $targetdir,'tn-'.$file,$logfile);
 5546:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5547:                                 unless ($registered_cleanup) {
 5548:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5549:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5550:                                     $registered_cleanup=1;
 5551:                                 }
 5552:                             } else {
 5553:                                 print $logfile "\nUnable to write ".$copyfile.
 5554:                                                ':'.$!."\n";
 5555:                             }
 5556:                         }
 5557:                     }
 5558:                 }
 5559:             }
 5560:         } else {
 5561:             $output = $versionresult;
 5562:         }
 5563:     }
 5564:     return ($output,$logourl);
 5565: }
 5566: 
 5567: sub logo_versioning {
 5568:     my ($targetdir,$file,$logfile) = @_;
 5569:     my $target = $targetdir.'/'.$file;
 5570:     my ($maxversion,$fn,$extn,$output);
 5571:     $maxversion = 0;
 5572:     if ($file =~ /^(.+)\.(\w+)$/) {
 5573:         $fn=$1;
 5574:         $extn=$2;
 5575:     }
 5576:     opendir(DIR,$targetdir);
 5577:     while (my $filename=readdir(DIR)) {
 5578:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5579:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5580:         }
 5581:     }
 5582:     $maxversion++;
 5583:     print $logfile "\nCreating old version ".$maxversion."\n";
 5584:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5585:     if (copy($target,$copyfile)) {
 5586:         print $logfile "Copied old target to ".$copyfile."\n";
 5587:         $copyfile=$copyfile.'.meta';
 5588:         if (copy($target.'.meta',$copyfile)) {
 5589:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5590:             $output = 'ok';
 5591:         } else {
 5592:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5593:             $output = &mt('Failed to copy old meta').", $!, ";
 5594:         }
 5595:     } else {
 5596:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5597:         $output = &mt('Failed to copy old target').", $!, ";
 5598:     }
 5599:     return $output;
 5600: }
 5601: 
 5602: sub write_metadata {
 5603:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5604:     my (%metadatafields,%metadatakeys,$output);
 5605:     $metadatafields{'title'}=$formname;
 5606:     $metadatafields{'creationdate'}=time;
 5607:     $metadatafields{'lastrevisiondate'}=time;
 5608:     $metadatafields{'copyright'}='public';
 5609:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5610:                                          $env{'user.domain'};
 5611:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5612:     $metadatafields{'domain'}=$dom;
 5613:     {
 5614:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5615:         my $mfh;
 5616:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5617:             foreach (sort(keys(%metadatafields))) {
 5618:                 unless ($_=~/\./) {
 5619:                     my $unikey=$_;
 5620:                     $unikey=~/^([A-Za-z]+)/;
 5621:                     my $tag=$1;
 5622:                     $tag=~tr/A-Z/a-z/;
 5623:                     print $mfh "\n\<$tag";
 5624:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5625:                         my $value=$metadatafields{$unikey.'.'.$_};
 5626:                         $value=~s/\"/\'\'/g;
 5627:                         print $mfh ' '.$_.'="'.$value.'"';
 5628:                     }
 5629:                     print $mfh '>'.
 5630:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5631:                             .'</'.$tag.'>';
 5632:                 }
 5633:             }
 5634:             $output = 'ok';
 5635:             print $logfile "\nWrote metadata";
 5636:             close($mfh);
 5637:         } else {
 5638:             print $logfile "\nFailed to open metadata file";
 5639:             $output = &mt('Could not write metadata');
 5640:         }
 5641:     }
 5642:     return $output;
 5643: }
 5644: 
 5645: sub notifysubscribed {
 5646:     foreach my $targetsource (@{$modified_urls}){
 5647:         next unless (ref($targetsource) eq 'ARRAY');
 5648:         my ($target,$source)=@{$targetsource};
 5649:         if ($source ne '') {
 5650:             if (open(my $logfh,'>>'.$source.'.log')) {
 5651:                 print $logfh "\nCleanup phase: Notifications\n";
 5652:                 my @subscribed=&subscribed_hosts($target);
 5653:                 foreach my $subhost (@subscribed) {
 5654:                     print $logfh "\nNotifying host ".$subhost.':';
 5655:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5656:                     print $logfh $reply;
 5657:                 }
 5658:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5659:                 foreach my $subhost (@subscribedmeta) {
 5660:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5661:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5662:                                                         $subhost);
 5663:                     print $logfh $reply;
 5664:                 }
 5665:                 print $logfh "\n============ Done ============\n";
 5666:                 close($logfh);
 5667:             }
 5668:         }
 5669:     }
 5670:     return OK;
 5671: }
 5672: 
 5673: sub subscribed_hosts {
 5674:     my ($target) = @_;
 5675:     my @subscribed;
 5676:     if (open(my $fh,"<$target.subscription")) {
 5677:         while (my $subline=<$fh>) {
 5678:             if ($subline =~ /^($match_lonid):/) {
 5679:                 my $host = $1;
 5680:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5681:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5682:                         push(@subscribed,$host);
 5683:                     }
 5684:                 }
 5685:             }
 5686:         }
 5687:     }
 5688:     return @subscribed;
 5689: }
 5690: 
 5691: sub check_switchserver {
 5692:     my ($dom,$confname) = @_;
 5693:     my ($allowed,$switchserver);
 5694:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5695:     if ($home eq 'no_host') {
 5696:         $home = &Apache::lonnet::domain($dom,'primary');
 5697:     }
 5698:     my @ids=&Apache::lonnet::current_machine_ids();
 5699:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5700:     if (!$allowed) {
 5701: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5702:     }
 5703:     return $switchserver;
 5704: }
 5705: 
 5706: sub modify_quotas {
 5707:     my ($dom,$action,%domconfig) = @_;
 5708:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5709:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5710:     if ($action eq 'quotas') {
 5711:         $context = 'tools'; 
 5712:     } else {
 5713:         $context = $action;
 5714:     }
 5715:     if ($context eq 'requestcourses') {
 5716:         @usertools = ('official','unofficial','community');
 5717:         @options =('norequest','approval','validate','autolimit');
 5718:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5719:         %titles = &courserequest_titles();
 5720:         $toolregexp = join('|',@usertools);
 5721:         %conditions = &courserequest_conditions();
 5722:     } elsif ($context eq 'requestauthor') {
 5723:         @usertools = ('author');
 5724:         %titles = &authorrequest_titles();
 5725:     } else {
 5726:         @usertools = ('aboutme','blog','webdav','portfolio');
 5727:         %titles = &tool_titles();
 5728:     }
 5729:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5730:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5731:     foreach my $key (keys(%env)) {
 5732:         if ($context eq 'requestcourses') {
 5733:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5734:                 my $item = $1;
 5735:                 my $type = $2;
 5736:                 if ($type =~ /^limit_(.+)/) {
 5737:                     $limithash{$item}{$1} = $env{$key};
 5738:                 } else {
 5739:                     $confhash{$item}{$type} = $env{$key};
 5740:                 }
 5741:             }
 5742:         } elsif ($context eq 'requestauthor') {
 5743:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5744:                 $confhash{$1} = $env{$key};
 5745:             }
 5746:         } else {
 5747:             if ($key =~ /^form\.quota_(.+)$/) {
 5748:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5749:             }
 5750:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5751:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5752:             }
 5753:         }
 5754:     }
 5755:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5756:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5757:         @approvalnotify = sort(@approvalnotify);
 5758:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5759:         if (ref($domconfig{$action}) eq 'HASH') {
 5760:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5761:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5762:                     $changes{'notify'}{'approval'} = 1;
 5763:                 }
 5764:             } else {
 5765:                 if ($confhash{'notify'}{'approval'}) {
 5766:                     $changes{'notify'}{'approval'} = 1;
 5767:                 }
 5768:             }
 5769:         } else {
 5770:             if ($confhash{'notify'}{'approval'}) {
 5771:                 $changes{'notify'}{'approval'} = 1;
 5772:             }
 5773:         }
 5774:     } else {
 5775:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5776:     }
 5777:     foreach my $item (@usertools) {
 5778:         foreach my $type (@{$types},'default','_LC_adv') {
 5779:             my $unset; 
 5780:             if ($context eq 'requestcourses') {
 5781:                 $unset = '0';
 5782:                 if ($type eq '_LC_adv') {
 5783:                     $unset = '';
 5784:                 }
 5785:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5786:                     $confhash{$item}{$type} .= '=';
 5787:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5788:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5789:                     }
 5790:                 }
 5791:             } elsif ($context eq 'requestauthor') {
 5792:                 $unset = '0';
 5793:                 if ($type eq '_LC_adv') {
 5794:                     $unset = '';
 5795:                 }
 5796:             } else {
 5797:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5798:                     $confhash{$item}{$type} = 1;
 5799:                 } else {
 5800:                     $confhash{$item}{$type} = 0;
 5801:                 }
 5802:             }
 5803:             if (ref($domconfig{$action}) eq 'HASH') {
 5804:                 if ($action eq 'requestauthor') {
 5805:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5806:                         $changes{$type} = 1;
 5807:                     }
 5808:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5809:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5810:                         $changes{$item}{$type} = 1;
 5811:                     }
 5812:                 } else {
 5813:                     if ($context eq 'requestcourses') {
 5814:                         if ($confhash{$item}{$type} ne $unset) {
 5815:                             $changes{$item}{$type} = 1;
 5816:                         }
 5817:                     } else {
 5818:                         if (!$confhash{$item}{$type}) {
 5819:                             $changes{$item}{$type} = 1;
 5820:                         }
 5821:                     }
 5822:                 }
 5823:             } else {
 5824:                 if ($context eq 'requestcourses') {
 5825:                     if ($confhash{$item}{$type} ne $unset) {
 5826:                         $changes{$item}{$type} = 1;
 5827:                     }
 5828:                 } elsif ($context eq 'requestauthor') {
 5829:                     if ($confhash{$type} ne $unset) {
 5830:                         $changes{$type} = 1;
 5831:                     }
 5832:                 } else {
 5833:                     if (!$confhash{$item}{$type}) {
 5834:                         $changes{$item}{$type} = 1;
 5835:                     }
 5836:                 }
 5837:             }
 5838:         }
 5839:     }
 5840:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5841:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5842:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5843:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5844:                     if (exists($confhash{'defaultquota'}{$key})) {
 5845:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5846:                             $changes{'defaultquota'}{$key} = 1;
 5847:                         }
 5848:                     } else {
 5849:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5850:                     }
 5851:                 }
 5852:             } else {
 5853:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5854:                     if (exists($confhash{'defaultquota'}{$key})) {
 5855:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5856:                             $changes{'defaultquota'}{$key} = 1;
 5857:                         }
 5858:                     } else {
 5859:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5860:                     }
 5861:                 }
 5862:             }
 5863:         }
 5864:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5865:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5866:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5867:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5868:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5869:                             $changes{'defaultquota'}{$key} = 1;
 5870:                         }
 5871:                     } else {
 5872:                         if (!exists($domconfig{'quotas'}{$key})) {
 5873:                             $changes{'defaultquota'}{$key} = 1;
 5874:                         }
 5875:                     }
 5876:                 } else {
 5877:                     $changes{'defaultquota'}{$key} = 1;
 5878:                 }
 5879:             }
 5880:         }
 5881:     }
 5882: 
 5883:     if ($context eq 'requestauthor') {
 5884:         $domdefaults{'requestauthor'} = \%confhash;
 5885:     } else {
 5886:         foreach my $key (keys(%confhash)) {
 5887:             $domdefaults{$key} = $confhash{$key};
 5888:         }
 5889:     }
 5890: 
 5891:     my %quotahash = (
 5892:                       $action => { %confhash }
 5893:                     );
 5894:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5895:                                              $dom);
 5896:     if ($putresult eq 'ok') {
 5897:         if (keys(%changes) > 0) {
 5898:             my $cachetime = 24*60*60;
 5899:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5900: 
 5901:             $resulttext = &mt('Changes made:').'<ul>';
 5902:             unless (($context eq 'requestcourses') || 
 5903:                     ($context eq 'requestauthor')) {
 5904:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5905:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5906:                     foreach my $type (@{$types},'default') {
 5907:                         if (defined($changes{'defaultquota'}{$type})) {
 5908:                             my $typetitle = $usertypes->{$type};
 5909:                             if ($type eq 'default') {
 5910:                                 $typetitle = $othertitle;
 5911:                             }
 5912:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5913:                         }
 5914:                     }
 5915:                     $resulttext .= '</ul></li>';
 5916:                 }
 5917:             }
 5918:             my %newenv;
 5919:             foreach my $item (@usertools) {
 5920:                 my (%haschgs,%inconf);
 5921:                 if ($context eq 'requestauthor') {
 5922:                     %haschgs = %changes;
 5923:                     %inconf = %confhash; 
 5924:                 } else {
 5925:                     if (ref($changes{$item}) eq 'HASH') {
 5926:                         %haschgs = %{$changes{$item}};
 5927:                     }
 5928:                     if (ref($confhash{$item}) eq 'HASH') {
 5929:                         %inconf = %{$confhash{$item}};
 5930:                     }
 5931:                 }
 5932:                 if (keys(%haschgs) > 0) {
 5933:                     my $newacc = 
 5934:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5935:                                                           $env{'user.domain'},
 5936:                                                           $item,'reload',$context);
 5937:                     if (($context eq 'requestcourses') || 
 5938:                         ($context eq 'requestauthor')) {
 5939:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5940:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5941:                         }
 5942:                     } else {
 5943:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5944:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5945:                         }
 5946:                     }
 5947:                     unless ($context eq 'requestauthor') {
 5948:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5949:                     }
 5950:                     foreach my $type (@{$types},'default','_LC_adv') {
 5951:                         if ($haschgs{$type}) {
 5952:                             my $typetitle = $usertypes->{$type};
 5953:                             if ($type eq 'default') {
 5954:                                 $typetitle = $othertitle;
 5955:                             } elsif ($type eq '_LC_adv') {
 5956:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5957:                             }
 5958:                             if ($inconf{$type}) {
 5959:                                 if ($context eq 'requestcourses') {
 5960:                                     my $cond;
 5961:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 5962:                                         if ($1 eq '') {
 5963:                                             $cond = &mt('(Automatic processing of any request).');
 5964:                                         } else {
 5965:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5966:                                         }
 5967:                                     } else { 
 5968:                                         $cond = $conditions{$inconf{$type}};
 5969:                                     }
 5970:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5971:                                 } elsif ($context eq 'requestauthor') {
 5972:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 5973:                                                              $titles{$inconf{$type}},$typetitle);
 5974: 
 5975:                                 } else {
 5976:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5977:                                 }
 5978:                             } else {
 5979:                                 if ($type eq '_LC_adv') {
 5980:                                     if ($inconf{$type} eq '0') {
 5981:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5982:                                     } else { 
 5983:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5984:                                     }
 5985:                                 } else {
 5986:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5987:                                 }
 5988:                             }
 5989:                         }
 5990:                     }
 5991:                     unless ($context eq 'requestauthor') {
 5992:                         $resulttext .= '</ul></li>';
 5993:                     }
 5994:                 }
 5995:             }
 5996:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 5997:                 if (ref($changes{'notify'}) eq 'HASH') {
 5998:                     if ($changes{'notify'}{'approval'}) {
 5999:                         if (ref($confhash{'notify'}) eq 'HASH') {
 6000:                             if ($confhash{'notify'}{'approval'}) {
 6001:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 6002:                             } else {
 6003:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 6004:                             }
 6005:                         }
 6006:                     }
 6007:                 }
 6008:             }
 6009:             $resulttext .= '</ul>';
 6010:             if (keys(%newenv)) {
 6011:                 &Apache::lonnet::appenv(\%newenv);
 6012:             }
 6013:         } else {
 6014:             if ($context eq 'requestcourses') {
 6015:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 6016:             } elsif ($context eq 'requestauthor') {
 6017:                 $resulttext = &mt('No changes made to rights to request author space.');
 6018:             } else {
 6019:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 6020:             }
 6021:         }
 6022:     } else {
 6023:         $resulttext = '<span class="LC_error">'.
 6024: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6025:     }
 6026:     return $resulttext;
 6027: }
 6028: 
 6029: sub modify_autoenroll {
 6030:     my ($dom,%domconfig) = @_;
 6031:     my ($resulttext,%changes);
 6032:     my %currautoenroll;
 6033:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 6034:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 6035:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 6036:         }
 6037:     }
 6038:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6039:     my %title = ( run => 'Auto-enrollment active',
 6040:                   sender => 'Sender for notification messages',
 6041:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6042:     my @offon = ('off','on');
 6043:     my $sender_uname = $env{'form.sender_uname'};
 6044:     my $sender_domain = $env{'form.sender_domain'};
 6045:     if ($sender_domain eq '') {
 6046:         $sender_uname = '';
 6047:     } elsif ($sender_uname eq '') {
 6048:         $sender_domain = '';
 6049:     }
 6050:     my $coowners = $env{'form.autoassign_coowners'};
 6051:     my %autoenrollhash =  (
 6052:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6053:                                        'sender_uname' => $sender_uname,
 6054:                                        'sender_domain' => $sender_domain,
 6055:                                        'co-owners' => $coowners,
 6056:                                 }
 6057:                      );
 6058:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6059:                                              $dom);
 6060:     if ($putresult eq 'ok') {
 6061:         if (exists($currautoenroll{'run'})) {
 6062:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6063:                  $changes{'run'} = 1;
 6064:              }
 6065:         } elsif ($autorun) {
 6066:             if ($env{'form.autoenroll_run'} ne '1') {
 6067:                  $changes{'run'} = 1;
 6068:             }
 6069:         }
 6070:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6071:             $changes{'sender'} = 1;
 6072:         }
 6073:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6074:             $changes{'sender'} = 1;
 6075:         }
 6076:         if ($currautoenroll{'co-owners'} ne '') {
 6077:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6078:                 $changes{'coowners'} = 1;
 6079:             }
 6080:         } elsif ($coowners) {
 6081:             $changes{'coowners'} = 1;
 6082:         }      
 6083:         if (keys(%changes) > 0) {
 6084:             $resulttext = &mt('Changes made:').'<ul>';
 6085:             if ($changes{'run'}) {
 6086:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6087:             }
 6088:             if ($changes{'sender'}) {
 6089:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6090:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6091:                 } else {
 6092:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6093:                 }
 6094:             }
 6095:             if ($changes{'coowners'}) {
 6096:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6097:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6098:             }
 6099:             $resulttext .= '</ul>';
 6100:         } else {
 6101:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6102:         }
 6103:     } else {
 6104:         $resulttext = '<span class="LC_error">'.
 6105: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6106:     }
 6107:     return $resulttext;
 6108: }
 6109: 
 6110: sub modify_autoupdate {
 6111:     my ($dom,%domconfig) = @_;
 6112:     my ($resulttext,%currautoupdate,%fields,%changes);
 6113:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6114:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6115:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6116:         }
 6117:     }
 6118:     my @offon = ('off','on');
 6119:     my %title = &Apache::lonlocal::texthash (
 6120:                    run => 'Auto-update:',
 6121:                    classlists => 'Updates to user information in classlists?'
 6122:                 );
 6123:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6124:     my %fieldtitles = &Apache::lonlocal::texthash (
 6125:                         id => 'Student/Employee ID',
 6126:                         permanentemail => 'E-mail address',
 6127:                         lastname => 'Last Name',
 6128:                         firstname => 'First Name',
 6129:                         middlename => 'Middle Name',
 6130:                         generation => 'Generation',
 6131:                       );
 6132:     $othertitle = &mt('All users');
 6133:     if (keys(%{$usertypes}) >  0) {
 6134:         $othertitle = &mt('Other users');
 6135:     }
 6136:     foreach my $key (keys(%env)) {
 6137:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6138:             my ($usertype,$item) = ($1,$2);
 6139:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6140:                 if ($usertype eq 'default') {   
 6141:                     push(@{$fields{$1}},$2);
 6142:                 } elsif (ref($types) eq 'ARRAY') {
 6143:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6144:                         push(@{$fields{$1}},$2);
 6145:                     }
 6146:                 }
 6147:             }
 6148:         }
 6149:     }
 6150:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6151:     @lockablenames = sort(@lockablenames);
 6152:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6153:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6154:         if (@changed) {
 6155:             $changes{'lockablenames'} = 1;
 6156:         }
 6157:     } else {
 6158:         if (@lockablenames) {
 6159:             $changes{'lockablenames'} = 1;
 6160:         }
 6161:     }
 6162:     my %updatehash = (
 6163:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6164:                                       classlists => $env{'form.classlists'},
 6165:                                       fields => {%fields},
 6166:                                       lockablenames => \@lockablenames,
 6167:                                     }
 6168:                      );
 6169:     foreach my $key (keys(%currautoupdate)) {
 6170:         if (($key eq 'run') || ($key eq 'classlists')) {
 6171:             if (exists($updatehash{autoupdate}{$key})) {
 6172:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6173:                     $changes{$key} = 1;
 6174:                 }
 6175:             }
 6176:         } elsif ($key eq 'fields') {
 6177:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6178:                 foreach my $item (@{$types},'default') {
 6179:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6180:                         my $change = 0;
 6181:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6182:                             if (!exists($fields{$item})) {
 6183:                                 $change = 1;
 6184:                                 last;
 6185:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6186:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6187:                                     $change = 1;
 6188:                                     last;
 6189:                                 }
 6190:                             }
 6191:                         }
 6192:                         if ($change) {
 6193:                             push(@{$changes{$key}},$item);
 6194:                         }
 6195:                     } 
 6196:                 }
 6197:             }
 6198:         } elsif ($key eq 'lockablenames') {
 6199:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6200:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6201:                 if (@changed) {
 6202:                     $changes{'lockablenames'} = 1;
 6203:                 }
 6204:             } else {
 6205:                 if (@lockablenames) {
 6206:                     $changes{'lockablenames'} = 1;
 6207:                 }
 6208:             }
 6209:         }
 6210:     }
 6211:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6212:         if (@lockablenames) {
 6213:             $changes{'lockablenames'} = 1;
 6214:         }
 6215:     }
 6216:     foreach my $item (@{$types},'default') {
 6217:         if (defined($fields{$item})) {
 6218:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6219:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6220:                     my $change = 0;
 6221:                     if (ref($fields{$item}) eq 'ARRAY') {
 6222:                         foreach my $type (@{$fields{$item}}) {
 6223:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6224:                                 $change = 1;
 6225:                                 last;
 6226:                             }
 6227:                         }
 6228:                     }
 6229:                     if ($change) {
 6230:                         push(@{$changes{'fields'}},$item);
 6231:                     }
 6232:                 } else {
 6233:                     push(@{$changes{'fields'}},$item);
 6234:                 }
 6235:             } else {
 6236:                 push(@{$changes{'fields'}},$item);
 6237:             }
 6238:         }
 6239:     }
 6240:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6241:                                              $dom);
 6242:     if ($putresult eq 'ok') {
 6243:         if (keys(%changes) > 0) {
 6244:             $resulttext = &mt('Changes made:').'<ul>';
 6245:             foreach my $key (sort(keys(%changes))) {
 6246:                 if ($key eq 'lockablenames') {
 6247:                     $resulttext .= '<li>';
 6248:                     if (@lockablenames) {
 6249:                         $usertypes->{'default'} = $othertitle;
 6250:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6251:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6252:                     } else {
 6253:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6254:                     }
 6255:                     $resulttext .= '</li>';
 6256:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6257:                     foreach my $item (@{$changes{$key}}) {
 6258:                         my @newvalues;
 6259:                         foreach my $type (@{$fields{$item}}) {
 6260:                             push(@newvalues,$fieldtitles{$type});
 6261:                         }
 6262:                         my $newvaluestr;
 6263:                         if (@newvalues > 0) {
 6264:                             $newvaluestr = join(', ',@newvalues);
 6265:                         } else {
 6266:                             $newvaluestr = &mt('none');
 6267:                         }
 6268:                         if ($item eq 'default') {
 6269:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6270:                         } else {
 6271:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6272:                         }
 6273:                     }
 6274:                 } else {
 6275:                     my $newvalue;
 6276:                     if ($key eq 'run') {
 6277:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6278:                     } else {
 6279:                         $newvalue = $offon[$env{'form.'.$key}];
 6280:                     }
 6281:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6282:                 }
 6283:             }
 6284:             $resulttext .= '</ul>';
 6285:         } else {
 6286:             $resulttext = &mt('No changes made to autoupdates');
 6287:         }
 6288:     } else {
 6289:         $resulttext = '<span class="LC_error">'.
 6290: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6291:     }
 6292:     return $resulttext;
 6293: }
 6294: 
 6295: sub modify_autocreate {
 6296:     my ($dom,%domconfig) = @_;
 6297:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6298:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6299:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6300:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6301:         }
 6302:     }
 6303:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6304:                  req => 'Auto-creation of validated requests for official courses',
 6305:                  xmldc => 'Identity of course creator of courses from XML files',
 6306:                );
 6307:     my @types = ('xml','req');
 6308:     foreach my $item (@types) {
 6309:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6310:         $newvals{$item} =~ s/\D//g;
 6311:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6312:     }
 6313:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6314:     my %domcoords = &get_active_dcs($dom);
 6315:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6316:         $newvals{'xmldc'} = '';
 6317:     } 
 6318:     %autocreatehash =  (
 6319:                         autocreate => { xml => $newvals{'xml'},
 6320:                                         req => $newvals{'req'},
 6321:                                       }
 6322:                        );
 6323:     if ($newvals{'xmldc'} ne '') {
 6324:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6325:     }
 6326:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6327:                                              $dom);
 6328:     if ($putresult eq 'ok') {
 6329:         my @items = @types;
 6330:         if ($newvals{'xml'}) {
 6331:             push(@items,'xmldc');
 6332:         }
 6333:         foreach my $item (@items) {
 6334:             if (exists($currautocreate{$item})) {
 6335:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6336:                     $changes{$item} = 1;
 6337:                 }
 6338:             } elsif ($newvals{$item}) {
 6339:                 $changes{$item} = 1;
 6340:             }
 6341:         }
 6342:         if (keys(%changes) > 0) {
 6343:             my @offon = ('off','on'); 
 6344:             $resulttext = &mt('Changes made:').'<ul>';
 6345:             foreach my $item (@types) {
 6346:                 if ($changes{$item}) {
 6347:                     my $newtxt = $offon[$newvals{$item}];
 6348:                     $resulttext .= '<li>'.
 6349:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 6350:                                        '<b>','</b>').
 6351:                                    '</li>';
 6352:                 }
 6353:             }
 6354:             if ($changes{'xmldc'}) {
 6355:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6356:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6357:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 6358:             }
 6359:             $resulttext .= '</ul>';
 6360:         } else {
 6361:             $resulttext = &mt('No changes made to auto-creation settings');
 6362:         }
 6363:     } else {
 6364:         $resulttext = '<span class="LC_error">'.
 6365:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6366:     }
 6367:     return $resulttext;
 6368: }
 6369: 
 6370: sub modify_directorysrch {
 6371:     my ($dom,%domconfig) = @_;
 6372:     my ($resulttext,%changes);
 6373:     my %currdirsrch;
 6374:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6375:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6376:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6377:         }
 6378:     }
 6379:     my %title = ( available => 'Directory search available',
 6380:                   localonly => 'Other domains can search',
 6381:                   searchby => 'Search types',
 6382:                   searchtypes => 'Search latitude');
 6383:     my @offon = ('off','on');
 6384:     my @otherdoms = ('Yes','No');
 6385: 
 6386:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6387:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6388:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6389: 
 6390:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6391:     if (keys(%{$usertypes}) == 0) {
 6392:         @cansearch = ('default');
 6393:     } else {
 6394:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6395:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6396:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6397:                     push(@{$changes{'cansearch'}},$type);
 6398:                 }
 6399:             }
 6400:             foreach my $type (@cansearch) {
 6401:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6402:                     push(@{$changes{'cansearch'}},$type);
 6403:                 }
 6404:             }
 6405:         } else {
 6406:             push(@{$changes{'cansearch'}},@cansearch);
 6407:         }
 6408:     }
 6409: 
 6410:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6411:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6412:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6413:                 push(@{$changes{'searchby'}},$by);
 6414:             }
 6415:         }
 6416:         foreach my $by (@searchby) {
 6417:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6418:                 push(@{$changes{'searchby'}},$by);
 6419:             }
 6420:         }
 6421:     } else {
 6422:         push(@{$changes{'searchby'}},@searchby);
 6423:     }
 6424: 
 6425:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6426:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6427:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6428:                 push(@{$changes{'searchtypes'}},$type);
 6429:             }
 6430:         }
 6431:         foreach my $type (@searchtypes) {
 6432:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6433:                 push(@{$changes{'searchtypes'}},$type);
 6434:             }
 6435:         }
 6436:     } else {
 6437:         if (exists($currdirsrch{'searchtypes'})) {
 6438:             foreach my $type (@searchtypes) {  
 6439:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6440:                     push(@{$changes{'searchtypes'}},$type);
 6441:                 }
 6442:             }
 6443:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6444:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6445:             }   
 6446:         } else {
 6447:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6448:         }
 6449:     }
 6450: 
 6451:     my %dirsrch_hash =  (
 6452:             directorysrch => { available => $env{'form.dirsrch_available'},
 6453:                                cansearch => \@cansearch,
 6454:                                localonly => $env{'form.dirsrch_localonly'},
 6455:                                searchby => \@searchby,
 6456:                                searchtypes => \@searchtypes,
 6457:                              }
 6458:             );
 6459:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6460:                                              $dom);
 6461:     if ($putresult eq 'ok') {
 6462:         if (exists($currdirsrch{'available'})) {
 6463:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6464:                  $changes{'available'} = 1;
 6465:              }
 6466:         } else {
 6467:             if ($env{'form.dirsrch_available'} eq '1') {
 6468:                 $changes{'available'} = 1;
 6469:             }
 6470:         }
 6471:         if (exists($currdirsrch{'localonly'})) {
 6472:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6473:                  $changes{'localonly'} = 1;
 6474:              }
 6475:         } else {
 6476:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6477:                 $changes{'localonly'} = 1;
 6478:             }
 6479:         }
 6480:         if (keys(%changes) > 0) {
 6481:             $resulttext = &mt('Changes made:').'<ul>';
 6482:             if ($changes{'available'}) {
 6483:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6484:             }
 6485:             if ($changes{'localonly'}) {
 6486:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6487:             }
 6488: 
 6489:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6490:                 my $chgtext;
 6491:                 if (ref($usertypes) eq 'HASH') {
 6492:                     if (keys(%{$usertypes}) > 0) {
 6493:                         foreach my $type (@{$types}) {
 6494:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6495:                                 $chgtext .= $usertypes->{$type}.'; ';
 6496:                             }
 6497:                         }
 6498:                         if (grep(/^default$/,@cansearch)) {
 6499:                             $chgtext .= $othertitle;
 6500:                         } else {
 6501:                             $chgtext =~ s/\; $//;
 6502:                         }
 6503:                         $resulttext .= 
 6504:                             '<li>'.
 6505:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 6506:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 6507:                             '</li>';
 6508:                     }
 6509:                 }
 6510:             }
 6511:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6512:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6513:                 my $chgtext;
 6514:                 foreach my $type (@{$titleorder}) {
 6515:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6516:                         if (defined($searchtitles->{$type})) {
 6517:                             $chgtext .= $searchtitles->{$type}.'; ';
 6518:                         }
 6519:                     }
 6520:                 }
 6521:                 $chgtext =~ s/\; $//;
 6522:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6523:             }
 6524:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6525:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6526:                 my $chgtext;
 6527:                 foreach my $type (@{$srchtypeorder}) {
 6528:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6529:                         if (defined($srchtypes_desc->{$type})) {
 6530:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6531:                         }
 6532:                     }
 6533:                 }
 6534:                 $chgtext =~ s/\; $//;
 6535:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 6536:             }
 6537:             $resulttext .= '</ul>';
 6538:         } else {
 6539:             $resulttext = &mt('No changes made to institution directory search settings');
 6540:         }
 6541:     } else {
 6542:         $resulttext = '<span class="LC_error">'.
 6543:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6544:     }
 6545:     return $resulttext;
 6546: }
 6547: 
 6548: sub modify_contacts {
 6549:     my ($dom,%domconfig) = @_;
 6550:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6551:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6552:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6553:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6554:         }
 6555:     }
 6556:     my (%others,%to,%bcc);
 6557:     my @contacts = ('supportemail','adminemail');
 6558:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6559:                     'requestsmail');
 6560:     foreach my $type (@mailings) {
 6561:         @{$newsetting{$type}} = 
 6562:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6563:         foreach my $item (@contacts) {
 6564:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6565:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6566:             } else {
 6567:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6568:             }
 6569:         }  
 6570:         $others{$type} = $env{'form.'.$type.'_others'};
 6571:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6572:         if ($type eq 'helpdeskmail') {
 6573:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6574:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6575:         }
 6576:     }
 6577:     foreach my $item (@contacts) {
 6578:         $to{$item} = $env{'form.'.$item};
 6579:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6580:     }
 6581:     if (keys(%currsetting) > 0) {
 6582:         foreach my $item (@contacts) {
 6583:             if ($to{$item} ne $currsetting{$item}) {
 6584:                 $changes{$item} = 1;
 6585:             }
 6586:         }
 6587:         foreach my $type (@mailings) {
 6588:             foreach my $item (@contacts) {
 6589:                 if (ref($currsetting{$type}) eq 'HASH') {
 6590:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6591:                         push(@{$changes{$type}},$item);
 6592:                     }
 6593:                 } else {
 6594:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6595:                 }
 6596:             }
 6597:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6598:                 push(@{$changes{$type}},'others');
 6599:             }
 6600:             if ($type eq 'helpdeskmail') {   
 6601:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6602:                     push(@{$changes{$type}},'bcc'); 
 6603:                 }
 6604:             }
 6605:         }
 6606:     } else {
 6607:         my %default;
 6608:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6609:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6610:         $default{'errormail'} = 'adminemail';
 6611:         $default{'packagesmail'} = 'adminemail';
 6612:         $default{'helpdeskmail'} = 'supportemail';
 6613:         $default{'lonstatusmail'} = 'adminemail';
 6614:         $default{'requestsmail'} = 'adminemail';
 6615:         foreach my $item (@contacts) {
 6616:            if ($to{$item} ne $default{$item}) {
 6617:               $changes{$item} = 1;
 6618:            } 
 6619:         }
 6620:         foreach my $type (@mailings) {
 6621:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6622:                
 6623:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6624:             }
 6625:             if ($others{$type} ne '') {
 6626:                 push(@{$changes{$type}},'others');
 6627:             }
 6628:             if ($type eq 'helpdeskmail') {
 6629:                 if ($bcc{$type} ne '') {
 6630:                     push(@{$changes{$type}},'bcc');
 6631:                 }
 6632:             }
 6633:         }
 6634:     }
 6635:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6636:                                              $dom);
 6637:     if ($putresult eq 'ok') {
 6638:         if (keys(%changes) > 0) {
 6639:             my ($titles,$short_titles)  = &contact_titles();
 6640:             $resulttext = &mt('Changes made:').'<ul>';
 6641:             foreach my $item (@contacts) {
 6642:                 if ($changes{$item}) {
 6643:                     $resulttext .= '<li>'.$titles->{$item}.
 6644:                                     &mt(' set to: ').
 6645:                                     '<span class="LC_cusr_emph">'.
 6646:                                     $to{$item}.'</span></li>';
 6647:                 }
 6648:             }
 6649:             foreach my $type (@mailings) {
 6650:                 if (ref($changes{$type}) eq 'ARRAY') {
 6651:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6652:                     my @text;
 6653:                     foreach my $item (@{$newsetting{$type}}) {
 6654:                         push(@text,$short_titles->{$item});
 6655:                     }
 6656:                     if ($others{$type} ne '') {
 6657:                         push(@text,$others{$type});
 6658:                     }
 6659:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6660:                                    join(', ',@text).'</span>';
 6661:                     if ($type eq 'helpdeskmail') {
 6662:                         if ($bcc{$type} ne '') {
 6663:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6664:                         }
 6665:                     }
 6666:                     $resulttext .= '</li>';
 6667:                 }
 6668:             }
 6669:             $resulttext .= '</ul>';
 6670:         } else {
 6671:             $resulttext = &mt('No changes made to contact information');
 6672:         }
 6673:     } else {
 6674:         $resulttext = '<span class="LC_error">'.
 6675:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6676:     }
 6677:     return $resulttext;
 6678: }
 6679: 
 6680: sub modify_usercreation {
 6681:     my ($dom,%domconfig) = @_;
 6682:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6683:     my $warningmsg;
 6684:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6685:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6686:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6687:         }
 6688:     }
 6689:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6690:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6691:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6692:     my @contexts = ('author','course','requestcrs','selfcreate');
 6693:     foreach my $item(@contexts) {
 6694:         if ($item eq 'selfcreate') {
 6695:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6696:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6697:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6698:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6699:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6700:                         $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.');   
 6701:                     }
 6702:                 }
 6703:             }
 6704:         } else {
 6705:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6706:         }
 6707:     }
 6708:     my ($othertitle,$usertypes,$types) = 
 6709:         &Apache::loncommon::sorted_inst_types($dom);
 6710:     if (ref($types) eq 'ARRAY') {
 6711:         if (@{$types} > 0) {
 6712:             @{$cancreate{'statustocreate'}} = 
 6713:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6714:         } else {
 6715:             @{$cancreate{'statustocreate'}} = ();
 6716:         }
 6717:         push(@contexts,'statustocreate');
 6718:     }
 6719:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6720:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6721:         foreach my $item (@contexts) {
 6722:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6723:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6724:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6725:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6726:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6727:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6728:                                     push(@{$changes{'cancreate'}},$item);
 6729:                                 }
 6730:                             }
 6731:                         }
 6732:                     }
 6733:                 } else {
 6734:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6735:                         if (@{$cancreate{$item}} > 0) {
 6736:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6737:                                 push(@{$changes{'cancreate'}},$item);
 6738:                             }
 6739:                         }
 6740:                     } else {
 6741:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6742:                             if (@{$cancreate{$item}} < 3) {
 6743:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6744:                                     push(@{$changes{'cancreate'}},$item);
 6745:                                 }
 6746:                             }
 6747:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6748:                             if (@{$cancreate{$item}} > 0) {
 6749:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6750:                                     push(@{$changes{'cancreate'}},$item);
 6751:                                 }
 6752:                             }
 6753:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6754:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6755:                                 push(@{$changes{'cancreate'}},$item);
 6756:                             }
 6757:                         }
 6758:                     }
 6759:                 }
 6760:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6761:                     foreach my $type (@{$cancreate{$item}}) {
 6762:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6763:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6764:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6765:                                     push(@{$changes{'cancreate'}},$item);
 6766:                                 }
 6767:                             }
 6768:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6769:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6770:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6771:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6772:                                     push(@{$changes{'cancreate'}},$item);
 6773:                                 }
 6774:                             }
 6775:                         }
 6776:                     }
 6777:                 }
 6778:             } else {
 6779:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6780:                     push(@{$changes{'cancreate'}},$item);
 6781:                 }
 6782:             }
 6783:         }
 6784:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6785:         foreach my $item (@contexts) {
 6786:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6787:                 if ($cancreate{$item} ne 'any') {
 6788:                     push(@{$changes{'cancreate'}},$item);
 6789:                 }
 6790:             } else {
 6791:                 if ($cancreate{$item} ne 'none') {
 6792:                     push(@{$changes{'cancreate'}},$item);
 6793:                 }
 6794:             }
 6795:         }
 6796:     } else {
 6797:         foreach my $item (@contexts)  {
 6798:             push(@{$changes{'cancreate'}},$item);
 6799:         }
 6800:     }
 6801: 
 6802:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6803:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6804:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6805:                 push(@{$changes{'username_rule'}},$type);
 6806:             }
 6807:         }
 6808:         foreach my $type (@username_rule) {
 6809:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6810:                 push(@{$changes{'username_rule'}},$type);
 6811:             }
 6812:         }
 6813:     } else {
 6814:         push(@{$changes{'username_rule'}},@username_rule);
 6815:     }
 6816: 
 6817:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6818:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 6819:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 6820:                 push(@{$changes{'id_rule'}},$type);
 6821:             }
 6822:         }
 6823:         foreach my $type (@id_rule) {
 6824:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 6825:                 push(@{$changes{'id_rule'}},$type);
 6826:             }
 6827:         }
 6828:     } else {
 6829:         push(@{$changes{'id_rule'}},@id_rule);
 6830:     }
 6831: 
 6832:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 6833:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 6834:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 6835:                 push(@{$changes{'email_rule'}},$type);
 6836:             }
 6837:         }
 6838:         foreach my $type (@email_rule) {
 6839:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 6840:                 push(@{$changes{'email_rule'}},$type);
 6841:             }
 6842:         }
 6843:     } else {
 6844:         push(@{$changes{'email_rule'}},@email_rule);
 6845:     }
 6846: 
 6847:     my @authen_contexts = ('author','course','domain');
 6848:     my @authtypes = ('int','krb4','krb5','loc');
 6849:     my %authhash;
 6850:     foreach my $item (@authen_contexts) {
 6851:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 6852:         foreach my $auth (@authtypes) {
 6853:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 6854:                 $authhash{$item}{$auth} = 1;
 6855:             } else {
 6856:                 $authhash{$item}{$auth} = 0;
 6857:             }
 6858:         }
 6859:     }
 6860:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 6861:         foreach my $item (@authen_contexts) {
 6862:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 6863:                 foreach my $auth (@authtypes) {
 6864:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 6865:                         push(@{$changes{'authtypes'}},$item);
 6866:                         last;
 6867:                     }
 6868:                 }
 6869:             }
 6870:         }
 6871:     } else {
 6872:         foreach my $item (@authen_contexts) {
 6873:             push(@{$changes{'authtypes'}},$item);
 6874:         }
 6875:     }
 6876: 
 6877:     my %usercreation_hash =  (
 6878:             usercreation => {
 6879:                               cancreate     => \%cancreate,
 6880:                               username_rule => \@username_rule,
 6881:                               id_rule       => \@id_rule,
 6882:                               email_rule    => \@email_rule,
 6883:                               authtypes     => \%authhash,
 6884:                             }
 6885:             );
 6886: 
 6887:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 6888:                                              $dom);
 6889: 
 6890:     my %selfcreatetypes = (
 6891:                              sso   => 'users authenticated by institutional single sign on',
 6892:                              login => 'users authenticated by institutional log-in',
 6893:                              email => 'users who provide a valid e-mail address for use as the username',
 6894:                           );
 6895:     if ($putresult eq 'ok') {
 6896:         if (keys(%changes) > 0) {
 6897:             $resulttext = &mt('Changes made:').'<ul>';
 6898:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 6899:                 my %lt = &usercreation_types();
 6900:                 foreach my $type (@{$changes{'cancreate'}}) {
 6901:                     my $chgtext;
 6902:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 6903:                         $chgtext = $lt{$type}.', ';
 6904:                     }
 6905:                     if ($type eq 'selfcreate') {
 6906:                         if (@{$cancreate{$type}} == 0) {
 6907:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 6908:                         } else {
 6909:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 6910:                             foreach my $case (@{$cancreate{$type}}) {
 6911:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 6912:                             }
 6913:                             $chgtext .= '</ul>';
 6914:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 6915:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 6916:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 6917:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 6918:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6919:                                         }
 6920:                                     }
 6921:                                 }
 6922:                             }
 6923:                         }
 6924:                     } elsif ($type eq 'statustocreate') {
 6925:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 6926:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 6927:                             if (@{$cancreate{'selfcreate'}} > 0) {
 6928:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 6929: 
 6930:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 6931:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 6932:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6933:                                     } 
 6934:                                 } elsif (ref($usertypes) eq 'HASH') {
 6935:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6936:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 6937:                                     } else {
 6938:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 6939:                                     }
 6940:                                     $chgtext .= '<ul>';
 6941:                                     foreach my $case (@{$cancreate{$type}}) {
 6942:                                         if ($case eq 'default') {
 6943:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 6944:                                         } else {
 6945:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 6946:                                         }
 6947:                                     }
 6948:                                     $chgtext .= '</ul>';
 6949:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6950:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 6951:                                     }
 6952:                                 }
 6953:                             } else {
 6954:                                 if (@{$cancreate{$type}} == 0) {
 6955:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 6956:                                 } else {
 6957:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 6958:                                 }
 6959:                             }
 6960:                         }
 6961:                     } elsif ($type eq 'captcha') {
 6962:                         if ($cancreate{$type} eq 'notused') {
 6963:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 6964:                         } else {
 6965:                             my %captchas = &captcha_phrases();
 6966:                             if ($captchas{$cancreate{$type}}) {
 6967:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 6968:                             } else {
 6969:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.'); 
 6970:                             }
 6971:                         }
 6972:                     } elsif ($type eq 'recaptchakeys') {
 6973:                         my ($privkey,$pubkey);
 6974:                         if (ref($cancreate{$type}) eq 'HASH') {
 6975:                             $pubkey = $cancreate{$type}{'public'};
 6976:                             $privkey = $cancreate{$type}{'private'};
 6977:                         }
 6978:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 6979:                         if (!$pubkey) {
 6980:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 6981:                         } else {
 6982:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6983:                         }
 6984:                         if (!$privkey) {
 6985:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 6986:                         } else {
 6987:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6988:                         }
 6989:                         $chgtext .= '</ul>';
 6990:                     } else {
 6991:                         if ($cancreate{$type} eq 'none') {
 6992:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 6993:                         } elsif ($cancreate{$type} eq 'any') {
 6994:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 6995:                         } elsif ($cancreate{$type} eq 'official') {
 6996:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 6997:                         } elsif ($cancreate{$type} eq 'unofficial') {
 6998:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 6999:                         }
 7000:                     }
 7001:                     $resulttext .= '<li>'.$chgtext.'</li>';
 7002:                 }
 7003:             }
 7004:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 7005:                 my ($rules,$ruleorder) = 
 7006:                     &Apache::lonnet::inst_userrules($dom,'username');
 7007:                 my $chgtext = '<ul>';
 7008:                 foreach my $type (@username_rule) {
 7009:                     if (ref($rules->{$type}) eq 'HASH') {
 7010:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 7011:                     }
 7012:                 }
 7013:                 $chgtext .= '</ul>';
 7014:                 if (@username_rule > 0) {
 7015:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 7016:                 } else {
 7017:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 7018:                 }
 7019:             }
 7020:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 7021:                 my ($idrules,$idruleorder) = 
 7022:                     &Apache::lonnet::inst_userrules($dom,'id');
 7023:                 my $chgtext = '<ul>';
 7024:                 foreach my $type (@id_rule) {
 7025:                     if (ref($idrules->{$type}) eq 'HASH') {
 7026:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 7027:                     }
 7028:                 }
 7029:                 $chgtext .= '</ul>';
 7030:                 if (@id_rule > 0) {
 7031:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 7032:                 } else {
 7033:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 7034:                 }
 7035:             }
 7036:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 7037:                 my ($emailrules,$emailruleorder) =
 7038:                     &Apache::lonnet::inst_userrules($dom,'email');
 7039:                 my $chgtext = '<ul>';
 7040:                 foreach my $type (@email_rule) {
 7041:                     if (ref($emailrules->{$type}) eq 'HASH') {
 7042:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 7043:                     }
 7044:                 }
 7045:                 $chgtext .= '</ul>';
 7046:                 if (@email_rule > 0) {
 7047:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 7048:                 } else {
 7049:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 7050:                 }
 7051:             }
 7052: 
 7053:             my %authname = &authtype_names();
 7054:             my %context_title = &context_names();
 7055:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7056:                 my $chgtext = '<ul>';
 7057:                 foreach my $type (@{$changes{'authtypes'}}) {
 7058:                     my @allowed;
 7059:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7060:                     foreach my $auth (@authtypes) {
 7061:                         if ($authhash{$type}{$auth}) {
 7062:                             push(@allowed,$authname{$auth});
 7063:                         }
 7064:                     }
 7065:                     if (@allowed > 0) {
 7066:                         $chgtext .= join(', ',@allowed).'</li>';
 7067:                     } else {
 7068:                         $chgtext .= &mt('none').'</li>';
 7069:                     }
 7070:                 }
 7071:                 $chgtext .= '</ul>';
 7072:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7073:                 $resulttext .= '</li>';
 7074:             }
 7075:             $resulttext .= '</ul>';
 7076:         } else {
 7077:             $resulttext = &mt('No changes made to user creation settings');
 7078:         }
 7079:     } else {
 7080:         $resulttext = '<span class="LC_error">'.
 7081:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7082:     }
 7083:     if ($warningmsg ne '') {
 7084:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7085:     }
 7086:     return $resulttext;
 7087: }
 7088: 
 7089: sub process_captcha {
 7090:     my ($container,$changes,$newsettings,$current) = @_;
 7091:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 7092:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 7093:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 7094:         $newsettings->{'captcha'} = 'original';
 7095:     }
 7096:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 7097:         if ($container eq 'cancreate') { 
 7098:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7099:                 push(@{$changes->{'cancreate'}},'captcha');
 7100:             } elsif (!defined($changes->{'cancreate'})) {
 7101:                 $changes->{'cancreate'} = ['captcha'];
 7102:             }
 7103:         } else {
 7104:             $changes->{'captcha'} = 1;
 7105:         }
 7106:     }
 7107:     my ($newpub,$newpriv,$currpub,$currpriv);
 7108:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 7109:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 7110:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 7111:         $newpub =~ s/\W//g;
 7112:         $newpriv =~ s/\W//g;
 7113:         $newsettings->{'recaptchakeys'} = {
 7114:                                              public  => $newpub,
 7115:                                              private => $newpriv,
 7116:                                           };
 7117:     }
 7118:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 7119:         $currpub = $current->{'recaptchakeys'}{'public'};
 7120:         $currpriv = $current->{'recaptchakeys'}{'private'};
 7121:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 7122:             $newsettings->{'recaptchakeys'} = {
 7123:                                                  public  => '',
 7124:                                                  private => '',
 7125:                                               }
 7126:         }
 7127:     }
 7128:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 7129:         if ($container eq 'cancreate') {
 7130:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7131:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 7132:             } elsif (!defined($changes->{'cancreate'})) {
 7133:                 $changes->{'cancreate'} = ['recaptchakeys'];
 7134:             }
 7135:         } else {
 7136:             $changes->{'recaptchakeys'} = 1; 
 7137:         }
 7138:     }
 7139:     return;
 7140: }
 7141: 
 7142: sub modify_usermodification {
 7143:     my ($dom,%domconfig) = @_;
 7144:     my ($resulttext,%curr_usermodification,%changes);
 7145:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7146:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7147:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 7148:         }
 7149:     }
 7150:     my @contexts = ('author','course','selfcreate');
 7151:     my %context_title = (
 7152:                            author => 'In author context',
 7153:                            course => 'In course context',
 7154:                            selfcreate => 'When self creating account', 
 7155:                         );
 7156:     my @fields = ('lastname','firstname','middlename','generation',
 7157:                   'permanentemail','id');
 7158:     my %roles = (
 7159:                   author => ['ca','aa'],
 7160:                   course => ['st','ep','ta','in','cr'],
 7161:                 );
 7162:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7163:     if (ref($types) eq 'ARRAY') {
 7164:         push(@{$types},'default');
 7165:         $usertypes->{'default'} = $othertitle;
 7166:     }
 7167:     $roles{'selfcreate'} = $types;  
 7168:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7169:     my %modifyhash;
 7170:     foreach my $context (@contexts) {
 7171:         foreach my $role (@{$roles{$context}}) {
 7172:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7173:             foreach my $item (@fields) {
 7174:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7175:                     $modifyhash{$context}{$role}{$item} = 1;
 7176:                 } else {
 7177:                     $modifyhash{$context}{$role}{$item} = 0;
 7178:                 }
 7179:             }
 7180:         }
 7181:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7182:             foreach my $role (@{$roles{$context}}) {
 7183:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7184:                     foreach my $field (@fields) {
 7185:                         if ($modifyhash{$context}{$role}{$field} ne 
 7186:                                 $curr_usermodification{$context}{$role}{$field}) {
 7187:                             push(@{$changes{$context}},$role);
 7188:                             last;
 7189:                         }
 7190:                     }
 7191:                 }
 7192:             }
 7193:         } else {
 7194:             foreach my $context (@contexts) {
 7195:                 foreach my $role (@{$roles{$context}}) {
 7196:                     push(@{$changes{$context}},$role);
 7197:                 }
 7198:             }
 7199:         }
 7200:     }
 7201:     my %usermodification_hash =  (
 7202:                                    usermodification => \%modifyhash,
 7203:                                  );
 7204:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7205:                                              \%usermodification_hash,$dom);
 7206:     if ($putresult eq 'ok') {
 7207:         if (keys(%changes) > 0) {
 7208:             $resulttext = &mt('Changes made: ').'<ul>';
 7209:             foreach my $context (@contexts) {
 7210:                 if (ref($changes{$context}) eq 'ARRAY') {
 7211:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7212:                     if (ref($changes{$context}) eq 'ARRAY') {
 7213:                         foreach my $role (@{$changes{$context}}) {
 7214:                             my $rolename;
 7215:                             if ($context eq 'selfcreate') {
 7216:                                 $rolename = $role;
 7217:                                 if (ref($usertypes) eq 'HASH') {
 7218:                                     if ($usertypes->{$role} ne '') {
 7219:                                         $rolename = $usertypes->{$role};
 7220:                                     }
 7221:                                 }
 7222:                             } else {
 7223:                                 if ($role eq 'cr') {
 7224:                                     $rolename = &mt('Custom');
 7225:                                 } else {
 7226:                                     $rolename = &Apache::lonnet::plaintext($role);
 7227:                                 }
 7228:                             }
 7229:                             my @modifiable;
 7230:                             if ($context eq 'selfcreate') {
 7231:                                 $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): ');
 7232:                             } else {
 7233:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7234:                             }
 7235:                             foreach my $field (@fields) {
 7236:                                 if ($modifyhash{$context}{$role}{$field}) {
 7237:                                     push(@modifiable,$fieldtitles{$field});
 7238:                                 }
 7239:                             }
 7240:                             if (@modifiable > 0) {
 7241:                                 $resulttext .= join(', ',@modifiable);
 7242:                             } else {
 7243:                                 $resulttext .= &mt('none'); 
 7244:                             }
 7245:                             $resulttext .= '</li>';
 7246:                         }
 7247:                         $resulttext .= '</ul></li>';
 7248:                     }
 7249:                 }
 7250:             }
 7251:             $resulttext .= '</ul>';
 7252:         } else {
 7253:             $resulttext = &mt('No changes made to user modification settings');
 7254:         }
 7255:     } else {
 7256:         $resulttext = '<span class="LC_error">'.
 7257:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7258:     }
 7259:     return $resulttext;
 7260: }
 7261: 
 7262: sub modify_defaults {
 7263:     my ($dom,$r) = @_;
 7264:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7265:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7266:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7267:     my @authtypes = ('internal','krb4','krb5','localauth');
 7268:     foreach my $item (@items) {
 7269:         $newvalues{$item} = $env{'form.'.$item};
 7270:         if ($item eq 'auth_def') {
 7271:             if ($newvalues{$item} ne '') {
 7272:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7273:                     push(@errors,$item);
 7274:                 }
 7275:             }
 7276:         } elsif ($item eq 'lang_def') {
 7277:             if ($newvalues{$item} ne '') {
 7278:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7279:                     my $langcode = $1;
 7280:                     if ($langcode ne 'x_chef') {
 7281:                         if (code2language($langcode) eq '') {
 7282:                             push(@errors,$item);
 7283:                         }
 7284:                     }
 7285:                 } else {
 7286:                     push(@errors,$item);
 7287:                 }
 7288:             }
 7289:         } elsif ($item eq 'timezone_def') {
 7290:             if ($newvalues{$item} ne '') {
 7291:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7292:                     push(@errors,$item);   
 7293:                 }
 7294:             }
 7295:         } elsif ($item eq 'datelocale_def') {
 7296:             if ($newvalues{$item} ne '') {
 7297:                 my @datelocale_ids = DateTime::Locale->ids();
 7298:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7299:                     push(@errors,$item);
 7300:                 }
 7301:             }
 7302:         } elsif ($item eq 'portal_def') {
 7303:             if ($newvalues{$item} ne '') {
 7304:                 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])\/?$/) {
 7305:                     push(@errors,$item);
 7306:                 }
 7307:             }
 7308:         }
 7309:         if (grep(/^\Q$item\E$/,@errors)) {
 7310:             $newvalues{$item} = $domdefaults{$item};
 7311:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7312:             $changes{$item} = 1;
 7313:         }
 7314:         $domdefaults{$item} = $newvalues{$item};
 7315:     }
 7316:     my %defaults_hash = (
 7317:                          defaults => \%newvalues,
 7318:                         );
 7319:     my $title = &defaults_titles();
 7320:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7321:                                              $dom);
 7322:     if ($putresult eq 'ok') {
 7323:         if (keys(%changes) > 0) {
 7324:             $resulttext = &mt('Changes made:').'<ul>';
 7325:             my $version = $r->dir_config('lonVersion');
 7326:             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";
 7327:             foreach my $item (sort(keys(%changes))) {
 7328:                 my $value = $env{'form.'.$item};
 7329:                 if ($value eq '') {
 7330:                     $value = &mt('none');
 7331:                 } elsif ($item eq 'auth_def') {
 7332:                     my %authnames = &authtype_names();
 7333:                     my %shortauth = (
 7334:                              internal => 'int',
 7335:                              krb4 => 'krb4',
 7336:                              krb5 => 'krb5',
 7337:                              localauth  => 'loc',
 7338:                     );
 7339:                     $value = $authnames{$shortauth{$value}};
 7340:                 }
 7341:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7342:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7343:             }
 7344:             $resulttext .= '</ul>';
 7345:             $mailmsgtext .= "\n";
 7346:             my $cachetime = 24*60*60;
 7347:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7348:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7349:                 my $sysmail = $r->dir_config('lonSysEMail');
 7350:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 7351:             }
 7352:         } else {
 7353:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 7354:         }
 7355:     } else {
 7356:         $resulttext = '<span class="LC_error">'.
 7357:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7358:     }
 7359:     if (@errors > 0) {
 7360:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7361:         foreach my $item (@errors) {
 7362:             $resulttext .= ' "'.$title->{$item}.'",';
 7363:         }
 7364:         $resulttext =~ s/,$//;
 7365:     }
 7366:     return $resulttext;
 7367: }
 7368: 
 7369: sub modify_scantron {
 7370:     my ($r,$dom,$confname,%domconfig) = @_;
 7371:     my ($resulttext,%confhash,%changes,$errors);
 7372:     my $custom = 'custom.tab';
 7373:     my $default = 'default.tab';
 7374:     my $servadm = $r->dir_config('lonAdmEMail');
 7375:     my ($configuserok,$author_ok,$switchserver) = 
 7376:         &config_check($dom,$confname,$servadm);
 7377:     if ($env{'form.scantronformat.filename'} ne '') {
 7378:         my $error;
 7379:         if ($configuserok eq 'ok') {
 7380:             if ($switchserver) {
 7381:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7382:             } else {
 7383:                 if ($author_ok eq 'ok') {
 7384:                     my ($result,$scantronurl) =
 7385:                         &publishlogo($r,'upload','scantronformat',$dom,
 7386:                                      $confname,'scantron','','',$custom);
 7387:                     if ($result eq 'ok') {
 7388:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7389:                         $changes{'scantronformat'} = 1;
 7390:                     } else {
 7391:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7392:                     }
 7393:                 } else {
 7394:                     $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);
 7395:                 }
 7396:             }
 7397:         } else {
 7398:             $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);
 7399:         }
 7400:         if ($error) {
 7401:             &Apache::lonnet::logthis($error);
 7402:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7403:         }
 7404:     }
 7405:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7406:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7407:             if ($env{'form.scantronformat_del'}) {
 7408:                 $confhash{'scantron'}{'scantronformat'} = '';
 7409:                 $changes{'scantronformat'} = 1;
 7410:             }
 7411:         }
 7412:     }
 7413:     if (keys(%confhash) > 0) {
 7414:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7415:                                                  $dom);
 7416:         if ($putresult eq 'ok') {
 7417:             if (keys(%changes) > 0) {
 7418:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7419:                     $resulttext = &mt('Changes made:').'<ul>';
 7420:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7421:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7422:                     } else {
 7423:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7424:                     }
 7425:                     $resulttext .= '</ul>';
 7426:                 } else {
 7427:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7428:                 }
 7429:                 $resulttext .= '</ul>';
 7430:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7431:             } else {
 7432:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7433:             }
 7434:         } else {
 7435:             $resulttext = '<span class="LC_error">'.
 7436:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7437:         }
 7438:     } else {
 7439:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7440:     }
 7441:     if ($errors) {
 7442:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7443:                        $errors.'</ul>';
 7444:     }
 7445:     return $resulttext;
 7446: }
 7447: 
 7448: sub modify_coursecategories {
 7449:     my ($dom,%domconfig) = @_;
 7450:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7451:         $cathash);
 7452:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7453:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7454:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7455:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7456:             $changes{'togglecats'} = 1;
 7457:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7458:         }
 7459:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7460:             $changes{'categorize'} = 1;
 7461:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7462:         }
 7463:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7464:             $changes{'togglecatscomm'} = 1;
 7465:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7466:         }
 7467:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7468:             $changes{'categorizecomm'} = 1;
 7469:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7470:         }
 7471:     } else {
 7472:         $changes{'togglecats'} = 1;
 7473:         $changes{'categorize'} = 1;
 7474:         $changes{'togglecatscomm'} = 1;
 7475:         $changes{'categorizecomm'} = 1;
 7476:         $domconfig{'coursecategories'} = {
 7477:                                              togglecats => $env{'form.togglecats'},
 7478:                                              categorize => $env{'form.categorize'},
 7479:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7480:                                              categorizecomm => $env{'form.categorizecomm'},
 7481:                                          };
 7482:     }
 7483:     if (ref($cathash) eq 'HASH') {
 7484:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7485:             push (@deletecategory,'instcode::0');
 7486:         }
 7487:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7488:             push(@deletecategory,'communities::0');
 7489:         }
 7490:     }
 7491:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7492:     if (ref($cathash) eq 'HASH') {
 7493:         if (@deletecategory > 0) {
 7494:             #FIXME Need to remove category from all courses using a deleted category 
 7495:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7496:             foreach my $item (@deletecategory) {
 7497:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7498:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7499:                     $deletions{$item} = 1;
 7500:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7501:                 }
 7502:             }
 7503:         }
 7504:         foreach my $item (keys(%{$cathash})) {
 7505:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7506:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7507:                 $reorderings{$item} = 1;
 7508:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7509:             }
 7510:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7511:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7512:                 my $newdepth = $depth+1;
 7513:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7514:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7515:                 $adds{$newitem} = 1; 
 7516:             }
 7517:             if ($env{'form.subcat_'.$item} ne '') {
 7518:                 my $newcat = $env{'form.subcat_'.$item};
 7519:                 my $newdepth = $depth+1;
 7520:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7521:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7522:                 $adds{$newitem} = 1;
 7523:             }
 7524:         }
 7525:     }
 7526:     if ($env{'form.instcode'} eq '1') {
 7527:         if (ref($cathash) eq 'HASH') {
 7528:             my $newitem = 'instcode::0';
 7529:             if ($cathash->{$newitem} eq '') {  
 7530:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7531:                 $adds{$newitem} = 1;
 7532:             }
 7533:         } else {
 7534:             my $newitem = 'instcode::0';
 7535:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7536:             $adds{$newitem} = 1;
 7537:         }
 7538:     }
 7539:     if ($env{'form.communities'} eq '1') {
 7540:         if (ref($cathash) eq 'HASH') {
 7541:             my $newitem = 'communities::0';
 7542:             if ($cathash->{$newitem} eq '') {
 7543:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7544:                 $adds{$newitem} = 1;
 7545:             }
 7546:         } else {
 7547:             my $newitem = 'communities::0';
 7548:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7549:             $adds{$newitem} = 1;
 7550:         }
 7551:     }
 7552:     if ($env{'form.addcategory_name'} ne '') {
 7553:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7554:             ($env{'form.addcategory_name'} ne 'communities')) {
 7555:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7556:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7557:             $adds{$newitem} = 1;
 7558:         }
 7559:     }
 7560:     my $putresult;
 7561:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7562:         if (keys(%deletions) > 0) {
 7563:             foreach my $key (keys(%deletions)) {
 7564:                 if ($predelallitems{$key} ne '') {
 7565:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7566:                 }
 7567:             }
 7568:         }
 7569:         my (@chkcats,@chktrails,%chkallitems);
 7570:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7571:         if (ref($chkcats[0]) eq 'ARRAY') {
 7572:             my $depth = 0;
 7573:             my $chg = 0;
 7574:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7575:                 my $name = $chkcats[0][$i];
 7576:                 my $item;
 7577:                 if ($name eq '') {
 7578:                     $chg ++;
 7579:                 } else {
 7580:                     $item = &escape($name).'::0';
 7581:                     if ($chg) {
 7582:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7583:                     }
 7584:                     $depth ++; 
 7585:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7586:                     $depth --;
 7587:                 }
 7588:             }
 7589:         }
 7590:     }
 7591:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7592:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7593:         if ($putresult eq 'ok') {
 7594:             my %title = (
 7595:                          togglecats     => 'Show/Hide a course in catalog',
 7596:                          categorize     => 'Assign a category to a course',
 7597:                          togglecatscomm => 'Show/Hide a community in catalog',
 7598:                          categorizecomm => 'Assign a category to a community',
 7599:                         );
 7600:             my %level = (
 7601:                          dom  => 'set in Domain ("Modify Course/Community")',
 7602:                          crs  => 'set in Course ("Course Configuration")',
 7603:                          comm => 'set in Community ("Community Configuration")',
 7604:                         );
 7605:             $resulttext = &mt('Changes made:').'<ul>';
 7606:             if ($changes{'togglecats'}) {
 7607:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7608:             }
 7609:             if ($changes{'categorize'}) {
 7610:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7611:             }
 7612:             if ($changes{'togglecatscomm'}) {
 7613:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7614:             }
 7615:             if ($changes{'categorizecomm'}) {
 7616:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7617:             }
 7618:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7619:                 my $cathash;
 7620:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7621:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7622:                 } else {
 7623:                     $cathash = {};
 7624:                 } 
 7625:                 my (@cats,@trails,%allitems);
 7626:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7627:                 if (keys(%deletions) > 0) {
 7628:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7629:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7630:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7631:                     }
 7632:                     $resulttext .= '</ul></li>';
 7633:                 }
 7634:                 if (keys(%reorderings) > 0) {
 7635:                     my %sort_by_trail;
 7636:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7637:                     foreach my $key (keys(%reorderings)) {
 7638:                         if ($allitems{$key} ne '') {
 7639:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7640:                         }
 7641:                     }
 7642:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7643:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7644:                     }
 7645:                     $resulttext .= '</ul></li>';
 7646:                 }
 7647:                 if (keys(%adds) > 0) {
 7648:                     my %sort_by_trail;
 7649:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7650:                     foreach my $key (keys(%adds)) {
 7651:                         if ($allitems{$key} ne '') {
 7652:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7653:                         }
 7654:                     }
 7655:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7656:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7657:                     }
 7658:                     $resulttext .= '</ul></li>';
 7659:                 }
 7660:             }
 7661:             $resulttext .= '</ul>';
 7662:         } else {
 7663:             $resulttext = '<span class="LC_error">'.
 7664:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7665:         }
 7666:     } else {
 7667:         $resulttext = &mt('No changes made to course and community categories');
 7668:     }
 7669:     return $resulttext;
 7670: }
 7671: 
 7672: sub modify_serverstatuses {
 7673:     my ($dom,%domconfig) = @_;
 7674:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7675:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7676:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7677:     }
 7678:     my @pages = &serverstatus_pages();
 7679:     foreach my $type (@pages) {
 7680:         $newserverstatus{$type}{'namedusers'} = '';
 7681:         $newserverstatus{$type}{'machines'} = '';
 7682:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7683:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7684:             my @okusers;
 7685:             foreach my $user (@users) {
 7686:                 my ($uname,$udom) = split(/:/,$user);
 7687:                 if (($udom =~ /^$match_domain$/) &&   
 7688:                     (&Apache::lonnet::domain($udom)) &&
 7689:                     ($uname =~ /^$match_username$/)) {
 7690:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7691:                         push(@okusers,$user);
 7692:                     }
 7693:                 }
 7694:             }
 7695:             if (@okusers > 0) {
 7696:                  @okusers = sort(@okusers);
 7697:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7698:             }
 7699:         }
 7700:         if (defined($env{'form.'.$type.'_machines'})) {
 7701:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7702:             my @okmachines;
 7703:             foreach my $ip (@machines) {
 7704:                 my @parts = split(/\./,$ip);
 7705:                 next if (@parts < 4);
 7706:                 my $badip = 0;
 7707:                 for (my $i=0; $i<4; $i++) {
 7708:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7709:                         $badip = 1;
 7710:                         last;
 7711:                     }
 7712:                 }
 7713:                 if (!$badip) {
 7714:                     push(@okmachines,$ip);     
 7715:                 }
 7716:             }
 7717:             @okmachines = sort(@okmachines);
 7718:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7719:         }
 7720:     }
 7721:     my %serverstatushash =  (
 7722:                                 serverstatuses => \%newserverstatus,
 7723:                             );
 7724:     foreach my $type (@pages) {
 7725:         foreach my $setting ('namedusers','machines') {
 7726:             my (@current,@new);
 7727:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7728:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7729:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7730:                 }
 7731:             }
 7732:             if ($newserverstatus{$type}{$setting} ne '') {
 7733:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7734:             }
 7735:             if (@current > 0) {
 7736:                 if (@new > 0) {
 7737:                     foreach my $item (@current) {
 7738:                         if (!grep(/^\Q$item\E$/,@new)) {
 7739:                             $changes{$type}{$setting} = 1;
 7740:                             last;
 7741:                         }
 7742:                     }
 7743:                     foreach my $item (@new) {
 7744:                         if (!grep(/^\Q$item\E$/,@current)) {
 7745:                             $changes{$type}{$setting} = 1;
 7746:                             last;
 7747:                         }
 7748:                     }
 7749:                 } else {
 7750:                     $changes{$type}{$setting} = 1;
 7751:                 }
 7752:             } elsif (@new > 0) {
 7753:                 $changes{$type}{$setting} = 1;
 7754:             }
 7755:         }
 7756:     }
 7757:     if (keys(%changes) > 0) {
 7758:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7759:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7760:                                                  \%serverstatushash,$dom);
 7761:         if ($putresult eq 'ok') {
 7762:             $resulttext .= &mt('Changes made:').'<ul>';
 7763:             foreach my $type (@pages) {
 7764:                 if (ref($changes{$type}) eq 'HASH') {
 7765:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7766:                     if ($changes{$type}{'namedusers'}) {
 7767:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7768:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7769:                         } else {
 7770:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7771:                         }
 7772:                     }
 7773:                     if ($changes{$type}{'machines'}) {
 7774:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7775:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7776:                         } else {
 7777:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7778:                         }
 7779: 
 7780:                     }
 7781:                     $resulttext .= '</ul></li>';
 7782:                 }
 7783:             }
 7784:             $resulttext .= '</ul>';
 7785:         } else {
 7786:             $resulttext = '<span class="LC_error">'.
 7787:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7788: 
 7789:         }
 7790:     } else {
 7791:         $resulttext = &mt('No changes made to access to server status pages');
 7792:     }
 7793:     return $resulttext;
 7794: }
 7795: 
 7796: sub modify_helpsettings {
 7797:     my ($r,$dom,$confname,%domconfig) = @_;
 7798:     my ($resulttext,$errors,%changes,%helphash);
 7799:     my %defaultchecked = ('submitbugs' => 'on');
 7800:     my @offon = ('off','on');
 7801:     my @toggles = ('submitbugs');
 7802:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7803:         foreach my $item (@toggles) {
 7804:             if ($defaultchecked{$item} eq 'on') { 
 7805:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7806:                     if ($env{'form.'.$item} eq '0') {
 7807:                         $changes{$item} = 1;
 7808:                     }
 7809:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7810:                     $changes{$item} = 1;
 7811:                 }
 7812:             } elsif ($defaultchecked{$item} eq 'off') {
 7813:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7814:                     if ($env{'form.'.$item} eq '1') {
 7815:                         $changes{$item} = 1;
 7816:                     }
 7817:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7818:                     $changes{$item} = 1;
 7819:                 }
 7820:             }
 7821:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) { 
 7822:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7823:             }
 7824:         }
 7825:     }
 7826:     my $putresult;
 7827:     if (keys(%changes) > 0) {
 7828:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7829:         if ($putresult eq 'ok') {
 7830:             $resulttext = &mt('Changes made:').'<ul>';
 7831:             foreach my $item (sort(keys(%changes))) {
 7832:                 if ($item eq 'submitbugs') {
 7833:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 7834:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 7835:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 7836:                 }
 7837:             }
 7838:             $resulttext .= '</ul>';
 7839:         } else {
 7840:             $resulttext = &mt('No changes made to help settings');
 7841:             $errors .= '<li><span class="LC_error">'.
 7842:                        &mt('An error occurred storing the settings: [_1]',
 7843:                            $putresult).'</span></li>';
 7844:         }
 7845:     }
 7846:     if ($errors) {
 7847:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7848:                        $errors.'</ul>';
 7849:     }
 7850:     return $resulttext;
 7851: }
 7852: 
 7853: sub modify_coursedefaults {
 7854:     my ($dom,%domconfig) = @_;
 7855:     my ($resulttext,$errors,%changes,%defaultshash);
 7856:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7857:     my @offon = ('off','on');
 7858:     my @toggles = ('canuse_pdfforms');
 7859: 
 7860:     $defaultshash{'coursedefaults'} = {};
 7861: 
 7862:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7863:         if ($domconfig{'coursedefaults'} eq '') {
 7864:             $domconfig{'coursedefaults'} = {};
 7865:         }
 7866:     }
 7867: 
 7868:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7869:         foreach my $item (@toggles) {
 7870:             if ($defaultchecked{$item} eq 'on') {
 7871:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7872:                     ($env{'form.'.$item} eq '0')) {
 7873:                     $changes{$item} = 1;
 7874:                 } elsif ($domconfig{'coursdefaults'}{$item} ne $env{'form.'.$item}) {
 7875:                     $changes{$item} = 1;
 7876:                 }
 7877:             } elsif ($defaultchecked{$item} eq 'off') {
 7878:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7879:                     ($env{'form.'.$item} eq '1')) {
 7880:                     $changes{$item} = 1;
 7881:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7882:                     $changes{$item} = 1;
 7883:                 }
 7884:             }
 7885:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7886:         }
 7887:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7888:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7889:         $newdefresponder =~ s/\D//g;
 7890:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7891:             $newdefresponder = 1;
 7892:         }
 7893:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7894:         if ($currdefresponder ne $newdefresponder) {
 7895:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7896:                 $changes{'anonsurvey_threshold'} = 1;
 7897:             }
 7898:         }
 7899:     }
 7900:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7901:                                              $dom);
 7902:     if ($putresult eq 'ok') {
 7903:         if (keys(%changes) > 0) {
 7904:             if ($changes{'canuse_pdfforms'}) {
 7905:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7906:                 $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7907:                 my $cachetime = 24*60*60;
 7908:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7909:             }
 7910:             $resulttext = &mt('Changes made:').'<ul>';
 7911:             foreach my $item (sort(keys(%changes))) {
 7912:                 if ($item eq 'canuse_pdfforms') {
 7913:                     if ($env{'form.'.$item} eq '1') {
 7914:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7915:                     } else {
 7916:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7917:                     }
 7918:                 } elsif ($item eq 'anonsurvey_threshold') {
 7919:                         $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7920:                 }
 7921:             }
 7922:             $resulttext .= '</ul>';
 7923:         } else {
 7924:             $resulttext = &mt('No changes made to course defaults');
 7925:         }
 7926:     } else {
 7927:         $resulttext = '<span class="LC_error">'.
 7928:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7929:     }
 7930:     return $resulttext;
 7931: }
 7932: 
 7933: sub modify_usersessions {
 7934:     my ($dom,%domconfig) = @_;
 7935:     my @hostingtypes = ('version','excludedomain','includedomain');
 7936:     my @offloadtypes = ('primary','default');
 7937:     my %types = (
 7938:                   remote => \@hostingtypes,
 7939:                   hosted => \@hostingtypes,
 7940:                   spares => \@offloadtypes,
 7941:                 );
 7942:     my @prefixes = ('remote','hosted','spares');
 7943:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7944:     my (%by_ip,%by_location,@intdoms);
 7945:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7946:     my @locations = sort(keys(%by_location));
 7947:     my (%defaultshash,%changes);
 7948:     foreach my $prefix (@prefixes) {
 7949:         $defaultshash{'usersessions'}{$prefix} = {};
 7950:     }
 7951:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7952:     my $resulttext;
 7953:     my %iphost = &Apache::lonnet::get_iphost();
 7954:     foreach my $prefix (@prefixes) {
 7955:         next if ($prefix eq 'spares');
 7956:         foreach my $type (@{$types{$prefix}}) {
 7957:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7958:             if ($type eq 'version') {
 7959:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7960:                 my $okvalue;
 7961:                 if ($value ne '') {
 7962:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 7963:                         $okvalue = $value;
 7964:                     }
 7965:                 }
 7966:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7967:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7968:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 7969:                             if ($inuse == 0) {
 7970:                                 $changes{$prefix}{$type} = 1;
 7971:                             } else {
 7972:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 7973:                                     $changes{$prefix}{$type} = 1;
 7974:                                 }
 7975:                                 if ($okvalue ne '') {
 7976:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7977:                                 } 
 7978:                             }
 7979:                         } else {
 7980:                             if (($inuse == 1) && ($okvalue ne '')) {
 7981:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7982:                                 $changes{$prefix}{$type} = 1;
 7983:                             }
 7984:                         }
 7985:                     } else {
 7986:                         if (($inuse == 1) && ($okvalue ne '')) {
 7987:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7988:                             $changes{$prefix}{$type} = 1;
 7989:                         }
 7990:                     }
 7991:                 } else {
 7992:                     if (($inuse == 1) && ($okvalue ne '')) {
 7993:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7994:                         $changes{$prefix}{$type} = 1;
 7995:                     }
 7996:                 }
 7997:             } else {
 7998:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 7999:                 my @okvals;
 8000:                 foreach my $val (@vals) {
 8001:                     if ($val =~ /:/) {
 8002:                         my @items = split(/:/,$val);
 8003:                         foreach my $item (@items) {
 8004:                             if (ref($by_location{$item}) eq 'ARRAY') {
 8005:                                 push(@okvals,$item);
 8006:                             }
 8007:                         }
 8008:                     } else {
 8009:                         if (ref($by_location{$val}) eq 'ARRAY') {
 8010:                             push(@okvals,$val);
 8011:                         }
 8012:                     }
 8013:                 }
 8014:                 @okvals = sort(@okvals);
 8015:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8016:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8017:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8018:                             if ($inuse == 0) {
 8019:                                 $changes{$prefix}{$type} = 1; 
 8020:                             } else {
 8021:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8022:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 8023:                                 if (@changed > 0) {
 8024:                                     $changes{$prefix}{$type} = 1;
 8025:                                 }
 8026:                             }
 8027:                         } else {
 8028:                             if ($inuse == 1) {
 8029:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8030:                                 $changes{$prefix}{$type} = 1;
 8031:                             }
 8032:                         } 
 8033:                     } else {
 8034:                         if ($inuse == 1) {
 8035:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8036:                             $changes{$prefix}{$type} = 1;
 8037:                         }
 8038:                     }
 8039:                 } else {
 8040:                     if ($inuse == 1) {
 8041:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8042:                         $changes{$prefix}{$type} = 1;
 8043:                     }
 8044:                 }
 8045:             }
 8046:         }
 8047:     }
 8048: 
 8049:     my @alldoms = &Apache::lonnet::all_domains();
 8050:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8051:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 8052:     my $savespares;
 8053: 
 8054:     foreach my $lonhost (sort(keys(%servers))) {
 8055:         my $serverhomeID =
 8056:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 8057:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 8058:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 8059:         my %spareschg;
 8060:         foreach my $type (@{$types{'spares'}}) {
 8061:             my @okspares;
 8062:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 8063:             foreach my $server (@checked) {
 8064:                 if (&Apache::lonnet::hostname($server) ne '') {
 8065:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 8066:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 8067:                             push(@okspares,$server);
 8068:                         }
 8069:                     }
 8070:                 }
 8071:             }
 8072:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 8073:             my $newspare;
 8074:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 8075:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 8076:                     $newspare = $new;
 8077:                 }
 8078:             }
 8079:             my @spares;
 8080:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 8081:                 @spares = sort(@okspares,$newspare);
 8082:             } else {
 8083:                 @spares = sort(@okspares);
 8084:             }
 8085:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 8086:             if (ref($spareid{$lonhost}) eq 'HASH') {
 8087:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 8088:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 8089:                     if (@diffs > 0) {
 8090:                         $spareschg{$type} = 1;
 8091:                     }
 8092:                 }
 8093:             }
 8094:         }
 8095:         if (keys(%spareschg) > 0) {
 8096:             $changes{'spares'}{$lonhost} = \%spareschg;
 8097:         }
 8098:     }
 8099: 
 8100:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8101:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 8102:             if (ref($changes{'spares'}) eq 'HASH') {
 8103:                 if (keys(%{$changes{'spares'}}) > 0) {
 8104:                     $savespares = 1;
 8105:                 }
 8106:             }
 8107:         } else {
 8108:             $savespares = 1;
 8109:         }
 8110:     }
 8111: 
 8112:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 8113:     if ((keys(%changes) > 0) || ($savespares)) {
 8114:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8115:                                                  $dom);
 8116:         if ($putresult eq 'ok') {
 8117:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8118:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 8119:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 8120:                 }
 8121:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 8122:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 8123:                 }
 8124:             }
 8125:             my $cachetime = 24*60*60;
 8126:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8127:             if (keys(%changes) > 0) {
 8128:                 my %lt = &usersession_titles();
 8129:                 $resulttext = &mt('Changes made:').'<ul>';
 8130:                 foreach my $prefix (@prefixes) {
 8131:                     if (ref($changes{$prefix}) eq 'HASH') {
 8132:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 8133:                         if ($prefix eq 'spares') {
 8134:                             if (ref($changes{$prefix}) eq 'HASH') {
 8135:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 8136:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 8137:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 8138:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 8139:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 8140:                                         foreach my $type (@{$types{$prefix}}) {
 8141:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 8142:                                                 my $offloadto = &mt('None');
 8143:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 8144:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 8145:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 8146:                                                     }
 8147:                                                 }
 8148:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 8149:                                             }
 8150:                                         }
 8151:                                     }
 8152:                                     $resulttext .= '</li>';
 8153:                                 }
 8154:                             }
 8155:                         } else {
 8156:                             foreach my $type (@{$types{$prefix}}) {
 8157:                                 if (defined($changes{$prefix}{$type})) {
 8158:                                     my $newvalue;
 8159:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8160:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 8161:                                             if ($type eq 'version') {
 8162:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 8163:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8164:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 8165:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 8166:                                                 }
 8167:                                             }
 8168:                                         }
 8169:                                     }
 8170:                                     if ($newvalue eq '') {
 8171:                                         if ($type eq 'version') {
 8172:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8173:                                         } else {
 8174:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8175:                                         }
 8176:                                     } else {
 8177:                                         if ($type eq 'version') {
 8178:                                             $newvalue .= ' '.&mt('(or later)'); 
 8179:                                         }
 8180:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8181:                                     }
 8182:                                 }
 8183:                             }
 8184:                         }
 8185:                         $resulttext .= '</ul>';
 8186:                     }
 8187:                 }
 8188:                 $resulttext .= '</ul>';
 8189:             } else {
 8190:                 $resulttext = $nochgmsg;
 8191:             }
 8192:         } else {
 8193:             $resulttext = '<span class="LC_error">'.
 8194:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8195:         }
 8196:     } else {
 8197:         $resulttext = $nochgmsg;
 8198:     }
 8199:     return $resulttext;
 8200: }
 8201: 
 8202: sub modify_loadbalancing {
 8203:     my ($dom,%domconfig) = @_;
 8204:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8205:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8206:     my ($othertitle,$usertypes,$types) =
 8207:         &Apache::loncommon::sorted_inst_types($dom);
 8208:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8209:     my @sparestypes = ('primary','default');
 8210:     my %typetitles = &sparestype_titles();
 8211:     my $resulttext;
 8212:     my (%currbalancer,%currtargets,%currrules,%existing);
 8213:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8214:         %existing = %{$domconfig{'loadbalancing'}};
 8215:     }
 8216:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8217:                               \%currtargets,\%currrules);
 8218:     my ($saveloadbalancing,%defaultshash,%changes);
 8219:     my ($alltypes,$othertypes,$titles) =
 8220:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8221:     my %ruletitles = &offloadtype_text();
 8222:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 8223:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 8224:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 8225:         if ($balancer eq '') {
 8226:             next;
 8227:         }
 8228:         if (!exists($servers{$balancer})) { 
 8229:             if (exists($currbalancer{$balancer})) {
 8230:                 push(@{$changes{'delete'}},$balancer);
 8231:             }
 8232:             next;
 8233:         }
 8234:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 8235:             push(@{$changes{'delete'}},$balancer);
 8236:             next;
 8237:         }
 8238:         if (!exists($currbalancer{$balancer})) {
 8239:             push(@{$changes{'add'}},$balancer);
 8240:         }
 8241:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 8242:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 8243:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 8244:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8245:             $saveloadbalancing = 1;
 8246:         }
 8247:         foreach my $sparetype (@sparestypes) {
 8248:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 8249:             my @offloadto;
 8250:             foreach my $target (@targets) {
 8251:                 if (($servers{$target}) && ($target ne $balancer)) {
 8252:                     if ($sparetype eq 'default') {
 8253:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 8254:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 8255:                         }
 8256:                     }
 8257:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 8258:                         push(@offloadto,$target);
 8259:                     }
 8260:                 }
 8261:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 8262:             }
 8263:         }
 8264:         if (ref($currtargets{$balancer}) eq 'HASH') {
 8265:             foreach my $sparetype (@sparestypes) {
 8266:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 8267:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 8268:                     if (@targetdiffs > 0) {
 8269:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8270:                     }
 8271:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8272:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8273:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8274:                     }
 8275:                 }
 8276:             }
 8277:         } else {
 8278:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 8279:                 foreach my $sparetype (@sparestypes) { 
 8280:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8281:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8282:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 8283:                         }
 8284:                     }
 8285:                 }
 8286:             }  
 8287:         }
 8288:         my $ishomedom;
 8289:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 8290:             $ishomedom = 1;
 8291:         }
 8292:         if (ref($alltypes) eq 'ARRAY') {
 8293:             foreach my $type (@{$alltypes}) {
 8294:                 my $rule;
 8295:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) && 
 8296:                          (!$ishomedom)) {
 8297:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 8298:                 }
 8299:                 if ($rule eq 'specific') {
 8300:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 8301:                 }
 8302:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 8303:                 if (ref($currrules{$balancer}) eq 'HASH') {
 8304:                     if ($rule ne $currrules{$balancer}{$type}) {
 8305:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8306:                     }
 8307:                 } elsif ($rule ne '') {
 8308:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8309:                 }
 8310:             }
 8311:         }
 8312:     }
 8313:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8314:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8315:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 8316:             $defaultshash{'loadbalancing'} = {};
 8317:         }
 8318:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8319:                                                  \%defaultshash,$dom);
 8320:  
 8321:         if ($putresult eq 'ok') {
 8322:             if (keys(%changes) > 0) {
 8323:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 8324:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 8325:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 8326:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8327:                     }
 8328:                 }
 8329:                 if (ref($changes{'add'}) eq 'ARRAY') {
 8330:                     foreach my $balancer (sort(@{$changes{'add'}})) {   
 8331:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 8332:                     }
 8333:                 }
 8334:                 if (ref($changes{'curr'}) eq 'HASH') {
 8335:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 8336:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 8337:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 8338:                                 my %offloadstr;
 8339:                                 foreach my $sparetype (@sparestypes) {
 8340:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8341:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8342:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 8343:                                         }
 8344:                                     }
 8345:                                 }
 8346:                                 if (keys(%offloadstr) == 0) {
 8347:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8348:                                 } else {
 8349:                                     my $showoffload;
 8350:                                     foreach my $sparetype (@sparestypes) {
 8351:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8352:                                         if (defined($offloadstr{$sparetype})) {
 8353:                                             $showoffload .= $offloadstr{$sparetype};
 8354:                                         } else {
 8355:                                             $showoffload .= &mt('None');
 8356:                                         }
 8357:                                         $showoffload .= ('&nbsp;'x3);
 8358:                                     }
 8359:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 8360:                                 }
 8361:                             }
 8362:                         }
 8363:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 8364:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8365:                                 foreach my $type (@{$alltypes}) {
 8366:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 8367:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 8368:                                         my $balancetext;
 8369:                                         if ($rule eq '') {
 8370:                                             $balancetext =  $ruletitles{'default'};
 8371:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 8372:                                             $balancetext =  $ruletitles{$rule};
 8373:                                         } else {
 8374:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 8375:                                         }
 8376:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';     
 8377:                                     }
 8378:                                 }
 8379:                             }
 8380:                         }
 8381:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8382:                     }
 8383:                 }
 8384:                 if ($resulttext ne '') {
 8385:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8386:                 } else {
 8387:                     $resulttext = $nochgmsg;
 8388:                 }
 8389:             } else {
 8390:                 $resulttext = $nochgmsg;
 8391:             }
 8392:         } else {
 8393:             $resulttext = '<span class="LC_error">'.
 8394:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8395:         }
 8396:     } else {
 8397:         $resulttext = $nochgmsg;
 8398:     }
 8399:     return $resulttext;
 8400: }
 8401: 
 8402: sub recurse_check {
 8403:     my ($chkcats,$categories,$depth,$name) = @_;
 8404:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8405:         my $chg = 0;
 8406:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8407:             my $category = $chkcats->[$depth]{$name}[$j];
 8408:             my $item;
 8409:             if ($category eq '') {
 8410:                 $chg ++;
 8411:             } else {
 8412:                 my $deeper = $depth + 1;
 8413:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8414:                 if ($chg) {
 8415:                     $categories->{$item} -= $chg;
 8416:                 }
 8417:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8418:                 $deeper --;
 8419:             }
 8420:         }
 8421:     }
 8422:     return;
 8423: }
 8424: 
 8425: sub recurse_cat_deletes {
 8426:     my ($item,$coursecategories,$deletions) = @_;
 8427:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8428:     my $subdepth = $depth + 1;
 8429:     if (ref($coursecategories) eq 'HASH') {
 8430:         foreach my $subitem (keys(%{$coursecategories})) {
 8431:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8432:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8433:                 delete($coursecategories->{$subitem});
 8434:                 $deletions->{$subitem} = 1;
 8435:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8436:             }
 8437:         }
 8438:     }
 8439:     return;
 8440: }
 8441: 
 8442: sub get_active_dcs {
 8443:     my ($dom) = @_;
 8444:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc']);
 8445:     my %domcoords;
 8446:     my $numdcs = 0;
 8447:     my $now = time;
 8448:     foreach my $server (keys(%dompersonnel)) {
 8449:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8450:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8451:             my ($end,$start) = split(':',$dompersonnel{$server}{$user});
 8452:             if (($end eq '') || ($end == 0) || ($end > $now)) {
 8453:                 if ($start <= $now) {
 8454:                     $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8455:                 }
 8456:             }
 8457:         }
 8458:     }
 8459:     return %domcoords;
 8460: }
 8461: 
 8462: sub active_dc_picker {
 8463:     my ($dom,$curr_dc) = @_;
 8464:     my %domcoords = &get_active_dcs($dom); 
 8465:     my @dcs = sort(keys(%domcoords));
 8466:     my $numdcs = scalar(@dcs); 
 8467:     my $datatable;
 8468:     my $numinrow = 2;
 8469:     if ($numdcs > 1) {
 8470:         $datatable = '<table>';
 8471:         for (my $i=0; $i<@dcs; $i++) {
 8472:             my $rem = $i%($numinrow);
 8473:             if ($rem == 0) {
 8474:                 if ($i > 0) {
 8475:                     $datatable .= '</tr>';
 8476:                 }
 8477:                 $datatable .= '<tr>';
 8478:             }
 8479:             my $check = ' ';
 8480:             if ($curr_dc eq '') {
 8481:                 if (!$i) { 
 8482:                     $check = ' checked="checked" ';
 8483:                 }
 8484:             } elsif ($dcs[$i] eq $curr_dc) {
 8485:                 $check = ' checked="checked" ';
 8486:             }
 8487:             if ($i == @dcs - 1) {
 8488:                 my $colsleft = $numinrow - $rem;
 8489:                 if ($colsleft > 1) {
 8490:                     $datatable .= '<td colspan="'.$colsleft.'">';
 8491:                 } else {
 8492:                     $datatable .= '<td>';
 8493:                 }
 8494:             } else {
 8495:                 $datatable .= '<td>';
 8496:             }
 8497:             my ($dcname,$dcdom) = split(':',$dcs[$i]);
 8498:             $datatable .= '<span class="LC_nobreak"><label>'.
 8499:                           '<input type="radio" name="autocreate_xmldc"'.
 8500:                           ' value="'.$dcs[$i].'"'.$check.'/>'.
 8501:                           &Apache::loncommon::plainname($dcname,$dcdom).
 8502:                           ' ('.$dcname.':'.$dcdom.')'.
 8503:                           '</label></span></td>';
 8504:         }
 8505:         $datatable .= '</tr></table>';
 8506:     } elsif (@dcs) {
 8507:         $datatable .= '<input type="hidden" name="autocreate_dc" value="'.
 8508:                       $dcs[0].'" />';
 8509:     }
 8510:     return ($numdcs,$datatable);
 8511: }
 8512: 
 8513: sub usersession_titles {
 8514:     return &Apache::lonlocal::texthash(
 8515:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8516:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8517:                spares => 'Servers offloaded to, when busy',
 8518:                version => 'LON-CAPA version requirement',
 8519:                excludedomain => 'Allow all, but exclude specific domains',
 8520:                includedomain => 'Deny all, but include specific domains',
 8521:                primary => 'Primary (checked first)',
 8522:                default => 'Default',
 8523:            );
 8524: }
 8525: 
 8526: sub id_for_thisdom {
 8527:     my (%servers) = @_;
 8528:     my %altids;
 8529:     foreach my $server (keys(%servers)) {
 8530:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8531:         if ($serverhome ne $server) {
 8532:             $altids{$serverhome} = $server;
 8533:         }
 8534:     }
 8535:     return %altids;
 8536: }
 8537: 
 8538: sub count_servers {
 8539:     my ($currbalancer,%servers) = @_;
 8540:     my (@spares,$numspares);
 8541:     foreach my $lonhost (sort(keys(%servers))) {
 8542:         next if ($currbalancer eq $lonhost);
 8543:         push(@spares,$lonhost);
 8544:     }
 8545:     if ($currbalancer) {
 8546:         $numspares = scalar(@spares);
 8547:     } else {
 8548:         $numspares = scalar(@spares) - 1;
 8549:     }
 8550:     return ($numspares,@spares);
 8551: }
 8552: 
 8553: sub lonbalance_targets_js {
 8554:     my ($dom,$types,$servers,$settings) = @_;
 8555:     my $select = &mt('Select');
 8556:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8557:     if (ref($servers) eq 'HASH') {
 8558:         $alltargets = join("','",sort(keys(%{$servers})));
 8559:         my @homedoms;
 8560:         foreach my $server (sort(keys(%{$servers}))) {
 8561:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8562:                 push(@homedoms,'1');
 8563:             } else {
 8564:                 push(@homedoms,'0');
 8565:             }
 8566:         }
 8567:         $allishome = join("','",@homedoms);
 8568:     }
 8569:     if (ref($types) eq 'ARRAY') {
 8570:         if (@{$types} > 0) {
 8571:             @alltypes = @{$types};
 8572:         }
 8573:     }
 8574:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8575:     $allinsttypes = join("','",@alltypes);
 8576:     my (%currbalancer,%currtargets,%currrules,%existing);
 8577:     if (ref($settings) eq 'HASH') {
 8578:         %existing = %{$settings};
 8579:     }
 8580:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 8581:                               \%currtargets,\%currrules);
 8582:     my $balancers = join("','",sort(keys(%currbalancer))); 
 8583:     return <<"END";
 8584: 
 8585: <script type="text/javascript">
 8586: // <![CDATA[
 8587: 
 8588: currBalancers = new Array('$balancers');
 8589: 
 8590: function toggleTargets(balnum) {
 8591:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8592:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 8593:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 8594:     var prevbalancer = prevhostitem.value;
 8595:     var baltotal = document.getElementById('loadbalancing_total').value;
 8596:     prevhostitem.value = balancer;
 8597:     if (prevbalancer != '') {
 8598:         var prevIdx = currBalancers.indexOf(prevbalancer);
 8599:         if (prevIdx != -1) {
 8600:             currBalancers.splice(prevIdx,1);
 8601:         }
 8602:     }
 8603:     if (balancer == '') {
 8604:         hideSpares(balnum);
 8605:     } else {
 8606:         var currIdx = currBalancers.indexOf(balancer);
 8607:         if (currIdx == -1) {
 8608:             currBalancers.push(balancer);
 8609:         }
 8610:         var homedoms = new Array('$allishome');
 8611:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 8612:         showSpares(balancer,ishomedom,balnum);
 8613:     }
 8614:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 8615:     return;
 8616: }
 8617: 
 8618: function showSpares(balancer,ishomedom,balnum) {
 8619:     var alltargets = new Array('$alltargets');
 8620:     var insttypes = new Array('$allinsttypes');
 8621:     var offloadtypes = new Array('primary','default');
 8622: 
 8623:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 8624:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 8625:  
 8626:     for (var i=0; i<offloadtypes.length; i++) {
 8627:         var count = 0;
 8628:         for (var j=0; j<alltargets.length; j++) {
 8629:             if (alltargets[j] != balancer) {
 8630:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 8631:                 item.value = alltargets[j];
 8632:                 item.style.textAlign='left';
 8633:                 item.style.textFace='normal';
 8634:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8635:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 8636:                     item.disabled = '';
 8637:                 } else {
 8638:                     item.disabled = 'disabled';
 8639:                     item.checked = false;
 8640:                 }
 8641:                 count ++;
 8642:             }
 8643:         }
 8644:     }
 8645:     for (var k=0; k<insttypes.length; k++) {
 8646:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8647:             if (ishomedom == 1) {
 8648:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8649:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8650:             } else {
 8651:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8652:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8653: 
 8654:             }
 8655:         } else {
 8656:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8657:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8658:         }
 8659:         if ((insttypes[k] != '_LC_external') && 
 8660:             ((insttypes[k] != '_LC_internetdom') ||
 8661:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8662:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 8663:             item.options.length = 0;
 8664:             item.options[0] = new Option("","",true,true);
 8665:             var idx = 0;  
 8666:             for (var m=0; m<alltargets.length; m++) {
 8667:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 8668:                     idx ++;
 8669:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8670:                     
 8671:                 }
 8672:             }
 8673:         }
 8674:     }
 8675:     return;
 8676: }
 8677: 
 8678: function hideSpares(balnum) {
 8679:     var alltargets = new Array('$alltargets');
 8680:     var insttypes = new Array('$allinsttypes');
 8681:     var offloadtypes = new Array('primary','default');
 8682: 
 8683:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 8684:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 8685: 
 8686:     var total = alltargets.length - 1;
 8687:     for (var i=0; i<offloadtypes; i++) {
 8688:         for (var j=0; j<total; j++) {
 8689:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 8690:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 8691:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8692:         }
 8693:     }
 8694:     for (var k=0; k<insttypes.length; k++) {
 8695:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8696:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8697:         if (insttypes[k] != '_LC_external') {
 8698:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 8699:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 8700:         }
 8701:     }
 8702:     return;
 8703: }
 8704: 
 8705: function checkOffloads(item,balnum,type) {
 8706:     var alltargets = new Array('$alltargets');
 8707:     var offloadtypes = new Array('primary','default');
 8708:     if (item.checked) {
 8709:         var total = alltargets.length - 1;
 8710:         var other;
 8711:         if (type == offloadtypes[0]) {
 8712:             other = offloadtypes[1];
 8713:         } else {
 8714:             other = offloadtypes[0];
 8715:         }
 8716:         for (var i=0; i<total; i++) {
 8717:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 8718:             if (server == item.value) {
 8719:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 8720:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 8721:                 }
 8722:             }
 8723:         }
 8724:     }
 8725:     return;
 8726: }
 8727: 
 8728: function singleServerToggle(balnum,type) {
 8729:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 8730:     if (offloadtoSelIdx == 0) {
 8731:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 8732:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8733: 
 8734:     } else {
 8735:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 8736:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8737:     }
 8738:     return;
 8739: }
 8740: 
 8741: function balanceruleChange(formname,balnum,type) {
 8742:     if (type == '_LC_external') {
 8743:         return;
 8744:     }
 8745:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 8746:     for (var i=0; i<typesRules.length; i++) {
 8747:         if (formname.elements[typesRules[i]].checked) {
 8748:             if (formname.elements[typesRules[i]].value != 'specific') {
 8749:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 8750:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8751:             } else {
 8752:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8753:             }
 8754:         }
 8755:     }
 8756:     return;
 8757: }
 8758: 
 8759: function balancerDeleteChange(balnum) {
 8760:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8761:     var baltotal = document.getElementById('loadbalancing_total').value;
 8762:     var addtarget;
 8763:     var removetarget;
 8764:     var action = 'delete';
 8765:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 8766:         var lonhost = hostitem.value;
 8767:         var currIdx = currBalancers.indexOf(lonhost);
 8768:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 8769:             if (currIdx != -1) {
 8770:                 currBalancers.splice(currIdx,1);
 8771:             }
 8772:             addtarget = lonhost;
 8773:         } else {
 8774:             if (currIdx == -1) {
 8775:                 currBalancers.push(lonhost);
 8776:             }
 8777:             removetarget = lonhost;
 8778:             action = 'undelete';
 8779:         }
 8780:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 8781:     }
 8782:     return;
 8783: }
 8784: 
 8785: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 8786:     if (baltotal > 1) {
 8787:         var offloadtypes = new Array('primary','default');
 8788:         var alltargets = new Array('$alltargets');
 8789:         var insttypes = new Array('$allinsttypes');
 8790:         for (var i=0; i<baltotal; i++) {
 8791:             if (i != balnum) {
 8792:                 for (var j=0; j<offloadtypes.length; j++) {
 8793:                     var total = alltargets.length - 1;
 8794:                     for (var k=0; k<total; k++) {
 8795:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 8796:                         var server = serveritem.value;
 8797:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 8798:                             if (server == addtarget) {
 8799:                                 serveritem.disabled = '';
 8800:                             }
 8801:                         }
 8802:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8803:                             if (server == removetarget) {
 8804:                                 serveritem.disabled = 'disabled';
 8805:                                 serveritem.checked = false;
 8806:                             }
 8807:                         }
 8808:                     }
 8809:                 }
 8810:                 for (var j=0; j<insttypes.length; j++) {
 8811:                     if (insttypes[j] != '_LC_external') {
 8812:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 8813:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 8814:                             var currSel = singleserver.selectedIndex;
 8815:                             var currVal = singleserver.options[currSel].value;
 8816:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 8817:                                 var numoptions = singleserver.options.length;
 8818:                                 var needsnew = 1;
 8819:                                 for (var k=0; k<numoptions; k++) {
 8820:                                     if (singleserver.options[k] == addtarget) {
 8821:                                         needsnew = 0;
 8822:                                         break;
 8823:                                     }
 8824:                                 }
 8825:                                 if (needsnew == 1) {
 8826:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 8827:                                 }
 8828:                             }
 8829:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8830:                                 singleserver.options.length = 0;
 8831:                                 if ((currVal) && (currVal != removetarget)) {
 8832:                                     singleserver.options[0] = new Option("","",false,false);
 8833:                                 } else {
 8834:                                     singleserver.options[0] = new Option("","",true,true);
 8835:                                 }
 8836:                                 var idx = 0;
 8837:                                 for (var m=0; m<alltargets.length; m++) {
 8838:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
 8839:                                         idx ++;
 8840:                                         if (currVal == alltargets[m]) {
 8841:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
 8842:                                         } else {
 8843:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8844:                                         }
 8845:                                     }
 8846:                                 }
 8847:                             }
 8848:                         }
 8849:                     }
 8850:                 }
 8851:             }
 8852:         }
 8853:     }
 8854:     return;
 8855: }
 8856: 
 8857: // ]]>
 8858: </script>
 8859: 
 8860: END
 8861: }
 8862: 
 8863: sub new_spares_js {
 8864:     my @sparestypes = ('primary','default');
 8865:     my $types = join("','",@sparestypes);
 8866:     my $select = &mt('Select');
 8867:     return <<"END";
 8868: 
 8869: <script type="text/javascript">
 8870: // <![CDATA[
 8871: 
 8872: function updateNewSpares(formname,lonhost) {
 8873:     var types = new Array('$types');
 8874:     var include = new Array();
 8875:     var exclude = new Array();
 8876:     for (var i=0; i<types.length; i++) {
 8877:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8878:         for (var j=0; j<spareboxes.length; j++) {
 8879:             if (formname.elements[spareboxes[j]].checked) {
 8880:                 exclude.push(formname.elements[spareboxes[j]].value);
 8881:             } else {
 8882:                 include.push(formname.elements[spareboxes[j]].value);
 8883:             }
 8884:         }
 8885:     }
 8886:     for (var i=0; i<types.length; i++) {
 8887:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8888:         var selIdx = newSpare.selectedIndex;
 8889:         var currnew = newSpare.options[selIdx].value;
 8890:         var okSpares = new Array();
 8891:         for (var j=0; j<newSpare.options.length; j++) {
 8892:             var possible = newSpare.options[j].value;
 8893:             if (possible != '') {
 8894:                 if (exclude.indexOf(possible) == -1) {
 8895:                     okSpares.push(possible);
 8896:                 } else {
 8897:                     if (currnew == possible) {
 8898:                         selIdx = 0;
 8899:                     }
 8900:                 }
 8901:             }
 8902:         }
 8903:         for (var k=0; k<include.length; k++) {
 8904:             if (okSpares.indexOf(include[k]) == -1) {
 8905:                 okSpares.push(include[k]);
 8906:             }
 8907:         }
 8908:         okSpares.sort();
 8909:         newSpare.options.length = 0;
 8910:         if (selIdx == 0) {
 8911:             newSpare.options[0] = new Option("$select","",true,true);
 8912:         } else {
 8913:             newSpare.options[0] = new Option("$select","",false,false);
 8914:         }
 8915:         for (var m=0; m<okSpares.length; m++) {
 8916:             var idx = m+1;
 8917:             var selThis = 0;
 8918:             if (selIdx != 0) {
 8919:                 if (okSpares[m] == currnew) {
 8920:                     selThis = 1;
 8921:                 }
 8922:             }
 8923:             if (selThis == 1) {
 8924:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8925:             } else {
 8926:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8927:             }
 8928:         }
 8929:     }
 8930:     return;
 8931: }
 8932: 
 8933: function checkNewSpares(lonhost,type) {
 8934:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8935:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8936:     if (chosen != '') { 
 8937:         var othertype;
 8938:         var othernewSpare;
 8939:         if (type == 'primary') {
 8940:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 8941:         }
 8942:         if (type == 'default') {
 8943:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 8944:         }
 8945:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 8946:             othernewSpare.selectedIndex = 0;
 8947:         }
 8948:     }
 8949:     return;
 8950: }
 8951: 
 8952: // ]]>
 8953: </script>
 8954: 
 8955: END
 8956: 
 8957: }
 8958: 
 8959: sub common_domprefs_js {
 8960:     return <<"END";
 8961: 
 8962: <script type="text/javascript">
 8963: // <![CDATA[
 8964: 
 8965: function getIndicesByName(formname,item) {
 8966:     var group = new Array();
 8967:     for (var i=0;i<formname.elements.length;i++) {
 8968:         if (formname.elements[i].name == item) {
 8969:             group.push(formname.elements[i].id);
 8970:         }
 8971:     }
 8972:     return group;
 8973: }
 8974: 
 8975: // ]]>
 8976: </script>
 8977: 
 8978: END
 8979: 
 8980: }
 8981: 
 8982: sub recaptcha_js {
 8983:     my %lt = &captcha_phrases();
 8984:     return <<"END";
 8985: 
 8986: <script type="text/javascript">
 8987: // <![CDATA[
 8988: 
 8989: function updateCaptcha(caller,context) {
 8990:     var privitem;
 8991:     var pubitem;
 8992:     var privtext;
 8993:     var pubtext;
 8994:     if (document.getElementById(context+'_recaptchapub')) {
 8995:         pubitem = document.getElementById(context+'_recaptchapub');
 8996:     } else {
 8997:         return;
 8998:     }
 8999:     if (document.getElementById(context+'_recaptchapriv')) {
 9000:         privitem = document.getElementById(context+'_recaptchapriv');
 9001:     } else {
 9002:         return;
 9003:     }
 9004:     if (document.getElementById(context+'_recaptchapubtxt')) {
 9005:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 9006:     } else {
 9007:         return;
 9008:     }
 9009:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 9010:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 9011:     } else {
 9012:         return;
 9013:     }
 9014:     if (caller.checked) {
 9015:         if (caller.value == 'recaptcha') {
 9016:             pubitem.type = 'text';
 9017:             privitem.type = 'text';
 9018:             pubitem.size = '40';
 9019:             privitem.size = '40';
 9020:             pubtext.innerHTML = "$lt{'pub'}";
 9021:             privtext.innerHTML = "$lt{'priv'}";
 9022:         } else {
 9023:             pubitem.type = 'hidden';
 9024:             privitem.type = 'hidden';
 9025:             pubtext.innerHTML = '';
 9026:             privtext.innerHTML = '';
 9027:         }
 9028:     }
 9029:     return;
 9030: }
 9031: 
 9032: // ]]>
 9033: </script>
 9034: 
 9035: END
 9036: 
 9037: }
 9038: 
 9039: sub captcha_phrases {
 9040:     return &Apache::lonlocal::texthash (
 9041:                  priv => 'Private key',
 9042:                  pub  => 'Public key',
 9043:                  original  => 'original (CAPTCHA)',
 9044:                  recaptcha => 'successor (ReCAPTCHA)',
 9045:                  notused   => 'unused',
 9046:     );
 9047: }
 9048: 
 9049: 1;

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