File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.215: download - view: text, annotated - select for diffs
Mon Dec 23 13:20:31 2013 UTC (10 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Typo in rev 1.211.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.215 2013/12/23 13:20:31 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affiliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas, 
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course, and to display/store
   98: default quota sizes for authoring spaces.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial and community).  In each case the radio buttons allow the 
  107: selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use LONCAPA qw(:DEFAULT :match);
  168: use LONCAPA::Enrollment;
  169: use LONCAPA::lonauthcgi();
  170: use File::Copy;
  171: use Locale::Language;
  172: use DateTime::TimeZone;
  173: use DateTime::Locale;
  174: 
  175: my $registered_cleanup;
  176: my $modified_urls;
  177: 
  178: sub handler {
  179:     my $r=shift;
  180:     if ($r->header_only) {
  181:         &Apache::loncommon::content_type($r,'text/html');
  182:         $r->send_http_header;
  183:         return OK;
  184:     }
  185: 
  186:     my $context = 'domain';
  187:     my $dom = $env{'request.role.domain'};
  188:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  189:     if (&Apache::lonnet::allowed('mau',$dom)) {
  190:         &Apache::loncommon::content_type($r,'text/html');
  191:         $r->send_http_header;
  192:     } else {
  193:         $env{'user.error.msg'}=
  194:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  195:         return HTTP_NOT_ACCEPTABLE;
  196:     }
  197: 
  198:     $registered_cleanup=0;
  199:     @{$modified_urls}=();
  200: 
  201:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  202:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  203:                                             ['phase','actions']);
  204:     my $phase = 'pickactions';
  205:     if ( exists($env{'form.phase'}) ) {
  206:         $phase = $env{'form.phase'};
  207:     }
  208:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  209:     my %domconfig =
  210:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  211:                 'quotas','autoenroll','autoupdate','autocreate',
  212:                 'directorysrch','usercreation','usermodification',
  213:                 'contacts','defaults','scantron','coursecategories',
  214:                 'serverstatuses','requestcourses','helpsettings',
  215:                 'coursedefaults','usersessions','loadbalancing',
  216:                 'requestauthor'],$dom);
  217:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  218:                        'autoupdate','autocreate','directorysrch','contacts',
  219:                        'usercreation','usermodification','scantron',
  220:                        'requestcourses','requestauthor','coursecategories',
  221:                        'serverstatuses','helpsettings',
  222:                        'coursedefaults','usersessions');
  223:     my %existing;
  224:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  225:         %existing = %{$domconfig{'loadbalancing'}};
  226:     }
  227:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  228:         push(@prefs_order,'loadbalancing');
  229:     }
  230:     my %prefs = (
  231:         'rolecolors' =>
  232:                    { text => 'Default color schemes',
  233:                      help => 'Domain_Configuration_Color_Schemes',
  234:                      header => [{col1 => 'Student Settings',
  235:                                  col2 => '',},
  236:                                 {col1 => 'Coordinator Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Author Settings',
  239:                                  col2 => '',},
  240:                                 {col1 => 'Administrator Settings',
  241:                                  col2 => '',}],
  242:                     },
  243:         'login' =>
  244:                     { text => 'Log-in page options',
  245:                       help => 'Domain_Configuration_Login_Page',
  246:                       header => [{col1 => 'Log-in Page Items',
  247:                                   col2 => '',},
  248:                                  {col1 => 'Log-in Help',
  249:                                   col2 => 'Value'}],
  250:                     },
  251:         'defaults' => 
  252:                     { text => 'Default authentication/language/timezone/portal',
  253:                       help => 'Domain_Configuration_LangTZAuth',
  254:                       header => [{col1 => 'Setting',
  255:                                   col2 => 'Value'}],
  256:                     },
  257:         'quotas' => 
  258:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  259:                       help => 'Domain_Configuration_Quotas',
  260:                       header => [{col1 => 'User affiliation',
  261:                                   col2 => 'Available tools',
  262:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  263:                     },
  264:         'autoenroll' =>
  265:                    { text => 'Auto-enrollment settings',
  266:                      help => 'Domain_Configuration_Auto_Enrollment',
  267:                      header => [{col1 => 'Configuration setting',
  268:                                  col2 => 'Value(s)'}],
  269:                    },
  270:         'autoupdate' => 
  271:                    { text => 'Auto-update settings',
  272:                      help => 'Domain_Configuration_Auto_Updates',
  273:                      header => [{col1 => 'Setting',
  274:                                  col2 => 'Value',},
  275:                                 {col1 => 'Setting',
  276:                                  col2 => 'Affiliation'},
  277:                                 {col1 => 'User population',
  278:                                  col2 => 'Updateable user data'}],
  279:                   },
  280:         'autocreate' => 
  281:                   { text => 'Auto-course creation settings',
  282:                      help => 'Domain_Configuration_Auto_Creation',
  283:                      header => [{col1 => 'Configuration Setting',
  284:                                  col2 => 'Value',}],
  285:                   },
  286:         'directorysrch' => 
  287:                   { text => 'Institutional directory searches',
  288:                     help => 'Domain_Configuration_InstDirectory_Search',
  289:                     header => [{col1 => 'Setting',
  290:                                 col2 => 'Value',}],
  291:                   },
  292:         'contacts' =>
  293:                   { text => 'Contact Information',
  294:                     help => 'Domain_Configuration_Contact_Info',
  295:                     header => [{col1 => 'Setting',
  296:                                 col2 => 'Value',}],
  297:                   },
  298: 
  299:         'usercreation' => 
  300:                   { text => 'User creation',
  301:                     help => 'Domain_Configuration_User_Creation',
  302:                     header => [{col1 => 'Format rule type',
  303:                                 col2 => 'Format rules in force'},
  304:                                {col1 => 'User account creation',
  305:                                 col2 => 'Usernames which may be created',},
  306:                                {col1 => 'Context',
  307:                                 col2 => 'Assignable authentication types'}],
  308:                   },
  309:         'usermodification' =>
  310:                   { text => 'User modification',
  311:                     help => 'Domain_Configuration_User_Modification',
  312:                     header => [{col1 => 'Target user has role',
  313:                                 col2 => 'User information updateable in author context'},
  314:                                {col1 => 'Target user has role',
  315:                                 col2 => 'User information updateable in course context'},
  316:                                {col1 => "Status of user",
  317:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  318:                   },
  319:         'scantron' =>
  320:                   { text => 'Bubblesheet format file',
  321:                     help => 'Domain_Configuration_Scantron_Format',
  322:                     header => [ {col1 => 'Item',
  323:                                  col2 => '',
  324:                               }],
  325:                   },
  326:         'requestcourses' => 
  327:                  {text => 'Request creation of courses',
  328:                   help => 'Domain_Configuration_Request_Courses',
  329:                   header => [{col1 => 'User affiliation',
  330:                               col2 => 'Availability/Processing of requests',},
  331:                              {col1 => 'Setting',
  332:                               col2 => 'Value'}],
  333:                  },
  334:         'requestauthor' =>
  335:                  {text => 'Request authoring space',
  336:                   help => 'Domain_Configuration_Request_Author',
  337:                   header => [{col1 => 'User affiliation',
  338:                               col2 => 'Availability/Processing of requests',},
  339:                              {col1 => 'Setting',
  340:                               col2 => 'Value'}],
  341:                  },
  342:         'coursecategories' =>
  343:                   { text => 'Cataloging of courses/communities',
  344:                     help => 'Domain_Configuration_Cataloging_Courses',
  345:                     header => [{col1 => 'Category settings',
  346:                                 col2 => '',},
  347:                                {col1 => 'Categories',
  348:                                 col2 => '',
  349:                                }],
  350:                   },
  351:         'serverstatuses' =>
  352:                  {text   => 'Access to server status pages',
  353:                   help   => 'Domain_Configuration_Server_Status',
  354:                   header => [{col1 => 'Status Page',
  355:                               col2 => 'Other named users',
  356:                               col3 => 'Specific IPs',
  357:                             }],
  358:                  },
  359:         'helpsettings' =>
  360:                  {text   => 'Help page settings',
  361:                   help   => 'Domain_Configuration_Help_Settings',
  362:                   header => [{col1 => 'Help Settings (logged-in users)',
  363:                               col2 => 'Value'}],
  364:                  },
  365:         'coursedefaults' => 
  366:                  {text => 'Course/Community defaults',
  367:                   help => 'Domain_Configuration_Course_Defaults',
  368:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  369:                               col2 => 'Value',},
  370:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  371:                               col2 => 'Value',},],
  372:                  },
  373:         'privacy' => 
  374:                  {text   => 'User Privacy',
  375:                   help   => 'Domain_Configuration_User_Privacy',
  376:                   header => [{col1 => 'Setting',
  377:                               col2 => 'Value',}],
  378:                  },
  379:         'usersessions' =>
  380:                  {text  => 'User session hosting/offloading',
  381:                   help  => 'Domain_Configuration_User_Sessions',
  382:                   header => [{col1 => 'Domain server',
  383:                               col2 => 'Servers to offload sessions to when busy'},
  384:                              {col1 => 'Hosting of users from other domains',
  385:                               col2 => 'Rules'},
  386:                              {col1 => "Hosting domain's own users elsewhere",
  387:                               col2 => 'Rules'}],
  388:                  },
  389:          'loadbalancing' =>
  390:                  {text  => 'Dedicated Load Balancer(s)',
  391:                   help  => 'Domain_Configuration_Load_Balancing',
  392:                   header => [{col1 => 'Balancers',
  393:                               col2 => 'Default destinations',
  394:                               col3 => 'User affiliation',
  395:                               col4 => 'Overrides'},
  396:                             ],
  397:                  },
  398:     );
  399:     if (keys(%servers) > 1) {
  400:         $prefs{'login'}  = { text   => 'Log-in page options',
  401:                              help   => 'Domain_Configuration_Login_Page',
  402:                             header => [{col1 => 'Log-in Service',
  403:                                         col2 => 'Server Setting',},
  404:                                        {col1 => 'Log-in Page Items',
  405:                                         col2 => ''},
  406:                                        {col1 => 'Log-in Help',
  407:                                         col2 => 'Value'}],
  408:                            };
  409:     }
  410: 
  411:     my @roles = ('student','coordinator','author','admin');
  412:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  413:     &Apache::lonhtmlcommon::add_breadcrumb
  414:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  415:       text=>"Settings to display/modify"});
  416:     my $confname = $dom.'-domainconfig';
  417: 
  418:     if ($phase eq 'process') {
  419:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  420:                                                               \%prefs,\%domconfig,$confname,\@roles);
  421:         if (ref($result) eq 'HASH') {
  422:             $r->rflush();
  423:             &devalidate_remote_domconfs($dom,$result);
  424:         }
  425:     } elsif ($phase eq 'display') {
  426:         my $js = &recaptcha_js().
  427:                  &credits_js();
  428:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  429:             my ($othertitle,$usertypes,$types) =
  430:                 &Apache::loncommon::sorted_inst_types($dom);
  431:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  432:                                           $domconfig{'loadbalancing'}).
  433:                    &new_spares_js().
  434:                    &common_domprefs_js().
  435:                    &Apache::loncommon::javascript_array_indexof();
  436:         }
  437:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  438:     } else {
  439: # check if domconfig user exists for the domain.
  440:         my $servadm = $r->dir_config('lonAdmEMail');
  441:         my ($configuserok,$author_ok,$switchserver) =
  442:             &config_check($dom,$confname,$servadm);
  443:         unless ($configuserok eq 'ok') {
  444:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  445:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  446:                           $confname).
  447:                       '<br />'
  448:             );
  449:             if ($switchserver) {
  450:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  451:                           '<br />'.
  452:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  453:                           '<br />'.
  454:                           &mt('The "[_1]" user can be created automatically when a Domain Coordinator visits the web-based "Set domain configuration" screen, in a session hosted on the primary library server.',$confname).
  455:                           '<br />'.
  456:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  457:                 );
  458:             } else {
  459:                 $r->print(&mt('To create that user from the command line run the ./UPDATE script found in the top level directory of the extracted LON-CAPA tarball.').
  460:                           '<br />'.
  461:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  462:                 );
  463:             }
  464:             $r->print(&Apache::loncommon::end_page());
  465:             return OK;
  466:         }
  467:         if (keys(%domconfig) == 0) {
  468:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  469:             my @ids=&Apache::lonnet::current_machine_ids();
  470:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  471:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  472:                 my @loginimages = ('img','logo','domlogo','login');
  473:                 my $custom_img_count = 0;
  474:                 foreach my $img (@loginimages) {
  475:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  476:                         $custom_img_count ++;
  477:                     }
  478:                 }
  479:                 foreach my $role (@roles) {
  480:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  481:                         $custom_img_count ++;
  482:                     }
  483:                 }
  484:                 if ($custom_img_count > 0) {
  485:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  486:                     my $switch_server = &check_switchserver($dom,$confname);
  487:                     $r->print(
  488:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  489:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  490:     &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 />'.
  491:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  492:                     if ($switch_server) {
  493:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  494:                     }
  495:                     $r->print(&Apache::loncommon::end_page());
  496:                     return OK;
  497:                 }
  498:             }
  499:         }
  500:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  501:     }
  502:     return OK;
  503: }
  504: 
  505: sub process_changes {
  506:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  507:     my %domconfig;
  508:     if (ref($values) eq 'HASH') {
  509:         %domconfig = %{$values};
  510:     }
  511:     my $output;
  512:     if ($action eq 'login') {
  513:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  514:     } elsif ($action eq 'rolecolors') {
  515:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  516:                                      $lastactref,%domconfig);
  517:     } elsif ($action eq 'quotas') {
  518:         $output = &modify_quotas($dom,$action,$lastactref,%domconfig);
  519:     } elsif ($action eq 'autoenroll') {
  520:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  521:     } elsif ($action eq 'autoupdate') {
  522:         $output = &modify_autoupdate($dom,%domconfig);
  523:     } elsif ($action eq 'autocreate') {
  524:         $output = &modify_autocreate($dom,%domconfig);
  525:     } elsif ($action eq 'directorysrch') {
  526:         $output = &modify_directorysrch($dom,%domconfig);
  527:     } elsif ($action eq 'usercreation') {
  528:         $output = &modify_usercreation($dom,%domconfig);
  529:     } elsif ($action eq 'usermodification') {
  530:         $output = &modify_usermodification($dom,%domconfig);
  531:     } elsif ($action eq 'contacts') {
  532:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  533:     } elsif ($action eq 'defaults') {
  534:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  535:     } elsif ($action eq 'scantron') {
  536:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  537:     } elsif ($action eq 'coursecategories') {
  538:         $output = &modify_coursecategories($dom,%domconfig);
  539:     } elsif ($action eq 'serverstatuses') {
  540:         $output = &modify_serverstatuses($dom,%domconfig);
  541:     } elsif ($action eq 'requestcourses') {
  542:         $output = &modify_quotas($dom,$action,$lastactref,%domconfig);
  543:     } elsif ($action eq 'requestauthor') {
  544:         $output = &modify_quotas($dom,$action,$lastactref,%domconfig);
  545:     } elsif ($action eq 'helpsettings') {
  546:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  547:     } elsif ($action eq 'coursedefaults') {
  548:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  549:     } elsif ($action eq 'usersessions') {
  550:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  551:     } elsif ($action eq 'loadbalancing') {
  552:         $output = &modify_loadbalancing($dom,%domconfig);
  553:     }
  554:     return $output;
  555: }
  556: 
  557: sub print_config_box {
  558:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  559:     my $rowtotal = 0;
  560:     my $output;
  561:     if ($action eq 'coursecategories') {
  562:         $output = &coursecategories_javascript($settings);
  563:     }
  564:     $output .= 
  565:          '<table class="LC_nested_outer">
  566:           <tr>
  567:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  568:            &mt($item->{text}).'&nbsp;'.
  569:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  570:           '</tr>';
  571:     $rowtotal ++;
  572:     my $numheaders = 1;
  573:     if (ref($item->{'header'}) eq 'ARRAY') {
  574:         $numheaders = scalar(@{$item->{'header'}});
  575:     }
  576:     if ($numheaders > 1) {
  577:         my $colspan = '';
  578:         my $rightcolspan = '';
  579:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  580:             (($action eq 'login') && ($numheaders < 3))) {
  581:             $colspan = ' colspan="2"';
  582:         }
  583:         if ($action eq 'usersessions') {
  584:             $rightcolspan = ' colspan="3"'; 
  585:         }
  586:         $output .= '
  587:           <tr>
  588:            <td>
  589:             <table class="LC_nested">
  590:              <tr class="LC_info_row">
  591:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  592:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  593:              </tr>';
  594:         $rowtotal ++;
  595:         if ($action eq 'autoupdate') {
  596:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  597:         } elsif ($action eq 'usercreation') {
  598:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  599:         } elsif ($action eq 'usermodification') {
  600:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  601:         } elsif ($action eq 'coursecategories') {
  602:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  603:         } elsif ($action eq 'login') {
  604:             if ($numheaders == 3) {
  605:                 $colspan = ' colspan="2"';
  606:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  607:             } else {
  608:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  609:             }
  610:         } elsif ($action eq 'requestcourses') {
  611:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  612:         } elsif ($action eq 'requestauthor') {
  613:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  614:         } elsif ($action eq 'usersessions') {
  615:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  616:         } elsif ($action eq 'rolecolors') {
  617:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  618:         } elsif ($action eq 'coursedefaults') {
  619:             $output .= &print_coursedefaults('top',$dom,$settings,\$rowtotal);
  620:         }
  621:         $output .= '
  622:            </table>
  623:           </td>
  624:          </tr>
  625:          <tr>
  626:            <td>
  627:             <table class="LC_nested">
  628:              <tr class="LC_info_row">
  629:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  630:         $output .= '
  631:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  632:              </tr>';
  633:             $rowtotal ++;
  634:         if ($action eq 'autoupdate') {
  635:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  636:            </table>
  637:           </td>
  638:          </tr>
  639:          <tr>
  640:            <td>
  641:             <table class="LC_nested">
  642:              <tr class="LC_info_row">
  643:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  644:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  645:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  646:             $rowtotal ++;
  647:         } elsif ($action eq 'usercreation') {
  648:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  649:            </table>
  650:           </td>
  651:          </tr>
  652:          <tr>
  653:            <td>
  654:             <table class="LC_nested">
  655:              <tr class="LC_info_row">
  656:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  657:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  658:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  659:             $rowtotal ++;
  660:         } elsif ($action eq 'usermodification') {
  661:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  662:            </table>
  663:           </td>
  664:          </tr>
  665:          <tr>
  666:            <td>
  667:             <table class="LC_nested">
  668:              <tr class="LC_info_row">
  669:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  670:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  671:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  672:             $rowtotal ++;
  673:         } elsif ($action eq 'coursecategories') {
  674:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  675:         } elsif ($action eq 'login') {
  676:             if ($numheaders == 3) {
  677:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  678:            </table>
  679:           </td>
  680:          </tr>
  681:          <tr>
  682:            <td>
  683:             <table class="LC_nested">
  684:              <tr class="LC_info_row">
  685:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  686:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  687:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  688:                 $rowtotal ++;
  689:             } else {
  690:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  691:             }
  692:         } elsif ($action eq 'requestcourses') {
  693:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  694:         } elsif ($action eq 'requestauthor') {
  695:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  696:         } elsif ($action eq 'usersessions') {
  697:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  698:            </table>
  699:           </td>
  700:          </tr>
  701:          <tr>
  702:            <td>
  703:             <table class="LC_nested">
  704:              <tr class="LC_info_row">
  705:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  706:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  707:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  708:             $rowtotal ++;
  709:         } elsif ($action eq 'coursedefaults') {
  710:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  711:         } elsif ($action eq 'rolecolors') {
  712:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  713:            </table>
  714:           </td>
  715:          </tr>
  716:          <tr>
  717:            <td>
  718:             <table class="LC_nested">
  719:              <tr class="LC_info_row">
  720:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  721:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  722:               <td class="LC_right_item" valign="top">'.
  723:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  724:              </tr>'.
  725:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  726:            </table>
  727:           </td>
  728:          </tr>
  729:          <tr>
  730:            <td>
  731:             <table class="LC_nested">
  732:              <tr class="LC_info_row">
  733:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  734:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  735:              </tr>'.
  736:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  737:             $rowtotal += 2;
  738:         }
  739:     } else {
  740:         $output .= '
  741:           <tr>
  742:            <td>
  743:             <table class="LC_nested">
  744:              <tr class="LC_info_row">';
  745:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  746:             $output .= '  
  747:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  748:         } elsif ($action eq 'serverstatuses') {
  749:             $output .= '
  750:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  751:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  752: 
  753:         } else {
  754:             $output .= '
  755:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  756:         }
  757:         if (defined($item->{'header'}->[0]->{'col3'})) {
  758:             $output .= '<td class="LC_left_item" valign="top">'.
  759:                        &mt($item->{'header'}->[0]->{'col2'});
  760:             if ($action eq 'serverstatuses') {
  761:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  762:             } 
  763:         } else {
  764:             $output .= '<td class="LC_right_item" valign="top">'.
  765:                        &mt($item->{'header'}->[0]->{'col2'});
  766:         }
  767:         $output .= '</td>';
  768:         if ($item->{'header'}->[0]->{'col3'}) {
  769:             if (defined($item->{'header'}->[0]->{'col4'})) {
  770:                 $output .= '<td class="LC_left_item" valign="top">'.
  771:                             &mt($item->{'header'}->[0]->{'col3'});
  772:             } else {
  773:                 $output .= '<td class="LC_right_item" valign="top">'.
  774:                            &mt($item->{'header'}->[0]->{'col3'});
  775:             }
  776:             if ($action eq 'serverstatuses') {
  777:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  778:             }
  779:             $output .= '</td>';
  780:         }
  781:         if ($item->{'header'}->[0]->{'col4'}) {
  782:             $output .= '<td class="LC_right_item" valign="top">'.
  783:                        &mt($item->{'header'}->[0]->{'col4'});
  784:         }
  785:         $output .= '</tr>';
  786:         $rowtotal ++;
  787:         if ($action eq 'quotas') {
  788:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  789:         } elsif ($action eq 'autoenroll') {
  790:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  791:         } elsif ($action eq 'autocreate') {
  792:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  793:         } elsif ($action eq 'directorysrch') {
  794:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  795:         } elsif ($action eq 'contacts') {
  796:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  797:         } elsif ($action eq 'defaults') {
  798:             $output .= &print_defaults($dom,$settings,\$rowtotal);
  799:         } elsif ($action eq 'scantron') {
  800:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  801:         } elsif ($action eq 'serverstatuses') {
  802:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  803:         } elsif ($action eq 'helpsettings') {
  804:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  805:         } elsif ($action eq 'loadbalancing') {
  806:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  807:         }
  808:     }
  809:     $output .= '
  810:    </table>
  811:   </td>
  812:  </tr>
  813: </table><br />';
  814:     return ($output,$rowtotal);
  815: }
  816: 
  817: sub print_login {
  818:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  819:     my ($css_class,$datatable);
  820:     my %choices = &login_choices();
  821: 
  822:     if ($caller eq 'service') {
  823:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  824:         my $choice = $choices{'disallowlogin'};
  825:         $css_class = ' class="LC_odd_row"';
  826:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  827:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  828:                       '<th>'.$choices{'server'}.'</th>'.
  829:                       '<th>'.$choices{'serverpath'}.'</th>'.
  830:                       '<th>'.$choices{'custompath'}.'</th>'.
  831:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  832:         my %disallowed;
  833:         if (ref($settings) eq 'HASH') {
  834:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  835:                %disallowed = %{$settings->{'loginvia'}};
  836:             }
  837:         }
  838:         foreach my $lonhost (sort(keys(%servers))) {
  839:             my $direct = 'selected="selected"';
  840:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  841:                 if ($disallowed{$lonhost}{'server'} ne '') {
  842:                     $direct = '';
  843:                 }
  844:             }
  845:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  846:                           '<td><select name="'.$lonhost.'_server">'.
  847:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  848:                           '</option>';
  849:             foreach my $hostid (sort(keys(%servers))) {
  850:                 next if ($servers{$hostid} eq $servers{$lonhost});
  851:                 my $selected = '';
  852:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  853:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  854:                         $selected = 'selected="selected"';
  855:                     }
  856:                 }
  857:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  858:                               $servers{$hostid}.'</option>';
  859:             }
  860:             $datatable .= '</select></td>'.
  861:                           '<td><select name="'.$lonhost.'_serverpath">';
  862:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  863:                 my $pathname = $path;
  864:                 if ($path eq 'custom') {
  865:                     $pathname = &mt('Custom Path').' ->';
  866:                 }
  867:                 my $selected = '';
  868:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  869:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  870:                         $selected = 'selected="selected"';
  871:                     }
  872:                 } elsif ($path eq '') {
  873:                     $selected = 'selected="selected"';
  874:                 }
  875:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  876:             }
  877:             $datatable .= '</select></td>';
  878:             my ($custom,$exempt);
  879:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  880:                 $custom = $disallowed{$lonhost}{'custompath'};
  881:                 $exempt = $disallowed{$lonhost}{'exempt'};
  882:             }
  883:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  884:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  885:                           '</tr>';
  886:         }
  887:         $datatable .= '</table></td></tr>';
  888:         return $datatable;
  889:     } elsif ($caller eq 'page') {
  890:         my %defaultchecked = ( 
  891:                                'coursecatalog' => 'on',
  892:                                'helpdesk'      => 'on',
  893:                                'adminmail'     => 'off',
  894:                                'newuser'       => 'off',
  895:                              );
  896:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  897:         my (%checkedon,%checkedoff);
  898:         foreach my $item (@toggles) {
  899:             if ($defaultchecked{$item} eq 'on') { 
  900:                 $checkedon{$item} = ' checked="checked" ';
  901:                 $checkedoff{$item} = ' ';
  902:             } elsif ($defaultchecked{$item} eq 'off') {
  903:                 $checkedoff{$item} = ' checked="checked" ';
  904:                 $checkedon{$item} = ' ';
  905:             }
  906:         }
  907:         my @images = ('img','logo','domlogo','login');
  908:         my @logintext = ('textcol','bgcol');
  909:         my @bgs = ('pgbg','mainbg','sidebg');
  910:         my @links = ('link','alink','vlink');
  911:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  912:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  913:         my (%is_custom,%designs);
  914:         my %defaults = (
  915:                        font => $defaultdesign{'login.font'},
  916:                        );
  917:         foreach my $item (@images) {
  918:             $defaults{$item} = $defaultdesign{'login.'.$item};
  919:             $defaults{'showlogo'}{$item} = 1;
  920:         }
  921:         foreach my $item (@bgs) {
  922:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  923:         }
  924:         foreach my $item (@logintext) {
  925:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  926:         }
  927:         foreach my $item (@links) {
  928:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  929:         }
  930:         if (ref($settings) eq 'HASH') {
  931:             foreach my $item (@toggles) {
  932:                 if ($settings->{$item} eq '1') {
  933:                     $checkedon{$item} =  ' checked="checked" ';
  934:                     $checkedoff{$item} = ' ';
  935:                 } elsif ($settings->{$item} eq '0') {
  936:                     $checkedoff{$item} =  ' checked="checked" ';
  937:                     $checkedon{$item} = ' ';
  938:                 }
  939:             }
  940:             foreach my $item (@images) {
  941:                 if (defined($settings->{$item})) {
  942:                     $designs{$item} = $settings->{$item};
  943:                     $is_custom{$item} = 1;
  944:                 }
  945:                 if (defined($settings->{'showlogo'}{$item})) {
  946:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  947:                 }
  948:             }
  949:             foreach my $item (@logintext) {
  950:                 if ($settings->{$item} ne '') {
  951:                     $designs{'logintext'}{$item} = $settings->{$item};
  952:                     $is_custom{$item} = 1;
  953:                 }
  954:             }
  955:             if ($settings->{'font'} ne '') {
  956:                 $designs{'font'} = $settings->{'font'};
  957:                 $is_custom{'font'} = 1;
  958:             }
  959:             foreach my $item (@bgs) {
  960:                 if ($settings->{$item} ne '') {
  961:                     $designs{'bgs'}{$item} = $settings->{$item};
  962:                     $is_custom{$item} = 1;
  963:                 }
  964:             }
  965:             foreach my $item (@links) {
  966:                 if ($settings->{$item} ne '') {
  967:                     $designs{'links'}{$item} = $settings->{$item};
  968:                     $is_custom{$item} = 1;
  969:                 }
  970:             }
  971:         } else {
  972:             if ($designhash{$dom.'.login.font'} ne '') {
  973:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  974:                 $is_custom{'font'} = 1;
  975:             }
  976:             foreach my $item (@images) {
  977:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  978:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  979:                     $is_custom{$item} = 1;
  980:                 }
  981:             }
  982:             foreach my $item (@bgs) {
  983:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  984:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  985:                     $is_custom{$item} = 1;
  986:                 }
  987:             }
  988:             foreach my $item (@links) {
  989:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  990:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  991:                     $is_custom{$item} = 1;
  992:                 }
  993:             }
  994:         }
  995:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  996:                                                       logo => 'Institution Logo',
  997:                                                       domlogo => 'Domain Logo',
  998:                                                       login => 'Login box');
  999:         my $itemcount = 1;
 1000:         foreach my $item (@toggles) {
 1001:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1002:             $datatable .=  
 1003:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1004:                 '</td><td>'.
 1005:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1006:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1007:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1008:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1009:                 '</tr>';
 1010:             $itemcount ++;
 1011:         }
 1012:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1013:         $datatable .= '</tr></table></td></tr>';
 1014:     } elsif ($caller eq 'help') {
 1015:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1016:         my $switchserver = &check_switchserver($dom,$confname);
 1017:         my $itemcount = 1;
 1018:         $defaulturl = '/adm/loginproblems.html';
 1019:         $defaulttype = 'default';
 1020:         %lt = &Apache::lonlocal::texthash (
 1021:                      del     => 'Delete?',
 1022:                      rep     => 'Replace:',
 1023:                      upl     => 'Upload:',
 1024:                      default => 'Default',
 1025:                      custom  => 'Custom',
 1026:                                              );
 1027:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1028:         my @currlangs;
 1029:         if (ref($settings) eq 'HASH') {
 1030:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1031:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1032:                     next if ($settings->{'helpurl'}{$key} eq '');
 1033:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1034:                     $type{$key} = 'custom';
 1035:                     unless ($key eq 'nolang') {
 1036:                         push(@currlangs,$key);
 1037:                     }
 1038:                 }
 1039:             } elsif ($settings->{'helpurl'} ne '') {
 1040:                 $type{'nolang'} = 'custom';
 1041:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1042:             }
 1043:         }
 1044:         foreach my $lang ('nolang',sort(@currlangs)) {
 1045:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1046:             $datatable .= '<tr'.$css_class.'>';
 1047:             if ($url{$lang} eq '') {
 1048:                 $url{$lang} = $defaulturl;
 1049:             }
 1050:             if ($type{$lang} eq '') {
 1051:                 $type{$lang} = $defaulttype;
 1052:             }
 1053:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1054:             if ($lang eq 'nolang') {
 1055:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1056:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1057:             } else {
 1058:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1059:                                   $langchoices{$lang},
 1060:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1061:             }
 1062:             $datatable .= '</span></td>'."\n".
 1063:                           '<td class="LC_left_item">';
 1064:             if ($type{$lang} eq 'custom') {
 1065:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1066:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1067:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1068:             } else {
 1069:                 $datatable .= $lt{'upl'};
 1070:             }
 1071:             $datatable .='<br />';
 1072:             if ($switchserver) {
 1073:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1074:             } else {
 1075:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1076:             }
 1077:             $datatable .= '</td></tr>';
 1078:             $itemcount ++;
 1079:         }
 1080:         my @addlangs;
 1081:         foreach my $lang (sort(keys(%langchoices))) {
 1082:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1083:             push(@addlangs,$lang);
 1084:         }
 1085:         if (@addlangs > 0) {
 1086:             my %toadd;
 1087:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1088:             $toadd{''} = &mt('Select');
 1089:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1090:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1091:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1092:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1093:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1094:             if ($switchserver) {
 1095:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1096:             } else {
 1097:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1098:             }
 1099:             $datatable .= '</td></tr>';
 1100:             $itemcount ++;
 1101:         }
 1102:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1103:     }
 1104:     return $datatable;
 1105: }
 1106: 
 1107: sub login_choices {
 1108:     my %choices =
 1109:         &Apache::lonlocal::texthash (
 1110:             coursecatalog => 'Display Course/Community Catalog link?',
 1111:             adminmail     => "Display Administrator's E-mail Address?",
 1112:             helpdesk      => 'Display "Contact Helpdesk" link',
 1113:             disallowlogin => "Login page requests redirected",
 1114:             hostid        => "Server",
 1115:             server        => "Redirect to:",
 1116:             serverpath    => "Path",
 1117:             custompath    => "Custom", 
 1118:             exempt        => "Exempt IP(s)",
 1119:             directlogin   => "No redirect",
 1120:             newuser       => "Link to create a user account",
 1121:             img           => "Header",
 1122:             logo          => "Main Logo",
 1123:             domlogo       => "Domain Logo",
 1124:             login         => "Log-in Header", 
 1125:             textcol       => "Text color",
 1126:             bgcol         => "Box color",
 1127:             bgs           => "Background colors",
 1128:             links         => "Link colors",
 1129:             font          => "Font color",
 1130:             pgbg          => "Header",
 1131:             mainbg        => "Page",
 1132:             sidebg        => "Login box",
 1133:             link          => "Link",
 1134:             alink         => "Active link",
 1135:             vlink         => "Visited link",
 1136:         );
 1137:     return %choices;
 1138: }
 1139: 
 1140: sub print_rolecolors {
 1141:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1142:     my %choices = &color_font_choices();
 1143:     my @bgs = ('pgbg','tabbg','sidebg');
 1144:     my @links = ('link','alink','vlink');
 1145:     my @images = ('img');
 1146:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1147:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1148:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1149:     my (%is_custom,%designs);
 1150:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1151:     if (ref($settings) eq 'HASH') {
 1152:         if (ref($settings->{$role}) eq 'HASH') {
 1153:             if ($settings->{$role}->{'img'} ne '') {
 1154:                 $designs{'img'} = $settings->{$role}->{'img'};
 1155:                 $is_custom{'img'} = 1;
 1156:             }
 1157:             if ($settings->{$role}->{'font'} ne '') {
 1158:                 $designs{'font'} = $settings->{$role}->{'font'};
 1159:                 $is_custom{'font'} = 1;
 1160:             }
 1161:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1162:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1163:                 $is_custom{'fontmenu'} = 1;
 1164:             }
 1165:             foreach my $item (@bgs) {
 1166:                 if ($settings->{$role}->{$item} ne '') {
 1167:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1168:                     $is_custom{$item} = 1;
 1169:                 }
 1170:             }
 1171:             foreach my $item (@links) {
 1172:                 if ($settings->{$role}->{$item} ne '') {
 1173:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1174:                     $is_custom{$item} = 1;
 1175:                 }
 1176:             }
 1177:         }
 1178:     } else {
 1179:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1180:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1181:             $is_custom{'img'} = 1;
 1182:         }
 1183:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1184:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1185:             $is_custom{'fontmenu'} = 1; 
 1186:         }
 1187:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1188:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1189:             $is_custom{'font'} = 1;
 1190:         }
 1191:         foreach my $item (@bgs) {
 1192:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1193:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1194:                 $is_custom{$item} = 1;
 1195:             
 1196:             }
 1197:         }
 1198:         foreach my $item (@links) {
 1199:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1200:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1201:                 $is_custom{$item} = 1;
 1202:             }
 1203:         }
 1204:     }
 1205:     my $itemcount = 1;
 1206:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1207:     $datatable .= '</tr></table></td></tr>';
 1208:     return $datatable;
 1209: }
 1210: 
 1211: sub role_defaults {
 1212:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1213:     my %defaults;
 1214:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1215:         return %defaults;
 1216:     }
 1217:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1218:     if ($role eq 'login') {
 1219:         %defaults = (
 1220:                        font => $defaultdesign{$role.'.font'},
 1221:                     );
 1222:         if (ref($logintext) eq 'ARRAY') {
 1223:             foreach my $item (@{$logintext}) {
 1224:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1225:             }
 1226:         }
 1227:         foreach my $item (@{$images}) {
 1228:             $defaults{'showlogo'}{$item} = 1;
 1229:         }
 1230:     } else {
 1231:         %defaults = (
 1232:                        img => $defaultdesign{$role.'.img'},
 1233:                        font => $defaultdesign{$role.'.font'},
 1234:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1235:                     );
 1236:     }
 1237:     foreach my $item (@{$bgs}) {
 1238:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1239:     }
 1240:     foreach my $item (@{$links}) {
 1241:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1242:     }
 1243:     foreach my $item (@{$images}) {
 1244:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1245:     }
 1246:     return %defaults;
 1247: }
 1248: 
 1249: sub display_color_options {
 1250:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1251:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1252:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1253:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1254:     my $datatable = '<tr'.$css_class.'>'.
 1255:         '<td>'.$choices->{'font'}.'</td>';
 1256:     if (!$is_custom->{'font'}) {
 1257:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1258:     } else {
 1259:         $datatable .= '<td>&nbsp;</td>';
 1260:     }
 1261:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1262: 
 1263:     $datatable .= '<td><span class="LC_nobreak">'.
 1264:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1265:                   ' value="'.$current_color.'" />&nbsp;'.
 1266:                   '&nbsp;</td></tr>';
 1267:     unless ($role eq 'login') { 
 1268:         $datatable .= '<tr'.$css_class.'>'.
 1269:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1270:         if (!$is_custom->{'fontmenu'}) {
 1271:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1272:         } else {
 1273:             $datatable .= '<td>&nbsp;</td>';
 1274:         }
 1275: 	$current_color = $designs->{'fontmenu'} ?
 1276: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1277:         $datatable .= '<td><span class="LC_nobreak">'.
 1278:                       '<input class="colorchooser" type="text" size="10" name="'
 1279: 		      .$role.'_fontmenu"'.
 1280:                       ' value="'.$current_color.'" />&nbsp;'.
 1281:                       '&nbsp;</td></tr>';
 1282:     }
 1283:     my $switchserver = &check_switchserver($dom,$confname);
 1284:     foreach my $img (@{$images}) {
 1285: 	$itemcount ++;
 1286:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1287:         $datatable .= '<tr'.$css_class.'>'.
 1288:                       '<td>'.$choices->{$img};
 1289:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1290:         if ($role eq 'login') {
 1291:             if ($img eq 'login') {
 1292:                 $login_hdr_pick =
 1293:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1294:                 $logincolors =
 1295:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1296:                                        $designs,$defaults);
 1297:             } elsif ($img ne 'domlogo') {
 1298:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1299:             }
 1300:         }
 1301:         $datatable .= '</td>';
 1302:         if ($designs->{$img} ne '') {
 1303:             $imgfile = $designs->{$img};
 1304: 	    $img_import = ($imgfile =~ m{^/adm/});
 1305:         } else {
 1306:             $imgfile = $defaults->{$img};
 1307:         }
 1308:         if ($imgfile) {
 1309:             my ($showfile,$fullsize);
 1310:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1311:                 my $urldir = $1;
 1312:                 my $filename = $2;
 1313:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1314:                 if (@info) {
 1315:                     my $thumbfile = 'tn-'.$filename;
 1316:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1317:                     if (@thumb) {
 1318:                         $showfile = $urldir.'/'.$thumbfile;
 1319:                     } else {
 1320:                         $showfile = $imgfile;
 1321:                     }
 1322:                 } else {
 1323:                     $showfile = '';
 1324:                 }
 1325:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1326:                 $showfile = $imgfile;
 1327:                 my $imgdir = $1;
 1328:                 my $filename = $2;
 1329:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1330:                     $showfile = "/$imgdir/tn-".$filename;
 1331:                 } else {
 1332:                     my $input = $londocroot.$imgfile;
 1333:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1334:                     if (!-e $output) {
 1335:                         my ($width,$height) = &thumb_dimensions();
 1336:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1337:                         if ($fullwidth ne '' && $fullheight ne '') {
 1338:                             if ($fullwidth > $width && $fullheight > $height) { 
 1339:                                 my $size = $width.'x'.$height;
 1340:                                 system("convert -sample $size $input $output");
 1341:                                 $showfile = "/$imgdir/tn-".$filename;
 1342:                             }
 1343:                         }
 1344:                     }
 1345:                 }
 1346:             }
 1347:             if ($showfile) {
 1348:                 if ($showfile =~ m{^/(adm|res)/}) {
 1349:                     if ($showfile =~ m{^/res/}) {
 1350:                         my $local_showfile =
 1351:                             &Apache::lonnet::filelocation('',$showfile);
 1352:                         &Apache::lonnet::repcopy($local_showfile);
 1353:                     }
 1354:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1355:                 }
 1356:                 if ($imgfile) {
 1357:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1358:                         if ($imgfile =~ m{^/res/}) {
 1359:                             my $local_imgfile =
 1360:                                 &Apache::lonnet::filelocation('',$imgfile);
 1361:                             &Apache::lonnet::repcopy($local_imgfile);
 1362:                         }
 1363:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1364:                     } else {
 1365:                         $fullsize = $imgfile;
 1366:                     }
 1367:                 }
 1368:                 $datatable .= '<td>';
 1369:                 if ($img eq 'login') {
 1370:                     $datatable .= $login_hdr_pick;
 1371:                 } 
 1372:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1373:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1374:             } else {
 1375:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1376:                               &mt('Upload:').'<br />';
 1377:             }
 1378:         } else {
 1379:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1380:                           &mt('Upload:').'<br />';
 1381:         }
 1382:         if ($switchserver) {
 1383:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1384:         } else {
 1385:             if ($img ne 'login') { # suppress file selection for Log-in header
 1386:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1387:             }
 1388:         }
 1389:         $datatable .= '</td></tr>';
 1390:     }
 1391:     $itemcount ++;
 1392:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1393:     $datatable .= '<tr'.$css_class.'>'.
 1394:                   '<td>'.$choices->{'bgs'}.'</td>';
 1395:     my $bgs_def;
 1396:     foreach my $item (@{$bgs}) {
 1397:         if (!$is_custom->{$item}) {
 1398:             $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>';
 1399:         }
 1400:     }
 1401:     if ($bgs_def) {
 1402:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1403:     } else {
 1404:         $datatable .= '<td>&nbsp;</td>';
 1405:     }
 1406:     $datatable .= '<td class="LC_right_item">'.
 1407:                   '<table border="0"><tr>';
 1408: 
 1409:     foreach my $item (@{$bgs}) {
 1410:         $datatable .= '<td align="center">'.$choices->{$item};
 1411: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1412:         if ($designs->{'bgs'}{$item}) {
 1413:             $datatable .= '&nbsp;';
 1414:         }
 1415:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1416:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1417:     }
 1418:     $datatable .= '</tr></table></td></tr>';
 1419:     $itemcount ++;
 1420:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1421:     $datatable .= '<tr'.$css_class.'>'.
 1422:                   '<td>'.$choices->{'links'}.'</td>';
 1423:     my $links_def;
 1424:     foreach my $item (@{$links}) {
 1425:         if (!$is_custom->{$item}) {
 1426:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1427:         }
 1428:     }
 1429:     if ($links_def) {
 1430:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1431:     } else {
 1432:         $datatable .= '<td>&nbsp;</td>';
 1433:     }
 1434:     $datatable .= '<td class="LC_right_item">'.
 1435:                   '<table border="0"><tr>';
 1436:     foreach my $item (@{$links}) {
 1437: 	my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1438:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1439:         if ($designs->{'links'}{$item}) {
 1440:             $datatable.='&nbsp;';
 1441:         }
 1442:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1443:                       '" /></td>';
 1444:     }
 1445:     $$rowtotal += $itemcount;
 1446:     return $datatable;
 1447: }
 1448: 
 1449: sub logo_display_options {
 1450:     my ($img,$defaults,$designs) = @_;
 1451:     my $checkedon;
 1452:     if (ref($defaults) eq 'HASH') {
 1453:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1454:             if ($defaults->{'showlogo'}{$img}) {
 1455:                 $checkedon = 'checked="checked" ';     
 1456:             }
 1457:         } 
 1458:     }
 1459:     if (ref($designs) eq 'HASH') {
 1460:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1461:             if (defined($designs->{'showlogo'}{$img})) {
 1462:                 if ($designs->{'showlogo'}{$img} == 0) {
 1463:                     $checkedon = '';
 1464:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1465:                     $checkedon = 'checked="checked" ';
 1466:                 }
 1467:             }
 1468:         }
 1469:     }
 1470:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1471:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1472:            &mt('show').'</label>'."\n";
 1473: }
 1474: 
 1475: sub login_header_options  {
 1476:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1477:     my $output = '';
 1478:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1479:         $output .= &mt('Text default(s):').'<br />';
 1480:         if (!$is_custom->{'textcol'}) {
 1481:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1482:                        '&nbsp;&nbsp;&nbsp;';
 1483:         }
 1484:         if (!$is_custom->{'bgcol'}) {
 1485:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1486:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1487:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1488:         }
 1489:         $output .= '<br />';
 1490:     }
 1491:     $output .='<br />';
 1492:     return $output;
 1493: }
 1494: 
 1495: sub login_text_colors {
 1496:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1497:     my $color_menu = '<table border="0"><tr>';
 1498:     foreach my $item (@{$logintext}) {
 1499:         $color_menu .= '<td align="center">'.$choices->{$item};
 1500:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1501:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1502:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1503:     }
 1504:     $color_menu .= '</tr></table><br />';
 1505:     return $color_menu;
 1506: }
 1507: 
 1508: sub image_changes {
 1509:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1510:     my $output;
 1511:     if ($img eq 'login') {
 1512:             # suppress image for Log-in header
 1513:     } elsif (!$is_custom) {
 1514:         if ($img ne 'domlogo') {
 1515:             $output .= &mt('Default image:').'<br />';
 1516:         } else {
 1517:             $output .= &mt('Default in use:').'<br />';
 1518:         }
 1519:     }
 1520:     if ($img eq 'login') { # suppress image for Log-in header
 1521:         $output .= '<td>'.$logincolors;
 1522:     } else {
 1523:         if ($img_import) {
 1524:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1525:         }
 1526:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1527:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1528:         if ($is_custom) {
 1529:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1530:                        '<input type="checkbox" name="'.
 1531:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1532:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1533:         } else {
 1534:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1535:         }
 1536:     }
 1537:     return $output;
 1538: }
 1539: 
 1540: sub print_quotas {
 1541:     my ($dom,$settings,$rowtotal,$action) = @_;
 1542:     my $context;
 1543:     if ($action eq 'quotas') {
 1544:         $context = 'tools';
 1545:     } else {
 1546:         $context = $action;
 1547:     }
 1548:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1549:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1550:     my $typecount = 0;
 1551:     my ($css_class,%titles);
 1552:     if ($context eq 'requestcourses') {
 1553:         @usertools = ('official','unofficial','community');
 1554:         @options =('norequest','approval','validate','autolimit');
 1555:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1556:         %titles = &courserequest_titles();
 1557:     } elsif ($context eq 'requestauthor') {
 1558:         @usertools = ('author');
 1559:         @options = ('norequest','approval','automatic');
 1560:         %titles = &authorrequest_titles();
 1561:     } else {
 1562:         @usertools = ('aboutme','blog','webdav','portfolio');
 1563:         %titles = &tool_titles();
 1564:     }
 1565:     if (ref($types) eq 'ARRAY') {
 1566:         foreach my $type (@{$types}) {
 1567:             my ($currdefquota,$currauthorquota);
 1568:             unless (($context eq 'requestcourses') ||
 1569:                     ($context eq 'requestauthor')) {
 1570:                 if (ref($settings) eq 'HASH') {
 1571:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1572:                         $currdefquota = $settings->{defaultquota}->{$type};
 1573:                     } else {
 1574:                         $currdefquota = $settings->{$type};
 1575:                     }
 1576:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1577:                         $currauthorquota = $settings->{authorquota}->{$type};
 1578:                     }
 1579:                 }
 1580:             }
 1581:             if (defined($usertypes->{$type})) {
 1582:                 $typecount ++;
 1583:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1584:                 $datatable .= '<tr'.$css_class.'>'.
 1585:                               '<td>'.$usertypes->{$type}.'</td>'.
 1586:                               '<td class="LC_left_item">';
 1587:                 if ($context eq 'requestcourses') {
 1588:                     $datatable .= '<table><tr>';
 1589:                 }
 1590:                 my %cell;  
 1591:                 foreach my $item (@usertools) {
 1592:                     if ($context eq 'requestcourses') {
 1593:                         my ($curroption,$currlimit);
 1594:                         if (ref($settings) eq 'HASH') {
 1595:                             if (ref($settings->{$item}) eq 'HASH') {
 1596:                                 $curroption = $settings->{$item}->{$type};
 1597:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1598:                                     $currlimit = $1; 
 1599:                                 }
 1600:                             }
 1601:                         }
 1602:                         if (!$curroption) {
 1603:                             $curroption = 'norequest';
 1604:                         }
 1605:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1606:                         foreach my $option (@options) {
 1607:                             my $val = $option;
 1608:                             if ($option eq 'norequest') {
 1609:                                 $val = 0;  
 1610:                             }
 1611:                             if ($option eq 'validate') {
 1612:                                 my $canvalidate = 0;
 1613:                                 if (ref($validations{$item}) eq 'HASH') { 
 1614:                                     if ($validations{$item}{$type}) {
 1615:                                         $canvalidate = 1;
 1616:                                     }
 1617:                                 }
 1618:                                 next if (!$canvalidate);
 1619:                             }
 1620:                             my $checked = '';
 1621:                             if ($option eq $curroption) {
 1622:                                 $checked = ' checked="checked"';
 1623:                             } elsif ($option eq 'autolimit') {
 1624:                                 if ($curroption =~ /^autolimit/) {
 1625:                                     $checked = ' checked="checked"';
 1626:                                 }                       
 1627:                             } 
 1628:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1629:                                   '<input type="radio" name="crsreq_'.$item.
 1630:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1631:                                   $titles{$option}.'</label>';
 1632:                             if ($option eq 'autolimit') {
 1633:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1634:                                                 $item.'_limit_'.$type.'" size="1" '.
 1635:                                                 'value="'.$currlimit.'" />';
 1636:                             }
 1637:                             $cell{$item} .= '</span> ';
 1638:                             if ($option eq 'autolimit') {
 1639:                                 $cell{$item} .= $titles{'unlimited'};
 1640:                             }
 1641:                         }
 1642:                     } elsif ($context eq 'requestauthor') {
 1643:                         my $curroption;
 1644:                         if (ref($settings) eq 'HASH') {
 1645:                             $curroption = $settings->{$type};
 1646:                         }
 1647:                         if (!$curroption) {
 1648:                             $curroption = 'norequest';
 1649:                         }
 1650:                         foreach my $option (@options) {
 1651:                             my $val = $option;
 1652:                             if ($option eq 'norequest') {
 1653:                                 $val = 0;
 1654:                             }
 1655:                             my $checked = '';
 1656:                             if ($option eq $curroption) {
 1657:                                 $checked = ' checked="checked"';
 1658:                             }
 1659:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1660:                                   '<input type="radio" name="authorreq_'.$type.
 1661:                                   '" value="'.$val.'"'.$checked.' />'.
 1662:                                   $titles{$option}.'</label></span>&nbsp; ';
 1663:                         }
 1664:                     } else {
 1665:                         my $checked = 'checked="checked" ';
 1666:                         if (ref($settings) eq 'HASH') {
 1667:                             if (ref($settings->{$item}) eq 'HASH') {
 1668:                                 if ($settings->{$item}->{$type} == 0) {
 1669:                                     $checked = '';
 1670:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1671:                                     $checked =  'checked="checked" ';
 1672:                                 }
 1673:                             }
 1674:                         }
 1675:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1676:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1677:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1678:                                       '</label></span>&nbsp; ';
 1679:                     }
 1680:                 }
 1681:                 if ($context eq 'requestcourses') {
 1682:                     $datatable .= '</tr><tr>';
 1683:                     foreach my $item (@usertools) {
 1684:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1685:                     }
 1686:                     $datatable .= '</tr></table>';
 1687:                 }
 1688:                 $datatable .= '</td>';
 1689:                 unless (($context eq 'requestcourses') ||
 1690:                         ($context eq 'requestauthor')) {
 1691:                     $datatable .= 
 1692:                               '<td class="LC_right_item">'.
 1693:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1694:                               '<input type="text" name="quota_'.$type.
 1695:                               '" value="'.$currdefquota.
 1696:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1697:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1698:                               '<input type="text" name="authorquota_'.$type.
 1699:                               '" value="'.$currauthorquota.
 1700:                               '" size="5" /></span></td>';
 1701:                 }
 1702:                 $datatable .= '</tr>';
 1703:             }
 1704:         }
 1705:     }
 1706:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1707:         $defaultquota = '20';
 1708:         $authorquota = '500';
 1709:         if (ref($settings) eq 'HASH') {
 1710:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1711:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1712:             } elsif (defined($settings->{'default'})) {
 1713:                 $defaultquota = $settings->{'default'};
 1714:             }
 1715:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1716:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1717:             }
 1718:         }
 1719:     }
 1720:     $typecount ++;
 1721:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1722:     $datatable .= '<tr'.$css_class.'>'.
 1723:                   '<td>'.$othertitle.'</td>'.
 1724:                   '<td class="LC_left_item">';
 1725:     if ($context eq 'requestcourses') {
 1726:         $datatable .= '<table><tr>';
 1727:     }
 1728:     my %defcell;
 1729:     foreach my $item (@usertools) {
 1730:         if ($context eq 'requestcourses') {
 1731:             my ($curroption,$currlimit);
 1732:             if (ref($settings) eq 'HASH') {
 1733:                 if (ref($settings->{$item}) eq 'HASH') {
 1734:                     $curroption = $settings->{$item}->{'default'};
 1735:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1736:                         $currlimit = $1;
 1737:                     }
 1738:                 }
 1739:             }
 1740:             if (!$curroption) {
 1741:                 $curroption = 'norequest';
 1742:             }
 1743:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1744:             foreach my $option (@options) {
 1745:                 my $val = $option;
 1746:                 if ($option eq 'norequest') {
 1747:                     $val = 0;
 1748:                 }
 1749:                 if ($option eq 'validate') {
 1750:                     my $canvalidate = 0;
 1751:                     if (ref($validations{$item}) eq 'HASH') {
 1752:                         if ($validations{$item}{'default'}) {
 1753:                             $canvalidate = 1;
 1754:                         }
 1755:                     }
 1756:                     next if (!$canvalidate);
 1757:                 }
 1758:                 my $checked = '';
 1759:                 if ($option eq $curroption) {
 1760:                     $checked = ' checked="checked"';
 1761:                 } elsif ($option eq 'autolimit') {
 1762:                     if ($curroption =~ /^autolimit/) {
 1763:                         $checked = ' checked="checked"';
 1764:                     }
 1765:                 }
 1766:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1767:                                   '<input type="radio" name="crsreq_'.$item.
 1768:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1769:                                   $titles{$option}.'</label>';
 1770:                 if ($option eq 'autolimit') {
 1771:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1772:                                        $item.'_limit_default" size="1" '.
 1773:                                        'value="'.$currlimit.'" />';
 1774:                 }
 1775:                 $defcell{$item} .= '</span> ';
 1776:                 if ($option eq 'autolimit') {
 1777:                     $defcell{$item} .= $titles{'unlimited'};
 1778:                 }
 1779:             }
 1780:         } elsif ($context eq 'requestauthor') {
 1781:             my $curroption;
 1782:             if (ref($settings) eq 'HASH') {
 1783:                 $curroption = $settings->{'default'};
 1784:             }
 1785:             if (!$curroption) {
 1786:                 $curroption = 'norequest';
 1787:             }
 1788:             foreach my $option (@options) {
 1789:                 my $val = $option;
 1790:                 if ($option eq 'norequest') {
 1791:                     $val = 0;
 1792:                 }
 1793:                 my $checked = '';
 1794:                 if ($option eq $curroption) {
 1795:                     $checked = ' checked="checked"';
 1796:                 }
 1797:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1798:                               '<input type="radio" name="authorreq_default"'.
 1799:                               ' value="'.$val.'"'.$checked.' />'.
 1800:                               $titles{$option}.'</label></span>&nbsp; ';
 1801:             }
 1802:         } else {
 1803:             my $checked = 'checked="checked" ';
 1804:             if (ref($settings) eq 'HASH') {
 1805:                 if (ref($settings->{$item}) eq 'HASH') {
 1806:                     if ($settings->{$item}->{'default'} == 0) {
 1807:                         $checked = '';
 1808:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1809:                         $checked = 'checked="checked" ';
 1810:                     }
 1811:                 }
 1812:             }
 1813:             $datatable .= '<span class="LC_nobreak"><label>'.
 1814:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1815:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1816:                           '</label></span>&nbsp; ';
 1817:         }
 1818:     }
 1819:     if ($context eq 'requestcourses') {
 1820:         $datatable .= '</tr><tr>';
 1821:         foreach my $item (@usertools) {
 1822:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1823:         }
 1824:         $datatable .= '</tr></table>';
 1825:     }
 1826:     $datatable .= '</td>';
 1827:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1828:         $datatable .= '<td class="LC_right_item">'.
 1829:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1830:                       '<input type="text" name="defaultquota" value="'.
 1831:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1832:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1833:                       '<input type="text" name="authorquota" value="'.
 1834:                       $authorquota.'" size="5" /></span></td>';
 1835:     }
 1836:     $datatable .= '</tr>';
 1837:     $typecount ++;
 1838:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1839:     $datatable .= '<tr'.$css_class.'>'.
 1840:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1841:     if ($context eq 'requestcourses') {
 1842:         $datatable .= &mt('(overrides affiliation, if set)').
 1843:                       '</td>'.
 1844:                       '<td class="LC_left_item">'.
 1845:                       '<table><tr>';
 1846:     } else {
 1847:         $datatable .= &mt('(overrides affiliation, if checked)').
 1848:                       '</td>'.
 1849:                       '<td class="LC_left_item" colspan="2">'.
 1850:                       '<br />';
 1851:     }
 1852:     my %advcell;
 1853:     foreach my $item (@usertools) {
 1854:         if ($context eq 'requestcourses') {
 1855:             my ($curroption,$currlimit);
 1856:             if (ref($settings) eq 'HASH') {
 1857:                 if (ref($settings->{$item}) eq 'HASH') {
 1858:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1859:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1860:                         $currlimit = $1;
 1861:                     }
 1862:                 }
 1863:             }
 1864:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1865:             my $checked = '';
 1866:             if ($curroption eq '') {
 1867:                 $checked = ' checked="checked"';
 1868:             }
 1869:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1870:                                '<input type="radio" name="crsreq_'.$item.
 1871:                                '__LC_adv" value=""'.$checked.' />'.
 1872:                                &mt('No override set').'</label></span>&nbsp; ';
 1873:             foreach my $option (@options) {
 1874:                 my $val = $option;
 1875:                 if ($option eq 'norequest') {
 1876:                     $val = 0;
 1877:                 }
 1878:                 if ($option eq 'validate') {
 1879:                     my $canvalidate = 0;
 1880:                     if (ref($validations{$item}) eq 'HASH') {
 1881:                         if ($validations{$item}{'_LC_adv'}) {
 1882:                             $canvalidate = 1;
 1883:                         }
 1884:                     }
 1885:                     next if (!$canvalidate);
 1886:                 }
 1887:                 my $checked = '';
 1888:                 if ($val eq $curroption) {
 1889:                     $checked = ' checked="checked"';
 1890:                 } elsif ($option eq 'autolimit') {
 1891:                     if ($curroption =~ /^autolimit/) {
 1892:                         $checked = ' checked="checked"';
 1893:                     }
 1894:                 }
 1895:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1896:                                   '<input type="radio" name="crsreq_'.$item.
 1897:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1898:                                   $titles{$option}.'</label>';
 1899:                 if ($option eq 'autolimit') {
 1900:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1901:                                        $item.'_limit__LC_adv" size="1" '.
 1902:                                        'value="'.$currlimit.'" />';
 1903:                 }
 1904:                 $advcell{$item} .= '</span> ';
 1905:                 if ($option eq 'autolimit') {
 1906:                     $advcell{$item} .= $titles{'unlimited'};
 1907:                 }
 1908:             }
 1909:         } elsif ($context eq 'requestauthor') {
 1910:             my $curroption;
 1911:             if (ref($settings) eq 'HASH') {
 1912:                 $curroption = $settings->{'_LC_adv'};
 1913:             }
 1914:             my $checked = '';
 1915:             if ($curroption eq '') {
 1916:                 $checked = ' checked="checked"';
 1917:             }
 1918:             $datatable .= '<span class="LC_nobreak"><label>'.
 1919:                           '<input type="radio" name="authorreq__LC_adv"'.
 1920:                           ' value=""'.$checked.' />'.
 1921:                           &mt('No override set').'</label></span>&nbsp; ';
 1922:             foreach my $option (@options) {
 1923:                 my $val = $option;
 1924:                 if ($option eq 'norequest') {
 1925:                     $val = 0;
 1926:                 }
 1927:                 my $checked = '';
 1928:                 if ($val eq $curroption) {
 1929:                     $checked = ' checked="checked"';
 1930:                 }
 1931:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1932:                               '<input type="radio" name="authorreq__LC_adv"'.
 1933:                               ' value="'.$val.'"'.$checked.' />'.
 1934:                               $titles{$option}.'</label></span>&nbsp; ';
 1935:             }
 1936:         } else {
 1937:             my $checked = 'checked="checked" ';
 1938:             if (ref($settings) eq 'HASH') {
 1939:                 if (ref($settings->{$item}) eq 'HASH') {
 1940:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1941:                         $checked = '';
 1942:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1943:                         $checked = 'checked="checked" ';
 1944:                     }
 1945:                 }
 1946:             }
 1947:             $datatable .= '<span class="LC_nobreak"><label>'.
 1948:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1949:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1950:                           '</label></span>&nbsp; ';
 1951:         }
 1952:     }
 1953:     if ($context eq 'requestcourses') {
 1954:         $datatable .= '</tr><tr>';
 1955:         foreach my $item (@usertools) {
 1956:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1957:         }
 1958:         $datatable .= '</tr></table>';
 1959:     }
 1960:     $datatable .= '</td></tr>';
 1961:     $$rowtotal += $typecount;
 1962:     return $datatable;
 1963: }
 1964: 
 1965: sub print_requestmail {
 1966:     my ($dom,$action,$settings,$rowtotal) = @_;
 1967:     my ($now,$datatable,%currapp);
 1968:     $now = time;
 1969:     if (ref($settings) eq 'HASH') {
 1970:         if (ref($settings->{'notify'}) eq 'HASH') {
 1971:             if ($settings->{'notify'}{'approval'} ne '') {
 1972:                map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 1973:             }
 1974:         }
 1975:     }
 1976:     my $numinrow = 2;
 1977:     my $css_class = 'class="LC_odd_row"';
 1978:     my $text;
 1979:     if ($action eq 'requestcourses') {
 1980:         $text = &mt('Receive notification of course requests requiring approval');
 1981:     } else {
 1982:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1983:     }
 1984:     $datatable = '<tr '.$css_class.'>'.
 1985:                  ' <td>'.$text.'</td>'.
 1986:                  ' <td class="LC_left_item">';
 1987:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 1988:                                                  'reqapprovalnotify',%currapp);
 1989:     if ($numdc > 0) {
 1990:         $datatable .= $table;
 1991:     } else {
 1992:         $datatable .= &mt('There are no active Domain Coordinators');
 1993:     }
 1994:     $datatable .='</td></tr>';
 1995:     $$rowtotal += $rows;
 1996:     return $datatable;
 1997: }
 1998: 
 1999: sub print_autoenroll {
 2000:     my ($dom,$settings,$rowtotal) = @_;
 2001:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2002:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2003:     if (ref($settings) eq 'HASH') {
 2004:         if (exists($settings->{'run'})) {
 2005:             if ($settings->{'run'} eq '0') {
 2006:                 $runoff = ' checked="checked" ';
 2007:                 $runon = ' ';
 2008:             } else {
 2009:                 $runon = ' checked="checked" ';
 2010:                 $runoff = ' ';
 2011:             }
 2012:         } else {
 2013:             if ($autorun) {
 2014:                 $runon = ' checked="checked" ';
 2015:                 $runoff = ' ';
 2016:             } else {
 2017:                 $runoff = ' checked="checked" ';
 2018:                 $runon = ' ';
 2019:             }
 2020:         }
 2021:         if (exists($settings->{'co-owners'})) {
 2022:             if ($settings->{'co-owners'} eq '0') {
 2023:                 $coownersoff = ' checked="checked" ';
 2024:                 $coownerson = ' ';
 2025:             } else {
 2026:                 $coownerson = ' checked="checked" ';
 2027:                 $coownersoff = ' ';
 2028:             }
 2029:         } else {
 2030:             $coownersoff = ' checked="checked" ';
 2031:             $coownerson = ' ';
 2032:         }
 2033:         if (exists($settings->{'sender_domain'})) {
 2034:             $defdom = $settings->{'sender_domain'};
 2035:         }
 2036:     } else {
 2037:         if ($autorun) {
 2038:             $runon = ' checked="checked" ';
 2039:             $runoff = ' ';
 2040:         } else {
 2041:             $runoff = ' checked="checked" ';
 2042:             $runon = ' ';
 2043:         }
 2044:     }
 2045:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2046:     my $notif_sender;
 2047:     if (ref($settings) eq 'HASH') {
 2048:         $notif_sender = $settings->{'sender_uname'};
 2049:     }
 2050:     my $datatable='<tr class="LC_odd_row">'.
 2051:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2052:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2053:                   '<input type="radio" name="autoenroll_run"'.
 2054:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2055:                   '<label><input type="radio" name="autoenroll_run"'.
 2056:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2057:                   '</tr><tr>'.
 2058:                   '<td>'.&mt('Notification messages - sender').
 2059:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2060:                   &mt('username').':&nbsp;'.
 2061:                   '<input type="text" name="sender_uname" value="'.
 2062:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2063:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2064:                   '<tr class="LC_odd_row">'.
 2065:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2066:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2067:                   '<input type="radio" name="autoassign_coowners"'.
 2068:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2069:                   '<label><input type="radio" name="autoassign_coowners"'.
 2070:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2071:                   '</tr>';
 2072:     $$rowtotal += 3;
 2073:     return $datatable;
 2074: }
 2075: 
 2076: sub print_autoupdate {
 2077:     my ($position,$dom,$settings,$rowtotal) = @_;
 2078:     my $datatable;
 2079:     if ($position eq 'top') {
 2080:         my $updateon = ' ';
 2081:         my $updateoff = ' checked="checked" ';
 2082:         my $classlistson = ' ';
 2083:         my $classlistsoff = ' checked="checked" ';
 2084:         if (ref($settings) eq 'HASH') {
 2085:             if ($settings->{'run'} eq '1') {
 2086:                 $updateon = $updateoff;
 2087:                 $updateoff = ' ';
 2088:             }
 2089:             if ($settings->{'classlists'} eq '1') {
 2090:                 $classlistson = $classlistsoff;
 2091:                 $classlistsoff = ' ';
 2092:             }
 2093:         }
 2094:         my %title = (
 2095:                    run => 'Auto-update active?',
 2096:                    classlists => 'Update information in classlists?',
 2097:                     );
 2098:         $datatable = '<tr class="LC_odd_row">'. 
 2099:                   '<td>'.&mt($title{'run'}).'</td>'.
 2100:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2101:                   '<input type="radio" name="autoupdate_run"'.
 2102:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2103:                   '<label><input type="radio" name="autoupdate_run"'.
 2104:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2105:                   '</tr><tr>'.
 2106:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2107:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2108:                   '<label><input type="radio" name="classlists"'.
 2109:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2110:                   '<label><input type="radio" name="classlists"'.
 2111:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2112:                   '</tr>';
 2113:         $$rowtotal += 2;
 2114:     } elsif ($position eq 'middle') {
 2115:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2116:         my $numinrow = 3;
 2117:         my $locknamesettings;
 2118:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2119:                                      $dom,$numinrow,$othertitle,
 2120:                                     'lockablenames');
 2121:         $$rowtotal ++;
 2122:     } else {
 2123:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2124:         my @fields = ('lastname','firstname','middlename','generation',
 2125:                       'permanentemail','id');
 2126:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2127:         my $numrows = 0;
 2128:         if (ref($types) eq 'ARRAY') {
 2129:             if (@{$types} > 0) {
 2130:                 $datatable = 
 2131:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2132:                                          \@fields,$types,\$numrows);
 2133:                     $$rowtotal += @{$types}; 
 2134:             }
 2135:         }
 2136:         $datatable .= 
 2137:             &usertype_update_row($settings,{'default' => $othertitle},
 2138:                                  \%fieldtitles,\@fields,['default'],
 2139:                                  \$numrows);
 2140:         $$rowtotal ++;     
 2141:     }
 2142:     return $datatable;
 2143: }
 2144: 
 2145: sub print_autocreate {
 2146:     my ($dom,$settings,$rowtotal) = @_;
 2147:     my (%createon,%createoff,%currhash);
 2148:     my @types = ('xml','req');
 2149:     if (ref($settings) eq 'HASH') {
 2150:         foreach my $item (@types) {
 2151:             $createoff{$item} = ' checked="checked" ';
 2152:             $createon{$item} = ' ';
 2153:             if (exists($settings->{$item})) {
 2154:                 if ($settings->{$item}) {
 2155:                     $createon{$item} = ' checked="checked" ';
 2156:                     $createoff{$item} = ' ';
 2157:                 }
 2158:             }
 2159:         }
 2160:         if ($settings->{'xmldc'} ne '') {
 2161:             $currhash{$settings->{'xmldc'}} = 1;
 2162:         }
 2163:     } else {
 2164:         foreach my $item (@types) {
 2165:             $createoff{$item} = ' checked="checked" ';
 2166:             $createon{$item} = ' ';
 2167:         }
 2168:     }
 2169:     $$rowtotal += 2;
 2170:     my $numinrow = 2;
 2171:     my $datatable='<tr class="LC_odd_row">'.
 2172:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2173:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2174:                   '<input type="radio" name="autocreate_xml"'.
 2175:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2176:                   '<label><input type="radio" name="autocreate_xml"'.
 2177:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2178:                   '</td></tr><tr>'.
 2179:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2180:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2181:                   '<input type="radio" name="autocreate_req"'.
 2182:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2183:                   '<label><input type="radio" name="autocreate_req"'.
 2184:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2185:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2186:                                                    'autocreate_xmldc',%currhash);
 2187:     if ($numdc > 1) {
 2188:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2189:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2190:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2191:     } else {
 2192:         $datatable .= $dctable.'</td></tr>';
 2193:     }
 2194:     $$rowtotal += $rows;
 2195:     return $datatable;
 2196: }
 2197: 
 2198: sub print_directorysrch {
 2199:     my ($dom,$settings,$rowtotal) = @_;
 2200:     my $srchon = ' ';
 2201:     my $srchoff = ' checked="checked" ';
 2202:     my ($exacton,$containson,$beginson);
 2203:     my $localon = ' ';
 2204:     my $localoff = ' checked="checked" ';
 2205:     if (ref($settings) eq 'HASH') {
 2206:         if ($settings->{'available'} eq '1') {
 2207:             $srchon = $srchoff;
 2208:             $srchoff = ' ';
 2209:         }
 2210:         if ($settings->{'localonly'} eq '1') {
 2211:             $localon = $localoff;
 2212:             $localoff = ' ';
 2213:         }
 2214:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2215:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2216:                 if ($type eq 'exact') {
 2217:                     $exacton = ' checked="checked" ';
 2218:                 } elsif ($type eq 'contains') {
 2219:                     $containson = ' checked="checked" ';
 2220:                 } elsif ($type eq 'begins') {
 2221:                     $beginson = ' checked="checked" ';
 2222:                 }
 2223:             }
 2224:         } else {
 2225:             if ($settings->{'searchtypes'} eq 'exact') {
 2226:                 $exacton = ' checked="checked" ';
 2227:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2228:                 $containson = ' checked="checked" ';
 2229:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2230:                 $exacton = ' checked="checked" ';
 2231:                 $containson = ' checked="checked" ';
 2232:             }
 2233:         }
 2234:     }
 2235:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2236:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2237: 
 2238:     my $numinrow = 4;
 2239:     my $cansrchrow = 0;
 2240:     my $datatable='<tr class="LC_odd_row">'.
 2241:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2242:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2243:                   '<input type="radio" name="dirsrch_available"'.
 2244:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2245:                   '<label><input type="radio" name="dirsrch_available"'.
 2246:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2247:                   '</tr><tr>'.
 2248:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2249:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2250:                   '<input type="radio" name="dirsrch_localonly"'.
 2251:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2252:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2253:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2254:                   '</tr>';
 2255:     $$rowtotal += 2;
 2256:     if (ref($usertypes) eq 'HASH') {
 2257:         if (keys(%{$usertypes}) > 0) {
 2258:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2259:                                          $numinrow,$othertitle,'cansearch');
 2260:             $cansrchrow = 1;
 2261:         }
 2262:     }
 2263:     if ($cansrchrow) {
 2264:         $$rowtotal ++;
 2265:         $datatable .= '<tr>';
 2266:     } else {
 2267:         $datatable .= '<tr class="LC_odd_row">';
 2268:     }
 2269:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2270:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2271:     foreach my $title (@{$titleorder}) {
 2272:         if (defined($searchtitles->{$title})) {
 2273:             my $check = ' ';
 2274:             if (ref($settings) eq 'HASH') {
 2275:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2276:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2277:                         $check = ' checked="checked" ';
 2278:                     }
 2279:                 }
 2280:             }
 2281:             $datatable .= '<td class="LC_left_item">'.
 2282:                           '<span class="LC_nobreak"><label>'.
 2283:                           '<input type="checkbox" name="searchby" '.
 2284:                           'value="'.$title.'"'.$check.'/>'.
 2285:                           $searchtitles->{$title}.'</label></span></td>';
 2286:         }
 2287:     }
 2288:     $datatable .= '</tr></table></td></tr>';
 2289:     $$rowtotal ++;
 2290:     if ($cansrchrow) {
 2291:         $datatable .= '<tr class="LC_odd_row">';
 2292:     } else {
 2293:         $datatable .= '<tr>';
 2294:     }
 2295:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2296:                   '<td class="LC_left_item" colspan="2">'.
 2297:                   '<span class="LC_nobreak"><label>'.
 2298:                   '<input type="checkbox" name="searchtypes" '.
 2299:                   $exacton.' value="exact" />'.&mt('Exact match').
 2300:                   '</label>&nbsp;'.
 2301:                   '<label><input type="checkbox" name="searchtypes" '.
 2302:                   $beginson.' value="begins" />'.&mt('Begins with').
 2303:                   '</label>&nbsp;'.
 2304:                   '<label><input type="checkbox" name="searchtypes" '.
 2305:                   $containson.' value="contains" />'.&mt('Contains').
 2306:                   '</label></span></td></tr>';
 2307:     $$rowtotal ++;
 2308:     return $datatable;
 2309: }
 2310: 
 2311: sub print_contacts {
 2312:     my ($dom,$settings,$rowtotal) = @_;
 2313:     my $datatable;
 2314:     my @contacts = ('adminemail','supportemail');
 2315:     my (%checked,%to,%otheremails,%bccemails);
 2316:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2317:                     'requestsmail','updatesmail','idconflictsmail');
 2318:     foreach my $type (@mailings) {
 2319:         $otheremails{$type} = '';
 2320:     }
 2321:     $bccemails{'helpdeskmail'} = '';
 2322:     if (ref($settings) eq 'HASH') {
 2323:         foreach my $item (@contacts) {
 2324:             if (exists($settings->{$item})) {
 2325:                 $to{$item} = $settings->{$item};
 2326:             }
 2327:         }
 2328:         foreach my $type (@mailings) {
 2329:             if (exists($settings->{$type})) {
 2330:                 if (ref($settings->{$type}) eq 'HASH') {
 2331:                     foreach my $item (@contacts) {
 2332:                         if ($settings->{$type}{$item}) {
 2333:                             $checked{$type}{$item} = ' checked="checked" ';
 2334:                         }
 2335:                     }
 2336:                     $otheremails{$type} = $settings->{$type}{'others'};
 2337:                     if ($type eq 'helpdeskmail') {
 2338:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2339:                     }
 2340:                 }
 2341:             } elsif ($type eq 'lonstatusmail') {
 2342:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2343:             }
 2344:         }
 2345:     } else {
 2346:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2347:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2348:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2349:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2350:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2351:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2352:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2353:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2354:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2355:     }
 2356:     my ($titles,$short_titles) = &contact_titles();
 2357:     my $rownum = 0;
 2358:     my $css_class;
 2359:     foreach my $item (@contacts) {
 2360:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2361:         $datatable .= '<tr'.$css_class.'>'. 
 2362:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2363:                   '</span></td><td class="LC_right_item">'.
 2364:                   '<input type="text" name="'.$item.'" value="'.
 2365:                   $to{$item}.'" /></td></tr>';
 2366:         $rownum ++;
 2367:     }
 2368:     foreach my $type (@mailings) {
 2369:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2370:         $datatable .= '<tr'.$css_class.'>'.
 2371:                       '<td><span class="LC_nobreak">'.
 2372:                       $titles->{$type}.': </span></td>'.
 2373:                       '<td class="LC_left_item">'.
 2374:                       '<span class="LC_nobreak">';
 2375:         foreach my $item (@contacts) {
 2376:             $datatable .= '<label>'.
 2377:                           '<input type="checkbox" name="'.$type.'"'.
 2378:                           $checked{$type}{$item}.
 2379:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2380:                           '</label>&nbsp;';
 2381:         }
 2382:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2383:                       '<input type="text" name="'.$type.'_others" '.
 2384:                       'value="'.$otheremails{$type}.'"  />';
 2385:         if ($type eq 'helpdeskmail') {
 2386:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2387:                           '<input type="text" name="'.$type.'_bcc" '.
 2388:                           'value="'.$bccemails{$type}.'"  />';
 2389:         }
 2390:         $datatable .= '</td></tr>'."\n";
 2391:         $rownum ++;
 2392:     }
 2393:     my %choices;
 2394:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2395:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2396:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2397:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2398:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2399:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2400:     my @toggles = ('reporterrors','reportupdates');
 2401:     my %defaultchecked = ('reporterrors'  => 'on',
 2402:                           'reportupdates' => 'on');
 2403:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2404:                                                \%choices,$rownum);
 2405:     $datatable .= $reports;
 2406:     $$rowtotal += $rownum;
 2407:     return $datatable;
 2408: }
 2409: 
 2410: sub print_helpsettings {
 2411:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2412:     my ($datatable,$itemcount);
 2413:     $itemcount = 1;
 2414:     my (%choices,%defaultchecked,@toggles);
 2415:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2416:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2417:                                  &mt('LON-CAPA bug tracker'),600,500));
 2418:     %defaultchecked = ('submitbugs' => 'on');
 2419:     @toggles = ('submitbugs',);
 2420: 
 2421:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2422:                                                  \%choices,$itemcount);
 2423:     return $datatable;
 2424: }
 2425: 
 2426: sub radiobutton_prefs {
 2427:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2428:         $additional) = @_;
 2429:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2430:                    (ref($choices) eq 'HASH'));
 2431: 
 2432:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2433: 
 2434:     foreach my $item (@{$toggles}) {
 2435:         if ($defaultchecked->{$item} eq 'on') {
 2436:             $checkedon{$item} = ' checked="checked" ';
 2437:             $checkedoff{$item} = ' ';
 2438:         } elsif ($defaultchecked->{$item} eq 'off') {
 2439:             $checkedoff{$item} = ' checked="checked" ';
 2440:             $checkedon{$item} = ' ';
 2441:         }
 2442:     }
 2443:     if (ref($settings) eq 'HASH') {
 2444:         foreach my $item (@{$toggles}) {
 2445:             if ($settings->{$item} eq '1') {
 2446:                 $checkedon{$item} =  ' checked="checked" ';
 2447:                 $checkedoff{$item} = ' ';
 2448:             } elsif ($settings->{$item} eq '0') {
 2449:                 $checkedoff{$item} =  ' checked="checked" ';
 2450:                 $checkedon{$item} = ' ';
 2451:             }
 2452:         }
 2453:     }
 2454:     if ($onclick) {
 2455:         $onclick = ' onclick="'.$onclick.'"';
 2456:     }
 2457:     foreach my $item (@{$toggles}) {
 2458:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2459:         $datatable .=
 2460:             '<tr'.$css_class.'><td valign="top">'.
 2461:             '<span class="LC_nobreak">'.$choices->{$item}.
 2462:             '</span></td>'.
 2463:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2464:             '<label><input type="radio" name="'.
 2465:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2466:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2467:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2468:             '</span>'.$additional.
 2469:             '</td>'.
 2470:             '</tr>';
 2471:         $itemcount ++;
 2472:     }
 2473:     return ($datatable,$itemcount);
 2474: }
 2475: 
 2476: sub print_coursedefaults {
 2477:     my ($position,$dom,$settings,$rowtotal) = @_;
 2478:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2479:     my $itemcount = 1;
 2480:     my %choices =  &Apache::lonlocal::texthash (
 2481:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2482:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2483:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2484:         coursecredits        => 'Credits can be specified for courses',
 2485:     );
 2486:     my %staticdefaults = (
 2487:                            anonsurvey_threshold => 10,
 2488:                            uploadquota          => 500,
 2489:                          );
 2490:     if ($position eq 'top') {
 2491:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2492:         @toggles = ('canuse_pdfforms');
 2493:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2494:                                                  \%choices,$itemcount);
 2495:     } else {
 2496:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2497:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,%curruploadquota);
 2498:         my $currusecredits = 0;
 2499:         my @types = ('official','unofficial','community');
 2500:         if (ref($settings) eq 'HASH') {
 2501:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2502:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2503:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2504:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2505:                 }
 2506:             }
 2507:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2508:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2509:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2510:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '')) {
 2511:                     $currusecredits = 1;
 2512:                 }
 2513:             }
 2514:         }
 2515:         if (!$currdefresponder) {
 2516:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2517:         } elsif ($currdefresponder < 1) {
 2518:             $currdefresponder = 1;
 2519:         }
 2520:         foreach my $type (@types) {
 2521:             if ($curruploadquota{$type} eq '') {
 2522:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2523:             }
 2524:         }
 2525:         $datatable .=
 2526:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2527:                 $choices{'anonsurvey_threshold'}.
 2528:                 '</span></td>'.
 2529:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2530:                 '<input type="text" name="anonsurvey_threshold"'.
 2531:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2532:                 '</td></tr>'."\n".
 2533:                 '<tr><td><span class="LC_nobreak">'.
 2534:                 $choices{'uploadquota'}.
 2535:                 '</span></td>'.
 2536:                 '<td align="right" class="LC_right_item">'.
 2537:                 '<table><tr>';
 2538:         foreach my $type (@types) {
 2539:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2540:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2541:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2542:         }
 2543:         $datatable .= '</tr></table></td></tr>'."\n";
 2544:         $itemcount += 2;
 2545:         my $onclick = 'toggleCredits(this.form);';
 2546:         my $display = 'none';
 2547:         if ($currusecredits) {
 2548:             $display = 'block';
 2549:         }
 2550:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2551:                          '<span class="LC_nobreak">'.
 2552:                          &mt('Default credits for official courses [_1]',
 2553:                          '<input type="text" name="official_credits" value="'.
 2554:                          $def_official_credits.'" size="3" />').
 2555:                          '</span><br />'.
 2556:                          '<span class="LC_nobreak">'.
 2557:                          &mt('Default credits for unofficial courses [_1]',
 2558:                          '<input type="text" name="unofficial_credits" value="'.
 2559:                          $def_unofficial_credits.'" size="3" />').
 2560:                          '</span></div>'."\n";
 2561:         %defaultchecked = ('coursecredits' => 'off');
 2562:         @toggles = ('coursecredits');
 2563:         my $current = {
 2564:                         'coursecredits' => $currusecredits,
 2565:                       };
 2566:         (my $table,$itemcount) =
 2567:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2568:                                \%choices,$itemcount,$onclick,$additional);
 2569:         $datatable .= $table;
 2570:     }
 2571:     $$rowtotal += $itemcount;
 2572:     return $datatable;
 2573: }
 2574: 
 2575: sub print_usersessions {
 2576:     my ($position,$dom,$settings,$rowtotal) = @_;
 2577:     my ($css_class,$datatable,%checked,%choices);
 2578:     my (%by_ip,%by_location,@intdoms);
 2579:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2580: 
 2581:     my @alldoms = &Apache::lonnet::all_domains();
 2582:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2583:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2584:     my %altids = &id_for_thisdom(%servers);
 2585:     my $itemcount = 1;
 2586:     if ($position eq 'top') {
 2587:         if (keys(%serverhomes) > 1) {
 2588:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2589:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2590:         } else {
 2591:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2592:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2593:         }
 2594:     } else {
 2595:         if (keys(%by_location) == 0) {
 2596:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2597:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2598:         } else {
 2599:             my %lt = &usersession_titles();
 2600:             my $numinrow = 5;
 2601:             my $prefix;
 2602:             my @types;
 2603:             if ($position eq 'bottom') {
 2604:                 $prefix = 'remote';
 2605:                 @types = ('version','excludedomain','includedomain');
 2606:             } else {
 2607:                 $prefix = 'hosted';
 2608:                 @types = ('excludedomain','includedomain');
 2609:             }
 2610:             my (%current,%checkedon,%checkedoff);
 2611:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2612:             my @locations = sort(keys(%by_location));
 2613:             foreach my $type (@types) {
 2614:                 $checkedon{$type} = '';
 2615:                 $checkedoff{$type} = ' checked="checked"';
 2616:             }
 2617:             if (ref($settings) eq 'HASH') {
 2618:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2619:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2620:                         $current{$key} = $settings->{$prefix}{$key};
 2621:                         if ($key eq 'version') {
 2622:                             if ($current{$key} ne '') {
 2623:                                 $checkedon{$key} = ' checked="checked"';
 2624:                                 $checkedoff{$key} = '';
 2625:                             }
 2626:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2627:                             $checkedon{$key} = ' checked="checked"';
 2628:                             $checkedoff{$key} = '';
 2629:                         }
 2630:                     }
 2631:                 }
 2632:             }
 2633:             foreach my $type (@types) {
 2634:                 next if ($type ne 'version' && !@locations);
 2635:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2636:                 $datatable .= '<tr'.$css_class.'>
 2637:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2638:                                <span class="LC_nobreak">&nbsp;
 2639:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2640:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2641:                 if ($type eq 'version') {
 2642:                     my $selector = '<select name="'.$prefix.'_version">';
 2643:                     foreach my $version (@lcversions) {
 2644:                         my $selected = '';
 2645:                         if ($current{'version'} eq $version) {
 2646:                             $selected = ' selected="selected"';
 2647:                         }
 2648:                         $selector .= ' <option value="'.$version.'"'.
 2649:                                      $selected.'>'.$version.'</option>';
 2650:                     }
 2651:                     $selector .= '</select> ';
 2652:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2653:                 } else {
 2654:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2655:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2656:                                  ' />'.('&nbsp;'x2).
 2657:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2658:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2659:                                  "\n".
 2660:                                  '</div><div><table>';
 2661:                     my $rem;
 2662:                     for (my $i=0; $i<@locations; $i++) {
 2663:                         my ($showloc,$value,$checkedtype);
 2664:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2665:                             my $ip = $by_location{$locations[$i]}->[0];
 2666:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2667:                                  $value = join(':',@{$by_ip{$ip}});
 2668:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2669:                                 if (ref($current{$type}) eq 'ARRAY') {
 2670:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2671:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2672:                                             $checkedtype = ' checked="checked"';
 2673:                                             last;
 2674:                                         }
 2675:                                     }
 2676:                                 }
 2677:                             }
 2678:                         }
 2679:                         $rem = $i%($numinrow);
 2680:                         if ($rem == 0) {
 2681:                             if ($i > 0) {
 2682:                                 $datatable .= '</tr>';
 2683:                             }
 2684:                             $datatable .= '<tr>';
 2685:                         }
 2686:                         $datatable .= '<td class="LC_left_item">'.
 2687:                                       '<span class="LC_nobreak"><label>'.
 2688:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2689:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2690:                                       '</label></span></td>';
 2691:                     }
 2692:                     $rem = @locations%($numinrow);
 2693:                     my $colsleft = $numinrow - $rem;
 2694:                     if ($colsleft > 1 ) {
 2695:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2696:                                       '&nbsp;</td>';
 2697:                     } elsif ($colsleft == 1) {
 2698:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2699:                     }
 2700:                     $datatable .= '</tr></table>';
 2701:                 }
 2702:                 $datatable .= '</td></tr>';
 2703:                 $itemcount ++;
 2704:             }
 2705:         }
 2706:     }
 2707:     $$rowtotal += $itemcount;
 2708:     return $datatable;
 2709: }
 2710: 
 2711: sub build_location_hashes {
 2712:     my ($intdoms,$by_ip,$by_location) = @_;
 2713:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2714:                   (ref($by_location) eq 'HASH')); 
 2715:     my %iphost = &Apache::lonnet::get_iphost();
 2716:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2717:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2718:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2719:         foreach my $id (@{$iphost{$primary_ip}}) {
 2720:             my $intdom = &Apache::lonnet::internet_dom($id);
 2721:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2722:                 push(@{$intdoms},$intdom);
 2723:             }
 2724:         }
 2725:     }
 2726:     foreach my $ip (keys(%iphost)) {
 2727:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2728:             foreach my $id (@{$iphost{$ip}}) {
 2729:                 my $location = &Apache::lonnet::internet_dom($id);
 2730:                 if ($location) {
 2731:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2732:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2733:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2734:                             push(@{$by_ip->{$ip}},$location);
 2735:                         }
 2736:                     } else {
 2737:                         $by_ip->{$ip} = [$location];
 2738:                     }
 2739:                 }
 2740:             }
 2741:         }
 2742:     }
 2743:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2744:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2745:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2746:             my $first = $by_ip->{$ip}->[0];
 2747:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2748:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2749:                     push(@{$by_location->{$first}},$ip);
 2750:                 }
 2751:             } else {
 2752:                 $by_location->{$first} = [$ip];
 2753:             }
 2754:         }
 2755:     }
 2756:     return;
 2757: }
 2758: 
 2759: sub current_offloads_to {
 2760:     my ($dom,$settings,$servers) = @_;
 2761:     my (%spareid,%otherdomconfigs);
 2762:     if (ref($servers) eq 'HASH') {
 2763:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2764:             my $gotspares;
 2765:             if (ref($settings) eq 'HASH') {
 2766:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2767:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2768:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2769:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2770:                         $gotspares = 1;
 2771:                     }
 2772:                 }
 2773:             }
 2774:             unless ($gotspares) {
 2775:                 my $gotspares;
 2776:                 my $serverhomeID =
 2777:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2778:                 my $serverhomedom =
 2779:                     &Apache::lonnet::host_domain($serverhomeID);
 2780:                 if ($serverhomedom ne $dom) {
 2781:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2782:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2783:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2784:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2785:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2786:                                 $gotspares = 1;
 2787:                             }
 2788:                         }
 2789:                     } else {
 2790:                         $otherdomconfigs{$serverhomedom} =
 2791:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2792:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2793:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2794:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2795:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2796:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2797:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2798:                                         $gotspares = 1;
 2799:                                     }
 2800:                                 }
 2801:                             }
 2802:                         }
 2803:                     }
 2804:                 }
 2805:             }
 2806:             unless ($gotspares) {
 2807:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2808:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2809:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2810:                } else {
 2811:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2812:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2813:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2814:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2815:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2816:                     } else {
 2817:                         my %what = (
 2818:                              spareid => 1,
 2819:                         );
 2820:                         my ($result,$returnhash) = 
 2821:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2822:                         if ($result eq 'ok') { 
 2823:                             if (ref($returnhash) eq 'HASH') {
 2824:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2825:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2826:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2827:                                 }
 2828:                             }
 2829:                         }
 2830:                     }
 2831:                 }
 2832:             }
 2833:         }
 2834:     }
 2835:     return %spareid;
 2836: }
 2837: 
 2838: sub spares_row {
 2839:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2840:     my $css_class;
 2841:     my $numinrow = 4;
 2842:     my $itemcount = 1;
 2843:     my $datatable;
 2844:     my %typetitles = &sparestype_titles();
 2845:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2846:         foreach my $server (sort(keys(%{$servers}))) {
 2847:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2848:             my ($othercontrol,$serverdom);
 2849:             if ($serverhome ne $server) {
 2850:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2851:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2852:             } else {
 2853:                 $serverdom = &Apache::lonnet::host_domain($server);
 2854:                 if ($serverdom ne $dom) {
 2855:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2856:                 }
 2857:             }
 2858:             next unless (ref($spareid->{$server}) eq 'HASH');
 2859:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2860:             $datatable .= '<tr'.$css_class.'>
 2861:                            <td rowspan="2">
 2862:                             <span class="LC_nobreak">'.
 2863:                           &mt('[_1] when busy, offloads to:'
 2864:                               ,'<b>'.$server.'</b>').
 2865:                           "\n";
 2866:             my (%current,%canselect);
 2867:             my @choices = 
 2868:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2869:             foreach my $type ('primary','default') {
 2870:                 if (ref($spareid->{$server}) eq 'HASH') {
 2871:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2872:                         my @spares = @{$spareid->{$server}{$type}};
 2873:                         if (@spares > 0) {
 2874:                             if ($othercontrol) {
 2875:                                 $current{$type} = join(', ',@spares);
 2876:                             } else {
 2877:                                 $current{$type} .= '<table>';
 2878:                                 my $numspares = scalar(@spares);
 2879:                                 for (my $i=0;  $i<@spares; $i++) {
 2880:                                     my $rem = $i%($numinrow);
 2881:                                     if ($rem == 0) {
 2882:                                         if ($i > 0) {
 2883:                                             $current{$type} .= '</tr>';
 2884:                                         }
 2885:                                         $current{$type} .= '<tr>';
 2886:                                     }
 2887:                                     $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;'.
 2888:                                                        $spareid->{$server}{$type}[$i].
 2889:                                                        '</label></td>'."\n";
 2890:                                 }
 2891:                                 my $rem = @spares%($numinrow);
 2892:                                 my $colsleft = $numinrow - $rem;
 2893:                                 if ($colsleft > 1 ) {
 2894:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2895:                                                        '" class="LC_left_item">'.
 2896:                                                        '&nbsp;</td>';
 2897:                                 } elsif ($colsleft == 1) {
 2898:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2899:                                 }
 2900:                                 $current{$type} .= '</tr></table>';
 2901:                             }
 2902:                         }
 2903:                     }
 2904:                     if ($current{$type} eq '') {
 2905:                         $current{$type} = &mt('None specified');
 2906:                     }
 2907:                     if ($othercontrol) {
 2908:                         if ($type eq 'primary') {
 2909:                             $canselect{$type} = $othercontrol;
 2910:                         }
 2911:                     } else {
 2912:                         $canselect{$type} = 
 2913:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2914:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2915:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2916:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2917:                         if (@choices > 0) {
 2918:                             foreach my $lonhost (@choices) {
 2919:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2920:                             }
 2921:                         }
 2922:                         $canselect{$type} .= '</select>'."\n";
 2923:                     }
 2924:                 } else {
 2925:                     $current{$type} = &mt('Could not be determined');
 2926:                     if ($type eq 'primary') {
 2927:                         $canselect{$type} =  $othercontrol;
 2928:                     }
 2929:                 }
 2930:                 if ($type eq 'default') {
 2931:                     $datatable .= '<tr'.$css_class.'>';
 2932:                 }
 2933:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2934:                               '<td>'.$current{$type}.'</td>'."\n".
 2935:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2936:             }
 2937:             $itemcount ++;
 2938:         }
 2939:     }
 2940:     $$rowtotal += $itemcount;
 2941:     return $datatable;
 2942: }
 2943: 
 2944: sub possible_newspares {
 2945:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2946:     my $serverhostname = &Apache::lonnet::hostname($server);
 2947:     my %excluded;
 2948:     if ($serverhostname ne '') {
 2949:         %excluded = (
 2950:                        $serverhostname => 1,
 2951:                     );
 2952:     }
 2953:     if (ref($currspares) eq 'HASH') {
 2954:         foreach my $type (keys(%{$currspares})) {
 2955:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2956:                 if (@{$currspares->{$type}} > 0) {
 2957:                     foreach my $curr (@{$currspares->{$type}}) {
 2958:                         my $hostname = &Apache::lonnet::hostname($curr);
 2959:                         $excluded{$hostname} = 1;
 2960:                     }
 2961:                 }
 2962:             }
 2963:         }
 2964:     }
 2965:     my @choices;
 2966:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2967:         if (keys(%{$serverhomes}) > 1) {
 2968:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2969:                 unless ($excluded{$name}) {
 2970:                     if (exists($altids->{$serverhomes->{$name}})) {
 2971:                         push(@choices,$altids->{$serverhomes->{$name}});
 2972:                     } else {
 2973:                         push(@choices,$serverhomes->{$name});
 2974:                     }
 2975:                 }
 2976:             }
 2977:         }
 2978:     }
 2979:     return sort(@choices);
 2980: }
 2981: 
 2982: sub print_loadbalancing {
 2983:     my ($dom,$settings,$rowtotal) = @_;
 2984:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2985:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2986:     my $numinrow = 1;
 2987:     my $datatable;
 2988:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2989:     my (%currbalancer,%currtargets,%currrules,%existing);
 2990:     if (ref($settings) eq 'HASH') {
 2991:         %existing = %{$settings};
 2992:     }
 2993:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 2994:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 2995:                                   \%currtargets,\%currrules);
 2996:     } else {
 2997:         return;
 2998:     }
 2999:     my ($othertitle,$usertypes,$types) =
 3000:         &Apache::loncommon::sorted_inst_types($dom);
 3001:     my $rownum = 8;
 3002:     if (ref($types) eq 'ARRAY') {
 3003:         $rownum += scalar(@{$types});
 3004:     }
 3005:     my @css_class = ('LC_odd_row','LC_even_row');
 3006:     my $balnum = 0;
 3007:     my $islast;
 3008:     my (@toshow,$disabledtext);
 3009:     if (keys(%currbalancer) > 0) {
 3010:         @toshow = sort(keys(%currbalancer));
 3011:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3012:             push(@toshow,'');
 3013:         }
 3014:     } else {
 3015:         @toshow = ('');
 3016:         $disabledtext = &mt('No existing load balancer');
 3017:     }
 3018:     foreach my $lonhost (@toshow) {
 3019:         if ($balnum == scalar(@toshow)-1) {
 3020:             $islast = 1;
 3021:         } else {
 3022:             $islast = 0;
 3023:         }
 3024:         my $cssidx = $balnum%2;
 3025:         my $targets_div_style = 'display: none';
 3026:         my $disabled_div_style = 'display: block';
 3027:         my $homedom_div_style = 'display: none';
 3028:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3029:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3030:                       '<p>';
 3031:         if ($lonhost eq '') {
 3032:             $datatable .= '<span class="LC_nobreak">';
 3033:             if (keys(%currbalancer) > 0) {
 3034:                 $datatable .= &mt('Add balancer:');
 3035:             } else {
 3036:                 $datatable .= &mt('Enable balancer:');
 3037:             }
 3038:             $datatable .= '&nbsp;'.
 3039:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3040:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3041:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3042:                           '<option value="" selected="selected">'.&mt('None').
 3043:                           '</option>'."\n";
 3044:             foreach my $server (sort(keys(%servers))) {
 3045:                 next if ($currbalancer{$server});
 3046:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3047:             }
 3048:             $datatable .=
 3049:                 '</select>'."\n".
 3050:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3051:         } else {
 3052:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3053:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3054:                            &mt('Stop balancing').'</label>'.
 3055:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3056:             $targets_div_style = 'display: block';
 3057:             $disabled_div_style = 'display: none';
 3058:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3059:                 $homedom_div_style = 'display: block';
 3060:             }
 3061:         }
 3062:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3063:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3064:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3065:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3066:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3067:         my @sparestypes = ('primary','default');
 3068:         my %typetitles = &sparestype_titles();
 3069:         foreach my $sparetype (@sparestypes) {
 3070:             my $targettable;
 3071:             for (my $i=0; $i<$numspares; $i++) {
 3072:                 my $checked;
 3073:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3074:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3075:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3076:                             $checked = ' checked="checked"';
 3077:                         }
 3078:                     }
 3079:                 }
 3080:                 my ($chkboxval,$disabled);
 3081:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3082:                     $chkboxval = $spares[$i];
 3083:                 }
 3084:                 if (exists($currbalancer{$spares[$i]})) {
 3085:                     $disabled = ' disabled="disabled"';
 3086:                 }
 3087:                 $targettable .=
 3088:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3089:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3090:                     '</span></label></td>';
 3091:                 my $rem = $i%($numinrow);
 3092:                 if ($rem == 0) {
 3093:                     if (($i > 0) && ($i < $numspares-1)) {
 3094:                         $targettable .= '</tr>';
 3095:                     }
 3096:                     if ($i < $numspares-1) {
 3097:                         $targettable .= '<tr>';
 3098:                     }
 3099:                 }
 3100:             }
 3101:             if ($targettable ne '') {
 3102:                 my $rem = $numspares%($numinrow);
 3103:                 my $colsleft = $numinrow - $rem;
 3104:                 if ($colsleft > 1 ) {
 3105:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3106:                                     '&nbsp;</td>';
 3107:                 } elsif ($colsleft == 1) {
 3108:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3109:                 }
 3110:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3111:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3112:             }
 3113:         }
 3114:         $datatable .= '</div></td></tr>'.
 3115:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3116:                                            $othertitle,$usertypes,$types,\%servers,
 3117:                                            \%currbalancer,$lonhost,
 3118:                                            $targets_div_style,$homedom_div_style,
 3119:                                            $css_class[$cssidx],$balnum,$islast);
 3120:         $$rowtotal += $rownum;
 3121:         $balnum ++;
 3122:     }
 3123:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3124:     return $datatable;
 3125: }
 3126: 
 3127: sub get_loadbalancers_config {
 3128:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3129:     return unless ((ref($servers) eq 'HASH') &&
 3130:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3131:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3132:     if (keys(%{$existing}) > 0) {
 3133:         my $oldlonhost;
 3134:         foreach my $key (sort(keys(%{$existing}))) {
 3135:             if ($key eq 'lonhost') {
 3136:                 $oldlonhost = $existing->{'lonhost'};
 3137:                 $currbalancer->{$oldlonhost} = 1;
 3138:             } elsif ($key eq 'targets') {
 3139:                 if ($oldlonhost) {
 3140:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3141:                 }
 3142:             } elsif ($key eq 'rules') {
 3143:                 if ($oldlonhost) {
 3144:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3145:                 }
 3146:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3147:                 $currbalancer->{$key} = 1;
 3148:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3149:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3150:             }
 3151:         }
 3152:     } else {
 3153:         my ($balancerref,$targetsref) =
 3154:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3155:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3156:             foreach my $server (sort(keys(%{$balancerref}))) {
 3157:                 $currbalancer->{$server} = 1;
 3158:                 $currtargets->{$server} = $targetsref->{$server};
 3159:             }
 3160:         }
 3161:     }
 3162:     return;
 3163: }
 3164: 
 3165: sub loadbalancing_rules {
 3166:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3167:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3168:         $css_class,$balnum,$islast) = @_;
 3169:     my $output;
 3170:     my $num = 0;
 3171:     my ($alltypes,$othertypes,$titles) =
 3172:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3173:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3174:         foreach my $type (@{$alltypes}) {
 3175:             $num ++;
 3176:             my $current;
 3177:             if (ref($currrules) eq 'HASH') {
 3178:                 $current = $currrules->{$type};
 3179:             }
 3180:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3181:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3182:                     $current = '';
 3183:                 }
 3184:             }
 3185:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3186:                                              $servers,$currbalancer,$lonhost,$dom,
 3187:                                              $targets_div_style,$homedom_div_style,
 3188:                                              $css_class,$balnum,$num,$islast);
 3189:         }
 3190:     }
 3191:     return $output;
 3192: }
 3193: 
 3194: sub loadbalancing_titles {
 3195:     my ($dom,$intdom,$usertypes,$types) = @_;
 3196:     my %othertypes = (
 3197:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3198:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3199:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3200:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3201:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3202:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3203:                      );
 3204:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3205:     if (ref($types) eq 'ARRAY') {
 3206:         unshift(@alltypes,@{$types},'default');
 3207:     }
 3208:     my %titles;
 3209:     foreach my $type (@alltypes) {
 3210:         if ($type =~ /^_LC_/) {
 3211:             $titles{$type} = $othertypes{$type};
 3212:         } elsif ($type eq 'default') {
 3213:             $titles{$type} = &mt('All users from [_1]',$dom);
 3214:             if (ref($types) eq 'ARRAY') {
 3215:                 if (@{$types} > 0) {
 3216:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3217:                 }
 3218:             }
 3219:         } elsif (ref($usertypes) eq 'HASH') {
 3220:             $titles{$type} = $usertypes->{$type};
 3221:         }
 3222:     }
 3223:     return (\@alltypes,\%othertypes,\%titles);
 3224: }
 3225: 
 3226: sub loadbalance_rule_row {
 3227:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3228:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3229:     my @rulenames;
 3230:     my %ruletitles = &offloadtype_text();
 3231:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3232:         @rulenames = ('balancer','offloadedto');
 3233:     } else {
 3234:         @rulenames = ('default','homeserver');
 3235:         if ($type eq '_LC_external') {
 3236:             push(@rulenames,'externalbalancer');
 3237:         } else {
 3238:             push(@rulenames,'specific');
 3239:         }
 3240:         push(@rulenames,'none');
 3241:     }
 3242:     my $style = $targets_div_style;
 3243:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3244:         $style = $homedom_div_style;
 3245:     }
 3246:     my $space;
 3247:     if ($islast && $num == 1) {
 3248:         $space = '<div display="inline-block">&nbsp;</div>';
 3249:     }
 3250:     my $output =
 3251:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3252:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3253:         '<td valaign="top">'.$space.
 3254:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3255:     for (my $i=0; $i<@rulenames; $i++) {
 3256:         my $rule = $rulenames[$i];
 3257:         my ($checked,$extra);
 3258:         if ($rulenames[$i] eq 'default') {
 3259:             $rule = '';
 3260:         }
 3261:         if ($rulenames[$i] eq 'specific') {
 3262:             if (ref($servers) eq 'HASH') {
 3263:                 my $default;
 3264:                 if (($current ne '') && (exists($servers->{$current}))) {
 3265:                     $checked = ' checked="checked"';
 3266:                 }
 3267:                 unless ($checked) {
 3268:                     $default = ' selected="selected"';
 3269:                 }
 3270:                 $extra =
 3271:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3272:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3273:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3274:                     '<option value=""'.$default.'></option>'."\n";
 3275:                 foreach my $server (sort(keys(%{$servers}))) {
 3276:                     if (ref($currbalancer) eq 'HASH') {
 3277:                         next if (exists($currbalancer->{$server}));
 3278:                     }
 3279:                     my $selected;
 3280:                     if ($server eq $current) {
 3281:                         $selected = ' selected="selected"';
 3282:                     }
 3283:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3284:                 }
 3285:                 $extra .= '</select>';
 3286:             }
 3287:         } elsif ($rule eq $current) {
 3288:             $checked = ' checked="checked"';
 3289:         }
 3290:         $output .= '<span class="LC_nobreak"><label>'.
 3291:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3292:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3293:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3294:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3295:                    '</label>'.$extra.'</span><br />'."\n";
 3296:     }
 3297:     $output .= '</div></td></tr>'."\n";
 3298:     return $output;
 3299: }
 3300: 
 3301: sub offloadtype_text {
 3302:     my %ruletitles = &Apache::lonlocal::texthash (
 3303:            'default'          => 'Offloads to default destinations',
 3304:            'homeserver'       => "Offloads to user's home server",
 3305:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3306:            'specific'         => 'Offloads to specific server',
 3307:            'none'             => 'No offload',
 3308:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3309:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3310:     );
 3311:     return %ruletitles;
 3312: }
 3313: 
 3314: sub sparestype_titles {
 3315:     my %typestitles = &Apache::lonlocal::texthash (
 3316:                           'primary' => 'primary',
 3317:                           'default' => 'default',
 3318:                       );
 3319:     return %typestitles;
 3320: }
 3321: 
 3322: sub contact_titles {
 3323:     my %titles = &Apache::lonlocal::texthash (
 3324:                    'supportemail' => 'Support E-mail address',
 3325:                    'adminemail'   => 'Default Server Admin E-mail address',
 3326:                    'errormail'    => 'Error reports to be e-mailed to',
 3327:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3328:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3329:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3330:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3331:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3332:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3333:                  );
 3334:     my %short_titles = &Apache::lonlocal::texthash (
 3335:                            adminemail   => 'Admin E-mail address',
 3336:                            supportemail => 'Support E-mail',
 3337:                        );   
 3338:     return (\%titles,\%short_titles);
 3339: }
 3340: 
 3341: sub tool_titles {
 3342:     my %titles = &Apache::lonlocal::texthash (
 3343:                      aboutme    => 'Personal web page',
 3344:                      blog       => 'Blog',
 3345:                      webdav     => 'WebDAV',
 3346:                      portfolio  => 'Portfolio',
 3347:                      official   => 'Official courses (with institutional codes)',
 3348:                      unofficial => 'Unofficial courses',
 3349:                      community  => 'Communities',
 3350:                  );
 3351:     return %titles;
 3352: }
 3353: 
 3354: sub courserequest_titles {
 3355:     my %titles = &Apache::lonlocal::texthash (
 3356:                                    official   => 'Official',
 3357:                                    unofficial => 'Unofficial',
 3358:                                    community  => 'Communities',
 3359:                                    norequest  => 'Not allowed',
 3360:                                    approval   => 'Approval by Dom. Coord.',
 3361:                                    validate   => 'With validation',
 3362:                                    autolimit  => 'Numerical limit',
 3363:                                    unlimited  => '(blank for unlimited)',
 3364:                  );
 3365:     return %titles;
 3366: }
 3367: 
 3368: sub authorrequest_titles {
 3369:     my %titles = &Apache::lonlocal::texthash (
 3370:                                    norequest  => 'Not allowed',
 3371:                                    approval   => 'Approval by Dom. Coord.',
 3372:                                    automatic  => 'Automatic approval',
 3373:                  );
 3374:     return %titles;
 3375: }
 3376: 
 3377: sub courserequest_conditions {
 3378:     my %conditions = &Apache::lonlocal::texthash (
 3379:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3380:        validate   => '(Processing of request subject to institutional validation).',
 3381:                  );
 3382:     return %conditions;
 3383: }
 3384: 
 3385: 
 3386: sub print_usercreation {
 3387:     my ($position,$dom,$settings,$rowtotal) = @_;
 3388:     my $numinrow = 4;
 3389:     my $datatable;
 3390:     if ($position eq 'top') {
 3391:         $$rowtotal ++;
 3392:         my $rowcount = 0;
 3393:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3394:         if (ref($rules) eq 'HASH') {
 3395:             if (keys(%{$rules}) > 0) {
 3396:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3397:                                                 $ruleorder,$numinrow,$rowcount);
 3398:                 $$rowtotal ++;
 3399:                 $rowcount ++;
 3400:             }
 3401:         }
 3402:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3403:         if (ref($idrules) eq 'HASH') {
 3404:             if (keys(%{$idrules}) > 0) {
 3405:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3406:                                                 $idruleorder,$numinrow,$rowcount);
 3407:                 $$rowtotal ++;
 3408:                 $rowcount ++;
 3409:             }
 3410:         }
 3411:         my ($emailrules,$emailruleorder) = 
 3412:             &Apache::lonnet::inst_userrules($dom,'email');
 3413:         if (ref($emailrules) eq 'HASH') {
 3414:             if (keys(%{$emailrules}) > 0) {
 3415:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3416:                                                 $emailruleorder,$numinrow,$rowcount);
 3417:                 $$rowtotal ++;
 3418:                 $rowcount ++;
 3419:             }
 3420:         }
 3421:         if ($rowcount == 0) {
 3422:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3423:             $$rowtotal ++;
 3424:             $rowcount ++;
 3425:         }
 3426:     } elsif ($position eq 'middle') {
 3427:         my @creators = ('author','course','requestcrs','selfcreate');
 3428:         my ($rules,$ruleorder) =
 3429:             &Apache::lonnet::inst_userrules($dom,'username');
 3430:         my %lt = &usercreation_types();
 3431:         my %checked;
 3432:         my @selfcreate; 
 3433:         if (ref($settings) eq 'HASH') {
 3434:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3435:                 foreach my $item (@creators) {
 3436:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3437:                 }
 3438:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3439:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3440:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3441:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3442:                         @selfcreate = ('email','login','sso');
 3443:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3444:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3445:                     }
 3446:                 }
 3447:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3448:                 foreach my $item (@creators) {
 3449:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3450:                         $checked{$item} = 'none';
 3451:                     }
 3452:                 }
 3453:             }
 3454:         }
 3455:         my $rownum = 0;
 3456:         foreach my $item (@creators) {
 3457:             $rownum ++;
 3458:             if ($item ne 'selfcreate') {  
 3459:                 if ($checked{$item} eq '') {
 3460:                     $checked{$item} = 'any';
 3461:                 }
 3462:             }
 3463:             my $css_class;
 3464:             if ($rownum%2) {
 3465:                 $css_class = '';
 3466:             } else {
 3467:                 $css_class = ' class="LC_odd_row" ';
 3468:             }
 3469:             $datatable .= '<tr'.$css_class.'>'.
 3470:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3471:                          '</span></td><td align="right">';
 3472:             my @options;
 3473:             if ($item eq 'selfcreate') {
 3474:                 push(@options,('email','login','sso'));
 3475:             } else {
 3476:                 @options = ('any');
 3477:                 if (ref($rules) eq 'HASH') {
 3478:                     if (keys(%{$rules}) > 0) {
 3479:                         push(@options,('official','unofficial'));
 3480:                     }
 3481:                 }
 3482:                 push(@options,'none');
 3483:             }
 3484:             foreach my $option (@options) {
 3485:                 my $type = 'radio';
 3486:                 my $check = ' ';
 3487:                 if ($item eq 'selfcreate') {
 3488:                     $type = 'checkbox';
 3489:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3490:                         $check = ' checked="checked" ';
 3491:                     }
 3492:                 } else {
 3493:                     if ($checked{$item} eq $option) {
 3494:                         $check = ' checked="checked" ';
 3495:                     }
 3496:                 } 
 3497:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3498:                               '<input type="'.$type.'" name="can_createuser_'.
 3499:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3500:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3501:             }
 3502:             $datatable .= '</td></tr>';
 3503:         }
 3504:         my ($othertitle,$usertypes,$types) =
 3505:             &Apache::loncommon::sorted_inst_types($dom);
 3506:         my $createsettings;
 3507:         if (ref($settings) eq 'HASH') {
 3508:             $createsettings = $settings->{cancreate};
 3509:         }
 3510:         if (ref($usertypes) eq 'HASH') {
 3511:             if (keys(%{$usertypes}) > 0) {
 3512:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3513:                                              $dom,$numinrow,$othertitle,
 3514:                                              'statustocreate');
 3515:                 $$rowtotal ++;
 3516:                 $rownum ++;
 3517:             }
 3518:         }
 3519:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3520:     } else {
 3521:         my @contexts = ('author','course','domain');
 3522:         my @authtypes = ('int','krb4','krb5','loc');
 3523:         my %checked;
 3524:         if (ref($settings) eq 'HASH') {
 3525:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3526:                 foreach my $item (@contexts) {
 3527:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3528:                         foreach my $auth (@authtypes) {
 3529:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3530:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3531:                             }
 3532:                         }
 3533:                     }
 3534:                 }
 3535:             }
 3536:         } else {
 3537:             foreach my $item (@contexts) {
 3538:                 foreach my $auth (@authtypes) {
 3539:                     $checked{$item}{$auth} = ' checked="checked" ';
 3540:                 }
 3541:             }
 3542:         }
 3543:         my %title = &context_names();
 3544:         my %authname = &authtype_names();
 3545:         my $rownum = 0;
 3546:         my $css_class; 
 3547:         foreach my $item (@contexts) {
 3548:             if ($rownum%2) {
 3549:                 $css_class = '';
 3550:             } else {
 3551:                 $css_class = ' class="LC_odd_row" ';
 3552:             }
 3553:             $datatable .=   '<tr'.$css_class.'>'.
 3554:                             '<td>'.$title{$item}.
 3555:                             '</td><td class="LC_left_item">'.
 3556:                             '<span class="LC_nobreak">';
 3557:             foreach my $auth (@authtypes) {
 3558:                 $datatable .= '<label>'. 
 3559:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3560:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3561:                               $authname{$auth}.'</label>&nbsp;';
 3562:             }
 3563:             $datatable .= '</span></td></tr>';
 3564:             $rownum ++;
 3565:         }
 3566:         $$rowtotal += $rownum;
 3567:     }
 3568:     return $datatable;
 3569: }
 3570: 
 3571: sub captcha_choice {
 3572:     my ($context,$settings,$itemcount) = @_;
 3573:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3574:     my %lt = &captcha_phrases();
 3575:     $keyentry = 'hidden';
 3576:     if ($context eq 'cancreate') {
 3577:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3578:     } elsif ($context eq 'login') {
 3579:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3580:     }
 3581:     if (ref($settings) eq 'HASH') {
 3582:         if ($settings->{'captcha'}) {
 3583:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3584:         } else {
 3585:             $checked{'original'} = ' checked="checked"';
 3586:         }
 3587:         if ($settings->{'captcha'} eq 'recaptcha') {
 3588:             $pubtext = $lt{'pub'};
 3589:             $privtext = $lt{'priv'};
 3590:             $keyentry = 'text';
 3591:         }
 3592:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3593:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3594:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3595:         }
 3596:     } else {
 3597:         $checked{'original'} = ' checked="checked"';
 3598:     }
 3599:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3600:     my $output = '<tr'.$css_class.'>'.
 3601:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3602:                  '<table><tr><td>'."\n";
 3603:     foreach my $option ('original','recaptcha','notused') {
 3604:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3605:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3606:                    $lt{$option}.'</label></span>';
 3607:         unless ($option eq 'notused') {
 3608:             $output .= ('&nbsp;'x2)."\n";
 3609:         }
 3610:     }
 3611: #
 3612: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3613: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3614: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 3615: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3616: #
 3617:     $output .= '</td></tr>'."\n".
 3618:                '<tr><td>'."\n".
 3619:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3620:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3621:                $currpub.'" size="40" /></span><br />'."\n".
 3622:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3623:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3624:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3625:                '</td></tr>';
 3626:     return $output;
 3627: }
 3628: 
 3629: sub user_formats_row {
 3630:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3631:     my $output;
 3632:     my %text = (
 3633:                    'username' => 'new usernames',
 3634:                    'id'       => 'IDs',
 3635:                    'email'    => 'self-created accounts (e-mail)',
 3636:                );
 3637:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3638:     $output = '<tr '.$css_class.'>'.
 3639:               '<td><span class="LC_nobreak">';
 3640:     if ($type eq 'email') {
 3641:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3642:     } else {
 3643:         $output .= &mt("Format rules to check for $text{$type}: ");
 3644:     }
 3645:     $output .= '</span></td>'.
 3646:                '<td class="LC_left_item" colspan="2"><table>';
 3647:     my $rem;
 3648:     if (ref($ruleorder) eq 'ARRAY') {
 3649:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3650:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3651:                 my $rem = $i%($numinrow);
 3652:                 if ($rem == 0) {
 3653:                     if ($i > 0) {
 3654:                         $output .= '</tr>';
 3655:                     }
 3656:                     $output .= '<tr>';
 3657:                 }
 3658:                 my $check = ' ';
 3659:                 if (ref($settings) eq 'HASH') {
 3660:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3661:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3662:                             $check = ' checked="checked" ';
 3663:                         }
 3664:                     }
 3665:                 }
 3666:                 $output .= '<td class="LC_left_item">'.
 3667:                            '<span class="LC_nobreak"><label>'.
 3668:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3669:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3670:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3671:             }
 3672:         }
 3673:         $rem = @{$ruleorder}%($numinrow);
 3674:     }
 3675:     my $colsleft = $numinrow - $rem;
 3676:     if ($colsleft > 1 ) {
 3677:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3678:                    '&nbsp;</td>';
 3679:     } elsif ($colsleft == 1) {
 3680:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3681:     }
 3682:     $output .= '</tr></table></td></tr>';
 3683:     return $output;
 3684: }
 3685: 
 3686: sub usercreation_types {
 3687:     my %lt = &Apache::lonlocal::texthash (
 3688:                     author     => 'When adding a co-author',
 3689:                     course     => 'When adding a user to a course',
 3690:                     requestcrs => 'When requesting a course',
 3691:                     selfcreate => 'User creates own account', 
 3692:                     any        => 'Any',
 3693:                     official   => 'Institutional only ',
 3694:                     unofficial => 'Non-institutional only',
 3695:                     email      => 'E-mail address',
 3696:                     login      => 'Institutional Login',
 3697:                     sso        => 'SSO', 
 3698:                     none       => 'None',
 3699:     );
 3700:     return %lt;
 3701: }
 3702: 
 3703: sub authtype_names {
 3704:     my %lt = &Apache::lonlocal::texthash(
 3705:                       int    => 'Internal',
 3706:                       krb4   => 'Kerberos 4',
 3707:                       krb5   => 'Kerberos 5',
 3708:                       loc    => 'Local',
 3709:                   );
 3710:     return %lt;
 3711: }
 3712: 
 3713: sub context_names {
 3714:     my %context_title = &Apache::lonlocal::texthash(
 3715:        author => 'Creating users when an Author',
 3716:        course => 'Creating users when in a course',
 3717:        domain => 'Creating users when a Domain Coordinator',
 3718:     );
 3719:     return %context_title;
 3720: }
 3721: 
 3722: sub print_usermodification {
 3723:     my ($position,$dom,$settings,$rowtotal) = @_;
 3724:     my $numinrow = 4;
 3725:     my ($context,$datatable,$rowcount);
 3726:     if ($position eq 'top') {
 3727:         $rowcount = 0;
 3728:         $context = 'author'; 
 3729:         foreach my $role ('ca','aa') {
 3730:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3731:                                                    $numinrow,$rowcount);
 3732:             $$rowtotal ++;
 3733:             $rowcount ++;
 3734:         }
 3735:     } elsif ($position eq 'middle') {
 3736:         $context = 'course';
 3737:         $rowcount = 0;
 3738:         foreach my $role ('st','ep','ta','in','cr') {
 3739:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3740:                                                    $numinrow,$rowcount);
 3741:             $$rowtotal ++;
 3742:             $rowcount ++;
 3743:         }
 3744:     } elsif ($position eq 'bottom') {
 3745:         $context = 'selfcreate';
 3746:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3747:         $usertypes->{'default'} = $othertitle;
 3748:         if (ref($types) eq 'ARRAY') {
 3749:             push(@{$types},'default');
 3750:             $usertypes->{'default'} = $othertitle;
 3751:             foreach my $status (@{$types}) {
 3752:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3753:                                                        $numinrow,$rowcount,$usertypes);
 3754:                 $$rowtotal ++;
 3755:                 $rowcount ++;
 3756:             }
 3757:         }
 3758:     }
 3759:     return $datatable;
 3760: }
 3761: 
 3762: sub print_defaults {
 3763:     my ($dom,$settings,$rowtotal) = @_;
 3764:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3765:                  'datelocale_def','portal_def');
 3766:     my %defaults;
 3767:     if (ref($settings) eq 'HASH') {
 3768:         %defaults = %{$settings};
 3769:     } else {
 3770:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 3771:         foreach my $item (@items) {
 3772:             $defaults{$item} = $domdefaults{$item};
 3773:         }
 3774:     }
 3775:     my $titles = &defaults_titles($dom);
 3776:     my $rownum = 0;
 3777:     my ($datatable,$css_class);
 3778:     foreach my $item (@items) {
 3779:         if ($rownum%2) {
 3780:             $css_class = '';
 3781:         } else {
 3782:             $css_class = ' class="LC_odd_row" ';
 3783:         }
 3784:         $datatable .= '<tr'.$css_class.'>'.
 3785:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3786:                   '</span></td><td class="LC_right_item">';
 3787:         if ($item eq 'auth_def') {
 3788:             my @authtypes = ('internal','krb4','krb5','localauth');
 3789:             my %shortauth = (
 3790:                              internal => 'int',
 3791:                              krb4 => 'krb4',
 3792:                              krb5 => 'krb5',
 3793:                              localauth  => 'loc'
 3794:                            );
 3795:             my %authnames = &authtype_names();
 3796:             foreach my $auth (@authtypes) {
 3797:                 my $checked = ' ';
 3798:                 if ($defaults{$item} eq $auth) {
 3799:                     $checked = ' checked="checked" ';
 3800:                 }
 3801:                 $datatable .= '<label><input type="radio" name="'.$item.
 3802:                               '" value="'.$auth.'"'.$checked.'/>'.
 3803:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3804:             }
 3805:         } elsif ($item eq 'timezone_def') {
 3806:             my $includeempty = 1;
 3807:             $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 3808:         } elsif ($item eq 'datelocale_def') {
 3809:             my $includeempty = 1;
 3810:             $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 3811:         } elsif ($item eq 'lang_def') {
 3812:             my %langchoices = &get_languages_hash();
 3813:             $langchoices{''} = 'No language preference';
 3814:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 3815:             $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 3816:                                                           \%langchoices);
 3817:         } else {
 3818:             my $size;
 3819:             if ($item eq 'portal_def') {
 3820:                 $size = ' size="25"';
 3821:             }
 3822:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3823:                           $defaults{$item}.'"'.$size.' />';
 3824:         }
 3825:         $datatable .= '</td></tr>';
 3826:         $rownum ++;
 3827:     }
 3828:     $$rowtotal += $rownum;
 3829:     return $datatable;
 3830: }
 3831: 
 3832: sub get_languages_hash {
 3833:     my %langchoices;
 3834:     foreach my $id (&Apache::loncommon::languageids()) {
 3835:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 3836:         if ($code ne '') {
 3837:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 3838:         }
 3839:     }
 3840:     return %langchoices;
 3841: }
 3842: 
 3843: sub defaults_titles {
 3844:     my ($dom) = @_;
 3845:     my %titles = &Apache::lonlocal::texthash (
 3846:                    'auth_def'      => 'Default authentication type',
 3847:                    'auth_arg_def'  => 'Default authentication argument',
 3848:                    'lang_def'      => 'Default language',
 3849:                    'timezone_def'  => 'Default timezone',
 3850:                    'datelocale_def' => 'Default locale for dates',
 3851:                    'portal_def'     => 'Portal/Default URL',
 3852:                  );
 3853:     if ($dom) {
 3854:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3855:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3856:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3857:         $protocol = 'http' if ($protocol ne 'https');
 3858:         if ($uint_dom) {
 3859:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3860:                                          $uint_dom);
 3861:         }
 3862:     }
 3863:     return (\%titles);
 3864: }
 3865: 
 3866: sub print_scantronformat {
 3867:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3868:     my $itemcount = 1;
 3869:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3870:         %confhash);
 3871:     my $switchserver = &check_switchserver($dom,$confname);
 3872:     my %lt = &Apache::lonlocal::texthash (
 3873:                 default => 'Default bubblesheet format file error',
 3874:                 custom  => 'Custom bubblesheet format file error',
 3875:              );
 3876:     my %scantronfiles = (
 3877:         default => 'default.tab',
 3878:         custom => 'custom.tab',
 3879:     );
 3880:     foreach my $key (keys(%scantronfiles)) {
 3881:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3882:                               .$scantronfiles{$key};
 3883:     }
 3884:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3885:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3886:         if (!$switchserver) {
 3887:             my $servadm = $r->dir_config('lonAdmEMail');
 3888:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3889:             if ($configuserok eq 'ok') {
 3890:                 if ($author_ok eq 'ok') {
 3891:                     my %legacyfile = (
 3892:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3893:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3894:                     );
 3895:                     my %md5chk;
 3896:                     foreach my $type (keys(%legacyfile)) {
 3897:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3898:                         chomp($md5chk{$type});
 3899:                     }
 3900:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3901:                         foreach my $type (keys(%legacyfile)) {
 3902:                             ($scantronurls{$type},my $error) = 
 3903:                                 &legacy_scantronformat($r,$dom,$confname,
 3904:                                                  $type,$legacyfile{$type},
 3905:                                                  $scantronurls{$type},
 3906:                                                  $scantronfiles{$type});
 3907:                             if ($error ne '') {
 3908:                                 $error{$type} = $error;
 3909:                             }
 3910:                         }
 3911:                         if (keys(%error) == 0) {
 3912:                             $is_custom = 1;
 3913:                             $confhash{'scantron'}{'scantronformat'} = 
 3914:                                 $scantronurls{'custom'};
 3915:                             my $putresult = 
 3916:                                 &Apache::lonnet::put_dom('configuration',
 3917:                                                          \%confhash,$dom);
 3918:                             if ($putresult ne 'ok') {
 3919:                                 $error{'custom'} = 
 3920:                                     '<span class="LC_error">'.
 3921:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3922:                             }
 3923:                         }
 3924:                     } else {
 3925:                         ($scantronurls{'default'},my $error) =
 3926:                             &legacy_scantronformat($r,$dom,$confname,
 3927:                                           'default',$legacyfile{'default'},
 3928:                                           $scantronurls{'default'},
 3929:                                           $scantronfiles{'default'});
 3930:                         if ($error eq '') {
 3931:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3932:                             my $putresult =
 3933:                                 &Apache::lonnet::put_dom('configuration',
 3934:                                                          \%confhash,$dom);
 3935:                             if ($putresult ne 'ok') {
 3936:                                 $error{'default'} =
 3937:                                     '<span class="LC_error">'.
 3938:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3939:                             }
 3940:                         } else {
 3941:                             $error{'default'} = $error;
 3942:                         }
 3943:                     }
 3944:                 }
 3945:             }
 3946:         } else {
 3947:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3948:         }
 3949:     }
 3950:     if (ref($settings) eq 'HASH') {
 3951:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3952:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3953:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3954:                 $scantronurl = '';
 3955:             } else {
 3956:                 $scantronurl = $settings->{'scantronformat'};
 3957:             }
 3958:             $is_custom = 1;
 3959:         } else {
 3960:             $scantronurl = $scantronurls{'default'};
 3961:         }
 3962:     } else {
 3963:         if ($is_custom) {
 3964:             $scantronurl = $scantronurls{'custom'};
 3965:         } else {
 3966:             $scantronurl = $scantronurls{'default'};
 3967:         }
 3968:     }
 3969:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3970:     $datatable .= '<tr'.$css_class.'>';
 3971:     if (!$is_custom) {
 3972:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3973:                       '<span class="LC_nobreak">';
 3974:         if ($scantronurl) {
 3975:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 3976:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 3977:         } else {
 3978:             $datatable = &mt('File unavailable for display');
 3979:         }
 3980:         $datatable .= '</span></td>';
 3981:         if (keys(%error) == 0) { 
 3982:             $datatable .= '<td valign="bottom">';
 3983:             if (!$switchserver) {
 3984:                 $datatable .= &mt('Upload:').'<br />';
 3985:             }
 3986:         } else {
 3987:             my $errorstr;
 3988:             foreach my $key (sort(keys(%error))) {
 3989:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3990:             }
 3991:             $datatable .= '<td>'.$errorstr;
 3992:         }
 3993:     } else {
 3994:         if (keys(%error) > 0) {
 3995:             my $errorstr;
 3996:             foreach my $key (sort(keys(%error))) {
 3997:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3998:             } 
 3999:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4000:         } elsif ($scantronurl) {
 4001:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4002:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4003:             $datatable .= '<td><span class="LC_nobreak">'.
 4004:                           $link.
 4005:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4006:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4007:                           '<td><span class="LC_nobreak">&nbsp;'.
 4008:                           &mt('Replace:').'</span><br />';
 4009:         }
 4010:     }
 4011:     if (keys(%error) == 0) {
 4012:         if ($switchserver) {
 4013:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4014:         } else {
 4015:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4016:                          '<input type="file" name="scantronformat" /></span>';
 4017:         }
 4018:     }
 4019:     $datatable .= '</td></tr>';
 4020:     $$rowtotal ++;
 4021:     return $datatable;
 4022: }
 4023: 
 4024: sub legacy_scantronformat {
 4025:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4026:     my ($url,$error);
 4027:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4028:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4029:         (my $result,$url) =
 4030:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4031:                          '','',$newfile);
 4032:         if ($result ne 'ok') {
 4033:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4034:         }
 4035:     }
 4036:     return ($url,$error);
 4037: }
 4038: 
 4039: sub print_coursecategories {
 4040:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4041:     my $datatable;
 4042:     if ($position eq 'top') {
 4043:         my $toggle_cats_crs = ' ';
 4044:         my $toggle_cats_dom = ' checked="checked" ';
 4045:         my $can_cat_crs = ' ';
 4046:         my $can_cat_dom = ' checked="checked" ';
 4047:         my $toggle_catscomm_comm = ' ';
 4048:         my $toggle_catscomm_dom = ' checked="checked" ';
 4049:         my $can_catcomm_comm = ' ';
 4050:         my $can_catcomm_dom = ' checked="checked" ';
 4051: 
 4052:         if (ref($settings) eq 'HASH') {
 4053:             if ($settings->{'togglecats'} eq 'crs') {
 4054:                 $toggle_cats_crs = $toggle_cats_dom;
 4055:                 $toggle_cats_dom = ' ';
 4056:             }
 4057:             if ($settings->{'categorize'} eq 'crs') {
 4058:                 $can_cat_crs = $can_cat_dom;
 4059:                 $can_cat_dom = ' ';
 4060:             }
 4061:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4062:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4063:                 $toggle_catscomm_dom = ' ';
 4064:             }
 4065:             if ($settings->{'categorizecomm'} eq 'comm') {
 4066:                 $can_catcomm_comm = $can_catcomm_dom;
 4067:                 $can_catcomm_dom = ' ';
 4068:             }
 4069:         }
 4070:         my %title = &Apache::lonlocal::texthash (
 4071:                      togglecats     => 'Show/Hide a course in catalog',
 4072:                      togglecatscomm => 'Show/Hide a community in catalog',
 4073:                      categorize     => 'Assign a category to a course',
 4074:                      categorizecomm => 'Assign a category to a community',
 4075:                     );
 4076:         my %level = &Apache::lonlocal::texthash (
 4077:                      dom  => 'Set in Domain',
 4078:                      crs  => 'Set in Course',
 4079:                      comm => 'Set in Community',
 4080:                     );
 4081:         $datatable = '<tr class="LC_odd_row">'.
 4082:                   '<td>'.$title{'togglecats'}.'</td>'.
 4083:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4084:                   '<input type="radio" name="togglecats"'.
 4085:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4086:                   '<label><input type="radio" name="togglecats"'.
 4087:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4088:                   '</tr><tr>'.
 4089:                   '<td>'.$title{'categorize'}.'</td>'.
 4090:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4091:                   '<label><input type="radio" name="categorize"'.
 4092:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4093:                   '<label><input type="radio" name="categorize"'.
 4094:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4095:                   '</tr><tr class="LC_odd_row">'.
 4096:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4097:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4098:                   '<input type="radio" name="togglecatscomm"'.
 4099:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4100:                   '<label><input type="radio" name="togglecatscomm"'.
 4101:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4102:                   '</tr><tr>'.
 4103:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4104:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4105:                   '<label><input type="radio" name="categorizecomm"'.
 4106:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4107:                   '<label><input type="radio" name="categorizecomm"'.
 4108:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4109:                   '</tr>';
 4110:         $$rowtotal += 4;
 4111:     } else {
 4112:         my $css_class;
 4113:         my $itemcount = 1;
 4114:         my $cathash; 
 4115:         if (ref($settings) eq 'HASH') {
 4116:             $cathash = $settings->{'cats'};
 4117:         }
 4118:         if (ref($cathash) eq 'HASH') {
 4119:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4120:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4121:                                                    \%allitems,\%idx,\@jsarray);
 4122:             my $maxdepth = scalar(@cats);
 4123:             my $colattrib = '';
 4124:             if ($maxdepth > 2) {
 4125:                 $colattrib = ' colspan="2" ';
 4126:             }
 4127:             my @path;
 4128:             if (@cats > 0) {
 4129:                 if (ref($cats[0]) eq 'ARRAY') {
 4130:                     my $numtop = @{$cats[0]};
 4131:                     my $maxnum = $numtop;
 4132:                     my %default_names = (
 4133:                           instcode    => &mt('Official courses'),
 4134:                           communities => &mt('Communities'),
 4135:                     );
 4136: 
 4137:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4138:                         ($cathash->{'instcode::0'} eq '') ||
 4139:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4140:                         ($cathash->{'communities::0'} eq '')) {
 4141:                         $maxnum ++;
 4142:                     }
 4143:                     my $lastidx;
 4144:                     for (my $i=0; $i<$numtop; $i++) {
 4145:                         my $parent = $cats[0][$i];
 4146:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4147:                         my $item = &escape($parent).'::0';
 4148:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4149:                         $lastidx = $idx{$item};
 4150:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4151:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4152:                         for (my $k=0; $k<=$maxnum; $k++) {
 4153:                             my $vpos = $k+1;
 4154:                             my $selstr;
 4155:                             if ($k == $i) {
 4156:                                 $selstr = ' selected="selected" ';
 4157:                             }
 4158:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4159:                         }
 4160:                         $datatable .= '</select></span></td><td>';
 4161:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4162:                             $datatable .=  '<span class="LC_nobreak">'
 4163:                                            .$default_names{$parent}.'</span>';
 4164:                             if ($parent eq 'instcode') {
 4165:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4166:                                               .&mt('with institutional codes')
 4167:                                               .')</span></td><td'.$colattrib.'>';
 4168:                             } else {
 4169:                                 $datatable .= '<table><tr><td>';
 4170:                             }
 4171:                             $datatable .= '<span class="LC_nobreak">'
 4172:                                           .'<label><input type="radio" name="'
 4173:                                           .$parent.'" value="1" checked="checked" />'
 4174:                                           .&mt('Display').'</label>';
 4175:                             if ($parent eq 'instcode') {
 4176:                                 $datatable .= '&nbsp;';
 4177:                             } else {
 4178:                                 $datatable .= '</span></td></tr><tr><td>'
 4179:                                               .'<span class="LC_nobreak">';
 4180:                             }
 4181:                             $datatable .= '<label><input type="radio" name="'
 4182:                                           .$parent.'" value="0" />'
 4183:                                           .&mt('Do not display').'</label></span>';
 4184:                             if ($parent eq 'communities') {
 4185:                                 $datatable .= '</td></tr></table>';
 4186:                             }
 4187:                             $datatable .= '</td>';
 4188:                         } else {
 4189:                             $datatable .= $parent
 4190:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 4191:                                           .'<input type="checkbox" name="deletecategory" '
 4192:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4193:                         }
 4194:                         my $depth = 1;
 4195:                         push(@path,$parent);
 4196:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4197:                         pop(@path);
 4198:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4199:                         $itemcount ++;
 4200:                     }
 4201:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4202:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4203:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4204:                     for (my $k=0; $k<=$maxnum; $k++) {
 4205:                         my $vpos = $k+1;
 4206:                         my $selstr;
 4207:                         if ($k == $numtop) {
 4208:                             $selstr = ' selected="selected" ';
 4209:                         }
 4210:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4211:                     }
 4212:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4213:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4214:                                   .'</tr>'."\n";
 4215:                     $itemcount ++;
 4216:                     foreach my $default ('instcode','communities') {
 4217:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4218:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4219:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4220:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4221:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4222:                             for (my $k=0; $k<=$maxnum; $k++) {
 4223:                                 my $vpos = $k+1;
 4224:                                 my $selstr;
 4225:                                 if ($k == $maxnum) {
 4226:                                     $selstr = ' selected="selected" ';
 4227:                                 }
 4228:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4229:                             }
 4230:                             $datatable .= '</select></span></td>'.
 4231:                                           '<td><span class="LC_nobreak">'.
 4232:                                           $default_names{$default}.'</span>';
 4233:                             if ($default eq 'instcode') {
 4234:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4235:                                               .&mt('with institutional codes').')</span>';
 4236:                             }
 4237:                             $datatable .= '</td>'
 4238:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4239:                                           .&mt('Display').'</label>&nbsp;'
 4240:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4241:                                           .&mt('Do not display').'</label></span></td></tr>';
 4242:                         }
 4243:                     }
 4244:                 }
 4245:             } else {
 4246:                 $datatable .= &initialize_categories($itemcount);
 4247:             }
 4248:         } else {
 4249:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4250:                           .&initialize_categories($itemcount);
 4251:         }
 4252:         $$rowtotal += $itemcount;
 4253:     }
 4254:     return $datatable;
 4255: }
 4256: 
 4257: sub print_serverstatuses {
 4258:     my ($dom,$settings,$rowtotal) = @_;
 4259:     my $datatable;
 4260:     my @pages = &serverstatus_pages();
 4261:     my (%namedaccess,%machineaccess);
 4262:     foreach my $type (@pages) {
 4263:         $namedaccess{$type} = '';
 4264:         $machineaccess{$type}= '';
 4265:     }
 4266:     if (ref($settings) eq 'HASH') {
 4267:         foreach my $type (@pages) {
 4268:             if (exists($settings->{$type})) {
 4269:                 if (ref($settings->{$type}) eq 'HASH') {
 4270:                     foreach my $key (keys(%{$settings->{$type}})) {
 4271:                         if ($key eq 'namedusers') {
 4272:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4273:                         } elsif ($key eq 'machines') {
 4274:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4275:                         }
 4276:                     }
 4277:                 }
 4278:             }
 4279:         }
 4280:     }
 4281:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4282:     my $rownum = 0;
 4283:     my $css_class;
 4284:     foreach my $type (@pages) {
 4285:         $rownum ++;
 4286:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4287:         $datatable .= '<tr'.$css_class.'>'.
 4288:                       '<td><span class="LC_nobreak">'.
 4289:                       $titles->{$type}.'</span></td>'.
 4290:                       '<td class="LC_left_item">'.
 4291:                       '<input type="text" name="'.$type.'_namedusers" '.
 4292:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4293:                       '<td class="LC_right_item">'.
 4294:                       '<span class="LC_nobreak">'.
 4295:                       '<input type="text" name="'.$type.'_machines" '.
 4296:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4297:                       '</td></tr>'."\n";
 4298:     }
 4299:     $$rowtotal += $rownum;
 4300:     return $datatable;
 4301: }
 4302: 
 4303: sub serverstatus_pages {
 4304:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4305:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4306:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4307: }
 4308: 
 4309: sub coursecategories_javascript {
 4310:     my ($settings) = @_;
 4311:     my ($output,$jstext,$cathash);
 4312:     if (ref($settings) eq 'HASH') {
 4313:         $cathash = $settings->{'cats'};
 4314:     }
 4315:     if (ref($cathash) eq 'HASH') {
 4316:         my (@cats,@jsarray,%idx);
 4317:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4318:         if (@jsarray > 0) {
 4319:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4320:             for (my $i=0; $i<@jsarray; $i++) {
 4321:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4322:                     my $catstr = join('","',@{$jsarray[$i]});
 4323:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4324:                 }
 4325:             }
 4326:         }
 4327:     } else {
 4328:         $jstext  = '    var categories = Array(1);'."\n".
 4329:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4330:     }
 4331:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4332:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4333:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4334:     $output = <<"ENDSCRIPT";
 4335: <script type="text/javascript">
 4336: // <![CDATA[
 4337: function reorderCats(form,parent,item,idx) {
 4338:     var changedVal;
 4339: $jstext
 4340:     var newpos = 'addcategory_pos';
 4341:     var current = new Array;
 4342:     if (parent == '') {
 4343:         var has_instcode = 0;
 4344:         var maxtop = categories[idx].length;
 4345:         for (var j=0; j<maxtop; j++) {
 4346:             if (categories[idx][j] == 'instcode::0') {
 4347:                 has_instcode == 1;
 4348:             }
 4349:         }
 4350:         if (has_instcode == 0) {
 4351:             categories[idx][maxtop] = 'instcode_pos';
 4352:         }
 4353:     } else {
 4354:         newpos += '_'+parent;
 4355:     }
 4356:     var maxh = 1 + categories[idx].length;
 4357:     var current = new Array;
 4358:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4359:     if (item == newpos) {
 4360:         changedVal = newitemVal;
 4361:     } else {
 4362:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4363:         current[newitemVal] = newpos;
 4364:     }
 4365:     for (var i=0; i<categories[idx].length; i++) {
 4366:         var elementName = categories[idx][i];
 4367:         if (elementName != item) {
 4368:             if (form.elements[elementName]) {
 4369:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4370:                 current[currVal] = elementName;
 4371:             }
 4372:         }
 4373:     }
 4374:     var oldVal;
 4375:     for (var j=0; j<maxh; j++) {
 4376:         if (current[j] == undefined) {
 4377:             oldVal = j;
 4378:         }
 4379:     }
 4380:     if (oldVal < changedVal) {
 4381:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4382:            var elementName = current[k];
 4383:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4384:         }
 4385:     } else {
 4386:         for (var k=changedVal; k<oldVal; k++) {
 4387:             var elementName = current[k];
 4388:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4389:         }
 4390:     }
 4391:     return;
 4392: }
 4393: 
 4394: function categoryCheck(form) {
 4395:     if (form.elements['addcategory_name'].value == 'instcode') {
 4396:         alert('$instcode_reserved\\n$choose_again');
 4397:         return false;
 4398:     }
 4399:     if (form.elements['addcategory_name'].value == 'communities') {
 4400:         alert('$communities_reserved\\n$choose_again');
 4401:         return false;
 4402:     }
 4403:     return true;
 4404: }
 4405: 
 4406: // ]]>
 4407: </script>
 4408: 
 4409: ENDSCRIPT
 4410:     return $output;
 4411: }
 4412: 
 4413: sub initialize_categories {
 4414:     my ($itemcount) = @_;
 4415:     my ($datatable,$css_class,$chgstr);
 4416:     my %default_names = (
 4417:                       instcode    => 'Official courses (with institutional codes)',
 4418:                       communities => 'Communities',
 4419:                         );
 4420:     my $select0 = ' selected="selected"';
 4421:     my $select1 = '';
 4422:     foreach my $default ('instcode','communities') {
 4423:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4424:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4425:         if ($default eq 'communities') {
 4426:             $select1 = $select0;
 4427:             $select0 = '';
 4428:         }
 4429:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4430:                      .'<select name="'.$default.'_pos">'
 4431:                      .'<option value="0"'.$select0.'>1</option>'
 4432:                      .'<option value="1"'.$select1.'>2</option>'
 4433:                      .'<option value="2">3</option></select>&nbsp;'
 4434:                      .$default_names{$default}
 4435:                      .'</span></td><td><span class="LC_nobreak">'
 4436:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4437:                      .&mt('Display').'</label>&nbsp;<label>'
 4438:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4439:                  .'</label></span></td></tr>';
 4440:         $itemcount ++;
 4441:     }
 4442:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4443:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4444:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4445:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4446:                   .'<option value="0">1</option>'
 4447:                   .'<option value="1">2</option>'
 4448:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4449:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4450:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4451:     return $datatable;
 4452: }
 4453: 
 4454: sub build_category_rows {
 4455:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4456:     my ($text,$name,$item,$chgstr);
 4457:     if (ref($cats) eq 'ARRAY') {
 4458:         my $maxdepth = scalar(@{$cats});
 4459:         if (ref($cats->[$depth]) eq 'HASH') {
 4460:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4461:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4462:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4463:                 $text .= '<td><table class="LC_data_table">';
 4464:                 my ($idxnum,$parent_name,$parent_item);
 4465:                 my $higher = $depth - 1;
 4466:                 if ($higher == 0) {
 4467:                     $parent_name = &escape($parent).'::'.$higher;
 4468:                 } else {
 4469:                     if (ref($path) eq 'ARRAY') {
 4470:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4471:                     }
 4472:                 }
 4473:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4474:                 for (my $j=0; $j<=$numchildren; $j++) {
 4475:                     if ($j < $numchildren) {
 4476:                         $name = $cats->[$depth]{$parent}[$j];
 4477:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4478:                         $idxnum = $idx->{$item};
 4479:                     } else {
 4480:                         $name = $parent_name;
 4481:                         $item = $parent_item;
 4482:                     }
 4483:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4484:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4485:                     for (my $i=0; $i<=$numchildren; $i++) {
 4486:                         my $vpos = $i+1;
 4487:                         my $selstr;
 4488:                         if ($j == $i) {
 4489:                             $selstr = ' selected="selected" ';
 4490:                         }
 4491:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4492:                     }
 4493:                     $text .= '</select>&nbsp;';
 4494:                     if ($j < $numchildren) {
 4495:                         my $deeper = $depth+1;
 4496:                         $text .= $name.'&nbsp;'
 4497:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4498:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4499:                         if(ref($path) eq 'ARRAY') {
 4500:                             push(@{$path},$name);
 4501:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4502:                             pop(@{$path});
 4503:                         }
 4504:                     } else {
 4505:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4506:                         if ($j == $numchildren) {
 4507:                             $text .= $name;
 4508:                         } else {
 4509:                             $text .= $item;
 4510:                         }
 4511:                         $text .= '" value="" />';
 4512:                     }
 4513:                     $text .= '</td></tr>';
 4514:                 }
 4515:                 $text .= '</table></td>';
 4516:             } else {
 4517:                 my $higher = $depth-1;
 4518:                 if ($higher == 0) {
 4519:                     $name = &escape($parent).'::'.$higher;
 4520:                 } else {
 4521:                     if (ref($path) eq 'ARRAY') {
 4522:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4523:                     }
 4524:                 }
 4525:                 my $colspan;
 4526:                 if ($parent ne 'instcode') {
 4527:                     $colspan = $maxdepth - $depth - 1;
 4528:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4529:                 }
 4530:             }
 4531:         }
 4532:     }
 4533:     return $text;
 4534: }
 4535: 
 4536: sub modifiable_userdata_row {
 4537:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4538:     my $rolename;
 4539:     if ($context eq 'selfcreate') {
 4540:         if (ref($usertypes) eq 'HASH') {
 4541:             $rolename = $usertypes->{$role};
 4542:         } else {
 4543:             $rolename = $role;
 4544:         }
 4545:     } else {
 4546:         if ($role eq 'cr') {
 4547:             $rolename = &mt('Custom role');
 4548:         } else {
 4549:             $rolename = &Apache::lonnet::plaintext($role);
 4550:         }
 4551:     }
 4552:     my @fields = ('lastname','firstname','middlename','generation',
 4553:                   'permanentemail','id');
 4554:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4555:     my $output;
 4556:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4557:     $output = '<tr '.$css_class.'>'.
 4558:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4559:               '<td class="LC_left_item" colspan="2"><table>';
 4560:     my $rem;
 4561:     my %checks;
 4562:     if (ref($settings) eq 'HASH') {
 4563:         if (ref($settings->{$context}) eq 'HASH') {
 4564:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4565:                 foreach my $field (@fields) {
 4566:                     if ($settings->{$context}->{$role}->{$field}) {
 4567:                         $checks{$field} = ' checked="checked" ';
 4568:                     }
 4569:                 }
 4570:             }
 4571:         }
 4572:     }
 4573:     for (my $i=0; $i<@fields; $i++) {
 4574:         my $rem = $i%($numinrow);
 4575:         if ($rem == 0) {
 4576:             if ($i > 0) {
 4577:                 $output .= '</tr>';
 4578:             }
 4579:             $output .= '<tr>';
 4580:         }
 4581:         my $check = ' ';
 4582:         if (exists($checks{$fields[$i]})) {
 4583:             $check = $checks{$fields[$i]}
 4584:         } else {
 4585:             if ($role eq 'st') {
 4586:                 if (ref($settings) ne 'HASH') {
 4587:                     $check = ' checked="checked" '; 
 4588:                 }
 4589:             }
 4590:         }
 4591:         $output .= '<td class="LC_left_item">'.
 4592:                    '<span class="LC_nobreak"><label>'.
 4593:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4594:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4595:                    '</label></span></td>';
 4596:         $rem = @fields%($numinrow);
 4597:     }
 4598:     my $colsleft = $numinrow - $rem;
 4599:     if ($colsleft > 1 ) {
 4600:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4601:                    '&nbsp;</td>';
 4602:     } elsif ($colsleft == 1) {
 4603:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4604:     }
 4605:     $output .= '</tr></table></td></tr>';
 4606:     return $output;
 4607: }
 4608: 
 4609: sub insttypes_row {
 4610:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4611:     my %lt = &Apache::lonlocal::texthash (
 4612:                       cansearch => 'Users allowed to search',
 4613:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4614:                       lockablenames => 'User preference to lock name',
 4615:              );
 4616:     my $showdom;
 4617:     if ($context eq 'cansearch') {
 4618:         $showdom = ' ('.$dom.')';
 4619:     }
 4620:     my $class = 'LC_left_item';
 4621:     if ($context eq 'statustocreate') {
 4622:         $class = 'LC_right_item';
 4623:     }
 4624:     my $output =  '<tr class="LC_odd_row">'.
 4625:                   '<td>'.$lt{$context}.$showdom.
 4626:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4627:     my $rem;
 4628:     if (ref($types) eq 'ARRAY') {
 4629:         for (my $i=0; $i<@{$types}; $i++) {
 4630:             if (defined($usertypes->{$types->[$i]})) {
 4631:                 my $rem = $i%($numinrow);
 4632:                 if ($rem == 0) {
 4633:                     if ($i > 0) {
 4634:                         $output .= '</tr>';
 4635:                     }
 4636:                     $output .= '<tr>';
 4637:                 }
 4638:                 my $check = ' ';
 4639:                 if (ref($settings) eq 'HASH') {
 4640:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4641:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4642:                             $check = ' checked="checked" ';
 4643:                         }
 4644:                     } elsif ($context eq 'statustocreate') {
 4645:                         $check = ' checked="checked" ';
 4646:                     }
 4647:                 }
 4648:                 $output .= '<td class="LC_left_item">'.
 4649:                            '<span class="LC_nobreak"><label>'.
 4650:                            '<input type="checkbox" name="'.$context.'" '.
 4651:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4652:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4653:             }
 4654:         }
 4655:         $rem = @{$types}%($numinrow);
 4656:     }
 4657:     my $colsleft = $numinrow - $rem;
 4658:     if (($rem == 0) && (@{$types} > 0)) {
 4659:         $output .= '<tr>';
 4660:     }
 4661:     if ($colsleft > 1) {
 4662:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4663:     } else {
 4664:         $output .= '<td class="LC_left_item">';
 4665:     }
 4666:     my $defcheck = ' ';
 4667:     if (ref($settings) eq 'HASH') {  
 4668:         if (ref($settings->{$context}) eq 'ARRAY') {
 4669:             if (grep(/^default$/,@{$settings->{$context}})) {
 4670:                 $defcheck = ' checked="checked" ';
 4671:             }
 4672:         } elsif ($context eq 'statustocreate') {
 4673:             $defcheck = ' checked="checked" ';
 4674:         }
 4675:     }
 4676:     $output .= '<span class="LC_nobreak"><label>'.
 4677:                '<input type="checkbox" name="'.$context.'" '.
 4678:                'value="default"'.$defcheck.'/>'.
 4679:                $othertitle.'</label></span></td>'.
 4680:                '</tr></table></td></tr>';
 4681:     return $output;
 4682: }
 4683: 
 4684: sub sorted_searchtitles {
 4685:     my %searchtitles = &Apache::lonlocal::texthash(
 4686:                          'uname' => 'username',
 4687:                          'lastname' => 'last name',
 4688:                          'lastfirst' => 'last name, first name',
 4689:                      );
 4690:     my @titleorder = ('uname','lastname','lastfirst');
 4691:     return (\%searchtitles,\@titleorder);
 4692: }
 4693: 
 4694: sub sorted_searchtypes {
 4695:     my %srchtypes_desc = (
 4696:                            exact    => 'is exact match',
 4697:                            contains => 'contains ..',
 4698:                            begins   => 'begins with ..',
 4699:                          );
 4700:     my @srchtypeorder = ('exact','begins','contains');
 4701:     return (\%srchtypes_desc,\@srchtypeorder);
 4702: }
 4703: 
 4704: sub usertype_update_row {
 4705:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4706:     my $datatable;
 4707:     my $numinrow = 4;
 4708:     foreach my $type (@{$types}) {
 4709:         if (defined($usertypes->{$type})) {
 4710:             $$rownums ++;
 4711:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4712:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4713:                           '</td><td class="LC_left_item"><table>';
 4714:             for (my $i=0; $i<@{$fields}; $i++) {
 4715:                 my $rem = $i%($numinrow);
 4716:                 if ($rem == 0) {
 4717:                     if ($i > 0) {
 4718:                         $datatable .= '</tr>';
 4719:                     }
 4720:                     $datatable .= '<tr>';
 4721:                 }
 4722:                 my $check = ' ';
 4723:                 if (ref($settings) eq 'HASH') {
 4724:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4725:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4726:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4727:                                 $check = ' checked="checked" ';
 4728:                             }
 4729:                         }
 4730:                     }
 4731:                 }
 4732: 
 4733:                 if ($i == @{$fields}-1) {
 4734:                     my $colsleft = $numinrow - $rem;
 4735:                     if ($colsleft > 1) {
 4736:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4737:                     } else {
 4738:                         $datatable .= '<td>';
 4739:                     }
 4740:                 } else {
 4741:                     $datatable .= '<td>';
 4742:                 }
 4743:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4744:                               '<input type="checkbox" name="updateable_'.$type.
 4745:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4746:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4747:             }
 4748:             $datatable .= '</tr></table></td></tr>';
 4749:         }
 4750:     }
 4751:     return $datatable;
 4752: }
 4753: 
 4754: sub modify_login {
 4755:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 4756:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4757:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4758:     %title = ( coursecatalog => 'Display course catalog',
 4759:                adminmail => 'Display administrator E-mail address',
 4760:                helpdesk  => 'Display "Contact Helpdesk" link',
 4761:                newuser => 'Link for visitors to create a user account',
 4762:                loginheader => 'Log-in box header');
 4763:     @offon = ('off','on');
 4764:     if (ref($domconfig{login}) eq 'HASH') {
 4765:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4766:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4767:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4768:             }
 4769:         }
 4770:     }
 4771:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4772:                                            \%domconfig,\%loginhash);
 4773:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 4774:     foreach my $item (@toggles) {
 4775:         $loginhash{login}{$item} = $env{'form.'.$item};
 4776:     }
 4777:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4778:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4779:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4780:                                          \%loginhash);
 4781:     }
 4782: 
 4783:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4784:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4785:     if (keys(%servers) > 1) {
 4786:         foreach my $lonhost (keys(%servers)) {
 4787:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4788:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4789:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4790:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4791:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4792:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4793:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4794:                         $changes{'loginvia'}{$lonhost} = 1;
 4795:                     } else {
 4796:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4797:                         $changes{'loginvia'}{$lonhost} = 1;
 4798:                     }
 4799:                 } else {
 4800:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4801:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4802:                         $changes{'loginvia'}{$lonhost} = 1;
 4803:                     }
 4804:                 }
 4805:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4806:                     foreach my $item (@loginvia_attribs) {
 4807:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4808:                     }
 4809:                 } else {
 4810:                     foreach my $item (@loginvia_attribs) {
 4811:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4812:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4813:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4814:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4815:                                 $new = '/';
 4816:                             }
 4817:                         }
 4818:                         if (($item eq 'custompath') && 
 4819:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4820:                             $new = '';
 4821:                         }
 4822:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4823:                             $changes{'loginvia'}{$lonhost} = 1;
 4824:                         }
 4825:                         if ($item eq 'exempt') {
 4826:                             $new =~ s/^\s+//;
 4827:                             $new =~ s/\s+$//;
 4828:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4829:                             my @okips;
 4830:                             foreach my $ip (@poss_ips) {
 4831:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4832:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4833:                                         push(@okips,$ip); 
 4834:                                     }
 4835:                                 }
 4836:                             }
 4837:                             if (@okips > 0) {
 4838:                                 $new = join(',',@okips); 
 4839:                             } else {
 4840:                                 $new = ''; 
 4841:                             }
 4842:                         }
 4843:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4844:                     }
 4845:                 }
 4846:             } else {
 4847:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4848:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4849:                     $changes{'loginvia'}{$lonhost} = 1;
 4850:                     foreach my $item (@loginvia_attribs) {
 4851:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4852:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4853:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4854:                                 $new = '/';
 4855:                             }
 4856:                         }
 4857:                         if (($item eq 'custompath') && 
 4858:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4859:                             $new = '';
 4860:                         }
 4861:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4862:                     }
 4863:                 }
 4864:             }
 4865:         }
 4866:     }
 4867: 
 4868:     my $servadm = $r->dir_config('lonAdmEMail');
 4869:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 4870:     if (ref($domconfig{'login'}) eq 'HASH') {
 4871:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 4872:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 4873:                 if ($lang eq 'nolang') {
 4874:                     push(@currlangs,$lang);
 4875:                 } elsif (defined($langchoices{$lang})) {
 4876:                     push(@currlangs,$lang);
 4877:                 } else {
 4878:                     next;
 4879:                 }
 4880:             }
 4881:         }
 4882:     }
 4883:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 4884:     if (@currlangs > 0) {
 4885:         foreach my $lang (@currlangs) {
 4886:             if (grep(/^\Q$lang\E$/,@delurls)) {
 4887:                 $changes{'helpurl'}{$lang} = 1;
 4888:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 4889:                 $changes{'helpurl'}{$lang} = 1;
 4890:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 4891:                 push(@newlangs,$lang);
 4892:             } else {
 4893:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4894:             }
 4895:         }
 4896:     }
 4897:     unless (grep(/^nolang$/,@currlangs)) {
 4898:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 4899:             $changes{'helpurl'}{'nolang'} = 1;
 4900:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 4901:             push(@newlangs,'nolang');
 4902:         }
 4903:     }
 4904:     if ($env{'form.loginhelpurl_add_lang'}) {
 4905:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 4906:             ($env{'form.loginhelpurl_add_file.filename'})) {
 4907:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 4908:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 4909:         }
 4910:     }
 4911:     if ((@newlangs > 0) || ($addedfile)) {
 4912:         my $error;
 4913:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 4914:         if ($configuserok eq 'ok') {
 4915:             if ($switchserver) {
 4916:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 4917:             } elsif ($author_ok eq 'ok') {
 4918:                 my @allnew = @newlangs;
 4919:                 if ($addedfile ne '') {
 4920:                     push(@allnew,$addedfile);
 4921:                 }
 4922:                 foreach my $lang (@allnew) {
 4923:                     my $formelem = 'loginhelpurl_'.$lang;
 4924:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 4925:                         $formelem = 'loginhelpurl_add_file';
 4926:                     }
 4927:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 4928:                                                                "help/$lang",'','',$newfile{$lang});
 4929:                     if ($result eq 'ok') {
 4930:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 4931:                         $changes{'helpurl'}{$lang} = 1;
 4932:                     } else {
 4933:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 4934:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 4935:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 4936:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 4937: 
 4938:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4939:                         }
 4940:                     }
 4941:                 }
 4942:             } else {
 4943:                 $error = &mt("Upload of custom log-in help file(s) failed because an author role could not be assigned to a Domain Configuration user ([_1]) in domain: [_2].  Error was: [_3].",$confname,$dom,$author_ok);
 4944:             }
 4945:         } else {
 4946:             $error = &mt("Upload of custom log-in help file(s) failed because a Domain Configuration user ([_1]) could not be created in domain: [_2].  Error was: [_3].",$confname,$dom,$configuserok);
 4947:         }
 4948:         if ($error) {
 4949:             &Apache::lonnet::logthis($error);
 4950:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4951:         }
 4952:     }
 4953:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 4954: 
 4955:     my $defaulthelpfile = '/adm/loginproblems.html';
 4956:     my $defaulttext = &mt('Default in use');
 4957: 
 4958:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4959:                                              $dom);
 4960:     if ($putresult eq 'ok') {
 4961:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 4962:         my %defaultchecked = (
 4963:                     'coursecatalog' => 'on',
 4964:                     'helpdesk'      => 'on',
 4965:                     'adminmail'     => 'off',
 4966:                     'newuser'       => 'off',
 4967:         );
 4968:         if (ref($domconfig{'login'}) eq 'HASH') {
 4969:             foreach my $item (@toggles) {
 4970:                 if ($defaultchecked{$item} eq 'on') { 
 4971:                     if (($domconfig{'login'}{$item} eq '0') &&
 4972:                         ($env{'form.'.$item} eq '1')) {
 4973:                         $changes{$item} = 1;
 4974:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4975:                               $domconfig{'login'}{$item} eq '1') &&
 4976:                              ($env{'form.'.$item} eq '0')) {
 4977:                         $changes{$item} = 1;
 4978:                     }
 4979:                 } elsif ($defaultchecked{$item} eq 'off') {
 4980:                     if (($domconfig{'login'}{$item} eq '1') &&
 4981:                         ($env{'form.'.$item} eq '0')) {
 4982:                         $changes{$item} = 1;
 4983:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4984:                               $domconfig{'login'}{$item} eq '0') &&
 4985:                              ($env{'form.'.$item} eq '1')) {
 4986:                         $changes{$item} = 1;
 4987:                     }
 4988:                 }
 4989:             }
 4990:         }
 4991:         if (keys(%changes) > 0 || $colchgtext) {
 4992:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4993:             if (ref($lastactref) eq 'HASH') {
 4994:                 $lastactref->{'domainconfig'} = 1;
 4995:             }
 4996:             $resulttext = &mt('Changes made:').'<ul>';
 4997:             foreach my $item (sort(keys(%changes))) {
 4998:                 if ($item eq 'loginvia') {
 4999:                     if (ref($changes{$item}) eq 'HASH') {
 5000:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5001:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5002:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5003:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5004:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5005:                                     $protocol = 'http' if ($protocol ne 'https');
 5006:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5007: 
 5008:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5009:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5010:                                     } else {
 5011:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5012:                                     }
 5013:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5014:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5015:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5016:                                     }
 5017:                                     $resulttext .= '</li>';
 5018:                                 } else {
 5019:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5020:                                 }
 5021:                             } else {
 5022:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5023:                             }
 5024:                         }
 5025:                         $resulttext .= '</ul></li>';
 5026:                     }
 5027:                 } elsif ($item eq 'helpurl') {
 5028:                     if (ref($changes{$item}) eq 'HASH') {
 5029:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5030:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5031:                                 my ($chg,$link);
 5032:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5033:                                 if ($lang eq 'nolang') {
 5034:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5035:                                 } else {
 5036:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5037:                                 }
 5038:                                 $resulttext .= '<li>'.$chg.'</li>';
 5039:                             } else {
 5040:                                 my $chg;
 5041:                                 if ($lang eq 'nolang') {
 5042:                                     $chg = &mt('custom log-in help file for no preferred language');
 5043:                                 } else {
 5044:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 5045:                                 }
 5046:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 5047:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 5048:                                                       '?inhibitmenu=yes',$chg,600,500).
 5049:                                                '</li>';
 5050:                             }
 5051:                         }
 5052:                     }
 5053:                 } elsif ($item eq 'captcha') {
 5054:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5055:                         my $chgtxt;
 5056:                         if ($loginhash{'login'}{$item} eq 'notused') {
 5057:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 5058:                         } else {
 5059:                             my %captchas = &captcha_phrases();
 5060:                             if ($captchas{$loginhash{'login'}{$item}}) {
 5061:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 5062:                             } else {
 5063:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 5064:                             }
 5065:                         }
 5066:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5067:                     }
 5068:                 } elsif ($item eq 'recaptchakeys') {
 5069:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5070:                         my ($privkey,$pubkey);
 5071:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 5072:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 5073:                             $privkey = $loginhash{'login'}{$item}{'private'};
 5074:                         }
 5075:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 5076:                         if (!$pubkey) {
 5077:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 5078:                         } else {
 5079:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 5080:                         }
 5081:                         if (!$privkey) {
 5082:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 5083:                         } else {
 5084:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 5085:                         }
 5086:                         $chgtxt .= '</ul>';
 5087:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5088:                     }
 5089:                 } else {
 5090:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 5091:                 }
 5092:             }
 5093:             $resulttext .= $colchgtext.'</ul>';
 5094:         } else {
 5095:             $resulttext = &mt('No changes made to log-in page settings');
 5096:         }
 5097:     } else {
 5098:         $resulttext = '<span class="LC_error">'.
 5099: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5100:     }
 5101:     if ($errors) {
 5102:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 5103:                        $errors.'</ul>';
 5104:     }
 5105:     return $resulttext;
 5106: }
 5107: 
 5108: sub color_font_choices {
 5109:     my %choices =
 5110:         &Apache::lonlocal::texthash (
 5111:             img => "Header",
 5112:             bgs => "Background colors",
 5113:             links => "Link colors",
 5114:             images => "Images",
 5115:             font => "Font color",
 5116:             fontmenu => "Font menu",
 5117:             pgbg => "Page",
 5118:             tabbg => "Header",
 5119:             sidebg => "Border",
 5120:             link => "Link",
 5121:             alink => "Active link",
 5122:             vlink => "Visited link",
 5123:         );
 5124:     return %choices;
 5125: }
 5126: 
 5127: sub modify_rolecolors {
 5128:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 5129:     my ($resulttext,%rolehash);
 5130:     $rolehash{'rolecolors'} = {};
 5131:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5132:         if ($domconfig{'rolecolors'} eq '') {
 5133:             $domconfig{'rolecolors'} = {};
 5134:         }
 5135:     }
 5136:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5137:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5138:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5139:                                              $dom);
 5140:     if ($putresult eq 'ok') {
 5141:         if (keys(%changes) > 0) {
 5142:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5143:             if (ref($lastactref) eq 'HASH') {
 5144:                 $lastactref->{'domainconfig'} = 1;
 5145:             }
 5146:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5147:                                              $rolehash{'rolecolors'});
 5148:         } else {
 5149:             $resulttext = &mt('No changes made to default color schemes');
 5150:         }
 5151:     } else {
 5152:         $resulttext = '<span class="LC_error">'.
 5153: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5154:     }
 5155:     if ($errors) {
 5156:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5157:                        $errors.'</ul>';
 5158:     }
 5159:     return $resulttext;
 5160: }
 5161: 
 5162: sub modify_colors {
 5163:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5164:     my (%changes,%choices);
 5165:     my @bgs;
 5166:     my @links = ('link','alink','vlink');
 5167:     my @logintext;
 5168:     my @images;
 5169:     my $servadm = $r->dir_config('lonAdmEMail');
 5170:     my $errors;
 5171:     my %defaults;
 5172:     foreach my $role (@{$roles}) {
 5173:         if ($role eq 'login') {
 5174:             %choices = &login_choices();
 5175:             @logintext = ('textcol','bgcol');
 5176:         } else {
 5177:             %choices = &color_font_choices();
 5178:         }
 5179:         if ($role eq 'login') {
 5180:             @images = ('img','logo','domlogo','login');
 5181:             @bgs = ('pgbg','mainbg','sidebg');
 5182:         } else {
 5183:             @images = ('img');
 5184:             @bgs = ('pgbg','tabbg','sidebg');
 5185:         }
 5186:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 5187:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 5188:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5189:         }
 5190:         if ($role eq 'login') {
 5191:             foreach my $item (@logintext) {
 5192:                 unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'logintext'}{$item}) {
 5193:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5194:                 }
 5195:             }
 5196:         } else {
 5197:             unless($env{'form.'.$role.'_fontmenu'} eq $defaults{'fontmenu'}) {
 5198:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5199:             }
 5200:         }
 5201:         foreach my $item (@bgs) {
 5202:             unless ($env{'form.'.$role.'_'.$item} eq $defaults{'bgs'}{$item} ) {
 5203:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5204:             }
 5205:         }
 5206:         foreach my $item (@links) {
 5207:             unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'links'}{$item}) {
 5208:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5209:             }
 5210:         }
 5211:         my ($configuserok,$author_ok,$switchserver) = 
 5212:             &config_check($dom,$confname,$servadm);
 5213:         my ($width,$height) = &thumb_dimensions();
 5214:         if (ref($domconfig->{$role}) ne 'HASH') {
 5215:             $domconfig->{$role} = {};
 5216:         }
 5217:         foreach my $img (@images) {
 5218:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5219:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5220:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5221:                 } else { 
 5222:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5223:                 }
 5224:             } 
 5225: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5226: 		 && !defined($domconfig->{$role}{$img})
 5227: 		 && !$env{'form.'.$role.'_del_'.$img}
 5228: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5229: 		# import the old configured image from the .tab setting
 5230: 		# if they haven't provided a new one 
 5231: 		$domconfig->{$role}{$img} = 
 5232: 		    $env{'form.'.$role.'_import_'.$img};
 5233: 	    }
 5234:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5235:                 my $error;
 5236:                 if ($configuserok eq 'ok') {
 5237:                     if ($switchserver) {
 5238:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5239:                     } else {
 5240:                         if ($author_ok eq 'ok') {
 5241:                             my ($result,$logourl) = 
 5242:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5243:                                            $dom,$confname,$img,$width,$height);
 5244:                             if ($result eq 'ok') {
 5245:                                 $confhash->{$role}{$img} = $logourl;
 5246:                                 $changes{$role}{'images'}{$img} = 1;
 5247:                             } else {
 5248:                                 $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);
 5249:                             }
 5250:                         } else {
 5251:                             $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);
 5252:                         }
 5253:                     }
 5254:                 } else {
 5255:                     $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);
 5256:                 }
 5257:                 if ($error) {
 5258:                     &Apache::lonnet::logthis($error);
 5259:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5260:                 }
 5261:             } elsif ($domconfig->{$role}{$img} ne '') {
 5262:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5263:                     my $error;
 5264:                     if ($configuserok eq 'ok') {
 5265: # is confname an author?
 5266:                         if ($switchserver eq '') {
 5267:                             if ($author_ok eq 'ok') {
 5268:                                 my ($result,$logourl) = 
 5269:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5270:                                             $dom,$confname,$img,$width,$height);
 5271:                                 if ($result eq 'ok') {
 5272:                                     $confhash->{$role}{$img} = $logourl;
 5273: 				    $changes{$role}{'images'}{$img} = 1;
 5274:                                 }
 5275:                             }
 5276:                         }
 5277:                     }
 5278:                 }
 5279:             }
 5280:         }
 5281:         if (ref($domconfig) eq 'HASH') {
 5282:             if (ref($domconfig->{$role}) eq 'HASH') {
 5283:                 foreach my $img (@images) {
 5284:                     if ($domconfig->{$role}{$img} ne '') {
 5285:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5286:                             $confhash->{$role}{$img} = '';
 5287:                             $changes{$role}{'images'}{$img} = 1;
 5288:                         } else {
 5289:                             if ($confhash->{$role}{$img} eq '') {
 5290:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5291:                             }
 5292:                         }
 5293:                     } else {
 5294:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5295:                             $confhash->{$role}{$img} = '';
 5296:                             $changes{$role}{'images'}{$img} = 1;
 5297:                         } 
 5298:                     }
 5299:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5300:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5301:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5302:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5303:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5304:                             }
 5305:                         } else {
 5306:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5307:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5308:                             }
 5309:                         }
 5310:                     }
 5311:                 }
 5312:                 if ($domconfig->{$role}{'font'} ne '') {
 5313:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5314:                         $changes{$role}{'font'} = 1;
 5315:                     }
 5316:                 } else {
 5317:                     if ($confhash->{$role}{'font'}) {
 5318:                         $changes{$role}{'font'} = 1;
 5319:                     }
 5320:                 }
 5321:                 if ($role ne 'login') {
 5322:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5323:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5324:                             $changes{$role}{'fontmenu'} = 1;
 5325:                         }
 5326:                     } else {
 5327:                         if ($confhash->{$role}{'fontmenu'}) {
 5328:                             $changes{$role}{'fontmenu'} = 1;
 5329:                         }
 5330:                     }
 5331:                 }
 5332:                 foreach my $item (@bgs) {
 5333:                     if ($domconfig->{$role}{$item} ne '') {
 5334:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5335:                             $changes{$role}{'bgs'}{$item} = 1;
 5336:                         } 
 5337:                     } else {
 5338:                         if ($confhash->{$role}{$item}) {
 5339:                             $changes{$role}{'bgs'}{$item} = 1;
 5340:                         }
 5341:                     }
 5342:                 }
 5343:                 foreach my $item (@links) {
 5344:                     if ($domconfig->{$role}{$item} ne '') {
 5345:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5346:                             $changes{$role}{'links'}{$item} = 1;
 5347:                         }
 5348:                     } else {
 5349:                         if ($confhash->{$role}{$item}) {
 5350:                             $changes{$role}{'links'}{$item} = 1;
 5351:                         }
 5352:                     }
 5353:                 }
 5354:                 foreach my $item (@logintext) {
 5355:                     if ($domconfig->{$role}{$item} ne '') {
 5356:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5357:                             $changes{$role}{'logintext'}{$item} = 1;
 5358:                         }
 5359:                     } else {
 5360:                         if ($confhash->{$role}{$item}) {
 5361:                             $changes{$role}{'logintext'}{$item} = 1;
 5362:                         }
 5363:                     }
 5364:                 }
 5365:             } else {
 5366:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5367:                                         \@logintext,$confhash,\%changes); 
 5368:             }
 5369:         } else {
 5370:             &default_change_checker($role,\@images,\@links,\@bgs,
 5371:                                     \@logintext,$confhash,\%changes); 
 5372:         }
 5373:     }
 5374:     return ($errors,%changes);
 5375: }
 5376: 
 5377: sub config_check {
 5378:     my ($dom,$confname,$servadm) = @_;
 5379:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5380:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5381:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5382:                                                    $confname,$servadm);
 5383:     if ($configuserok eq 'ok') {
 5384:         $switchserver = &check_switchserver($dom,$confname);
 5385:         if ($switchserver eq '') {
 5386:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5387:         }
 5388:     }
 5389:     return ($configuserok,$author_ok,$switchserver);
 5390: }
 5391: 
 5392: sub default_change_checker {
 5393:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5394:     foreach my $item (@{$links}) {
 5395:         if ($confhash->{$role}{$item}) {
 5396:             $changes->{$role}{'links'}{$item} = 1;
 5397:         }
 5398:     }
 5399:     foreach my $item (@{$bgs}) {
 5400:         if ($confhash->{$role}{$item}) {
 5401:             $changes->{$role}{'bgs'}{$item} = 1;
 5402:         }
 5403:     }
 5404:     foreach my $item (@{$logintext}) {
 5405:         if ($confhash->{$role}{$item}) {
 5406:             $changes->{$role}{'logintext'}{$item} = 1;
 5407:         }
 5408:     }
 5409:     foreach my $img (@{$images}) {
 5410:         if ($env{'form.'.$role.'_del_'.$img}) {
 5411:             $confhash->{$role}{$img} = '';
 5412:             $changes->{$role}{'images'}{$img} = 1;
 5413:         }
 5414:         if ($role eq 'login') {
 5415:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5416:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5417:             }
 5418:         }
 5419:     }
 5420:     if ($confhash->{$role}{'font'}) {
 5421:         $changes->{$role}{'font'} = 1;
 5422:     }
 5423: }
 5424: 
 5425: sub display_colorchgs {
 5426:     my ($dom,$changes,$roles,$confhash) = @_;
 5427:     my (%choices,$resulttext);
 5428:     if (!grep(/^login$/,@{$roles})) {
 5429:         $resulttext = &mt('Changes made:').'<br />';
 5430:     }
 5431:     foreach my $role (@{$roles}) {
 5432:         if ($role eq 'login') {
 5433:             %choices = &login_choices();
 5434:         } else {
 5435:             %choices = &color_font_choices();
 5436:         }
 5437:         if (ref($changes->{$role}) eq 'HASH') {
 5438:             if ($role ne 'login') {
 5439:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5440:             }
 5441:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5442:                 if ($role ne 'login') {
 5443:                     $resulttext .= '<ul>';
 5444:                 }
 5445:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5446:                     if ($role ne 'login') {
 5447:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5448:                     }
 5449:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5450:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5451:                             if ($confhash->{$role}{$key}{$item}) {
 5452:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5453:                             } else {
 5454:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5455:                             }
 5456:                         } elsif ($confhash->{$role}{$item} eq '') {
 5457:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5458:                         } else {
 5459:                             my $newitem = $confhash->{$role}{$item};
 5460:                             if ($key eq 'images') {
 5461:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5462:                             }
 5463:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5464:                         }
 5465:                     }
 5466:                     if ($role ne 'login') {
 5467:                         $resulttext .= '</ul></li>';
 5468:                     }
 5469:                 } else {
 5470:                     if ($confhash->{$role}{$key} eq '') {
 5471:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5472:                     } else {
 5473:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5474:                     }
 5475:                 }
 5476:                 if ($role ne 'login') {
 5477:                     $resulttext .= '</ul>';
 5478:                 }
 5479:             }
 5480:         }
 5481:     }
 5482:     return $resulttext;
 5483: }
 5484: 
 5485: sub thumb_dimensions {
 5486:     return ('200','50');
 5487: }
 5488: 
 5489: sub check_dimensions {
 5490:     my ($inputfile) = @_;
 5491:     my ($fullwidth,$fullheight);
 5492:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5493:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5494:             my $imageinfo = <PIPE>;
 5495:             if (!close(PIPE)) {
 5496:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5497:             }
 5498:             chomp($imageinfo);
 5499:             my ($fullsize) = 
 5500:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5501:             if ($fullsize) {
 5502:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5503:             }
 5504:         }
 5505:     }
 5506:     return ($fullwidth,$fullheight);
 5507: }
 5508: 
 5509: sub check_configuser {
 5510:     my ($uhome,$dom,$confname,$servadm) = @_;
 5511:     my ($configuserok,%currroles);
 5512:     if ($uhome eq 'no_host') {
 5513:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5514:         my $configpass = &LONCAPA::Enrollment::create_password();
 5515:         $configuserok = 
 5516:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5517:                              $configpass,'','','','','',undef,$servadm);
 5518:     } else {
 5519:         $configuserok = 'ok';
 5520:         %currroles = 
 5521:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5522:     }
 5523:     return ($configuserok,%currroles);
 5524: }
 5525: 
 5526: sub check_authorstatus {
 5527:     my ($dom,$confname,%currroles) = @_;
 5528:     my $author_ok;
 5529:     if (!$currroles{':'.$dom.':au'}) {
 5530:         my $start = time;
 5531:         my $end = 0;
 5532:         $author_ok = 
 5533:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5534:                                         'au',$end,$start,'','','domconfig');
 5535:     } else {
 5536:         $author_ok = 'ok';
 5537:     }
 5538:     return $author_ok;
 5539: }
 5540: 
 5541: sub publishlogo {
 5542:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5543:     my ($output,$fname,$logourl);
 5544:     if ($action eq 'upload') {
 5545:         $fname=$env{'form.'.$formname.'.filename'};
 5546:         chop($env{'form.'.$formname});
 5547:     } else {
 5548:         ($fname) = ($formname =~ /([^\/]+)$/);
 5549:     }
 5550:     if ($savefileas ne '') {
 5551:         $fname = $savefileas;
 5552:     }
 5553:     $fname=&Apache::lonnet::clean_filename($fname);
 5554: # See if there is anything left
 5555:     unless ($fname) { return ('error: no uploaded file'); }
 5556:     $fname="$subdir/$fname";
 5557:     my $docroot=$r->dir_config('lonDocRoot');
 5558:     my $filepath="$docroot/priv";
 5559:     my $relpath = "$dom/$confname";
 5560:     my ($fnamepath,$file,$fetchthumb);
 5561:     $file=$fname;
 5562:     if ($fname=~m|/|) {
 5563:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5564:     }
 5565:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5566:     my $count;
 5567:     for ($count=5;$count<=$#parts;$count++) {
 5568:         $filepath.="/$parts[$count]";
 5569:         if ((-e $filepath)!=1) {
 5570:             mkdir($filepath,02770);
 5571:         }
 5572:     }
 5573:     # Check for bad extension and disallow upload
 5574:     if ($file=~/\.(\w+)$/ &&
 5575:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5576:         $output = 
 5577:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 5578:     } elsif ($file=~/\.(\w+)$/ &&
 5579:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5580:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5581:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5582:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5583:     } elsif (-d "$filepath/$file") {
 5584:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 5585:     } else {
 5586:         my $source = $filepath.'/'.$file;
 5587:         my $logfile;
 5588:         if (!open($logfile,">>$source".'.log')) {
 5589:             return (&mt('No write permission to Authoring Space'));
 5590:         }
 5591:         print $logfile
 5592: "\n================= Publish ".localtime()." ================\n".
 5593: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5594: # Save the file
 5595:         if (!open(FH,'>'.$source)) {
 5596:             &Apache::lonnet::logthis('Failed to create '.$source);
 5597:             return (&mt('Failed to create file'));
 5598:         }
 5599:         if ($action eq 'upload') {
 5600:             if (!print FH ($env{'form.'.$formname})) {
 5601:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5602:                 return (&mt('Failed to write file'));
 5603:             }
 5604:         } else {
 5605:             my $original = &Apache::lonnet::filelocation('',$formname);
 5606:             if(!copy($original,$source)) {
 5607:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5608:                 return (&mt('Failed to write file'));
 5609:             }
 5610:         }
 5611:         close(FH);
 5612:         chmod(0660, $source); # Permissions to rw-rw---.
 5613: 
 5614:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5615:         my $copyfile=$targetdir.'/'.$file;
 5616: 
 5617:         my @parts=split(/\//,$targetdir);
 5618:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5619:         for (my $count=5;$count<=$#parts;$count++) {
 5620:             $path.="/$parts[$count]";
 5621:             if (!-e $path) {
 5622:                 print $logfile "\nCreating directory ".$path;
 5623:                 mkdir($path,02770);
 5624:             }
 5625:         }
 5626:         my $versionresult;
 5627:         if (-e $copyfile) {
 5628:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5629:         } else {
 5630:             $versionresult = 'ok';
 5631:         }
 5632:         if ($versionresult eq 'ok') {
 5633:             if (copy($source,$copyfile)) {
 5634:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5635:                 $output = 'ok';
 5636:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5637:                 push(@{$modified_urls},[$copyfile,$source]);
 5638:                 my $metaoutput = 
 5639:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5640:                 unless ($registered_cleanup) {
 5641:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5642:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5643:                     $registered_cleanup=1;
 5644:                 }
 5645:             } else {
 5646:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5647:                 $output = &mt('Failed to copy file to RES space').", $!";
 5648:             }
 5649:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5650:                 my $inputfile = $filepath.'/'.$file;
 5651:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5652:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5653:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5654:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5655:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5656:                         system("convert -sample $thumbsize $inputfile $outfile");
 5657:                         chmod(0660, $filepath.'/tn-'.$file);
 5658:                         if (-e $outfile) {
 5659:                             my $copyfile=$targetdir.'/tn-'.$file;
 5660:                             if (copy($outfile,$copyfile)) {
 5661:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5662:                                 my $thumb_metaoutput = 
 5663:                                     &write_metadata($dom,$confname,$formname,
 5664:                                                     $targetdir,'tn-'.$file,$logfile);
 5665:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5666:                                 unless ($registered_cleanup) {
 5667:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5668:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5669:                                     $registered_cleanup=1;
 5670:                                 }
 5671:                             } else {
 5672:                                 print $logfile "\nUnable to write ".$copyfile.
 5673:                                                ':'.$!."\n";
 5674:                             }
 5675:                         }
 5676:                     }
 5677:                 }
 5678:             }
 5679:         } else {
 5680:             $output = $versionresult;
 5681:         }
 5682:     }
 5683:     return ($output,$logourl);
 5684: }
 5685: 
 5686: sub logo_versioning {
 5687:     my ($targetdir,$file,$logfile) = @_;
 5688:     my $target = $targetdir.'/'.$file;
 5689:     my ($maxversion,$fn,$extn,$output);
 5690:     $maxversion = 0;
 5691:     if ($file =~ /^(.+)\.(\w+)$/) {
 5692:         $fn=$1;
 5693:         $extn=$2;
 5694:     }
 5695:     opendir(DIR,$targetdir);
 5696:     while (my $filename=readdir(DIR)) {
 5697:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5698:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5699:         }
 5700:     }
 5701:     $maxversion++;
 5702:     print $logfile "\nCreating old version ".$maxversion."\n";
 5703:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5704:     if (copy($target,$copyfile)) {
 5705:         print $logfile "Copied old target to ".$copyfile."\n";
 5706:         $copyfile=$copyfile.'.meta';
 5707:         if (copy($target.'.meta',$copyfile)) {
 5708:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5709:             $output = 'ok';
 5710:         } else {
 5711:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5712:             $output = &mt('Failed to copy old meta').", $!, ";
 5713:         }
 5714:     } else {
 5715:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5716:         $output = &mt('Failed to copy old target').", $!, ";
 5717:     }
 5718:     return $output;
 5719: }
 5720: 
 5721: sub write_metadata {
 5722:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5723:     my (%metadatafields,%metadatakeys,$output);
 5724:     $metadatafields{'title'}=$formname;
 5725:     $metadatafields{'creationdate'}=time;
 5726:     $metadatafields{'lastrevisiondate'}=time;
 5727:     $metadatafields{'copyright'}='public';
 5728:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5729:                                          $env{'user.domain'};
 5730:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5731:     $metadatafields{'domain'}=$dom;
 5732:     {
 5733:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5734:         my $mfh;
 5735:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5736:             foreach (sort(keys(%metadatafields))) {
 5737:                 unless ($_=~/\./) {
 5738:                     my $unikey=$_;
 5739:                     $unikey=~/^([A-Za-z]+)/;
 5740:                     my $tag=$1;
 5741:                     $tag=~tr/A-Z/a-z/;
 5742:                     print $mfh "\n\<$tag";
 5743:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5744:                         my $value=$metadatafields{$unikey.'.'.$_};
 5745:                         $value=~s/\"/\'\'/g;
 5746:                         print $mfh ' '.$_.'="'.$value.'"';
 5747:                     }
 5748:                     print $mfh '>'.
 5749:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5750:                             .'</'.$tag.'>';
 5751:                 }
 5752:             }
 5753:             $output = 'ok';
 5754:             print $logfile "\nWrote metadata";
 5755:             close($mfh);
 5756:         } else {
 5757:             print $logfile "\nFailed to open metadata file";
 5758:             $output = &mt('Could not write metadata');
 5759:         }
 5760:     }
 5761:     return $output;
 5762: }
 5763: 
 5764: sub notifysubscribed {
 5765:     foreach my $targetsource (@{$modified_urls}){
 5766:         next unless (ref($targetsource) eq 'ARRAY');
 5767:         my ($target,$source)=@{$targetsource};
 5768:         if ($source ne '') {
 5769:             if (open(my $logfh,'>>'.$source.'.log')) {
 5770:                 print $logfh "\nCleanup phase: Notifications\n";
 5771:                 my @subscribed=&subscribed_hosts($target);
 5772:                 foreach my $subhost (@subscribed) {
 5773:                     print $logfh "\nNotifying host ".$subhost.':';
 5774:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5775:                     print $logfh $reply;
 5776:                 }
 5777:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5778:                 foreach my $subhost (@subscribedmeta) {
 5779:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5780:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5781:                                                         $subhost);
 5782:                     print $logfh $reply;
 5783:                 }
 5784:                 print $logfh "\n============ Done ============\n";
 5785:                 close($logfh);
 5786:             }
 5787:         }
 5788:     }
 5789:     return OK;
 5790: }
 5791: 
 5792: sub subscribed_hosts {
 5793:     my ($target) = @_;
 5794:     my @subscribed;
 5795:     if (open(my $fh,"<$target.subscription")) {
 5796:         while (my $subline=<$fh>) {
 5797:             if ($subline =~ /^($match_lonid):/) {
 5798:                 my $host = $1;
 5799:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5800:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5801:                         push(@subscribed,$host);
 5802:                     }
 5803:                 }
 5804:             }
 5805:         }
 5806:     }
 5807:     return @subscribed;
 5808: }
 5809: 
 5810: sub check_switchserver {
 5811:     my ($dom,$confname) = @_;
 5812:     my ($allowed,$switchserver);
 5813:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5814:     if ($home eq 'no_host') {
 5815:         $home = &Apache::lonnet::domain($dom,'primary');
 5816:     }
 5817:     my @ids=&Apache::lonnet::current_machine_ids();
 5818:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5819:     if (!$allowed) {
 5820: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5821:     }
 5822:     return $switchserver;
 5823: }
 5824: 
 5825: sub modify_quotas {
 5826:     my ($dom,$action,$lastactref,%domconfig) = @_;
 5827:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5828:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5829:     if ($action eq 'quotas') {
 5830:         $context = 'tools'; 
 5831:     } else {
 5832:         $context = $action;
 5833:     }
 5834:     if ($context eq 'requestcourses') {
 5835:         @usertools = ('official','unofficial','community');
 5836:         @options =('norequest','approval','validate','autolimit');
 5837:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5838:         %titles = &courserequest_titles();
 5839:         $toolregexp = join('|',@usertools);
 5840:         %conditions = &courserequest_conditions();
 5841:     } elsif ($context eq 'requestauthor') {
 5842:         @usertools = ('author');
 5843:         %titles = &authorrequest_titles();
 5844:     } else {
 5845:         @usertools = ('aboutme','blog','webdav','portfolio');
 5846:         %titles = &tool_titles();
 5847:     }
 5848:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 5849:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5850:     foreach my $key (keys(%env)) {
 5851:         if ($context eq 'requestcourses') {
 5852:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5853:                 my $item = $1;
 5854:                 my $type = $2;
 5855:                 if ($type =~ /^limit_(.+)/) {
 5856:                     $limithash{$item}{$1} = $env{$key};
 5857:                 } else {
 5858:                     $confhash{$item}{$type} = $env{$key};
 5859:                 }
 5860:             }
 5861:         } elsif ($context eq 'requestauthor') {
 5862:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5863:                 $confhash{$1} = $env{$key};
 5864:             }
 5865:         } else {
 5866:             if ($key =~ /^form\.quota_(.+)$/) {
 5867:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5868:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 5869:                 $confhash{'authorquota'}{$1} = $env{$key};
 5870:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5871:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5872:             }
 5873:         }
 5874:     }
 5875:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5876:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5877:         @approvalnotify = sort(@approvalnotify);
 5878:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5879:         if (ref($domconfig{$action}) eq 'HASH') {
 5880:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5881:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5882:                     $changes{'notify'}{'approval'} = 1;
 5883:                 }
 5884:             } else {
 5885:                 if ($confhash{'notify'}{'approval'}) {
 5886:                     $changes{'notify'}{'approval'} = 1;
 5887:                 }
 5888:             }
 5889:         } else {
 5890:             if ($confhash{'notify'}{'approval'}) {
 5891:                 $changes{'notify'}{'approval'} = 1;
 5892:             }
 5893:         }
 5894:     } else {
 5895:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5896:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 5897:     }
 5898:     foreach my $item (@usertools) {
 5899:         foreach my $type (@{$types},'default','_LC_adv') {
 5900:             my $unset; 
 5901:             if ($context eq 'requestcourses') {
 5902:                 $unset = '0';
 5903:                 if ($type eq '_LC_adv') {
 5904:                     $unset = '';
 5905:                 }
 5906:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5907:                     $confhash{$item}{$type} .= '=';
 5908:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5909:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5910:                     }
 5911:                 }
 5912:             } elsif ($context eq 'requestauthor') {
 5913:                 $unset = '0';
 5914:                 if ($type eq '_LC_adv') {
 5915:                     $unset = '';
 5916:                 }
 5917:             } else {
 5918:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5919:                     $confhash{$item}{$type} = 1;
 5920:                 } else {
 5921:                     $confhash{$item}{$type} = 0;
 5922:                 }
 5923:             }
 5924:             if (ref($domconfig{$action}) eq 'HASH') {
 5925:                 if ($action eq 'requestauthor') {
 5926:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5927:                         $changes{$type} = 1;
 5928:                     }
 5929:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5930:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5931:                         $changes{$item}{$type} = 1;
 5932:                     }
 5933:                 } else {
 5934:                     if ($context eq 'requestcourses') {
 5935:                         if ($confhash{$item}{$type} ne $unset) {
 5936:                             $changes{$item}{$type} = 1;
 5937:                         }
 5938:                     } else {
 5939:                         if (!$confhash{$item}{$type}) {
 5940:                             $changes{$item}{$type} = 1;
 5941:                         }
 5942:                     }
 5943:                 }
 5944:             } else {
 5945:                 if ($context eq 'requestcourses') {
 5946:                     if ($confhash{$item}{$type} ne $unset) {
 5947:                         $changes{$item}{$type} = 1;
 5948:                     }
 5949:                 } elsif ($context eq 'requestauthor') {
 5950:                     if ($confhash{$type} ne $unset) {
 5951:                         $changes{$type} = 1;
 5952:                     }
 5953:                 } else {
 5954:                     if (!$confhash{$item}{$type}) {
 5955:                         $changes{$item}{$type} = 1;
 5956:                     }
 5957:                 }
 5958:             }
 5959:         }
 5960:     }
 5961:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5962:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5963:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5964:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5965:                     if (exists($confhash{'defaultquota'}{$key})) {
 5966:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5967:                             $changes{'defaultquota'}{$key} = 1;
 5968:                         }
 5969:                     } else {
 5970:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5971:                     }
 5972:                 }
 5973:             } else {
 5974:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5975:                     if (exists($confhash{'defaultquota'}{$key})) {
 5976:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5977:                             $changes{'defaultquota'}{$key} = 1;
 5978:                         }
 5979:                     } else {
 5980:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5981:                     }
 5982:                 }
 5983:             }
 5984:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 5985:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 5986:                     if (exists($confhash{'authorquota'}{$key})) {
 5987:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 5988:                             $changes{'authorquota'}{$key} = 1;
 5989:                         }
 5990:                     } else {
 5991:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 5992:                     }
 5993:                 }
 5994:             }
 5995:         }
 5996:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5997:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5998:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5999:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6000:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 6001:                             $changes{'defaultquota'}{$key} = 1;
 6002:                         }
 6003:                     } else {
 6004:                         if (!exists($domconfig{'quotas'}{$key})) {
 6005:                             $changes{'defaultquota'}{$key} = 1;
 6006:                         }
 6007:                     }
 6008:                 } else {
 6009:                     $changes{'defaultquota'}{$key} = 1;
 6010:                 }
 6011:             }
 6012:         }
 6013:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 6014:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 6015:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6016:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6017:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 6018:                             $changes{'authorquota'}{$key} = 1;
 6019:                         }
 6020:                     } else {
 6021:                         $changes{'authorquota'}{$key} = 1;
 6022:                     }
 6023:                 } else {
 6024:                     $changes{'authorquota'}{$key} = 1;
 6025:                 }
 6026:             }
 6027:         }
 6028:     }
 6029: 
 6030:     if ($context eq 'requestauthor') {
 6031:         $domdefaults{'requestauthor'} = \%confhash;
 6032:     } else {
 6033:         foreach my $key (keys(%confhash)) {
 6034:             $domdefaults{$key} = $confhash{$key};
 6035:         }
 6036:     }
 6037: 
 6038:     my %quotahash = (
 6039:                       $action => { %confhash }
 6040:                     );
 6041:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 6042:                                              $dom);
 6043:     if ($putresult eq 'ok') {
 6044:         if (keys(%changes) > 0) {
 6045:             my $cachetime = 24*60*60;
 6046:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6047:             if (ref($lastactref) eq 'HASH') {
 6048:                 $lastactref->{'domdefaults'} = 1;
 6049:             }
 6050:             $resulttext = &mt('Changes made:').'<ul>';
 6051:             unless (($context eq 'requestcourses') ||
 6052:                     ($context eq 'requestauthor')) {
 6053:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 6054:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 6055:                     foreach my $type (@{$types},'default') {
 6056:                         if (defined($changes{'defaultquota'}{$type})) {
 6057:                             my $typetitle = $usertypes->{$type};
 6058:                             if ($type eq 'default') {
 6059:                                 $typetitle = $othertitle;
 6060:                             }
 6061:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 6062:                         }
 6063:                     }
 6064:                     $resulttext .= '</ul></li>';
 6065:                 }
 6066:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 6067:                     $resulttext .= '<li>'.&mt('Authoring space default quotas').'<ul>';
 6068:                     foreach my $type (@{$types},'default') {
 6069:                         if (defined($changes{'authorquota'}{$type})) {
 6070:                             my $typetitle = $usertypes->{$type};
 6071:                             if ($type eq 'default') {
 6072:                                 $typetitle = $othertitle;
 6073:                             }
 6074:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 6075:                         }
 6076:                     }
 6077:                     $resulttext .= '</ul></li>';
 6078:                 }
 6079:             }
 6080:             my %newenv;
 6081:             foreach my $item (@usertools) {
 6082:                 my (%haschgs,%inconf);
 6083:                 if ($context eq 'requestauthor') {
 6084:                     %haschgs = %changes;
 6085:                     %inconf = %confhash;
 6086:                 } else {
 6087:                     if (ref($changes{$item}) eq 'HASH') {
 6088:                         %haschgs = %{$changes{$item}};
 6089:                     }
 6090:                     if (ref($confhash{$item}) eq 'HASH') {
 6091:                         %inconf = %{$confhash{$item}};
 6092:                     }
 6093:                 }
 6094:                 if (keys(%haschgs) > 0) {
 6095:                     my $newacc = 
 6096:                         &Apache::lonnet::usertools_access($env{'user.name'},
 6097:                                                           $env{'user.domain'},
 6098:                                                           $item,'reload',$context);
 6099:                     if (($context eq 'requestcourses') ||
 6100:                         ($context eq 'requestauthor')) {
 6101:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 6102:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 6103:                         }
 6104:                     } else {
 6105:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 6106:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 6107:                         }
 6108:                     }
 6109:                     unless ($context eq 'requestauthor') {
 6110:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 6111:                     }
 6112:                     foreach my $type (@{$types},'default','_LC_adv') {
 6113:                         if ($haschgs{$type}) {
 6114:                             my $typetitle = $usertypes->{$type};
 6115:                             if ($type eq 'default') {
 6116:                                 $typetitle = $othertitle;
 6117:                             } elsif ($type eq '_LC_adv') {
 6118:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 6119:                             }
 6120:                             if ($inconf{$type}) {
 6121:                                 if ($context eq 'requestcourses') {
 6122:                                     my $cond;
 6123:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 6124:                                         if ($1 eq '') {
 6125:                                             $cond = &mt('(Automatic processing of any request).');
 6126:                                         } else {
 6127:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 6128:                                         }
 6129:                                     } else { 
 6130:                                         $cond = $conditions{$inconf{$type}};
 6131:                                     }
 6132:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 6133:                                 } elsif ($context eq 'requestauthor') {
 6134:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 6135:                                                              $titles{$inconf{$type}},$typetitle);
 6136: 
 6137:                                 } else {
 6138:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 6139:                                 }
 6140:                             } else {
 6141:                                 if ($type eq '_LC_adv') {
 6142:                                     if ($inconf{$type} eq '0') {
 6143:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6144:                                     } else { 
 6145:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 6146:                                     }
 6147:                                 } else {
 6148:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6149:                                 }
 6150:                             }
 6151:                         }
 6152:                     }
 6153:                     unless ($context eq 'requestauthor') {
 6154:                         $resulttext .= '</ul></li>';
 6155:                     }
 6156:                 }
 6157:             }
 6158:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 6159:                 if (ref($changes{'notify'}) eq 'HASH') {
 6160:                     if ($changes{'notify'}{'approval'}) {
 6161:                         if (ref($confhash{'notify'}) eq 'HASH') {
 6162:                             if ($confhash{'notify'}{'approval'}) {
 6163:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 6164:                             } else {
 6165:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 6166:                             }
 6167:                         }
 6168:                     }
 6169:                 }
 6170:             }
 6171:             $resulttext .= '</ul>';
 6172:             if (keys(%newenv)) {
 6173:                 &Apache::lonnet::appenv(\%newenv);
 6174:             }
 6175:         } else {
 6176:             if ($context eq 'requestcourses') {
 6177:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 6178:             } elsif ($context eq 'requestauthor') {
 6179:                 $resulttext = &mt('No changes made to rights to request author space.');
 6180:             } else {
 6181:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 6182:             }
 6183:         }
 6184:     } else {
 6185:         $resulttext = '<span class="LC_error">'.
 6186: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6187:     }
 6188:     return $resulttext;
 6189: }
 6190: 
 6191: sub modify_autoenroll {
 6192:     my ($dom,$lastactref,%domconfig) = @_;
 6193:     my ($resulttext,%changes);
 6194:     my %currautoenroll;
 6195:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 6196:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 6197:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 6198:         }
 6199:     }
 6200:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6201:     my %title = ( run => 'Auto-enrollment active',
 6202:                   sender => 'Sender for notification messages',
 6203:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6204:     my @offon = ('off','on');
 6205:     my $sender_uname = $env{'form.sender_uname'};
 6206:     my $sender_domain = $env{'form.sender_domain'};
 6207:     if ($sender_domain eq '') {
 6208:         $sender_uname = '';
 6209:     } elsif ($sender_uname eq '') {
 6210:         $sender_domain = '';
 6211:     }
 6212:     my $coowners = $env{'form.autoassign_coowners'};
 6213:     my %autoenrollhash =  (
 6214:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6215:                                        'sender_uname' => $sender_uname,
 6216:                                        'sender_domain' => $sender_domain,
 6217:                                        'co-owners' => $coowners,
 6218:                                 }
 6219:                      );
 6220:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6221:                                              $dom);
 6222:     if ($putresult eq 'ok') {
 6223:         if (exists($currautoenroll{'run'})) {
 6224:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6225:                  $changes{'run'} = 1;
 6226:              }
 6227:         } elsif ($autorun) {
 6228:             if ($env{'form.autoenroll_run'} ne '1') {
 6229:                  $changes{'run'} = 1;
 6230:             }
 6231:         }
 6232:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6233:             $changes{'sender'} = 1;
 6234:         }
 6235:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6236:             $changes{'sender'} = 1;
 6237:         }
 6238:         if ($currautoenroll{'co-owners'} ne '') {
 6239:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6240:                 $changes{'coowners'} = 1;
 6241:             }
 6242:         } elsif ($coowners) {
 6243:             $changes{'coowners'} = 1;
 6244:         }      
 6245:         if (keys(%changes) > 0) {
 6246:             $resulttext = &mt('Changes made:').'<ul>';
 6247:             if ($changes{'run'}) {
 6248:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6249:             }
 6250:             if ($changes{'sender'}) {
 6251:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6252:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6253:                 } else {
 6254:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6255:                 }
 6256:             }
 6257:             if ($changes{'coowners'}) {
 6258:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6259:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6260:                 if (ref($lastactref) eq 'HASH') {
 6261:                     $lastactref->{'domainconfig'} = 1;
 6262:                 }
 6263:             }
 6264:             $resulttext .= '</ul>';
 6265:         } else {
 6266:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6267:         }
 6268:     } else {
 6269:         $resulttext = '<span class="LC_error">'.
 6270: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6271:     }
 6272:     return $resulttext;
 6273: }
 6274: 
 6275: sub modify_autoupdate {
 6276:     my ($dom,%domconfig) = @_;
 6277:     my ($resulttext,%currautoupdate,%fields,%changes);
 6278:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6279:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6280:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6281:         }
 6282:     }
 6283:     my @offon = ('off','on');
 6284:     my %title = &Apache::lonlocal::texthash (
 6285:                    run => 'Auto-update:',
 6286:                    classlists => 'Updates to user information in classlists?'
 6287:                 );
 6288:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6289:     my %fieldtitles = &Apache::lonlocal::texthash (
 6290:                         id => 'Student/Employee ID',
 6291:                         permanentemail => 'E-mail address',
 6292:                         lastname => 'Last Name',
 6293:                         firstname => 'First Name',
 6294:                         middlename => 'Middle Name',
 6295:                         generation => 'Generation',
 6296:                       );
 6297:     $othertitle = &mt('All users');
 6298:     if (keys(%{$usertypes}) >  0) {
 6299:         $othertitle = &mt('Other users');
 6300:     }
 6301:     foreach my $key (keys(%env)) {
 6302:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6303:             my ($usertype,$item) = ($1,$2);
 6304:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6305:                 if ($usertype eq 'default') {   
 6306:                     push(@{$fields{$1}},$2);
 6307:                 } elsif (ref($types) eq 'ARRAY') {
 6308:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6309:                         push(@{$fields{$1}},$2);
 6310:                     }
 6311:                 }
 6312:             }
 6313:         }
 6314:     }
 6315:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6316:     @lockablenames = sort(@lockablenames);
 6317:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6318:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6319:         if (@changed) {
 6320:             $changes{'lockablenames'} = 1;
 6321:         }
 6322:     } else {
 6323:         if (@lockablenames) {
 6324:             $changes{'lockablenames'} = 1;
 6325:         }
 6326:     }
 6327:     my %updatehash = (
 6328:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6329:                                       classlists => $env{'form.classlists'},
 6330:                                       fields => {%fields},
 6331:                                       lockablenames => \@lockablenames,
 6332:                                     }
 6333:                      );
 6334:     foreach my $key (keys(%currautoupdate)) {
 6335:         if (($key eq 'run') || ($key eq 'classlists')) {
 6336:             if (exists($updatehash{autoupdate}{$key})) {
 6337:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6338:                     $changes{$key} = 1;
 6339:                 }
 6340:             }
 6341:         } elsif ($key eq 'fields') {
 6342:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6343:                 foreach my $item (@{$types},'default') {
 6344:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6345:                         my $change = 0;
 6346:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6347:                             if (!exists($fields{$item})) {
 6348:                                 $change = 1;
 6349:                                 last;
 6350:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6351:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6352:                                     $change = 1;
 6353:                                     last;
 6354:                                 }
 6355:                             }
 6356:                         }
 6357:                         if ($change) {
 6358:                             push(@{$changes{$key}},$item);
 6359:                         }
 6360:                     } 
 6361:                 }
 6362:             }
 6363:         } elsif ($key eq 'lockablenames') {
 6364:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6365:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6366:                 if (@changed) {
 6367:                     $changes{'lockablenames'} = 1;
 6368:                 }
 6369:             } else {
 6370:                 if (@lockablenames) {
 6371:                     $changes{'lockablenames'} = 1;
 6372:                 }
 6373:             }
 6374:         }
 6375:     }
 6376:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6377:         if (@lockablenames) {
 6378:             $changes{'lockablenames'} = 1;
 6379:         }
 6380:     }
 6381:     foreach my $item (@{$types},'default') {
 6382:         if (defined($fields{$item})) {
 6383:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6384:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6385:                     my $change = 0;
 6386:                     if (ref($fields{$item}) eq 'ARRAY') {
 6387:                         foreach my $type (@{$fields{$item}}) {
 6388:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6389:                                 $change = 1;
 6390:                                 last;
 6391:                             }
 6392:                         }
 6393:                     }
 6394:                     if ($change) {
 6395:                         push(@{$changes{'fields'}},$item);
 6396:                     }
 6397:                 } else {
 6398:                     push(@{$changes{'fields'}},$item);
 6399:                 }
 6400:             } else {
 6401:                 push(@{$changes{'fields'}},$item);
 6402:             }
 6403:         }
 6404:     }
 6405:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6406:                                              $dom);
 6407:     if ($putresult eq 'ok') {
 6408:         if (keys(%changes) > 0) {
 6409:             $resulttext = &mt('Changes made:').'<ul>';
 6410:             foreach my $key (sort(keys(%changes))) {
 6411:                 if ($key eq 'lockablenames') {
 6412:                     $resulttext .= '<li>';
 6413:                     if (@lockablenames) {
 6414:                         $usertypes->{'default'} = $othertitle;
 6415:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6416:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6417:                     } else {
 6418:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6419:                     }
 6420:                     $resulttext .= '</li>';
 6421:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6422:                     foreach my $item (@{$changes{$key}}) {
 6423:                         my @newvalues;
 6424:                         foreach my $type (@{$fields{$item}}) {
 6425:                             push(@newvalues,$fieldtitles{$type});
 6426:                         }
 6427:                         my $newvaluestr;
 6428:                         if (@newvalues > 0) {
 6429:                             $newvaluestr = join(', ',@newvalues);
 6430:                         } else {
 6431:                             $newvaluestr = &mt('none');
 6432:                         }
 6433:                         if ($item eq 'default') {
 6434:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6435:                         } else {
 6436:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6437:                         }
 6438:                     }
 6439:                 } else {
 6440:                     my $newvalue;
 6441:                     if ($key eq 'run') {
 6442:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6443:                     } else {
 6444:                         $newvalue = $offon[$env{'form.'.$key}];
 6445:                     }
 6446:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6447:                 }
 6448:             }
 6449:             $resulttext .= '</ul>';
 6450:         } else {
 6451:             $resulttext = &mt('No changes made to autoupdates');
 6452:         }
 6453:     } else {
 6454:         $resulttext = '<span class="LC_error">'.
 6455: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6456:     }
 6457:     return $resulttext;
 6458: }
 6459: 
 6460: sub modify_autocreate {
 6461:     my ($dom,%domconfig) = @_;
 6462:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6463:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6464:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6465:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6466:         }
 6467:     }
 6468:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6469:                  req => 'Auto-creation of validated requests for official courses',
 6470:                  xmldc => 'Identity of course creator of courses from XML files',
 6471:                );
 6472:     my @types = ('xml','req');
 6473:     foreach my $item (@types) {
 6474:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6475:         $newvals{$item} =~ s/\D//g;
 6476:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6477:     }
 6478:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6479:     my %domcoords = &get_active_dcs($dom);
 6480:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6481:         $newvals{'xmldc'} = '';
 6482:     } 
 6483:     %autocreatehash =  (
 6484:                         autocreate => { xml => $newvals{'xml'},
 6485:                                         req => $newvals{'req'},
 6486:                                       }
 6487:                        );
 6488:     if ($newvals{'xmldc'} ne '') {
 6489:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6490:     }
 6491:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6492:                                              $dom);
 6493:     if ($putresult eq 'ok') {
 6494:         my @items = @types;
 6495:         if ($newvals{'xml'}) {
 6496:             push(@items,'xmldc');
 6497:         }
 6498:         foreach my $item (@items) {
 6499:             if (exists($currautocreate{$item})) {
 6500:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6501:                     $changes{$item} = 1;
 6502:                 }
 6503:             } elsif ($newvals{$item}) {
 6504:                 $changes{$item} = 1;
 6505:             }
 6506:         }
 6507:         if (keys(%changes) > 0) {
 6508:             my @offon = ('off','on'); 
 6509:             $resulttext = &mt('Changes made:').'<ul>';
 6510:             foreach my $item (@types) {
 6511:                 if ($changes{$item}) {
 6512:                     my $newtxt = $offon[$newvals{$item}];
 6513:                     $resulttext .= '<li>'.
 6514:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 6515:                                        '<b>','</b>').
 6516:                                    '</li>';
 6517:                 }
 6518:             }
 6519:             if ($changes{'xmldc'}) {
 6520:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6521:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6522:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 6523:             }
 6524:             $resulttext .= '</ul>';
 6525:         } else {
 6526:             $resulttext = &mt('No changes made to auto-creation settings');
 6527:         }
 6528:     } else {
 6529:         $resulttext = '<span class="LC_error">'.
 6530:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6531:     }
 6532:     return $resulttext;
 6533: }
 6534: 
 6535: sub modify_directorysrch {
 6536:     my ($dom,%domconfig) = @_;
 6537:     my ($resulttext,%changes);
 6538:     my %currdirsrch;
 6539:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6540:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6541:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6542:         }
 6543:     }
 6544:     my %title = ( available => 'Directory search available',
 6545:                   localonly => 'Other domains can search',
 6546:                   searchby => 'Search types',
 6547:                   searchtypes => 'Search latitude');
 6548:     my @offon = ('off','on');
 6549:     my @otherdoms = ('Yes','No');
 6550: 
 6551:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6552:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6553:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6554: 
 6555:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6556:     if (keys(%{$usertypes}) == 0) {
 6557:         @cansearch = ('default');
 6558:     } else {
 6559:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6560:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6561:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6562:                     push(@{$changes{'cansearch'}},$type);
 6563:                 }
 6564:             }
 6565:             foreach my $type (@cansearch) {
 6566:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6567:                     push(@{$changes{'cansearch'}},$type);
 6568:                 }
 6569:             }
 6570:         } else {
 6571:             push(@{$changes{'cansearch'}},@cansearch);
 6572:         }
 6573:     }
 6574: 
 6575:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6576:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6577:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6578:                 push(@{$changes{'searchby'}},$by);
 6579:             }
 6580:         }
 6581:         foreach my $by (@searchby) {
 6582:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6583:                 push(@{$changes{'searchby'}},$by);
 6584:             }
 6585:         }
 6586:     } else {
 6587:         push(@{$changes{'searchby'}},@searchby);
 6588:     }
 6589: 
 6590:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6591:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6592:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6593:                 push(@{$changes{'searchtypes'}},$type);
 6594:             }
 6595:         }
 6596:         foreach my $type (@searchtypes) {
 6597:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6598:                 push(@{$changes{'searchtypes'}},$type);
 6599:             }
 6600:         }
 6601:     } else {
 6602:         if (exists($currdirsrch{'searchtypes'})) {
 6603:             foreach my $type (@searchtypes) {  
 6604:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6605:                     push(@{$changes{'searchtypes'}},$type);
 6606:                 }
 6607:             }
 6608:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6609:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6610:             }   
 6611:         } else {
 6612:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6613:         }
 6614:     }
 6615: 
 6616:     my %dirsrch_hash =  (
 6617:             directorysrch => { available => $env{'form.dirsrch_available'},
 6618:                                cansearch => \@cansearch,
 6619:                                localonly => $env{'form.dirsrch_localonly'},
 6620:                                searchby => \@searchby,
 6621:                                searchtypes => \@searchtypes,
 6622:                              }
 6623:             );
 6624:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6625:                                              $dom);
 6626:     if ($putresult eq 'ok') {
 6627:         if (exists($currdirsrch{'available'})) {
 6628:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6629:                  $changes{'available'} = 1;
 6630:              }
 6631:         } else {
 6632:             if ($env{'form.dirsrch_available'} eq '1') {
 6633:                 $changes{'available'} = 1;
 6634:             }
 6635:         }
 6636:         if (exists($currdirsrch{'localonly'})) {
 6637:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6638:                  $changes{'localonly'} = 1;
 6639:              }
 6640:         } else {
 6641:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6642:                 $changes{'localonly'} = 1;
 6643:             }
 6644:         }
 6645:         if (keys(%changes) > 0) {
 6646:             $resulttext = &mt('Changes made:').'<ul>';
 6647:             if ($changes{'available'}) {
 6648:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6649:             }
 6650:             if ($changes{'localonly'}) {
 6651:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6652:             }
 6653: 
 6654:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6655:                 my $chgtext;
 6656:                 if (ref($usertypes) eq 'HASH') {
 6657:                     if (keys(%{$usertypes}) > 0) {
 6658:                         foreach my $type (@{$types}) {
 6659:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6660:                                 $chgtext .= $usertypes->{$type}.'; ';
 6661:                             }
 6662:                         }
 6663:                         if (grep(/^default$/,@cansearch)) {
 6664:                             $chgtext .= $othertitle;
 6665:                         } else {
 6666:                             $chgtext =~ s/\; $//;
 6667:                         }
 6668:                         $resulttext .=
 6669:                             '<li>'.
 6670:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 6671:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 6672:                             '</li>';
 6673:                     }
 6674:                 }
 6675:             }
 6676:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6677:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6678:                 my $chgtext;
 6679:                 foreach my $type (@{$titleorder}) {
 6680:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6681:                         if (defined($searchtitles->{$type})) {
 6682:                             $chgtext .= $searchtitles->{$type}.'; ';
 6683:                         }
 6684:                     }
 6685:                 }
 6686:                 $chgtext =~ s/\; $//;
 6687:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6688:             }
 6689:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6690:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6691:                 my $chgtext;
 6692:                 foreach my $type (@{$srchtypeorder}) {
 6693:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6694:                         if (defined($srchtypes_desc->{$type})) {
 6695:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6696:                         }
 6697:                     }
 6698:                 }
 6699:                 $chgtext =~ s/\; $//;
 6700:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 6701:             }
 6702:             $resulttext .= '</ul>';
 6703:         } else {
 6704:             $resulttext = &mt('No changes made to institution directory search settings');
 6705:         }
 6706:     } else {
 6707:         $resulttext = '<span class="LC_error">'.
 6708:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6709:     }
 6710:     return $resulttext;
 6711: }
 6712: 
 6713: sub modify_contacts {
 6714:     my ($dom,$lastactref,%domconfig) = @_;
 6715:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6716:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6717:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6718:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6719:         }
 6720:     }
 6721:     my (%others,%to,%bcc);
 6722:     my @contacts = ('supportemail','adminemail');
 6723:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6724:                     'requestsmail','updatesmail','idconflictsmail');
 6725:     my @toggles = ('reporterrors','reportupdates');
 6726:     foreach my $type (@mailings) {
 6727:         @{$newsetting{$type}} = 
 6728:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6729:         foreach my $item (@contacts) {
 6730:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6731:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6732:             } else {
 6733:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6734:             }
 6735:         }  
 6736:         $others{$type} = $env{'form.'.$type.'_others'};
 6737:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6738:         if ($type eq 'helpdeskmail') {
 6739:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6740:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6741:         }
 6742:     }
 6743:     foreach my $item (@contacts) {
 6744:         $to{$item} = $env{'form.'.$item};
 6745:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6746:     }
 6747:     foreach my $item (@toggles) {
 6748:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 6749:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 6750:         }
 6751:     }
 6752:     if (keys(%currsetting) > 0) {
 6753:         foreach my $item (@contacts) {
 6754:             if ($to{$item} ne $currsetting{$item}) {
 6755:                 $changes{$item} = 1;
 6756:             }
 6757:         }
 6758:         foreach my $type (@mailings) {
 6759:             foreach my $item (@contacts) {
 6760:                 if (ref($currsetting{$type}) eq 'HASH') {
 6761:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6762:                         push(@{$changes{$type}},$item);
 6763:                     }
 6764:                 } else {
 6765:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6766:                 }
 6767:             }
 6768:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6769:                 push(@{$changes{$type}},'others');
 6770:             }
 6771:             if ($type eq 'helpdeskmail') {   
 6772:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6773:                     push(@{$changes{$type}},'bcc'); 
 6774:                 }
 6775:             }
 6776:         }
 6777:     } else {
 6778:         my %default;
 6779:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6780:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6781:         $default{'errormail'} = 'adminemail';
 6782:         $default{'packagesmail'} = 'adminemail';
 6783:         $default{'helpdeskmail'} = 'supportemail';
 6784:         $default{'lonstatusmail'} = 'adminemail';
 6785:         $default{'requestsmail'} = 'adminemail';
 6786:         $default{'updatesmail'} = 'adminemail';
 6787:         foreach my $item (@contacts) {
 6788:            if ($to{$item} ne $default{$item}) {
 6789:               $changes{$item} = 1;
 6790:            }
 6791:         }
 6792:         foreach my $type (@mailings) {
 6793:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6794:                
 6795:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6796:             }
 6797:             if ($others{$type} ne '') {
 6798:                 push(@{$changes{$type}},'others');
 6799:             }
 6800:             if ($type eq 'helpdeskmail') {
 6801:                 if ($bcc{$type} ne '') {
 6802:                     push(@{$changes{$type}},'bcc');
 6803:                 }
 6804:             }
 6805:         }
 6806:     }
 6807:     foreach my $item (@toggles) {
 6808:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 6809:             $changes{$item} = 1;
 6810:         } elsif ((!$env{'form.'.$item}) &&
 6811:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 6812:             $changes{$item} = 1;
 6813:         }
 6814:     }
 6815:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6816:                                              $dom);
 6817:     if ($putresult eq 'ok') {
 6818:         if (keys(%changes) > 0) {
 6819:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6820:             if (ref($lastactref) eq 'HASH') {
 6821:                 $lastactref->{'domainconfig'} = 1;
 6822:             }
 6823:             my ($titles,$short_titles)  = &contact_titles();
 6824:             $resulttext = &mt('Changes made:').'<ul>';
 6825:             foreach my $item (@contacts) {
 6826:                 if ($changes{$item}) {
 6827:                     $resulttext .= '<li>'.$titles->{$item}.
 6828:                                     &mt(' set to: ').
 6829:                                     '<span class="LC_cusr_emph">'.
 6830:                                     $to{$item}.'</span></li>';
 6831:                 }
 6832:             }
 6833:             foreach my $type (@mailings) {
 6834:                 if (ref($changes{$type}) eq 'ARRAY') {
 6835:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6836:                     my @text;
 6837:                     foreach my $item (@{$newsetting{$type}}) {
 6838:                         push(@text,$short_titles->{$item});
 6839:                     }
 6840:                     if ($others{$type} ne '') {
 6841:                         push(@text,$others{$type});
 6842:                     }
 6843:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6844:                                    join(', ',@text).'</span>';
 6845:                     if ($type eq 'helpdeskmail') {
 6846:                         if ($bcc{$type} ne '') {
 6847:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6848:                         }
 6849:                     }
 6850:                     $resulttext .= '</li>';
 6851:                 }
 6852:             }
 6853:             my @offon = ('off','on');
 6854:             if ($changes{'reporterrors'}) {
 6855:                 $resulttext .= '<li>'.
 6856:                                &mt('E-mail error reports to [_1] set to "'.
 6857:                                    $offon[$env{'form.reporterrors'}].'".',
 6858:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 6859:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 6860:                                '</li>';
 6861:             }
 6862:             if ($changes{'reportupdates'}) {
 6863:                 $resulttext .= '<li>'.
 6864:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 6865:                                     $offon[$env{'form.reportupdates'}].'".',
 6866:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 6867:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 6868:                                 '</li>';
 6869:             }
 6870:             $resulttext .= '</ul>';
 6871:         } else {
 6872:             $resulttext = &mt('No changes made to contact information');
 6873:         }
 6874:     } else {
 6875:         $resulttext = '<span class="LC_error">'.
 6876:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6877:     }
 6878:     return $resulttext;
 6879: }
 6880: 
 6881: sub modify_usercreation {
 6882:     my ($dom,%domconfig) = @_;
 6883:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6884:     my $warningmsg;
 6885:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6886:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6887:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6888:         }
 6889:     }
 6890:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6891:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6892:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6893:     my @contexts = ('author','course','requestcrs','selfcreate');
 6894:     foreach my $item(@contexts) {
 6895:         if ($item eq 'selfcreate') {
 6896:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6897:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6898:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6899:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6900:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6901:                         $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.');   
 6902:                     }
 6903:                 }
 6904:             }
 6905:         } else {
 6906:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6907:         }
 6908:     }
 6909:     my ($othertitle,$usertypes,$types) = 
 6910:         &Apache::loncommon::sorted_inst_types($dom);
 6911:     if (ref($types) eq 'ARRAY') {
 6912:         if (@{$types} > 0) {
 6913:             @{$cancreate{'statustocreate'}} = 
 6914:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6915:         } else {
 6916:             @{$cancreate{'statustocreate'}} = ();
 6917:         }
 6918:         push(@contexts,'statustocreate');
 6919:     }
 6920:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6921:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6922:         foreach my $item (@contexts) {
 6923:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6924:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6925:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6926:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6927:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6928:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6929:                                     push(@{$changes{'cancreate'}},$item);
 6930:                                 }
 6931:                             }
 6932:                         }
 6933:                     }
 6934:                 } else {
 6935:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6936:                         if (@{$cancreate{$item}} > 0) {
 6937:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6938:                                 push(@{$changes{'cancreate'}},$item);
 6939:                             }
 6940:                         }
 6941:                     } else {
 6942:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6943:                             if (@{$cancreate{$item}} < 3) {
 6944:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6945:                                     push(@{$changes{'cancreate'}},$item);
 6946:                                 }
 6947:                             }
 6948:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6949:                             if (@{$cancreate{$item}} > 0) {
 6950:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6951:                                     push(@{$changes{'cancreate'}},$item);
 6952:                                 }
 6953:                             }
 6954:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6955:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6956:                                 push(@{$changes{'cancreate'}},$item);
 6957:                             }
 6958:                         }
 6959:                     }
 6960:                 }
 6961:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6962:                     foreach my $type (@{$cancreate{$item}}) {
 6963:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6964:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6965:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6966:                                     push(@{$changes{'cancreate'}},$item);
 6967:                                 }
 6968:                             }
 6969:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6970:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6971:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6972:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6973:                                     push(@{$changes{'cancreate'}},$item);
 6974:                                 }
 6975:                             }
 6976:                         }
 6977:                     }
 6978:                 }
 6979:             } else {
 6980:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6981:                     push(@{$changes{'cancreate'}},$item);
 6982:                 }
 6983:             }
 6984:         }
 6985:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6986:         foreach my $item (@contexts) {
 6987:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6988:                 if ($cancreate{$item} ne 'any') {
 6989:                     push(@{$changes{'cancreate'}},$item);
 6990:                 }
 6991:             } else {
 6992:                 if ($cancreate{$item} ne 'none') {
 6993:                     push(@{$changes{'cancreate'}},$item);
 6994:                 }
 6995:             }
 6996:         }
 6997:     } else {
 6998:         foreach my $item (@contexts)  {
 6999:             push(@{$changes{'cancreate'}},$item);
 7000:         }
 7001:     }
 7002: 
 7003:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 7004:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 7005:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 7006:                 push(@{$changes{'username_rule'}},$type);
 7007:             }
 7008:         }
 7009:         foreach my $type (@username_rule) {
 7010:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 7011:                 push(@{$changes{'username_rule'}},$type);
 7012:             }
 7013:         }
 7014:     } else {
 7015:         push(@{$changes{'username_rule'}},@username_rule);
 7016:     }
 7017: 
 7018:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 7019:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 7020:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 7021:                 push(@{$changes{'id_rule'}},$type);
 7022:             }
 7023:         }
 7024:         foreach my $type (@id_rule) {
 7025:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 7026:                 push(@{$changes{'id_rule'}},$type);
 7027:             }
 7028:         }
 7029:     } else {
 7030:         push(@{$changes{'id_rule'}},@id_rule);
 7031:     }
 7032: 
 7033:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 7034:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 7035:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 7036:                 push(@{$changes{'email_rule'}},$type);
 7037:             }
 7038:         }
 7039:         foreach my $type (@email_rule) {
 7040:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 7041:                 push(@{$changes{'email_rule'}},$type);
 7042:             }
 7043:         }
 7044:     } else {
 7045:         push(@{$changes{'email_rule'}},@email_rule);
 7046:     }
 7047: 
 7048:     my @authen_contexts = ('author','course','domain');
 7049:     my @authtypes = ('int','krb4','krb5','loc');
 7050:     my %authhash;
 7051:     foreach my $item (@authen_contexts) {
 7052:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 7053:         foreach my $auth (@authtypes) {
 7054:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 7055:                 $authhash{$item}{$auth} = 1;
 7056:             } else {
 7057:                 $authhash{$item}{$auth} = 0;
 7058:             }
 7059:         }
 7060:     }
 7061:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 7062:         foreach my $item (@authen_contexts) {
 7063:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 7064:                 foreach my $auth (@authtypes) {
 7065:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 7066:                         push(@{$changes{'authtypes'}},$item);
 7067:                         last;
 7068:                     }
 7069:                 }
 7070:             }
 7071:         }
 7072:     } else {
 7073:         foreach my $item (@authen_contexts) {
 7074:             push(@{$changes{'authtypes'}},$item);
 7075:         }
 7076:     }
 7077: 
 7078:     my %usercreation_hash =  (
 7079:             usercreation => {
 7080:                               cancreate     => \%cancreate,
 7081:                               username_rule => \@username_rule,
 7082:                               id_rule       => \@id_rule,
 7083:                               email_rule    => \@email_rule,
 7084:                               authtypes     => \%authhash,
 7085:                             }
 7086:             );
 7087: 
 7088:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 7089:                                              $dom);
 7090: 
 7091:     my %selfcreatetypes = (
 7092:                              sso   => 'users authenticated by institutional single sign on',
 7093:                              login => 'users authenticated by institutional log-in',
 7094:                              email => 'users who provide a valid e-mail address for use as the username',
 7095:                           );
 7096:     if ($putresult eq 'ok') {
 7097:         if (keys(%changes) > 0) {
 7098:             $resulttext = &mt('Changes made:').'<ul>';
 7099:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 7100:                 my %lt = &usercreation_types();
 7101:                 foreach my $type (@{$changes{'cancreate'}}) {
 7102:                     my $chgtext;
 7103:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 7104:                         $chgtext = $lt{$type}.', ';
 7105:                     }
 7106:                     if ($type eq 'selfcreate') {
 7107:                         if (@{$cancreate{$type}} == 0) {
 7108:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 7109:                         } else {
 7110:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 7111:                             foreach my $case (@{$cancreate{$type}}) {
 7112:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 7113:                             }
 7114:                             $chgtext .= '</ul>';
 7115:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 7116:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 7117:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 7118:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 7119:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 7120:                                         }
 7121:                                     }
 7122:                                 }
 7123:                             }
 7124:                         }
 7125:                     } elsif ($type eq 'statustocreate') {
 7126:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 7127:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 7128:                             if (@{$cancreate{'selfcreate'}} > 0) {
 7129:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 7130: 
 7131:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 7132:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 7133:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 7134:                                     } 
 7135:                                 } elsif (ref($usertypes) eq 'HASH') {
 7136:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 7137:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 7138:                                     } else {
 7139:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 7140:                                     }
 7141:                                     $chgtext .= '<ul>';
 7142:                                     foreach my $case (@{$cancreate{$type}}) {
 7143:                                         if ($case eq 'default') {
 7144:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 7145:                                         } else {
 7146:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 7147:                                         }
 7148:                                     }
 7149:                                     $chgtext .= '</ul>';
 7150:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 7151:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 7152:                                     }
 7153:                                 }
 7154:                             } else {
 7155:                                 if (@{$cancreate{$type}} == 0) {
 7156:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 7157:                                 } else {
 7158:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 7159:                                 }
 7160:                             }
 7161:                         }
 7162:                     } elsif ($type eq 'captcha') {
 7163:                         if ($cancreate{$type} eq 'notused') {
 7164:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 7165:                         } else {
 7166:                             my %captchas = &captcha_phrases();
 7167:                             if ($captchas{$cancreate{$type}}) {
 7168:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 7169:                             } else {
 7170:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 7171:                             }
 7172:                         }
 7173:                     } elsif ($type eq 'recaptchakeys') {
 7174:                         my ($privkey,$pubkey);
 7175:                         if (ref($cancreate{$type}) eq 'HASH') {
 7176:                             $pubkey = $cancreate{$type}{'public'};
 7177:                             $privkey = $cancreate{$type}{'private'};
 7178:                         }
 7179:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 7180:                         if (!$pubkey) {
 7181:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 7182:                         } else {
 7183:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7184:                         }
 7185:                         if (!$privkey) {
 7186:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 7187:                         } else {
 7188:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 7189:                         }
 7190:                         $chgtext .= '</ul>';
 7191:                     } else {
 7192:                         if ($cancreate{$type} eq 'none') {
 7193:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 7194:                         } elsif ($cancreate{$type} eq 'any') {
 7195:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 7196:                         } elsif ($cancreate{$type} eq 'official') {
 7197:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 7198:                         } elsif ($cancreate{$type} eq 'unofficial') {
 7199:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 7200:                         }
 7201:                     }
 7202:                     $resulttext .= '<li>'.$chgtext.'</li>';
 7203:                 }
 7204:             }
 7205:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 7206:                 my ($rules,$ruleorder) = 
 7207:                     &Apache::lonnet::inst_userrules($dom,'username');
 7208:                 my $chgtext = '<ul>';
 7209:                 foreach my $type (@username_rule) {
 7210:                     if (ref($rules->{$type}) eq 'HASH') {
 7211:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 7212:                     }
 7213:                 }
 7214:                 $chgtext .= '</ul>';
 7215:                 if (@username_rule > 0) {
 7216:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 7217:                 } else {
 7218:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 7219:                 }
 7220:             }
 7221:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 7222:                 my ($idrules,$idruleorder) = 
 7223:                     &Apache::lonnet::inst_userrules($dom,'id');
 7224:                 my $chgtext = '<ul>';
 7225:                 foreach my $type (@id_rule) {
 7226:                     if (ref($idrules->{$type}) eq 'HASH') {
 7227:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 7228:                     }
 7229:                 }
 7230:                 $chgtext .= '</ul>';
 7231:                 if (@id_rule > 0) {
 7232:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 7233:                 } else {
 7234:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 7235:                 }
 7236:             }
 7237:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 7238:                 my ($emailrules,$emailruleorder) =
 7239:                     &Apache::lonnet::inst_userrules($dom,'email');
 7240:                 my $chgtext = '<ul>';
 7241:                 foreach my $type (@email_rule) {
 7242:                     if (ref($emailrules->{$type}) eq 'HASH') {
 7243:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 7244:                     }
 7245:                 }
 7246:                 $chgtext .= '</ul>';
 7247:                 if (@email_rule > 0) {
 7248:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 7249:                 } else {
 7250:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 7251:                 }
 7252:             }
 7253: 
 7254:             my %authname = &authtype_names();
 7255:             my %context_title = &context_names();
 7256:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7257:                 my $chgtext = '<ul>';
 7258:                 foreach my $type (@{$changes{'authtypes'}}) {
 7259:                     my @allowed;
 7260:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7261:                     foreach my $auth (@authtypes) {
 7262:                         if ($authhash{$type}{$auth}) {
 7263:                             push(@allowed,$authname{$auth});
 7264:                         }
 7265:                     }
 7266:                     if (@allowed > 0) {
 7267:                         $chgtext .= join(', ',@allowed).'</li>';
 7268:                     } else {
 7269:                         $chgtext .= &mt('none').'</li>';
 7270:                     }
 7271:                 }
 7272:                 $chgtext .= '</ul>';
 7273:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7274:                 $resulttext .= '</li>';
 7275:             }
 7276:             $resulttext .= '</ul>';
 7277:         } else {
 7278:             $resulttext = &mt('No changes made to user creation settings');
 7279:         }
 7280:     } else {
 7281:         $resulttext = '<span class="LC_error">'.
 7282:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7283:     }
 7284:     if ($warningmsg ne '') {
 7285:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7286:     }
 7287:     return $resulttext;
 7288: }
 7289: 
 7290: sub process_captcha {
 7291:     my ($container,$changes,$newsettings,$current) = @_;
 7292:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 7293:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 7294:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 7295:         $newsettings->{'captcha'} = 'original';
 7296:     }
 7297:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 7298:         if ($container eq 'cancreate') {
 7299:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7300:                 push(@{$changes->{'cancreate'}},'captcha');
 7301:             } elsif (!defined($changes->{'cancreate'})) {
 7302:                 $changes->{'cancreate'} = ['captcha'];
 7303:             }
 7304:         } else {
 7305:             $changes->{'captcha'} = 1;
 7306:         }
 7307:     }
 7308:     my ($newpub,$newpriv,$currpub,$currpriv);
 7309:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 7310:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 7311:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 7312:         $newpub =~ s/\W//g;
 7313:         $newpriv =~ s/\W//g;
 7314:         $newsettings->{'recaptchakeys'} = {
 7315:                                              public  => $newpub,
 7316:                                              private => $newpriv,
 7317:                                           };
 7318:     }
 7319:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 7320:         $currpub = $current->{'recaptchakeys'}{'public'};
 7321:         $currpriv = $current->{'recaptchakeys'}{'private'};
 7322:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 7323:             $newsettings->{'recaptchakeys'} = {
 7324:                                                  public  => '',
 7325:                                                  private => '',
 7326:                                               }
 7327:         }
 7328:     }
 7329:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 7330:         if ($container eq 'cancreate') {
 7331:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7332:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 7333:             } elsif (!defined($changes->{'cancreate'})) {
 7334:                 $changes->{'cancreate'} = ['recaptchakeys'];
 7335:             }
 7336:         } else {
 7337:             $changes->{'recaptchakeys'} = 1;
 7338:         }
 7339:     }
 7340:     return;
 7341: }
 7342: 
 7343: sub modify_usermodification {
 7344:     my ($dom,%domconfig) = @_;
 7345:     my ($resulttext,%curr_usermodification,%changes);
 7346:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7347:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7348:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 7349:         }
 7350:     }
 7351:     my @contexts = ('author','course','selfcreate');
 7352:     my %context_title = (
 7353:                            author => 'In author context',
 7354:                            course => 'In course context',
 7355:                            selfcreate => 'When self creating account', 
 7356:                         );
 7357:     my @fields = ('lastname','firstname','middlename','generation',
 7358:                   'permanentemail','id');
 7359:     my %roles = (
 7360:                   author => ['ca','aa'],
 7361:                   course => ['st','ep','ta','in','cr'],
 7362:                 );
 7363:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7364:     if (ref($types) eq 'ARRAY') {
 7365:         push(@{$types},'default');
 7366:         $usertypes->{'default'} = $othertitle;
 7367:     }
 7368:     $roles{'selfcreate'} = $types;  
 7369:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7370:     my %modifyhash;
 7371:     foreach my $context (@contexts) {
 7372:         foreach my $role (@{$roles{$context}}) {
 7373:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7374:             foreach my $item (@fields) {
 7375:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7376:                     $modifyhash{$context}{$role}{$item} = 1;
 7377:                 } else {
 7378:                     $modifyhash{$context}{$role}{$item} = 0;
 7379:                 }
 7380:             }
 7381:         }
 7382:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7383:             foreach my $role (@{$roles{$context}}) {
 7384:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7385:                     foreach my $field (@fields) {
 7386:                         if ($modifyhash{$context}{$role}{$field} ne 
 7387:                                 $curr_usermodification{$context}{$role}{$field}) {
 7388:                             push(@{$changes{$context}},$role);
 7389:                             last;
 7390:                         }
 7391:                     }
 7392:                 }
 7393:             }
 7394:         } else {
 7395:             foreach my $context (@contexts) {
 7396:                 foreach my $role (@{$roles{$context}}) {
 7397:                     push(@{$changes{$context}},$role);
 7398:                 }
 7399:             }
 7400:         }
 7401:     }
 7402:     my %usermodification_hash =  (
 7403:                                    usermodification => \%modifyhash,
 7404:                                  );
 7405:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7406:                                              \%usermodification_hash,$dom);
 7407:     if ($putresult eq 'ok') {
 7408:         if (keys(%changes) > 0) {
 7409:             $resulttext = &mt('Changes made: ').'<ul>';
 7410:             foreach my $context (@contexts) {
 7411:                 if (ref($changes{$context}) eq 'ARRAY') {
 7412:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7413:                     if (ref($changes{$context}) eq 'ARRAY') {
 7414:                         foreach my $role (@{$changes{$context}}) {
 7415:                             my $rolename;
 7416:                             if ($context eq 'selfcreate') {
 7417:                                 $rolename = $role;
 7418:                                 if (ref($usertypes) eq 'HASH') {
 7419:                                     if ($usertypes->{$role} ne '') {
 7420:                                         $rolename = $usertypes->{$role};
 7421:                                     }
 7422:                                 }
 7423:                             } else {
 7424:                                 if ($role eq 'cr') {
 7425:                                     $rolename = &mt('Custom');
 7426:                                 } else {
 7427:                                     $rolename = &Apache::lonnet::plaintext($role);
 7428:                                 }
 7429:                             }
 7430:                             my @modifiable;
 7431:                             if ($context eq 'selfcreate') {
 7432:                                 $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): ');
 7433:                             } else {
 7434:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7435:                             }
 7436:                             foreach my $field (@fields) {
 7437:                                 if ($modifyhash{$context}{$role}{$field}) {
 7438:                                     push(@modifiable,$fieldtitles{$field});
 7439:                                 }
 7440:                             }
 7441:                             if (@modifiable > 0) {
 7442:                                 $resulttext .= join(', ',@modifiable);
 7443:                             } else {
 7444:                                 $resulttext .= &mt('none'); 
 7445:                             }
 7446:                             $resulttext .= '</li>';
 7447:                         }
 7448:                         $resulttext .= '</ul></li>';
 7449:                     }
 7450:                 }
 7451:             }
 7452:             $resulttext .= '</ul>';
 7453:         } else {
 7454:             $resulttext = &mt('No changes made to user modification settings');
 7455:         }
 7456:     } else {
 7457:         $resulttext = '<span class="LC_error">'.
 7458:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7459:     }
 7460:     return $resulttext;
 7461: }
 7462: 
 7463: sub modify_defaults {
 7464:     my ($dom,$lastactref,%domconfig) = @_;
 7465:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7466:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7467:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7468:     my @authtypes = ('internal','krb4','krb5','localauth');
 7469:     foreach my $item (@items) {
 7470:         $newvalues{$item} = $env{'form.'.$item};
 7471:         if ($item eq 'auth_def') {
 7472:             if ($newvalues{$item} ne '') {
 7473:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7474:                     push(@errors,$item);
 7475:                 }
 7476:             }
 7477:         } elsif ($item eq 'lang_def') {
 7478:             if ($newvalues{$item} ne '') {
 7479:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7480:                     my $langcode = $1;
 7481:                     if ($langcode ne 'x_chef') {
 7482:                         if (code2language($langcode) eq '') {
 7483:                             push(@errors,$item);
 7484:                         }
 7485:                     }
 7486:                 } else {
 7487:                     push(@errors,$item);
 7488:                 }
 7489:             }
 7490:         } elsif ($item eq 'timezone_def') {
 7491:             if ($newvalues{$item} ne '') {
 7492:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7493:                     push(@errors,$item);   
 7494:                 }
 7495:             }
 7496:         } elsif ($item eq 'datelocale_def') {
 7497:             if ($newvalues{$item} ne '') {
 7498:                 my @datelocale_ids = DateTime::Locale->ids();
 7499:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7500:                     push(@errors,$item);
 7501:                 }
 7502:             }
 7503:         } elsif ($item eq 'portal_def') {
 7504:             if ($newvalues{$item} ne '') {
 7505:                 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])\/?$/) {
 7506:                     push(@errors,$item);
 7507:                 }
 7508:             }
 7509:         }
 7510:         if (grep(/^\Q$item\E$/,@errors)) {
 7511:             $newvalues{$item} = $domdefaults{$item};
 7512:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7513:             $changes{$item} = 1;
 7514:         }
 7515:         $domdefaults{$item} = $newvalues{$item};
 7516:     }
 7517:     my %defaults_hash = (
 7518:                          defaults => \%newvalues,
 7519:                         );
 7520:     my $title = &defaults_titles();
 7521:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7522:                                              $dom);
 7523:     if ($putresult eq 'ok') {
 7524:         if (keys(%changes) > 0) {
 7525:             $resulttext = &mt('Changes made:').'<ul>';
 7526:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 7527:             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";
 7528:             foreach my $item (sort(keys(%changes))) {
 7529:                 my $value = $env{'form.'.$item};
 7530:                 if ($value eq '') {
 7531:                     $value = &mt('none');
 7532:                 } elsif ($item eq 'auth_def') {
 7533:                     my %authnames = &authtype_names();
 7534:                     my %shortauth = (
 7535:                              internal => 'int',
 7536:                              krb4 => 'krb4',
 7537:                              krb5 => 'krb5',
 7538:                              localauth  => 'loc',
 7539:                     );
 7540:                     $value = $authnames{$shortauth{$value}};
 7541:                 }
 7542:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7543:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7544:             }
 7545:             $resulttext .= '</ul>';
 7546:             $mailmsgtext .= "\n";
 7547:             my $cachetime = 24*60*60;
 7548:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7549:             if (ref($lastactref) eq 'HASH') {
 7550:                 $lastactref->{'domdefaults'} = 1;
 7551:             }
 7552:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7553:                 my $notify = 1;
 7554:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 7555:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 7556:                         $notify = 0;
 7557:                     }
 7558:                 }
 7559:                 if ($notify) {
 7560:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 7561:                                                "LON-CAPA Domain Settings Change - $dom",
 7562:                                                $mailmsgtext);
 7563:                 }
 7564:             }
 7565:         } else {
 7566:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 7567:         }
 7568:     } else {
 7569:         $resulttext = '<span class="LC_error">'.
 7570:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7571:     }
 7572:     if (@errors > 0) {
 7573:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7574:         foreach my $item (@errors) {
 7575:             $resulttext .= ' "'.$title->{$item}.'",';
 7576:         }
 7577:         $resulttext =~ s/,$//;
 7578:     }
 7579:     return $resulttext;
 7580: }
 7581: 
 7582: sub modify_scantron {
 7583:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7584:     my ($resulttext,%confhash,%changes,$errors);
 7585:     my $custom = 'custom.tab';
 7586:     my $default = 'default.tab';
 7587:     my $servadm = $r->dir_config('lonAdmEMail');
 7588:     my ($configuserok,$author_ok,$switchserver) = 
 7589:         &config_check($dom,$confname,$servadm);
 7590:     if ($env{'form.scantronformat.filename'} ne '') {
 7591:         my $error;
 7592:         if ($configuserok eq 'ok') {
 7593:             if ($switchserver) {
 7594:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7595:             } else {
 7596:                 if ($author_ok eq 'ok') {
 7597:                     my ($result,$scantronurl) =
 7598:                         &publishlogo($r,'upload','scantronformat',$dom,
 7599:                                      $confname,'scantron','','',$custom);
 7600:                     if ($result eq 'ok') {
 7601:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7602:                         $changes{'scantronformat'} = 1;
 7603:                     } else {
 7604:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7605:                     }
 7606:                 } else {
 7607:                     $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);
 7608:                 }
 7609:             }
 7610:         } else {
 7611:             $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);
 7612:         }
 7613:         if ($error) {
 7614:             &Apache::lonnet::logthis($error);
 7615:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7616:         }
 7617:     }
 7618:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7619:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7620:             if ($env{'form.scantronformat_del'}) {
 7621:                 $confhash{'scantron'}{'scantronformat'} = '';
 7622:                 $changes{'scantronformat'} = 1;
 7623:             }
 7624:         }
 7625:     }
 7626:     if (keys(%confhash) > 0) {
 7627:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7628:                                                  $dom);
 7629:         if ($putresult eq 'ok') {
 7630:             if (keys(%changes) > 0) {
 7631:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7632:                     $resulttext = &mt('Changes made:').'<ul>';
 7633:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7634:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7635:                     } else {
 7636:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7637:                     }
 7638:                     $resulttext .= '</ul>';
 7639:                 } else {
 7640:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7641:                 }
 7642:                 $resulttext .= '</ul>';
 7643:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7644:                 if (ref($lastactref) eq 'HASH') {
 7645:                     $lastactref->{'domainconfig'} = 1;
 7646:                 }
 7647:             } else {
 7648:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7649:             }
 7650:         } else {
 7651:             $resulttext = '<span class="LC_error">'.
 7652:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7653:         }
 7654:     } else {
 7655:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7656:     }
 7657:     if ($errors) {
 7658:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7659:                        $errors.'</ul>';
 7660:     }
 7661:     return $resulttext;
 7662: }
 7663: 
 7664: sub modify_coursecategories {
 7665:     my ($dom,%domconfig) = @_;
 7666:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7667:         $cathash);
 7668:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7669:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7670:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7671:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7672:             $changes{'togglecats'} = 1;
 7673:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7674:         }
 7675:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7676:             $changes{'categorize'} = 1;
 7677:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7678:         }
 7679:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7680:             $changes{'togglecatscomm'} = 1;
 7681:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7682:         }
 7683:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7684:             $changes{'categorizecomm'} = 1;
 7685:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7686:         }
 7687:     } else {
 7688:         $changes{'togglecats'} = 1;
 7689:         $changes{'categorize'} = 1;
 7690:         $changes{'togglecatscomm'} = 1;
 7691:         $changes{'categorizecomm'} = 1;
 7692:         $domconfig{'coursecategories'} = {
 7693:                                              togglecats => $env{'form.togglecats'},
 7694:                                              categorize => $env{'form.categorize'},
 7695:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7696:                                              categorizecomm => $env{'form.categorizecomm'},
 7697:                                          };
 7698:     }
 7699:     if (ref($cathash) eq 'HASH') {
 7700:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7701:             push (@deletecategory,'instcode::0');
 7702:         }
 7703:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7704:             push(@deletecategory,'communities::0');
 7705:         }
 7706:     }
 7707:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7708:     if (ref($cathash) eq 'HASH') {
 7709:         if (@deletecategory > 0) {
 7710:             #FIXME Need to remove category from all courses using a deleted category 
 7711:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7712:             foreach my $item (@deletecategory) {
 7713:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7714:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7715:                     $deletions{$item} = 1;
 7716:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7717:                 }
 7718:             }
 7719:         }
 7720:         foreach my $item (keys(%{$cathash})) {
 7721:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7722:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7723:                 $reorderings{$item} = 1;
 7724:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7725:             }
 7726:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7727:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7728:                 my $newdepth = $depth+1;
 7729:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7730:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7731:                 $adds{$newitem} = 1; 
 7732:             }
 7733:             if ($env{'form.subcat_'.$item} ne '') {
 7734:                 my $newcat = $env{'form.subcat_'.$item};
 7735:                 my $newdepth = $depth+1;
 7736:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7737:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7738:                 $adds{$newitem} = 1;
 7739:             }
 7740:         }
 7741:     }
 7742:     if ($env{'form.instcode'} eq '1') {
 7743:         if (ref($cathash) eq 'HASH') {
 7744:             my $newitem = 'instcode::0';
 7745:             if ($cathash->{$newitem} eq '') {  
 7746:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7747:                 $adds{$newitem} = 1;
 7748:             }
 7749:         } else {
 7750:             my $newitem = 'instcode::0';
 7751:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7752:             $adds{$newitem} = 1;
 7753:         }
 7754:     }
 7755:     if ($env{'form.communities'} eq '1') {
 7756:         if (ref($cathash) eq 'HASH') {
 7757:             my $newitem = 'communities::0';
 7758:             if ($cathash->{$newitem} eq '') {
 7759:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7760:                 $adds{$newitem} = 1;
 7761:             }
 7762:         } else {
 7763:             my $newitem = 'communities::0';
 7764:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7765:             $adds{$newitem} = 1;
 7766:         }
 7767:     }
 7768:     if ($env{'form.addcategory_name'} ne '') {
 7769:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7770:             ($env{'form.addcategory_name'} ne 'communities')) {
 7771:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7772:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7773:             $adds{$newitem} = 1;
 7774:         }
 7775:     }
 7776:     my $putresult;
 7777:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7778:         if (keys(%deletions) > 0) {
 7779:             foreach my $key (keys(%deletions)) {
 7780:                 if ($predelallitems{$key} ne '') {
 7781:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7782:                 }
 7783:             }
 7784:         }
 7785:         my (@chkcats,@chktrails,%chkallitems);
 7786:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7787:         if (ref($chkcats[0]) eq 'ARRAY') {
 7788:             my $depth = 0;
 7789:             my $chg = 0;
 7790:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7791:                 my $name = $chkcats[0][$i];
 7792:                 my $item;
 7793:                 if ($name eq '') {
 7794:                     $chg ++;
 7795:                 } else {
 7796:                     $item = &escape($name).'::0';
 7797:                     if ($chg) {
 7798:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7799:                     }
 7800:                     $depth ++; 
 7801:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7802:                     $depth --;
 7803:                 }
 7804:             }
 7805:         }
 7806:     }
 7807:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7808:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7809:         if ($putresult eq 'ok') {
 7810:             my %title = (
 7811:                          togglecats     => 'Show/Hide a course in catalog',
 7812:                          categorize     => 'Assign a category to a course',
 7813:                          togglecatscomm => 'Show/Hide a community in catalog',
 7814:                          categorizecomm => 'Assign a category to a community',
 7815:                         );
 7816:             my %level = (
 7817:                          dom  => 'set in Domain ("Modify Course/Community")',
 7818:                          crs  => 'set in Course ("Course Configuration")',
 7819:                          comm => 'set in Community ("Community Configuration")',
 7820:                         );
 7821:             $resulttext = &mt('Changes made:').'<ul>';
 7822:             if ($changes{'togglecats'}) {
 7823:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7824:             }
 7825:             if ($changes{'categorize'}) {
 7826:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7827:             }
 7828:             if ($changes{'togglecatscomm'}) {
 7829:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7830:             }
 7831:             if ($changes{'categorizecomm'}) {
 7832:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7833:             }
 7834:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7835:                 my $cathash;
 7836:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7837:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7838:                 } else {
 7839:                     $cathash = {};
 7840:                 } 
 7841:                 my (@cats,@trails,%allitems);
 7842:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7843:                 if (keys(%deletions) > 0) {
 7844:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7845:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7846:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7847:                     }
 7848:                     $resulttext .= '</ul></li>';
 7849:                 }
 7850:                 if (keys(%reorderings) > 0) {
 7851:                     my %sort_by_trail;
 7852:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7853:                     foreach my $key (keys(%reorderings)) {
 7854:                         if ($allitems{$key} ne '') {
 7855:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7856:                         }
 7857:                     }
 7858:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7859:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7860:                     }
 7861:                     $resulttext .= '</ul></li>';
 7862:                 }
 7863:                 if (keys(%adds) > 0) {
 7864:                     my %sort_by_trail;
 7865:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7866:                     foreach my $key (keys(%adds)) {
 7867:                         if ($allitems{$key} ne '') {
 7868:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7869:                         }
 7870:                     }
 7871:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7872:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7873:                     }
 7874:                     $resulttext .= '</ul></li>';
 7875:                 }
 7876:             }
 7877:             $resulttext .= '</ul>';
 7878:         } else {
 7879:             $resulttext = '<span class="LC_error">'.
 7880:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7881:         }
 7882:     } else {
 7883:         $resulttext = &mt('No changes made to course and community categories');
 7884:     }
 7885:     return $resulttext;
 7886: }
 7887: 
 7888: sub modify_serverstatuses {
 7889:     my ($dom,%domconfig) = @_;
 7890:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7891:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7892:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7893:     }
 7894:     my @pages = &serverstatus_pages();
 7895:     foreach my $type (@pages) {
 7896:         $newserverstatus{$type}{'namedusers'} = '';
 7897:         $newserverstatus{$type}{'machines'} = '';
 7898:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7899:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7900:             my @okusers;
 7901:             foreach my $user (@users) {
 7902:                 my ($uname,$udom) = split(/:/,$user);
 7903:                 if (($udom =~ /^$match_domain$/) &&   
 7904:                     (&Apache::lonnet::domain($udom)) &&
 7905:                     ($uname =~ /^$match_username$/)) {
 7906:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7907:                         push(@okusers,$user);
 7908:                     }
 7909:                 }
 7910:             }
 7911:             if (@okusers > 0) {
 7912:                  @okusers = sort(@okusers);
 7913:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7914:             }
 7915:         }
 7916:         if (defined($env{'form.'.$type.'_machines'})) {
 7917:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7918:             my @okmachines;
 7919:             foreach my $ip (@machines) {
 7920:                 my @parts = split(/\./,$ip);
 7921:                 next if (@parts < 4);
 7922:                 my $badip = 0;
 7923:                 for (my $i=0; $i<4; $i++) {
 7924:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7925:                         $badip = 1;
 7926:                         last;
 7927:                     }
 7928:                 }
 7929:                 if (!$badip) {
 7930:                     push(@okmachines,$ip);     
 7931:                 }
 7932:             }
 7933:             @okmachines = sort(@okmachines);
 7934:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7935:         }
 7936:     }
 7937:     my %serverstatushash =  (
 7938:                                 serverstatuses => \%newserverstatus,
 7939:                             );
 7940:     foreach my $type (@pages) {
 7941:         foreach my $setting ('namedusers','machines') {
 7942:             my (@current,@new);
 7943:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7944:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7945:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7946:                 }
 7947:             }
 7948:             if ($newserverstatus{$type}{$setting} ne '') {
 7949:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7950:             }
 7951:             if (@current > 0) {
 7952:                 if (@new > 0) {
 7953:                     foreach my $item (@current) {
 7954:                         if (!grep(/^\Q$item\E$/,@new)) {
 7955:                             $changes{$type}{$setting} = 1;
 7956:                             last;
 7957:                         }
 7958:                     }
 7959:                     foreach my $item (@new) {
 7960:                         if (!grep(/^\Q$item\E$/,@current)) {
 7961:                             $changes{$type}{$setting} = 1;
 7962:                             last;
 7963:                         }
 7964:                     }
 7965:                 } else {
 7966:                     $changes{$type}{$setting} = 1;
 7967:                 }
 7968:             } elsif (@new > 0) {
 7969:                 $changes{$type}{$setting} = 1;
 7970:             }
 7971:         }
 7972:     }
 7973:     if (keys(%changes) > 0) {
 7974:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7975:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7976:                                                  \%serverstatushash,$dom);
 7977:         if ($putresult eq 'ok') {
 7978:             $resulttext .= &mt('Changes made:').'<ul>';
 7979:             foreach my $type (@pages) {
 7980:                 if (ref($changes{$type}) eq 'HASH') {
 7981:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7982:                     if ($changes{$type}{'namedusers'}) {
 7983:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7984:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7985:                         } else {
 7986:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7987:                         }
 7988:                     }
 7989:                     if ($changes{$type}{'machines'}) {
 7990:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7991:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7992:                         } else {
 7993:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7994:                         }
 7995: 
 7996:                     }
 7997:                     $resulttext .= '</ul></li>';
 7998:                 }
 7999:             }
 8000:             $resulttext .= '</ul>';
 8001:         } else {
 8002:             $resulttext = '<span class="LC_error">'.
 8003:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 8004: 
 8005:         }
 8006:     } else {
 8007:         $resulttext = &mt('No changes made to access to server status pages');
 8008:     }
 8009:     return $resulttext;
 8010: }
 8011: 
 8012: sub modify_helpsettings {
 8013:     my ($r,$dom,$confname,%domconfig) = @_;
 8014:     my ($resulttext,$errors,%changes,%helphash);
 8015:     my %defaultchecked = ('submitbugs' => 'on');
 8016:     my @offon = ('off','on');
 8017:     my @toggles = ('submitbugs');
 8018:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 8019:         foreach my $item (@toggles) {
 8020:             if ($defaultchecked{$item} eq 'on') { 
 8021:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 8022:                     if ($env{'form.'.$item} eq '0') {
 8023:                         $changes{$item} = 1;
 8024:                     }
 8025:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8026:                     $changes{$item} = 1;
 8027:                 }
 8028:             } elsif ($defaultchecked{$item} eq 'off') {
 8029:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 8030:                     if ($env{'form.'.$item} eq '1') {
 8031:                         $changes{$item} = 1;
 8032:                     }
 8033:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8034:                     $changes{$item} = 1;
 8035:                 }
 8036:             }
 8037:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 8038:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 8039:             }
 8040:         }
 8041:     }
 8042:     my $putresult;
 8043:     if (keys(%changes) > 0) {
 8044:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 8045:         if ($putresult eq 'ok') {
 8046:             $resulttext = &mt('Changes made:').'<ul>';
 8047:             foreach my $item (sort(keys(%changes))) {
 8048:                 if ($item eq 'submitbugs') {
 8049:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 8050:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 8051:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 8052:                 }
 8053:             }
 8054:             $resulttext .= '</ul>';
 8055:         } else {
 8056:             $resulttext = &mt('No changes made to help settings');
 8057:             $errors .= '<li><span class="LC_error">'.
 8058:                        &mt('An error occurred storing the settings: [_1]',
 8059:                            $putresult).'</span></li>';
 8060:         }
 8061:     }
 8062:     if ($errors) {
 8063:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8064:                        $errors.'</ul>';
 8065:     }
 8066:     return $resulttext;
 8067: }
 8068: 
 8069: sub modify_coursedefaults {
 8070:     my ($dom,$lastactref,%domconfig) = @_;
 8071:     my ($resulttext,$errors,%changes,%defaultshash);
 8072:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 8073:     my @toggles = ('canuse_pdfforms');
 8074:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 8075:                    'uploadquota_community');
 8076:     my @types = ('official','unofficial','community');
 8077:     my %staticdefaults = (
 8078:                            anonsurvey_threshold => 10,
 8079:                            uploadquota          => 500,
 8080:                          );
 8081: 
 8082:     $defaultshash{'coursedefaults'} = {};
 8083: 
 8084:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 8085:         if ($domconfig{'coursedefaults'} eq '') {
 8086:             $domconfig{'coursedefaults'} = {};
 8087:         }
 8088:     }
 8089: 
 8090:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 8091:         foreach my $item (@toggles) {
 8092:             if ($defaultchecked{$item} eq 'on') {
 8093:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 8094:                     ($env{'form.'.$item} eq '0')) {
 8095:                     $changes{$item} = 1;
 8096:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 8097:                     $changes{$item} = 1;
 8098:                 }
 8099:             } elsif ($defaultchecked{$item} eq 'off') {
 8100:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 8101:                     ($env{'form.'.$item} eq '1')) {
 8102:                     $changes{$item} = 1;
 8103:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 8104:                     $changes{$item} = 1;
 8105:                 }
 8106:             }
 8107:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 8108:         }
 8109:         foreach my $item (@numbers) {
 8110:             my ($currdef,$newdef);
 8111:             $newdef = $env{'form.'.$item};
 8112:             if ($item eq 'anonsurvey_threshold') {
 8113:                 $currdef = $domconfig{'coursedefaults'}{$item};
 8114:                 $newdef =~ s/\D//g;
 8115:                 if ($newdef eq '' || $newdef < 1) {
 8116:                     $newdef = 1;
 8117:                 }
 8118:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 8119:             } else {
 8120:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 8121:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8122:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 8123:                 }
 8124:                 $newdef =~ s/[^\w.\-]//g;
 8125:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 8126:             }
 8127:             if ($currdef ne $newdef) {
 8128:                 my $staticdef;
 8129:                 if ($item eq 'anonsurvey_threshold') {
 8130:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
 8131:                         $changes{$item} = 1;
 8132:                     }
 8133:                 } else {
 8134:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
 8135:                         $changes{'uploadquota'} = 1;
 8136:                     }
 8137:                 }
 8138:             }
 8139:         }
 8140:         my $officialcreds = $env{'form.official_credits'};
 8141:         $officialcreds =~ s/^[^\d\.]//g;
 8142:         my $unofficialcreds = $env{'form.unofficial_credits'};
 8143:         $unofficialcreds =~ s/^[^\d\.]//g;
 8144:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 8145:                 ($env{'form.coursecredits'} eq '1')) {
 8146:                 $changes{'coursecredits'} = 1;
 8147:         } else {
 8148:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 8149:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds)) {
 8150:                 $changes{'coursecredits'} = 1;
 8151:             }
 8152:         }
 8153:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 8154:             official   => $officialcreds,
 8155:             unofficial => $unofficialcreds,
 8156:         }
 8157:     }
 8158:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8159:                                              $dom);
 8160:     if ($putresult eq 'ok') {
 8161:         if (keys(%changes) > 0) {
 8162:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8163:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || ($changes{'uploadquota'})) {
 8164:                 if ($changes{'canuse_pdfforms'}) {
 8165:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 8166:                 }
 8167:                 if ($changes{'coursecredits'}) {
 8168:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 8169:                         $domdefaults{'officialcredits'} =
 8170:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 8171:                         $domdefaults{'unofficialcredits'} =
 8172:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 8173:                     }
 8174:                 }
 8175:                 if ($changes{'uploadquota'}) {
 8176:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8177:                         foreach my $type (@types) {
 8178:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
 8179:                         }
 8180:                     }
 8181:                 }
 8182:                 my $cachetime = 24*60*60;
 8183:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8184:                 if (ref($lastactref) eq 'HASH') {
 8185:                     $lastactref->{'domdefaults'} = 1;
 8186:                 }
 8187:             }
 8188:             $resulttext = &mt('Changes made:').'<ul>';
 8189:             foreach my $item (sort(keys(%changes))) {
 8190:                 if ($item eq 'canuse_pdfforms') {
 8191:                     if ($env{'form.'.$item} eq '1') {
 8192:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 8193:                     } else {
 8194:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 8195:                     }
 8196:                 } elsif ($item eq 'anonsurvey_threshold') {
 8197:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 8198:                 } elsif ($item eq 'uploadquota') {
 8199:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8200:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
 8201:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
 8202:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
 8203:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
 8204:                                        '</ul>'.
 8205:                                        '</li>';
 8206:                     } else {
 8207:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
 8208:                     }
 8209:                 } elsif ($item eq 'coursecredits') {
 8210:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 8211:                         if (($domdefaults{'officialcredits'} eq '') &&
 8212:                             ($domdefaults{'unofficialcredits'} eq '')) {
 8213:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 8214:                         } else {
 8215:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
 8216:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
 8217:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
 8218:                                            '</ul>'.
 8219:                                            '</li>';
 8220:                         }
 8221:                     } else {
 8222:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 8223:                     }
 8224:                 }
 8225:             }
 8226:             $resulttext .= '</ul>';
 8227:         } else {
 8228:             $resulttext = &mt('No changes made to course defaults');
 8229:         }
 8230:     } else {
 8231:         $resulttext = '<span class="LC_error">'.
 8232:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8233:     }
 8234:     return $resulttext;
 8235: }
 8236: 
 8237: sub modify_usersessions {
 8238:     my ($dom,$lastactref,%domconfig) = @_;
 8239:     my @hostingtypes = ('version','excludedomain','includedomain');
 8240:     my @offloadtypes = ('primary','default');
 8241:     my %types = (
 8242:                   remote => \@hostingtypes,
 8243:                   hosted => \@hostingtypes,
 8244:                   spares => \@offloadtypes,
 8245:                 );
 8246:     my @prefixes = ('remote','hosted','spares');
 8247:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 8248:     my (%by_ip,%by_location,@intdoms);
 8249:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 8250:     my @locations = sort(keys(%by_location));
 8251:     my (%defaultshash,%changes);
 8252:     foreach my $prefix (@prefixes) {
 8253:         $defaultshash{'usersessions'}{$prefix} = {};
 8254:     }
 8255:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8256:     my $resulttext;
 8257:     my %iphost = &Apache::lonnet::get_iphost();
 8258:     foreach my $prefix (@prefixes) {
 8259:         next if ($prefix eq 'spares');
 8260:         foreach my $type (@{$types{$prefix}}) {
 8261:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 8262:             if ($type eq 'version') {
 8263:                 my $value = $env{'form.'.$prefix.'_'.$type};
 8264:                 my $okvalue;
 8265:                 if ($value ne '') {
 8266:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 8267:                         $okvalue = $value;
 8268:                     }
 8269:                 }
 8270:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8271:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8272:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 8273:                             if ($inuse == 0) {
 8274:                                 $changes{$prefix}{$type} = 1;
 8275:                             } else {
 8276:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 8277:                                     $changes{$prefix}{$type} = 1;
 8278:                                 }
 8279:                                 if ($okvalue ne '') {
 8280:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8281:                                 } 
 8282:                             }
 8283:                         } else {
 8284:                             if (($inuse == 1) && ($okvalue ne '')) {
 8285:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8286:                                 $changes{$prefix}{$type} = 1;
 8287:                             }
 8288:                         }
 8289:                     } else {
 8290:                         if (($inuse == 1) && ($okvalue ne '')) {
 8291:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8292:                             $changes{$prefix}{$type} = 1;
 8293:                         }
 8294:                     }
 8295:                 } else {
 8296:                     if (($inuse == 1) && ($okvalue ne '')) {
 8297:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8298:                         $changes{$prefix}{$type} = 1;
 8299:                     }
 8300:                 }
 8301:             } else {
 8302:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 8303:                 my @okvals;
 8304:                 foreach my $val (@vals) {
 8305:                     if ($val =~ /:/) {
 8306:                         my @items = split(/:/,$val);
 8307:                         foreach my $item (@items) {
 8308:                             if (ref($by_location{$item}) eq 'ARRAY') {
 8309:                                 push(@okvals,$item);
 8310:                             }
 8311:                         }
 8312:                     } else {
 8313:                         if (ref($by_location{$val}) eq 'ARRAY') {
 8314:                             push(@okvals,$val);
 8315:                         }
 8316:                     }
 8317:                 }
 8318:                 @okvals = sort(@okvals);
 8319:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8320:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8321:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8322:                             if ($inuse == 0) {
 8323:                                 $changes{$prefix}{$type} = 1; 
 8324:                             } else {
 8325:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8326:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 8327:                                 if (@changed > 0) {
 8328:                                     $changes{$prefix}{$type} = 1;
 8329:                                 }
 8330:                             }
 8331:                         } else {
 8332:                             if ($inuse == 1) {
 8333:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8334:                                 $changes{$prefix}{$type} = 1;
 8335:                             }
 8336:                         } 
 8337:                     } else {
 8338:                         if ($inuse == 1) {
 8339:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8340:                             $changes{$prefix}{$type} = 1;
 8341:                         }
 8342:                     }
 8343:                 } else {
 8344:                     if ($inuse == 1) {
 8345:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8346:                         $changes{$prefix}{$type} = 1;
 8347:                     }
 8348:                 }
 8349:             }
 8350:         }
 8351:     }
 8352: 
 8353:     my @alldoms = &Apache::lonnet::all_domains();
 8354:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8355:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 8356:     my $savespares;
 8357: 
 8358:     foreach my $lonhost (sort(keys(%servers))) {
 8359:         my $serverhomeID =
 8360:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 8361:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 8362:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 8363:         my %spareschg;
 8364:         foreach my $type (@{$types{'spares'}}) {
 8365:             my @okspares;
 8366:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 8367:             foreach my $server (@checked) {
 8368:                 if (&Apache::lonnet::hostname($server) ne '') {
 8369:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 8370:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 8371:                             push(@okspares,$server);
 8372:                         }
 8373:                     }
 8374:                 }
 8375:             }
 8376:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 8377:             my $newspare;
 8378:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 8379:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 8380:                     $newspare = $new;
 8381:                 }
 8382:             }
 8383:             my @spares;
 8384:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 8385:                 @spares = sort(@okspares,$newspare);
 8386:             } else {
 8387:                 @spares = sort(@okspares);
 8388:             }
 8389:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 8390:             if (ref($spareid{$lonhost}) eq 'HASH') {
 8391:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 8392:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 8393:                     if (@diffs > 0) {
 8394:                         $spareschg{$type} = 1;
 8395:                     }
 8396:                 }
 8397:             }
 8398:         }
 8399:         if (keys(%spareschg) > 0) {
 8400:             $changes{'spares'}{$lonhost} = \%spareschg;
 8401:         }
 8402:     }
 8403: 
 8404:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8405:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 8406:             if (ref($changes{'spares'}) eq 'HASH') {
 8407:                 if (keys(%{$changes{'spares'}}) > 0) {
 8408:                     $savespares = 1;
 8409:                 }
 8410:             }
 8411:         } else {
 8412:             $savespares = 1;
 8413:         }
 8414:     }
 8415: 
 8416:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 8417:     if ((keys(%changes) > 0) || ($savespares)) {
 8418:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8419:                                                  $dom);
 8420:         if ($putresult eq 'ok') {
 8421:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8422:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 8423:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 8424:                 }
 8425:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 8426:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 8427:                 }
 8428:             }
 8429:             my $cachetime = 24*60*60;
 8430:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8431:             if (ref($lastactref) eq 'HASH') {
 8432:                 $lastactref->{'domdefaults'} = 1;
 8433:             }
 8434:             if (keys(%changes) > 0) {
 8435:                 my %lt = &usersession_titles();
 8436:                 $resulttext = &mt('Changes made:').'<ul>';
 8437:                 foreach my $prefix (@prefixes) {
 8438:                     if (ref($changes{$prefix}) eq 'HASH') {
 8439:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 8440:                         if ($prefix eq 'spares') {
 8441:                             if (ref($changes{$prefix}) eq 'HASH') {
 8442:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 8443:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 8444:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 8445:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
 8446:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
 8447:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 8448:                                         foreach my $type (@{$types{$prefix}}) {
 8449:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 8450:                                                 my $offloadto = &mt('None');
 8451:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 8452:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 8453:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 8454:                                                     }
 8455:                                                 }
 8456:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 8457:                                             }
 8458:                                         }
 8459:                                     }
 8460:                                     $resulttext .= '</li>';
 8461:                                 }
 8462:                             }
 8463:                         } else {
 8464:                             foreach my $type (@{$types{$prefix}}) {
 8465:                                 if (defined($changes{$prefix}{$type})) {
 8466:                                     my $newvalue;
 8467:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8468:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 8469:                                             if ($type eq 'version') {
 8470:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 8471:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8472:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 8473:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 8474:                                                 }
 8475:                                             }
 8476:                                         }
 8477:                                     }
 8478:                                     if ($newvalue eq '') {
 8479:                                         if ($type eq 'version') {
 8480:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8481:                                         } else {
 8482:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8483:                                         }
 8484:                                     } else {
 8485:                                         if ($type eq 'version') {
 8486:                                             $newvalue .= ' '.&mt('(or later)'); 
 8487:                                         }
 8488:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8489:                                     }
 8490:                                 }
 8491:                             }
 8492:                         }
 8493:                         $resulttext .= '</ul>';
 8494:                     }
 8495:                 }
 8496:                 $resulttext .= '</ul>';
 8497:             } else {
 8498:                 $resulttext = $nochgmsg;
 8499:             }
 8500:         } else {
 8501:             $resulttext = '<span class="LC_error">'.
 8502:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8503:         }
 8504:     } else {
 8505:         $resulttext = $nochgmsg;
 8506:     }
 8507:     return $resulttext;
 8508: }
 8509: 
 8510: sub modify_loadbalancing {
 8511:     my ($dom,%domconfig) = @_;
 8512:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8513:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8514:     my ($othertitle,$usertypes,$types) =
 8515:         &Apache::loncommon::sorted_inst_types($dom);
 8516:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8517:     my @sparestypes = ('primary','default');
 8518:     my %typetitles = &sparestype_titles();
 8519:     my $resulttext;
 8520:     my (%currbalancer,%currtargets,%currrules,%existing);
 8521:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8522:         %existing = %{$domconfig{'loadbalancing'}};
 8523:     }
 8524:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8525:                               \%currtargets,\%currrules);
 8526:     my ($saveloadbalancing,%defaultshash,%changes);
 8527:     my ($alltypes,$othertypes,$titles) =
 8528:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8529:     my %ruletitles = &offloadtype_text();
 8530:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 8531:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 8532:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 8533:         if ($balancer eq '') {
 8534:             next;
 8535:         }
 8536:         if (!exists($servers{$balancer})) {
 8537:             if (exists($currbalancer{$balancer})) {
 8538:                 push(@{$changes{'delete'}},$balancer);
 8539:             }
 8540:             next;
 8541:         }
 8542:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 8543:             push(@{$changes{'delete'}},$balancer);
 8544:             next;
 8545:         }
 8546:         if (!exists($currbalancer{$balancer})) {
 8547:             push(@{$changes{'add'}},$balancer);
 8548:         }
 8549:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 8550:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 8551:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 8552:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8553:             $saveloadbalancing = 1;
 8554:         }
 8555:         foreach my $sparetype (@sparestypes) {
 8556:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 8557:             my @offloadto;
 8558:             foreach my $target (@targets) {
 8559:                 if (($servers{$target}) && ($target ne $balancer)) {
 8560:                     if ($sparetype eq 'default') {
 8561:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 8562:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 8563:                         }
 8564:                     }
 8565:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 8566:                         push(@offloadto,$target);
 8567:                     }
 8568:                 }
 8569:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 8570:             }
 8571:         }
 8572:         if (ref($currtargets{$balancer}) eq 'HASH') {
 8573:             foreach my $sparetype (@sparestypes) {
 8574:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 8575:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 8576:                     if (@targetdiffs > 0) {
 8577:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8578:                     }
 8579:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8580:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8581:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8582:                     }
 8583:                 }
 8584:             }
 8585:         } else {
 8586:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 8587:                 foreach my $sparetype (@sparestypes) {
 8588:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8589:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8590:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 8591:                         }
 8592:                     }
 8593:                 }
 8594:             }
 8595:         }
 8596:         my $ishomedom;
 8597:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 8598:             $ishomedom = 1;
 8599:         }
 8600:         if (ref($alltypes) eq 'ARRAY') {
 8601:             foreach my $type (@{$alltypes}) {
 8602:                 my $rule;
 8603:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
 8604:                          (!$ishomedom)) {
 8605:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 8606:                 }
 8607:                 if ($rule eq 'specific') {
 8608:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 8609:                 }
 8610:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 8611:                 if (ref($currrules{$balancer}) eq 'HASH') {
 8612:                     if ($rule ne $currrules{$balancer}{$type}) {
 8613:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8614:                     }
 8615:                 } elsif ($rule ne '') {
 8616:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8617:                 }
 8618:             }
 8619:         }
 8620:     }
 8621:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8622:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8623:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 8624:             $defaultshash{'loadbalancing'} = {};
 8625:         }
 8626:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8627:                                                  \%defaultshash,$dom);
 8628:         if ($putresult eq 'ok') {
 8629:             if (keys(%changes) > 0) {
 8630:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 8631:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 8632:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 8633:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
 8634:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
 8635:                     }
 8636:                 }
 8637:                 if (ref($changes{'add'}) eq 'ARRAY') {
 8638:                     foreach my $balancer (sort(@{$changes{'add'}})) {
 8639:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 8640:                     }
 8641:                 }
 8642:                 if (ref($changes{'curr'}) eq 'HASH') {
 8643:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 8644:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 8645:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 8646:                                 my %offloadstr;
 8647:                                 foreach my $sparetype (@sparestypes) {
 8648:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8649:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8650:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 8651:                                         }
 8652:                                     }
 8653:                                 }
 8654:                                 if (keys(%offloadstr) == 0) {
 8655:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8656:                                 } else {
 8657:                                     my $showoffload;
 8658:                                     foreach my $sparetype (@sparestypes) {
 8659:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8660:                                         if (defined($offloadstr{$sparetype})) {
 8661:                                             $showoffload .= $offloadstr{$sparetype};
 8662:                                         } else {
 8663:                                             $showoffload .= &mt('None');
 8664:                                         }
 8665:                                         $showoffload .= ('&nbsp;'x3);
 8666:                                     }
 8667:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 8668:                                 }
 8669:                             }
 8670:                         }
 8671:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 8672:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8673:                                 foreach my $type (@{$alltypes}) {
 8674:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 8675:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 8676:                                         my $balancetext;
 8677:                                         if ($rule eq '') {
 8678:                                             $balancetext =  $ruletitles{'default'};
 8679:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
 8680:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
 8681:                                             $balancetext =  $ruletitles{$rule};
 8682:                                         } else {
 8683:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 8684:                                         }
 8685:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
 8686:                                     }
 8687:                                 }
 8688:                             }
 8689:                         }
 8690:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
 8691:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
 8692:                     }
 8693:                 }
 8694:                 if ($resulttext ne '') {
 8695:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8696:                 } else {
 8697:                     $resulttext = $nochgmsg;
 8698:                 }
 8699:             } else {
 8700:                 $resulttext = $nochgmsg;
 8701:             }
 8702:         } else {
 8703:             $resulttext = '<span class="LC_error">'.
 8704:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8705:         }
 8706:     } else {
 8707:         $resulttext = $nochgmsg;
 8708:     }
 8709:     return $resulttext;
 8710: }
 8711: 
 8712: sub recurse_check {
 8713:     my ($chkcats,$categories,$depth,$name) = @_;
 8714:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8715:         my $chg = 0;
 8716:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8717:             my $category = $chkcats->[$depth]{$name}[$j];
 8718:             my $item;
 8719:             if ($category eq '') {
 8720:                 $chg ++;
 8721:             } else {
 8722:                 my $deeper = $depth + 1;
 8723:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8724:                 if ($chg) {
 8725:                     $categories->{$item} -= $chg;
 8726:                 }
 8727:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8728:                 $deeper --;
 8729:             }
 8730:         }
 8731:     }
 8732:     return;
 8733: }
 8734: 
 8735: sub recurse_cat_deletes {
 8736:     my ($item,$coursecategories,$deletions) = @_;
 8737:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8738:     my $subdepth = $depth + 1;
 8739:     if (ref($coursecategories) eq 'HASH') {
 8740:         foreach my $subitem (keys(%{$coursecategories})) {
 8741:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8742:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8743:                 delete($coursecategories->{$subitem});
 8744:                 $deletions->{$subitem} = 1;
 8745:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8746:             }
 8747:         }
 8748:     }
 8749:     return;
 8750: }
 8751: 
 8752: sub get_active_dcs {
 8753:     my ($dom) = @_;
 8754:     my $now = time;
 8755:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 8756:     my %domcoords;
 8757:     my $numdcs = 0;
 8758:     foreach my $server (keys(%dompersonnel)) {
 8759:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8760:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8761:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8762:         }
 8763:     }
 8764:     return %domcoords;
 8765: }
 8766: 
 8767: sub active_dc_picker {
 8768:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
 8769:     my %domcoords = &get_active_dcs($dom); 
 8770:     my @domcoord = keys(%domcoords);
 8771:     if (keys(%currhash)) {
 8772:         foreach my $dc (keys(%currhash)) {
 8773:             unless (exists($domcoords{$dc})) {
 8774:                 push(@domcoord,$dc);
 8775:             }
 8776:         }
 8777:     }
 8778:     @domcoord = sort(@domcoord);
 8779:     my $numdcs = scalar(@domcoord);
 8780:     my $rows = 0;
 8781:     my $table;
 8782:     if ($numdcs > 1) {
 8783:         $table = '<table>';
 8784:         for (my $i=0; $i<@domcoord; $i++) {
 8785:             my $rem = $i%($numinrow);
 8786:             if ($rem == 0) {
 8787:                 if ($i > 0) {
 8788:                     $table .= '</tr>';
 8789:                 }
 8790:                 $table .= '<tr>';
 8791:                 $rows ++;
 8792:             }
 8793:             my $check = '';
 8794:             if ($inputtype eq 'radio') {
 8795:                 if (keys(%currhash) == 0) {
 8796:                     if (!$i) {
 8797:                         $check = ' checked="checked"';
 8798:                     }
 8799:                 } elsif (exists($currhash{$domcoord[$i]})) {
 8800:                     $check = ' checked="checked"';
 8801:                 }
 8802:             } else {
 8803:                 if (exists($currhash{$domcoord[$i]})) {
 8804:                     $check = ' checked="checked"';
 8805:                 }
 8806:             }
 8807:             if ($i == @domcoord - 1) {
 8808:                 my $colsleft = $numinrow - $rem;
 8809:                 if ($colsleft > 1) {
 8810:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
 8811:                 } else {
 8812:                     $table .= '<td class="LC_left_item">';
 8813:                 }
 8814:             } else {
 8815:                 $table .= '<td class="LC_left_item">';
 8816:             }
 8817:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
 8818:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
 8819:             $table .= '<span class="LC_nobreak"><label>'.
 8820:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
 8821:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
 8822:             if ($user ne $dcname.':'.$dcdom) {
 8823:                 $table .=  ' ('.$dcname.':'.$dcdom.')'.
 8824:                            '</label></span></td>';
 8825:             }
 8826:         }
 8827:         $table .= '</tr></table>';
 8828:     } elsif ($numdcs == 1) {
 8829:         if ($inputtype eq 'radio') {
 8830:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />';
 8831:         } else {
 8832:             my $check;
 8833:             if (exists($currhash{$domcoord[0]})) {
 8834:                 $check = ' checked="checked"';
 8835:             }
 8836:             $table .= '<input type="checkbox" name="'.$name.'" '.
 8837:                       'value="'.$domcoord[0].'"'.$check.' />';
 8838:             $rows ++;
 8839:         }
 8840:     }
 8841:     return ($numdcs,$table,$rows);
 8842: }
 8843: 
 8844: sub usersession_titles {
 8845:     return &Apache::lonlocal::texthash(
 8846:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8847:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8848:                spares => 'Servers offloaded to, when busy',
 8849:                version => 'LON-CAPA version requirement',
 8850:                excludedomain => 'Allow all, but exclude specific domains',
 8851:                includedomain => 'Deny all, but include specific domains',
 8852:                primary => 'Primary (checked first)',
 8853:                default => 'Default',
 8854:            );
 8855: }
 8856: 
 8857: sub id_for_thisdom {
 8858:     my (%servers) = @_;
 8859:     my %altids;
 8860:     foreach my $server (keys(%servers)) {
 8861:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8862:         if ($serverhome ne $server) {
 8863:             $altids{$serverhome} = $server;
 8864:         }
 8865:     }
 8866:     return %altids;
 8867: }
 8868: 
 8869: sub count_servers {
 8870:     my ($currbalancer,%servers) = @_;
 8871:     my (@spares,$numspares);
 8872:     foreach my $lonhost (sort(keys(%servers))) {
 8873:         next if ($currbalancer eq $lonhost);
 8874:         push(@spares,$lonhost);
 8875:     }
 8876:     if ($currbalancer) {
 8877:         $numspares = scalar(@spares);
 8878:     } else {
 8879:         $numspares = scalar(@spares) - 1;
 8880:     }
 8881:     return ($numspares,@spares);
 8882: }
 8883: 
 8884: sub lonbalance_targets_js {
 8885:     my ($dom,$types,$servers,$settings) = @_;
 8886:     my $select = &mt('Select');
 8887:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8888:     if (ref($servers) eq 'HASH') {
 8889:         $alltargets = join("','",sort(keys(%{$servers})));
 8890:         my @homedoms;
 8891:         foreach my $server (sort(keys(%{$servers}))) {
 8892:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8893:                 push(@homedoms,'1');
 8894:             } else {
 8895:                 push(@homedoms,'0');
 8896:             }
 8897:         }
 8898:         $allishome = join("','",@homedoms);
 8899:     }
 8900:     if (ref($types) eq 'ARRAY') {
 8901:         if (@{$types} > 0) {
 8902:             @alltypes = @{$types};
 8903:         }
 8904:     }
 8905:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8906:     $allinsttypes = join("','",@alltypes);
 8907:     my (%currbalancer,%currtargets,%currrules,%existing);
 8908:     if (ref($settings) eq 'HASH') {
 8909:         %existing = %{$settings};
 8910:     }
 8911:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 8912:                               \%currtargets,\%currrules);
 8913:     my $balancers = join("','",sort(keys(%currbalancer)));
 8914:     return <<"END";
 8915: 
 8916: <script type="text/javascript">
 8917: // <![CDATA[
 8918: 
 8919: currBalancers = new Array('$balancers');
 8920: 
 8921: function toggleTargets(balnum) {
 8922:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8923:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 8924:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 8925:     var prevbalancer = prevhostitem.value;
 8926:     var baltotal = document.getElementById('loadbalancing_total').value;
 8927:     prevhostitem.value = balancer;
 8928:     if (prevbalancer != '') {
 8929:         var prevIdx = currBalancers.indexOf(prevbalancer);
 8930:         if (prevIdx != -1) {
 8931:             currBalancers.splice(prevIdx,1);
 8932:         }
 8933:     }
 8934:     if (balancer == '') {
 8935:         hideSpares(balnum);
 8936:     } else {
 8937:         var currIdx = currBalancers.indexOf(balancer);
 8938:         if (currIdx == -1) {
 8939:             currBalancers.push(balancer);
 8940:         }
 8941:         var homedoms = new Array('$allishome');
 8942:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 8943:         showSpares(balancer,ishomedom,balnum);
 8944:     }
 8945:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 8946:     return;
 8947: }
 8948: 
 8949: function showSpares(balancer,ishomedom,balnum) {
 8950:     var alltargets = new Array('$alltargets');
 8951:     var insttypes = new Array('$allinsttypes');
 8952:     var offloadtypes = new Array('primary','default');
 8953: 
 8954:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 8955:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 8956:  
 8957:     for (var i=0; i<offloadtypes.length; i++) {
 8958:         var count = 0;
 8959:         for (var j=0; j<alltargets.length; j++) {
 8960:             if (alltargets[j] != balancer) {
 8961:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 8962:                 item.value = alltargets[j];
 8963:                 item.style.textAlign='left';
 8964:                 item.style.textFace='normal';
 8965:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8966:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 8967:                     item.disabled = '';
 8968:                 } else {
 8969:                     item.disabled = 'disabled';
 8970:                     item.checked = false;
 8971:                 }
 8972:                 count ++;
 8973:             }
 8974:         }
 8975:     }
 8976:     for (var k=0; k<insttypes.length; k++) {
 8977:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8978:             if (ishomedom == 1) {
 8979:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8980:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8981:             } else {
 8982:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8983:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8984:             }
 8985:         } else {
 8986:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8987:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8988:         }
 8989:         if ((insttypes[k] != '_LC_external') && 
 8990:             ((insttypes[k] != '_LC_internetdom') ||
 8991:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8992:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 8993:             item.options.length = 0;
 8994:             item.options[0] = new Option("","",true,true);
 8995:             var idx = 0;
 8996:             for (var m=0; m<alltargets.length; m++) {
 8997:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 8998:                     idx ++;
 8999:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 9000:                 }
 9001:             }
 9002:         }
 9003:     }
 9004:     return;
 9005: }
 9006: 
 9007: function hideSpares(balnum) {
 9008:     var alltargets = new Array('$alltargets');
 9009:     var insttypes = new Array('$allinsttypes');
 9010:     var offloadtypes = new Array('primary','default');
 9011: 
 9012:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 9013:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 9014: 
 9015:     var total = alltargets.length - 1;
 9016:     for (var i=0; i<offloadtypes; i++) {
 9017:         for (var j=0; j<total; j++) {
 9018:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 9019:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 9020:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 9021:         }
 9022:     }
 9023:     for (var k=0; k<insttypes.length; k++) {
 9024:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 9025:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 9026:         if (insttypes[k] != '_LC_external') {
 9027:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 9028:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 9029:         }
 9030:     }
 9031:     return;
 9032: }
 9033: 
 9034: function checkOffloads(item,balnum,type) {
 9035:     var alltargets = new Array('$alltargets');
 9036:     var offloadtypes = new Array('primary','default');
 9037:     if (item.checked) {
 9038:         var total = alltargets.length - 1;
 9039:         var other;
 9040:         if (type == offloadtypes[0]) {
 9041:             other = offloadtypes[1];
 9042:         } else {
 9043:             other = offloadtypes[0];
 9044:         }
 9045:         for (var i=0; i<total; i++) {
 9046:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 9047:             if (server == item.value) {
 9048:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 9049:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 9050:                 }
 9051:             }
 9052:         }
 9053:     }
 9054:     return;
 9055: }
 9056: 
 9057: function singleServerToggle(balnum,type) {
 9058:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 9059:     if (offloadtoSelIdx == 0) {
 9060:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 9061:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9062: 
 9063:     } else {
 9064:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 9065:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 9066:     }
 9067:     return;
 9068: }
 9069: 
 9070: function balanceruleChange(formname,balnum,type) {
 9071:     if (type == '_LC_external') {
 9072:         return;
 9073:     }
 9074:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 9075:     for (var i=0; i<typesRules.length; i++) {
 9076:         if (formname.elements[typesRules[i]].checked) {
 9077:             if (formname.elements[typesRules[i]].value != 'specific') {
 9078:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 9079:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9080:             } else {
 9081:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 9082:             }
 9083:         }
 9084:     }
 9085:     return;
 9086: }
 9087: 
 9088: function balancerDeleteChange(balnum) {
 9089:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 9090:     var baltotal = document.getElementById('loadbalancing_total').value;
 9091:     var addtarget;
 9092:     var removetarget;
 9093:     var action = 'delete';
 9094:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 9095:         var lonhost = hostitem.value;
 9096:         var currIdx = currBalancers.indexOf(lonhost);
 9097:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 9098:             if (currIdx != -1) {
 9099:                 currBalancers.splice(currIdx,1);
 9100:             }
 9101:             addtarget = lonhost;
 9102:         } else {
 9103:             if (currIdx == -1) {
 9104:                 currBalancers.push(lonhost);
 9105:             }
 9106:             removetarget = lonhost;
 9107:             action = 'undelete';
 9108:         }
 9109:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 9110:     }
 9111:     return;
 9112: }
 9113: 
 9114: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 9115:     if (baltotal > 1) {
 9116:         var offloadtypes = new Array('primary','default');
 9117:         var alltargets = new Array('$alltargets');
 9118:         var insttypes = new Array('$allinsttypes');
 9119:         for (var i=0; i<baltotal; i++) {
 9120:             if (i != balnum) {
 9121:                 for (var j=0; j<offloadtypes.length; j++) {
 9122:                     var total = alltargets.length - 1;
 9123:                     for (var k=0; k<total; k++) {
 9124:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 9125:                         var server = serveritem.value;
 9126:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 9127:                             if (server == addtarget) {
 9128:                                 serveritem.disabled = '';
 9129:                             }
 9130:                         }
 9131:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 9132:                             if (server == removetarget) {
 9133:                                 serveritem.disabled = 'disabled';
 9134:                                 serveritem.checked = false;
 9135:                             }
 9136:                         }
 9137:                     }
 9138:                 }
 9139:                 for (var j=0; j<insttypes.length; j++) {
 9140:                     if (insttypes[j] != '_LC_external') {
 9141:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 9142:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 9143:                             var currSel = singleserver.selectedIndex;
 9144:                             var currVal = singleserver.options[currSel].value;
 9145:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 9146:                                 var numoptions = singleserver.options.length;
 9147:                                 var needsnew = 1;
 9148:                                 for (var k=0; k<numoptions; k++) {
 9149:                                     if (singleserver.options[k] == addtarget) {
 9150:                                         needsnew = 0;
 9151:                                         break;
 9152:                                     }
 9153:                                 }
 9154:                                 if (needsnew == 1) {
 9155:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 9156:                                 }
 9157:                             }
 9158:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 9159:                                 singleserver.options.length = 0;
 9160:                                 if ((currVal) && (currVal != removetarget)) {
 9161:                                     singleserver.options[0] = new Option("","",false,false);
 9162:                                 } else {
 9163:                                     singleserver.options[0] = new Option("","",true,true);
 9164:                                 }
 9165:                                 var idx = 0;
 9166:                                 for (var m=0; m<alltargets.length; m++) {
 9167:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
 9168:                                         idx ++;
 9169:                                         if (currVal == alltargets[m]) {
 9170:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
 9171:                                         } else {
 9172:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 9173:                                         }
 9174:                                     }
 9175:                                 }
 9176:                             }
 9177:                         }
 9178:                     }
 9179:                 }
 9180:             }
 9181:         }
 9182:     }
 9183:     return;
 9184: }
 9185: 
 9186: // ]]>
 9187: </script>
 9188: 
 9189: END
 9190: }
 9191: 
 9192: sub new_spares_js {
 9193:     my @sparestypes = ('primary','default');
 9194:     my $types = join("','",@sparestypes);
 9195:     my $select = &mt('Select');
 9196:     return <<"END";
 9197: 
 9198: <script type="text/javascript">
 9199: // <![CDATA[
 9200: 
 9201: function updateNewSpares(formname,lonhost) {
 9202:     var types = new Array('$types');
 9203:     var include = new Array();
 9204:     var exclude = new Array();
 9205:     for (var i=0; i<types.length; i++) {
 9206:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 9207:         for (var j=0; j<spareboxes.length; j++) {
 9208:             if (formname.elements[spareboxes[j]].checked) {
 9209:                 exclude.push(formname.elements[spareboxes[j]].value);
 9210:             } else {
 9211:                 include.push(formname.elements[spareboxes[j]].value);
 9212:             }
 9213:         }
 9214:     }
 9215:     for (var i=0; i<types.length; i++) {
 9216:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 9217:         var selIdx = newSpare.selectedIndex;
 9218:         var currnew = newSpare.options[selIdx].value;
 9219:         var okSpares = new Array();
 9220:         for (var j=0; j<newSpare.options.length; j++) {
 9221:             var possible = newSpare.options[j].value;
 9222:             if (possible != '') {
 9223:                 if (exclude.indexOf(possible) == -1) {
 9224:                     okSpares.push(possible);
 9225:                 } else {
 9226:                     if (currnew == possible) {
 9227:                         selIdx = 0;
 9228:                     }
 9229:                 }
 9230:             }
 9231:         }
 9232:         for (var k=0; k<include.length; k++) {
 9233:             if (okSpares.indexOf(include[k]) == -1) {
 9234:                 okSpares.push(include[k]);
 9235:             }
 9236:         }
 9237:         okSpares.sort();
 9238:         newSpare.options.length = 0;
 9239:         if (selIdx == 0) {
 9240:             newSpare.options[0] = new Option("$select","",true,true);
 9241:         } else {
 9242:             newSpare.options[0] = new Option("$select","",false,false);
 9243:         }
 9244:         for (var m=0; m<okSpares.length; m++) {
 9245:             var idx = m+1;
 9246:             var selThis = 0;
 9247:             if (selIdx != 0) {
 9248:                 if (okSpares[m] == currnew) {
 9249:                     selThis = 1;
 9250:                 }
 9251:             }
 9252:             if (selThis == 1) {
 9253:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 9254:             } else {
 9255:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 9256:             }
 9257:         }
 9258:     }
 9259:     return;
 9260: }
 9261: 
 9262: function checkNewSpares(lonhost,type) {
 9263:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 9264:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 9265:     if (chosen != '') { 
 9266:         var othertype;
 9267:         var othernewSpare;
 9268:         if (type == 'primary') {
 9269:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 9270:         }
 9271:         if (type == 'default') {
 9272:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 9273:         }
 9274:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 9275:             othernewSpare.selectedIndex = 0;
 9276:         }
 9277:     }
 9278:     return;
 9279: }
 9280: 
 9281: // ]]>
 9282: </script>
 9283: 
 9284: END
 9285: 
 9286: }
 9287: 
 9288: sub common_domprefs_js {
 9289:     return <<"END";
 9290: 
 9291: <script type="text/javascript">
 9292: // <![CDATA[
 9293: 
 9294: function getIndicesByName(formname,item) {
 9295:     var group = new Array();
 9296:     for (var i=0;i<formname.elements.length;i++) {
 9297:         if (formname.elements[i].name == item) {
 9298:             group.push(formname.elements[i].id);
 9299:         }
 9300:     }
 9301:     return group;
 9302: }
 9303: 
 9304: // ]]>
 9305: </script>
 9306: 
 9307: END
 9308: 
 9309: }
 9310: 
 9311: sub recaptcha_js {
 9312:     my %lt = &captcha_phrases();
 9313:     return <<"END";
 9314: 
 9315: <script type="text/javascript">
 9316: // <![CDATA[
 9317: 
 9318: function updateCaptcha(caller,context) {
 9319:     var privitem;
 9320:     var pubitem;
 9321:     var privtext;
 9322:     var pubtext;
 9323:     if (document.getElementById(context+'_recaptchapub')) {
 9324:         pubitem = document.getElementById(context+'_recaptchapub');
 9325:     } else {
 9326:         return;
 9327:     }
 9328:     if (document.getElementById(context+'_recaptchapriv')) {
 9329:         privitem = document.getElementById(context+'_recaptchapriv');
 9330:     } else {
 9331:         return;
 9332:     }
 9333:     if (document.getElementById(context+'_recaptchapubtxt')) {
 9334:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 9335:     } else {
 9336:         return;
 9337:     }
 9338:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 9339:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 9340:     } else {
 9341:         return;
 9342:     }
 9343:     if (caller.checked) {
 9344:         if (caller.value == 'recaptcha') {
 9345:             pubitem.type = 'text';
 9346:             privitem.type = 'text';
 9347:             pubitem.size = '40';
 9348:             privitem.size = '40';
 9349:             pubtext.innerHTML = "$lt{'pub'}";
 9350:             privtext.innerHTML = "$lt{'priv'}";
 9351:         } else {
 9352:             pubitem.type = 'hidden';
 9353:             privitem.type = 'hidden';
 9354:             pubtext.innerHTML = '';
 9355:             privtext.innerHTML = '';
 9356:         }
 9357:     }
 9358:     return;
 9359: }
 9360: 
 9361: // ]]>
 9362: </script>
 9363: 
 9364: END
 9365: 
 9366: }
 9367: 
 9368: sub credits_js {
 9369:     return <<"END";
 9370: 
 9371: <script type="text/javascript">
 9372: // <![CDATA[
 9373: 
 9374: function toggleCredits(domForm) {
 9375:     if (document.getElementById('credits')) {
 9376:         creditsitem = document.getElementById('credits');
 9377:         var creditsLength = domForm.coursecredits.length;
 9378:         if (creditsLength) {
 9379:             var currval;
 9380:             for (var i=0; i<creditsLength; i++) {
 9381:                 if (domForm.coursecredits[i].checked) {
 9382:                    currval = domForm.coursecredits[i].value;
 9383:                 }
 9384:             }
 9385:             if (currval == 1) {
 9386:                 creditsitem.style.display = 'block';
 9387:             } else {
 9388:                 creditsitem.style.display = 'none';
 9389:             }
 9390:         }
 9391:     }
 9392:     return;
 9393: }
 9394: 
 9395: // ]]>
 9396: </script>
 9397: 
 9398: END
 9399: 
 9400: }
 9401: 
 9402: sub captcha_phrases {
 9403:     return &Apache::lonlocal::texthash (
 9404:                  priv => 'Private key',
 9405:                  pub  => 'Public key',
 9406:                  original  => 'original (CAPTCHA)',
 9407:                  recaptcha => 'successor (ReCAPTCHA)',
 9408:                  notused   => 'unused',
 9409:     );
 9410: }
 9411: 
 9412: sub devalidate_remote_domconfs {
 9413:     my ($dom,$cachekeys) = @_;
 9414:     return unless (ref($cachekeys) eq 'HASH');
 9415:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9416:     my %thismachine;
 9417:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
 9418:     my @posscached = ('domainconfig','domdefaults');
 9419:     if (keys(%servers) > 1) {
 9420:         foreach my $server (keys(%servers)) {
 9421:             next if ($thismachine{$server});
 9422:             my @cached;
 9423:             foreach my $name (@posscached) {
 9424:                 if ($cachekeys->{$name}) {
 9425:                     push(@cached,&escape($name).':'.&escape($dom));
 9426:                 }
 9427:             }
 9428:             if (@cached) {
 9429:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
 9430:             }
 9431:         }
 9432:     }
 9433:     return;
 9434: }
 9435: 
 9436: 1;

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