File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.166: download - view: text, annotated - select for diffs
Sat Aug 25 21:48:12 2012 UTC (11 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Domain configuration for help pages.
   - Code re-use (radiobutton_prefs) in &print_helpsettings().
   - Overhaul coding style in &modify_helpsettings().

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.166 2012/08/25 21:48:12 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 => 'Help Settings (logged-in users)',
  357:                               col2 => 'Value'},
  358:                              {col1 => 'Help Settings (before log-in)',
  359:                               col2 => 'Value'}],
  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')) {
  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:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 2267:     my ($css_class,$datatable,$itemcount);
 2268:     my $switchserver = &check_switchserver($dom,$confname);
 2269:     $itemcount = 1;
 2270:     if ($position eq 'top') {
 2271:         my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles,$align);
 2272:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2273:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2274:                                      &mt('LON-CAPA bug tracker'),600,500));
 2275:         %defaultchecked = ('submitbugs' => 'on');
 2276:         @toggles = ('submitbugs',);
 2277:         $align = 'LC_left_item';
 2278: 
 2279:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2280:                                                      \%choices,$itemcount);
 2281:     } else {
 2282:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2283:         $datatable .= '<tr'.$css_class.'>';
 2284:         my $url = '/adm/loginproblems.html'; 
 2285:         my $type = 'default';
 2286:         my %lt = &Apache::lonlocal::texthash (
 2287:                      del     => 'Delete?',
 2288:                      rep     => 'Replace:',
 2289:                      upl     => 'Upload:',
 2290:                      default => 'Default',
 2291:                      custom  => 'Custom', 
 2292:                                              ); 
 2293:         if (ref($settings) eq 'HASH') {
 2294:             if (ref($settings->{'loginhelpurl'}) eq 'HASH') {
 2295: 
 2296:             } elsif ($settings->{'loginhelpurl'} ne '') {
 2297:                 $type = 'custom';
 2298:                 $url = $settings->{'loginhelpurl'}.'?inhibitmenu=yes';
 2299:             }
 2300:         }
 2301:         $datatable .= '<td class="LC_left_item"><span class="LC_nobreak">'.
 2302:                       &mt('Log-in help page currently in use: [_1]',
 2303:                           &Apache::loncommon::modal_link($url,$lt{$type},600,500)).
 2304:                       '</span></td>'."\n".
 2305:                       '<td class="LC_left_item">';
 2306:         if ($type eq 'custom') {
 2307:             $datatable .= '<span class="LC_nobreak"><label>'.
 2308:                           '<input type="checkbox" name="loginhelpurl_del" value="1" />'.$lt{'del'}. 
 2309:                           '</label>&nbsp;'.$lt{'rep'}.'</span>';
 2310:         } else {
 2311:             $datatable .= $lt{'upl'};
 2312:         }
 2313:         $datatable .='<br />';
 2314:         if ($switchserver) {
 2315:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2316:         } else {
 2317:             $datatable .= '<input type="file" name="loginhelpurl" />';
 2318:         } 
 2319:         $datatable .= '</td></tr>';
 2320:     }
 2321:     return $datatable;
 2322: }
 2323: 
 2324: sub radiobutton_prefs {
 2325:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount) = @_;
 2326:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2327:                    (ref($choices) eq 'HASH'));
 2328: 
 2329:     my (%checkedon,%checkedoff,$datatable,$css_class,$cell_colspan);
 2330: 
 2331:     foreach my $item (@{$toggles}) {
 2332:         if ($defaultchecked->{$item} eq 'on') {
 2333:             $checkedon{$item} = ' checked="checked" ';
 2334:             $checkedoff{$item} = ' ';
 2335:         } elsif ($defaultchecked->{$item} eq 'off') {
 2336:             $checkedoff{$item} = ' checked="checked" ';
 2337:             $checkedon{$item} = ' ';
 2338:         }
 2339:     }
 2340:     if (ref($settings) eq 'HASH') {
 2341:         foreach my $item (@{$toggles}) {
 2342:             if ($settings->{$item} eq '1') {
 2343:                 $checkedon{$item} =  ' checked="checked" ';
 2344:                 $checkedoff{$item} = ' ';
 2345:             } elsif ($settings->{$item} eq '0') {
 2346:                 $checkedoff{$item} =  ' checked="checked" ';
 2347:                 $checkedon{$item} = ' ';
 2348:             }
 2349:         }
 2350:     }
 2351:     foreach my $item (@{$toggles}) {
 2352:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2353:         $datatable .=
 2354:             '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices->{$item}.
 2355:             '</span></td>'.
 2356:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2357:             '<label><input type="radio" name="'.
 2358:             $item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').
 2359:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2360:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2361:             '</span></td>'.
 2362:             '</tr>';
 2363:         $itemcount ++;
 2364:     }
 2365:     return ($datatable,$itemcount);
 2366: }
 2367: 
 2368: sub print_coursedefaults {
 2369:     my ($position,$dom,$settings,$rowtotal) = @_;
 2370:     my ($css_class,$datatable);
 2371:     my $itemcount = 1;
 2372:     if ($position eq 'top') {
 2373:         my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2374:         %choices =
 2375:             &Apache::lonlocal::texthash (
 2376:                 canuse_pdfforms => 'Course/Community users can create/upload PDF forms',
 2377:         );
 2378:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2379:         @toggles = ('canuse_pdfforms',);
 2380:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2381:                                                  \%choices,$itemcount);
 2382:         $$rowtotal += $itemcount;
 2383:     } else {
 2384:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2385:         my %choices =
 2386:             &Apache::lonlocal::texthash (
 2387:                 anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2388:         );
 2389:         my $currdefresponder;
 2390:         if (ref($settings) eq 'HASH') {
 2391:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2392:         }
 2393:         if (!$currdefresponder) {
 2394:             $currdefresponder = 10;
 2395:         } elsif ($currdefresponder < 1) {
 2396:             $currdefresponder = 1;
 2397:         }
 2398:         $datatable .=
 2399:                '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices{'anonsurvey_threshold'}.
 2400:                 '</span></td>'.
 2401:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2402:                 '<input type="text" name="anonsurvey_threshold"'.
 2403:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2404:                 '</td></tr>';
 2405:     }
 2406:     return $datatable;
 2407: }
 2408: 
 2409: sub print_usersessions {
 2410:     my ($position,$dom,$settings,$rowtotal) = @_;
 2411:     my ($css_class,$datatable,%checked,%choices);
 2412:     my (%by_ip,%by_location,@intdoms);
 2413:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2414: 
 2415:     my @alldoms = &Apache::lonnet::all_domains();
 2416:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2417:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2418:     my %altids = &id_for_thisdom(%servers);
 2419:     my $itemcount = 1;
 2420:     if ($position eq 'top') {
 2421:         if (keys(%serverhomes) > 1) {
 2422:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2423:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2424:         } else {
 2425:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2426:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2427:         }
 2428:     } else {
 2429:         if (keys(%by_location) == 0) {
 2430:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2431:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2432:         } else {
 2433:             my %lt = &usersession_titles();
 2434:             my $numinrow = 5;
 2435:             my $prefix;
 2436:             my @types;
 2437:             if ($position eq 'bottom') {
 2438:                 $prefix = 'remote';
 2439:                 @types = ('version','excludedomain','includedomain');
 2440:             } else {
 2441:                 $prefix = 'hosted';
 2442:                 @types = ('excludedomain','includedomain');
 2443:             }
 2444:             my (%current,%checkedon,%checkedoff);
 2445:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2446:             my @locations = sort(keys(%by_location));
 2447:             foreach my $type (@types) {
 2448:                 $checkedon{$type} = '';
 2449:                 $checkedoff{$type} = ' checked="checked"';
 2450:             }
 2451:             if (ref($settings) eq 'HASH') {
 2452:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2453:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2454:                         $current{$key} = $settings->{$prefix}{$key};
 2455:                         if ($key eq 'version') {
 2456:                             if ($current{$key} ne '') {
 2457:                                 $checkedon{$key} = ' checked="checked"';
 2458:                                 $checkedoff{$key} = '';
 2459:                             }
 2460:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2461:                             $checkedon{$key} = ' checked="checked"';
 2462:                             $checkedoff{$key} = '';
 2463:                         }
 2464:                     }
 2465:                 }
 2466:             }
 2467:             foreach my $type (@types) {
 2468:                 next if ($type ne 'version' && !@locations);
 2469:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2470:                 $datatable .= '<tr'.$css_class.'>
 2471:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2472:                                <span class="LC_nobreak">&nbsp;
 2473:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2474:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2475:                 if ($type eq 'version') {
 2476:                     my $selector = '<select name="'.$prefix.'_version">';
 2477:                     foreach my $version (@lcversions) {
 2478:                         my $selected = '';
 2479:                         if ($current{'version'} eq $version) {
 2480:                             $selected = ' selected="selected"';
 2481:                         }
 2482:                         $selector .= ' <option value="'.$version.'"'.
 2483:                                      $selected.'>'.$version.'</option>';
 2484:                     }
 2485:                     $selector .= '</select> ';
 2486:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2487:                 } else {
 2488:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2489:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2490:                                  ' />'.('&nbsp;'x2).
 2491:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2492:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2493:                                  "\n".
 2494:                                  '</div><div><table>';
 2495:                     my $rem;
 2496:                     for (my $i=0; $i<@locations; $i++) {
 2497:                         my ($showloc,$value,$checkedtype);
 2498:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2499:                             my $ip = $by_location{$locations[$i]}->[0];
 2500:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2501:                                  $value = join(':',@{$by_ip{$ip}});
 2502:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2503:                                 if (ref($current{$type}) eq 'ARRAY') {
 2504:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2505:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2506:                                             $checkedtype = ' checked="checked"';
 2507:                                             last;
 2508:                                         }
 2509:                                     }
 2510:                                 }
 2511:                             }
 2512:                         }
 2513:                         $rem = $i%($numinrow);
 2514:                         if ($rem == 0) {
 2515:                             if ($i > 0) {
 2516:                                 $datatable .= '</tr>';
 2517:                             }
 2518:                             $datatable .= '<tr>';
 2519:                         }
 2520:                         $datatable .= '<td class="LC_left_item">'.
 2521:                                       '<span class="LC_nobreak"><label>'.
 2522:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2523:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2524:                                       '</label></span></td>';
 2525:                     }
 2526:                     $rem = @locations%($numinrow);
 2527:                     my $colsleft = $numinrow - $rem;
 2528:                     if ($colsleft > 1 ) {
 2529:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2530:                                       '&nbsp;</td>';
 2531:                     } elsif ($colsleft == 1) {
 2532:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2533:                     }
 2534:                     $datatable .= '</tr></table>';
 2535:                 }
 2536:                 $datatable .= '</td></tr>';
 2537:                 $itemcount ++;
 2538:             }
 2539:         }
 2540:     }
 2541:     $$rowtotal += $itemcount;
 2542:     return $datatable;
 2543: }
 2544: 
 2545: sub build_location_hashes {
 2546:     my ($intdoms,$by_ip,$by_location) = @_;
 2547:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2548:                   (ref($by_location) eq 'HASH')); 
 2549:     my %iphost = &Apache::lonnet::get_iphost();
 2550:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2551:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2552:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2553:         foreach my $id (@{$iphost{$primary_ip}}) {
 2554:             my $intdom = &Apache::lonnet::internet_dom($id);
 2555:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2556:                 push(@{$intdoms},$intdom);
 2557:             }
 2558:         }
 2559:     }
 2560:     foreach my $ip (keys(%iphost)) {
 2561:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2562:             foreach my $id (@{$iphost{$ip}}) {
 2563:                 my $location = &Apache::lonnet::internet_dom($id);
 2564:                 if ($location) {
 2565:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2566:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2567:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2568:                             push(@{$by_ip->{$ip}},$location);
 2569:                         }
 2570:                     } else {
 2571:                         $by_ip->{$ip} = [$location];
 2572:                     }
 2573:                 }
 2574:             }
 2575:         }
 2576:     }
 2577:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2578:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2579:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2580:             my $first = $by_ip->{$ip}->[0];
 2581:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2582:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2583:                     push(@{$by_location->{$first}},$ip);
 2584:                 }
 2585:             } else {
 2586:                 $by_location->{$first} = [$ip];
 2587:             }
 2588:         }
 2589:     }
 2590:     return;
 2591: }
 2592: 
 2593: sub current_offloads_to {
 2594:     my ($dom,$settings,$servers) = @_;
 2595:     my (%spareid,%otherdomconfigs);
 2596:     if (ref($servers) eq 'HASH') {
 2597:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2598:             my $gotspares;
 2599:             if (ref($settings) eq 'HASH') {
 2600:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2601:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2602:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2603:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2604:                         $gotspares = 1;
 2605:                     }
 2606:                 }
 2607:             }
 2608:             unless ($gotspares) {
 2609:                 my $gotspares;
 2610:                 my $serverhomeID =
 2611:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2612:                 my $serverhomedom =
 2613:                     &Apache::lonnet::host_domain($serverhomeID);
 2614:                 if ($serverhomedom ne $dom) {
 2615:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2616:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2617:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2618:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2619:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2620:                                 $gotspares = 1;
 2621:                             }
 2622:                         }
 2623:                     } else {
 2624:                         $otherdomconfigs{$serverhomedom} =
 2625:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2626:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2627:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2628:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2629:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2630:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2631:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2632:                                         $gotspares = 1;
 2633:                                     }
 2634:                                 }
 2635:                             }
 2636:                         }
 2637:                     }
 2638:                 }
 2639:             }
 2640:             unless ($gotspares) {
 2641:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2642:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2643:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2644:                } else {
 2645:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2646:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2647:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2648:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2649:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2650:                     } else {
 2651:                         my %what = (
 2652:                              spareid => 1,
 2653:                         );
 2654:                         my ($result,$returnhash) = 
 2655:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2656:                         if ($result eq 'ok') { 
 2657:                             if (ref($returnhash) eq 'HASH') {
 2658:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2659:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2660:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2661:                                 }
 2662:                             }
 2663:                         }
 2664:                     }
 2665:                 }
 2666:             }
 2667:         }
 2668:     }
 2669:     return %spareid;
 2670: }
 2671: 
 2672: sub spares_row {
 2673:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2674:     my $css_class;
 2675:     my $numinrow = 4;
 2676:     my $itemcount = 1;
 2677:     my $datatable;
 2678:     my %typetitles = &sparestype_titles();
 2679:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2680:         foreach my $server (sort(keys(%{$servers}))) {
 2681:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2682:             my ($othercontrol,$serverdom);
 2683:             if ($serverhome ne $server) {
 2684:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2685:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2686:             } else {
 2687:                 $serverdom = &Apache::lonnet::host_domain($server);
 2688:                 if ($serverdom ne $dom) {
 2689:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2690:                 }
 2691:             }
 2692:             next unless (ref($spareid->{$server}) eq 'HASH');
 2693:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2694:             $datatable .= '<tr'.$css_class.'>
 2695:                            <td rowspan="2">
 2696:                             <span class="LC_nobreak"><b>'.$server.'</b> when busy, offloads to:</span></td>'."\n";
 2697:             my (%current,%canselect);
 2698:             my @choices = 
 2699:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2700:             foreach my $type ('primary','default') {
 2701:                 if (ref($spareid->{$server}) eq 'HASH') {
 2702:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2703:                         my @spares = @{$spareid->{$server}{$type}};
 2704:                         if (@spares > 0) {
 2705:                             if ($othercontrol) {
 2706:                                 $current{$type} = join(', ',@spares);
 2707:                             } else {
 2708:                                 $current{$type} .= '<table>';
 2709:                                 my $numspares = scalar(@spares);
 2710:                                 for (my $i=0;  $i<@spares; $i++) {
 2711:                                     my $rem = $i%($numinrow);
 2712:                                     if ($rem == 0) {
 2713:                                         if ($i > 0) {
 2714:                                             $current{$type} .= '</tr>';
 2715:                                         }
 2716:                                         $current{$type} .= '<tr>';
 2717:                                     }
 2718:                                     $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;'.
 2719:                                                        $spareid->{$server}{$type}[$i].
 2720:                                                        '</label></td>'."\n";
 2721:                                 }
 2722:                                 my $rem = @spares%($numinrow);
 2723:                                 my $colsleft = $numinrow - $rem;
 2724:                                 if ($colsleft > 1 ) {
 2725:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2726:                                                        '" class="LC_left_item">'.
 2727:                                                        '&nbsp;</td>';
 2728:                                 } elsif ($colsleft == 1) {
 2729:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2730:                                 }
 2731:                                 $current{$type} .= '</tr></table>';
 2732:                             }
 2733:                         }
 2734:                     }
 2735:                     if ($current{$type} eq '') {
 2736:                         $current{$type} = &mt('None specified');
 2737:                     }
 2738:                     if ($othercontrol) {
 2739:                         if ($type eq 'primary') {
 2740:                             $canselect{$type} = $othercontrol;
 2741:                         }
 2742:                     } else {
 2743:                         $canselect{$type} = 
 2744:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2745:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2746:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2747:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2748:                         if (@choices > 0) {
 2749:                             foreach my $lonhost (@choices) {
 2750:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2751:                             }
 2752:                         }
 2753:                         $canselect{$type} .= '</select>'."\n";
 2754:                     }
 2755:                 } else {
 2756:                     $current{$type} = &mt('Could not be determined');
 2757:                     if ($type eq 'primary') {
 2758:                         $canselect{$type} =  $othercontrol;
 2759:                     }
 2760:                 }
 2761:                 if ($type eq 'default') {
 2762:                     $datatable .= '<tr'.$css_class.'>';
 2763:                 }
 2764:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2765:                               '<td>'.$current{$type}.'</td>'."\n".
 2766:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2767:             }
 2768:             $itemcount ++;
 2769:         }
 2770:     }
 2771:     $$rowtotal += $itemcount;
 2772:     return $datatable;
 2773: }
 2774: 
 2775: sub possible_newspares {
 2776:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2777:     my $serverhostname = &Apache::lonnet::hostname($server);
 2778:     my %excluded;
 2779:     if ($serverhostname ne '') {
 2780:         %excluded = (
 2781:                        $serverhostname => 1,
 2782:                     );
 2783:     }
 2784:     if (ref($currspares) eq 'HASH') {
 2785:         foreach my $type (keys(%{$currspares})) {
 2786:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2787:                 if (@{$currspares->{$type}} > 0) {
 2788:                     foreach my $curr (@{$currspares->{$type}}) {
 2789:                         my $hostname = &Apache::lonnet::hostname($curr);
 2790:                         $excluded{$hostname} = 1;
 2791:                     }
 2792:                 }
 2793:             }
 2794:         }
 2795:     }
 2796:     my @choices;
 2797:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2798:         if (keys(%{$serverhomes}) > 1) {
 2799:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2800:                 unless ($excluded{$name}) {
 2801:                     if (exists($altids->{$serverhomes->{$name}})) {
 2802:                         push(@choices,$altids->{$serverhomes->{$name}});
 2803:                     } else {
 2804:                         push(@choices,$serverhomes->{$name});
 2805:                     }
 2806:                 }
 2807:             }
 2808:         }
 2809:     }
 2810:     return sort(@choices);
 2811: }
 2812: 
 2813: sub print_loadbalancing {
 2814:     my ($dom,$settings,$rowtotal) = @_;
 2815:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2816:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2817:     my $numinrow = 1;
 2818:     my $datatable;
 2819:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2820:     my ($currbalancer,$currtargets,$currrules);
 2821:     if (keys(%servers) > 1) {
 2822:         if (ref($settings) eq 'HASH') {
 2823:             $currbalancer = $settings->{'lonhost'};
 2824:             $currtargets = $settings->{'targets'};
 2825:             $currrules = $settings->{'rules'};
 2826:         } else {
 2827:             ($currbalancer,$currtargets) = 
 2828:                 &Apache::lonnet::get_lonbalancer_config(\%servers);
 2829:         }
 2830:     } else {
 2831:         return;
 2832:     }
 2833:     my ($othertitle,$usertypes,$types) =
 2834:         &Apache::loncommon::sorted_inst_types($dom);
 2835:     my $rownum = 6;
 2836:     if (ref($types) eq 'ARRAY') {
 2837:         $rownum += scalar(@{$types});
 2838:     }
 2839:     my $css_class = ' class="LC_odd_row"';
 2840:     my $targets_div_style = 'display: none';
 2841:     my $disabled_div_style = 'display: block';
 2842:     my $homedom_div_style = 'display: none';
 2843:     $datatable = '<tr'.$css_class.'>'.
 2844:                  '<td rowspan="'.$rownum.'" valign="top">'.
 2845:                  '<p><select name="loadbalancing_lonhost" onchange="toggleTargets();">'."\n".
 2846:                  '<option value=""';
 2847:     if (($currbalancer eq '') || (!grep(/^\Q$currbalancer\E$/,keys(%servers)))) {
 2848:         $datatable .= ' selected="selected"';
 2849:     } else {
 2850:         $targets_div_style = 'display: block';
 2851:         $disabled_div_style = 'display: none';
 2852:         if ($dom eq &Apache::lonnet::host_domain($currbalancer)) {
 2853:             $homedom_div_style = 'display: block'; 
 2854:         }
 2855:     }
 2856:     $datatable .= '>'.&mt('None').'</option>'."\n";
 2857:     foreach my $lonhost (sort(keys(%servers))) {
 2858:         my $selected;
 2859:         if ($lonhost eq $currbalancer) {
 2860:             $selected .= ' selected="selected"';
 2861:         }
 2862:         $datatable .= '<option value="'.$lonhost.'"'.$selected.'>'.$lonhost.'</option>'."\n";
 2863:     }
 2864:     $datatable .= '</select></p></td><td rowspan="'.$rownum.'" valign="top">'.
 2865:                   '<div id="loadbalancing_disabled" style="'.$disabled_div_style.'">'.&mt('No dedicated Load Balancer').'</div>'."\n".
 2866:                   '<div id="loadbalancing_targets" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2867:     my ($numspares,@spares) = &count_servers($currbalancer,%servers);
 2868:     my @sparestypes = ('primary','default');
 2869:     my %typetitles = &sparestype_titles();
 2870:     foreach my $sparetype (@sparestypes) {
 2871:         my $targettable;
 2872:         for (my $i=0; $i<$numspares; $i++) {
 2873:             my $checked;
 2874:             if (ref($currtargets) eq 'HASH') {
 2875:                 if (ref($currtargets->{$sparetype}) eq 'ARRAY') {
 2876:                     if (grep(/^\Q$spares[$i]\E$/,@{$currtargets->{$sparetype}})) {
 2877:                         $checked = ' checked="checked"';
 2878:                     }
 2879:                 }
 2880:             }
 2881:             my $chkboxval;
 2882:             if (($currbalancer ne '') && (grep((/^\Q$currbalancer\E$/,keys(%servers))))) {
 2883:                 $chkboxval = $spares[$i];
 2884:             }
 2885:             $targettable .= '<td><label><input type="checkbox" name="loadbalancing_target_'.$sparetype.'"'.
 2886:                       $checked.' value="'.$chkboxval.'" id="loadbalancing_target_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$sparetype'".');" /><span id="loadbalancing_targettxt_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 2887:                       '</span></label></td>';
 2888:             my $rem = $i%($numinrow);
 2889:             if ($rem == 0) {
 2890:                 if ($i > 0) {
 2891:                     $targettable .= '</tr>';
 2892:                 }
 2893:                 $targettable .= '<tr>';
 2894:             }
 2895:         }
 2896:         if ($targettable ne '') {
 2897:             my $rem = $numspares%($numinrow);
 2898:             my $colsleft = $numinrow - $rem;
 2899:             if ($colsleft > 1 ) {
 2900:                 $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2901:                                 '&nbsp;</td>';
 2902:             } elsif ($colsleft == 1) {
 2903:                 $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 2904:             }
 2905:             $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 2906:                            '<table><tr>'.$targettable.'</table><br />';
 2907:         }
 2908:     }
 2909:     $datatable .= '</div></td></tr>'.
 2910:                   &loadbalancing_rules($dom,$intdom,$currrules,$othertitle,
 2911:                                        $usertypes,$types,\%servers,$currbalancer,
 2912:                                        $targets_div_style,$homedom_div_style,$css_class);
 2913:     $$rowtotal += $rownum;
 2914:     return $datatable;
 2915: }
 2916: 
 2917: sub loadbalancing_rules {
 2918:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 2919:         $currbalancer,$targets_div_style,$homedom_div_style,$css_class) = @_;
 2920:     my $output;
 2921:     my ($alltypes,$othertypes,$titles) = 
 2922:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 2923:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 2924:         foreach my $type (@{$alltypes}) {
 2925:             my $current;
 2926:             if (ref($currrules) eq 'HASH') {
 2927:                 $current = $currrules->{$type};
 2928:             }
 2929:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 2930:                 if ($dom ne &Apache::lonnet::host_domain($currbalancer)) {
 2931:                     $current = '';
 2932:                 }
 2933:             }
 2934:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 2935:                                              $servers,$currbalancer,$dom,
 2936:                                              $targets_div_style,$homedom_div_style,$css_class);
 2937:         }
 2938:     }
 2939:     return $output;
 2940: }
 2941: 
 2942: sub loadbalancing_titles {
 2943:     my ($dom,$intdom,$usertypes,$types) = @_;
 2944:     my %othertypes = (
 2945:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 2946:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 2947:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 2948:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 2949:                      );
 2950:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 2951:     if (ref($types) eq 'ARRAY') {
 2952:         unshift(@alltypes,@{$types},'default');
 2953:     }
 2954:     my %titles;
 2955:     foreach my $type (@alltypes) {
 2956:         if ($type =~ /^_LC_/) {
 2957:             $titles{$type} = $othertypes{$type};
 2958:         } elsif ($type eq 'default') {
 2959:             $titles{$type} = &mt('All users from [_1]',$dom);
 2960:             if (ref($types) eq 'ARRAY') {
 2961:                 if (@{$types} > 0) {
 2962:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 2963:                 }
 2964:             }
 2965:         } elsif (ref($usertypes) eq 'HASH') {
 2966:             $titles{$type} = $usertypes->{$type};
 2967:         }
 2968:     }
 2969:     return (\@alltypes,\%othertypes,\%titles);
 2970: }
 2971: 
 2972: sub loadbalance_rule_row {
 2973:     my ($type,$title,$current,$servers,$currbalancer,$dom,$targets_div_style,
 2974:         $homedom_div_style,$css_class) = @_;
 2975:     my @rulenames = ('default','homeserver');
 2976:     my %ruletitles = &offloadtype_text();
 2977:     if ($type eq '_LC_external') {
 2978:         push(@rulenames,'externalbalancer');
 2979:     } else {
 2980:         push(@rulenames,'specific');
 2981:     }
 2982:     push(@rulenames,'none');
 2983:     my $style = $targets_div_style;
 2984:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 2985:         $style = $homedom_div_style;
 2986:     }
 2987:     my $output = 
 2988:         '<tr'.$css_class.'><td valign="top"><div id="balanceruletitle_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 2989:         '<td><div id="balancerule_'.$type.'" style="'.$style.'">'."\n";
 2990:     for (my $i=0; $i<@rulenames; $i++) {
 2991:         my $rule = $rulenames[$i];
 2992:         my ($checked,$extra);
 2993:         if ($rulenames[$i] eq 'default') {
 2994:             $rule = '';
 2995:         }
 2996:         if ($rulenames[$i] eq 'specific') {
 2997:             if (ref($servers) eq 'HASH') {
 2998:                 my $default;
 2999:                 if (($current ne '') && (exists($servers->{$current}))) {
 3000:                     $checked = ' checked="checked"';
 3001:                 }
 3002:                 unless ($checked) {
 3003:                     $default = ' selected="selected"';
 3004:                 }
 3005:                 $extra = ':&nbsp;<select name="loadbalancing_singleserver_'.$type.
 3006:                          '" id="loadbalancing_singleserver_'.$type.
 3007:                          '" onchange="singleServerToggle('."'$type'".')">'."\n".
 3008:                          '<option value=""'.$default.'></option>'."\n";
 3009:                 foreach my $lonhost (sort(keys(%{$servers}))) {
 3010:                     next if ($lonhost eq $currbalancer);
 3011:                     my $selected;
 3012:                     if ($lonhost eq $current) {
 3013:                         $selected = ' selected="selected"';
 3014:                     }
 3015:                     $extra .= '<option value="'.$lonhost.'"'.$selected.'>'.$lonhost.'</option>';
 3016:                 }
 3017:                 $extra .= '</select>';
 3018:             }
 3019:         } elsif ($rule eq $current) {
 3020:             $checked = ' checked="checked"';
 3021:         }
 3022:         $output .= '<span class="LC_nobreak"><label>'.
 3023:                    '<input type="radio" name="loadbalancing_rules_'.$type.
 3024:                    '" id="loadbalancing_rules_'.$type.'_'.$i.'" value="'.
 3025:                    $rule.'" onclick="balanceruleChange('."this.form,'$type'".
 3026:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3027:                    '</label>'.$extra.'</span><br />'."\n";
 3028:     }
 3029:     $output .= '</div></td></tr>'."\n";
 3030:     return $output;
 3031: }
 3032: 
 3033: sub offloadtype_text {
 3034:     my %ruletitles = &Apache::lonlocal::texthash (
 3035:            'default'          => 'Offloads to default destinations',
 3036:            'homeserver'       => "Offloads to user's home server",
 3037:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3038:            'specific'         => 'Offloads to specific server',
 3039:            'none'             => 'No offload',
 3040:     );
 3041:     return %ruletitles;
 3042: }
 3043: 
 3044: sub sparestype_titles {
 3045:     my %typestitles = &Apache::lonlocal::texthash (
 3046:                           'primary' => 'primary',
 3047:                           'default' => 'default',
 3048:                       );
 3049:     return %typestitles;
 3050: }
 3051: 
 3052: sub contact_titles {
 3053:     my %titles = &Apache::lonlocal::texthash (
 3054:                    'supportemail' => 'Support E-mail address',
 3055:                    'adminemail'   => 'Default Server Admin E-mail address',
 3056:                    'errormail'    => 'Error reports to be e-mailed to',
 3057:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3058:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3059:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3060:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3061:                  );
 3062:     my %short_titles = &Apache::lonlocal::texthash (
 3063:                            adminemail   => 'Admin E-mail address',
 3064:                            supportemail => 'Support E-mail',
 3065:                        );   
 3066:     return (\%titles,\%short_titles);
 3067: }
 3068: 
 3069: sub tool_titles {
 3070:     my %titles = &Apache::lonlocal::texthash (
 3071:                      aboutme    => 'Personal web page',
 3072:                      blog       => 'Blog',
 3073:                      webdav     => 'WebDAV',
 3074:                      portfolio  => 'Portfolio',
 3075:                      official   => 'Official courses (with institutional codes)',
 3076:                      unofficial => 'Unofficial courses',
 3077:                      community  => 'Communities',
 3078:                  );
 3079:     return %titles;
 3080: }
 3081: 
 3082: sub courserequest_titles {
 3083:     my %titles = &Apache::lonlocal::texthash (
 3084:                                    official   => 'Official',
 3085:                                    unofficial => 'Unofficial',
 3086:                                    community  => 'Communities',
 3087:                                    norequest  => 'Not allowed',
 3088:                                    approval   => 'Approval by Dom. Coord.',
 3089:                                    validate   => 'With validation',
 3090:                                    autolimit  => 'Numerical limit',
 3091:                                    unlimited  => '(blank for unlimited)',
 3092:                  );
 3093:     return %titles;
 3094: }
 3095: 
 3096: sub authorrequest_titles {
 3097:     my %titles = &Apache::lonlocal::texthash (
 3098:                                    norequest  => 'Not allowed',
 3099:                                    approval   => 'Approval by Dom. Coord.',
 3100:                                    automatic  => 'Automatic approval',
 3101:                  );
 3102:     return %titles;
 3103: } 
 3104: 
 3105: sub courserequest_conditions {
 3106:     my %conditions = &Apache::lonlocal::texthash (
 3107:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3108:        validate   => '(Processing of request subject to instittutional validation).',
 3109:                  );
 3110:     return %conditions;
 3111: }
 3112: 
 3113: 
 3114: sub print_usercreation {
 3115:     my ($position,$dom,$settings,$rowtotal) = @_;
 3116:     my $numinrow = 4;
 3117:     my $datatable;
 3118:     if ($position eq 'top') {
 3119:         $$rowtotal ++;
 3120:         my $rowcount = 0;
 3121:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3122:         if (ref($rules) eq 'HASH') {
 3123:             if (keys(%{$rules}) > 0) {
 3124:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3125:                                                 $ruleorder,$numinrow,$rowcount);
 3126:                 $$rowtotal ++;
 3127:                 $rowcount ++;
 3128:             }
 3129:         }
 3130:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3131:         if (ref($idrules) eq 'HASH') {
 3132:             if (keys(%{$idrules}) > 0) {
 3133:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3134:                                                 $idruleorder,$numinrow,$rowcount);
 3135:                 $$rowtotal ++;
 3136:                 $rowcount ++;
 3137:             }
 3138:         }
 3139:         my ($emailrules,$emailruleorder) = 
 3140:             &Apache::lonnet::inst_userrules($dom,'email');
 3141:         if (ref($emailrules) eq 'HASH') {
 3142:             if (keys(%{$emailrules}) > 0) {
 3143:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3144:                                                 $emailruleorder,$numinrow,$rowcount);
 3145:                 $$rowtotal ++;
 3146:                 $rowcount ++;
 3147:             }
 3148:         }
 3149:         if ($rowcount == 0) {
 3150:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3151:             $$rowtotal ++;
 3152:             $rowcount ++;
 3153:         }
 3154:     } elsif ($position eq 'middle') {
 3155:         my @creators = ('author','course','requestcrs','selfcreate');
 3156:         my ($rules,$ruleorder) =
 3157:             &Apache::lonnet::inst_userrules($dom,'username');
 3158:         my %lt = &usercreation_types();
 3159:         my %checked;
 3160:         my @selfcreate; 
 3161:         if (ref($settings) eq 'HASH') {
 3162:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3163:                 foreach my $item (@creators) {
 3164:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3165:                 }
 3166:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3167:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3168:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3169:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3170:                         @selfcreate = ('email','login','sso');
 3171:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3172:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3173:                     }
 3174:                 }
 3175:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3176:                 foreach my $item (@creators) {
 3177:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3178:                         $checked{$item} = 'none';
 3179:                     }
 3180:                 }
 3181:             }
 3182:         }
 3183:         my $rownum = 0;
 3184:         foreach my $item (@creators) {
 3185:             $rownum ++;
 3186:             if ($item ne 'selfcreate') {  
 3187:                 if ($checked{$item} eq '') {
 3188:                     $checked{$item} = 'any';
 3189:                 }
 3190:             }
 3191:             my $css_class;
 3192:             if ($rownum%2) {
 3193:                 $css_class = '';
 3194:             } else {
 3195:                 $css_class = ' class="LC_odd_row" ';
 3196:             }
 3197:             $datatable .= '<tr'.$css_class.'>'.
 3198:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3199:                          '</span></td><td align="right">';
 3200:             my @options;
 3201:             if ($item eq 'selfcreate') {
 3202:                 push(@options,('email','login','sso'));
 3203:             } else {
 3204:                 @options = ('any');
 3205:                 if (ref($rules) eq 'HASH') {
 3206:                     if (keys(%{$rules}) > 0) {
 3207:                         push(@options,('official','unofficial'));
 3208:                     }
 3209:                 }
 3210:                 push(@options,'none');
 3211:             }
 3212:             foreach my $option (@options) {
 3213:                 my $type = 'radio';
 3214:                 my $check = ' ';
 3215:                 if ($item eq 'selfcreate') {
 3216:                     $type = 'checkbox';
 3217:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3218:                         $check = ' checked="checked" ';
 3219:                     }
 3220:                 } else {
 3221:                     if ($checked{$item} eq $option) {
 3222:                         $check = ' checked="checked" ';
 3223:                     }
 3224:                 } 
 3225:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3226:                               '<input type="'.$type.'" name="can_createuser_'.
 3227:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3228:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3229:             }
 3230:             $datatable .= '</td></tr>';
 3231:         }
 3232:         my ($othertitle,$usertypes,$types) =
 3233:             &Apache::loncommon::sorted_inst_types($dom);
 3234:         my $createsettings;
 3235:         if (ref($settings) eq 'HASH') {
 3236:             $createsettings = $settings->{cancreate};
 3237:         }
 3238:         if (ref($usertypes) eq 'HASH') {
 3239:             if (keys(%{$usertypes}) > 0) {
 3240:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3241:                                              $dom,$numinrow,$othertitle,
 3242:                                              'statustocreate');
 3243:                 $$rowtotal ++;
 3244:             }
 3245:         }
 3246:         $datatable .= &captcha_choice('cancreate',$createsettings);
 3247:     } else {
 3248:         my @contexts = ('author','course','domain');
 3249:         my @authtypes = ('int','krb4','krb5','loc');
 3250:         my %checked;
 3251:         if (ref($settings) eq 'HASH') {
 3252:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3253:                 foreach my $item (@contexts) {
 3254:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3255:                         foreach my $auth (@authtypes) {
 3256:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3257:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3258:                             }
 3259:                         }
 3260:                     }
 3261:                 }
 3262:             }
 3263:         } else {
 3264:             foreach my $item (@contexts) {
 3265:                 foreach my $auth (@authtypes) {
 3266:                     $checked{$item}{$auth} = ' checked="checked" ';
 3267:                 }
 3268:             }
 3269:         }
 3270:         my %title = &context_names();
 3271:         my %authname = &authtype_names();
 3272:         my $rownum = 0;
 3273:         my $css_class; 
 3274:         foreach my $item (@contexts) {
 3275:             if ($rownum%2) {
 3276:                 $css_class = '';
 3277:             } else {
 3278:                 $css_class = ' class="LC_odd_row" ';
 3279:             }
 3280:             $datatable .=   '<tr'.$css_class.'>'.
 3281:                             '<td>'.$title{$item}.
 3282:                             '</td><td class="LC_left_item">'.
 3283:                             '<span class="LC_nobreak">';
 3284:             foreach my $auth (@authtypes) {
 3285:                 $datatable .= '<label>'. 
 3286:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3287:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3288:                               $authname{$auth}.'</label>&nbsp;';
 3289:             }
 3290:             $datatable .= '</span></td></tr>';
 3291:             $rownum ++;
 3292:         }
 3293:         $$rowtotal += $rownum;
 3294:     }
 3295:     return $datatable;
 3296: }
 3297: 
 3298: sub captcha_choice {
 3299:     my ($context,$settings) = @_;
 3300:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3301:     my %lt = &captcha_phrases();
 3302:     $keyentry = 'hidden';
 3303:     if ($context eq 'cancreate') {
 3304:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3305:     } elsif ($context eq 'help') {
 3306:         $rowname =  &mt('CAPTCHA validation');
 3307:     }
 3308:     if (ref($settings) eq 'HASH') {
 3309:         if ($settings->{'captcha'}) {
 3310:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3311:         } else {
 3312:             $checked{'original'} = ' checked="checked"';
 3313:         }
 3314:         if ($settings->{'captcha'} eq 'recaptcha') {
 3315:             $pubtext = $lt{'pub'};
 3316:             $privtext = $lt{'priv'};
 3317:             $keyentry = 'text';
 3318:         }
 3319:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3320:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3321:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3322:         }
 3323:     } else {
 3324:         $checked{'original'} = ' checked="checked"';
 3325:     }
 3326:     my $output = '<tr class="LC_odd_row">'.
 3327:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_right_item" colspan="2">'."\n".
 3328:                  '<table><tr><td>'."\n";
 3329:     foreach my $option ('original','recaptcha','notused') {
 3330:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3331:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3332:                    $lt{$option}.'</label></span>';
 3333:         unless ($option eq 'notused') {
 3334:             $output .= ('&nbsp;'x2)."\n";
 3335:         }
 3336:     }
 3337: #
 3338: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3339: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3340: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu) 
 3341: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3342: #  
 3343:     $output .= '</td></tr>'."\n".
 3344:                '<tr><td>'."\n".
 3345:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3346:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3347:                $currpub.'" size="40" /></span><br />'."\n".
 3348:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3349:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3350:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3351:                '</td></tr>';
 3352:     return $output;
 3353: }
 3354: 
 3355: sub user_formats_row {
 3356:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3357:     my $output;
 3358:     my %text = (
 3359:                    'username' => 'new usernames',
 3360:                    'id'       => 'IDs',
 3361:                    'email'    => 'self-created accounts (e-mail)',
 3362:                );
 3363:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3364:     $output = '<tr '.$css_class.'>'.
 3365:               '<td><span class="LC_nobreak">';
 3366:     if ($type eq 'email') {
 3367:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3368:     } else {
 3369:         $output .= &mt("Format rules to check for $text{$type}: ");
 3370:     }
 3371:     $output .= '</span></td>'.
 3372:                '<td class="LC_left_item" colspan="2"><table>';
 3373:     my $rem;
 3374:     if (ref($ruleorder) eq 'ARRAY') {
 3375:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3376:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3377:                 my $rem = $i%($numinrow);
 3378:                 if ($rem == 0) {
 3379:                     if ($i > 0) {
 3380:                         $output .= '</tr>';
 3381:                     }
 3382:                     $output .= '<tr>';
 3383:                 }
 3384:                 my $check = ' ';
 3385:                 if (ref($settings) eq 'HASH') {
 3386:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3387:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3388:                             $check = ' checked="checked" ';
 3389:                         }
 3390:                     }
 3391:                 }
 3392:                 $output .= '<td class="LC_left_item">'.
 3393:                            '<span class="LC_nobreak"><label>'.
 3394:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3395:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3396:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3397:             }
 3398:         }
 3399:         $rem = @{$ruleorder}%($numinrow);
 3400:     }
 3401:     my $colsleft = $numinrow - $rem;
 3402:     if ($colsleft > 1 ) {
 3403:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3404:                    '&nbsp;</td>';
 3405:     } elsif ($colsleft == 1) {
 3406:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3407:     }
 3408:     $output .= '</tr></table></td></tr>';
 3409:     return $output;
 3410: }
 3411: 
 3412: sub usercreation_types {
 3413:     my %lt = &Apache::lonlocal::texthash (
 3414:                     author     => 'When adding a co-author',
 3415:                     course     => 'When adding a user to a course',
 3416:                     requestcrs => 'When requesting a course',
 3417:                     selfcreate => 'User creates own account', 
 3418:                     any        => 'Any',
 3419:                     official   => 'Institutional only ',
 3420:                     unofficial => 'Non-institutional only',
 3421:                     email      => 'E-mail address',
 3422:                     login      => 'Institutional Login',
 3423:                     sso        => 'SSO', 
 3424:                     none       => 'None',
 3425:     );
 3426:     return %lt;
 3427: }
 3428: 
 3429: sub authtype_names {
 3430:     my %lt = &Apache::lonlocal::texthash(
 3431:                       int    => 'Internal',
 3432:                       krb4   => 'Kerberos 4',
 3433:                       krb5   => 'Kerberos 5',
 3434:                       loc    => 'Local',
 3435:                   );
 3436:     return %lt;
 3437: }
 3438: 
 3439: sub context_names {
 3440:     my %context_title = &Apache::lonlocal::texthash(
 3441:        author => 'Creating users when an Author',
 3442:        course => 'Creating users when in a course',
 3443:        domain => 'Creating users when a Domain Coordinator',
 3444:     );
 3445:     return %context_title;
 3446: }
 3447: 
 3448: sub print_usermodification {
 3449:     my ($position,$dom,$settings,$rowtotal) = @_;
 3450:     my $numinrow = 4;
 3451:     my ($context,$datatable,$rowcount);
 3452:     if ($position eq 'top') {
 3453:         $rowcount = 0;
 3454:         $context = 'author'; 
 3455:         foreach my $role ('ca','aa') {
 3456:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3457:                                                    $numinrow,$rowcount);
 3458:             $$rowtotal ++;
 3459:             $rowcount ++;
 3460:         }
 3461:     } elsif ($position eq 'middle') {
 3462:         $context = 'course';
 3463:         $rowcount = 0;
 3464:         foreach my $role ('st','ep','ta','in','cr') {
 3465:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3466:                                                    $numinrow,$rowcount);
 3467:             $$rowtotal ++;
 3468:             $rowcount ++;
 3469:         }
 3470:     } elsif ($position eq 'bottom') {
 3471:         $context = 'selfcreate';
 3472:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3473:         $usertypes->{'default'} = $othertitle;
 3474:         if (ref($types) eq 'ARRAY') {
 3475:             push(@{$types},'default');
 3476:             $usertypes->{'default'} = $othertitle;
 3477:             foreach my $status (@{$types}) {
 3478:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3479:                                                        $numinrow,$rowcount,$usertypes);
 3480:                 $$rowtotal ++;
 3481:                 $rowcount ++;
 3482:             }
 3483:         }
 3484:     }
 3485:     return $datatable;
 3486: }
 3487: 
 3488: sub print_defaults {
 3489:     my ($dom,$rowtotal) = @_;
 3490:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3491:                  'datelocale_def','portal_def');
 3492:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3493:     my $titles = &defaults_titles($dom);
 3494:     my $rownum = 0;
 3495:     my ($datatable,$css_class);
 3496:     foreach my $item (@items) {
 3497:         if ($rownum%2) {
 3498:             $css_class = '';
 3499:         } else {
 3500:             $css_class = ' class="LC_odd_row" ';
 3501:         }
 3502:         $datatable .= '<tr'.$css_class.'>'.
 3503:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3504:                   '</span></td><td class="LC_right_item">';
 3505:         if ($item eq 'auth_def') {
 3506:             my @authtypes = ('internal','krb4','krb5','localauth');
 3507:             my %shortauth = (
 3508:                              internal => 'int',
 3509:                              krb4 => 'krb4',
 3510:                              krb5 => 'krb5',
 3511:                              localauth  => 'loc'
 3512:                            );
 3513:             my %authnames = &authtype_names();
 3514:             foreach my $auth (@authtypes) {
 3515:                 my $checked = ' ';
 3516:                 if ($domdefaults{$item} eq $auth) {
 3517:                     $checked = ' checked="checked" ';
 3518:                 }
 3519:                 $datatable .= '<label><input type="radio" name="'.$item.
 3520:                               '" value="'.$auth.'"'.$checked.'/>'.
 3521:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3522:             }
 3523:         } elsif ($item eq 'timezone_def') {
 3524:             my $includeempty = 1;
 3525:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3526:         } elsif ($item eq 'datelocale_def') {
 3527:             my $includeempty = 1;
 3528:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3529:         } else {
 3530:             my $size;
 3531:             if ($item eq 'portal_def') {
 3532:                 $size = ' size="25"';
 3533:             }
 3534:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3535:                           $domdefaults{$item}.'"'.$size.' />';
 3536:         }
 3537:         $datatable .= '</td></tr>';
 3538:         $rownum ++;
 3539:     }
 3540:     $$rowtotal += $rownum;
 3541:     return $datatable;
 3542: }
 3543: 
 3544: sub defaults_titles {
 3545:     my ($dom) = @_;
 3546:     my %titles = &Apache::lonlocal::texthash (
 3547:                    'auth_def'      => 'Default authentication type',
 3548:                    'auth_arg_def'  => 'Default authentication argument',
 3549:                    'lang_def'      => 'Default language',
 3550:                    'timezone_def'  => 'Default timezone',
 3551:                    'datelocale_def' => 'Default locale for dates',
 3552:                    'portal_def'     => 'Portal/Default URL',
 3553:                  );
 3554:     if ($dom) {
 3555:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3556:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3557:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3558:         $protocol = 'http' if ($protocol ne 'https');
 3559:         if ($uint_dom) {
 3560:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3561:                                          $uint_dom);
 3562:         }
 3563:     }
 3564:     return (\%titles);
 3565: }
 3566: 
 3567: sub print_scantronformat {
 3568:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3569:     my $itemcount = 1;
 3570:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3571:         %confhash);
 3572:     my $switchserver = &check_switchserver($dom,$confname);
 3573:     my %lt = &Apache::lonlocal::texthash (
 3574:                 default => 'Default bubblesheet format file error',
 3575:                 custom  => 'Custom bubblesheet format file error',
 3576:              );
 3577:     my %scantronfiles = (
 3578:         default => 'default.tab',
 3579:         custom => 'custom.tab',
 3580:     );
 3581:     foreach my $key (keys(%scantronfiles)) {
 3582:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3583:                               .$scantronfiles{$key};
 3584:     }
 3585:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3586:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3587:         if (!$switchserver) {
 3588:             my $servadm = $r->dir_config('lonAdmEMail');
 3589:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3590:             if ($configuserok eq 'ok') {
 3591:                 if ($author_ok eq 'ok') {
 3592:                     my %legacyfile = (
 3593:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3594:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3595:                     );
 3596:                     my %md5chk;
 3597:                     foreach my $type (keys(%legacyfile)) {
 3598:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3599:                         chomp($md5chk{$type});
 3600:                     }
 3601:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3602:                         foreach my $type (keys(%legacyfile)) {
 3603:                             ($scantronurls{$type},my $error) = 
 3604:                                 &legacy_scantronformat($r,$dom,$confname,
 3605:                                                  $type,$legacyfile{$type},
 3606:                                                  $scantronurls{$type},
 3607:                                                  $scantronfiles{$type});
 3608:                             if ($error ne '') {
 3609:                                 $error{$type} = $error;
 3610:                             }
 3611:                         }
 3612:                         if (keys(%error) == 0) {
 3613:                             $is_custom = 1;
 3614:                             $confhash{'scantron'}{'scantronformat'} = 
 3615:                                 $scantronurls{'custom'};
 3616:                             my $putresult = 
 3617:                                 &Apache::lonnet::put_dom('configuration',
 3618:                                                          \%confhash,$dom);
 3619:                             if ($putresult ne 'ok') {
 3620:                                 $error{'custom'} = 
 3621:                                     '<span class="LC_error">'.
 3622:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3623:                             }
 3624:                         }
 3625:                     } else {
 3626:                         ($scantronurls{'default'},my $error) =
 3627:                             &legacy_scantronformat($r,$dom,$confname,
 3628:                                           'default',$legacyfile{'default'},
 3629:                                           $scantronurls{'default'},
 3630:                                           $scantronfiles{'default'});
 3631:                         if ($error eq '') {
 3632:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3633:                             my $putresult =
 3634:                                 &Apache::lonnet::put_dom('configuration',
 3635:                                                          \%confhash,$dom);
 3636:                             if ($putresult ne 'ok') {
 3637:                                 $error{'default'} =
 3638:                                     '<span class="LC_error">'.
 3639:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3640:                             }
 3641:                         } else {
 3642:                             $error{'default'} = $error;
 3643:                         }
 3644:                     }
 3645:                 }
 3646:             }
 3647:         } else {
 3648:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3649:         }
 3650:     }
 3651:     if (ref($settings) eq 'HASH') {
 3652:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3653:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3654:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3655:                 $scantronurl = '';
 3656:             } else {
 3657:                 $scantronurl = $settings->{'scantronformat'};
 3658:             }
 3659:             $is_custom = 1;
 3660:         } else {
 3661:             $scantronurl = $scantronurls{'default'};
 3662:         }
 3663:     } else {
 3664:         if ($is_custom) {
 3665:             $scantronurl = $scantronurls{'custom'};
 3666:         } else {
 3667:             $scantronurl = $scantronurls{'default'};
 3668:         }
 3669:     }
 3670:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3671:     $datatable .= '<tr'.$css_class.'>';
 3672:     if (!$is_custom) {
 3673:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3674:                       '<span class="LC_nobreak">';
 3675:         if ($scantronurl) {
 3676:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3677:                           &mt('Default bubblesheet format file').'</a>';
 3678:         } else {
 3679:             $datatable = &mt('File unavailable for display');
 3680:         }
 3681:         $datatable .= '</span></td>';
 3682:         if (keys(%error) == 0) { 
 3683:             $datatable .= '<td valign="bottom">';
 3684:             if (!$switchserver) {
 3685:                 $datatable .= &mt('Upload:').'<br />';
 3686:             }
 3687:         } else {
 3688:             my $errorstr;
 3689:             foreach my $key (sort(keys(%error))) {
 3690:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3691:             }
 3692:             $datatable .= '<td>'.$errorstr;
 3693:         }
 3694:     } else {
 3695:         if (keys(%error) > 0) {
 3696:             my $errorstr;
 3697:             foreach my $key (sort(keys(%error))) {
 3698:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3699:             } 
 3700:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3701:         } elsif ($scantronurl) {
 3702:             $datatable .= '<td><span class="LC_nobreak">'.
 3703:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3704:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3705:                           '<input type="checkbox" name="scantronformat_del"'.
 3706:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3707:                           '<td><span class="LC_nobreak">&nbsp;'.
 3708:                           &mt('Replace:').'</span><br />';
 3709:         }
 3710:     }
 3711:     if (keys(%error) == 0) {
 3712:         if ($switchserver) {
 3713:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3714:         } else {
 3715:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3716:                          '<input type="file" name="scantronformat" /></span>';
 3717:         }
 3718:     }
 3719:     $datatable .= '</td></tr>';
 3720:     $$rowtotal ++;
 3721:     return $datatable;
 3722: }
 3723: 
 3724: sub legacy_scantronformat {
 3725:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3726:     my ($url,$error);
 3727:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3728:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3729:         (my $result,$url) =
 3730:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3731:                          '','',$newfile);
 3732:         if ($result ne 'ok') {
 3733:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3734:         }
 3735:     }
 3736:     return ($url,$error);
 3737: }
 3738: 
 3739: sub print_coursecategories {
 3740:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3741:     my $datatable;
 3742:     if ($position eq 'top') {
 3743:         my $toggle_cats_crs = ' ';
 3744:         my $toggle_cats_dom = ' checked="checked" ';
 3745:         my $can_cat_crs = ' ';
 3746:         my $can_cat_dom = ' checked="checked" ';
 3747:         my $toggle_catscomm_comm = ' ';
 3748:         my $toggle_catscomm_dom = ' checked="checked" ';
 3749:         my $can_catcomm_comm = ' ';
 3750:         my $can_catcomm_dom = ' checked="checked" ';
 3751: 
 3752:         if (ref($settings) eq 'HASH') {
 3753:             if ($settings->{'togglecats'} eq 'crs') {
 3754:                 $toggle_cats_crs = $toggle_cats_dom;
 3755:                 $toggle_cats_dom = ' ';
 3756:             }
 3757:             if ($settings->{'categorize'} eq 'crs') {
 3758:                 $can_cat_crs = $can_cat_dom;
 3759:                 $can_cat_dom = ' ';
 3760:             }
 3761:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3762:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3763:                 $toggle_catscomm_dom = ' ';
 3764:             }
 3765:             if ($settings->{'categorizecomm'} eq 'comm') {
 3766:                 $can_catcomm_comm = $can_catcomm_dom;
 3767:                 $can_catcomm_dom = ' ';
 3768:             }
 3769:         }
 3770:         my %title = &Apache::lonlocal::texthash (
 3771:                      togglecats     => 'Show/Hide a course in catalog',
 3772:                      togglecatscomm => 'Show/Hide a community in catalog',
 3773:                      categorize     => 'Assign a category to a course',
 3774:                      categorizecomm => 'Assign a category to a community',
 3775:                     );
 3776:         my %level = &Apache::lonlocal::texthash (
 3777:                      dom  => 'Set in Domain',
 3778:                      crs  => 'Set in Course',
 3779:                      comm => 'Set in Community',
 3780:                     );
 3781:         $datatable = '<tr class="LC_odd_row">'.
 3782:                   '<td>'.$title{'togglecats'}.'</td>'.
 3783:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3784:                   '<input type="radio" name="togglecats"'.
 3785:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3786:                   '<label><input type="radio" name="togglecats"'.
 3787:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3788:                   '</tr><tr>'.
 3789:                   '<td>'.$title{'categorize'}.'</td>'.
 3790:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3791:                   '<label><input type="radio" name="categorize"'.
 3792:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3793:                   '<label><input type="radio" name="categorize"'.
 3794:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3795:                   '</tr><tr class="LC_odd_row">'.
 3796:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 3797:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3798:                   '<input type="radio" name="togglecatscomm"'.
 3799:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3800:                   '<label><input type="radio" name="togglecatscomm"'.
 3801:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3802:                   '</tr><tr>'.
 3803:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 3804:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3805:                   '<label><input type="radio" name="categorizecomm"'.
 3806:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3807:                   '<label><input type="radio" name="categorizecomm"'.
 3808:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3809:                   '</tr>';
 3810:         $$rowtotal += 4;
 3811:     } else {
 3812:         my $css_class;
 3813:         my $itemcount = 1;
 3814:         my $cathash; 
 3815:         if (ref($settings) eq 'HASH') {
 3816:             $cathash = $settings->{'cats'};
 3817:         }
 3818:         if (ref($cathash) eq 'HASH') {
 3819:             my (@cats,@trails,%allitems,%idx,@jsarray);
 3820:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 3821:                                                    \%allitems,\%idx,\@jsarray);
 3822:             my $maxdepth = scalar(@cats);
 3823:             my $colattrib = '';
 3824:             if ($maxdepth > 2) {
 3825:                 $colattrib = ' colspan="2" ';
 3826:             }
 3827:             my @path;
 3828:             if (@cats > 0) {
 3829:                 if (ref($cats[0]) eq 'ARRAY') {
 3830:                     my $numtop = @{$cats[0]};
 3831:                     my $maxnum = $numtop;
 3832:                     my %default_names = (
 3833:                           instcode    => &mt('Official courses'),
 3834:                           communities => &mt('Communities'),
 3835:                     );
 3836: 
 3837:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 3838:                         ($cathash->{'instcode::0'} eq '') ||
 3839:                         (!grep(/^communities$/,@{$cats[0]})) || 
 3840:                         ($cathash->{'communities::0'} eq '')) {
 3841:                         $maxnum ++;
 3842:                     }
 3843:                     my $lastidx;
 3844:                     for (my $i=0; $i<$numtop; $i++) {
 3845:                         my $parent = $cats[0][$i];
 3846:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3847:                         my $item = &escape($parent).'::0';
 3848:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 3849:                         $lastidx = $idx{$item};
 3850:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3851:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 3852:                         for (my $k=0; $k<=$maxnum; $k++) {
 3853:                             my $vpos = $k+1;
 3854:                             my $selstr;
 3855:                             if ($k == $i) {
 3856:                                 $selstr = ' selected="selected" ';
 3857:                             }
 3858:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3859:                         }
 3860:                         $datatable .= '</select></td><td>';
 3861:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 3862:                             $datatable .=  '<span class="LC_nobreak">'
 3863:                                            .$default_names{$parent}.'</span>';
 3864:                             if ($parent eq 'instcode') {
 3865:                                 $datatable .= '<br /><span class="LC_nobreak">('
 3866:                                               .&mt('with institutional codes')
 3867:                                               .')</span></td><td'.$colattrib.'>';
 3868:                             } else {
 3869:                                 $datatable .= '<table><tr><td>';
 3870:                             }
 3871:                             $datatable .= '<span class="LC_nobreak">'
 3872:                                           .'<label><input type="radio" name="'
 3873:                                           .$parent.'" value="1" checked="checked" />'
 3874:                                           .&mt('Display').'</label>';
 3875:                             if ($parent eq 'instcode') {
 3876:                                 $datatable .= '&nbsp;';
 3877:                             } else {
 3878:                                 $datatable .= '</span></td></tr><tr><td>'
 3879:                                               .'<span class="LC_nobreak">';
 3880:                             }
 3881:                             $datatable .= '<label><input type="radio" name="'
 3882:                                           .$parent.'" value="0" />'
 3883:                                           .&mt('Do not display').'</label></span>';
 3884:                             if ($parent eq 'communities') {
 3885:                                 $datatable .= '</td></tr></table>';
 3886:                             }
 3887:                             $datatable .= '</td>';
 3888:                         } else {
 3889:                             $datatable .= $parent
 3890:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 3891:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 3892:                         }
 3893:                         my $depth = 1;
 3894:                         push(@path,$parent);
 3895:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 3896:                         pop(@path);
 3897:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 3898:                         $itemcount ++;
 3899:                     }
 3900:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3901:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 3902:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 3903:                     for (my $k=0; $k<=$maxnum; $k++) {
 3904:                         my $vpos = $k+1;
 3905:                         my $selstr;
 3906:                         if ($k == $numtop) {
 3907:                             $selstr = ' selected="selected" ';
 3908:                         }
 3909:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3910:                     }
 3911:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 3912:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 3913:                                   .'</tr>'."\n";
 3914:                     $itemcount ++;
 3915:                     foreach my $default ('instcode','communities') {
 3916:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 3917:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3918:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 3919:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 3920:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 3921:                             for (my $k=0; $k<=$maxnum; $k++) {
 3922:                                 my $vpos = $k+1;
 3923:                                 my $selstr;
 3924:                                 if ($k == $maxnum) {
 3925:                                     $selstr = ' selected="selected" ';
 3926:                                 }
 3927:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3928:                             }
 3929:                             $datatable .= '</select></span></td>'.
 3930:                                           '<td><span class="LC_nobreak">'.
 3931:                                           $default_names{$default}.'</span>';
 3932:                             if ($default eq 'instcode') {
 3933:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 3934:                                               .&mt('with institutional codes').')</span>';
 3935:                             }
 3936:                             $datatable .= '</td>'
 3937:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 3938:                                           .&mt('Display').'</label>&nbsp;'
 3939:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 3940:                                           .&mt('Do not display').'</label></span></td></tr>';
 3941:                         }
 3942:                     }
 3943:                 }
 3944:             } else {
 3945:                 $datatable .= &initialize_categories($itemcount);
 3946:             }
 3947:         } else {
 3948:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 3949:                           .&initialize_categories($itemcount);
 3950:         }
 3951:         $$rowtotal += $itemcount;
 3952:     }
 3953:     return $datatable;
 3954: }
 3955: 
 3956: sub print_serverstatuses {
 3957:     my ($dom,$settings,$rowtotal) = @_;
 3958:     my $datatable;
 3959:     my @pages = &serverstatus_pages();
 3960:     my (%namedaccess,%machineaccess);
 3961:     foreach my $type (@pages) {
 3962:         $namedaccess{$type} = '';
 3963:         $machineaccess{$type}= '';
 3964:     }
 3965:     if (ref($settings) eq 'HASH') {
 3966:         foreach my $type (@pages) {
 3967:             if (exists($settings->{$type})) {
 3968:                 if (ref($settings->{$type}) eq 'HASH') {
 3969:                     foreach my $key (keys(%{$settings->{$type}})) {
 3970:                         if ($key eq 'namedusers') {
 3971:                             $namedaccess{$type} = $settings->{$type}->{$key};
 3972:                         } elsif ($key eq 'machines') {
 3973:                             $machineaccess{$type} = $settings->{$type}->{$key};
 3974:                         }
 3975:                     }
 3976:                 }
 3977:             }
 3978:         }
 3979:     }
 3980:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 3981:     my $rownum = 0;
 3982:     my $css_class;
 3983:     foreach my $type (@pages) {
 3984:         $rownum ++;
 3985:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3986:         $datatable .= '<tr'.$css_class.'>'.
 3987:                       '<td><span class="LC_nobreak">'.
 3988:                       $titles->{$type}.'</span></td>'.
 3989:                       '<td class="LC_left_item">'.
 3990:                       '<input type="text" name="'.$type.'_namedusers" '.
 3991:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 3992:                       '<td class="LC_right_item">'.
 3993:                       '<span class="LC_nobreak">'.
 3994:                       '<input type="text" name="'.$type.'_machines" '.
 3995:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 3996:                       '</td></tr>'."\n";
 3997:     }
 3998:     $$rowtotal += $rownum;
 3999:     return $datatable;
 4000: }
 4001: 
 4002: sub serverstatus_pages {
 4003:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4004:             'clusterstatus','metadata_keywords','metadata_harvest',
 4005:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4006: }
 4007: 
 4008: sub coursecategories_javascript {
 4009:     my ($settings) = @_;
 4010:     my ($output,$jstext,$cathash);
 4011:     if (ref($settings) eq 'HASH') {
 4012:         $cathash = $settings->{'cats'};
 4013:     }
 4014:     if (ref($cathash) eq 'HASH') {
 4015:         my (@cats,@jsarray,%idx);
 4016:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4017:         if (@jsarray > 0) {
 4018:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4019:             for (my $i=0; $i<@jsarray; $i++) {
 4020:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4021:                     my $catstr = join('","',@{$jsarray[$i]});
 4022:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4023:                 }
 4024:             }
 4025:         }
 4026:     } else {
 4027:         $jstext  = '    var categories = Array(1);'."\n".
 4028:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4029:     }
 4030:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4031:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4032:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4033:     $output = <<"ENDSCRIPT";
 4034: <script type="text/javascript">
 4035: // <![CDATA[
 4036: function reorderCats(form,parent,item,idx) {
 4037:     var changedVal;
 4038: $jstext
 4039:     var newpos = 'addcategory_pos';
 4040:     var current = new Array;
 4041:     if (parent == '') {
 4042:         var has_instcode = 0;
 4043:         var maxtop = categories[idx].length;
 4044:         for (var j=0; j<maxtop; j++) {
 4045:             if (categories[idx][j] == 'instcode::0') {
 4046:                 has_instcode == 1;
 4047:             }
 4048:         }
 4049:         if (has_instcode == 0) {
 4050:             categories[idx][maxtop] = 'instcode_pos';
 4051:         }
 4052:     } else {
 4053:         newpos += '_'+parent;
 4054:     }
 4055:     var maxh = 1 + categories[idx].length;
 4056:     var current = new Array;
 4057:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4058:     if (item == newpos) {
 4059:         changedVal = newitemVal;
 4060:     } else {
 4061:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4062:         current[newitemVal] = newpos;
 4063:     }
 4064:     for (var i=0; i<categories[idx].length; i++) {
 4065:         var elementName = categories[idx][i];
 4066:         if (elementName != item) {
 4067:             if (form.elements[elementName]) {
 4068:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4069:                 current[currVal] = elementName;
 4070:             }
 4071:         }
 4072:     }
 4073:     var oldVal;
 4074:     for (var j=0; j<maxh; j++) {
 4075:         if (current[j] == undefined) {
 4076:             oldVal = j;
 4077:         }
 4078:     }
 4079:     if (oldVal < changedVal) {
 4080:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4081:            var elementName = current[k];
 4082:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4083:         }
 4084:     } else {
 4085:         for (var k=changedVal; k<oldVal; k++) {
 4086:             var elementName = current[k];
 4087:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4088:         }
 4089:     }
 4090:     return;
 4091: }
 4092: 
 4093: function categoryCheck(form) {
 4094:     if (form.elements['addcategory_name'].value == 'instcode') {
 4095:         alert('$instcode_reserved\\n$choose_again');
 4096:         return false;
 4097:     }
 4098:     if (form.elements['addcategory_name'].value == 'communities') {
 4099:         alert('$communities_reserved\\n$choose_again');
 4100:         return false;
 4101:     }
 4102:     return true;
 4103: }
 4104: 
 4105: // ]]>
 4106: </script>
 4107: 
 4108: ENDSCRIPT
 4109:     return $output;
 4110: }
 4111: 
 4112: sub initialize_categories {
 4113:     my ($itemcount) = @_;
 4114:     my ($datatable,$css_class,$chgstr);
 4115:     my %default_names = (
 4116:                       instcode    => 'Official courses (with institutional codes)',
 4117:                       communities => 'Communities',
 4118:                         );
 4119:     my $select0 = ' selected="selected"';
 4120:     my $select1 = '';
 4121:     foreach my $default ('instcode','communities') {
 4122:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4123:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4124:         if ($default eq 'communities') {
 4125:             $select1 = $select0;
 4126:             $select0 = '';
 4127:         }
 4128:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4129:                      .'<select name="'.$default.'_pos">'
 4130:                      .'<option value="0"'.$select0.'>1</option>'
 4131:                      .'<option value="1"'.$select1.'>2</option>'
 4132:                      .'<option value="2">3</option></select>&nbsp;'
 4133:                      .$default_names{$default}
 4134:                      .'</span></td><td><span class="LC_nobreak">'
 4135:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4136:                      .&mt('Display').'</label>&nbsp;<label>'
 4137:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4138:                  .'</label></span></td></tr>';
 4139:         $itemcount ++;
 4140:     }
 4141:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4142:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4143:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4144:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4145:                   .'<option value="0">1</option>'
 4146:                   .'<option value="1">2</option>'
 4147:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4148:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4149:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4150:     return $datatable;
 4151: }
 4152: 
 4153: sub build_category_rows {
 4154:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4155:     my ($text,$name,$item,$chgstr);
 4156:     if (ref($cats) eq 'ARRAY') {
 4157:         my $maxdepth = scalar(@{$cats});
 4158:         if (ref($cats->[$depth]) eq 'HASH') {
 4159:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4160:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4161:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4162:                 $text .= '<td><table class="LC_datatable">';
 4163:                 my ($idxnum,$parent_name,$parent_item);
 4164:                 my $higher = $depth - 1;
 4165:                 if ($higher == 0) {
 4166:                     $parent_name = &escape($parent).'::'.$higher;
 4167:                 } else {
 4168:                     if (ref($path) eq 'ARRAY') {
 4169:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4170:                     }
 4171:                 }
 4172:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4173:                 for (my $j=0; $j<=$numchildren; $j++) {
 4174:                     if ($j < $numchildren) {
 4175:                         $name = $cats->[$depth]{$parent}[$j];
 4176:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4177:                         $idxnum = $idx->{$item};
 4178:                     } else {
 4179:                         $name = $parent_name;
 4180:                         $item = $parent_item;
 4181:                     }
 4182:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4183:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4184:                     for (my $i=0; $i<=$numchildren; $i++) {
 4185:                         my $vpos = $i+1;
 4186:                         my $selstr;
 4187:                         if ($j == $i) {
 4188:                             $selstr = ' selected="selected" ';
 4189:                         }
 4190:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4191:                     }
 4192:                     $text .= '</select>&nbsp;';
 4193:                     if ($j < $numchildren) {
 4194:                         my $deeper = $depth+1;
 4195:                         $text .= $name.'&nbsp;'
 4196:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4197:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4198:                         if(ref($path) eq 'ARRAY') {
 4199:                             push(@{$path},$name);
 4200:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4201:                             pop(@{$path});
 4202:                         }
 4203:                     } else {
 4204:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4205:                         if ($j == $numchildren) {
 4206:                             $text .= $name;
 4207:                         } else {
 4208:                             $text .= $item;
 4209:                         }
 4210:                         $text .= '" value="" />';
 4211:                     }
 4212:                     $text .= '</td></tr>';
 4213:                 }
 4214:                 $text .= '</table></td>';
 4215:             } else {
 4216:                 my $higher = $depth-1;
 4217:                 if ($higher == 0) {
 4218:                     $name = &escape($parent).'::'.$higher;
 4219:                 } else {
 4220:                     if (ref($path) eq 'ARRAY') {
 4221:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4222:                     }
 4223:                 }
 4224:                 my $colspan;
 4225:                 if ($parent ne 'instcode') {
 4226:                     $colspan = $maxdepth - $depth - 1;
 4227:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4228:                 }
 4229:             }
 4230:         }
 4231:     }
 4232:     return $text;
 4233: }
 4234: 
 4235: sub modifiable_userdata_row {
 4236:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4237:     my $rolename;
 4238:     if ($context eq 'selfcreate') {
 4239:         if (ref($usertypes) eq 'HASH') {
 4240:             $rolename = $usertypes->{$role};
 4241:         } else {
 4242:             $rolename = $role;
 4243:         }
 4244:     } else {
 4245:         if ($role eq 'cr') {
 4246:             $rolename = &mt('Custom role');
 4247:         } else {
 4248:             $rolename = &Apache::lonnet::plaintext($role);
 4249:         }
 4250:     }
 4251:     my @fields = ('lastname','firstname','middlename','generation',
 4252:                   'permanentemail','id');
 4253:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4254:     my $output;
 4255:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4256:     $output = '<tr '.$css_class.'>'.
 4257:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4258:               '<td class="LC_left_item" colspan="2"><table>';
 4259:     my $rem;
 4260:     my %checks;
 4261:     if (ref($settings) eq 'HASH') {
 4262:         if (ref($settings->{$context}) eq 'HASH') {
 4263:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4264:                 foreach my $field (@fields) {
 4265:                     if ($settings->{$context}->{$role}->{$field}) {
 4266:                         $checks{$field} = ' checked="checked" ';
 4267:                     }
 4268:                 }
 4269:             }
 4270:         }
 4271:     }
 4272:     for (my $i=0; $i<@fields; $i++) {
 4273:         my $rem = $i%($numinrow);
 4274:         if ($rem == 0) {
 4275:             if ($i > 0) {
 4276:                 $output .= '</tr>';
 4277:             }
 4278:             $output .= '<tr>';
 4279:         }
 4280:         my $check = ' ';
 4281:         if (exists($checks{$fields[$i]})) {
 4282:             $check = $checks{$fields[$i]}
 4283:         } else {
 4284:             if ($role eq 'st') {
 4285:                 if (ref($settings) ne 'HASH') {
 4286:                     $check = ' checked="checked" '; 
 4287:                 }
 4288:             }
 4289:         }
 4290:         $output .= '<td class="LC_left_item">'.
 4291:                    '<span class="LC_nobreak"><label>'.
 4292:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4293:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4294:                    '</label></span></td>';
 4295:         $rem = @fields%($numinrow);
 4296:     }
 4297:     my $colsleft = $numinrow - $rem;
 4298:     if ($colsleft > 1 ) {
 4299:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4300:                    '&nbsp;</td>';
 4301:     } elsif ($colsleft == 1) {
 4302:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4303:     }
 4304:     $output .= '</tr></table></td></tr>';
 4305:     return $output;
 4306: }
 4307: 
 4308: sub insttypes_row {
 4309:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4310:     my %lt = &Apache::lonlocal::texthash (
 4311:                       cansearch => 'Users allowed to search',
 4312:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4313:                       lockablenames => 'User preference to lock name',
 4314:              );
 4315:     my $showdom;
 4316:     if ($context eq 'cansearch') {
 4317:         $showdom = ' ('.$dom.')';
 4318:     }
 4319:     my $class = 'LC_left_item';
 4320:     if ($context eq 'statustocreate') {
 4321:         $class = 'LC_right_item';
 4322:     }
 4323:     my $output =  '<tr class="LC_odd_row">'.
 4324:                   '<td>'.$lt{$context}.$showdom.
 4325:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4326:     my $rem;
 4327:     if (ref($types) eq 'ARRAY') {
 4328:         for (my $i=0; $i<@{$types}; $i++) {
 4329:             if (defined($usertypes->{$types->[$i]})) {
 4330:                 my $rem = $i%($numinrow);
 4331:                 if ($rem == 0) {
 4332:                     if ($i > 0) {
 4333:                         $output .= '</tr>';
 4334:                     }
 4335:                     $output .= '<tr>';
 4336:                 }
 4337:                 my $check = ' ';
 4338:                 if (ref($settings) eq 'HASH') {
 4339:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4340:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4341:                             $check = ' checked="checked" ';
 4342:                         }
 4343:                     } elsif ($context eq 'statustocreate') {
 4344:                         $check = ' checked="checked" ';
 4345:                     }
 4346:                 }
 4347:                 $output .= '<td class="LC_left_item">'.
 4348:                            '<span class="LC_nobreak"><label>'.
 4349:                            '<input type="checkbox" name="'.$context.'" '.
 4350:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4351:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4352:             }
 4353:         }
 4354:         $rem = @{$types}%($numinrow);
 4355:     }
 4356:     my $colsleft = $numinrow - $rem;
 4357:     if (($rem == 0) && (@{$types} > 0)) {
 4358:         $output .= '<tr>';
 4359:     }
 4360:     if ($colsleft > 1) {
 4361:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4362:     } else {
 4363:         $output .= '<td class="LC_left_item">';
 4364:     }
 4365:     my $defcheck = ' ';
 4366:     if (ref($settings) eq 'HASH') {  
 4367:         if (ref($settings->{$context}) eq 'ARRAY') {
 4368:             if (grep(/^default$/,@{$settings->{$context}})) {
 4369:                 $defcheck = ' checked="checked" ';
 4370:             }
 4371:         } elsif ($context eq 'statustocreate') {
 4372:             $defcheck = ' checked="checked" ';
 4373:         }
 4374:     }
 4375:     $output .= '<span class="LC_nobreak"><label>'.
 4376:                '<input type="checkbox" name="'.$context.'" '.
 4377:                'value="default"'.$defcheck.'/>'.
 4378:                $othertitle.'</label></span></td>'.
 4379:                '</tr></table></td></tr>';
 4380:     return $output;
 4381: }
 4382: 
 4383: sub sorted_searchtitles {
 4384:     my %searchtitles = &Apache::lonlocal::texthash(
 4385:                          'uname' => 'username',
 4386:                          'lastname' => 'last name',
 4387:                          'lastfirst' => 'last name, first name',
 4388:                      );
 4389:     my @titleorder = ('uname','lastname','lastfirst');
 4390:     return (\%searchtitles,\@titleorder);
 4391: }
 4392: 
 4393: sub sorted_searchtypes {
 4394:     my %srchtypes_desc = (
 4395:                            exact    => 'is exact match',
 4396:                            contains => 'contains ..',
 4397:                            begins   => 'begins with ..',
 4398:                          );
 4399:     my @srchtypeorder = ('exact','begins','contains');
 4400:     return (\%srchtypes_desc,\@srchtypeorder);
 4401: }
 4402: 
 4403: sub usertype_update_row {
 4404:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4405:     my $datatable;
 4406:     my $numinrow = 4;
 4407:     foreach my $type (@{$types}) {
 4408:         if (defined($usertypes->{$type})) {
 4409:             $$rownums ++;
 4410:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4411:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4412:                           '</td><td class="LC_left_item"><table>';
 4413:             for (my $i=0; $i<@{$fields}; $i++) {
 4414:                 my $rem = $i%($numinrow);
 4415:                 if ($rem == 0) {
 4416:                     if ($i > 0) {
 4417:                         $datatable .= '</tr>';
 4418:                     }
 4419:                     $datatable .= '<tr>';
 4420:                 }
 4421:                 my $check = ' ';
 4422:                 if (ref($settings) eq 'HASH') {
 4423:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4424:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4425:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4426:                                 $check = ' checked="checked" ';
 4427:                             }
 4428:                         }
 4429:                     }
 4430:                 }
 4431: 
 4432:                 if ($i == @{$fields}-1) {
 4433:                     my $colsleft = $numinrow - $rem;
 4434:                     if ($colsleft > 1) {
 4435:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4436:                     } else {
 4437:                         $datatable .= '<td>';
 4438:                     }
 4439:                 } else {
 4440:                     $datatable .= '<td>';
 4441:                 }
 4442:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4443:                               '<input type="checkbox" name="updateable_'.$type.
 4444:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4445:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4446:             }
 4447:             $datatable .= '</tr></table></td></tr>';
 4448:         }
 4449:     }
 4450:     return $datatable;
 4451: }
 4452: 
 4453: sub modify_login {
 4454:     my ($r,$dom,$confname,%domconfig) = @_;
 4455:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges);
 4456:     my %title = ( coursecatalog => 'Display course catalog',
 4457:                   adminmail => 'Display administrator E-mail address',
 4458:                   newuser => 'Link for visitors to create a user account',
 4459:                   loginheader => 'Log-in box header');
 4460:     my @offon = ('off','on');
 4461:     my %curr_loginvia;
 4462:     if (ref($domconfig{login}) eq 'HASH') {
 4463:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4464:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4465:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4466:             }
 4467:         }
 4468:     }
 4469:     my %loginhash;
 4470:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4471:                                            \%domconfig,\%loginhash);
 4472:     my @toggles = ('coursecatalog','adminmail','newuser');
 4473:     foreach my $item (@toggles) {
 4474:         $loginhash{login}{$item} = $env{'form.'.$item};
 4475:     }
 4476:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4477:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4478:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4479:                                          \%loginhash);
 4480:     }
 4481: 
 4482:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4483:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4484:     if (keys(%servers) > 1) {
 4485:         foreach my $lonhost (keys(%servers)) {
 4486:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4487:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4488:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4489:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4490:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4491:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4492:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4493:                         $changes{'loginvia'}{$lonhost} = 1;
 4494:                     } else {
 4495:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4496:                         $changes{'loginvia'}{$lonhost} = 1;
 4497:                     }
 4498:                 } else {
 4499:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4500:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4501:                         $changes{'loginvia'}{$lonhost} = 1;
 4502:                     }
 4503:                 }
 4504:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4505:                     foreach my $item (@loginvia_attribs) {
 4506:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4507:                     }
 4508:                 } else {
 4509:                     foreach my $item (@loginvia_attribs) {
 4510:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4511:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4512:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4513:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4514:                                 $new = '/';
 4515:                             }
 4516:                         }
 4517:                         if (($item eq 'custompath') && 
 4518:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4519:                             $new = '';
 4520:                         }
 4521:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4522:                             $changes{'loginvia'}{$lonhost} = 1;
 4523:                         }
 4524:                         if ($item eq 'exempt') {
 4525:                             $new =~ s/^\s+//;
 4526:                             $new =~ s/\s+$//;
 4527:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4528:                             my @okips;
 4529:                             foreach my $ip (@poss_ips) {
 4530:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4531:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4532:                                         push(@okips,$ip); 
 4533:                                     }
 4534:                                 }
 4535:                             }
 4536:                             if (@okips > 0) {
 4537:                                 $new = join(',',@okips); 
 4538:                             } else {
 4539:                                 $new = ''; 
 4540:                             }
 4541:                         }
 4542: 
 4543:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4544:                     }
 4545:                 }
 4546:             } else {
 4547:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4548:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4549:                     $changes{'loginvia'}{$lonhost} = 1;
 4550:                     foreach my $item (@loginvia_attribs) {
 4551:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4552:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4553:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4554:                                 $new = '/';
 4555:                             }
 4556:                         }
 4557:                         if (($item eq 'custompath') && 
 4558:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4559:                             $new = '';
 4560:                         }
 4561:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4562:                     }
 4563:                 }
 4564:             }
 4565:         }
 4566:     }
 4567: 
 4568:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4569:                                              $dom);
 4570:     if ($putresult eq 'ok') {
 4571:         my @toggles = ('coursecatalog','adminmail','newuser');
 4572:         my %defaultchecked = (
 4573:                     'coursecatalog' => 'on',
 4574:                     'adminmail'     => 'off',
 4575:                     'newuser'       => 'off',
 4576:         );
 4577:         if (ref($domconfig{'login'}) eq 'HASH') {
 4578:             foreach my $item (@toggles) {
 4579:                 if ($defaultchecked{$item} eq 'on') { 
 4580:                     if (($domconfig{'login'}{$item} eq '0') &&
 4581:                         ($env{'form.'.$item} eq '1')) {
 4582:                         $changes{$item} = 1;
 4583:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4584:                               $domconfig{'login'}{$item} eq '1') &&
 4585:                              ($env{'form.'.$item} eq '0')) {
 4586:                         $changes{$item} = 1;
 4587:                     }
 4588:                 } elsif ($defaultchecked{$item} eq 'off') {
 4589:                     if (($domconfig{'login'}{$item} eq '1') &&
 4590:                         ($env{'form.'.$item} eq '0')) {
 4591:                         $changes{$item} = 1;
 4592:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4593:                               $domconfig{'login'}{$item} eq '0') &&
 4594:                              ($env{'form.'.$item} eq '1')) {
 4595:                         $changes{$item} = 1;
 4596:                     }
 4597:                 }
 4598:             }
 4599:         }
 4600:         if (keys(%changes) > 0 || $colchgtext) {
 4601:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4602:             $resulttext = &mt('Changes made:').'<ul>';
 4603:             foreach my $item (sort(keys(%changes))) {
 4604:                 if ($item eq 'loginvia') {
 4605:                     if (ref($changes{$item}) eq 'HASH') {
 4606:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4607:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4608:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4609:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4610:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4611:                                     $protocol = 'http' if ($protocol ne 'https');
 4612:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4613: 
 4614:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4615:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4616:                                     } else {
 4617:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4618:                                     }
 4619:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4620:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4621:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4622:                                     }
 4623:                                     $resulttext .= '</li>';
 4624:                                 } else {
 4625:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4626:                                 }
 4627:                             } else {
 4628:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4629:                             }
 4630:                         }
 4631:                         $resulttext .= '</ul></li>';
 4632:                     }
 4633:                 } else {
 4634:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4635:                 }
 4636:             }
 4637:             $resulttext .= $colchgtext.'</ul>';
 4638:         } else {
 4639:             $resulttext = &mt('No changes made to log-in page settings');
 4640:         }
 4641:     } else {
 4642:         $resulttext = '<span class="LC_error">'.
 4643: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4644:     }
 4645:     if ($errors) {
 4646:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 4647:                        $errors.'</ul>';
 4648:     }
 4649:     return $resulttext;
 4650: }
 4651: 
 4652: sub color_font_choices {
 4653:     my %choices =
 4654:         &Apache::lonlocal::texthash (
 4655:             img => "Header",
 4656:             bgs => "Background colors",
 4657:             links => "Link colors",
 4658:             images => "Images",
 4659:             font => "Font color",
 4660:             fontmenu => "Font Menu",
 4661:             pgbg => "Page",
 4662:             tabbg => "Header",
 4663:             sidebg => "Border",
 4664:             link => "Link",
 4665:             alink => "Active link",
 4666:             vlink => "Visited link",
 4667:         );
 4668:     return %choices;
 4669: }
 4670: 
 4671: sub modify_rolecolors {
 4672:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 4673:     my ($resulttext,%rolehash);
 4674:     $rolehash{'rolecolors'} = {};
 4675:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 4676:         if ($domconfig{'rolecolors'} eq '') {
 4677:             $domconfig{'rolecolors'} = {};
 4678:         }
 4679:     }
 4680:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 4681:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 4682:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 4683:                                              $dom);
 4684:     if ($putresult eq 'ok') {
 4685:         if (keys(%changes) > 0) {
 4686:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4687:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 4688:                                              $rolehash{'rolecolors'});
 4689:         } else {
 4690:             $resulttext = &mt('No changes made to default color schemes');
 4691:         }
 4692:     } else {
 4693:         $resulttext = '<span class="LC_error">'.
 4694: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4695:     }
 4696:     if ($errors) {
 4697:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 4698:                        $errors.'</ul>';
 4699:     }
 4700:     return $resulttext;
 4701: }
 4702: 
 4703: sub modify_colors {
 4704:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 4705:     my (%changes,%choices);
 4706:     my @bgs;
 4707:     my @links = ('link','alink','vlink');
 4708:     my @logintext;
 4709:     my @images;
 4710:     my $servadm = $r->dir_config('lonAdmEMail');
 4711:     my $errors;
 4712:     foreach my $role (@{$roles}) {
 4713:         if ($role eq 'login') {
 4714:             %choices = &login_choices();
 4715:             @logintext = ('textcol','bgcol');
 4716:         } else {
 4717:             %choices = &color_font_choices();
 4718:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 4719:         }
 4720:         if ($role eq 'login') {
 4721:             @images = ('img','logo','domlogo','login');
 4722:             @bgs = ('pgbg','mainbg','sidebg');
 4723:         } else {
 4724:             @images = ('img');
 4725:             @bgs = ('pgbg','tabbg','sidebg'); 
 4726:         }
 4727:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 4728:         foreach my $item (@bgs,@links,@logintext) {
 4729:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 4730:         }
 4731:         my ($configuserok,$author_ok,$switchserver) = 
 4732:             &config_check($dom,$confname,$servadm);
 4733:         my ($width,$height) = &thumb_dimensions();
 4734:         if (ref($domconfig->{$role}) ne 'HASH') {
 4735:             $domconfig->{$role} = {};
 4736:         }
 4737:         foreach my $img (@images) {
 4738:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 4739:                 if (defined($env{'form.login_showlogo_'.$img})) {
 4740:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 4741:                 } else { 
 4742:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 4743:                 }
 4744:             } 
 4745: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 4746: 		 && !defined($domconfig->{$role}{$img})
 4747: 		 && !$env{'form.'.$role.'_del_'.$img}
 4748: 		 && $env{'form.'.$role.'_import_'.$img}) {
 4749: 		# import the old configured image from the .tab setting
 4750: 		# if they haven't provided a new one 
 4751: 		$domconfig->{$role}{$img} = 
 4752: 		    $env{'form.'.$role.'_import_'.$img};
 4753: 	    }
 4754:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 4755:                 my $error;
 4756:                 if ($configuserok eq 'ok') {
 4757:                     if ($switchserver) {
 4758:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 4759:                     } else {
 4760:                         if ($author_ok eq 'ok') {
 4761:                             my ($result,$logourl) = 
 4762:                                 &publishlogo($r,'upload',$role.'_'.$img,
 4763:                                            $dom,$confname,$img,$width,$height);
 4764:                             if ($result eq 'ok') {
 4765:                                 $confhash->{$role}{$img} = $logourl;
 4766:                                 $changes{$role}{'images'}{$img} = 1;
 4767:                             } else {
 4768:                                 $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);
 4769:                             }
 4770:                         } else {
 4771:                             $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);
 4772:                         }
 4773:                     }
 4774:                 } else {
 4775:                     $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);
 4776:                 }
 4777:                 if ($error) {
 4778:                     &Apache::lonnet::logthis($error);
 4779:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4780:                 }
 4781:             } elsif ($domconfig->{$role}{$img} ne '') {
 4782:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 4783:                     my $error;
 4784:                     if ($configuserok eq 'ok') {
 4785: # is confname an author?
 4786:                         if ($switchserver eq '') {
 4787:                             if ($author_ok eq 'ok') {
 4788:                                 my ($result,$logourl) = 
 4789:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 4790:                                             $dom,$confname,$img,$width,$height);
 4791:                                 if ($result eq 'ok') {
 4792:                                     $confhash->{$role}{$img} = $logourl;
 4793: 				    $changes{$role}{'images'}{$img} = 1;
 4794:                                 }
 4795:                             }
 4796:                         }
 4797:                     }
 4798:                 }
 4799:             }
 4800:         }
 4801:         if (ref($domconfig) eq 'HASH') {
 4802:             if (ref($domconfig->{$role}) eq 'HASH') {
 4803:                 foreach my $img (@images) {
 4804:                     if ($domconfig->{$role}{$img} ne '') {
 4805:                         if ($env{'form.'.$role.'_del_'.$img}) {
 4806:                             $confhash->{$role}{$img} = '';
 4807:                             $changes{$role}{'images'}{$img} = 1;
 4808:                         } else {
 4809:                             if ($confhash->{$role}{$img} eq '') {
 4810:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 4811:                             }
 4812:                         }
 4813:                     } else {
 4814:                         if ($env{'form.'.$role.'_del_'.$img}) {
 4815:                             $confhash->{$role}{$img} = '';
 4816:                             $changes{$role}{'images'}{$img} = 1;
 4817:                         } 
 4818:                     }
 4819:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 4820:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 4821:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 4822:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 4823:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 4824:                             }
 4825:                         } else {
 4826:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 4827:                                 $changes{$role}{'showlogo'}{$img} = 1;
 4828:                             }
 4829:                         }
 4830:                     }
 4831:                 }
 4832:                 if ($domconfig->{$role}{'font'} ne '') {
 4833:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 4834:                         $changes{$role}{'font'} = 1;
 4835:                     }
 4836:                 } else {
 4837:                     if ($confhash->{$role}{'font'}) {
 4838:                         $changes{$role}{'font'} = 1;
 4839:                     }
 4840:                 }
 4841:                 if ($role ne 'login') {
 4842:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 4843:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 4844:                             $changes{$role}{'fontmenu'} = 1;
 4845:                         }
 4846:                     } else {
 4847:                         if ($confhash->{$role}{'fontmenu'}) {
 4848:                             $changes{$role}{'fontmenu'} = 1;
 4849:                         }
 4850:                     }
 4851:                 }
 4852:                 foreach my $item (@bgs) {
 4853:                     if ($domconfig->{$role}{$item} ne '') {
 4854:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 4855:                             $changes{$role}{'bgs'}{$item} = 1;
 4856:                         } 
 4857:                     } else {
 4858:                         if ($confhash->{$role}{$item}) {
 4859:                             $changes{$role}{'bgs'}{$item} = 1;
 4860:                         }
 4861:                     }
 4862:                 }
 4863:                 foreach my $item (@links) {
 4864:                     if ($domconfig->{$role}{$item} ne '') {
 4865:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 4866:                             $changes{$role}{'links'}{$item} = 1;
 4867:                         }
 4868:                     } else {
 4869:                         if ($confhash->{$role}{$item}) {
 4870:                             $changes{$role}{'links'}{$item} = 1;
 4871:                         }
 4872:                     }
 4873:                 }
 4874:                 foreach my $item (@logintext) {
 4875:                     if ($domconfig->{$role}{$item} ne '') {
 4876:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 4877:                             $changes{$role}{'logintext'}{$item} = 1;
 4878:                         }
 4879:                     } else {
 4880:                         if ($confhash->{$role}{$item}) {
 4881:                             $changes{$role}{'logintext'}{$item} = 1;
 4882:                         }
 4883:                     }
 4884:                 }
 4885:             } else {
 4886:                 &default_change_checker($role,\@images,\@links,\@bgs,
 4887:                                         \@logintext,$confhash,\%changes); 
 4888:             }
 4889:         } else {
 4890:             &default_change_checker($role,\@images,\@links,\@bgs,
 4891:                                     \@logintext,$confhash,\%changes); 
 4892:         }
 4893:     }
 4894:     return ($errors,%changes);
 4895: }
 4896: 
 4897: sub config_check {
 4898:     my ($dom,$confname,$servadm) = @_;
 4899:     my ($configuserok,$author_ok,$switchserver,%currroles);
 4900:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 4901:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 4902:                                                    $confname,$servadm);
 4903:     if ($configuserok eq 'ok') {
 4904:         $switchserver = &check_switchserver($dom,$confname);
 4905:         if ($switchserver eq '') {
 4906:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 4907:         }
 4908:     }
 4909:     return ($configuserok,$author_ok,$switchserver);
 4910: }
 4911: 
 4912: sub default_change_checker {
 4913:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 4914:     foreach my $item (@{$links}) {
 4915:         if ($confhash->{$role}{$item}) {
 4916:             $changes->{$role}{'links'}{$item} = 1;
 4917:         }
 4918:     }
 4919:     foreach my $item (@{$bgs}) {
 4920:         if ($confhash->{$role}{$item}) {
 4921:             $changes->{$role}{'bgs'}{$item} = 1;
 4922:         }
 4923:     }
 4924:     foreach my $item (@{$logintext}) {
 4925:         if ($confhash->{$role}{$item}) {
 4926:             $changes->{$role}{'logintext'}{$item} = 1;
 4927:         }
 4928:     }
 4929:     foreach my $img (@{$images}) {
 4930:         if ($env{'form.'.$role.'_del_'.$img}) {
 4931:             $confhash->{$role}{$img} = '';
 4932:             $changes->{$role}{'images'}{$img} = 1;
 4933:         }
 4934:         if ($role eq 'login') {
 4935:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 4936:                 $changes->{$role}{'showlogo'}{$img} = 1;
 4937:             }
 4938:         }
 4939:     }
 4940:     if ($confhash->{$role}{'font'}) {
 4941:         $changes->{$role}{'font'} = 1;
 4942:     }
 4943: }
 4944: 
 4945: sub display_colorchgs {
 4946:     my ($dom,$changes,$roles,$confhash) = @_;
 4947:     my (%choices,$resulttext);
 4948:     if (!grep(/^login$/,@{$roles})) {
 4949:         $resulttext = &mt('Changes made:').'<br />';
 4950:     }
 4951:     foreach my $role (@{$roles}) {
 4952:         if ($role eq 'login') {
 4953:             %choices = &login_choices();
 4954:         } else {
 4955:             %choices = &color_font_choices();
 4956:         }
 4957:         if (ref($changes->{$role}) eq 'HASH') {
 4958:             if ($role ne 'login') {
 4959:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 4960:             }
 4961:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 4962:                 if ($role ne 'login') {
 4963:                     $resulttext .= '<ul>';
 4964:                 }
 4965:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 4966:                     if ($role ne 'login') {
 4967:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 4968:                     }
 4969:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 4970:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 4971:                             if ($confhash->{$role}{$key}{$item}) {
 4972:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 4973:                             } else {
 4974:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 4975:                             }
 4976:                         } elsif ($confhash->{$role}{$item} eq '') {
 4977:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 4978:                         } else {
 4979:                             my $newitem = $confhash->{$role}{$item};
 4980:                             if ($key eq 'images') {
 4981:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 4982:                             }
 4983:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 4984:                         }
 4985:                     }
 4986:                     if ($role ne 'login') {
 4987:                         $resulttext .= '</ul></li>';
 4988:                     }
 4989:                 } else {
 4990:                     if ($confhash->{$role}{$key} eq '') {
 4991:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 4992:                     } else {
 4993:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 4994:                     }
 4995:                 }
 4996:                 if ($role ne 'login') {
 4997:                     $resulttext .= '</ul>';
 4998:                 }
 4999:             }
 5000:         }
 5001:     }
 5002:     return $resulttext;
 5003: }
 5004: 
 5005: sub thumb_dimensions {
 5006:     return ('200','50');
 5007: }
 5008: 
 5009: sub check_dimensions {
 5010:     my ($inputfile) = @_;
 5011:     my ($fullwidth,$fullheight);
 5012:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5013:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5014:             my $imageinfo = <PIPE>;
 5015:             if (!close(PIPE)) {
 5016:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5017:             }
 5018:             chomp($imageinfo);
 5019:             my ($fullsize) = 
 5020:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5021:             if ($fullsize) {
 5022:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5023:             }
 5024:         }
 5025:     }
 5026:     return ($fullwidth,$fullheight);
 5027: }
 5028: 
 5029: sub check_configuser {
 5030:     my ($uhome,$dom,$confname,$servadm) = @_;
 5031:     my ($configuserok,%currroles);
 5032:     if ($uhome eq 'no_host') {
 5033:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5034:         my $configpass = &LONCAPA::Enrollment::create_password();
 5035:         $configuserok = 
 5036:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5037:                              $configpass,'','','','','',undef,$servadm);
 5038:     } else {
 5039:         $configuserok = 'ok';
 5040:         %currroles = 
 5041:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5042:     }
 5043:     return ($configuserok,%currroles);
 5044: }
 5045: 
 5046: sub check_authorstatus {
 5047:     my ($dom,$confname,%currroles) = @_;
 5048:     my $author_ok;
 5049:     if (!$currroles{':'.$dom.':au'}) {
 5050:         my $start = time;
 5051:         my $end = 0;
 5052:         $author_ok = 
 5053:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5054:                                         'au',$end,$start,'','','domconfig');
 5055:     } else {
 5056:         $author_ok = 'ok';
 5057:     }
 5058:     return $author_ok;
 5059: }
 5060: 
 5061: sub publishlogo {
 5062:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5063:     my ($output,$fname,$logourl);
 5064:     if ($action eq 'upload') {
 5065:         $fname=$env{'form.'.$formname.'.filename'};
 5066:         chop($env{'form.'.$formname});
 5067:     } else {
 5068:         ($fname) = ($formname =~ /([^\/]+)$/);
 5069:     }
 5070:     if ($savefileas ne '') {
 5071:         $fname = $savefileas;
 5072:     }
 5073:     $fname=&Apache::lonnet::clean_filename($fname);
 5074: # See if there is anything left
 5075:     unless ($fname) { return ('error: no uploaded file'); }
 5076:     $fname="$subdir/$fname";
 5077:     my $docroot=$r->dir_config('lonDocRoot'); 
 5078:     my $filepath="$docroot/priv";
 5079:     my $relpath = "$dom/$confname";
 5080:     my ($fnamepath,$file,$fetchthumb);
 5081:     $file=$fname;
 5082:     if ($fname=~m|/|) {
 5083:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5084:     }
 5085:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5086:     my $count;
 5087:     for ($count=5;$count<=$#parts;$count++) {
 5088:         $filepath.="/$parts[$count]";
 5089:         if ((-e $filepath)!=1) {
 5090:             mkdir($filepath,02770);
 5091:         }
 5092:     }
 5093:     # Check for bad extension and disallow upload
 5094:     if ($file=~/\.(\w+)$/ &&
 5095:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5096:         $output = 
 5097:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 5098:     } elsif ($file=~/\.(\w+)$/ &&
 5099:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5100:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5101:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5102:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5103:     } elsif (-d "$filepath/$file") {
 5104:         $output = &mt('File name is a directory name - rename the file and re-upload');
 5105:     } else {
 5106:         my $source = $filepath.'/'.$file;
 5107:         my $logfile;
 5108:         if (!open($logfile,">>$source".'.log')) {
 5109:             return (&mt('No write permission to Construction Space'));
 5110:         }
 5111:         print $logfile
 5112: "\n================= Publish ".localtime()." ================\n".
 5113: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5114: # Save the file
 5115:         if (!open(FH,'>'.$source)) {
 5116:             &Apache::lonnet::logthis('Failed to create '.$source);
 5117:             return (&mt('Failed to create file'));
 5118:         }
 5119:         if ($action eq 'upload') {
 5120:             if (!print FH ($env{'form.'.$formname})) {
 5121:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5122:                 return (&mt('Failed to write file'));
 5123:             }
 5124:         } else {
 5125:             my $original = &Apache::lonnet::filelocation('',$formname);
 5126:             if(!copy($original,$source)) {
 5127:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5128:                 return (&mt('Failed to write file'));
 5129:             }
 5130:         }
 5131:         close(FH);
 5132:         chmod(0660, $source); # Permissions to rw-rw---.
 5133: 
 5134:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5135:         my $copyfile=$targetdir.'/'.$file;
 5136: 
 5137:         my @parts=split(/\//,$targetdir);
 5138:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5139:         for (my $count=5;$count<=$#parts;$count++) {
 5140:             $path.="/$parts[$count]";
 5141:             if (!-e $path) {
 5142:                 print $logfile "\nCreating directory ".$path;
 5143:                 mkdir($path,02770);
 5144:             }
 5145:         }
 5146:         my $versionresult;
 5147:         if (-e $copyfile) {
 5148:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5149:         } else {
 5150:             $versionresult = 'ok';
 5151:         }
 5152:         if ($versionresult eq 'ok') {
 5153:             if (copy($source,$copyfile)) {
 5154:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5155:                 $output = 'ok';
 5156:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5157:                 push(@{$modified_urls},[$copyfile,$source]);
 5158:                 my $metaoutput = 
 5159:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5160:                 unless ($registered_cleanup) {
 5161:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5162:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5163:                     $registered_cleanup=1;
 5164:                 }
 5165:             } else {
 5166:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5167:                 $output = &mt('Failed to copy file to RES space').", $!";
 5168:             }
 5169:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5170:                 my $inputfile = $filepath.'/'.$file;
 5171:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5172:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5173:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5174:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5175:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5176:                         system("convert -sample $thumbsize $inputfile $outfile");
 5177:                         chmod(0660, $filepath.'/tn-'.$file);
 5178:                         if (-e $outfile) {
 5179:                             my $copyfile=$targetdir.'/tn-'.$file;
 5180:                             if (copy($outfile,$copyfile)) {
 5181:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5182:                                 my $thumb_metaoutput = 
 5183:                                     &write_metadata($dom,$confname,$formname,
 5184:                                                     $targetdir,'tn-'.$file,$logfile);
 5185:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5186:                                 unless ($registered_cleanup) {
 5187:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5188:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5189:                                     $registered_cleanup=1;
 5190:                                 }
 5191:                             } else {
 5192:                                 print $logfile "\nUnable to write ".$copyfile.
 5193:                                                ':'.$!."\n";
 5194:                             }
 5195:                         }
 5196:                     }
 5197:                 }
 5198:             }
 5199:         } else {
 5200:             $output = $versionresult;
 5201:         }
 5202:     }
 5203:     return ($output,$logourl);
 5204: }
 5205: 
 5206: sub logo_versioning {
 5207:     my ($targetdir,$file,$logfile) = @_;
 5208:     my $target = $targetdir.'/'.$file;
 5209:     my ($maxversion,$fn,$extn,$output);
 5210:     $maxversion = 0;
 5211:     if ($file =~ /^(.+)\.(\w+)$/) {
 5212:         $fn=$1;
 5213:         $extn=$2;
 5214:     }
 5215:     opendir(DIR,$targetdir);
 5216:     while (my $filename=readdir(DIR)) {
 5217:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5218:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5219:         }
 5220:     }
 5221:     $maxversion++;
 5222:     print $logfile "\nCreating old version ".$maxversion."\n";
 5223:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5224:     if (copy($target,$copyfile)) {
 5225:         print $logfile "Copied old target to ".$copyfile."\n";
 5226:         $copyfile=$copyfile.'.meta';
 5227:         if (copy($target.'.meta',$copyfile)) {
 5228:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5229:             $output = 'ok';
 5230:         } else {
 5231:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5232:             $output = &mt('Failed to copy old meta').", $!, ";
 5233:         }
 5234:     } else {
 5235:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5236:         $output = &mt('Failed to copy old target').", $!, ";
 5237:     }
 5238:     return $output;
 5239: }
 5240: 
 5241: sub write_metadata {
 5242:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5243:     my (%metadatafields,%metadatakeys,$output);
 5244:     $metadatafields{'title'}=$formname;
 5245:     $metadatafields{'creationdate'}=time;
 5246:     $metadatafields{'lastrevisiondate'}=time;
 5247:     $metadatafields{'copyright'}='public';
 5248:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5249:                                          $env{'user.domain'};
 5250:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5251:     $metadatafields{'domain'}=$dom;
 5252:     {
 5253:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5254:         my $mfh;
 5255:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5256:             foreach (sort keys %metadatafields) {
 5257:                 unless ($_=~/\./) {
 5258:                     my $unikey=$_;
 5259:                     $unikey=~/^([A-Za-z]+)/;
 5260:                     my $tag=$1;
 5261:                     $tag=~tr/A-Z/a-z/;
 5262:                     print $mfh "\n\<$tag";
 5263:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5264:                         my $value=$metadatafields{$unikey.'.'.$_};
 5265:                         $value=~s/\"/\'\'/g;
 5266:                         print $mfh ' '.$_.'="'.$value.'"';
 5267:                     }
 5268:                     print $mfh '>'.
 5269:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5270:                             .'</'.$tag.'>';
 5271:                 }
 5272:             }
 5273:             $output = 'ok';
 5274:             print $logfile "\nWrote metadata";
 5275:             close($mfh);
 5276:         } else {
 5277:             print $logfile "\nFailed to open metadata file";
 5278:             $output = &mt('Could not write metadata');
 5279:         }
 5280:     }
 5281:     return $output;
 5282: }
 5283: 
 5284: sub notifysubscribed {
 5285:     foreach my $targetsource (@{$modified_urls}){
 5286:         next unless (ref($targetsource) eq 'ARRAY');
 5287:         my ($target,$source)=@{$targetsource};
 5288:         if ($source ne '') {
 5289:             if (open(my $logfh,'>>'.$source.'.log')) {
 5290:                 print $logfh "\nCleanup phase: Notifications\n";
 5291:                 my @subscribed=&subscribed_hosts($target);
 5292:                 foreach my $subhost (@subscribed) {
 5293:                     print $logfh "\nNotifying host ".$subhost.':';
 5294:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5295:                     print $logfh $reply;
 5296:                 }
 5297:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5298:                 foreach my $subhost (@subscribedmeta) {
 5299:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5300:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5301:                                                         $subhost);
 5302:                     print $logfh $reply;
 5303:                 }
 5304:                 print $logfh "\n============ Done ============\n";
 5305:                 close($logfh);
 5306:             }
 5307:         }
 5308:     }
 5309:     return OK;
 5310: }
 5311: 
 5312: sub subscribed_hosts {
 5313:     my ($target) = @_;
 5314:     my @subscribed;
 5315:     if (open(my $fh,"<$target.subscription")) {
 5316:         while (my $subline=<$fh>) {
 5317:             if ($subline =~ /^($match_lonid):/) {
 5318:                 my $host = $1;
 5319:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5320:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5321:                         push(@subscribed,$host);
 5322:                     }
 5323:                 }
 5324:             }
 5325:         }
 5326:     }
 5327:     return @subscribed;
 5328: }
 5329: 
 5330: sub check_switchserver {
 5331:     my ($dom,$confname) = @_;
 5332:     my ($allowed,$switchserver);
 5333:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5334:     if ($home eq 'no_host') {
 5335:         $home = &Apache::lonnet::domain($dom,'primary');
 5336:     }
 5337:     my @ids=&Apache::lonnet::current_machine_ids();
 5338:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5339:     if (!$allowed) {
 5340: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&role=dc./'.$dom.'/">'.&mt('Switch Server').'</a>';
 5341:     }
 5342:     return $switchserver;
 5343: }
 5344: 
 5345: sub modify_quotas {
 5346:     my ($dom,$action,%domconfig) = @_;
 5347:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5348:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5349:     if ($action eq 'quotas') {
 5350:         $context = 'tools'; 
 5351:     } else {
 5352:         $context = $action;
 5353:     }
 5354:     if ($context eq 'requestcourses') {
 5355:         @usertools = ('official','unofficial','community');
 5356:         @options =('norequest','approval','validate','autolimit');
 5357:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5358:         %titles = &courserequest_titles();
 5359:         $toolregexp = join('|',@usertools);
 5360:         %conditions = &courserequest_conditions();
 5361:     } elsif ($context eq 'requestauthor') {
 5362:         @usertools = ('author');
 5363:         %titles = &authorrequest_titles();
 5364:     } else {
 5365:         @usertools = ('aboutme','blog','webdav','portfolio');
 5366:         %titles = &tool_titles();
 5367:     }
 5368:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5369:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5370:     foreach my $key (keys(%env)) {
 5371:         if ($context eq 'requestcourses') {
 5372:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5373:                 my $item = $1;
 5374:                 my $type = $2;
 5375:                 if ($type =~ /^limit_(.+)/) {
 5376:                     $limithash{$item}{$1} = $env{$key};
 5377:                 } else {
 5378:                     $confhash{$item}{$type} = $env{$key};
 5379:                 }
 5380:             }
 5381:         } elsif ($context eq 'requestauthor') {
 5382:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5383:                 $confhash{$1} = $env{$key};
 5384:             }
 5385:         } else {
 5386:             if ($key =~ /^form\.quota_(.+)$/) {
 5387:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5388:             }
 5389:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5390:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5391:             }
 5392:         }
 5393:     }
 5394:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5395:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5396:         @approvalnotify = sort(@approvalnotify);
 5397:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5398:         if (ref($domconfig{$action}) eq 'HASH') {
 5399:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5400:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5401:                     $changes{'notify'}{'approval'} = 1;
 5402:                 }
 5403:             } else {
 5404:                 if ($confhash{'notify'}{'approval'}) {
 5405:                     $changes{'notify'}{'approval'} = 1;
 5406:                 }
 5407:             }
 5408:         } else {
 5409:             if ($confhash{'notify'}{'approval'}) {
 5410:                 $changes{'notify'}{'approval'} = 1;
 5411:             }
 5412:         }
 5413:     } else {
 5414:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5415:     }
 5416:     foreach my $item (@usertools) {
 5417:         foreach my $type (@{$types},'default','_LC_adv') {
 5418:             my $unset; 
 5419:             if ($context eq 'requestcourses') {
 5420:                 $unset = '0';
 5421:                 if ($type eq '_LC_adv') {
 5422:                     $unset = '';
 5423:                 }
 5424:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5425:                     $confhash{$item}{$type} .= '=';
 5426:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5427:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5428:                     }
 5429:                 }
 5430:             } elsif ($context eq 'requestauthor') {
 5431:                 $unset = '0';
 5432:                 if ($type eq '_LC_adv') {
 5433:                     $unset = '';
 5434:                 }
 5435:             } else {
 5436:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5437:                     $confhash{$item}{$type} = 1;
 5438:                 } else {
 5439:                     $confhash{$item}{$type} = 0;
 5440:                 }
 5441:             }
 5442:             if (ref($domconfig{$action}) eq 'HASH') {
 5443:                 if ($action eq 'requestauthor') {
 5444:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5445:                         $changes{$type} = 1;
 5446:                     }
 5447:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5448:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5449:                         $changes{$item}{$type} = 1;
 5450:                     }
 5451:                 } else {
 5452:                     if ($context eq 'requestcourses') {
 5453:                         if ($confhash{$item}{$type} ne $unset) {
 5454:                             $changes{$item}{$type} = 1;
 5455:                         }
 5456:                     } else {
 5457:                         if (!$confhash{$item}{$type}) {
 5458:                             $changes{$item}{$type} = 1;
 5459:                         }
 5460:                     }
 5461:                 }
 5462:             } else {
 5463:                 if ($context eq 'requestcourses') {
 5464:                     if ($confhash{$item}{$type} ne $unset) {
 5465:                         $changes{$item}{$type} = 1;
 5466:                     }
 5467:                 } elsif ($context eq 'requestauthor') {
 5468:                     if ($confhash{$type} ne $unset) {
 5469:                         $changes{$type} = 1;
 5470:                     }
 5471:                 } else {
 5472:                     if (!$confhash{$item}{$type}) {
 5473:                         $changes{$item}{$type} = 1;
 5474:                     }
 5475:                 }
 5476:             }
 5477:         }
 5478:     }
 5479:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5480:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5481:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5482:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5483:                     if (exists($confhash{'defaultquota'}{$key})) {
 5484:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5485:                             $changes{'defaultquota'}{$key} = 1;
 5486:                         }
 5487:                     } else {
 5488:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5489:                     }
 5490:                 }
 5491:             } else {
 5492:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5493:                     if (exists($confhash{'defaultquota'}{$key})) {
 5494:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5495:                             $changes{'defaultquota'}{$key} = 1;
 5496:                         }
 5497:                     } else {
 5498:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5499:                     }
 5500:                 }
 5501:             }
 5502:         }
 5503:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5504:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5505:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5506:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5507:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5508:                             $changes{'defaultquota'}{$key} = 1;
 5509:                         }
 5510:                     } else {
 5511:                         if (!exists($domconfig{'quotas'}{$key})) {
 5512:                             $changes{'defaultquota'}{$key} = 1;
 5513:                         }
 5514:                     }
 5515:                 } else {
 5516:                     $changes{'defaultquota'}{$key} = 1;
 5517:                 }
 5518:             }
 5519:         }
 5520:     }
 5521: 
 5522:     if ($context eq 'requestauthor') {
 5523:         $domdefaults{'requestauthor'} = \%confhash;
 5524:     } else {
 5525:         foreach my $key (keys(%confhash)) {
 5526:             $domdefaults{$key} = $confhash{$key};
 5527:         }
 5528:     }
 5529: 
 5530:     my %quotahash = (
 5531:                       $action => { %confhash }
 5532:                     );
 5533:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5534:                                              $dom);
 5535:     if ($putresult eq 'ok') {
 5536:         if (keys(%changes) > 0) {
 5537:             my $cachetime = 24*60*60;
 5538:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5539: 
 5540:             $resulttext = &mt('Changes made:').'<ul>';
 5541:             unless (($context eq 'requestcourses') || 
 5542:                     ($context eq 'requestauthor')) {
 5543:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5544:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5545:                     foreach my $type (@{$types},'default') {
 5546:                         if (defined($changes{'defaultquota'}{$type})) {
 5547:                             my $typetitle = $usertypes->{$type};
 5548:                             if ($type eq 'default') {
 5549:                                 $typetitle = $othertitle;
 5550:                             }
 5551:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5552:                         }
 5553:                     }
 5554:                     $resulttext .= '</ul></li>';
 5555:                 }
 5556:             }
 5557:             my %newenv;
 5558:             foreach my $item (@usertools) {
 5559:                 my (%haschgs,%inconf);
 5560:                 if ($context eq 'requestauthor') {
 5561:                     %haschgs = %changes;
 5562:                     %inconf = %confhash; 
 5563:                 } else {
 5564:                     if (ref($changes{$item}) eq 'HASH') {
 5565:                         %haschgs = %{$changes{$item}};
 5566:                     }
 5567:                     if (ref($confhash{$item}) eq 'HASH') {
 5568:                         %inconf = %{$confhash{$item}};
 5569:                     }
 5570:                 }
 5571:                 if (keys(%haschgs) > 0) {
 5572:                     my $newacc = 
 5573:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5574:                                                           $env{'user.domain'},
 5575:                                                           $item,'reload',$context);
 5576:                     if (($context eq 'requestcourses') || 
 5577:                         ($context eq 'requestauthor')) {
 5578:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5579:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5580:                         }
 5581:                     } else {
 5582:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5583:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5584:                         }
 5585:                     }
 5586:                     unless ($context eq 'requestauthor') {
 5587:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5588:                     }
 5589:                     foreach my $type (@{$types},'default','_LC_adv') {
 5590:                         if ($haschgs{$type}) {
 5591:                             my $typetitle = $usertypes->{$type};
 5592:                             if ($type eq 'default') {
 5593:                                 $typetitle = $othertitle;
 5594:                             } elsif ($type eq '_LC_adv') {
 5595:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5596:                             }
 5597:                             if ($inconf{$type}) {
 5598:                                 if ($context eq 'requestcourses') {
 5599:                                     my $cond;
 5600:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 5601:                                         if ($1 eq '') {
 5602:                                             $cond = &mt('(Automatic processing of any request).');
 5603:                                         } else {
 5604:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5605:                                         }
 5606:                                     } else { 
 5607:                                         $cond = $conditions{$inconf{$type}};
 5608:                                     }
 5609:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5610:                                 } else {
 5611:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5612:                                 }
 5613:                             } else {
 5614:                                 if ($type eq '_LC_adv') {
 5615:                                     if ($inconf{$type} eq '0') {
 5616:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5617:                                     } else { 
 5618:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5619:                                     }
 5620:                                 } else {
 5621:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5622:                                 }
 5623:                             }
 5624:                         }
 5625:                     }
 5626:                     unless ($context eq 'requestauthor') {
 5627:                         $resulttext .= '</ul></li>';
 5628:                     }
 5629:                 }
 5630:             }
 5631:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 5632:                 if (ref($changes{'notify'}) eq 'HASH') {
 5633:                     if ($changes{'notify'}{'approval'}) {
 5634:                         if (ref($confhash{'notify'}) eq 'HASH') {
 5635:                             if ($confhash{'notify'}{'approval'}) {
 5636:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 5637:                             } else {
 5638:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 5639:                             }
 5640:                         }
 5641:                     }
 5642:                 }
 5643:             }
 5644:             $resulttext .= '</ul>';
 5645:             if (keys(%newenv)) {
 5646:                 &Apache::lonnet::appenv(\%newenv);
 5647:             }
 5648:         } else {
 5649:             if ($context eq 'requestcourses') {
 5650:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 5651:             } elsif ($context eq 'requestauthor') {
 5652:                 $resulttext = &mt('No changes made to rights to request author space.');
 5653:             } else {
 5654:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 5655:             }
 5656:         }
 5657:     } else {
 5658:         $resulttext = '<span class="LC_error">'.
 5659: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5660:     }
 5661:     return $resulttext;
 5662: }
 5663: 
 5664: sub modify_autoenroll {
 5665:     my ($dom,%domconfig) = @_;
 5666:     my ($resulttext,%changes);
 5667:     my %currautoenroll;
 5668:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 5669:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 5670:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 5671:         }
 5672:     }
 5673:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 5674:     my %title = ( run => 'Auto-enrollment active',
 5675:                   sender => 'Sender for notification messages',
 5676:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 5677:     my @offon = ('off','on');
 5678:     my $sender_uname = $env{'form.sender_uname'};
 5679:     my $sender_domain = $env{'form.sender_domain'};
 5680:     if ($sender_domain eq '') {
 5681:         $sender_uname = '';
 5682:     } elsif ($sender_uname eq '') {
 5683:         $sender_domain = '';
 5684:     }
 5685:     my $coowners = $env{'form.autoassign_coowners'};
 5686:     my %autoenrollhash =  (
 5687:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 5688:                                        'sender_uname' => $sender_uname,
 5689:                                        'sender_domain' => $sender_domain,
 5690:                                        'co-owners' => $coowners,
 5691:                                 }
 5692:                      );
 5693:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 5694:                                              $dom);
 5695:     if ($putresult eq 'ok') {
 5696:         if (exists($currautoenroll{'run'})) {
 5697:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 5698:                  $changes{'run'} = 1;
 5699:              }
 5700:         } elsif ($autorun) {
 5701:             if ($env{'form.autoenroll_run'} ne '1') {
 5702:                  $changes{'run'} = 1;
 5703:             }
 5704:         }
 5705:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 5706:             $changes{'sender'} = 1;
 5707:         }
 5708:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 5709:             $changes{'sender'} = 1;
 5710:         }
 5711:         if ($currautoenroll{'co-owners'} ne '') {
 5712:             if ($currautoenroll{'co-owners'} ne $coowners) {
 5713:                 $changes{'coowners'} = 1;
 5714:             }
 5715:         } elsif ($coowners) {
 5716:             $changes{'coowners'} = 1;
 5717:         }      
 5718:         if (keys(%changes) > 0) {
 5719:             $resulttext = &mt('Changes made:').'<ul>';
 5720:             if ($changes{'run'}) {
 5721:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 5722:             }
 5723:             if ($changes{'sender'}) {
 5724:                 if ($sender_uname eq '' || $sender_domain eq '') {
 5725:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 5726:                 } else {
 5727:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 5728:                 }
 5729:             }
 5730:             if ($changes{'coowners'}) {
 5731:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 5732:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 5733:             }
 5734:             $resulttext .= '</ul>';
 5735:         } else {
 5736:             $resulttext = &mt('No changes made to auto-enrollment settings');
 5737:         }
 5738:     } else {
 5739:         $resulttext = '<span class="LC_error">'.
 5740: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5741:     }
 5742:     return $resulttext;
 5743: }
 5744: 
 5745: sub modify_autoupdate {
 5746:     my ($dom,%domconfig) = @_;
 5747:     my ($resulttext,%currautoupdate,%fields,%changes);
 5748:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 5749:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 5750:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 5751:         }
 5752:     }
 5753:     my @offon = ('off','on');
 5754:     my %title = &Apache::lonlocal::texthash (
 5755:                    run => 'Auto-update:',
 5756:                    classlists => 'Updates to user information in classlists?'
 5757:                 );
 5758:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5759:     my %fieldtitles = &Apache::lonlocal::texthash (
 5760:                         id => 'Student/Employee ID',
 5761:                         permanentemail => 'E-mail address',
 5762:                         lastname => 'Last Name',
 5763:                         firstname => 'First Name',
 5764:                         middlename => 'Middle Name',
 5765:                         generation => 'Generation',
 5766:                       );
 5767:     $othertitle = &mt('All users');
 5768:     if (keys(%{$usertypes}) >  0) {
 5769:         $othertitle = &mt('Other users');
 5770:     }
 5771:     foreach my $key (keys(%env)) {
 5772:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 5773:             my ($usertype,$item) = ($1,$2);
 5774:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 5775:                 if ($usertype eq 'default') {   
 5776:                     push(@{$fields{$1}},$2);
 5777:                 } elsif (ref($types) eq 'ARRAY') {
 5778:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 5779:                         push(@{$fields{$1}},$2);
 5780:                     }
 5781:                 }
 5782:             }
 5783:         }
 5784:     }
 5785:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 5786:     @lockablenames = sort(@lockablenames);
 5787:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 5788:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 5789:         if (@changed) {
 5790:             $changes{'lockablenames'} = 1;
 5791:         }
 5792:     } else {
 5793:         if (@lockablenames) {
 5794:             $changes{'lockablenames'} = 1;
 5795:         }
 5796:     }
 5797:     my %updatehash = (
 5798:                       autoupdate => { run => $env{'form.autoupdate_run'},
 5799:                                       classlists => $env{'form.classlists'},
 5800:                                       fields => {%fields},
 5801:                                       lockablenames => \@lockablenames,
 5802:                                     }
 5803:                      );
 5804:     foreach my $key (keys(%currautoupdate)) {
 5805:         if (($key eq 'run') || ($key eq 'classlists')) {
 5806:             if (exists($updatehash{autoupdate}{$key})) {
 5807:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 5808:                     $changes{$key} = 1;
 5809:                 }
 5810:             }
 5811:         } elsif ($key eq 'fields') {
 5812:             if (ref($currautoupdate{$key}) eq 'HASH') {
 5813:                 foreach my $item (@{$types},'default') {
 5814:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 5815:                         my $change = 0;
 5816:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 5817:                             if (!exists($fields{$item})) {
 5818:                                 $change = 1;
 5819:                                 last;
 5820:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 5821:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 5822:                                     $change = 1;
 5823:                                     last;
 5824:                                 }
 5825:                             }
 5826:                         }
 5827:                         if ($change) {
 5828:                             push(@{$changes{$key}},$item);
 5829:                         }
 5830:                     } 
 5831:                 }
 5832:             }
 5833:         } elsif ($key eq 'lockablenames') {
 5834:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 5835:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 5836:                 if (@changed) {
 5837:                     $changes{'lockablenames'} = 1;
 5838:                 }
 5839:             } else {
 5840:                 if (@lockablenames) {
 5841:                     $changes{'lockablenames'} = 1;
 5842:                 }
 5843:             }
 5844:         }
 5845:     }
 5846:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 5847:         if (@lockablenames) {
 5848:             $changes{'lockablenames'} = 1;
 5849:         }
 5850:     }
 5851:     foreach my $item (@{$types},'default') {
 5852:         if (defined($fields{$item})) {
 5853:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 5854:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 5855:                     my $change = 0;
 5856:                     if (ref($fields{$item}) eq 'ARRAY') {
 5857:                         foreach my $type (@{$fields{$item}}) {
 5858:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 5859:                                 $change = 1;
 5860:                                 last;
 5861:                             }
 5862:                         }
 5863:                     }
 5864:                     if ($change) {
 5865:                         push(@{$changes{'fields'}},$item);
 5866:                     }
 5867:                 } else {
 5868:                     push(@{$changes{'fields'}},$item);
 5869:                 }
 5870:             } else {
 5871:                 push(@{$changes{'fields'}},$item);
 5872:             }
 5873:         }
 5874:     }
 5875:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 5876:                                              $dom);
 5877:     if ($putresult eq 'ok') {
 5878:         if (keys(%changes) > 0) {
 5879:             $resulttext = &mt('Changes made:').'<ul>';
 5880:             foreach my $key (sort(keys(%changes))) {
 5881:                 if ($key eq 'lockablenames') {
 5882:                     $resulttext .= '<li>';
 5883:                     if (@lockablenames) {
 5884:                         $usertypes->{'default'} = $othertitle;
 5885:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 5886:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 5887:                     } else {
 5888:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 5889:                     }
 5890:                     $resulttext .= '</li>';
 5891:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 5892:                     foreach my $item (@{$changes{$key}}) {
 5893:                         my @newvalues;
 5894:                         foreach my $type (@{$fields{$item}}) {
 5895:                             push(@newvalues,$fieldtitles{$type});
 5896:                         }
 5897:                         my $newvaluestr;
 5898:                         if (@newvalues > 0) {
 5899:                             $newvaluestr = join(', ',@newvalues);
 5900:                         } else {
 5901:                             $newvaluestr = &mt('none');
 5902:                         }
 5903:                         if ($item eq 'default') {
 5904:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 5905:                         } else {
 5906:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 5907:                         }
 5908:                     }
 5909:                 } else {
 5910:                     my $newvalue;
 5911:                     if ($key eq 'run') {
 5912:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 5913:                     } else {
 5914:                         $newvalue = $offon[$env{'form.'.$key}];
 5915:                     }
 5916:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 5917:                 }
 5918:             }
 5919:             $resulttext .= '</ul>';
 5920:         } else {
 5921:             $resulttext = &mt('No changes made to autoupdates');
 5922:         }
 5923:     } else {
 5924:         $resulttext = '<span class="LC_error">'.
 5925: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5926:     }
 5927:     return $resulttext;
 5928: }
 5929: 
 5930: sub modify_autocreate {
 5931:     my ($dom,%domconfig) = @_;
 5932:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 5933:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 5934:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 5935:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 5936:         }
 5937:     }
 5938:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 5939:                  req => 'Auto-creation of validated requests for official courses',
 5940:                  xmldc => 'Identity of course creator of courses from XML files',
 5941:                );
 5942:     my @types = ('xml','req');
 5943:     foreach my $item (@types) {
 5944:         $newvals{$item} = $env{'form.autocreate_'.$item};
 5945:         $newvals{$item} =~ s/\D//g;
 5946:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 5947:     }
 5948:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 5949:     my %domcoords = &get_active_dcs($dom);
 5950:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 5951:         $newvals{'xmldc'} = '';
 5952:     } 
 5953:     %autocreatehash =  (
 5954:                         autocreate => { xml => $newvals{'xml'},
 5955:                                         req => $newvals{'req'},
 5956:                                       }
 5957:                        );
 5958:     if ($newvals{'xmldc'} ne '') {
 5959:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 5960:     }
 5961:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 5962:                                              $dom);
 5963:     if ($putresult eq 'ok') {
 5964:         my @items = @types;
 5965:         if ($newvals{'xml'}) {
 5966:             push(@items,'xmldc');
 5967:         }
 5968:         foreach my $item (@items) {
 5969:             if (exists($currautocreate{$item})) {
 5970:                 if ($currautocreate{$item} ne $newvals{$item}) {
 5971:                     $changes{$item} = 1;
 5972:                 }
 5973:             } elsif ($newvals{$item}) {
 5974:                 $changes{$item} = 1;
 5975:             }
 5976:         }
 5977:         if (keys(%changes) > 0) {
 5978:             my @offon = ('off','on'); 
 5979:             $resulttext = &mt('Changes made:').'<ul>';
 5980:             foreach my $item (@types) {
 5981:                 if ($changes{$item}) {
 5982:                     my $newtxt = $offon[$newvals{$item}];
 5983:                     $resulttext .= '<li>'.&mt("$title{$item} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>';
 5984:                 }
 5985:             }
 5986:             if ($changes{'xmldc'}) {
 5987:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 5988:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 5989:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>'; 
 5990:             }
 5991:             $resulttext .= '</ul>';
 5992:         } else {
 5993:             $resulttext = &mt('No changes made to auto-creation settings');
 5994:         }
 5995:     } else {
 5996:         $resulttext = '<span class="LC_error">'.
 5997:             &mt('An error occurred: [_1]',$putresult).'</span>';
 5998:     }
 5999:     return $resulttext;
 6000: }
 6001: 
 6002: sub modify_directorysrch {
 6003:     my ($dom,%domconfig) = @_;
 6004:     my ($resulttext,%changes);
 6005:     my %currdirsrch;
 6006:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6007:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6008:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6009:         }
 6010:     }
 6011:     my %title = ( available => 'Directory search available',
 6012:                   localonly => 'Other domains can search',
 6013:                   searchby => 'Search types',
 6014:                   searchtypes => 'Search latitude');
 6015:     my @offon = ('off','on');
 6016:     my @otherdoms = ('Yes','No');
 6017: 
 6018:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6019:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6020:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6021: 
 6022:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6023:     if (keys(%{$usertypes}) == 0) {
 6024:         @cansearch = ('default');
 6025:     } else {
 6026:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6027:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6028:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6029:                     push(@{$changes{'cansearch'}},$type);
 6030:                 }
 6031:             }
 6032:             foreach my $type (@cansearch) {
 6033:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6034:                     push(@{$changes{'cansearch'}},$type);
 6035:                 }
 6036:             }
 6037:         } else {
 6038:             push(@{$changes{'cansearch'}},@cansearch);
 6039:         }
 6040:     }
 6041: 
 6042:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6043:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6044:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6045:                 push(@{$changes{'searchby'}},$by);
 6046:             }
 6047:         }
 6048:         foreach my $by (@searchby) {
 6049:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6050:                 push(@{$changes{'searchby'}},$by);
 6051:             }
 6052:         }
 6053:     } else {
 6054:         push(@{$changes{'searchby'}},@searchby);
 6055:     }
 6056: 
 6057:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6058:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6059:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6060:                 push(@{$changes{'searchtypes'}},$type);
 6061:             }
 6062:         }
 6063:         foreach my $type (@searchtypes) {
 6064:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6065:                 push(@{$changes{'searchtypes'}},$type);
 6066:             }
 6067:         }
 6068:     } else {
 6069:         if (exists($currdirsrch{'searchtypes'})) {
 6070:             foreach my $type (@searchtypes) {  
 6071:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6072:                     push(@{$changes{'searchtypes'}},$type);
 6073:                 }
 6074:             }
 6075:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6076:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6077:             }   
 6078:         } else {
 6079:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6080:         }
 6081:     }
 6082: 
 6083:     my %dirsrch_hash =  (
 6084:             directorysrch => { available => $env{'form.dirsrch_available'},
 6085:                                cansearch => \@cansearch,
 6086:                                localonly => $env{'form.dirsrch_localonly'},
 6087:                                searchby => \@searchby,
 6088:                                searchtypes => \@searchtypes,
 6089:                              }
 6090:             );
 6091:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6092:                                              $dom);
 6093:     if ($putresult eq 'ok') {
 6094:         if (exists($currdirsrch{'available'})) {
 6095:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6096:                  $changes{'available'} = 1;
 6097:              }
 6098:         } else {
 6099:             if ($env{'form.dirsrch_available'} eq '1') {
 6100:                 $changes{'available'} = 1;
 6101:             }
 6102:         }
 6103:         if (exists($currdirsrch{'localonly'})) {
 6104:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6105:                  $changes{'localonly'} = 1;
 6106:              }
 6107:         } else {
 6108:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6109:                 $changes{'localonly'} = 1;
 6110:             }
 6111:         }
 6112:         if (keys(%changes) > 0) {
 6113:             $resulttext = &mt('Changes made:').'<ul>';
 6114:             if ($changes{'available'}) {
 6115:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6116:             }
 6117:             if ($changes{'localonly'}) {
 6118:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6119:             }
 6120: 
 6121:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6122:                 my $chgtext;
 6123:                 if (ref($usertypes) eq 'HASH') {
 6124:                     if (keys(%{$usertypes}) > 0) {
 6125:                         foreach my $type (@{$types}) {
 6126:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6127:                                 $chgtext .= $usertypes->{$type}.'; ';
 6128:                             }
 6129:                         }
 6130:                         if (grep(/^default$/,@cansearch)) {
 6131:                             $chgtext .= $othertitle;
 6132:                         } else {
 6133:                             $chgtext =~ s/\; $//;
 6134:                         }
 6135:                         $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>';
 6136:                     }
 6137:                 }
 6138:             }
 6139:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6140:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6141:                 my $chgtext;
 6142:                 foreach my $type (@{$titleorder}) {
 6143:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6144:                         if (defined($searchtitles->{$type})) {
 6145:                             $chgtext .= $searchtitles->{$type}.'; ';
 6146:                         }
 6147:                     }
 6148:                 }
 6149:                 $chgtext =~ s/\; $//;
 6150:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6151:             }
 6152:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6153:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6154:                 my $chgtext;
 6155:                 foreach my $type (@{$srchtypeorder}) {
 6156:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6157:                         if (defined($srchtypes_desc->{$type})) {
 6158:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6159:                         }
 6160:                     }
 6161:                 }
 6162:                 $chgtext =~ s/\; $//;
 6163:                 $resulttext .= '<li>'.&mt("$title{'searchtypes'} set to: \"[_1]\"",$chgtext).'</li>';
 6164:             }
 6165:             $resulttext .= '</ul>';
 6166:         } else {
 6167:             $resulttext = &mt('No changes made to institution directory search settings');
 6168:         }
 6169:     } else {
 6170:         $resulttext = '<span class="LC_error">'.
 6171:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6172:     }
 6173:     return $resulttext;
 6174: }
 6175: 
 6176: sub modify_contacts {
 6177:     my ($dom,%domconfig) = @_;
 6178:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6179:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6180:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6181:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6182:         }
 6183:     }
 6184:     my (%others,%to,%bcc);
 6185:     my @contacts = ('supportemail','adminemail');
 6186:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6187:                     'requestsmail');
 6188:     foreach my $type (@mailings) {
 6189:         @{$newsetting{$type}} = 
 6190:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6191:         foreach my $item (@contacts) {
 6192:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6193:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6194:             } else {
 6195:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6196:             }
 6197:         }  
 6198:         $others{$type} = $env{'form.'.$type.'_others'};
 6199:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6200:         if ($type eq 'helpdeskmail') {
 6201:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6202:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6203:         }
 6204:     }
 6205:     foreach my $item (@contacts) {
 6206:         $to{$item} = $env{'form.'.$item};
 6207:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6208:     }
 6209:     if (keys(%currsetting) > 0) {
 6210:         foreach my $item (@contacts) {
 6211:             if ($to{$item} ne $currsetting{$item}) {
 6212:                 $changes{$item} = 1;
 6213:             }
 6214:         }
 6215:         foreach my $type (@mailings) {
 6216:             foreach my $item (@contacts) {
 6217:                 if (ref($currsetting{$type}) eq 'HASH') {
 6218:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6219:                         push(@{$changes{$type}},$item);
 6220:                     }
 6221:                 } else {
 6222:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6223:                 }
 6224:             }
 6225:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6226:                 push(@{$changes{$type}},'others');
 6227:             }
 6228:             if ($type eq 'helpdeskmail') {   
 6229:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6230:                     push(@{$changes{$type}},'bcc'); 
 6231:                 }
 6232:             }
 6233:         }
 6234:     } else {
 6235:         my %default;
 6236:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6237:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6238:         $default{'errormail'} = 'adminemail';
 6239:         $default{'packagesmail'} = 'adminemail';
 6240:         $default{'helpdeskmail'} = 'supportemail';
 6241:         $default{'lonstatusmail'} = 'adminemail';
 6242:         $default{'requestsmail'} = 'adminemail';
 6243:         foreach my $item (@contacts) {
 6244:            if ($to{$item} ne $default{$item}) {
 6245:               $changes{$item} = 1;
 6246:            } 
 6247:         }
 6248:         foreach my $type (@mailings) {
 6249:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6250:                
 6251:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6252:             }
 6253:             if ($others{$type} ne '') {
 6254:                 push(@{$changes{$type}},'others');
 6255:             }
 6256:             if ($type eq 'helpdeskmail') {
 6257:                 if ($bcc{$type} ne '') {
 6258:                     push(@{$changes{$type}},'bcc');
 6259:                 }
 6260:             }
 6261:         }
 6262:     }
 6263:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6264:                                              $dom);
 6265:     if ($putresult eq 'ok') {
 6266:         if (keys(%changes) > 0) {
 6267:             my ($titles,$short_titles)  = &contact_titles();
 6268:             $resulttext = &mt('Changes made:').'<ul>';
 6269:             foreach my $item (@contacts) {
 6270:                 if ($changes{$item}) {
 6271:                     $resulttext .= '<li>'.$titles->{$item}.
 6272:                                     &mt(' set to: ').
 6273:                                     '<span class="LC_cusr_emph">'.
 6274:                                     $to{$item}.'</span></li>';
 6275:                 }
 6276:             }
 6277:             foreach my $type (@mailings) {
 6278:                 if (ref($changes{$type}) eq 'ARRAY') {
 6279:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6280:                     my @text;
 6281:                     foreach my $item (@{$newsetting{$type}}) {
 6282:                         push(@text,$short_titles->{$item});
 6283:                     }
 6284:                     if ($others{$type} ne '') {
 6285:                         push(@text,$others{$type});
 6286:                     }
 6287:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6288:                                    join(', ',@text).'</span>';
 6289:                     if ($type eq 'helpdeskmail') {
 6290:                         if ($bcc{$type} ne '') {
 6291:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6292:                         }
 6293:                     }
 6294:                     $resulttext .= '</li>';
 6295:                 }
 6296:             }
 6297:             $resulttext .= '</ul>';
 6298:         } else {
 6299:             $resulttext = &mt('No changes made to contact information');
 6300:         }
 6301:     } else {
 6302:         $resulttext = '<span class="LC_error">'.
 6303:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6304:     }
 6305:     return $resulttext;
 6306: }
 6307: 
 6308: sub modify_usercreation {
 6309:     my ($dom,%domconfig) = @_;
 6310:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6311:     my $warningmsg;
 6312:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6313:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6314:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6315:         }
 6316:     }
 6317:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6318:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6319:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6320:     my @contexts = ('author','course','requestcrs','selfcreate');
 6321:     foreach my $item(@contexts) {
 6322:         if ($item eq 'selfcreate') {
 6323:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6324:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6325:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6326:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6327:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6328:                         $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.');   
 6329:                     }
 6330:                 }
 6331:             }
 6332:         } else {
 6333:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6334:         }
 6335:     }
 6336:     my ($othertitle,$usertypes,$types) = 
 6337:         &Apache::loncommon::sorted_inst_types($dom);
 6338:     if (ref($types) eq 'ARRAY') {
 6339:         if (@{$types} > 0) {
 6340:             @{$cancreate{'statustocreate'}} = 
 6341:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6342:         } else {
 6343:             @{$cancreate{'statustocreate'}} = ();
 6344:         }
 6345:         push(@contexts,'statustocreate');
 6346:     }
 6347:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6348:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6349:         foreach my $item (@contexts) {
 6350:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6351:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6352:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6353:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6354:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6355:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6356:                                     push(@{$changes{'cancreate'}},$item);
 6357:                                 }
 6358:                             }
 6359:                         }
 6360:                     }
 6361:                 } else {
 6362:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6363:                         if (@{$cancreate{$item}} > 0) {
 6364:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6365:                                 push(@{$changes{'cancreate'}},$item);
 6366:                             }
 6367:                         }
 6368:                     } else {
 6369:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6370:                             if (@{$cancreate{$item}} < 3) {
 6371:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6372:                                     push(@{$changes{'cancreate'}},$item);
 6373:                                 }
 6374:                             }
 6375:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6376:                             if (@{$cancreate{$item}} > 0) {
 6377:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6378:                                     push(@{$changes{'cancreate'}},$item);
 6379:                                 }
 6380:                             }
 6381:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6382:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6383:                                 push(@{$changes{'cancreate'}},$item);
 6384:                             }
 6385:                         }
 6386:                     }
 6387:                 }
 6388:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6389:                     foreach my $type (@{$cancreate{$item}}) {
 6390:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6391:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6392:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6393:                                     push(@{$changes{'cancreate'}},$item);
 6394:                                 }
 6395:                             }
 6396:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6397:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6398:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6399:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6400:                                     push(@{$changes{'cancreate'}},$item);
 6401:                                 }
 6402:                             }
 6403:                         }
 6404:                     }
 6405:                 }
 6406:             } else {
 6407:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6408:                     push(@{$changes{'cancreate'}},$item);
 6409:                 }
 6410:             }
 6411:         }
 6412:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6413:         foreach my $item (@contexts) {
 6414:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6415:                 if ($cancreate{$item} ne 'any') {
 6416:                     push(@{$changes{'cancreate'}},$item);
 6417:                 }
 6418:             } else {
 6419:                 if ($cancreate{$item} ne 'none') {
 6420:                     push(@{$changes{'cancreate'}},$item);
 6421:                 }
 6422:             }
 6423:         }
 6424:     } else {
 6425:         foreach my $item (@contexts)  {
 6426:             push(@{$changes{'cancreate'}},$item);
 6427:         }
 6428:     }
 6429: 
 6430:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6431:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6432:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6433:                 push(@{$changes{'username_rule'}},$type);
 6434:             }
 6435:         }
 6436:         foreach my $type (@username_rule) {
 6437:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6438:                 push(@{$changes{'username_rule'}},$type);
 6439:             }
 6440:         }
 6441:     } else {
 6442:         push(@{$changes{'username_rule'}},@username_rule);
 6443:     }
 6444: 
 6445:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6446:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 6447:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 6448:                 push(@{$changes{'id_rule'}},$type);
 6449:             }
 6450:         }
 6451:         foreach my $type (@id_rule) {
 6452:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 6453:                 push(@{$changes{'id_rule'}},$type);
 6454:             }
 6455:         }
 6456:     } else {
 6457:         push(@{$changes{'id_rule'}},@id_rule);
 6458:     }
 6459: 
 6460:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 6461:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 6462:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 6463:                 push(@{$changes{'email_rule'}},$type);
 6464:             }
 6465:         }
 6466:         foreach my $type (@email_rule) {
 6467:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 6468:                 push(@{$changes{'email_rule'}},$type);
 6469:             }
 6470:         }
 6471:     } else {
 6472:         push(@{$changes{'email_rule'}},@email_rule);
 6473:     }
 6474: 
 6475:     my @authen_contexts = ('author','course','domain');
 6476:     my @authtypes = ('int','krb4','krb5','loc');
 6477:     my %authhash;
 6478:     foreach my $item (@authen_contexts) {
 6479:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 6480:         foreach my $auth (@authtypes) {
 6481:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 6482:                 $authhash{$item}{$auth} = 1;
 6483:             } else {
 6484:                 $authhash{$item}{$auth} = 0;
 6485:             }
 6486:         }
 6487:     }
 6488:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 6489:         foreach my $item (@authen_contexts) {
 6490:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 6491:                 foreach my $auth (@authtypes) {
 6492:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 6493:                         push(@{$changes{'authtypes'}},$item);
 6494:                         last;
 6495:                     }
 6496:                 }
 6497:             }
 6498:         }
 6499:     } else {
 6500:         foreach my $item (@authen_contexts) {
 6501:             push(@{$changes{'authtypes'}},$item);
 6502:         }
 6503:     }
 6504: 
 6505:     my %usercreation_hash =  (
 6506:             usercreation => {
 6507:                               cancreate     => \%cancreate,
 6508:                               username_rule => \@username_rule,
 6509:                               id_rule       => \@id_rule,
 6510:                               email_rule    => \@email_rule,
 6511:                               authtypes     => \%authhash,
 6512:                             }
 6513:             );
 6514: 
 6515:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 6516:                                              $dom);
 6517: 
 6518:     my %selfcreatetypes = (
 6519:                              sso   => 'users authenticated by institutional single sign on',
 6520:                              login => 'users authenticated by institutional log-in',
 6521:                              email => 'users who provide a valid e-mail address for use as the username',
 6522:                           );
 6523:     if ($putresult eq 'ok') {
 6524:         if (keys(%changes) > 0) {
 6525:             $resulttext = &mt('Changes made:').'<ul>';
 6526:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 6527:                 my %lt = &usercreation_types();
 6528:                 foreach my $type (@{$changes{'cancreate'}}) {
 6529:                     my $chgtext;
 6530:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 6531:                         $chgtext = $lt{$type}.', ';
 6532:                     }
 6533:                     if ($type eq 'selfcreate') {
 6534:                         if (@{$cancreate{$type}} == 0) {
 6535:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 6536:                         } else {
 6537:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 6538:                             foreach my $case (@{$cancreate{$type}}) {
 6539:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 6540:                             }
 6541:                             $chgtext .= '</ul>';
 6542:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 6543:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 6544:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 6545:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 6546:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6547:                                         }
 6548:                                     }
 6549:                                 }
 6550:                             }
 6551:                         }
 6552:                     } elsif ($type eq 'statustocreate') {
 6553:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 6554:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 6555:                             if (@{$cancreate{'selfcreate'}} > 0) {
 6556:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 6557: 
 6558:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 6559:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 6560:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6561:                                     } 
 6562:                                 } elsif (ref($usertypes) eq 'HASH') {
 6563:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6564:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 6565:                                     } else {
 6566:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 6567:                                     }
 6568:                                     $chgtext .= '<ul>';
 6569:                                     foreach my $case (@{$cancreate{$type}}) {
 6570:                                         if ($case eq 'default') {
 6571:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 6572:                                         } else {
 6573:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 6574:                                         }
 6575:                                     }
 6576:                                     $chgtext .= '</ul>';
 6577:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6578:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 6579:                                     }
 6580:                                 }
 6581:                             } else {
 6582:                                 if (@{$cancreate{$type}} == 0) {
 6583:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 6584:                                 } else {
 6585:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 6586:                                 }
 6587:                             }
 6588:                         }
 6589:                     } elsif ($type eq 'captcha') {
 6590:                         if ($cancreate{$type} eq 'notused') {
 6591:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 6592:                         } else {
 6593:                             my %captchas = &captcha_phrases();
 6594:                             if ($captchas{$cancreate{$type}}) {
 6595:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 6596:                             } else {
 6597:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.'); 
 6598:                             }
 6599:                         }
 6600:                     } elsif ($type eq 'recaptchakeys') {
 6601:                         my ($privkey,$pubkey);
 6602:                         if (ref($cancreate{$type}) eq 'HASH') {
 6603:                             $pubkey = $cancreate{$type}{'public'};
 6604:                             $privkey = $cancreate{$type}{'private'};
 6605:                         }
 6606:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 6607:                         if (!$pubkey) {
 6608:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 6609:                         } else {
 6610:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6611:                         }
 6612:                         if (!$privkey) {
 6613:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 6614:                         } else {
 6615:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6616:                         }
 6617:                         $chgtext .= '</ul>';
 6618:                     } else {
 6619:                         if ($cancreate{$type} eq 'none') {
 6620:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 6621:                         } elsif ($cancreate{$type} eq 'any') {
 6622:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 6623:                         } elsif ($cancreate{$type} eq 'official') {
 6624:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 6625:                         } elsif ($cancreate{$type} eq 'unofficial') {
 6626:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 6627:                         }
 6628:                     }
 6629:                     $resulttext .= '<li>'.$chgtext.'</li>';
 6630:                 }
 6631:             }
 6632:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 6633:                 my ($rules,$ruleorder) = 
 6634:                     &Apache::lonnet::inst_userrules($dom,'username');
 6635:                 my $chgtext = '<ul>';
 6636:                 foreach my $type (@username_rule) {
 6637:                     if (ref($rules->{$type}) eq 'HASH') {
 6638:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 6639:                     }
 6640:                 }
 6641:                 $chgtext .= '</ul>';
 6642:                 if (@username_rule > 0) {
 6643:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 6644:                 } else {
 6645:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 6646:                 }
 6647:             }
 6648:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 6649:                 my ($idrules,$idruleorder) = 
 6650:                     &Apache::lonnet::inst_userrules($dom,'id');
 6651:                 my $chgtext = '<ul>';
 6652:                 foreach my $type (@id_rule) {
 6653:                     if (ref($idrules->{$type}) eq 'HASH') {
 6654:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 6655:                     }
 6656:                 }
 6657:                 $chgtext .= '</ul>';
 6658:                 if (@id_rule > 0) {
 6659:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 6660:                 } else {
 6661:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 6662:                 }
 6663:             }
 6664:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 6665:                 my ($emailrules,$emailruleorder) =
 6666:                     &Apache::lonnet::inst_userrules($dom,'email');
 6667:                 my $chgtext = '<ul>';
 6668:                 foreach my $type (@email_rule) {
 6669:                     if (ref($emailrules->{$type}) eq 'HASH') {
 6670:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 6671:                     }
 6672:                 }
 6673:                 $chgtext .= '</ul>';
 6674:                 if (@email_rule > 0) {
 6675:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 6676:                 } else {
 6677:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 6678:                 }
 6679:             }
 6680: 
 6681:             my %authname = &authtype_names();
 6682:             my %context_title = &context_names();
 6683:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 6684:                 my $chgtext = '<ul>';
 6685:                 foreach my $type (@{$changes{'authtypes'}}) {
 6686:                     my @allowed;
 6687:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 6688:                     foreach my $auth (@authtypes) {
 6689:                         if ($authhash{$type}{$auth}) {
 6690:                             push(@allowed,$authname{$auth});
 6691:                         }
 6692:                     }
 6693:                     if (@allowed > 0) {
 6694:                         $chgtext .= join(', ',@allowed).'</li>';
 6695:                     } else {
 6696:                         $chgtext .= &mt('none').'</li>';
 6697:                     }
 6698:                 }
 6699:                 $chgtext .= '</ul>';
 6700:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 6701:                 $resulttext .= '</li>';
 6702:             }
 6703:             $resulttext .= '</ul>';
 6704:         } else {
 6705:             $resulttext = &mt('No changes made to user creation settings');
 6706:         }
 6707:     } else {
 6708:         $resulttext = '<span class="LC_error">'.
 6709:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6710:     }
 6711:     if ($warningmsg ne '') {
 6712:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 6713:     }
 6714:     return $resulttext;
 6715: }
 6716: 
 6717: sub process_captcha {
 6718:     my ($container,$changes,$newsettings,$current) = @_;
 6719:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 6720:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 6721:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 6722:         $newsettings->{'captcha'} = 'original';
 6723:     }
 6724:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 6725:         if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 6726:             push(@{$changes->{'cancreate'}},'captcha');
 6727:         } elsif (!defined($changes->{'cancreate'})) {
 6728:             $changes->{'cancreate'} = ['captcha'];
 6729:         }
 6730:     }
 6731:     my ($newpub,$newpriv,$currpub,$currpriv);
 6732:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 6733:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 6734:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 6735:     }
 6736:     $newsettings->{'recaptchakeys'} = {
 6737:                                          public  => $newpub,
 6738:                                          private => $newpriv,
 6739:                                       };
 6740:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 6741:         $currpub = $current->{'recaptchakeys'}{'public'};
 6742:         $currpriv = $current->{'recaptchakeys'}{'private'};
 6743:     }
 6744:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 6745:         if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 6746:             push(@{$changes->{'cancreate'}},'recaptchakeys');
 6747:         } elsif (!defined($changes->{'cancreate'})) {
 6748:             $changes->{'cancreate'} = ['recaptchakeys'];
 6749:         }
 6750:     }
 6751:     return;
 6752: }
 6753: 
 6754: sub modify_usermodification {
 6755:     my ($dom,%domconfig) = @_;
 6756:     my ($resulttext,%curr_usermodification,%changes);
 6757:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 6758:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 6759:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 6760:         }
 6761:     }
 6762:     my @contexts = ('author','course','selfcreate');
 6763:     my %context_title = (
 6764:                            author => 'In author context',
 6765:                            course => 'In course context',
 6766:                            selfcreate => 'When self creating account', 
 6767:                         );
 6768:     my @fields = ('lastname','firstname','middlename','generation',
 6769:                   'permanentemail','id');
 6770:     my %roles = (
 6771:                   author => ['ca','aa'],
 6772:                   course => ['st','ep','ta','in','cr'],
 6773:                 );
 6774:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6775:     if (ref($types) eq 'ARRAY') {
 6776:         push(@{$types},'default');
 6777:         $usertypes->{'default'} = $othertitle;
 6778:     }
 6779:     $roles{'selfcreate'} = $types;  
 6780:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6781:     my %modifyhash;
 6782:     foreach my $context (@contexts) {
 6783:         foreach my $role (@{$roles{$context}}) {
 6784:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 6785:             foreach my $item (@fields) {
 6786:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 6787:                     $modifyhash{$context}{$role}{$item} = 1;
 6788:                 } else {
 6789:                     $modifyhash{$context}{$role}{$item} = 0;
 6790:                 }
 6791:             }
 6792:         }
 6793:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 6794:             foreach my $role (@{$roles{$context}}) {
 6795:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 6796:                     foreach my $field (@fields) {
 6797:                         if ($modifyhash{$context}{$role}{$field} ne 
 6798:                                 $curr_usermodification{$context}{$role}{$field}) {
 6799:                             push(@{$changes{$context}},$role);
 6800:                             last;
 6801:                         }
 6802:                     }
 6803:                 }
 6804:             }
 6805:         } else {
 6806:             foreach my $context (@contexts) {
 6807:                 foreach my $role (@{$roles{$context}}) {
 6808:                     push(@{$changes{$context}},$role);
 6809:                 }
 6810:             }
 6811:         }
 6812:     }
 6813:     my %usermodification_hash =  (
 6814:                                    usermodification => \%modifyhash,
 6815:                                  );
 6816:     my $putresult = &Apache::lonnet::put_dom('configuration',
 6817:                                              \%usermodification_hash,$dom);
 6818:     if ($putresult eq 'ok') {
 6819:         if (keys(%changes) > 0) {
 6820:             $resulttext = &mt('Changes made: ').'<ul>';
 6821:             foreach my $context (@contexts) {
 6822:                 if (ref($changes{$context}) eq 'ARRAY') {
 6823:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 6824:                     if (ref($changes{$context}) eq 'ARRAY') {
 6825:                         foreach my $role (@{$changes{$context}}) {
 6826:                             my $rolename;
 6827:                             if ($context eq 'selfcreate') {
 6828:                                 $rolename = $role;
 6829:                                 if (ref($usertypes) eq 'HASH') {
 6830:                                     if ($usertypes->{$role} ne '') {
 6831:                                         $rolename = $usertypes->{$role};
 6832:                                     }
 6833:                                 }
 6834:                             } else {
 6835:                                 if ($role eq 'cr') {
 6836:                                     $rolename = &mt('Custom');
 6837:                                 } else {
 6838:                                     $rolename = &Apache::lonnet::plaintext($role);
 6839:                                 }
 6840:                             }
 6841:                             my @modifiable;
 6842:                             if ($context eq 'selfcreate') {
 6843:                                 $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): ');
 6844:                             } else {
 6845:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 6846:                             }
 6847:                             foreach my $field (@fields) {
 6848:                                 if ($modifyhash{$context}{$role}{$field}) {
 6849:                                     push(@modifiable,$fieldtitles{$field});
 6850:                                 }
 6851:                             }
 6852:                             if (@modifiable > 0) {
 6853:                                 $resulttext .= join(', ',@modifiable);
 6854:                             } else {
 6855:                                 $resulttext .= &mt('none'); 
 6856:                             }
 6857:                             $resulttext .= '</li>';
 6858:                         }
 6859:                         $resulttext .= '</ul></li>';
 6860:                     }
 6861:                 }
 6862:             }
 6863:             $resulttext .= '</ul>';
 6864:         } else {
 6865:             $resulttext = &mt('No changes made to user modification settings');
 6866:         }
 6867:     } else {
 6868:         $resulttext = '<span class="LC_error">'.
 6869:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6870:     }
 6871:     return $resulttext;
 6872: }
 6873: 
 6874: sub modify_defaults {
 6875:     my ($dom,$r) = @_;
 6876:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 6877:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6878:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 6879:     my @authtypes = ('internal','krb4','krb5','localauth');
 6880:     foreach my $item (@items) {
 6881:         $newvalues{$item} = $env{'form.'.$item};
 6882:         if ($item eq 'auth_def') {
 6883:             if ($newvalues{$item} ne '') {
 6884:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 6885:                     push(@errors,$item);
 6886:                 }
 6887:             }
 6888:         } elsif ($item eq 'lang_def') {
 6889:             if ($newvalues{$item} ne '') {
 6890:                 if ($newvalues{$item} =~ /^(\w+)/) {
 6891:                     my $langcode = $1;
 6892:                     if ($langcode ne 'x_chef') {
 6893:                         if (code2language($langcode) eq '') {
 6894:                             push(@errors,$item);
 6895:                         }
 6896:                     }
 6897:                 } else {
 6898:                     push(@errors,$item);
 6899:                 }
 6900:             }
 6901:         } elsif ($item eq 'timezone_def') {
 6902:             if ($newvalues{$item} ne '') {
 6903:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 6904:                     push(@errors,$item);   
 6905:                 }
 6906:             }
 6907:         } elsif ($item eq 'datelocale_def') {
 6908:             if ($newvalues{$item} ne '') {
 6909:                 my @datelocale_ids = DateTime::Locale->ids();
 6910:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 6911:                     push(@errors,$item);
 6912:                 }
 6913:             }
 6914:         } elsif ($item eq 'portal_def') {
 6915:             if ($newvalues{$item} ne '') {
 6916:                 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])\/?$/) {
 6917:                     push(@errors,$item);
 6918:                 }
 6919:             }
 6920:         }
 6921:         if (grep(/^\Q$item\E$/,@errors)) {
 6922:             $newvalues{$item} = $domdefaults{$item};
 6923:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 6924:             $changes{$item} = 1;
 6925:         }
 6926:         $domdefaults{$item} = $newvalues{$item};
 6927:     }
 6928:     my %defaults_hash = (
 6929:                          defaults => \%newvalues,
 6930:                         );
 6931:     my $title = &defaults_titles();
 6932:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 6933:                                              $dom);
 6934:     if ($putresult eq 'ok') {
 6935:         if (keys(%changes) > 0) {
 6936:             $resulttext = &mt('Changes made:').'<ul>';
 6937:             my $version = $r->dir_config('lonVersion');
 6938:             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";
 6939:             foreach my $item (sort(keys(%changes))) {
 6940:                 my $value = $env{'form.'.$item};
 6941:                 if ($value eq '') {
 6942:                     $value = &mt('none');
 6943:                 } elsif ($item eq 'auth_def') {
 6944:                     my %authnames = &authtype_names();
 6945:                     my %shortauth = (
 6946:                              internal => 'int',
 6947:                              krb4 => 'krb4',
 6948:                              krb5 => 'krb5',
 6949:                              localauth  => 'loc',
 6950:                     );
 6951:                     $value = $authnames{$shortauth{$value}};
 6952:                 }
 6953:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 6954:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 6955:             }
 6956:             $resulttext .= '</ul>';
 6957:             $mailmsgtext .= "\n";
 6958:             my $cachetime = 24*60*60;
 6959:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6960:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 6961:                 my $sysmail = $r->dir_config('lonSysEMail');
 6962:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 6963:             }
 6964:         } else {
 6965:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 6966:         }
 6967:     } else {
 6968:         $resulttext = '<span class="LC_error">'.
 6969:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6970:     }
 6971:     if (@errors > 0) {
 6972:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 6973:         foreach my $item (@errors) {
 6974:             $resulttext .= ' "'.$title->{$item}.'",';
 6975:         }
 6976:         $resulttext =~ s/,$//;
 6977:     }
 6978:     return $resulttext;
 6979: }
 6980: 
 6981: sub modify_scantron {
 6982:     my ($r,$dom,$confname,%domconfig) = @_;
 6983:     my ($resulttext,%confhash,%changes,$errors);
 6984:     my $custom = 'custom.tab';
 6985:     my $default = 'default.tab';
 6986:     my $servadm = $r->dir_config('lonAdmEMail');
 6987:     my ($configuserok,$author_ok,$switchserver) = 
 6988:         &config_check($dom,$confname,$servadm);
 6989:     if ($env{'form.scantronformat.filename'} ne '') {
 6990:         my $error;
 6991:         if ($configuserok eq 'ok') {
 6992:             if ($switchserver) {
 6993:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 6994:             } else {
 6995:                 if ($author_ok eq 'ok') {
 6996:                     my ($result,$scantronurl) =
 6997:                         &publishlogo($r,'upload','scantronformat',$dom,
 6998:                                      $confname,'scantron','','',$custom);
 6999:                     if ($result eq 'ok') {
 7000:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7001:                         $changes{'scantronformat'} = 1;
 7002:                     } else {
 7003:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7004:                     }
 7005:                 } else {
 7006:                     $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);
 7007:                 }
 7008:             }
 7009:         } else {
 7010:             $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);
 7011:         }
 7012:         if ($error) {
 7013:             &Apache::lonnet::logthis($error);
 7014:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7015:         }
 7016:     }
 7017:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7018:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7019:             if ($env{'form.scantronformat_del'}) {
 7020:                 $confhash{'scantron'}{'scantronformat'} = '';
 7021:                 $changes{'scantronformat'} = 1;
 7022:             }
 7023:         }
 7024:     }
 7025:     if (keys(%confhash) > 0) {
 7026:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7027:                                                  $dom);
 7028:         if ($putresult eq 'ok') {
 7029:             if (keys(%changes) > 0) {
 7030:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7031:                     $resulttext = &mt('Changes made:').'<ul>';
 7032:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7033:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7034:                     } else {
 7035:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7036:                     }
 7037:                     $resulttext .= '</ul>';
 7038:                 } else {
 7039:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7040:                 }
 7041:                 $resulttext .= '</ul>';
 7042:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7043:             } else {
 7044:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7045:             }
 7046:         } else {
 7047:             $resulttext = '<span class="LC_error">'.
 7048:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7049:         }
 7050:     } else {
 7051:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7052:     }
 7053:     if ($errors) {
 7054:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7055:                        $errors.'</ul>';
 7056:     }
 7057:     return $resulttext;
 7058: }
 7059: 
 7060: sub modify_coursecategories {
 7061:     my ($dom,%domconfig) = @_;
 7062:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7063:         $cathash);
 7064:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7065:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7066:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7067:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7068:             $changes{'togglecats'} = 1;
 7069:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7070:         }
 7071:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7072:             $changes{'categorize'} = 1;
 7073:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7074:         }
 7075:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7076:             $changes{'togglecatscomm'} = 1;
 7077:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7078:         }
 7079:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7080:             $changes{'categorizecomm'} = 1;
 7081:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7082:         }
 7083:     } else {
 7084:         $changes{'togglecats'} = 1;
 7085:         $changes{'categorize'} = 1;
 7086:         $changes{'togglecatscomm'} = 1;
 7087:         $changes{'categorizecomm'} = 1;
 7088:         $domconfig{'coursecategories'} = {
 7089:                                              togglecats => $env{'form.togglecats'},
 7090:                                              categorize => $env{'form.categorize'},
 7091:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7092:                                              categorizecomm => $env{'form.categorizecomm'},
 7093:                                          };
 7094:     }
 7095:     if (ref($cathash) eq 'HASH') {
 7096:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7097:             push (@deletecategory,'instcode::0');
 7098:         }
 7099:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7100:             push(@deletecategory,'communities::0');
 7101:         }
 7102:     }
 7103:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7104:     if (ref($cathash) eq 'HASH') {
 7105:         if (@deletecategory > 0) {
 7106:             #FIXME Need to remove category from all courses using a deleted category 
 7107:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7108:             foreach my $item (@deletecategory) {
 7109:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7110:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7111:                     $deletions{$item} = 1;
 7112:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7113:                 }
 7114:             }
 7115:         }
 7116:         foreach my $item (keys(%{$cathash})) {
 7117:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7118:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7119:                 $reorderings{$item} = 1;
 7120:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7121:             }
 7122:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7123:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7124:                 my $newdepth = $depth+1;
 7125:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7126:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7127:                 $adds{$newitem} = 1; 
 7128:             }
 7129:             if ($env{'form.subcat_'.$item} ne '') {
 7130:                 my $newcat = $env{'form.subcat_'.$item};
 7131:                 my $newdepth = $depth+1;
 7132:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7133:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7134:                 $adds{$newitem} = 1;
 7135:             }
 7136:         }
 7137:     }
 7138:     if ($env{'form.instcode'} eq '1') {
 7139:         if (ref($cathash) eq 'HASH') {
 7140:             my $newitem = 'instcode::0';
 7141:             if ($cathash->{$newitem} eq '') {  
 7142:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7143:                 $adds{$newitem} = 1;
 7144:             }
 7145:         } else {
 7146:             my $newitem = 'instcode::0';
 7147:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7148:             $adds{$newitem} = 1;
 7149:         }
 7150:     }
 7151:     if ($env{'form.communities'} eq '1') {
 7152:         if (ref($cathash) eq 'HASH') {
 7153:             my $newitem = 'communities::0';
 7154:             if ($cathash->{$newitem} eq '') {
 7155:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7156:                 $adds{$newitem} = 1;
 7157:             }
 7158:         } else {
 7159:             my $newitem = 'communities::0';
 7160:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7161:             $adds{$newitem} = 1;
 7162:         }
 7163:     }
 7164:     if ($env{'form.addcategory_name'} ne '') {
 7165:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7166:             ($env{'form.addcategory_name'} ne 'communities')) {
 7167:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7168:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7169:             $adds{$newitem} = 1;
 7170:         }
 7171:     }
 7172:     my $putresult;
 7173:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7174:         if (keys(%deletions) > 0) {
 7175:             foreach my $key (keys(%deletions)) {
 7176:                 if ($predelallitems{$key} ne '') {
 7177:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7178:                 }
 7179:             }
 7180:         }
 7181:         my (@chkcats,@chktrails,%chkallitems);
 7182:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7183:         if (ref($chkcats[0]) eq 'ARRAY') {
 7184:             my $depth = 0;
 7185:             my $chg = 0;
 7186:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7187:                 my $name = $chkcats[0][$i];
 7188:                 my $item;
 7189:                 if ($name eq '') {
 7190:                     $chg ++;
 7191:                 } else {
 7192:                     $item = &escape($name).'::0';
 7193:                     if ($chg) {
 7194:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7195:                     }
 7196:                     $depth ++; 
 7197:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7198:                     $depth --;
 7199:                 }
 7200:             }
 7201:         }
 7202:     }
 7203:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7204:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7205:         if ($putresult eq 'ok') {
 7206:             my %title = (
 7207:                          togglecats     => 'Show/Hide a course in catalog',
 7208:                          categorize     => 'Assign a category to a course',
 7209:                          togglecatscomm => 'Show/Hide a community in catalog',
 7210:                          categorizecomm => 'Assign a category to a community',
 7211:                         );
 7212:             my %level = (
 7213:                          dom  => 'set in Domain ("Modify Course/Community")',
 7214:                          crs  => 'set in Course ("Course Configuration")',
 7215:                          comm => 'set in Community ("Community Configuration")',
 7216:                         );
 7217:             $resulttext = &mt('Changes made:').'<ul>';
 7218:             if ($changes{'togglecats'}) {
 7219:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7220:             }
 7221:             if ($changes{'categorize'}) {
 7222:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7223:             }
 7224:             if ($changes{'togglecatscomm'}) {
 7225:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7226:             }
 7227:             if ($changes{'categorizecomm'}) {
 7228:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7229:             }
 7230:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7231:                 my $cathash;
 7232:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7233:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7234:                 } else {
 7235:                     $cathash = {};
 7236:                 } 
 7237:                 my (@cats,@trails,%allitems);
 7238:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7239:                 if (keys(%deletions) > 0) {
 7240:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7241:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7242:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7243:                     }
 7244:                     $resulttext .= '</ul></li>';
 7245:                 }
 7246:                 if (keys(%reorderings) > 0) {
 7247:                     my %sort_by_trail;
 7248:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7249:                     foreach my $key (keys(%reorderings)) {
 7250:                         if ($allitems{$key} ne '') {
 7251:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7252:                         }
 7253:                     }
 7254:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7255:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7256:                     }
 7257:                     $resulttext .= '</ul></li>';
 7258:                 }
 7259:                 if (keys(%adds) > 0) {
 7260:                     my %sort_by_trail;
 7261:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7262:                     foreach my $key (keys(%adds)) {
 7263:                         if ($allitems{$key} ne '') {
 7264:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7265:                         }
 7266:                     }
 7267:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7268:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7269:                     }
 7270:                     $resulttext .= '</ul></li>';
 7271:                 }
 7272:             }
 7273:             $resulttext .= '</ul>';
 7274:         } else {
 7275:             $resulttext = '<span class="LC_error">'.
 7276:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7277:         }
 7278:     } else {
 7279:         $resulttext = &mt('No changes made to course and community categories');
 7280:     }
 7281:     return $resulttext;
 7282: }
 7283: 
 7284: sub modify_serverstatuses {
 7285:     my ($dom,%domconfig) = @_;
 7286:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7287:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7288:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7289:     }
 7290:     my @pages = &serverstatus_pages();
 7291:     foreach my $type (@pages) {
 7292:         $newserverstatus{$type}{'namedusers'} = '';
 7293:         $newserverstatus{$type}{'machines'} = '';
 7294:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7295:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7296:             my @okusers;
 7297:             foreach my $user (@users) {
 7298:                 my ($uname,$udom) = split(/:/,$user);
 7299:                 if (($udom =~ /^$match_domain$/) &&   
 7300:                     (&Apache::lonnet::domain($udom)) &&
 7301:                     ($uname =~ /^$match_username$/)) {
 7302:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7303:                         push(@okusers,$user);
 7304:                     }
 7305:                 }
 7306:             }
 7307:             if (@okusers > 0) {
 7308:                  @okusers = sort(@okusers);
 7309:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7310:             }
 7311:         }
 7312:         if (defined($env{'form.'.$type.'_machines'})) {
 7313:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7314:             my @okmachines;
 7315:             foreach my $ip (@machines) {
 7316:                 my @parts = split(/\./,$ip);
 7317:                 next if (@parts < 4);
 7318:                 my $badip = 0;
 7319:                 for (my $i=0; $i<4; $i++) {
 7320:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7321:                         $badip = 1;
 7322:                         last;
 7323:                     }
 7324:                 }
 7325:                 if (!$badip) {
 7326:                     push(@okmachines,$ip);     
 7327:                 }
 7328:             }
 7329:             @okmachines = sort(@okmachines);
 7330:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7331:         }
 7332:     }
 7333:     my %serverstatushash =  (
 7334:                                 serverstatuses => \%newserverstatus,
 7335:                             );
 7336:     foreach my $type (@pages) {
 7337:         foreach my $setting ('namedusers','machines') {
 7338:             my (@current,@new);
 7339:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7340:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7341:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7342:                 }
 7343:             }
 7344:             if ($newserverstatus{$type}{$setting} ne '') {
 7345:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7346:             }
 7347:             if (@current > 0) {
 7348:                 if (@new > 0) {
 7349:                     foreach my $item (@current) {
 7350:                         if (!grep(/^\Q$item\E$/,@new)) {
 7351:                             $changes{$type}{$setting} = 1;
 7352:                             last;
 7353:                         }
 7354:                     }
 7355:                     foreach my $item (@new) {
 7356:                         if (!grep(/^\Q$item\E$/,@current)) {
 7357:                             $changes{$type}{$setting} = 1;
 7358:                             last;
 7359:                         }
 7360:                     }
 7361:                 } else {
 7362:                     $changes{$type}{$setting} = 1;
 7363:                 }
 7364:             } elsif (@new > 0) {
 7365:                 $changes{$type}{$setting} = 1;
 7366:             }
 7367:         }
 7368:     }
 7369:     if (keys(%changes) > 0) {
 7370:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7371:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7372:                                                  \%serverstatushash,$dom);
 7373:         if ($putresult eq 'ok') {
 7374:             $resulttext .= &mt('Changes made:').'<ul>';
 7375:             foreach my $type (@pages) {
 7376:                 if (ref($changes{$type}) eq 'HASH') {
 7377:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7378:                     if ($changes{$type}{'namedusers'}) {
 7379:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7380:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7381:                         } else {
 7382:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7383:                         }
 7384:                     }
 7385:                     if ($changes{$type}{'machines'}) {
 7386:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7387:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7388:                         } else {
 7389:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7390:                         }
 7391: 
 7392:                     }
 7393:                     $resulttext .= '</ul></li>';
 7394:                 }
 7395:             }
 7396:             $resulttext .= '</ul>';
 7397:         } else {
 7398:             $resulttext = '<span class="LC_error">'.
 7399:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7400: 
 7401:         }
 7402:     } else {
 7403:         $resulttext = &mt('No changes made to access to server status pages');
 7404:     }
 7405:     return $resulttext;
 7406: }
 7407: 
 7408: sub modify_helpsettings {
 7409:     my ($r,$dom,$confname,%domconfig) = @_;
 7410:     my ($resulttext,$errors,%changes,%helphash);
 7411:     my $customhelpfile = $env{'form.loginhelpurl.filename'};
 7412:     my $defaulthelpfile = '/adm/loginproblems.html';
 7413:     my $defaulttext = &mt('Default in use');
 7414:     my $servadm = $r->dir_config('lonAdmEMail');
 7415:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7416:     my %defaultchecked = ('submitbugs' => 'on');
 7417:     my @offon = ('off','on');
 7418:     my %title = (
 7419:                     submitbugs    => 'Display link for users to submit a bug', 
 7420:                     loginhelpurl  => 'Unauthenticated login help page set to custom file'
 7421:                 );
 7422:     my @toggles = ('submitbugs');
 7423:     $helphash{'helpsettings'} = {};
 7424:     if (ref($domconfig{'helpsettings'}) ne 'HASH') {
 7425:         if ($domconfig{'helpsettings'} eq '') {
 7426:             $domconfig{'helpsettings'} = {};
 7427:         }
 7428:     }
 7429:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7430:         foreach my $item (@toggles) {
 7431:             if ($defaultchecked{$item} eq 'on') { 
 7432:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7433:                     if ($env{'form.'.$item} eq '0') {
 7434:                         $changes{$item} = 1;
 7435:                     }
 7436:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7437:                     $changes{$item} = 1;
 7438:                 }
 7439:             } elsif ($defaultchecked{$item} eq 'off') {
 7440:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7441:                     if ($env{'form.'.$item} eq '1') {
 7442:                         $changes{$item} = 1;
 7443:                     }
 7444:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7445:                     $changes{$item} = 1;
 7446:                 }
 7447:             }
 7448:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) { 
 7449:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7450:             }
 7451:         }
 7452:         if ($customhelpfile ne '') {
 7453:             my $error;
 7454:             if ($configuserok eq 'ok') {
 7455:                 if ($switchserver) {
 7456:                     $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7457:                 } else {
 7458:                     if ($author_ok eq 'ok') {
 7459:                         my ($result,$loginhelpurl) = &publishlogo($r,'upload','loginhelpurl',$dom,
 7460:                                                                   $confname,'help','','',$customhelpfile);
 7461:                         if ($result eq 'ok') {
 7462:                             $helphash{'helpsettings'}{'loginhelpurl'} = $loginhelpurl;
 7463:                             $changes{'loginhelpurl'} = 1;
 7464:                         } else {
 7465:                             $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customhelpfile,$result);
 7466:                         }
 7467:                     } else {
 7468:                         $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);
 7469:                     }
 7470:                 }
 7471:             } else {
 7472:                 $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);
 7473:             }
 7474:             if ($error) {
 7475:                 &Apache::lonnet::logthis($error);
 7476:                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7477:             }
 7478:         }
 7479:         if ($domconfig{'helpsettings'}{'loginhelpurl'} ne '') {
 7480:             if ($env{'form.loginhelpurl_del'}) {
 7481:                 $helphash{'helpsettings'}{'loginhelpurl'} = '';
 7482:                 $changes{'loginhelpurl'} = 1;
 7483:             }
 7484:         }
 7485:     }
 7486:     my $putresult;
 7487:     if (keys(%changes) > 0) {
 7488:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7489:     } else {
 7490:         $putresult = 'ok';
 7491:     }
 7492:     if ($putresult eq 'ok') {
 7493:         if (keys(%changes) > 0) {
 7494:             $resulttext = &mt('Changes made:').'<ul>';
 7495:             foreach my $item (sort(keys(%changes))) {
 7496:                 if ($item eq 'submitbugs') {
 7497:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 7498:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 7499:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 7500:                 }
 7501:                 if ($item eq 'loginhelpurl') {
 7502:                     if ($helphash{'helpsettings'}{'loginhelpurl'} eq '') {
 7503:                         $resulttext .= '<li>'.&mt('custom log-in help file removed.').'&nbsp;'.
 7504:                                               &Apache::loncommon::modal_link($defaulthelpfile,
 7505:                                               $defaulttext,600,500).'</li>';
 7506:                     } else {
 7507:                         $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 7508:                                                   $helphash{'helpsettings'}{'loginhelpurl'}.
 7509:                                                   '?inhibitmenu=yes',
 7510:                                                   &mt('custom log-in help file'),600,500).
 7511:                                        '</li>';
 7512:                     }
 7513:                 }
 7514:             }
 7515:             $resulttext .= '</ul>';
 7516:         } else {
 7517:             $resulttext = &mt('No changes made to help settings');
 7518:         }
 7519:     } else {
 7520:         $errors .= '<li><span class="LC_error">'.&mt('An error occurred storing the settings: [_1]',
 7521:                                                      $putresult).'</span></li>';
 7522:     }
 7523:     if ($errors) {
 7524:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7525:                        $errors.'</ul>';
 7526:     }
 7527:     return $resulttext;
 7528: }
 7529: 
 7530: sub modify_coursedefaults {
 7531:     my ($dom,%domconfig) = @_;
 7532:     my ($resulttext,$errors,%changes,%defaultshash);
 7533:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7534:     my @offon = ('off','on');
 7535:     my @toggles = ('canuse_pdfforms');
 7536: 
 7537:     $defaultshash{'coursedefaults'} = {};
 7538: 
 7539:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7540:         if ($domconfig{'coursedefaults'} eq '') {
 7541:             $domconfig{'coursedefaults'} = {};
 7542:         }
 7543:     }
 7544: 
 7545:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7546:         foreach my $item (@toggles) {
 7547:             if ($defaultchecked{$item} eq 'on') {
 7548:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7549:                     ($env{'form.'.$item} eq '0')) {
 7550:                     $changes{$item} = 1;
 7551:                 } elsif ($domconfig{'coursdefaults'}{$item} ne $env{'form.'.$item}) {
 7552:                     $changes{$item} = 1;
 7553:                 }
 7554:             } elsif ($defaultchecked{$item} eq 'off') {
 7555:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7556:                     ($env{'form.'.$item} eq '1')) {
 7557:                     $changes{$item} = 1;
 7558:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7559:                     $changes{$item} = 1;
 7560:                 }
 7561:             }
 7562:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7563:         }
 7564:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7565:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7566:         $newdefresponder =~ s/\D//g;
 7567:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7568:             $newdefresponder = 1;
 7569:         }
 7570:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7571:         if ($currdefresponder ne $newdefresponder) {
 7572:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7573:                 $changes{'anonsurvey_threshold'} = 1;
 7574:             }
 7575:         }
 7576:     }
 7577:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7578:                                              $dom);
 7579:     if ($putresult eq 'ok') {
 7580:         if (keys(%changes) > 0) {
 7581:             if ($changes{'canuse_pdfforms'}) {
 7582:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7583:                 $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7584:                 my $cachetime = 24*60*60;
 7585:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7586:             }
 7587:             $resulttext = &mt('Changes made:').'<ul>';
 7588:             foreach my $item (sort(keys(%changes))) {
 7589:                 if ($item eq 'canuse_pdfforms') {
 7590:                     if ($env{'form.'.$item} eq '1') {
 7591:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7592:                     } else {
 7593:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7594:                     }
 7595:                 } elsif ($item eq 'anonsurvey_threshold') {
 7596:                         $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7597:                 }
 7598:             }
 7599:             $resulttext .= '</ul>';
 7600:         } else {
 7601:             $resulttext = &mt('No changes made to course defaults');
 7602:         }
 7603:     } else {
 7604:         $resulttext = '<span class="LC_error">'.
 7605:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7606:     }
 7607:     return $resulttext;
 7608: }
 7609: 
 7610: sub modify_usersessions {
 7611:     my ($dom,%domconfig) = @_;
 7612:     my @hostingtypes = ('version','excludedomain','includedomain');
 7613:     my @offloadtypes = ('primary','default');
 7614:     my %types = (
 7615:                   remote => \@hostingtypes,
 7616:                   hosted => \@hostingtypes,
 7617:                   spares => \@offloadtypes,
 7618:                 );
 7619:     my @prefixes = ('remote','hosted','spares');
 7620:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7621:     my (%by_ip,%by_location,@intdoms);
 7622:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7623:     my @locations = sort(keys(%by_location));
 7624:     my (%defaultshash,%changes);
 7625:     foreach my $prefix (@prefixes) {
 7626:         $defaultshash{'usersessions'}{$prefix} = {};
 7627:     }
 7628:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7629:     my $resulttext;
 7630:     my %iphost = &Apache::lonnet::get_iphost();
 7631:     foreach my $prefix (@prefixes) {
 7632:         next if ($prefix eq 'spares');
 7633:         foreach my $type (@{$types{$prefix}}) {
 7634:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7635:             if ($type eq 'version') {
 7636:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7637:                 my $okvalue;
 7638:                 if ($value ne '') {
 7639:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 7640:                         $okvalue = $value;
 7641:                     }
 7642:                 }
 7643:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7644:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7645:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 7646:                             if ($inuse == 0) {
 7647:                                 $changes{$prefix}{$type} = 1;
 7648:                             } else {
 7649:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 7650:                                     $changes{$prefix}{$type} = 1;
 7651:                                 }
 7652:                                 if ($okvalue ne '') {
 7653:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7654:                                 } 
 7655:                             }
 7656:                         } else {
 7657:                             if (($inuse == 1) && ($okvalue ne '')) {
 7658:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7659:                                 $changes{$prefix}{$type} = 1;
 7660:                             }
 7661:                         }
 7662:                     } else {
 7663:                         if (($inuse == 1) && ($okvalue ne '')) {
 7664:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7665:                             $changes{$prefix}{$type} = 1;
 7666:                         }
 7667:                     }
 7668:                 } else {
 7669:                     if (($inuse == 1) && ($okvalue ne '')) {
 7670:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7671:                         $changes{$prefix}{$type} = 1;
 7672:                     }
 7673:                 }
 7674:             } else {
 7675:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 7676:                 my @okvals;
 7677:                 foreach my $val (@vals) {
 7678:                     if ($val =~ /:/) {
 7679:                         my @items = split(/:/,$val);
 7680:                         foreach my $item (@items) {
 7681:                             if (ref($by_location{$item}) eq 'ARRAY') {
 7682:                                 push(@okvals,$item);
 7683:                             }
 7684:                         }
 7685:                     } else {
 7686:                         if (ref($by_location{$val}) eq 'ARRAY') {
 7687:                             push(@okvals,$val);
 7688:                         }
 7689:                     }
 7690:                 }
 7691:                 @okvals = sort(@okvals);
 7692:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7693:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7694:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7695:                             if ($inuse == 0) {
 7696:                                 $changes{$prefix}{$type} = 1; 
 7697:                             } else {
 7698:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7699:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 7700:                                 if (@changed > 0) {
 7701:                                     $changes{$prefix}{$type} = 1;
 7702:                                 }
 7703:                             }
 7704:                         } else {
 7705:                             if ($inuse == 1) {
 7706:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7707:                                 $changes{$prefix}{$type} = 1;
 7708:                             }
 7709:                         } 
 7710:                     } else {
 7711:                         if ($inuse == 1) {
 7712:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7713:                             $changes{$prefix}{$type} = 1;
 7714:                         }
 7715:                     }
 7716:                 } else {
 7717:                     if ($inuse == 1) {
 7718:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7719:                         $changes{$prefix}{$type} = 1;
 7720:                     }
 7721:                 }
 7722:             }
 7723:         }
 7724:     }
 7725: 
 7726:     my @alldoms = &Apache::lonnet::all_domains();
 7727:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7728:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 7729:     my $savespares;
 7730: 
 7731:     foreach my $lonhost (sort(keys(%servers))) {
 7732:         my $serverhomeID =
 7733:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 7734:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 7735:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 7736:         my %spareschg;
 7737:         foreach my $type (@{$types{'spares'}}) {
 7738:             my @okspares;
 7739:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 7740:             foreach my $server (@checked) {
 7741:                 if (&Apache::lonnet::hostname($server) ne '') {
 7742:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 7743:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 7744:                             push(@okspares,$server);
 7745:                         }
 7746:                     }
 7747:                 }
 7748:             }
 7749:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 7750:             my $newspare;
 7751:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 7752:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 7753:                     $newspare = $new;
 7754:                 }
 7755:             }
 7756:             my @spares;
 7757:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 7758:                 @spares = sort(@okspares,$newspare);
 7759:             } else {
 7760:                 @spares = sort(@okspares);
 7761:             }
 7762:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 7763:             if (ref($spareid{$lonhost}) eq 'HASH') {
 7764:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 7765:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 7766:                     if (@diffs > 0) {
 7767:                         $spareschg{$type} = 1;
 7768:                     }
 7769:                 }
 7770:             }
 7771:         }
 7772:         if (keys(%spareschg) > 0) {
 7773:             $changes{'spares'}{$lonhost} = \%spareschg;
 7774:         }
 7775:     }
 7776: 
 7777:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7778:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 7779:             if (ref($changes{'spares'}) eq 'HASH') {
 7780:                 if (keys(%{$changes{'spares'}}) > 0) {
 7781:                     $savespares = 1;
 7782:                 }
 7783:             }
 7784:         } else {
 7785:             $savespares = 1;
 7786:         }
 7787:     }
 7788: 
 7789:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 7790:     if ((keys(%changes) > 0) || ($savespares)) {
 7791:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7792:                                                  $dom);
 7793:         if ($putresult eq 'ok') {
 7794:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 7795:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 7796:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 7797:                 }
 7798:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 7799:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 7800:                 }
 7801:             }
 7802:             my $cachetime = 24*60*60;
 7803:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7804:             if (keys(%changes) > 0) {
 7805:                 my %lt = &usersession_titles();
 7806:                 $resulttext = &mt('Changes made:').'<ul>';
 7807:                 foreach my $prefix (@prefixes) {
 7808:                     if (ref($changes{$prefix}) eq 'HASH') {
 7809:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 7810:                         if ($prefix eq 'spares') {
 7811:                             if (ref($changes{$prefix}) eq 'HASH') {
 7812:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 7813:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 7814:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 7815:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 7816:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 7817:                                         foreach my $type (@{$types{$prefix}}) {
 7818:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 7819:                                                 my $offloadto = &mt('None');
 7820:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 7821:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 7822:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 7823:                                                     }
 7824:                                                 }
 7825:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 7826:                                             }
 7827:                                         }
 7828:                                     }
 7829:                                     $resulttext .= '</li>';
 7830:                                 }
 7831:                             }
 7832:                         } else {
 7833:                             foreach my $type (@{$types{$prefix}}) {
 7834:                                 if (defined($changes{$prefix}{$type})) {
 7835:                                     my $newvalue;
 7836:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 7837:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 7838:                                             if ($type eq 'version') {
 7839:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 7840:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7841:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 7842:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 7843:                                                 }
 7844:                                             }
 7845:                                         }
 7846:                                     }
 7847:                                     if ($newvalue eq '') {
 7848:                                         if ($type eq 'version') {
 7849:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 7850:                                         } else {
 7851:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 7852:                                         }
 7853:                                     } else {
 7854:                                         if ($type eq 'version') {
 7855:                                             $newvalue .= ' '.&mt('(or later)'); 
 7856:                                         }
 7857:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 7858:                                     }
 7859:                                 }
 7860:                             }
 7861:                         }
 7862:                         $resulttext .= '</ul>';
 7863:                     }
 7864:                 }
 7865:                 $resulttext .= '</ul>';
 7866:             } else {
 7867:                 $resulttext = $nochgmsg;
 7868:             }
 7869:         } else {
 7870:             $resulttext = '<span class="LC_error">'.
 7871:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7872:         }
 7873:     } else {
 7874:         $resulttext = $nochgmsg;
 7875:     }
 7876:     return $resulttext;
 7877: }
 7878: 
 7879: sub modify_loadbalancing {
 7880:     my ($dom,%domconfig) = @_;
 7881:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7882:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7883:     my ($othertitle,$usertypes,$types) =
 7884:         &Apache::loncommon::sorted_inst_types($dom);
 7885:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7886:     my @sparestypes = ('primary','default');
 7887:     my %typetitles = &sparestype_titles();
 7888:     my $resulttext;
 7889:     if (keys(%servers) > 1) {
 7890:         my ($currbalancer,$currtargets,$currrules);
 7891:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 7892:             $currbalancer = $domconfig{'loadbalancing'}{'lonhost'};
 7893:             $currtargets = $domconfig{'loadbalancing'}{'targets'};
 7894:             $currrules = $domconfig{'loadbalancing'}{'rules'};
 7895:         } else {
 7896:             ($currbalancer,$currtargets) = 
 7897:                 &Apache::lonnet::get_lonbalancer_config(\%servers);
 7898:         }
 7899:         my ($saveloadbalancing,%defaultshash,%changes);
 7900:         my ($alltypes,$othertypes,$titles) =
 7901:             &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 7902:         my %ruletitles = &offloadtype_text();
 7903:         my $balancer = $env{'form.loadbalancing_lonhost'};
 7904:         if (!$servers{$balancer}) {
 7905:             undef($balancer);
 7906:         }
 7907:         if ($currbalancer ne $balancer) {
 7908:             $changes{'lonhost'} = 1;
 7909:         }
 7910:         $defaultshash{'loadbalancing'}{'lonhost'} = $balancer;
 7911:         if ($balancer ne '') {
 7912:             unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 7913:                 $saveloadbalancing = 1;
 7914:             }
 7915:             foreach my $sparetype (@sparestypes) {
 7916:                 my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$sparetype);
 7917:                 my @offloadto;
 7918:                 foreach my $target (@targets) {
 7919:                     if (($servers{$target}) && ($target ne $balancer)) {
 7920:                         if ($sparetype eq 'default') {
 7921:                             if (ref($defaultshash{'loadbalancing'}{'targets'}{'primary'}) eq 'ARRAY') {
 7922:                                 next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{'targets'}{'primary'}}));
 7923:                             }
 7924:                         }
 7925:                         unless(grep(/^\Q$target\E$/,@offloadto)) {
 7926:                             push(@offloadto,$target);
 7927:                         }
 7928:                     }
 7929:                     $defaultshash{'loadbalancing'}{'targets'}{$sparetype} = \@offloadto;
 7930:                 }
 7931:             }
 7932:         } else {
 7933:             foreach my $sparetype (@sparestypes) {
 7934:                 $defaultshash{'loadbalancing'}{'targets'}{$sparetype} = [];
 7935:             }
 7936:         }
 7937:         if (ref($currtargets) eq 'HASH') {
 7938:             foreach my $sparetype (@sparestypes) {
 7939:                 if (ref($currtargets->{$sparetype}) eq 'ARRAY') {
 7940:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets->{$sparetype},$defaultshash{'loadbalancing'}{'targets'}{$sparetype});
 7941:                     if (@targetdiffs > 0) {
 7942:                         $changes{'targets'} = 1;
 7943:                     }
 7944:                 } elsif (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 7945:                     if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 7946:                         $changes{'targets'} = 1;
 7947:                     }
 7948:                 }
 7949:             }
 7950:         } else {
 7951:             foreach my $sparetype (@sparestypes) {
 7952:                 if (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 7953:                     if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 7954:                         $changes{'targets'} = 1;  
 7955:                     }
 7956:                 }
 7957:             }  
 7958:         }
 7959:         my $ishomedom;
 7960:         if ($balancer ne '') {
 7961:             if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 7962:                 $ishomedom = 1;
 7963:             }
 7964:         }
 7965:         if (ref($alltypes) eq 'ARRAY') {
 7966:             foreach my $type (@{$alltypes}) {
 7967:                 my $rule;
 7968:                 if ($balancer ne '') {
 7969:                     unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) && 
 7970:                          (!$ishomedom)) {
 7971:                         $rule = $env{'form.loadbalancing_rules_'.$type};
 7972:                     }
 7973:                     if ($rule eq 'specific') {
 7974:                         $rule = $env{'form.loadbalancing_singleserver_'.$type};
 7975:                     }
 7976:                 }
 7977:                 $defaultshash{'loadbalancing'}{'rules'}{$type} = $rule;
 7978:                 if (ref($currrules) eq 'HASH') {
 7979:                     if ($rule ne $currrules->{$type}) {
 7980:                         $changes{'rules'}{$type} = 1;
 7981:                     }
 7982:                 } elsif ($rule ne '') {
 7983:                     $changes{'rules'}{$type} = 1;
 7984:                 }
 7985:             }
 7986:         }
 7987:         my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 7988:         if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 7989:             my $putresult = &Apache::lonnet::put_dom('configuration',
 7990:                                                      \%defaultshash,$dom);
 7991:             if ($putresult eq 'ok') {
 7992:                 if (keys(%changes) > 0) {
 7993:                     if ($changes{'lonhost'}) {
 7994:                         if ($currbalancer ne '') {
 7995:                             &Apache::lonnet::remote_devalidate_cache($currbalancer,'loadbalancing',$dom);
 7996:                         }
 7997:                         if ($balancer eq '') {
 7998:                             $resulttext .= '<li>'.&mt('Load Balancing with dedicated server discontinued').'</li>'; 
 7999:                         } else {
 8000:                             &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8001:                             $resulttext .= '<li>'.&mt('Dedicated Load Balancer server set to [_1]',$balancer);
 8002:                         }
 8003:                     } else {
 8004:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8005:                     }
 8006:                     if (($changes{'targets'}) && ($balancer ne '')) {
 8007:                         my %offloadstr;
 8008:                         foreach my $sparetype (@sparestypes) {
 8009:                             if (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 8010:                                 if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 8011:                                     $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}});
 8012:                                 }
 8013:                             }
 8014:                         }
 8015:                         if (keys(%offloadstr) == 0) {
 8016:                             $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8017:                         } else {
 8018:                             my $showoffload;
 8019:                             foreach my $sparetype (@sparestypes) {
 8020:                                 $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8021:                                 if (defined($offloadstr{$sparetype})) {
 8022:                                     $showoffload .= $offloadstr{$sparetype};
 8023:                                 } else {
 8024:                                     $showoffload .= &mt('None');
 8025:                                 }
 8026:                                 $showoffload .= ('&nbsp;'x3);
 8027:                             }
 8028:                             $resulttext .= '<li>'.&mt('By default, Load Balancer server set to offload to: [_1]',$showoffload).'</li>';
 8029:                         }
 8030:                     }
 8031:                     if ((ref($changes{'rules'}) eq 'HASH') && ($balancer ne '')) {
 8032:                         if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8033:                             foreach my $type (@{$alltypes}) {
 8034:                                 if ($changes{'rules'}{$type}) {
 8035:                                     my $rule = $defaultshash{'loadbalancing'}{'rules'}{$type};
 8036:                                     my $balancetext;
 8037:                                     if ($rule eq '') {
 8038:                                         $balancetext =  $ruletitles{'default'};
 8039:                                     } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 8040:                                         $balancetext =  $ruletitles{$rule};
 8041:                                     } else {
 8042:                                         $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{'rules'}{$type});
 8043:                                     }
 8044:                                     $resulttext .= '<li>'.&mt('Load Balancing for [_1] set to: [_2]',$titles->{$type},$balancetext).'</li>';     
 8045:                                 }
 8046:                             }
 8047:                         }
 8048:                     }
 8049:                     if ($resulttext ne '') {
 8050:                         $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8051:                     } else {
 8052:                         $resulttext = $nochgmsg;
 8053:                     }
 8054:                 } else {
 8055:                     $resulttext = $nochgmsg;
 8056:                     if ($balancer ne '') {
 8057:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8058:                     }
 8059:                 }
 8060:             } else {
 8061:                 $resulttext = '<span class="LC_error">'.
 8062:                               &mt('An error occurred: [_1]',$putresult).'</span>';
 8063:             }
 8064:         } else {
 8065:             $resulttext = $nochgmsg;
 8066:         }
 8067:     } else {
 8068:         $resulttext =  &mt('Load Balancing unavailable as this domain only has one server.');
 8069:     }
 8070:     return $resulttext;
 8071: }
 8072: 
 8073: sub recurse_check {
 8074:     my ($chkcats,$categories,$depth,$name) = @_;
 8075:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8076:         my $chg = 0;
 8077:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8078:             my $category = $chkcats->[$depth]{$name}[$j];
 8079:             my $item;
 8080:             if ($category eq '') {
 8081:                 $chg ++;
 8082:             } else {
 8083:                 my $deeper = $depth + 1;
 8084:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8085:                 if ($chg) {
 8086:                     $categories->{$item} -= $chg;
 8087:                 }
 8088:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8089:                 $deeper --;
 8090:             }
 8091:         }
 8092:     }
 8093:     return;
 8094: }
 8095: 
 8096: sub recurse_cat_deletes {
 8097:     my ($item,$coursecategories,$deletions) = @_;
 8098:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8099:     my $subdepth = $depth + 1;
 8100:     if (ref($coursecategories) eq 'HASH') {
 8101:         foreach my $subitem (keys(%{$coursecategories})) {
 8102:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8103:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8104:                 delete($coursecategories->{$subitem});
 8105:                 $deletions->{$subitem} = 1;
 8106:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8107:             }  
 8108:         }
 8109:     }
 8110:     return;
 8111: }
 8112: 
 8113: sub get_active_dcs {
 8114:     my ($dom) = @_;
 8115:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc']);
 8116:     my %domcoords;
 8117:     my $numdcs = 0;
 8118:     my $now = time;
 8119:     foreach my $server (keys(%dompersonnel)) {
 8120:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8121:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8122:             my ($end,$start) = split(':',$dompersonnel{$server}{$user});
 8123:             if (($end eq '') || ($end == 0) || ($end > $now)) {
 8124:                 if ($start <= $now) {
 8125:                     $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8126:                 }
 8127:             }
 8128:         }
 8129:     }
 8130:     return %domcoords;
 8131: }
 8132: 
 8133: sub active_dc_picker {
 8134:     my ($dom,$curr_dc) = @_;
 8135:     my %domcoords = &get_active_dcs($dom); 
 8136:     my @dcs = sort(keys(%domcoords));
 8137:     my $numdcs = scalar(@dcs); 
 8138:     my $datatable;
 8139:     my $numinrow = 2;
 8140:     if ($numdcs > 1) {
 8141:         $datatable = '<table>';
 8142:         for (my $i=0; $i<@dcs; $i++) {
 8143:             my $rem = $i%($numinrow);
 8144:             if ($rem == 0) {
 8145:                 if ($i > 0) {
 8146:                     $datatable .= '</tr>';
 8147:                 }
 8148:                 $datatable .= '<tr>';
 8149:             }
 8150:             my $check = ' ';
 8151:             if ($curr_dc eq '') {
 8152:                 if (!$i) { 
 8153:                     $check = ' checked="checked" ';
 8154:                 }
 8155:             } elsif ($dcs[$i] eq $curr_dc) {
 8156:                 $check = ' checked="checked" ';
 8157:             }
 8158:             if ($i == @dcs - 1) {
 8159:                 my $colsleft = $numinrow - $rem;
 8160:                 if ($colsleft > 1) {
 8161:                     $datatable .= '<td colspan="'.$colsleft.'">';
 8162:                 } else {
 8163:                     $datatable .= '<td>';
 8164:                 }
 8165:             } else {
 8166:                 $datatable .= '<td>';
 8167:             }
 8168:             my ($dcname,$dcdom) = split(':',$dcs[$i]);
 8169:             $datatable .= '<span class="LC_nobreak"><label>'.
 8170:                           '<input type="radio" name="autocreate_xmldc"'.
 8171:                           ' value="'.$dcs[$i].'"'.$check.'/>'.
 8172:                           &Apache::loncommon::plainname($dcname,$dcdom).
 8173:                           '</label></span></td>';
 8174:         }
 8175:         $datatable .= '</tr></table>';
 8176:     } elsif (@dcs) {
 8177:         $datatable .= '<input type="hidden" name="autocreate_dc" value="'.
 8178:                       $dcs[0].'" />';
 8179:     }
 8180:     return ($numdcs,$datatable);
 8181: }
 8182: 
 8183: sub usersession_titles {
 8184:     return &Apache::lonlocal::texthash(
 8185:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8186:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8187:                spares => 'Servers offloaded to, when busy',
 8188:                version => 'LON-CAPA version requirement',
 8189:                excludedomain => 'Allow all, but exclude specific domains',
 8190:                includedomain => 'Deny all, but include specific domains',
 8191:                primary => 'Primary (checked first)',
 8192:                default => 'Default',
 8193:            );
 8194: }
 8195: 
 8196: sub id_for_thisdom {
 8197:     my (%servers) = @_;
 8198:     my %altids;
 8199:     foreach my $server (keys(%servers)) {
 8200:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8201:         if ($serverhome ne $server) {
 8202:             $altids{$serverhome} = $server;
 8203:         }
 8204:     }
 8205:     return %altids;
 8206: }
 8207: 
 8208: sub count_servers {
 8209:     my ($currbalancer,%servers) = @_;
 8210:     my (@spares,$numspares);
 8211:     foreach my $lonhost (sort(keys(%servers))) {
 8212:         next if ($currbalancer eq $lonhost);
 8213:         push(@spares,$lonhost);
 8214:     }
 8215:     if ($currbalancer) {
 8216:         $numspares = scalar(@spares);
 8217:     } else {
 8218:         $numspares = scalar(@spares) - 1;
 8219:     }
 8220:     return ($numspares,@spares);
 8221: }
 8222: 
 8223: sub lonbalance_targets_js {
 8224:     my ($dom,$types,$servers) = @_;
 8225:     my $select = &mt('Select');
 8226:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8227:     if (ref($servers) eq 'HASH') {
 8228:         $alltargets = join("','",sort(keys(%{$servers})));
 8229:         my @homedoms;
 8230:         foreach my $server (sort(keys(%{$servers}))) {
 8231:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8232:                 push(@homedoms,'1');
 8233:             } else {
 8234:                 push(@homedoms,'0');
 8235:             }
 8236:         }
 8237:         $allishome = join("','",@homedoms);
 8238:     }
 8239:     if (ref($types) eq 'ARRAY') {
 8240:         if (@{$types} > 0) {
 8241:             @alltypes = @{$types};
 8242:         }
 8243:     }
 8244:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8245:     $allinsttypes = join("','",@alltypes);
 8246:     return <<"END";
 8247: 
 8248: <script type="text/javascript">
 8249: // <![CDATA[
 8250: 
 8251: function toggleTargets() {
 8252:     var balancer = document.display.loadbalancing_lonhost.options[document.display.loadbalancing_lonhost.selectedIndex].value;
 8253:     if (balancer == '') {
 8254:         hideSpares();
 8255:     } else {
 8256:         var homedoms = new Array('$allishome');
 8257:         var ishomedom = homedoms[document.display.loadbalancing_lonhost.selectedIndex];
 8258:         showSpares(balancer,ishomedom);
 8259:     }
 8260:     return;
 8261: }
 8262: 
 8263: function showSpares(balancer,ishomedom) {
 8264:     var alltargets = new Array('$alltargets');
 8265:     var insttypes = new Array('$allinsttypes');
 8266:     var offloadtypes = new Array('primary','default');
 8267: 
 8268:     document.getElementById('loadbalancing_targets').style.display='block';
 8269:     document.getElementById('loadbalancing_disabled').style.display='none';
 8270:  
 8271:     for (var i=0; i<offloadtypes.length; i++) {
 8272:         var count = 0;
 8273:         for (var j=0; j<alltargets.length; j++) {
 8274:             if (alltargets[j] != balancer) {
 8275:                 document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+count).value = alltargets[j];
 8276:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).style.textAlign='left';
 8277:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).style.textFace='normal';
 8278:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8279:                 count ++;
 8280:             }
 8281:         }
 8282:     }
 8283:     for (var k=0; k<insttypes.length; k++) {
 8284:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8285:             if (ishomedom == 1) {
 8286:                 document.getElementById('balanceruletitle_'+insttypes[k]).style.display='block';
 8287:                 document.getElementById('balancerule_'+insttypes[k]).style.display='block';
 8288:             } else {
 8289:                 document.getElementById('balanceruletitle_'+insttypes[k]).style.display='none';
 8290:                 document.getElementById('balancerule_'+insttypes[k]).style.display='none';
 8291: 
 8292:             }
 8293:         } else {
 8294:             document.getElementById('balanceruletitle_'+insttypes[k]).style.display='block';
 8295:             document.getElementById('balancerule_'+insttypes[k]).style.display='block';
 8296:         }
 8297:         if ((insttypes[k] != '_LC_external') && 
 8298:             ((insttypes[k] != '_LC_internetdom') ||
 8299:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8300:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[0] = new Option("","",true,true);
 8301:             for (var m=0; m<alltargets.length; m++) {
 8302:                 var idx = m+1;
 8303:                 if (alltargets[m] != balancer) {
 8304:                     document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8305:                 }
 8306:             }
 8307:         }
 8308:     }
 8309:     return;
 8310: }
 8311: 
 8312: function hideSpares() {
 8313:     var alltargets = new Array('$alltargets');
 8314:     var insttypes = new Array('$allinsttypes');
 8315:     var offloadtypes = new Array('primary','default');
 8316: 
 8317:     document.getElementById('loadbalancing_targets').style.display='none';
 8318:     document.getElementById('loadbalancing_disabled').style.display='block';
 8319: 
 8320:     var total = alltargets.length - 1;
 8321:     for (var i=0; i<offloadtypes; i++) {
 8322:         for (var j=0; j<total; j++) {
 8323:            document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+j).checked = false;
 8324:            document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+j).value = '';
 8325:            document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8326:         }
 8327:     }
 8328:     for (var k=0; k<insttypes.length; k++) {
 8329:         document.getElementById('balanceruletitle_'+insttypes[k]).style.display='none';
 8330:         document.getElementById('balancerule_'+insttypes[k]).style.display='none';
 8331:         if (insttypes[k] != '_LC_external') {
 8332:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).length = 0;
 8333:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[0] = new Option("","",true,true);
 8334:         }
 8335:     }
 8336:     return;
 8337: }
 8338: 
 8339: function checkOffloads(item,type) {
 8340:     var alltargets = new Array('$alltargets');
 8341:     var offloadtypes = new Array('primary','default');
 8342:     if (item.checked) {
 8343:         var total = alltargets.length - 1;
 8344:         var other;
 8345:         if (type == offloadtypes[0]) {
 8346:             other = offloadtypes[1];
 8347:         } else {
 8348:             other = offloadtypes[0];
 8349:         }
 8350:         for (var i=0; i<total; i++) {
 8351:             var server = document.getElementById('loadbalancing_target_'+other+'_'+i).value;
 8352:             if (server == item.value) {
 8353:                 if (document.getElementById('loadbalancing_target_'+other+'_'+i).checked) {
 8354:                     document.getElementById('loadbalancing_target_'+other+'_'+i).checked = false;
 8355:                 }
 8356:             }
 8357:         }
 8358:     }
 8359:     return;
 8360: }
 8361: 
 8362: function singleServerToggle(type) {
 8363:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+type).selectedIndex;
 8364:     if (offloadtoSelIdx == 0) {
 8365:         document.getElementById('loadbalancing_rules_'+type+'_0').checked = true;
 8366:         document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '';
 8367: 
 8368:     } else {
 8369:         document.getElementById('loadbalancing_rules_'+type+'_2').checked = true;
 8370:         document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '$select';
 8371:     }
 8372:     return;
 8373: }
 8374: 
 8375: function balanceruleChange(formname,type) {
 8376:     if (type == '_LC_external') {
 8377:         return; 
 8378:     }
 8379:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+type);
 8380:     for (var i=0; i<typesRules.length; i++) {
 8381:         if (formname.elements[typesRules[i]].checked) {
 8382:             if (formname.elements[typesRules[i]].value != 'specific') {
 8383:                 document.getElementById('loadbalancing_singleserver_'+type).selectedIndex = 0;
 8384:                 document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '';
 8385:             } else {
 8386:                 document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '$select';
 8387:             }
 8388:         }
 8389:     }
 8390:     return;
 8391: }
 8392: 
 8393: // ]]>
 8394: </script>
 8395: 
 8396: END
 8397: }
 8398: 
 8399: sub new_spares_js {
 8400:     my @sparestypes = ('primary','default');
 8401:     my $types = join("','",@sparestypes);
 8402:     my $select = &mt('Select');
 8403:     return <<"END";
 8404: 
 8405: <script type="text/javascript">
 8406: // <![CDATA[
 8407: 
 8408: function updateNewSpares(formname,lonhost) {
 8409:     var types = new Array('$types');
 8410:     var include = new Array();
 8411:     var exclude = new Array();
 8412:     for (var i=0; i<types.length; i++) {
 8413:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8414:         for (var j=0; j<spareboxes.length; j++) {
 8415:             if (formname.elements[spareboxes[j]].checked) {
 8416:                 exclude.push(formname.elements[spareboxes[j]].value);
 8417:             } else {
 8418:                 include.push(formname.elements[spareboxes[j]].value);
 8419:             }
 8420:         }
 8421:     }
 8422:     for (var i=0; i<types.length; i++) {
 8423:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8424:         var selIdx = newSpare.selectedIndex;
 8425:         var currnew = newSpare.options[selIdx].value;
 8426:         var okSpares = new Array();
 8427:         for (var j=0; j<newSpare.options.length; j++) {
 8428:             var possible = newSpare.options[j].value;
 8429:             if (possible != '') {
 8430:                 if (exclude.indexOf(possible) == -1) {
 8431:                     okSpares.push(possible);
 8432:                 } else {
 8433:                     if (currnew == possible) {
 8434:                         selIdx = 0;
 8435:                     }
 8436:                 }
 8437:             }
 8438:         }
 8439:         for (var k=0; k<include.length; k++) {
 8440:             if (okSpares.indexOf(include[k]) == -1) {
 8441:                 okSpares.push(include[k]);
 8442:             }
 8443:         }
 8444:         okSpares.sort();
 8445:         newSpare.options.length = 0;
 8446:         if (selIdx == 0) {
 8447:             newSpare.options[0] = new Option("$select","",true,true);
 8448:         } else {
 8449:             newSpare.options[0] = new Option("$select","",false,false);
 8450:         }
 8451:         for (var m=0; m<okSpares.length; m++) {
 8452:             var idx = m+1;
 8453:             var selThis = 0;
 8454:             if (selIdx != 0) {
 8455:                 if (okSpares[m] == currnew) {
 8456:                     selThis = 1;
 8457:                 }
 8458:             }
 8459:             if (selThis == 1) {
 8460:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8461:             } else {
 8462:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8463:             }
 8464:         }
 8465:     }
 8466:     return;
 8467: }
 8468: 
 8469: function checkNewSpares(lonhost,type) {
 8470:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8471:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8472:     if (chosen != '') { 
 8473:         var othertype;
 8474:         var othernewSpare;
 8475:         if (type == 'primary') {
 8476:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 8477:         }
 8478:         if (type == 'default') {
 8479:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 8480:         }
 8481:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 8482:             othernewSpare.selectedIndex = 0;
 8483:         }
 8484:     }
 8485:     return;
 8486: }
 8487: 
 8488: // ]]>
 8489: </script>
 8490: 
 8491: END
 8492: 
 8493: }
 8494: 
 8495: sub common_domprefs_js {
 8496:     return <<"END";
 8497: 
 8498: <script type="text/javascript">
 8499: // <![CDATA[
 8500: 
 8501: function getIndicesByName(formname,item) {
 8502:     var group = new Array();
 8503:     for (var i=0;i<formname.elements.length;i++) {
 8504:         if (formname.elements[i].name == item) {
 8505:             group.push(formname.elements[i].id);
 8506:         }
 8507:     }
 8508:     return group;
 8509: }
 8510: 
 8511: // ]]>
 8512: </script>
 8513: 
 8514: END
 8515: 
 8516: }
 8517: 
 8518: sub recaptcha_js {
 8519:     my %lt = &captcha_phrases();
 8520:     return <<"END";
 8521: 
 8522: <script type="text/javascript">
 8523: // <![CDATA[
 8524: 
 8525: function updateCaptcha(caller,context) {
 8526:     var privitem;
 8527:     var pubitem;
 8528:     var privtext;
 8529:     var pubtext;
 8530:     if (document.getElementById(context+'_recaptchapub')) {
 8531:         pubitem = document.getElementById(context+'_recaptchapub');
 8532:     } else {
 8533:         return;
 8534:     }
 8535:     if (document.getElementById(context+'_recaptchapriv')) {
 8536:         privitem = document.getElementById(context+'_recaptchapriv');
 8537:     } else {
 8538:         return;
 8539:     }
 8540:     if (document.getElementById(context+'_recaptchapubtxt')) {
 8541:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 8542:     } else {
 8543:         return;
 8544:     }
 8545:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 8546:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 8547:     } else {
 8548:         return;
 8549:     }
 8550:     if (caller.checked) {
 8551:         if (caller.value == 'recaptcha') {
 8552:             pubitem.type = 'text';
 8553:             privitem.type = 'text';
 8554:             pubitem.size = '40';
 8555:             privitem.size = '40';
 8556:             pubtext.innerHTML = "$lt{'pub'}";
 8557:             privtext.innerHTML = "$lt{'priv'}";
 8558:         } else {
 8559:             pubitem.type = 'hidden';
 8560:             privitem.type = 'hidden';
 8561:             pubtext.innerHTML = '';
 8562:             privtext.innerHTML = '';
 8563:         }
 8564:     }
 8565:     return;
 8566: }
 8567: 
 8568: // ]]>
 8569: </script>
 8570: 
 8571: END
 8572: 
 8573: }
 8574: 
 8575: sub captcha_phrases {
 8576:     return &Apache::lonlocal::texthash (
 8577:                  priv => 'Private key',
 8578:                  pub  => 'Public key',
 8579:                  original  => 'original (CAPTCHA)',
 8580:                  recaptcha => 'successor (ReCAPTCHA)',
 8581:                  notused   => 'unused',
 8582:     );
 8583: }
 8584: 
 8585: 1;

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