File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.56: download - view: text, annotated - select for diffs
Wed Mar 11 15:56:36 2015 UTC (9 years, 3 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
 Backport 1.256 and one additional change from 1.254, missed in earlier
 backport (rev. 1.160.6.55).

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.56 2015/03/11 15:56:36 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, community, and textbook).  In each case the radio buttons 
  107: allow the 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 Apache::lonuserutils();
  168: use Apache::loncoursequeueadmin();
  169: use LONCAPA qw(:DEFAULT :match);
  170: use LONCAPA::Enrollment;
  171: use LONCAPA::lonauthcgi();
  172: use File::Copy;
  173: use Locale::Language;
  174: use DateTime::TimeZone;
  175: use DateTime::Locale;
  176: 
  177: my $registered_cleanup;
  178: my $modified_urls;
  179: 
  180: sub handler {
  181:     my $r=shift;
  182:     if ($r->header_only) {
  183:         &Apache::loncommon::content_type($r,'text/html');
  184:         $r->send_http_header;
  185:         return OK;
  186:     }
  187: 
  188:     my $context = 'domain';
  189:     my $dom = $env{'request.role.domain'};
  190:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  191:     if (&Apache::lonnet::allowed('mau',$dom)) {
  192:         &Apache::loncommon::content_type($r,'text/html');
  193:         $r->send_http_header;
  194:     } else {
  195:         $env{'user.error.msg'}=
  196:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  197:         return HTTP_NOT_ACCEPTABLE;
  198:     }
  199: 
  200:     $registered_cleanup=0;
  201:     @{$modified_urls}=();
  202: 
  203:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  204:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  205:                                             ['phase','actions']);
  206:     my $phase = 'pickactions';
  207:     if ( exists($env{'form.phase'}) ) {
  208:         $phase = $env{'form.phase'};
  209:     }
  210:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  211:     my %domconfig =
  212:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  213:                 'quotas','autoenroll','autoupdate','autocreate',
  214:                 'directorysrch','usercreation','usermodification',
  215:                 'contacts','defaults','scantron','coursecategories',
  216:                 'serverstatuses','requestcourses','coursedefaults',
  217:                 'usersessions','loadbalancing','requestauthor',
  218:                 'selfenrollment','inststatus'],$dom);
  219:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  220:                        'autoupdate','autocreate','directorysrch','contacts',
  221:                        'usercreation','selfcreation','usermodification','scantron',
  222:                        'requestcourses','requestauthor','coursecategories',
  223:                        'serverstatuses','coursedefaults','selfenrollment',
  224:                        'usersessions');
  225:     my %existing;
  226:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  227:         %existing = %{$domconfig{'loadbalancing'}};
  228:     }
  229:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  230:         push(@prefs_order,'loadbalancing');
  231:     }
  232:     my %prefs = (
  233:         'rolecolors' =>
  234:                    { text => 'Default color schemes',
  235:                      help => 'Domain_Configuration_Color_Schemes',
  236:                      header => [{col1 => 'Student Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Coordinator Settings',
  239:                                  col2 => '',},
  240:                                 {col1 => 'Author Settings',
  241:                                  col2 => '',},
  242:                                 {col1 => 'Administrator Settings',
  243:                                  col2 => '',}],
  244:                       print => \&print_rolecolors,
  245:                       modify => \&modify_rolecolors,
  246:                     },
  247:         'login' =>
  248:                     { text => 'Log-in page options',
  249:                       help => 'Domain_Configuration_Login_Page',
  250:                       header => [{col1 => 'Log-in Page Items',
  251:                                   col2 => '',},
  252:                                  {col1 => 'Log-in Help',
  253:                                   col2 => 'Value'},
  254:                                  {col1 => 'Custom HTML in document head',
  255:                                   col2 => 'Value'}],
  256:                       print => \&print_login,
  257:                       modify => \&modify_login,
  258:                     },
  259:         'defaults' => 
  260:                     { text => 'Default authentication/language/timezone/portal/types',
  261:                       help => 'Domain_Configuration_LangTZAuth',
  262:                       header => [{col1 => 'Setting',
  263:                                   col2 => 'Value'},
  264:                                  {col1 => 'Institutional user types',
  265:                                   col2 => 'Assignable to e-mail usernames'}],
  266:                       print => \&print_defaults,
  267:                       modify => \&modify_defaults,
  268:                     },
  269:         'quotas' => 
  270:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  271:                       help => 'Domain_Configuration_Quotas',
  272:                       header => [{col1 => 'User affiliation',
  273:                                   col2 => 'Available tools',
  274:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  275:                       print => \&print_quotas,
  276:                       modify => \&modify_quotas,
  277:                     },
  278:         'autoenroll' =>
  279:                    { text => 'Auto-enrollment settings',
  280:                      help => 'Domain_Configuration_Auto_Enrollment',
  281:                      header => [{col1 => 'Configuration setting',
  282:                                  col2 => 'Value(s)'}],
  283:                      print => \&print_autoenroll,
  284:                      modify => \&modify_autoenroll,
  285:                    },
  286:         'autoupdate' => 
  287:                    { text => 'Auto-update settings',
  288:                      help => 'Domain_Configuration_Auto_Updates',
  289:                      header => [{col1 => 'Setting',
  290:                                  col2 => 'Value',},
  291:                                 {col1 => 'Setting',
  292:                                  col2 => 'Affiliation'},
  293:                                 {col1 => 'User population',
  294:                                  col2 => 'Updatable user data'}],
  295:                      print => \&print_autoupdate,
  296:                      modify => \&modify_autoupdate,
  297:                   },
  298:         'autocreate' => 
  299:                   { text => 'Auto-course creation settings',
  300:                      help => 'Domain_Configuration_Auto_Creation',
  301:                      header => [{col1 => 'Configuration Setting',
  302:                                  col2 => 'Value',}],
  303:                      print => \&print_autocreate,
  304:                      modify => \&modify_autocreate,
  305:                   },
  306:         'directorysrch' => 
  307:                   { text => 'Institutional directory searches',
  308:                     help => 'Domain_Configuration_InstDirectory_Search',
  309:                     header => [{col1 => 'Setting',
  310:                                 col2 => 'Value',}],
  311:                     print => \&print_directorysrch,
  312:                     modify => \&modify_directorysrch,
  313:                   },
  314:         'contacts' =>
  315:                   { text => 'Contact Information',
  316:                     help => 'Domain_Configuration_Contact_Info',
  317:                     header => [{col1 => 'Setting',
  318:                                 col2 => 'Value',}],
  319:                     print => \&print_contacts,
  320:                     modify => \&modify_contacts,
  321:                   },
  322:         'usercreation' => 
  323:                   { text => 'User creation',
  324:                     help => 'Domain_Configuration_User_Creation',
  325:                     header => [{col1 => 'Format rule type',
  326:                                 col2 => 'Format rules in force'},
  327:                                {col1 => 'User account creation',
  328:                                 col2 => 'Usernames which may be created',},
  329:                                {col1 => 'Context',
  330:                                 col2 => 'Assignable authentication types'}],
  331:                     print => \&print_usercreation,
  332:                     modify => \&modify_usercreation,
  333:                   },
  334:         'selfcreation' => 
  335:                   { text => 'Users self-creating accounts',
  336:                     help => 'Domain_Configuration_Self_Creation', 
  337:                     header => [{col1 => 'Self-creation with institutional username',
  338:                                 col2 => 'Enabled?'},
  339:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  340:                                 col2 => 'Information user can enter'},
  341:                                {col1 => 'Self-creation with e-mail as username',
  342:                                 col2 => 'Settings'}],
  343:                     print => \&print_selfcreation,
  344:                     modify => \&modify_selfcreation,
  345:                   },
  346:         'usermodification' =>
  347:                   { text => 'User modification',
  348:                     help => 'Domain_Configuration_User_Modification',
  349:                     header => [{col1 => 'Target user has role',
  350:                                 col2 => 'User information updatable in author context'},
  351:                                {col1 => 'Target user has role',
  352:                                 col2 => 'User information updatable in course context'}],
  353:                     print => \&print_usermodification,
  354:                     modify => \&modify_usermodification,
  355:                   },
  356:         'scantron' =>
  357:                   { text => 'Bubblesheet format file',
  358:                     help => 'Domain_Configuration_Scantron_Format',
  359:                     header => [ {col1 => 'Item',
  360:                                  col2 => '',
  361:                               }],
  362:                     print => \&print_scantron,
  363:                     modify => \&modify_scantron,
  364:                   },
  365:         'requestcourses' => 
  366:                  {text => 'Request creation of courses',
  367:                   help => 'Domain_Configuration_Request_Courses',
  368:                   header => [{col1 => 'User affiliation',
  369:                               col2 => 'Availability/Processing of requests',},
  370:                              {col1 => 'Setting',
  371:                               col2 => 'Value'},
  372:                              {col1 => 'Available textbooks',
  373:                               col2 => ''},
  374:                              {col1 => 'Available templates',
  375:                               col2 => ''},
  376:                              {col1 => 'Validation (not official courses)',
  377:                               col2 => 'Value'},],
  378:                   print => \&print_quotas,
  379:                   modify => \&modify_quotas,
  380:                  },
  381:         'requestauthor' =>
  382:                  {text => 'Request Authoring Space',
  383:                   help => 'Domain_Configuration_Request_Author',
  384:                   header => [{col1 => 'User affiliation',
  385:                               col2 => 'Availability/Processing of requests',},
  386:                              {col1 => 'Setting',
  387:                               col2 => 'Value'}],
  388:                   print => \&print_quotas,
  389:                   modify => \&modify_quotas,
  390:                  },
  391:         'coursecategories' =>
  392:                   { text => 'Cataloging of courses/communities',
  393:                     help => 'Domain_Configuration_Cataloging_Courses',
  394:                     header => [{col1 => 'Catalog type/availability',
  395:                                 col2 => '',},
  396:                                {col1 => 'Category settings for standard catalog',
  397:                                 col2 => '',},
  398:                                {col1 => 'Categories',
  399:                                 col2 => '',
  400:                                }],
  401:                     print => \&print_coursecategories,
  402:                     modify => \&modify_coursecategories,
  403:                   },
  404:         'serverstatuses' =>
  405:                  {text   => 'Access to server status pages',
  406:                   help   => 'Domain_Configuration_Server_Status',
  407:                   header => [{col1 => 'Status Page',
  408:                               col2 => 'Other named users',
  409:                               col3 => 'Specific IPs',
  410:                             }],
  411:                   print => \&print_serverstatuses,
  412:                   modify => \&modify_serverstatuses,
  413:                  },
  414:         'coursedefaults' => 
  415:                  {text => 'Course/Community defaults',
  416:                   help => 'Domain_Configuration_Course_Defaults',
  417:                   header => [{col1 => 'Defaults which can be overridden for each course by a DC',
  418:                               col2 => 'Value',},],
  419:                   print => \&print_coursedefaults,
  420:                   modify => \&modify_coursedefaults,
  421:                  },
  422:         'selfenrollment' => 
  423:                  {text   => 'Self-enrollment in Course/Community',
  424:                   help   => 'Domain_Configuration_Selfenrollment',
  425:                   header => [{col1 => 'Configuration Rights',
  426:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  427:                              {col1 => 'Defaults',
  428:                               col2 => 'Value'},
  429:                              {col1 => 'Self-enrollment validation (optional)',
  430:                               col2 => 'Value'},],
  431:                   print => \&print_selfenrollment,
  432:                   modify => \&modify_selfenrollment,
  433:                  },
  434:         'usersessions' =>
  435:                  {text  => 'User session hosting/offloading',
  436:                   help  => 'Domain_Configuration_User_Sessions',
  437:                   header => [{col1 => 'Domain server',
  438:                               col2 => 'Servers to offload sessions to when busy'},
  439:                              {col1 => 'Hosting of users from other domains',
  440:                               col2 => 'Rules'},
  441:                              {col1 => "Hosting domain's own users elsewhere",
  442:                               col2 => 'Rules'}],
  443:                   print => \&print_usersessions,
  444:                   modify => \&modify_usersessions,
  445:                  },
  446:          'loadbalancing' =>
  447:                  {text  => 'Dedicated Load Balancer(s)',
  448:                   help  => 'Domain_Configuration_Load_Balancing',
  449:                   header => [{col1 => 'Balancers',
  450:                               col2 => 'Default destinations',
  451:                               col3 => 'User affiliation',
  452:                               col4 => 'Overrides'},
  453:                             ],
  454:                   print => \&print_loadbalancing,
  455:                   modify => \&modify_loadbalancing,
  456:                  },
  457:     );
  458:     if (keys(%servers) > 1) {
  459:         $prefs{'login'}  = { text   => 'Log-in page options',
  460:                              help   => 'Domain_Configuration_Login_Page',
  461:                             header => [{col1 => 'Log-in Service',
  462:                                         col2 => 'Server Setting',},
  463:                                        {col1 => 'Log-in Page Items',
  464:                                         col2 => ''},
  465:                                        {col1 => 'Log-in Help',
  466:                                         col2 => 'Value'},
  467:                                        {col1 => 'Custom HTML in document head',
  468:                                         col2 => 'Value'}],
  469:                             print => \&print_login,
  470:                             modify => \&modify_login,
  471:                            };
  472:     }
  473: 
  474:     my @roles = ('student','coordinator','author','admin');
  475:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  476:     &Apache::lonhtmlcommon::add_breadcrumb
  477:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  478:       text=>"Settings to display/modify"});
  479:     my $confname = $dom.'-domainconfig';
  480: 
  481:     if ($phase eq 'process') {
  482:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  483:                                                               \%prefs,\%domconfig,$confname,\@roles);
  484:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  485:             $r->rflush();
  486:             &devalidate_remote_domconfs($dom,$result);
  487:         }
  488:     } elsif ($phase eq 'display') {
  489:         my $js = &recaptcha_js().
  490:                  &toggle_display_js();
  491:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  492:             my ($othertitle,$usertypes,$types) =
  493:                 &Apache::loncommon::sorted_inst_types($dom);
  494:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  495:                                           $domconfig{'loadbalancing'}).
  496:                    &new_spares_js().
  497:                    &common_domprefs_js().
  498:                    &Apache::loncommon::javascript_array_indexof();
  499:         }
  500:         if (grep(/^requestcourses$/,@actions)) {
  501:             my $javascript_validations;
  502:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  503:             $js .= <<END;
  504: <script type="text/javascript">
  505: $javascript_validations
  506: </script>
  507: $coursebrowserjs
  508: END
  509:         }
  510:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  511:     } else {
  512: # check if domconfig user exists for the domain.
  513:         my $servadm = $r->dir_config('lonAdmEMail');
  514:         my ($configuserok,$author_ok,$switchserver) =
  515:             &config_check($dom,$confname,$servadm);
  516:         unless ($configuserok eq 'ok') {
  517:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  518:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  519:                           $confname).
  520:                       '<br />'
  521:             );
  522:             if ($switchserver) {
  523:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  524:                           '<br />'.
  525:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  526:                           '<br />'.
  527:                           &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).
  528:                           '<br />'.
  529:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  530:                 );
  531:             } else {
  532:                 $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.').
  533:                           '<br />'.
  534:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  535:                 );
  536:             }
  537:             $r->print(&Apache::loncommon::end_page());
  538:             return OK;
  539:         }
  540:         if (keys(%domconfig) == 0) {
  541:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  542:             my @ids=&Apache::lonnet::current_machine_ids();
  543:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  544:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  545:                 my @loginimages = ('img','logo','domlogo','login');
  546:                 my $custom_img_count = 0;
  547:                 foreach my $img (@loginimages) {
  548:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  549:                         $custom_img_count ++;
  550:                     }
  551:                 }
  552:                 foreach my $role (@roles) {
  553:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  554:                         $custom_img_count ++;
  555:                     }
  556:                 }
  557:                 if ($custom_img_count > 0) {
  558:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  559:                     my $switch_server = &check_switchserver($dom,$confname);
  560:                     $r->print(
  561:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  562:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  563:     &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 />'.
  564:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  565:                     if ($switch_server) {
  566:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  567:                     }
  568:                     $r->print(&Apache::loncommon::end_page());
  569:                     return OK;
  570:                 }
  571:             }
  572:         }
  573:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  574:     }
  575:     return OK;
  576: }
  577: 
  578: sub process_changes {
  579:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  580:     my %domconfig;
  581:     if (ref($values) eq 'HASH') {
  582:         %domconfig = %{$values};
  583:     }
  584:     my $output;
  585:     if ($action eq 'login') {
  586:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  587:     } elsif ($action eq 'rolecolors') {
  588:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  589:                                      $lastactref,%domconfig);
  590:     } elsif ($action eq 'quotas') {
  591:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  592:     } elsif ($action eq 'autoenroll') {
  593:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  594:     } elsif ($action eq 'autoupdate') {
  595:         $output = &modify_autoupdate($dom,%domconfig);
  596:     } elsif ($action eq 'autocreate') {
  597:         $output = &modify_autocreate($dom,%domconfig);
  598:     } elsif ($action eq 'directorysrch') {
  599:         $output = &modify_directorysrch($dom,%domconfig);
  600:     } elsif ($action eq 'usercreation') {
  601:         $output = &modify_usercreation($dom,%domconfig);
  602:     } elsif ($action eq 'selfcreation') {
  603:         $output = &modify_selfcreation($dom,%domconfig);
  604:     } elsif ($action eq 'usermodification') {
  605:         $output = &modify_usermodification($dom,%domconfig);
  606:     } elsif ($action eq 'contacts') {
  607:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  608:     } elsif ($action eq 'defaults') {
  609:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  610:     } elsif ($action eq 'scantron') {
  611:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  612:     } elsif ($action eq 'coursecategories') {
  613:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  614:     } elsif ($action eq 'serverstatuses') {
  615:         $output = &modify_serverstatuses($dom,%domconfig);
  616:     } elsif ($action eq 'requestcourses') {
  617:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  618:     } elsif ($action eq 'requestauthor') {
  619:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  620:     } elsif ($action eq 'coursedefaults') {
  621:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  622:     } elsif ($action eq 'selfenrollment') {
  623:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  624:     } elsif ($action eq 'usersessions') {
  625:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  626:     } elsif ($action eq 'loadbalancing') {
  627:         $output = &modify_loadbalancing($dom,%domconfig);
  628:     }
  629:     return $output;
  630: }
  631: 
  632: sub print_config_box {
  633:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  634:     my $rowtotal = 0;
  635:     my $output;
  636:     if ($action eq 'coursecategories') {
  637:         $output = &coursecategories_javascript($settings);
  638:     } elsif ($action eq 'defaults') {
  639:         $output = &defaults_javascript($settings); 
  640:     }
  641:     $output .=
  642:          '<table class="LC_nested_outer">
  643:           <tr>
  644:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  645:            &mt($item->{text}).'&nbsp;'.
  646:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  647:           '</tr>';
  648:     $rowtotal ++;
  649:     my $numheaders = 1;
  650:     if (ref($item->{'header'}) eq 'ARRAY') {
  651:         $numheaders = scalar(@{$item->{'header'}});
  652:     }
  653:     if ($numheaders > 1) {
  654:         my $colspan = '';
  655:         my $rightcolspan = '';
  656:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  657:             (($action eq 'login') && ($numheaders < 4))) {
  658:             $colspan = ' colspan="2"';
  659:         }
  660:         if ($action eq 'usersessions') {
  661:             $rightcolspan = ' colspan="3"'; 
  662:         }
  663:         $output .= '
  664:           <tr>
  665:            <td>
  666:             <table class="LC_nested">
  667:              <tr class="LC_info_row">
  668:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  669:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  670:              </tr>';
  671:         $rowtotal ++;
  672:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  673:             ($action eq 'usermodification') || ($action eq 'defaults') ||
  674:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
  675:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  676:         } elsif ($action eq 'coursecategories') {
  677:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  678:         } elsif ($action eq 'login') {
  679:             if ($numheaders == 4) {
  680:                 $colspan = ' colspan="2"';
  681:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  682:             } else {
  683:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  684:             }
  685:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  686:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  687:         } elsif ($action eq 'rolecolors') {
  688:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  689:         }
  690:         $output .= '
  691:            </table>
  692:           </td>
  693:          </tr>
  694:          <tr>
  695:            <td>
  696:             <table class="LC_nested">
  697:              <tr class="LC_info_row">
  698:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  699:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  700:              </tr>';
  701:             $rowtotal ++;
  702:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  703:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  704:             ($action eq 'usersessions') || ($action eq 'coursecategories')) {
  705:             if ($action eq 'coursecategories') {
  706:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  707:                 $colspan = ' colspan="2"';
  708:             } else {
  709:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  710:             }
  711:             $output .= '
  712:            </table>
  713:           </td>
  714:          </tr>
  715:          <tr>
  716:            <td>
  717:             <table class="LC_nested">
  718:              <tr class="LC_info_row">
  719:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  720:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  721:              </tr>'."\n";
  722:             if ($action eq 'coursecategories') {
  723:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  724:             } else {
  725:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  726:             }
  727:             $rowtotal ++;
  728:         } elsif (($action eq 'usermodification') || ($action eq 'defaults')) {
  729:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  730:         } elsif ($action eq 'login') {
  731:             if ($numheaders == 4) {
  732:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  733:            </table>
  734:           </td>
  735:          </tr>
  736:          <tr>
  737:            <td>
  738:             <table class="LC_nested">
  739:              <tr class="LC_info_row">
  740:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  741:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  742:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  743:                 $rowtotal ++;
  744:             } else {
  745:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  746:             }
  747:             $output .= '
  748:            </table>
  749:           </td>
  750:          </tr>
  751:          <tr>
  752:            <td>
  753:             <table class="LC_nested">
  754:              <tr class="LC_info_row">';
  755:             if ($numheaders == 4) {
  756:                 $output .= '
  757:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  758:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  759:              </tr>';
  760:             } else {
  761:                 $output .= '
  762:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  763:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  764:              </tr>';
  765:             }
  766:             $rowtotal ++;
  767:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  768:         } elsif ($action eq 'requestcourses') {
  769:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  770:             $rowtotal ++;
  771:             $output .= &print_studentcode($settings,\$rowtotal).'
  772:            </table>
  773:           </td>
  774:          </tr>
  775:          <tr>
  776:            <td>
  777:             <table class="LC_nested">
  778:              <tr class="LC_info_row">
  779:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  780:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  781:                        &textbookcourses_javascript($settings).
  782:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  783:             </table>
  784:            </td>
  785:           </tr>
  786:          <tr>
  787:            <td>
  788:             <table class="LC_nested">
  789:              <tr class="LC_info_row">
  790:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  791:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  792:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  793:             </table>
  794:            </td>
  795:           </tr>
  796:           <tr>
  797:            <td>
  798:             <table class="LC_nested">
  799:              <tr class="LC_info_row">
  800:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  801:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  802:              </tr>'.
  803:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  804:         } elsif ($action eq 'requestauthor') {
  805:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  806:             $rowtotal ++;
  807:         } elsif ($action eq 'rolecolors') {
  808:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  809:            </table>
  810:           </td>
  811:          </tr>
  812:          <tr>
  813:            <td>
  814:             <table class="LC_nested">
  815:              <tr class="LC_info_row">
  816:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  817:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  818:               <td class="LC_right_item" valign="top">'.
  819:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  820:              </tr>'.
  821:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  822:            </table>
  823:           </td>
  824:          </tr>
  825:          <tr>
  826:            <td>
  827:             <table class="LC_nested">
  828:              <tr class="LC_info_row">
  829:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  830:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  831:              </tr>'.
  832:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  833:             $rowtotal += 2;
  834:         }
  835:     } else {
  836:         $output .= '
  837:           <tr>
  838:            <td>
  839:             <table class="LC_nested">
  840:              <tr class="LC_info_row">';
  841:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  842:             $output .= '  
  843:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  844:         } elsif ($action eq 'serverstatuses') {
  845:             $output .= '
  846:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  847:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  848: 
  849:         } else {
  850:             $output .= '
  851:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  852:         }
  853:         if (defined($item->{'header'}->[0]->{'col3'})) {
  854:             $output .= '<td class="LC_left_item" valign="top">'.
  855:                        &mt($item->{'header'}->[0]->{'col2'});
  856:             if ($action eq 'serverstatuses') {
  857:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  858:             } 
  859:         } else {
  860:             $output .= '<td class="LC_right_item" valign="top">'.
  861:                        &mt($item->{'header'}->[0]->{'col2'});
  862:         }
  863:         $output .= '</td>';
  864:         if ($item->{'header'}->[0]->{'col3'}) {
  865:             if (defined($item->{'header'}->[0]->{'col4'})) {
  866:                 $output .= '<td class="LC_left_item" valign="top">'.
  867:                             &mt($item->{'header'}->[0]->{'col3'});
  868:             } else {
  869:                 $output .= '<td class="LC_right_item" valign="top">'.
  870:                            &mt($item->{'header'}->[0]->{'col3'});
  871:             }
  872:             if ($action eq 'serverstatuses') {
  873:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  874:             }
  875:             $output .= '</td>';
  876:         }
  877:         if ($item->{'header'}->[0]->{'col4'}) {
  878:             $output .= '<td class="LC_right_item" valign="top">'.
  879:                        &mt($item->{'header'}->[0]->{'col4'});
  880:         }
  881:         $output .= '</tr>';
  882:         $rowtotal ++;
  883:         if ($action eq 'quotas') {
  884:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  885:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  886:                  ($action eq 'contacts') || ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  887:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  888:         } elsif ($action eq 'scantron') {
  889:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  890:         } elsif ($action eq 'helpsettings') {
  891:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  892:         } elsif ($action eq 'coursedefaults') {
  893:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  894:         }
  895:     }
  896:     $output .= '
  897:    </table>
  898:   </td>
  899:  </tr>
  900: </table><br />';
  901:     return ($output,$rowtotal);
  902: }
  903: 
  904: sub print_login {
  905:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  906:     my ($css_class,$datatable);
  907:     my %choices = &login_choices();
  908: 
  909:     if ($caller eq 'service') {
  910:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  911:         my $choice = $choices{'disallowlogin'};
  912:         $css_class = ' class="LC_odd_row"';
  913:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  914:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  915:                       '<th>'.$choices{'server'}.'</th>'.
  916:                       '<th>'.$choices{'serverpath'}.'</th>'.
  917:                       '<th>'.$choices{'custompath'}.'</th>'.
  918:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  919:         my %disallowed;
  920:         if (ref($settings) eq 'HASH') {
  921:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  922:                %disallowed = %{$settings->{'loginvia'}};
  923:             }
  924:         }
  925:         foreach my $lonhost (sort(keys(%servers))) {
  926:             my $direct = 'selected="selected"';
  927:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  928:                 if ($disallowed{$lonhost}{'server'} ne '') {
  929:                     $direct = '';
  930:                 }
  931:             }
  932:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  933:                           '<td><select name="'.$lonhost.'_server">'.
  934:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  935:                           '</option>';
  936:             foreach my $hostid (sort(keys(%servers))) {
  937:                 next if ($servers{$hostid} eq $servers{$lonhost});
  938:                 my $selected = '';
  939:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  940:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  941:                         $selected = 'selected="selected"';
  942:                     }
  943:                 }
  944:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  945:                               $servers{$hostid}.'</option>';
  946:             }
  947:             $datatable .= '</select></td>'.
  948:                           '<td><select name="'.$lonhost.'_serverpath">';
  949:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  950:                 my $pathname = $path;
  951:                 if ($path eq 'custom') {
  952:                     $pathname = &mt('Custom Path').' ->';
  953:                 }
  954:                 my $selected = '';
  955:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  956:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  957:                         $selected = 'selected="selected"';
  958:                     }
  959:                 } elsif ($path eq '') {
  960:                     $selected = 'selected="selected"';
  961:                 }
  962:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  963:             }
  964:             $datatable .= '</select></td>';
  965:             my ($custom,$exempt);
  966:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  967:                 $custom = $disallowed{$lonhost}{'custompath'};
  968:                 $exempt = $disallowed{$lonhost}{'exempt'};
  969:             }
  970:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  971:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  972:                           '</tr>';
  973:         }
  974:         $datatable .= '</table></td></tr>';
  975:         return $datatable;
  976:     } elsif ($caller eq 'page') {
  977:         my %defaultchecked = ( 
  978:                                'coursecatalog' => 'on',
  979:                                'helpdesk'      => 'on',
  980:                                'adminmail'     => 'off',
  981:                                'newuser'       => 'off',
  982:                              );
  983:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  984:         my (%checkedon,%checkedoff);
  985:         foreach my $item (@toggles) {
  986:             if ($defaultchecked{$item} eq 'on') { 
  987:                 $checkedon{$item} = ' checked="checked" ';
  988:                 $checkedoff{$item} = ' ';
  989:             } elsif ($defaultchecked{$item} eq 'off') {
  990:                 $checkedoff{$item} = ' checked="checked" ';
  991:                 $checkedon{$item} = ' ';
  992:             }
  993:         }
  994:         my @images = ('img','logo','domlogo','login');
  995:         my @logintext = ('textcol','bgcol');
  996:         my @bgs = ('pgbg','mainbg','sidebg');
  997:         my @links = ('link','alink','vlink');
  998:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  999:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1000:         my (%is_custom,%designs);
 1001:         my %defaults = (
 1002:                        font => $defaultdesign{'login.font'},
 1003:                        );
 1004:         foreach my $item (@images) {
 1005:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1006:             $defaults{'showlogo'}{$item} = 1;
 1007:         }
 1008:         foreach my $item (@bgs) {
 1009:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1010:         }
 1011:         foreach my $item (@logintext) {
 1012:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1013:         }
 1014:         foreach my $item (@links) {
 1015:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1016:         }
 1017:         if (ref($settings) eq 'HASH') {
 1018:             foreach my $item (@toggles) {
 1019:                 if ($settings->{$item} eq '1') {
 1020:                     $checkedon{$item} =  ' checked="checked" ';
 1021:                     $checkedoff{$item} = ' ';
 1022:                 } elsif ($settings->{$item} eq '0') {
 1023:                     $checkedoff{$item} =  ' checked="checked" ';
 1024:                     $checkedon{$item} = ' ';
 1025:                 }
 1026:             }
 1027:             foreach my $item (@images) {
 1028:                 if (defined($settings->{$item})) {
 1029:                     $designs{$item} = $settings->{$item};
 1030:                     $is_custom{$item} = 1;
 1031:                 }
 1032:                 if (defined($settings->{'showlogo'}{$item})) {
 1033:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1034:                 }
 1035:             }
 1036:             foreach my $item (@logintext) {
 1037:                 if ($settings->{$item} ne '') {
 1038:                     $designs{'logintext'}{$item} = $settings->{$item};
 1039:                     $is_custom{$item} = 1;
 1040:                 }
 1041:             }
 1042:             if ($settings->{'font'} ne '') {
 1043:                 $designs{'font'} = $settings->{'font'};
 1044:                 $is_custom{'font'} = 1;
 1045:             }
 1046:             foreach my $item (@bgs) {
 1047:                 if ($settings->{$item} ne '') {
 1048:                     $designs{'bgs'}{$item} = $settings->{$item};
 1049:                     $is_custom{$item} = 1;
 1050:                 }
 1051:             }
 1052:             foreach my $item (@links) {
 1053:                 if ($settings->{$item} ne '') {
 1054:                     $designs{'links'}{$item} = $settings->{$item};
 1055:                     $is_custom{$item} = 1;
 1056:                 }
 1057:             }
 1058:         } else {
 1059:             if ($designhash{$dom.'.login.font'} ne '') {
 1060:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1061:                 $is_custom{'font'} = 1;
 1062:             }
 1063:             foreach my $item (@images) {
 1064:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1065:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1066:                     $is_custom{$item} = 1;
 1067:                 }
 1068:             }
 1069:             foreach my $item (@bgs) {
 1070:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1071:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1072:                     $is_custom{$item} = 1;
 1073:                 }
 1074:             }
 1075:             foreach my $item (@links) {
 1076:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1077:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1078:                     $is_custom{$item} = 1;
 1079:                 }
 1080:             }
 1081:         }
 1082:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1083:                                                       logo => 'Institution Logo',
 1084:                                                       domlogo => 'Domain Logo',
 1085:                                                       login => 'Login box');
 1086:         my $itemcount = 1;
 1087:         foreach my $item (@toggles) {
 1088:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1089:             $datatable .=  
 1090:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1091:                 '</td><td>'.
 1092:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1093:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1094:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1095:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1096:                 '</tr>';
 1097:             $itemcount ++;
 1098:         }
 1099:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1100:         $datatable .= '</tr></table></td></tr>';
 1101:     } elsif ($caller eq 'help') {
 1102:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1103:         my $switchserver = &check_switchserver($dom,$confname);
 1104:         my $itemcount = 1;
 1105:         $defaulturl = '/adm/loginproblems.html';
 1106:         $defaulttype = 'default';
 1107:         %lt = &Apache::lonlocal::texthash (
 1108:                      del     => 'Delete?',
 1109:                      rep     => 'Replace:',
 1110:                      upl     => 'Upload:',
 1111:                      default => 'Default',
 1112:                      custom  => 'Custom',
 1113:                                              );
 1114:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1115:         my @currlangs;
 1116:         if (ref($settings) eq 'HASH') {
 1117:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1118:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1119:                     next if ($settings->{'helpurl'}{$key} eq '');
 1120:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1121:                     $type{$key} = 'custom';
 1122:                     unless ($key eq 'nolang') {
 1123:                         push(@currlangs,$key);
 1124:                     }
 1125:                 }
 1126:             } elsif ($settings->{'helpurl'} ne '') {
 1127:                 $type{'nolang'} = 'custom';
 1128:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1129:             }
 1130:         }
 1131:         foreach my $lang ('nolang',sort(@currlangs)) {
 1132:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1133:             $datatable .= '<tr'.$css_class.'>';
 1134:             if ($url{$lang} eq '') {
 1135:                 $url{$lang} = $defaulturl;
 1136:             }
 1137:             if ($type{$lang} eq '') {
 1138:                 $type{$lang} = $defaulttype;
 1139:             }
 1140:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1141:             if ($lang eq 'nolang') {
 1142:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1143:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1144:             } else {
 1145:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1146:                                   $langchoices{$lang},
 1147:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1148:             }
 1149:             $datatable .= '</span></td>'."\n".
 1150:                           '<td class="LC_left_item">';
 1151:             if ($type{$lang} eq 'custom') {
 1152:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1153:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1154:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1155:             } else {
 1156:                 $datatable .= $lt{'upl'};
 1157:             }
 1158:             $datatable .='<br />';
 1159:             if ($switchserver) {
 1160:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1161:             } else {
 1162:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1163:             }
 1164:             $datatable .= '</td></tr>';
 1165:             $itemcount ++;
 1166:         }
 1167:         my @addlangs;
 1168:         foreach my $lang (sort(keys(%langchoices))) {
 1169:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1170:             push(@addlangs,$lang);
 1171:         }
 1172:         if (@addlangs > 0) {
 1173:             my %toadd;
 1174:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1175:             $toadd{''} = &mt('Select');
 1176:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1177:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1178:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1179:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1180:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1181:             if ($switchserver) {
 1182:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1183:             } else {
 1184:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1185:             }
 1186:             $datatable .= '</td></tr>';
 1187:             $itemcount ++;
 1188:         }
 1189:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1190:     } elsif ($caller eq 'headtag') {
 1191:         my %domservers = &Apache::lonnet::get_servers($dom);
 1192:         my $choice = $choices{'headtag'};
 1193:         $css_class = ' class="LC_odd_row"';
 1194:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1195:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1196:                       '<th>'.$choices{'current'}.'</th>'.
 1197:                       '<th>'.$choices{'action'}.'</th>'.
 1198:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1199:         my (%currurls,%currexempt);
 1200:         if (ref($settings) eq 'HASH') {
 1201:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1202:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1203:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1204:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1205:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1206:                     }
 1207:                 }
 1208:             }
 1209:         }
 1210:         my %lt = &Apache::lonlocal::texthash(
 1211:                                                del  => 'Delete?',
 1212:                                                rep  => 'Replace:',
 1213:                                                upl  => 'Upload:',
 1214:                                                curr => 'View contents',
 1215:                                                none => 'None',
 1216:         );
 1217:         my $switchserver = &check_switchserver($dom,$confname);
 1218:         foreach my $lonhost (sort(keys(%domservers))) {
 1219:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1220:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1221:             if ($currurls{$lonhost}) {
 1222:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1223:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1224:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1225:                               '">'.$lt{'curr'}.'</a></td>'.
 1226:                               '<td><span class="LC_nobreak"><label>'.
 1227:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1228:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1229:             } else {
 1230:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1231:             }
 1232:             $datatable .='<br />';
 1233:             if ($switchserver) {
 1234:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1235:             } else {
 1236:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1237:             }
 1238:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1239:         }
 1240:         $datatable .= '</table></td></tr>';
 1241:     }
 1242:     return $datatable;
 1243: }
 1244: 
 1245: sub login_choices {
 1246:     my %choices =
 1247:         &Apache::lonlocal::texthash (
 1248:             coursecatalog => 'Display Course/Community Catalog link?',
 1249:             adminmail     => "Display Administrator's E-mail Address?",
 1250:             helpdesk      => 'Display "Contact Helpdesk" link',
 1251:             disallowlogin => "Login page requests redirected",
 1252:             hostid        => "Server",
 1253:             server        => "Redirect to:",
 1254:             serverpath    => "Path",
 1255:             custompath    => "Custom", 
 1256:             exempt        => "Exempt IP(s)",
 1257:             directlogin   => "No redirect",
 1258:             newuser       => "Link to create a user account",
 1259:             img           => "Header",
 1260:             logo          => "Main Logo",
 1261:             domlogo       => "Domain Logo",
 1262:             login         => "Log-in Header", 
 1263:             textcol       => "Text color",
 1264:             bgcol         => "Box color",
 1265:             bgs           => "Background colors",
 1266:             links         => "Link colors",
 1267:             font          => "Font color",
 1268:             pgbg          => "Header",
 1269:             mainbg        => "Page",
 1270:             sidebg        => "Login box",
 1271:             link          => "Link",
 1272:             alink         => "Active link",
 1273:             vlink         => "Visited link",
 1274:             headtag       => "Custom markup",
 1275:             action        => "Action",
 1276:             current       => "Current",
 1277:         );
 1278:     return %choices;
 1279: }
 1280: 
 1281: sub print_rolecolors {
 1282:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1283:     my %choices = &color_font_choices();
 1284:     my @bgs = ('pgbg','tabbg','sidebg');
 1285:     my @links = ('link','alink','vlink');
 1286:     my @images = ('img');
 1287:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1288:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1289:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1290:     my (%is_custom,%designs);
 1291:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1292:     if (ref($settings) eq 'HASH') {
 1293:         if (ref($settings->{$role}) eq 'HASH') {
 1294:             if ($settings->{$role}->{'img'} ne '') {
 1295:                 $designs{'img'} = $settings->{$role}->{'img'};
 1296:                 $is_custom{'img'} = 1;
 1297:             }
 1298:             if ($settings->{$role}->{'font'} ne '') {
 1299:                 $designs{'font'} = $settings->{$role}->{'font'};
 1300:                 $is_custom{'font'} = 1;
 1301:             }
 1302:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1303:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1304:                 $is_custom{'fontmenu'} = 1;
 1305:             }
 1306:             foreach my $item (@bgs) {
 1307:                 if ($settings->{$role}->{$item} ne '') {
 1308:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1309:                     $is_custom{$item} = 1;
 1310:                 }
 1311:             }
 1312:             foreach my $item (@links) {
 1313:                 if ($settings->{$role}->{$item} ne '') {
 1314:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1315:                     $is_custom{$item} = 1;
 1316:                 }
 1317:             }
 1318:         }
 1319:     } else {
 1320:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1321:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1322:             $is_custom{'img'} = 1;
 1323:         }
 1324:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1325:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1326:             $is_custom{'fontmenu'} = 1; 
 1327:         }
 1328:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1329:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1330:             $is_custom{'font'} = 1;
 1331:         }
 1332:         foreach my $item (@bgs) {
 1333:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1334:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1335:                 $is_custom{$item} = 1;
 1336:             
 1337:             }
 1338:         }
 1339:         foreach my $item (@links) {
 1340:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1341:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1342:                 $is_custom{$item} = 1;
 1343:             }
 1344:         }
 1345:     }
 1346:     my $itemcount = 1;
 1347:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1348:     $datatable .= '</tr></table></td></tr>';
 1349:     return $datatable;
 1350: }
 1351: 
 1352: sub role_defaults {
 1353:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1354:     my %defaults;
 1355:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1356:         return %defaults;
 1357:     }
 1358:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1359:     if ($role eq 'login') {
 1360:         %defaults = (
 1361:                        font => $defaultdesign{$role.'.font'},
 1362:                     );
 1363:         if (ref($logintext) eq 'ARRAY') {
 1364:             foreach my $item (@{$logintext}) {
 1365:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1366:             }
 1367:         }
 1368:         foreach my $item (@{$images}) {
 1369:             $defaults{'showlogo'}{$item} = 1;
 1370:         }
 1371:     } else {
 1372:         %defaults = (
 1373:                        img => $defaultdesign{$role.'.img'},
 1374:                        font => $defaultdesign{$role.'.font'},
 1375:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1376:                     );
 1377:     }
 1378:     foreach my $item (@{$bgs}) {
 1379:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1380:     }
 1381:     foreach my $item (@{$links}) {
 1382:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1383:     }
 1384:     foreach my $item (@{$images}) {
 1385:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1386:     }
 1387:     return %defaults;
 1388: }
 1389: 
 1390: sub display_color_options {
 1391:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1392:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1393:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1394:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1395:     my $datatable = '<tr'.$css_class.'>'.
 1396:         '<td>'.$choices->{'font'}.'</td>';
 1397:     if (!$is_custom->{'font'}) {
 1398:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1399:     } else {
 1400:         $datatable .= '<td>&nbsp;</td>';
 1401:     }
 1402:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1403: 
 1404:     $datatable .= '<td><span class="LC_nobreak">'.
 1405:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1406:                   ' value="'.$current_color.'" />&nbsp;'.
 1407:                   '&nbsp;</td></tr>';
 1408:     unless ($role eq 'login') { 
 1409:         $datatable .= '<tr'.$css_class.'>'.
 1410:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1411:         if (!$is_custom->{'fontmenu'}) {
 1412:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1413:         } else {
 1414:             $datatable .= '<td>&nbsp;</td>';
 1415:         }
 1416: 	$current_color = $designs->{'fontmenu'} ?
 1417: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1418:         $datatable .= '<td><span class="LC_nobreak">'.
 1419:                       '<input class="colorchooser" type="text" size="10" name="'
 1420: 		      .$role.'_fontmenu"'.
 1421:                       ' value="'.$current_color.'" />&nbsp;'.
 1422:                       '&nbsp;</td></tr>';
 1423:     }
 1424:     my $switchserver = &check_switchserver($dom,$confname);
 1425:     foreach my $img (@{$images}) {
 1426: 	$itemcount ++;
 1427:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1428:         $datatable .= '<tr'.$css_class.'>'.
 1429:                       '<td>'.$choices->{$img};
 1430:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1431:         if ($role eq 'login') {
 1432:             if ($img eq 'login') {
 1433:                 $login_hdr_pick =
 1434:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1435:                 $logincolors =
 1436:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1437:                                        $designs,$defaults);
 1438:             } elsif ($img ne 'domlogo') {
 1439:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1440:             }
 1441:         }
 1442:         $datatable .= '</td>';
 1443:         if ($designs->{$img} ne '') {
 1444:             $imgfile = $designs->{$img};
 1445: 	    $img_import = ($imgfile =~ m{^/adm/});
 1446:         } else {
 1447:             $imgfile = $defaults->{$img};
 1448:         }
 1449:         if ($imgfile) {
 1450:             my ($showfile,$fullsize);
 1451:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1452:                 my $urldir = $1;
 1453:                 my $filename = $2;
 1454:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1455:                 if (@info) {
 1456:                     my $thumbfile = 'tn-'.$filename;
 1457:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1458:                     if (@thumb) {
 1459:                         $showfile = $urldir.'/'.$thumbfile;
 1460:                     } else {
 1461:                         $showfile = $imgfile;
 1462:                     }
 1463:                 } else {
 1464:                     $showfile = '';
 1465:                 }
 1466:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1467:                 $showfile = $imgfile;
 1468:                 my $imgdir = $1;
 1469:                 my $filename = $2;
 1470:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1471:                     $showfile = "/$imgdir/tn-".$filename;
 1472:                 } else {
 1473:                     my $input = $londocroot.$imgfile;
 1474:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1475:                     if (!-e $output) {
 1476:                         my ($width,$height) = &thumb_dimensions();
 1477:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1478:                         if ($fullwidth ne '' && $fullheight ne '') {
 1479:                             if ($fullwidth > $width && $fullheight > $height) { 
 1480:                                 my $size = $width.'x'.$height;
 1481:                                 system("convert -sample $size $input $output");
 1482:                                 $showfile = "/$imgdir/tn-".$filename;
 1483:                             }
 1484:                         }
 1485:                     }
 1486:                 }
 1487:             }
 1488:             if ($showfile) {
 1489:                 if ($showfile =~ m{^/(adm|res)/}) {
 1490:                     if ($showfile =~ m{^/res/}) {
 1491:                         my $local_showfile =
 1492:                             &Apache::lonnet::filelocation('',$showfile);
 1493:                         &Apache::lonnet::repcopy($local_showfile);
 1494:                     }
 1495:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1496:                 }
 1497:                 if ($imgfile) {
 1498:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1499:                         if ($imgfile =~ m{^/res/}) {
 1500:                             my $local_imgfile =
 1501:                                 &Apache::lonnet::filelocation('',$imgfile);
 1502:                             &Apache::lonnet::repcopy($local_imgfile);
 1503:                         }
 1504:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1505:                     } else {
 1506:                         $fullsize = $imgfile;
 1507:                     }
 1508:                 }
 1509:                 $datatable .= '<td>';
 1510:                 if ($img eq 'login') {
 1511:                     $datatable .= $login_hdr_pick;
 1512:                 } 
 1513:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1514:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1515:             } else {
 1516:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1517:                               &mt('Upload:').'<br />';
 1518:             }
 1519:         } else {
 1520:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1521:                           &mt('Upload:').'<br />';
 1522:         }
 1523:         if ($switchserver) {
 1524:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1525:         } else {
 1526:             if ($img ne 'login') { # suppress file selection for Log-in header
 1527:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1528:             }
 1529:         }
 1530:         $datatable .= '</td></tr>';
 1531:     }
 1532:     $itemcount ++;
 1533:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1534:     $datatable .= '<tr'.$css_class.'>'.
 1535:                   '<td>'.$choices->{'bgs'}.'</td>';
 1536:     my $bgs_def;
 1537:     foreach my $item (@{$bgs}) {
 1538:         if (!$is_custom->{$item}) {
 1539:             $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>';
 1540:         }
 1541:     }
 1542:     if ($bgs_def) {
 1543:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1544:     } else {
 1545:         $datatable .= '<td>&nbsp;</td>';
 1546:     }
 1547:     $datatable .= '<td class="LC_right_item">'.
 1548:                   '<table border="0"><tr>';
 1549: 
 1550:     foreach my $item (@{$bgs}) {
 1551:         $datatable .= '<td align="center">'.$choices->{$item};
 1552: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1553:         if ($designs->{'bgs'}{$item}) {
 1554:             $datatable .= '&nbsp;';
 1555:         }
 1556:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1557:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1558:     }
 1559:     $datatable .= '</tr></table></td></tr>';
 1560:     $itemcount ++;
 1561:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1562:     $datatable .= '<tr'.$css_class.'>'.
 1563:                   '<td>'.$choices->{'links'}.'</td>';
 1564:     my $links_def;
 1565:     foreach my $item (@{$links}) {
 1566:         if (!$is_custom->{$item}) {
 1567:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1568:         }
 1569:     }
 1570:     if ($links_def) {
 1571:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1572:     } else {
 1573:         $datatable .= '<td>&nbsp;</td>';
 1574:     }
 1575:     $datatable .= '<td class="LC_right_item">'.
 1576:                   '<table border="0"><tr>';
 1577:     foreach my $item (@{$links}) {
 1578: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1579:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1580:         if ($designs->{'links'}{$item}) {
 1581:             $datatable.='&nbsp;';
 1582:         }
 1583:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1584:                       '" /></td>';
 1585:     }
 1586:     $$rowtotal += $itemcount;
 1587:     return $datatable;
 1588: }
 1589: 
 1590: sub logo_display_options {
 1591:     my ($img,$defaults,$designs) = @_;
 1592:     my $checkedon;
 1593:     if (ref($defaults) eq 'HASH') {
 1594:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1595:             if ($defaults->{'showlogo'}{$img}) {
 1596:                 $checkedon = 'checked="checked" ';     
 1597:             }
 1598:         } 
 1599:     }
 1600:     if (ref($designs) eq 'HASH') {
 1601:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1602:             if (defined($designs->{'showlogo'}{$img})) {
 1603:                 if ($designs->{'showlogo'}{$img} == 0) {
 1604:                     $checkedon = '';
 1605:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1606:                     $checkedon = 'checked="checked" ';
 1607:                 }
 1608:             }
 1609:         }
 1610:     }
 1611:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1612:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1613:            &mt('show').'</label>'."\n";
 1614: }
 1615: 
 1616: sub login_header_options  {
 1617:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1618:     my $output = '';
 1619:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1620:         $output .= &mt('Text default(s):').'<br />';
 1621:         if (!$is_custom->{'textcol'}) {
 1622:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1623:                        '&nbsp;&nbsp;&nbsp;';
 1624:         }
 1625:         if (!$is_custom->{'bgcol'}) {
 1626:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1627:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1628:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1629:         }
 1630:         $output .= '<br />';
 1631:     }
 1632:     $output .='<br />';
 1633:     return $output;
 1634: }
 1635: 
 1636: sub login_text_colors {
 1637:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1638:     my $color_menu = '<table border="0"><tr>';
 1639:     foreach my $item (@{$logintext}) {
 1640:         $color_menu .= '<td align="center">'.$choices->{$item};
 1641:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1642:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1643:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1644:     }
 1645:     $color_menu .= '</tr></table><br />';
 1646:     return $color_menu;
 1647: }
 1648: 
 1649: sub image_changes {
 1650:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1651:     my $output;
 1652:     if ($img eq 'login') {
 1653:             # suppress image for Log-in header
 1654:     } elsif (!$is_custom) {
 1655:         if ($img ne 'domlogo') {
 1656:             $output .= &mt('Default image:').'<br />';
 1657:         } else {
 1658:             $output .= &mt('Default in use:').'<br />';
 1659:         }
 1660:     }
 1661:     if ($img eq 'login') { # suppress image for Log-in header
 1662:         $output .= '<td>'.$logincolors;
 1663:     } else {
 1664:         if ($img_import) {
 1665:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1666:         }
 1667:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1668:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1669:         if ($is_custom) {
 1670:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1671:                        '<input type="checkbox" name="'.
 1672:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1673:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1674:         } else {
 1675:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1676:         }
 1677:     }
 1678:     return $output;
 1679: }
 1680: 
 1681: sub print_quotas {
 1682:     my ($dom,$settings,$rowtotal,$action) = @_;
 1683:     my $context;
 1684:     if ($action eq 'quotas') {
 1685:         $context = 'tools';
 1686:     } else {
 1687:         $context = $action;
 1688:     }
 1689:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1690:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1691:     my $typecount = 0;
 1692:     my ($css_class,%titles);
 1693:     if ($context eq 'requestcourses') {
 1694:         @usertools = ('official','unofficial','community','textbook');
 1695:         @options =('norequest','approval','validate','autolimit');
 1696:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1697:         %titles = &courserequest_titles();
 1698:     } elsif ($context eq 'requestauthor') {
 1699:         @usertools = ('author');
 1700:         @options = ('norequest','approval','automatic');
 1701:         %titles = &authorrequest_titles();
 1702:     } else {
 1703:         @usertools = ('aboutme','blog','webdav','portfolio');
 1704:         %titles = &tool_titles();
 1705:     }
 1706:     if (ref($types) eq 'ARRAY') {
 1707:         foreach my $type (@{$types}) {
 1708:             my ($currdefquota,$currauthorquota);
 1709:             unless (($context eq 'requestcourses') ||
 1710:                     ($context eq 'requestauthor')) {
 1711:                 if (ref($settings) eq 'HASH') {
 1712:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1713:                         $currdefquota = $settings->{defaultquota}->{$type};
 1714:                     } else {
 1715:                         $currdefquota = $settings->{$type};
 1716:                     }
 1717:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1718:                         $currauthorquota = $settings->{authorquota}->{$type};
 1719:                     }
 1720:                 }
 1721:             }
 1722:             if (defined($usertypes->{$type})) {
 1723:                 $typecount ++;
 1724:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1725:                 $datatable .= '<tr'.$css_class.'>'.
 1726:                               '<td>'.$usertypes->{$type}.'</td>'.
 1727:                               '<td class="LC_left_item">';
 1728:                 if ($context eq 'requestcourses') {
 1729:                     $datatable .= '<table><tr>';
 1730:                 }
 1731:                 my %cell;  
 1732:                 foreach my $item (@usertools) {
 1733:                     if ($context eq 'requestcourses') {
 1734:                         my ($curroption,$currlimit);
 1735:                         if (ref($settings) eq 'HASH') {
 1736:                             if (ref($settings->{$item}) eq 'HASH') {
 1737:                                 $curroption = $settings->{$item}->{$type};
 1738:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1739:                                     $currlimit = $1; 
 1740:                                 }
 1741:                             }
 1742:                         }
 1743:                         if (!$curroption) {
 1744:                             $curroption = 'norequest';
 1745:                         }
 1746:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1747:                         foreach my $option (@options) {
 1748:                             my $val = $option;
 1749:                             if ($option eq 'norequest') {
 1750:                                 $val = 0;  
 1751:                             }
 1752:                             if ($option eq 'validate') {
 1753:                                 my $canvalidate = 0;
 1754:                                 if (ref($validations{$item}) eq 'HASH') { 
 1755:                                     if ($validations{$item}{$type}) {
 1756:                                         $canvalidate = 1;
 1757:                                     }
 1758:                                 }
 1759:                                 next if (!$canvalidate);
 1760:                             }
 1761:                             my $checked = '';
 1762:                             if ($option eq $curroption) {
 1763:                                 $checked = ' checked="checked"';
 1764:                             } elsif ($option eq 'autolimit') {
 1765:                                 if ($curroption =~ /^autolimit/) {
 1766:                                     $checked = ' checked="checked"';
 1767:                                 }                       
 1768:                             } 
 1769:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1770:                                   '<input type="radio" name="crsreq_'.$item.
 1771:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1772:                                   $titles{$option}.'</label>';
 1773:                             if ($option eq 'autolimit') {
 1774:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1775:                                                 $item.'_limit_'.$type.'" size="1" '.
 1776:                                                 'value="'.$currlimit.'" />';
 1777:                             }
 1778:                             $cell{$item} .= '</span> ';
 1779:                             if ($option eq 'autolimit') {
 1780:                                 $cell{$item} .= $titles{'unlimited'};
 1781:                             }
 1782:                         }
 1783:                     } elsif ($context eq 'requestauthor') {
 1784:                         my $curroption;
 1785:                         if (ref($settings) eq 'HASH') {
 1786:                             $curroption = $settings->{$type};
 1787:                         }
 1788:                         if (!$curroption) {
 1789:                             $curroption = 'norequest';
 1790:                         }
 1791:                         foreach my $option (@options) {
 1792:                             my $val = $option;
 1793:                             if ($option eq 'norequest') {
 1794:                                 $val = 0;
 1795:                             }
 1796:                             my $checked = '';
 1797:                             if ($option eq $curroption) {
 1798:                                 $checked = ' checked="checked"';
 1799:                             }
 1800:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1801:                                   '<input type="radio" name="authorreq_'.$type.
 1802:                                   '" value="'.$val.'"'.$checked.' />'.
 1803:                                   $titles{$option}.'</label></span>&nbsp; ';
 1804:                         }
 1805:                     } else {
 1806:                         my $checked = 'checked="checked" ';
 1807:                         if (ref($settings) eq 'HASH') {
 1808:                             if (ref($settings->{$item}) eq 'HASH') {
 1809:                                 if ($settings->{$item}->{$type} == 0) {
 1810:                                     $checked = '';
 1811:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1812:                                     $checked =  'checked="checked" ';
 1813:                                 }
 1814:                             }
 1815:                         }
 1816:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1817:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1818:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1819:                                       '</label></span>&nbsp; ';
 1820:                     }
 1821:                 }
 1822:                 if ($context eq 'requestcourses') {
 1823:                     $datatable .= '</tr><tr>';
 1824:                     foreach my $item (@usertools) {
 1825:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1826:                     }
 1827:                     $datatable .= '</tr></table>';
 1828:                 }
 1829:                 $datatable .= '</td>';
 1830:                 unless (($context eq 'requestcourses') ||
 1831:                         ($context eq 'requestauthor')) {
 1832:                     $datatable .= 
 1833:                               '<td class="LC_right_item">'.
 1834:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1835:                               '<input type="text" name="quota_'.$type.
 1836:                               '" value="'.$currdefquota.
 1837:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1838:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1839:                               '<input type="text" name="authorquota_'.$type.
 1840:                               '" value="'.$currauthorquota.
 1841:                               '" size="5" /></span></td>';
 1842:                 }
 1843:                 $datatable .= '</tr>';
 1844:             }
 1845:         }
 1846:     }
 1847:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1848:         $defaultquota = '20';
 1849:         $authorquota = '500';
 1850:         if (ref($settings) eq 'HASH') {
 1851:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1852:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1853:             } elsif (defined($settings->{'default'})) {
 1854:                 $defaultquota = $settings->{'default'};
 1855:             }
 1856:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1857:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1858:             }
 1859:         }
 1860:     }
 1861:     $typecount ++;
 1862:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1863:     $datatable .= '<tr'.$css_class.'>'.
 1864:                   '<td>'.$othertitle.'</td>'.
 1865:                   '<td class="LC_left_item">';
 1866:     if ($context eq 'requestcourses') {
 1867:         $datatable .= '<table><tr>';
 1868:     }
 1869:     my %defcell;
 1870:     foreach my $item (@usertools) {
 1871:         if ($context eq 'requestcourses') {
 1872:             my ($curroption,$currlimit);
 1873:             if (ref($settings) eq 'HASH') {
 1874:                 if (ref($settings->{$item}) eq 'HASH') {
 1875:                     $curroption = $settings->{$item}->{'default'};
 1876:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1877:                         $currlimit = $1;
 1878:                     }
 1879:                 }
 1880:             }
 1881:             if (!$curroption) {
 1882:                 $curroption = 'norequest';
 1883:             }
 1884:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1885:             foreach my $option (@options) {
 1886:                 my $val = $option;
 1887:                 if ($option eq 'norequest') {
 1888:                     $val = 0;
 1889:                 }
 1890:                 if ($option eq 'validate') {
 1891:                     my $canvalidate = 0;
 1892:                     if (ref($validations{$item}) eq 'HASH') {
 1893:                         if ($validations{$item}{'default'}) {
 1894:                             $canvalidate = 1;
 1895:                         }
 1896:                     }
 1897:                     next if (!$canvalidate);
 1898:                 }
 1899:                 my $checked = '';
 1900:                 if ($option eq $curroption) {
 1901:                     $checked = ' checked="checked"';
 1902:                 } elsif ($option eq 'autolimit') {
 1903:                     if ($curroption =~ /^autolimit/) {
 1904:                         $checked = ' checked="checked"';
 1905:                     }
 1906:                 }
 1907:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1908:                                   '<input type="radio" name="crsreq_'.$item.
 1909:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1910:                                   $titles{$option}.'</label>';
 1911:                 if ($option eq 'autolimit') {
 1912:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1913:                                        $item.'_limit_default" size="1" '.
 1914:                                        'value="'.$currlimit.'" />';
 1915:                 }
 1916:                 $defcell{$item} .= '</span> ';
 1917:                 if ($option eq 'autolimit') {
 1918:                     $defcell{$item} .= $titles{'unlimited'};
 1919:                 }
 1920:             }
 1921:         } elsif ($context eq 'requestauthor') {
 1922:             my $curroption;
 1923:             if (ref($settings) eq 'HASH') {
 1924:                 $curroption = $settings->{'default'};
 1925:             }
 1926:             if (!$curroption) {
 1927:                 $curroption = 'norequest';
 1928:             }
 1929:             foreach my $option (@options) {
 1930:                 my $val = $option;
 1931:                 if ($option eq 'norequest') {
 1932:                     $val = 0;
 1933:                 }
 1934:                 my $checked = '';
 1935:                 if ($option eq $curroption) {
 1936:                     $checked = ' checked="checked"';
 1937:                 }
 1938:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1939:                               '<input type="radio" name="authorreq_default"'.
 1940:                               ' value="'.$val.'"'.$checked.' />'.
 1941:                               $titles{$option}.'</label></span>&nbsp; ';
 1942:             }
 1943:         } else {
 1944:             my $checked = 'checked="checked" ';
 1945:             if (ref($settings) eq 'HASH') {
 1946:                 if (ref($settings->{$item}) eq 'HASH') {
 1947:                     if ($settings->{$item}->{'default'} == 0) {
 1948:                         $checked = '';
 1949:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1950:                         $checked = 'checked="checked" ';
 1951:                     }
 1952:                 }
 1953:             }
 1954:             $datatable .= '<span class="LC_nobreak"><label>'.
 1955:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1956:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1957:                           '</label></span>&nbsp; ';
 1958:         }
 1959:     }
 1960:     if ($context eq 'requestcourses') {
 1961:         $datatable .= '</tr><tr>';
 1962:         foreach my $item (@usertools) {
 1963:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1964:         }
 1965:         $datatable .= '</tr></table>';
 1966:     }
 1967:     $datatable .= '</td>';
 1968:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1969:         $datatable .= '<td class="LC_right_item">'.
 1970:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1971:                       '<input type="text" name="defaultquota" value="'.
 1972:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1973:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1974:                       '<input type="text" name="authorquota" value="'.
 1975:                       $authorquota.'" size="5" /></span></td>';
 1976:     }
 1977:     $datatable .= '</tr>';
 1978:     $typecount ++;
 1979:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1980:     $datatable .= '<tr'.$css_class.'>'.
 1981:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1982:     if ($context eq 'requestcourses') {
 1983:         $datatable .= &mt('(overrides affiliation, if set)').
 1984:                       '</td>'.
 1985:                       '<td class="LC_left_item">'.
 1986:                       '<table><tr>';
 1987:     } else {
 1988:         $datatable .= &mt('(overrides affiliation, if checked)').
 1989:                       '</td>'.
 1990:                       '<td class="LC_left_item" colspan="2">'.
 1991:                       '<br />';
 1992:     }
 1993:     my %advcell;
 1994:     foreach my $item (@usertools) {
 1995:         if ($context eq 'requestcourses') {
 1996:             my ($curroption,$currlimit);
 1997:             if (ref($settings) eq 'HASH') {
 1998:                 if (ref($settings->{$item}) eq 'HASH') {
 1999:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2000:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2001:                         $currlimit = $1;
 2002:                     }
 2003:                 }
 2004:             }
 2005:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2006:             my $checked = '';
 2007:             if ($curroption eq '') {
 2008:                 $checked = ' checked="checked"';
 2009:             }
 2010:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2011:                                '<input type="radio" name="crsreq_'.$item.
 2012:                                '__LC_adv" value=""'.$checked.' />'.
 2013:                                &mt('No override set').'</label></span>&nbsp; ';
 2014:             foreach my $option (@options) {
 2015:                 my $val = $option;
 2016:                 if ($option eq 'norequest') {
 2017:                     $val = 0;
 2018:                 }
 2019:                 if ($option eq 'validate') {
 2020:                     my $canvalidate = 0;
 2021:                     if (ref($validations{$item}) eq 'HASH') {
 2022:                         if ($validations{$item}{'_LC_adv'}) {
 2023:                             $canvalidate = 1;
 2024:                         }
 2025:                     }
 2026:                     next if (!$canvalidate);
 2027:                 }
 2028:                 my $checked = '';
 2029:                 if ($val eq $curroption) {
 2030:                     $checked = ' checked="checked"';
 2031:                 } elsif ($option eq 'autolimit') {
 2032:                     if ($curroption =~ /^autolimit/) {
 2033:                         $checked = ' checked="checked"';
 2034:                     }
 2035:                 }
 2036:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2037:                                   '<input type="radio" name="crsreq_'.$item.
 2038:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2039:                                   $titles{$option}.'</label>';
 2040:                 if ($option eq 'autolimit') {
 2041:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2042:                                        $item.'_limit__LC_adv" size="1" '.
 2043:                                        'value="'.$currlimit.'" />';
 2044:                 }
 2045:                 $advcell{$item} .= '</span> ';
 2046:                 if ($option eq 'autolimit') {
 2047:                     $advcell{$item} .= $titles{'unlimited'};
 2048:                 }
 2049:             }
 2050:         } elsif ($context eq 'requestauthor') {
 2051:             my $curroption;
 2052:             if (ref($settings) eq 'HASH') {
 2053:                 $curroption = $settings->{'_LC_adv'};
 2054:             }
 2055:             my $checked = '';
 2056:             if ($curroption eq '') {
 2057:                 $checked = ' checked="checked"';
 2058:             }
 2059:             $datatable .= '<span class="LC_nobreak"><label>'.
 2060:                           '<input type="radio" name="authorreq__LC_adv"'.
 2061:                           ' value=""'.$checked.' />'.
 2062:                           &mt('No override set').'</label></span>&nbsp; ';
 2063:             foreach my $option (@options) {
 2064:                 my $val = $option;
 2065:                 if ($option eq 'norequest') {
 2066:                     $val = 0;
 2067:                 }
 2068:                 my $checked = '';
 2069:                 if ($val eq $curroption) {
 2070:                     $checked = ' checked="checked"';
 2071:                 }
 2072:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2073:                               '<input type="radio" name="authorreq__LC_adv"'.
 2074:                               ' value="'.$val.'"'.$checked.' />'.
 2075:                               $titles{$option}.'</label></span>&nbsp; ';
 2076:             }
 2077:         } else {
 2078:             my $checked = 'checked="checked" ';
 2079:             if (ref($settings) eq 'HASH') {
 2080:                 if (ref($settings->{$item}) eq 'HASH') {
 2081:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2082:                         $checked = '';
 2083:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2084:                         $checked = 'checked="checked" ';
 2085:                     }
 2086:                 }
 2087:             }
 2088:             $datatable .= '<span class="LC_nobreak"><label>'.
 2089:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2090:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2091:                           '</label></span>&nbsp; ';
 2092:         }
 2093:     }
 2094:     if ($context eq 'requestcourses') {
 2095:         $datatable .= '</tr><tr>';
 2096:         foreach my $item (@usertools) {
 2097:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2098:         }
 2099:         $datatable .= '</tr></table>';
 2100:     }
 2101:     $datatable .= '</td></tr>';
 2102:     $$rowtotal += $typecount;
 2103:     return $datatable;
 2104: }
 2105: 
 2106: sub print_requestmail {
 2107:     my ($dom,$action,$settings,$rowtotal) = @_;
 2108:     my ($now,$datatable,%currapp);
 2109:     $now = time;
 2110:     if (ref($settings) eq 'HASH') {
 2111:         if (ref($settings->{'notify'}) eq 'HASH') {
 2112:             if ($settings->{'notify'}{'approval'} ne '') {
 2113:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2114:             }
 2115:         }
 2116:     }
 2117:     my $numinrow = 2;
 2118:     my $css_class;
 2119:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2120:     my $text;
 2121:     if ($action eq 'requestcourses') {
 2122:         $text = &mt('Receive notification of course requests requiring approval');
 2123:     } elsif ($action eq 'requestauthor') {
 2124:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2125:     } else {
 2126:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2127:     }
 2128:     $datatable = '<tr'.$css_class.'>'.
 2129:                  ' <td>'.$text.'</td>'.
 2130:                  ' <td class="LC_left_item">';
 2131:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2132:                                                  $action.'notifyapproval',%currapp);
 2133:     if ($numdc > 0) {
 2134:         $datatable .= $table;
 2135:     } else {
 2136:         $datatable .= &mt('There are no active Domain Coordinators');
 2137:     }
 2138:     $datatable .='</td></tr>';
 2139:     return $datatable;
 2140: }
 2141: 
 2142: sub print_studentcode {
 2143:     my ($settings,$rowtotal) = @_;
 2144:     my $rownum = 0; 
 2145:     my ($output,%current);
 2146:     my @crstypes = ('official','unofficial','community','textbook');
 2147:     if (ref($settings) eq 'HASH') {
 2148:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2149:             foreach my $type (@crstypes) {
 2150:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2151:             }
 2152:         }
 2153:     }
 2154:     $output .= '<tr>'.
 2155:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2156:                '<td class="LC_left_item">';
 2157:     foreach my $type (@crstypes) {
 2158:         my $check = ' ';
 2159:         if ($current{$type}) {
 2160:             $check = ' checked="checked" ';
 2161:         }
 2162:         $output .= '<span class="LC_nobreak"><label>'.
 2163:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2164:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2165:     }
 2166:     $output .= '</td></tr>';
 2167:     $$rowtotal ++;
 2168:     return $output;
 2169: }
 2170: 
 2171: sub print_textbookcourses {
 2172:     my ($dom,$type,$settings,$rowtotal) = @_;
 2173:     my $rownum = 0;
 2174:     my $css_class;
 2175:     my $itemcount = 1;
 2176:     my $maxnum = 0;
 2177:     my $bookshash;
 2178:     if (ref($settings) eq 'HASH') {
 2179:         $bookshash = $settings->{$type};
 2180:     }
 2181:     my %ordered;
 2182:     if (ref($bookshash) eq 'HASH') {
 2183:         foreach my $item (keys(%{$bookshash})) {
 2184:             if (ref($bookshash->{$item}) eq 'HASH') {
 2185:                 my $num = $bookshash->{$item}{'order'};
 2186:                 $ordered{$num} = $item;
 2187:             }
 2188:         }
 2189:     }
 2190:     my $confname = $dom.'-domainconfig';
 2191:     my $switchserver = &check_switchserver($dom,$confname);
 2192:     my $maxnum = scalar(keys(%ordered));
 2193:     my $datatable;
 2194:     if (keys(%ordered)) {
 2195:         my @items = sort { $a <=> $b } keys(%ordered);
 2196:         for (my $i=0; $i<@items; $i++) {
 2197:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2198:             my $key = $ordered{$items[$i]};
 2199:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2200:             my $coursetitle = $coursehash{'description'};
 2201:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2202:             if (ref($bookshash->{$key}) eq 'HASH') {
 2203:                 $subject = $bookshash->{$key}->{'subject'};
 2204:                 $title = $bookshash->{$key}->{'title'};
 2205:                 if ($type eq 'textbooks') {
 2206:                     $publisher = $bookshash->{$key}->{'publisher'};
 2207:                     $author = $bookshash->{$key}->{'author'};
 2208:                     $image = $bookshash->{$key}->{'image'};
 2209:                     if ($image ne '') {
 2210:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2211:                         my $imagethumb = "$path/tn-".$imagefile;
 2212:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2213:                     }
 2214:                 }
 2215:             }
 2216:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2217:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2218:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2219:             for (my $k=0; $k<=$maxnum; $k++) {
 2220:                 my $vpos = $k+1;
 2221:                 my $selstr;
 2222:                 if ($k == $i) {
 2223:                     $selstr = ' selected="selected" ';
 2224:                 }
 2225:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2226:             }
 2227:             $datatable .= '</select>'.('&nbsp;'x2).
 2228:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2229:                 &mt('Delete?').'</label></span></td>'.
 2230:                 '<td colspan="2">'.
 2231:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2232:                 ('&nbsp;'x2).
 2233:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2234:             if ($type eq 'textbooks') {
 2235:                 $datatable .= ('&nbsp;'x2).
 2236:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2237:                               ('&nbsp;'x2).
 2238:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2239:                               ('&nbsp;'x2).
 2240:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2241:                 if ($image) {
 2242:                     $datatable .= '<span class="LC_nobreak">'.
 2243:                                   $imgsrc.
 2244:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2245:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2246:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2247:                 }
 2248:                 if ($switchserver) {
 2249:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2250:                 } else {
 2251:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2252:                 }
 2253:             }
 2254:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2255:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2256:                           $coursetitle.'</span></td></tr>'."\n";
 2257:             $itemcount ++;
 2258:         }
 2259:     }
 2260:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2261:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2262:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2263:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2264:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2265:     for (my $k=0; $k<$maxnum+1; $k++) {
 2266:         my $vpos = $k+1;
 2267:         my $selstr;
 2268:         if ($k == $maxnum) {
 2269:             $selstr = ' selected="selected" ';
 2270:         }
 2271:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2272:     }
 2273:     $datatable .= '</select>&nbsp;'."\n".
 2274:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2275:                   '<td colspan="2">'.
 2276:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2277:                   ('&nbsp;'x2).
 2278:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2279:                   ('&nbsp;'x2);
 2280:     if ($type eq 'textbooks') {
 2281:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2282:                       ('&nbsp;'x2).
 2283:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2284:                       ('&nbsp;'x2).
 2285:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2286:         if ($switchserver) {
 2287:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2288:         } else {
 2289:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2290:         }
 2291:     }
 2292:     $datatable .= '</span>'."\n".
 2293:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2294:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2295:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2296:                   &Apache::loncommon::selectcourse_link
 2297:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2298:                   '</span></td>'."\n".
 2299:                   '</tr>'."\n";
 2300:     $itemcount ++;
 2301:     return $datatable;
 2302: }
 2303: 
 2304: sub textbookcourses_javascript {
 2305:     my ($settings) = @_;
 2306:     return unless(ref($settings) eq 'HASH');
 2307:     my (%ordered,%total,%jstext);
 2308:     foreach my $type ('textbooks','templates') {
 2309:         $total{$type} = 0;
 2310:         if (ref($settings->{$type}) eq 'HASH') {
 2311:             foreach my $item (keys(%{$settings->{$type}})) {
 2312:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2313:                     my $num = $settings->{$type}->{$item}{'order'};
 2314:                     $ordered{$type}{$num} = $item;
 2315:                 }
 2316:             }
 2317:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2318:         }
 2319:         my @jsarray = ();
 2320:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2321:             push(@jsarray,$ordered{$type}{$item});
 2322:         }
 2323:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2324:     }
 2325:     return <<"ENDSCRIPT";
 2326: <script type="text/javascript">
 2327: // <![CDATA[
 2328: function reorderBooks(form,item,caller) {
 2329:     var changedVal;
 2330: $jstext{'textbooks'};
 2331: $jstext{'templates'};
 2332:     var newpos;
 2333:     var maxh;
 2334:     if (caller == 'textbooks') {  
 2335:         newpos = 'textbooks_addbook_pos';
 2336:         maxh = 1 + $total{'textbooks'};
 2337:     } else {
 2338:         newpos = 'templates_addbook_pos';
 2339:         maxh = 1 + $total{'templates'};
 2340:     }
 2341:     var current = new Array;
 2342:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2343:     if (item == newpos) {
 2344:         changedVal = newitemVal;
 2345:     } else {
 2346:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2347:         current[newitemVal] = newpos;
 2348:     }
 2349:     if (caller == 'textbooks') {
 2350:         for (var i=0; i<textbooks.length; i++) {
 2351:             var elementName = 'textbooks_'+textbooks[i];
 2352:             if (elementName != item) {
 2353:                 if (form.elements[elementName]) {
 2354:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2355:                     current[currVal] = elementName;
 2356:                 }
 2357:             }
 2358:         }
 2359:     }
 2360:     if (caller == 'templates') {
 2361:         for (var i=0; i<templates.length; i++) {
 2362:             var elementName = 'templates_'+templates[i];
 2363:             if (elementName != item) {
 2364:                 if (form.elements[elementName]) {
 2365:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2366:                     current[currVal] = elementName;
 2367:                 }
 2368:             }
 2369:         }
 2370:     }
 2371:     var oldVal;
 2372:     for (var j=0; j<maxh; j++) {
 2373:         if (current[j] == undefined) {
 2374:             oldVal = j;
 2375:         }
 2376:     }
 2377:     if (oldVal < changedVal) {
 2378:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2379:            var elementName = current[k];
 2380:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2381:         }
 2382:     } else {
 2383:         for (var k=changedVal; k<oldVal; k++) {
 2384:             var elementName = current[k];
 2385:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2386:         }
 2387:     }
 2388:     return;
 2389: }
 2390: 
 2391: // ]]>
 2392: </script>
 2393: 
 2394: ENDSCRIPT
 2395: }
 2396: 
 2397: sub print_autoenroll {
 2398:     my ($dom,$settings,$rowtotal) = @_;
 2399:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2400:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2401:     if (ref($settings) eq 'HASH') {
 2402:         if (exists($settings->{'run'})) {
 2403:             if ($settings->{'run'} eq '0') {
 2404:                 $runoff = ' checked="checked" ';
 2405:                 $runon = ' ';
 2406:             } else {
 2407:                 $runon = ' checked="checked" ';
 2408:                 $runoff = ' ';
 2409:             }
 2410:         } else {
 2411:             if ($autorun) {
 2412:                 $runon = ' checked="checked" ';
 2413:                 $runoff = ' ';
 2414:             } else {
 2415:                 $runoff = ' checked="checked" ';
 2416:                 $runon = ' ';
 2417:             }
 2418:         }
 2419:         if (exists($settings->{'co-owners'})) {
 2420:             if ($settings->{'co-owners'} eq '0') {
 2421:                 $coownersoff = ' checked="checked" ';
 2422:                 $coownerson = ' ';
 2423:             } else {
 2424:                 $coownerson = ' checked="checked" ';
 2425:                 $coownersoff = ' ';
 2426:             }
 2427:         } else {
 2428:             $coownersoff = ' checked="checked" ';
 2429:             $coownerson = ' ';
 2430:         }
 2431:         if (exists($settings->{'sender_domain'})) {
 2432:             $defdom = $settings->{'sender_domain'};
 2433:         }
 2434:     } else {
 2435:         if ($autorun) {
 2436:             $runon = ' checked="checked" ';
 2437:             $runoff = ' ';
 2438:         } else {
 2439:             $runoff = ' checked="checked" ';
 2440:             $runon = ' ';
 2441:         }
 2442:     }
 2443:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2444:     my $notif_sender;
 2445:     if (ref($settings) eq 'HASH') {
 2446:         $notif_sender = $settings->{'sender_uname'};
 2447:     }
 2448:     my $datatable='<tr class="LC_odd_row">'.
 2449:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2450:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2451:                   '<input type="radio" name="autoenroll_run"'.
 2452:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2453:                   '<label><input type="radio" name="autoenroll_run"'.
 2454:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2455:                   '</tr><tr>'.
 2456:                   '<td>'.&mt('Notification messages - sender').
 2457:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2458:                   &mt('username').':&nbsp;'.
 2459:                   '<input type="text" name="sender_uname" value="'.
 2460:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2461:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2462:                   '<tr class="LC_odd_row">'.
 2463:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2464:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2465:                   '<input type="radio" name="autoassign_coowners"'.
 2466:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2467:                   '<label><input type="radio" name="autoassign_coowners"'.
 2468:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2469:                   '</tr>';
 2470:     $$rowtotal += 3;
 2471:     return $datatable;
 2472: }
 2473: 
 2474: sub print_autoupdate {
 2475:     my ($position,$dom,$settings,$rowtotal) = @_;
 2476:     my $datatable;
 2477:     if ($position eq 'top') {
 2478:         my $updateon = ' ';
 2479:         my $updateoff = ' checked="checked" ';
 2480:         my $classlistson = ' ';
 2481:         my $classlistsoff = ' checked="checked" ';
 2482:         if (ref($settings) eq 'HASH') {
 2483:             if ($settings->{'run'} eq '1') {
 2484:                 $updateon = $updateoff;
 2485:                 $updateoff = ' ';
 2486:             }
 2487:             if ($settings->{'classlists'} eq '1') {
 2488:                 $classlistson = $classlistsoff;
 2489:                 $classlistsoff = ' ';
 2490:             }
 2491:         }
 2492:         my %title = (
 2493:                    run => 'Auto-update active?',
 2494:                    classlists => 'Update information in classlists?',
 2495:                     );
 2496:         $datatable = '<tr class="LC_odd_row">'. 
 2497:                   '<td>'.&mt($title{'run'}).'</td>'.
 2498:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2499:                   '<input type="radio" name="autoupdate_run"'.
 2500:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2501:                   '<label><input type="radio" name="autoupdate_run"'.
 2502:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2503:                   '</tr><tr>'.
 2504:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2505:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2506:                   '<label><input type="radio" name="classlists"'.
 2507:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2508:                   '<label><input type="radio" name="classlists"'.
 2509:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2510:                   '</tr>';
 2511:         $$rowtotal += 2;
 2512:     } elsif ($position eq 'middle') {
 2513:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2514:         my $numinrow = 3;
 2515:         my $locknamesettings;
 2516:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2517:                                      $dom,$numinrow,$othertitle,
 2518:                                     'lockablenames');
 2519:         $$rowtotal ++;
 2520:     } else {
 2521:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2522:         my @fields = ('lastname','firstname','middlename','generation',
 2523:                       'permanentemail','id');
 2524:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2525:         my $numrows = 0;
 2526:         if (ref($types) eq 'ARRAY') {
 2527:             if (@{$types} > 0) {
 2528:                 $datatable = 
 2529:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2530:                                          \@fields,$types,\$numrows);
 2531:                     $$rowtotal += @{$types}; 
 2532:             }
 2533:         }
 2534:         $datatable .= 
 2535:             &usertype_update_row($settings,{'default' => $othertitle},
 2536:                                  \%fieldtitles,\@fields,['default'],
 2537:                                  \$numrows);
 2538:         $$rowtotal ++;     
 2539:     }
 2540:     return $datatable;
 2541: }
 2542: 
 2543: sub print_autocreate {
 2544:     my ($dom,$settings,$rowtotal) = @_;
 2545:     my (%createon,%createoff,%currhash);
 2546:     my @types = ('xml','req');
 2547:     if (ref($settings) eq 'HASH') {
 2548:         foreach my $item (@types) {
 2549:             $createoff{$item} = ' checked="checked" ';
 2550:             $createon{$item} = ' ';
 2551:             if (exists($settings->{$item})) {
 2552:                 if ($settings->{$item}) {
 2553:                     $createon{$item} = ' checked="checked" ';
 2554:                     $createoff{$item} = ' ';
 2555:                 }
 2556:             }
 2557:         }
 2558:         if ($settings->{'xmldc'} ne '') {
 2559:             $currhash{$settings->{'xmldc'}} = 1;
 2560:         }
 2561:     } else {
 2562:         foreach my $item (@types) {
 2563:             $createoff{$item} = ' checked="checked" ';
 2564:             $createon{$item} = ' ';
 2565:         }
 2566:     }
 2567:     $$rowtotal += 2;
 2568:     my $numinrow = 2;
 2569:     my $datatable='<tr class="LC_odd_row">'.
 2570:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2571:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2572:                   '<input type="radio" name="autocreate_xml"'.
 2573:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2574:                   '<label><input type="radio" name="autocreate_xml"'.
 2575:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2576:                   '</td></tr><tr>'.
 2577:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2578:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2579:                   '<input type="radio" name="autocreate_req"'.
 2580:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2581:                   '<label><input type="radio" name="autocreate_req"'.
 2582:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2583:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2584:                                                    'autocreate_xmldc',%currhash);
 2585:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2586:     if ($numdc > 1) {
 2587:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2588:                       '</td><td class="LC_left_item">';
 2589:     } else {
 2590:         $datatable .= &mt('Course creation processed as:').
 2591:                       '</td><td class="LC_right_item">';
 2592:     }
 2593:     $datatable .= $dctable.'</td></tr>';
 2594:     $$rowtotal += $rows;
 2595:     return $datatable;
 2596: }
 2597: 
 2598: sub print_directorysrch {
 2599:     my ($dom,$settings,$rowtotal) = @_;
 2600:     my $srchon = ' ';
 2601:     my $srchoff = ' checked="checked" ';
 2602:     my ($exacton,$containson,$beginson);
 2603:     my $localon = ' ';
 2604:     my $localoff = ' checked="checked" ';
 2605:     if (ref($settings) eq 'HASH') {
 2606:         if ($settings->{'available'} eq '1') {
 2607:             $srchon = $srchoff;
 2608:             $srchoff = ' ';
 2609:         }
 2610:         if ($settings->{'localonly'} eq '1') {
 2611:             $localon = $localoff;
 2612:             $localoff = ' ';
 2613:         }
 2614:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2615:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2616:                 if ($type eq 'exact') {
 2617:                     $exacton = ' checked="checked" ';
 2618:                 } elsif ($type eq 'contains') {
 2619:                     $containson = ' checked="checked" ';
 2620:                 } elsif ($type eq 'begins') {
 2621:                     $beginson = ' checked="checked" ';
 2622:                 }
 2623:             }
 2624:         } else {
 2625:             if ($settings->{'searchtypes'} eq 'exact') {
 2626:                 $exacton = ' checked="checked" ';
 2627:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2628:                 $containson = ' checked="checked" ';
 2629:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2630:                 $exacton = ' checked="checked" ';
 2631:                 $containson = ' checked="checked" ';
 2632:             }
 2633:         }
 2634:     }
 2635:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2636:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2637: 
 2638:     my $numinrow = 4;
 2639:     my $cansrchrow = 0;
 2640:     my $datatable='<tr class="LC_odd_row">'.
 2641:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2642:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2643:                   '<input type="radio" name="dirsrch_available"'.
 2644:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2645:                   '<label><input type="radio" name="dirsrch_available"'.
 2646:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2647:                   '</tr><tr>'.
 2648:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2649:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2650:                   '<input type="radio" name="dirsrch_localonly"'.
 2651:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2652:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2653:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2654:                   '</tr>';
 2655:     $$rowtotal += 2;
 2656:     if (ref($usertypes) eq 'HASH') {
 2657:         if (keys(%{$usertypes}) > 0) {
 2658:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2659:                                          $numinrow,$othertitle,'cansearch');
 2660:             $cansrchrow = 1;
 2661:         }
 2662:     }
 2663:     if ($cansrchrow) {
 2664:         $$rowtotal ++;
 2665:         $datatable .= '<tr>';
 2666:     } else {
 2667:         $datatable .= '<tr class="LC_odd_row">';
 2668:     }
 2669:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2670:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2671:     foreach my $title (@{$titleorder}) {
 2672:         if (defined($searchtitles->{$title})) {
 2673:             my $check = ' ';
 2674:             if (ref($settings) eq 'HASH') {
 2675:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2676:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2677:                         $check = ' checked="checked" ';
 2678:                     }
 2679:                 }
 2680:             }
 2681:             $datatable .= '<td class="LC_left_item">'.
 2682:                           '<span class="LC_nobreak"><label>'.
 2683:                           '<input type="checkbox" name="searchby" '.
 2684:                           'value="'.$title.'"'.$check.'/>'.
 2685:                           $searchtitles->{$title}.'</label></span></td>';
 2686:         }
 2687:     }
 2688:     $datatable .= '</tr></table></td></tr>';
 2689:     $$rowtotal ++;
 2690:     if ($cansrchrow) {
 2691:         $datatable .= '<tr class="LC_odd_row">';
 2692:     } else {
 2693:         $datatable .= '<tr>';
 2694:     }
 2695:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2696:                   '<td class="LC_left_item" colspan="2">'.
 2697:                   '<span class="LC_nobreak"><label>'.
 2698:                   '<input type="checkbox" name="searchtypes" '.
 2699:                   $exacton.' value="exact" />'.&mt('Exact match').
 2700:                   '</label>&nbsp;'.
 2701:                   '<label><input type="checkbox" name="searchtypes" '.
 2702:                   $beginson.' value="begins" />'.&mt('Begins with').
 2703:                   '</label>&nbsp;'.
 2704:                   '<label><input type="checkbox" name="searchtypes" '.
 2705:                   $containson.' value="contains" />'.&mt('Contains').
 2706:                   '</label></span></td></tr>';
 2707:     $$rowtotal ++;
 2708:     return $datatable;
 2709: }
 2710: 
 2711: sub print_contacts {
 2712:     my ($dom,$settings,$rowtotal) = @_;
 2713:     my $datatable;
 2714:     my @contacts = ('adminemail','supportemail');
 2715:     my (%checked,%to,%otheremails,%bccemails);
 2716:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2717:                     'requestsmail','updatesmail','idconflictsmail');
 2718:     foreach my $type (@mailings) {
 2719:         $otheremails{$type} = '';
 2720:     }
 2721:     $bccemails{'helpdeskmail'} = '';
 2722:     if (ref($settings) eq 'HASH') {
 2723:         foreach my $item (@contacts) {
 2724:             if (exists($settings->{$item})) {
 2725:                 $to{$item} = $settings->{$item};
 2726:             }
 2727:         }
 2728:         foreach my $type (@mailings) {
 2729:             if (exists($settings->{$type})) {
 2730:                 if (ref($settings->{$type}) eq 'HASH') {
 2731:                     foreach my $item (@contacts) {
 2732:                         if ($settings->{$type}{$item}) {
 2733:                             $checked{$type}{$item} = ' checked="checked" ';
 2734:                         }
 2735:                     }
 2736:                     $otheremails{$type} = $settings->{$type}{'others'};
 2737:                     if ($type eq 'helpdeskmail') {
 2738:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2739:                     }
 2740:                 }
 2741:             } elsif ($type eq 'lonstatusmail') {
 2742:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2743:             }
 2744:         }
 2745:     } else {
 2746:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2747:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2748:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2749:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2750:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2751:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2752:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2753:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2754:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2755:     }
 2756:     my ($titles,$short_titles) = &contact_titles();
 2757:     my $rownum = 0;
 2758:     my $css_class;
 2759:     foreach my $item (@contacts) {
 2760:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2761:         $datatable .= '<tr'.$css_class.'>'. 
 2762:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2763:                   '</span></td><td class="LC_right_item">'.
 2764:                   '<input type="text" name="'.$item.'" value="'.
 2765:                   $to{$item}.'" /></td></tr>';
 2766:         $rownum ++;
 2767:     }
 2768:     foreach my $type (@mailings) {
 2769:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2770:         $datatable .= '<tr'.$css_class.'>'.
 2771:                       '<td><span class="LC_nobreak">'.
 2772:                       $titles->{$type}.': </span></td>'.
 2773:                       '<td class="LC_left_item">'.
 2774:                       '<span class="LC_nobreak">';
 2775:         foreach my $item (@contacts) {
 2776:             $datatable .= '<label>'.
 2777:                           '<input type="checkbox" name="'.$type.'"'.
 2778:                           $checked{$type}{$item}.
 2779:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2780:                           '</label>&nbsp;';
 2781:         }
 2782:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2783:                       '<input type="text" name="'.$type.'_others" '.
 2784:                       'value="'.$otheremails{$type}.'"  />';
 2785:         if ($type eq 'helpdeskmail') {
 2786:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2787:                           '<input type="text" name="'.$type.'_bcc" '.
 2788:                           'value="'.$bccemails{$type}.'"  />';
 2789:         }
 2790:         $datatable .= '</td></tr>'."\n";
 2791:         $rownum ++;
 2792:     }
 2793:     my %choices;
 2794:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2795:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2796:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2797:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2798:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2799:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2800:     my @toggles = ('reporterrors','reportupdates');
 2801:     my %defaultchecked = ('reporterrors'  => 'on',
 2802:                           'reportupdates' => 'on');
 2803:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2804:                                                \%choices,$rownum);
 2805:     $datatable .= $reports;
 2806:     $$rowtotal += $rownum;
 2807:     return $datatable;
 2808: }
 2809: 
 2810: sub print_helpsettings {
 2811:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2812:     my ($datatable,$itemcount);
 2813:     $itemcount = 1;
 2814:     my (%choices,%defaultchecked,@toggles);
 2815:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2816:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2817:                                  &mt('LON-CAPA bug tracker'),600,500));
 2818:     %defaultchecked = ('submitbugs' => 'on');
 2819:     @toggles = ('submitbugs',);
 2820: 
 2821:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2822:                                                  \%choices,$itemcount);
 2823:     return $datatable;
 2824: }
 2825: 
 2826: sub radiobutton_prefs {
 2827:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2828:         $additional) = @_;
 2829:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2830:                    (ref($choices) eq 'HASH'));
 2831: 
 2832:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2833: 
 2834:     foreach my $item (@{$toggles}) {
 2835:         if ($defaultchecked->{$item} eq 'on') {
 2836:             $checkedon{$item} = ' checked="checked" ';
 2837:             $checkedoff{$item} = ' ';
 2838:         } elsif ($defaultchecked->{$item} eq 'off') {
 2839:             $checkedoff{$item} = ' checked="checked" ';
 2840:             $checkedon{$item} = ' ';
 2841:         }
 2842:     }
 2843:     if (ref($settings) eq 'HASH') {
 2844:         foreach my $item (@{$toggles}) {
 2845:             if ($settings->{$item} eq '1') {
 2846:                 $checkedon{$item} =  ' checked="checked" ';
 2847:                 $checkedoff{$item} = ' ';
 2848:             } elsif ($settings->{$item} eq '0') {
 2849:                 $checkedoff{$item} =  ' checked="checked" ';
 2850:                 $checkedon{$item} = ' ';
 2851:             }
 2852:         }
 2853:     }
 2854:     if ($onclick) {
 2855:         $onclick = ' onclick="'.$onclick.'"';
 2856:     }
 2857:     foreach my $item (@{$toggles}) {
 2858:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2859:         $datatable .=
 2860:             '<tr'.$css_class.'><td valign="top">'.
 2861:             '<span class="LC_nobreak">'.$choices->{$item}.
 2862:             '</span></td>'.
 2863:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2864:             '<label><input type="radio" name="'.
 2865:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2866:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2867:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2868:             '</span>'.$additional.
 2869:             '</td>'.
 2870:             '</tr>';
 2871:         $itemcount ++;
 2872:     }
 2873:     return ($datatable,$itemcount);
 2874: }
 2875: 
 2876: sub print_coursedefaults {
 2877:     my ($position,$dom,$settings,$rowtotal) = @_;
 2878:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2879:     my $itemcount = 1;
 2880:     my %choices =  &Apache::lonlocal::texthash (
 2881:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2882:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2883:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2884:         coursecredits        => 'Credits can be specified for courses',
 2885:     );
 2886:     my %staticdefaults = (
 2887:                            anonsurvey_threshold => 10,
 2888:                            uploadquota          => 500,
 2889:                          );
 2890:     if ($position eq 'top') {
 2891:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2892:         @toggles = ('canuse_pdfforms');
 2893:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2894:                                                  \%choices,$itemcount);
 2895:     } else {
 2896:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2897:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2898:             %curruploadquota);
 2899:         my $currusecredits = 0;
 2900:         my @types = ('official','unofficial','community','textbook');
 2901:         if (ref($settings) eq 'HASH') {
 2902:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2903:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2904:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2905:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2906:                 }
 2907:             }
 2908:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2909:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2910:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2911:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2912:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2913:                     ($def_textbook_credits ne '')) {
 2914:                     $currusecredits = 1;
 2915:                 }
 2916:             }
 2917:         }
 2918:         if (!$currdefresponder) {
 2919:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2920:         } elsif ($currdefresponder < 1) {
 2921:             $currdefresponder = 1;
 2922:         }
 2923:         foreach my $type (@types) {
 2924:             if ($curruploadquota{$type} eq '') {
 2925:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2926:             }
 2927:         }
 2928:         $datatable .=
 2929:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2930:                 $choices{'anonsurvey_threshold'}.
 2931:                 '</span></td>'.
 2932:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2933:                 '<input type="text" name="anonsurvey_threshold"'.
 2934:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2935:                 '</td></tr>'."\n";
 2936:         $itemcount ++;
 2937:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2938:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2939:                       $choices{'uploadquota'}.
 2940:                       '</span></td>'.
 2941:                       '<td align="right" class="LC_right_item">'.
 2942:                       '<table><tr>';
 2943:         foreach my $type (@types) {
 2944:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2945:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2946:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2947:         }
 2948:         $datatable .= '</tr></table></td></tr>'."\n";
 2949:         $itemcount ++;
 2950:         my $onclick = "toggleDisplay(this.form,'credits');";
 2951:         my $display = 'none';
 2952:         if ($currusecredits) {
 2953:             $display = 'block';
 2954:         }
 2955:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2956:                          '<span class="LC_nobreak">'.
 2957:                          &mt('Default credits for official courses [_1]',
 2958:                          '<input type="text" name="official_credits" value="'.
 2959:                          $def_official_credits.'" size="3" />').
 2960:                          '</span><br />'.
 2961:                          '<span class="LC_nobreak">'.
 2962:                          &mt('Default credits for unofficial courses [_1]',
 2963:                          '<input type="text" name="unofficial_credits" value="'.
 2964:                          $def_unofficial_credits.'" size="3" />').
 2965:                          '</span><br />'.
 2966:                          '<span class="LC_nobreak">'.
 2967:                          &mt('Default credits for textbook courses [_1]',
 2968:                          '<input type="text" name="textbook_credits" value="'.
 2969:                          $def_textbook_credits.'" size="3" />').
 2970:                          '</span></div>'."\n";
 2971:         %defaultchecked = ('coursecredits' => 'off');
 2972:         @toggles = ('coursecredits');
 2973:         my $current = {
 2974:                         'coursecredits' => $currusecredits,
 2975:                       };
 2976:         (my $table,$itemcount) =
 2977:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2978:                                \%choices,$itemcount,$onclick,$additional);
 2979:         $datatable .= $table;
 2980:         $itemcount ++;
 2981:     }
 2982:     $$rowtotal += $itemcount;
 2983:     return $datatable;
 2984: }
 2985: 
 2986: sub print_selfenrollment {
 2987:     my ($position,$dom,$settings,$rowtotal) = @_;
 2988:     my ($css_class,$datatable);
 2989:     my $itemcount = 1;
 2990:     my @types = ('official','unofficial','community','textbook');
 2991:     if (($position eq 'top') || ($position eq 'middle')) {
 2992:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 2993:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 2994:         my @rows;
 2995:         my $key;
 2996:         if ($position eq 'top') {
 2997:             $key = 'admin'; 
 2998:             if (ref($rowsref) eq 'ARRAY') {
 2999:                 @rows = @{$rowsref};
 3000:             }
 3001:         } elsif ($position eq 'middle') {
 3002:             $key = 'default';
 3003:             @rows = ('types','registered','approval','limit');
 3004:         }
 3005:         foreach my $row (@rows) {
 3006:             if (defined($titlesref->{$row})) {
 3007:                 $itemcount ++;
 3008:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3009:                 $datatable .= '<tr'.$css_class.'>'.
 3010:                               '<td>'.$titlesref->{$row}.'</td>'.
 3011:                               '<td class="LC_left_item">'.
 3012:                               '<table><tr>';
 3013:                 my (%current,%currentcap);
 3014:                 if (ref($settings) eq 'HASH') {
 3015:                     if (ref($settings->{$key}) eq 'HASH') {
 3016:                         foreach my $type (@types) {
 3017:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3018:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 3019:                             }
 3020:                             if (($row eq 'limit') && ($key eq 'default')) {
 3021:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3022:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 3023:                                 }
 3024:                             }
 3025:                         }
 3026:                     }
 3027:                 }
 3028:                 my %roles = (
 3029:                              '0' => &Apache::lonnet::plaintext('dc'),
 3030:                             ); 
 3031:             
 3032:                 foreach my $type (@types) {
 3033:                     unless (($row eq 'registered') && ($key eq 'default')) {
 3034:                         $datatable .= '<th>'.&mt($type).'</th>';
 3035:                     }
 3036:                 }
 3037:                 unless (($row eq 'registered') && ($key eq 'default')) {
 3038:                     $datatable .= '</tr><tr>';
 3039:                 }
 3040:                 foreach my $type (@types) {
 3041:                     if ($type eq 'community') {
 3042:                         $roles{'1'} = &mt('Community personnel');
 3043:                     } else {
 3044:                         $roles{'1'} = &mt('Course personnel');
 3045:                     }
 3046:                     $datatable .= '<td style="vertical-align: top">';
 3047:                     if ($position eq 'top') {
 3048:                         my %checked;
 3049:                         if ($current{$type} eq '0') {
 3050:                             $checked{'0'} = ' checked="checked"';
 3051:                         } else {
 3052:                             $checked{'1'} = ' checked="checked"';
 3053:                         }
 3054:                         foreach my $role ('1','0') {
 3055:                             $datatable .= '<span class="LC_nobreak"><label>'.
 3056:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 3057:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 3058:                                           $roles{$role}.'</label></span> ';
 3059:                         }
 3060:                     } else {
 3061:                         if ($row eq 'types') {
 3062:                             my %checked;
 3063:                             if ($current{$type} =~ /^(all|dom)$/) {
 3064:                                 $checked{$1} = ' checked="checked"';
 3065:                             } else {
 3066:                                 $checked{''} = ' checked="checked"';
 3067:                             }
 3068:                             foreach my $val ('','dom','all') {
 3069:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3070:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3071:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3072:                             }
 3073:                         } elsif ($row eq 'registered') {
 3074:                             my %checked;
 3075:                             if ($current{$type} eq '1') {
 3076:                                 $checked{'1'} = ' checked="checked"';
 3077:                             } else {
 3078:                                 $checked{'0'} = ' checked="checked"';
 3079:                             }
 3080:                             foreach my $val ('0','1') {
 3081:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3082:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3083:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3084:                             }
 3085:                         } elsif ($row eq 'approval') {
 3086:                             my %checked;
 3087:                             if ($current{$type} =~ /^([12])$/) {
 3088:                                 $checked{$1} = ' checked="checked"';
 3089:                             } else {
 3090:                                 $checked{'0'} = ' checked="checked"';
 3091:                             }
 3092:                             for my $val (0..2) {
 3093:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3094:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3095:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3096:                             }
 3097:                         } elsif ($row eq 'limit') {
 3098:                             my %checked;
 3099:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3100:                                 $checked{$1} = ' checked="checked"';
 3101:                             } else {
 3102:                                 $checked{'none'} = ' checked="checked"';
 3103:                             }
 3104:                             my $cap;
 3105:                             if ($currentcap{$type} =~ /^\d+$/) {
 3106:                                 $cap = $currentcap{$type};
 3107:                             }
 3108:                             foreach my $val ('none','allstudents','selfenrolled') {
 3109:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3110:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3111:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3112:                             }
 3113:                             $datatable .= '<br />'.
 3114:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3115:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3116:                                           '</span>'; 
 3117:                         }
 3118:                     }
 3119:                     $datatable .= '</td>';
 3120:                 }
 3121:                 $datatable .= '</tr>';
 3122:             }
 3123:             $datatable .= '</table></td></tr>';
 3124:         }
 3125:     } elsif ($position eq 'bottom') {
 3126:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3127:     }
 3128:     $$rowtotal += $itemcount;
 3129:     return $datatable;
 3130: }
 3131: 
 3132: sub print_validation_rows {
 3133:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3134:     my ($itemsref,$namesref,$fieldsref);
 3135:     if ($caller eq 'selfenroll') { 
 3136:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3137:     } elsif ($caller eq 'requestcourses') {
 3138:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3139:     }
 3140:     my %currvalidation;
 3141:     if (ref($settings) eq 'HASH') {
 3142:         if (ref($settings->{'validation'}) eq 'HASH') {
 3143:             %currvalidation = %{$settings->{'validation'}};
 3144:         }
 3145:     }
 3146:     my $datatable;
 3147:     my $itemcount = 0;
 3148:     foreach my $item (@{$itemsref}) {
 3149:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3150:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3151:                       $namesref->{$item}.
 3152:                       '</span></td>'.
 3153:                       '<td class="LC_left_item">';
 3154:         if (($item eq 'url') || ($item eq 'button')) {
 3155:             $datatable .= '<span class="LC_nobreak">'.
 3156:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3157:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3158:         } elsif ($item eq 'fields') {
 3159:             my @currfields;
 3160:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3161:                 @currfields = @{$currvalidation{$item}};
 3162:             }
 3163:             foreach my $field (@{$fieldsref}) {
 3164:                 my $check = '';
 3165:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3166:                     $check = ' checked="checked"';
 3167:                 }
 3168:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3169:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3170:                               ' value="'.$field.'"'.$check.' />'.$field.
 3171:                               '</label></span> ';
 3172:             }
 3173:         } elsif ($item eq 'markup') {
 3174:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3175:                            $currvalidation{$item}.
 3176:                               '</textarea>';
 3177:         }
 3178:         $datatable .= '</td></tr>'."\n";
 3179:         if (ref($rowtotal)) {
 3180:             $itemcount ++;
 3181:         }
 3182:     }
 3183:     if ($caller eq 'requestcourses') {
 3184:         my %currhash;
 3185:         if (ref($settings) eq 'HASH') {
 3186:             if (ref($settings->{'validation'}) eq 'HASH') {
 3187:                 if ($settings->{'validation'}{'dc'} ne '') {
 3188:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 3189:                 }
 3190:             }
 3191:         }
 3192:         my $numinrow = 2;
 3193:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3194:                                                        'validationdc',%currhash);
 3195:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3196:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 3197:         if ($numdc > 1) {
 3198:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 3199:         } else {
 3200:             $datatable .=  &mt('Course creation processed as: ');
 3201:         }
 3202:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3203:         $itemcount ++;
 3204:     }
 3205:     if (ref($rowtotal)) {
 3206:         $$rowtotal += $itemcount;
 3207:     }
 3208:     return $datatable;
 3209: }
 3210: 
 3211: sub print_usersessions {
 3212:     my ($position,$dom,$settings,$rowtotal) = @_;
 3213:     my ($css_class,$datatable,%checked,%choices);
 3214:     my (%by_ip,%by_location,@intdoms);
 3215:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3216: 
 3217:     my @alldoms = &Apache::lonnet::all_domains();
 3218:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3219:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3220:     my %altids = &id_for_thisdom(%servers);
 3221:     my $itemcount = 1;
 3222:     if ($position eq 'top') {
 3223:         if (keys(%serverhomes) > 1) {
 3224:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3225:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 3226:         } else {
 3227:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3228:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3229:         }
 3230:     } else {
 3231:         if (keys(%by_location) == 0) {
 3232:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3233:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3234:         } else {
 3235:             my %lt = &usersession_titles();
 3236:             my $numinrow = 5;
 3237:             my $prefix;
 3238:             my @types;
 3239:             if ($position eq 'bottom') {
 3240:                 $prefix = 'remote';
 3241:                 @types = ('version','excludedomain','includedomain');
 3242:             } else {
 3243:                 $prefix = 'hosted';
 3244:                 @types = ('excludedomain','includedomain');
 3245:             }
 3246:             my (%current,%checkedon,%checkedoff);
 3247:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3248:             my @locations = sort(keys(%by_location));
 3249:             foreach my $type (@types) {
 3250:                 $checkedon{$type} = '';
 3251:                 $checkedoff{$type} = ' checked="checked"';
 3252:             }
 3253:             if (ref($settings) eq 'HASH') {
 3254:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3255:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3256:                         $current{$key} = $settings->{$prefix}{$key};
 3257:                         if ($key eq 'version') {
 3258:                             if ($current{$key} ne '') {
 3259:                                 $checkedon{$key} = ' checked="checked"';
 3260:                                 $checkedoff{$key} = '';
 3261:                             }
 3262:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3263:                             $checkedon{$key} = ' checked="checked"';
 3264:                             $checkedoff{$key} = '';
 3265:                         }
 3266:                     }
 3267:                 }
 3268:             }
 3269:             foreach my $type (@types) {
 3270:                 next if ($type ne 'version' && !@locations);
 3271:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3272:                 $datatable .= '<tr'.$css_class.'>
 3273:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3274:                                <span class="LC_nobreak">&nbsp;
 3275:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3276:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3277:                 if ($type eq 'version') {
 3278:                     my $selector = '<select name="'.$prefix.'_version">';
 3279:                     foreach my $version (@lcversions) {
 3280:                         my $selected = '';
 3281:                         if ($current{'version'} eq $version) {
 3282:                             $selected = ' selected="selected"';
 3283:                         }
 3284:                         $selector .= ' <option value="'.$version.'"'.
 3285:                                      $selected.'>'.$version.'</option>';
 3286:                     }
 3287:                     $selector .= '</select> ';
 3288:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3289:                 } else {
 3290:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3291:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3292:                                  ' />'.('&nbsp;'x2).
 3293:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3294:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3295:                                  "\n".
 3296:                                  '</div><div><table>';
 3297:                     my $rem;
 3298:                     for (my $i=0; $i<@locations; $i++) {
 3299:                         my ($showloc,$value,$checkedtype);
 3300:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3301:                             my $ip = $by_location{$locations[$i]}->[0];
 3302:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3303:                                  $value = join(':',@{$by_ip{$ip}});
 3304:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3305:                                 if (ref($current{$type}) eq 'ARRAY') {
 3306:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3307:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3308:                                             $checkedtype = ' checked="checked"';
 3309:                                             last;
 3310:                                         }
 3311:                                     }
 3312:                                 }
 3313:                             }
 3314:                         }
 3315:                         $rem = $i%($numinrow);
 3316:                         if ($rem == 0) {
 3317:                             if ($i > 0) {
 3318:                                 $datatable .= '</tr>';
 3319:                             }
 3320:                             $datatable .= '<tr>';
 3321:                         }
 3322:                         $datatable .= '<td class="LC_left_item">'.
 3323:                                       '<span class="LC_nobreak"><label>'.
 3324:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3325:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3326:                                       '</label></span></td>';
 3327:                     }
 3328:                     $rem = @locations%($numinrow);
 3329:                     my $colsleft = $numinrow - $rem;
 3330:                     if ($colsleft > 1 ) {
 3331:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3332:                                       '&nbsp;</td>';
 3333:                     } elsif ($colsleft == 1) {
 3334:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3335:                     }
 3336:                     $datatable .= '</tr></table>';
 3337:                 }
 3338:                 $datatable .= '</td></tr>';
 3339:                 $itemcount ++;
 3340:             }
 3341:         }
 3342:     }
 3343:     $$rowtotal += $itemcount;
 3344:     return $datatable;
 3345: }
 3346: 
 3347: sub build_location_hashes {
 3348:     my ($intdoms,$by_ip,$by_location) = @_;
 3349:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3350:                   (ref($by_location) eq 'HASH')); 
 3351:     my %iphost = &Apache::lonnet::get_iphost();
 3352:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3353:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3354:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3355:         foreach my $id (@{$iphost{$primary_ip}}) {
 3356:             my $intdom = &Apache::lonnet::internet_dom($id);
 3357:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3358:                 push(@{$intdoms},$intdom);
 3359:             }
 3360:         }
 3361:     }
 3362:     foreach my $ip (keys(%iphost)) {
 3363:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3364:             foreach my $id (@{$iphost{$ip}}) {
 3365:                 my $location = &Apache::lonnet::internet_dom($id);
 3366:                 if ($location) {
 3367:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3368:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3369:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3370:                             push(@{$by_ip->{$ip}},$location);
 3371:                         }
 3372:                     } else {
 3373:                         $by_ip->{$ip} = [$location];
 3374:                     }
 3375:                 }
 3376:             }
 3377:         }
 3378:     }
 3379:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3380:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3381:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3382:             my $first = $by_ip->{$ip}->[0];
 3383:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3384:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3385:                     push(@{$by_location->{$first}},$ip);
 3386:                 }
 3387:             } else {
 3388:                 $by_location->{$first} = [$ip];
 3389:             }
 3390:         }
 3391:     }
 3392:     return;
 3393: }
 3394: 
 3395: sub current_offloads_to {
 3396:     my ($dom,$settings,$servers) = @_;
 3397:     my (%spareid,%otherdomconfigs);
 3398:     if (ref($servers) eq 'HASH') {
 3399:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3400:             my $gotspares;
 3401:             if (ref($settings) eq 'HASH') {
 3402:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3403:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3404:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3405:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3406:                         $gotspares = 1;
 3407:                     }
 3408:                 }
 3409:             }
 3410:             unless ($gotspares) {
 3411:                 my $gotspares;
 3412:                 my $serverhomeID =
 3413:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3414:                 my $serverhomedom =
 3415:                     &Apache::lonnet::host_domain($serverhomeID);
 3416:                 if ($serverhomedom ne $dom) {
 3417:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3418:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3419:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3420:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3421:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3422:                                 $gotspares = 1;
 3423:                             }
 3424:                         }
 3425:                     } else {
 3426:                         $otherdomconfigs{$serverhomedom} =
 3427:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3428:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3429:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3430:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3431:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3432:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3433:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3434:                                         $gotspares = 1;
 3435:                                     }
 3436:                                 }
 3437:                             }
 3438:                         }
 3439:                     }
 3440:                 }
 3441:             }
 3442:             unless ($gotspares) {
 3443:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3444:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3445:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3446:                } else {
 3447:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3448:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3449:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3450:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3451:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3452:                     } else {
 3453:                         my %what = (
 3454:                              spareid => 1,
 3455:                         );
 3456:                         my ($result,$returnhash) = 
 3457:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3458:                         if ($result eq 'ok') { 
 3459:                             if (ref($returnhash) eq 'HASH') {
 3460:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3461:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3462:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3463:                                 }
 3464:                             }
 3465:                         }
 3466:                     }
 3467:                 }
 3468:             }
 3469:         }
 3470:     }
 3471:     return %spareid;
 3472: }
 3473: 
 3474: sub spares_row {
 3475:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3476:     my $css_class;
 3477:     my $numinrow = 4;
 3478:     my $itemcount = 1;
 3479:     my $datatable;
 3480:     my %typetitles = &sparestype_titles();
 3481:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3482:         foreach my $server (sort(keys(%{$servers}))) {
 3483:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3484:             my ($othercontrol,$serverdom);
 3485:             if ($serverhome ne $server) {
 3486:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3487:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3488:             } else {
 3489:                 $serverdom = &Apache::lonnet::host_domain($server);
 3490:                 if ($serverdom ne $dom) {
 3491:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3492:                 }
 3493:             }
 3494:             next unless (ref($spareid->{$server}) eq 'HASH');
 3495:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3496:             $datatable .= '<tr'.$css_class.'>
 3497:                            <td rowspan="2">
 3498:                             <span class="LC_nobreak">'.
 3499:                           &mt('[_1] when busy, offloads to:'
 3500:                               ,'<b>'.$server.'</b>').
 3501:                           "\n";
 3502:             my (%current,%canselect);
 3503:             my @choices = 
 3504:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3505:             foreach my $type ('primary','default') {
 3506:                 if (ref($spareid->{$server}) eq 'HASH') {
 3507:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3508:                         my @spares = @{$spareid->{$server}{$type}};
 3509:                         if (@spares > 0) {
 3510:                             if ($othercontrol) {
 3511:                                 $current{$type} = join(', ',@spares);
 3512:                             } else {
 3513:                                 $current{$type} .= '<table>';
 3514:                                 my $numspares = scalar(@spares);
 3515:                                 for (my $i=0;  $i<@spares; $i++) {
 3516:                                     my $rem = $i%($numinrow);
 3517:                                     if ($rem == 0) {
 3518:                                         if ($i > 0) {
 3519:                                             $current{$type} .= '</tr>';
 3520:                                         }
 3521:                                         $current{$type} .= '<tr>';
 3522:                                     }
 3523:                                     $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;'.
 3524:                                                        $spareid->{$server}{$type}[$i].
 3525:                                                        '</label></td>'."\n";
 3526:                                 }
 3527:                                 my $rem = @spares%($numinrow);
 3528:                                 my $colsleft = $numinrow - $rem;
 3529:                                 if ($colsleft > 1 ) {
 3530:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3531:                                                        '" class="LC_left_item">'.
 3532:                                                        '&nbsp;</td>';
 3533:                                 } elsif ($colsleft == 1) {
 3534:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3535:                                 }
 3536:                                 $current{$type} .= '</tr></table>';
 3537:                             }
 3538:                         }
 3539:                     }
 3540:                     if ($current{$type} eq '') {
 3541:                         $current{$type} = &mt('None specified');
 3542:                     }
 3543:                     if ($othercontrol) {
 3544:                         if ($type eq 'primary') {
 3545:                             $canselect{$type} = $othercontrol;
 3546:                         }
 3547:                     } else {
 3548:                         $canselect{$type} = 
 3549:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3550:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3551:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3552:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3553:                         if (@choices > 0) {
 3554:                             foreach my $lonhost (@choices) {
 3555:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3556:                             }
 3557:                         }
 3558:                         $canselect{$type} .= '</select>'."\n";
 3559:                     }
 3560:                 } else {
 3561:                     $current{$type} = &mt('Could not be determined');
 3562:                     if ($type eq 'primary') {
 3563:                         $canselect{$type} =  $othercontrol;
 3564:                     }
 3565:                 }
 3566:                 if ($type eq 'default') {
 3567:                     $datatable .= '<tr'.$css_class.'>';
 3568:                 }
 3569:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3570:                               '<td>'.$current{$type}.'</td>'."\n".
 3571:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3572:             }
 3573:             $itemcount ++;
 3574:         }
 3575:     }
 3576:     $$rowtotal += $itemcount;
 3577:     return $datatable;
 3578: }
 3579: 
 3580: sub possible_newspares {
 3581:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3582:     my $serverhostname = &Apache::lonnet::hostname($server);
 3583:     my %excluded;
 3584:     if ($serverhostname ne '') {
 3585:         %excluded = (
 3586:                        $serverhostname => 1,
 3587:                     );
 3588:     }
 3589:     if (ref($currspares) eq 'HASH') {
 3590:         foreach my $type (keys(%{$currspares})) {
 3591:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3592:                 if (@{$currspares->{$type}} > 0) {
 3593:                     foreach my $curr (@{$currspares->{$type}}) {
 3594:                         my $hostname = &Apache::lonnet::hostname($curr);
 3595:                         $excluded{$hostname} = 1;
 3596:                     }
 3597:                 }
 3598:             }
 3599:         }
 3600:     }
 3601:     my @choices;
 3602:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3603:         if (keys(%{$serverhomes}) > 1) {
 3604:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3605:                 unless ($excluded{$name}) {
 3606:                     if (exists($altids->{$serverhomes->{$name}})) {
 3607:                         push(@choices,$altids->{$serverhomes->{$name}});
 3608:                     } else {
 3609:                         push(@choices,$serverhomes->{$name});
 3610:                     }
 3611:                 }
 3612:             }
 3613:         }
 3614:     }
 3615:     return sort(@choices);
 3616: }
 3617: 
 3618: sub print_loadbalancing {
 3619:     my ($dom,$settings,$rowtotal) = @_;
 3620:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3621:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3622:     my $numinrow = 1;
 3623:     my $datatable;
 3624:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3625:     my (%currbalancer,%currtargets,%currrules,%existing);
 3626:     if (ref($settings) eq 'HASH') {
 3627:         %existing = %{$settings};
 3628:     }
 3629:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3630:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3631:                                   \%currtargets,\%currrules);
 3632:     } else {
 3633:         return;
 3634:     }
 3635:     my ($othertitle,$usertypes,$types) =
 3636:         &Apache::loncommon::sorted_inst_types($dom);
 3637:     my $rownum = 8;
 3638:     if (ref($types) eq 'ARRAY') {
 3639:         $rownum += scalar(@{$types});
 3640:     }
 3641:     my @css_class = ('LC_odd_row','LC_even_row');
 3642:     my $balnum = 0;
 3643:     my $islast;
 3644:     my (@toshow,$disabledtext);
 3645:     if (keys(%currbalancer) > 0) {
 3646:         @toshow = sort(keys(%currbalancer));
 3647:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3648:             push(@toshow,'');
 3649:         }
 3650:     } else {
 3651:         @toshow = ('');
 3652:         $disabledtext = &mt('No existing load balancer');
 3653:     }
 3654:     foreach my $lonhost (@toshow) {
 3655:         if ($balnum == scalar(@toshow)-1) {
 3656:             $islast = 1;
 3657:         } else {
 3658:             $islast = 0;
 3659:         }
 3660:         my $cssidx = $balnum%2;
 3661:         my $targets_div_style = 'display: none';
 3662:         my $disabled_div_style = 'display: block';
 3663:         my $homedom_div_style = 'display: none';
 3664:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3665:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3666:                       '<p>';
 3667:         if ($lonhost eq '') {
 3668:             $datatable .= '<span class="LC_nobreak">';
 3669:             if (keys(%currbalancer) > 0) {
 3670:                 $datatable .= &mt('Add balancer:');
 3671:             } else {
 3672:                 $datatable .= &mt('Enable balancer:');
 3673:             }
 3674:             $datatable .= '&nbsp;'.
 3675:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3676:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3677:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3678:                           '<option value="" selected="selected">'.&mt('None').
 3679:                           '</option>'."\n";
 3680:             foreach my $server (sort(keys(%servers))) {
 3681:                 next if ($currbalancer{$server});
 3682:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3683:             }
 3684:             $datatable .=
 3685:                 '</select>'."\n".
 3686:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3687:         } else {
 3688:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3689:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3690:                            &mt('Stop balancing').'</label>'.
 3691:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3692:             $targets_div_style = 'display: block';
 3693:             $disabled_div_style = 'display: none';
 3694:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3695:                 $homedom_div_style = 'display: block';
 3696:             }
 3697:         }
 3698:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3699:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3700:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3701:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3702:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3703:         my @sparestypes = ('primary','default');
 3704:         my %typetitles = &sparestype_titles();
 3705:         foreach my $sparetype (@sparestypes) {
 3706:             my $targettable;
 3707:             for (my $i=0; $i<$numspares; $i++) {
 3708:                 my $checked;
 3709:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3710:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3711:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3712:                             $checked = ' checked="checked"';
 3713:                         }
 3714:                     }
 3715:                 }
 3716:                 my ($chkboxval,$disabled);
 3717:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3718:                     $chkboxval = $spares[$i];
 3719:                 }
 3720:                 if (exists($currbalancer{$spares[$i]})) {
 3721:                     $disabled = ' disabled="disabled"';
 3722:                 }
 3723:                 $targettable .=
 3724:                     '<td><span class="LC_nobreak"><label>'.
 3725:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3726:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3727:                     '</span></label></span></td>';
 3728:                 my $rem = $i%($numinrow);
 3729:                 if ($rem == 0) {
 3730:                     if (($i > 0) && ($i < $numspares-1)) {
 3731:                         $targettable .= '</tr>';
 3732:                     }
 3733:                     if ($i < $numspares-1) {
 3734:                         $targettable .= '<tr>';
 3735:                     }
 3736:                 }
 3737:             }
 3738:             if ($targettable ne '') {
 3739:                 my $rem = $numspares%($numinrow);
 3740:                 my $colsleft = $numinrow - $rem;
 3741:                 if ($colsleft > 1 ) {
 3742:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3743:                                     '&nbsp;</td>';
 3744:                 } elsif ($colsleft == 1) {
 3745:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3746:                 }
 3747:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3748:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3749:             }
 3750:         }
 3751:         $datatable .= '</div></td></tr>'.
 3752:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3753:                                            $othertitle,$usertypes,$types,\%servers,
 3754:                                            \%currbalancer,$lonhost,
 3755:                                            $targets_div_style,$homedom_div_style,
 3756:                                            $css_class[$cssidx],$balnum,$islast);
 3757:         $$rowtotal += $rownum;
 3758:         $balnum ++;
 3759:     }
 3760:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3761:     return $datatable;
 3762: }
 3763: 
 3764: sub get_loadbalancers_config {
 3765:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3766:     return unless ((ref($servers) eq 'HASH') &&
 3767:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3768:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3769:     if (keys(%{$existing}) > 0) {
 3770:         my $oldlonhost;
 3771:         foreach my $key (sort(keys(%{$existing}))) {
 3772:             if ($key eq 'lonhost') {
 3773:                 $oldlonhost = $existing->{'lonhost'};
 3774:                 $currbalancer->{$oldlonhost} = 1;
 3775:             } elsif ($key eq 'targets') {
 3776:                 if ($oldlonhost) {
 3777:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3778:                 }
 3779:             } elsif ($key eq 'rules') {
 3780:                 if ($oldlonhost) {
 3781:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3782:                 }
 3783:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3784:                 $currbalancer->{$key} = 1;
 3785:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3786:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3787:             }
 3788:         }
 3789:     } else {
 3790:         my ($balancerref,$targetsref) =
 3791:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3792:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3793:             foreach my $server (sort(keys(%{$balancerref}))) {
 3794:                 $currbalancer->{$server} = 1;
 3795:                 $currtargets->{$server} = $targetsref->{$server};
 3796:             }
 3797:         }
 3798:     }
 3799:     return;
 3800: }
 3801: 
 3802: sub loadbalancing_rules {
 3803:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3804:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3805:         $css_class,$balnum,$islast) = @_;
 3806:     my $output;
 3807:     my $num = 0;
 3808:     my ($alltypes,$othertypes,$titles) =
 3809:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3810:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3811:         foreach my $type (@{$alltypes}) {
 3812:             $num ++;
 3813:             my $current;
 3814:             if (ref($currrules) eq 'HASH') {
 3815:                 $current = $currrules->{$type};
 3816:             }
 3817:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3818:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3819:                     $current = '';
 3820:                 }
 3821:             }
 3822:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3823:                                              $servers,$currbalancer,$lonhost,$dom,
 3824:                                              $targets_div_style,$homedom_div_style,
 3825:                                              $css_class,$balnum,$num,$islast);
 3826:         }
 3827:     }
 3828:     return $output;
 3829: }
 3830: 
 3831: sub loadbalancing_titles {
 3832:     my ($dom,$intdom,$usertypes,$types) = @_;
 3833:     my %othertypes = (
 3834:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3835:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3836:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3837:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3838:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3839:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3840:                      );
 3841:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3842:     if (ref($types) eq 'ARRAY') {
 3843:         unshift(@alltypes,@{$types},'default');
 3844:     }
 3845:     my %titles;
 3846:     foreach my $type (@alltypes) {
 3847:         if ($type =~ /^_LC_/) {
 3848:             $titles{$type} = $othertypes{$type};
 3849:         } elsif ($type eq 'default') {
 3850:             $titles{$type} = &mt('All users from [_1]',$dom);
 3851:             if (ref($types) eq 'ARRAY') {
 3852:                 if (@{$types} > 0) {
 3853:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3854:                 }
 3855:             }
 3856:         } elsif (ref($usertypes) eq 'HASH') {
 3857:             $titles{$type} = $usertypes->{$type};
 3858:         }
 3859:     }
 3860:     return (\@alltypes,\%othertypes,\%titles);
 3861: }
 3862: 
 3863: sub loadbalance_rule_row {
 3864:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3865:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3866:     my @rulenames;
 3867:     my %ruletitles = &offloadtype_text();
 3868:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3869:         @rulenames = ('balancer','offloadedto','specific');
 3870:     } else {
 3871:         @rulenames = ('default','homeserver');
 3872:         if ($type eq '_LC_external') {
 3873:             push(@rulenames,'externalbalancer');
 3874:         } else {
 3875:             push(@rulenames,'specific');
 3876:         }
 3877:         push(@rulenames,'none');
 3878:     }
 3879:     my $style = $targets_div_style;
 3880:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3881:         $style = $homedom_div_style;
 3882:     }
 3883:     my $space;
 3884:     if ($islast && $num == 1) {
 3885:         $space = '<div display="inline-block">&nbsp;</div>';
 3886:     }
 3887:     my $output =
 3888:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3889:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3890:         '<td valaign="top">'.$space.
 3891:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3892:     for (my $i=0; $i<@rulenames; $i++) {
 3893:         my $rule = $rulenames[$i];
 3894:         my ($checked,$extra);
 3895:         if ($rulenames[$i] eq 'default') {
 3896:             $rule = '';
 3897:         }
 3898:         if ($rulenames[$i] eq 'specific') {
 3899:             if (ref($servers) eq 'HASH') {
 3900:                 my $default;
 3901:                 if (($current ne '') && (exists($servers->{$current}))) {
 3902:                     $checked = ' checked="checked"';
 3903:                 }
 3904:                 unless ($checked) {
 3905:                     $default = ' selected="selected"';
 3906:                 }
 3907:                 $extra =
 3908:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3909:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3910:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3911:                     '<option value=""'.$default.'></option>'."\n";
 3912:                 foreach my $server (sort(keys(%{$servers}))) {
 3913:                     if (ref($currbalancer) eq 'HASH') {
 3914:                         next if (exists($currbalancer->{$server}));
 3915:                     }
 3916:                     my $selected;
 3917:                     if ($server eq $current) {
 3918:                         $selected = ' selected="selected"';
 3919:                     }
 3920:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3921:                 }
 3922:                 $extra .= '</select>';
 3923:             }
 3924:         } elsif ($rule eq $current) {
 3925:             $checked = ' checked="checked"';
 3926:         }
 3927:         $output .= '<span class="LC_nobreak"><label>'.
 3928:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3929:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3930:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3931:                    ')"'.$checked.' />&nbsp;';
 3932:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 3933:             $output .= $ruletitles{'particular'};
 3934:         } else {
 3935:             $output .= $ruletitles{$rulenames[$i]};
 3936:         }
 3937:         $output .= '</label>'.$extra.'</span><br />'."\n";
 3938:     }
 3939:     $output .= '</div></td></tr>'."\n";
 3940:     return $output;
 3941: }
 3942: 
 3943: sub offloadtype_text {
 3944:     my %ruletitles = &Apache::lonlocal::texthash (
 3945:            'default'          => 'Offloads to default destinations',
 3946:            'homeserver'       => "Offloads to user's home server",
 3947:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3948:            'specific'         => 'Offloads to specific server',
 3949:            'none'             => 'No offload',
 3950:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3951:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3952:            'particular'       => 'Session hosted (after re-auth) on server:',
 3953:     );
 3954:     return %ruletitles;
 3955: }
 3956: 
 3957: sub sparestype_titles {
 3958:     my %typestitles = &Apache::lonlocal::texthash (
 3959:                           'primary' => 'primary',
 3960:                           'default' => 'default',
 3961:                       );
 3962:     return %typestitles;
 3963: }
 3964: 
 3965: sub contact_titles {
 3966:     my %titles = &Apache::lonlocal::texthash (
 3967:                    'supportemail' => 'Support E-mail address',
 3968:                    'adminemail'   => 'Default Server Admin E-mail address',
 3969:                    'errormail'    => 'Error reports to be e-mailed to',
 3970:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3971:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3972:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3973:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3974:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3975:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3976:                  );
 3977:     my %short_titles = &Apache::lonlocal::texthash (
 3978:                            adminemail   => 'Admin E-mail address',
 3979:                            supportemail => 'Support E-mail',
 3980:                        );   
 3981:     return (\%titles,\%short_titles);
 3982: }
 3983: 
 3984: sub tool_titles {
 3985:     my %titles = &Apache::lonlocal::texthash (
 3986:                      aboutme    => 'Personal web page',
 3987:                      blog       => 'Blog',
 3988:                      webdav     => 'WebDAV',
 3989:                      portfolio  => 'Portfolio',
 3990:                      official   => 'Official courses (with institutional codes)',
 3991:                      unofficial => 'Unofficial courses',
 3992:                      community  => 'Communities',
 3993:                      textbook   => 'Textbook courses',
 3994:                  );
 3995:     return %titles;
 3996: }
 3997: 
 3998: sub courserequest_titles {
 3999:     my %titles = &Apache::lonlocal::texthash (
 4000:                                    official   => 'Official',
 4001:                                    unofficial => 'Unofficial',
 4002:                                    community  => 'Communities',
 4003:                                    textbook   => 'Textbook',
 4004:                                    norequest  => 'Not allowed',
 4005:                                    approval   => 'Approval by Dom. Coord.',
 4006:                                    validate   => 'With validation',
 4007:                                    autolimit  => 'Numerical limit',
 4008:                                    unlimited  => '(blank for unlimited)',
 4009:                  );
 4010:     return %titles;
 4011: }
 4012: 
 4013: sub authorrequest_titles {
 4014:     my %titles = &Apache::lonlocal::texthash (
 4015:                                    norequest  => 'Not allowed',
 4016:                                    approval   => 'Approval by Dom. Coord.',
 4017:                                    automatic  => 'Automatic approval',
 4018:                  );
 4019:     return %titles;
 4020: }
 4021: 
 4022: sub courserequest_conditions {
 4023:     my %conditions = &Apache::lonlocal::texthash (
 4024:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 4025:        validate   => '(Processing of request subject to institutional validation).',
 4026:                  );
 4027:     return %conditions;
 4028: }
 4029: 
 4030: 
 4031: sub print_usercreation {
 4032:     my ($position,$dom,$settings,$rowtotal) = @_;
 4033:     my $numinrow = 4;
 4034:     my $datatable;
 4035:     if ($position eq 'top') {
 4036:         $$rowtotal ++;
 4037:         my $rowcount = 0;
 4038:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 4039:         if (ref($rules) eq 'HASH') {
 4040:             if (keys(%{$rules}) > 0) {
 4041:                 $datatable .= &user_formats_row('username',$settings,$rules,
 4042:                                                 $ruleorder,$numinrow,$rowcount);
 4043:                 $$rowtotal ++;
 4044:                 $rowcount ++;
 4045:             }
 4046:         }
 4047:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 4048:         if (ref($idrules) eq 'HASH') {
 4049:             if (keys(%{$idrules}) > 0) {
 4050:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 4051:                                                 $idruleorder,$numinrow,$rowcount);
 4052:                 $$rowtotal ++;
 4053:                 $rowcount ++;
 4054:             }
 4055:         }
 4056:         if ($rowcount == 0) {
 4057:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 4058:             $$rowtotal ++;
 4059:             $rowcount ++;
 4060:         }
 4061:     } elsif ($position eq 'middle') {
 4062:         my @creators = ('author','course','requestcrs');
 4063:         my ($rules,$ruleorder) =
 4064:             &Apache::lonnet::inst_userrules($dom,'username');
 4065:         my %lt = &usercreation_types();
 4066:         my %checked;
 4067:         if (ref($settings) eq 'HASH') {
 4068:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 4069:                 foreach my $item (@creators) {
 4070:                     $checked{$item} = $settings->{'cancreate'}{$item};
 4071:                 }
 4072:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 4073:                 foreach my $item (@creators) {
 4074:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 4075:                         $checked{$item} = 'none';
 4076:                     }
 4077:                 }
 4078:             }
 4079:         }
 4080:         my $rownum = 0;
 4081:         foreach my $item (@creators) {
 4082:             $rownum ++;
 4083:             if ($checked{$item} eq '') {
 4084:                 $checked{$item} = 'any';
 4085:             }
 4086:             my $css_class;
 4087:             if ($rownum%2) {
 4088:                 $css_class = '';
 4089:             } else {
 4090:                 $css_class = ' class="LC_odd_row" ';
 4091:             }
 4092:             $datatable .= '<tr'.$css_class.'>'.
 4093:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 4094:                          '</span></td><td align="right">';
 4095:             my @options = ('any');
 4096:             if (ref($rules) eq 'HASH') {
 4097:                 if (keys(%{$rules}) > 0) {
 4098:                     push(@options,('official','unofficial'));
 4099:                 }
 4100:             }
 4101:             push(@options,'none');
 4102:             foreach my $option (@options) {
 4103:                 my $type = 'radio';
 4104:                 my $check = ' ';
 4105:                 if ($checked{$item} eq $option) {
 4106:                     $check = ' checked="checked" ';
 4107:                 } 
 4108:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4109:                               '<input type="'.$type.'" name="can_createuser_'.
 4110:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 4111:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 4112:             }
 4113:             $datatable .= '</td></tr>';
 4114:         }
 4115:     } else {
 4116:         my @contexts = ('author','course','domain');
 4117:         my @authtypes = ('int','krb4','krb5','loc');
 4118:         my %checked;
 4119:         if (ref($settings) eq 'HASH') {
 4120:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 4121:                 foreach my $item (@contexts) {
 4122:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 4123:                         foreach my $auth (@authtypes) {
 4124:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 4125:                                 $checked{$item}{$auth} = ' checked="checked" ';
 4126:                             }
 4127:                         }
 4128:                     }
 4129:                 }
 4130:             }
 4131:         } else {
 4132:             foreach my $item (@contexts) {
 4133:                 foreach my $auth (@authtypes) {
 4134:                     $checked{$item}{$auth} = ' checked="checked" ';
 4135:                 }
 4136:             }
 4137:         }
 4138:         my %title = &context_names();
 4139:         my %authname = &authtype_names();
 4140:         my $rownum = 0;
 4141:         my $css_class; 
 4142:         foreach my $item (@contexts) {
 4143:             if ($rownum%2) {
 4144:                 $css_class = '';
 4145:             } else {
 4146:                 $css_class = ' class="LC_odd_row" ';
 4147:             }
 4148:             $datatable .=   '<tr'.$css_class.'>'.
 4149:                             '<td>'.$title{$item}.
 4150:                             '</td><td class="LC_left_item">'.
 4151:                             '<span class="LC_nobreak">';
 4152:             foreach my $auth (@authtypes) {
 4153:                 $datatable .= '<label>'. 
 4154:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4155:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4156:                               $authname{$auth}.'</label>&nbsp;';
 4157:             }
 4158:             $datatable .= '</span></td></tr>';
 4159:             $rownum ++;
 4160:         }
 4161:         $$rowtotal += $rownum;
 4162:     }
 4163:     return $datatable;
 4164: }
 4165: 
 4166: sub print_selfcreation {
 4167:     my ($position,$dom,$settings,$rowtotal) = @_;
 4168:     my (@selfcreate,$createsettings,$processing,$datatable);
 4169:     if (ref($settings) eq 'HASH') {
 4170:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4171:             $createsettings = $settings->{'cancreate'};
 4172:             if (ref($createsettings) eq 'HASH') {
 4173:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4174:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4175:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4176:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4177:                         @selfcreate = ('email','login','sso');
 4178:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4179:                         @selfcreate = ($createsettings->{'selfcreate'});
 4180:                     }
 4181:                 }
 4182:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4183:                     $processing = $createsettings->{'selfcreateprocessing'};
 4184:                 }
 4185:             }
 4186:         }
 4187:     }
 4188:     my %radiohash;
 4189:     my $numinrow = 4;
 4190:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4191:     if ($position eq 'top') {
 4192:         my %choices = &Apache::lonlocal::texthash (
 4193:                                                       cancreate_login      => 'Institutional Login',
 4194:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4195:                                                   );
 4196:         my @toggles = sort(keys(%choices));
 4197:         my %defaultchecked = (
 4198:                                'cancreate_login' => 'off',
 4199:                                'cancreate_sso'   => 'off',
 4200:                              );
 4201:         my ($onclick,$itemcount);
 4202:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4203:                                                      \%choices,$itemcount,$onclick);
 4204:         $$rowtotal += $itemcount;
 4205:         
 4206:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4207: 
 4208:         if (ref($usertypes) eq 'HASH') {
 4209:             if (keys(%{$usertypes}) > 0) {
 4210:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4211:                                              $dom,$numinrow,$othertitle,
 4212:                                              'statustocreate',$$rowtotal);
 4213:                 $$rowtotal ++;
 4214:             }
 4215:         }
 4216:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 4217:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4218:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 4219:         my $rem;
 4220:         my $numperrow = 2;
 4221:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4222:         $datatable .= '<tr'.$css_class.'>'.
 4223:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 4224:                      '<td class="LC_left_item">'."\n".
 4225:                      '<table><tr><td>'."\n";
 4226:         for (my $i=0; $i<@fields; $i++) {
 4227:             $rem = $i%($numperrow);
 4228:             if ($rem == 0) {
 4229:                 if ($i > 0) {
 4230:                     $datatable .= '</tr>';
 4231:                 }
 4232:                 $datatable .= '<tr>';
 4233:             }
 4234:             my $currval;
 4235:             if (ref($createsettings) eq 'HASH') {
 4236:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 4237:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 4238:                 }
 4239:             }
 4240:             $datatable .= '<td class="LC_left_item">'.
 4241:                           '<span class="LC_nobreak">'.
 4242:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 4243:                           'value="'.$currval.'" size="10" />&nbsp;'.
 4244:                           $fieldtitles{$fields[$i]}.'</span></td>';
 4245:         }
 4246:         my $colsleft = $numperrow - $rem;
 4247:         if ($colsleft > 1 ) {
 4248:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4249:                          '&nbsp;</td>';
 4250:         } elsif ($colsleft == 1) {
 4251:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4252:         }
 4253:         $datatable .= '</tr></table></td></tr>';
 4254:         $$rowtotal ++;
 4255:     } elsif ($position eq 'middle') {
 4256:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4257:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4258:         $usertypes->{'default'} = $othertitle;
 4259:         if (ref($types) eq 'ARRAY') {
 4260:             push(@{$types},'default');
 4261:             $usertypes->{'default'} = $othertitle;
 4262:             foreach my $status (@{$types}) {
 4263:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4264:                                                        $numinrow,$$rowtotal,$usertypes);
 4265:                 $$rowtotal ++;
 4266:             }
 4267:         }
 4268:     } else {
 4269:         my %choices = &Apache::lonlocal::texthash (
 4270:                                                       cancreate_email => 'E-mail address as username',
 4271:                                                   );
 4272:         my @toggles = sort(keys(%choices));
 4273:         my %defaultchecked = (
 4274:                                'cancreate_email' => 'off',
 4275:                              );
 4276:         my $itemcount = 0;
 4277:         my $display = 'none';
 4278:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 4279:             $display = 'block';
 4280:         }
 4281:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 4282:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 4283:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4284:         my $usertypes = {};
 4285:         my $order = [];
 4286:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 4287:             $usertypes = $domdefaults{'inststatustypes'};
 4288:             $order = $domdefaults{'inststatusguest'};
 4289:         }
 4290:         if (ref($order) eq 'ARRAY') {
 4291:             push(@{$order},'default');
 4292:             if (@{$order} > 1) {
 4293:                 $usertypes->{'default'} = &mt('Other users');
 4294:                 $additional .= '<table><tr>';
 4295:                 foreach my $status (@{$order}) {
 4296:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 4297:                 }
 4298:                 $additional .= '</tr><tr>';
 4299:                 foreach my $status (@{$order}) {
 4300:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 4301:                 }
 4302:                 $additional .= '</tr></table>';
 4303:             } else {
 4304:                 $usertypes->{'default'} = &mt('All users');
 4305:                 $additional .= &email_as_username($rowtotal,$processing);
 4306:             }
 4307:         }
 4308:         $additional .= '</div>'."\n";
 4309: 
 4310:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4311:                                                      \%choices,$$rowtotal,$onclick,$additional);
 4312:         $$rowtotal ++;
 4313:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4314:         $$rowtotal ++;
 4315:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4316:         $numinrow = 1;
 4317:         if (ref($order) eq 'ARRAY') {
 4318:             foreach my $status (@{$order}) {
 4319:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4320:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4321:                 $$rowtotal ++;
 4322:             }
 4323:         }
 4324:         my ($emailrules,$emailruleorder) =
 4325:             &Apache::lonnet::inst_userrules($dom,'email');
 4326:         if (ref($emailrules) eq 'HASH') {
 4327:             if (keys(%{$emailrules}) > 0) {
 4328:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4329:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4330:                 $$rowtotal ++;
 4331:             }
 4332:         }
 4333:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4334:     }
 4335:     return $datatable;
 4336: }
 4337: 
 4338: sub email_as_username {
 4339:     my ($rowtotal,$processing,$type) = @_;
 4340:     my %choices =
 4341:         &Apache::lonlocal::texthash (
 4342:                                       automatic => 'Automatic approval',
 4343:                                       approval  => 'Queued for approval',
 4344:                                     );
 4345:     my $output;
 4346:     foreach my $option ('automatic','approval') {
 4347:         my $checked;
 4348:         if (ref($processing) eq 'HASH') {
 4349:             if ($type eq '') {   
 4350:                 if (!exists($processing->{'default'})) {
 4351:                     if ($option eq 'automatic') {
 4352:                         $checked = ' checked="checked"';
 4353:                     }
 4354:                 } else {
 4355:                     if ($processing->{'default'} eq $option) {
 4356:                         $checked = ' checked="checked"';
 4357:                     }
 4358:                 }
 4359:             } else {
 4360:                 if (!exists($processing->{$type})) {
 4361:                     if ($option eq 'automatic') {
 4362:                         $checked = ' checked="checked"';
 4363:                     }
 4364:                 } else {
 4365:                     if ($processing->{$type} eq $option) {
 4366:                         $checked = ' checked="checked"';
 4367:                     }
 4368:                 }
 4369:             }
 4370:         } elsif ($option eq 'automatic') {
 4371:             $checked = ' checked="checked"'; 
 4372:         }
 4373:         my $name = 'cancreate_emailprocess';
 4374:         if (($type ne '') && ($type ne 'default')) {
 4375:             $name .= '_'.$type;
 4376:         }
 4377:         $output .= '<span class="LC_nobreak"><label>'.
 4378:                    '<input type="radio" name="'.$name.'"'.
 4379:                    $checked.' value="'.$option.'" />'.
 4380:                    $choices{$option}.'</label></span>';
 4381:         if ($type eq '') {
 4382:             $output .= '&nbsp;';
 4383:         } else {
 4384:             $output .= '<br />';
 4385:         }
 4386:     }
 4387:     $$rowtotal ++;
 4388:     return $output;
 4389: }
 4390: 
 4391: sub captcha_choice {
 4392:     my ($context,$settings,$itemcount) = @_;
 4393:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4394:     my %lt = &captcha_phrases();
 4395:     $keyentry = 'hidden';
 4396:     if ($context eq 'cancreate') {
 4397:         $rowname = &mt('CAPTCHA validation');
 4398:     } elsif ($context eq 'login') {
 4399:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4400:     }
 4401:     if (ref($settings) eq 'HASH') {
 4402:         if ($settings->{'captcha'}) {
 4403:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4404:         } else {
 4405:             $checked{'original'} = ' checked="checked"';
 4406:         }
 4407:         if ($settings->{'captcha'} eq 'recaptcha') {
 4408:             $pubtext = $lt{'pub'};
 4409:             $privtext = $lt{'priv'};
 4410:             $keyentry = 'text';
 4411:         }
 4412:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4413:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4414:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4415:         }
 4416:     } else {
 4417:         $checked{'original'} = ' checked="checked"';
 4418:     }
 4419:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4420:     my $output = '<tr'.$css_class.'>'.
 4421:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4422:                  '<table><tr><td>'."\n";
 4423:     foreach my $option ('original','recaptcha','notused') {
 4424:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4425:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4426:                    $lt{$option}.'</label></span>';
 4427:         unless ($option eq 'notused') {
 4428:             $output .= ('&nbsp;'x2)."\n";
 4429:         }
 4430:     }
 4431: #
 4432: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4433: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4434: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4435: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4436: #
 4437:     $output .= '</td></tr>'."\n".
 4438:                '<tr><td>'."\n".
 4439:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4440:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4441:                $currpub.'" size="40" /></span><br />'."\n".
 4442:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4443:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4444:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4445:                '</td></tr>';
 4446:     return $output;
 4447: }
 4448: 
 4449: sub user_formats_row {
 4450:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4451:     my $output;
 4452:     my %text = (
 4453:                    'username' => 'new usernames',
 4454:                    'id'       => 'IDs',
 4455:                    'email'    => 'self-created accounts (e-mail)',
 4456:                );
 4457:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4458:     $output = '<tr '.$css_class.'>'.
 4459:               '<td><span class="LC_nobreak">';
 4460:     if ($type eq 'email') {
 4461:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4462:     } else {
 4463:         $output .= &mt("Format rules to check for $text{$type}: ");
 4464:     }
 4465:     $output .= '</span></td>'.
 4466:                '<td class="LC_left_item" colspan="2"><table>';
 4467:     my $rem;
 4468:     if (ref($ruleorder) eq 'ARRAY') {
 4469:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4470:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4471:                 my $rem = $i%($numinrow);
 4472:                 if ($rem == 0) {
 4473:                     if ($i > 0) {
 4474:                         $output .= '</tr>';
 4475:                     }
 4476:                     $output .= '<tr>';
 4477:                 }
 4478:                 my $check = ' ';
 4479:                 if (ref($settings) eq 'HASH') {
 4480:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4481:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4482:                             $check = ' checked="checked" ';
 4483:                         }
 4484:                     }
 4485:                 }
 4486:                 $output .= '<td class="LC_left_item">'.
 4487:                            '<span class="LC_nobreak"><label>'.
 4488:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4489:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4490:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4491:             }
 4492:         }
 4493:         $rem = @{$ruleorder}%($numinrow);
 4494:     }
 4495:     my $colsleft = $numinrow - $rem;
 4496:     if ($colsleft > 1 ) {
 4497:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4498:                    '&nbsp;</td>';
 4499:     } elsif ($colsleft == 1) {
 4500:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4501:     }
 4502:     $output .= '</tr></table></td></tr>';
 4503:     return $output;
 4504: }
 4505: 
 4506: sub usercreation_types {
 4507:     my %lt = &Apache::lonlocal::texthash (
 4508:                     author     => 'When adding a co-author',
 4509:                     course     => 'When adding a user to a course',
 4510:                     requestcrs => 'When requesting a course',
 4511:                     any        => 'Any',
 4512:                     official   => 'Institutional only ',
 4513:                     unofficial => 'Non-institutional only',
 4514:                     none       => 'None',
 4515:     );
 4516:     return %lt;
 4517: }
 4518: 
 4519: sub selfcreation_types {
 4520:     my %lt = &Apache::lonlocal::texthash (
 4521:                     selfcreate => 'User creates own account',
 4522:                     any        => 'Any',
 4523:                     official   => 'Institutional only ',
 4524:                     unofficial => 'Non-institutional only',
 4525:                     email      => 'E-mail address',
 4526:                     login      => 'Institutional Login',
 4527:                     sso        => 'SSO',
 4528:              );
 4529: }
 4530: 
 4531: sub authtype_names {
 4532:     my %lt = &Apache::lonlocal::texthash(
 4533:                       int    => 'Internal',
 4534:                       krb4   => 'Kerberos 4',
 4535:                       krb5   => 'Kerberos 5',
 4536:                       loc    => 'Local',
 4537:                   );
 4538:     return %lt;
 4539: }
 4540: 
 4541: sub context_names {
 4542:     my %context_title = &Apache::lonlocal::texthash(
 4543:        author => 'Creating users when an Author',
 4544:        course => 'Creating users when in a course',
 4545:        domain => 'Creating users when a Domain Coordinator',
 4546:     );
 4547:     return %context_title;
 4548: }
 4549: 
 4550: sub print_usermodification {
 4551:     my ($position,$dom,$settings,$rowtotal) = @_;
 4552:     my $numinrow = 4;
 4553:     my ($context,$datatable,$rowcount);
 4554:     if ($position eq 'top') {
 4555:         $rowcount = 0;
 4556:         $context = 'author'; 
 4557:         foreach my $role ('ca','aa') {
 4558:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4559:                                                    $numinrow,$rowcount);
 4560:             $$rowtotal ++;
 4561:             $rowcount ++;
 4562:         }
 4563:     } elsif ($position eq 'bottom') {
 4564:         $context = 'course';
 4565:         $rowcount = 0;
 4566:         foreach my $role ('st','ep','ta','in','cr') {
 4567:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4568:                                                    $numinrow,$rowcount);
 4569:             $$rowtotal ++;
 4570:             $rowcount ++;
 4571:         }
 4572:     }
 4573:     return $datatable;
 4574: }
 4575: 
 4576: sub print_defaults {
 4577:     my ($position,$dom,$settings,$rowtotal) = @_;
 4578:     my $rownum = 0;
 4579:     my ($datatable,$css_class);
 4580:     if ($position eq 'top') {
 4581:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4582:                      'datelocale_def','portal_def');
 4583:         my %defaults;
 4584:         if (ref($settings) eq 'HASH') {
 4585:             %defaults = %{$settings};
 4586:         } else {
 4587:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4588:             foreach my $item (@items) {
 4589:                 $defaults{$item} = $domdefaults{$item};
 4590:             }
 4591:         }
 4592:         my $titles = &defaults_titles($dom);
 4593:         foreach my $item (@items) {
 4594:             if ($rownum%2) {
 4595:                 $css_class = '';
 4596:             } else {
 4597:                 $css_class = ' class="LC_odd_row" ';
 4598:             }
 4599:             $datatable .= '<tr'.$css_class.'>'.
 4600:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4601:                           '</span></td><td class="LC_right_item" colspan="3">';
 4602:             if ($item eq 'auth_def') {
 4603:                 my @authtypes = ('internal','krb4','krb5','localauth');
 4604:                 my %shortauth = (
 4605:                                  internal => 'int',
 4606:                                  krb4 => 'krb4',
 4607:                                  krb5 => 'krb5',
 4608:                                  localauth  => 'loc'
 4609:                                 );
 4610:                 my %authnames = &authtype_names();
 4611:                 foreach my $auth (@authtypes) {
 4612:                     my $checked = ' ';
 4613:                     if ($defaults{$item} eq $auth) {
 4614:                         $checked = ' checked="checked" ';
 4615:                     }
 4616:                     $datatable .= '<label><input type="radio" name="'.$item.
 4617:                                   '" value="'.$auth.'"'.$checked.'/>'.
 4618:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4619:                 }
 4620:             } elsif ($item eq 'timezone_def') {
 4621:                 my $includeempty = 1;
 4622:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4623:             } elsif ($item eq 'datelocale_def') {
 4624:                 my $includeempty = 1;
 4625:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4626:             } elsif ($item eq 'lang_def') {
 4627:                 my %langchoices = &get_languages_hash();
 4628:                 $langchoices{''} = 'No language preference';
 4629:                 %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4630:                 $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4631:                                                               \%langchoices);
 4632:             } else {
 4633:                 my $size;
 4634:                 if ($item eq 'portal_def') {
 4635:                     $size = ' size="25"';
 4636:                 }
 4637:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4638:                               $defaults{$item}.'"'.$size.' />';
 4639:             }
 4640:             $datatable .= '</td></tr>';
 4641:             $rownum ++;
 4642:         }
 4643:     } else {
 4644:         my (%defaults);
 4645:         if (ref($settings) eq 'HASH') {
 4646:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 4647:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 4648:                 my $maxnum = @{$settings->{'inststatusorder'}};
 4649:                 for (my $i=0; $i<$maxnum; $i++) {
 4650:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 4651:                     my $item = $settings->{'inststatusorder'}->[$i];
 4652:                     my $title = $settings->{'inststatustypes'}->{$item};
 4653:                     my $guestok;
 4654:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 4655:                         $guestok = 1;
 4656:                     }
 4657:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 4658:                     $datatable .= '<tr'.$css_class.'>'.
 4659:                                   '<td><span class="LC_nobreak">'.
 4660:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 4661:                     for (my $k=0; $k<=$maxnum; $k++) {
 4662:                         my $vpos = $k+1;
 4663:                         my $selstr;
 4664:                         if ($k == $i) {
 4665:                             $selstr = ' selected="selected" ';
 4666:                         }
 4667:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4668:                     }
 4669:                     my ($checkedon,$checkedoff);
 4670:                     $checkedoff = ' checked="checked"';
 4671:                     if ($guestok) {
 4672:                         $checkedon = $checkedoff;
 4673:                         $checkedoff = ''; 
 4674:                     }
 4675:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 4676:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 4677:                                   &mt('delete').'</span></td>'.
 4678:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 4679:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 4680:                                   '</span></td>'.
 4681:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4682:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 4683:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 4684:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 4685:                                   &mt('No').'</label></span></td></tr>';
 4686:                 }
 4687:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 4688:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 4689:                 $datatable .= '<tr '.$css_class.'>'.
 4690:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 4691:                 for (my $k=0; $k<=$maxnum; $k++) {
 4692:                     my $vpos = $k+1;
 4693:                     my $selstr;
 4694:                     if ($k == $maxnum) {
 4695:                         $selstr = ' selected="selected" ';
 4696:                     }
 4697:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4698:                 }
 4699:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 4700:                               '<input type="text" size="10" name="addinststatus" value="" /></span>'.
 4701:                               '&nbsp;'.&mt('(new)').
 4702:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 4703:                               &mt('Name displayed:').
 4704:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 4705:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 4706:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 4707:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 4708:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 4709:                               &mt('No').'</label></span></td></tr>';
 4710:                               '</tr>'."\n";
 4711:                 $rownum ++;
 4712:             }
 4713:         }
 4714:     }
 4715:     $$rowtotal += $rownum;
 4716:     return $datatable;
 4717: }
 4718: 
 4719: sub get_languages_hash {
 4720:     my %langchoices;
 4721:     foreach my $id (&Apache::loncommon::languageids()) {
 4722:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4723:         if ($code ne '') {
 4724:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4725:         }
 4726:     }
 4727:     return %langchoices;
 4728: }
 4729: 
 4730: sub defaults_titles {
 4731:     my ($dom) = @_;
 4732:     my %titles = &Apache::lonlocal::texthash (
 4733:                    'auth_def'      => 'Default authentication type',
 4734:                    'auth_arg_def'  => 'Default authentication argument',
 4735:                    'lang_def'      => 'Default language',
 4736:                    'timezone_def'  => 'Default timezone',
 4737:                    'datelocale_def' => 'Default locale for dates',
 4738:                    'portal_def'     => 'Portal/Default URL',
 4739:                  );
 4740:     if ($dom) {
 4741:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4742:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4743:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4744:         $protocol = 'http' if ($protocol ne 'https');
 4745:         if ($uint_dom) {
 4746:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4747:                                          $uint_dom);
 4748:         }
 4749:     }
 4750:     return (\%titles);
 4751: }
 4752: 
 4753: sub print_scantronformat {
 4754:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4755:     my $itemcount = 1;
 4756:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4757:         %confhash);
 4758:     my $switchserver = &check_switchserver($dom,$confname);
 4759:     my %lt = &Apache::lonlocal::texthash (
 4760:                 default => 'Default bubblesheet format file error',
 4761:                 custom  => 'Custom bubblesheet format file error',
 4762:              );
 4763:     my %scantronfiles = (
 4764:         default => 'default.tab',
 4765:         custom => 'custom.tab',
 4766:     );
 4767:     foreach my $key (keys(%scantronfiles)) {
 4768:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4769:                               .$scantronfiles{$key};
 4770:     }
 4771:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4772:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4773:         if (!$switchserver) {
 4774:             my $servadm = $r->dir_config('lonAdmEMail');
 4775:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4776:             if ($configuserok eq 'ok') {
 4777:                 if ($author_ok eq 'ok') {
 4778:                     my %legacyfile = (
 4779:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4780:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4781:                     );
 4782:                     my %md5chk;
 4783:                     foreach my $type (keys(%legacyfile)) {
 4784:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4785:                         chomp($md5chk{$type});
 4786:                     }
 4787:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4788:                         foreach my $type (keys(%legacyfile)) {
 4789:                             ($scantronurls{$type},my $error) = 
 4790:                                 &legacy_scantronformat($r,$dom,$confname,
 4791:                                                  $type,$legacyfile{$type},
 4792:                                                  $scantronurls{$type},
 4793:                                                  $scantronfiles{$type});
 4794:                             if ($error ne '') {
 4795:                                 $error{$type} = $error;
 4796:                             }
 4797:                         }
 4798:                         if (keys(%error) == 0) {
 4799:                             $is_custom = 1;
 4800:                             $confhash{'scantron'}{'scantronformat'} = 
 4801:                                 $scantronurls{'custom'};
 4802:                             my $putresult = 
 4803:                                 &Apache::lonnet::put_dom('configuration',
 4804:                                                          \%confhash,$dom);
 4805:                             if ($putresult ne 'ok') {
 4806:                                 $error{'custom'} = 
 4807:                                     '<span class="LC_error">'.
 4808:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4809:                             }
 4810:                         }
 4811:                     } else {
 4812:                         ($scantronurls{'default'},my $error) =
 4813:                             &legacy_scantronformat($r,$dom,$confname,
 4814:                                           'default',$legacyfile{'default'},
 4815:                                           $scantronurls{'default'},
 4816:                                           $scantronfiles{'default'});
 4817:                         if ($error eq '') {
 4818:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4819:                             my $putresult =
 4820:                                 &Apache::lonnet::put_dom('configuration',
 4821:                                                          \%confhash,$dom);
 4822:                             if ($putresult ne 'ok') {
 4823:                                 $error{'default'} =
 4824:                                     '<span class="LC_error">'.
 4825:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4826:                             }
 4827:                         } else {
 4828:                             $error{'default'} = $error;
 4829:                         }
 4830:                     }
 4831:                 }
 4832:             }
 4833:         } else {
 4834:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4835:         }
 4836:     }
 4837:     if (ref($settings) eq 'HASH') {
 4838:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4839:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4840:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4841:                 $scantronurl = '';
 4842:             } else {
 4843:                 $scantronurl = $settings->{'scantronformat'};
 4844:             }
 4845:             $is_custom = 1;
 4846:         } else {
 4847:             $scantronurl = $scantronurls{'default'};
 4848:         }
 4849:     } else {
 4850:         if ($is_custom) {
 4851:             $scantronurl = $scantronurls{'custom'};
 4852:         } else {
 4853:             $scantronurl = $scantronurls{'default'};
 4854:         }
 4855:     }
 4856:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4857:     $datatable .= '<tr'.$css_class.'>';
 4858:     if (!$is_custom) {
 4859:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4860:                       '<span class="LC_nobreak">';
 4861:         if ($scantronurl) {
 4862:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4863:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4864:         } else {
 4865:             $datatable = &mt('File unavailable for display');
 4866:         }
 4867:         $datatable .= '</span></td>';
 4868:         if (keys(%error) == 0) { 
 4869:             $datatable .= '<td valign="bottom">';
 4870:             if (!$switchserver) {
 4871:                 $datatable .= &mt('Upload:').'<br />';
 4872:             }
 4873:         } else {
 4874:             my $errorstr;
 4875:             foreach my $key (sort(keys(%error))) {
 4876:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4877:             }
 4878:             $datatable .= '<td>'.$errorstr;
 4879:         }
 4880:     } else {
 4881:         if (keys(%error) > 0) {
 4882:             my $errorstr;
 4883:             foreach my $key (sort(keys(%error))) {
 4884:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4885:             } 
 4886:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4887:         } elsif ($scantronurl) {
 4888:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4889:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4890:             $datatable .= '<td><span class="LC_nobreak">'.
 4891:                           $link.
 4892:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4893:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4894:                           '<td><span class="LC_nobreak">&nbsp;'.
 4895:                           &mt('Replace:').'</span><br />';
 4896:         }
 4897:     }
 4898:     if (keys(%error) == 0) {
 4899:         if ($switchserver) {
 4900:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4901:         } else {
 4902:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4903:                          '<input type="file" name="scantronformat" /></span>';
 4904:         }
 4905:     }
 4906:     $datatable .= '</td></tr>';
 4907:     $$rowtotal ++;
 4908:     return $datatable;
 4909: }
 4910: 
 4911: sub legacy_scantronformat {
 4912:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4913:     my ($url,$error);
 4914:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4915:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4916:         (my $result,$url) =
 4917:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4918:                          '','',$newfile);
 4919:         if ($result ne 'ok') {
 4920:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4921:         }
 4922:     }
 4923:     return ($url,$error);
 4924: }
 4925: 
 4926: sub print_coursecategories {
 4927:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4928:     my $datatable;
 4929:     if ($position eq 'top') {
 4930:         my (%checked);
 4931:         my @catitems = ('unauth','auth');
 4932:         my @cattypes = ('std','domonly','codesrch','none');
 4933:         $checked{'unauth'} = 'std';
 4934:         $checked{'auth'} = 'std';
 4935:         if (ref($settings) eq 'HASH') {
 4936:             foreach my $type (@cattypes) {
 4937:                 if ($type eq $settings->{'unauth'}) {
 4938:                     $checked{'unauth'} = $type;
 4939:                 }
 4940:                 if ($type eq $settings->{'auth'}) {
 4941:                     $checked{'auth'} = $type;
 4942:                 }
 4943:             }
 4944:         }
 4945:         my %lt = &Apache::lonlocal::texthash (
 4946:                                                unauth   => 'Catalog type for unauthenticated users',
 4947:                                                auth     => 'Catalog type for authenticated users',
 4948:                                                none     => 'No catalog',
 4949:                                                std      => 'Standard catalog',
 4950:                                                domonly  => 'Domain-only catalog',
 4951:                                                codesrch => "Code search form",
 4952:                                              );
 4953:        my $itemcount = 0;
 4954:        foreach my $item (@catitems) {
 4955:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 4956:            $datatable .= '<tr '.$css_class.'>'.
 4957:                          '<td>'.$lt{$item}.'</td>'.
 4958:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 4959:            foreach my $type (@cattypes) {
 4960:                my $ischecked;
 4961:                if ($checked{$item} eq $type) {
 4962:                    $ischecked=' checked="checked"';
 4963:                }
 4964:                $datatable .= '<label>'.
 4965:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 4966:                              ' />'.$lt{$type}.'</label>&nbsp;';
 4967:            }
 4968:            $datatable .= '</td></tr>';
 4969:            $itemcount ++;
 4970:         }
 4971:         $$rowtotal += $itemcount;
 4972:     } elsif ($position eq 'middle') {
 4973:         my $toggle_cats_crs = ' ';
 4974:         my $toggle_cats_dom = ' checked="checked" ';
 4975:         my $can_cat_crs = ' ';
 4976:         my $can_cat_dom = ' checked="checked" ';
 4977:         my $toggle_catscomm_comm = ' ';
 4978:         my $toggle_catscomm_dom = ' checked="checked" ';
 4979:         my $can_catcomm_comm = ' ';
 4980:         my $can_catcomm_dom = ' checked="checked" ';
 4981: 
 4982:         if (ref($settings) eq 'HASH') {
 4983:             if ($settings->{'togglecats'} eq 'crs') {
 4984:                 $toggle_cats_crs = $toggle_cats_dom;
 4985:                 $toggle_cats_dom = ' ';
 4986:             }
 4987:             if ($settings->{'categorize'} eq 'crs') {
 4988:                 $can_cat_crs = $can_cat_dom;
 4989:                 $can_cat_dom = ' ';
 4990:             }
 4991:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4992:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4993:                 $toggle_catscomm_dom = ' ';
 4994:             }
 4995:             if ($settings->{'categorizecomm'} eq 'comm') {
 4996:                 $can_catcomm_comm = $can_catcomm_dom;
 4997:                 $can_catcomm_dom = ' ';
 4998:             }
 4999:         }
 5000:         my %title = &Apache::lonlocal::texthash (
 5001:                      togglecats     => 'Show/Hide a course in catalog',
 5002:                      togglecatscomm => 'Show/Hide a community in catalog',
 5003:                      categorize     => 'Assign a category to a course',
 5004:                      categorizecomm => 'Assign a category to a community',
 5005:                     );
 5006:         my %level = &Apache::lonlocal::texthash (
 5007:                      dom  => 'Set in Domain',
 5008:                      crs  => 'Set in Course',
 5009:                      comm => 'Set in Community',
 5010:                     );
 5011:         $datatable = '<tr class="LC_odd_row">'.
 5012:                   '<td>'.$title{'togglecats'}.'</td>'.
 5013:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5014:                   '<input type="radio" name="togglecats"'.
 5015:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5016:                   '<label><input type="radio" name="togglecats"'.
 5017:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5018:                   '</tr><tr>'.
 5019:                   '<td>'.$title{'categorize'}.'</td>'.
 5020:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5021:                   '<label><input type="radio" name="categorize"'.
 5022:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5023:                   '<label><input type="radio" name="categorize"'.
 5024:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5025:                   '</tr><tr class="LC_odd_row">'.
 5026:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 5027:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5028:                   '<input type="radio" name="togglecatscomm"'.
 5029:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5030:                   '<label><input type="radio" name="togglecatscomm"'.
 5031:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5032:                   '</tr><tr>'.
 5033:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 5034:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5035:                   '<label><input type="radio" name="categorizecomm"'.
 5036:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5037:                   '<label><input type="radio" name="categorizecomm"'.
 5038:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5039:                   '</tr>';
 5040:         $$rowtotal += 4;
 5041:     } else {
 5042:         my $css_class;
 5043:         my $itemcount = 1;
 5044:         my $cathash; 
 5045:         if (ref($settings) eq 'HASH') {
 5046:             $cathash = $settings->{'cats'};
 5047:         }
 5048:         if (ref($cathash) eq 'HASH') {
 5049:             my (@cats,@trails,%allitems,%idx,@jsarray);
 5050:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 5051:                                                    \%allitems,\%idx,\@jsarray);
 5052:             my $maxdepth = scalar(@cats);
 5053:             my $colattrib = '';
 5054:             if ($maxdepth > 2) {
 5055:                 $colattrib = ' colspan="2" ';
 5056:             }
 5057:             my @path;
 5058:             if (@cats > 0) {
 5059:                 if (ref($cats[0]) eq 'ARRAY') {
 5060:                     my $numtop = @{$cats[0]};
 5061:                     my $maxnum = $numtop;
 5062:                     my %default_names = (
 5063:                           instcode    => &mt('Official courses'),
 5064:                           communities => &mt('Communities'),
 5065:                     );
 5066: 
 5067:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 5068:                         ($cathash->{'instcode::0'} eq '') ||
 5069:                         (!grep(/^communities$/,@{$cats[0]})) || 
 5070:                         ($cathash->{'communities::0'} eq '')) {
 5071:                         $maxnum ++;
 5072:                     }
 5073:                     my $lastidx;
 5074:                     for (my $i=0; $i<$numtop; $i++) {
 5075:                         my $parent = $cats[0][$i];
 5076:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5077:                         my $item = &escape($parent).'::0';
 5078:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 5079:                         $lastidx = $idx{$item};
 5080:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5081:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 5082:                         for (my $k=0; $k<=$maxnum; $k++) {
 5083:                             my $vpos = $k+1;
 5084:                             my $selstr;
 5085:                             if ($k == $i) {
 5086:                                 $selstr = ' selected="selected" ';
 5087:                             }
 5088:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5089:                         }
 5090:                         $datatable .= '</select></span></td><td>';
 5091:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 5092:                             $datatable .=  '<span class="LC_nobreak">'
 5093:                                            .$default_names{$parent}.'</span>';
 5094:                             if ($parent eq 'instcode') {
 5095:                                 $datatable .= '<br /><span class="LC_nobreak">('
 5096:                                               .&mt('with institutional codes')
 5097:                                               .')</span></td><td'.$colattrib.'>';
 5098:                             } else {
 5099:                                 $datatable .= '<table><tr><td>';
 5100:                             }
 5101:                             $datatable .= '<span class="LC_nobreak">'
 5102:                                           .'<label><input type="radio" name="'
 5103:                                           .$parent.'" value="1" checked="checked" />'
 5104:                                           .&mt('Display').'</label>';
 5105:                             if ($parent eq 'instcode') {
 5106:                                 $datatable .= '&nbsp;';
 5107:                             } else {
 5108:                                 $datatable .= '</span></td></tr><tr><td>'
 5109:                                               .'<span class="LC_nobreak">';
 5110:                             }
 5111:                             $datatable .= '<label><input type="radio" name="'
 5112:                                           .$parent.'" value="0" />'
 5113:                                           .&mt('Do not display').'</label></span>';
 5114:                             if ($parent eq 'communities') {
 5115:                                 $datatable .= '</td></tr></table>';
 5116:                             }
 5117:                             $datatable .= '</td>';
 5118:                         } else {
 5119:                             $datatable .= $parent
 5120:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 5121:                                           .'<input type="checkbox" name="deletecategory" '
 5122:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 5123:                         }
 5124:                         my $depth = 1;
 5125:                         push(@path,$parent);
 5126:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 5127:                         pop(@path);
 5128:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 5129:                         $itemcount ++;
 5130:                     }
 5131:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5132:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 5133:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 5134:                     for (my $k=0; $k<=$maxnum; $k++) {
 5135:                         my $vpos = $k+1;
 5136:                         my $selstr;
 5137:                         if ($k == $numtop) {
 5138:                             $selstr = ' selected="selected" ';
 5139:                         }
 5140:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5141:                     }
 5142:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 5143:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 5144:                                   .'</tr>'."\n";
 5145:                     $itemcount ++;
 5146:                     foreach my $default ('instcode','communities') {
 5147:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 5148:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5149:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 5150:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 5151:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5152:                             for (my $k=0; $k<=$maxnum; $k++) {
 5153:                                 my $vpos = $k+1;
 5154:                                 my $selstr;
 5155:                                 if ($k == $maxnum) {
 5156:                                     $selstr = ' selected="selected" ';
 5157:                                 }
 5158:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5159:                             }
 5160:                             $datatable .= '</select></span></td>'.
 5161:                                           '<td><span class="LC_nobreak">'.
 5162:                                           $default_names{$default}.'</span>';
 5163:                             if ($default eq 'instcode') {
 5164:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5165:                                               .&mt('with institutional codes').')</span>';
 5166:                             }
 5167:                             $datatable .= '</td>'
 5168:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5169:                                           .&mt('Display').'</label>&nbsp;'
 5170:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5171:                                           .&mt('Do not display').'</label></span></td></tr>';
 5172:                         }
 5173:                     }
 5174:                 }
 5175:             } else {
 5176:                 $datatable .= &initialize_categories($itemcount);
 5177:             }
 5178:         } else {
 5179:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 5180:                           .&initialize_categories($itemcount);
 5181:         }
 5182:         $$rowtotal += $itemcount;
 5183:     }
 5184:     return $datatable;
 5185: }
 5186: 
 5187: sub print_serverstatuses {
 5188:     my ($dom,$settings,$rowtotal) = @_;
 5189:     my $datatable;
 5190:     my @pages = &serverstatus_pages();
 5191:     my (%namedaccess,%machineaccess);
 5192:     foreach my $type (@pages) {
 5193:         $namedaccess{$type} = '';
 5194:         $machineaccess{$type}= '';
 5195:     }
 5196:     if (ref($settings) eq 'HASH') {
 5197:         foreach my $type (@pages) {
 5198:             if (exists($settings->{$type})) {
 5199:                 if (ref($settings->{$type}) eq 'HASH') {
 5200:                     foreach my $key (keys(%{$settings->{$type}})) {
 5201:                         if ($key eq 'namedusers') {
 5202:                             $namedaccess{$type} = $settings->{$type}->{$key};
 5203:                         } elsif ($key eq 'machines') {
 5204:                             $machineaccess{$type} = $settings->{$type}->{$key};
 5205:                         }
 5206:                     }
 5207:                 }
 5208:             }
 5209:         }
 5210:     }
 5211:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 5212:     my $rownum = 0;
 5213:     my $css_class;
 5214:     foreach my $type (@pages) {
 5215:         $rownum ++;
 5216:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 5217:         $datatable .= '<tr'.$css_class.'>'.
 5218:                       '<td><span class="LC_nobreak">'.
 5219:                       $titles->{$type}.'</span></td>'.
 5220:                       '<td class="LC_left_item">'.
 5221:                       '<input type="text" name="'.$type.'_namedusers" '.
 5222:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 5223:                       '<td class="LC_right_item">'.
 5224:                       '<span class="LC_nobreak">'.
 5225:                       '<input type="text" name="'.$type.'_machines" '.
 5226:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 5227:                       '</td></tr>'."\n";
 5228:     }
 5229:     $$rowtotal += $rownum;
 5230:     return $datatable;
 5231: }
 5232: 
 5233: sub serverstatus_pages {
 5234:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 5235:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 5236:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 5237:             'uniquecodes','diskusage');
 5238: }
 5239: 
 5240: sub defaults_javascript {
 5241:     my ($settings) = @_;
 5242:     return unless (ref($settings) eq 'HASH');
 5243:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5244:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 5245:         if ($maxnum eq '') {
 5246:             $maxnum = 0;
 5247:         }
 5248:         $maxnum ++;
 5249:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 5250:         return <<"ENDSCRIPT";
 5251: <script type="text/javascript">
 5252: // <![CDATA[
 5253: function reorderTypes(form,caller) {
 5254:     var changedVal;
 5255: $jstext 
 5256:     var newpos = 'addinststatus_pos';
 5257:     var current = new Array;
 5258:     var maxh = $maxnum;
 5259:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5260:     var oldVal;
 5261:     if (caller == newpos) {
 5262:         changedVal = newitemVal;
 5263:     } else {
 5264:         var curritem = 'inststatus_pos_'+caller;
 5265:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 5266:         current[newitemVal] = newpos;
 5267:     }
 5268:     for (var i=0; i<inststatuses.length; i++) {
 5269:         if (inststatuses[i] != caller) {
 5270:             var elementName = 'inststatus_pos_'+inststatuses[i];
 5271:             if (form.elements[elementName]) {
 5272:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5273:                 current[currVal] = elementName;
 5274:             }
 5275:         }
 5276:     }
 5277:     for (var j=0; j<maxh; j++) {
 5278:         if (current[j] == undefined) {
 5279:             oldVal = j;
 5280:         }
 5281:     }
 5282:     if (oldVal < changedVal) {
 5283:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5284:            var elementName = current[k];
 5285:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5286:         }
 5287:     } else {
 5288:         for (var k=changedVal; k<oldVal; k++) {
 5289:             var elementName = current[k];
 5290:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5291:         }
 5292:     }
 5293:     return;
 5294: }
 5295: 
 5296: // ]]>
 5297: </script>
 5298: 
 5299: ENDSCRIPT
 5300:     }
 5301: }
 5302: 
 5303: sub coursecategories_javascript {
 5304:     my ($settings) = @_;
 5305:     my ($output,$jstext,$cathash);
 5306:     if (ref($settings) eq 'HASH') {
 5307:         $cathash = $settings->{'cats'};
 5308:     }
 5309:     if (ref($cathash) eq 'HASH') {
 5310:         my (@cats,@jsarray,%idx);
 5311:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 5312:         if (@jsarray > 0) {
 5313:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 5314:             for (my $i=0; $i<@jsarray; $i++) {
 5315:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 5316:                     my $catstr = join('","',@{$jsarray[$i]});
 5317:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 5318:                 }
 5319:             }
 5320:         }
 5321:     } else {
 5322:         $jstext  = '    var categories = Array(1);'."\n".
 5323:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 5324:     }
 5325:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 5326:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 5327:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category.'); 
 5328:     $output = <<"ENDSCRIPT";
 5329: <script type="text/javascript">
 5330: // <![CDATA[
 5331: function reorderCats(form,parent,item,idx) {
 5332:     var changedVal;
 5333: $jstext
 5334:     var newpos = 'addcategory_pos';
 5335:     if (parent == '') {
 5336:         var has_instcode = 0;
 5337:         var maxtop = categories[idx].length;
 5338:         for (var j=0; j<maxtop; j++) {
 5339:             if (categories[idx][j] == 'instcode::0') {
 5340:                 has_instcode == 1;
 5341:             }
 5342:         }
 5343:         if (has_instcode == 0) {
 5344:             categories[idx][maxtop] = 'instcode_pos';
 5345:         }
 5346:     } else {
 5347:         newpos += '_'+parent;
 5348:     }
 5349:     var maxh = 1 + categories[idx].length;
 5350:     var current = new Array;
 5351:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5352:     if (item == newpos) {
 5353:         changedVal = newitemVal;
 5354:     } else {
 5355:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5356:         current[newitemVal] = newpos;
 5357:     }
 5358:     for (var i=0; i<categories[idx].length; i++) {
 5359:         var elementName = categories[idx][i];
 5360:         if (elementName != item) {
 5361:             if (form.elements[elementName]) {
 5362:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5363:                 current[currVal] = elementName;
 5364:             }
 5365:         }
 5366:     }
 5367:     var oldVal;
 5368:     for (var j=0; j<maxh; j++) {
 5369:         if (current[j] == undefined) {
 5370:             oldVal = j;
 5371:         }
 5372:     }
 5373:     if (oldVal < changedVal) {
 5374:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5375:            var elementName = current[k];
 5376:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5377:         }
 5378:     } else {
 5379:         for (var k=changedVal; k<oldVal; k++) {
 5380:             var elementName = current[k];
 5381:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5382:         }
 5383:     }
 5384:     return;
 5385: }
 5386: 
 5387: function categoryCheck(form) {
 5388:     if (form.elements['addcategory_name'].value == 'instcode') {
 5389:         alert('$instcode_reserved\\n$choose_again');
 5390:         return false;
 5391:     }
 5392:     if (form.elements['addcategory_name'].value == 'communities') {
 5393:         alert('$communities_reserved\\n$choose_again');
 5394:         return false;
 5395:     }
 5396:     return true;
 5397: }
 5398: 
 5399: // ]]>
 5400: </script>
 5401: 
 5402: ENDSCRIPT
 5403:     return $output;
 5404: }
 5405: 
 5406: sub initialize_categories {
 5407:     my ($itemcount) = @_;
 5408:     my ($datatable,$css_class,$chgstr);
 5409:     my %default_names = (
 5410:                       instcode    => 'Official courses (with institutional codes)',
 5411:                       communities => 'Communities',
 5412:                         );
 5413:     my $select0 = ' selected="selected"';
 5414:     my $select1 = '';
 5415:     foreach my $default ('instcode','communities') {
 5416:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5417:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 5418:         if ($default eq 'communities') {
 5419:             $select1 = $select0;
 5420:             $select0 = '';
 5421:         }
 5422:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5423:                      .'<select name="'.$default.'_pos">'
 5424:                      .'<option value="0"'.$select0.'>1</option>'
 5425:                      .'<option value="1"'.$select1.'>2</option>'
 5426:                      .'<option value="2">3</option></select>&nbsp;'
 5427:                      .$default_names{$default}
 5428:                      .'</span></td><td><span class="LC_nobreak">'
 5429:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5430:                      .&mt('Display').'</label>&nbsp;<label>'
 5431:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5432:                  .'</label></span></td></tr>';
 5433:         $itemcount ++;
 5434:     }
 5435:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5436:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5437:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5438:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5439:                   .'<option value="0">1</option>'
 5440:                   .'<option value="1">2</option>'
 5441:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5442:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5443:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5444:     return $datatable;
 5445: }
 5446: 
 5447: sub build_category_rows {
 5448:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5449:     my ($text,$name,$item,$chgstr);
 5450:     if (ref($cats) eq 'ARRAY') {
 5451:         my $maxdepth = scalar(@{$cats});
 5452:         if (ref($cats->[$depth]) eq 'HASH') {
 5453:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5454:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5455:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5456:                 $text .= '<td><table class="LC_data_table">';
 5457:                 my ($idxnum,$parent_name,$parent_item);
 5458:                 my $higher = $depth - 1;
 5459:                 if ($higher == 0) {
 5460:                     $parent_name = &escape($parent).'::'.$higher;
 5461:                 } else {
 5462:                     if (ref($path) eq 'ARRAY') {
 5463:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5464:                     }
 5465:                 }
 5466:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5467:                 for (my $j=0; $j<=$numchildren; $j++) {
 5468:                     if ($j < $numchildren) {
 5469:                         $name = $cats->[$depth]{$parent}[$j];
 5470:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5471:                         $idxnum = $idx->{$item};
 5472:                     } else {
 5473:                         $name = $parent_name;
 5474:                         $item = $parent_item;
 5475:                     }
 5476:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5477:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5478:                     for (my $i=0; $i<=$numchildren; $i++) {
 5479:                         my $vpos = $i+1;
 5480:                         my $selstr;
 5481:                         if ($j == $i) {
 5482:                             $selstr = ' selected="selected" ';
 5483:                         }
 5484:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5485:                     }
 5486:                     $text .= '</select>&nbsp;';
 5487:                     if ($j < $numchildren) {
 5488:                         my $deeper = $depth+1;
 5489:                         $text .= $name.'&nbsp;'
 5490:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5491:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5492:                         if(ref($path) eq 'ARRAY') {
 5493:                             push(@{$path},$name);
 5494:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5495:                             pop(@{$path});
 5496:                         }
 5497:                     } else {
 5498:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5499:                         if ($j == $numchildren) {
 5500:                             $text .= $name;
 5501:                         } else {
 5502:                             $text .= $item;
 5503:                         }
 5504:                         $text .= '" value="" />';
 5505:                     }
 5506:                     $text .= '</td></tr>';
 5507:                 }
 5508:                 $text .= '</table></td>';
 5509:             } else {
 5510:                 my $higher = $depth-1;
 5511:                 if ($higher == 0) {
 5512:                     $name = &escape($parent).'::'.$higher;
 5513:                 } else {
 5514:                     if (ref($path) eq 'ARRAY') {
 5515:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5516:                     }
 5517:                 }
 5518:                 my $colspan;
 5519:                 if ($parent ne 'instcode') {
 5520:                     $colspan = $maxdepth - $depth - 1;
 5521:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5522:                 }
 5523:             }
 5524:         }
 5525:     }
 5526:     return $text;
 5527: }
 5528: 
 5529: sub modifiable_userdata_row {
 5530:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5531:     my ($role,$rolename,$statustype);
 5532:     $role = $item;
 5533:     if ($context eq 'cancreate') {
 5534:         if ($item =~ /^emailusername_(.+)$/) {
 5535:             $statustype = $1;
 5536:             $role = 'emailusername';
 5537:             if (ref($usertypes) eq 'HASH') {
 5538:                 if ($usertypes->{$statustype}) {
 5539:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5540:                 } else {
 5541:                     $rolename = &mt('Data provided by user');
 5542:                 }
 5543:             }
 5544:         }
 5545:     } elsif ($context eq 'selfcreate') {
 5546:         if (ref($usertypes) eq 'HASH') {
 5547:             $rolename = $usertypes->{$role};
 5548:         } else {
 5549:             $rolename = $role;
 5550:         }
 5551:     } else {
 5552:         if ($role eq 'cr') {
 5553:             $rolename = &mt('Custom role');
 5554:         } else {
 5555:             $rolename = &Apache::lonnet::plaintext($role);
 5556:         }
 5557:     }
 5558:     my (@fields,%fieldtitles);
 5559:     if (ref($fieldsref) eq 'ARRAY') {
 5560:         @fields = @{$fieldsref};
 5561:     } else {
 5562:         @fields = ('lastname','firstname','middlename','generation',
 5563:                    'permanentemail','id');
 5564:     }
 5565:     if ((ref($titlesref) eq 'HASH')) {
 5566:         %fieldtitles = %{$titlesref};
 5567:     } else {
 5568:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5569:     }
 5570:     my $output;
 5571:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5572:     $output = '<tr '.$css_class.'>'.
 5573:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5574:               '<td class="LC_left_item" colspan="2"><table>';
 5575:     my $rem;
 5576:     my %checks;
 5577:     if (ref($settings) eq 'HASH') {
 5578:         if (ref($settings->{$context}) eq 'HASH') {
 5579:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5580:                 my $hashref = $settings->{$context}->{$role};
 5581:                 if ($role eq 'emailusername') {
 5582:                     if ($statustype) {
 5583:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5584:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5585:                             if (ref($hashref) eq 'HASH') { 
 5586:                                 foreach my $field (@fields) {
 5587:                                     if ($hashref->{$field}) {
 5588:                                         $checks{$field} = $hashref->{$field};
 5589:                                     }
 5590:                                 }
 5591:                             }
 5592:                         }
 5593:                     }
 5594:                 } else {
 5595:                     if (ref($hashref) eq 'HASH') {
 5596:                         foreach my $field (@fields) {
 5597:                             if ($hashref->{$field}) {
 5598:                                 $checks{$field} = ' checked="checked" ';
 5599:                             }
 5600:                         }
 5601:                     }
 5602:                 }
 5603:             }
 5604:         }
 5605:     }
 5606:      
 5607:     for (my $i=0; $i<@fields; $i++) {
 5608:         my $rem = $i%($numinrow);
 5609:         if ($rem == 0) {
 5610:             if ($i > 0) {
 5611:                 $output .= '</tr>';
 5612:             }
 5613:             $output .= '<tr>';
 5614:         }
 5615:         my $check = ' ';
 5616:         unless ($role eq 'emailusername') {
 5617:             if (exists($checks{$fields[$i]})) {
 5618:                 $check = $checks{$fields[$i]}
 5619:             } else {
 5620:                 if ($role eq 'st') {
 5621:                     if (ref($settings) ne 'HASH') {
 5622:                         $check = ' checked="checked" '; 
 5623:                     }
 5624:                 }
 5625:             }
 5626:         }
 5627:         $output .= '<td class="LC_left_item">'.
 5628:                    '<span class="LC_nobreak">';
 5629:         if ($role eq 'emailusername') {
 5630:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5631:                 $checks{$fields[$i]} = 'omit';
 5632:             }
 5633:             foreach my $option ('required','optional','omit') {
 5634:                 my $checked='';
 5635:                 if ($checks{$fields[$i]} eq $option) {
 5636:                     $checked='checked="checked" ';
 5637:                 }
 5638:                 $output .= '<label>'.
 5639:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5640:                            &mt($option).'</label>'.('&nbsp;' x2);
 5641:             }
 5642:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5643:         } else {
 5644:             $output .= '<label>'.
 5645:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5646:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5647:                        '</label>';
 5648:         }
 5649:         $output .= '</span></td>';
 5650:         $rem = @fields%($numinrow);
 5651:     }
 5652:     my $colsleft = $numinrow - $rem;
 5653:     if ($colsleft > 1 ) {
 5654:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5655:                    '&nbsp;</td>';
 5656:     } elsif ($colsleft == 1) {
 5657:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5658:     }
 5659:     $output .= '</tr></table></td></tr>';
 5660:     return $output;
 5661: }
 5662: 
 5663: sub insttypes_row {
 5664:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5665:     my %lt = &Apache::lonlocal::texthash (
 5666:                       cansearch => 'Users allowed to search',
 5667:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5668:                       lockablenames => 'User preference to lock name',
 5669:              );
 5670:     my $showdom;
 5671:     if ($context eq 'cansearch') {
 5672:         $showdom = ' ('.$dom.')';
 5673:     }
 5674:     my $class = 'LC_left_item';
 5675:     if ($context eq 'statustocreate') {
 5676:         $class = 'LC_right_item';
 5677:     }
 5678:     my $css_class = ' class="LC_odd_row"';
 5679:     if ($rownum ne '') { 
 5680:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5681:     }
 5682:     my $output = '<tr'.$css_class.'>'.
 5683:                  '<td>'.$lt{$context}.$showdom.
 5684:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5685:     my $rem;
 5686:     if (ref($types) eq 'ARRAY') {
 5687:         for (my $i=0; $i<@{$types}; $i++) {
 5688:             if (defined($usertypes->{$types->[$i]})) {
 5689:                 my $rem = $i%($numinrow);
 5690:                 if ($rem == 0) {
 5691:                     if ($i > 0) {
 5692:                         $output .= '</tr>';
 5693:                     }
 5694:                     $output .= '<tr>';
 5695:                 }
 5696:                 my $check = ' ';
 5697:                 if (ref($settings) eq 'HASH') {
 5698:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5699:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5700:                             $check = ' checked="checked" ';
 5701:                         }
 5702:                     } elsif ($context eq 'statustocreate') {
 5703:                         $check = ' checked="checked" ';
 5704:                     }
 5705:                 }
 5706:                 $output .= '<td class="LC_left_item">'.
 5707:                            '<span class="LC_nobreak"><label>'.
 5708:                            '<input type="checkbox" name="'.$context.'" '.
 5709:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5710:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5711:             }
 5712:         }
 5713:         $rem = @{$types}%($numinrow);
 5714:     }
 5715:     my $colsleft = $numinrow - $rem;
 5716:     if (($rem == 0) && (@{$types} > 0)) {
 5717:         $output .= '<tr>';
 5718:     }
 5719:     if ($colsleft > 1) {
 5720:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5721:     } else {
 5722:         $output .= '<td class="LC_left_item">';
 5723:     }
 5724:     my $defcheck = ' ';
 5725:     if (ref($settings) eq 'HASH') {  
 5726:         if (ref($settings->{$context}) eq 'ARRAY') {
 5727:             if (grep(/^default$/,@{$settings->{$context}})) {
 5728:                 $defcheck = ' checked="checked" ';
 5729:             }
 5730:         } elsif ($context eq 'statustocreate') {
 5731:             $defcheck = ' checked="checked" ';
 5732:         }
 5733:     }
 5734:     $output .= '<span class="LC_nobreak"><label>'.
 5735:                '<input type="checkbox" name="'.$context.'" '.
 5736:                'value="default"'.$defcheck.'/>'.
 5737:                $othertitle.'</label></span></td>'.
 5738:                '</tr></table></td></tr>';
 5739:     return $output;
 5740: }
 5741: 
 5742: sub sorted_searchtitles {
 5743:     my %searchtitles = &Apache::lonlocal::texthash(
 5744:                          'uname' => 'username',
 5745:                          'lastname' => 'last name',
 5746:                          'lastfirst' => 'last name, first name',
 5747:                      );
 5748:     my @titleorder = ('uname','lastname','lastfirst');
 5749:     return (\%searchtitles,\@titleorder);
 5750: }
 5751: 
 5752: sub sorted_searchtypes {
 5753:     my %srchtypes_desc = (
 5754:                            exact    => 'is exact match',
 5755:                            contains => 'contains ..',
 5756:                            begins   => 'begins with ..',
 5757:                          );
 5758:     my @srchtypeorder = ('exact','begins','contains');
 5759:     return (\%srchtypes_desc,\@srchtypeorder);
 5760: }
 5761: 
 5762: sub usertype_update_row {
 5763:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5764:     my $datatable;
 5765:     my $numinrow = 4;
 5766:     foreach my $type (@{$types}) {
 5767:         if (defined($usertypes->{$type})) {
 5768:             $$rownums ++;
 5769:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5770:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5771:                           '</td><td class="LC_left_item"><table>';
 5772:             for (my $i=0; $i<@{$fields}; $i++) {
 5773:                 my $rem = $i%($numinrow);
 5774:                 if ($rem == 0) {
 5775:                     if ($i > 0) {
 5776:                         $datatable .= '</tr>';
 5777:                     }
 5778:                     $datatable .= '<tr>';
 5779:                 }
 5780:                 my $check = ' ';
 5781:                 if (ref($settings) eq 'HASH') {
 5782:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5783:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5784:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5785:                                 $check = ' checked="checked" ';
 5786:                             }
 5787:                         }
 5788:                     }
 5789:                 }
 5790: 
 5791:                 if ($i == @{$fields}-1) {
 5792:                     my $colsleft = $numinrow - $rem;
 5793:                     if ($colsleft > 1) {
 5794:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5795:                     } else {
 5796:                         $datatable .= '<td>';
 5797:                     }
 5798:                 } else {
 5799:                     $datatable .= '<td>';
 5800:                 }
 5801:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5802:                               '<input type="checkbox" name="updateable_'.$type.
 5803:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5804:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5805:             }
 5806:             $datatable .= '</tr></table></td></tr>';
 5807:         }
 5808:     }
 5809:     return $datatable;
 5810: }
 5811: 
 5812: sub modify_login {
 5813:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5814:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5815:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5816:     %title = ( coursecatalog => 'Display course catalog',
 5817:                adminmail => 'Display administrator E-mail address',
 5818:                helpdesk  => 'Display "Contact Helpdesk" link',
 5819:                newuser => 'Link for visitors to create a user account',
 5820:                loginheader => 'Log-in box header');
 5821:     @offon = ('off','on');
 5822:     if (ref($domconfig{login}) eq 'HASH') {
 5823:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5824:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5825:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5826:             }
 5827:         }
 5828:     }
 5829:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5830:                                            \%domconfig,\%loginhash);
 5831:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5832:     foreach my $item (@toggles) {
 5833:         $loginhash{login}{$item} = $env{'form.'.$item};
 5834:     }
 5835:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5836:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5837:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5838:                                          \%loginhash);
 5839:     }
 5840: 
 5841:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5842:     my %domservers = &Apache::lonnet::get_servers($dom);
 5843:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5844:     if (keys(%servers) > 1) {
 5845:         foreach my $lonhost (keys(%servers)) {
 5846:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5847:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5848:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5849:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5850:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5851:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5852:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5853:                         $changes{'loginvia'}{$lonhost} = 1;
 5854:                     } else {
 5855:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5856:                         $changes{'loginvia'}{$lonhost} = 1;
 5857:                     }
 5858:                 } else {
 5859:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5860:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5861:                         $changes{'loginvia'}{$lonhost} = 1;
 5862:                     }
 5863:                 }
 5864:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5865:                     foreach my $item (@loginvia_attribs) {
 5866:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5867:                     }
 5868:                 } else {
 5869:                     foreach my $item (@loginvia_attribs) {
 5870:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5871:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5872:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5873:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5874:                                 $new = '/';
 5875:                             }
 5876:                         }
 5877:                         if (($item eq 'custompath') && 
 5878:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5879:                             $new = '';
 5880:                         }
 5881:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5882:                             $changes{'loginvia'}{$lonhost} = 1;
 5883:                         }
 5884:                         if ($item eq 'exempt') {
 5885:                             $new = &check_exempt_addresses($new);
 5886:                         }
 5887:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5888:                     }
 5889:                 }
 5890:             } else {
 5891:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5892:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5893:                     $changes{'loginvia'}{$lonhost} = 1;
 5894:                     foreach my $item (@loginvia_attribs) {
 5895:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5896:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5897:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5898:                                 $new = '/';
 5899:                             }
 5900:                         }
 5901:                         if (($item eq 'custompath') && 
 5902:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5903:                             $new = '';
 5904:                         }
 5905:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5906:                     }
 5907:                 }
 5908:             }
 5909:         }
 5910:     }
 5911: 
 5912:     my $servadm = $r->dir_config('lonAdmEMail');
 5913:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5914:     if (ref($domconfig{'login'}) eq 'HASH') {
 5915:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5916:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5917:                 if ($lang eq 'nolang') {
 5918:                     push(@currlangs,$lang);
 5919:                 } elsif (defined($langchoices{$lang})) {
 5920:                     push(@currlangs,$lang);
 5921:                 } else {
 5922:                     next;
 5923:                 }
 5924:             }
 5925:         }
 5926:     }
 5927:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5928:     if (@currlangs > 0) {
 5929:         foreach my $lang (@currlangs) {
 5930:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5931:                 $changes{'helpurl'}{$lang} = 1;
 5932:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5933:                 $changes{'helpurl'}{$lang} = 1;
 5934:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5935:                 push(@newlangs,$lang);
 5936:             } else {
 5937:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5938:             }
 5939:         }
 5940:     }
 5941:     unless (grep(/^nolang$/,@currlangs)) {
 5942:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5943:             $changes{'helpurl'}{'nolang'} = 1;
 5944:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5945:             push(@newlangs,'nolang');
 5946:         }
 5947:     }
 5948:     if ($env{'form.loginhelpurl_add_lang'}) {
 5949:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5950:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5951:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5952:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5953:         }
 5954:     }
 5955:     if ((@newlangs > 0) || ($addedfile)) {
 5956:         my $error;
 5957:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5958:         if ($configuserok eq 'ok') {
 5959:             if ($switchserver) {
 5960:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5961:             } elsif ($author_ok eq 'ok') {
 5962:                 my @allnew = @newlangs;
 5963:                 if ($addedfile ne '') {
 5964:                     push(@allnew,$addedfile);
 5965:                 }
 5966:                 foreach my $lang (@allnew) {
 5967:                     my $formelem = 'loginhelpurl_'.$lang;
 5968:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5969:                         $formelem = 'loginhelpurl_add_file';
 5970:                     }
 5971:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5972:                                                                "help/$lang",'','',$newfile{$lang});
 5973:                     if ($result eq 'ok') {
 5974:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5975:                         $changes{'helpurl'}{$lang} = 1;
 5976:                     } else {
 5977:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5978:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5979:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5980:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5981: 
 5982:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5983:                         }
 5984:                     }
 5985:                 }
 5986:             } else {
 5987:                 $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);
 5988:             }
 5989:         } else {
 5990:             $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);
 5991:         }
 5992:         if ($error) {
 5993:             &Apache::lonnet::logthis($error);
 5994:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5995:         }
 5996:     }
 5997: 
 5998:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 5999:     if (ref($domconfig{'login'}) eq 'HASH') {
 6000:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 6001:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 6002:                 if ($domservers{$lonhost}) {
 6003:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 6004:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 6005:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtagexempt'}{$lonhost}{'exempt'}
 6006:                     }
 6007:                 }
 6008:             }
 6009:         }
 6010:     }
 6011:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 6012:     foreach my $lonhost (sort(keys(%domservers))) {
 6013:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 6014:             $changes{'headtag'}{$lonhost} = 1;
 6015:         } else {
 6016:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 6017:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 6018:             }
 6019:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 6020:                 push(@newhosts,$lonhost);
 6021:             } elsif ($currheadtagurls{$lonhost}) {
 6022:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 6023:                 if ($currexempt{$lonhost}) {
 6024:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 6025:                         $changes{'headtag'}{$lonhost} = 1;
 6026:                     }
 6027:                 } elsif ($possexempt{$lonhost}) {
 6028:                     $changes{'headtag'}{$lonhost} = 1;
 6029:                 }
 6030:                 if ($possexempt{$lonhost}) {
 6031:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6032:                 }
 6033:             }
 6034:         }
 6035:     }
 6036:     if (@newhosts) {
 6037:         my $error;
 6038:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6039:         if ($configuserok eq 'ok') {
 6040:             if ($switchserver) {
 6041:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 6042:             } elsif ($author_ok eq 'ok') {
 6043:                 foreach my $lonhost (@newhosts) {
 6044:                     my $formelem = 'loginheadtag_'.$lonhost;
 6045:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 6046:                                                                           "login/headtag/$lonhost",'','',
 6047:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 6048:                     if ($result eq 'ok') {
 6049:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 6050:                         $changes{'headtag'}{$lonhost} = 1;
 6051:                         if ($possexempt{$lonhost}) {
 6052:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6053:                         }
 6054:                     } else {
 6055:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 6056:                                            $newheadtagurls{$lonhost},$result);
 6057:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 6058:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 6059:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 6060:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 6061:                         }
 6062:                     }
 6063:                 }
 6064:             } else {
 6065:                 $error = &mt("Upload of custom markup 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);
 6066:             }
 6067:         } else {
 6068:             $error = &mt("Upload of custom markup file(s) failed because a Domain Configuration user ([_1]) could not be created in domain: [_2].  Error was: [_3].",$confname,$dom,$configuserok);
 6069:         }
 6070:         if ($error) {
 6071:             &Apache::lonnet::logthis($error);
 6072:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6073:         }
 6074:     }
 6075: 
 6076:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 6077: 
 6078:     my $defaulthelpfile = '/adm/loginproblems.html';
 6079:     my $defaulttext = &mt('Default in use');
 6080: 
 6081:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 6082:                                              $dom);
 6083:     if ($putresult eq 'ok') {
 6084:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 6085:         my %defaultchecked = (
 6086:                     'coursecatalog' => 'on',
 6087:                     'helpdesk'      => 'on',
 6088:                     'adminmail'     => 'off',
 6089:                     'newuser'       => 'off',
 6090:         );
 6091:         if (ref($domconfig{'login'}) eq 'HASH') {
 6092:             foreach my $item (@toggles) {
 6093:                 if ($defaultchecked{$item} eq 'on') { 
 6094:                     if (($domconfig{'login'}{$item} eq '0') &&
 6095:                         ($env{'form.'.$item} eq '1')) {
 6096:                         $changes{$item} = 1;
 6097:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 6098:                               $domconfig{'login'}{$item} eq '1') &&
 6099:                              ($env{'form.'.$item} eq '0')) {
 6100:                         $changes{$item} = 1;
 6101:                     }
 6102:                 } elsif ($defaultchecked{$item} eq 'off') {
 6103:                     if (($domconfig{'login'}{$item} eq '1') &&
 6104:                         ($env{'form.'.$item} eq '0')) {
 6105:                         $changes{$item} = 1;
 6106:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 6107:                               $domconfig{'login'}{$item} eq '0') &&
 6108:                              ($env{'form.'.$item} eq '1')) {
 6109:                         $changes{$item} = 1;
 6110:                     }
 6111:                 }
 6112:             }
 6113:         }
 6114:         if (keys(%changes) > 0 || $colchgtext) {
 6115:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6116:             if (ref($lastactref) eq 'HASH') {
 6117:                 $lastactref->{'domainconfig'} = 1;
 6118:             }
 6119:             $resulttext = &mt('Changes made:').'<ul>';
 6120:             foreach my $item (sort(keys(%changes))) {
 6121:                 if ($item eq 'loginvia') {
 6122:                     if (ref($changes{$item}) eq 'HASH') {
 6123:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 6124:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 6125:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 6126:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 6127:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 6128:                                     $protocol = 'http' if ($protocol ne 'https');
 6129:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 6130: 
 6131:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 6132:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 6133:                                     } else {
 6134:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 6135:                                     }
 6136:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 6137:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 6138:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 6139:                                     }
 6140:                                     $resulttext .= '</li>';
 6141:                                 } else {
 6142:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 6143:                                 }
 6144:                             } else {
 6145:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 6146:                             }
 6147:                         }
 6148:                         $resulttext .= '</ul></li>';
 6149:                     }
 6150:                 } elsif ($item eq 'helpurl') {
 6151:                     if (ref($changes{$item}) eq 'HASH') {
 6152:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 6153:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 6154:                                 my ($chg,$link);
 6155:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 6156:                                 if ($lang eq 'nolang') {
 6157:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 6158:                                 } else {
 6159:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 6160:                                 }
 6161:                                 $resulttext .= '<li>'.$chg.'</li>';
 6162:                             } else {
 6163:                                 my $chg;
 6164:                                 if ($lang eq 'nolang') {
 6165:                                     $chg = &mt('custom log-in help file for no preferred language');
 6166:                                 } else {
 6167:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 6168:                                 }
 6169:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 6170:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 6171:                                                       '?inhibitmenu=yes',$chg,600,500).
 6172:                                                '</li>';
 6173:                             }
 6174:                         }
 6175:                     }
 6176:                 } elsif ($item eq 'headtag') {
 6177:                     if (ref($changes{$item}) eq 'HASH') {
 6178:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 6179:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 6180:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 6181:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 6182:                                 $resulttext .= '<li><a href="'.
 6183:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 6184:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 6185:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 6186:                                 if ($possexempt{$lonhost}) {
 6187:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 6188:                                 } else {
 6189:                                     $resulttext .= &mt('included for any client IP');
 6190:                                 }
 6191:                                 $resulttext .= '</li>';
 6192:                             }
 6193:                         }
 6194:                     }
 6195:                 } elsif ($item eq 'captcha') {
 6196:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6197:                         my $chgtxt;
 6198:                         if ($loginhash{'login'}{$item} eq 'notused') {
 6199:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 6200:                         } else {
 6201:                             my %captchas = &captcha_phrases();
 6202:                             if ($captchas{$loginhash{'login'}{$item}}) {
 6203:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 6204:                             } else {
 6205:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 6206:                             }
 6207:                         }
 6208:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6209:                     }
 6210:                 } elsif ($item eq 'recaptchakeys') {
 6211:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6212:                         my ($privkey,$pubkey);
 6213:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 6214:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 6215:                             $privkey = $loginhash{'login'}{$item}{'private'};
 6216:                         }
 6217:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 6218:                         if (!$pubkey) {
 6219:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 6220:                         } else {
 6221:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6222:                         }
 6223:                         if (!$privkey) {
 6224:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 6225:                         } else {
 6226:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 6227:                         }
 6228:                         $chgtxt .= '</ul>';
 6229:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6230:                     }
 6231:                 } else {
 6232:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 6233:                 }
 6234:             }
 6235:             $resulttext .= $colchgtext.'</ul>';
 6236:         } else {
 6237:             $resulttext = &mt('No changes made to log-in page settings');
 6238:         }
 6239:     } else {
 6240:         $resulttext = '<span class="LC_error">'.
 6241: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6242:     }
 6243:     if ($errors) {
 6244:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 6245:                        $errors.'</ul>';
 6246:     }
 6247:     return $resulttext;
 6248: }
 6249: 
 6250: sub check_exempt_addresses {
 6251:     my ($iplist) = @_;
 6252:     $iplist =~ s/^\s+//;
 6253:     $iplist =~ s/\s+$//;
 6254:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 6255:     my (@okips,$new);
 6256:     foreach my $ip (@poss_ips) {
 6257:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 6258:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 6259:                 push(@okips,$ip);
 6260:             }
 6261:         }
 6262:     }
 6263:     if (@okips > 0) {
 6264:         $new = join(',',@okips);
 6265:     } else {
 6266:         $new = '';
 6267:     }
 6268:     return $new;
 6269: }
 6270: 
 6271: sub color_font_choices {
 6272:     my %choices =
 6273:         &Apache::lonlocal::texthash (
 6274:             img => "Header",
 6275:             bgs => "Background colors",
 6276:             links => "Link colors",
 6277:             images => "Images",
 6278:             font => "Font color",
 6279:             fontmenu => "Font menu",
 6280:             pgbg => "Page",
 6281:             tabbg => "Header",
 6282:             sidebg => "Border",
 6283:             link => "Link",
 6284:             alink => "Active link",
 6285:             vlink => "Visited link",
 6286:         );
 6287:     return %choices;
 6288: }
 6289: 
 6290: sub modify_rolecolors {
 6291:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 6292:     my ($resulttext,%rolehash);
 6293:     $rolehash{'rolecolors'} = {};
 6294:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 6295:         if ($domconfig{'rolecolors'} eq '') {
 6296:             $domconfig{'rolecolors'} = {};
 6297:         }
 6298:     }
 6299:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 6300:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 6301:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 6302:                                              $dom);
 6303:     if ($putresult eq 'ok') {
 6304:         if (keys(%changes) > 0) {
 6305:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6306:             if (ref($lastactref) eq 'HASH') {
 6307:                 $lastactref->{'domainconfig'} = 1;
 6308:             }
 6309:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 6310:                                              $rolehash{'rolecolors'});
 6311:         } else {
 6312:             $resulttext = &mt('No changes made to default color schemes');
 6313:         }
 6314:     } else {
 6315:         $resulttext = '<span class="LC_error">'.
 6316: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6317:     }
 6318:     if ($errors) {
 6319:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6320:                        $errors.'</ul>';
 6321:     }
 6322:     return $resulttext;
 6323: }
 6324: 
 6325: sub modify_colors {
 6326:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 6327:     my (%changes,%choices);
 6328:     my @bgs;
 6329:     my @links = ('link','alink','vlink');
 6330:     my @logintext;
 6331:     my @images;
 6332:     my $servadm = $r->dir_config('lonAdmEMail');
 6333:     my $errors;
 6334:     my %defaults;
 6335:     foreach my $role (@{$roles}) {
 6336:         if ($role eq 'login') {
 6337:             %choices = &login_choices();
 6338:             @logintext = ('textcol','bgcol');
 6339:         } else {
 6340:             %choices = &color_font_choices();
 6341:         }
 6342:         if ($role eq 'login') {
 6343:             @images = ('img','logo','domlogo','login');
 6344:             @bgs = ('pgbg','mainbg','sidebg');
 6345:         } else {
 6346:             @images = ('img');
 6347:             @bgs = ('pgbg','tabbg','sidebg');
 6348:         }
 6349:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 6350:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 6351:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 6352:         }
 6353:         if ($role eq 'login') {
 6354:             foreach my $item (@logintext) {
 6355:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6356:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6357:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6358:                 }
 6359:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 6360:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6361:                 }
 6362:             }
 6363:         } else {
 6364:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 6365:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 6366:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 6367:             }
 6368:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 6369:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 6370:             }
 6371:         }
 6372:         foreach my $item (@bgs) {
 6373:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6374:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6375:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6376:             }
 6377:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 6378:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6379:             }
 6380:         }
 6381:         foreach my $item (@links) {
 6382:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6383:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6384:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6385:             }
 6386:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 6387:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6388:             }
 6389:         }
 6390:         my ($configuserok,$author_ok,$switchserver) = 
 6391:             &config_check($dom,$confname,$servadm);
 6392:         my ($width,$height) = &thumb_dimensions();
 6393:         if (ref($domconfig->{$role}) ne 'HASH') {
 6394:             $domconfig->{$role} = {};
 6395:         }
 6396:         foreach my $img (@images) {
 6397:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 6398:                 if (defined($env{'form.login_showlogo_'.$img})) {
 6399:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 6400:                 } else { 
 6401:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 6402:                 }
 6403:             } 
 6404: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 6405: 		 && !defined($domconfig->{$role}{$img})
 6406: 		 && !$env{'form.'.$role.'_del_'.$img}
 6407: 		 && $env{'form.'.$role.'_import_'.$img}) {
 6408: 		# import the old configured image from the .tab setting
 6409: 		# if they haven't provided a new one 
 6410: 		$domconfig->{$role}{$img} = 
 6411: 		    $env{'form.'.$role.'_import_'.$img};
 6412: 	    }
 6413:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 6414:                 my $error;
 6415:                 if ($configuserok eq 'ok') {
 6416:                     if ($switchserver) {
 6417:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 6418:                     } else {
 6419:                         if ($author_ok eq 'ok') {
 6420:                             my ($result,$logourl) = 
 6421:                                 &publishlogo($r,'upload',$role.'_'.$img,
 6422:                                            $dom,$confname,$img,$width,$height);
 6423:                             if ($result eq 'ok') {
 6424:                                 $confhash->{$role}{$img} = $logourl;
 6425:                                 $changes{$role}{'images'}{$img} = 1;
 6426:                             } else {
 6427:                                 $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);
 6428:                             }
 6429:                         } else {
 6430:                             $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);
 6431:                         }
 6432:                     }
 6433:                 } else {
 6434:                     $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);
 6435:                 }
 6436:                 if ($error) {
 6437:                     &Apache::lonnet::logthis($error);
 6438:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6439:                 }
 6440:             } elsif ($domconfig->{$role}{$img} ne '') {
 6441:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 6442:                     my $error;
 6443:                     if ($configuserok eq 'ok') {
 6444: # is confname an author?
 6445:                         if ($switchserver eq '') {
 6446:                             if ($author_ok eq 'ok') {
 6447:                                 my ($result,$logourl) = 
 6448:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 6449:                                             $dom,$confname,$img,$width,$height);
 6450:                                 if ($result eq 'ok') {
 6451:                                     $confhash->{$role}{$img} = $logourl;
 6452: 				    $changes{$role}{'images'}{$img} = 1;
 6453:                                 }
 6454:                             }
 6455:                         }
 6456:                     }
 6457:                 }
 6458:             }
 6459:         }
 6460:         if (ref($domconfig) eq 'HASH') {
 6461:             if (ref($domconfig->{$role}) eq 'HASH') {
 6462:                 foreach my $img (@images) {
 6463:                     if ($domconfig->{$role}{$img} ne '') {
 6464:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6465:                             $confhash->{$role}{$img} = '';
 6466:                             $changes{$role}{'images'}{$img} = 1;
 6467:                         } else {
 6468:                             if ($confhash->{$role}{$img} eq '') {
 6469:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 6470:                             }
 6471:                         }
 6472:                     } else {
 6473:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6474:                             $confhash->{$role}{$img} = '';
 6475:                             $changes{$role}{'images'}{$img} = 1;
 6476:                         } 
 6477:                     }
 6478:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 6479:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 6480:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 6481:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 6482:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 6483:                             }
 6484:                         } else {
 6485:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6486:                                 $changes{$role}{'showlogo'}{$img} = 1;
 6487:                             }
 6488:                         }
 6489:                     }
 6490:                 }
 6491:                 if ($domconfig->{$role}{'font'} ne '') {
 6492:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 6493:                         $changes{$role}{'font'} = 1;
 6494:                     }
 6495:                 } else {
 6496:                     if ($confhash->{$role}{'font'}) {
 6497:                         $changes{$role}{'font'} = 1;
 6498:                     }
 6499:                 }
 6500:                 if ($role ne 'login') {
 6501:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 6502:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 6503:                             $changes{$role}{'fontmenu'} = 1;
 6504:                         }
 6505:                     } else {
 6506:                         if ($confhash->{$role}{'fontmenu'}) {
 6507:                             $changes{$role}{'fontmenu'} = 1;
 6508:                         }
 6509:                     }
 6510:                 }
 6511:                 foreach my $item (@bgs) {
 6512:                     if ($domconfig->{$role}{$item} ne '') {
 6513:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6514:                             $changes{$role}{'bgs'}{$item} = 1;
 6515:                         } 
 6516:                     } else {
 6517:                         if ($confhash->{$role}{$item}) {
 6518:                             $changes{$role}{'bgs'}{$item} = 1;
 6519:                         }
 6520:                     }
 6521:                 }
 6522:                 foreach my $item (@links) {
 6523:                     if ($domconfig->{$role}{$item} ne '') {
 6524:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6525:                             $changes{$role}{'links'}{$item} = 1;
 6526:                         }
 6527:                     } else {
 6528:                         if ($confhash->{$role}{$item}) {
 6529:                             $changes{$role}{'links'}{$item} = 1;
 6530:                         }
 6531:                     }
 6532:                 }
 6533:                 foreach my $item (@logintext) {
 6534:                     if ($domconfig->{$role}{$item} ne '') {
 6535:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6536:                             $changes{$role}{'logintext'}{$item} = 1;
 6537:                         }
 6538:                     } else {
 6539:                         if ($confhash->{$role}{$item}) {
 6540:                             $changes{$role}{'logintext'}{$item} = 1;
 6541:                         }
 6542:                     }
 6543:                 }
 6544:             } else {
 6545:                 &default_change_checker($role,\@images,\@links,\@bgs,
 6546:                                         \@logintext,$confhash,\%changes); 
 6547:             }
 6548:         } else {
 6549:             &default_change_checker($role,\@images,\@links,\@bgs,
 6550:                                     \@logintext,$confhash,\%changes); 
 6551:         }
 6552:     }
 6553:     return ($errors,%changes);
 6554: }
 6555: 
 6556: sub config_check {
 6557:     my ($dom,$confname,$servadm) = @_;
 6558:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6559:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6560:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6561:                                                    $confname,$servadm);
 6562:     if ($configuserok eq 'ok') {
 6563:         $switchserver = &check_switchserver($dom,$confname);
 6564:         if ($switchserver eq '') {
 6565:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6566:         }
 6567:     }
 6568:     return ($configuserok,$author_ok,$switchserver);
 6569: }
 6570: 
 6571: sub default_change_checker {
 6572:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6573:     foreach my $item (@{$links}) {
 6574:         if ($confhash->{$role}{$item}) {
 6575:             $changes->{$role}{'links'}{$item} = 1;
 6576:         }
 6577:     }
 6578:     foreach my $item (@{$bgs}) {
 6579:         if ($confhash->{$role}{$item}) {
 6580:             $changes->{$role}{'bgs'}{$item} = 1;
 6581:         }
 6582:     }
 6583:     foreach my $item (@{$logintext}) {
 6584:         if ($confhash->{$role}{$item}) {
 6585:             $changes->{$role}{'logintext'}{$item} = 1;
 6586:         }
 6587:     }
 6588:     foreach my $img (@{$images}) {
 6589:         if ($env{'form.'.$role.'_del_'.$img}) {
 6590:             $confhash->{$role}{$img} = '';
 6591:             $changes->{$role}{'images'}{$img} = 1;
 6592:         }
 6593:         if ($role eq 'login') {
 6594:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6595:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6596:             }
 6597:         }
 6598:     }
 6599:     if ($confhash->{$role}{'font'}) {
 6600:         $changes->{$role}{'font'} = 1;
 6601:     }
 6602: }
 6603: 
 6604: sub display_colorchgs {
 6605:     my ($dom,$changes,$roles,$confhash) = @_;
 6606:     my (%choices,$resulttext);
 6607:     if (!grep(/^login$/,@{$roles})) {
 6608:         $resulttext = &mt('Changes made:').'<br />';
 6609:     }
 6610:     foreach my $role (@{$roles}) {
 6611:         if ($role eq 'login') {
 6612:             %choices = &login_choices();
 6613:         } else {
 6614:             %choices = &color_font_choices();
 6615:         }
 6616:         if (ref($changes->{$role}) eq 'HASH') {
 6617:             if ($role ne 'login') {
 6618:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6619:             }
 6620:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6621:                 if ($role ne 'login') {
 6622:                     $resulttext .= '<ul>';
 6623:                 }
 6624:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6625:                     if ($role ne 'login') {
 6626:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6627:                     }
 6628:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6629:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6630:                             if ($confhash->{$role}{$key}{$item}) {
 6631:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6632:                             } else {
 6633:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6634:                             }
 6635:                         } elsif ($confhash->{$role}{$item} eq '') {
 6636:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6637:                         } else {
 6638:                             my $newitem = $confhash->{$role}{$item};
 6639:                             if ($key eq 'images') {
 6640:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6641:                             }
 6642:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6643:                         }
 6644:                     }
 6645:                     if ($role ne 'login') {
 6646:                         $resulttext .= '</ul></li>';
 6647:                     }
 6648:                 } else {
 6649:                     if ($confhash->{$role}{$key} eq '') {
 6650:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6651:                     } else {
 6652:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6653:                     }
 6654:                 }
 6655:                 if ($role ne 'login') {
 6656:                     $resulttext .= '</ul>';
 6657:                 }
 6658:             }
 6659:         }
 6660:     }
 6661:     return $resulttext;
 6662: }
 6663: 
 6664: sub thumb_dimensions {
 6665:     return ('200','50');
 6666: }
 6667: 
 6668: sub check_dimensions {
 6669:     my ($inputfile) = @_;
 6670:     my ($fullwidth,$fullheight);
 6671:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6672:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6673:             my $imageinfo = <PIPE>;
 6674:             if (!close(PIPE)) {
 6675:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6676:             }
 6677:             chomp($imageinfo);
 6678:             my ($fullsize) = 
 6679:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6680:             if ($fullsize) {
 6681:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6682:             }
 6683:         }
 6684:     }
 6685:     return ($fullwidth,$fullheight);
 6686: }
 6687: 
 6688: sub check_configuser {
 6689:     my ($uhome,$dom,$confname,$servadm) = @_;
 6690:     my ($configuserok,%currroles);
 6691:     if ($uhome eq 'no_host') {
 6692:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6693:         my $configpass = &LONCAPA::Enrollment::create_password();
 6694:         $configuserok = 
 6695:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6696:                              $configpass,'','','','','',undef,$servadm);
 6697:     } else {
 6698:         $configuserok = 'ok';
 6699:         %currroles = 
 6700:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6701:     }
 6702:     return ($configuserok,%currroles);
 6703: }
 6704: 
 6705: sub check_authorstatus {
 6706:     my ($dom,$confname,%currroles) = @_;
 6707:     my $author_ok;
 6708:     if (!$currroles{':'.$dom.':au'}) {
 6709:         my $start = time;
 6710:         my $end = 0;
 6711:         $author_ok = 
 6712:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6713:                                         'au',$end,$start,'','','domconfig');
 6714:     } else {
 6715:         $author_ok = 'ok';
 6716:     }
 6717:     return $author_ok;
 6718: }
 6719: 
 6720: sub publishlogo {
 6721:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6722:     my ($output,$fname,$logourl);
 6723:     if ($action eq 'upload') {
 6724:         $fname=$env{'form.'.$formname.'.filename'};
 6725:         chop($env{'form.'.$formname});
 6726:     } else {
 6727:         ($fname) = ($formname =~ /([^\/]+)$/);
 6728:     }
 6729:     if ($savefileas ne '') {
 6730:         $fname = $savefileas;
 6731:     }
 6732:     $fname=&Apache::lonnet::clean_filename($fname);
 6733: # See if there is anything left
 6734:     unless ($fname) { return ('error: no uploaded file'); }
 6735:     $fname="$subdir/$fname";
 6736:     my $docroot=$r->dir_config('lonDocRoot');
 6737:     my $filepath="$docroot/priv";
 6738:     my $relpath = "$dom/$confname";
 6739:     my ($fnamepath,$file,$fetchthumb);
 6740:     $file=$fname;
 6741:     if ($fname=~m|/|) {
 6742:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6743:     }
 6744:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6745:     my $count;
 6746:     for ($count=5;$count<=$#parts;$count++) {
 6747:         $filepath.="/$parts[$count]";
 6748:         if ((-e $filepath)!=1) {
 6749:             mkdir($filepath,02770);
 6750:         }
 6751:     }
 6752:     # Check for bad extension and disallow upload
 6753:     if ($file=~/\.(\w+)$/ &&
 6754:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6755:         $output = 
 6756:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6757:     } elsif ($file=~/\.(\w+)$/ &&
 6758:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6759:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6760:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6761:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6762:     } elsif (-d "$filepath/$file") {
 6763:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6764:     } else {
 6765:         my $source = $filepath.'/'.$file;
 6766:         my $logfile;
 6767:         if (!open($logfile,">>$source".'.log')) {
 6768:             return (&mt('No write permission to Authoring Space'));
 6769:         }
 6770:         print $logfile
 6771: "\n================= Publish ".localtime()." ================\n".
 6772: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6773: # Save the file
 6774:         if (!open(FH,'>'.$source)) {
 6775:             &Apache::lonnet::logthis('Failed to create '.$source);
 6776:             return (&mt('Failed to create file'));
 6777:         }
 6778:         if ($action eq 'upload') {
 6779:             if (!print FH ($env{'form.'.$formname})) {
 6780:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6781:                 return (&mt('Failed to write file'));
 6782:             }
 6783:         } else {
 6784:             my $original = &Apache::lonnet::filelocation('',$formname);
 6785:             if(!copy($original,$source)) {
 6786:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6787:                 return (&mt('Failed to write file'));
 6788:             }
 6789:         }
 6790:         close(FH);
 6791:         chmod(0660, $source); # Permissions to rw-rw---.
 6792: 
 6793:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6794:         my $copyfile=$targetdir.'/'.$file;
 6795: 
 6796:         my @parts=split(/\//,$targetdir);
 6797:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6798:         for (my $count=5;$count<=$#parts;$count++) {
 6799:             $path.="/$parts[$count]";
 6800:             if (!-e $path) {
 6801:                 print $logfile "\nCreating directory ".$path;
 6802:                 mkdir($path,02770);
 6803:             }
 6804:         }
 6805:         my $versionresult;
 6806:         if (-e $copyfile) {
 6807:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6808:         } else {
 6809:             $versionresult = 'ok';
 6810:         }
 6811:         if ($versionresult eq 'ok') {
 6812:             if (copy($source,$copyfile)) {
 6813:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6814:                 $output = 'ok';
 6815:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6816:                 push(@{$modified_urls},[$copyfile,$source]);
 6817:                 my $metaoutput = 
 6818:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6819:                 unless ($registered_cleanup) {
 6820:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6821:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6822:                     $registered_cleanup=1;
 6823:                 }
 6824:             } else {
 6825:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6826:                 $output = &mt('Failed to copy file to RES space').", $!";
 6827:             }
 6828:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6829:                 my $inputfile = $filepath.'/'.$file;
 6830:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6831:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6832:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6833:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6834:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6835:                         system("convert -sample $thumbsize $inputfile $outfile");
 6836:                         chmod(0660, $filepath.'/tn-'.$file);
 6837:                         if (-e $outfile) {
 6838:                             my $copyfile=$targetdir.'/tn-'.$file;
 6839:                             if (copy($outfile,$copyfile)) {
 6840:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6841:                                 my $thumb_metaoutput = 
 6842:                                     &write_metadata($dom,$confname,$formname,
 6843:                                                     $targetdir,'tn-'.$file,$logfile);
 6844:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6845:                                 unless ($registered_cleanup) {
 6846:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6847:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6848:                                     $registered_cleanup=1;
 6849:                                 }
 6850:                             } else {
 6851:                                 print $logfile "\nUnable to write ".$copyfile.
 6852:                                                ':'.$!."\n";
 6853:                             }
 6854:                         }
 6855:                     }
 6856:                 }
 6857:             }
 6858:         } else {
 6859:             $output = $versionresult;
 6860:         }
 6861:     }
 6862:     return ($output,$logourl);
 6863: }
 6864: 
 6865: sub logo_versioning {
 6866:     my ($targetdir,$file,$logfile) = @_;
 6867:     my $target = $targetdir.'/'.$file;
 6868:     my ($maxversion,$fn,$extn,$output);
 6869:     $maxversion = 0;
 6870:     if ($file =~ /^(.+)\.(\w+)$/) {
 6871:         $fn=$1;
 6872:         $extn=$2;
 6873:     }
 6874:     opendir(DIR,$targetdir);
 6875:     while (my $filename=readdir(DIR)) {
 6876:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6877:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6878:         }
 6879:     }
 6880:     $maxversion++;
 6881:     print $logfile "\nCreating old version ".$maxversion."\n";
 6882:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6883:     if (copy($target,$copyfile)) {
 6884:         print $logfile "Copied old target to ".$copyfile."\n";
 6885:         $copyfile=$copyfile.'.meta';
 6886:         if (copy($target.'.meta',$copyfile)) {
 6887:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6888:             $output = 'ok';
 6889:         } else {
 6890:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6891:             $output = &mt('Failed to copy old meta').", $!, ";
 6892:         }
 6893:     } else {
 6894:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6895:         $output = &mt('Failed to copy old target').", $!, ";
 6896:     }
 6897:     return $output;
 6898: }
 6899: 
 6900: sub write_metadata {
 6901:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6902:     my (%metadatafields,%metadatakeys,$output);
 6903:     $metadatafields{'title'}=$formname;
 6904:     $metadatafields{'creationdate'}=time;
 6905:     $metadatafields{'lastrevisiondate'}=time;
 6906:     $metadatafields{'copyright'}='public';
 6907:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6908:                                          $env{'user.domain'};
 6909:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6910:     $metadatafields{'domain'}=$dom;
 6911:     {
 6912:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6913:         my $mfh;
 6914:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6915:             foreach (sort(keys(%metadatafields))) {
 6916:                 unless ($_=~/\./) {
 6917:                     my $unikey=$_;
 6918:                     $unikey=~/^([A-Za-z]+)/;
 6919:                     my $tag=$1;
 6920:                     $tag=~tr/A-Z/a-z/;
 6921:                     print $mfh "\n\<$tag";
 6922:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6923:                         my $value=$metadatafields{$unikey.'.'.$_};
 6924:                         $value=~s/\"/\'\'/g;
 6925:                         print $mfh ' '.$_.'="'.$value.'"';
 6926:                     }
 6927:                     print $mfh '>'.
 6928:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6929:                             .'</'.$tag.'>';
 6930:                 }
 6931:             }
 6932:             $output = 'ok';
 6933:             print $logfile "\nWrote metadata";
 6934:             close($mfh);
 6935:         } else {
 6936:             print $logfile "\nFailed to open metadata file";
 6937:             $output = &mt('Could not write metadata');
 6938:         }
 6939:     }
 6940:     return $output;
 6941: }
 6942: 
 6943: sub notifysubscribed {
 6944:     foreach my $targetsource (@{$modified_urls}){
 6945:         next unless (ref($targetsource) eq 'ARRAY');
 6946:         my ($target,$source)=@{$targetsource};
 6947:         if ($source ne '') {
 6948:             if (open(my $logfh,'>>'.$source.'.log')) {
 6949:                 print $logfh "\nCleanup phase: Notifications\n";
 6950:                 my @subscribed=&subscribed_hosts($target);
 6951:                 foreach my $subhost (@subscribed) {
 6952:                     print $logfh "\nNotifying host ".$subhost.':';
 6953:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6954:                     print $logfh $reply;
 6955:                 }
 6956:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6957:                 foreach my $subhost (@subscribedmeta) {
 6958:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6959:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6960:                                                         $subhost);
 6961:                     print $logfh $reply;
 6962:                 }
 6963:                 print $logfh "\n============ Done ============\n";
 6964:                 close($logfh);
 6965:             }
 6966:         }
 6967:     }
 6968:     return OK;
 6969: }
 6970: 
 6971: sub subscribed_hosts {
 6972:     my ($target) = @_;
 6973:     my @subscribed;
 6974:     if (open(my $fh,"<$target.subscription")) {
 6975:         while (my $subline=<$fh>) {
 6976:             if ($subline =~ /^($match_lonid):/) {
 6977:                 my $host = $1;
 6978:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6979:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6980:                         push(@subscribed,$host);
 6981:                     }
 6982:                 }
 6983:             }
 6984:         }
 6985:     }
 6986:     return @subscribed;
 6987: }
 6988: 
 6989: sub check_switchserver {
 6990:     my ($dom,$confname) = @_;
 6991:     my ($allowed,$switchserver);
 6992:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6993:     if ($home eq 'no_host') {
 6994:         $home = &Apache::lonnet::domain($dom,'primary');
 6995:     }
 6996:     my @ids=&Apache::lonnet::current_machine_ids();
 6997:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6998:     if (!$allowed) {
 6999: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 7000:     }
 7001:     return $switchserver;
 7002: }
 7003: 
 7004: sub modify_quotas {
 7005:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 7006:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 7007:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 7008:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 7009:         $validationfieldsref);
 7010:     if ($action eq 'quotas') {
 7011:         $context = 'tools'; 
 7012:     } else {
 7013:         $context = $action;
 7014:     }
 7015:     if ($context eq 'requestcourses') {
 7016:         @usertools = ('official','unofficial','community','textbook');
 7017:         @options =('norequest','approval','validate','autolimit');
 7018:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 7019:         %titles = &courserequest_titles();
 7020:         $toolregexp = join('|',@usertools);
 7021:         %conditions = &courserequest_conditions();
 7022:         $confname = $dom.'-domainconfig';
 7023:         my $servadm = $r->dir_config('lonAdmEMail');
 7024:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7025:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 7026:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7027:     } elsif ($context eq 'requestauthor') {
 7028:         @usertools = ('author');
 7029:         %titles = &authorrequest_titles();
 7030:     } else {
 7031:         @usertools = ('aboutme','blog','webdav','portfolio');
 7032:         %titles = &tool_titles();
 7033:     }
 7034:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7035:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7036:     foreach my $key (keys(%env)) {
 7037:         if ($context eq 'requestcourses') {
 7038:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 7039:                 my $item = $1;
 7040:                 my $type = $2;
 7041:                 if ($type =~ /^limit_(.+)/) {
 7042:                     $limithash{$item}{$1} = $env{$key};
 7043:                 } else {
 7044:                     $confhash{$item}{$type} = $env{$key};
 7045:                 }
 7046:             }
 7047:         } elsif ($context eq 'requestauthor') {
 7048:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 7049:                 $confhash{$1} = $env{$key};
 7050:             }
 7051:         } else {
 7052:             if ($key =~ /^form\.quota_(.+)$/) {
 7053:                 $confhash{'defaultquota'}{$1} = $env{$key};
 7054:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 7055:                 $confhash{'authorquota'}{$1} = $env{$key};
 7056:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 7057:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 7058:             }
 7059:         }
 7060:     }
 7061:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7062:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 7063:         @approvalnotify = sort(@approvalnotify);
 7064:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 7065:         my @crstypes = ('official','unofficial','community','textbook');
 7066:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 7067:         foreach my $type (@hasuniquecode) {
 7068:             if (grep(/^\Q$type\E$/,@crstypes)) {
 7069:                 $confhash{'uniquecode'}{$type} = 1;
 7070:             }
 7071:         }
 7072:         my (%newbook,%allpos);
 7073:         if ($context eq 'requestcourses') {
 7074:             foreach my $type ('textbooks','templates') {
 7075:                 @{$allpos{$type}} = (); 
 7076:                 my $invalid;
 7077:                 if ($type eq 'textbooks') {
 7078:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 7079:                 } else {
 7080:                     $invalid = &mt('Invalid LON-CAPA course for template');
 7081:                 }
 7082:                 if ($env{'form.'.$type.'_addbook'}) {
 7083:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 7084:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 7085:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 7086:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 7087:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 7088:                         } else {
 7089:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 7090:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 7091:                             $position =~ s/\D+//g;
 7092:                             if ($position ne '') {
 7093:                                 $allpos{$type}[$position] = $newbook{$type};
 7094:                             }
 7095:                         }
 7096:                     } else {
 7097:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 7098:                     }
 7099:                 }
 7100:             } 
 7101:         }
 7102:         if (ref($domconfig{$action}) eq 'HASH') {
 7103:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 7104:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 7105:                     $changes{'notify'}{'approval'} = 1;
 7106:                 }
 7107:             } else {
 7108:                 if ($confhash{'notify'}{'approval'}) {
 7109:                     $changes{'notify'}{'approval'} = 1;
 7110:                 }
 7111:             }
 7112:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 7113:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7114:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 7115:                         unless ($confhash{'uniquecode'}{$crstype}) {
 7116:                             $changes{'uniquecode'} = 1;
 7117:                         }
 7118:                     }
 7119:                     unless ($changes{'uniquecode'}) {
 7120:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 7121:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 7122:                                 $changes{'uniquecode'} = 1;
 7123:                             }
 7124:                         }
 7125:                     }
 7126:                } else {
 7127:                    $changes{'uniquecode'} = 1;
 7128:                }
 7129:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 7130:                 $changes{'uniquecode'} = 1;
 7131:             }
 7132:             if ($context eq 'requestcourses') {
 7133:                 foreach my $type ('textbooks','templates') {
 7134:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7135:                         my %deletions;
 7136:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 7137:                         if (@todelete) {
 7138:                             map { $deletions{$_} = 1; } @todelete;
 7139:                         }
 7140:                         my %imgdeletions;
 7141:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 7142:                         if (@todeleteimages) {
 7143:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 7144:                         }
 7145:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 7146:                         for (my $i=0; $i<=$maxnum; $i++) {
 7147:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 7148:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 7149:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 7150:                                 if ($deletions{$key}) {
 7151:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 7152:                                         #FIXME need to obsolete item in RES space
 7153:                                     }
 7154:                                     next;
 7155:                                 } else {
 7156:                                     my $newpos = $env{'form.'.$itemid};
 7157:                                     $newpos =~ s/\D+//g;
 7158:                                     foreach my $item ('subject','title','publisher','author') {
 7159:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7160:                                                  ($type eq 'templates'));
 7161:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 7162:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 7163:                                             $changes{$type}{$key} = 1;
 7164:                                         }
 7165:                                     }
 7166:                                     $allpos{$type}[$newpos] = $key;
 7167:                                 }
 7168:                                 if ($imgdeletions{$key}) {
 7169:                                     $changes{$type}{$key} = 1;
 7170:                                     #FIXME need to obsolete item in RES space
 7171:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 7172:                                     my ($cdom,$cnum) = split(/_/,$key);
 7173:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 7174:                                                                                   $cdom,$cnum,$type,$configuserok,
 7175:                                                                                   $switchserver,$author_ok);
 7176:                                     if ($imgurl) {
 7177:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 7178:                                         $changes{$type}{$key} = 1; 
 7179:                                     }
 7180:                                     if ($error) {
 7181:                                         &Apache::lonnet::logthis($error);
 7182:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7183:                                     } 
 7184:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 7185:                                     $confhash{$type}{$key}{'image'} = 
 7186:                                         $domconfig{$action}{$type}{$key}{'image'};
 7187:                                 }
 7188:                             }
 7189:                         }
 7190:                     }
 7191:                 }
 7192:             }
 7193:         } else {
 7194:             if ($confhash{'notify'}{'approval'}) {
 7195:                 $changes{'notify'}{'approval'} = 1;
 7196:             }
 7197:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 7198:                 $changes{'uniquecode'} = 1;
 7199:             }
 7200:         }
 7201:         if ($context eq 'requestcourses') {
 7202:             foreach my $type ('textbooks','templates') {
 7203:                 if ($newbook{$type}) {
 7204:                     $changes{$type}{$newbook{$type}} = 1;
 7205:                     foreach my $item ('subject','title','publisher','author') {
 7206:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7207:                                  ($type eq 'template'));
 7208:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 7209:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 7210:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 7211:                         }
 7212:                     }
 7213:                     if ($type eq 'textbooks') {
 7214:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 7215:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 7216:                             my ($imageurl,$error) =
 7217:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 7218:                                                         $configuserok,$switchserver,$author_ok);
 7219:                             if ($imageurl) {
 7220:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 7221:                             }
 7222:                             if ($error) {
 7223:                                 &Apache::lonnet::logthis($error);
 7224:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7225:                             }
 7226:                         }
 7227:                     }
 7228:                 }
 7229:                 if (@{$allpos{$type}} > 0) {
 7230:                     my $idx = 0;
 7231:                     foreach my $item (@{$allpos{$type}}) {
 7232:                         if ($item ne '') {
 7233:                             $confhash{$type}{$item}{'order'} = $idx;
 7234:                             if (ref($domconfig{$action}) eq 'HASH') {
 7235:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7236:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 7237:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 7238:                                             $changes{$type}{$item} = 1;
 7239:                                         }
 7240:                                     }
 7241:                                 }
 7242:                             }
 7243:                             $idx ++;
 7244:                         }
 7245:                     }
 7246:                 }
 7247:             }
 7248:             if (ref($validationitemsref) eq 'ARRAY') {
 7249:                 foreach my $item (@{$validationitemsref}) {
 7250:                     if ($item eq 'fields') {
 7251:                         my @changed;
 7252:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 7253:                         if (@{$confhash{'validation'}{$item}} > 0) {
 7254:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 7255:                         }
 7256:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7257:                             if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 7258:                                 @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 7259:                                                                               $domconfig{'requestcourses'}{'validation'}{$item});
 7260:                             } else {
 7261:                                 @changed = @{$confhash{'validation'}{$item}};
 7262:                             }
 7263:                         } else {
 7264:                             @changed = @{$confhash{'validation'}{$item}};
 7265:                         }
 7266:                         if (@changed) {
 7267:                             if ($confhash{'validation'}{$item}) {
 7268:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 7269:                             } else {
 7270:                                 $changes{'validation'}{$item} = &mt('None');
 7271:                             }
 7272:                         }
 7273:                     } else {
 7274:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 7275:                         if ($item eq 'markup') {
 7276:                             if ($env{'form.requestcourses_validation_'.$item}) {
 7277:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 7278:                             }
 7279:                         }
 7280:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7281:                             if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 7282:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7283:                             }
 7284:                         } else {
 7285:                             if ($confhash{'validation'}{$item} ne '') {
 7286:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7287:                             }
 7288:                         }
 7289:                     }
 7290:                 }
 7291:             }
 7292:             if ($env{'form.validationdc'}) {
 7293:                 my $newval = $env{'form.validationdc'};
 7294:                 my %domcoords = &get_active_dcs($dom);
 7295:                 if (exists($domcoords{$newval})) {
 7296:                     $confhash{'validation'}{'dc'} = $newval;
 7297:                 }
 7298:             }
 7299:             if (ref($confhash{'validation'}) eq 'HASH') {
 7300:                 if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7301:                     if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7302:                         unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7303:                             if ($confhash{'validation'}{'dc'} eq '') {
 7304:                                 $changes{'validation'}{'dc'} = &mt('None');
 7305:                             } else {
 7306:                                 $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7307:                             }
 7308:                         }
 7309:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 7310:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7311:                     }
 7312:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 7313:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7314:                 }
 7315:             } elsif (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7316:                 if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7317:                     $changes{'validation'}{'dc'} = &mt('None');
 7318:                 }
 7319:             }
 7320:         }
 7321:     } else {
 7322:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 7323:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 7324:     }
 7325:     foreach my $item (@usertools) {
 7326:         foreach my $type (@{$types},'default','_LC_adv') {
 7327:             my $unset; 
 7328:             if ($context eq 'requestcourses') {
 7329:                 $unset = '0';
 7330:                 if ($type eq '_LC_adv') {
 7331:                     $unset = '';
 7332:                 }
 7333:                 if ($confhash{$item}{$type} eq 'autolimit') {
 7334:                     $confhash{$item}{$type} .= '=';
 7335:                     unless ($limithash{$item}{$type} =~ /\D/) {
 7336:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 7337:                     }
 7338:                 }
 7339:             } elsif ($context eq 'requestauthor') {
 7340:                 $unset = '0';
 7341:                 if ($type eq '_LC_adv') {
 7342:                     $unset = '';
 7343:                 }
 7344:             } else {
 7345:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 7346:                     $confhash{$item}{$type} = 1;
 7347:                 } else {
 7348:                     $confhash{$item}{$type} = 0;
 7349:                 }
 7350:             }
 7351:             if (ref($domconfig{$action}) eq 'HASH') {
 7352:                 if ($action eq 'requestauthor') {
 7353:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 7354:                         $changes{$type} = 1;
 7355:                     }
 7356:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 7357:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 7358:                         $changes{$item}{$type} = 1;
 7359:                     }
 7360:                 } else {
 7361:                     if ($context eq 'requestcourses') {
 7362:                         if ($confhash{$item}{$type} ne $unset) {
 7363:                             $changes{$item}{$type} = 1;
 7364:                         }
 7365:                     } else {
 7366:                         if (!$confhash{$item}{$type}) {
 7367:                             $changes{$item}{$type} = 1;
 7368:                         }
 7369:                     }
 7370:                 }
 7371:             } else {
 7372:                 if ($context eq 'requestcourses') {
 7373:                     if ($confhash{$item}{$type} ne $unset) {
 7374:                         $changes{$item}{$type} = 1;
 7375:                     }
 7376:                 } elsif ($context eq 'requestauthor') {
 7377:                     if ($confhash{$type} ne $unset) {
 7378:                         $changes{$type} = 1;
 7379:                     }
 7380:                 } else {
 7381:                     if (!$confhash{$item}{$type}) {
 7382:                         $changes{$item}{$type} = 1;
 7383:                     }
 7384:                 }
 7385:             }
 7386:         }
 7387:     }
 7388:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7389:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 7390:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7391:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 7392:                     if (exists($confhash{'defaultquota'}{$key})) {
 7393:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 7394:                             $changes{'defaultquota'}{$key} = 1;
 7395:                         }
 7396:                     } else {
 7397:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 7398:                     }
 7399:                 }
 7400:             } else {
 7401:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 7402:                     if (exists($confhash{'defaultquota'}{$key})) {
 7403:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 7404:                             $changes{'defaultquota'}{$key} = 1;
 7405:                         }
 7406:                     } else {
 7407:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 7408:                     }
 7409:                 }
 7410:             }
 7411:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7412:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 7413:                     if (exists($confhash{'authorquota'}{$key})) {
 7414:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 7415:                             $changes{'authorquota'}{$key} = 1;
 7416:                         }
 7417:                     } else {
 7418:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 7419:                     }
 7420:                 }
 7421:             }
 7422:         }
 7423:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 7424:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 7425:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7426:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7427:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 7428:                             $changes{'defaultquota'}{$key} = 1;
 7429:                         }
 7430:                     } else {
 7431:                         if (!exists($domconfig{'quotas'}{$key})) {
 7432:                             $changes{'defaultquota'}{$key} = 1;
 7433:                         }
 7434:                     }
 7435:                 } else {
 7436:                     $changes{'defaultquota'}{$key} = 1;
 7437:                 }
 7438:             }
 7439:         }
 7440:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 7441:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 7442:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7443:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7444:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 7445:                             $changes{'authorquota'}{$key} = 1;
 7446:                         }
 7447:                     } else {
 7448:                         $changes{'authorquota'}{$key} = 1;
 7449:                     }
 7450:                 } else {
 7451:                     $changes{'authorquota'}{$key} = 1;
 7452:                 }
 7453:             }
 7454:         }
 7455:     }
 7456: 
 7457:     if ($context eq 'requestauthor') {
 7458:         $domdefaults{'requestauthor'} = \%confhash;
 7459:     } else {
 7460:         foreach my $key (keys(%confhash)) {
 7461:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 7462:                 $domdefaults{$key} = $confhash{$key};
 7463:             }
 7464:         }
 7465:     }
 7466: 
 7467:     my %quotahash = (
 7468:                       $action => { %confhash }
 7469:                     );
 7470:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 7471:                                              $dom);
 7472:     if ($putresult eq 'ok') {
 7473:         if (keys(%changes) > 0) {
 7474:             my $cachetime = 24*60*60;
 7475:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7476:             if (ref($lastactref) eq 'HASH') {
 7477:                 $lastactref->{'domdefaults'} = 1;
 7478:             }
 7479:             $resulttext = &mt('Changes made:').'<ul>';
 7480:             unless (($context eq 'requestcourses') ||
 7481:                     ($context eq 'requestauthor')) {
 7482:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 7483:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 7484:                     foreach my $type (@{$types},'default') {
 7485:                         if (defined($changes{'defaultquota'}{$type})) {
 7486:                             my $typetitle = $usertypes->{$type};
 7487:                             if ($type eq 'default') {
 7488:                                 $typetitle = $othertitle;
 7489:                             }
 7490:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 7491:                         }
 7492:                     }
 7493:                     $resulttext .= '</ul></li>';
 7494:                 }
 7495:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 7496:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 7497:                     foreach my $type (@{$types},'default') {
 7498:                         if (defined($changes{'authorquota'}{$type})) {
 7499:                             my $typetitle = $usertypes->{$type};
 7500:                             if ($type eq 'default') {
 7501:                                 $typetitle = $othertitle;
 7502:                             }
 7503:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 7504:                         }
 7505:                     }
 7506:                     $resulttext .= '</ul></li>';
 7507:                 }
 7508:             }
 7509:             my %newenv;
 7510:             foreach my $item (@usertools) {
 7511:                 my (%haschgs,%inconf);
 7512:                 if ($context eq 'requestauthor') {
 7513:                     %haschgs = %changes;
 7514:                     %inconf = %confhash;
 7515:                 } else {
 7516:                     if (ref($changes{$item}) eq 'HASH') {
 7517:                         %haschgs = %{$changes{$item}};
 7518:                     }
 7519:                     if (ref($confhash{$item}) eq 'HASH') {
 7520:                         %inconf = %{$confhash{$item}};
 7521:                     }
 7522:                 }
 7523:                 if (keys(%haschgs) > 0) {
 7524:                     my $newacc = 
 7525:                         &Apache::lonnet::usertools_access($env{'user.name'},
 7526:                                                           $env{'user.domain'},
 7527:                                                           $item,'reload',$context);
 7528:                     if (($context eq 'requestcourses') ||
 7529:                         ($context eq 'requestauthor')) {
 7530:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 7531:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 7532:                         }
 7533:                     } else {
 7534:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 7535:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 7536:                         }
 7537:                     }
 7538:                     unless ($context eq 'requestauthor') {
 7539:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 7540:                     }
 7541:                     foreach my $type (@{$types},'default','_LC_adv') {
 7542:                         if ($haschgs{$type}) {
 7543:                             my $typetitle = $usertypes->{$type};
 7544:                             if ($type eq 'default') {
 7545:                                 $typetitle = $othertitle;
 7546:                             } elsif ($type eq '_LC_adv') {
 7547:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 7548:                             }
 7549:                             if ($inconf{$type}) {
 7550:                                 if ($context eq 'requestcourses') {
 7551:                                     my $cond;
 7552:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 7553:                                         if ($1 eq '') {
 7554:                                             $cond = &mt('(Automatic processing of any request).');
 7555:                                         } else {
 7556:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 7557:                                         }
 7558:                                     } else { 
 7559:                                         $cond = $conditions{$inconf{$type}};
 7560:                                     }
 7561:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 7562:                                 } elsif ($context eq 'requestauthor') {
 7563:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 7564:                                                              $titles{$inconf{$type}},$typetitle);
 7565: 
 7566:                                 } else {
 7567:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7568:                                 }
 7569:                             } else {
 7570:                                 if ($type eq '_LC_adv') {
 7571:                                     if ($inconf{$type} eq '0') {
 7572:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7573:                                     } else { 
 7574:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7575:                                     }
 7576:                                 } else {
 7577:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7578:                                 }
 7579:                             }
 7580:                         }
 7581:                     }
 7582:                     unless ($context eq 'requestauthor') {
 7583:                         $resulttext .= '</ul></li>';
 7584:                     }
 7585:                 }
 7586:             }
 7587:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7588:                 if (ref($changes{'notify'}) eq 'HASH') {
 7589:                     if ($changes{'notify'}{'approval'}) {
 7590:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7591:                             if ($confhash{'notify'}{'approval'}) {
 7592:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7593:                             } else {
 7594:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7595:                             }
 7596:                         }
 7597:                     }
 7598:                 }
 7599:             }
 7600:             if ($action eq 'requestcourses') {
 7601:                 my @offon = ('off','on');
 7602:                 if ($changes{'uniquecode'}) {
 7603:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7604:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7605:                         $resulttext .= '<li>'.
 7606:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7607:                                        '</li>';
 7608:                     } else {
 7609:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7610:                                        '</li>';
 7611:                     }
 7612:                 }
 7613:                 foreach my $type ('textbooks','templates') {
 7614:                     if (ref($changes{$type}) eq 'HASH') {
 7615:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 7616:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 7617:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 7618:                             my $coursetitle = $coursehash{'description'};
 7619:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 7620:                             $resulttext .= '<li>';
 7621:                             foreach my $item ('subject','title','publisher','author') {
 7622:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7623:                                          ($type eq 'templates'));
 7624:                                 my $name = $item.':';
 7625:                                 $name =~ s/^(\w)/\U$1/;
 7626:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 7627:                             }
 7628:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7629:                             if ($type eq 'textbooks') {
 7630:                                 if ($confhash{$type}{$key}{'image'}) {
 7631:                                     $resulttext .= ' '.&mt('Image: [_1]',
 7632:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 7633:                                                    ' alt="Textbook cover" />').'<br />';
 7634:                                 }
 7635:                             }
 7636:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7637:                         }
 7638:                         $resulttext .= '</ul></li>';
 7639:                     }
 7640:                 }
 7641:                 if (ref($changes{'validation'}) eq 'HASH') {
 7642:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7643:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7644:                         foreach my $item (@{$validationitemsref}) {
 7645:                             if (exists($changes{'validation'}{$item})) {
 7646:                                 if ($item eq 'markup') {
 7647:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7648:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7649:                                 } else {
 7650:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7651:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7652:                                 }
 7653:                             }
 7654:                         }
 7655:                         if (exists($changes{'validation'}{'dc'})) {
 7656:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7657:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7658:                         }
 7659:                     }
 7660:                 }
 7661:             }
 7662:             $resulttext .= '</ul>';
 7663:             if (keys(%newenv)) {
 7664:                 &Apache::lonnet::appenv(\%newenv);
 7665:             }
 7666:         } else {
 7667:             if ($context eq 'requestcourses') {
 7668:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7669:             } elsif ($context eq 'requestauthor') {
 7670:                 $resulttext = &mt('No changes made to rights to request author space.');
 7671:             } else {
 7672:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7673:             }
 7674:         }
 7675:     } else {
 7676:         $resulttext = '<span class="LC_error">'.
 7677: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7678:     }
 7679:     if ($errors) {
 7680:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7681:                        '<ul>'.$errors.'</ul></p>';
 7682:     }
 7683:     return $resulttext;
 7684: }
 7685: 
 7686: sub process_textbook_image {
 7687:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 7688:     my $filename = $env{'form.'.$caller.'.filename'};
 7689:     my ($error,$url);
 7690:     my ($width,$height) = (50,50);
 7691:     if ($configuserok eq 'ok') {
 7692:         if ($switchserver) {
 7693:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7694:                          $switchserver);
 7695:         } elsif ($author_ok eq 'ok') {
 7696:             my ($result,$imageurl) =
 7697:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7698:                              "$type/$dom/$cnum/cover",$width,$height);
 7699:             if ($result eq 'ok') {
 7700:                 $url = $imageurl;
 7701:             } else {
 7702:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7703:             }
 7704:         } else {
 7705:             $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].",$filename,$confname,$dom,$author_ok);
 7706:         }
 7707:     } else {
 7708:         $error = &mt("Upload of [_1] failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$filename,$confname,$dom,$configuserok);
 7709:     }
 7710:     return ($url,$error);
 7711: }
 7712: 
 7713: sub modify_autoenroll {
 7714:     my ($dom,$lastactref,%domconfig) = @_;
 7715:     my ($resulttext,%changes);
 7716:     my %currautoenroll;
 7717:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7718:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7719:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7720:         }
 7721:     }
 7722:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7723:     my %title = ( run => 'Auto-enrollment active',
 7724:                   sender => 'Sender for notification messages',
 7725:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7726:     my @offon = ('off','on');
 7727:     my $sender_uname = $env{'form.sender_uname'};
 7728:     my $sender_domain = $env{'form.sender_domain'};
 7729:     if ($sender_domain eq '') {
 7730:         $sender_uname = '';
 7731:     } elsif ($sender_uname eq '') {
 7732:         $sender_domain = '';
 7733:     }
 7734:     my $coowners = $env{'form.autoassign_coowners'};
 7735:     my %autoenrollhash =  (
 7736:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7737:                                        'sender_uname' => $sender_uname,
 7738:                                        'sender_domain' => $sender_domain,
 7739:                                        'co-owners' => $coowners,
 7740:                                 }
 7741:                      );
 7742:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7743:                                              $dom);
 7744:     if ($putresult eq 'ok') {
 7745:         if (exists($currautoenroll{'run'})) {
 7746:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7747:                  $changes{'run'} = 1;
 7748:              }
 7749:         } elsif ($autorun) {
 7750:             if ($env{'form.autoenroll_run'} ne '1') {
 7751:                  $changes{'run'} = 1;
 7752:             }
 7753:         }
 7754:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7755:             $changes{'sender'} = 1;
 7756:         }
 7757:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7758:             $changes{'sender'} = 1;
 7759:         }
 7760:         if ($currautoenroll{'co-owners'} ne '') {
 7761:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7762:                 $changes{'coowners'} = 1;
 7763:             }
 7764:         } elsif ($coowners) {
 7765:             $changes{'coowners'} = 1;
 7766:         }      
 7767:         if (keys(%changes) > 0) {
 7768:             $resulttext = &mt('Changes made:').'<ul>';
 7769:             if ($changes{'run'}) {
 7770:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7771:             }
 7772:             if ($changes{'sender'}) {
 7773:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7774:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7775:                 } else {
 7776:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7777:                 }
 7778:             }
 7779:             if ($changes{'coowners'}) {
 7780:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7781:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7782:                 if (ref($lastactref) eq 'HASH') {
 7783:                     $lastactref->{'domainconfig'} = 1;
 7784:                 }
 7785:             }
 7786:             $resulttext .= '</ul>';
 7787:         } else {
 7788:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7789:         }
 7790:     } else {
 7791:         $resulttext = '<span class="LC_error">'.
 7792: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7793:     }
 7794:     return $resulttext;
 7795: }
 7796: 
 7797: sub modify_autoupdate {
 7798:     my ($dom,%domconfig) = @_;
 7799:     my ($resulttext,%currautoupdate,%fields,%changes);
 7800:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7801:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7802:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7803:         }
 7804:     }
 7805:     my @offon = ('off','on');
 7806:     my %title = &Apache::lonlocal::texthash (
 7807:                    run => 'Auto-update:',
 7808:                    classlists => 'Updates to user information in classlists?'
 7809:                 );
 7810:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7811:     my %fieldtitles = &Apache::lonlocal::texthash (
 7812:                         id => 'Student/Employee ID',
 7813:                         permanentemail => 'E-mail address',
 7814:                         lastname => 'Last Name',
 7815:                         firstname => 'First Name',
 7816:                         middlename => 'Middle Name',
 7817:                         generation => 'Generation',
 7818:                       );
 7819:     $othertitle = &mt('All users');
 7820:     if (keys(%{$usertypes}) >  0) {
 7821:         $othertitle = &mt('Other users');
 7822:     }
 7823:     foreach my $key (keys(%env)) {
 7824:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7825:             my ($usertype,$item) = ($1,$2);
 7826:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7827:                 if ($usertype eq 'default') {   
 7828:                     push(@{$fields{$1}},$2);
 7829:                 } elsif (ref($types) eq 'ARRAY') {
 7830:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7831:                         push(@{$fields{$1}},$2);
 7832:                     }
 7833:                 }
 7834:             }
 7835:         }
 7836:     }
 7837:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 7838:     @lockablenames = sort(@lockablenames);
 7839:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 7840:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7841:         if (@changed) {
 7842:             $changes{'lockablenames'} = 1;
 7843:         }
 7844:     } else {
 7845:         if (@lockablenames) {
 7846:             $changes{'lockablenames'} = 1;
 7847:         }
 7848:     }
 7849:     my %updatehash = (
 7850:                       autoupdate => { run => $env{'form.autoupdate_run'},
 7851:                                       classlists => $env{'form.classlists'},
 7852:                                       fields => {%fields},
 7853:                                       lockablenames => \@lockablenames,
 7854:                                     }
 7855:                      );
 7856:     foreach my $key (keys(%currautoupdate)) {
 7857:         if (($key eq 'run') || ($key eq 'classlists')) {
 7858:             if (exists($updatehash{autoupdate}{$key})) {
 7859:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 7860:                     $changes{$key} = 1;
 7861:                 }
 7862:             }
 7863:         } elsif ($key eq 'fields') {
 7864:             if (ref($currautoupdate{$key}) eq 'HASH') {
 7865:                 foreach my $item (@{$types},'default') {
 7866:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 7867:                         my $change = 0;
 7868:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 7869:                             if (!exists($fields{$item})) {
 7870:                                 $change = 1;
 7871:                                 last;
 7872:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 7873:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 7874:                                     $change = 1;
 7875:                                     last;
 7876:                                 }
 7877:                             }
 7878:                         }
 7879:                         if ($change) {
 7880:                             push(@{$changes{$key}},$item);
 7881:                         }
 7882:                     } 
 7883:                 }
 7884:             }
 7885:         } elsif ($key eq 'lockablenames') {
 7886:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 7887:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7888:                 if (@changed) {
 7889:                     $changes{'lockablenames'} = 1;
 7890:                 }
 7891:             } else {
 7892:                 if (@lockablenames) {
 7893:                     $changes{'lockablenames'} = 1;
 7894:                 }
 7895:             }
 7896:         }
 7897:     }
 7898:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 7899:         if (@lockablenames) {
 7900:             $changes{'lockablenames'} = 1;
 7901:         }
 7902:     }
 7903:     foreach my $item (@{$types},'default') {
 7904:         if (defined($fields{$item})) {
 7905:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 7906:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 7907:                     my $change = 0;
 7908:                     if (ref($fields{$item}) eq 'ARRAY') {
 7909:                         foreach my $type (@{$fields{$item}}) {
 7910:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 7911:                                 $change = 1;
 7912:                                 last;
 7913:                             }
 7914:                         }
 7915:                     }
 7916:                     if ($change) {
 7917:                         push(@{$changes{'fields'}},$item);
 7918:                     }
 7919:                 } else {
 7920:                     push(@{$changes{'fields'}},$item);
 7921:                 }
 7922:             } else {
 7923:                 push(@{$changes{'fields'}},$item);
 7924:             }
 7925:         }
 7926:     }
 7927:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7928:                                              $dom);
 7929:     if ($putresult eq 'ok') {
 7930:         if (keys(%changes) > 0) {
 7931:             $resulttext = &mt('Changes made:').'<ul>';
 7932:             foreach my $key (sort(keys(%changes))) {
 7933:                 if ($key eq 'lockablenames') {
 7934:                     $resulttext .= '<li>';
 7935:                     if (@lockablenames) {
 7936:                         $usertypes->{'default'} = $othertitle;
 7937:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7938:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7939:                     } else {
 7940:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7941:                     }
 7942:                     $resulttext .= '</li>';
 7943:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7944:                     foreach my $item (@{$changes{$key}}) {
 7945:                         my @newvalues;
 7946:                         foreach my $type (@{$fields{$item}}) {
 7947:                             push(@newvalues,$fieldtitles{$type});
 7948:                         }
 7949:                         my $newvaluestr;
 7950:                         if (@newvalues > 0) {
 7951:                             $newvaluestr = join(', ',@newvalues);
 7952:                         } else {
 7953:                             $newvaluestr = &mt('none');
 7954:                         }
 7955:                         if ($item eq 'default') {
 7956:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7957:                         } else {
 7958:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7959:                         }
 7960:                     }
 7961:                 } else {
 7962:                     my $newvalue;
 7963:                     if ($key eq 'run') {
 7964:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7965:                     } else {
 7966:                         $newvalue = $offon[$env{'form.'.$key}];
 7967:                     }
 7968:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7969:                 }
 7970:             }
 7971:             $resulttext .= '</ul>';
 7972:         } else {
 7973:             $resulttext = &mt('No changes made to autoupdates');
 7974:         }
 7975:     } else {
 7976:         $resulttext = '<span class="LC_error">'.
 7977: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7978:     }
 7979:     return $resulttext;
 7980: }
 7981: 
 7982: sub modify_autocreate {
 7983:     my ($dom,%domconfig) = @_;
 7984:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7985:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7986:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7987:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7988:         }
 7989:     }
 7990:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7991:                  req => 'Auto-creation of validated requests for official courses',
 7992:                  xmldc => 'Identity of course creator of courses from XML files',
 7993:                );
 7994:     my @types = ('xml','req');
 7995:     foreach my $item (@types) {
 7996:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7997:         $newvals{$item} =~ s/\D//g;
 7998:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7999:     }
 8000:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 8001:     my %domcoords = &get_active_dcs($dom);
 8002:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 8003:         $newvals{'xmldc'} = '';
 8004:     } 
 8005:     %autocreatehash =  (
 8006:                         autocreate => { xml => $newvals{'xml'},
 8007:                                         req => $newvals{'req'},
 8008:                                       }
 8009:                        );
 8010:     if ($newvals{'xmldc'} ne '') {
 8011:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 8012:     }
 8013:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 8014:                                              $dom);
 8015:     if ($putresult eq 'ok') {
 8016:         my @items = @types;
 8017:         if ($newvals{'xml'}) {
 8018:             push(@items,'xmldc');
 8019:         }
 8020:         foreach my $item (@items) {
 8021:             if (exists($currautocreate{$item})) {
 8022:                 if ($currautocreate{$item} ne $newvals{$item}) {
 8023:                     $changes{$item} = 1;
 8024:                 }
 8025:             } elsif ($newvals{$item}) {
 8026:                 $changes{$item} = 1;
 8027:             }
 8028:         }
 8029:         if (keys(%changes) > 0) {
 8030:             my @offon = ('off','on'); 
 8031:             $resulttext = &mt('Changes made:').'<ul>';
 8032:             foreach my $item (@types) {
 8033:                 if ($changes{$item}) {
 8034:                     my $newtxt = $offon[$newvals{$item}];
 8035:                     $resulttext .= '<li>'.
 8036:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 8037:                                        '<b>','</b>').
 8038:                                    '</li>';
 8039:                 }
 8040:             }
 8041:             if ($changes{'xmldc'}) {
 8042:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 8043:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 8044:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 8045:             }
 8046:             $resulttext .= '</ul>';
 8047:         } else {
 8048:             $resulttext = &mt('No changes made to auto-creation settings');
 8049:         }
 8050:     } else {
 8051:         $resulttext = '<span class="LC_error">'.
 8052:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8053:     }
 8054:     return $resulttext;
 8055: }
 8056: 
 8057: sub modify_directorysrch {
 8058:     my ($dom,%domconfig) = @_;
 8059:     my ($resulttext,%changes);
 8060:     my %currdirsrch;
 8061:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 8062:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 8063:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 8064:         }
 8065:     }
 8066:     my %title = ( available => 'Directory search available',
 8067:                   localonly => 'Other domains can search',
 8068:                   searchby => 'Search types',
 8069:                   searchtypes => 'Search latitude');
 8070:     my @offon = ('off','on');
 8071:     my @otherdoms = ('Yes','No');
 8072: 
 8073:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 8074:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 8075:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 8076: 
 8077:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8078:     if (keys(%{$usertypes}) == 0) {
 8079:         @cansearch = ('default');
 8080:     } else {
 8081:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 8082:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 8083:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 8084:                     push(@{$changes{'cansearch'}},$type);
 8085:                 }
 8086:             }
 8087:             foreach my $type (@cansearch) {
 8088:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 8089:                     push(@{$changes{'cansearch'}},$type);
 8090:                 }
 8091:             }
 8092:         } else {
 8093:             push(@{$changes{'cansearch'}},@cansearch);
 8094:         }
 8095:     }
 8096: 
 8097:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 8098:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 8099:             if (!grep(/^\Q$by\E$/,@searchby)) {
 8100:                 push(@{$changes{'searchby'}},$by);
 8101:             }
 8102:         }
 8103:         foreach my $by (@searchby) {
 8104:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 8105:                 push(@{$changes{'searchby'}},$by);
 8106:             }
 8107:         }
 8108:     } else {
 8109:         push(@{$changes{'searchby'}},@searchby);
 8110:     }
 8111: 
 8112:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 8113:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 8114:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 8115:                 push(@{$changes{'searchtypes'}},$type);
 8116:             }
 8117:         }
 8118:         foreach my $type (@searchtypes) {
 8119:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 8120:                 push(@{$changes{'searchtypes'}},$type);
 8121:             }
 8122:         }
 8123:     } else {
 8124:         if (exists($currdirsrch{'searchtypes'})) {
 8125:             foreach my $type (@searchtypes) {  
 8126:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 8127:                     push(@{$changes{'searchtypes'}},$type);
 8128:                 }
 8129:             }
 8130:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 8131:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 8132:             }   
 8133:         } else {
 8134:             push(@{$changes{'searchtypes'}},@searchtypes); 
 8135:         }
 8136:     }
 8137: 
 8138:     my %dirsrch_hash =  (
 8139:             directorysrch => { available => $env{'form.dirsrch_available'},
 8140:                                cansearch => \@cansearch,
 8141:                                localonly => $env{'form.dirsrch_localonly'},
 8142:                                searchby => \@searchby,
 8143:                                searchtypes => \@searchtypes,
 8144:                              }
 8145:             );
 8146:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 8147:                                              $dom);
 8148:     if ($putresult eq 'ok') {
 8149:         if (exists($currdirsrch{'available'})) {
 8150:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 8151:                  $changes{'available'} = 1;
 8152:              }
 8153:         } else {
 8154:             if ($env{'form.dirsrch_available'} eq '1') {
 8155:                 $changes{'available'} = 1;
 8156:             }
 8157:         }
 8158:         if (exists($currdirsrch{'localonly'})) {
 8159:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 8160:                  $changes{'localonly'} = 1;
 8161:              }
 8162:         } else {
 8163:             if ($env{'form.dirsrch_localonly'} eq '1') {
 8164:                 $changes{'localonly'} = 1;
 8165:             }
 8166:         }
 8167:         if (keys(%changes) > 0) {
 8168:             $resulttext = &mt('Changes made:').'<ul>';
 8169:             if ($changes{'available'}) {
 8170:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 8171:             }
 8172:             if ($changes{'localonly'}) {
 8173:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 8174:             }
 8175: 
 8176:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 8177:                 my $chgtext;
 8178:                 if (ref($usertypes) eq 'HASH') {
 8179:                     if (keys(%{$usertypes}) > 0) {
 8180:                         foreach my $type (@{$types}) {
 8181:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 8182:                                 $chgtext .= $usertypes->{$type}.'; ';
 8183:                             }
 8184:                         }
 8185:                         if (grep(/^default$/,@cansearch)) {
 8186:                             $chgtext .= $othertitle;
 8187:                         } else {
 8188:                             $chgtext =~ s/\; $//;
 8189:                         }
 8190:                         $resulttext .=
 8191:                             '<li>'.
 8192:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 8193:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 8194:                             '</li>';
 8195:                     }
 8196:                 }
 8197:             }
 8198:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 8199:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 8200:                 my $chgtext;
 8201:                 foreach my $type (@{$titleorder}) {
 8202:                     if (grep(/^\Q$type\E$/,@searchby)) {
 8203:                         if (defined($searchtitles->{$type})) {
 8204:                             $chgtext .= $searchtitles->{$type}.'; ';
 8205:                         }
 8206:                     }
 8207:                 }
 8208:                 $chgtext =~ s/\; $//;
 8209:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 8210:             }
 8211:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 8212:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 8213:                 my $chgtext;
 8214:                 foreach my $type (@{$srchtypeorder}) {
 8215:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 8216:                         if (defined($srchtypes_desc->{$type})) {
 8217:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 8218:                         }
 8219:                     }
 8220:                 }
 8221:                 $chgtext =~ s/\; $//;
 8222:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 8223:             }
 8224:             $resulttext .= '</ul>';
 8225:         } else {
 8226:             $resulttext = &mt('No changes made to institution directory search settings');
 8227:         }
 8228:     } else {
 8229:         $resulttext = '<span class="LC_error">'.
 8230:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 8231:     }
 8232:     return $resulttext;
 8233: }
 8234: 
 8235: sub modify_contacts {
 8236:     my ($dom,$lastactref,%domconfig) = @_;
 8237:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 8238:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 8239:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 8240:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 8241:         }
 8242:     }
 8243:     my (%others,%to,%bcc);
 8244:     my @contacts = ('supportemail','adminemail');
 8245:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 8246:                     'requestsmail','updatesmail','idconflictsmail');
 8247:     my @toggles = ('reporterrors','reportupdates');
 8248:     foreach my $type (@mailings) {
 8249:         @{$newsetting{$type}} = 
 8250:             &Apache::loncommon::get_env_multiple('form.'.$type);
 8251:         foreach my $item (@contacts) {
 8252:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 8253:                 $contacts_hash{contacts}{$type}{$item} = 1;
 8254:             } else {
 8255:                 $contacts_hash{contacts}{$type}{$item} = 0;
 8256:             }
 8257:         }  
 8258:         $others{$type} = $env{'form.'.$type.'_others'};
 8259:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 8260:         if ($type eq 'helpdeskmail') {
 8261:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 8262:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 8263:         }
 8264:     }
 8265:     foreach my $item (@contacts) {
 8266:         $to{$item} = $env{'form.'.$item};
 8267:         $contacts_hash{'contacts'}{$item} = $to{$item};
 8268:     }
 8269:     foreach my $item (@toggles) {
 8270:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 8271:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 8272:         }
 8273:     }
 8274:     if (keys(%currsetting) > 0) {
 8275:         foreach my $item (@contacts) {
 8276:             if ($to{$item} ne $currsetting{$item}) {
 8277:                 $changes{$item} = 1;
 8278:             }
 8279:         }
 8280:         foreach my $type (@mailings) {
 8281:             foreach my $item (@contacts) {
 8282:                 if (ref($currsetting{$type}) eq 'HASH') {
 8283:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 8284:                         push(@{$changes{$type}},$item);
 8285:                     }
 8286:                 } else {
 8287:                     push(@{$changes{$type}},@{$newsetting{$type}});
 8288:                 }
 8289:             }
 8290:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 8291:                 push(@{$changes{$type}},'others');
 8292:             }
 8293:             if ($type eq 'helpdeskmail') {   
 8294:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 8295:                     push(@{$changes{$type}},'bcc'); 
 8296:                 }
 8297:             }
 8298:         }
 8299:     } else {
 8300:         my %default;
 8301:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 8302:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 8303:         $default{'errormail'} = 'adminemail';
 8304:         $default{'packagesmail'} = 'adminemail';
 8305:         $default{'helpdeskmail'} = 'supportemail';
 8306:         $default{'lonstatusmail'} = 'adminemail';
 8307:         $default{'requestsmail'} = 'adminemail';
 8308:         $default{'updatesmail'} = 'adminemail';
 8309:         foreach my $item (@contacts) {
 8310:            if ($to{$item} ne $default{$item}) {
 8311:               $changes{$item} = 1;
 8312:            }
 8313:         }
 8314:         foreach my $type (@mailings) {
 8315:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 8316:                
 8317:                 push(@{$changes{$type}},@{$newsetting{$type}});
 8318:             }
 8319:             if ($others{$type} ne '') {
 8320:                 push(@{$changes{$type}},'others');
 8321:             }
 8322:             if ($type eq 'helpdeskmail') {
 8323:                 if ($bcc{$type} ne '') {
 8324:                     push(@{$changes{$type}},'bcc');
 8325:                 }
 8326:             }
 8327:         }
 8328:     }
 8329:     foreach my $item (@toggles) {
 8330:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 8331:             $changes{$item} = 1;
 8332:         } elsif ((!$env{'form.'.$item}) &&
 8333:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 8334:             $changes{$item} = 1;
 8335:         }
 8336:     }
 8337:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 8338:                                              $dom);
 8339:     if ($putresult eq 'ok') {
 8340:         if (keys(%changes) > 0) {
 8341:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8342:             if (ref($lastactref) eq 'HASH') {
 8343:                 $lastactref->{'domainconfig'} = 1;
 8344:             }
 8345:             my ($titles,$short_titles)  = &contact_titles();
 8346:             $resulttext = &mt('Changes made:').'<ul>';
 8347:             foreach my $item (@contacts) {
 8348:                 if ($changes{$item}) {
 8349:                     $resulttext .= '<li>'.$titles->{$item}.
 8350:                                     &mt(' set to: ').
 8351:                                     '<span class="LC_cusr_emph">'.
 8352:                                     $to{$item}.'</span></li>';
 8353:                 }
 8354:             }
 8355:             foreach my $type (@mailings) {
 8356:                 if (ref($changes{$type}) eq 'ARRAY') {
 8357:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 8358:                     my @text;
 8359:                     foreach my $item (@{$newsetting{$type}}) {
 8360:                         push(@text,$short_titles->{$item});
 8361:                     }
 8362:                     if ($others{$type} ne '') {
 8363:                         push(@text,$others{$type});
 8364:                     }
 8365:                     $resulttext .= '<span class="LC_cusr_emph">'.
 8366:                                    join(', ',@text).'</span>';
 8367:                     if ($type eq 'helpdeskmail') {
 8368:                         if ($bcc{$type} ne '') {
 8369:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 8370:                         }
 8371:                     }
 8372:                     $resulttext .= '</li>';
 8373:                 }
 8374:             }
 8375:             my @offon = ('off','on');
 8376:             if ($changes{'reporterrors'}) {
 8377:                 $resulttext .= '<li>'.
 8378:                                &mt('E-mail error reports to [_1] set to "'.
 8379:                                    $offon[$env{'form.reporterrors'}].'".',
 8380:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8381:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 8382:                                '</li>';
 8383:             }
 8384:             if ($changes{'reportupdates'}) {
 8385:                 $resulttext .= '<li>'.
 8386:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 8387:                                     $offon[$env{'form.reportupdates'}].'".',
 8388:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8389:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 8390:                                 '</li>';
 8391:             }
 8392:             $resulttext .= '</ul>';
 8393:         } else {
 8394:             $resulttext = &mt('No changes made to contact information');
 8395:         }
 8396:     } else {
 8397:         $resulttext = '<span class="LC_error">'.
 8398:             &mt('An error occurred: [_1].',$putresult).'</span>';
 8399:     }
 8400:     return $resulttext;
 8401: }
 8402: 
 8403: sub modify_usercreation {
 8404:     my ($dom,%domconfig) = @_;
 8405:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 8406:     my $warningmsg;
 8407:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8408:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8409:             if ($key eq 'cancreate') {
 8410:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8411:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8412:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8413:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 8414:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8415:                         } else {
 8416:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8417:                         }
 8418:                     }
 8419:                 }
 8420:             } elsif ($key eq 'email_rule') {
 8421:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8422:             } else {
 8423:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8424:             }
 8425:         }
 8426:     }
 8427:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 8428:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 8429:     my @contexts = ('author','course','requestcrs');
 8430:     foreach my $item(@contexts) {
 8431:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 8432:     }
 8433:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8434:         foreach my $item (@contexts) {
 8435:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 8436:                 push(@{$changes{'cancreate'}},$item);
 8437:             }
 8438:         }
 8439:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 8440:         foreach my $item (@contexts) {
 8441:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 8442:                 if ($cancreate{$item} ne 'any') {
 8443:                     push(@{$changes{'cancreate'}},$item);
 8444:                 }
 8445:             } else {
 8446:                 if ($cancreate{$item} ne 'none') {
 8447:                     push(@{$changes{'cancreate'}},$item);
 8448:                 }
 8449:             }
 8450:         }
 8451:     } else {
 8452:         foreach my $item (@contexts)  {
 8453:             push(@{$changes{'cancreate'}},$item);
 8454:         }
 8455:     }
 8456: 
 8457:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 8458:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 8459:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 8460:                 push(@{$changes{'username_rule'}},$type);
 8461:             }
 8462:         }
 8463:         foreach my $type (@username_rule) {
 8464:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 8465:                 push(@{$changes{'username_rule'}},$type);
 8466:             }
 8467:         }
 8468:     } else {
 8469:         push(@{$changes{'username_rule'}},@username_rule);
 8470:     }
 8471: 
 8472:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 8473:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 8474:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 8475:                 push(@{$changes{'id_rule'}},$type);
 8476:             }
 8477:         }
 8478:         foreach my $type (@id_rule) {
 8479:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 8480:                 push(@{$changes{'id_rule'}},$type);
 8481:             }
 8482:         }
 8483:     } else {
 8484:         push(@{$changes{'id_rule'}},@id_rule);
 8485:     }
 8486: 
 8487:     my @authen_contexts = ('author','course','domain');
 8488:     my @authtypes = ('int','krb4','krb5','loc');
 8489:     my %authhash;
 8490:     foreach my $item (@authen_contexts) {
 8491:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 8492:         foreach my $auth (@authtypes) {
 8493:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 8494:                 $authhash{$item}{$auth} = 1;
 8495:             } else {
 8496:                 $authhash{$item}{$auth} = 0;
 8497:             }
 8498:         }
 8499:     }
 8500:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 8501:         foreach my $item (@authen_contexts) {
 8502:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 8503:                 foreach my $auth (@authtypes) {
 8504:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 8505:                         push(@{$changes{'authtypes'}},$item);
 8506:                         last;
 8507:                     }
 8508:                 }
 8509:             }
 8510:         }
 8511:     } else {
 8512:         foreach my $item (@authen_contexts) {
 8513:             push(@{$changes{'authtypes'}},$item);
 8514:         }
 8515:     }
 8516: 
 8517:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 8518:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 8519:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 8520:     $save_usercreate{'id_rule'} = \@id_rule;
 8521:     $save_usercreate{'username_rule'} = \@username_rule,
 8522:     $save_usercreate{'authtypes'} = \%authhash;
 8523: 
 8524:     my %usercreation_hash =  (
 8525:         usercreation     => \%save_usercreate,
 8526:     );
 8527: 
 8528:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 8529:                                              $dom);
 8530: 
 8531:     if ($putresult eq 'ok') {
 8532:         if (keys(%changes) > 0) {
 8533:             $resulttext = &mt('Changes made:').'<ul>';
 8534:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8535:                 my %lt = &usercreation_types();
 8536:                 foreach my $type (@{$changes{'cancreate'}}) {
 8537:                     my $chgtext = $lt{$type}.', ';
 8538:                     if ($cancreate{$type} eq 'none') {
 8539:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 8540:                     } elsif ($cancreate{$type} eq 'any') {
 8541:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 8542:                     } elsif ($cancreate{$type} eq 'official') {
 8543:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 8544:                     } elsif ($cancreate{$type} eq 'unofficial') {
 8545:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 8546:                     }
 8547:                     $resulttext .= '<li>'.$chgtext.'</li>';
 8548:                 }
 8549:             }
 8550:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 8551:                 my ($rules,$ruleorder) = 
 8552:                     &Apache::lonnet::inst_userrules($dom,'username');
 8553:                 my $chgtext = '<ul>';
 8554:                 foreach my $type (@username_rule) {
 8555:                     if (ref($rules->{$type}) eq 'HASH') {
 8556:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 8557:                     }
 8558:                 }
 8559:                 $chgtext .= '</ul>';
 8560:                 if (@username_rule > 0) {
 8561:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 8562:                 } else {
 8563:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 8564:                 }
 8565:             }
 8566:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 8567:                 my ($idrules,$idruleorder) = 
 8568:                     &Apache::lonnet::inst_userrules($dom,'id');
 8569:                 my $chgtext = '<ul>';
 8570:                 foreach my $type (@id_rule) {
 8571:                     if (ref($idrules->{$type}) eq 'HASH') {
 8572:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8573:                     }
 8574:                 }
 8575:                 $chgtext .= '</ul>';
 8576:                 if (@id_rule > 0) {
 8577:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8578:                 } else {
 8579:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8580:                 }
 8581:             }
 8582:             my %authname = &authtype_names();
 8583:             my %context_title = &context_names();
 8584:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8585:                 my $chgtext = '<ul>';
 8586:                 foreach my $type (@{$changes{'authtypes'}}) {
 8587:                     my @allowed;
 8588:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8589:                     foreach my $auth (@authtypes) {
 8590:                         if ($authhash{$type}{$auth}) {
 8591:                             push(@allowed,$authname{$auth});
 8592:                         }
 8593:                     }
 8594:                     if (@allowed > 0) {
 8595:                         $chgtext .= join(', ',@allowed).'</li>';
 8596:                     } else {
 8597:                         $chgtext .= &mt('none').'</li>';
 8598:                     }
 8599:                 }
 8600:                 $chgtext .= '</ul>';
 8601:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8602:                 $resulttext .= '</li>';
 8603:             }
 8604:             $resulttext .= '</ul>';
 8605:         } else {
 8606:             $resulttext = &mt('No changes made to user creation settings');
 8607:         }
 8608:     } else {
 8609:         $resulttext = '<span class="LC_error">'.
 8610:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8611:     }
 8612:     if ($warningmsg ne '') {
 8613:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8614:     }
 8615:     return $resulttext;
 8616: }
 8617: 
 8618: sub modify_selfcreation {
 8619:     my ($dom,%domconfig) = @_;
 8620:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8621:     my (%save_usercreate,%save_usermodify);
 8622:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8623:     if (ref($types) eq 'ARRAY') {
 8624:         $usertypes->{'default'} = $othertitle;
 8625:         push(@{$types},'default');
 8626:     }
 8627: #
 8628: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8629: #
 8630:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8631:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8632:             if ($key eq 'cancreate') {
 8633:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8634:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8635:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8636:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8637:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 8638:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 8639:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8640:                         } else {
 8641:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8642:                         }
 8643:                     }
 8644:                 }
 8645:             } elsif ($key eq 'email_rule') {
 8646:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8647:             } else {
 8648:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8649:             }
 8650:         }
 8651:     }
 8652: #
 8653: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8654: #
 8655:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8656:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8657:             if ($key eq 'selfcreate') {
 8658:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8659:             } else {
 8660:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8661:             }
 8662:         }
 8663:     }
 8664: 
 8665:     my @contexts = ('selfcreate');
 8666:     @{$cancreate{'selfcreate'}} = ();
 8667:     %{$cancreate{'emailusername'}} = ();
 8668:     @{$cancreate{'statustocreate'}} = ();
 8669:     %{$cancreate{'selfcreateprocessing'}} = ();
 8670:     %{$cancreate{'shibenv'}} = ();
 8671:     my %selfcreatetypes = (
 8672:                              sso   => 'users authenticated by institutional single sign on',
 8673:                              login => 'users authenticated by institutional log-in',
 8674:                              email => 'users who provide a valid e-mail address for use as username',
 8675:                           );
 8676: #
 8677: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8678: # is permitted.
 8679: #
 8680: 
 8681:     my @statuses;
 8682:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 8683:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 8684:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 8685:         }
 8686:     }
 8687:     push(@statuses,'default');
 8688: 
 8689:     foreach my $item ('login','sso','email') {
 8690:         if ($item eq 'email') {
 8691:             if ($env{'form.cancreate_email'}) {
 8692:                 push(@{$cancreate{'selfcreate'}},'email');
 8693:                 push(@contexts,'selfcreateprocessing');
 8694:                 foreach my $type (@statuses) {
 8695:                     if ($type eq 'default') {
 8696:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 8697:                     } else { 
 8698:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 8699:                     }
 8700:                 }
 8701:             }
 8702:         } else {
 8703:             if ($env{'form.cancreate_'.$item}) {
 8704:                 push(@{$cancreate{'selfcreate'}},$item);
 8705:             }
 8706:         }
 8707:     }
 8708:     my (@email_rule,%userinfo,%savecaptcha);
 8709:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8710: #
 8711: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8712: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8713: #
 8714: 
 8715:     if ($env{'form.cancreate_email'}) {
 8716:         push(@contexts,'emailusername');
 8717:         if (ref($types) eq 'ARRAY') {
 8718:             foreach my $type (@{$types}) {
 8719:                 if (ref($infofields) eq 'ARRAY') {
 8720:                     foreach my $field (@{$infofields}) {
 8721:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8722:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8723:                         }
 8724:                     }
 8725:                 }
 8726:             }
 8727:         }
 8728: #
 8729: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8730: # queued requests for self-creation of account using e-mail address as username
 8731: #
 8732: 
 8733:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8734:         @approvalnotify = sort(@approvalnotify);
 8735:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8736:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8737:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8738:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8739:                     push(@{$changes{'cancreate'}},'notify');
 8740:                 }
 8741:             } else {
 8742:                 if ($cancreate{'notify'}{'approval'}) {
 8743:                     push(@{$changes{'cancreate'}},'notify');
 8744:                 }
 8745:             }
 8746:         } elsif ($cancreate{'notify'}{'approval'}) {
 8747:             push(@{$changes{'cancreate'}},'notify');
 8748:         }
 8749: 
 8750: #
 8751: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8752: #
 8753:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8754:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8755:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8756:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8757:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8758:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8759:                         push(@{$changes{'email_rule'}},$type);
 8760:                     }
 8761:                 }
 8762:             }
 8763:             if (@email_rule > 0) {
 8764:                 foreach my $type (@email_rule) {
 8765:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8766:                         push(@{$changes{'email_rule'}},$type);
 8767:                     }
 8768:                 }
 8769:             }
 8770:         } elsif (@email_rule > 0) {
 8771:             push(@{$changes{'email_rule'}},@email_rule);
 8772:         }
 8773:     }
 8774: #  
 8775: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 8776: # institutional log-in.
 8777: #
 8778:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8779:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8780:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8781:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8782:             $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.').' '.
 8783:                           &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.');
 8784:         }
 8785:     }
 8786:     my @fields = ('lastname','firstname','middlename','generation',
 8787:                   'permanentemail','id');
 8788:     my @shibfields = (@fields,'inststatus');
 8789:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8790: #
 8791: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8792: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8793: # may self-create accounts 
 8794: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8795: # which the user may supply, if institutional data is unavailable.
 8796: #
 8797:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8798:         if (ref($types) eq 'ARRAY') {
 8799:             if (@{$types} > 1) {
 8800:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8801:                 push(@contexts,'statustocreate');
 8802:             } else {
 8803:                 undef($cancreate{'statustocreate'});
 8804:             } 
 8805:             foreach my $type (@{$types}) {
 8806:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8807:                 foreach my $field (@fields) {
 8808:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8809:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8810:                     } else {
 8811:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8812:                     }
 8813:                 }
 8814:             }
 8815:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8816:                 foreach my $type (@{$types}) {
 8817:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8818:                         foreach my $field (@fields) {
 8819:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8820:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8821:                                 push(@{$changes{'selfcreate'}},$type);
 8822:                                 last;
 8823:                             }
 8824:                         }
 8825:                     }
 8826:                 }
 8827:             } else {
 8828:                 foreach my $type (@{$types}) {
 8829:                     push(@{$changes{'selfcreate'}},$type);
 8830:                 }
 8831:             }
 8832:         }
 8833:         foreach my $field (@shibfields) {
 8834:             if ($env{'form.shibenv_'.$field} ne '') {
 8835:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 8836:             }
 8837:         }
 8838:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8839:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 8840:                 foreach my $field (@shibfields) {
 8841:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 8842:                         push(@{$changes{'cancreate'}},'shibenv');
 8843:                     }
 8844:                 }
 8845:             } else {
 8846:                 foreach my $field (@shibfields) {
 8847:                     if ($env{'form.shibenv_'.$field}) {
 8848:                         push(@{$changes{'cancreate'}},'shibenv');
 8849:                         last;
 8850:                     }
 8851:                 }
 8852:             }
 8853:         }
 8854:     }
 8855:     foreach my $item (@contexts) {
 8856:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 8857:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 8858:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 8859:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 8860:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8861:                             push(@{$changes{'cancreate'}},$item);
 8862:                         }
 8863:                     }
 8864:                 }
 8865:             }
 8866:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8867:                 foreach my $type (@{$cancreate{$item}}) {
 8868:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 8869:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8870:                             push(@{$changes{'cancreate'}},$item);
 8871:                         }
 8872:                     }
 8873:                 }
 8874:             }
 8875:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 8876:             if (ref($cancreate{$item}) eq 'HASH') {
 8877:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 8878:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 8879:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 8880:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 8881:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8882:                                     push(@{$changes{'cancreate'}},$item);
 8883:                                 }
 8884:                             }
 8885:                         }
 8886:                     } elsif ($item eq 'selfcreateprocessing') {
 8887:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 8888:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8889:                                 push(@{$changes{'cancreate'}},$item);
 8890:                             }
 8891:                         }
 8892:                     } else {
 8893:                         if (!$cancreate{$item}{$curr}) {
 8894:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8895:                                 push(@{$changes{'cancreate'}},$item);
 8896:                             }
 8897:                         }
 8898:                     }
 8899:                 }
 8900:                 foreach my $field (keys(%{$cancreate{$item}})) {
 8901:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 8902:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 8903:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 8904:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 8905:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8906:                                         push(@{$changes{'cancreate'}},$item);
 8907:                                     }
 8908:                                 }
 8909:                             } else {
 8910:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8911:                                     push(@{$changes{'cancreate'}},$item);
 8912:                                 }
 8913:                             }
 8914:                         }
 8915:                     } elsif ($item eq 'selfcreateprocessing') {
 8916:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 8917:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8918:                                 push(@{$changes{'cancreate'}},$item);
 8919:                             }
 8920:                         }
 8921:                     } else {
 8922:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 8923:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8924:                                 push(@{$changes{'cancreate'}},$item);
 8925:                             }
 8926:                         }
 8927:                     }
 8928:                 }
 8929:             }
 8930:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 8931:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8932:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 8933:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8934:                         push(@{$changes{'cancreate'}},$item);
 8935:                     }
 8936:                 }
 8937:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 8938:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 8939:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8940:                         push(@{$changes{'cancreate'}},$item);
 8941:                     }
 8942:                 }
 8943:             }
 8944:         } elsif ($item eq 'emailusername') {
 8945:             if (ref($cancreate{$item}) eq 'HASH') {
 8946:                 foreach my $type (keys(%{$cancreate{$item}})) {
 8947:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 8948:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 8949:                             if ($cancreate{$item}{$type}{$field}) {
 8950:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8951:                                     push(@{$changes{'cancreate'}},$item);
 8952:                                 }
 8953:                                 last;
 8954:                             }
 8955:                         }
 8956:                     }
 8957:                 }
 8958:             }
 8959:         }
 8960:     }
 8961: #
 8962: # Populate %save_usercreate hash with updates to self-creation configuration.
 8963: #
 8964:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 8965:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 8966:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 8967:     if (ref($cancreate{'notify'}) eq 'HASH') {
 8968:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 8969:     }
 8970:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
 8971:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
 8972:     }
 8973:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8974:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 8975:     }
 8976:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
 8977:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
 8978:     }
 8979:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 8980:     $save_usercreate{'emailrule'} = \@email_rule;
 8981: 
 8982:     my %userconfig_hash = (
 8983:             usercreation     => \%save_usercreate,
 8984:             usermodification => \%save_usermodify,
 8985:     );
 8986:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8987:                                              $dom);
 8988: #
 8989: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8990: #
 8991:     if ($putresult eq 'ok') {
 8992:         if (keys(%changes) > 0) {
 8993:             $resulttext = &mt('Changes made:').'<ul>';
 8994:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8995:                 my %lt = &selfcreation_types();
 8996:                 foreach my $type (@{$changes{'cancreate'}}) {
 8997:                     my $chgtext;
 8998:                     if ($type eq 'selfcreate') {
 8999:                         if (@{$cancreate{$type}} == 0) {
 9000:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 9001:                         } else {
 9002:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 9003:                                         '<ul>';
 9004:                             foreach my $case (@{$cancreate{$type}}) {
 9005:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 9006:                             }
 9007:                             $chgtext .= '</ul>';
 9008:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 9009:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 9010:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 9011:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 9012:                                             $chgtext .= '<br />'.
 9013:                                                         '<span class="LC_warning">'.
 9014:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 9015:                                                         '</span>';
 9016:                                         }
 9017:                                     }
 9018:                                 }
 9019:                             }
 9020:                         }
 9021:                     } elsif ($type eq 'shibenv') {
 9022:                         if (keys(%{$cancreate{$type}}) == 0) {
 9023:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
 9024:                         } else {
 9025:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
 9026:                                         '<ul>';
 9027:                             foreach my $field (@shibfields) {
 9028:                                 next if ($cancreate{$type}{$field} eq '');
 9029:                                 if ($field eq 'inststatus') {
 9030:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
 9031:                                 } else {
 9032:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
 9033:                                 }
 9034:                             }
 9035:                             $chgtext .= '</ul>';
 9036:                         }  
 9037:                     } elsif ($type eq 'statustocreate') {
 9038:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 9039:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 9040:                             if (@{$cancreate{'selfcreate'}} > 0) {
 9041:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 9042:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 9043:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 9044:                                         $chgtext .= '<br />'.
 9045:                                                     '<span class="LC_warning">'.
 9046:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 9047:                                                     '</span>';
 9048:                                     }
 9049:                                 } elsif (ref($usertypes) eq 'HASH') {
 9050:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 9051:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 9052:                                     } else {
 9053:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 9054:                                     }
 9055:                                     $chgtext .= '<ul>';
 9056:                                     foreach my $case (@{$cancreate{$type}}) {
 9057:                                         if ($case eq 'default') {
 9058:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 9059:                                         } else {
 9060:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 9061:                                         }
 9062:                                     }
 9063:                                     $chgtext .= '</ul>';
 9064:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 9065:                                         $chgtext .= '<br /><span class="LC_warning">'.
 9066:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 9067:                                                     '</span>';
 9068:                                     }
 9069:                                 }
 9070:                             } else {
 9071:                                 if (@{$cancreate{$type}} == 0) {
 9072:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 9073:                                 } else {
 9074:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 9075:                                 }
 9076:                             }
 9077:                         }
 9078:                     } elsif ($type eq 'selfcreateprocessing') {
 9079:                         my %choices = &Apache::lonlocal::texthash (
 9080:                                                                     automatic => 'Automatic approval',
 9081:                                                                     approval  => 'Queued for approval',
 9082:                                                                   );
 9083:                         if (@statuses > 1) {
 9084:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
 9085:                                         '<ul>';
 9086:                            foreach my $type (@statuses) {
 9087:                                if ($type eq 'default') {
 9088:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 9089:                                } else {
 9090:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 9091:                                }
 9092:                            }
 9093:                            $chgtext .= '</ul>';
 9094:                         } else {
 9095:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
 9096:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
 9097:                         }
 9098:                     } elsif ($type eq 'captcha') {
 9099:                         if ($savecaptcha{$type} eq 'notused') {
 9100:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 9101:                         } else {
 9102:                             my %captchas = &captcha_phrases();
 9103:                             if ($captchas{$savecaptcha{$type}}) {
 9104:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 9105:                             } else {
 9106:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 9107:                             }
 9108:                         }
 9109:                     } elsif ($type eq 'recaptchakeys') {
 9110:                         my ($privkey,$pubkey);
 9111:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 9112:                             $pubkey = $savecaptcha{$type}{'public'};
 9113:                             $privkey = $savecaptcha{$type}{'private'};
 9114:                         }
 9115:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 9116:                         if (!$pubkey) {
 9117:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 9118:                         } else {
 9119:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 9120:                         }
 9121:                         if (!$privkey) {
 9122:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 9123:                         } else {
 9124:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 9125:                         }
 9126:                         $chgtext .= '</ul>';
 9127:                     } elsif ($type eq 'emailusername') {
 9128:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 9129:                             if (ref($types) eq 'ARRAY') {
 9130:                                 foreach my $type (@{$types}) {
 9131:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 9132:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 9133:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
 9134:                                                     '<ul>';
 9135:                                             foreach my $field (@{$infofields}) {
 9136:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 9137:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 9138:                                                 }
 9139:                                             }
 9140:                                             $chgtext .= '</ul>';
 9141:                                         } else {
 9142:                                             $chgtext .= &mt('When self creating account with e-mail as username, no information besides e-mail address will be provided by [_1].',"'$usertypes->{$type}'").'<br />';
 9143:                                         }
 9144:                                     } else {
 9145:                                         $chgtext .= &mt('When self creating account with e-mail as username, no information besides e-mail address will be provided by [_1].',"'$usertypes->{$type}'").'<br />';
 9146:                                     }
 9147:                                 }
 9148:                             }
 9149:                         }
 9150:                     } elsif ($type eq 'notify') {
 9151:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 9152:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 9153:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 9154:                                 if ($cancreate{'notify'}{'approval'}) {
 9155:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 9156:                                 }
 9157:                             }
 9158:                         }
 9159:                     }
 9160:                     if ($chgtext) {
 9161:                         $resulttext .= '<li>'.$chgtext.'</li>';
 9162:                     }
 9163:                 }
 9164:             }
 9165:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 9166:                 my ($emailrules,$emailruleorder) =
 9167:                     &Apache::lonnet::inst_userrules($dom,'email');
 9168:                 my $chgtext = '<ul>';
 9169:                 foreach my $type (@email_rule) {
 9170:                     if (ref($emailrules->{$type}) eq 'HASH') {
 9171:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 9172:                     }
 9173:                 }
 9174:                 $chgtext .= '</ul>';
 9175:                 if (@email_rule > 0) {
 9176:                     $resulttext .= '<li>'.
 9177:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 9178:                                        $chgtext.
 9179:                                    '</li>';
 9180:                 } else {
 9181:                     $resulttext .= '<li>'.
 9182:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 9183:                                    '</li>';
 9184:                 }
 9185:             }
 9186:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 9187:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 9188:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9189:                 foreach my $type (@{$changes{'selfcreate'}}) {
 9190:                     my $typename = $type;
 9191:                     if (ref($usertypes) eq 'HASH') {
 9192:                         if ($usertypes->{$type} ne '') {
 9193:                             $typename = $usertypes->{$type};
 9194:                         }
 9195:                     }
 9196:                     my @modifiable;
 9197:                     $resulttext .= '<li>'.
 9198:                                     &mt('Self-creation of account by users with status: [_1]',
 9199:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 9200:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 9201:                     foreach my $field (@fields) {
 9202:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 9203:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 9204:                         }
 9205:                     }
 9206:                     if (@modifiable > 0) {
 9207:                         $resulttext .= join(', ',@modifiable);
 9208:                     } else {
 9209:                         $resulttext .= &mt('none');
 9210:                     }
 9211:                     $resulttext .= '</li>';
 9212:                 }
 9213:                 $resulttext .= '</ul></li>';
 9214:             }
 9215:             $resulttext .= '</ul>';
 9216:         } else {
 9217:             $resulttext = &mt('No changes made to self-creation settings');
 9218:         }
 9219:     } else {
 9220:         $resulttext = '<span class="LC_error">'.
 9221:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9222:     }
 9223:     if ($warningmsg ne '') {
 9224:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9225:     }
 9226:     return $resulttext;
 9227: }
 9228: 
 9229: sub process_captcha {
 9230:     my ($container,$changes,$newsettings,$current) = @_;
 9231:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 9232:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 9233:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 9234:         $newsettings->{'captcha'} = 'original';
 9235:     }
 9236:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 9237:         if ($container eq 'cancreate') {
 9238:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9239:                 push(@{$changes->{'cancreate'}},'captcha');
 9240:             } elsif (!defined($changes->{'cancreate'})) {
 9241:                 $changes->{'cancreate'} = ['captcha'];
 9242:             }
 9243:         } else {
 9244:             $changes->{'captcha'} = 1;
 9245:         }
 9246:     }
 9247:     my ($newpub,$newpriv,$currpub,$currpriv);
 9248:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 9249:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 9250:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 9251:         $newpub =~ s/[^\w\-]//g;
 9252:         $newpriv =~ s/[^\w\-]//g;
 9253:         $newsettings->{'recaptchakeys'} = {
 9254:                                              public  => $newpub,
 9255:                                              private => $newpriv,
 9256:                                           };
 9257:     }
 9258:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 9259:         $currpub = $current->{'recaptchakeys'}{'public'};
 9260:         $currpriv = $current->{'recaptchakeys'}{'private'};
 9261:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 9262:             $newsettings->{'recaptchakeys'} = {
 9263:                                                  public  => '',
 9264:                                                  private => '',
 9265:                                               }
 9266:         }
 9267:     }
 9268:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 9269:         if ($container eq 'cancreate') {
 9270:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9271:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 9272:             } elsif (!defined($changes->{'cancreate'})) {
 9273:                 $changes->{'cancreate'} = ['recaptchakeys'];
 9274:             }
 9275:         } else {
 9276:             $changes->{'recaptchakeys'} = 1;
 9277:         }
 9278:     }
 9279:     return;
 9280: }
 9281: 
 9282: sub modify_usermodification {
 9283:     my ($dom,%domconfig) = @_;
 9284:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 9285:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9286:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9287:             if ($key eq 'selfcreate') {
 9288:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 9289:             } else {  
 9290:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 9291:             }
 9292:         }
 9293:     }
 9294:     my @contexts = ('author','course');
 9295:     my %context_title = (
 9296:                            author => 'In author context',
 9297:                            course => 'In course context',
 9298:                         );
 9299:     my @fields = ('lastname','firstname','middlename','generation',
 9300:                   'permanentemail','id');
 9301:     my %roles = (
 9302:                   author => ['ca','aa'],
 9303:                   course => ['st','ep','ta','in','cr'],
 9304:                 );
 9305:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9306:     foreach my $context (@contexts) {
 9307:         foreach my $role (@{$roles{$context}}) {
 9308:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 9309:             foreach my $item (@fields) {
 9310:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 9311:                     $modifyhash{$context}{$role}{$item} = 1;
 9312:                 } else {
 9313:                     $modifyhash{$context}{$role}{$item} = 0;
 9314:                 }
 9315:             }
 9316:         }
 9317:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 9318:             foreach my $role (@{$roles{$context}}) {
 9319:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 9320:                     foreach my $field (@fields) {
 9321:                         if ($modifyhash{$context}{$role}{$field} ne 
 9322:                                 $curr_usermodification{$context}{$role}{$field}) {
 9323:                             push(@{$changes{$context}},$role);
 9324:                             last;
 9325:                         }
 9326:                     }
 9327:                 }
 9328:             }
 9329:         } else {
 9330:             foreach my $context (@contexts) {
 9331:                 foreach my $role (@{$roles{$context}}) {
 9332:                     push(@{$changes{$context}},$role);
 9333:                 }
 9334:             }
 9335:         }
 9336:     }
 9337:     my %usermodification_hash =  (
 9338:                                    usermodification => \%modifyhash,
 9339:                                  );
 9340:     my $putresult = &Apache::lonnet::put_dom('configuration',
 9341:                                              \%usermodification_hash,$dom);
 9342:     if ($putresult eq 'ok') {
 9343:         if (keys(%changes) > 0) {
 9344:             $resulttext = &mt('Changes made: ').'<ul>';
 9345:             foreach my $context (@contexts) {
 9346:                 if (ref($changes{$context}) eq 'ARRAY') {
 9347:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 9348:                     if (ref($changes{$context}) eq 'ARRAY') {
 9349:                         foreach my $role (@{$changes{$context}}) {
 9350:                             my $rolename;
 9351:                             if ($role eq 'cr') {
 9352:                                 $rolename = &mt('Custom');
 9353:                             } else {
 9354:                                 $rolename = &Apache::lonnet::plaintext($role);
 9355:                             }
 9356:                             my @modifiable;
 9357:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 9358:                             foreach my $field (@fields) {
 9359:                                 if ($modifyhash{$context}{$role}{$field}) {
 9360:                                     push(@modifiable,$fieldtitles{$field});
 9361:                                 }
 9362:                             }
 9363:                             if (@modifiable > 0) {
 9364:                                 $resulttext .= join(', ',@modifiable);
 9365:                             } else {
 9366:                                 $resulttext .= &mt('none'); 
 9367:                             }
 9368:                             $resulttext .= '</li>';
 9369:                         }
 9370:                         $resulttext .= '</ul></li>';
 9371:                     }
 9372:                 }
 9373:             }
 9374:             $resulttext .= '</ul>';
 9375:         } else {
 9376:             $resulttext = &mt('No changes made to user modification settings');
 9377:         }
 9378:     } else {
 9379:         $resulttext = '<span class="LC_error">'.
 9380:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9381:     }
 9382:     return $resulttext;
 9383: }
 9384: 
 9385: sub modify_defaults {
 9386:     my ($dom,$lastactref,%domconfig) = @_;
 9387:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 9388:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9389:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 9390:     my @authtypes = ('internal','krb4','krb5','localauth');
 9391:     foreach my $item (@items) {
 9392:         $newvalues{$item} = $env{'form.'.$item};
 9393:         if ($item eq 'auth_def') {
 9394:             if ($newvalues{$item} ne '') {
 9395:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 9396:                     push(@errors,$item);
 9397:                 }
 9398:             }
 9399:         } elsif ($item eq 'lang_def') {
 9400:             if ($newvalues{$item} ne '') {
 9401:                 if ($newvalues{$item} =~ /^(\w+)/) {
 9402:                     my $langcode = $1;
 9403:                     if ($langcode ne 'x_chef') {
 9404:                         if (code2language($langcode) eq '') {
 9405:                             push(@errors,$item);
 9406:                         }
 9407:                     }
 9408:                 } else {
 9409:                     push(@errors,$item);
 9410:                 }
 9411:             }
 9412:         } elsif ($item eq 'timezone_def') {
 9413:             if ($newvalues{$item} ne '') {
 9414:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 9415:                     push(@errors,$item);   
 9416:                 }
 9417:             }
 9418:         } elsif ($item eq 'datelocale_def') {
 9419:             if ($newvalues{$item} ne '') {
 9420:                 my @datelocale_ids = DateTime::Locale->ids();
 9421:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 9422:                     push(@errors,$item);
 9423:                 }
 9424:             }
 9425:         } elsif ($item eq 'portal_def') {
 9426:             if ($newvalues{$item} ne '') {
 9427:                 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])\/?$/) {
 9428:                     push(@errors,$item);
 9429:                 }
 9430:             }
 9431:         }
 9432:         if (grep(/^\Q$item\E$/,@errors)) {
 9433:             $newvalues{$item} = $domdefaults{$item};
 9434:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 9435:             $changes{$item} = 1;
 9436:         }
 9437:         $domdefaults{$item} = $newvalues{$item};
 9438:     }
 9439:     my %defaults_hash = (
 9440:                          defaults => \%newvalues,
 9441:                         );
 9442:     my $title = &defaults_titles();
 9443: 
 9444:     my $currinststatus;
 9445:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9446:         $currinststatus = $domconfig{'inststatus'};
 9447:     } else {
 9448:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9449:         $currinststatus = {
 9450:                              inststatustypes => $usertypes,
 9451:                              inststatusorder => $types,
 9452:                              inststatusguest => [],
 9453:                           };
 9454:     }
 9455:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
 9456:     my @allpos;
 9457:     my %guests;
 9458:     my %alltypes;
 9459:     my ($currtitles,$currguests,$currorder);
 9460:     if (ref($currinststatus) eq 'HASH') {
 9461:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
 9462:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
 9463:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
 9464:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
 9465:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
 9466:                     }
 9467:                 }
 9468:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
 9469:                     my $position = $env{'form.inststatus_pos_'.$type};
 9470:                     $position =~ s/\D+//g;
 9471:                     $allpos[$position] = $type;
 9472:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
 9473:                     $alltypes{$type} =~ s/`//g;
 9474:                     if ($env{'form.inststatus_guest_'.$type}) {
 9475:                         $guests{$type} = 1;
 9476:                     }
 9477:                 }
 9478:             }
 9479:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
 9480:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
 9481:             }
 9482:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
 9483:             $currtitles =~ s/,$//;
 9484:         }
 9485:     }
 9486:     if ($env{'form.addinststatus'}) {
 9487:         my $newtype = $env{'form.addinststatus'};
 9488:         $newtype =~ s/\W//g;
 9489:         unless (exists($alltypes{$newtype})) {
 9490:             if ($env{'form.addinststatus_guest'}) {
 9491:                 $guests{$newtype} = 1;
 9492:             }
 9493:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
 9494:             $alltypes{$newtype} =~ s/`//g; 
 9495:             my $position = $env{'form.addinststatus_pos'};
 9496:             $position =~ s/\D+//g;
 9497:             if ($position ne '') {
 9498:                 $allpos[$position] = $newtype;
 9499:             }
 9500:         }
 9501:     }
 9502:     my (@orderedstatus,@orderedguests);
 9503:     foreach my $type (@allpos) {
 9504:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
 9505:             push(@orderedstatus,$type);
 9506:             if ($guests{$type}) {
 9507:                 push(@orderedguests,$type);
 9508:             }
 9509:         }
 9510:     }
 9511:     foreach my $type (keys(%alltypes)) {
 9512:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
 9513:             delete($alltypes{$type});
 9514:         }
 9515:     }
 9516:     $defaults_hash{'inststatus'} = {
 9517:                                      inststatustypes => \%alltypes,
 9518:                                      inststatusorder => \@orderedstatus,
 9519:                                      inststatusguest => \@orderedguests,
 9520:                                    };
 9521:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
 9522:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 9523:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
 9524:         }
 9525:     }
 9526:     if ($currorder ne join(',',@orderedstatus)) {
 9527:         $changes{'inststatus'}{'inststatusorder'} = 1;
 9528:     }
 9529:     if ($currguests ne join(',',@orderedguests)) {
 9530:         $changes{'inststatus'}{'inststatusguest'} = 1;
 9531:     }
 9532:     my $newtitles;
 9533:     foreach my $item (@orderedstatus) {
 9534:         $newtitles .= $alltypes{$item}.',';
 9535:     }
 9536:     $newtitles =~ s/,$//;
 9537:     if ($currtitles ne $newtitles) {
 9538:         $changes{'inststatus'}{'inststatustypes'} = 1;
 9539:     }
 9540:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 9541:                                              $dom);
 9542:     if ($putresult eq 'ok') {
 9543:         if (keys(%changes) > 0) {
 9544:             $resulttext = &mt('Changes made:').'<ul>';
 9545:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 9546:             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";
 9547:             foreach my $item (sort(keys(%changes))) {
 9548:                 if ($item eq 'inststatus') {
 9549:                     if (ref($changes{'inststatus'}) eq 'HASH') {
 9550:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
 9551:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
 9552:                             foreach my $type (@orderedstatus) { 
 9553:                                 $resulttext .= $alltypes{$type}.', ';
 9554:                             }
 9555:                             $resulttext =~ s/, $//;
 9556:                             $resulttext .= '</li>';
 9557:                         }
 9558:                         if ($changes{'inststatus'}{'inststatusguest'}) {
 9559:                             $resulttext .= '<li>'; 
 9560:                             if (@orderedguests) {
 9561:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
 9562:                                 foreach my $type (@orderedguests) {
 9563:                                     $resulttext .= $alltypes{$type}.', ';
 9564:                                 }
 9565:                                 $resulttext =~ s/, $//;
 9566:                             } else {
 9567:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
 9568:                             }
 9569:                             $resulttext .= '</li>';
 9570:                         }
 9571:                     }
 9572:                 } else {
 9573:                     my $value = $env{'form.'.$item};
 9574:                     if ($value eq '') {
 9575:                         $value = &mt('none');
 9576:                     } elsif ($item eq 'auth_def') {
 9577:                         my %authnames = &authtype_names();
 9578:                         my %shortauth = (
 9579:                                           internal   => 'int',
 9580:                                           krb4       => 'krb4',
 9581:                                           krb5       => 'krb5',
 9582:                                           localauth  => 'loc',
 9583:                         );
 9584:                         $value = $authnames{$shortauth{$value}};
 9585:                     }
 9586:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 9587:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
 9588:                 }
 9589:             }
 9590:             $resulttext .= '</ul>';
 9591:             $mailmsgtext .= "\n";
 9592:             my $cachetime = 24*60*60;
 9593:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9594:             if (ref($lastactref) eq 'HASH') {
 9595:                 $lastactref->{'domdefaults'} = 1;
 9596:             }
 9597:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 9598:                 my $notify = 1;
 9599:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 9600:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 9601:                         $notify = 0;
 9602:                     }
 9603:                 }
 9604:                 if ($notify) {
 9605:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 9606:                                                "LON-CAPA Domain Settings Change - $dom",
 9607:                                                $mailmsgtext);
 9608:                 }
 9609:             }
 9610:         } else {
 9611:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 9612:         }
 9613:     } else {
 9614:         $resulttext = '<span class="LC_error">'.
 9615:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9616:     }
 9617:     if (@errors > 0) {
 9618:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 9619:         foreach my $item (@errors) {
 9620:             $resulttext .= ' "'.$title->{$item}.'",';
 9621:         }
 9622:         $resulttext =~ s/,$//;
 9623:     }
 9624:     return $resulttext;
 9625: }
 9626: 
 9627: sub modify_scantron {
 9628:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9629:     my ($resulttext,%confhash,%changes,$errors);
 9630:     my $custom = 'custom.tab';
 9631:     my $default = 'default.tab';
 9632:     my $servadm = $r->dir_config('lonAdmEMail');
 9633:     my ($configuserok,$author_ok,$switchserver) = 
 9634:         &config_check($dom,$confname,$servadm);
 9635:     if ($env{'form.scantronformat.filename'} ne '') {
 9636:         my $error;
 9637:         if ($configuserok eq 'ok') {
 9638:             if ($switchserver) {
 9639:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 9640:             } else {
 9641:                 if ($author_ok eq 'ok') {
 9642:                     my ($result,$scantronurl) =
 9643:                         &publishlogo($r,'upload','scantronformat',$dom,
 9644:                                      $confname,'scantron','','',$custom);
 9645:                     if ($result eq 'ok') {
 9646:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 9647:                         $changes{'scantronformat'} = 1;
 9648:                     } else {
 9649:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 9650:                     }
 9651:                 } else {
 9652:                     $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);
 9653:                 }
 9654:             }
 9655:         } else {
 9656:             $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);
 9657:         }
 9658:         if ($error) {
 9659:             &Apache::lonnet::logthis($error);
 9660:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9661:         }
 9662:     }
 9663:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 9664:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 9665:             if ($env{'form.scantronformat_del'}) {
 9666:                 $confhash{'scantron'}{'scantronformat'} = '';
 9667:                 $changes{'scantronformat'} = 1;
 9668:             }
 9669:         }
 9670:     }
 9671:     if (keys(%confhash) > 0) {
 9672:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 9673:                                                  $dom);
 9674:         if ($putresult eq 'ok') {
 9675:             if (keys(%changes) > 0) {
 9676:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 9677:                     $resulttext = &mt('Changes made:').'<ul>';
 9678:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 9679:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 9680:                     } else {
 9681:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 9682:                     }
 9683:                     $resulttext .= '</ul>';
 9684:                 } else {
 9685:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 9686:                 }
 9687:                 $resulttext .= '</ul>';
 9688:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9689:                 if (ref($lastactref) eq 'HASH') {
 9690:                     $lastactref->{'domainconfig'} = 1;
 9691:                 }
 9692:             } else {
 9693:                 $resulttext = &mt('No changes made to bubblesheet format file');
 9694:             }
 9695:         } else {
 9696:             $resulttext = '<span class="LC_error">'.
 9697:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 9698:         }
 9699:     } else {
 9700:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 9701:     }
 9702:     if ($errors) {
 9703:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9704:                        $errors.'</ul>';
 9705:     }
 9706:     return $resulttext;
 9707: }
 9708: 
 9709: sub modify_coursecategories {
 9710:     my ($dom,$lastactref,%domconfig) = @_;
 9711:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 9712:         $cathash);
 9713:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 9714:     my @catitems = ('unauth','auth');
 9715:     my @cattypes = ('std','domonly','codesrch','none');
 9716:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9717:         $cathash = $domconfig{'coursecategories'}{'cats'};
 9718:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 9719:             $changes{'togglecats'} = 1;
 9720:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 9721:         }
 9722:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 9723:             $changes{'categorize'} = 1;
 9724:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 9725:         }
 9726:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 9727:             $changes{'togglecatscomm'} = 1;
 9728:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 9729:         }
 9730:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 9731:             $changes{'categorizecomm'} = 1;
 9732:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 9733:         }
 9734:         foreach my $item (@catitems) {
 9735:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9736:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
 9737:                     $changes{$item} = 1;
 9738:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9739:                 }
 9740:             }
 9741:         }
 9742:     } else {
 9743:         $changes{'togglecats'} = 1;
 9744:         $changes{'categorize'} = 1;
 9745:         $changes{'togglecatscomm'} = 1;
 9746:         $changes{'categorizecomm'} = 1;
 9747:         $domconfig{'coursecategories'} = {
 9748:                                              togglecats => $env{'form.togglecats'},
 9749:                                              categorize => $env{'form.categorize'},
 9750:                                              togglecatscomm => $env{'form.togglecatscomm'},
 9751:                                              categorizecomm => $env{'form.categorizecomm'},
 9752:                                          };
 9753:         foreach my $item (@catitems) {
 9754:             if ($env{'form.coursecat_'.$item} ne 'std') {
 9755:                 $changes{$item} = 1;
 9756:             }
 9757:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9758:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9759:             }
 9760:         }
 9761:     }
 9762:     if (ref($cathash) eq 'HASH') {
 9763:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 9764:             push (@deletecategory,'instcode::0');
 9765:         }
 9766:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 9767:             push(@deletecategory,'communities::0');
 9768:         }
 9769:     }
 9770:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 9771:     if (ref($cathash) eq 'HASH') {
 9772:         if (@deletecategory > 0) {
 9773:             #FIXME Need to remove category from all courses using a deleted category 
 9774:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 9775:             foreach my $item (@deletecategory) {
 9776:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 9777:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 9778:                     $deletions{$item} = 1;
 9779:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 9780:                 }
 9781:             }
 9782:         }
 9783:         foreach my $item (keys(%{$cathash})) {
 9784:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9785:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 9786:                 $reorderings{$item} = 1;
 9787:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 9788:             }
 9789:             if ($env{'form.addcategory_name_'.$item} ne '') {
 9790:                 my $newcat = $env{'form.addcategory_name_'.$item};
 9791:                 my $newdepth = $depth+1;
 9792:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9793:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 9794:                 $adds{$newitem} = 1; 
 9795:             }
 9796:             if ($env{'form.subcat_'.$item} ne '') {
 9797:                 my $newcat = $env{'form.subcat_'.$item};
 9798:                 my $newdepth = $depth+1;
 9799:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9800:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 9801:                 $adds{$newitem} = 1;
 9802:             }
 9803:         }
 9804:     }
 9805:     if ($env{'form.instcode'} eq '1') {
 9806:         if (ref($cathash) eq 'HASH') {
 9807:             my $newitem = 'instcode::0';
 9808:             if ($cathash->{$newitem} eq '') {  
 9809:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9810:                 $adds{$newitem} = 1;
 9811:             }
 9812:         } else {
 9813:             my $newitem = 'instcode::0';
 9814:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9815:             $adds{$newitem} = 1;
 9816:         }
 9817:     }
 9818:     if ($env{'form.communities'} eq '1') {
 9819:         if (ref($cathash) eq 'HASH') {
 9820:             my $newitem = 'communities::0';
 9821:             if ($cathash->{$newitem} eq '') {
 9822:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9823:                 $adds{$newitem} = 1;
 9824:             }
 9825:         } else {
 9826:             my $newitem = 'communities::0';
 9827:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9828:             $adds{$newitem} = 1;
 9829:         }
 9830:     }
 9831:     if ($env{'form.addcategory_name'} ne '') {
 9832:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 9833:             ($env{'form.addcategory_name'} ne 'communities')) {
 9834:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 9835:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 9836:             $adds{$newitem} = 1;
 9837:         }
 9838:     }
 9839:     my $putresult;
 9840:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9841:         if (keys(%deletions) > 0) {
 9842:             foreach my $key (keys(%deletions)) {
 9843:                 if ($predelallitems{$key} ne '') {
 9844:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 9845:                 }
 9846:             }
 9847:         }
 9848:         my (@chkcats,@chktrails,%chkallitems);
 9849:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 9850:         if (ref($chkcats[0]) eq 'ARRAY') {
 9851:             my $depth = 0;
 9852:             my $chg = 0;
 9853:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 9854:                 my $name = $chkcats[0][$i];
 9855:                 my $item;
 9856:                 if ($name eq '') {
 9857:                     $chg ++;
 9858:                 } else {
 9859:                     $item = &escape($name).'::0';
 9860:                     if ($chg) {
 9861:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 9862:                     }
 9863:                     $depth ++; 
 9864:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 9865:                     $depth --;
 9866:                 }
 9867:             }
 9868:         }
 9869:     }
 9870:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9871:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 9872:         if ($putresult eq 'ok') {
 9873:             my %title = (
 9874:                          togglecats     => 'Show/Hide a course in catalog',
 9875:                          categorize     => 'Assign a category to a course',
 9876:                          togglecatscomm => 'Show/Hide a community in catalog',
 9877:                          categorizecomm => 'Assign a category to a community',
 9878:                         );
 9879:             my %level = (
 9880:                          dom  => 'set in Domain ("Modify Course/Community")',
 9881:                          crs  => 'set in Course ("Course Configuration")',
 9882:                          comm => 'set in Community ("Community Configuration")',
 9883:                          none     => 'No catalog',
 9884:                          std      => 'Standard catalog',
 9885:                          domonly  => 'Domain-only catalog',
 9886:                          codesrch => 'Code search form',
 9887:                         );
 9888:             $resulttext = &mt('Changes made:').'<ul>';
 9889:             if ($changes{'togglecats'}) {
 9890:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 9891:             }
 9892:             if ($changes{'categorize'}) {
 9893:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 9894:             }
 9895:             if ($changes{'togglecatscomm'}) {
 9896:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 9897:             }
 9898:             if ($changes{'categorizecomm'}) {
 9899:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 9900:             }
 9901:             if ($changes{'unauth'}) {
 9902:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
 9903:             }
 9904:             if ($changes{'auth'}) {
 9905:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
 9906:             }
 9907:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9908:                 my $cathash;
 9909:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9910:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 9911:                 } else {
 9912:                     $cathash = {};
 9913:                 } 
 9914:                 my (@cats,@trails,%allitems);
 9915:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 9916:                 if (keys(%deletions) > 0) {
 9917:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 9918:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 9919:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 9920:                     }
 9921:                     $resulttext .= '</ul></li>';
 9922:                 }
 9923:                 if (keys(%reorderings) > 0) {
 9924:                     my %sort_by_trail;
 9925:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 9926:                     foreach my $key (keys(%reorderings)) {
 9927:                         if ($allitems{$key} ne '') {
 9928:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9929:                         }
 9930:                     }
 9931:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9932:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9933:                     }
 9934:                     $resulttext .= '</ul></li>';
 9935:                 }
 9936:                 if (keys(%adds) > 0) {
 9937:                     my %sort_by_trail;
 9938:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 9939:                     foreach my $key (keys(%adds)) {
 9940:                         if ($allitems{$key} ne '') {
 9941:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9942:                         }
 9943:                     }
 9944:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9945:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9946:                     }
 9947:                     $resulttext .= '</ul></li>';
 9948:                 }
 9949:             }
 9950:             $resulttext .= '</ul>';
 9951:             if ($changes{'unauth'} || $changes{'auth'}) {
 9952:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 9953:                 if ($changes{'auth'}) {
 9954:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 9955:                 }
 9956:                 if ($changes{'unauth'}) {
 9957:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 9958:                 }
 9959:                 my $cachetime = 24*60*60;
 9960:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9961:                 if (ref($lastactref) eq 'HASH') {
 9962:                     $lastactref->{'domdefaults'} = 1;
 9963:                 }
 9964:             }
 9965:         } else {
 9966:             $resulttext = '<span class="LC_error">'.
 9967:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9968:         }
 9969:     } else {
 9970:         $resulttext = &mt('No changes made to course and community categories');
 9971:     }
 9972:     return $resulttext;
 9973: }
 9974: 
 9975: sub modify_serverstatuses {
 9976:     my ($dom,%domconfig) = @_;
 9977:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 9978:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 9979:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 9980:     }
 9981:     my @pages = &serverstatus_pages();
 9982:     foreach my $type (@pages) {
 9983:         $newserverstatus{$type}{'namedusers'} = '';
 9984:         $newserverstatus{$type}{'machines'} = '';
 9985:         if (defined($env{'form.'.$type.'_namedusers'})) {
 9986:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 9987:             my @okusers;
 9988:             foreach my $user (@users) {
 9989:                 my ($uname,$udom) = split(/:/,$user);
 9990:                 if (($udom =~ /^$match_domain$/) &&   
 9991:                     (&Apache::lonnet::domain($udom)) &&
 9992:                     ($uname =~ /^$match_username$/)) {
 9993:                     if (!grep(/^\Q$user\E/,@okusers)) {
 9994:                         push(@okusers,$user);
 9995:                     }
 9996:                 }
 9997:             }
 9998:             if (@okusers > 0) {
 9999:                  @okusers = sort(@okusers);
10000:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
10001:             }
10002:         }
10003:         if (defined($env{'form.'.$type.'_machines'})) {
10004:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
10005:             my @okmachines;
10006:             foreach my $ip (@machines) {
10007:                 my @parts = split(/\./,$ip);
10008:                 next if (@parts < 4);
10009:                 my $badip = 0;
10010:                 for (my $i=0; $i<4; $i++) {
10011:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
10012:                         $badip = 1;
10013:                         last;
10014:                     }
10015:                 }
10016:                 if (!$badip) {
10017:                     push(@okmachines,$ip);     
10018:                 }
10019:             }
10020:             @okmachines = sort(@okmachines);
10021:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
10022:         }
10023:     }
10024:     my %serverstatushash =  (
10025:                                 serverstatuses => \%newserverstatus,
10026:                             );
10027:     foreach my $type (@pages) {
10028:         foreach my $setting ('namedusers','machines') {
10029:             my (@current,@new);
10030:             if (ref($currserverstatus{$type}) eq 'HASH') {
10031:                 if ($currserverstatus{$type}{$setting} ne '') { 
10032:                     @current = split(/,/,$currserverstatus{$type}{$setting});
10033:                 }
10034:             }
10035:             if ($newserverstatus{$type}{$setting} ne '') {
10036:                 @new = split(/,/,$newserverstatus{$type}{$setting});
10037:             }
10038:             if (@current > 0) {
10039:                 if (@new > 0) {
10040:                     foreach my $item (@current) {
10041:                         if (!grep(/^\Q$item\E$/,@new)) {
10042:                             $changes{$type}{$setting} = 1;
10043:                             last;
10044:                         }
10045:                     }
10046:                     foreach my $item (@new) {
10047:                         if (!grep(/^\Q$item\E$/,@current)) {
10048:                             $changes{$type}{$setting} = 1;
10049:                             last;
10050:                         }
10051:                     }
10052:                 } else {
10053:                     $changes{$type}{$setting} = 1;
10054:                 }
10055:             } elsif (@new > 0) {
10056:                 $changes{$type}{$setting} = 1;
10057:             }
10058:         }
10059:     }
10060:     if (keys(%changes) > 0) {
10061:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
10062:         my $putresult = &Apache::lonnet::put_dom('configuration',
10063:                                                  \%serverstatushash,$dom);
10064:         if ($putresult eq 'ok') {
10065:             $resulttext .= &mt('Changes made:').'<ul>';
10066:             foreach my $type (@pages) {
10067:                 if (ref($changes{$type}) eq 'HASH') {
10068:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
10069:                     if ($changes{$type}{'namedusers'}) {
10070:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
10071:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
10072:                         } else {
10073:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
10074:                         }
10075:                     }
10076:                     if ($changes{$type}{'machines'}) {
10077:                         if ($newserverstatus{$type}{'machines'} eq '') {
10078:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
10079:                         } else {
10080:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
10081:                         }
10082: 
10083:                     }
10084:                     $resulttext .= '</ul></li>';
10085:                 }
10086:             }
10087:             $resulttext .= '</ul>';
10088:         } else {
10089:             $resulttext = '<span class="LC_error">'.
10090:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
10091: 
10092:         }
10093:     } else {
10094:         $resulttext = &mt('No changes made to access to server status pages');
10095:     }
10096:     return $resulttext;
10097: }
10098: 
10099: sub modify_helpsettings {
10100:     my ($r,$dom,$confname,%domconfig) = @_;
10101:     my ($resulttext,$errors,%changes,%helphash);
10102:     my %defaultchecked = ('submitbugs' => 'on');
10103:     my @offon = ('off','on');
10104:     my @toggles = ('submitbugs');
10105:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
10106:         foreach my $item (@toggles) {
10107:             if ($defaultchecked{$item} eq 'on') { 
10108:                 if ($domconfig{'helpsettings'}{$item} eq '') {
10109:                     if ($env{'form.'.$item} eq '0') {
10110:                         $changes{$item} = 1;
10111:                     }
10112:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
10113:                     $changes{$item} = 1;
10114:                 }
10115:             } elsif ($defaultchecked{$item} eq 'off') {
10116:                 if ($domconfig{'helpsettings'}{$item} eq '') {
10117:                     if ($env{'form.'.$item} eq '1') {
10118:                         $changes{$item} = 1;
10119:                     }
10120:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
10121:                     $changes{$item} = 1;
10122:                 }
10123:             }
10124:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
10125:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
10126:             }
10127:         }
10128:     }
10129:     my $putresult;
10130:     if (keys(%changes) > 0) {
10131:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
10132:         if ($putresult eq 'ok') {
10133:             $resulttext = &mt('Changes made:').'<ul>';
10134:             foreach my $item (sort(keys(%changes))) {
10135:                 if ($item eq 'submitbugs') {
10136:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
10137:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
10138:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
10139:                 }
10140:             }
10141:             $resulttext .= '</ul>';
10142:         } else {
10143:             $resulttext = &mt('No changes made to help settings');
10144:             $errors .= '<li><span class="LC_error">'.
10145:                        &mt('An error occurred storing the settings: [_1]',
10146:                            $putresult).'</span></li>';
10147:         }
10148:     }
10149:     if ($errors) {
10150:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
10151:                        $errors.'</ul>';
10152:     }
10153:     return $resulttext;
10154: }
10155: 
10156: sub modify_coursedefaults {
10157:     my ($dom,$lastactref,%domconfig) = @_;
10158:     my ($resulttext,$errors,%changes,%defaultshash);
10159:     my %defaultchecked = ('canuse_pdfforms' => 'off');
10160:     my @toggles = ('canuse_pdfforms');
10161:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
10162:                    'uploadquota_community','uploadquota_textbook');
10163:     my @types = ('official','unofficial','community','textbook');
10164:     my %staticdefaults = (
10165:                            anonsurvey_threshold => 10,
10166:                            uploadquota          => 500,
10167:                          );
10168: 
10169:     $defaultshash{'coursedefaults'} = {};
10170: 
10171:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
10172:         if ($domconfig{'coursedefaults'} eq '') {
10173:             $domconfig{'coursedefaults'} = {};
10174:         }
10175:     }
10176: 
10177:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
10178:         foreach my $item (@toggles) {
10179:             if ($defaultchecked{$item} eq 'on') {
10180:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
10181:                     ($env{'form.'.$item} eq '0')) {
10182:                     $changes{$item} = 1;
10183:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10184:                     $changes{$item} = 1;
10185:                 }
10186:             } elsif ($defaultchecked{$item} eq 'off') {
10187:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
10188:                     ($env{'form.'.$item} eq '1')) {
10189:                     $changes{$item} = 1;
10190:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10191:                     $changes{$item} = 1;
10192:                 }
10193:             }
10194:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
10195:         }
10196:         foreach my $item (@numbers) {
10197:             my ($currdef,$newdef);
10198:             $newdef = $env{'form.'.$item};
10199:             if ($item eq 'anonsurvey_threshold') {
10200:                 $currdef = $domconfig{'coursedefaults'}{$item};
10201:                 $newdef =~ s/\D//g;
10202:                 if ($newdef eq '' || $newdef < 1) {
10203:                     $newdef = 1;
10204:                 }
10205:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
10206:             } else {
10207:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
10208:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10209:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
10210:                 }
10211:                 $newdef =~ s/[^\w.\-]//g;
10212:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
10213:             }
10214:             if ($currdef ne $newdef) {
10215:                 my $staticdef;
10216:                 if ($item eq 'anonsurvey_threshold') {
10217:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
10218:                         $changes{$item} = 1;
10219:                     }
10220:                 } else {
10221:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
10222:                         $changes{'uploadquota'} = 1;
10223:                     }
10224:                 }
10225:             }
10226:         }
10227: 
10228:         my $officialcreds = $env{'form.official_credits'};
10229:         $officialcreds =~ s/[^\d.]+//g;
10230:         my $unofficialcreds = $env{'form.unofficial_credits'};
10231:         $unofficialcreds =~ s/[^\d.]+//g;
10232:         my $textbookcreds = $env{'form.textbook_credits'};
10233:         $textbookcreds =~ s/[^\d.]+//g;
10234:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
10235:                 ($env{'form.coursecredits'} eq '1')) {
10236:                 $changes{'coursecredits'} = 1;
10237:         } else {
10238:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
10239:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
10240:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
10241:                 $changes{'coursecredits'} = 1;
10242:             }
10243:         }
10244:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
10245:             official   => $officialcreds,
10246:             unofficial => $unofficialcreds,
10247:             textbook   => $textbookcreds,
10248:         }
10249:     }
10250:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10251:                                              $dom);
10252:     if ($putresult eq 'ok') {
10253:         if (keys(%changes) > 0) {
10254:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10255:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || 
10256:                 ($changes{'uploadquota'})) { 
10257:                 if ($changes{'canuse_pdfforms'}) {
10258:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
10259:                 }
10260:                 if ($changes{'coursecredits'}) {
10261:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10262:                         $domdefaults{'officialcredits'} =
10263:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
10264:                         $domdefaults{'unofficialcredits'} =
10265:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
10266:                         $domdefaults{'textbookcredits'} =
10267:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
10268:                     }
10269:                 }
10270:                 if ($changes{'uploadquota'}) {
10271:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10272:                         foreach my $type (@types) {
10273:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
10274:                         }
10275:                     }
10276:                 }
10277:                 my $cachetime = 24*60*60;
10278:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10279:                 if (ref($lastactref) eq 'HASH') {
10280:                     $lastactref->{'domdefaults'} = 1;
10281:                 }
10282:             }
10283:             $resulttext = &mt('Changes made:').'<ul>';
10284:             foreach my $item (sort(keys(%changes))) {
10285:                 if ($item eq 'canuse_pdfforms') {
10286:                     if ($env{'form.'.$item} eq '1') {
10287:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
10288:                     } else {
10289:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
10290:                     }
10291:                 } elsif ($item eq 'anonsurvey_threshold') {
10292:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
10293:                 } elsif ($item eq 'uploadquota') {
10294:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10295:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
10296:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
10297:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
10298:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
10299: 
10300:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
10301:                                        '</ul>'.
10302:                                        '</li>';
10303:                     } else {
10304:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
10305:                     }
10306:                 } elsif ($item eq 'coursecredits') {
10307:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10308:                         if (($domdefaults{'officialcredits'} eq '') &&
10309:                             ($domdefaults{'unofficialcredits'} eq '') &&
10310:                             ($domdefaults{'textbookcredits'} eq '')) {
10311:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10312:                         } else {
10313:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
10314:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
10315:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
10316:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
10317:                                            '</ul>'.
10318:                                            '</li>';
10319:                         }
10320:                     } else {
10321:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10322:                     }
10323:                 }
10324:             }
10325:             $resulttext .= '</ul>';
10326:         } else {
10327:             $resulttext = &mt('No changes made to course defaults');
10328:         }
10329:     } else {
10330:         $resulttext = '<span class="LC_error">'.
10331:             &mt('An error occurred: [_1]',$putresult).'</span>';
10332:     }
10333:     return $resulttext;
10334: }
10335: 
10336: sub modify_selfenrollment {
10337:     my ($dom,$lastactref,%domconfig) = @_;
10338:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
10339:     my @types = ('official','unofficial','community','textbook');
10340:     my %titles = &tool_titles();
10341:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
10342:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
10343:     $ordered{'default'} = ['types','registered','approval','limit'];
10344: 
10345:     my (%roles,%shown,%toplevel);
10346:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
10347: 
10348:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
10349:         if ($domconfig{'selfenrollment'} eq '') {
10350:             $domconfig{'selfenrollment'} = {};
10351:         }
10352:     }
10353:     %toplevel = (
10354:                   admin      => 'Configuration Rights',
10355:                   default    => 'Default settings',
10356:                   validation => 'Validation of self-enrollment requests',
10357:                 );
10358:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
10359: 
10360:     if (ref($ordered{'admin'}) eq 'ARRAY') {
10361:         foreach my $item (@{$ordered{'admin'}}) {
10362:             foreach my $type (@types) {
10363:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
10364:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
10365:                 } else {
10366:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
10367:                 }
10368:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
10369:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
10370:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
10371:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
10372:                             push(@{$changes{'admin'}{$type}},$item);
10373:                         }
10374:                     } else {
10375:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
10376:                             push(@{$changes{'admin'}{$type}},$item);
10377:                         }
10378:                     }
10379:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
10380:                     push(@{$changes{'admin'}{$type}},$item);
10381:                 }
10382:             }
10383:         }
10384:     }
10385: 
10386:     foreach my $item (@{$ordered{'default'}}) {
10387:         foreach my $type (@types) {
10388:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
10389:             if ($item eq 'types') {
10390:                 unless (($value eq 'all') || ($value eq 'dom')) {
10391:                     $value = '';
10392:                 }
10393:             } elsif ($item eq 'registered') {
10394:                 unless ($value eq '1') {
10395:                     $value = 0;
10396:                 }
10397:             } elsif ($item eq 'approval') {
10398:                 unless ($value =~ /^[012]$/) {
10399:                     $value = 0;
10400:                 }
10401:             } else {
10402:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10403:                     $value = 'none';
10404:                 }
10405:             }
10406:             $selfenrollhash{'default'}{$type}{$item} = $value;
10407:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
10408:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10409:                     if ($selfenrollhash{'default'}{$type}{$item} ne
10410:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
10411:                          push(@{$changes{'default'}{$type}},$item);
10412:                     }
10413:                 } else {
10414:                     push(@{$changes{'default'}{$type}},$item);
10415:                 }
10416:             } else {
10417:                 push(@{$changes{'default'}{$type}},$item);
10418:             }
10419:             if ($item eq 'limit') {
10420:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10421:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
10422:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
10423:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
10424:                     }
10425:                 } else {
10426:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
10427:                 }
10428:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10429:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
10430:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
10431:                          push(@{$changes{'default'}{$type}},'cap');
10432:                     }
10433:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
10434:                     push(@{$changes{'default'}{$type}},'cap');
10435:                 }
10436:             }
10437:         }
10438:     }
10439: 
10440:     foreach my $item (@{$itemsref}) {
10441:         if ($item eq 'fields') {
10442:             my @changed;
10443:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
10444:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
10445:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
10446:             }
10447:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10448:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
10449:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
10450:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
10451:                 } else {
10452:                     @changed = @{$selfenrollhash{'validation'}{$item}};
10453:                 }
10454:             } else {
10455:                 @changed = @{$selfenrollhash{'validation'}{$item}};
10456:             }
10457:             if (@changed) {
10458:                 if ($selfenrollhash{'validation'}{$item}) { 
10459:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
10460:                 } else {
10461:                     $changes{'validation'}{$item} = &mt('None');
10462:                 }
10463:             }
10464:         } else {
10465:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
10466:             if ($item eq 'markup') {
10467:                if ($env{'form.selfenroll_validation_'.$item}) {
10468:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10469:                }
10470:             }
10471:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10472:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
10473:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
10474:                 }
10475:             }
10476:         }
10477:     }
10478: 
10479:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
10480:                                              $dom);
10481:     if ($putresult eq 'ok') {
10482:         if (keys(%changes) > 0) {
10483:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10484:             $resulttext = &mt('Changes made:').'<ul>';
10485:             foreach my $key ('admin','default','validation') {
10486:                 if (ref($changes{$key}) eq 'HASH') {
10487:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
10488:                     if ($key eq 'validation') {
10489:                         foreach my $item (@{$itemsref}) {
10490:                             if (exists($changes{$key}{$item})) {
10491:                                 if ($item eq 'markup') {
10492:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10493:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
10494:                                 } else {  
10495:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10496:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
10497:                                 }
10498:                             }
10499:                         }
10500:                     } else {
10501:                         foreach my $type (@types) {
10502:                             if ($type eq 'community') {
10503:                                 $roles{'1'} = &mt('Community personnel');
10504:                             } else {
10505:                                 $roles{'1'} = &mt('Course personnel');
10506:                             }
10507:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
10508:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
10509:                                     if ($key eq 'admin') {
10510:                                         my @mgrdc = ();
10511:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10512:                                             foreach my $item (@{$ordered{'admin'}}) {
10513:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
10514:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
10515:                                                         push(@mgrdc,$item);
10516:                                                     }
10517:                                                 }
10518:                                             }
10519:                                             if (@mgrdc) {
10520:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
10521:                                             } else {
10522:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
10523:                                             }
10524:                                         }
10525:                                     } else {
10526:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10527:                                             foreach my $item (@{$ordered{$key}}) {
10528:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10529:                                                     $domdefaults{$type.'selfenroll'.$item} =
10530:                                                         $selfenrollhash{$key}{$type}{$item};
10531:                                                 }
10532:                                             }
10533:                                         }
10534:                                     }
10535:                                 }
10536:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
10537:                                 foreach my $item (@{$ordered{$key}}) {
10538:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10539:                                         $resulttext .= '<li>';
10540:                                         if ($key eq 'admin') {
10541:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
10542:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
10543:                                         } else {
10544:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
10545:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
10546:                                         }
10547:                                         $resulttext .= '</li>';
10548:                                     }
10549:                                 }
10550:                                 $resulttext .= '</ul></li>';
10551:                             }
10552:                         }
10553:                         $resulttext .= '</ul></li>'; 
10554:                     }
10555:                 }
10556:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
10557:                     my $cachetime = 24*60*60;
10558:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10559:                     if (ref($lastactref) eq 'HASH') {
10560:                         $lastactref->{'domdefaults'} = 1;
10561:                     }
10562:                 }
10563:             }
10564:             $resulttext .= '</ul>';
10565:         } else {
10566:             $resulttext = &mt('No changes made to self-enrollment settings');
10567:         }
10568:     } else {
10569:         $resulttext = '<span class="LC_error">'.
10570:             &mt('An error occurred: [_1]',$putresult).'</span>';
10571:     }
10572:     return $resulttext;
10573: }
10574: 
10575: sub modify_usersessions {
10576:     my ($dom,$lastactref,%domconfig) = @_;
10577:     my @hostingtypes = ('version','excludedomain','includedomain');
10578:     my @offloadtypes = ('primary','default');
10579:     my %types = (
10580:                   remote => \@hostingtypes,
10581:                   hosted => \@hostingtypes,
10582:                   spares => \@offloadtypes,
10583:                 );
10584:     my @prefixes = ('remote','hosted','spares');
10585:     my @lcversions = &Apache::lonnet::all_loncaparevs();
10586:     my (%by_ip,%by_location,@intdoms);
10587:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
10588:     my @locations = sort(keys(%by_location));
10589:     my (%defaultshash,%changes);
10590:     foreach my $prefix (@prefixes) {
10591:         $defaultshash{'usersessions'}{$prefix} = {};
10592:     }
10593:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10594:     my $resulttext;
10595:     my %iphost = &Apache::lonnet::get_iphost();
10596:     foreach my $prefix (@prefixes) {
10597:         next if ($prefix eq 'spares');
10598:         foreach my $type (@{$types{$prefix}}) {
10599:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
10600:             if ($type eq 'version') {
10601:                 my $value = $env{'form.'.$prefix.'_'.$type};
10602:                 my $okvalue;
10603:                 if ($value ne '') {
10604:                     if (grep(/^\Q$value\E$/,@lcversions)) {
10605:                         $okvalue = $value;
10606:                     }
10607:                 }
10608:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10609:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10610:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
10611:                             if ($inuse == 0) {
10612:                                 $changes{$prefix}{$type} = 1;
10613:                             } else {
10614:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
10615:                                     $changes{$prefix}{$type} = 1;
10616:                                 }
10617:                                 if ($okvalue ne '') {
10618:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10619:                                 } 
10620:                             }
10621:                         } else {
10622:                             if (($inuse == 1) && ($okvalue ne '')) {
10623:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10624:                                 $changes{$prefix}{$type} = 1;
10625:                             }
10626:                         }
10627:                     } else {
10628:                         if (($inuse == 1) && ($okvalue ne '')) {
10629:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10630:                             $changes{$prefix}{$type} = 1;
10631:                         }
10632:                     }
10633:                 } else {
10634:                     if (($inuse == 1) && ($okvalue ne '')) {
10635:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10636:                         $changes{$prefix}{$type} = 1;
10637:                     }
10638:                 }
10639:             } else {
10640:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
10641:                 my @okvals;
10642:                 foreach my $val (@vals) {
10643:                     if ($val =~ /:/) {
10644:                         my @items = split(/:/,$val);
10645:                         foreach my $item (@items) {
10646:                             if (ref($by_location{$item}) eq 'ARRAY') {
10647:                                 push(@okvals,$item);
10648:                             }
10649:                         }
10650:                     } else {
10651:                         if (ref($by_location{$val}) eq 'ARRAY') {
10652:                             push(@okvals,$val);
10653:                         }
10654:                     }
10655:                 }
10656:                 @okvals = sort(@okvals);
10657:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10658:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10659:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10660:                             if ($inuse == 0) {
10661:                                 $changes{$prefix}{$type} = 1; 
10662:                             } else {
10663:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10664:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
10665:                                 if (@changed > 0) {
10666:                                     $changes{$prefix}{$type} = 1;
10667:                                 }
10668:                             }
10669:                         } else {
10670:                             if ($inuse == 1) {
10671:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10672:                                 $changes{$prefix}{$type} = 1;
10673:                             }
10674:                         } 
10675:                     } else {
10676:                         if ($inuse == 1) {
10677:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10678:                             $changes{$prefix}{$type} = 1;
10679:                         }
10680:                     }
10681:                 } else {
10682:                     if ($inuse == 1) {
10683:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10684:                         $changes{$prefix}{$type} = 1;
10685:                     }
10686:                 }
10687:             }
10688:         }
10689:     }
10690: 
10691:     my @alldoms = &Apache::lonnet::all_domains();
10692:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10693:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
10694:     my $savespares;
10695: 
10696:     foreach my $lonhost (sort(keys(%servers))) {
10697:         my $serverhomeID =
10698:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
10699:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
10700:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
10701:         my %spareschg;
10702:         foreach my $type (@{$types{'spares'}}) {
10703:             my @okspares;
10704:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
10705:             foreach my $server (@checked) {
10706:                 if (&Apache::lonnet::hostname($server) ne '') {
10707:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
10708:                         unless (grep(/^\Q$server\E$/,@okspares)) {
10709:                             push(@okspares,$server);
10710:                         }
10711:                     }
10712:                 }
10713:             }
10714:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
10715:             my $newspare;
10716:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
10717:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
10718:                     $newspare = $new;
10719:                 }
10720:             }
10721:             my @spares;
10722:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
10723:                 @spares = sort(@okspares,$newspare);
10724:             } else {
10725:                 @spares = sort(@okspares);
10726:             }
10727:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
10728:             if (ref($spareid{$lonhost}) eq 'HASH') {
10729:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
10730:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
10731:                     if (@diffs > 0) {
10732:                         $spareschg{$type} = 1;
10733:                     }
10734:                 }
10735:             }
10736:         }
10737:         if (keys(%spareschg) > 0) {
10738:             $changes{'spares'}{$lonhost} = \%spareschg;
10739:         }
10740:     }
10741: 
10742:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
10743:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
10744:             if (ref($changes{'spares'}) eq 'HASH') {
10745:                 if (keys(%{$changes{'spares'}}) > 0) {
10746:                     $savespares = 1;
10747:                 }
10748:             }
10749:         } else {
10750:             $savespares = 1;
10751:         }
10752:     }
10753: 
10754:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
10755:     if ((keys(%changes) > 0) || ($savespares)) {
10756:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10757:                                                  $dom);
10758:         if ($putresult eq 'ok') {
10759:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10760:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
10761:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
10762:                 }
10763:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
10764:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
10765:                 }
10766:             }
10767:             my $cachetime = 24*60*60;
10768:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10769:             if (ref($lastactref) eq 'HASH') {
10770:                 $lastactref->{'domdefaults'} = 1;
10771:             }
10772:             if (keys(%changes) > 0) {
10773:                 my %lt = &usersession_titles();
10774:                 $resulttext = &mt('Changes made:').'<ul>';
10775:                 foreach my $prefix (@prefixes) {
10776:                     if (ref($changes{$prefix}) eq 'HASH') {
10777:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
10778:                         if ($prefix eq 'spares') {
10779:                             if (ref($changes{$prefix}) eq 'HASH') {
10780:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
10781:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
10782:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
10783:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
10784:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
10785:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
10786:                                         foreach my $type (@{$types{$prefix}}) {
10787:                                             if ($changes{$prefix}{$lonhost}{$type}) {
10788:                                                 my $offloadto = &mt('None');
10789:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
10790:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
10791:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
10792:                                                     }
10793:                                                 }
10794:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
10795:                                             }
10796:                                         }
10797:                                     }
10798:                                     $resulttext .= '</li>';
10799:                                 }
10800:                             }
10801:                         } else {
10802:                             foreach my $type (@{$types{$prefix}}) {
10803:                                 if (defined($changes{$prefix}{$type})) {
10804:                                     my $newvalue;
10805:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10806:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
10807:                                             if ($type eq 'version') {
10808:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
10809:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10810:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
10811:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
10812:                                                 }
10813:                                             }
10814:                                         }
10815:                                     }
10816:                                     if ($newvalue eq '') {
10817:                                         if ($type eq 'version') {
10818:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
10819:                                         } else {
10820:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
10821:                                         }
10822:                                     } else {
10823:                                         if ($type eq 'version') {
10824:                                             $newvalue .= ' '.&mt('(or later)'); 
10825:                                         }
10826:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
10827:                                     }
10828:                                 }
10829:                             }
10830:                         }
10831:                         $resulttext .= '</ul>';
10832:                     }
10833:                 }
10834:                 $resulttext .= '</ul>';
10835:             } else {
10836:                 $resulttext = $nochgmsg;
10837:             }
10838:         } else {
10839:             $resulttext = '<span class="LC_error">'.
10840:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10841:         }
10842:     } else {
10843:         $resulttext = $nochgmsg;
10844:     }
10845:     return $resulttext;
10846: }
10847: 
10848: sub modify_loadbalancing {
10849:     my ($dom,%domconfig) = @_;
10850:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
10851:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
10852:     my ($othertitle,$usertypes,$types) =
10853:         &Apache::loncommon::sorted_inst_types($dom);
10854:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10855:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
10856:     my @sparestypes = ('primary','default');
10857:     my %typetitles = &sparestype_titles();
10858:     my $resulttext;
10859:     my (%currbalancer,%currtargets,%currrules,%existing);
10860:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10861:         %existing = %{$domconfig{'loadbalancing'}};
10862:     }
10863:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
10864:                               \%currtargets,\%currrules);
10865:     my ($saveloadbalancing,%defaultshash,%changes);
10866:     my ($alltypes,$othertypes,$titles) =
10867:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
10868:     my %ruletitles = &offloadtype_text();
10869:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
10870:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
10871:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
10872:         if ($balancer eq '') {
10873:             next;
10874:         }
10875:         if (!exists($servers{$balancer})) {
10876:             if (exists($currbalancer{$balancer})) {
10877:                 push(@{$changes{'delete'}},$balancer);
10878:             }
10879:             next;
10880:         }
10881:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
10882:             push(@{$changes{'delete'}},$balancer);
10883:             next;
10884:         }
10885:         if (!exists($currbalancer{$balancer})) {
10886:             push(@{$changes{'add'}},$balancer);
10887:         }
10888:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
10889:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
10890:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
10891:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10892:             $saveloadbalancing = 1;
10893:         }
10894:         foreach my $sparetype (@sparestypes) {
10895:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
10896:             my @offloadto;
10897:             foreach my $target (@targets) {
10898:                 if (($servers{$target}) && ($target ne $balancer)) {
10899:                     if ($sparetype eq 'default') {
10900:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
10901:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
10902:                         }
10903:                     }
10904:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
10905:                         push(@offloadto,$target);
10906:                     }
10907:                 }
10908:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
10909:             }
10910:         }
10911:         if (ref($currtargets{$balancer}) eq 'HASH') {
10912:             foreach my $sparetype (@sparestypes) {
10913:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
10914:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
10915:                     if (@targetdiffs > 0) {
10916:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10917:                     }
10918:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10919:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10920:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10921:                     }
10922:                 }
10923:             }
10924:         } else {
10925:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
10926:                 foreach my $sparetype (@sparestypes) {
10927:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10928:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10929:                             $changes{'curr'}{$balancer}{'targets'} = 1;
10930:                         }
10931:                     }
10932:                 }
10933:             }
10934:         }
10935:         my $ishomedom;
10936:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
10937:             $ishomedom = 1;
10938:         }
10939:         if (ref($alltypes) eq 'ARRAY') {
10940:             foreach my $type (@{$alltypes}) {
10941:                 my $rule;
10942:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
10943:                          (!$ishomedom)) {
10944:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
10945:                 }
10946:                 if ($rule eq 'specific') {
10947:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
10948:                     if (exists($servers{$specifiedhost})) {
10949:                         $rule = $specifiedhost;
10950:                     }
10951:                 }
10952:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
10953:                 if (ref($currrules{$balancer}) eq 'HASH') {
10954:                     if ($rule ne $currrules{$balancer}{$type}) {
10955:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10956:                     }
10957:                 } elsif ($rule ne '') {
10958:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10959:                 }
10960:             }
10961:         }
10962:     }
10963:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
10964:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
10965:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
10966:             $defaultshash{'loadbalancing'} = {};
10967:         }
10968:         my $putresult = &Apache::lonnet::put_dom('configuration',
10969:                                                  \%defaultshash,$dom);
10970:         if ($putresult eq 'ok') {
10971:             if (keys(%changes) > 0) {
10972:                 my %toupdate;
10973:                 if (ref($changes{'delete'}) eq 'ARRAY') {
10974:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
10975:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
10976:                         $toupdate{$balancer} = 1;
10977:                     }
10978:                 }
10979:                 if (ref($changes{'add'}) eq 'ARRAY') {
10980:                     foreach my $balancer (sort(@{$changes{'add'}})) {
10981:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
10982:                         $toupdate{$balancer} = 1;
10983:                     }
10984:                 }
10985:                 if (ref($changes{'curr'}) eq 'HASH') {
10986:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
10987:                         $toupdate{$balancer} = 1;
10988:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
10989:                             if ($changes{'curr'}{$balancer}{'targets'}) {
10990:                                 my %offloadstr;
10991:                                 foreach my $sparetype (@sparestypes) {
10992:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10993:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10994:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10995:                                         }
10996:                                     }
10997:                                 }
10998:                                 if (keys(%offloadstr) == 0) {
10999:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
11000:                                 } else {
11001:                                     my $showoffload;
11002:                                     foreach my $sparetype (@sparestypes) {
11003:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
11004:                                         if (defined($offloadstr{$sparetype})) {
11005:                                             $showoffload .= $offloadstr{$sparetype};
11006:                                         } else {
11007:                                             $showoffload .= &mt('None');
11008:                                         }
11009:                                         $showoffload .= ('&nbsp;'x3);
11010:                                     }
11011:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
11012:                                 }
11013:                             }
11014:                         }
11015:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
11016:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
11017:                                 foreach my $type (@{$alltypes}) {
11018:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
11019:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
11020:                                         my $balancetext;
11021:                                         if ($rule eq '') {
11022:                                             $balancetext =  $ruletitles{'default'};
11023:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
11024:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
11025:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
11026:                                                 foreach my $sparetype (@sparestypes) {
11027:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
11028:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
11029:                                                     }
11030:                                                 }
11031:                                                 foreach my $item (@{$alltypes}) {
11032:                                                     next if ($item =~  /^_LC_ipchange/);
11033:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
11034:                                                     if ($hasrule eq 'homeserver') {
11035:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
11036:                                                     } else {
11037:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
11038:                                                             if ($servers{$hasrule}) {
11039:                                                                 $toupdate{$hasrule} = 1;
11040:                                                             }
11041:                                                         }
11042:                                                     }
11043:                                                 }
11044:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
11045:                                                     $balancetext =  $ruletitles{$rule};
11046:                                                 } else {
11047:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
11048:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
11049:                                                     if ($receiver) {
11050:                                                         $toupdate{$receiver};
11051:                                                     }
11052:                                                 }
11053:                                             } else {
11054:                                                 $balancetext =  $ruletitles{$rule};
11055:                                             }
11056:                                         } else {
11057:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
11058:                                         }
11059:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
11060:                                     }
11061:                                 }
11062:                             }
11063:                         }
11064:                         if (keys(%toupdate)) {
11065:                             my %thismachine;
11066:                             my $updatedhere;
11067:                             my $cachetime = 60*60*24;
11068:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
11069:                             foreach my $lonhost (keys(%toupdate)) {
11070:                                 if ($thismachine{$lonhost}) {
11071:                                     unless ($updatedhere) {
11072:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
11073:                                                                       $defaultshash{'loadbalancing'},
11074:                                                                       $cachetime);
11075:                                         $updatedhere = 1;
11076:                                     }
11077:                                 } else {
11078:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
11079:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
11080:                                 }
11081:                             }
11082:                         }
11083:                     }
11084:                 }
11085:                 if ($resulttext ne '') {
11086:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
11087:                 } else {
11088:                     $resulttext = $nochgmsg;
11089:                 }
11090:             } else {
11091:                 $resulttext = $nochgmsg;
11092:             }
11093:         } else {
11094:             $resulttext = '<span class="LC_error">'.
11095:                           &mt('An error occurred: [_1]',$putresult).'</span>';
11096:         }
11097:     } else {
11098:         $resulttext = $nochgmsg;
11099:     }
11100:     return $resulttext;
11101: }
11102: 
11103: sub recurse_check {
11104:     my ($chkcats,$categories,$depth,$name) = @_;
11105:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
11106:         my $chg = 0;
11107:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
11108:             my $category = $chkcats->[$depth]{$name}[$j];
11109:             my $item;
11110:             if ($category eq '') {
11111:                 $chg ++;
11112:             } else {
11113:                 my $deeper = $depth + 1;
11114:                 $item = &escape($category).':'.&escape($name).':'.$depth;
11115:                 if ($chg) {
11116:                     $categories->{$item} -= $chg;
11117:                 }
11118:                 &recurse_check($chkcats,$categories,$deeper,$category);
11119:                 $deeper --;
11120:             }
11121:         }
11122:     }
11123:     return;
11124: }
11125: 
11126: sub recurse_cat_deletes {
11127:     my ($item,$coursecategories,$deletions) = @_;
11128:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
11129:     my $subdepth = $depth + 1;
11130:     if (ref($coursecategories) eq 'HASH') {
11131:         foreach my $subitem (keys(%{$coursecategories})) {
11132:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
11133:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
11134:                 delete($coursecategories->{$subitem});
11135:                 $deletions->{$subitem} = 1;
11136:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
11137:             }
11138:         }
11139:     }
11140:     return;
11141: }
11142: 
11143: sub get_active_dcs {
11144:     my ($dom) = @_;
11145:     my $now = time;
11146:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
11147:     my %domcoords;
11148:     my $numdcs = 0;
11149:     foreach my $server (keys(%dompersonnel)) {
11150:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
11151:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
11152:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
11153:         }
11154:     }
11155:     return %domcoords;
11156: }
11157: 
11158: sub active_dc_picker {
11159:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
11160:     my %domcoords = &get_active_dcs($dom);
11161:     my @domcoord = keys(%domcoords);
11162:     if (keys(%currhash)) {
11163:         foreach my $dc (keys(%currhash)) {
11164:             unless (exists($domcoords{$dc})) {
11165:                 push(@domcoord,$dc);
11166:             }
11167:         }
11168:     }
11169:     @domcoord = sort(@domcoord);
11170:     my $numdcs = scalar(@domcoord);
11171:     my $rows = 0;
11172:     my $table;
11173:     if ($numdcs > 1) {
11174:         $table = '<table>';
11175:         for (my $i=0; $i<@domcoord; $i++) {
11176:             my $rem = $i%($numinrow);
11177:             if ($rem == 0) {
11178:                 if ($i > 0) {
11179:                     $table .= '</tr>';
11180:                 }
11181:                 $table .= '<tr>';
11182:                 $rows ++;
11183:             }
11184:             my $check = '';
11185:             if ($inputtype eq 'radio') {
11186:                 if (keys(%currhash) == 0) {
11187:                     if (!$i) {
11188:                         $check = ' checked="checked"';
11189:                     }
11190:                 } elsif (exists($currhash{$domcoord[$i]})) {
11191:                     $check = ' checked="checked"';
11192:                 }
11193:             } else {
11194:                 if (exists($currhash{$domcoord[$i]})) {
11195:                     $check = ' checked="checked"';
11196:                 }
11197:             }
11198:             if ($i == @domcoord - 1) {
11199:                 my $colsleft = $numinrow - $rem;
11200:                 if ($colsleft > 1) {
11201:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
11202:                 } else {
11203:                     $table .= '<td class="LC_left_item">';
11204:                 }
11205:             } else {
11206:                 $table .= '<td class="LC_left_item">';
11207:             }
11208:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
11209:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
11210:             $table .= '<span class="LC_nobreak"><label>'.
11211:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
11212:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
11213:             if ($user ne $dcname.':'.$dcdom) {
11214:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11215:             }
11216:             $table .= '</label></span></td>';
11217:         }
11218:         $table .= '</tr></table>';
11219:     } elsif ($numdcs == 1) {
11220:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
11221:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
11222:         if ($inputtype eq 'radio') {
11223:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
11224:             if ($user ne $dcname.':'.$dcdom) {
11225:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11226:             }
11227:         } else {
11228:             my $check;
11229:             if (exists($currhash{$domcoord[0]})) {
11230:                 $check = ' checked="checked"';
11231:             }
11232:             $table = '<span class="LC_nobreak"><label>'.
11233:                      '<input type="checkbox" name="'.$name.'" '.
11234:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
11235:             if ($user ne $dcname.':'.$dcdom) {
11236:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11237:             }
11238:             $table .= '</label></span>';
11239:             $rows ++;
11240:         }
11241:     }
11242:     return ($numdcs,$table,$rows);
11243: }
11244: 
11245: sub usersession_titles {
11246:     return &Apache::lonlocal::texthash(
11247:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
11248:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
11249:                spares => 'Servers offloaded to, when busy',
11250:                version => 'LON-CAPA version requirement',
11251:                excludedomain => 'Allow all, but exclude specific domains',
11252:                includedomain => 'Deny all, but include specific domains',
11253:                primary => 'Primary (checked first)',
11254:                default => 'Default',
11255:            );
11256: }
11257: 
11258: sub id_for_thisdom {
11259:     my (%servers) = @_;
11260:     my %altids;
11261:     foreach my $server (keys(%servers)) {
11262:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
11263:         if ($serverhome ne $server) {
11264:             $altids{$serverhome} = $server;
11265:         }
11266:     }
11267:     return %altids;
11268: }
11269: 
11270: sub count_servers {
11271:     my ($currbalancer,%servers) = @_;
11272:     my (@spares,$numspares);
11273:     foreach my $lonhost (sort(keys(%servers))) {
11274:         next if ($currbalancer eq $lonhost);
11275:         push(@spares,$lonhost);
11276:     }
11277:     if ($currbalancer) {
11278:         $numspares = scalar(@spares);
11279:     } else {
11280:         $numspares = scalar(@spares) - 1;
11281:     }
11282:     return ($numspares,@spares);
11283: }
11284: 
11285: sub lonbalance_targets_js {
11286:     my ($dom,$types,$servers,$settings) = @_;
11287:     my $select = &mt('Select');
11288:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
11289:     if (ref($servers) eq 'HASH') {
11290:         $alltargets = join("','",sort(keys(%{$servers})));
11291:         my @homedoms;
11292:         foreach my $server (sort(keys(%{$servers}))) {
11293:             if (&Apache::lonnet::host_domain($server) eq $dom) {
11294:                 push(@homedoms,'1');
11295:             } else {
11296:                 push(@homedoms,'0');
11297:             }
11298:         }
11299:         $allishome = join("','",@homedoms);
11300:     }
11301:     if (ref($types) eq 'ARRAY') {
11302:         if (@{$types} > 0) {
11303:             @alltypes = @{$types};
11304:         }
11305:     }
11306:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
11307:     $allinsttypes = join("','",@alltypes);
11308:     my (%currbalancer,%currtargets,%currrules,%existing);
11309:     if (ref($settings) eq 'HASH') {
11310:         %existing = %{$settings};
11311:     }
11312:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
11313:                               \%currtargets,\%currrules);
11314:     my $balancers = join("','",sort(keys(%currbalancer)));
11315:     return <<"END";
11316: 
11317: <script type="text/javascript">
11318: // <![CDATA[
11319: 
11320: currBalancers = new Array('$balancers');
11321: 
11322: function toggleTargets(balnum) {
11323:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11324:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
11325:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
11326:     var prevbalancer = prevhostitem.value;
11327:     var baltotal = document.getElementById('loadbalancing_total').value;
11328:     prevhostitem.value = balancer;
11329:     if (prevbalancer != '') {
11330:         var prevIdx = currBalancers.indexOf(prevbalancer);
11331:         if (prevIdx != -1) {
11332:             currBalancers.splice(prevIdx,1);
11333:         }
11334:     }
11335:     if (balancer == '') {
11336:         hideSpares(balnum);
11337:     } else {
11338:         var currIdx = currBalancers.indexOf(balancer);
11339:         if (currIdx == -1) {
11340:             currBalancers.push(balancer);
11341:         }
11342:         var homedoms = new Array('$allishome');
11343:         var ishomedom = homedoms[lonhostitem.selectedIndex];
11344:         showSpares(balancer,ishomedom,balnum);
11345:     }
11346:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
11347:     return;
11348: }
11349: 
11350: function showSpares(balancer,ishomedom,balnum) {
11351:     var alltargets = new Array('$alltargets');
11352:     var insttypes = new Array('$allinsttypes');
11353:     var offloadtypes = new Array('primary','default');
11354: 
11355:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
11356:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
11357:  
11358:     for (var i=0; i<offloadtypes.length; i++) {
11359:         var count = 0;
11360:         for (var j=0; j<alltargets.length; j++) {
11361:             if (alltargets[j] != balancer) {
11362:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
11363:                 item.value = alltargets[j];
11364:                 item.style.textAlign='left';
11365:                 item.style.textFace='normal';
11366:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
11367:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
11368:                     item.disabled = '';
11369:                 } else {
11370:                     item.disabled = 'disabled';
11371:                     item.checked = false;
11372:                 }
11373:                 count ++;
11374:             }
11375:         }
11376:     }
11377:     for (var k=0; k<insttypes.length; k++) {
11378:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
11379:             if (ishomedom == 1) {
11380:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11381:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11382:             } else {
11383:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11384:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11385:             }
11386:         } else {
11387:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11388:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11389:         }
11390:         if ((insttypes[k] != '_LC_external') && 
11391:             ((insttypes[k] != '_LC_internetdom') ||
11392:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
11393:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
11394:             item.options.length = 0;
11395:             item.options[0] = new Option("","",true,true);
11396:             var idx = 0;
11397:             for (var m=0; m<alltargets.length; m++) {
11398:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
11399:                     idx ++;
11400:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11401:                 }
11402:             }
11403:         }
11404:     }
11405:     return;
11406: }
11407: 
11408: function hideSpares(balnum) {
11409:     var alltargets = new Array('$alltargets');
11410:     var insttypes = new Array('$allinsttypes');
11411:     var offloadtypes = new Array('primary','default');
11412: 
11413:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
11414:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
11415: 
11416:     var total = alltargets.length - 1;
11417:     for (var i=0; i<offloadtypes; i++) {
11418:         for (var j=0; j<total; j++) {
11419:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
11420:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
11421:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
11422:         }
11423:     }
11424:     for (var k=0; k<insttypes.length; k++) {
11425:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11426:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11427:         if (insttypes[k] != '_LC_external') {
11428:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
11429:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
11430:         }
11431:     }
11432:     return;
11433: }
11434: 
11435: function checkOffloads(item,balnum,type) {
11436:     var alltargets = new Array('$alltargets');
11437:     var offloadtypes = new Array('primary','default');
11438:     if (item.checked) {
11439:         var total = alltargets.length - 1;
11440:         var other;
11441:         if (type == offloadtypes[0]) {
11442:             other = offloadtypes[1];
11443:         } else {
11444:             other = offloadtypes[0];
11445:         }
11446:         for (var i=0; i<total; i++) {
11447:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
11448:             if (server == item.value) {
11449:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
11450:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
11451:                 }
11452:             }
11453:         }
11454:     }
11455:     return;
11456: }
11457: 
11458: function singleServerToggle(balnum,type) {
11459:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
11460:     if (offloadtoSelIdx == 0) {
11461:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
11462:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11463: 
11464:     } else {
11465:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
11466:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11467:     }
11468:     return;
11469: }
11470: 
11471: function balanceruleChange(formname,balnum,type) {
11472:     if (type == '_LC_external') {
11473:         return;
11474:     }
11475:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
11476:     for (var i=0; i<typesRules.length; i++) {
11477:         if (formname.elements[typesRules[i]].checked) {
11478:             if (formname.elements[typesRules[i]].value != 'specific') {
11479:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
11480:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11481:             } else {
11482:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11483:             }
11484:         }
11485:     }
11486:     return;
11487: }
11488: 
11489: function balancerDeleteChange(balnum) {
11490:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11491:     var baltotal = document.getElementById('loadbalancing_total').value;
11492:     var addtarget;
11493:     var removetarget;
11494:     var action = 'delete';
11495:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
11496:         var lonhost = hostitem.value;
11497:         var currIdx = currBalancers.indexOf(lonhost);
11498:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
11499:             if (currIdx != -1) {
11500:                 currBalancers.splice(currIdx,1);
11501:             }
11502:             addtarget = lonhost;
11503:         } else {
11504:             if (currIdx == -1) {
11505:                 currBalancers.push(lonhost);
11506:             }
11507:             removetarget = lonhost;
11508:             action = 'undelete';
11509:         }
11510:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
11511:     }
11512:     return;
11513: }
11514: 
11515: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
11516:     if (baltotal > 1) {
11517:         var offloadtypes = new Array('primary','default');
11518:         var alltargets = new Array('$alltargets');
11519:         var insttypes = new Array('$allinsttypes');
11520:         for (var i=0; i<baltotal; i++) {
11521:             if (i != balnum) {
11522:                 for (var j=0; j<offloadtypes.length; j++) {
11523:                     var total = alltargets.length - 1;
11524:                     for (var k=0; k<total; k++) {
11525:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
11526:                         var server = serveritem.value;
11527:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
11528:                             if (server == addtarget) {
11529:                                 serveritem.disabled = '';
11530:                             }
11531:                         }
11532:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11533:                             if (server == removetarget) {
11534:                                 serveritem.disabled = 'disabled';
11535:                                 serveritem.checked = false;
11536:                             }
11537:                         }
11538:                     }
11539:                 }
11540:                 for (var j=0; j<insttypes.length; j++) {
11541:                     if (insttypes[j] != '_LC_external') {
11542:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
11543:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
11544:                             var currSel = singleserver.selectedIndex;
11545:                             var currVal = singleserver.options[currSel].value;
11546:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
11547:                                 var numoptions = singleserver.options.length;
11548:                                 var needsnew = 1;
11549:                                 for (var k=0; k<numoptions; k++) {
11550:                                     if (singleserver.options[k] == addtarget) {
11551:                                         needsnew = 0;
11552:                                         break;
11553:                                     }
11554:                                 }
11555:                                 if (needsnew == 1) {
11556:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
11557:                                 }
11558:                             }
11559:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11560:                                 singleserver.options.length = 0;
11561:                                 if ((currVal) && (currVal != removetarget)) {
11562:                                     singleserver.options[0] = new Option("","",false,false);
11563:                                 } else {
11564:                                     singleserver.options[0] = new Option("","",true,true);
11565:                                 }
11566:                                 var idx = 0;
11567:                                 for (var m=0; m<alltargets.length; m++) {
11568:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
11569:                                         idx ++;
11570:                                         if (currVal == alltargets[m]) {
11571:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
11572:                                         } else {
11573:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11574:                                         }
11575:                                     }
11576:                                 }
11577:                             }
11578:                         }
11579:                     }
11580:                 }
11581:             }
11582:         }
11583:     }
11584:     return;
11585: }
11586: 
11587: // ]]>
11588: </script>
11589: 
11590: END
11591: }
11592: 
11593: sub new_spares_js {
11594:     my @sparestypes = ('primary','default');
11595:     my $types = join("','",@sparestypes);
11596:     my $select = &mt('Select');
11597:     return <<"END";
11598: 
11599: <script type="text/javascript">
11600: // <![CDATA[
11601: 
11602: function updateNewSpares(formname,lonhost) {
11603:     var types = new Array('$types');
11604:     var include = new Array();
11605:     var exclude = new Array();
11606:     for (var i=0; i<types.length; i++) {
11607:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
11608:         for (var j=0; j<spareboxes.length; j++) {
11609:             if (formname.elements[spareboxes[j]].checked) {
11610:                 exclude.push(formname.elements[spareboxes[j]].value);
11611:             } else {
11612:                 include.push(formname.elements[spareboxes[j]].value);
11613:             }
11614:         }
11615:     }
11616:     for (var i=0; i<types.length; i++) {
11617:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
11618:         var selIdx = newSpare.selectedIndex;
11619:         var currnew = newSpare.options[selIdx].value;
11620:         var okSpares = new Array();
11621:         for (var j=0; j<newSpare.options.length; j++) {
11622:             var possible = newSpare.options[j].value;
11623:             if (possible != '') {
11624:                 if (exclude.indexOf(possible) == -1) {
11625:                     okSpares.push(possible);
11626:                 } else {
11627:                     if (currnew == possible) {
11628:                         selIdx = 0;
11629:                     }
11630:                 }
11631:             }
11632:         }
11633:         for (var k=0; k<include.length; k++) {
11634:             if (okSpares.indexOf(include[k]) == -1) {
11635:                 okSpares.push(include[k]);
11636:             }
11637:         }
11638:         okSpares.sort();
11639:         newSpare.options.length = 0;
11640:         if (selIdx == 0) {
11641:             newSpare.options[0] = new Option("$select","",true,true);
11642:         } else {
11643:             newSpare.options[0] = new Option("$select","",false,false);
11644:         }
11645:         for (var m=0; m<okSpares.length; m++) {
11646:             var idx = m+1;
11647:             var selThis = 0;
11648:             if (selIdx != 0) {
11649:                 if (okSpares[m] == currnew) {
11650:                     selThis = 1;
11651:                 }
11652:             }
11653:             if (selThis == 1) {
11654:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
11655:             } else {
11656:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
11657:             }
11658:         }
11659:     }
11660:     return;
11661: }
11662: 
11663: function checkNewSpares(lonhost,type) {
11664:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
11665:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
11666:     if (chosen != '') { 
11667:         var othertype;
11668:         var othernewSpare;
11669:         if (type == 'primary') {
11670:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
11671:         }
11672:         if (type == 'default') {
11673:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
11674:         }
11675:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
11676:             othernewSpare.selectedIndex = 0;
11677:         }
11678:     }
11679:     return;
11680: }
11681: 
11682: // ]]>
11683: </script>
11684: 
11685: END
11686: 
11687: }
11688: 
11689: sub common_domprefs_js {
11690:     return <<"END";
11691: 
11692: <script type="text/javascript">
11693: // <![CDATA[
11694: 
11695: function getIndicesByName(formname,item) {
11696:     var group = new Array();
11697:     for (var i=0;i<formname.elements.length;i++) {
11698:         if (formname.elements[i].name == item) {
11699:             group.push(formname.elements[i].id);
11700:         }
11701:     }
11702:     return group;
11703: }
11704: 
11705: // ]]>
11706: </script>
11707: 
11708: END
11709: 
11710: }
11711: 
11712: sub recaptcha_js {
11713:     my %lt = &captcha_phrases();
11714:     return <<"END";
11715: 
11716: <script type="text/javascript">
11717: // <![CDATA[
11718: 
11719: function updateCaptcha(caller,context) {
11720:     var privitem;
11721:     var pubitem;
11722:     var privtext;
11723:     var pubtext;
11724:     if (document.getElementById(context+'_recaptchapub')) {
11725:         pubitem = document.getElementById(context+'_recaptchapub');
11726:     } else {
11727:         return;
11728:     }
11729:     if (document.getElementById(context+'_recaptchapriv')) {
11730:         privitem = document.getElementById(context+'_recaptchapriv');
11731:     } else {
11732:         return;
11733:     }
11734:     if (document.getElementById(context+'_recaptchapubtxt')) {
11735:         pubtext = document.getElementById(context+'_recaptchapubtxt');
11736:     } else {
11737:         return;
11738:     }
11739:     if (document.getElementById(context+'_recaptchaprivtxt')) {
11740:         privtext = document.getElementById(context+'_recaptchaprivtxt');
11741:     } else {
11742:         return;
11743:     }
11744:     if (caller.checked) {
11745:         if (caller.value == 'recaptcha') {
11746:             pubitem.type = 'text';
11747:             privitem.type = 'text';
11748:             pubitem.size = '40';
11749:             privitem.size = '40';
11750:             pubtext.innerHTML = "$lt{'pub'}";
11751:             privtext.innerHTML = "$lt{'priv'}";
11752:         } else {
11753:             pubitem.type = 'hidden';
11754:             privitem.type = 'hidden';
11755:             pubtext.innerHTML = '';
11756:             privtext.innerHTML = '';
11757:         }
11758:     }
11759:     return;
11760: }
11761: 
11762: // ]]>
11763: </script>
11764: 
11765: END
11766: 
11767: }
11768: 
11769: sub toggle_display_js {
11770:     return <<"END";
11771: 
11772: <script type="text/javascript">
11773: // <![CDATA[
11774: 
11775: function toggleDisplay(domForm,caller) {
11776:     if (document.getElementById(caller)) {
11777:         var divitem = document.getElementById(caller);
11778:         var optionsElement = domForm.coursecredits;
11779:         if (caller == 'emailoptions') {
11780:             optionsElement = domForm.cancreate_email; 
11781:         }
11782:         if (optionsElement.length) {
11783:             var currval;
11784:             for (var i=0; i<optionsElement.length; i++) {
11785:                 if (optionsElement[i].checked) {
11786:                    currval = optionsElement[i].value;
11787:                 }
11788:             }
11789:             if (currval == 1) {
11790:                 divitem.style.display = 'block';
11791:             } else {
11792:                 divitem.style.display = 'none';
11793:             }
11794:         }
11795:     }
11796:     return;
11797: }
11798: 
11799: // ]]>
11800: </script>
11801: 
11802: END
11803: 
11804: }
11805: 
11806: sub captcha_phrases {
11807:     return &Apache::lonlocal::texthash (
11808:                  priv => 'Private key',
11809:                  pub  => 'Public key',
11810:                  original  => 'original (CAPTCHA)',
11811:                  recaptcha => 'successor (ReCAPTCHA)',
11812:                  notused   => 'unused',
11813:     );
11814: }
11815: 
11816: sub devalidate_remote_domconfs {
11817:     my ($dom,$cachekeys) = @_;
11818:     return unless (ref($cachekeys) eq 'HASH');
11819:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11820:     my %thismachine;
11821:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
11822:     my @posscached = ('domainconfig','domdefaults');
11823:     if (keys(%servers) > 1) {
11824:         foreach my $server (keys(%servers)) {
11825:             next if ($thismachine{$server});
11826:             my @cached;
11827:             foreach my $name (@posscached) {
11828:                 if ($cachekeys->{$name}) {
11829:                     push(@cached,&escape($name).':'.&escape($dom));
11830:                 }
11831:             }
11832:             if (@cached) {
11833:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
11834:             }
11835:         }
11836:     }
11837:     return;
11838: }
11839: 
11840: 1;

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