File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.165: download - view: text, annotated - select for diffs
Sat Aug 25 04:34:44 2012 UTC (11 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Validation of human (CAPTCHA).
  - self creation of account (e-mail address as username).
  - choice of CAPTCHA:  original (Authen::CAPTCHA) or newer reCAPTCHA (requires)
    DC to create account with http://www.google.com/recaptcha/, or not used.
  - Routines to generate CAPTCHA input and validate response moved to loncommon.pm
    to facilitate re-use.
  - Domain configuration for self-creation of accounts allows DC to choose which
    CAPTCHA option to use.
- CPAN module: Captcha::reCAPTCHA now needed. rpm being added to msu/testing repos,
  and dependency included in LONCAPA-prerequisites 1-21.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.165 2012/08/25 04:34:44 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas,  
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course.
   98: 
   99: Outputs: 1
  100: 
  101: $datatable  - HTML containing form elements which allow settings to be changed. 
  102: 
  103: In the case of course requests, radio buttons are displayed for each institutional
  104: affiliate type (and also default, and _LC_adv) for each of the course types 
  105: (official, unofficial and community).  In each case the radio buttons allow the 
  106: selection of one of four values:
  107: 
  108: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  109: which have the following effects:
  110: 
  111: 0
  112: 
  113: =over
  114: 
  115: - course requests are not allowed for this course types/affiliation
  116: 
  117: =back
  118: 
  119: approval 
  120: 
  121: =over 
  122: 
  123: - course requests must be approved by a Doman Coordinator in the 
  124: course's domain
  125: 
  126: =back
  127: 
  128: validate 
  129: 
  130: =over
  131: 
  132: - an institutional validation (e.g., check requestor is instructor
  133: of record) needs to be passed before the course will be created.  The required
  134: validation is in localenroll.pm on the primary library server for the course 
  135: domain.
  136: 
  137: =back
  138: 
  139: autolimit 
  140: 
  141: =over
  142:  
  143: - course requests will be processed automatically up to a limit of
  144: N requests for the course type for the particular requestor.
  145: If N is undefined, there is no limit to the number of course requests
  146: which a course owner may submit and have processed automatically. 
  147: 
  148: =back
  149: 
  150: =item modify_quotas() 
  151: 
  152: =back
  153: 
  154: =cut
  155: 
  156: package Apache::domainprefs;
  157: 
  158: use strict;
  159: use Apache::Constants qw(:common :http);
  160: use Apache::lonnet;
  161: use Apache::loncommon();
  162: use Apache::lonhtmlcommon();
  163: use Apache::lonlocal;
  164: use Apache::lonmsg();
  165: use Apache::lonconfigsettings;
  166: use LONCAPA qw(:DEFAULT :match);
  167: use LONCAPA::Enrollment;
  168: use LONCAPA::lonauthcgi();
  169: use File::Copy;
  170: use Locale::Language;
  171: use DateTime::TimeZone;
  172: use DateTime::Locale;
  173: 
  174: my $registered_cleanup;
  175: my $modified_urls;
  176: 
  177: sub handler {
  178:     my $r=shift;
  179:     if ($r->header_only) {
  180:         &Apache::loncommon::content_type($r,'text/html');
  181:         $r->send_http_header;
  182:         return OK;
  183:     }
  184: 
  185:     my $context = 'domain';
  186:     my $dom = $env{'request.role.domain'};
  187:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  188:     if (&Apache::lonnet::allowed('mau',$dom)) {
  189:         &Apache::loncommon::content_type($r,'text/html');
  190:         $r->send_http_header;
  191:     } else {
  192:         $env{'user.error.msg'}=
  193:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  194:         return HTTP_NOT_ACCEPTABLE;
  195:     }
  196: 
  197:     $registered_cleanup=0;
  198:     @{$modified_urls}=();
  199: 
  200:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  201:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  202:                                             ['phase','actions']);
  203:     my $phase = 'pickactions';
  204:     if ( exists($env{'form.phase'}) ) {
  205:         $phase = $env{'form.phase'};
  206:     }
  207:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  208:     my %domconfig =
  209:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  210:                 'quotas','autoenroll','autoupdate','autocreate',
  211:                 'directorysrch','usercreation','usermodification',
  212:                 'contacts','defaults','scantron','coursecategories',
  213:                 'serverstatuses','requestcourses','helpsettings',
  214:                 'coursedefaults','usersessions','loadbalancing',
  215:                 'requestauthor'],$dom);
  216:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  217:                        'autoupdate','autocreate','directorysrch','contacts',
  218:                        'usercreation','usermodification','scantron',
  219:                        'requestcourses','requestauthor','coursecategories',
  220:                        'serverstatuses','helpsettings',
  221:                        'coursedefaults','usersessions');
  222:     if (keys(%servers) > 1) {
  223:         push(@prefs_order,'loadbalancing');
  224:     }
  225:     my %prefs = (
  226:         'rolecolors' =>
  227:                    { text => 'Default color schemes',
  228:                      help => 'Domain_Configuration_Color_Schemes',
  229:                      header => [{col1 => 'Student Settings',
  230:                                  col2 => '',},
  231:                                 {col1 => 'Coordinator Settings',
  232:                                  col2 => '',},
  233:                                 {col1 => 'Author Settings',
  234:                                  col2 => '',},
  235:                                 {col1 => 'Administrator Settings',
  236:                                  col2 => '',}],
  237:                     },
  238:         'login' =>
  239:                     { text => 'Log-in page options',
  240:                       help => 'Domain_Configuration_Login_Page',
  241:                       header => [{col1 => 'Item',
  242:                                   col2 => '',}],
  243:                     },
  244: 
  245:         'defaults' => 
  246:                     { text => 'Default authentication/language/timezone/portal',
  247:                       help => 'Domain_Configuration_LangTZAuth',
  248:                       header => [{col1 => 'Setting',
  249:                                   col2 => 'Value'}],
  250:                     },
  251:         'quotas' => 
  252:                     { text => 'Blogs, personal web pages, webDAV, portfolios',
  253:                       help => 'Domain_Configuration_Quotas',
  254:                       header => [{col1 => 'User affiliation',
  255:                                   col2 => 'Available tools',
  256:                                   col3 => 'Portfolio quota',}],
  257:                     },
  258:         'autoenroll' =>
  259:                    { text => 'Auto-enrollment settings',
  260:                      help => 'Domain_Configuration_Auto_Enrollment',
  261:                      header => [{col1 => 'Configuration setting',
  262:                                  col2 => 'Value(s)'}],
  263:                    },
  264:         'autoupdate' => 
  265:                    { text => 'Auto-update settings',
  266:                      help => 'Domain_Configuration_Auto_Updates',
  267:                      header => [{col1 => 'Setting',
  268:                                  col2 => 'Value',},
  269:                                 {col1 => 'Setting',
  270:                                  col2 => 'Affiliation'},
  271:                                 {col1 => 'User population',
  272:                                  col2 => 'Updateable user data'}],
  273:                   },
  274:         'autocreate' => 
  275:                   { text => 'Auto-course creation settings',
  276:                      help => 'Domain_Configuration_Auto_Creation',
  277:                      header => [{col1 => 'Configuration Setting',
  278:                                  col2 => 'Value',}],
  279:                   },
  280:         'directorysrch' => 
  281:                   { text => 'Institutional directory searches',
  282:                     help => 'Domain_Configuration_InstDirectory_Search',
  283:                     header => [{col1 => 'Setting',
  284:                                 col2 => 'Value',}],
  285:                   },
  286:         'contacts' =>
  287:                   { text => 'Contact Information',
  288:                     help => 'Domain_Configuration_Contact_Info',
  289:                     header => [{col1 => 'Setting',
  290:                                 col2 => 'Value',}],
  291:                   },
  292: 
  293:         'usercreation' => 
  294:                   { text => 'User creation',
  295:                     help => 'Domain_Configuration_User_Creation',
  296:                     header => [{col1 => 'Format rule type',
  297:                                 col2 => 'Format rules in force'},
  298:                                {col1 => 'User account creation',
  299:                                 col2 => 'Usernames which may be created',},
  300:                                {col1 => 'Context',
  301:                                 col2 => 'Assignable authentication types'}],
  302:                   },
  303:         'usermodification' =>
  304:                   { text => 'User modification',
  305:                     help => 'Domain_Configuration_User_Modification',
  306:                     header => [{col1 => 'Target user has role',
  307:                                 col2 => 'User information updateable in author context'},
  308:                                {col1 => 'Target user has role',
  309:                                 col2 => 'User information updateable in course context'},
  310:                                {col1 => "Status of user",
  311:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  312:                   },
  313:         'scantron' =>
  314:                   { text => 'Bubblesheet format file',
  315:                     help => 'Domain_Configuration_Scantron_Format',
  316:                     header => [ {col1 => 'Item',
  317:                                  col2 => '',
  318:                               }],
  319:                   },
  320:         'requestcourses' => 
  321:                  {text => 'Request creation of courses',
  322:                   help => 'Domain_Configuration_Request_Courses',
  323:                   header => [{col1 => 'User affiliation',
  324:                               col2 => 'Availability/Processing of requests',},
  325:                              {col1 => 'Setting',
  326:                               col2 => 'Value'}],
  327:                  },
  328:         'requestauthor' =>
  329:                  {text => 'Request authoring space',
  330:                   help => 'Domain_Configuration_Request_Author',
  331:                   header => [{col1 => 'User affiliation',
  332:                               col2 => 'Availability/Processing of requests',},
  333:                              {col1 => 'Setting',
  334:                               col2 => 'Value'}],
  335:                  },
  336:         'coursecategories' =>
  337:                   { text => 'Cataloging of courses/communities',
  338:                     help => 'Domain_Configuration_Cataloging_Courses',
  339:                     header => [{col1 => 'Category settings',
  340:                                 col2 => '',},
  341:                                {col1 => 'Categories',
  342:                                 col2 => '',
  343:                                }],
  344:                   },
  345:         'serverstatuses' =>
  346:                  {text   => 'Access to server status pages',
  347:                   help   => 'Domain_Configuration_Server_Status',
  348:                   header => [{col1 => 'Status Page',
  349:                               col2 => 'Other named users',
  350:                               col3 => 'Specific IPs',
  351:                             }],
  352:                  },
  353:         'helpsettings' =>
  354:                  {text   => 'Help page settings',
  355:                   help   => 'Domain_Configuration_Help_Settings',
  356:                   header => [{col1 => 'Authenticated Help Settings',
  357:                               col2 => ''},
  358:                              {col1 => 'Unauthenticated Help Settings',
  359:                               col2 => ''}],
  360:                  },
  361:         'coursedefaults' => 
  362:                  {text => 'Course/Community defaults',
  363:                   help => 'Domain_Configuration_Course_Defaults',
  364:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  365:                               col2 => 'Value',},
  366:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  367:                               col2 => 'Value',},],
  368:                  },
  369:         'privacy' => 
  370:                  {text   => 'User Privacy',
  371:                   help   => 'Domain_Configuration_User_Privacy',
  372:                   header => [{col1 => 'Setting',
  373:                               col2 => 'Value',}],
  374:                  },
  375:         'usersessions' =>
  376:                  {text  => 'User session hosting/offloading',
  377:                   help  => 'Domain_Configuration_User_Sessions',
  378:                   header => [{col1 => 'Domain server',
  379:                               col2 => 'Servers to offload sessions to when busy'},
  380:                              {col1 => 'Hosting of users from other domains',
  381:                               col2 => 'Rules'},
  382:                              {col1 => "Hosting domain's own users elsewhere",
  383:                               col2 => 'Rules'}],
  384:                  },
  385:          'loadbalancing' =>
  386:                  {text  => 'Dedicated Load Balancer',
  387:                   help  => 'Domain_Configuration_Load_Balancing',
  388:                   header => [{col1 => 'Server',
  389:                               col2 => 'Default destinations',
  390:                               col3 => 'User affliation',
  391:                               col4 => 'Overrides'},
  392:                             ],
  393:                  },
  394:     );
  395:     if (keys(%servers) > 1) {
  396:         $prefs{'login'}  = { text   => 'Log-in page options',
  397:                              help   => 'Domain_Configuration_Login_Page',
  398:                             header => [{col1 => 'Log-in Service',
  399:                                         col2 => 'Server Setting',},
  400:                                        {col1 => 'Log-in Page Items',
  401:                                         col2 => ''}],
  402:                            };
  403:     }
  404:     my @roles = ('student','coordinator','author','admin');
  405:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  406:     &Apache::lonhtmlcommon::add_breadcrumb
  407:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  408:       text=>"Settings to display/modify"});
  409:     my $confname = $dom.'-domainconfig';
  410:     if ($phase eq 'process') {
  411:         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
  412:     } elsif ($phase eq 'display') {
  413:         my $js = &recaptcha_js();
  414:         if (keys(%servers) > 1) {
  415:             my ($othertitle,$usertypes,$types) =
  416:                 &Apache::loncommon::sorted_inst_types($dom);
  417:             $js = &lonbalance_targets_js($dom,$types,\%servers).
  418:                   &new_spares_js().
  419:                   &common_domprefs_js().
  420:                   &Apache::loncommon::javascript_array_indexof();
  421:         }
  422:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  423:     } else {
  424:         if (keys(%domconfig) == 0) {
  425:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  426:             my @ids=&Apache::lonnet::current_machine_ids();
  427:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  428:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  429:                 my @loginimages = ('img','logo','domlogo','login');
  430:                 my $custom_img_count = 0;
  431:                 foreach my $img (@loginimages) {
  432:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  433:                         $custom_img_count ++;
  434:                     }
  435:                 }
  436:                 foreach my $role (@roles) {
  437:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  438:                         $custom_img_count ++;
  439:                     }
  440:                 }
  441:                 if ($custom_img_count > 0) {
  442:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  443:                     my $switch_server = &check_switchserver($dom,$confname);
  444:                     $r->print(
  445:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  446:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  447:     &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 />'.
  448:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  449:                     if ($switch_server) {
  450:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  451:                     }
  452:                     $r->print(&Apache::loncommon::end_page());
  453:                     return OK;
  454:                 }
  455:             }
  456:         }
  457:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  458:     }
  459:     return OK;
  460: }
  461: 
  462: sub process_changes {
  463:     my ($r,$dom,$confname,$action,$roles,$values) = @_;
  464:     my %domconfig;
  465:     if (ref($values) eq 'HASH') {
  466:         %domconfig = %{$values};
  467:     }
  468:     my $output;
  469:     if ($action eq 'login') {
  470:         $output = &modify_login($r,$dom,$confname,%domconfig);
  471:     } elsif ($action eq 'rolecolors') {
  472:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  473:                                      %domconfig);
  474:     } elsif ($action eq 'quotas') {
  475:         $output = &modify_quotas($dom,$action,%domconfig);
  476:     } elsif ($action eq 'autoenroll') {
  477:         $output = &modify_autoenroll($dom,%domconfig);
  478:     } elsif ($action eq 'autoupdate') {
  479:         $output = &modify_autoupdate($dom,%domconfig);
  480:     } elsif ($action eq 'autocreate') {
  481:         $output = &modify_autocreate($dom,%domconfig);
  482:     } elsif ($action eq 'directorysrch') {
  483:         $output = &modify_directorysrch($dom,%domconfig);
  484:     } elsif ($action eq 'usercreation') {
  485:         $output = &modify_usercreation($dom,%domconfig);
  486:     } elsif ($action eq 'usermodification') {
  487:         $output = &modify_usermodification($dom,%domconfig);
  488:     } elsif ($action eq 'contacts') {
  489:         $output = &modify_contacts($dom,%domconfig);
  490:     } elsif ($action eq 'defaults') {
  491:         $output = &modify_defaults($dom,$r);
  492:     } elsif ($action eq 'scantron') {
  493:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  494:     } elsif ($action eq 'coursecategories') {
  495:         $output = &modify_coursecategories($dom,%domconfig);
  496:     } elsif ($action eq 'serverstatuses') {
  497:         $output = &modify_serverstatuses($dom,%domconfig);
  498:     } elsif ($action eq 'requestcourses') {
  499:         $output = &modify_quotas($dom,$action,%domconfig);
  500:     } elsif ($action eq 'requestauthor') {
  501:         $output = &modify_quotas($dom,$action,%domconfig);
  502:     } elsif ($action eq 'helpsettings') {
  503:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  504:     } elsif ($action eq 'coursedefaults') {
  505:         $output = &modify_coursedefaults($dom,%domconfig);
  506:     } elsif ($action eq 'usersessions') {
  507:         $output = &modify_usersessions($dom,%domconfig);
  508:     } elsif ($action eq 'loadbalancing') {
  509:         $output = &modify_loadbalancing($dom,%domconfig);
  510:     }
  511:     return $output;
  512: }
  513: 
  514: sub print_config_box {
  515:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  516:     my $rowtotal = 0;
  517:     my $output;
  518:     if ($action eq 'coursecategories') {
  519:         $output = &coursecategories_javascript($settings);
  520:     }
  521:     $output .= 
  522:          '<table class="LC_nested_outer">
  523:           <tr>
  524:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  525:            &mt($item->{text}).'&nbsp;'.
  526:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  527:           '</tr>';
  528:     $rowtotal ++;
  529:     my $numheaders = 1;
  530:     if (ref($item->{'header'}) eq 'ARRAY') {
  531:         $numheaders = scalar(@{$item->{'header'}});
  532:     }
  533:     if ($numheaders > 1) {
  534:         my $colspan = '';
  535:         my $rightcolspan = '';
  536:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') || ($action eq 'helpsettings')) {
  537:             $colspan = ' colspan="2"';
  538:         }
  539:         if ($action eq 'usersessions') {
  540:             $rightcolspan = ' colspan="3"'; 
  541:         }
  542:         $output .= '
  543:           <tr>
  544:            <td>
  545:             <table class="LC_nested">
  546:              <tr class="LC_info_row">
  547:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  548:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  549:              </tr>';
  550:         $rowtotal ++;
  551:         if ($action eq 'autoupdate') {
  552:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  553:         } elsif ($action eq 'usercreation') {
  554:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  555:         } elsif ($action eq 'usermodification') {
  556:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  557:         } elsif ($action eq 'coursecategories') {
  558:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  559:         } elsif ($action eq 'login') {
  560:             $output .= &print_login('top',$dom,$confname,$phase,$settings,\$rowtotal);
  561:             $colspan = ' colspan="2"';
  562:         } elsif ($action eq 'requestcourses') {
  563:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  564:         } elsif ($action eq 'requestauthor') {
  565:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  566:         } elsif ($action eq 'helpsettings') {
  567:             $output .= &print_helpsettings('top',$dom,$confname,$settings,\$rowtotal);
  568:         } elsif ($action eq 'usersessions') {
  569:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  570:         } elsif ($action eq 'rolecolors') {
  571:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  572:         } elsif ($action eq 'coursedefaults') {
  573:             $output .= &print_coursedefaults('top',$dom,$settings,\$rowtotal);
  574:         }
  575:         $output .= '
  576:            </table>
  577:           </td>
  578:          </tr>
  579:          <tr>
  580:            <td>
  581:             <table class="LC_nested">
  582:              <tr class="LC_info_row">
  583:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  584:         $output .= '
  585:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  586:              </tr>';
  587:             $rowtotal ++;
  588:         if ($action eq 'autoupdate') {
  589:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  590:            </table>
  591:           </td>
  592:          </tr>
  593:          <tr>
  594:            <td>
  595:             <table class="LC_nested">
  596:              <tr class="LC_info_row">
  597:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  598:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  599:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  600:             $rowtotal ++;
  601:         } elsif ($action eq 'usercreation') {
  602:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  603:            </table>
  604:           </td>
  605:          </tr>
  606:          <tr>
  607:            <td>
  608:             <table class="LC_nested">
  609:              <tr class="LC_info_row">
  610:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  611:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  612:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  613:             $rowtotal ++;
  614:         } elsif ($action eq 'usermodification') {
  615:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  616:            </table>
  617:           </td>
  618:          </tr>
  619:          <tr>
  620:            <td>
  621:             <table class="LC_nested">
  622:              <tr class="LC_info_row">
  623:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  624:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  625:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  626:             $rowtotal ++;
  627:         } elsif ($action eq 'coursecategories') {
  628:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  629:         } elsif ($action eq 'login') {
  630:             $output .= &print_login('bottom',$dom,$confname,$phase,$settings,\$rowtotal);
  631:         } elsif ($action eq 'requestcourses') {
  632:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  633:         } elsif ($action eq 'requestauthor') {
  634:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  635:         } elsif ($action eq 'helpsettings') {
  636:             $output .= &print_helpsettings('bottom',$dom,$confname,$settings,\$rowtotal);
  637:         } elsif ($action eq 'usersessions') {
  638:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  639:            </table>
  640:           </td>
  641:          </tr>
  642:          <tr>
  643:            <td>
  644:             <table class="LC_nested">
  645:              <tr class="LC_info_row">
  646:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  647:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  648:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  649:             $rowtotal ++;
  650:         } elsif ($action eq 'coursedefaults') {
  651:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  652:         } elsif ($action eq 'rolecolors') {
  653:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  654:            </table>
  655:           </td>
  656:          </tr>
  657:          <tr>
  658:            <td>
  659:             <table class="LC_nested">
  660:              <tr class="LC_info_row">
  661:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  662:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  663:               <td class="LC_right_item" valign="top">'.
  664:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  665:              </tr>'.
  666:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  667:            </table>
  668:           </td>
  669:          </tr>
  670:          <tr>
  671:            <td>
  672:             <table class="LC_nested">
  673:              <tr class="LC_info_row">
  674:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  675:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  676:              </tr>'.
  677:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  678:             $rowtotal += 2;
  679:         }
  680:     } else {
  681:         $output .= '
  682:           <tr>
  683:            <td>
  684:             <table class="LC_nested">
  685:              <tr class="LC_info_row">';
  686:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  687:             $output .= '  
  688:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  689:         } elsif ($action eq 'serverstatuses') {
  690:             $output .= '
  691:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  692:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  693: 
  694:         } else {
  695:             $output .= '
  696:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  697:         }
  698:         if (defined($item->{'header'}->[0]->{'col3'})) {
  699:             $output .= '<td class="LC_left_item" valign="top">'.
  700:                        &mt($item->{'header'}->[0]->{'col2'});
  701:             if ($action eq 'serverstatuses') {
  702:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  703:             } 
  704:         } else {
  705:             $output .= '<td class="LC_right_item" valign="top">'.
  706:                        &mt($item->{'header'}->[0]->{'col2'});
  707:         }
  708:         $output .= '</td>';
  709:         if ($item->{'header'}->[0]->{'col3'}) {
  710:             if (defined($item->{'header'}->[0]->{'col4'})) {
  711:                 $output .= '<td class="LC_left_item" valign="top">'.
  712:                             &mt($item->{'header'}->[0]->{'col3'});
  713:             } else {
  714:                 $output .= '<td class="LC_right_item" valign="top">'.
  715:                            &mt($item->{'header'}->[0]->{'col3'});
  716:             }
  717:             if ($action eq 'serverstatuses') {
  718:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  719:             }
  720:             $output .= '</td>';
  721:         }
  722:         if ($item->{'header'}->[0]->{'col4'}) {
  723:             $output .= '<td class="LC_right_item" valign="top">'.
  724:                        &mt($item->{'header'}->[0]->{'col4'});
  725:         }
  726:         $output .= '</tr>';
  727:         $rowtotal ++;
  728:         if ($action eq 'login') {
  729:             $output .= &print_login('bottom',$dom,$confname,$phase,$settings,
  730:                                     \$rowtotal);
  731:         } elsif ($action eq 'quotas') {
  732:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  733:         } elsif ($action eq 'autoenroll') {
  734:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  735:         } elsif ($action eq 'autocreate') {
  736:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  737:         } elsif ($action eq 'directorysrch') {
  738:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  739:         } elsif ($action eq 'contacts') {
  740:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  741:         } elsif ($action eq 'defaults') {
  742:             $output .= &print_defaults($dom,\$rowtotal);
  743:         } elsif ($action eq 'scantron') {
  744:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  745:         } elsif ($action eq 'serverstatuses') {
  746:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  747:         } elsif ($action eq 'helpsettings') {
  748:             $output .= &print_helpsettings('top',$dom,$confname,$settings,\$rowtotal);
  749:         } elsif ($action eq 'loadbalancing') {
  750:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  751:         }
  752:     }
  753:     $output .= '
  754:    </table>
  755:   </td>
  756:  </tr>
  757: </table><br />';
  758:     return ($output,$rowtotal);
  759: }
  760: 
  761: sub print_login {
  762:     my ($position,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  763:     my ($css_class,$datatable);
  764:     my %choices = &login_choices();
  765: 
  766:     if ($position eq 'top') {
  767:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  768:         my $choice = $choices{'disallowlogin'};
  769:         $css_class = ' class="LC_odd_row"';
  770:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  771:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  772:                       '<th>'.$choices{'server'}.'</th>'.
  773:                       '<th>'.$choices{'serverpath'}.'</th>'.
  774:                       '<th>'.$choices{'custompath'}.'</th>'.
  775:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  776:         my %disallowed;
  777:         if (ref($settings) eq 'HASH') {
  778:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  779:                %disallowed = %{$settings->{'loginvia'}};
  780:             }
  781:         }
  782:         foreach my $lonhost (sort(keys(%servers))) {
  783:             my $direct = 'selected="selected"';
  784:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  785:                 if ($disallowed{$lonhost}{'server'} ne '') {
  786:                     $direct = '';
  787:                 }
  788:             }
  789:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  790:                           '<td><select name="'.$lonhost.'_server">'.
  791:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  792:                           '</option>';
  793:             foreach my $hostid (keys(%servers)) {
  794:                 next if ($servers{$hostid} eq $servers{$lonhost});
  795:                 my $selected = '';
  796:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  797:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  798:                         $selected = 'selected="selected"';
  799:                     }
  800:                 }
  801:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  802:                               $servers{$hostid}.'</option>';
  803:             }
  804:             $datatable .= '</select></td>'.
  805:                           '<td><select name="'.$lonhost.'_serverpath">';
  806:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  807:                 my $pathname = $path;
  808:                 if ($path eq 'custom') {
  809:                     $pathname = &mt('Custom Path').' ->';
  810:                 }
  811:                 my $selected = '';
  812:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  813:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  814:                         $selected = 'selected="selected"';
  815:                     }
  816:                 } elsif ($path eq '') {
  817:                     $selected = 'selected="selected"';
  818:                 }
  819:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  820:             }
  821:             $datatable .= '</select></td>';
  822:             my ($custom,$exempt);
  823:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  824:                 $custom = $disallowed{$lonhost}{'custompath'};
  825:                 $exempt = $disallowed{$lonhost}{'exempt'};
  826:             }
  827:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  828:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  829:                           '</tr>';
  830:         }
  831:         $datatable .= '</table></td></tr>';
  832:         return $datatable;
  833:     }
  834: 
  835:     my %defaultchecked = ( 
  836:                            'coursecatalog' => 'on',
  837:                            'adminmail'     => 'off',
  838:                            'newuser'       => 'off',
  839:                          );
  840:     my @toggles = ('coursecatalog','adminmail','newuser');
  841:     my (%checkedon,%checkedoff);
  842:     foreach my $item (@toggles) {
  843:         if ($defaultchecked{$item} eq 'on') { 
  844:             $checkedon{$item} = ' checked="checked" ';
  845:             $checkedoff{$item} = ' ';
  846:         } elsif ($defaultchecked{$item} eq 'off') {
  847:             $checkedoff{$item} = ' checked="checked" ';
  848:             $checkedon{$item} = ' ';
  849:         }
  850:     }
  851:     my @images = ('img','logo','domlogo','login');
  852:     my @logintext = ('textcol','bgcol');
  853:     my @bgs = ('pgbg','mainbg','sidebg');
  854:     my @links = ('link','alink','vlink');
  855:     my %designhash = &Apache::loncommon::get_domainconf($dom);
  856:     my %defaultdesign = %Apache::loncommon::defaultdesign;
  857:     my (%is_custom,%designs);
  858:     my %defaults = (
  859:                    font => $defaultdesign{'login.font'},
  860:                    );
  861:     foreach my $item (@images) {
  862:         $defaults{$item} = $defaultdesign{'login.'.$item};
  863:         $defaults{'showlogo'}{$item} = 1;
  864:     }
  865:     foreach my $item (@bgs) {
  866:         $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  867:     }
  868:     foreach my $item (@logintext) {
  869:         $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  870:     }
  871:     foreach my $item (@links) {
  872:         $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  873:     }
  874:     if (ref($settings) eq 'HASH') {
  875:         foreach my $item (@toggles) {
  876:             if ($settings->{$item} eq '1') {
  877:                 $checkedon{$item} =  ' checked="checked" ';
  878:                 $checkedoff{$item} = ' ';
  879:             } elsif ($settings->{$item} eq '0') {
  880:                 $checkedoff{$item} =  ' checked="checked" ';
  881:                 $checkedon{$item} = ' ';
  882:             }
  883:         }
  884:         foreach my $item (@images) {
  885:             if (defined($settings->{$item})) {
  886:                 $designs{$item} = $settings->{$item};
  887:                 $is_custom{$item} = 1;
  888:             }
  889:             if (defined($settings->{'showlogo'}{$item})) {
  890:                 $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  891:             }
  892:         }
  893:         foreach my $item (@logintext) {
  894:             if ($settings->{$item} ne '') {
  895:                 $designs{'logintext'}{$item} = $settings->{$item};
  896:                 $is_custom{$item} = 1;
  897:             }
  898:         }
  899:         if ($settings->{'font'} ne '') {
  900:             $designs{'font'} = $settings->{'font'};
  901:             $is_custom{'font'} = 1;
  902:         }
  903:         foreach my $item (@bgs) {
  904:             if ($settings->{$item} ne '') {
  905:                 $designs{'bgs'}{$item} = $settings->{$item};
  906:                 $is_custom{$item} = 1;
  907:             }
  908:         }
  909:         foreach my $item (@links) {
  910:             if ($settings->{$item} ne '') {
  911:                 $designs{'links'}{$item} = $settings->{$item};
  912:                 $is_custom{$item} = 1;
  913:             }
  914:         }
  915:     } else {
  916:         if ($designhash{$dom.'.login.font'} ne '') {
  917:             $designs{'font'} = $designhash{$dom.'.login.font'};
  918:             $is_custom{'font'} = 1;
  919:         }
  920:         foreach my $item (@images) {
  921:             if ($designhash{$dom.'.login.'.$item} ne '') {
  922:                 $designs{$item} = $designhash{$dom.'.login.'.$item};
  923:                 $is_custom{$item} = 1;
  924:             }
  925:         }
  926:         foreach my $item (@bgs) {
  927:             if ($designhash{$dom.'.login.'.$item} ne '') {
  928:                 $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  929:                 $is_custom{$item} = 1;
  930:             }
  931:         }
  932:         foreach my $item (@links) {
  933:             if ($designhash{$dom.'.login.'.$item} ne '') {
  934:                 $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  935:                 $is_custom{$item} = 1;
  936:             }
  937:         }
  938:     }
  939:     my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  940:                                                   logo => 'Institution Logo',
  941:                                                   domlogo => 'Domain Logo',
  942:                                                   login => 'Login box');
  943:     my $itemcount = 1;
  944:     foreach my $item (@toggles) {
  945:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
  946:         $datatable .=  
  947:             '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  948:             '</td><td>'.
  949:             '<span class="LC_nobreak"><label><input type="radio" name="'.
  950:             $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
  951:             '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
  952:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
  953:             '</tr>';
  954:         $itemcount ++;
  955:     }
  956:     $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
  957:     $datatable .= '</tr></table></td></tr>';
  958:     return $datatable;
  959: }
  960: 
  961: sub login_choices {
  962:     my %choices =
  963:         &Apache::lonlocal::texthash (
  964:             coursecatalog => 'Display Course/Community Catalog link?',
  965:             adminmail     => "Display Administrator's E-mail Address?",
  966:             disallowlogin => "Login page requests redirected",
  967:             hostid        => "Server",
  968:             server        => "Redirect to:",
  969:             serverpath    => "Path",
  970:             custompath    => "Custom", 
  971:             exempt        => "Exempt IP(s)",
  972:             directlogin   => "No redirect",
  973:             newuser       => "Link to create a user account",
  974:             img           => "Header",
  975:             logo          => "Main Logo",
  976:             domlogo       => "Domain Logo",
  977:             login         => "Log-in Header", 
  978:             textcol       => "Text color",
  979:             bgcol         => "Box color",
  980:             bgs           => "Background colors",
  981:             links         => "Link colors",
  982:             font          => "Font color",
  983:             pgbg          => "Header",
  984:             mainbg        => "Page",
  985:             sidebg        => "Login box",
  986:             link          => "Link",
  987:             alink         => "Active link",
  988:             vlink         => "Visited link",
  989:         );
  990:     return %choices;
  991: }
  992: 
  993: sub print_rolecolors {
  994:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
  995:     my %choices = &color_font_choices();
  996:     my @bgs = ('pgbg','tabbg','sidebg');
  997:     my @links = ('link','alink','vlink');
  998:     my @images = ('img');
  999:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1000:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1001:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1002:     my (%is_custom,%designs);
 1003:     my %defaults = (
 1004:                    img => $defaultdesign{$role.'.img'},
 1005:                    font => $defaultdesign{$role.'.font'},
 1006: 		   fontmenu => $defaultdesign{$role.'.fontmenu'},
 1007:                    );
 1008:     foreach my $item (@bgs) {
 1009:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1010:     }
 1011:     foreach my $item (@links) {
 1012:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1013:     }
 1014:     if (ref($settings) eq 'HASH') {
 1015:         if (ref($settings->{$role}) eq 'HASH') {
 1016:             if ($settings->{$role}->{'img'} ne '') {
 1017:                 $designs{'img'} = $settings->{$role}->{'img'};
 1018:                 $is_custom{'img'} = 1;
 1019:             }
 1020:             if ($settings->{$role}->{'font'} ne '') {
 1021:                 $designs{'font'} = $settings->{$role}->{'font'};
 1022:                 $is_custom{'font'} = 1;
 1023:             }
 1024:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1025:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1026:                 $is_custom{'fontmenu'} = 1;
 1027:             }
 1028:             foreach my $item (@bgs) {
 1029:                 if ($settings->{$role}->{$item} ne '') {
 1030:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1031:                     $is_custom{$item} = 1;
 1032:                 }
 1033:             }
 1034:             foreach my $item (@links) {
 1035:                 if ($settings->{$role}->{$item} ne '') {
 1036:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1037:                     $is_custom{$item} = 1;
 1038:                 }
 1039:             }
 1040:         }
 1041:     } else {
 1042:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1043:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1044:             $is_custom{'img'} = 1;
 1045:         }
 1046:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1047:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1048:             $is_custom{'fontmenu'} = 1; 
 1049:         }
 1050:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1051:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1052:             $is_custom{'font'} = 1;
 1053:         }
 1054:         foreach my $item (@bgs) {
 1055:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1056:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1057:                 $is_custom{$item} = 1;
 1058:             
 1059:             }
 1060:         }
 1061:         foreach my $item (@links) {
 1062:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1063:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1064:                 $is_custom{$item} = 1;
 1065:             }
 1066:         }
 1067:     }
 1068:     my $itemcount = 1;
 1069:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1070:     $datatable .= '</tr></table></td></tr>';
 1071:     return $datatable;
 1072: }
 1073: 
 1074: sub display_color_options {
 1075:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1076:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1077:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1078:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1079:     my $datatable = '<tr'.$css_class.'>'.
 1080:         '<td>'.$choices->{'font'}.'</td>';
 1081:     if (!$is_custom->{'font'}) {
 1082:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1083:     } else {
 1084:         $datatable .= '<td>&nbsp;</td>';
 1085:     }
 1086:     my $fontlink = &color_pick($phase,$role,'font',$choices->{'font'},$designs->{'font'});
 1087:     $datatable .= '<td><span class="LC_nobreak">'.
 1088:                   '<input type="text" size="10" name="'.$role.'_font"'.
 1089:                   ' value="'.$designs->{'font'}.'" />&nbsp;'.$fontlink.
 1090:                   '&nbsp;<span id="css_'.$role.'_font" style="background-color: '.
 1091:                   $designs->{'font'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1092:                   '</span></td></tr>';
 1093:     unless ($role eq 'login') { 
 1094:         $datatable .= '<tr'.$css_class.'>'.
 1095:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1096:         if (!$is_custom->{'fontmenu'}) {
 1097:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1098:         } else {
 1099:             $datatable .= '<td>&nbsp;</td>';
 1100:         }
 1101:         $fontlink = &color_pick($phase,$role,'fontmenu',$choices->{'fontmenu'},$designs->{'fontmenu'});
 1102:         $datatable .= '<td><span class="LC_nobreak">'.
 1103:                       '<input type="text" size="10" name="'.$role.'_fontmenu"'.
 1104:                       ' value="'.$designs->{'fontmenu'}.'" />&nbsp;'.$fontlink.
 1105:                       '&nbsp;<span id="css_'.$role.'_fontmenu" style="background-color: '.
 1106:                       $designs->{'fontmenu'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1107:                       '</span></td></tr>';
 1108:     }
 1109:     my $switchserver = &check_switchserver($dom,$confname);
 1110:     foreach my $img (@{$images}) {
 1111: 	$itemcount ++;
 1112:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1113:         $datatable .= '<tr'.$css_class.'>'.
 1114:                       '<td>'.$choices->{$img};
 1115:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1116:         if ($role eq 'login') {
 1117:             if ($img eq 'login') {
 1118:                 $login_hdr_pick =
 1119:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1120:                 $logincolors =
 1121:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1122:                                             $designs);
 1123:             } elsif ($img ne 'domlogo') {
 1124:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1125:             }
 1126:         }
 1127:         $datatable .= '</td>';
 1128:         if ($designs->{$img} ne '') {
 1129:             $imgfile = $designs->{$img};
 1130: 	    $img_import = ($imgfile =~ m{^/adm/});
 1131:         } else {
 1132:             $imgfile = $defaults->{$img};
 1133:         }
 1134:         if ($imgfile) {
 1135:             my ($showfile,$fullsize);
 1136:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1137:                 my $urldir = $1;
 1138:                 my $filename = $2;
 1139:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1140:                 if (@info) {
 1141:                     my $thumbfile = 'tn-'.$filename;
 1142:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1143:                     if (@thumb) {
 1144:                         $showfile = $urldir.'/'.$thumbfile;
 1145:                     } else {
 1146:                         $showfile = $imgfile;
 1147:                     }
 1148:                 } else {
 1149:                     $showfile = '';
 1150:                 }
 1151:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1152:                 $showfile = $imgfile;
 1153:                 my $imgdir = $1;
 1154:                 my $filename = $2;
 1155:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1156:                     $showfile = "/$imgdir/tn-".$filename;
 1157:                 } else {
 1158:                     my $input = $londocroot.$imgfile;
 1159:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1160:                     if (!-e $output) {
 1161:                         my ($width,$height) = &thumb_dimensions();
 1162:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1163:                         if ($fullwidth ne '' && $fullheight ne '') {
 1164:                             if ($fullwidth > $width && $fullheight > $height) { 
 1165:                                 my $size = $width.'x'.$height;
 1166:                                 system("convert -sample $size $input $output");
 1167:                                 $showfile = "/$imgdir/tn-".$filename;
 1168:                             }
 1169:                         }
 1170:                     }
 1171:                 }
 1172:             }
 1173:             if ($showfile) {
 1174:                 if ($showfile =~ m{^/(adm|res)/}) {
 1175:                     if ($showfile =~ m{^/res/}) {
 1176:                         my $local_showfile =
 1177:                             &Apache::lonnet::filelocation('',$showfile);
 1178:                         &Apache::lonnet::repcopy($local_showfile);
 1179:                     }
 1180:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1181:                 }
 1182:                 if ($imgfile) {
 1183:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1184:                         if ($imgfile =~ m{^/res/}) {
 1185:                             my $local_imgfile =
 1186:                                 &Apache::lonnet::filelocation('',$imgfile);
 1187:                             &Apache::lonnet::repcopy($local_imgfile);
 1188:                         }
 1189:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1190:                     } else {
 1191:                         $fullsize = $imgfile;
 1192:                     }
 1193:                 }
 1194:                 $datatable .= '<td>';
 1195:                 if ($img eq 'login') {
 1196:                     $datatable .= $login_hdr_pick;
 1197:                 } 
 1198:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1199:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1200:             } else {
 1201:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1202:                               &mt('Upload:');
 1203:             }
 1204:         } else {
 1205:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1206:                           &mt('Upload:');
 1207:         }
 1208:         if ($switchserver) {
 1209:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1210:         } else {
 1211:             if ($img ne 'login') { # suppress file selection for Log-in header
 1212:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1213:             }
 1214:         }
 1215:         $datatable .= '</td></tr>';
 1216:     }
 1217:     $itemcount ++;
 1218:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1219:     $datatable .= '<tr'.$css_class.'>'.
 1220:                   '<td>'.$choices->{'bgs'}.'</td>';
 1221:     my $bgs_def;
 1222:     foreach my $item (@{$bgs}) {
 1223:         if (!$is_custom->{$item}) {
 1224:             $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>';
 1225:         }
 1226:     }
 1227:     if ($bgs_def) {
 1228:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1229:     } else {
 1230:         $datatable .= '<td>&nbsp;</td>';
 1231:     }
 1232:     $datatable .= '<td class="LC_right_item">'.
 1233:                   '<table border="0"><tr>';
 1234:     foreach my $item (@{$bgs}) {
 1235:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'bgs'}{$item});
 1236:         $datatable .= '<td align="center">'.$link;
 1237:         if ($designs->{'bgs'}{$item}) {
 1238:             $datatable .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1239:         }
 1240:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'bgs'}{$item}.
 1241:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1242:     }
 1243:     $datatable .= '</tr></table></td></tr>';
 1244:     $itemcount ++;
 1245:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1246:     $datatable .= '<tr'.$css_class.'>'.
 1247:                   '<td>'.$choices->{'links'}.'</td>';
 1248:     my $links_def;
 1249:     foreach my $item (@{$links}) {
 1250:         if (!$is_custom->{$item}) {
 1251:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1252:         }
 1253:     }
 1254:     if ($links_def) {
 1255:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1256:     } else {
 1257:         $datatable .= '<td>&nbsp;</td>';
 1258:     }
 1259:     $datatable .= '<td class="LC_right_item">'.
 1260:                   '<table border="0"><tr>';
 1261:     foreach my $item (@{$links}) {
 1262:         $datatable .= '<td align="center">'."\n".
 1263:                       &color_pick($phase,$role,$item,$choices->{$item},
 1264:                                   $designs->{'links'}{$item});
 1265:         if ($designs->{'links'}{$item}) {
 1266:             $datatable.='&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'links'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1267:         }
 1268:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'links'}{$item}.
 1269:                       '" /></td>';
 1270:     }
 1271:     $$rowtotal += $itemcount;
 1272:     return $datatable;
 1273: }
 1274: 
 1275: sub logo_display_options {
 1276:     my ($img,$defaults,$designs) = @_;
 1277:     my $checkedon;
 1278:     if (ref($defaults) eq 'HASH') {
 1279:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1280:             if ($defaults->{'showlogo'}{$img}) {
 1281:                 $checkedon = 'checked="checked" ';     
 1282:             }
 1283:         } 
 1284:     }
 1285:     if (ref($designs) eq 'HASH') {
 1286:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1287:             if (defined($designs->{'showlogo'}{$img})) {
 1288:                 if ($designs->{'showlogo'}{$img} == 0) {
 1289:                     $checkedon = '';
 1290:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1291:                     $checkedon = 'checked="checked" ';
 1292:                 }
 1293:             }
 1294:         }
 1295:     }
 1296:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1297:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1298:            &mt('show').'</label>'."\n";
 1299: }
 1300: 
 1301: sub login_header_options  {
 1302:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1303:     my $output = '';
 1304:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1305:         $output .= &mt('Text default(s):').'<br />';
 1306:         if (!$is_custom->{'textcol'}) {
 1307:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1308:                        '&nbsp;&nbsp;&nbsp;';
 1309:         }
 1310:         if (!$is_custom->{'bgcol'}) {
 1311:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1312:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1313:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1314:         }
 1315:         $output .= '<br />';
 1316:     }
 1317:     $output .='<br />';
 1318:     return $output;
 1319: }
 1320: 
 1321: sub login_text_colors {
 1322:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1323:     my $color_menu = '<table border="0"><tr>';
 1324:     foreach my $item (@{$logintext}) {
 1325:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1326:         $color_menu .= '<td align="center">'.$link;
 1327:         if ($designs->{'logintext'}{$item}) {
 1328:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1329:         }
 1330:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1331:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1332:                        '<td>&nbsp;</td>';
 1333:     }
 1334:     $color_menu .= '</tr></table><br />';
 1335:     return $color_menu;
 1336: }
 1337: 
 1338: sub image_changes {
 1339:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1340:     my $output;
 1341:     if ($img eq 'login') {
 1342:             # suppress image for Log-in header
 1343:     } elsif (!$is_custom) {
 1344:         if ($img ne 'domlogo') {
 1345:             $output .= &mt('Default image:').'<br />';
 1346:         } else {
 1347:             $output .= &mt('Default in use:').'<br />';
 1348:         }
 1349:     }
 1350:     if ($img eq 'login') { # suppress image for Log-in header
 1351:         $output .= '<td>'.$logincolors;
 1352:     } else {
 1353:         if ($img_import) {
 1354:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1355:         }
 1356:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1357:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1358:         if ($is_custom) {
 1359:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1360:                        '<input type="checkbox" name="'.
 1361:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1362:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1363:         } else {
 1364:             $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1365:         }
 1366:     }
 1367:     return $output;
 1368: }
 1369: 
 1370: sub color_pick {
 1371:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1372:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1373:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1374:                ');">'.$desc.'</a>';
 1375:     return $link;
 1376: }
 1377: 
 1378: sub print_quotas {
 1379:     my ($dom,$settings,$rowtotal,$action) = @_;
 1380:     my $context;
 1381:     if ($action eq 'quotas') {
 1382:         $context = 'tools';
 1383:     } else {
 1384:         $context = $action;
 1385:     }
 1386:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 1387:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1388:     my $typecount = 0;
 1389:     my ($css_class,%titles);
 1390:     if ($context eq 'requestcourses') {
 1391:         @usertools = ('official','unofficial','community');
 1392:         @options =('norequest','approval','validate','autolimit');
 1393:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1394:         %titles = &courserequest_titles();
 1395:     } elsif ($context eq 'requestauthor') {
 1396:         @usertools = ('author');
 1397:         @options = ('norequest','approval','automatic');
 1398:         %titles = &authorrequest_titles(); 
 1399:     } else {
 1400:         @usertools = ('aboutme','blog','webdav','portfolio');
 1401:         %titles = &tool_titles();
 1402:     }
 1403:     if (ref($types) eq 'ARRAY') {
 1404:         foreach my $type (@{$types}) {
 1405:             my $currdefquota;
 1406:             unless (($context eq 'requestcourses') ||
 1407:                     ($context eq 'requestauthor')) {
 1408:                 if (ref($settings) eq 'HASH') {
 1409:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1410:                         $currdefquota = $settings->{defaultquota}->{$type}; 
 1411:                     } else {
 1412:                         $currdefquota = $settings->{$type};
 1413:                     }
 1414:                 }
 1415:             }
 1416:             if (defined($usertypes->{$type})) {
 1417:                 $typecount ++;
 1418:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1419:                 $datatable .= '<tr'.$css_class.'>'.
 1420:                               '<td>'.$usertypes->{$type}.'</td>'.
 1421:                               '<td class="LC_left_item">';
 1422:                 if ($context eq 'requestcourses') {
 1423:                     $datatable .= '<table><tr>';
 1424:                 }
 1425:                 my %cell;  
 1426:                 foreach my $item (@usertools) {
 1427:                     if ($context eq 'requestcourses') {
 1428:                         my ($curroption,$currlimit);
 1429:                         if (ref($settings) eq 'HASH') {
 1430:                             if (ref($settings->{$item}) eq 'HASH') {
 1431:                                 $curroption = $settings->{$item}->{$type};
 1432:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1433:                                     $currlimit = $1; 
 1434:                                 }
 1435:                             }
 1436:                         }
 1437:                         if (!$curroption) {
 1438:                             $curroption = 'norequest';
 1439:                         }
 1440:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1441:                         foreach my $option (@options) {
 1442:                             my $val = $option;
 1443:                             if ($option eq 'norequest') {
 1444:                                 $val = 0;  
 1445:                             }
 1446:                             if ($option eq 'validate') {
 1447:                                 my $canvalidate = 0;
 1448:                                 if (ref($validations{$item}) eq 'HASH') { 
 1449:                                     if ($validations{$item}{$type}) {
 1450:                                         $canvalidate = 1;
 1451:                                     }
 1452:                                 }
 1453:                                 next if (!$canvalidate);
 1454:                             }
 1455:                             my $checked = '';
 1456:                             if ($option eq $curroption) {
 1457:                                 $checked = ' checked="checked"';
 1458:                             } elsif ($option eq 'autolimit') {
 1459:                                 if ($curroption =~ /^autolimit/) {
 1460:                                     $checked = ' checked="checked"';
 1461:                                 }                       
 1462:                             } 
 1463:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1464:                                   '<input type="radio" name="crsreq_'.$item.
 1465:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1466:                                   $titles{$option}.'</label>';
 1467:                             if ($option eq 'autolimit') {
 1468:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1469:                                                 $item.'_limit_'.$type.'" size="1" '.
 1470:                                                 'value="'.$currlimit.'" />';
 1471:                             }
 1472:                             $cell{$item} .= '</span> ';
 1473:                             if ($option eq 'autolimit') {
 1474:                                 $cell{$item} .= $titles{'unlimited'};
 1475:                             }
 1476:                         }
 1477:                     } elsif ($context eq 'requestauthor') {
 1478:                         my $curroption;
 1479:                         if (ref($settings) eq 'HASH') {
 1480:                             $curroption = $settings->{$type};
 1481:                         }
 1482:                         if (!$curroption) {
 1483:                             $curroption = 'norequest';
 1484:                         }
 1485:                         foreach my $option (@options) {
 1486:                             my $val = $option;
 1487:                             if ($option eq 'norequest') {
 1488:                                 $val = 0;
 1489:                             }
 1490:                             my $checked = '';
 1491:                             if ($option eq $curroption) {
 1492:                                 $checked = ' checked="checked"';
 1493:                             }
 1494:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1495:                                   '<input type="radio" name="authorreq_'.$type.
 1496:                                   '" value="'.$val.'"'.$checked.' />'.
 1497:                                   $titles{$option}.'</label></span>&nbsp; ';
 1498:                         }
 1499:                     } else {
 1500:                         my $checked = 'checked="checked" ';
 1501:                         if (ref($settings) eq 'HASH') {
 1502:                             if (ref($settings->{$item}) eq 'HASH') {
 1503:                                 if ($settings->{$item}->{$type} == 0) {
 1504:                                     $checked = '';
 1505:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1506:                                     $checked =  'checked="checked" ';
 1507:                                 }
 1508:                             }
 1509:                         }
 1510:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1511:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1512:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1513:                                       '</label></span>&nbsp; ';
 1514:                     }
 1515:                 }
 1516:                 if ($context eq 'requestcourses') {
 1517:                     $datatable .= '</tr><tr>';
 1518:                     foreach my $item (@usertools) {
 1519:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1520:                     }
 1521:                     $datatable .= '</tr></table>';
 1522:                 }
 1523:                 $datatable .= '</td>';
 1524:                 unless (($context eq 'requestcourses') ||
 1525:                         ($context eq 'requestauthor')) {
 1526:                     $datatable .= 
 1527:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1528:                               '<input type="text" name="quota_'.$type.
 1529:                               '" value="'.$currdefquota.
 1530:                               '" size="5" /> Mb</span></td>';
 1531:                 }
 1532:                 $datatable .= '</tr>';
 1533:             }
 1534:         }
 1535:     }
 1536:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1537:         $defaultquota = '20';
 1538:         if (ref($settings) eq 'HASH') {
 1539:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1540:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1541:             } elsif (defined($settings->{'default'})) {
 1542:                 $defaultquota = $settings->{'default'};
 1543:             }
 1544:         }
 1545:     }
 1546:     $typecount ++;
 1547:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1548:     $datatable .= '<tr'.$css_class.'>'.
 1549:                   '<td>'.$othertitle.'</td>'.
 1550:                   '<td class="LC_left_item">';
 1551:     if ($context eq 'requestcourses') {
 1552:         $datatable .= '<table><tr>';
 1553:     }
 1554:     my %defcell;
 1555:     foreach my $item (@usertools) {
 1556:         if ($context eq 'requestcourses') {
 1557:             my ($curroption,$currlimit);
 1558:             if (ref($settings) eq 'HASH') {
 1559:                 if (ref($settings->{$item}) eq 'HASH') {
 1560:                     $curroption = $settings->{$item}->{'default'};
 1561:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1562:                         $currlimit = $1;
 1563:                     }
 1564:                 }
 1565:             }
 1566:             if (!$curroption) {
 1567:                 $curroption = 'norequest';
 1568:             }
 1569:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1570:             foreach my $option (@options) {
 1571:                 my $val = $option;
 1572:                 if ($option eq 'norequest') {
 1573:                     $val = 0;
 1574:                 }
 1575:                 if ($option eq 'validate') {
 1576:                     my $canvalidate = 0;
 1577:                     if (ref($validations{$item}) eq 'HASH') {
 1578:                         if ($validations{$item}{'default'}) {
 1579:                             $canvalidate = 1;
 1580:                         }
 1581:                     }
 1582:                     next if (!$canvalidate);
 1583:                 }
 1584:                 my $checked = '';
 1585:                 if ($option eq $curroption) {
 1586:                     $checked = ' checked="checked"';
 1587:                 } elsif ($option eq 'autolimit') {
 1588:                     if ($curroption =~ /^autolimit/) {
 1589:                         $checked = ' checked="checked"';
 1590:                     }
 1591:                 }
 1592:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1593:                                   '<input type="radio" name="crsreq_'.$item.
 1594:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1595:                                   $titles{$option}.'</label>';
 1596:                 if ($option eq 'autolimit') {
 1597:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1598:                                        $item.'_limit_default" size="1" '.
 1599:                                        'value="'.$currlimit.'" />';
 1600:                 }
 1601:                 $defcell{$item} .= '</span> ';
 1602:                 if ($option eq 'autolimit') {
 1603:                     $defcell{$item} .= $titles{'unlimited'};
 1604:                 }
 1605:             }
 1606:         } elsif ($context eq 'requestauthor') {
 1607:             my $curroption;
 1608:             if (ref($settings) eq 'HASH') {
 1609:                 if (ref($settings->{'requestauthor'}) eq 'HASH') {
 1610:                     $curroption = $settings->{'requestauthor'};
 1611:                 }
 1612:             }
 1613:             if (!$curroption) {
 1614:                 $curroption = 'norequest';
 1615:             }
 1616:             foreach my $option (@options) {
 1617:                 my $val = $option;
 1618:                 if ($option eq 'norequest') {
 1619:                     $val = 0;
 1620:                 }
 1621:                 my $checked = '';
 1622:                 if ($option eq $curroption) {
 1623:                     $checked = ' checked="checked"';
 1624:                 }
 1625:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1626:                               '<input type="radio" name="authorreq_default"'.
 1627:                               ' value="'.$val.'"'.$checked.' />'.
 1628:                               $titles{$option}.'</label></span>&nbsp; ';
 1629:             }
 1630:         } else {
 1631:             my $checked = 'checked="checked" ';
 1632:             if (ref($settings) eq 'HASH') {
 1633:                 if (ref($settings->{$item}) eq 'HASH') {
 1634:                     if ($settings->{$item}->{'default'} == 0) {
 1635:                         $checked = '';
 1636:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1637:                         $checked = 'checked="checked" ';
 1638:                     }
 1639:                 }
 1640:             }
 1641:             $datatable .= '<span class="LC_nobreak"><label>'.
 1642:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1643:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1644:                           '</label></span>&nbsp; ';
 1645:         }
 1646:     }
 1647:     if ($context eq 'requestcourses') {
 1648:         $datatable .= '</tr><tr>';
 1649:         foreach my $item (@usertools) {
 1650:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1651:         }
 1652:         $datatable .= '</tr></table>';
 1653:     }
 1654:     $datatable .= '</td>';
 1655:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1656:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1657:                       '<input type="text" name="defaultquota" value="'.
 1658:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1659:     }
 1660:     $datatable .= '</tr>';
 1661:     $typecount ++;
 1662:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1663:     $datatable .= '<tr'.$css_class.'>'.
 1664:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1665:     if ($context eq 'requestcourses') {
 1666:         $datatable .= &mt('(overrides affiliation, if set)').
 1667:                       '</td>'.
 1668:                       '<td class="LC_left_item">'.
 1669:                       '<table><tr>';
 1670:     } else {
 1671:         $datatable .= &mt('(overrides affiliation, if checked)').
 1672:                       '</td>'.
 1673:                       '<td class="LC_left_item" colspan="2">'.
 1674:                       '<br />';
 1675:     }
 1676:     my %advcell;
 1677:     foreach my $item (@usertools) {
 1678:         if ($context eq 'requestcourses') {
 1679:             my ($curroption,$currlimit);
 1680:             if (ref($settings) eq 'HASH') {
 1681:                 if (ref($settings->{$item}) eq 'HASH') {
 1682:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1683:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1684:                         $currlimit = $1;
 1685:                     }
 1686:                 }
 1687:             }
 1688:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1689:             my $checked = '';
 1690:             if ($curroption eq '') {
 1691:                 $checked = ' checked="checked"';
 1692:             }
 1693:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1694:                                '<input type="radio" name="crsreq_'.$item.
 1695:                                '__LC_adv" value=""'.$checked.' />'.
 1696:                                &mt('No override set').'</label></span>&nbsp; ';
 1697:             foreach my $option (@options) {
 1698:                 my $val = $option;
 1699:                 if ($option eq 'norequest') {
 1700:                     $val = 0;
 1701:                 }
 1702:                 if ($option eq 'validate') {
 1703:                     my $canvalidate = 0;
 1704:                     if (ref($validations{$item}) eq 'HASH') {
 1705:                         if ($validations{$item}{'_LC_adv'}) {
 1706:                             $canvalidate = 1;
 1707:                         }
 1708:                     }
 1709:                     next if (!$canvalidate);
 1710:                 }
 1711:                 my $checked = '';
 1712:                 if ($val eq $curroption) {
 1713:                     $checked = ' checked="checked"';
 1714:                 } elsif ($option eq 'autolimit') {
 1715:                     if ($curroption =~ /^autolimit/) {
 1716:                         $checked = ' checked="checked"';
 1717:                     }
 1718:                 }
 1719:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1720:                                   '<input type="radio" name="crsreq_'.$item.
 1721:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1722:                                   $titles{$option}.'</label>';
 1723:                 if ($option eq 'autolimit') {
 1724:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1725:                                        $item.'_limit__LC_adv" size="1" '.
 1726:                                        'value="'.$currlimit.'" />';
 1727:                 }
 1728:                 $advcell{$item} .= '</span> ';
 1729:                 if ($option eq 'autolimit') {
 1730:                     $advcell{$item} .= $titles{'unlimited'};
 1731:                 }
 1732:             }
 1733:         } elsif ($context eq 'requestauthor') {
 1734:             my $curroption;
 1735:             if (ref($settings) eq 'HASH') {
 1736:                 $curroption = $settings->{'_LC_adv'};
 1737:             }
 1738:             my $checked = '';
 1739:             if ($curroption eq '') {
 1740:                 $checked = ' checked="checked"';
 1741:             }
 1742:             $datatable .= '<span class="LC_nobreak"><label>'.
 1743:                           '<input type="radio" name="authorreq__LC_adv"'.
 1744:                           ' value=""'.$checked.' />'.
 1745:                           &mt('No override set').'</label></span>&nbsp; ';
 1746:             foreach my $option (@options) {
 1747:                 my $val = $option;
 1748:                 if ($option eq 'norequest') {
 1749:                     $val = 0;
 1750:                 }
 1751:                 my $checked = '';
 1752:                 if ($val eq $curroption) {
 1753:                     $checked = ' checked="checked"';
 1754:                 }
 1755:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1756:                               '<input type="radio" name="crsreq_'.$item.
 1757:                               '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1758:                               $titles{$option}.'</label></span>&nbsp; ';
 1759:             }
 1760:         } else {
 1761:             my $checked = 'checked="checked" ';
 1762:             if (ref($settings) eq 'HASH') {
 1763:                 if (ref($settings->{$item}) eq 'HASH') {
 1764:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1765:                         $checked = '';
 1766:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1767:                         $checked = 'checked="checked" ';
 1768:                     }
 1769:                 }
 1770:             }
 1771:             $datatable .= '<span class="LC_nobreak"><label>'.
 1772:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1773:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1774:                           '</label></span>&nbsp; ';
 1775:         }
 1776:     }
 1777:     if ($context eq 'requestcourses') {
 1778:         $datatable .= '</tr><tr>';
 1779:         foreach my $item (@usertools) {
 1780:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1781:         }
 1782:         $datatable .= '</tr></table>';
 1783:     }
 1784:     $datatable .= '</td></tr>';
 1785:     $$rowtotal += $typecount;
 1786:     return $datatable;
 1787: }
 1788: 
 1789: sub print_requestmail {
 1790:     my ($dom,$action,$settings,$rowtotal) = @_;
 1791:     my ($now,$datatable,%dompersonnel,@domcoord,@currapproval,$rows);
 1792:     $now = time;
 1793:     $rows = 0;
 1794:     %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 1795:     foreach my $server (keys(%dompersonnel)) {
 1796:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 1797:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 1798:             if (!grep(/^$uname:$udom$/,@domcoord)) {
 1799:                 push(@domcoord,$uname.':'.$udom);
 1800:             }
 1801:         }
 1802:     }
 1803:     if (ref($settings) eq 'HASH') {
 1804:         if (ref($settings->{'notify'}) eq 'HASH') {
 1805:             if ($settings->{'notify'}{'approval'} ne '') {
 1806:                @currapproval = split(',',$settings->{'notify'}{'approval'});
 1807:             }
 1808:         }
 1809:     }
 1810:     if (@currapproval) {
 1811:         foreach my $dc (@currapproval) {
 1812:             unless (grep(/^\Q$dc\E$/,@domcoord)) {
 1813:                 push(@domcoord,$dc);
 1814:             }
 1815:         }
 1816:     }
 1817:     @domcoord = sort(@domcoord);
 1818:     my $numinrow = 4;
 1819:     my $numdc = @domcoord;
 1820:     my $css_class = 'class="LC_odd_row"';
 1821:     my $text;
 1822:     if ($action eq 'requestcourses') {
 1823:         $text = &mt('Receive notification of course requests requiring approval');
 1824:     } else {
 1825:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1826:     }
 1827:     $datatable = '<tr '.$css_class.'>'.
 1828:                  ' <td>'.$text.'</td>'.
 1829:                  ' <td class="LC_left_item">';
 1830:     if (@domcoord > 0) {
 1831:         $datatable .= '<table>';
 1832:         for (my $i=0; $i<$numdc; $i++) {
 1833:             my $rem = $i%($numinrow);
 1834:             if ($rem == 0) {
 1835:                 if ($i > 0) {
 1836:                     $datatable .= '</tr>';
 1837:                 }
 1838:                 $datatable .= '<tr>';
 1839:                 $rows ++;
 1840:             }
 1841:             my $check = ' ';
 1842:             if (grep(/^\Q$domcoord[$i]\E$/,@currapproval)) {
 1843:                 $check = ' checked="checked" ';
 1844:             }
 1845:             my ($uname,$udom) = split(':',$domcoord[$i]);
 1846:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
 1847:             if ($i == $numdc-1) {
 1848:                 my $colsleft = $numinrow-$rem;
 1849:                 if ($colsleft > 1) {
 1850:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 1851:                 } else {
 1852:                     $datatable .= '<td class="LC_left_item">';
 1853:                 }
 1854:             } else {
 1855:                 $datatable .= '<td class="LC_left_item">';
 1856:             }
 1857:             $datatable .= '<span class="LC_nobreak"><label>'.
 1858:                           '<input type="checkbox" name="reqapprovalnotify" '.
 1859:                           'value="'.$domcoord[$i].'"'.$check.'/>'.
 1860:                           $fullname.'</label></span></td>';
 1861:         }
 1862:         $datatable .= '</tr></table>';
 1863:     } else {
 1864:         $datatable .= &mt('There are no active Domain Coordinators');
 1865:         $rows ++;
 1866:     }
 1867:     $datatable .='</td></tr>';
 1868:     $$rowtotal += $rows;
 1869:     return $datatable;
 1870: }
 1871: 
 1872: sub print_autoenroll {
 1873:     my ($dom,$settings,$rowtotal) = @_;
 1874:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1875:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1876:     if (ref($settings) eq 'HASH') {
 1877:         if (exists($settings->{'run'})) {
 1878:             if ($settings->{'run'} eq '0') {
 1879:                 $runoff = ' checked="checked" ';
 1880:                 $runon = ' ';
 1881:             } else {
 1882:                 $runon = ' checked="checked" ';
 1883:                 $runoff = ' ';
 1884:             }
 1885:         } else {
 1886:             if ($autorun) {
 1887:                 $runon = ' checked="checked" ';
 1888:                 $runoff = ' ';
 1889:             } else {
 1890:                 $runoff = ' checked="checked" ';
 1891:                 $runon = ' ';
 1892:             }
 1893:         }
 1894:         if (exists($settings->{'co-owners'})) {
 1895:             if ($settings->{'co-owners'} eq '0') {
 1896:                 $coownersoff = ' checked="checked" ';
 1897:                 $coownerson = ' ';
 1898:             } else {
 1899:                 $coownerson = ' checked="checked" ';
 1900:                 $coownersoff = ' ';
 1901:             }
 1902:         } else {
 1903:             $coownersoff = ' checked="checked" ';
 1904:             $coownerson = ' ';
 1905:         }
 1906:         if (exists($settings->{'sender_domain'})) {
 1907:             $defdom = $settings->{'sender_domain'};
 1908:         }
 1909:     } else {
 1910:         if ($autorun) {
 1911:             $runon = ' checked="checked" ';
 1912:             $runoff = ' ';
 1913:         } else {
 1914:             $runoff = ' checked="checked" ';
 1915:             $runon = ' ';
 1916:         }
 1917:     }
 1918:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 1919:     my $notif_sender;
 1920:     if (ref($settings) eq 'HASH') {
 1921:         $notif_sender = $settings->{'sender_uname'};
 1922:     }
 1923:     my $datatable='<tr class="LC_odd_row">'.
 1924:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 1925:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1926:                   '<input type="radio" name="autoenroll_run"'.
 1927:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1928:                   '<label><input type="radio" name="autoenroll_run"'.
 1929:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 1930:                   '</tr><tr>'.
 1931:                   '<td>'.&mt('Notification messages - sender').
 1932:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 1933:                   &mt('username').':&nbsp;'.
 1934:                   '<input type="text" name="sender_uname" value="'.
 1935:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 1936:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 1937:                   '<tr class="LC_odd_row">'.
 1938:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 1939:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1940:                   '<input type="radio" name="autoassign_coowners"'.
 1941:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1942:                   '<label><input type="radio" name="autoassign_coowners"'.
 1943:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 1944:                   '</tr>';
 1945:     $$rowtotal += 3;
 1946:     return $datatable;
 1947: }
 1948: 
 1949: sub print_autoupdate {
 1950:     my ($position,$dom,$settings,$rowtotal) = @_;
 1951:     my $datatable;
 1952:     if ($position eq 'top') {
 1953:         my $updateon = ' ';
 1954:         my $updateoff = ' checked="checked" ';
 1955:         my $classlistson = ' ';
 1956:         my $classlistsoff = ' checked="checked" ';
 1957:         if (ref($settings) eq 'HASH') {
 1958:             if ($settings->{'run'} eq '1') {
 1959:                 $updateon = $updateoff;
 1960:                 $updateoff = ' ';
 1961:             }
 1962:             if ($settings->{'classlists'} eq '1') {
 1963:                 $classlistson = $classlistsoff;
 1964:                 $classlistsoff = ' ';
 1965:             }
 1966:         }
 1967:         my %title = (
 1968:                    run => 'Auto-update active?',
 1969:                    classlists => 'Update information in classlists?',
 1970:                     );
 1971:         $datatable = '<tr class="LC_odd_row">'. 
 1972:                   '<td>'.&mt($title{'run'}).'</td>'.
 1973:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1974:                   '<input type="radio" name="autoupdate_run"'.
 1975:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1976:                   '<label><input type="radio" name="autoupdate_run"'.
 1977:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 1978:                   '</tr><tr>'.
 1979:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 1980:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 1981:                   '<label><input type="radio" name="classlists"'.
 1982:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1983:                   '<label><input type="radio" name="classlists"'.
 1984:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 1985:                   '</tr>';
 1986:         $$rowtotal += 2;
 1987:     } elsif ($position eq 'middle') {
 1988:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1989:         my $numinrow = 3;
 1990:         my $locknamesettings;
 1991:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 1992:                                      $dom,$numinrow,$othertitle,
 1993:                                     'lockablenames');
 1994:         $$rowtotal ++;
 1995:     } else {
 1996:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1997:         my @fields = ('lastname','firstname','middlename','generation',
 1998:                       'permanentemail','id');
 1999:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2000:         my $numrows = 0;
 2001:         if (ref($types) eq 'ARRAY') {
 2002:             if (@{$types} > 0) {
 2003:                 $datatable = 
 2004:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2005:                                          \@fields,$types,\$numrows);
 2006:                     $$rowtotal += @{$types}; 
 2007:             }
 2008:         }
 2009:         $datatable .= 
 2010:             &usertype_update_row($settings,{'default' => $othertitle},
 2011:                                  \%fieldtitles,\@fields,['default'],
 2012:                                  \$numrows);
 2013:         $$rowtotal ++;     
 2014:     }
 2015:     return $datatable;
 2016: }
 2017: 
 2018: sub print_autocreate {
 2019:     my ($dom,$settings,$rowtotal) = @_;
 2020:     my (%createon,%createoff);
 2021:     my $curr_dc;
 2022:     my @types = ('xml','req');
 2023:     if (ref($settings) eq 'HASH') {
 2024:         foreach my $item (@types) {
 2025:             $createoff{$item} = ' checked="checked" ';
 2026:             $createon{$item} = ' ';
 2027:             if (exists($settings->{$item})) {
 2028:                 if ($settings->{$item}) {
 2029:                     $createon{$item} = ' checked="checked" ';
 2030:                     $createoff{$item} = ' ';
 2031:                 }
 2032:             }
 2033:         }
 2034:         $curr_dc = $settings->{'xmldc'};
 2035:     } else {
 2036:         foreach my $item (@types) {
 2037:             $createoff{$item} = ' checked="checked" ';
 2038:             $createon{$item} = ' ';
 2039:         }
 2040:     }
 2041:     $$rowtotal += 2;
 2042:     my $datatable='<tr class="LC_odd_row">'.
 2043:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2044:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2045:                   '<input type="radio" name="autocreate_xml"'.
 2046:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2047:                   '<label><input type="radio" name="autocreate_xml"'.
 2048:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2049:                   '</td></tr><tr>'.
 2050:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2051:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2052:                   '<input type="radio" name="autocreate_req"'.
 2053:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2054:                   '<label><input type="radio" name="autocreate_req"'.
 2055:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2056:     my ($numdc,$dctable) = &active_dc_picker($dom,$curr_dc);
 2057:     if ($numdc > 1) {
 2058:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2059:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2060:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2061:         $$rowtotal ++ ;
 2062:     } else {
 2063:         $datatable .= $dctable.'</td></tr>';
 2064:     }
 2065:     return $datatable;
 2066: }
 2067: 
 2068: sub print_directorysrch {
 2069:     my ($dom,$settings,$rowtotal) = @_;
 2070:     my $srchon = ' ';
 2071:     my $srchoff = ' checked="checked" ';
 2072:     my ($exacton,$containson,$beginson);
 2073:     my $localon = ' ';
 2074:     my $localoff = ' checked="checked" ';
 2075:     if (ref($settings) eq 'HASH') {
 2076:         if ($settings->{'available'} eq '1') {
 2077:             $srchon = $srchoff;
 2078:             $srchoff = ' ';
 2079:         }
 2080:         if ($settings->{'localonly'} eq '1') {
 2081:             $localon = $localoff;
 2082:             $localoff = ' ';
 2083:         }
 2084:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2085:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2086:                 if ($type eq 'exact') {
 2087:                     $exacton = ' checked="checked" ';
 2088:                 } elsif ($type eq 'contains') {
 2089:                     $containson = ' checked="checked" ';
 2090:                 } elsif ($type eq 'begins') {
 2091:                     $beginson = ' checked="checked" ';
 2092:                 }
 2093:             }
 2094:         } else {
 2095:             if ($settings->{'searchtypes'} eq 'exact') {
 2096:                 $exacton = ' checked="checked" ';
 2097:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2098:                 $containson = ' checked="checked" ';
 2099:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2100:                 $exacton = ' checked="checked" ';
 2101:                 $containson = ' checked="checked" ';
 2102:             }
 2103:         }
 2104:     }
 2105:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2106:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2107: 
 2108:     my $numinrow = 4;
 2109:     my $cansrchrow = 0;
 2110:     my $datatable='<tr class="LC_odd_row">'.
 2111:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2112:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2113:                   '<input type="radio" name="dirsrch_available"'.
 2114:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2115:                   '<label><input type="radio" name="dirsrch_available"'.
 2116:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2117:                   '</tr><tr>'.
 2118:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2119:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2120:                   '<input type="radio" name="dirsrch_localonly"'.
 2121:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2122:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2123:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2124:                   '</tr>';
 2125:     $$rowtotal += 2;
 2126:     if (ref($usertypes) eq 'HASH') {
 2127:         if (keys(%{$usertypes}) > 0) {
 2128:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2129:                                          $numinrow,$othertitle,'cansearch');
 2130:             $cansrchrow = 1;
 2131:         }
 2132:     }
 2133:     if ($cansrchrow) {
 2134:         $$rowtotal ++;
 2135:         $datatable .= '<tr>';
 2136:     } else {
 2137:         $datatable .= '<tr class="LC_odd_row">';
 2138:     }
 2139:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2140:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2141:     foreach my $title (@{$titleorder}) {
 2142:         if (defined($searchtitles->{$title})) {
 2143:             my $check = ' ';
 2144:             if (ref($settings) eq 'HASH') {
 2145:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2146:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2147:                         $check = ' checked="checked" ';
 2148:                     }
 2149:                 }
 2150:             }
 2151:             $datatable .= '<td class="LC_left_item">'.
 2152:                           '<span class="LC_nobreak"><label>'.
 2153:                           '<input type="checkbox" name="searchby" '.
 2154:                           'value="'.$title.'"'.$check.'/>'.
 2155:                           $searchtitles->{$title}.'</label></span></td>';
 2156:         }
 2157:     }
 2158:     $datatable .= '</tr></table></td></tr>';
 2159:     $$rowtotal ++;
 2160:     if ($cansrchrow) {
 2161:         $datatable .= '<tr class="LC_odd_row">';
 2162:     } else {
 2163:         $datatable .= '<tr>';
 2164:     }
 2165:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2166:                   '<td class="LC_left_item" colspan="2">'.
 2167:                   '<span class="LC_nobreak"><label>'.
 2168:                   '<input type="checkbox" name="searchtypes" '.
 2169:                   $exacton.' value="exact" />'.&mt('Exact match').
 2170:                   '</label>&nbsp;'.
 2171:                   '<label><input type="checkbox" name="searchtypes" '.
 2172:                   $beginson.' value="begins" />'.&mt('Begins with').
 2173:                   '</label>&nbsp;'.
 2174:                   '<label><input type="checkbox" name="searchtypes" '.
 2175:                   $containson.' value="contains" />'.&mt('Contains').
 2176:                   '</label></span></td></tr>';
 2177:     $$rowtotal ++;
 2178:     return $datatable;
 2179: }
 2180: 
 2181: sub print_contacts {
 2182:     my ($dom,$settings,$rowtotal) = @_;
 2183:     my $datatable;
 2184:     my @contacts = ('adminemail','supportemail');
 2185:     my (%checked,%to,%otheremails,%bccemails);
 2186:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2187:                     'requestsmail');
 2188:     foreach my $type (@mailings) {
 2189:         $otheremails{$type} = '';
 2190:     }
 2191:     $bccemails{'helpdeskmail'} = '';
 2192:     if (ref($settings) eq 'HASH') {
 2193:         foreach my $item (@contacts) {
 2194:             if (exists($settings->{$item})) {
 2195:                 $to{$item} = $settings->{$item};
 2196:             }
 2197:         }
 2198:         foreach my $type (@mailings) {
 2199:             if (exists($settings->{$type})) {
 2200:                 if (ref($settings->{$type}) eq 'HASH') {
 2201:                     foreach my $item (@contacts) {
 2202:                         if ($settings->{$type}{$item}) {
 2203:                             $checked{$type}{$item} = ' checked="checked" ';
 2204:                         }
 2205:                     }
 2206:                     $otheremails{$type} = $settings->{$type}{'others'};
 2207:                     if ($type eq 'helpdeskmail') {
 2208:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2209:                     }
 2210:                 }
 2211:             } elsif ($type eq 'lonstatusmail') {
 2212:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2213:             }
 2214:         }
 2215:     } else {
 2216:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2217:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2218:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2219:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2220:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2221:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2222:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2223:     }
 2224:     my ($titles,$short_titles) = &contact_titles();
 2225:     my $rownum = 0;
 2226:     my $css_class;
 2227:     foreach my $item (@contacts) {
 2228:         $rownum ++;
 2229:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2230:         $datatable .= '<tr'.$css_class.'>'. 
 2231:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2232:                   '</span></td><td class="LC_right_item">'.
 2233:                   '<input type="text" name="'.$item.'" value="'.
 2234:                   $to{$item}.'" /></td></tr>';
 2235:     }
 2236:     foreach my $type (@mailings) {
 2237:         $rownum ++;
 2238:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2239:         $datatable .= '<tr'.$css_class.'>'.
 2240:                       '<td><span class="LC_nobreak">'.
 2241:                       $titles->{$type}.': </span></td>'.
 2242:                       '<td class="LC_left_item">'.
 2243:                       '<span class="LC_nobreak">';
 2244:         foreach my $item (@contacts) {
 2245:             $datatable .= '<label>'.
 2246:                           '<input type="checkbox" name="'.$type.'"'.
 2247:                           $checked{$type}{$item}.
 2248:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2249:                           '</label>&nbsp;';
 2250:         }
 2251:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2252:                       '<input type="text" name="'.$type.'_others" '.
 2253:                       'value="'.$otheremails{$type}.'"  />';
 2254:         if ($type eq 'helpdeskmail') {
 2255:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2256:                           '<input type="text" name="'.$type.'_bcc" '.
 2257:                           'value="'.$bccemails{$type}.'"  />';
 2258:         }
 2259:         $datatable .= '</td></tr>'."\n";
 2260:     }
 2261:     $$rowtotal += $rownum;
 2262:     return $datatable;
 2263: }
 2264: 
 2265: sub print_helpsettings {
 2266: 
 2267: 	my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 2268: 	my ($css_class,$datatable);
 2269: 	
 2270: 	my $switchserver = &check_switchserver($dom,$confname);
 2271: 	
 2272: 	my $itemcount = 1;
 2273: 	
 2274: 	if ($position eq 'top') {
 2275: 		
 2276: 		my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2277: 		
 2278: 		%choices =
 2279: 			&Apache::lonlocal::texthash (
 2280: 				submitbugs => 'Display &quot;Submit a bug&quot; link?',
 2281: 		);
 2282: 		
 2283: 		%defaultchecked = ('submitbugs' => 'on');
 2284: 		
 2285: 		@toggles = ('submitbugs',);
 2286: 		
 2287: 		foreach my $item (@toggles) {
 2288: 			if ($defaultchecked{$item} eq 'on') { 
 2289: 				$checkedon{$item} = ' checked="checked" ';
 2290: 				$checkedoff{$item} = ' ';
 2291: 			} elsif ($defaultchecked{$item} eq 'off') {
 2292: 				$checkedoff{$item} = ' checked="checked" ';
 2293: 				$checkedon{$item} = ' ';
 2294: 			}
 2295: 		}
 2296: 		
 2297: 		if (ref($settings) eq 'HASH') {
 2298: 			foreach my $item (@toggles) {
 2299: 				if ($settings->{$item} eq '1') {
 2300: 					$checkedon{$item} =  ' checked="checked" ';
 2301: 					$checkedoff{$item} = ' ';
 2302: 				} elsif ($settings->{$item} eq '0') {
 2303: 					$checkedoff{$item} =  ' checked="checked" ';
 2304: 					$checkedon{$item} = ' ';
 2305: 				}
 2306: 			}
 2307: 		 }
 2308: 		
 2309: 		 foreach my $item (@toggles) {
 2310: 			$css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2311: 			$datatable .=  
 2312: 				'<tr'.$css_class.'>
 2313: 				<td><span class="LC_nobreak">'.$choices{$item}.'</span></td>
 2314: 				<td><span class="LC_nobreak">&nbsp;</span></td>
 2315: 				<td class="LC_right_item"><span class="LC_nobreak">
 2316: 				<label><input type="radio" name="'.$item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').'</label>&nbsp;
 2317: 				<label><input type="radio" name="'.$item.'" '.$checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2318: 				'</span></td>'.
 2319: 				'</tr>';
 2320: 			$itemcount ++;
 2321: 		 }
 2322:      
 2323:      } else {
 2324:      
 2325:      	$css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2326:      	
 2327:      	$datatable .= '<tr'.$css_class.'>';
 2328:      	
 2329:      	if (ref($settings) eq 'HASH') {
 2330: 			if ($settings->{'loginhelpurl'} ne '') {
 2331: 				my($directory, $filename) = $settings->{'loginhelpurl'} =~ m/(.*\/)(.*)$/;
 2332: 				$datatable .= '<td width="33%"><span class="LC_left_item"><label><a href="'.$settings->{'loginhelpurl'}.'" target="_blank">'.&mt('Custom Login Page Help File In Use').'</a></label></span></td>';
 2333: 				$datatable .= '<td width="33%"><span class="LC_right_item"><label><input type="checkbox" name="loginhelpurl_del" value="1" />'.&mt('Delete?').'</label></span></td>'
 2334: 			} else {
 2335: 				$datatable .= '<td width="33%"><span class="LC_left_item"><label>'.&mt('Default Login Page Help File In Use').'</label></span></td>';
 2336: 				$datatable .= '<td width="33%"><span class="LC_right_item">&nbsp;</span></td>';
 2337: 			}
 2338: 		} else {
 2339: 			$datatable .= '<td><span class="LC_left_item">&nbsp;</span></td>';
 2340: 			$datatable .= '<td><span class="LC_right_item">&nbsp;</span></td>';
 2341: 		}
 2342:     	
 2343:      	$datatable .= '<td width="33%"><span class="LC_right_item">';
 2344:      	if ($switchserver) {
 2345:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2346:         } else {
 2347:         	$datatable .= &mt('Upload Custom Login Page Help File:');
 2348:             $datatable .='<input type="file" name="loginhelpurl" />';
 2349:         }
 2350:         $datatable .= '</span></td></tr>';
 2351:         
 2352:      }
 2353:      return $datatable;
 2354: }
 2355: 
 2356: 
 2357: sub radiobutton_prefs {
 2358:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount) = @_;
 2359:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2360:                    (ref($choices) eq 'HASH'));
 2361: 
 2362:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2363: 
 2364:     foreach my $item (@{$toggles}) {
 2365:         if ($defaultchecked->{$item} eq 'on') {
 2366:             $checkedon{$item} = ' checked="checked" ';
 2367:             $checkedoff{$item} = ' ';
 2368:         } elsif ($defaultchecked->{$item} eq 'off') {
 2369:             $checkedoff{$item} = ' checked="checked" ';
 2370:             $checkedon{$item} = ' ';
 2371:         }
 2372:     }
 2373:     if (ref($settings) eq 'HASH') {
 2374:         foreach my $item (@{$toggles}) {
 2375:             if ($settings->{$item} eq '1') {
 2376:                 $checkedon{$item} =  ' checked="checked" ';
 2377:                 $checkedoff{$item} = ' ';
 2378:             } elsif ($settings->{$item} eq '0') {
 2379:                 $checkedoff{$item} =  ' checked="checked" ';
 2380:                 $checkedon{$item} = ' ';
 2381:             }
 2382:         }
 2383:     }
 2384:     foreach my $item (@{$toggles}) {
 2385:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2386:         $datatable .=
 2387:             '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices->{$item}.
 2388:             '</span></td>'.
 2389:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2390:             '<label><input type="radio" name="'.
 2391:             $item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').
 2392:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2393:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2394:             '</span></td>'.
 2395:             '</tr>';
 2396:         $itemcount ++;
 2397:     }
 2398:     return ($datatable,$itemcount);
 2399: }
 2400: 
 2401: sub print_coursedefaults {
 2402:     my ($position,$dom,$settings,$rowtotal) = @_;
 2403:     my ($css_class,$datatable);
 2404:     my $itemcount = 1;
 2405:     if ($position eq 'top') {
 2406:         my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2407:         %choices =
 2408:             &Apache::lonlocal::texthash (
 2409:                 canuse_pdfforms => 'Course/Community users can create/upload PDF forms',
 2410:         );
 2411:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2412:         @toggles = ('canuse_pdfforms',);
 2413:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2414:                                                  \%choices,$itemcount);
 2415:         $$rowtotal += $itemcount;
 2416:     } else {
 2417:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2418:         my %choices =
 2419:             &Apache::lonlocal::texthash (
 2420:                 anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2421:         );
 2422:         my $currdefresponder;
 2423:         if (ref($settings) eq 'HASH') {
 2424:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2425:         }
 2426:         if (!$currdefresponder) {
 2427:             $currdefresponder = 10;
 2428:         } elsif ($currdefresponder < 1) {
 2429:             $currdefresponder = 1;
 2430:         }
 2431:         $datatable .=
 2432:                '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices{'anonsurvey_threshold'}.
 2433:                 '</span></td>'.
 2434:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2435:                 '<input type="text" name="anonsurvey_threshold"'.
 2436:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2437:                 '</td></tr>';
 2438:     }
 2439:     return $datatable;
 2440: }
 2441: 
 2442: sub print_usersessions {
 2443:     my ($position,$dom,$settings,$rowtotal) = @_;
 2444:     my ($css_class,$datatable,%checked,%choices);
 2445:     my (%by_ip,%by_location,@intdoms);
 2446:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2447: 
 2448:     my @alldoms = &Apache::lonnet::all_domains();
 2449:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2450:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2451:     my %altids = &id_for_thisdom(%servers);
 2452:     my $itemcount = 1;
 2453:     if ($position eq 'top') {
 2454:         if (keys(%serverhomes) > 1) {
 2455:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2456:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2457:         } else {
 2458:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2459:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2460:         }
 2461:     } else {
 2462:         if (keys(%by_location) == 0) {
 2463:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2464:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2465:         } else {
 2466:             my %lt = &usersession_titles();
 2467:             my $numinrow = 5;
 2468:             my $prefix;
 2469:             my @types;
 2470:             if ($position eq 'bottom') {
 2471:                 $prefix = 'remote';
 2472:                 @types = ('version','excludedomain','includedomain');
 2473:             } else {
 2474:                 $prefix = 'hosted';
 2475:                 @types = ('excludedomain','includedomain');
 2476:             }
 2477:             my (%current,%checkedon,%checkedoff);
 2478:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2479:             my @locations = sort(keys(%by_location));
 2480:             foreach my $type (@types) {
 2481:                 $checkedon{$type} = '';
 2482:                 $checkedoff{$type} = ' checked="checked"';
 2483:             }
 2484:             if (ref($settings) eq 'HASH') {
 2485:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2486:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2487:                         $current{$key} = $settings->{$prefix}{$key};
 2488:                         if ($key eq 'version') {
 2489:                             if ($current{$key} ne '') {
 2490:                                 $checkedon{$key} = ' checked="checked"';
 2491:                                 $checkedoff{$key} = '';
 2492:                             }
 2493:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2494:                             $checkedon{$key} = ' checked="checked"';
 2495:                             $checkedoff{$key} = '';
 2496:                         }
 2497:                     }
 2498:                 }
 2499:             }
 2500:             foreach my $type (@types) {
 2501:                 next if ($type ne 'version' && !@locations);
 2502:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2503:                 $datatable .= '<tr'.$css_class.'>
 2504:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2505:                                <span class="LC_nobreak">&nbsp;
 2506:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2507:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2508:                 if ($type eq 'version') {
 2509:                     my $selector = '<select name="'.$prefix.'_version">';
 2510:                     foreach my $version (@lcversions) {
 2511:                         my $selected = '';
 2512:                         if ($current{'version'} eq $version) {
 2513:                             $selected = ' selected="selected"';
 2514:                         }
 2515:                         $selector .= ' <option value="'.$version.'"'.
 2516:                                      $selected.'>'.$version.'</option>';
 2517:                     }
 2518:                     $selector .= '</select> ';
 2519:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2520:                 } else {
 2521:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2522:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2523:                                  ' />'.('&nbsp;'x2).
 2524:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2525:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2526:                                  "\n".
 2527:                                  '</div><div><table>';
 2528:                     my $rem;
 2529:                     for (my $i=0; $i<@locations; $i++) {
 2530:                         my ($showloc,$value,$checkedtype);
 2531:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2532:                             my $ip = $by_location{$locations[$i]}->[0];
 2533:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2534:                                  $value = join(':',@{$by_ip{$ip}});
 2535:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2536:                                 if (ref($current{$type}) eq 'ARRAY') {
 2537:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2538:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2539:                                             $checkedtype = ' checked="checked"';
 2540:                                             last;
 2541:                                         }
 2542:                                     }
 2543:                                 }
 2544:                             }
 2545:                         }
 2546:                         $rem = $i%($numinrow);
 2547:                         if ($rem == 0) {
 2548:                             if ($i > 0) {
 2549:                                 $datatable .= '</tr>';
 2550:                             }
 2551:                             $datatable .= '<tr>';
 2552:                         }
 2553:                         $datatable .= '<td class="LC_left_item">'.
 2554:                                       '<span class="LC_nobreak"><label>'.
 2555:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2556:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2557:                                       '</label></span></td>';
 2558:                     }
 2559:                     $rem = @locations%($numinrow);
 2560:                     my $colsleft = $numinrow - $rem;
 2561:                     if ($colsleft > 1 ) {
 2562:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2563:                                       '&nbsp;</td>';
 2564:                     } elsif ($colsleft == 1) {
 2565:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2566:                     }
 2567:                     $datatable .= '</tr></table>';
 2568:                 }
 2569:                 $datatable .= '</td></tr>';
 2570:                 $itemcount ++;
 2571:             }
 2572:         }
 2573:     }
 2574:     $$rowtotal += $itemcount;
 2575:     return $datatable;
 2576: }
 2577: 
 2578: sub build_location_hashes {
 2579:     my ($intdoms,$by_ip,$by_location) = @_;
 2580:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2581:                   (ref($by_location) eq 'HASH')); 
 2582:     my %iphost = &Apache::lonnet::get_iphost();
 2583:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2584:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2585:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2586:         foreach my $id (@{$iphost{$primary_ip}}) {
 2587:             my $intdom = &Apache::lonnet::internet_dom($id);
 2588:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2589:                 push(@{$intdoms},$intdom);
 2590:             }
 2591:         }
 2592:     }
 2593:     foreach my $ip (keys(%iphost)) {
 2594:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2595:             foreach my $id (@{$iphost{$ip}}) {
 2596:                 my $location = &Apache::lonnet::internet_dom($id);
 2597:                 if ($location) {
 2598:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2599:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2600:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2601:                             push(@{$by_ip->{$ip}},$location);
 2602:                         }
 2603:                     } else {
 2604:                         $by_ip->{$ip} = [$location];
 2605:                     }
 2606:                 }
 2607:             }
 2608:         }
 2609:     }
 2610:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2611:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2612:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2613:             my $first = $by_ip->{$ip}->[0];
 2614:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2615:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2616:                     push(@{$by_location->{$first}},$ip);
 2617:                 }
 2618:             } else {
 2619:                 $by_location->{$first} = [$ip];
 2620:             }
 2621:         }
 2622:     }
 2623:     return;
 2624: }
 2625: 
 2626: sub current_offloads_to {
 2627:     my ($dom,$settings,$servers) = @_;
 2628:     my (%spareid,%otherdomconfigs);
 2629:     if (ref($servers) eq 'HASH') {
 2630:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2631:             my $gotspares;
 2632:             if (ref($settings) eq 'HASH') {
 2633:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2634:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2635:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2636:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2637:                         $gotspares = 1;
 2638:                     }
 2639:                 }
 2640:             }
 2641:             unless ($gotspares) {
 2642:                 my $gotspares;
 2643:                 my $serverhomeID =
 2644:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2645:                 my $serverhomedom =
 2646:                     &Apache::lonnet::host_domain($serverhomeID);
 2647:                 if ($serverhomedom ne $dom) {
 2648:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2649:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2650:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2651:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2652:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2653:                                 $gotspares = 1;
 2654:                             }
 2655:                         }
 2656:                     } else {
 2657:                         $otherdomconfigs{$serverhomedom} =
 2658:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2659:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2660:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2661:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2662:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2663:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2664:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2665:                                         $gotspares = 1;
 2666:                                     }
 2667:                                 }
 2668:                             }
 2669:                         }
 2670:                     }
 2671:                 }
 2672:             }
 2673:             unless ($gotspares) {
 2674:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2675:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2676:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2677:                } else {
 2678:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2679:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2680:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2681:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2682:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2683:                     } else {
 2684:                         my %what = (
 2685:                              spareid => 1,
 2686:                         );
 2687:                         my ($result,$returnhash) = 
 2688:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2689:                         if ($result eq 'ok') { 
 2690:                             if (ref($returnhash) eq 'HASH') {
 2691:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2692:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2693:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2694:                                 }
 2695:                             }
 2696:                         }
 2697:                     }
 2698:                 }
 2699:             }
 2700:         }
 2701:     }
 2702:     return %spareid;
 2703: }
 2704: 
 2705: sub spares_row {
 2706:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2707:     my $css_class;
 2708:     my $numinrow = 4;
 2709:     my $itemcount = 1;
 2710:     my $datatable;
 2711:     my %typetitles = &sparestype_titles();
 2712:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2713:         foreach my $server (sort(keys(%{$servers}))) {
 2714:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2715:             my ($othercontrol,$serverdom);
 2716:             if ($serverhome ne $server) {
 2717:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2718:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2719:             } else {
 2720:                 $serverdom = &Apache::lonnet::host_domain($server);
 2721:                 if ($serverdom ne $dom) {
 2722:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2723:                 }
 2724:             }
 2725:             next unless (ref($spareid->{$server}) eq 'HASH');
 2726:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2727:             $datatable .= '<tr'.$css_class.'>
 2728:                            <td rowspan="2">
 2729:                             <span class="LC_nobreak"><b>'.$server.'</b> when busy, offloads to:</span></td>'."\n";
 2730:             my (%current,%canselect);
 2731:             my @choices = 
 2732:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2733:             foreach my $type ('primary','default') {
 2734:                 if (ref($spareid->{$server}) eq 'HASH') {
 2735:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2736:                         my @spares = @{$spareid->{$server}{$type}};
 2737:                         if (@spares > 0) {
 2738:                             if ($othercontrol) {
 2739:                                 $current{$type} = join(', ',@spares);
 2740:                             } else {
 2741:                                 $current{$type} .= '<table>';
 2742:                                 my $numspares = scalar(@spares);
 2743:                                 for (my $i=0;  $i<@spares; $i++) {
 2744:                                     my $rem = $i%($numinrow);
 2745:                                     if ($rem == 0) {
 2746:                                         if ($i > 0) {
 2747:                                             $current{$type} .= '</tr>';
 2748:                                         }
 2749:                                         $current{$type} .= '<tr>';
 2750:                                     }
 2751:                                     $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;'.
 2752:                                                        $spareid->{$server}{$type}[$i].
 2753:                                                        '</label></td>'."\n";
 2754:                                 }
 2755:                                 my $rem = @spares%($numinrow);
 2756:                                 my $colsleft = $numinrow - $rem;
 2757:                                 if ($colsleft > 1 ) {
 2758:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2759:                                                        '" class="LC_left_item">'.
 2760:                                                        '&nbsp;</td>';
 2761:                                 } elsif ($colsleft == 1) {
 2762:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2763:                                 }
 2764:                                 $current{$type} .= '</tr></table>';
 2765:                             }
 2766:                         }
 2767:                     }
 2768:                     if ($current{$type} eq '') {
 2769:                         $current{$type} = &mt('None specified');
 2770:                     }
 2771:                     if ($othercontrol) {
 2772:                         if ($type eq 'primary') {
 2773:                             $canselect{$type} = $othercontrol;
 2774:                         }
 2775:                     } else {
 2776:                         $canselect{$type} = 
 2777:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2778:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2779:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2780:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2781:                         if (@choices > 0) {
 2782:                             foreach my $lonhost (@choices) {
 2783:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2784:                             }
 2785:                         }
 2786:                         $canselect{$type} .= '</select>'."\n";
 2787:                     }
 2788:                 } else {
 2789:                     $current{$type} = &mt('Could not be determined');
 2790:                     if ($type eq 'primary') {
 2791:                         $canselect{$type} =  $othercontrol;
 2792:                     }
 2793:                 }
 2794:                 if ($type eq 'default') {
 2795:                     $datatable .= '<tr'.$css_class.'>';
 2796:                 }
 2797:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2798:                               '<td>'.$current{$type}.'</td>'."\n".
 2799:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2800:             }
 2801:             $itemcount ++;
 2802:         }
 2803:     }
 2804:     $$rowtotal += $itemcount;
 2805:     return $datatable;
 2806: }
 2807: 
 2808: sub possible_newspares {
 2809:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2810:     my $serverhostname = &Apache::lonnet::hostname($server);
 2811:     my %excluded;
 2812:     if ($serverhostname ne '') {
 2813:         %excluded = (
 2814:                        $serverhostname => 1,
 2815:                     );
 2816:     }
 2817:     if (ref($currspares) eq 'HASH') {
 2818:         foreach my $type (keys(%{$currspares})) {
 2819:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2820:                 if (@{$currspares->{$type}} > 0) {
 2821:                     foreach my $curr (@{$currspares->{$type}}) {
 2822:                         my $hostname = &Apache::lonnet::hostname($curr);
 2823:                         $excluded{$hostname} = 1;
 2824:                     }
 2825:                 }
 2826:             }
 2827:         }
 2828:     }
 2829:     my @choices;
 2830:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2831:         if (keys(%{$serverhomes}) > 1) {
 2832:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2833:                 unless ($excluded{$name}) {
 2834:                     if (exists($altids->{$serverhomes->{$name}})) {
 2835:                         push(@choices,$altids->{$serverhomes->{$name}});
 2836:                     } else {
 2837:                         push(@choices,$serverhomes->{$name});
 2838:                     }
 2839:                 }
 2840:             }
 2841:         }
 2842:     }
 2843:     return sort(@choices);
 2844: }
 2845: 
 2846: sub print_loadbalancing {
 2847:     my ($dom,$settings,$rowtotal) = @_;
 2848:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2849:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2850:     my $numinrow = 1;
 2851:     my $datatable;
 2852:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2853:     my ($currbalancer,$currtargets,$currrules);
 2854:     if (keys(%servers) > 1) {
 2855:         if (ref($settings) eq 'HASH') {
 2856:             $currbalancer = $settings->{'lonhost'};
 2857:             $currtargets = $settings->{'targets'};
 2858:             $currrules = $settings->{'rules'};
 2859:         } else {
 2860:             ($currbalancer,$currtargets) = 
 2861:                 &Apache::lonnet::get_lonbalancer_config(\%servers);
 2862:         }
 2863:     } else {
 2864:         return;
 2865:     }
 2866:     my ($othertitle,$usertypes,$types) =
 2867:         &Apache::loncommon::sorted_inst_types($dom);
 2868:     my $rownum = 6;
 2869:     if (ref($types) eq 'ARRAY') {
 2870:         $rownum += scalar(@{$types});
 2871:     }
 2872:     my $css_class = ' class="LC_odd_row"';
 2873:     my $targets_div_style = 'display: none';
 2874:     my $disabled_div_style = 'display: block';
 2875:     my $homedom_div_style = 'display: none';
 2876:     $datatable = '<tr'.$css_class.'>'.
 2877:                  '<td rowspan="'.$rownum.'" valign="top">'.
 2878:                  '<p><select name="loadbalancing_lonhost" onchange="toggleTargets();">'."\n".
 2879:                  '<option value=""';
 2880:     if (($currbalancer eq '') || (!grep(/^\Q$currbalancer\E$/,keys(%servers)))) {
 2881:         $datatable .= ' selected="selected"';
 2882:     } else {
 2883:         $targets_div_style = 'display: block';
 2884:         $disabled_div_style = 'display: none';
 2885:         if ($dom eq &Apache::lonnet::host_domain($currbalancer)) {
 2886:             $homedom_div_style = 'display: block'; 
 2887:         }
 2888:     }
 2889:     $datatable .= '>'.&mt('None').'</option>'."\n";
 2890:     foreach my $lonhost (sort(keys(%servers))) {
 2891:         my $selected;
 2892:         if ($lonhost eq $currbalancer) {
 2893:             $selected .= ' selected="selected"';
 2894:         }
 2895:         $datatable .= '<option value="'.$lonhost.'"'.$selected.'>'.$lonhost.'</option>'."\n";
 2896:     }
 2897:     $datatable .= '</select></p></td><td rowspan="'.$rownum.'" valign="top">'.
 2898:                   '<div id="loadbalancing_disabled" style="'.$disabled_div_style.'">'.&mt('No dedicated Load Balancer').'</div>'."\n".
 2899:                   '<div id="loadbalancing_targets" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2900:     my ($numspares,@spares) = &count_servers($currbalancer,%servers);
 2901:     my @sparestypes = ('primary','default');
 2902:     my %typetitles = &sparestype_titles();
 2903:     foreach my $sparetype (@sparestypes) {
 2904:         my $targettable;
 2905:         for (my $i=0; $i<$numspares; $i++) {
 2906:             my $checked;
 2907:             if (ref($currtargets) eq 'HASH') {
 2908:                 if (ref($currtargets->{$sparetype}) eq 'ARRAY') {
 2909:                     if (grep(/^\Q$spares[$i]\E$/,@{$currtargets->{$sparetype}})) {
 2910:                         $checked = ' checked="checked"';
 2911:                     }
 2912:                 }
 2913:             }
 2914:             my $chkboxval;
 2915:             if (($currbalancer ne '') && (grep((/^\Q$currbalancer\E$/,keys(%servers))))) {
 2916:                 $chkboxval = $spares[$i];
 2917:             }
 2918:             $targettable .= '<td><label><input type="checkbox" name="loadbalancing_target_'.$sparetype.'"'.
 2919:                       $checked.' value="'.$chkboxval.'" id="loadbalancing_target_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$sparetype'".');" /><span id="loadbalancing_targettxt_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 2920:                       '</span></label></td>';
 2921:             my $rem = $i%($numinrow);
 2922:             if ($rem == 0) {
 2923:                 if ($i > 0) {
 2924:                     $targettable .= '</tr>';
 2925:                 }
 2926:                 $targettable .= '<tr>';
 2927:             }
 2928:         }
 2929:         if ($targettable ne '') {
 2930:             my $rem = $numspares%($numinrow);
 2931:             my $colsleft = $numinrow - $rem;
 2932:             if ($colsleft > 1 ) {
 2933:                 $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2934:                                 '&nbsp;</td>';
 2935:             } elsif ($colsleft == 1) {
 2936:                 $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 2937:             }
 2938:             $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 2939:                            '<table><tr>'.$targettable.'</table><br />';
 2940:         }
 2941:     }
 2942:     $datatable .= '</div></td></tr>'.
 2943:                   &loadbalancing_rules($dom,$intdom,$currrules,$othertitle,
 2944:                                        $usertypes,$types,\%servers,$currbalancer,
 2945:                                        $targets_div_style,$homedom_div_style,$css_class);
 2946:     $$rowtotal += $rownum;
 2947:     return $datatable;
 2948: }
 2949: 
 2950: sub loadbalancing_rules {
 2951:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 2952:         $currbalancer,$targets_div_style,$homedom_div_style,$css_class) = @_;
 2953:     my $output;
 2954:     my ($alltypes,$othertypes,$titles) = 
 2955:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 2956:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 2957:         foreach my $type (@{$alltypes}) {
 2958:             my $current;
 2959:             if (ref($currrules) eq 'HASH') {
 2960:                 $current = $currrules->{$type};
 2961:             }
 2962:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 2963:                 if ($dom ne &Apache::lonnet::host_domain($currbalancer)) {
 2964:                     $current = '';
 2965:                 }
 2966:             }
 2967:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 2968:                                              $servers,$currbalancer,$dom,
 2969:                                              $targets_div_style,$homedom_div_style,$css_class);
 2970:         }
 2971:     }
 2972:     return $output;
 2973: }
 2974: 
 2975: sub loadbalancing_titles {
 2976:     my ($dom,$intdom,$usertypes,$types) = @_;
 2977:     my %othertypes = (
 2978:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 2979:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 2980:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 2981:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 2982:                      );
 2983:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 2984:     if (ref($types) eq 'ARRAY') {
 2985:         unshift(@alltypes,@{$types},'default');
 2986:     }
 2987:     my %titles;
 2988:     foreach my $type (@alltypes) {
 2989:         if ($type =~ /^_LC_/) {
 2990:             $titles{$type} = $othertypes{$type};
 2991:         } elsif ($type eq 'default') {
 2992:             $titles{$type} = &mt('All users from [_1]',$dom);
 2993:             if (ref($types) eq 'ARRAY') {
 2994:                 if (@{$types} > 0) {
 2995:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 2996:                 }
 2997:             }
 2998:         } elsif (ref($usertypes) eq 'HASH') {
 2999:             $titles{$type} = $usertypes->{$type};
 3000:         }
 3001:     }
 3002:     return (\@alltypes,\%othertypes,\%titles);
 3003: }
 3004: 
 3005: sub loadbalance_rule_row {
 3006:     my ($type,$title,$current,$servers,$currbalancer,$dom,$targets_div_style,
 3007:         $homedom_div_style,$css_class) = @_;
 3008:     my @rulenames = ('default','homeserver');
 3009:     my %ruletitles = &offloadtype_text();
 3010:     if ($type eq '_LC_external') {
 3011:         push(@rulenames,'externalbalancer');
 3012:     } else {
 3013:         push(@rulenames,'specific');
 3014:     }
 3015:     push(@rulenames,'none');
 3016:     my $style = $targets_div_style;
 3017:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3018:         $style = $homedom_div_style;
 3019:     }
 3020:     my $output = 
 3021:         '<tr'.$css_class.'><td valign="top"><div id="balanceruletitle_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3022:         '<td><div id="balancerule_'.$type.'" style="'.$style.'">'."\n";
 3023:     for (my $i=0; $i<@rulenames; $i++) {
 3024:         my $rule = $rulenames[$i];
 3025:         my ($checked,$extra);
 3026:         if ($rulenames[$i] eq 'default') {
 3027:             $rule = '';
 3028:         }
 3029:         if ($rulenames[$i] eq 'specific') {
 3030:             if (ref($servers) eq 'HASH') {
 3031:                 my $default;
 3032:                 if (($current ne '') && (exists($servers->{$current}))) {
 3033:                     $checked = ' checked="checked"';
 3034:                 }
 3035:                 unless ($checked) {
 3036:                     $default = ' selected="selected"';
 3037:                 }
 3038:                 $extra = ':&nbsp;<select name="loadbalancing_singleserver_'.$type.
 3039:                          '" id="loadbalancing_singleserver_'.$type.
 3040:                          '" onchange="singleServerToggle('."'$type'".')">'."\n".
 3041:                          '<option value=""'.$default.'></option>'."\n";
 3042:                 foreach my $lonhost (sort(keys(%{$servers}))) {
 3043:                     next if ($lonhost eq $currbalancer);
 3044:                     my $selected;
 3045:                     if ($lonhost eq $current) {
 3046:                         $selected = ' selected="selected"';
 3047:                     }
 3048:                     $extra .= '<option value="'.$lonhost.'"'.$selected.'>'.$lonhost.'</option>';
 3049:                 }
 3050:                 $extra .= '</select>';
 3051:             }
 3052:         } elsif ($rule eq $current) {
 3053:             $checked = ' checked="checked"';
 3054:         }
 3055:         $output .= '<span class="LC_nobreak"><label>'.
 3056:                    '<input type="radio" name="loadbalancing_rules_'.$type.
 3057:                    '" id="loadbalancing_rules_'.$type.'_'.$i.'" value="'.
 3058:                    $rule.'" onclick="balanceruleChange('."this.form,'$type'".
 3059:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3060:                    '</label>'.$extra.'</span><br />'."\n";
 3061:     }
 3062:     $output .= '</div></td></tr>'."\n";
 3063:     return $output;
 3064: }
 3065: 
 3066: sub offloadtype_text {
 3067:     my %ruletitles = &Apache::lonlocal::texthash (
 3068:            'default'          => 'Offloads to default destinations',
 3069:            'homeserver'       => "Offloads to user's home server",
 3070:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3071:            'specific'         => 'Offloads to specific server',
 3072:            'none'             => 'No offload',
 3073:     );
 3074:     return %ruletitles;
 3075: }
 3076: 
 3077: sub sparestype_titles {
 3078:     my %typestitles = &Apache::lonlocal::texthash (
 3079:                           'primary' => 'primary',
 3080:                           'default' => 'default',
 3081:                       );
 3082:     return %typestitles;
 3083: }
 3084: 
 3085: sub contact_titles {
 3086:     my %titles = &Apache::lonlocal::texthash (
 3087:                    'supportemail' => 'Support E-mail address',
 3088:                    'adminemail'   => 'Default Server Admin E-mail address',
 3089:                    'errormail'    => 'Error reports to be e-mailed to',
 3090:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3091:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3092:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3093:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3094:                  );
 3095:     my %short_titles = &Apache::lonlocal::texthash (
 3096:                            adminemail   => 'Admin E-mail address',
 3097:                            supportemail => 'Support E-mail',
 3098:                        );   
 3099:     return (\%titles,\%short_titles);
 3100: }
 3101: 
 3102: sub tool_titles {
 3103:     my %titles = &Apache::lonlocal::texthash (
 3104:                      aboutme    => 'Personal web page',
 3105:                      blog       => 'Blog',
 3106:                      webdav     => 'WebDAV',
 3107:                      portfolio  => 'Portfolio',
 3108:                      official   => 'Official courses (with institutional codes)',
 3109:                      unofficial => 'Unofficial courses',
 3110:                      community  => 'Communities',
 3111:                  );
 3112:     return %titles;
 3113: }
 3114: 
 3115: sub courserequest_titles {
 3116:     my %titles = &Apache::lonlocal::texthash (
 3117:                                    official   => 'Official',
 3118:                                    unofficial => 'Unofficial',
 3119:                                    community  => 'Communities',
 3120:                                    norequest  => 'Not allowed',
 3121:                                    approval   => 'Approval by Dom. Coord.',
 3122:                                    validate   => 'With validation',
 3123:                                    autolimit  => 'Numerical limit',
 3124:                                    unlimited  => '(blank for unlimited)',
 3125:                  );
 3126:     return %titles;
 3127: }
 3128: 
 3129: sub authorrequest_titles {
 3130:     my %titles = &Apache::lonlocal::texthash (
 3131:                                    norequest  => 'Not allowed',
 3132:                                    approval   => 'Approval by Dom. Coord.',
 3133:                                    automatic  => 'Automatic approval',
 3134:                  );
 3135:     return %titles;
 3136: } 
 3137: 
 3138: sub courserequest_conditions {
 3139:     my %conditions = &Apache::lonlocal::texthash (
 3140:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3141:        validate   => '(Processing of request subject to instittutional validation).',
 3142:                  );
 3143:     return %conditions;
 3144: }
 3145: 
 3146: 
 3147: sub print_usercreation {
 3148:     my ($position,$dom,$settings,$rowtotal) = @_;
 3149:     my $numinrow = 4;
 3150:     my $datatable;
 3151:     if ($position eq 'top') {
 3152:         $$rowtotal ++;
 3153:         my $rowcount = 0;
 3154:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3155:         if (ref($rules) eq 'HASH') {
 3156:             if (keys(%{$rules}) > 0) {
 3157:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3158:                                                 $ruleorder,$numinrow,$rowcount);
 3159:                 $$rowtotal ++;
 3160:                 $rowcount ++;
 3161:             }
 3162:         }
 3163:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3164:         if (ref($idrules) eq 'HASH') {
 3165:             if (keys(%{$idrules}) > 0) {
 3166:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3167:                                                 $idruleorder,$numinrow,$rowcount);
 3168:                 $$rowtotal ++;
 3169:                 $rowcount ++;
 3170:             }
 3171:         }
 3172:         my ($emailrules,$emailruleorder) = 
 3173:             &Apache::lonnet::inst_userrules($dom,'email');
 3174:         if (ref($emailrules) eq 'HASH') {
 3175:             if (keys(%{$emailrules}) > 0) {
 3176:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3177:                                                 $emailruleorder,$numinrow,$rowcount);
 3178:                 $$rowtotal ++;
 3179:                 $rowcount ++;
 3180:             }
 3181:         }
 3182:         if ($rowcount == 0) {
 3183:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3184:             $$rowtotal ++;
 3185:             $rowcount ++;
 3186:         }
 3187:     } elsif ($position eq 'middle') {
 3188:         my @creators = ('author','course','requestcrs','selfcreate');
 3189:         my ($rules,$ruleorder) =
 3190:             &Apache::lonnet::inst_userrules($dom,'username');
 3191:         my %lt = &usercreation_types();
 3192:         my %checked;
 3193:         my @selfcreate; 
 3194:         if (ref($settings) eq 'HASH') {
 3195:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3196:                 foreach my $item (@creators) {
 3197:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3198:                 }
 3199:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3200:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3201:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3202:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3203:                         @selfcreate = ('email','login','sso');
 3204:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3205:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3206:                     }
 3207:                 }
 3208:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3209:                 foreach my $item (@creators) {
 3210:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3211:                         $checked{$item} = 'none';
 3212:                     }
 3213:                 }
 3214:             }
 3215:         }
 3216:         my $rownum = 0;
 3217:         foreach my $item (@creators) {
 3218:             $rownum ++;
 3219:             if ($item ne 'selfcreate') {  
 3220:                 if ($checked{$item} eq '') {
 3221:                     $checked{$item} = 'any';
 3222:                 }
 3223:             }
 3224:             my $css_class;
 3225:             if ($rownum%2) {
 3226:                 $css_class = '';
 3227:             } else {
 3228:                 $css_class = ' class="LC_odd_row" ';
 3229:             }
 3230:             $datatable .= '<tr'.$css_class.'>'.
 3231:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3232:                          '</span></td><td align="right">';
 3233:             my @options;
 3234:             if ($item eq 'selfcreate') {
 3235:                 push(@options,('email','login','sso'));
 3236:             } else {
 3237:                 @options = ('any');
 3238:                 if (ref($rules) eq 'HASH') {
 3239:                     if (keys(%{$rules}) > 0) {
 3240:                         push(@options,('official','unofficial'));
 3241:                     }
 3242:                 }
 3243:                 push(@options,'none');
 3244:             }
 3245:             foreach my $option (@options) {
 3246:                 my $type = 'radio';
 3247:                 my $check = ' ';
 3248:                 if ($item eq 'selfcreate') {
 3249:                     $type = 'checkbox';
 3250:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3251:                         $check = ' checked="checked" ';
 3252:                     }
 3253:                 } else {
 3254:                     if ($checked{$item} eq $option) {
 3255:                         $check = ' checked="checked" ';
 3256:                     }
 3257:                 } 
 3258:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3259:                               '<input type="'.$type.'" name="can_createuser_'.
 3260:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3261:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3262:             }
 3263:             $datatable .= '</td></tr>';
 3264:         }
 3265:         my ($othertitle,$usertypes,$types) =
 3266:             &Apache::loncommon::sorted_inst_types($dom);
 3267:         my $createsettings;
 3268:         if (ref($settings) eq 'HASH') {
 3269:             $createsettings = $settings->{cancreate};
 3270:         }
 3271:         if (ref($usertypes) eq 'HASH') {
 3272:             if (keys(%{$usertypes}) > 0) {
 3273:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3274:                                              $dom,$numinrow,$othertitle,
 3275:                                              'statustocreate');
 3276:                 $$rowtotal ++;
 3277:             }
 3278:         }
 3279:         $datatable .= &captcha_choice('cancreate',$createsettings);
 3280:     } else {
 3281:         my @contexts = ('author','course','domain');
 3282:         my @authtypes = ('int','krb4','krb5','loc');
 3283:         my %checked;
 3284:         if (ref($settings) eq 'HASH') {
 3285:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3286:                 foreach my $item (@contexts) {
 3287:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3288:                         foreach my $auth (@authtypes) {
 3289:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3290:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3291:                             }
 3292:                         }
 3293:                     }
 3294:                 }
 3295:             }
 3296:         } else {
 3297:             foreach my $item (@contexts) {
 3298:                 foreach my $auth (@authtypes) {
 3299:                     $checked{$item}{$auth} = ' checked="checked" ';
 3300:                 }
 3301:             }
 3302:         }
 3303:         my %title = &context_names();
 3304:         my %authname = &authtype_names();
 3305:         my $rownum = 0;
 3306:         my $css_class; 
 3307:         foreach my $item (@contexts) {
 3308:             if ($rownum%2) {
 3309:                 $css_class = '';
 3310:             } else {
 3311:                 $css_class = ' class="LC_odd_row" ';
 3312:             }
 3313:             $datatable .=   '<tr'.$css_class.'>'.
 3314:                             '<td>'.$title{$item}.
 3315:                             '</td><td class="LC_left_item">'.
 3316:                             '<span class="LC_nobreak">';
 3317:             foreach my $auth (@authtypes) {
 3318:                 $datatable .= '<label>'. 
 3319:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3320:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3321:                               $authname{$auth}.'</label>&nbsp;';
 3322:             }
 3323:             $datatable .= '</span></td></tr>';
 3324:             $rownum ++;
 3325:         }
 3326:         $$rowtotal += $rownum;
 3327:     }
 3328:     return $datatable;
 3329: }
 3330: 
 3331: sub captcha_choice {
 3332:     my ($context,$settings) = @_;
 3333:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3334:     my %lt = &captcha_phrases();
 3335:     $keyentry = 'hidden';
 3336:     if ($context eq 'cancreate') {
 3337:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3338:     } elsif ($context eq 'help') {
 3339:         $rowname =  &mt('CAPTCHA validation');
 3340:     }
 3341:     if (ref($settings) eq 'HASH') {
 3342:         if ($settings->{'captcha'}) {
 3343:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3344:         } else {
 3345:             $checked{'original'} = ' checked="checked"';
 3346:         }
 3347:         if ($settings->{'captcha'} eq 'recaptcha') {
 3348:             $pubtext = $lt{'pub'};
 3349:             $privtext = $lt{'priv'};
 3350:             $keyentry = 'text';
 3351:         }
 3352:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3353:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3354:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3355:         }
 3356:     } else {
 3357:         $checked{'original'} = ' checked="checked"';
 3358:     }
 3359:     my $output = '<tr class="LC_odd_row">'.
 3360:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_right_item" colspan="2">'."\n".
 3361:                  '<table><tr><td>'."\n";
 3362:     foreach my $option ('original','recaptcha','notused') {
 3363:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3364:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3365:                    $lt{$option}.'</label></span>';
 3366:         unless ($option eq 'notused') {
 3367:             $output .= ('&nbsp;'x2)."\n";
 3368:         }
 3369:     }
 3370: #
 3371: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3372: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3373: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu) 
 3374: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3375: #  
 3376:     $output .= '</td></tr>'."\n".
 3377:                '<tr><td>'."\n".
 3378:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3379:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3380:                $currpub.'" size="40" /></span><br />'."\n".
 3381:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3382:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3383:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3384:                '</td></tr>';
 3385:     return $output;
 3386: }
 3387: 
 3388: sub user_formats_row {
 3389:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3390:     my $output;
 3391:     my %text = (
 3392:                    'username' => 'new usernames',
 3393:                    'id'       => 'IDs',
 3394:                    'email'    => 'self-created accounts (e-mail)',
 3395:                );
 3396:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3397:     $output = '<tr '.$css_class.'>'.
 3398:               '<td><span class="LC_nobreak">';
 3399:     if ($type eq 'email') {
 3400:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3401:     } else {
 3402:         $output .= &mt("Format rules to check for $text{$type}: ");
 3403:     }
 3404:     $output .= '</span></td>'.
 3405:                '<td class="LC_left_item" colspan="2"><table>';
 3406:     my $rem;
 3407:     if (ref($ruleorder) eq 'ARRAY') {
 3408:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3409:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3410:                 my $rem = $i%($numinrow);
 3411:                 if ($rem == 0) {
 3412:                     if ($i > 0) {
 3413:                         $output .= '</tr>';
 3414:                     }
 3415:                     $output .= '<tr>';
 3416:                 }
 3417:                 my $check = ' ';
 3418:                 if (ref($settings) eq 'HASH') {
 3419:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3420:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3421:                             $check = ' checked="checked" ';
 3422:                         }
 3423:                     }
 3424:                 }
 3425:                 $output .= '<td class="LC_left_item">'.
 3426:                            '<span class="LC_nobreak"><label>'.
 3427:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3428:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3429:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3430:             }
 3431:         }
 3432:         $rem = @{$ruleorder}%($numinrow);
 3433:     }
 3434:     my $colsleft = $numinrow - $rem;
 3435:     if ($colsleft > 1 ) {
 3436:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3437:                    '&nbsp;</td>';
 3438:     } elsif ($colsleft == 1) {
 3439:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3440:     }
 3441:     $output .= '</tr></table></td></tr>';
 3442:     return $output;
 3443: }
 3444: 
 3445: sub usercreation_types {
 3446:     my %lt = &Apache::lonlocal::texthash (
 3447:                     author     => 'When adding a co-author',
 3448:                     course     => 'When adding a user to a course',
 3449:                     requestcrs => 'When requesting a course',
 3450:                     selfcreate => 'User creates own account', 
 3451:                     any        => 'Any',
 3452:                     official   => 'Institutional only ',
 3453:                     unofficial => 'Non-institutional only',
 3454:                     email      => 'E-mail address',
 3455:                     login      => 'Institutional Login',
 3456:                     sso        => 'SSO', 
 3457:                     none       => 'None',
 3458:     );
 3459:     return %lt;
 3460: }
 3461: 
 3462: sub authtype_names {
 3463:     my %lt = &Apache::lonlocal::texthash(
 3464:                       int    => 'Internal',
 3465:                       krb4   => 'Kerberos 4',
 3466:                       krb5   => 'Kerberos 5',
 3467:                       loc    => 'Local',
 3468:                   );
 3469:     return %lt;
 3470: }
 3471: 
 3472: sub context_names {
 3473:     my %context_title = &Apache::lonlocal::texthash(
 3474:        author => 'Creating users when an Author',
 3475:        course => 'Creating users when in a course',
 3476:        domain => 'Creating users when a Domain Coordinator',
 3477:     );
 3478:     return %context_title;
 3479: }
 3480: 
 3481: sub print_usermodification {
 3482:     my ($position,$dom,$settings,$rowtotal) = @_;
 3483:     my $numinrow = 4;
 3484:     my ($context,$datatable,$rowcount);
 3485:     if ($position eq 'top') {
 3486:         $rowcount = 0;
 3487:         $context = 'author'; 
 3488:         foreach my $role ('ca','aa') {
 3489:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3490:                                                    $numinrow,$rowcount);
 3491:             $$rowtotal ++;
 3492:             $rowcount ++;
 3493:         }
 3494:     } elsif ($position eq 'middle') {
 3495:         $context = 'course';
 3496:         $rowcount = 0;
 3497:         foreach my $role ('st','ep','ta','in','cr') {
 3498:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3499:                                                    $numinrow,$rowcount);
 3500:             $$rowtotal ++;
 3501:             $rowcount ++;
 3502:         }
 3503:     } elsif ($position eq 'bottom') {
 3504:         $context = 'selfcreate';
 3505:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3506:         $usertypes->{'default'} = $othertitle;
 3507:         if (ref($types) eq 'ARRAY') {
 3508:             push(@{$types},'default');
 3509:             $usertypes->{'default'} = $othertitle;
 3510:             foreach my $status (@{$types}) {
 3511:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3512:                                                        $numinrow,$rowcount,$usertypes);
 3513:                 $$rowtotal ++;
 3514:                 $rowcount ++;
 3515:             }
 3516:         }
 3517:     }
 3518:     return $datatable;
 3519: }
 3520: 
 3521: sub print_defaults {
 3522:     my ($dom,$rowtotal) = @_;
 3523:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3524:                  'datelocale_def','portal_def');
 3525:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3526:     my $titles = &defaults_titles($dom);
 3527:     my $rownum = 0;
 3528:     my ($datatable,$css_class);
 3529:     foreach my $item (@items) {
 3530:         if ($rownum%2) {
 3531:             $css_class = '';
 3532:         } else {
 3533:             $css_class = ' class="LC_odd_row" ';
 3534:         }
 3535:         $datatable .= '<tr'.$css_class.'>'.
 3536:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3537:                   '</span></td><td class="LC_right_item">';
 3538:         if ($item eq 'auth_def') {
 3539:             my @authtypes = ('internal','krb4','krb5','localauth');
 3540:             my %shortauth = (
 3541:                              internal => 'int',
 3542:                              krb4 => 'krb4',
 3543:                              krb5 => 'krb5',
 3544:                              localauth  => 'loc'
 3545:                            );
 3546:             my %authnames = &authtype_names();
 3547:             foreach my $auth (@authtypes) {
 3548:                 my $checked = ' ';
 3549:                 if ($domdefaults{$item} eq $auth) {
 3550:                     $checked = ' checked="checked" ';
 3551:                 }
 3552:                 $datatable .= '<label><input type="radio" name="'.$item.
 3553:                               '" value="'.$auth.'"'.$checked.'/>'.
 3554:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3555:             }
 3556:         } elsif ($item eq 'timezone_def') {
 3557:             my $includeempty = 1;
 3558:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3559:         } elsif ($item eq 'datelocale_def') {
 3560:             my $includeempty = 1;
 3561:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3562:         } else {
 3563:             my $size;
 3564:             if ($item eq 'portal_def') {
 3565:                 $size = ' size="25"';
 3566:             }
 3567:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3568:                           $domdefaults{$item}.'"'.$size.' />';
 3569:         }
 3570:         $datatable .= '</td></tr>';
 3571:         $rownum ++;
 3572:     }
 3573:     $$rowtotal += $rownum;
 3574:     return $datatable;
 3575: }
 3576: 
 3577: sub defaults_titles {
 3578:     my ($dom) = @_;
 3579:     my %titles = &Apache::lonlocal::texthash (
 3580:                    'auth_def'      => 'Default authentication type',
 3581:                    'auth_arg_def'  => 'Default authentication argument',
 3582:                    'lang_def'      => 'Default language',
 3583:                    'timezone_def'  => 'Default timezone',
 3584:                    'datelocale_def' => 'Default locale for dates',
 3585:                    'portal_def'     => 'Portal/Default URL',
 3586:                  );
 3587:     if ($dom) {
 3588:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3589:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3590:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3591:         $protocol = 'http' if ($protocol ne 'https');
 3592:         if ($uint_dom) {
 3593:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3594:                                          $uint_dom);
 3595:         }
 3596:     }
 3597:     return (\%titles);
 3598: }
 3599: 
 3600: sub print_scantronformat {
 3601:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3602:     my $itemcount = 1;
 3603:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3604:         %confhash);
 3605:     my $switchserver = &check_switchserver($dom,$confname);
 3606:     my %lt = &Apache::lonlocal::texthash (
 3607:                 default => 'Default bubblesheet format file error',
 3608:                 custom  => 'Custom bubblesheet format file error',
 3609:              );
 3610:     my %scantronfiles = (
 3611:         default => 'default.tab',
 3612:         custom => 'custom.tab',
 3613:     );
 3614:     foreach my $key (keys(%scantronfiles)) {
 3615:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3616:                               .$scantronfiles{$key};
 3617:     }
 3618:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3619:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3620:         if (!$switchserver) {
 3621:             my $servadm = $r->dir_config('lonAdmEMail');
 3622:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3623:             if ($configuserok eq 'ok') {
 3624:                 if ($author_ok eq 'ok') {
 3625:                     my %legacyfile = (
 3626:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3627:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3628:                     );
 3629:                     my %md5chk;
 3630:                     foreach my $type (keys(%legacyfile)) {
 3631:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3632:                         chomp($md5chk{$type});
 3633:                     }
 3634:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3635:                         foreach my $type (keys(%legacyfile)) {
 3636:                             ($scantronurls{$type},my $error) = 
 3637:                                 &legacy_scantronformat($r,$dom,$confname,
 3638:                                                  $type,$legacyfile{$type},
 3639:                                                  $scantronurls{$type},
 3640:                                                  $scantronfiles{$type});
 3641:                             if ($error ne '') {
 3642:                                 $error{$type} = $error;
 3643:                             }
 3644:                         }
 3645:                         if (keys(%error) == 0) {
 3646:                             $is_custom = 1;
 3647:                             $confhash{'scantron'}{'scantronformat'} = 
 3648:                                 $scantronurls{'custom'};
 3649:                             my $putresult = 
 3650:                                 &Apache::lonnet::put_dom('configuration',
 3651:                                                          \%confhash,$dom);
 3652:                             if ($putresult ne 'ok') {
 3653:                                 $error{'custom'} = 
 3654:                                     '<span class="LC_error">'.
 3655:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3656:                             }
 3657:                         }
 3658:                     } else {
 3659:                         ($scantronurls{'default'},my $error) =
 3660:                             &legacy_scantronformat($r,$dom,$confname,
 3661:                                           'default',$legacyfile{'default'},
 3662:                                           $scantronurls{'default'},
 3663:                                           $scantronfiles{'default'});
 3664:                         if ($error eq '') {
 3665:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3666:                             my $putresult =
 3667:                                 &Apache::lonnet::put_dom('configuration',
 3668:                                                          \%confhash,$dom);
 3669:                             if ($putresult ne 'ok') {
 3670:                                 $error{'default'} =
 3671:                                     '<span class="LC_error">'.
 3672:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3673:                             }
 3674:                         } else {
 3675:                             $error{'default'} = $error;
 3676:                         }
 3677:                     }
 3678:                 }
 3679:             }
 3680:         } else {
 3681:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3682:         }
 3683:     }
 3684:     if (ref($settings) eq 'HASH') {
 3685:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3686:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3687:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3688:                 $scantronurl = '';
 3689:             } else {
 3690:                 $scantronurl = $settings->{'scantronformat'};
 3691:             }
 3692:             $is_custom = 1;
 3693:         } else {
 3694:             $scantronurl = $scantronurls{'default'};
 3695:         }
 3696:     } else {
 3697:         if ($is_custom) {
 3698:             $scantronurl = $scantronurls{'custom'};
 3699:         } else {
 3700:             $scantronurl = $scantronurls{'default'};
 3701:         }
 3702:     }
 3703:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3704:     $datatable .= '<tr'.$css_class.'>';
 3705:     if (!$is_custom) {
 3706:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3707:                       '<span class="LC_nobreak">';
 3708:         if ($scantronurl) {
 3709:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3710:                           &mt('Default bubblesheet format file').'</a>';
 3711:         } else {
 3712:             $datatable = &mt('File unavailable for display');
 3713:         }
 3714:         $datatable .= '</span></td>';
 3715:         if (keys(%error) == 0) { 
 3716:             $datatable .= '<td valign="bottom">';
 3717:             if (!$switchserver) {
 3718:                 $datatable .= &mt('Upload:').'<br />';
 3719:             }
 3720:         } else {
 3721:             my $errorstr;
 3722:             foreach my $key (sort(keys(%error))) {
 3723:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3724:             }
 3725:             $datatable .= '<td>'.$errorstr;
 3726:         }
 3727:     } else {
 3728:         if (keys(%error) > 0) {
 3729:             my $errorstr;
 3730:             foreach my $key (sort(keys(%error))) {
 3731:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3732:             } 
 3733:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3734:         } elsif ($scantronurl) {
 3735:             $datatable .= '<td><span class="LC_nobreak">'.
 3736:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3737:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3738:                           '<input type="checkbox" name="scantronformat_del"'.
 3739:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3740:                           '<td><span class="LC_nobreak">&nbsp;'.
 3741:                           &mt('Replace:').'</span><br />';
 3742:         }
 3743:     }
 3744:     if (keys(%error) == 0) {
 3745:         if ($switchserver) {
 3746:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3747:         } else {
 3748:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3749:                          '<input type="file" name="scantronformat" /></span>';
 3750:         }
 3751:     }
 3752:     $datatable .= '</td></tr>';
 3753:     $$rowtotal ++;
 3754:     return $datatable;
 3755: }
 3756: 
 3757: sub legacy_scantronformat {
 3758:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3759:     my ($url,$error);
 3760:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3761:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3762:         (my $result,$url) =
 3763:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3764:                          '','',$newfile);
 3765:         if ($result ne 'ok') {
 3766:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3767:         }
 3768:     }
 3769:     return ($url,$error);
 3770: }
 3771: 
 3772: sub print_coursecategories {
 3773:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3774:     my $datatable;
 3775:     if ($position eq 'top') {
 3776:         my $toggle_cats_crs = ' ';
 3777:         my $toggle_cats_dom = ' checked="checked" ';
 3778:         my $can_cat_crs = ' ';
 3779:         my $can_cat_dom = ' checked="checked" ';
 3780:         my $toggle_catscomm_comm = ' ';
 3781:         my $toggle_catscomm_dom = ' checked="checked" ';
 3782:         my $can_catcomm_comm = ' ';
 3783:         my $can_catcomm_dom = ' checked="checked" ';
 3784: 
 3785:         if (ref($settings) eq 'HASH') {
 3786:             if ($settings->{'togglecats'} eq 'crs') {
 3787:                 $toggle_cats_crs = $toggle_cats_dom;
 3788:                 $toggle_cats_dom = ' ';
 3789:             }
 3790:             if ($settings->{'categorize'} eq 'crs') {
 3791:                 $can_cat_crs = $can_cat_dom;
 3792:                 $can_cat_dom = ' ';
 3793:             }
 3794:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3795:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3796:                 $toggle_catscomm_dom = ' ';
 3797:             }
 3798:             if ($settings->{'categorizecomm'} eq 'comm') {
 3799:                 $can_catcomm_comm = $can_catcomm_dom;
 3800:                 $can_catcomm_dom = ' ';
 3801:             }
 3802:         }
 3803:         my %title = &Apache::lonlocal::texthash (
 3804:                      togglecats     => 'Show/Hide a course in catalog',
 3805:                      togglecatscomm => 'Show/Hide a community in catalog',
 3806:                      categorize     => 'Assign a category to a course',
 3807:                      categorizecomm => 'Assign a category to a community',
 3808:                     );
 3809:         my %level = &Apache::lonlocal::texthash (
 3810:                      dom  => 'Set in Domain',
 3811:                      crs  => 'Set in Course',
 3812:                      comm => 'Set in Community',
 3813:                     );
 3814:         $datatable = '<tr class="LC_odd_row">'.
 3815:                   '<td>'.$title{'togglecats'}.'</td>'.
 3816:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3817:                   '<input type="radio" name="togglecats"'.
 3818:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3819:                   '<label><input type="radio" name="togglecats"'.
 3820:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3821:                   '</tr><tr>'.
 3822:                   '<td>'.$title{'categorize'}.'</td>'.
 3823:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3824:                   '<label><input type="radio" name="categorize"'.
 3825:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3826:                   '<label><input type="radio" name="categorize"'.
 3827:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3828:                   '</tr><tr class="LC_odd_row">'.
 3829:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 3830:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3831:                   '<input type="radio" name="togglecatscomm"'.
 3832:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3833:                   '<label><input type="radio" name="togglecatscomm"'.
 3834:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3835:                   '</tr><tr>'.
 3836:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 3837:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3838:                   '<label><input type="radio" name="categorizecomm"'.
 3839:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3840:                   '<label><input type="radio" name="categorizecomm"'.
 3841:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3842:                   '</tr>';
 3843:         $$rowtotal += 4;
 3844:     } else {
 3845:         my $css_class;
 3846:         my $itemcount = 1;
 3847:         my $cathash; 
 3848:         if (ref($settings) eq 'HASH') {
 3849:             $cathash = $settings->{'cats'};
 3850:         }
 3851:         if (ref($cathash) eq 'HASH') {
 3852:             my (@cats,@trails,%allitems,%idx,@jsarray);
 3853:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 3854:                                                    \%allitems,\%idx,\@jsarray);
 3855:             my $maxdepth = scalar(@cats);
 3856:             my $colattrib = '';
 3857:             if ($maxdepth > 2) {
 3858:                 $colattrib = ' colspan="2" ';
 3859:             }
 3860:             my @path;
 3861:             if (@cats > 0) {
 3862:                 if (ref($cats[0]) eq 'ARRAY') {
 3863:                     my $numtop = @{$cats[0]};
 3864:                     my $maxnum = $numtop;
 3865:                     my %default_names = (
 3866:                           instcode    => &mt('Official courses'),
 3867:                           communities => &mt('Communities'),
 3868:                     );
 3869: 
 3870:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 3871:                         ($cathash->{'instcode::0'} eq '') ||
 3872:                         (!grep(/^communities$/,@{$cats[0]})) || 
 3873:                         ($cathash->{'communities::0'} eq '')) {
 3874:                         $maxnum ++;
 3875:                     }
 3876:                     my $lastidx;
 3877:                     for (my $i=0; $i<$numtop; $i++) {
 3878:                         my $parent = $cats[0][$i];
 3879:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3880:                         my $item = &escape($parent).'::0';
 3881:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 3882:                         $lastidx = $idx{$item};
 3883:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3884:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 3885:                         for (my $k=0; $k<=$maxnum; $k++) {
 3886:                             my $vpos = $k+1;
 3887:                             my $selstr;
 3888:                             if ($k == $i) {
 3889:                                 $selstr = ' selected="selected" ';
 3890:                             }
 3891:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3892:                         }
 3893:                         $datatable .= '</select></td><td>';
 3894:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 3895:                             $datatable .=  '<span class="LC_nobreak">'
 3896:                                            .$default_names{$parent}.'</span>';
 3897:                             if ($parent eq 'instcode') {
 3898:                                 $datatable .= '<br /><span class="LC_nobreak">('
 3899:                                               .&mt('with institutional codes')
 3900:                                               .')</span></td><td'.$colattrib.'>';
 3901:                             } else {
 3902:                                 $datatable .= '<table><tr><td>';
 3903:                             }
 3904:                             $datatable .= '<span class="LC_nobreak">'
 3905:                                           .'<label><input type="radio" name="'
 3906:                                           .$parent.'" value="1" checked="checked" />'
 3907:                                           .&mt('Display').'</label>';
 3908:                             if ($parent eq 'instcode') {
 3909:                                 $datatable .= '&nbsp;';
 3910:                             } else {
 3911:                                 $datatable .= '</span></td></tr><tr><td>'
 3912:                                               .'<span class="LC_nobreak">';
 3913:                             }
 3914:                             $datatable .= '<label><input type="radio" name="'
 3915:                                           .$parent.'" value="0" />'
 3916:                                           .&mt('Do not display').'</label></span>';
 3917:                             if ($parent eq 'communities') {
 3918:                                 $datatable .= '</td></tr></table>';
 3919:                             }
 3920:                             $datatable .= '</td>';
 3921:                         } else {
 3922:                             $datatable .= $parent
 3923:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 3924:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 3925:                         }
 3926:                         my $depth = 1;
 3927:                         push(@path,$parent);
 3928:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 3929:                         pop(@path);
 3930:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 3931:                         $itemcount ++;
 3932:                     }
 3933:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3934:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 3935:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 3936:                     for (my $k=0; $k<=$maxnum; $k++) {
 3937:                         my $vpos = $k+1;
 3938:                         my $selstr;
 3939:                         if ($k == $numtop) {
 3940:                             $selstr = ' selected="selected" ';
 3941:                         }
 3942:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3943:                     }
 3944:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 3945:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 3946:                                   .'</tr>'."\n";
 3947:                     $itemcount ++;
 3948:                     foreach my $default ('instcode','communities') {
 3949:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 3950:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3951:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 3952:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 3953:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 3954:                             for (my $k=0; $k<=$maxnum; $k++) {
 3955:                                 my $vpos = $k+1;
 3956:                                 my $selstr;
 3957:                                 if ($k == $maxnum) {
 3958:                                     $selstr = ' selected="selected" ';
 3959:                                 }
 3960:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3961:                             }
 3962:                             $datatable .= '</select></span></td>'.
 3963:                                           '<td><span class="LC_nobreak">'.
 3964:                                           $default_names{$default}.'</span>';
 3965:                             if ($default eq 'instcode') {
 3966:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 3967:                                               .&mt('with institutional codes').')</span>';
 3968:                             }
 3969:                             $datatable .= '</td>'
 3970:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 3971:                                           .&mt('Display').'</label>&nbsp;'
 3972:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 3973:                                           .&mt('Do not display').'</label></span></td></tr>';
 3974:                         }
 3975:                     }
 3976:                 }
 3977:             } else {
 3978:                 $datatable .= &initialize_categories($itemcount);
 3979:             }
 3980:         } else {
 3981:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 3982:                           .&initialize_categories($itemcount);
 3983:         }
 3984:         $$rowtotal += $itemcount;
 3985:     }
 3986:     return $datatable;
 3987: }
 3988: 
 3989: sub print_serverstatuses {
 3990:     my ($dom,$settings,$rowtotal) = @_;
 3991:     my $datatable;
 3992:     my @pages = &serverstatus_pages();
 3993:     my (%namedaccess,%machineaccess);
 3994:     foreach my $type (@pages) {
 3995:         $namedaccess{$type} = '';
 3996:         $machineaccess{$type}= '';
 3997:     }
 3998:     if (ref($settings) eq 'HASH') {
 3999:         foreach my $type (@pages) {
 4000:             if (exists($settings->{$type})) {
 4001:                 if (ref($settings->{$type}) eq 'HASH') {
 4002:                     foreach my $key (keys(%{$settings->{$type}})) {
 4003:                         if ($key eq 'namedusers') {
 4004:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4005:                         } elsif ($key eq 'machines') {
 4006:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4007:                         }
 4008:                     }
 4009:                 }
 4010:             }
 4011:         }
 4012:     }
 4013:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4014:     my $rownum = 0;
 4015:     my $css_class;
 4016:     foreach my $type (@pages) {
 4017:         $rownum ++;
 4018:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4019:         $datatable .= '<tr'.$css_class.'>'.
 4020:                       '<td><span class="LC_nobreak">'.
 4021:                       $titles->{$type}.'</span></td>'.
 4022:                       '<td class="LC_left_item">'.
 4023:                       '<input type="text" name="'.$type.'_namedusers" '.
 4024:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4025:                       '<td class="LC_right_item">'.
 4026:                       '<span class="LC_nobreak">'.
 4027:                       '<input type="text" name="'.$type.'_machines" '.
 4028:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4029:                       '</td></tr>'."\n";
 4030:     }
 4031:     $$rowtotal += $rownum;
 4032:     return $datatable;
 4033: }
 4034: 
 4035: sub serverstatus_pages {
 4036:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4037:             'clusterstatus','metadata_keywords','metadata_harvest',
 4038:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4039: }
 4040: 
 4041: sub coursecategories_javascript {
 4042:     my ($settings) = @_;
 4043:     my ($output,$jstext,$cathash);
 4044:     if (ref($settings) eq 'HASH') {
 4045:         $cathash = $settings->{'cats'};
 4046:     }
 4047:     if (ref($cathash) eq 'HASH') {
 4048:         my (@cats,@jsarray,%idx);
 4049:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4050:         if (@jsarray > 0) {
 4051:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4052:             for (my $i=0; $i<@jsarray; $i++) {
 4053:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4054:                     my $catstr = join('","',@{$jsarray[$i]});
 4055:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4056:                 }
 4057:             }
 4058:         }
 4059:     } else {
 4060:         $jstext  = '    var categories = Array(1);'."\n".
 4061:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4062:     }
 4063:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4064:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4065:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4066:     $output = <<"ENDSCRIPT";
 4067: <script type="text/javascript">
 4068: // <![CDATA[
 4069: function reorderCats(form,parent,item,idx) {
 4070:     var changedVal;
 4071: $jstext
 4072:     var newpos = 'addcategory_pos';
 4073:     var current = new Array;
 4074:     if (parent == '') {
 4075:         var has_instcode = 0;
 4076:         var maxtop = categories[idx].length;
 4077:         for (var j=0; j<maxtop; j++) {
 4078:             if (categories[idx][j] == 'instcode::0') {
 4079:                 has_instcode == 1;
 4080:             }
 4081:         }
 4082:         if (has_instcode == 0) {
 4083:             categories[idx][maxtop] = 'instcode_pos';
 4084:         }
 4085:     } else {
 4086:         newpos += '_'+parent;
 4087:     }
 4088:     var maxh = 1 + categories[idx].length;
 4089:     var current = new Array;
 4090:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4091:     if (item == newpos) {
 4092:         changedVal = newitemVal;
 4093:     } else {
 4094:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4095:         current[newitemVal] = newpos;
 4096:     }
 4097:     for (var i=0; i<categories[idx].length; i++) {
 4098:         var elementName = categories[idx][i];
 4099:         if (elementName != item) {
 4100:             if (form.elements[elementName]) {
 4101:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4102:                 current[currVal] = elementName;
 4103:             }
 4104:         }
 4105:     }
 4106:     var oldVal;
 4107:     for (var j=0; j<maxh; j++) {
 4108:         if (current[j] == undefined) {
 4109:             oldVal = j;
 4110:         }
 4111:     }
 4112:     if (oldVal < changedVal) {
 4113:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4114:            var elementName = current[k];
 4115:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4116:         }
 4117:     } else {
 4118:         for (var k=changedVal; k<oldVal; k++) {
 4119:             var elementName = current[k];
 4120:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4121:         }
 4122:     }
 4123:     return;
 4124: }
 4125: 
 4126: function categoryCheck(form) {
 4127:     if (form.elements['addcategory_name'].value == 'instcode') {
 4128:         alert('$instcode_reserved\\n$choose_again');
 4129:         return false;
 4130:     }
 4131:     if (form.elements['addcategory_name'].value == 'communities') {
 4132:         alert('$communities_reserved\\n$choose_again');
 4133:         return false;
 4134:     }
 4135:     return true;
 4136: }
 4137: 
 4138: // ]]>
 4139: </script>
 4140: 
 4141: ENDSCRIPT
 4142:     return $output;
 4143: }
 4144: 
 4145: sub initialize_categories {
 4146:     my ($itemcount) = @_;
 4147:     my ($datatable,$css_class,$chgstr);
 4148:     my %default_names = (
 4149:                       instcode    => 'Official courses (with institutional codes)',
 4150:                       communities => 'Communities',
 4151:                         );
 4152:     my $select0 = ' selected="selected"';
 4153:     my $select1 = '';
 4154:     foreach my $default ('instcode','communities') {
 4155:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4156:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4157:         if ($default eq 'communities') {
 4158:             $select1 = $select0;
 4159:             $select0 = '';
 4160:         }
 4161:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4162:                      .'<select name="'.$default.'_pos">'
 4163:                      .'<option value="0"'.$select0.'>1</option>'
 4164:                      .'<option value="1"'.$select1.'>2</option>'
 4165:                      .'<option value="2">3</option></select>&nbsp;'
 4166:                      .$default_names{$default}
 4167:                      .'</span></td><td><span class="LC_nobreak">'
 4168:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4169:                      .&mt('Display').'</label>&nbsp;<label>'
 4170:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4171:                  .'</label></span></td></tr>';
 4172:         $itemcount ++;
 4173:     }
 4174:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4175:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4176:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4177:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4178:                   .'<option value="0">1</option>'
 4179:                   .'<option value="1">2</option>'
 4180:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4181:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4182:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4183:     return $datatable;
 4184: }
 4185: 
 4186: sub build_category_rows {
 4187:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4188:     my ($text,$name,$item,$chgstr);
 4189:     if (ref($cats) eq 'ARRAY') {
 4190:         my $maxdepth = scalar(@{$cats});
 4191:         if (ref($cats->[$depth]) eq 'HASH') {
 4192:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4193:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4194:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4195:                 $text .= '<td><table class="LC_datatable">';
 4196:                 my ($idxnum,$parent_name,$parent_item);
 4197:                 my $higher = $depth - 1;
 4198:                 if ($higher == 0) {
 4199:                     $parent_name = &escape($parent).'::'.$higher;
 4200:                 } else {
 4201:                     if (ref($path) eq 'ARRAY') {
 4202:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4203:                     }
 4204:                 }
 4205:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4206:                 for (my $j=0; $j<=$numchildren; $j++) {
 4207:                     if ($j < $numchildren) {
 4208:                         $name = $cats->[$depth]{$parent}[$j];
 4209:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4210:                         $idxnum = $idx->{$item};
 4211:                     } else {
 4212:                         $name = $parent_name;
 4213:                         $item = $parent_item;
 4214:                     }
 4215:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4216:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4217:                     for (my $i=0; $i<=$numchildren; $i++) {
 4218:                         my $vpos = $i+1;
 4219:                         my $selstr;
 4220:                         if ($j == $i) {
 4221:                             $selstr = ' selected="selected" ';
 4222:                         }
 4223:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4224:                     }
 4225:                     $text .= '</select>&nbsp;';
 4226:                     if ($j < $numchildren) {
 4227:                         my $deeper = $depth+1;
 4228:                         $text .= $name.'&nbsp;'
 4229:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4230:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4231:                         if(ref($path) eq 'ARRAY') {
 4232:                             push(@{$path},$name);
 4233:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4234:                             pop(@{$path});
 4235:                         }
 4236:                     } else {
 4237:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4238:                         if ($j == $numchildren) {
 4239:                             $text .= $name;
 4240:                         } else {
 4241:                             $text .= $item;
 4242:                         }
 4243:                         $text .= '" value="" />';
 4244:                     }
 4245:                     $text .= '</td></tr>';
 4246:                 }
 4247:                 $text .= '</table></td>';
 4248:             } else {
 4249:                 my $higher = $depth-1;
 4250:                 if ($higher == 0) {
 4251:                     $name = &escape($parent).'::'.$higher;
 4252:                 } else {
 4253:                     if (ref($path) eq 'ARRAY') {
 4254:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4255:                     }
 4256:                 }
 4257:                 my $colspan;
 4258:                 if ($parent ne 'instcode') {
 4259:                     $colspan = $maxdepth - $depth - 1;
 4260:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4261:                 }
 4262:             }
 4263:         }
 4264:     }
 4265:     return $text;
 4266: }
 4267: 
 4268: sub modifiable_userdata_row {
 4269:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4270:     my $rolename;
 4271:     if ($context eq 'selfcreate') {
 4272:         if (ref($usertypes) eq 'HASH') {
 4273:             $rolename = $usertypes->{$role};
 4274:         } else {
 4275:             $rolename = $role;
 4276:         }
 4277:     } else {
 4278:         if ($role eq 'cr') {
 4279:             $rolename = &mt('Custom role');
 4280:         } else {
 4281:             $rolename = &Apache::lonnet::plaintext($role);
 4282:         }
 4283:     }
 4284:     my @fields = ('lastname','firstname','middlename','generation',
 4285:                   'permanentemail','id');
 4286:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4287:     my $output;
 4288:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4289:     $output = '<tr '.$css_class.'>'.
 4290:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4291:               '<td class="LC_left_item" colspan="2"><table>';
 4292:     my $rem;
 4293:     my %checks;
 4294:     if (ref($settings) eq 'HASH') {
 4295:         if (ref($settings->{$context}) eq 'HASH') {
 4296:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4297:                 foreach my $field (@fields) {
 4298:                     if ($settings->{$context}->{$role}->{$field}) {
 4299:                         $checks{$field} = ' checked="checked" ';
 4300:                     }
 4301:                 }
 4302:             }
 4303:         }
 4304:     }
 4305:     for (my $i=0; $i<@fields; $i++) {
 4306:         my $rem = $i%($numinrow);
 4307:         if ($rem == 0) {
 4308:             if ($i > 0) {
 4309:                 $output .= '</tr>';
 4310:             }
 4311:             $output .= '<tr>';
 4312:         }
 4313:         my $check = ' ';
 4314:         if (exists($checks{$fields[$i]})) {
 4315:             $check = $checks{$fields[$i]}
 4316:         } else {
 4317:             if ($role eq 'st') {
 4318:                 if (ref($settings) ne 'HASH') {
 4319:                     $check = ' checked="checked" '; 
 4320:                 }
 4321:             }
 4322:         }
 4323:         $output .= '<td class="LC_left_item">'.
 4324:                    '<span class="LC_nobreak"><label>'.
 4325:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4326:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4327:                    '</label></span></td>';
 4328:         $rem = @fields%($numinrow);
 4329:     }
 4330:     my $colsleft = $numinrow - $rem;
 4331:     if ($colsleft > 1 ) {
 4332:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4333:                    '&nbsp;</td>';
 4334:     } elsif ($colsleft == 1) {
 4335:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4336:     }
 4337:     $output .= '</tr></table></td></tr>';
 4338:     return $output;
 4339: }
 4340: 
 4341: sub insttypes_row {
 4342:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4343:     my %lt = &Apache::lonlocal::texthash (
 4344:                       cansearch => 'Users allowed to search',
 4345:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4346:                       lockablenames => 'User preference to lock name',
 4347:              );
 4348:     my $showdom;
 4349:     if ($context eq 'cansearch') {
 4350:         $showdom = ' ('.$dom.')';
 4351:     }
 4352:     my $class = 'LC_left_item';
 4353:     if ($context eq 'statustocreate') {
 4354:         $class = 'LC_right_item';
 4355:     }
 4356:     my $output =  '<tr class="LC_odd_row">'.
 4357:                   '<td>'.$lt{$context}.$showdom.
 4358:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4359:     my $rem;
 4360:     if (ref($types) eq 'ARRAY') {
 4361:         for (my $i=0; $i<@{$types}; $i++) {
 4362:             if (defined($usertypes->{$types->[$i]})) {
 4363:                 my $rem = $i%($numinrow);
 4364:                 if ($rem == 0) {
 4365:                     if ($i > 0) {
 4366:                         $output .= '</tr>';
 4367:                     }
 4368:                     $output .= '<tr>';
 4369:                 }
 4370:                 my $check = ' ';
 4371:                 if (ref($settings) eq 'HASH') {
 4372:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4373:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4374:                             $check = ' checked="checked" ';
 4375:                         }
 4376:                     } elsif ($context eq 'statustocreate') {
 4377:                         $check = ' checked="checked" ';
 4378:                     }
 4379:                 }
 4380:                 $output .= '<td class="LC_left_item">'.
 4381:                            '<span class="LC_nobreak"><label>'.
 4382:                            '<input type="checkbox" name="'.$context.'" '.
 4383:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4384:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4385:             }
 4386:         }
 4387:         $rem = @{$types}%($numinrow);
 4388:     }
 4389:     my $colsleft = $numinrow - $rem;
 4390:     if (($rem == 0) && (@{$types} > 0)) {
 4391:         $output .= '<tr>';
 4392:     }
 4393:     if ($colsleft > 1) {
 4394:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4395:     } else {
 4396:         $output .= '<td class="LC_left_item">';
 4397:     }
 4398:     my $defcheck = ' ';
 4399:     if (ref($settings) eq 'HASH') {  
 4400:         if (ref($settings->{$context}) eq 'ARRAY') {
 4401:             if (grep(/^default$/,@{$settings->{$context}})) {
 4402:                 $defcheck = ' checked="checked" ';
 4403:             }
 4404:         } elsif ($context eq 'statustocreate') {
 4405:             $defcheck = ' checked="checked" ';
 4406:         }
 4407:     }
 4408:     $output .= '<span class="LC_nobreak"><label>'.
 4409:                '<input type="checkbox" name="'.$context.'" '.
 4410:                'value="default"'.$defcheck.'/>'.
 4411:                $othertitle.'</label></span></td>'.
 4412:                '</tr></table></td></tr>';
 4413:     return $output;
 4414: }
 4415: 
 4416: sub sorted_searchtitles {
 4417:     my %searchtitles = &Apache::lonlocal::texthash(
 4418:                          'uname' => 'username',
 4419:                          'lastname' => 'last name',
 4420:                          'lastfirst' => 'last name, first name',
 4421:                      );
 4422:     my @titleorder = ('uname','lastname','lastfirst');
 4423:     return (\%searchtitles,\@titleorder);
 4424: }
 4425: 
 4426: sub sorted_searchtypes {
 4427:     my %srchtypes_desc = (
 4428:                            exact    => 'is exact match',
 4429:                            contains => 'contains ..',
 4430:                            begins   => 'begins with ..',
 4431:                          );
 4432:     my @srchtypeorder = ('exact','begins','contains');
 4433:     return (\%srchtypes_desc,\@srchtypeorder);
 4434: }
 4435: 
 4436: sub usertype_update_row {
 4437:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4438:     my $datatable;
 4439:     my $numinrow = 4;
 4440:     foreach my $type (@{$types}) {
 4441:         if (defined($usertypes->{$type})) {
 4442:             $$rownums ++;
 4443:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4444:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4445:                           '</td><td class="LC_left_item"><table>';
 4446:             for (my $i=0; $i<@{$fields}; $i++) {
 4447:                 my $rem = $i%($numinrow);
 4448:                 if ($rem == 0) {
 4449:                     if ($i > 0) {
 4450:                         $datatable .= '</tr>';
 4451:                     }
 4452:                     $datatable .= '<tr>';
 4453:                 }
 4454:                 my $check = ' ';
 4455:                 if (ref($settings) eq 'HASH') {
 4456:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4457:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4458:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4459:                                 $check = ' checked="checked" ';
 4460:                             }
 4461:                         }
 4462:                     }
 4463:                 }
 4464: 
 4465:                 if ($i == @{$fields}-1) {
 4466:                     my $colsleft = $numinrow - $rem;
 4467:                     if ($colsleft > 1) {
 4468:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4469:                     } else {
 4470:                         $datatable .= '<td>';
 4471:                     }
 4472:                 } else {
 4473:                     $datatable .= '<td>';
 4474:                 }
 4475:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4476:                               '<input type="checkbox" name="updateable_'.$type.
 4477:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4478:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4479:             }
 4480:             $datatable .= '</tr></table></td></tr>';
 4481:         }
 4482:     }
 4483:     return $datatable;
 4484: }
 4485: 
 4486: sub modify_login {
 4487:     my ($r,$dom,$confname,%domconfig) = @_;
 4488:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges);
 4489:     my %title = ( coursecatalog => 'Display course catalog',
 4490:                   adminmail => 'Display administrator E-mail address',
 4491:                   newuser => 'Link for visitors to create a user account',
 4492:                   loginheader => 'Log-in box header');
 4493:     my @offon = ('off','on');
 4494:     my %curr_loginvia;
 4495:     if (ref($domconfig{login}) eq 'HASH') {
 4496:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4497:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4498:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4499:             }
 4500:         }
 4501:     }
 4502:     my %loginhash;
 4503:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4504:                                            \%domconfig,\%loginhash);
 4505:     my @toggles = ('coursecatalog','adminmail','newuser');
 4506:     foreach my $item (@toggles) {
 4507:         $loginhash{login}{$item} = $env{'form.'.$item};
 4508:     }
 4509:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4510:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4511:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4512:                                          \%loginhash);
 4513:     }
 4514: 
 4515:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4516:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4517:     if (keys(%servers) > 1) {
 4518:         foreach my $lonhost (keys(%servers)) {
 4519:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4520:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4521:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4522:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4523:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4524:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4525:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4526:                         $changes{'loginvia'}{$lonhost} = 1;
 4527:                     } else {
 4528:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4529:                         $changes{'loginvia'}{$lonhost} = 1;
 4530:                     }
 4531:                 } else {
 4532:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4533:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4534:                         $changes{'loginvia'}{$lonhost} = 1;
 4535:                     }
 4536:                 }
 4537:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4538:                     foreach my $item (@loginvia_attribs) {
 4539:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4540:                     }
 4541:                 } else {
 4542:                     foreach my $item (@loginvia_attribs) {
 4543:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4544:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4545:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4546:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4547:                                 $new = '/';
 4548:                             }
 4549:                         }
 4550:                         if (($item eq 'custompath') && 
 4551:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4552:                             $new = '';
 4553:                         }
 4554:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4555:                             $changes{'loginvia'}{$lonhost} = 1;
 4556:                         }
 4557:                         if ($item eq 'exempt') {
 4558:                             $new =~ s/^\s+//;
 4559:                             $new =~ s/\s+$//;
 4560:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4561:                             my @okips;
 4562:                             foreach my $ip (@poss_ips) {
 4563:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4564:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4565:                                         push(@okips,$ip); 
 4566:                                     }
 4567:                                 }
 4568:                             }
 4569:                             if (@okips > 0) {
 4570:                                 $new = join(',',@okips); 
 4571:                             } else {
 4572:                                 $new = ''; 
 4573:                             }
 4574:                         }
 4575: 
 4576:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4577:                     }
 4578:                 }
 4579:             } else {
 4580:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4581:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4582:                     $changes{'loginvia'}{$lonhost} = 1;
 4583:                     foreach my $item (@loginvia_attribs) {
 4584:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4585:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4586:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4587:                                 $new = '/';
 4588:                             }
 4589:                         }
 4590:                         if (($item eq 'custompath') && 
 4591:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4592:                             $new = '';
 4593:                         }
 4594:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4595:                     }
 4596:                 }
 4597:             }
 4598:         }
 4599:     }
 4600: 
 4601:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4602:                                              $dom);
 4603:     if ($putresult eq 'ok') {
 4604:         my @toggles = ('coursecatalog','adminmail','newuser');
 4605:         my %defaultchecked = (
 4606:                     'coursecatalog' => 'on',
 4607:                     'adminmail'     => 'off',
 4608:                     'newuser'       => 'off',
 4609:         );
 4610:         if (ref($domconfig{'login'}) eq 'HASH') {
 4611:             foreach my $item (@toggles) {
 4612:                 if ($defaultchecked{$item} eq 'on') { 
 4613:                     if (($domconfig{'login'}{$item} eq '0') &&
 4614:                         ($env{'form.'.$item} eq '1')) {
 4615:                         $changes{$item} = 1;
 4616:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4617:                               $domconfig{'login'}{$item} eq '1') &&
 4618:                              ($env{'form.'.$item} eq '0')) {
 4619:                         $changes{$item} = 1;
 4620:                     }
 4621:                 } elsif ($defaultchecked{$item} eq 'off') {
 4622:                     if (($domconfig{'login'}{$item} eq '1') &&
 4623:                         ($env{'form.'.$item} eq '0')) {
 4624:                         $changes{$item} = 1;
 4625:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4626:                               $domconfig{'login'}{$item} eq '0') &&
 4627:                              ($env{'form.'.$item} eq '1')) {
 4628:                         $changes{$item} = 1;
 4629:                     }
 4630:                 }
 4631:             }
 4632:         }
 4633:         if (keys(%changes) > 0 || $colchgtext) {
 4634:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4635:             $resulttext = &mt('Changes made:').'<ul>';
 4636:             foreach my $item (sort(keys(%changes))) {
 4637:                 if ($item eq 'loginvia') {
 4638:                     if (ref($changes{$item}) eq 'HASH') {
 4639:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4640:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4641:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4642:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4643:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4644:                                     $protocol = 'http' if ($protocol ne 'https');
 4645:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4646: 
 4647:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4648:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4649:                                     } else {
 4650:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4651:                                     }
 4652:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4653:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4654:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4655:                                     }
 4656:                                     $resulttext .= '</li>';
 4657:                                 } else {
 4658:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4659:                                 }
 4660:                             } else {
 4661:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4662:                             }
 4663:                         }
 4664:                         $resulttext .= '</ul></li>';
 4665:                     }
 4666:                 } else {
 4667:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4668:                 }
 4669:             }
 4670:             $resulttext .= $colchgtext.'</ul>';
 4671:         } else {
 4672:             $resulttext = &mt('No changes made to log-in page settings');
 4673:         }
 4674:     } else {
 4675:         $resulttext = '<span class="LC_error">'.
 4676: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4677:     }
 4678:     if ($errors) {
 4679:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 4680:                        $errors.'</ul>';
 4681:     }
 4682:     return $resulttext;
 4683: }
 4684: 
 4685: sub color_font_choices {
 4686:     my %choices =
 4687:         &Apache::lonlocal::texthash (
 4688:             img => "Header",
 4689:             bgs => "Background colors",
 4690:             links => "Link colors",
 4691:             images => "Images",
 4692:             font => "Font color",
 4693:             fontmenu => "Font Menu",
 4694:             pgbg => "Page",
 4695:             tabbg => "Header",
 4696:             sidebg => "Border",
 4697:             link => "Link",
 4698:             alink => "Active link",
 4699:             vlink => "Visited link",
 4700:         );
 4701:     return %choices;
 4702: }
 4703: 
 4704: sub modify_rolecolors {
 4705:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 4706:     my ($resulttext,%rolehash);
 4707:     $rolehash{'rolecolors'} = {};
 4708:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 4709:         if ($domconfig{'rolecolors'} eq '') {
 4710:             $domconfig{'rolecolors'} = {};
 4711:         }
 4712:     }
 4713:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 4714:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 4715:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 4716:                                              $dom);
 4717:     if ($putresult eq 'ok') {
 4718:         if (keys(%changes) > 0) {
 4719:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4720:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 4721:                                              $rolehash{'rolecolors'});
 4722:         } else {
 4723:             $resulttext = &mt('No changes made to default color schemes');
 4724:         }
 4725:     } else {
 4726:         $resulttext = '<span class="LC_error">'.
 4727: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4728:     }
 4729:     if ($errors) {
 4730:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 4731:                        $errors.'</ul>';
 4732:     }
 4733:     return $resulttext;
 4734: }
 4735: 
 4736: sub modify_colors {
 4737:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 4738:     my (%changes,%choices);
 4739:     my @bgs;
 4740:     my @links = ('link','alink','vlink');
 4741:     my @logintext;
 4742:     my @images;
 4743:     my $servadm = $r->dir_config('lonAdmEMail');
 4744:     my $errors;
 4745:     foreach my $role (@{$roles}) {
 4746:         if ($role eq 'login') {
 4747:             %choices = &login_choices();
 4748:             @logintext = ('textcol','bgcol');
 4749:         } else {
 4750:             %choices = &color_font_choices();
 4751:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 4752:         }
 4753:         if ($role eq 'login') {
 4754:             @images = ('img','logo','domlogo','login');
 4755:             @bgs = ('pgbg','mainbg','sidebg');
 4756:         } else {
 4757:             @images = ('img');
 4758:             @bgs = ('pgbg','tabbg','sidebg'); 
 4759:         }
 4760:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 4761:         foreach my $item (@bgs,@links,@logintext) {
 4762:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 4763:         }
 4764:         my ($configuserok,$author_ok,$switchserver) = 
 4765:             &config_check($dom,$confname,$servadm);
 4766:         my ($width,$height) = &thumb_dimensions();
 4767:         if (ref($domconfig->{$role}) ne 'HASH') {
 4768:             $domconfig->{$role} = {};
 4769:         }
 4770:         foreach my $img (@images) {
 4771:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 4772:                 if (defined($env{'form.login_showlogo_'.$img})) {
 4773:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 4774:                 } else { 
 4775:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 4776:                 }
 4777:             } 
 4778: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 4779: 		 && !defined($domconfig->{$role}{$img})
 4780: 		 && !$env{'form.'.$role.'_del_'.$img}
 4781: 		 && $env{'form.'.$role.'_import_'.$img}) {
 4782: 		# import the old configured image from the .tab setting
 4783: 		# if they haven't provided a new one 
 4784: 		$domconfig->{$role}{$img} = 
 4785: 		    $env{'form.'.$role.'_import_'.$img};
 4786: 	    }
 4787:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 4788:                 my $error;
 4789:                 if ($configuserok eq 'ok') {
 4790:                     if ($switchserver) {
 4791:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 4792:                     } else {
 4793:                         if ($author_ok eq 'ok') {
 4794:                             my ($result,$logourl) = 
 4795:                                 &publishlogo($r,'upload',$role.'_'.$img,
 4796:                                            $dom,$confname,$img,$width,$height);
 4797:                             if ($result eq 'ok') {
 4798:                                 $confhash->{$role}{$img} = $logourl;
 4799:                                 $changes{$role}{'images'}{$img} = 1;
 4800:                             } else {
 4801:                                 $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);
 4802:                             }
 4803:                         } else {
 4804:                             $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);
 4805:                         }
 4806:                     }
 4807:                 } else {
 4808:                     $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);
 4809:                 }
 4810:                 if ($error) {
 4811:                     &Apache::lonnet::logthis($error);
 4812:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4813:                 }
 4814:             } elsif ($domconfig->{$role}{$img} ne '') {
 4815:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 4816:                     my $error;
 4817:                     if ($configuserok eq 'ok') {
 4818: # is confname an author?
 4819:                         if ($switchserver eq '') {
 4820:                             if ($author_ok eq 'ok') {
 4821:                                 my ($result,$logourl) = 
 4822:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 4823:                                             $dom,$confname,$img,$width,$height);
 4824:                                 if ($result eq 'ok') {
 4825:                                     $confhash->{$role}{$img} = $logourl;
 4826: 				    $changes{$role}{'images'}{$img} = 1;
 4827:                                 }
 4828:                             }
 4829:                         }
 4830:                     }
 4831:                 }
 4832:             }
 4833:         }
 4834:         if (ref($domconfig) eq 'HASH') {
 4835:             if (ref($domconfig->{$role}) eq 'HASH') {
 4836:                 foreach my $img (@images) {
 4837:                     if ($domconfig->{$role}{$img} ne '') {
 4838:                         if ($env{'form.'.$role.'_del_'.$img}) {
 4839:                             $confhash->{$role}{$img} = '';
 4840:                             $changes{$role}{'images'}{$img} = 1;
 4841:                         } else {
 4842:                             if ($confhash->{$role}{$img} eq '') {
 4843:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 4844:                             }
 4845:                         }
 4846:                     } else {
 4847:                         if ($env{'form.'.$role.'_del_'.$img}) {
 4848:                             $confhash->{$role}{$img} = '';
 4849:                             $changes{$role}{'images'}{$img} = 1;
 4850:                         } 
 4851:                     }
 4852:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 4853:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 4854:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 4855:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 4856:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 4857:                             }
 4858:                         } else {
 4859:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 4860:                                 $changes{$role}{'showlogo'}{$img} = 1;
 4861:                             }
 4862:                         }
 4863:                     }
 4864:                 }
 4865:                 if ($domconfig->{$role}{'font'} ne '') {
 4866:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 4867:                         $changes{$role}{'font'} = 1;
 4868:                     }
 4869:                 } else {
 4870:                     if ($confhash->{$role}{'font'}) {
 4871:                         $changes{$role}{'font'} = 1;
 4872:                     }
 4873:                 }
 4874:                 if ($role ne 'login') {
 4875:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 4876:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 4877:                             $changes{$role}{'fontmenu'} = 1;
 4878:                         }
 4879:                     } else {
 4880:                         if ($confhash->{$role}{'fontmenu'}) {
 4881:                             $changes{$role}{'fontmenu'} = 1;
 4882:                         }
 4883:                     }
 4884:                 }
 4885:                 foreach my $item (@bgs) {
 4886:                     if ($domconfig->{$role}{$item} ne '') {
 4887:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 4888:                             $changes{$role}{'bgs'}{$item} = 1;
 4889:                         } 
 4890:                     } else {
 4891:                         if ($confhash->{$role}{$item}) {
 4892:                             $changes{$role}{'bgs'}{$item} = 1;
 4893:                         }
 4894:                     }
 4895:                 }
 4896:                 foreach my $item (@links) {
 4897:                     if ($domconfig->{$role}{$item} ne '') {
 4898:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 4899:                             $changes{$role}{'links'}{$item} = 1;
 4900:                         }
 4901:                     } else {
 4902:                         if ($confhash->{$role}{$item}) {
 4903:                             $changes{$role}{'links'}{$item} = 1;
 4904:                         }
 4905:                     }
 4906:                 }
 4907:                 foreach my $item (@logintext) {
 4908:                     if ($domconfig->{$role}{$item} ne '') {
 4909:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 4910:                             $changes{$role}{'logintext'}{$item} = 1;
 4911:                         }
 4912:                     } else {
 4913:                         if ($confhash->{$role}{$item}) {
 4914:                             $changes{$role}{'logintext'}{$item} = 1;
 4915:                         }
 4916:                     }
 4917:                 }
 4918:             } else {
 4919:                 &default_change_checker($role,\@images,\@links,\@bgs,
 4920:                                         \@logintext,$confhash,\%changes); 
 4921:             }
 4922:         } else {
 4923:             &default_change_checker($role,\@images,\@links,\@bgs,
 4924:                                     \@logintext,$confhash,\%changes); 
 4925:         }
 4926:     }
 4927:     return ($errors,%changes);
 4928: }
 4929: 
 4930: sub config_check {
 4931:     my ($dom,$confname,$servadm) = @_;
 4932:     my ($configuserok,$author_ok,$switchserver,%currroles);
 4933:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 4934:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 4935:                                                    $confname,$servadm);
 4936:     if ($configuserok eq 'ok') {
 4937:         $switchserver = &check_switchserver($dom,$confname);
 4938:         if ($switchserver eq '') {
 4939:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 4940:         }
 4941:     }
 4942:     return ($configuserok,$author_ok,$switchserver);
 4943: }
 4944: 
 4945: sub default_change_checker {
 4946:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 4947:     foreach my $item (@{$links}) {
 4948:         if ($confhash->{$role}{$item}) {
 4949:             $changes->{$role}{'links'}{$item} = 1;
 4950:         }
 4951:     }
 4952:     foreach my $item (@{$bgs}) {
 4953:         if ($confhash->{$role}{$item}) {
 4954:             $changes->{$role}{'bgs'}{$item} = 1;
 4955:         }
 4956:     }
 4957:     foreach my $item (@{$logintext}) {
 4958:         if ($confhash->{$role}{$item}) {
 4959:             $changes->{$role}{'logintext'}{$item} = 1;
 4960:         }
 4961:     }
 4962:     foreach my $img (@{$images}) {
 4963:         if ($env{'form.'.$role.'_del_'.$img}) {
 4964:             $confhash->{$role}{$img} = '';
 4965:             $changes->{$role}{'images'}{$img} = 1;
 4966:         }
 4967:         if ($role eq 'login') {
 4968:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 4969:                 $changes->{$role}{'showlogo'}{$img} = 1;
 4970:             }
 4971:         }
 4972:     }
 4973:     if ($confhash->{$role}{'font'}) {
 4974:         $changes->{$role}{'font'} = 1;
 4975:     }
 4976: }
 4977: 
 4978: sub display_colorchgs {
 4979:     my ($dom,$changes,$roles,$confhash) = @_;
 4980:     my (%choices,$resulttext);
 4981:     if (!grep(/^login$/,@{$roles})) {
 4982:         $resulttext = &mt('Changes made:').'<br />';
 4983:     }
 4984:     foreach my $role (@{$roles}) {
 4985:         if ($role eq 'login') {
 4986:             %choices = &login_choices();
 4987:         } else {
 4988:             %choices = &color_font_choices();
 4989:         }
 4990:         if (ref($changes->{$role}) eq 'HASH') {
 4991:             if ($role ne 'login') {
 4992:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 4993:             }
 4994:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 4995:                 if ($role ne 'login') {
 4996:                     $resulttext .= '<ul>';
 4997:                 }
 4998:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 4999:                     if ($role ne 'login') {
 5000:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5001:                     }
 5002:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5003:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5004:                             if ($confhash->{$role}{$key}{$item}) {
 5005:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5006:                             } else {
 5007:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5008:                             }
 5009:                         } elsif ($confhash->{$role}{$item} eq '') {
 5010:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5011:                         } else {
 5012:                             my $newitem = $confhash->{$role}{$item};
 5013:                             if ($key eq 'images') {
 5014:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5015:                             }
 5016:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5017:                         }
 5018:                     }
 5019:                     if ($role ne 'login') {
 5020:                         $resulttext .= '</ul></li>';
 5021:                     }
 5022:                 } else {
 5023:                     if ($confhash->{$role}{$key} eq '') {
 5024:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5025:                     } else {
 5026:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5027:                     }
 5028:                 }
 5029:                 if ($role ne 'login') {
 5030:                     $resulttext .= '</ul>';
 5031:                 }
 5032:             }
 5033:         }
 5034:     }
 5035:     return $resulttext;
 5036: }
 5037: 
 5038: sub thumb_dimensions {
 5039:     return ('200','50');
 5040: }
 5041: 
 5042: sub check_dimensions {
 5043:     my ($inputfile) = @_;
 5044:     my ($fullwidth,$fullheight);
 5045:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5046:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5047:             my $imageinfo = <PIPE>;
 5048:             if (!close(PIPE)) {
 5049:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5050:             }
 5051:             chomp($imageinfo);
 5052:             my ($fullsize) = 
 5053:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5054:             if ($fullsize) {
 5055:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5056:             }
 5057:         }
 5058:     }
 5059:     return ($fullwidth,$fullheight);
 5060: }
 5061: 
 5062: sub check_configuser {
 5063:     my ($uhome,$dom,$confname,$servadm) = @_;
 5064:     my ($configuserok,%currroles);
 5065:     if ($uhome eq 'no_host') {
 5066:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5067:         my $configpass = &LONCAPA::Enrollment::create_password();
 5068:         $configuserok = 
 5069:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5070:                              $configpass,'','','','','',undef,$servadm);
 5071:     } else {
 5072:         $configuserok = 'ok';
 5073:         %currroles = 
 5074:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5075:     }
 5076:     return ($configuserok,%currroles);
 5077: }
 5078: 
 5079: sub check_authorstatus {
 5080:     my ($dom,$confname,%currroles) = @_;
 5081:     my $author_ok;
 5082:     if (!$currroles{':'.$dom.':au'}) {
 5083:         my $start = time;
 5084:         my $end = 0;
 5085:         $author_ok = 
 5086:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5087:                                         'au',$end,$start,'','','domconfig');
 5088:     } else {
 5089:         $author_ok = 'ok';
 5090:     }
 5091:     return $author_ok;
 5092: }
 5093: 
 5094: sub publishlogo {
 5095:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5096:     my ($output,$fname,$logourl);
 5097:     if ($action eq 'upload') {
 5098:         $fname=$env{'form.'.$formname.'.filename'};
 5099:         chop($env{'form.'.$formname});
 5100:     } else {
 5101:         ($fname) = ($formname =~ /([^\/]+)$/);
 5102:     }
 5103:     if ($savefileas ne '') {
 5104:         $fname = $savefileas;
 5105:     }
 5106:     $fname=&Apache::lonnet::clean_filename($fname);
 5107: # See if there is anything left
 5108:     unless ($fname) { return ('error: no uploaded file'); }
 5109:     $fname="$subdir/$fname";
 5110:     my $docroot=$r->dir_config('lonDocRoot'); 
 5111:     my $filepath="$docroot/priv";
 5112:     my $relpath = "$dom/$confname";
 5113:     my ($fnamepath,$file,$fetchthumb);
 5114:     $file=$fname;
 5115:     if ($fname=~m|/|) {
 5116:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5117:     }
 5118:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5119:     my $count;
 5120:     for ($count=5;$count<=$#parts;$count++) {
 5121:         $filepath.="/$parts[$count]";
 5122:         if ((-e $filepath)!=1) {
 5123:             mkdir($filepath,02770);
 5124:         }
 5125:     }
 5126:     # Check for bad extension and disallow upload
 5127:     if ($file=~/\.(\w+)$/ &&
 5128:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5129:         $output = 
 5130:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 5131:     } elsif ($file=~/\.(\w+)$/ &&
 5132:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5133:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5134:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5135:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5136:     } elsif (-d "$filepath/$file") {
 5137:         $output = &mt('File name is a directory name - rename the file and re-upload');
 5138:     } else {
 5139:         my $source = $filepath.'/'.$file;
 5140:         my $logfile;
 5141:         if (!open($logfile,">>$source".'.log')) {
 5142:             return (&mt('No write permission to Construction Space'));
 5143:         }
 5144:         print $logfile
 5145: "\n================= Publish ".localtime()." ================\n".
 5146: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5147: # Save the file
 5148:         if (!open(FH,'>'.$source)) {
 5149:             &Apache::lonnet::logthis('Failed to create '.$source);
 5150:             return (&mt('Failed to create file'));
 5151:         }
 5152:         if ($action eq 'upload') {
 5153:             if (!print FH ($env{'form.'.$formname})) {
 5154:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5155:                 return (&mt('Failed to write file'));
 5156:             }
 5157:         } else {
 5158:             my $original = &Apache::lonnet::filelocation('',$formname);
 5159:             if(!copy($original,$source)) {
 5160:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5161:                 return (&mt('Failed to write file'));
 5162:             }
 5163:         }
 5164:         close(FH);
 5165:         chmod(0660, $source); # Permissions to rw-rw---.
 5166: 
 5167:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5168:         my $copyfile=$targetdir.'/'.$file;
 5169: 
 5170:         my @parts=split(/\//,$targetdir);
 5171:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5172:         for (my $count=5;$count<=$#parts;$count++) {
 5173:             $path.="/$parts[$count]";
 5174:             if (!-e $path) {
 5175:                 print $logfile "\nCreating directory ".$path;
 5176:                 mkdir($path,02770);
 5177:             }
 5178:         }
 5179:         my $versionresult;
 5180:         if (-e $copyfile) {
 5181:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5182:         } else {
 5183:             $versionresult = 'ok';
 5184:         }
 5185:         if ($versionresult eq 'ok') {
 5186:             if (copy($source,$copyfile)) {
 5187:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5188:                 $output = 'ok';
 5189:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5190:                 push(@{$modified_urls},[$copyfile,$source]);
 5191:                 my $metaoutput = 
 5192:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5193:                 unless ($registered_cleanup) {
 5194:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5195:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5196:                     $registered_cleanup=1;
 5197:                 }
 5198:             } else {
 5199:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5200:                 $output = &mt('Failed to copy file to RES space').", $!";
 5201:             }
 5202:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5203:                 my $inputfile = $filepath.'/'.$file;
 5204:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5205:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5206:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5207:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5208:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5209:                         system("convert -sample $thumbsize $inputfile $outfile");
 5210:                         chmod(0660, $filepath.'/tn-'.$file);
 5211:                         if (-e $outfile) {
 5212:                             my $copyfile=$targetdir.'/tn-'.$file;
 5213:                             if (copy($outfile,$copyfile)) {
 5214:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5215:                                 my $thumb_metaoutput = 
 5216:                                     &write_metadata($dom,$confname,$formname,
 5217:                                                     $targetdir,'tn-'.$file,$logfile);
 5218:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5219:                                 unless ($registered_cleanup) {
 5220:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5221:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5222:                                     $registered_cleanup=1;
 5223:                                 }
 5224:                             } else {
 5225:                                 print $logfile "\nUnable to write ".$copyfile.
 5226:                                                ':'.$!."\n";
 5227:                             }
 5228:                         }
 5229:                     }
 5230:                 }
 5231:             }
 5232:         } else {
 5233:             $output = $versionresult;
 5234:         }
 5235:     }
 5236:     return ($output,$logourl);
 5237: }
 5238: 
 5239: sub logo_versioning {
 5240:     my ($targetdir,$file,$logfile) = @_;
 5241:     my $target = $targetdir.'/'.$file;
 5242:     my ($maxversion,$fn,$extn,$output);
 5243:     $maxversion = 0;
 5244:     if ($file =~ /^(.+)\.(\w+)$/) {
 5245:         $fn=$1;
 5246:         $extn=$2;
 5247:     }
 5248:     opendir(DIR,$targetdir);
 5249:     while (my $filename=readdir(DIR)) {
 5250:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5251:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5252:         }
 5253:     }
 5254:     $maxversion++;
 5255:     print $logfile "\nCreating old version ".$maxversion."\n";
 5256:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5257:     if (copy($target,$copyfile)) {
 5258:         print $logfile "Copied old target to ".$copyfile."\n";
 5259:         $copyfile=$copyfile.'.meta';
 5260:         if (copy($target.'.meta',$copyfile)) {
 5261:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5262:             $output = 'ok';
 5263:         } else {
 5264:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5265:             $output = &mt('Failed to copy old meta').", $!, ";
 5266:         }
 5267:     } else {
 5268:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5269:         $output = &mt('Failed to copy old target').", $!, ";
 5270:     }
 5271:     return $output;
 5272: }
 5273: 
 5274: sub write_metadata {
 5275:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5276:     my (%metadatafields,%metadatakeys,$output);
 5277:     $metadatafields{'title'}=$formname;
 5278:     $metadatafields{'creationdate'}=time;
 5279:     $metadatafields{'lastrevisiondate'}=time;
 5280:     $metadatafields{'copyright'}='public';
 5281:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5282:                                          $env{'user.domain'};
 5283:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5284:     $metadatafields{'domain'}=$dom;
 5285:     {
 5286:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5287:         my $mfh;
 5288:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5289:             foreach (sort keys %metadatafields) {
 5290:                 unless ($_=~/\./) {
 5291:                     my $unikey=$_;
 5292:                     $unikey=~/^([A-Za-z]+)/;
 5293:                     my $tag=$1;
 5294:                     $tag=~tr/A-Z/a-z/;
 5295:                     print $mfh "\n\<$tag";
 5296:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5297:                         my $value=$metadatafields{$unikey.'.'.$_};
 5298:                         $value=~s/\"/\'\'/g;
 5299:                         print $mfh ' '.$_.'="'.$value.'"';
 5300:                     }
 5301:                     print $mfh '>'.
 5302:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5303:                             .'</'.$tag.'>';
 5304:                 }
 5305:             }
 5306:             $output = 'ok';
 5307:             print $logfile "\nWrote metadata";
 5308:             close($mfh);
 5309:         } else {
 5310:             print $logfile "\nFailed to open metadata file";
 5311:             $output = &mt('Could not write metadata');
 5312:         }
 5313:     }
 5314:     return $output;
 5315: }
 5316: 
 5317: sub notifysubscribed {
 5318:     foreach my $targetsource (@{$modified_urls}){
 5319:         next unless (ref($targetsource) eq 'ARRAY');
 5320:         my ($target,$source)=@{$targetsource};
 5321:         if ($source ne '') {
 5322:             if (open(my $logfh,'>>'.$source.'.log')) {
 5323:                 print $logfh "\nCleanup phase: Notifications\n";
 5324:                 my @subscribed=&subscribed_hosts($target);
 5325:                 foreach my $subhost (@subscribed) {
 5326:                     print $logfh "\nNotifying host ".$subhost.':';
 5327:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5328:                     print $logfh $reply;
 5329:                 }
 5330:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5331:                 foreach my $subhost (@subscribedmeta) {
 5332:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5333:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5334:                                                         $subhost);
 5335:                     print $logfh $reply;
 5336:                 }
 5337:                 print $logfh "\n============ Done ============\n";
 5338:                 close($logfh);
 5339:             }
 5340:         }
 5341:     }
 5342:     return OK;
 5343: }
 5344: 
 5345: sub subscribed_hosts {
 5346:     my ($target) = @_;
 5347:     my @subscribed;
 5348:     if (open(my $fh,"<$target.subscription")) {
 5349:         while (my $subline=<$fh>) {
 5350:             if ($subline =~ /^($match_lonid):/) {
 5351:                 my $host = $1;
 5352:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5353:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5354:                         push(@subscribed,$host);
 5355:                     }
 5356:                 }
 5357:             }
 5358:         }
 5359:     }
 5360:     return @subscribed;
 5361: }
 5362: 
 5363: sub check_switchserver {
 5364:     my ($dom,$confname) = @_;
 5365:     my ($allowed,$switchserver);
 5366:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5367:     if ($home eq 'no_host') {
 5368:         $home = &Apache::lonnet::domain($dom,'primary');
 5369:     }
 5370:     my @ids=&Apache::lonnet::current_machine_ids();
 5371:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5372:     if (!$allowed) {
 5373: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&role=dc./'.$dom.'/">'.&mt('Switch Server').'</a>';
 5374:     }
 5375:     return $switchserver;
 5376: }
 5377: 
 5378: sub modify_quotas {
 5379:     my ($dom,$action,%domconfig) = @_;
 5380:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5381:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5382:     if ($action eq 'quotas') {
 5383:         $context = 'tools'; 
 5384:     } else {
 5385:         $context = $action;
 5386:     }
 5387:     if ($context eq 'requestcourses') {
 5388:         @usertools = ('official','unofficial','community');
 5389:         @options =('norequest','approval','validate','autolimit');
 5390:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5391:         %titles = &courserequest_titles();
 5392:         $toolregexp = join('|',@usertools);
 5393:         %conditions = &courserequest_conditions();
 5394:     } elsif ($context eq 'requestauthor') {
 5395:         @usertools = ('author');
 5396:         %titles = &authorrequest_titles();
 5397:     } else {
 5398:         @usertools = ('aboutme','blog','webdav','portfolio');
 5399:         %titles = &tool_titles();
 5400:     }
 5401:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5402:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5403:     foreach my $key (keys(%env)) {
 5404:         if ($context eq 'requestcourses') {
 5405:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5406:                 my $item = $1;
 5407:                 my $type = $2;
 5408:                 if ($type =~ /^limit_(.+)/) {
 5409:                     $limithash{$item}{$1} = $env{$key};
 5410:                 } else {
 5411:                     $confhash{$item}{$type} = $env{$key};
 5412:                 }
 5413:             }
 5414:         } elsif ($context eq 'requestauthor') {
 5415:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5416:                 $confhash{$1} = $env{$key};
 5417:             }
 5418:         } else {
 5419:             if ($key =~ /^form\.quota_(.+)$/) {
 5420:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5421:             }
 5422:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5423:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5424:             }
 5425:         }
 5426:     }
 5427:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5428:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5429:         @approvalnotify = sort(@approvalnotify);
 5430:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5431:         if (ref($domconfig{$action}) eq 'HASH') {
 5432:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5433:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5434:                     $changes{'notify'}{'approval'} = 1;
 5435:                 }
 5436:             } else {
 5437:                 if ($confhash{'notify'}{'approval'}) {
 5438:                     $changes{'notify'}{'approval'} = 1;
 5439:                 }
 5440:             }
 5441:         } else {
 5442:             if ($confhash{'notify'}{'approval'}) {
 5443:                 $changes{'notify'}{'approval'} = 1;
 5444:             }
 5445:         }
 5446:     } else {
 5447:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5448:     }
 5449:     foreach my $item (@usertools) {
 5450:         foreach my $type (@{$types},'default','_LC_adv') {
 5451:             my $unset; 
 5452:             if ($context eq 'requestcourses') {
 5453:                 $unset = '0';
 5454:                 if ($type eq '_LC_adv') {
 5455:                     $unset = '';
 5456:                 }
 5457:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5458:                     $confhash{$item}{$type} .= '=';
 5459:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5460:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5461:                     }
 5462:                 }
 5463:             } elsif ($context eq 'requestauthor') {
 5464:                 $unset = '0';
 5465:                 if ($type eq '_LC_adv') {
 5466:                     $unset = '';
 5467:                 }
 5468:             } else {
 5469:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5470:                     $confhash{$item}{$type} = 1;
 5471:                 } else {
 5472:                     $confhash{$item}{$type} = 0;
 5473:                 }
 5474:             }
 5475:             if (ref($domconfig{$action}) eq 'HASH') {
 5476:                 if ($action eq 'requestauthor') {
 5477:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5478:                         $changes{$type} = 1;
 5479:                     }
 5480:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5481:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5482:                         $changes{$item}{$type} = 1;
 5483:                     }
 5484:                 } else {
 5485:                     if ($context eq 'requestcourses') {
 5486:                         if ($confhash{$item}{$type} ne $unset) {
 5487:                             $changes{$item}{$type} = 1;
 5488:                         }
 5489:                     } else {
 5490:                         if (!$confhash{$item}{$type}) {
 5491:                             $changes{$item}{$type} = 1;
 5492:                         }
 5493:                     }
 5494:                 }
 5495:             } else {
 5496:                 if ($context eq 'requestcourses') {
 5497:                     if ($confhash{$item}{$type} ne $unset) {
 5498:                         $changes{$item}{$type} = 1;
 5499:                     }
 5500:                 } elsif ($context eq 'requestauthor') {
 5501:                     if ($confhash{$type} ne $unset) {
 5502:                         $changes{$type} = 1;
 5503:                     }
 5504:                 } else {
 5505:                     if (!$confhash{$item}{$type}) {
 5506:                         $changes{$item}{$type} = 1;
 5507:                     }
 5508:                 }
 5509:             }
 5510:         }
 5511:     }
 5512:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5513:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5514:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5515:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5516:                     if (exists($confhash{'defaultquota'}{$key})) {
 5517:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5518:                             $changes{'defaultquota'}{$key} = 1;
 5519:                         }
 5520:                     } else {
 5521:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5522:                     }
 5523:                 }
 5524:             } else {
 5525:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5526:                     if (exists($confhash{'defaultquota'}{$key})) {
 5527:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5528:                             $changes{'defaultquota'}{$key} = 1;
 5529:                         }
 5530:                     } else {
 5531:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5532:                     }
 5533:                 }
 5534:             }
 5535:         }
 5536:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5537:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5538:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5539:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5540:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5541:                             $changes{'defaultquota'}{$key} = 1;
 5542:                         }
 5543:                     } else {
 5544:                         if (!exists($domconfig{'quotas'}{$key})) {
 5545:                             $changes{'defaultquota'}{$key} = 1;
 5546:                         }
 5547:                     }
 5548:                 } else {
 5549:                     $changes{'defaultquota'}{$key} = 1;
 5550:                 }
 5551:             }
 5552:         }
 5553:     }
 5554: 
 5555:     if ($context eq 'requestauthor') {
 5556:         $domdefaults{'requestauthor'} = \%confhash;
 5557:     } else {
 5558:         foreach my $key (keys(%confhash)) {
 5559:             $domdefaults{$key} = $confhash{$key};
 5560:         }
 5561:     }
 5562: 
 5563:     my %quotahash = (
 5564:                       $action => { %confhash }
 5565:                     );
 5566:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5567:                                              $dom);
 5568:     if ($putresult eq 'ok') {
 5569:         if (keys(%changes) > 0) {
 5570:             my $cachetime = 24*60*60;
 5571:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5572: 
 5573:             $resulttext = &mt('Changes made:').'<ul>';
 5574:             unless (($context eq 'requestcourses') || 
 5575:                     ($context eq 'requestauthor')) {
 5576:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5577:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5578:                     foreach my $type (@{$types},'default') {
 5579:                         if (defined($changes{'defaultquota'}{$type})) {
 5580:                             my $typetitle = $usertypes->{$type};
 5581:                             if ($type eq 'default') {
 5582:                                 $typetitle = $othertitle;
 5583:                             }
 5584:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5585:                         }
 5586:                     }
 5587:                     $resulttext .= '</ul></li>';
 5588:                 }
 5589:             }
 5590:             my %newenv;
 5591:             foreach my $item (@usertools) {
 5592:                 my (%haschgs,%inconf);
 5593:                 if ($context eq 'requestauthor') {
 5594:                     %haschgs = %changes;
 5595:                     %inconf = %confhash; 
 5596:                 } else {
 5597:                     if (ref($changes{$item}) eq 'HASH') {
 5598:                         %haschgs = %{$changes{$item}};
 5599:                     }
 5600:                     if (ref($confhash{$item}) eq 'HASH') {
 5601:                         %inconf = %{$confhash{$item}};
 5602:                     }
 5603:                 }
 5604:                 if (keys(%haschgs) > 0) {
 5605:                     my $newacc = 
 5606:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5607:                                                           $env{'user.domain'},
 5608:                                                           $item,'reload',$context);
 5609:                     if (($context eq 'requestcourses') || 
 5610:                         ($context eq 'requestauthor')) {
 5611:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5612:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5613:                         }
 5614:                     } else {
 5615:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5616:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5617:                         }
 5618:                     }
 5619:                     unless ($context eq 'requestauthor') {
 5620:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5621:                     }
 5622:                     foreach my $type (@{$types},'default','_LC_adv') {
 5623:                         if ($haschgs{$type}) {
 5624:                             my $typetitle = $usertypes->{$type};
 5625:                             if ($type eq 'default') {
 5626:                                 $typetitle = $othertitle;
 5627:                             } elsif ($type eq '_LC_adv') {
 5628:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5629:                             }
 5630:                             if ($inconf{$type}) {
 5631:                                 if ($context eq 'requestcourses') {
 5632:                                     my $cond;
 5633:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 5634:                                         if ($1 eq '') {
 5635:                                             $cond = &mt('(Automatic processing of any request).');
 5636:                                         } else {
 5637:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5638:                                         }
 5639:                                     } else { 
 5640:                                         $cond = $conditions{$inconf{$type}};
 5641:                                     }
 5642:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5643:                                 } else {
 5644:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5645:                                 }
 5646:                             } else {
 5647:                                 if ($type eq '_LC_adv') {
 5648:                                     if ($inconf{$type} eq '0') {
 5649:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5650:                                     } else { 
 5651:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5652:                                     }
 5653:                                 } else {
 5654:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5655:                                 }
 5656:                             }
 5657:                         }
 5658:                     }
 5659:                     unless ($context eq 'requestauthor') {
 5660:                         $resulttext .= '</ul></li>';
 5661:                     }
 5662:                 }
 5663:             }
 5664:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 5665:                 if (ref($changes{'notify'}) eq 'HASH') {
 5666:                     if ($changes{'notify'}{'approval'}) {
 5667:                         if (ref($confhash{'notify'}) eq 'HASH') {
 5668:                             if ($confhash{'notify'}{'approval'}) {
 5669:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 5670:                             } else {
 5671:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 5672:                             }
 5673:                         }
 5674:                     }
 5675:                 }
 5676:             }
 5677:             $resulttext .= '</ul>';
 5678:             if (keys(%newenv)) {
 5679:                 &Apache::lonnet::appenv(\%newenv);
 5680:             }
 5681:         } else {
 5682:             if ($context eq 'requestcourses') {
 5683:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 5684:             } elsif ($context eq 'requestauthor') {
 5685:                 $resulttext = &mt('No changes made to rights to request author space.');
 5686:             } else {
 5687:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 5688:             }
 5689:         }
 5690:     } else {
 5691:         $resulttext = '<span class="LC_error">'.
 5692: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5693:     }
 5694:     return $resulttext;
 5695: }
 5696: 
 5697: sub modify_autoenroll {
 5698:     my ($dom,%domconfig) = @_;
 5699:     my ($resulttext,%changes);
 5700:     my %currautoenroll;
 5701:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 5702:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 5703:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 5704:         }
 5705:     }
 5706:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 5707:     my %title = ( run => 'Auto-enrollment active',
 5708:                   sender => 'Sender for notification messages',
 5709:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 5710:     my @offon = ('off','on');
 5711:     my $sender_uname = $env{'form.sender_uname'};
 5712:     my $sender_domain = $env{'form.sender_domain'};
 5713:     if ($sender_domain eq '') {
 5714:         $sender_uname = '';
 5715:     } elsif ($sender_uname eq '') {
 5716:         $sender_domain = '';
 5717:     }
 5718:     my $coowners = $env{'form.autoassign_coowners'};
 5719:     my %autoenrollhash =  (
 5720:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 5721:                                        'sender_uname' => $sender_uname,
 5722:                                        'sender_domain' => $sender_domain,
 5723:                                        'co-owners' => $coowners,
 5724:                                 }
 5725:                      );
 5726:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 5727:                                              $dom);
 5728:     if ($putresult eq 'ok') {
 5729:         if (exists($currautoenroll{'run'})) {
 5730:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 5731:                  $changes{'run'} = 1;
 5732:              }
 5733:         } elsif ($autorun) {
 5734:             if ($env{'form.autoenroll_run'} ne '1') {
 5735:                  $changes{'run'} = 1;
 5736:             }
 5737:         }
 5738:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 5739:             $changes{'sender'} = 1;
 5740:         }
 5741:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 5742:             $changes{'sender'} = 1;
 5743:         }
 5744:         if ($currautoenroll{'co-owners'} ne '') {
 5745:             if ($currautoenroll{'co-owners'} ne $coowners) {
 5746:                 $changes{'coowners'} = 1;
 5747:             }
 5748:         } elsif ($coowners) {
 5749:             $changes{'coowners'} = 1;
 5750:         }      
 5751:         if (keys(%changes) > 0) {
 5752:             $resulttext = &mt('Changes made:').'<ul>';
 5753:             if ($changes{'run'}) {
 5754:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 5755:             }
 5756:             if ($changes{'sender'}) {
 5757:                 if ($sender_uname eq '' || $sender_domain eq '') {
 5758:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 5759:                 } else {
 5760:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 5761:                 }
 5762:             }
 5763:             if ($changes{'coowners'}) {
 5764:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 5765:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 5766:             }
 5767:             $resulttext .= '</ul>';
 5768:         } else {
 5769:             $resulttext = &mt('No changes made to auto-enrollment settings');
 5770:         }
 5771:     } else {
 5772:         $resulttext = '<span class="LC_error">'.
 5773: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5774:     }
 5775:     return $resulttext;
 5776: }
 5777: 
 5778: sub modify_autoupdate {
 5779:     my ($dom,%domconfig) = @_;
 5780:     my ($resulttext,%currautoupdate,%fields,%changes);
 5781:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 5782:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 5783:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 5784:         }
 5785:     }
 5786:     my @offon = ('off','on');
 5787:     my %title = &Apache::lonlocal::texthash (
 5788:                    run => 'Auto-update:',
 5789:                    classlists => 'Updates to user information in classlists?'
 5790:                 );
 5791:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5792:     my %fieldtitles = &Apache::lonlocal::texthash (
 5793:                         id => 'Student/Employee ID',
 5794:                         permanentemail => 'E-mail address',
 5795:                         lastname => 'Last Name',
 5796:                         firstname => 'First Name',
 5797:                         middlename => 'Middle Name',
 5798:                         generation => 'Generation',
 5799:                       );
 5800:     $othertitle = &mt('All users');
 5801:     if (keys(%{$usertypes}) >  0) {
 5802:         $othertitle = &mt('Other users');
 5803:     }
 5804:     foreach my $key (keys(%env)) {
 5805:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 5806:             my ($usertype,$item) = ($1,$2);
 5807:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 5808:                 if ($usertype eq 'default') {   
 5809:                     push(@{$fields{$1}},$2);
 5810:                 } elsif (ref($types) eq 'ARRAY') {
 5811:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 5812:                         push(@{$fields{$1}},$2);
 5813:                     }
 5814:                 }
 5815:             }
 5816:         }
 5817:     }
 5818:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 5819:     @lockablenames = sort(@lockablenames);
 5820:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 5821:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 5822:         if (@changed) {
 5823:             $changes{'lockablenames'} = 1;
 5824:         }
 5825:     } else {
 5826:         if (@lockablenames) {
 5827:             $changes{'lockablenames'} = 1;
 5828:         }
 5829:     }
 5830:     my %updatehash = (
 5831:                       autoupdate => { run => $env{'form.autoupdate_run'},
 5832:                                       classlists => $env{'form.classlists'},
 5833:                                       fields => {%fields},
 5834:                                       lockablenames => \@lockablenames,
 5835:                                     }
 5836:                      );
 5837:     foreach my $key (keys(%currautoupdate)) {
 5838:         if (($key eq 'run') || ($key eq 'classlists')) {
 5839:             if (exists($updatehash{autoupdate}{$key})) {
 5840:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 5841:                     $changes{$key} = 1;
 5842:                 }
 5843:             }
 5844:         } elsif ($key eq 'fields') {
 5845:             if (ref($currautoupdate{$key}) eq 'HASH') {
 5846:                 foreach my $item (@{$types},'default') {
 5847:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 5848:                         my $change = 0;
 5849:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 5850:                             if (!exists($fields{$item})) {
 5851:                                 $change = 1;
 5852:                                 last;
 5853:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 5854:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 5855:                                     $change = 1;
 5856:                                     last;
 5857:                                 }
 5858:                             }
 5859:                         }
 5860:                         if ($change) {
 5861:                             push(@{$changes{$key}},$item);
 5862:                         }
 5863:                     } 
 5864:                 }
 5865:             }
 5866:         } elsif ($key eq 'lockablenames') {
 5867:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 5868:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 5869:                 if (@changed) {
 5870:                     $changes{'lockablenames'} = 1;
 5871:                 }
 5872:             } else {
 5873:                 if (@lockablenames) {
 5874:                     $changes{'lockablenames'} = 1;
 5875:                 }
 5876:             }
 5877:         }
 5878:     }
 5879:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 5880:         if (@lockablenames) {
 5881:             $changes{'lockablenames'} = 1;
 5882:         }
 5883:     }
 5884:     foreach my $item (@{$types},'default') {
 5885:         if (defined($fields{$item})) {
 5886:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 5887:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 5888:                     my $change = 0;
 5889:                     if (ref($fields{$item}) eq 'ARRAY') {
 5890:                         foreach my $type (@{$fields{$item}}) {
 5891:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 5892:                                 $change = 1;
 5893:                                 last;
 5894:                             }
 5895:                         }
 5896:                     }
 5897:                     if ($change) {
 5898:                         push(@{$changes{'fields'}},$item);
 5899:                     }
 5900:                 } else {
 5901:                     push(@{$changes{'fields'}},$item);
 5902:                 }
 5903:             } else {
 5904:                 push(@{$changes{'fields'}},$item);
 5905:             }
 5906:         }
 5907:     }
 5908:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 5909:                                              $dom);
 5910:     if ($putresult eq 'ok') {
 5911:         if (keys(%changes) > 0) {
 5912:             $resulttext = &mt('Changes made:').'<ul>';
 5913:             foreach my $key (sort(keys(%changes))) {
 5914:                 if ($key eq 'lockablenames') {
 5915:                     $resulttext .= '<li>';
 5916:                     if (@lockablenames) {
 5917:                         $usertypes->{'default'} = $othertitle;
 5918:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 5919:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 5920:                     } else {
 5921:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 5922:                     }
 5923:                     $resulttext .= '</li>';
 5924:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 5925:                     foreach my $item (@{$changes{$key}}) {
 5926:                         my @newvalues;
 5927:                         foreach my $type (@{$fields{$item}}) {
 5928:                             push(@newvalues,$fieldtitles{$type});
 5929:                         }
 5930:                         my $newvaluestr;
 5931:                         if (@newvalues > 0) {
 5932:                             $newvaluestr = join(', ',@newvalues);
 5933:                         } else {
 5934:                             $newvaluestr = &mt('none');
 5935:                         }
 5936:                         if ($item eq 'default') {
 5937:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 5938:                         } else {
 5939:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 5940:                         }
 5941:                     }
 5942:                 } else {
 5943:                     my $newvalue;
 5944:                     if ($key eq 'run') {
 5945:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 5946:                     } else {
 5947:                         $newvalue = $offon[$env{'form.'.$key}];
 5948:                     }
 5949:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 5950:                 }
 5951:             }
 5952:             $resulttext .= '</ul>';
 5953:         } else {
 5954:             $resulttext = &mt('No changes made to autoupdates');
 5955:         }
 5956:     } else {
 5957:         $resulttext = '<span class="LC_error">'.
 5958: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5959:     }
 5960:     return $resulttext;
 5961: }
 5962: 
 5963: sub modify_autocreate {
 5964:     my ($dom,%domconfig) = @_;
 5965:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 5966:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 5967:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 5968:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 5969:         }
 5970:     }
 5971:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 5972:                  req => 'Auto-creation of validated requests for official courses',
 5973:                  xmldc => 'Identity of course creator of courses from XML files',
 5974:                );
 5975:     my @types = ('xml','req');
 5976:     foreach my $item (@types) {
 5977:         $newvals{$item} = $env{'form.autocreate_'.$item};
 5978:         $newvals{$item} =~ s/\D//g;
 5979:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 5980:     }
 5981:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 5982:     my %domcoords = &get_active_dcs($dom);
 5983:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 5984:         $newvals{'xmldc'} = '';
 5985:     } 
 5986:     %autocreatehash =  (
 5987:                         autocreate => { xml => $newvals{'xml'},
 5988:                                         req => $newvals{'req'},
 5989:                                       }
 5990:                        );
 5991:     if ($newvals{'xmldc'} ne '') {
 5992:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 5993:     }
 5994:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 5995:                                              $dom);
 5996:     if ($putresult eq 'ok') {
 5997:         my @items = @types;
 5998:         if ($newvals{'xml'}) {
 5999:             push(@items,'xmldc');
 6000:         }
 6001:         foreach my $item (@items) {
 6002:             if (exists($currautocreate{$item})) {
 6003:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6004:                     $changes{$item} = 1;
 6005:                 }
 6006:             } elsif ($newvals{$item}) {
 6007:                 $changes{$item} = 1;
 6008:             }
 6009:         }
 6010:         if (keys(%changes) > 0) {
 6011:             my @offon = ('off','on'); 
 6012:             $resulttext = &mt('Changes made:').'<ul>';
 6013:             foreach my $item (@types) {
 6014:                 if ($changes{$item}) {
 6015:                     my $newtxt = $offon[$newvals{$item}];
 6016:                     $resulttext .= '<li>'.&mt("$title{$item} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>';
 6017:                 }
 6018:             }
 6019:             if ($changes{'xmldc'}) {
 6020:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6021:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6022:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>'; 
 6023:             }
 6024:             $resulttext .= '</ul>';
 6025:         } else {
 6026:             $resulttext = &mt('No changes made to auto-creation settings');
 6027:         }
 6028:     } else {
 6029:         $resulttext = '<span class="LC_error">'.
 6030:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6031:     }
 6032:     return $resulttext;
 6033: }
 6034: 
 6035: sub modify_directorysrch {
 6036:     my ($dom,%domconfig) = @_;
 6037:     my ($resulttext,%changes);
 6038:     my %currdirsrch;
 6039:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6040:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6041:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6042:         }
 6043:     }
 6044:     my %title = ( available => 'Directory search available',
 6045:                   localonly => 'Other domains can search',
 6046:                   searchby => 'Search types',
 6047:                   searchtypes => 'Search latitude');
 6048:     my @offon = ('off','on');
 6049:     my @otherdoms = ('Yes','No');
 6050: 
 6051:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6052:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6053:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6054: 
 6055:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6056:     if (keys(%{$usertypes}) == 0) {
 6057:         @cansearch = ('default');
 6058:     } else {
 6059:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6060:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6061:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6062:                     push(@{$changes{'cansearch'}},$type);
 6063:                 }
 6064:             }
 6065:             foreach my $type (@cansearch) {
 6066:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6067:                     push(@{$changes{'cansearch'}},$type);
 6068:                 }
 6069:             }
 6070:         } else {
 6071:             push(@{$changes{'cansearch'}},@cansearch);
 6072:         }
 6073:     }
 6074: 
 6075:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6076:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6077:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6078:                 push(@{$changes{'searchby'}},$by);
 6079:             }
 6080:         }
 6081:         foreach my $by (@searchby) {
 6082:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6083:                 push(@{$changes{'searchby'}},$by);
 6084:             }
 6085:         }
 6086:     } else {
 6087:         push(@{$changes{'searchby'}},@searchby);
 6088:     }
 6089: 
 6090:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6091:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6092:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6093:                 push(@{$changes{'searchtypes'}},$type);
 6094:             }
 6095:         }
 6096:         foreach my $type (@searchtypes) {
 6097:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6098:                 push(@{$changes{'searchtypes'}},$type);
 6099:             }
 6100:         }
 6101:     } else {
 6102:         if (exists($currdirsrch{'searchtypes'})) {
 6103:             foreach my $type (@searchtypes) {  
 6104:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6105:                     push(@{$changes{'searchtypes'}},$type);
 6106:                 }
 6107:             }
 6108:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6109:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6110:             }   
 6111:         } else {
 6112:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6113:         }
 6114:     }
 6115: 
 6116:     my %dirsrch_hash =  (
 6117:             directorysrch => { available => $env{'form.dirsrch_available'},
 6118:                                cansearch => \@cansearch,
 6119:                                localonly => $env{'form.dirsrch_localonly'},
 6120:                                searchby => \@searchby,
 6121:                                searchtypes => \@searchtypes,
 6122:                              }
 6123:             );
 6124:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6125:                                              $dom);
 6126:     if ($putresult eq 'ok') {
 6127:         if (exists($currdirsrch{'available'})) {
 6128:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6129:                  $changes{'available'} = 1;
 6130:              }
 6131:         } else {
 6132:             if ($env{'form.dirsrch_available'} eq '1') {
 6133:                 $changes{'available'} = 1;
 6134:             }
 6135:         }
 6136:         if (exists($currdirsrch{'localonly'})) {
 6137:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6138:                  $changes{'localonly'} = 1;
 6139:              }
 6140:         } else {
 6141:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6142:                 $changes{'localonly'} = 1;
 6143:             }
 6144:         }
 6145:         if (keys(%changes) > 0) {
 6146:             $resulttext = &mt('Changes made:').'<ul>';
 6147:             if ($changes{'available'}) {
 6148:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6149:             }
 6150:             if ($changes{'localonly'}) {
 6151:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6152:             }
 6153: 
 6154:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6155:                 my $chgtext;
 6156:                 if (ref($usertypes) eq 'HASH') {
 6157:                     if (keys(%{$usertypes}) > 0) {
 6158:                         foreach my $type (@{$types}) {
 6159:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6160:                                 $chgtext .= $usertypes->{$type}.'; ';
 6161:                             }
 6162:                         }
 6163:                         if (grep(/^default$/,@cansearch)) {
 6164:                             $chgtext .= $othertitle;
 6165:                         } else {
 6166:                             $chgtext =~ s/\; $//;
 6167:                         }
 6168:                         $resulttext .= '<li>'.&mt("Users from domain '<span class=\"LC_cusr_emph\">[_1]</span>' permitted to search the institutional directory set to: [_2]",$dom,$chgtext).'</li>';
 6169:                     }
 6170:                 }
 6171:             }
 6172:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6173:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6174:                 my $chgtext;
 6175:                 foreach my $type (@{$titleorder}) {
 6176:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6177:                         if (defined($searchtitles->{$type})) {
 6178:                             $chgtext .= $searchtitles->{$type}.'; ';
 6179:                         }
 6180:                     }
 6181:                 }
 6182:                 $chgtext =~ s/\; $//;
 6183:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6184:             }
 6185:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6186:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6187:                 my $chgtext;
 6188:                 foreach my $type (@{$srchtypeorder}) {
 6189:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6190:                         if (defined($srchtypes_desc->{$type})) {
 6191:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6192:                         }
 6193:                     }
 6194:                 }
 6195:                 $chgtext =~ s/\; $//;
 6196:                 $resulttext .= '<li>'.&mt("$title{'searchtypes'} set to: \"[_1]\"",$chgtext).'</li>';
 6197:             }
 6198:             $resulttext .= '</ul>';
 6199:         } else {
 6200:             $resulttext = &mt('No changes made to institution directory search settings');
 6201:         }
 6202:     } else {
 6203:         $resulttext = '<span class="LC_error">'.
 6204:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6205:     }
 6206:     return $resulttext;
 6207: }
 6208: 
 6209: sub modify_contacts {
 6210:     my ($dom,%domconfig) = @_;
 6211:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6212:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6213:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6214:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6215:         }
 6216:     }
 6217:     my (%others,%to,%bcc);
 6218:     my @contacts = ('supportemail','adminemail');
 6219:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6220:                     'requestsmail');
 6221:     foreach my $type (@mailings) {
 6222:         @{$newsetting{$type}} = 
 6223:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6224:         foreach my $item (@contacts) {
 6225:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6226:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6227:             } else {
 6228:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6229:             }
 6230:         }  
 6231:         $others{$type} = $env{'form.'.$type.'_others'};
 6232:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6233:         if ($type eq 'helpdeskmail') {
 6234:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6235:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6236:         }
 6237:     }
 6238:     foreach my $item (@contacts) {
 6239:         $to{$item} = $env{'form.'.$item};
 6240:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6241:     }
 6242:     if (keys(%currsetting) > 0) {
 6243:         foreach my $item (@contacts) {
 6244:             if ($to{$item} ne $currsetting{$item}) {
 6245:                 $changes{$item} = 1;
 6246:             }
 6247:         }
 6248:         foreach my $type (@mailings) {
 6249:             foreach my $item (@contacts) {
 6250:                 if (ref($currsetting{$type}) eq 'HASH') {
 6251:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6252:                         push(@{$changes{$type}},$item);
 6253:                     }
 6254:                 } else {
 6255:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6256:                 }
 6257:             }
 6258:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6259:                 push(@{$changes{$type}},'others');
 6260:             }
 6261:             if ($type eq 'helpdeskmail') {   
 6262:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6263:                     push(@{$changes{$type}},'bcc'); 
 6264:                 }
 6265:             }
 6266:         }
 6267:     } else {
 6268:         my %default;
 6269:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6270:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6271:         $default{'errormail'} = 'adminemail';
 6272:         $default{'packagesmail'} = 'adminemail';
 6273:         $default{'helpdeskmail'} = 'supportemail';
 6274:         $default{'lonstatusmail'} = 'adminemail';
 6275:         $default{'requestsmail'} = 'adminemail';
 6276:         foreach my $item (@contacts) {
 6277:            if ($to{$item} ne $default{$item}) {
 6278:               $changes{$item} = 1;
 6279:            } 
 6280:         }
 6281:         foreach my $type (@mailings) {
 6282:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6283:                
 6284:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6285:             }
 6286:             if ($others{$type} ne '') {
 6287:                 push(@{$changes{$type}},'others');
 6288:             }
 6289:             if ($type eq 'helpdeskmail') {
 6290:                 if ($bcc{$type} ne '') {
 6291:                     push(@{$changes{$type}},'bcc');
 6292:                 }
 6293:             }
 6294:         }
 6295:     }
 6296:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6297:                                              $dom);
 6298:     if ($putresult eq 'ok') {
 6299:         if (keys(%changes) > 0) {
 6300:             my ($titles,$short_titles)  = &contact_titles();
 6301:             $resulttext = &mt('Changes made:').'<ul>';
 6302:             foreach my $item (@contacts) {
 6303:                 if ($changes{$item}) {
 6304:                     $resulttext .= '<li>'.$titles->{$item}.
 6305:                                     &mt(' set to: ').
 6306:                                     '<span class="LC_cusr_emph">'.
 6307:                                     $to{$item}.'</span></li>';
 6308:                 }
 6309:             }
 6310:             foreach my $type (@mailings) {
 6311:                 if (ref($changes{$type}) eq 'ARRAY') {
 6312:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6313:                     my @text;
 6314:                     foreach my $item (@{$newsetting{$type}}) {
 6315:                         push(@text,$short_titles->{$item});
 6316:                     }
 6317:                     if ($others{$type} ne '') {
 6318:                         push(@text,$others{$type});
 6319:                     }
 6320:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6321:                                    join(', ',@text).'</span>';
 6322:                     if ($type eq 'helpdeskmail') {
 6323:                         if ($bcc{$type} ne '') {
 6324:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6325:                         }
 6326:                     }
 6327:                     $resulttext .= '</li>';
 6328:                 }
 6329:             }
 6330:             $resulttext .= '</ul>';
 6331:         } else {
 6332:             $resulttext = &mt('No changes made to contact information');
 6333:         }
 6334:     } else {
 6335:         $resulttext = '<span class="LC_error">'.
 6336:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6337:     }
 6338:     return $resulttext;
 6339: }
 6340: 
 6341: sub modify_usercreation {
 6342:     my ($dom,%domconfig) = @_;
 6343:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6344:     my $warningmsg;
 6345:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6346:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6347:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6348:         }
 6349:     }
 6350:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6351:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6352:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6353:     my @contexts = ('author','course','requestcrs','selfcreate');
 6354:     foreach my $item(@contexts) {
 6355:         if ($item eq 'selfcreate') {
 6356:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6357:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6358:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6359:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6360:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6361:                         $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.');   
 6362:                     }
 6363:                 }
 6364:             }
 6365:         } else {
 6366:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6367:         }
 6368:     }
 6369:     my ($othertitle,$usertypes,$types) = 
 6370:         &Apache::loncommon::sorted_inst_types($dom);
 6371:     if (ref($types) eq 'ARRAY') {
 6372:         if (@{$types} > 0) {
 6373:             @{$cancreate{'statustocreate'}} = 
 6374:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6375:         } else {
 6376:             @{$cancreate{'statustocreate'}} = ();
 6377:         }
 6378:         push(@contexts,'statustocreate');
 6379:     }
 6380:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6381:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6382:         foreach my $item (@contexts) {
 6383:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6384:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6385:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6386:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6387:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6388:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6389:                                     push(@{$changes{'cancreate'}},$item);
 6390:                                 }
 6391:                             }
 6392:                         }
 6393:                     }
 6394:                 } else {
 6395:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6396:                         if (@{$cancreate{$item}} > 0) {
 6397:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6398:                                 push(@{$changes{'cancreate'}},$item);
 6399:                             }
 6400:                         }
 6401:                     } else {
 6402:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6403:                             if (@{$cancreate{$item}} < 3) {
 6404:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6405:                                     push(@{$changes{'cancreate'}},$item);
 6406:                                 }
 6407:                             }
 6408:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6409:                             if (@{$cancreate{$item}} > 0) {
 6410:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6411:                                     push(@{$changes{'cancreate'}},$item);
 6412:                                 }
 6413:                             }
 6414:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6415:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6416:                                 push(@{$changes{'cancreate'}},$item);
 6417:                             }
 6418:                         }
 6419:                     }
 6420:                 }
 6421:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6422:                     foreach my $type (@{$cancreate{$item}}) {
 6423:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6424:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6425:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6426:                                     push(@{$changes{'cancreate'}},$item);
 6427:                                 }
 6428:                             }
 6429:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6430:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6431:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6432:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6433:                                     push(@{$changes{'cancreate'}},$item);
 6434:                                 }
 6435:                             }
 6436:                         }
 6437:                     }
 6438:                 }
 6439:             } else {
 6440:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6441:                     push(@{$changes{'cancreate'}},$item);
 6442:                 }
 6443:             }
 6444:         }
 6445:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6446:         foreach my $item (@contexts) {
 6447:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6448:                 if ($cancreate{$item} ne 'any') {
 6449:                     push(@{$changes{'cancreate'}},$item);
 6450:                 }
 6451:             } else {
 6452:                 if ($cancreate{$item} ne 'none') {
 6453:                     push(@{$changes{'cancreate'}},$item);
 6454:                 }
 6455:             }
 6456:         }
 6457:     } else {
 6458:         foreach my $item (@contexts)  {
 6459:             push(@{$changes{'cancreate'}},$item);
 6460:         }
 6461:     }
 6462: 
 6463:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6464:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6465:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6466:                 push(@{$changes{'username_rule'}},$type);
 6467:             }
 6468:         }
 6469:         foreach my $type (@username_rule) {
 6470:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6471:                 push(@{$changes{'username_rule'}},$type);
 6472:             }
 6473:         }
 6474:     } else {
 6475:         push(@{$changes{'username_rule'}},@username_rule);
 6476:     }
 6477: 
 6478:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6479:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 6480:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 6481:                 push(@{$changes{'id_rule'}},$type);
 6482:             }
 6483:         }
 6484:         foreach my $type (@id_rule) {
 6485:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 6486:                 push(@{$changes{'id_rule'}},$type);
 6487:             }
 6488:         }
 6489:     } else {
 6490:         push(@{$changes{'id_rule'}},@id_rule);
 6491:     }
 6492: 
 6493:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 6494:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 6495:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 6496:                 push(@{$changes{'email_rule'}},$type);
 6497:             }
 6498:         }
 6499:         foreach my $type (@email_rule) {
 6500:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 6501:                 push(@{$changes{'email_rule'}},$type);
 6502:             }
 6503:         }
 6504:     } else {
 6505:         push(@{$changes{'email_rule'}},@email_rule);
 6506:     }
 6507: 
 6508:     my @authen_contexts = ('author','course','domain');
 6509:     my @authtypes = ('int','krb4','krb5','loc');
 6510:     my %authhash;
 6511:     foreach my $item (@authen_contexts) {
 6512:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 6513:         foreach my $auth (@authtypes) {
 6514:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 6515:                 $authhash{$item}{$auth} = 1;
 6516:             } else {
 6517:                 $authhash{$item}{$auth} = 0;
 6518:             }
 6519:         }
 6520:     }
 6521:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 6522:         foreach my $item (@authen_contexts) {
 6523:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 6524:                 foreach my $auth (@authtypes) {
 6525:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 6526:                         push(@{$changes{'authtypes'}},$item);
 6527:                         last;
 6528:                     }
 6529:                 }
 6530:             }
 6531:         }
 6532:     } else {
 6533:         foreach my $item (@authen_contexts) {
 6534:             push(@{$changes{'authtypes'}},$item);
 6535:         }
 6536:     }
 6537: 
 6538:     my %usercreation_hash =  (
 6539:             usercreation => {
 6540:                               cancreate     => \%cancreate,
 6541:                               username_rule => \@username_rule,
 6542:                               id_rule       => \@id_rule,
 6543:                               email_rule    => \@email_rule,
 6544:                               authtypes     => \%authhash,
 6545:                             }
 6546:             );
 6547: 
 6548:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 6549:                                              $dom);
 6550: 
 6551:     my %selfcreatetypes = (
 6552:                              sso   => 'users authenticated by institutional single sign on',
 6553:                              login => 'users authenticated by institutional log-in',
 6554:                              email => 'users who provide a valid e-mail address for use as the username',
 6555:                           );
 6556:     if ($putresult eq 'ok') {
 6557:         if (keys(%changes) > 0) {
 6558:             $resulttext = &mt('Changes made:').'<ul>';
 6559:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 6560:                 my %lt = &usercreation_types();
 6561:                 foreach my $type (@{$changes{'cancreate'}}) {
 6562:                     my $chgtext;
 6563:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 6564:                         $chgtext = $lt{$type}.', ';
 6565:                     }
 6566:                     if ($type eq 'selfcreate') {
 6567:                         if (@{$cancreate{$type}} == 0) {
 6568:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 6569:                         } else {
 6570:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 6571:                             foreach my $case (@{$cancreate{$type}}) {
 6572:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 6573:                             }
 6574:                             $chgtext .= '</ul>';
 6575:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 6576:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 6577:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 6578:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 6579:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6580:                                         }
 6581:                                     }
 6582:                                 }
 6583:                             }
 6584:                         }
 6585:                     } elsif ($type eq 'statustocreate') {
 6586:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 6587:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 6588:                             if (@{$cancreate{'selfcreate'}} > 0) {
 6589:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 6590: 
 6591:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 6592:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 6593:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6594:                                     } 
 6595:                                 } elsif (ref($usertypes) eq 'HASH') {
 6596:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6597:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 6598:                                     } else {
 6599:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 6600:                                     }
 6601:                                     $chgtext .= '<ul>';
 6602:                                     foreach my $case (@{$cancreate{$type}}) {
 6603:                                         if ($case eq 'default') {
 6604:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 6605:                                         } else {
 6606:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 6607:                                         }
 6608:                                     }
 6609:                                     $chgtext .= '</ul>';
 6610:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6611:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 6612:                                     }
 6613:                                 }
 6614:                             } else {
 6615:                                 if (@{$cancreate{$type}} == 0) {
 6616:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 6617:                                 } else {
 6618:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 6619:                                 }
 6620:                             }
 6621:                         }
 6622:                     } elsif ($type eq 'captcha') {
 6623:                         if ($cancreate{$type} eq 'notused') {
 6624:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 6625:                         } else {
 6626:                             my %captchas = &captcha_phrases();
 6627:                             if ($captchas{$cancreate{$type}}) {
 6628:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 6629:                             } else {
 6630:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.'); 
 6631:                             }
 6632:                         }
 6633:                     } elsif ($type eq 'recaptchakeys') {
 6634:                         my ($privkey,$pubkey);
 6635:                         if (ref($cancreate{$type}) eq 'HASH') {
 6636:                             $pubkey = $cancreate{$type}{'public'};
 6637:                             $privkey = $cancreate{$type}{'private'};
 6638:                         }
 6639:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 6640:                         if (!$pubkey) {
 6641:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 6642:                         } else {
 6643:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6644:                         }
 6645:                         if (!$privkey) {
 6646:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 6647:                         } else {
 6648:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6649:                         }
 6650:                         $chgtext .= '</ul>';
 6651:                     } else {
 6652:                         if ($cancreate{$type} eq 'none') {
 6653:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 6654:                         } elsif ($cancreate{$type} eq 'any') {
 6655:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 6656:                         } elsif ($cancreate{$type} eq 'official') {
 6657:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 6658:                         } elsif ($cancreate{$type} eq 'unofficial') {
 6659:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 6660:                         }
 6661:                     }
 6662:                     $resulttext .= '<li>'.$chgtext.'</li>';
 6663:                 }
 6664:             }
 6665:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 6666:                 my ($rules,$ruleorder) = 
 6667:                     &Apache::lonnet::inst_userrules($dom,'username');
 6668:                 my $chgtext = '<ul>';
 6669:                 foreach my $type (@username_rule) {
 6670:                     if (ref($rules->{$type}) eq 'HASH') {
 6671:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 6672:                     }
 6673:                 }
 6674:                 $chgtext .= '</ul>';
 6675:                 if (@username_rule > 0) {
 6676:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 6677:                 } else {
 6678:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 6679:                 }
 6680:             }
 6681:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 6682:                 my ($idrules,$idruleorder) = 
 6683:                     &Apache::lonnet::inst_userrules($dom,'id');
 6684:                 my $chgtext = '<ul>';
 6685:                 foreach my $type (@id_rule) {
 6686:                     if (ref($idrules->{$type}) eq 'HASH') {
 6687:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 6688:                     }
 6689:                 }
 6690:                 $chgtext .= '</ul>';
 6691:                 if (@id_rule > 0) {
 6692:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 6693:                 } else {
 6694:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 6695:                 }
 6696:             }
 6697:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 6698:                 my ($emailrules,$emailruleorder) =
 6699:                     &Apache::lonnet::inst_userrules($dom,'email');
 6700:                 my $chgtext = '<ul>';
 6701:                 foreach my $type (@email_rule) {
 6702:                     if (ref($emailrules->{$type}) eq 'HASH') {
 6703:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 6704:                     }
 6705:                 }
 6706:                 $chgtext .= '</ul>';
 6707:                 if (@email_rule > 0) {
 6708:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 6709:                 } else {
 6710:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 6711:                 }
 6712:             }
 6713: 
 6714:             my %authname = &authtype_names();
 6715:             my %context_title = &context_names();
 6716:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 6717:                 my $chgtext = '<ul>';
 6718:                 foreach my $type (@{$changes{'authtypes'}}) {
 6719:                     my @allowed;
 6720:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 6721:                     foreach my $auth (@authtypes) {
 6722:                         if ($authhash{$type}{$auth}) {
 6723:                             push(@allowed,$authname{$auth});
 6724:                         }
 6725:                     }
 6726:                     if (@allowed > 0) {
 6727:                         $chgtext .= join(', ',@allowed).'</li>';
 6728:                     } else {
 6729:                         $chgtext .= &mt('none').'</li>';
 6730:                     }
 6731:                 }
 6732:                 $chgtext .= '</ul>';
 6733:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 6734:                 $resulttext .= '</li>';
 6735:             }
 6736:             $resulttext .= '</ul>';
 6737:         } else {
 6738:             $resulttext = &mt('No changes made to user creation settings');
 6739:         }
 6740:     } else {
 6741:         $resulttext = '<span class="LC_error">'.
 6742:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6743:     }
 6744:     if ($warningmsg ne '') {
 6745:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 6746:     }
 6747:     return $resulttext;
 6748: }
 6749: 
 6750: sub process_captcha {
 6751:     my ($container,$changes,$newsettings,$current) = @_;
 6752:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 6753:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 6754:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 6755:         $newsettings->{'captcha'} = 'original';
 6756:     }
 6757:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 6758:         if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 6759:             push(@{$changes->{'cancreate'}},'captcha');
 6760:         } elsif (!defined($changes->{'cancreate'})) {
 6761:             $changes->{'cancreate'} = ['captcha'];
 6762:         }
 6763:     }
 6764:     my ($newpub,$newpriv,$currpub,$currpriv);
 6765:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 6766:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 6767:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 6768:     }
 6769:     $newsettings->{'recaptchakeys'} = {
 6770:                                          public  => $newpub,
 6771:                                          private => $newpriv,
 6772:                                       };
 6773:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 6774:         $currpub = $current->{'recaptchakeys'}{'public'};
 6775:         $currpriv = $current->{'recaptchakeys'}{'private'};
 6776:     }
 6777:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 6778:         if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 6779:             push(@{$changes->{'cancreate'}},'recaptchakeys');
 6780:         } elsif (!defined($changes->{'cancreate'})) {
 6781:             $changes->{'cancreate'} = ['recaptchakeys'];
 6782:         }
 6783:     }
 6784:     return;
 6785: }
 6786: 
 6787: sub modify_usermodification {
 6788:     my ($dom,%domconfig) = @_;
 6789:     my ($resulttext,%curr_usermodification,%changes);
 6790:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 6791:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 6792:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 6793:         }
 6794:     }
 6795:     my @contexts = ('author','course','selfcreate');
 6796:     my %context_title = (
 6797:                            author => 'In author context',
 6798:                            course => 'In course context',
 6799:                            selfcreate => 'When self creating account', 
 6800:                         );
 6801:     my @fields = ('lastname','firstname','middlename','generation',
 6802:                   'permanentemail','id');
 6803:     my %roles = (
 6804:                   author => ['ca','aa'],
 6805:                   course => ['st','ep','ta','in','cr'],
 6806:                 );
 6807:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6808:     if (ref($types) eq 'ARRAY') {
 6809:         push(@{$types},'default');
 6810:         $usertypes->{'default'} = $othertitle;
 6811:     }
 6812:     $roles{'selfcreate'} = $types;  
 6813:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6814:     my %modifyhash;
 6815:     foreach my $context (@contexts) {
 6816:         foreach my $role (@{$roles{$context}}) {
 6817:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 6818:             foreach my $item (@fields) {
 6819:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 6820:                     $modifyhash{$context}{$role}{$item} = 1;
 6821:                 } else {
 6822:                     $modifyhash{$context}{$role}{$item} = 0;
 6823:                 }
 6824:             }
 6825:         }
 6826:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 6827:             foreach my $role (@{$roles{$context}}) {
 6828:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 6829:                     foreach my $field (@fields) {
 6830:                         if ($modifyhash{$context}{$role}{$field} ne 
 6831:                                 $curr_usermodification{$context}{$role}{$field}) {
 6832:                             push(@{$changes{$context}},$role);
 6833:                             last;
 6834:                         }
 6835:                     }
 6836:                 }
 6837:             }
 6838:         } else {
 6839:             foreach my $context (@contexts) {
 6840:                 foreach my $role (@{$roles{$context}}) {
 6841:                     push(@{$changes{$context}},$role);
 6842:                 }
 6843:             }
 6844:         }
 6845:     }
 6846:     my %usermodification_hash =  (
 6847:                                    usermodification => \%modifyhash,
 6848:                                  );
 6849:     my $putresult = &Apache::lonnet::put_dom('configuration',
 6850:                                              \%usermodification_hash,$dom);
 6851:     if ($putresult eq 'ok') {
 6852:         if (keys(%changes) > 0) {
 6853:             $resulttext = &mt('Changes made: ').'<ul>';
 6854:             foreach my $context (@contexts) {
 6855:                 if (ref($changes{$context}) eq 'ARRAY') {
 6856:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 6857:                     if (ref($changes{$context}) eq 'ARRAY') {
 6858:                         foreach my $role (@{$changes{$context}}) {
 6859:                             my $rolename;
 6860:                             if ($context eq 'selfcreate') {
 6861:                                 $rolename = $role;
 6862:                                 if (ref($usertypes) eq 'HASH') {
 6863:                                     if ($usertypes->{$role} ne '') {
 6864:                                         $rolename = $usertypes->{$role};
 6865:                                     }
 6866:                                 }
 6867:                             } else {
 6868:                                 if ($role eq 'cr') {
 6869:                                     $rolename = &mt('Custom');
 6870:                                 } else {
 6871:                                     $rolename = &Apache::lonnet::plaintext($role);
 6872:                                 }
 6873:                             }
 6874:                             my @modifiable;
 6875:                             if ($context eq 'selfcreate') {
 6876:                                 $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): ');
 6877:                             } else {
 6878:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 6879:                             }
 6880:                             foreach my $field (@fields) {
 6881:                                 if ($modifyhash{$context}{$role}{$field}) {
 6882:                                     push(@modifiable,$fieldtitles{$field});
 6883:                                 }
 6884:                             }
 6885:                             if (@modifiable > 0) {
 6886:                                 $resulttext .= join(', ',@modifiable);
 6887:                             } else {
 6888:                                 $resulttext .= &mt('none'); 
 6889:                             }
 6890:                             $resulttext .= '</li>';
 6891:                         }
 6892:                         $resulttext .= '</ul></li>';
 6893:                     }
 6894:                 }
 6895:             }
 6896:             $resulttext .= '</ul>';
 6897:         } else {
 6898:             $resulttext = &mt('No changes made to user modification settings');
 6899:         }
 6900:     } else {
 6901:         $resulttext = '<span class="LC_error">'.
 6902:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6903:     }
 6904:     return $resulttext;
 6905: }
 6906: 
 6907: sub modify_defaults {
 6908:     my ($dom,$r) = @_;
 6909:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 6910:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6911:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 6912:     my @authtypes = ('internal','krb4','krb5','localauth');
 6913:     foreach my $item (@items) {
 6914:         $newvalues{$item} = $env{'form.'.$item};
 6915:         if ($item eq 'auth_def') {
 6916:             if ($newvalues{$item} ne '') {
 6917:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 6918:                     push(@errors,$item);
 6919:                 }
 6920:             }
 6921:         } elsif ($item eq 'lang_def') {
 6922:             if ($newvalues{$item} ne '') {
 6923:                 if ($newvalues{$item} =~ /^(\w+)/) {
 6924:                     my $langcode = $1;
 6925:                     if ($langcode ne 'x_chef') {
 6926:                         if (code2language($langcode) eq '') {
 6927:                             push(@errors,$item);
 6928:                         }
 6929:                     }
 6930:                 } else {
 6931:                     push(@errors,$item);
 6932:                 }
 6933:             }
 6934:         } elsif ($item eq 'timezone_def') {
 6935:             if ($newvalues{$item} ne '') {
 6936:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 6937:                     push(@errors,$item);   
 6938:                 }
 6939:             }
 6940:         } elsif ($item eq 'datelocale_def') {
 6941:             if ($newvalues{$item} ne '') {
 6942:                 my @datelocale_ids = DateTime::Locale->ids();
 6943:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 6944:                     push(@errors,$item);
 6945:                 }
 6946:             }
 6947:         } elsif ($item eq 'portal_def') {
 6948:             if ($newvalues{$item} ne '') {
 6949:                 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])\/?$/) {
 6950:                     push(@errors,$item);
 6951:                 }
 6952:             }
 6953:         }
 6954:         if (grep(/^\Q$item\E$/,@errors)) {
 6955:             $newvalues{$item} = $domdefaults{$item};
 6956:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 6957:             $changes{$item} = 1;
 6958:         }
 6959:         $domdefaults{$item} = $newvalues{$item};
 6960:     }
 6961:     my %defaults_hash = (
 6962:                          defaults => \%newvalues,
 6963:                         );
 6964:     my $title = &defaults_titles();
 6965:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 6966:                                              $dom);
 6967:     if ($putresult eq 'ok') {
 6968:         if (keys(%changes) > 0) {
 6969:             $resulttext = &mt('Changes made:').'<ul>';
 6970:             my $version = $r->dir_config('lonVersion');
 6971:             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";
 6972:             foreach my $item (sort(keys(%changes))) {
 6973:                 my $value = $env{'form.'.$item};
 6974:                 if ($value eq '') {
 6975:                     $value = &mt('none');
 6976:                 } elsif ($item eq 'auth_def') {
 6977:                     my %authnames = &authtype_names();
 6978:                     my %shortauth = (
 6979:                              internal => 'int',
 6980:                              krb4 => 'krb4',
 6981:                              krb5 => 'krb5',
 6982:                              localauth  => 'loc',
 6983:                     );
 6984:                     $value = $authnames{$shortauth{$value}};
 6985:                 }
 6986:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 6987:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 6988:             }
 6989:             $resulttext .= '</ul>';
 6990:             $mailmsgtext .= "\n";
 6991:             my $cachetime = 24*60*60;
 6992:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6993:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 6994:                 my $sysmail = $r->dir_config('lonSysEMail');
 6995:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 6996:             }
 6997:         } else {
 6998:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 6999:         }
 7000:     } else {
 7001:         $resulttext = '<span class="LC_error">'.
 7002:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7003:     }
 7004:     if (@errors > 0) {
 7005:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7006:         foreach my $item (@errors) {
 7007:             $resulttext .= ' "'.$title->{$item}.'",';
 7008:         }
 7009:         $resulttext =~ s/,$//;
 7010:     }
 7011:     return $resulttext;
 7012: }
 7013: 
 7014: sub modify_scantron {
 7015:     my ($r,$dom,$confname,%domconfig) = @_;
 7016:     my ($resulttext,%confhash,%changes,$errors);
 7017:     my $custom = 'custom.tab';
 7018:     my $default = 'default.tab';
 7019:     my $servadm = $r->dir_config('lonAdmEMail');
 7020:     my ($configuserok,$author_ok,$switchserver) = 
 7021:         &config_check($dom,$confname,$servadm);
 7022:     if ($env{'form.scantronformat.filename'} ne '') {
 7023:         my $error;
 7024:         if ($configuserok eq 'ok') {
 7025:             if ($switchserver) {
 7026:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7027:             } else {
 7028:                 if ($author_ok eq 'ok') {
 7029:                     my ($result,$scantronurl) =
 7030:                         &publishlogo($r,'upload','scantronformat',$dom,
 7031:                                      $confname,'scantron','','',$custom);
 7032:                     if ($result eq 'ok') {
 7033:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7034:                         $changes{'scantronformat'} = 1;
 7035:                     } else {
 7036:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7037:                     }
 7038:                 } else {
 7039:                     $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);
 7040:                 }
 7041:             }
 7042:         } else {
 7043:             $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);
 7044:         }
 7045:         if ($error) {
 7046:             &Apache::lonnet::logthis($error);
 7047:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7048:         }
 7049:     }
 7050:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7051:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7052:             if ($env{'form.scantronformat_del'}) {
 7053:                 $confhash{'scantron'}{'scantronformat'} = '';
 7054:                 $changes{'scantronformat'} = 1;
 7055:             }
 7056:         }
 7057:     }
 7058:     if (keys(%confhash) > 0) {
 7059:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7060:                                                  $dom);
 7061:         if ($putresult eq 'ok') {
 7062:             if (keys(%changes) > 0) {
 7063:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7064:                     $resulttext = &mt('Changes made:').'<ul>';
 7065:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7066:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7067:                     } else {
 7068:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7069:                     }
 7070:                     $resulttext .= '</ul>';
 7071:                 } else {
 7072:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7073:                 }
 7074:                 $resulttext .= '</ul>';
 7075:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7076:             } else {
 7077:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7078:             }
 7079:         } else {
 7080:             $resulttext = '<span class="LC_error">'.
 7081:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7082:         }
 7083:     } else {
 7084:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7085:     }
 7086:     if ($errors) {
 7087:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7088:                        $errors.'</ul>';
 7089:     }
 7090:     return $resulttext;
 7091: }
 7092: 
 7093: sub modify_coursecategories {
 7094:     my ($dom,%domconfig) = @_;
 7095:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7096:         $cathash);
 7097:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7098:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7099:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7100:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7101:             $changes{'togglecats'} = 1;
 7102:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7103:         }
 7104:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7105:             $changes{'categorize'} = 1;
 7106:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7107:         }
 7108:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7109:             $changes{'togglecatscomm'} = 1;
 7110:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7111:         }
 7112:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7113:             $changes{'categorizecomm'} = 1;
 7114:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7115:         }
 7116:     } else {
 7117:         $changes{'togglecats'} = 1;
 7118:         $changes{'categorize'} = 1;
 7119:         $changes{'togglecatscomm'} = 1;
 7120:         $changes{'categorizecomm'} = 1;
 7121:         $domconfig{'coursecategories'} = {
 7122:                                              togglecats => $env{'form.togglecats'},
 7123:                                              categorize => $env{'form.categorize'},
 7124:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7125:                                              categorizecomm => $env{'form.categorizecomm'},
 7126:                                          };
 7127:     }
 7128:     if (ref($cathash) eq 'HASH') {
 7129:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7130:             push (@deletecategory,'instcode::0');
 7131:         }
 7132:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7133:             push(@deletecategory,'communities::0');
 7134:         }
 7135:     }
 7136:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7137:     if (ref($cathash) eq 'HASH') {
 7138:         if (@deletecategory > 0) {
 7139:             #FIXME Need to remove category from all courses using a deleted category 
 7140:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7141:             foreach my $item (@deletecategory) {
 7142:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7143:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7144:                     $deletions{$item} = 1;
 7145:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7146:                 }
 7147:             }
 7148:         }
 7149:         foreach my $item (keys(%{$cathash})) {
 7150:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7151:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7152:                 $reorderings{$item} = 1;
 7153:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7154:             }
 7155:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7156:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7157:                 my $newdepth = $depth+1;
 7158:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7159:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7160:                 $adds{$newitem} = 1; 
 7161:             }
 7162:             if ($env{'form.subcat_'.$item} ne '') {
 7163:                 my $newcat = $env{'form.subcat_'.$item};
 7164:                 my $newdepth = $depth+1;
 7165:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7166:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7167:                 $adds{$newitem} = 1;
 7168:             }
 7169:         }
 7170:     }
 7171:     if ($env{'form.instcode'} eq '1') {
 7172:         if (ref($cathash) eq 'HASH') {
 7173:             my $newitem = 'instcode::0';
 7174:             if ($cathash->{$newitem} eq '') {  
 7175:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7176:                 $adds{$newitem} = 1;
 7177:             }
 7178:         } else {
 7179:             my $newitem = 'instcode::0';
 7180:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7181:             $adds{$newitem} = 1;
 7182:         }
 7183:     }
 7184:     if ($env{'form.communities'} eq '1') {
 7185:         if (ref($cathash) eq 'HASH') {
 7186:             my $newitem = 'communities::0';
 7187:             if ($cathash->{$newitem} eq '') {
 7188:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7189:                 $adds{$newitem} = 1;
 7190:             }
 7191:         } else {
 7192:             my $newitem = 'communities::0';
 7193:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7194:             $adds{$newitem} = 1;
 7195:         }
 7196:     }
 7197:     if ($env{'form.addcategory_name'} ne '') {
 7198:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7199:             ($env{'form.addcategory_name'} ne 'communities')) {
 7200:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7201:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7202:             $adds{$newitem} = 1;
 7203:         }
 7204:     }
 7205:     my $putresult;
 7206:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7207:         if (keys(%deletions) > 0) {
 7208:             foreach my $key (keys(%deletions)) {
 7209:                 if ($predelallitems{$key} ne '') {
 7210:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7211:                 }
 7212:             }
 7213:         }
 7214:         my (@chkcats,@chktrails,%chkallitems);
 7215:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7216:         if (ref($chkcats[0]) eq 'ARRAY') {
 7217:             my $depth = 0;
 7218:             my $chg = 0;
 7219:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7220:                 my $name = $chkcats[0][$i];
 7221:                 my $item;
 7222:                 if ($name eq '') {
 7223:                     $chg ++;
 7224:                 } else {
 7225:                     $item = &escape($name).'::0';
 7226:                     if ($chg) {
 7227:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7228:                     }
 7229:                     $depth ++; 
 7230:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7231:                     $depth --;
 7232:                 }
 7233:             }
 7234:         }
 7235:     }
 7236:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7237:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7238:         if ($putresult eq 'ok') {
 7239:             my %title = (
 7240:                          togglecats     => 'Show/Hide a course in catalog',
 7241:                          categorize     => 'Assign a category to a course',
 7242:                          togglecatscomm => 'Show/Hide a community in catalog',
 7243:                          categorizecomm => 'Assign a category to a community',
 7244:                         );
 7245:             my %level = (
 7246:                          dom  => 'set in Domain ("Modify Course/Community")',
 7247:                          crs  => 'set in Course ("Course Configuration")',
 7248:                          comm => 'set in Community ("Community Configuration")',
 7249:                         );
 7250:             $resulttext = &mt('Changes made:').'<ul>';
 7251:             if ($changes{'togglecats'}) {
 7252:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7253:             }
 7254:             if ($changes{'categorize'}) {
 7255:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7256:             }
 7257:             if ($changes{'togglecatscomm'}) {
 7258:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7259:             }
 7260:             if ($changes{'categorizecomm'}) {
 7261:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7262:             }
 7263:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7264:                 my $cathash;
 7265:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7266:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7267:                 } else {
 7268:                     $cathash = {};
 7269:                 } 
 7270:                 my (@cats,@trails,%allitems);
 7271:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7272:                 if (keys(%deletions) > 0) {
 7273:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7274:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7275:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7276:                     }
 7277:                     $resulttext .= '</ul></li>';
 7278:                 }
 7279:                 if (keys(%reorderings) > 0) {
 7280:                     my %sort_by_trail;
 7281:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7282:                     foreach my $key (keys(%reorderings)) {
 7283:                         if ($allitems{$key} ne '') {
 7284:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7285:                         }
 7286:                     }
 7287:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7288:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7289:                     }
 7290:                     $resulttext .= '</ul></li>';
 7291:                 }
 7292:                 if (keys(%adds) > 0) {
 7293:                     my %sort_by_trail;
 7294:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7295:                     foreach my $key (keys(%adds)) {
 7296:                         if ($allitems{$key} ne '') {
 7297:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7298:                         }
 7299:                     }
 7300:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7301:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7302:                     }
 7303:                     $resulttext .= '</ul></li>';
 7304:                 }
 7305:             }
 7306:             $resulttext .= '</ul>';
 7307:         } else {
 7308:             $resulttext = '<span class="LC_error">'.
 7309:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7310:         }
 7311:     } else {
 7312:         $resulttext = &mt('No changes made to course and community categories');
 7313:     }
 7314:     return $resulttext;
 7315: }
 7316: 
 7317: sub modify_serverstatuses {
 7318:     my ($dom,%domconfig) = @_;
 7319:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7320:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7321:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7322:     }
 7323:     my @pages = &serverstatus_pages();
 7324:     foreach my $type (@pages) {
 7325:         $newserverstatus{$type}{'namedusers'} = '';
 7326:         $newserverstatus{$type}{'machines'} = '';
 7327:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7328:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7329:             my @okusers;
 7330:             foreach my $user (@users) {
 7331:                 my ($uname,$udom) = split(/:/,$user);
 7332:                 if (($udom =~ /^$match_domain$/) &&   
 7333:                     (&Apache::lonnet::domain($udom)) &&
 7334:                     ($uname =~ /^$match_username$/)) {
 7335:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7336:                         push(@okusers,$user);
 7337:                     }
 7338:                 }
 7339:             }
 7340:             if (@okusers > 0) {
 7341:                  @okusers = sort(@okusers);
 7342:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7343:             }
 7344:         }
 7345:         if (defined($env{'form.'.$type.'_machines'})) {
 7346:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7347:             my @okmachines;
 7348:             foreach my $ip (@machines) {
 7349:                 my @parts = split(/\./,$ip);
 7350:                 next if (@parts < 4);
 7351:                 my $badip = 0;
 7352:                 for (my $i=0; $i<4; $i++) {
 7353:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7354:                         $badip = 1;
 7355:                         last;
 7356:                     }
 7357:                 }
 7358:                 if (!$badip) {
 7359:                     push(@okmachines,$ip);     
 7360:                 }
 7361:             }
 7362:             @okmachines = sort(@okmachines);
 7363:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7364:         }
 7365:     }
 7366:     my %serverstatushash =  (
 7367:                                 serverstatuses => \%newserverstatus,
 7368:                             );
 7369:     foreach my $type (@pages) {
 7370:         foreach my $setting ('namedusers','machines') {
 7371:             my (@current,@new);
 7372:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7373:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7374:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7375:                 }
 7376:             }
 7377:             if ($newserverstatus{$type}{$setting} ne '') {
 7378:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7379:             }
 7380:             if (@current > 0) {
 7381:                 if (@new > 0) {
 7382:                     foreach my $item (@current) {
 7383:                         if (!grep(/^\Q$item\E$/,@new)) {
 7384:                             $changes{$type}{$setting} = 1;
 7385:                             last;
 7386:                         }
 7387:                     }
 7388:                     foreach my $item (@new) {
 7389:                         if (!grep(/^\Q$item\E$/,@current)) {
 7390:                             $changes{$type}{$setting} = 1;
 7391:                             last;
 7392:                         }
 7393:                     }
 7394:                 } else {
 7395:                     $changes{$type}{$setting} = 1;
 7396:                 }
 7397:             } elsif (@new > 0) {
 7398:                 $changes{$type}{$setting} = 1;
 7399:             }
 7400:         }
 7401:     }
 7402:     if (keys(%changes) > 0) {
 7403:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7404:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7405:                                                  \%serverstatushash,$dom);
 7406:         if ($putresult eq 'ok') {
 7407:             $resulttext .= &mt('Changes made:').'<ul>';
 7408:             foreach my $type (@pages) {
 7409:                 if (ref($changes{$type}) eq 'HASH') {
 7410:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7411:                     if ($changes{$type}{'namedusers'}) {
 7412:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7413:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7414:                         } else {
 7415:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7416:                         }
 7417:                     }
 7418:                     if ($changes{$type}{'machines'}) {
 7419:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7420:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7421:                         } else {
 7422:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7423:                         }
 7424: 
 7425:                     }
 7426:                     $resulttext .= '</ul></li>';
 7427:                 }
 7428:             }
 7429:             $resulttext .= '</ul>';
 7430:         } else {
 7431:             $resulttext = '<span class="LC_error">'.
 7432:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7433: 
 7434:         }
 7435:     } else {
 7436:         $resulttext = &mt('No changes made to access to server status pages');
 7437:     }
 7438:     return $resulttext;
 7439: }
 7440: 
 7441: sub modify_helpsettings {
 7442:     my ($r,$dom,$confname,%domconfig) = @_;
 7443:  	my ($resulttext,$errors,%changes,%helphash);
 7444:  	
 7445:  	my $customhelpfile  = $env{'form.loginhelpurl.filename'};
 7446:     my $defaulthelpfile = 'defaulthelp.html';
 7447:  	my $servadm = $r->dir_config('lonAdmEMail');
 7448:     my ($configuserok,$author_ok,$switchserver) = 
 7449:         &config_check($dom,$confname,$servadm);
 7450:  	
 7451:  	my %defaultchecked = ('submitbugs'	=> 'on');
 7452:  	my @offon = ('off','on');
 7453:     my %title = ( submitbugs     => 'Display link for users to submit a bug', 
 7454:     			  loginhelpurl  => 'Unauthenticated login help page set to custom file');
 7455:     			  
 7456:     my @toggles = ('submitbugs');
 7457: 
 7458:     $helphash{'helpsettings'} = {};
 7459:     
 7460:     if (ref($domconfig{'helpsettings'}) ne 'HASH') {
 7461:         if ($domconfig{'helpsettings'} eq '') {
 7462:             $domconfig{'helpsettings'} = {};
 7463:         }
 7464:     }
 7465:     
 7466:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7467:     	
 7468:         foreach my $item (@toggles) {
 7469:         	
 7470: 			if ($defaultchecked{$item} eq 'on') { 
 7471: 				if (($domconfig{'helpsettings'}{$item} eq '') &&
 7472: 					 ($env{'form.'.$item} eq '0')) {
 7473: 					$changes{$item} = 1;
 7474: 				} elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7475: 					$changes{$item} = 1;
 7476: 				}
 7477: 			} elsif ($defaultchecked{$item} eq 'off') {
 7478: 				if (($domconfig{'helpsettings'}{$item} eq '') &&
 7479: 					 ($env{'form.'.$item} eq '1')) {
 7480: 					$changes{$item} = 1;
 7481: 				} elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7482: 					$changes{$item} = 1;
 7483: 				}
 7484: 			}
 7485: 			$helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7486: 		}
 7487: 		
 7488: 		if ($customhelpfile ne '') {
 7489: 			my $error;
 7490: 			if ($configuserok eq 'ok') {
 7491: 				if ($switchserver) {
 7492: 					$error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7493: 				} else {
 7494: 					if ($author_ok eq 'ok') {
 7495: 						my ($result,$loginhelpurl) =
 7496: 							&publishlogo($r,'upload','loginhelpurl',$dom,
 7497: 										 $confname,'help','','',$customhelpfile);
 7498: 						if ($result eq 'ok') {
 7499: 							$helphash{'helpsettings'}{'loginhelpurl'} = $loginhelpurl;
 7500: 							$changes{'loginhelpurl'} = 1;
 7501: 						} else {
 7502: 							$error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customhelpfile,$result);
 7503: 						}
 7504: 					} else {
 7505: 						$error = &mt("Upload of [_1] failed because an author role could not be assigned to a Domain Configuration user ([_2]) in domain: [_3].  Error was: [_4].",$customhelpfile,$confname,$dom,$author_ok);
 7506: 					}
 7507: 				}
 7508: 			} else {
 7509: 				$error = &mt("Upload of [_1] failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$customhelpfile,$confname,$dom,$configuserok);
 7510: 			}
 7511: 			if ($error) {
 7512: 				&Apache::lonnet::logthis($error);
 7513: 				$errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7514: 			}
 7515: 		}
 7516: 		
 7517:         if ($domconfig{'helpsettings'}{'loginhelpurl'} ne '') {
 7518:             if ($env{'form.loginhelpurl_del'}) {
 7519:                 $helphash{'helpsettings'}{'loginhelpurl'} = '';
 7520:                 $changes{'loginhelpurl'} = 1;
 7521:             }
 7522:         }
 7523:     }
 7524:     
 7525:     
 7526:     my $putresult;
 7527:     
 7528:     if (keys(%changes) > 0) {
 7529:     	$putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7530:     } else {
 7531:     	$putresult = 'ok';
 7532:     }
 7533:                                              
 7534:     if ($putresult eq 'ok') {
 7535:         if (keys(%changes) > 0) {
 7536: 			$resulttext = &mt('Changes made:').'<ul>';
 7537: 			foreach my $item (sort(keys(%changes))) {
 7538: 				if ($item eq 'submitbugs') {
 7539: 					$resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7540: 				}
 7541: 				if ($item eq 'loginhelpurl') {
 7542: 					if ($helphash{'helpsettings'}{'loginhelpurl'} eq '') {
 7543:                         $resulttext .= '<li>'.&mt('[_1] help file removed; [_2] file will be used for the unathorized help page in this domain.',$customhelpfile,$defaulthelpfile).'</li>';
 7544:                     } else {
 7545:                         $resulttext .= '<li>'.&mt("$title{$item} [_1]",$customhelpfile).'</li>';
 7546:                     }
 7547: 				}
 7548: 			}
 7549: 			$resulttext .= '</ul>';
 7550: 		} else {
 7551: 			$resulttext = &mt('No changes made to help settings');
 7552: 		}
 7553:     } else {
 7554:         $resulttext = '<span class="LC_error">'.
 7555: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7556:     }
 7557:     if ($errors) {
 7558:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7559:                        $errors.'</ul>';
 7560:     }
 7561:     return $resulttext;
 7562: }
 7563: 
 7564: sub modify_coursedefaults {
 7565:     my ($dom,%domconfig) = @_;
 7566:     my ($resulttext,$errors,%changes,%defaultshash);
 7567:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7568:     my @offon = ('off','on');
 7569:     my @toggles = ('canuse_pdfforms');
 7570: 
 7571:     $defaultshash{'coursedefaults'} = {};
 7572: 
 7573:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7574:         if ($domconfig{'coursedefaults'} eq '') {
 7575:             $domconfig{'coursedefaults'} = {};
 7576:         }
 7577:     }
 7578: 
 7579:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7580:         foreach my $item (@toggles) {
 7581:             if ($defaultchecked{$item} eq 'on') {
 7582:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7583:                     ($env{'form.'.$item} eq '0')) {
 7584:                     $changes{$item} = 1;
 7585:                 } elsif ($domconfig{'coursdefaults'}{$item} ne $env{'form.'.$item}) {
 7586:                     $changes{$item} = 1;
 7587:                 }
 7588:             } elsif ($defaultchecked{$item} eq 'off') {
 7589:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7590:                     ($env{'form.'.$item} eq '1')) {
 7591:                     $changes{$item} = 1;
 7592:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7593:                     $changes{$item} = 1;
 7594:                 }
 7595:             }
 7596:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7597:         }
 7598:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7599:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7600:         $newdefresponder =~ s/\D//g;
 7601:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7602:             $newdefresponder = 1;
 7603:         }
 7604:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7605:         if ($currdefresponder ne $newdefresponder) {
 7606:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7607:                 $changes{'anonsurvey_threshold'} = 1;
 7608:             }
 7609:         }
 7610:     }
 7611:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7612:                                              $dom);
 7613:     if ($putresult eq 'ok') {
 7614:         if (keys(%changes) > 0) {
 7615:             if ($changes{'canuse_pdfforms'}) {
 7616:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7617:                 $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7618:                 my $cachetime = 24*60*60;
 7619:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7620:             }
 7621:             $resulttext = &mt('Changes made:').'<ul>';
 7622:             foreach my $item (sort(keys(%changes))) {
 7623:                 if ($item eq 'canuse_pdfforms') {
 7624:                     if ($env{'form.'.$item} eq '1') {
 7625:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7626:                     } else {
 7627:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7628:                     }
 7629:                 } elsif ($item eq 'anonsurvey_threshold') {
 7630:                         $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7631:                 }
 7632:             }
 7633:             $resulttext .= '</ul>';
 7634:         } else {
 7635:             $resulttext = &mt('No changes made to course defaults');
 7636:         }
 7637:     } else {
 7638:         $resulttext = '<span class="LC_error">'.
 7639:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7640:     }
 7641:     return $resulttext;
 7642: }
 7643: 
 7644: sub modify_usersessions {
 7645:     my ($dom,%domconfig) = @_;
 7646:     my @hostingtypes = ('version','excludedomain','includedomain');
 7647:     my @offloadtypes = ('primary','default');
 7648:     my %types = (
 7649:                   remote => \@hostingtypes,
 7650:                   hosted => \@hostingtypes,
 7651:                   spares => \@offloadtypes,
 7652:                 );
 7653:     my @prefixes = ('remote','hosted','spares');
 7654:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7655:     my (%by_ip,%by_location,@intdoms);
 7656:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7657:     my @locations = sort(keys(%by_location));
 7658:     my (%defaultshash,%changes);
 7659:     foreach my $prefix (@prefixes) {
 7660:         $defaultshash{'usersessions'}{$prefix} = {};
 7661:     }
 7662:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7663:     my $resulttext;
 7664:     my %iphost = &Apache::lonnet::get_iphost();
 7665:     foreach my $prefix (@prefixes) {
 7666:         next if ($prefix eq 'spares');
 7667:         foreach my $type (@{$types{$prefix}}) {
 7668:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7669:             if ($type eq 'version') {
 7670:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7671:                 my $okvalue;
 7672:                 if ($value ne '') {
 7673:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 7674:                         $okvalue = $value;
 7675:                     }
 7676:                 }
 7677:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7678:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7679:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 7680:                             if ($inuse == 0) {
 7681:                                 $changes{$prefix}{$type} = 1;
 7682:                             } else {
 7683:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 7684:                                     $changes{$prefix}{$type} = 1;
 7685:                                 }
 7686:                                 if ($okvalue ne '') {
 7687:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7688:                                 } 
 7689:                             }
 7690:                         } else {
 7691:                             if (($inuse == 1) && ($okvalue ne '')) {
 7692:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7693:                                 $changes{$prefix}{$type} = 1;
 7694:                             }
 7695:                         }
 7696:                     } else {
 7697:                         if (($inuse == 1) && ($okvalue ne '')) {
 7698:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7699:                             $changes{$prefix}{$type} = 1;
 7700:                         }
 7701:                     }
 7702:                 } else {
 7703:                     if (($inuse == 1) && ($okvalue ne '')) {
 7704:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7705:                         $changes{$prefix}{$type} = 1;
 7706:                     }
 7707:                 }
 7708:             } else {
 7709:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 7710:                 my @okvals;
 7711:                 foreach my $val (@vals) {
 7712:                     if ($val =~ /:/) {
 7713:                         my @items = split(/:/,$val);
 7714:                         foreach my $item (@items) {
 7715:                             if (ref($by_location{$item}) eq 'ARRAY') {
 7716:                                 push(@okvals,$item);
 7717:                             }
 7718:                         }
 7719:                     } else {
 7720:                         if (ref($by_location{$val}) eq 'ARRAY') {
 7721:                             push(@okvals,$val);
 7722:                         }
 7723:                     }
 7724:                 }
 7725:                 @okvals = sort(@okvals);
 7726:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7727:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7728:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7729:                             if ($inuse == 0) {
 7730:                                 $changes{$prefix}{$type} = 1; 
 7731:                             } else {
 7732:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7733:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 7734:                                 if (@changed > 0) {
 7735:                                     $changes{$prefix}{$type} = 1;
 7736:                                 }
 7737:                             }
 7738:                         } else {
 7739:                             if ($inuse == 1) {
 7740:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7741:                                 $changes{$prefix}{$type} = 1;
 7742:                             }
 7743:                         } 
 7744:                     } else {
 7745:                         if ($inuse == 1) {
 7746:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7747:                             $changes{$prefix}{$type} = 1;
 7748:                         }
 7749:                     }
 7750:                 } else {
 7751:                     if ($inuse == 1) {
 7752:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7753:                         $changes{$prefix}{$type} = 1;
 7754:                     }
 7755:                 }
 7756:             }
 7757:         }
 7758:     }
 7759: 
 7760:     my @alldoms = &Apache::lonnet::all_domains();
 7761:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7762:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 7763:     my $savespares;
 7764: 
 7765:     foreach my $lonhost (sort(keys(%servers))) {
 7766:         my $serverhomeID =
 7767:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 7768:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 7769:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 7770:         my %spareschg;
 7771:         foreach my $type (@{$types{'spares'}}) {
 7772:             my @okspares;
 7773:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 7774:             foreach my $server (@checked) {
 7775:                 if (&Apache::lonnet::hostname($server) ne '') {
 7776:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 7777:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 7778:                             push(@okspares,$server);
 7779:                         }
 7780:                     }
 7781:                 }
 7782:             }
 7783:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 7784:             my $newspare;
 7785:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 7786:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 7787:                     $newspare = $new;
 7788:                 }
 7789:             }
 7790:             my @spares;
 7791:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 7792:                 @spares = sort(@okspares,$newspare);
 7793:             } else {
 7794:                 @spares = sort(@okspares);
 7795:             }
 7796:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 7797:             if (ref($spareid{$lonhost}) eq 'HASH') {
 7798:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 7799:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 7800:                     if (@diffs > 0) {
 7801:                         $spareschg{$type} = 1;
 7802:                     }
 7803:                 }
 7804:             }
 7805:         }
 7806:         if (keys(%spareschg) > 0) {
 7807:             $changes{'spares'}{$lonhost} = \%spareschg;
 7808:         }
 7809:     }
 7810: 
 7811:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7812:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 7813:             if (ref($changes{'spares'}) eq 'HASH') {
 7814:                 if (keys(%{$changes{'spares'}}) > 0) {
 7815:                     $savespares = 1;
 7816:                 }
 7817:             }
 7818:         } else {
 7819:             $savespares = 1;
 7820:         }
 7821:     }
 7822: 
 7823:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 7824:     if ((keys(%changes) > 0) || ($savespares)) {
 7825:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7826:                                                  $dom);
 7827:         if ($putresult eq 'ok') {
 7828:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 7829:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 7830:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 7831:                 }
 7832:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 7833:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 7834:                 }
 7835:             }
 7836:             my $cachetime = 24*60*60;
 7837:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7838:             if (keys(%changes) > 0) {
 7839:                 my %lt = &usersession_titles();
 7840:                 $resulttext = &mt('Changes made:').'<ul>';
 7841:                 foreach my $prefix (@prefixes) {
 7842:                     if (ref($changes{$prefix}) eq 'HASH') {
 7843:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 7844:                         if ($prefix eq 'spares') {
 7845:                             if (ref($changes{$prefix}) eq 'HASH') {
 7846:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 7847:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 7848:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 7849:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 7850:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 7851:                                         foreach my $type (@{$types{$prefix}}) {
 7852:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 7853:                                                 my $offloadto = &mt('None');
 7854:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 7855:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 7856:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 7857:                                                     }
 7858:                                                 }
 7859:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 7860:                                             }
 7861:                                         }
 7862:                                     }
 7863:                                     $resulttext .= '</li>';
 7864:                                 }
 7865:                             }
 7866:                         } else {
 7867:                             foreach my $type (@{$types{$prefix}}) {
 7868:                                 if (defined($changes{$prefix}{$type})) {
 7869:                                     my $newvalue;
 7870:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 7871:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 7872:                                             if ($type eq 'version') {
 7873:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 7874:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7875:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 7876:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 7877:                                                 }
 7878:                                             }
 7879:                                         }
 7880:                                     }
 7881:                                     if ($newvalue eq '') {
 7882:                                         if ($type eq 'version') {
 7883:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 7884:                                         } else {
 7885:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 7886:                                         }
 7887:                                     } else {
 7888:                                         if ($type eq 'version') {
 7889:                                             $newvalue .= ' '.&mt('(or later)'); 
 7890:                                         }
 7891:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 7892:                                     }
 7893:                                 }
 7894:                             }
 7895:                         }
 7896:                         $resulttext .= '</ul>';
 7897:                     }
 7898:                 }
 7899:                 $resulttext .= '</ul>';
 7900:             } else {
 7901:                 $resulttext = $nochgmsg;
 7902:             }
 7903:         } else {
 7904:             $resulttext = '<span class="LC_error">'.
 7905:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7906:         }
 7907:     } else {
 7908:         $resulttext = $nochgmsg;
 7909:     }
 7910:     return $resulttext;
 7911: }
 7912: 
 7913: sub modify_loadbalancing {
 7914:     my ($dom,%domconfig) = @_;
 7915:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7916:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7917:     my ($othertitle,$usertypes,$types) =
 7918:         &Apache::loncommon::sorted_inst_types($dom);
 7919:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7920:     my @sparestypes = ('primary','default');
 7921:     my %typetitles = &sparestype_titles();
 7922:     my $resulttext;
 7923:     if (keys(%servers) > 1) {
 7924:         my ($currbalancer,$currtargets,$currrules);
 7925:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 7926:             $currbalancer = $domconfig{'loadbalancing'}{'lonhost'};
 7927:             $currtargets = $domconfig{'loadbalancing'}{'targets'};
 7928:             $currrules = $domconfig{'loadbalancing'}{'rules'};
 7929:         } else {
 7930:             ($currbalancer,$currtargets) = 
 7931:                 &Apache::lonnet::get_lonbalancer_config(\%servers);
 7932:         }
 7933:         my ($saveloadbalancing,%defaultshash,%changes);
 7934:         my ($alltypes,$othertypes,$titles) =
 7935:             &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 7936:         my %ruletitles = &offloadtype_text();
 7937:         my $balancer = $env{'form.loadbalancing_lonhost'};
 7938:         if (!$servers{$balancer}) {
 7939:             undef($balancer);
 7940:         }
 7941:         if ($currbalancer ne $balancer) {
 7942:             $changes{'lonhost'} = 1;
 7943:         }
 7944:         $defaultshash{'loadbalancing'}{'lonhost'} = $balancer;
 7945:         if ($balancer ne '') {
 7946:             unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 7947:                 $saveloadbalancing = 1;
 7948:             }
 7949:             foreach my $sparetype (@sparestypes) {
 7950:                 my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$sparetype);
 7951:                 my @offloadto;
 7952:                 foreach my $target (@targets) {
 7953:                     if (($servers{$target}) && ($target ne $balancer)) {
 7954:                         if ($sparetype eq 'default') {
 7955:                             if (ref($defaultshash{'loadbalancing'}{'targets'}{'primary'}) eq 'ARRAY') {
 7956:                                 next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{'targets'}{'primary'}}));
 7957:                             }
 7958:                         }
 7959:                         unless(grep(/^\Q$target\E$/,@offloadto)) {
 7960:                             push(@offloadto,$target);
 7961:                         }
 7962:                     }
 7963:                     $defaultshash{'loadbalancing'}{'targets'}{$sparetype} = \@offloadto;
 7964:                 }
 7965:             }
 7966:         } else {
 7967:             foreach my $sparetype (@sparestypes) {
 7968:                 $defaultshash{'loadbalancing'}{'targets'}{$sparetype} = [];
 7969:             }
 7970:         }
 7971:         if (ref($currtargets) eq 'HASH') {
 7972:             foreach my $sparetype (@sparestypes) {
 7973:                 if (ref($currtargets->{$sparetype}) eq 'ARRAY') {
 7974:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets->{$sparetype},$defaultshash{'loadbalancing'}{'targets'}{$sparetype});
 7975:                     if (@targetdiffs > 0) {
 7976:                         $changes{'targets'} = 1;
 7977:                     }
 7978:                 } elsif (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 7979:                     if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 7980:                         $changes{'targets'} = 1;
 7981:                     }
 7982:                 }
 7983:             }
 7984:         } else {
 7985:             foreach my $sparetype (@sparestypes) {
 7986:                 if (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 7987:                     if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 7988:                         $changes{'targets'} = 1;  
 7989:                     }
 7990:                 }
 7991:             }  
 7992:         }
 7993:         my $ishomedom;
 7994:         if ($balancer ne '') {
 7995:             if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 7996:                 $ishomedom = 1;
 7997:             }
 7998:         }
 7999:         if (ref($alltypes) eq 'ARRAY') {
 8000:             foreach my $type (@{$alltypes}) {
 8001:                 my $rule;
 8002:                 if ($balancer ne '') {
 8003:                     unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) && 
 8004:                          (!$ishomedom)) {
 8005:                         $rule = $env{'form.loadbalancing_rules_'.$type};
 8006:                     }
 8007:                     if ($rule eq 'specific') {
 8008:                         $rule = $env{'form.loadbalancing_singleserver_'.$type};
 8009:                     }
 8010:                 }
 8011:                 $defaultshash{'loadbalancing'}{'rules'}{$type} = $rule;
 8012:                 if (ref($currrules) eq 'HASH') {
 8013:                     if ($rule ne $currrules->{$type}) {
 8014:                         $changes{'rules'}{$type} = 1;
 8015:                     }
 8016:                 } elsif ($rule ne '') {
 8017:                     $changes{'rules'}{$type} = 1;
 8018:                 }
 8019:             }
 8020:         }
 8021:         my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8022:         if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8023:             my $putresult = &Apache::lonnet::put_dom('configuration',
 8024:                                                      \%defaultshash,$dom);
 8025:             if ($putresult eq 'ok') {
 8026:                 if (keys(%changes) > 0) {
 8027:                     if ($changes{'lonhost'}) {
 8028:                         if ($currbalancer ne '') {
 8029:                             &Apache::lonnet::remote_devalidate_cache($currbalancer,'loadbalancing',$dom);
 8030:                         }
 8031:                         if ($balancer eq '') {
 8032:                             $resulttext .= '<li>'.&mt('Load Balancing with dedicated server discontinued').'</li>'; 
 8033:                         } else {
 8034:                             &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8035:                             $resulttext .= '<li>'.&mt('Dedicated Load Balancer server set to [_1]',$balancer);
 8036:                         }
 8037:                     } else {
 8038:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8039:                     }
 8040:                     if (($changes{'targets'}) && ($balancer ne '')) {
 8041:                         my %offloadstr;
 8042:                         foreach my $sparetype (@sparestypes) {
 8043:                             if (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 8044:                                 if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 8045:                                     $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}});
 8046:                                 }
 8047:                             }
 8048:                         }
 8049:                         if (keys(%offloadstr) == 0) {
 8050:                             $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8051:                         } else {
 8052:                             my $showoffload;
 8053:                             foreach my $sparetype (@sparestypes) {
 8054:                                 $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8055:                                 if (defined($offloadstr{$sparetype})) {
 8056:                                     $showoffload .= $offloadstr{$sparetype};
 8057:                                 } else {
 8058:                                     $showoffload .= &mt('None');
 8059:                                 }
 8060:                                 $showoffload .= ('&nbsp;'x3);
 8061:                             }
 8062:                             $resulttext .= '<li>'.&mt('By default, Load Balancer server set to offload to: [_1]',$showoffload).'</li>';
 8063:                         }
 8064:                     }
 8065:                     if ((ref($changes{'rules'}) eq 'HASH') && ($balancer ne '')) {
 8066:                         if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8067:                             foreach my $type (@{$alltypes}) {
 8068:                                 if ($changes{'rules'}{$type}) {
 8069:                                     my $rule = $defaultshash{'loadbalancing'}{'rules'}{$type};
 8070:                                     my $balancetext;
 8071:                                     if ($rule eq '') {
 8072:                                         $balancetext =  $ruletitles{'default'};
 8073:                                     } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 8074:                                         $balancetext =  $ruletitles{$rule};
 8075:                                     } else {
 8076:                                         $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{'rules'}{$type});
 8077:                                     }
 8078:                                     $resulttext .= '<li>'.&mt('Load Balancing for [_1] set to: [_2]',$titles->{$type},$balancetext).'</li>';     
 8079:                                 }
 8080:                             }
 8081:                         }
 8082:                     }
 8083:                     if ($resulttext ne '') {
 8084:                         $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8085:                     } else {
 8086:                         $resulttext = $nochgmsg;
 8087:                     }
 8088:                 } else {
 8089:                     $resulttext = $nochgmsg;
 8090:                     if ($balancer ne '') {
 8091:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8092:                     }
 8093:                 }
 8094:             } else {
 8095:                 $resulttext = '<span class="LC_error">'.
 8096:                               &mt('An error occurred: [_1]',$putresult).'</span>';
 8097:             }
 8098:         } else {
 8099:             $resulttext = $nochgmsg;
 8100:         }
 8101:     } else {
 8102:         $resulttext =  &mt('Load Balancing unavailable as this domain only has one server.');
 8103:     }
 8104:     return $resulttext;
 8105: }
 8106: 
 8107: sub recurse_check {
 8108:     my ($chkcats,$categories,$depth,$name) = @_;
 8109:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8110:         my $chg = 0;
 8111:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8112:             my $category = $chkcats->[$depth]{$name}[$j];
 8113:             my $item;
 8114:             if ($category eq '') {
 8115:                 $chg ++;
 8116:             } else {
 8117:                 my $deeper = $depth + 1;
 8118:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8119:                 if ($chg) {
 8120:                     $categories->{$item} -= $chg;
 8121:                 }
 8122:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8123:                 $deeper --;
 8124:             }
 8125:         }
 8126:     }
 8127:     return;
 8128: }
 8129: 
 8130: sub recurse_cat_deletes {
 8131:     my ($item,$coursecategories,$deletions) = @_;
 8132:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8133:     my $subdepth = $depth + 1;
 8134:     if (ref($coursecategories) eq 'HASH') {
 8135:         foreach my $subitem (keys(%{$coursecategories})) {
 8136:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8137:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8138:                 delete($coursecategories->{$subitem});
 8139:                 $deletions->{$subitem} = 1;
 8140:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8141:             }  
 8142:         }
 8143:     }
 8144:     return;
 8145: }
 8146: 
 8147: sub get_active_dcs {
 8148:     my ($dom) = @_;
 8149:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc']);
 8150:     my %domcoords;
 8151:     my $numdcs = 0;
 8152:     my $now = time;
 8153:     foreach my $server (keys(%dompersonnel)) {
 8154:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8155:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8156:             my ($end,$start) = split(':',$dompersonnel{$server}{$user});
 8157:             if (($end eq '') || ($end == 0) || ($end > $now)) {
 8158:                 if ($start <= $now) {
 8159:                     $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8160:                 }
 8161:             }
 8162:         }
 8163:     }
 8164:     return %domcoords;
 8165: }
 8166: 
 8167: sub active_dc_picker {
 8168:     my ($dom,$curr_dc) = @_;
 8169:     my %domcoords = &get_active_dcs($dom); 
 8170:     my @dcs = sort(keys(%domcoords));
 8171:     my $numdcs = scalar(@dcs); 
 8172:     my $datatable;
 8173:     my $numinrow = 2;
 8174:     if ($numdcs > 1) {
 8175:         $datatable = '<table>';
 8176:         for (my $i=0; $i<@dcs; $i++) {
 8177:             my $rem = $i%($numinrow);
 8178:             if ($rem == 0) {
 8179:                 if ($i > 0) {
 8180:                     $datatable .= '</tr>';
 8181:                 }
 8182:                 $datatable .= '<tr>';
 8183:             }
 8184:             my $check = ' ';
 8185:             if ($curr_dc eq '') {
 8186:                 if (!$i) { 
 8187:                     $check = ' checked="checked" ';
 8188:                 }
 8189:             } elsif ($dcs[$i] eq $curr_dc) {
 8190:                 $check = ' checked="checked" ';
 8191:             }
 8192:             if ($i == @dcs - 1) {
 8193:                 my $colsleft = $numinrow - $rem;
 8194:                 if ($colsleft > 1) {
 8195:                     $datatable .= '<td colspan="'.$colsleft.'">';
 8196:                 } else {
 8197:                     $datatable .= '<td>';
 8198:                 }
 8199:             } else {
 8200:                 $datatable .= '<td>';
 8201:             }
 8202:             my ($dcname,$dcdom) = split(':',$dcs[$i]);
 8203:             $datatable .= '<span class="LC_nobreak"><label>'.
 8204:                           '<input type="radio" name="autocreate_xmldc"'.
 8205:                           ' value="'.$dcs[$i].'"'.$check.'/>'.
 8206:                           &Apache::loncommon::plainname($dcname,$dcdom).
 8207:                           '</label></span></td>';
 8208:         }
 8209:         $datatable .= '</tr></table>';
 8210:     } elsif (@dcs) {
 8211:         $datatable .= '<input type="hidden" name="autocreate_dc" value="'.
 8212:                       $dcs[0].'" />';
 8213:     }
 8214:     return ($numdcs,$datatable);
 8215: }
 8216: 
 8217: sub usersession_titles {
 8218:     return &Apache::lonlocal::texthash(
 8219:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8220:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8221:                spares => 'Servers offloaded to, when busy',
 8222:                version => 'LON-CAPA version requirement',
 8223:                excludedomain => 'Allow all, but exclude specific domains',
 8224:                includedomain => 'Deny all, but include specific domains',
 8225:                primary => 'Primary (checked first)',
 8226:                default => 'Default',
 8227:            );
 8228: }
 8229: 
 8230: sub id_for_thisdom {
 8231:     my (%servers) = @_;
 8232:     my %altids;
 8233:     foreach my $server (keys(%servers)) {
 8234:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8235:         if ($serverhome ne $server) {
 8236:             $altids{$serverhome} = $server;
 8237:         }
 8238:     }
 8239:     return %altids;
 8240: }
 8241: 
 8242: sub count_servers {
 8243:     my ($currbalancer,%servers) = @_;
 8244:     my (@spares,$numspares);
 8245:     foreach my $lonhost (sort(keys(%servers))) {
 8246:         next if ($currbalancer eq $lonhost);
 8247:         push(@spares,$lonhost);
 8248:     }
 8249:     if ($currbalancer) {
 8250:         $numspares = scalar(@spares);
 8251:     } else {
 8252:         $numspares = scalar(@spares) - 1;
 8253:     }
 8254:     return ($numspares,@spares);
 8255: }
 8256: 
 8257: sub lonbalance_targets_js {
 8258:     my ($dom,$types,$servers) = @_;
 8259:     my $select = &mt('Select');
 8260:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8261:     if (ref($servers) eq 'HASH') {
 8262:         $alltargets = join("','",sort(keys(%{$servers})));
 8263:         my @homedoms;
 8264:         foreach my $server (sort(keys(%{$servers}))) {
 8265:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8266:                 push(@homedoms,'1');
 8267:             } else {
 8268:                 push(@homedoms,'0');
 8269:             }
 8270:         }
 8271:         $allishome = join("','",@homedoms);
 8272:     }
 8273:     if (ref($types) eq 'ARRAY') {
 8274:         if (@{$types} > 0) {
 8275:             @alltypes = @{$types};
 8276:         }
 8277:     }
 8278:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8279:     $allinsttypes = join("','",@alltypes);
 8280:     return <<"END";
 8281: 
 8282: <script type="text/javascript">
 8283: // <![CDATA[
 8284: 
 8285: function toggleTargets() {
 8286:     var balancer = document.display.loadbalancing_lonhost.options[document.display.loadbalancing_lonhost.selectedIndex].value;
 8287:     if (balancer == '') {
 8288:         hideSpares();
 8289:     } else {
 8290:         var homedoms = new Array('$allishome');
 8291:         var ishomedom = homedoms[document.display.loadbalancing_lonhost.selectedIndex];
 8292:         showSpares(balancer,ishomedom);
 8293:     }
 8294:     return;
 8295: }
 8296: 
 8297: function showSpares(balancer,ishomedom) {
 8298:     var alltargets = new Array('$alltargets');
 8299:     var insttypes = new Array('$allinsttypes');
 8300:     var offloadtypes = new Array('primary','default');
 8301: 
 8302:     document.getElementById('loadbalancing_targets').style.display='block';
 8303:     document.getElementById('loadbalancing_disabled').style.display='none';
 8304:  
 8305:     for (var i=0; i<offloadtypes.length; i++) {
 8306:         var count = 0;
 8307:         for (var j=0; j<alltargets.length; j++) {
 8308:             if (alltargets[j] != balancer) {
 8309:                 document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+count).value = alltargets[j];
 8310:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).style.textAlign='left';
 8311:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).style.textFace='normal';
 8312:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8313:                 count ++;
 8314:             }
 8315:         }
 8316:     }
 8317:     for (var k=0; k<insttypes.length; k++) {
 8318:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8319:             if (ishomedom == 1) {
 8320:                 document.getElementById('balanceruletitle_'+insttypes[k]).style.display='block';
 8321:                 document.getElementById('balancerule_'+insttypes[k]).style.display='block';
 8322:             } else {
 8323:                 document.getElementById('balanceruletitle_'+insttypes[k]).style.display='none';
 8324:                 document.getElementById('balancerule_'+insttypes[k]).style.display='none';
 8325: 
 8326:             }
 8327:         } else {
 8328:             document.getElementById('balanceruletitle_'+insttypes[k]).style.display='block';
 8329:             document.getElementById('balancerule_'+insttypes[k]).style.display='block';
 8330:         }
 8331:         if ((insttypes[k] != '_LC_external') && 
 8332:             ((insttypes[k] != '_LC_internetdom') ||
 8333:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8334:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[0] = new Option("","",true,true);
 8335:             for (var m=0; m<alltargets.length; m++) {
 8336:                 var idx = m+1;
 8337:                 if (alltargets[m] != balancer) {
 8338:                     document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8339:                 }
 8340:             }
 8341:         }
 8342:     }
 8343:     return;
 8344: }
 8345: 
 8346: function hideSpares() {
 8347:     var alltargets = new Array('$alltargets');
 8348:     var insttypes = new Array('$allinsttypes');
 8349:     var offloadtypes = new Array('primary','default');
 8350: 
 8351:     document.getElementById('loadbalancing_targets').style.display='none';
 8352:     document.getElementById('loadbalancing_disabled').style.display='block';
 8353: 
 8354:     var total = alltargets.length - 1;
 8355:     for (var i=0; i<offloadtypes; i++) {
 8356:         for (var j=0; j<total; j++) {
 8357:            document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+j).checked = false;
 8358:            document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+j).value = '';
 8359:            document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8360:         }
 8361:     }
 8362:     for (var k=0; k<insttypes.length; k++) {
 8363:         document.getElementById('balanceruletitle_'+insttypes[k]).style.display='none';
 8364:         document.getElementById('balancerule_'+insttypes[k]).style.display='none';
 8365:         if (insttypes[k] != '_LC_external') {
 8366:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).length = 0;
 8367:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[0] = new Option("","",true,true);
 8368:         }
 8369:     }
 8370:     return;
 8371: }
 8372: 
 8373: function checkOffloads(item,type) {
 8374:     var alltargets = new Array('$alltargets');
 8375:     var offloadtypes = new Array('primary','default');
 8376:     if (item.checked) {
 8377:         var total = alltargets.length - 1;
 8378:         var other;
 8379:         if (type == offloadtypes[0]) {
 8380:             other = offloadtypes[1];
 8381:         } else {
 8382:             other = offloadtypes[0];
 8383:         }
 8384:         for (var i=0; i<total; i++) {
 8385:             var server = document.getElementById('loadbalancing_target_'+other+'_'+i).value;
 8386:             if (server == item.value) {
 8387:                 if (document.getElementById('loadbalancing_target_'+other+'_'+i).checked) {
 8388:                     document.getElementById('loadbalancing_target_'+other+'_'+i).checked = false;
 8389:                 }
 8390:             }
 8391:         }
 8392:     }
 8393:     return;
 8394: }
 8395: 
 8396: function singleServerToggle(type) {
 8397:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+type).selectedIndex;
 8398:     if (offloadtoSelIdx == 0) {
 8399:         document.getElementById('loadbalancing_rules_'+type+'_0').checked = true;
 8400:         document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '';
 8401: 
 8402:     } else {
 8403:         document.getElementById('loadbalancing_rules_'+type+'_2').checked = true;
 8404:         document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '$select';
 8405:     }
 8406:     return;
 8407: }
 8408: 
 8409: function balanceruleChange(formname,type) {
 8410:     if (type == '_LC_external') {
 8411:         return; 
 8412:     }
 8413:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+type);
 8414:     for (var i=0; i<typesRules.length; i++) {
 8415:         if (formname.elements[typesRules[i]].checked) {
 8416:             if (formname.elements[typesRules[i]].value != 'specific') {
 8417:                 document.getElementById('loadbalancing_singleserver_'+type).selectedIndex = 0;
 8418:                 document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '';
 8419:             } else {
 8420:                 document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '$select';
 8421:             }
 8422:         }
 8423:     }
 8424:     return;
 8425: }
 8426: 
 8427: // ]]>
 8428: </script>
 8429: 
 8430: END
 8431: }
 8432: 
 8433: sub new_spares_js {
 8434:     my @sparestypes = ('primary','default');
 8435:     my $types = join("','",@sparestypes);
 8436:     my $select = &mt('Select');
 8437:     return <<"END";
 8438: 
 8439: <script type="text/javascript">
 8440: // <![CDATA[
 8441: 
 8442: function updateNewSpares(formname,lonhost) {
 8443:     var types = new Array('$types');
 8444:     var include = new Array();
 8445:     var exclude = new Array();
 8446:     for (var i=0; i<types.length; i++) {
 8447:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8448:         for (var j=0; j<spareboxes.length; j++) {
 8449:             if (formname.elements[spareboxes[j]].checked) {
 8450:                 exclude.push(formname.elements[spareboxes[j]].value);
 8451:             } else {
 8452:                 include.push(formname.elements[spareboxes[j]].value);
 8453:             }
 8454:         }
 8455:     }
 8456:     for (var i=0; i<types.length; i++) {
 8457:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8458:         var selIdx = newSpare.selectedIndex;
 8459:         var currnew = newSpare.options[selIdx].value;
 8460:         var okSpares = new Array();
 8461:         for (var j=0; j<newSpare.options.length; j++) {
 8462:             var possible = newSpare.options[j].value;
 8463:             if (possible != '') {
 8464:                 if (exclude.indexOf(possible) == -1) {
 8465:                     okSpares.push(possible);
 8466:                 } else {
 8467:                     if (currnew == possible) {
 8468:                         selIdx = 0;
 8469:                     }
 8470:                 }
 8471:             }
 8472:         }
 8473:         for (var k=0; k<include.length; k++) {
 8474:             if (okSpares.indexOf(include[k]) == -1) {
 8475:                 okSpares.push(include[k]);
 8476:             }
 8477:         }
 8478:         okSpares.sort();
 8479:         newSpare.options.length = 0;
 8480:         if (selIdx == 0) {
 8481:             newSpare.options[0] = new Option("$select","",true,true);
 8482:         } else {
 8483:             newSpare.options[0] = new Option("$select","",false,false);
 8484:         }
 8485:         for (var m=0; m<okSpares.length; m++) {
 8486:             var idx = m+1;
 8487:             var selThis = 0;
 8488:             if (selIdx != 0) {
 8489:                 if (okSpares[m] == currnew) {
 8490:                     selThis = 1;
 8491:                 }
 8492:             }
 8493:             if (selThis == 1) {
 8494:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8495:             } else {
 8496:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8497:             }
 8498:         }
 8499:     }
 8500:     return;
 8501: }
 8502: 
 8503: function checkNewSpares(lonhost,type) {
 8504:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8505:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8506:     if (chosen != '') { 
 8507:         var othertype;
 8508:         var othernewSpare;
 8509:         if (type == 'primary') {
 8510:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 8511:         }
 8512:         if (type == 'default') {
 8513:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 8514:         }
 8515:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 8516:             othernewSpare.selectedIndex = 0;
 8517:         }
 8518:     }
 8519:     return;
 8520: }
 8521: 
 8522: // ]]>
 8523: </script>
 8524: 
 8525: END
 8526: 
 8527: }
 8528: 
 8529: sub common_domprefs_js {
 8530:     return <<"END";
 8531: 
 8532: <script type="text/javascript">
 8533: // <![CDATA[
 8534: 
 8535: function getIndicesByName(formname,item) {
 8536:     var group = new Array();
 8537:     for (var i=0;i<formname.elements.length;i++) {
 8538:         if (formname.elements[i].name == item) {
 8539:             group.push(formname.elements[i].id);
 8540:         }
 8541:     }
 8542:     return group;
 8543: }
 8544: 
 8545: // ]]>
 8546: </script>
 8547: 
 8548: END
 8549: 
 8550: }
 8551: 
 8552: sub recaptcha_js {
 8553:     my %lt = &captcha_phrases();
 8554:     return <<"END";
 8555: 
 8556: <script type="text/javascript">
 8557: // <![CDATA[
 8558: 
 8559: function updateCaptcha(caller,context) {
 8560:     var privitem;
 8561:     var pubitem;
 8562:     var privtext;
 8563:     var pubtext;
 8564:     if (document.getElementById(context+'_recaptchapub')) {
 8565:         pubitem = document.getElementById(context+'_recaptchapub');
 8566:     } else {
 8567:         return;
 8568:     }
 8569:     if (document.getElementById(context+'_recaptchapriv')) {
 8570:         privitem = document.getElementById(context+'_recaptchapriv');
 8571:     } else {
 8572:         return;
 8573:     }
 8574:     if (document.getElementById(context+'_recaptchapubtxt')) {
 8575:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 8576:     } else {
 8577:         return;
 8578:     }
 8579:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 8580:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 8581:     } else {
 8582:         return;
 8583:     }
 8584:     if (caller.checked) {
 8585:         if (caller.value == 'recaptcha') {
 8586:             pubitem.type = 'text';
 8587:             privitem.type = 'text';
 8588:             pubitem.size = '40';
 8589:             privitem.size = '40';
 8590:             pubtext.innerHTML = "$lt{'pub'}";
 8591:             privtext.innerHTML = "$lt{'priv'}";
 8592:         } else {
 8593:             pubitem.type = 'hidden';
 8594:             privitem.type = 'hidden';
 8595:             pubtext.innerHTML = '';
 8596:             privtext.innerHTML = '';
 8597:         }
 8598:     }
 8599:     return;
 8600: }
 8601: 
 8602: // ]]>
 8603: </script>
 8604: 
 8605: END
 8606: 
 8607: }
 8608: 
 8609: sub captcha_phrases {
 8610:     return &Apache::lonlocal::texthash (
 8611:                  priv => 'Private key',
 8612:                  pub  => 'Public key',
 8613:                  original  => 'original (CAPTCHA)',
 8614:                  recaptcha => 'successor (ReCAPTCHA)',
 8615:                  notused   => 'unused',
 8616:     );
 8617: }
 8618: 
 8619: 1;

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