File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.55: download - view: text, annotated - select for diffs
Tue Oct 7 03:16:05 2014 UTC (9 years, 7 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  - Backport 1.253, 1.254, 1.255.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.55 2014/10/07 03:16:05 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:                       print => \&print_login,
  255:                       modify => \&modify_login,
  256:                     },
  257:         'defaults' => 
  258:                     { text => 'Default authentication/language/timezone/portal/types',
  259:                       help => 'Domain_Configuration_LangTZAuth',
  260:                       header => [{col1 => 'Setting',
  261:                                   col2 => 'Value'},
  262:                                  {col1 => 'Institutional user types',
  263:                                   col2 => 'Assignable to e-mail usernames'}],
  264:                       print => \&print_defaults,
  265:                       modify => \&modify_defaults,
  266:                     },
  267:         'quotas' => 
  268:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  269:                       help => 'Domain_Configuration_Quotas',
  270:                       header => [{col1 => 'User affiliation',
  271:                                   col2 => 'Available tools',
  272:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  273:                       print => \&print_quotas,
  274:                       modify => \&modify_quotas,
  275:                     },
  276:         'autoenroll' =>
  277:                    { text => 'Auto-enrollment settings',
  278:                      help => 'Domain_Configuration_Auto_Enrollment',
  279:                      header => [{col1 => 'Configuration setting',
  280:                                  col2 => 'Value(s)'}],
  281:                      print => \&print_autoenroll,
  282:                      modify => \&modify_autoenroll,
  283:                    },
  284:         'autoupdate' => 
  285:                    { text => 'Auto-update settings',
  286:                      help => 'Domain_Configuration_Auto_Updates',
  287:                      header => [{col1 => 'Setting',
  288:                                  col2 => 'Value',},
  289:                                 {col1 => 'Setting',
  290:                                  col2 => 'Affiliation'},
  291:                                 {col1 => 'User population',
  292:                                  col2 => 'Updatable user data'}],
  293:                      print => \&print_autoupdate,
  294:                      modify => \&modify_autoupdate,
  295:                   },
  296:         'autocreate' => 
  297:                   { text => 'Auto-course creation settings',
  298:                      help => 'Domain_Configuration_Auto_Creation',
  299:                      header => [{col1 => 'Configuration Setting',
  300:                                  col2 => 'Value',}],
  301:                      print => \&print_autocreate,
  302:                      modify => \&modify_autocreate,
  303:                   },
  304:         'directorysrch' => 
  305:                   { text => 'Institutional directory searches',
  306:                     help => 'Domain_Configuration_InstDirectory_Search',
  307:                     header => [{col1 => 'Setting',
  308:                                 col2 => 'Value',}],
  309:                     print => \&print_directorysrch,
  310:                     modify => \&modify_directorysrch,
  311:                   },
  312:         'contacts' =>
  313:                   { text => 'Contact Information',
  314:                     help => 'Domain_Configuration_Contact_Info',
  315:                     header => [{col1 => 'Setting',
  316:                                 col2 => 'Value',}],
  317:                     print => \&print_contacts,
  318:                     modify => \&modify_contacts,
  319:                   },
  320:         'usercreation' => 
  321:                   { text => 'User creation',
  322:                     help => 'Domain_Configuration_User_Creation',
  323:                     header => [{col1 => 'Format rule type',
  324:                                 col2 => 'Format rules in force'},
  325:                                {col1 => 'User account creation',
  326:                                 col2 => 'Usernames which may be created',},
  327:                                {col1 => 'Context',
  328:                                 col2 => 'Assignable authentication types'}],
  329:                     print => \&print_usercreation,
  330:                     modify => \&modify_usercreation,
  331:                   },
  332:         'selfcreation' => 
  333:                   { text => 'Users self-creating accounts',
  334:                     help => 'Domain_Configuration_Self_Creation', 
  335:                     header => [{col1 => 'Self-creation with institutional username',
  336:                                 col2 => 'Enabled?'},
  337:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  338:                                 col2 => 'Information user can enter'},
  339:                                {col1 => 'Self-creation with e-mail as username',
  340:                                 col2 => 'Settings'}],
  341:                     print => \&print_selfcreation,
  342:                     modify => \&modify_selfcreation,
  343:                   },
  344:         'usermodification' =>
  345:                   { text => 'User modification',
  346:                     help => 'Domain_Configuration_User_Modification',
  347:                     header => [{col1 => 'Target user has role',
  348:                                 col2 => 'User information updatable in author context'},
  349:                                {col1 => 'Target user has role',
  350:                                 col2 => 'User information updatable in course context'}],
  351:                     print => \&print_usermodification,
  352:                     modify => \&modify_usermodification,
  353:                   },
  354:         'scantron' =>
  355:                   { text => 'Bubblesheet format file',
  356:                     help => 'Domain_Configuration_Scantron_Format',
  357:                     header => [ {col1 => 'Item',
  358:                                  col2 => '',
  359:                               }],
  360:                     print => \&print_scantron,
  361:                     modify => \&modify_scantron,
  362:                   },
  363:         'requestcourses' => 
  364:                  {text => 'Request creation of courses',
  365:                   help => 'Domain_Configuration_Request_Courses',
  366:                   header => [{col1 => 'User affiliation',
  367:                               col2 => 'Availability/Processing of requests',},
  368:                              {col1 => 'Setting',
  369:                               col2 => 'Value'},
  370:                              {col1 => 'Available textbooks',
  371:                               col2 => ''},
  372:                              {col1 => 'Available templates',
  373:                               col2 => ''},
  374:                              {col1 => 'Validation (not official courses)',
  375:                               col2 => 'Value'},],
  376:                   print => \&print_quotas,
  377:                   modify => \&modify_quotas,
  378:                  },
  379:         'requestauthor' =>
  380:                  {text => 'Request Authoring Space',
  381:                   help => 'Domain_Configuration_Request_Author',
  382:                   header => [{col1 => 'User affiliation',
  383:                               col2 => 'Availability/Processing of requests',},
  384:                              {col1 => 'Setting',
  385:                               col2 => 'Value'}],
  386:                   print => \&print_quotas,
  387:                   modify => \&modify_quotas,
  388:                  },
  389:         'coursecategories' =>
  390:                   { text => 'Cataloging of courses/communities',
  391:                     help => 'Domain_Configuration_Cataloging_Courses',
  392:                     header => [{col1 => 'Catalog type/availability',
  393:                                 col2 => '',},
  394:                                {col1 => 'Category settings for standard catalog',
  395:                                 col2 => '',},
  396:                                {col1 => 'Categories',
  397:                                 col2 => '',
  398:                                }],
  399:                     print => \&print_coursecategories,
  400:                     modify => \&modify_coursecategories,
  401:                   },
  402:         'serverstatuses' =>
  403:                  {text   => 'Access to server status pages',
  404:                   help   => 'Domain_Configuration_Server_Status',
  405:                   header => [{col1 => 'Status Page',
  406:                               col2 => 'Other named users',
  407:                               col3 => 'Specific IPs',
  408:                             }],
  409:                   print => \&print_serverstatuses,
  410:                   modify => \&modify_serverstatuses,
  411:                  },
  412:         'coursedefaults' => 
  413:                  {text => 'Course/Community defaults',
  414:                   help => 'Domain_Configuration_Course_Defaults',
  415:                   header => [{col1 => 'Defaults which can be overridden for each course by a DC',
  416:                               col2 => 'Value',},],
  417:                   print => \&print_coursedefaults,
  418:                   modify => \&modify_coursedefaults,
  419:                  },
  420:         'selfenrollment' => 
  421:                  {text   => 'Self-enrollment in Course/Community',
  422:                   help   => 'Domain_Configuration_Selfenrollment',
  423:                   header => [{col1 => 'Configuration Rights',
  424:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  425:                              {col1 => 'Defaults',
  426:                               col2 => 'Value'},
  427:                              {col1 => 'Self-enrollment validation (optional)',
  428:                               col2 => 'Value'},],
  429:                   print => \&print_selfenrollment,
  430:                   modify => \&modify_selfenrollment,
  431:                  },
  432:         'usersessions' =>
  433:                  {text  => 'User session hosting/offloading',
  434:                   help  => 'Domain_Configuration_User_Sessions',
  435:                   header => [{col1 => 'Domain server',
  436:                               col2 => 'Servers to offload sessions to when busy'},
  437:                              {col1 => 'Hosting of users from other domains',
  438:                               col2 => 'Rules'},
  439:                              {col1 => "Hosting domain's own users elsewhere",
  440:                               col2 => 'Rules'}],
  441:                   print => \&print_usersessions,
  442:                   modify => \&modify_usersessions,
  443:                  },
  444:          'loadbalancing' =>
  445:                  {text  => 'Dedicated Load Balancer(s)',
  446:                   help  => 'Domain_Configuration_Load_Balancing',
  447:                   header => [{col1 => 'Balancers',
  448:                               col2 => 'Default destinations',
  449:                               col3 => 'User affiliation',
  450:                               col4 => 'Overrides'},
  451:                             ],
  452:                   print => \&print_loadbalancing,
  453:                   modify => \&modify_loadbalancing,
  454:                  },
  455:     );
  456:     if (keys(%servers) > 1) {
  457:         $prefs{'login'}  = { text   => 'Log-in page options',
  458:                              help   => 'Domain_Configuration_Login_Page',
  459:                             header => [{col1 => 'Log-in Service',
  460:                                         col2 => 'Server Setting',},
  461:                                        {col1 => 'Log-in Page Items',
  462:                                         col2 => ''},
  463:                                        {col1 => 'Log-in Help',
  464:                                         col2 => 'Value'}],
  465:                             print => \&print_login,
  466:                             modify => \&modify_login,
  467:                            };
  468:     }
  469: 
  470:     my @roles = ('student','coordinator','author','admin');
  471:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  472:     &Apache::lonhtmlcommon::add_breadcrumb
  473:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  474:       text=>"Settings to display/modify"});
  475:     my $confname = $dom.'-domainconfig';
  476: 
  477:     if ($phase eq 'process') {
  478:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  479:                                                               \%prefs,\%domconfig,$confname,\@roles);
  480:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  481:             $r->rflush();
  482:             &devalidate_remote_domconfs($dom,$result);
  483:         }
  484:     } elsif ($phase eq 'display') {
  485:         my $js = &recaptcha_js().
  486:                  &toggle_display_js();
  487:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  488:             my ($othertitle,$usertypes,$types) =
  489:                 &Apache::loncommon::sorted_inst_types($dom);
  490:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  491:                                           $domconfig{'loadbalancing'}).
  492:                    &new_spares_js().
  493:                    &common_domprefs_js().
  494:                    &Apache::loncommon::javascript_array_indexof();
  495:         }
  496:         if (grep(/^requestcourses$/,@actions)) {
  497:             my $javascript_validations;
  498:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  499:             $js .= <<END;
  500: <script type="text/javascript">
  501: $javascript_validations
  502: </script>
  503: $coursebrowserjs
  504: END
  505:         }
  506:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  507:     } else {
  508: # check if domconfig user exists for the domain.
  509:         my $servadm = $r->dir_config('lonAdmEMail');
  510:         my ($configuserok,$author_ok,$switchserver) =
  511:             &config_check($dom,$confname,$servadm);
  512:         unless ($configuserok eq 'ok') {
  513:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  514:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  515:                           $confname).
  516:                       '<br />'
  517:             );
  518:             if ($switchserver) {
  519:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  520:                           '<br />'.
  521:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  522:                           '<br />'.
  523:                           &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).
  524:                           '<br />'.
  525:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  526:                 );
  527:             } else {
  528:                 $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.').
  529:                           '<br />'.
  530:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  531:                 );
  532:             }
  533:             $r->print(&Apache::loncommon::end_page());
  534:             return OK;
  535:         }
  536:         if (keys(%domconfig) == 0) {
  537:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  538:             my @ids=&Apache::lonnet::current_machine_ids();
  539:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  540:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  541:                 my @loginimages = ('img','logo','domlogo','login');
  542:                 my $custom_img_count = 0;
  543:                 foreach my $img (@loginimages) {
  544:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  545:                         $custom_img_count ++;
  546:                     }
  547:                 }
  548:                 foreach my $role (@roles) {
  549:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  550:                         $custom_img_count ++;
  551:                     }
  552:                 }
  553:                 if ($custom_img_count > 0) {
  554:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  555:                     my $switch_server = &check_switchserver($dom,$confname);
  556:                     $r->print(
  557:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  558:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  559:     &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 />'.
  560:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  561:                     if ($switch_server) {
  562:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  563:                     }
  564:                     $r->print(&Apache::loncommon::end_page());
  565:                     return OK;
  566:                 }
  567:             }
  568:         }
  569:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  570:     }
  571:     return OK;
  572: }
  573: 
  574: sub process_changes {
  575:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  576:     my %domconfig;
  577:     if (ref($values) eq 'HASH') {
  578:         %domconfig = %{$values};
  579:     }
  580:     my $output;
  581:     if ($action eq 'login') {
  582:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  583:     } elsif ($action eq 'rolecolors') {
  584:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  585:                                      $lastactref,%domconfig);
  586:     } elsif ($action eq 'quotas') {
  587:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  588:     } elsif ($action eq 'autoenroll') {
  589:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  590:     } elsif ($action eq 'autoupdate') {
  591:         $output = &modify_autoupdate($dom,%domconfig);
  592:     } elsif ($action eq 'autocreate') {
  593:         $output = &modify_autocreate($dom,%domconfig);
  594:     } elsif ($action eq 'directorysrch') {
  595:         $output = &modify_directorysrch($dom,%domconfig);
  596:     } elsif ($action eq 'usercreation') {
  597:         $output = &modify_usercreation($dom,%domconfig);
  598:     } elsif ($action eq 'selfcreation') {
  599:         $output = &modify_selfcreation($dom,%domconfig);
  600:     } elsif ($action eq 'usermodification') {
  601:         $output = &modify_usermodification($dom,%domconfig);
  602:     } elsif ($action eq 'contacts') {
  603:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  604:     } elsif ($action eq 'defaults') {
  605:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  606:     } elsif ($action eq 'scantron') {
  607:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  608:     } elsif ($action eq 'coursecategories') {
  609:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  610:     } elsif ($action eq 'serverstatuses') {
  611:         $output = &modify_serverstatuses($dom,%domconfig);
  612:     } elsif ($action eq 'requestcourses') {
  613:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  614:     } elsif ($action eq 'requestauthor') {
  615:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  616:     } elsif ($action eq 'coursedefaults') {
  617:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  618:     } elsif ($action eq 'selfenrollment') {
  619:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  620:     } elsif ($action eq 'usersessions') {
  621:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  622:     } elsif ($action eq 'loadbalancing') {
  623:         $output = &modify_loadbalancing($dom,%domconfig);
  624:     }
  625:     return $output;
  626: }
  627: 
  628: sub print_config_box {
  629:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  630:     my $rowtotal = 0;
  631:     my $output;
  632:     if ($action eq 'coursecategories') {
  633:         $output = &coursecategories_javascript($settings);
  634:     } elsif ($action eq 'defaults') {
  635:         $output = &defaults_javascript($settings); 
  636:     }
  637:     $output .=
  638:          '<table class="LC_nested_outer">
  639:           <tr>
  640:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  641:            &mt($item->{text}).'&nbsp;'.
  642:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  643:           '</tr>';
  644:     $rowtotal ++;
  645:     my $numheaders = 1;
  646:     if (ref($item->{'header'}) eq 'ARRAY') {
  647:         $numheaders = scalar(@{$item->{'header'}});
  648:     }
  649:     if ($numheaders > 1) {
  650:         my $colspan = '';
  651:         my $rightcolspan = '';
  652:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  653:             (($action eq 'login') && ($numheaders < 3))) {
  654:             $colspan = ' colspan="2"';
  655:         }
  656:         if ($action eq 'usersessions') {
  657:             $rightcolspan = ' colspan="3"'; 
  658:         }
  659:         $output .= '
  660:           <tr>
  661:            <td>
  662:             <table class="LC_nested">
  663:              <tr class="LC_info_row">
  664:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  665:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  666:              </tr>';
  667:         $rowtotal ++;
  668:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  669:             ($action eq 'usermodification') || ($action eq 'defaults') ||
  670:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
  671:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  672:         } elsif ($action eq 'coursecategories') {
  673:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  674:         } elsif ($action eq 'login') {
  675:             if ($numheaders == 3) {
  676:                 $colspan = ' colspan="2"';
  677:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  678:             } else {
  679:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  680:             }
  681:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  682:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  683:         } elsif ($action eq 'rolecolors') {
  684:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  685:         }
  686:         $output .= '
  687:            </table>
  688:           </td>
  689:          </tr>
  690:          <tr>
  691:            <td>
  692:             <table class="LC_nested">
  693:              <tr class="LC_info_row">
  694:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  695:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  696:              </tr>';
  697:             $rowtotal ++;
  698:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  699:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  700:             ($action eq 'usersessions') || ($action eq 'coursecategories')) {
  701:             if ($action eq 'coursecategories') {
  702:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  703:                 $colspan = ' colspan="2"';
  704:             } else {
  705:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  706:             }
  707:             $output .= '
  708:            </table>
  709:           </td>
  710:          </tr>
  711:          <tr>
  712:            <td>
  713:             <table class="LC_nested">
  714:              <tr class="LC_info_row">
  715:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  716:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  717:              </tr>'."\n";
  718:             if ($action eq 'coursecategories') {
  719:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  720:             } else {
  721:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  722:             }
  723:             $rowtotal ++;
  724:         } elsif (($action eq 'usermodification') || ($action eq 'defaults')) {
  725:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  726:         } elsif ($action eq 'login') {
  727:             if ($numheaders == 3) {
  728:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  729:            </table>
  730:           </td>
  731:          </tr>
  732:          <tr>
  733:            <td>
  734:             <table class="LC_nested">
  735:              <tr class="LC_info_row">
  736:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  737:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  738:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  739:                 $rowtotal ++;
  740:             } else {
  741:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  742:             }
  743:         } elsif ($action eq 'requestcourses') {
  744:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  745:             $rowtotal ++;
  746:             $output .= &print_studentcode($settings,\$rowtotal).'
  747:            </table>
  748:           </td>
  749:          </tr>
  750:          <tr>
  751:            <td>
  752:             <table class="LC_nested">
  753:              <tr class="LC_info_row">
  754:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  755:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  756:                        &textbookcourses_javascript($settings).
  757:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  758:             </table>
  759:            </td>
  760:           </tr>
  761:          <tr>
  762:            <td>
  763:             <table class="LC_nested">
  764:              <tr class="LC_info_row">
  765:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  766:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  767:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  768:             </table>
  769:            </td>
  770:           </tr>
  771:           <tr>
  772:            <td>
  773:             <table class="LC_nested">
  774:              <tr class="LC_info_row">
  775:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  776:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  777:              </tr>'.
  778:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  779:         } elsif ($action eq 'requestauthor') {
  780:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  781:             $rowtotal ++;
  782:         } elsif ($action eq 'rolecolors') {
  783:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  784:            </table>
  785:           </td>
  786:          </tr>
  787:          <tr>
  788:            <td>
  789:             <table class="LC_nested">
  790:              <tr class="LC_info_row">
  791:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  792:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  793:               <td class="LC_right_item" valign="top">'.
  794:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  795:              </tr>'.
  796:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  797:            </table>
  798:           </td>
  799:          </tr>
  800:          <tr>
  801:            <td>
  802:             <table class="LC_nested">
  803:              <tr class="LC_info_row">
  804:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  805:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  806:              </tr>'.
  807:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  808:             $rowtotal += 2;
  809:         }
  810:     } else {
  811:         $output .= '
  812:           <tr>
  813:            <td>
  814:             <table class="LC_nested">
  815:              <tr class="LC_info_row">';
  816:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  817:             $output .= '  
  818:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  819:         } elsif ($action eq 'serverstatuses') {
  820:             $output .= '
  821:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  822:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  823: 
  824:         } else {
  825:             $output .= '
  826:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  827:         }
  828:         if (defined($item->{'header'}->[0]->{'col3'})) {
  829:             $output .= '<td class="LC_left_item" valign="top">'.
  830:                        &mt($item->{'header'}->[0]->{'col2'});
  831:             if ($action eq 'serverstatuses') {
  832:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  833:             } 
  834:         } else {
  835:             $output .= '<td class="LC_right_item" valign="top">'.
  836:                        &mt($item->{'header'}->[0]->{'col2'});
  837:         }
  838:         $output .= '</td>';
  839:         if ($item->{'header'}->[0]->{'col3'}) {
  840:             if (defined($item->{'header'}->[0]->{'col4'})) {
  841:                 $output .= '<td class="LC_left_item" valign="top">'.
  842:                             &mt($item->{'header'}->[0]->{'col3'});
  843:             } else {
  844:                 $output .= '<td class="LC_right_item" valign="top">'.
  845:                            &mt($item->{'header'}->[0]->{'col3'});
  846:             }
  847:             if ($action eq 'serverstatuses') {
  848:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  849:             }
  850:             $output .= '</td>';
  851:         }
  852:         if ($item->{'header'}->[0]->{'col4'}) {
  853:             $output .= '<td class="LC_right_item" valign="top">'.
  854:                        &mt($item->{'header'}->[0]->{'col4'});
  855:         }
  856:         $output .= '</tr>';
  857:         $rowtotal ++;
  858:         if ($action eq 'quotas') {
  859:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  860:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  861:                  ($action eq 'contacts') || ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  862:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  863:         } elsif ($action eq 'scantron') {
  864:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  865:         } elsif ($action eq 'helpsettings') {
  866:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  867:         } elsif ($action eq 'coursedefaults') {
  868:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  869:         }
  870:     }
  871:     $output .= '
  872:    </table>
  873:   </td>
  874:  </tr>
  875: </table><br />';
  876:     return ($output,$rowtotal);
  877: }
  878: 
  879: sub print_login {
  880:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  881:     my ($css_class,$datatable);
  882:     my %choices = &login_choices();
  883: 
  884:     if ($caller eq 'service') {
  885:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  886:         my $choice = $choices{'disallowlogin'};
  887:         $css_class = ' class="LC_odd_row"';
  888:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  889:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  890:                       '<th>'.$choices{'server'}.'</th>'.
  891:                       '<th>'.$choices{'serverpath'}.'</th>'.
  892:                       '<th>'.$choices{'custompath'}.'</th>'.
  893:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  894:         my %disallowed;
  895:         if (ref($settings) eq 'HASH') {
  896:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  897:                %disallowed = %{$settings->{'loginvia'}};
  898:             }
  899:         }
  900:         foreach my $lonhost (sort(keys(%servers))) {
  901:             my $direct = 'selected="selected"';
  902:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  903:                 if ($disallowed{$lonhost}{'server'} ne '') {
  904:                     $direct = '';
  905:                 }
  906:             }
  907:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  908:                           '<td><select name="'.$lonhost.'_server">'.
  909:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  910:                           '</option>';
  911:             foreach my $hostid (sort(keys(%servers))) {
  912:                 next if ($servers{$hostid} eq $servers{$lonhost});
  913:                 my $selected = '';
  914:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  915:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  916:                         $selected = 'selected="selected"';
  917:                     }
  918:                 }
  919:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  920:                               $servers{$hostid}.'</option>';
  921:             }
  922:             $datatable .= '</select></td>'.
  923:                           '<td><select name="'.$lonhost.'_serverpath">';
  924:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  925:                 my $pathname = $path;
  926:                 if ($path eq 'custom') {
  927:                     $pathname = &mt('Custom Path').' ->';
  928:                 }
  929:                 my $selected = '';
  930:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  931:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  932:                         $selected = 'selected="selected"';
  933:                     }
  934:                 } elsif ($path eq '') {
  935:                     $selected = 'selected="selected"';
  936:                 }
  937:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  938:             }
  939:             $datatable .= '</select></td>';
  940:             my ($custom,$exempt);
  941:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  942:                 $custom = $disallowed{$lonhost}{'custompath'};
  943:                 $exempt = $disallowed{$lonhost}{'exempt'};
  944:             }
  945:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  946:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  947:                           '</tr>';
  948:         }
  949:         $datatable .= '</table></td></tr>';
  950:         return $datatable;
  951:     } elsif ($caller eq 'page') {
  952:         my %defaultchecked = ( 
  953:                                'coursecatalog' => 'on',
  954:                                'helpdesk'      => 'on',
  955:                                'adminmail'     => 'off',
  956:                                'newuser'       => 'off',
  957:                              );
  958:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  959:         my (%checkedon,%checkedoff);
  960:         foreach my $item (@toggles) {
  961:             if ($defaultchecked{$item} eq 'on') { 
  962:                 $checkedon{$item} = ' checked="checked" ';
  963:                 $checkedoff{$item} = ' ';
  964:             } elsif ($defaultchecked{$item} eq 'off') {
  965:                 $checkedoff{$item} = ' checked="checked" ';
  966:                 $checkedon{$item} = ' ';
  967:             }
  968:         }
  969:         my @images = ('img','logo','domlogo','login');
  970:         my @logintext = ('textcol','bgcol');
  971:         my @bgs = ('pgbg','mainbg','sidebg');
  972:         my @links = ('link','alink','vlink');
  973:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  974:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  975:         my (%is_custom,%designs);
  976:         my %defaults = (
  977:                        font => $defaultdesign{'login.font'},
  978:                        );
  979:         foreach my $item (@images) {
  980:             $defaults{$item} = $defaultdesign{'login.'.$item};
  981:             $defaults{'showlogo'}{$item} = 1;
  982:         }
  983:         foreach my $item (@bgs) {
  984:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  985:         }
  986:         foreach my $item (@logintext) {
  987:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  988:         }
  989:         foreach my $item (@links) {
  990:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  991:         }
  992:         if (ref($settings) eq 'HASH') {
  993:             foreach my $item (@toggles) {
  994:                 if ($settings->{$item} eq '1') {
  995:                     $checkedon{$item} =  ' checked="checked" ';
  996:                     $checkedoff{$item} = ' ';
  997:                 } elsif ($settings->{$item} eq '0') {
  998:                     $checkedoff{$item} =  ' checked="checked" ';
  999:                     $checkedon{$item} = ' ';
 1000:                 }
 1001:             }
 1002:             foreach my $item (@images) {
 1003:                 if (defined($settings->{$item})) {
 1004:                     $designs{$item} = $settings->{$item};
 1005:                     $is_custom{$item} = 1;
 1006:                 }
 1007:                 if (defined($settings->{'showlogo'}{$item})) {
 1008:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1009:                 }
 1010:             }
 1011:             foreach my $item (@logintext) {
 1012:                 if ($settings->{$item} ne '') {
 1013:                     $designs{'logintext'}{$item} = $settings->{$item};
 1014:                     $is_custom{$item} = 1;
 1015:                 }
 1016:             }
 1017:             if ($settings->{'font'} ne '') {
 1018:                 $designs{'font'} = $settings->{'font'};
 1019:                 $is_custom{'font'} = 1;
 1020:             }
 1021:             foreach my $item (@bgs) {
 1022:                 if ($settings->{$item} ne '') {
 1023:                     $designs{'bgs'}{$item} = $settings->{$item};
 1024:                     $is_custom{$item} = 1;
 1025:                 }
 1026:             }
 1027:             foreach my $item (@links) {
 1028:                 if ($settings->{$item} ne '') {
 1029:                     $designs{'links'}{$item} = $settings->{$item};
 1030:                     $is_custom{$item} = 1;
 1031:                 }
 1032:             }
 1033:         } else {
 1034:             if ($designhash{$dom.'.login.font'} ne '') {
 1035:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1036:                 $is_custom{'font'} = 1;
 1037:             }
 1038:             foreach my $item (@images) {
 1039:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1040:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1041:                     $is_custom{$item} = 1;
 1042:                 }
 1043:             }
 1044:             foreach my $item (@bgs) {
 1045:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1046:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1047:                     $is_custom{$item} = 1;
 1048:                 }
 1049:             }
 1050:             foreach my $item (@links) {
 1051:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1052:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1053:                     $is_custom{$item} = 1;
 1054:                 }
 1055:             }
 1056:         }
 1057:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1058:                                                       logo => 'Institution Logo',
 1059:                                                       domlogo => 'Domain Logo',
 1060:                                                       login => 'Login box');
 1061:         my $itemcount = 1;
 1062:         foreach my $item (@toggles) {
 1063:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1064:             $datatable .=  
 1065:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1066:                 '</td><td>'.
 1067:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1068:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1069:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1070:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1071:                 '</tr>';
 1072:             $itemcount ++;
 1073:         }
 1074:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1075:         $datatable .= '</tr></table></td></tr>';
 1076:     } elsif ($caller eq 'help') {
 1077:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1078:         my $switchserver = &check_switchserver($dom,$confname);
 1079:         my $itemcount = 1;
 1080:         $defaulturl = '/adm/loginproblems.html';
 1081:         $defaulttype = 'default';
 1082:         %lt = &Apache::lonlocal::texthash (
 1083:                      del     => 'Delete?',
 1084:                      rep     => 'Replace:',
 1085:                      upl     => 'Upload:',
 1086:                      default => 'Default',
 1087:                      custom  => 'Custom',
 1088:                                              );
 1089:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1090:         my @currlangs;
 1091:         if (ref($settings) eq 'HASH') {
 1092:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1093:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1094:                     next if ($settings->{'helpurl'}{$key} eq '');
 1095:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1096:                     $type{$key} = 'custom';
 1097:                     unless ($key eq 'nolang') {
 1098:                         push(@currlangs,$key);
 1099:                     }
 1100:                 }
 1101:             } elsif ($settings->{'helpurl'} ne '') {
 1102:                 $type{'nolang'} = 'custom';
 1103:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1104:             }
 1105:         }
 1106:         foreach my $lang ('nolang',sort(@currlangs)) {
 1107:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1108:             $datatable .= '<tr'.$css_class.'>';
 1109:             if ($url{$lang} eq '') {
 1110:                 $url{$lang} = $defaulturl;
 1111:             }
 1112:             if ($type{$lang} eq '') {
 1113:                 $type{$lang} = $defaulttype;
 1114:             }
 1115:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1116:             if ($lang eq 'nolang') {
 1117:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1118:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1119:             } else {
 1120:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1121:                                   $langchoices{$lang},
 1122:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1123:             }
 1124:             $datatable .= '</span></td>'."\n".
 1125:                           '<td class="LC_left_item">';
 1126:             if ($type{$lang} eq 'custom') {
 1127:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1128:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1129:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1130:             } else {
 1131:                 $datatable .= $lt{'upl'};
 1132:             }
 1133:             $datatable .='<br />';
 1134:             if ($switchserver) {
 1135:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1136:             } else {
 1137:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1138:             }
 1139:             $datatable .= '</td></tr>';
 1140:             $itemcount ++;
 1141:         }
 1142:         my @addlangs;
 1143:         foreach my $lang (sort(keys(%langchoices))) {
 1144:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1145:             push(@addlangs,$lang);
 1146:         }
 1147:         if (@addlangs > 0) {
 1148:             my %toadd;
 1149:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1150:             $toadd{''} = &mt('Select');
 1151:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1152:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1153:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1154:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1155:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1156:             if ($switchserver) {
 1157:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1158:             } else {
 1159:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1160:             }
 1161:             $datatable .= '</td></tr>';
 1162:             $itemcount ++;
 1163:         }
 1164:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1165:     }
 1166:     return $datatable;
 1167: }
 1168: 
 1169: sub login_choices {
 1170:     my %choices =
 1171:         &Apache::lonlocal::texthash (
 1172:             coursecatalog => 'Display Course/Community Catalog link?',
 1173:             adminmail     => "Display Administrator's E-mail Address?",
 1174:             helpdesk      => 'Display "Contact Helpdesk" link',
 1175:             disallowlogin => "Login page requests redirected",
 1176:             hostid        => "Server",
 1177:             server        => "Redirect to:",
 1178:             serverpath    => "Path",
 1179:             custompath    => "Custom", 
 1180:             exempt        => "Exempt IP(s)",
 1181:             directlogin   => "No redirect",
 1182:             newuser       => "Link to create a user account",
 1183:             img           => "Header",
 1184:             logo          => "Main Logo",
 1185:             domlogo       => "Domain Logo",
 1186:             login         => "Log-in Header", 
 1187:             textcol       => "Text color",
 1188:             bgcol         => "Box color",
 1189:             bgs           => "Background colors",
 1190:             links         => "Link colors",
 1191:             font          => "Font color",
 1192:             pgbg          => "Header",
 1193:             mainbg        => "Page",
 1194:             sidebg        => "Login box",
 1195:             link          => "Link",
 1196:             alink         => "Active link",
 1197:             vlink         => "Visited link",
 1198:         );
 1199:     return %choices;
 1200: }
 1201: 
 1202: sub print_rolecolors {
 1203:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1204:     my %choices = &color_font_choices();
 1205:     my @bgs = ('pgbg','tabbg','sidebg');
 1206:     my @links = ('link','alink','vlink');
 1207:     my @images = ('img');
 1208:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1209:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1210:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1211:     my (%is_custom,%designs);
 1212:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1213:     if (ref($settings) eq 'HASH') {
 1214:         if (ref($settings->{$role}) eq 'HASH') {
 1215:             if ($settings->{$role}->{'img'} ne '') {
 1216:                 $designs{'img'} = $settings->{$role}->{'img'};
 1217:                 $is_custom{'img'} = 1;
 1218:             }
 1219:             if ($settings->{$role}->{'font'} ne '') {
 1220:                 $designs{'font'} = $settings->{$role}->{'font'};
 1221:                 $is_custom{'font'} = 1;
 1222:             }
 1223:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1224:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1225:                 $is_custom{'fontmenu'} = 1;
 1226:             }
 1227:             foreach my $item (@bgs) {
 1228:                 if ($settings->{$role}->{$item} ne '') {
 1229:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1230:                     $is_custom{$item} = 1;
 1231:                 }
 1232:             }
 1233:             foreach my $item (@links) {
 1234:                 if ($settings->{$role}->{$item} ne '') {
 1235:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1236:                     $is_custom{$item} = 1;
 1237:                 }
 1238:             }
 1239:         }
 1240:     } else {
 1241:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1242:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1243:             $is_custom{'img'} = 1;
 1244:         }
 1245:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1246:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1247:             $is_custom{'fontmenu'} = 1; 
 1248:         }
 1249:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1250:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1251:             $is_custom{'font'} = 1;
 1252:         }
 1253:         foreach my $item (@bgs) {
 1254:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1255:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1256:                 $is_custom{$item} = 1;
 1257:             
 1258:             }
 1259:         }
 1260:         foreach my $item (@links) {
 1261:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1262:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1263:                 $is_custom{$item} = 1;
 1264:             }
 1265:         }
 1266:     }
 1267:     my $itemcount = 1;
 1268:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1269:     $datatable .= '</tr></table></td></tr>';
 1270:     return $datatable;
 1271: }
 1272: 
 1273: sub role_defaults {
 1274:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1275:     my %defaults;
 1276:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1277:         return %defaults;
 1278:     }
 1279:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1280:     if ($role eq 'login') {
 1281:         %defaults = (
 1282:                        font => $defaultdesign{$role.'.font'},
 1283:                     );
 1284:         if (ref($logintext) eq 'ARRAY') {
 1285:             foreach my $item (@{$logintext}) {
 1286:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1287:             }
 1288:         }
 1289:         foreach my $item (@{$images}) {
 1290:             $defaults{'showlogo'}{$item} = 1;
 1291:         }
 1292:     } else {
 1293:         %defaults = (
 1294:                        img => $defaultdesign{$role.'.img'},
 1295:                        font => $defaultdesign{$role.'.font'},
 1296:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1297:                     );
 1298:     }
 1299:     foreach my $item (@{$bgs}) {
 1300:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1301:     }
 1302:     foreach my $item (@{$links}) {
 1303:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1304:     }
 1305:     foreach my $item (@{$images}) {
 1306:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1307:     }
 1308:     return %defaults;
 1309: }
 1310: 
 1311: sub display_color_options {
 1312:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1313:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1314:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1315:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1316:     my $datatable = '<tr'.$css_class.'>'.
 1317:         '<td>'.$choices->{'font'}.'</td>';
 1318:     if (!$is_custom->{'font'}) {
 1319:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1320:     } else {
 1321:         $datatable .= '<td>&nbsp;</td>';
 1322:     }
 1323:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1324: 
 1325:     $datatable .= '<td><span class="LC_nobreak">'.
 1326:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1327:                   ' value="'.$current_color.'" />&nbsp;'.
 1328:                   '&nbsp;</td></tr>';
 1329:     unless ($role eq 'login') { 
 1330:         $datatable .= '<tr'.$css_class.'>'.
 1331:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1332:         if (!$is_custom->{'fontmenu'}) {
 1333:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1334:         } else {
 1335:             $datatable .= '<td>&nbsp;</td>';
 1336:         }
 1337: 	$current_color = $designs->{'fontmenu'} ?
 1338: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1339:         $datatable .= '<td><span class="LC_nobreak">'.
 1340:                       '<input class="colorchooser" type="text" size="10" name="'
 1341: 		      .$role.'_fontmenu"'.
 1342:                       ' value="'.$current_color.'" />&nbsp;'.
 1343:                       '&nbsp;</td></tr>';
 1344:     }
 1345:     my $switchserver = &check_switchserver($dom,$confname);
 1346:     foreach my $img (@{$images}) {
 1347: 	$itemcount ++;
 1348:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1349:         $datatable .= '<tr'.$css_class.'>'.
 1350:                       '<td>'.$choices->{$img};
 1351:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1352:         if ($role eq 'login') {
 1353:             if ($img eq 'login') {
 1354:                 $login_hdr_pick =
 1355:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1356:                 $logincolors =
 1357:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1358:                                        $designs,$defaults);
 1359:             } elsif ($img ne 'domlogo') {
 1360:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1361:             }
 1362:         }
 1363:         $datatable .= '</td>';
 1364:         if ($designs->{$img} ne '') {
 1365:             $imgfile = $designs->{$img};
 1366: 	    $img_import = ($imgfile =~ m{^/adm/});
 1367:         } else {
 1368:             $imgfile = $defaults->{$img};
 1369:         }
 1370:         if ($imgfile) {
 1371:             my ($showfile,$fullsize);
 1372:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1373:                 my $urldir = $1;
 1374:                 my $filename = $2;
 1375:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1376:                 if (@info) {
 1377:                     my $thumbfile = 'tn-'.$filename;
 1378:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1379:                     if (@thumb) {
 1380:                         $showfile = $urldir.'/'.$thumbfile;
 1381:                     } else {
 1382:                         $showfile = $imgfile;
 1383:                     }
 1384:                 } else {
 1385:                     $showfile = '';
 1386:                 }
 1387:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1388:                 $showfile = $imgfile;
 1389:                 my $imgdir = $1;
 1390:                 my $filename = $2;
 1391:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1392:                     $showfile = "/$imgdir/tn-".$filename;
 1393:                 } else {
 1394:                     my $input = $londocroot.$imgfile;
 1395:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1396:                     if (!-e $output) {
 1397:                         my ($width,$height) = &thumb_dimensions();
 1398:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1399:                         if ($fullwidth ne '' && $fullheight ne '') {
 1400:                             if ($fullwidth > $width && $fullheight > $height) { 
 1401:                                 my $size = $width.'x'.$height;
 1402:                                 system("convert -sample $size $input $output");
 1403:                                 $showfile = "/$imgdir/tn-".$filename;
 1404:                             }
 1405:                         }
 1406:                     }
 1407:                 }
 1408:             }
 1409:             if ($showfile) {
 1410:                 if ($showfile =~ m{^/(adm|res)/}) {
 1411:                     if ($showfile =~ m{^/res/}) {
 1412:                         my $local_showfile =
 1413:                             &Apache::lonnet::filelocation('',$showfile);
 1414:                         &Apache::lonnet::repcopy($local_showfile);
 1415:                     }
 1416:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1417:                 }
 1418:                 if ($imgfile) {
 1419:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1420:                         if ($imgfile =~ m{^/res/}) {
 1421:                             my $local_imgfile =
 1422:                                 &Apache::lonnet::filelocation('',$imgfile);
 1423:                             &Apache::lonnet::repcopy($local_imgfile);
 1424:                         }
 1425:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1426:                     } else {
 1427:                         $fullsize = $imgfile;
 1428:                     }
 1429:                 }
 1430:                 $datatable .= '<td>';
 1431:                 if ($img eq 'login') {
 1432:                     $datatable .= $login_hdr_pick;
 1433:                 } 
 1434:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1435:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1436:             } else {
 1437:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1438:                               &mt('Upload:').'<br />';
 1439:             }
 1440:         } else {
 1441:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1442:                           &mt('Upload:').'<br />';
 1443:         }
 1444:         if ($switchserver) {
 1445:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1446:         } else {
 1447:             if ($img ne 'login') { # suppress file selection for Log-in header
 1448:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1449:             }
 1450:         }
 1451:         $datatable .= '</td></tr>';
 1452:     }
 1453:     $itemcount ++;
 1454:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1455:     $datatable .= '<tr'.$css_class.'>'.
 1456:                   '<td>'.$choices->{'bgs'}.'</td>';
 1457:     my $bgs_def;
 1458:     foreach my $item (@{$bgs}) {
 1459:         if (!$is_custom->{$item}) {
 1460:             $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>';
 1461:         }
 1462:     }
 1463:     if ($bgs_def) {
 1464:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1465:     } else {
 1466:         $datatable .= '<td>&nbsp;</td>';
 1467:     }
 1468:     $datatable .= '<td class="LC_right_item">'.
 1469:                   '<table border="0"><tr>';
 1470: 
 1471:     foreach my $item (@{$bgs}) {
 1472:         $datatable .= '<td align="center">'.$choices->{$item};
 1473: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1474:         if ($designs->{'bgs'}{$item}) {
 1475:             $datatable .= '&nbsp;';
 1476:         }
 1477:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1478:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1479:     }
 1480:     $datatable .= '</tr></table></td></tr>';
 1481:     $itemcount ++;
 1482:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1483:     $datatable .= '<tr'.$css_class.'>'.
 1484:                   '<td>'.$choices->{'links'}.'</td>';
 1485:     my $links_def;
 1486:     foreach my $item (@{$links}) {
 1487:         if (!$is_custom->{$item}) {
 1488:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1489:         }
 1490:     }
 1491:     if ($links_def) {
 1492:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1493:     } else {
 1494:         $datatable .= '<td>&nbsp;</td>';
 1495:     }
 1496:     $datatable .= '<td class="LC_right_item">'.
 1497:                   '<table border="0"><tr>';
 1498:     foreach my $item (@{$links}) {
 1499: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1500:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1501:         if ($designs->{'links'}{$item}) {
 1502:             $datatable.='&nbsp;';
 1503:         }
 1504:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1505:                       '" /></td>';
 1506:     }
 1507:     $$rowtotal += $itemcount;
 1508:     return $datatable;
 1509: }
 1510: 
 1511: sub logo_display_options {
 1512:     my ($img,$defaults,$designs) = @_;
 1513:     my $checkedon;
 1514:     if (ref($defaults) eq 'HASH') {
 1515:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1516:             if ($defaults->{'showlogo'}{$img}) {
 1517:                 $checkedon = 'checked="checked" ';     
 1518:             }
 1519:         } 
 1520:     }
 1521:     if (ref($designs) eq 'HASH') {
 1522:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1523:             if (defined($designs->{'showlogo'}{$img})) {
 1524:                 if ($designs->{'showlogo'}{$img} == 0) {
 1525:                     $checkedon = '';
 1526:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1527:                     $checkedon = 'checked="checked" ';
 1528:                 }
 1529:             }
 1530:         }
 1531:     }
 1532:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1533:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1534:            &mt('show').'</label>'."\n";
 1535: }
 1536: 
 1537: sub login_header_options  {
 1538:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1539:     my $output = '';
 1540:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1541:         $output .= &mt('Text default(s):').'<br />';
 1542:         if (!$is_custom->{'textcol'}) {
 1543:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1544:                        '&nbsp;&nbsp;&nbsp;';
 1545:         }
 1546:         if (!$is_custom->{'bgcol'}) {
 1547:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1548:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1549:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1550:         }
 1551:         $output .= '<br />';
 1552:     }
 1553:     $output .='<br />';
 1554:     return $output;
 1555: }
 1556: 
 1557: sub login_text_colors {
 1558:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1559:     my $color_menu = '<table border="0"><tr>';
 1560:     foreach my $item (@{$logintext}) {
 1561:         $color_menu .= '<td align="center">'.$choices->{$item};
 1562:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1563:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1564:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1565:     }
 1566:     $color_menu .= '</tr></table><br />';
 1567:     return $color_menu;
 1568: }
 1569: 
 1570: sub image_changes {
 1571:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1572:     my $output;
 1573:     if ($img eq 'login') {
 1574:             # suppress image for Log-in header
 1575:     } elsif (!$is_custom) {
 1576:         if ($img ne 'domlogo') {
 1577:             $output .= &mt('Default image:').'<br />';
 1578:         } else {
 1579:             $output .= &mt('Default in use:').'<br />';
 1580:         }
 1581:     }
 1582:     if ($img eq 'login') { # suppress image for Log-in header
 1583:         $output .= '<td>'.$logincolors;
 1584:     } else {
 1585:         if ($img_import) {
 1586:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1587:         }
 1588:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1589:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1590:         if ($is_custom) {
 1591:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1592:                        '<input type="checkbox" name="'.
 1593:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1594:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1595:         } else {
 1596:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1597:         }
 1598:     }
 1599:     return $output;
 1600: }
 1601: 
 1602: sub print_quotas {
 1603:     my ($dom,$settings,$rowtotal,$action) = @_;
 1604:     my $context;
 1605:     if ($action eq 'quotas') {
 1606:         $context = 'tools';
 1607:     } else {
 1608:         $context = $action;
 1609:     }
 1610:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1611:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1612:     my $typecount = 0;
 1613:     my ($css_class,%titles);
 1614:     if ($context eq 'requestcourses') {
 1615:         @usertools = ('official','unofficial','community','textbook');
 1616:         @options =('norequest','approval','validate','autolimit');
 1617:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1618:         %titles = &courserequest_titles();
 1619:     } elsif ($context eq 'requestauthor') {
 1620:         @usertools = ('author');
 1621:         @options = ('norequest','approval','automatic');
 1622:         %titles = &authorrequest_titles();
 1623:     } else {
 1624:         @usertools = ('aboutme','blog','webdav','portfolio');
 1625:         %titles = &tool_titles();
 1626:     }
 1627:     if (ref($types) eq 'ARRAY') {
 1628:         foreach my $type (@{$types}) {
 1629:             my ($currdefquota,$currauthorquota);
 1630:             unless (($context eq 'requestcourses') ||
 1631:                     ($context eq 'requestauthor')) {
 1632:                 if (ref($settings) eq 'HASH') {
 1633:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1634:                         $currdefquota = $settings->{defaultquota}->{$type};
 1635:                     } else {
 1636:                         $currdefquota = $settings->{$type};
 1637:                     }
 1638:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1639:                         $currauthorquota = $settings->{authorquota}->{$type};
 1640:                     }
 1641:                 }
 1642:             }
 1643:             if (defined($usertypes->{$type})) {
 1644:                 $typecount ++;
 1645:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1646:                 $datatable .= '<tr'.$css_class.'>'.
 1647:                               '<td>'.$usertypes->{$type}.'</td>'.
 1648:                               '<td class="LC_left_item">';
 1649:                 if ($context eq 'requestcourses') {
 1650:                     $datatable .= '<table><tr>';
 1651:                 }
 1652:                 my %cell;  
 1653:                 foreach my $item (@usertools) {
 1654:                     if ($context eq 'requestcourses') {
 1655:                         my ($curroption,$currlimit);
 1656:                         if (ref($settings) eq 'HASH') {
 1657:                             if (ref($settings->{$item}) eq 'HASH') {
 1658:                                 $curroption = $settings->{$item}->{$type};
 1659:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1660:                                     $currlimit = $1; 
 1661:                                 }
 1662:                             }
 1663:                         }
 1664:                         if (!$curroption) {
 1665:                             $curroption = 'norequest';
 1666:                         }
 1667:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1668:                         foreach my $option (@options) {
 1669:                             my $val = $option;
 1670:                             if ($option eq 'norequest') {
 1671:                                 $val = 0;  
 1672:                             }
 1673:                             if ($option eq 'validate') {
 1674:                                 my $canvalidate = 0;
 1675:                                 if (ref($validations{$item}) eq 'HASH') { 
 1676:                                     if ($validations{$item}{$type}) {
 1677:                                         $canvalidate = 1;
 1678:                                     }
 1679:                                 }
 1680:                                 next if (!$canvalidate);
 1681:                             }
 1682:                             my $checked = '';
 1683:                             if ($option eq $curroption) {
 1684:                                 $checked = ' checked="checked"';
 1685:                             } elsif ($option eq 'autolimit') {
 1686:                                 if ($curroption =~ /^autolimit/) {
 1687:                                     $checked = ' checked="checked"';
 1688:                                 }                       
 1689:                             } 
 1690:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1691:                                   '<input type="radio" name="crsreq_'.$item.
 1692:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1693:                                   $titles{$option}.'</label>';
 1694:                             if ($option eq 'autolimit') {
 1695:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1696:                                                 $item.'_limit_'.$type.'" size="1" '.
 1697:                                                 'value="'.$currlimit.'" />';
 1698:                             }
 1699:                             $cell{$item} .= '</span> ';
 1700:                             if ($option eq 'autolimit') {
 1701:                                 $cell{$item} .= $titles{'unlimited'};
 1702:                             }
 1703:                         }
 1704:                     } elsif ($context eq 'requestauthor') {
 1705:                         my $curroption;
 1706:                         if (ref($settings) eq 'HASH') {
 1707:                             $curroption = $settings->{$type};
 1708:                         }
 1709:                         if (!$curroption) {
 1710:                             $curroption = 'norequest';
 1711:                         }
 1712:                         foreach my $option (@options) {
 1713:                             my $val = $option;
 1714:                             if ($option eq 'norequest') {
 1715:                                 $val = 0;
 1716:                             }
 1717:                             my $checked = '';
 1718:                             if ($option eq $curroption) {
 1719:                                 $checked = ' checked="checked"';
 1720:                             }
 1721:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1722:                                   '<input type="radio" name="authorreq_'.$type.
 1723:                                   '" value="'.$val.'"'.$checked.' />'.
 1724:                                   $titles{$option}.'</label></span>&nbsp; ';
 1725:                         }
 1726:                     } else {
 1727:                         my $checked = 'checked="checked" ';
 1728:                         if (ref($settings) eq 'HASH') {
 1729:                             if (ref($settings->{$item}) eq 'HASH') {
 1730:                                 if ($settings->{$item}->{$type} == 0) {
 1731:                                     $checked = '';
 1732:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1733:                                     $checked =  'checked="checked" ';
 1734:                                 }
 1735:                             }
 1736:                         }
 1737:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1738:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1739:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1740:                                       '</label></span>&nbsp; ';
 1741:                     }
 1742:                 }
 1743:                 if ($context eq 'requestcourses') {
 1744:                     $datatable .= '</tr><tr>';
 1745:                     foreach my $item (@usertools) {
 1746:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1747:                     }
 1748:                     $datatable .= '</tr></table>';
 1749:                 }
 1750:                 $datatable .= '</td>';
 1751:                 unless (($context eq 'requestcourses') ||
 1752:                         ($context eq 'requestauthor')) {
 1753:                     $datatable .= 
 1754:                               '<td class="LC_right_item">'.
 1755:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1756:                               '<input type="text" name="quota_'.$type.
 1757:                               '" value="'.$currdefquota.
 1758:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1759:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1760:                               '<input type="text" name="authorquota_'.$type.
 1761:                               '" value="'.$currauthorquota.
 1762:                               '" size="5" /></span></td>';
 1763:                 }
 1764:                 $datatable .= '</tr>';
 1765:             }
 1766:         }
 1767:     }
 1768:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1769:         $defaultquota = '20';
 1770:         $authorquota = '500';
 1771:         if (ref($settings) eq 'HASH') {
 1772:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1773:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1774:             } elsif (defined($settings->{'default'})) {
 1775:                 $defaultquota = $settings->{'default'};
 1776:             }
 1777:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1778:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1779:             }
 1780:         }
 1781:     }
 1782:     $typecount ++;
 1783:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1784:     $datatable .= '<tr'.$css_class.'>'.
 1785:                   '<td>'.$othertitle.'</td>'.
 1786:                   '<td class="LC_left_item">';
 1787:     if ($context eq 'requestcourses') {
 1788:         $datatable .= '<table><tr>';
 1789:     }
 1790:     my %defcell;
 1791:     foreach my $item (@usertools) {
 1792:         if ($context eq 'requestcourses') {
 1793:             my ($curroption,$currlimit);
 1794:             if (ref($settings) eq 'HASH') {
 1795:                 if (ref($settings->{$item}) eq 'HASH') {
 1796:                     $curroption = $settings->{$item}->{'default'};
 1797:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1798:                         $currlimit = $1;
 1799:                     }
 1800:                 }
 1801:             }
 1802:             if (!$curroption) {
 1803:                 $curroption = 'norequest';
 1804:             }
 1805:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1806:             foreach my $option (@options) {
 1807:                 my $val = $option;
 1808:                 if ($option eq 'norequest') {
 1809:                     $val = 0;
 1810:                 }
 1811:                 if ($option eq 'validate') {
 1812:                     my $canvalidate = 0;
 1813:                     if (ref($validations{$item}) eq 'HASH') {
 1814:                         if ($validations{$item}{'default'}) {
 1815:                             $canvalidate = 1;
 1816:                         }
 1817:                     }
 1818:                     next if (!$canvalidate);
 1819:                 }
 1820:                 my $checked = '';
 1821:                 if ($option eq $curroption) {
 1822:                     $checked = ' checked="checked"';
 1823:                 } elsif ($option eq 'autolimit') {
 1824:                     if ($curroption =~ /^autolimit/) {
 1825:                         $checked = ' checked="checked"';
 1826:                     }
 1827:                 }
 1828:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1829:                                   '<input type="radio" name="crsreq_'.$item.
 1830:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1831:                                   $titles{$option}.'</label>';
 1832:                 if ($option eq 'autolimit') {
 1833:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1834:                                        $item.'_limit_default" size="1" '.
 1835:                                        'value="'.$currlimit.'" />';
 1836:                 }
 1837:                 $defcell{$item} .= '</span> ';
 1838:                 if ($option eq 'autolimit') {
 1839:                     $defcell{$item} .= $titles{'unlimited'};
 1840:                 }
 1841:             }
 1842:         } elsif ($context eq 'requestauthor') {
 1843:             my $curroption;
 1844:             if (ref($settings) eq 'HASH') {
 1845:                 $curroption = $settings->{'default'};
 1846:             }
 1847:             if (!$curroption) {
 1848:                 $curroption = 'norequest';
 1849:             }
 1850:             foreach my $option (@options) {
 1851:                 my $val = $option;
 1852:                 if ($option eq 'norequest') {
 1853:                     $val = 0;
 1854:                 }
 1855:                 my $checked = '';
 1856:                 if ($option eq $curroption) {
 1857:                     $checked = ' checked="checked"';
 1858:                 }
 1859:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1860:                               '<input type="radio" name="authorreq_default"'.
 1861:                               ' value="'.$val.'"'.$checked.' />'.
 1862:                               $titles{$option}.'</label></span>&nbsp; ';
 1863:             }
 1864:         } else {
 1865:             my $checked = 'checked="checked" ';
 1866:             if (ref($settings) eq 'HASH') {
 1867:                 if (ref($settings->{$item}) eq 'HASH') {
 1868:                     if ($settings->{$item}->{'default'} == 0) {
 1869:                         $checked = '';
 1870:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1871:                         $checked = 'checked="checked" ';
 1872:                     }
 1873:                 }
 1874:             }
 1875:             $datatable .= '<span class="LC_nobreak"><label>'.
 1876:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1877:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1878:                           '</label></span>&nbsp; ';
 1879:         }
 1880:     }
 1881:     if ($context eq 'requestcourses') {
 1882:         $datatable .= '</tr><tr>';
 1883:         foreach my $item (@usertools) {
 1884:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1885:         }
 1886:         $datatable .= '</tr></table>';
 1887:     }
 1888:     $datatable .= '</td>';
 1889:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1890:         $datatable .= '<td class="LC_right_item">'.
 1891:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1892:                       '<input type="text" name="defaultquota" value="'.
 1893:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1894:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1895:                       '<input type="text" name="authorquota" value="'.
 1896:                       $authorquota.'" size="5" /></span></td>';
 1897:     }
 1898:     $datatable .= '</tr>';
 1899:     $typecount ++;
 1900:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1901:     $datatable .= '<tr'.$css_class.'>'.
 1902:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1903:     if ($context eq 'requestcourses') {
 1904:         $datatable .= &mt('(overrides affiliation, if set)').
 1905:                       '</td>'.
 1906:                       '<td class="LC_left_item">'.
 1907:                       '<table><tr>';
 1908:     } else {
 1909:         $datatable .= &mt('(overrides affiliation, if checked)').
 1910:                       '</td>'.
 1911:                       '<td class="LC_left_item" colspan="2">'.
 1912:                       '<br />';
 1913:     }
 1914:     my %advcell;
 1915:     foreach my $item (@usertools) {
 1916:         if ($context eq 'requestcourses') {
 1917:             my ($curroption,$currlimit);
 1918:             if (ref($settings) eq 'HASH') {
 1919:                 if (ref($settings->{$item}) eq 'HASH') {
 1920:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1921:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1922:                         $currlimit = $1;
 1923:                     }
 1924:                 }
 1925:             }
 1926:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1927:             my $checked = '';
 1928:             if ($curroption eq '') {
 1929:                 $checked = ' checked="checked"';
 1930:             }
 1931:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1932:                                '<input type="radio" name="crsreq_'.$item.
 1933:                                '__LC_adv" value=""'.$checked.' />'.
 1934:                                &mt('No override set').'</label></span>&nbsp; ';
 1935:             foreach my $option (@options) {
 1936:                 my $val = $option;
 1937:                 if ($option eq 'norequest') {
 1938:                     $val = 0;
 1939:                 }
 1940:                 if ($option eq 'validate') {
 1941:                     my $canvalidate = 0;
 1942:                     if (ref($validations{$item}) eq 'HASH') {
 1943:                         if ($validations{$item}{'_LC_adv'}) {
 1944:                             $canvalidate = 1;
 1945:                         }
 1946:                     }
 1947:                     next if (!$canvalidate);
 1948:                 }
 1949:                 my $checked = '';
 1950:                 if ($val eq $curroption) {
 1951:                     $checked = ' checked="checked"';
 1952:                 } elsif ($option eq 'autolimit') {
 1953:                     if ($curroption =~ /^autolimit/) {
 1954:                         $checked = ' checked="checked"';
 1955:                     }
 1956:                 }
 1957:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1958:                                   '<input type="radio" name="crsreq_'.$item.
 1959:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1960:                                   $titles{$option}.'</label>';
 1961:                 if ($option eq 'autolimit') {
 1962:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1963:                                        $item.'_limit__LC_adv" size="1" '.
 1964:                                        'value="'.$currlimit.'" />';
 1965:                 }
 1966:                 $advcell{$item} .= '</span> ';
 1967:                 if ($option eq 'autolimit') {
 1968:                     $advcell{$item} .= $titles{'unlimited'};
 1969:                 }
 1970:             }
 1971:         } elsif ($context eq 'requestauthor') {
 1972:             my $curroption;
 1973:             if (ref($settings) eq 'HASH') {
 1974:                 $curroption = $settings->{'_LC_adv'};
 1975:             }
 1976:             my $checked = '';
 1977:             if ($curroption eq '') {
 1978:                 $checked = ' checked="checked"';
 1979:             }
 1980:             $datatable .= '<span class="LC_nobreak"><label>'.
 1981:                           '<input type="radio" name="authorreq__LC_adv"'.
 1982:                           ' value=""'.$checked.' />'.
 1983:                           &mt('No override set').'</label></span>&nbsp; ';
 1984:             foreach my $option (@options) {
 1985:                 my $val = $option;
 1986:                 if ($option eq 'norequest') {
 1987:                     $val = 0;
 1988:                 }
 1989:                 my $checked = '';
 1990:                 if ($val eq $curroption) {
 1991:                     $checked = ' checked="checked"';
 1992:                 }
 1993:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1994:                               '<input type="radio" name="authorreq__LC_adv"'.
 1995:                               ' value="'.$val.'"'.$checked.' />'.
 1996:                               $titles{$option}.'</label></span>&nbsp; ';
 1997:             }
 1998:         } else {
 1999:             my $checked = 'checked="checked" ';
 2000:             if (ref($settings) eq 'HASH') {
 2001:                 if (ref($settings->{$item}) eq 'HASH') {
 2002:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2003:                         $checked = '';
 2004:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2005:                         $checked = 'checked="checked" ';
 2006:                     }
 2007:                 }
 2008:             }
 2009:             $datatable .= '<span class="LC_nobreak"><label>'.
 2010:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2011:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2012:                           '</label></span>&nbsp; ';
 2013:         }
 2014:     }
 2015:     if ($context eq 'requestcourses') {
 2016:         $datatable .= '</tr><tr>';
 2017:         foreach my $item (@usertools) {
 2018:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2019:         }
 2020:         $datatable .= '</tr></table>';
 2021:     }
 2022:     $datatable .= '</td></tr>';
 2023:     $$rowtotal += $typecount;
 2024:     return $datatable;
 2025: }
 2026: 
 2027: sub print_requestmail {
 2028:     my ($dom,$action,$settings,$rowtotal) = @_;
 2029:     my ($now,$datatable,%currapp);
 2030:     $now = time;
 2031:     if (ref($settings) eq 'HASH') {
 2032:         if (ref($settings->{'notify'}) eq 'HASH') {
 2033:             if ($settings->{'notify'}{'approval'} ne '') {
 2034:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2035:             }
 2036:         }
 2037:     }
 2038:     my $numinrow = 2;
 2039:     my $css_class;
 2040:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2041:     my $text;
 2042:     if ($action eq 'requestcourses') {
 2043:         $text = &mt('Receive notification of course requests requiring approval');
 2044:     } elsif ($action eq 'requestauthor') {
 2045:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2046:     } else {
 2047:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2048:     }
 2049:     $datatable = '<tr'.$css_class.'>'.
 2050:                  ' <td>'.$text.'</td>'.
 2051:                  ' <td class="LC_left_item">';
 2052:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2053:                                                  $action.'notifyapproval',%currapp);
 2054:     if ($numdc > 0) {
 2055:         $datatable .= $table;
 2056:     } else {
 2057:         $datatable .= &mt('There are no active Domain Coordinators');
 2058:     }
 2059:     $datatable .='</td></tr>';
 2060:     return $datatable;
 2061: }
 2062: 
 2063: sub print_studentcode {
 2064:     my ($settings,$rowtotal) = @_;
 2065:     my $rownum = 0; 
 2066:     my ($output,%current);
 2067:     my @crstypes = ('official','unofficial','community','textbook');
 2068:     if (ref($settings) eq 'HASH') {
 2069:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2070:             foreach my $type (@crstypes) {
 2071:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2072:             }
 2073:         }
 2074:     }
 2075:     $output .= '<tr>'.
 2076:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2077:                '<td class="LC_left_item">';
 2078:     foreach my $type (@crstypes) {
 2079:         my $check = ' ';
 2080:         if ($current{$type}) {
 2081:             $check = ' checked="checked" ';
 2082:         }
 2083:         $output .= '<span class="LC_nobreak"><label>'.
 2084:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2085:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2086:     }
 2087:     $output .= '</td></tr>';
 2088:     $$rowtotal ++;
 2089:     return $output;
 2090: }
 2091: 
 2092: sub print_textbookcourses {
 2093:     my ($dom,$type,$settings,$rowtotal) = @_;
 2094:     my $rownum = 0;
 2095:     my $css_class;
 2096:     my $itemcount = 1;
 2097:     my $maxnum = 0;
 2098:     my $bookshash;
 2099:     if (ref($settings) eq 'HASH') {
 2100:         $bookshash = $settings->{$type};
 2101:     }
 2102:     my %ordered;
 2103:     if (ref($bookshash) eq 'HASH') {
 2104:         foreach my $item (keys(%{$bookshash})) {
 2105:             if (ref($bookshash->{$item}) eq 'HASH') {
 2106:                 my $num = $bookshash->{$item}{'order'};
 2107:                 $ordered{$num} = $item;
 2108:             }
 2109:         }
 2110:     }
 2111:     my $confname = $dom.'-domainconfig';
 2112:     my $switchserver = &check_switchserver($dom,$confname);
 2113:     my $maxnum = scalar(keys(%ordered));
 2114:     my $datatable;
 2115:     if (keys(%ordered)) {
 2116:         my @items = sort { $a <=> $b } keys(%ordered);
 2117:         for (my $i=0; $i<@items; $i++) {
 2118:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2119:             my $key = $ordered{$items[$i]};
 2120:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2121:             my $coursetitle = $coursehash{'description'};
 2122:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2123:             if (ref($bookshash->{$key}) eq 'HASH') {
 2124:                 $subject = $bookshash->{$key}->{'subject'};
 2125:                 $title = $bookshash->{$key}->{'title'};
 2126:                 if ($type eq 'textbooks') {
 2127:                     $publisher = $bookshash->{$key}->{'publisher'};
 2128:                     $author = $bookshash->{$key}->{'author'};
 2129:                     $image = $bookshash->{$key}->{'image'};
 2130:                     if ($image ne '') {
 2131:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2132:                         my $imagethumb = "$path/tn-".$imagefile;
 2133:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2134:                     }
 2135:                 }
 2136:             }
 2137:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2138:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2139:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2140:             for (my $k=0; $k<=$maxnum; $k++) {
 2141:                 my $vpos = $k+1;
 2142:                 my $selstr;
 2143:                 if ($k == $i) {
 2144:                     $selstr = ' selected="selected" ';
 2145:                 }
 2146:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2147:             }
 2148:             $datatable .= '</select>'.('&nbsp;'x2).
 2149:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2150:                 &mt('Delete?').'</label></span></td>'.
 2151:                 '<td colspan="2">'.
 2152:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2153:                 ('&nbsp;'x2).
 2154:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2155:             if ($type eq 'textbooks') {
 2156:                 $datatable .= ('&nbsp;'x2).
 2157:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2158:                               ('&nbsp;'x2).
 2159:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2160:                               ('&nbsp;'x2).
 2161:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2162:                 if ($image) {
 2163:                     $datatable .= '<span class="LC_nobreak">'.
 2164:                                   $imgsrc.
 2165:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2166:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2167:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2168:                 }
 2169:                 if ($switchserver) {
 2170:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2171:                 } else {
 2172:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2173:                 }
 2174:             }
 2175:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2176:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2177:                           $coursetitle.'</span></td></tr>'."\n";
 2178:             $itemcount ++;
 2179:         }
 2180:     }
 2181:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2182:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2183:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2184:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2185:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2186:     for (my $k=0; $k<$maxnum+1; $k++) {
 2187:         my $vpos = $k+1;
 2188:         my $selstr;
 2189:         if ($k == $maxnum) {
 2190:             $selstr = ' selected="selected" ';
 2191:         }
 2192:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2193:     }
 2194:     $datatable .= '</select>&nbsp;'."\n".
 2195:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2196:                   '<td colspan="2">'.
 2197:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2198:                   ('&nbsp;'x2).
 2199:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2200:                   ('&nbsp;'x2);
 2201:     if ($type eq 'textbooks') {
 2202:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2203:                       ('&nbsp;'x2).
 2204:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2205:                       ('&nbsp;'x2).
 2206:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2207:         if ($switchserver) {
 2208:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2209:         } else {
 2210:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2211:         }
 2212:     }
 2213:     $datatable .= '</span>'."\n".
 2214:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2215:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2216:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2217:                   &Apache::loncommon::selectcourse_link
 2218:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2219:                   '</span></td>'."\n".
 2220:                   '</tr>'."\n";
 2221:     $itemcount ++;
 2222:     return $datatable;
 2223: }
 2224: 
 2225: sub textbookcourses_javascript {
 2226:     my ($settings) = @_;
 2227:     return unless(ref($settings) eq 'HASH');
 2228:     my (%ordered,%total,%jstext);
 2229:     foreach my $type ('textbooks','templates') {
 2230:         $total{$type} = 0;
 2231:         if (ref($settings->{$type}) eq 'HASH') {
 2232:             foreach my $item (keys(%{$settings->{$type}})) {
 2233:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2234:                     my $num = $settings->{$type}->{$item}{'order'};
 2235:                     $ordered{$type}{$num} = $item;
 2236:                 }
 2237:             }
 2238:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2239:         }
 2240:         my @jsarray = ();
 2241:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2242:             push(@jsarray,$ordered{$type}{$item});
 2243:         }
 2244:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2245:     }
 2246:     return <<"ENDSCRIPT";
 2247: <script type="text/javascript">
 2248: // <![CDATA[
 2249: function reorderBooks(form,item,caller) {
 2250:     var changedVal;
 2251: $jstext{'textbooks'};
 2252: $jstext{'templates'};
 2253:     var newpos;
 2254:     var maxh;
 2255:     if (caller == 'textbooks') {  
 2256:         newpos = 'textbooks_addbook_pos';
 2257:         maxh = 1 + $total{'textbooks'};
 2258:     } else {
 2259:         newpos = 'templates_addbook_pos';
 2260:         maxh = 1 + $total{'templates'};
 2261:     }
 2262:     var current = new Array;
 2263:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2264:     if (item == newpos) {
 2265:         changedVal = newitemVal;
 2266:     } else {
 2267:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2268:         current[newitemVal] = newpos;
 2269:     }
 2270:     if (caller == 'textbooks') {
 2271:         for (var i=0; i<textbooks.length; i++) {
 2272:             var elementName = 'textbooks_'+textbooks[i];
 2273:             if (elementName != item) {
 2274:                 if (form.elements[elementName]) {
 2275:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2276:                     current[currVal] = elementName;
 2277:                 }
 2278:             }
 2279:         }
 2280:     }
 2281:     if (caller == 'templates') {
 2282:         for (var i=0; i<templates.length; i++) {
 2283:             var elementName = 'templates_'+templates[i];
 2284:             if (elementName != item) {
 2285:                 if (form.elements[elementName]) {
 2286:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2287:                     current[currVal] = elementName;
 2288:                 }
 2289:             }
 2290:         }
 2291:     }
 2292:     var oldVal;
 2293:     for (var j=0; j<maxh; j++) {
 2294:         if (current[j] == undefined) {
 2295:             oldVal = j;
 2296:         }
 2297:     }
 2298:     if (oldVal < changedVal) {
 2299:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2300:            var elementName = current[k];
 2301:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2302:         }
 2303:     } else {
 2304:         for (var k=changedVal; k<oldVal; k++) {
 2305:             var elementName = current[k];
 2306:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2307:         }
 2308:     }
 2309:     return;
 2310: }
 2311: 
 2312: // ]]>
 2313: </script>
 2314: 
 2315: ENDSCRIPT
 2316: }
 2317: 
 2318: sub print_autoenroll {
 2319:     my ($dom,$settings,$rowtotal) = @_;
 2320:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2321:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2322:     if (ref($settings) eq 'HASH') {
 2323:         if (exists($settings->{'run'})) {
 2324:             if ($settings->{'run'} eq '0') {
 2325:                 $runoff = ' checked="checked" ';
 2326:                 $runon = ' ';
 2327:             } else {
 2328:                 $runon = ' checked="checked" ';
 2329:                 $runoff = ' ';
 2330:             }
 2331:         } else {
 2332:             if ($autorun) {
 2333:                 $runon = ' checked="checked" ';
 2334:                 $runoff = ' ';
 2335:             } else {
 2336:                 $runoff = ' checked="checked" ';
 2337:                 $runon = ' ';
 2338:             }
 2339:         }
 2340:         if (exists($settings->{'co-owners'})) {
 2341:             if ($settings->{'co-owners'} eq '0') {
 2342:                 $coownersoff = ' checked="checked" ';
 2343:                 $coownerson = ' ';
 2344:             } else {
 2345:                 $coownerson = ' checked="checked" ';
 2346:                 $coownersoff = ' ';
 2347:             }
 2348:         } else {
 2349:             $coownersoff = ' checked="checked" ';
 2350:             $coownerson = ' ';
 2351:         }
 2352:         if (exists($settings->{'sender_domain'})) {
 2353:             $defdom = $settings->{'sender_domain'};
 2354:         }
 2355:     } else {
 2356:         if ($autorun) {
 2357:             $runon = ' checked="checked" ';
 2358:             $runoff = ' ';
 2359:         } else {
 2360:             $runoff = ' checked="checked" ';
 2361:             $runon = ' ';
 2362:         }
 2363:     }
 2364:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2365:     my $notif_sender;
 2366:     if (ref($settings) eq 'HASH') {
 2367:         $notif_sender = $settings->{'sender_uname'};
 2368:     }
 2369:     my $datatable='<tr class="LC_odd_row">'.
 2370:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2371:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2372:                   '<input type="radio" name="autoenroll_run"'.
 2373:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2374:                   '<label><input type="radio" name="autoenroll_run"'.
 2375:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2376:                   '</tr><tr>'.
 2377:                   '<td>'.&mt('Notification messages - sender').
 2378:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2379:                   &mt('username').':&nbsp;'.
 2380:                   '<input type="text" name="sender_uname" value="'.
 2381:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2382:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2383:                   '<tr class="LC_odd_row">'.
 2384:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2385:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2386:                   '<input type="radio" name="autoassign_coowners"'.
 2387:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2388:                   '<label><input type="radio" name="autoassign_coowners"'.
 2389:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2390:                   '</tr>';
 2391:     $$rowtotal += 3;
 2392:     return $datatable;
 2393: }
 2394: 
 2395: sub print_autoupdate {
 2396:     my ($position,$dom,$settings,$rowtotal) = @_;
 2397:     my $datatable;
 2398:     if ($position eq 'top') {
 2399:         my $updateon = ' ';
 2400:         my $updateoff = ' checked="checked" ';
 2401:         my $classlistson = ' ';
 2402:         my $classlistsoff = ' checked="checked" ';
 2403:         if (ref($settings) eq 'HASH') {
 2404:             if ($settings->{'run'} eq '1') {
 2405:                 $updateon = $updateoff;
 2406:                 $updateoff = ' ';
 2407:             }
 2408:             if ($settings->{'classlists'} eq '1') {
 2409:                 $classlistson = $classlistsoff;
 2410:                 $classlistsoff = ' ';
 2411:             }
 2412:         }
 2413:         my %title = (
 2414:                    run => 'Auto-update active?',
 2415:                    classlists => 'Update information in classlists?',
 2416:                     );
 2417:         $datatable = '<tr class="LC_odd_row">'. 
 2418:                   '<td>'.&mt($title{'run'}).'</td>'.
 2419:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2420:                   '<input type="radio" name="autoupdate_run"'.
 2421:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2422:                   '<label><input type="radio" name="autoupdate_run"'.
 2423:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2424:                   '</tr><tr>'.
 2425:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2426:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2427:                   '<label><input type="radio" name="classlists"'.
 2428:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2429:                   '<label><input type="radio" name="classlists"'.
 2430:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2431:                   '</tr>';
 2432:         $$rowtotal += 2;
 2433:     } elsif ($position eq 'middle') {
 2434:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2435:         my $numinrow = 3;
 2436:         my $locknamesettings;
 2437:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2438:                                      $dom,$numinrow,$othertitle,
 2439:                                     'lockablenames');
 2440:         $$rowtotal ++;
 2441:     } else {
 2442:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2443:         my @fields = ('lastname','firstname','middlename','generation',
 2444:                       'permanentemail','id');
 2445:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2446:         my $numrows = 0;
 2447:         if (ref($types) eq 'ARRAY') {
 2448:             if (@{$types} > 0) {
 2449:                 $datatable = 
 2450:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2451:                                          \@fields,$types,\$numrows);
 2452:                     $$rowtotal += @{$types}; 
 2453:             }
 2454:         }
 2455:         $datatable .= 
 2456:             &usertype_update_row($settings,{'default' => $othertitle},
 2457:                                  \%fieldtitles,\@fields,['default'],
 2458:                                  \$numrows);
 2459:         $$rowtotal ++;     
 2460:     }
 2461:     return $datatable;
 2462: }
 2463: 
 2464: sub print_autocreate {
 2465:     my ($dom,$settings,$rowtotal) = @_;
 2466:     my (%createon,%createoff,%currhash);
 2467:     my @types = ('xml','req');
 2468:     if (ref($settings) eq 'HASH') {
 2469:         foreach my $item (@types) {
 2470:             $createoff{$item} = ' checked="checked" ';
 2471:             $createon{$item} = ' ';
 2472:             if (exists($settings->{$item})) {
 2473:                 if ($settings->{$item}) {
 2474:                     $createon{$item} = ' checked="checked" ';
 2475:                     $createoff{$item} = ' ';
 2476:                 }
 2477:             }
 2478:         }
 2479:         if ($settings->{'xmldc'} ne '') {
 2480:             $currhash{$settings->{'xmldc'}} = 1;
 2481:         }
 2482:     } else {
 2483:         foreach my $item (@types) {
 2484:             $createoff{$item} = ' checked="checked" ';
 2485:             $createon{$item} = ' ';
 2486:         }
 2487:     }
 2488:     $$rowtotal += 2;
 2489:     my $numinrow = 2;
 2490:     my $datatable='<tr class="LC_odd_row">'.
 2491:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2492:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2493:                   '<input type="radio" name="autocreate_xml"'.
 2494:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2495:                   '<label><input type="radio" name="autocreate_xml"'.
 2496:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2497:                   '</td></tr><tr>'.
 2498:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2499:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2500:                   '<input type="radio" name="autocreate_req"'.
 2501:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2502:                   '<label><input type="radio" name="autocreate_req"'.
 2503:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2504:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2505:                                                    'autocreate_xmldc',%currhash);
 2506:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2507:     if ($numdc > 1) {
 2508:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2509:                       '</td><td class="LC_left_item">';
 2510:     } else {
 2511:         $datatable .= &mt('Course creation processed as:').
 2512:                       '</td><td class="LC_right_item">';
 2513:     }
 2514:     $datatable .= $dctable.'</td></tr>';
 2515:     $$rowtotal += $rows;
 2516:     return $datatable;
 2517: }
 2518: 
 2519: sub print_directorysrch {
 2520:     my ($dom,$settings,$rowtotal) = @_;
 2521:     my $srchon = ' ';
 2522:     my $srchoff = ' checked="checked" ';
 2523:     my ($exacton,$containson,$beginson);
 2524:     my $localon = ' ';
 2525:     my $localoff = ' checked="checked" ';
 2526:     if (ref($settings) eq 'HASH') {
 2527:         if ($settings->{'available'} eq '1') {
 2528:             $srchon = $srchoff;
 2529:             $srchoff = ' ';
 2530:         }
 2531:         if ($settings->{'localonly'} eq '1') {
 2532:             $localon = $localoff;
 2533:             $localoff = ' ';
 2534:         }
 2535:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2536:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2537:                 if ($type eq 'exact') {
 2538:                     $exacton = ' checked="checked" ';
 2539:                 } elsif ($type eq 'contains') {
 2540:                     $containson = ' checked="checked" ';
 2541:                 } elsif ($type eq 'begins') {
 2542:                     $beginson = ' checked="checked" ';
 2543:                 }
 2544:             }
 2545:         } else {
 2546:             if ($settings->{'searchtypes'} eq 'exact') {
 2547:                 $exacton = ' checked="checked" ';
 2548:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2549:                 $containson = ' checked="checked" ';
 2550:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2551:                 $exacton = ' checked="checked" ';
 2552:                 $containson = ' checked="checked" ';
 2553:             }
 2554:         }
 2555:     }
 2556:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2557:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2558: 
 2559:     my $numinrow = 4;
 2560:     my $cansrchrow = 0;
 2561:     my $datatable='<tr class="LC_odd_row">'.
 2562:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2563:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2564:                   '<input type="radio" name="dirsrch_available"'.
 2565:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2566:                   '<label><input type="radio" name="dirsrch_available"'.
 2567:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2568:                   '</tr><tr>'.
 2569:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2570:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2571:                   '<input type="radio" name="dirsrch_localonly"'.
 2572:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2573:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2574:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2575:                   '</tr>';
 2576:     $$rowtotal += 2;
 2577:     if (ref($usertypes) eq 'HASH') {
 2578:         if (keys(%{$usertypes}) > 0) {
 2579:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2580:                                          $numinrow,$othertitle,'cansearch');
 2581:             $cansrchrow = 1;
 2582:         }
 2583:     }
 2584:     if ($cansrchrow) {
 2585:         $$rowtotal ++;
 2586:         $datatable .= '<tr>';
 2587:     } else {
 2588:         $datatable .= '<tr class="LC_odd_row">';
 2589:     }
 2590:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2591:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2592:     foreach my $title (@{$titleorder}) {
 2593:         if (defined($searchtitles->{$title})) {
 2594:             my $check = ' ';
 2595:             if (ref($settings) eq 'HASH') {
 2596:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2597:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2598:                         $check = ' checked="checked" ';
 2599:                     }
 2600:                 }
 2601:             }
 2602:             $datatable .= '<td class="LC_left_item">'.
 2603:                           '<span class="LC_nobreak"><label>'.
 2604:                           '<input type="checkbox" name="searchby" '.
 2605:                           'value="'.$title.'"'.$check.'/>'.
 2606:                           $searchtitles->{$title}.'</label></span></td>';
 2607:         }
 2608:     }
 2609:     $datatable .= '</tr></table></td></tr>';
 2610:     $$rowtotal ++;
 2611:     if ($cansrchrow) {
 2612:         $datatable .= '<tr class="LC_odd_row">';
 2613:     } else {
 2614:         $datatable .= '<tr>';
 2615:     }
 2616:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2617:                   '<td class="LC_left_item" colspan="2">'.
 2618:                   '<span class="LC_nobreak"><label>'.
 2619:                   '<input type="checkbox" name="searchtypes" '.
 2620:                   $exacton.' value="exact" />'.&mt('Exact match').
 2621:                   '</label>&nbsp;'.
 2622:                   '<label><input type="checkbox" name="searchtypes" '.
 2623:                   $beginson.' value="begins" />'.&mt('Begins with').
 2624:                   '</label>&nbsp;'.
 2625:                   '<label><input type="checkbox" name="searchtypes" '.
 2626:                   $containson.' value="contains" />'.&mt('Contains').
 2627:                   '</label></span></td></tr>';
 2628:     $$rowtotal ++;
 2629:     return $datatable;
 2630: }
 2631: 
 2632: sub print_contacts {
 2633:     my ($dom,$settings,$rowtotal) = @_;
 2634:     my $datatable;
 2635:     my @contacts = ('adminemail','supportemail');
 2636:     my (%checked,%to,%otheremails,%bccemails);
 2637:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2638:                     'requestsmail','updatesmail','idconflictsmail');
 2639:     foreach my $type (@mailings) {
 2640:         $otheremails{$type} = '';
 2641:     }
 2642:     $bccemails{'helpdeskmail'} = '';
 2643:     if (ref($settings) eq 'HASH') {
 2644:         foreach my $item (@contacts) {
 2645:             if (exists($settings->{$item})) {
 2646:                 $to{$item} = $settings->{$item};
 2647:             }
 2648:         }
 2649:         foreach my $type (@mailings) {
 2650:             if (exists($settings->{$type})) {
 2651:                 if (ref($settings->{$type}) eq 'HASH') {
 2652:                     foreach my $item (@contacts) {
 2653:                         if ($settings->{$type}{$item}) {
 2654:                             $checked{$type}{$item} = ' checked="checked" ';
 2655:                         }
 2656:                     }
 2657:                     $otheremails{$type} = $settings->{$type}{'others'};
 2658:                     if ($type eq 'helpdeskmail') {
 2659:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2660:                     }
 2661:                 }
 2662:             } elsif ($type eq 'lonstatusmail') {
 2663:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2664:             }
 2665:         }
 2666:     } else {
 2667:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2668:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2669:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2670:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2671:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2672:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2673:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2674:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2675:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2676:     }
 2677:     my ($titles,$short_titles) = &contact_titles();
 2678:     my $rownum = 0;
 2679:     my $css_class;
 2680:     foreach my $item (@contacts) {
 2681:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2682:         $datatable .= '<tr'.$css_class.'>'. 
 2683:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2684:                   '</span></td><td class="LC_right_item">'.
 2685:                   '<input type="text" name="'.$item.'" value="'.
 2686:                   $to{$item}.'" /></td></tr>';
 2687:         $rownum ++;
 2688:     }
 2689:     foreach my $type (@mailings) {
 2690:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2691:         $datatable .= '<tr'.$css_class.'>'.
 2692:                       '<td><span class="LC_nobreak">'.
 2693:                       $titles->{$type}.': </span></td>'.
 2694:                       '<td class="LC_left_item">'.
 2695:                       '<span class="LC_nobreak">';
 2696:         foreach my $item (@contacts) {
 2697:             $datatable .= '<label>'.
 2698:                           '<input type="checkbox" name="'.$type.'"'.
 2699:                           $checked{$type}{$item}.
 2700:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2701:                           '</label>&nbsp;';
 2702:         }
 2703:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2704:                       '<input type="text" name="'.$type.'_others" '.
 2705:                       'value="'.$otheremails{$type}.'"  />';
 2706:         if ($type eq 'helpdeskmail') {
 2707:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2708:                           '<input type="text" name="'.$type.'_bcc" '.
 2709:                           'value="'.$bccemails{$type}.'"  />';
 2710:         }
 2711:         $datatable .= '</td></tr>'."\n";
 2712:         $rownum ++;
 2713:     }
 2714:     my %choices;
 2715:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2716:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2717:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2718:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2719:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2720:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2721:     my @toggles = ('reporterrors','reportupdates');
 2722:     my %defaultchecked = ('reporterrors'  => 'on',
 2723:                           'reportupdates' => 'on');
 2724:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2725:                                                \%choices,$rownum);
 2726:     $datatable .= $reports;
 2727:     $$rowtotal += $rownum;
 2728:     return $datatable;
 2729: }
 2730: 
 2731: sub print_helpsettings {
 2732:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2733:     my ($datatable,$itemcount);
 2734:     $itemcount = 1;
 2735:     my (%choices,%defaultchecked,@toggles);
 2736:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2737:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2738:                                  &mt('LON-CAPA bug tracker'),600,500));
 2739:     %defaultchecked = ('submitbugs' => 'on');
 2740:     @toggles = ('submitbugs',);
 2741: 
 2742:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2743:                                                  \%choices,$itemcount);
 2744:     return $datatable;
 2745: }
 2746: 
 2747: sub radiobutton_prefs {
 2748:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2749:         $additional) = @_;
 2750:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2751:                    (ref($choices) eq 'HASH'));
 2752: 
 2753:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2754: 
 2755:     foreach my $item (@{$toggles}) {
 2756:         if ($defaultchecked->{$item} eq 'on') {
 2757:             $checkedon{$item} = ' checked="checked" ';
 2758:             $checkedoff{$item} = ' ';
 2759:         } elsif ($defaultchecked->{$item} eq 'off') {
 2760:             $checkedoff{$item} = ' checked="checked" ';
 2761:             $checkedon{$item} = ' ';
 2762:         }
 2763:     }
 2764:     if (ref($settings) eq 'HASH') {
 2765:         foreach my $item (@{$toggles}) {
 2766:             if ($settings->{$item} eq '1') {
 2767:                 $checkedon{$item} =  ' checked="checked" ';
 2768:                 $checkedoff{$item} = ' ';
 2769:             } elsif ($settings->{$item} eq '0') {
 2770:                 $checkedoff{$item} =  ' checked="checked" ';
 2771:                 $checkedon{$item} = ' ';
 2772:             }
 2773:         }
 2774:     }
 2775:     if ($onclick) {
 2776:         $onclick = ' onclick="'.$onclick.'"';
 2777:     }
 2778:     foreach my $item (@{$toggles}) {
 2779:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2780:         $datatable .=
 2781:             '<tr'.$css_class.'><td valign="top">'.
 2782:             '<span class="LC_nobreak">'.$choices->{$item}.
 2783:             '</span></td>'.
 2784:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2785:             '<label><input type="radio" name="'.
 2786:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2787:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2788:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2789:             '</span>'.$additional.
 2790:             '</td>'.
 2791:             '</tr>';
 2792:         $itemcount ++;
 2793:     }
 2794:     return ($datatable,$itemcount);
 2795: }
 2796: 
 2797: sub print_coursedefaults {
 2798:     my ($position,$dom,$settings,$rowtotal) = @_;
 2799:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2800:     my $itemcount = 1;
 2801:     my %choices =  &Apache::lonlocal::texthash (
 2802:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2803:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2804:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2805:         coursecredits        => 'Credits can be specified for courses',
 2806:     );
 2807:     my %staticdefaults = (
 2808:                            anonsurvey_threshold => 10,
 2809:                            uploadquota          => 500,
 2810:                          );
 2811:     if ($position eq 'top') {
 2812:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2813:         @toggles = ('canuse_pdfforms');
 2814:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2815:                                                  \%choices,$itemcount);
 2816:     } else {
 2817:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2818:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2819:             %curruploadquota);
 2820:         my $currusecredits = 0;
 2821:         my @types = ('official','unofficial','community','textbook');
 2822:         if (ref($settings) eq 'HASH') {
 2823:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2824:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2825:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2826:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2827:                 }
 2828:             }
 2829:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2830:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2831:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2832:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2833:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2834:                     ($def_textbook_credits ne '')) {
 2835:                     $currusecredits = 1;
 2836:                 }
 2837:             }
 2838:         }
 2839:         if (!$currdefresponder) {
 2840:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2841:         } elsif ($currdefresponder < 1) {
 2842:             $currdefresponder = 1;
 2843:         }
 2844:         foreach my $type (@types) {
 2845:             if ($curruploadquota{$type} eq '') {
 2846:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2847:             }
 2848:         }
 2849:         $datatable .=
 2850:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2851:                 $choices{'anonsurvey_threshold'}.
 2852:                 '</span></td>'.
 2853:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2854:                 '<input type="text" name="anonsurvey_threshold"'.
 2855:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2856:                 '</td></tr>'."\n";
 2857:         $itemcount ++;
 2858:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2859:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2860:                       $choices{'uploadquota'}.
 2861:                       '</span></td>'.
 2862:                       '<td align="right" class="LC_right_item">'.
 2863:                       '<table><tr>';
 2864:         foreach my $type (@types) {
 2865:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2866:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2867:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2868:         }
 2869:         $datatable .= '</tr></table></td></tr>'."\n";
 2870:         $itemcount ++;
 2871:         my $onclick = "toggleDisplay(this.form,'credits');";
 2872:         my $display = 'none';
 2873:         if ($currusecredits) {
 2874:             $display = 'block';
 2875:         }
 2876:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2877:                          '<span class="LC_nobreak">'.
 2878:                          &mt('Default credits for official courses [_1]',
 2879:                          '<input type="text" name="official_credits" value="'.
 2880:                          $def_official_credits.'" size="3" />').
 2881:                          '</span><br />'.
 2882:                          '<span class="LC_nobreak">'.
 2883:                          &mt('Default credits for unofficial courses [_1]',
 2884:                          '<input type="text" name="unofficial_credits" value="'.
 2885:                          $def_unofficial_credits.'" size="3" />').
 2886:                          '</span><br />'.
 2887:                          '<span class="LC_nobreak">'.
 2888:                          &mt('Default credits for textbook courses [_1]',
 2889:                          '<input type="text" name="textbook_credits" value="'.
 2890:                          $def_textbook_credits.'" size="3" />').
 2891:                          '</span></div>'."\n";
 2892:         %defaultchecked = ('coursecredits' => 'off');
 2893:         @toggles = ('coursecredits');
 2894:         my $current = {
 2895:                         'coursecredits' => $currusecredits,
 2896:                       };
 2897:         (my $table,$itemcount) =
 2898:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2899:                                \%choices,$itemcount,$onclick,$additional);
 2900:         $datatable .= $table;
 2901:         $itemcount ++;
 2902:     }
 2903:     $$rowtotal += $itemcount;
 2904:     return $datatable;
 2905: }
 2906: 
 2907: sub print_selfenrollment {
 2908:     my ($position,$dom,$settings,$rowtotal) = @_;
 2909:     my ($css_class,$datatable);
 2910:     my $itemcount = 1;
 2911:     my @types = ('official','unofficial','community','textbook');
 2912:     if (($position eq 'top') || ($position eq 'middle')) {
 2913:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 2914:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 2915:         my @rows;
 2916:         my $key;
 2917:         if ($position eq 'top') {
 2918:             $key = 'admin'; 
 2919:             if (ref($rowsref) eq 'ARRAY') {
 2920:                 @rows = @{$rowsref};
 2921:             }
 2922:         } elsif ($position eq 'middle') {
 2923:             $key = 'default';
 2924:             @rows = ('types','registered','approval','limit');
 2925:         }
 2926:         foreach my $row (@rows) {
 2927:             if (defined($titlesref->{$row})) {
 2928:                 $itemcount ++;
 2929:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2930:                 $datatable .= '<tr'.$css_class.'>'.
 2931:                               '<td>'.$titlesref->{$row}.'</td>'.
 2932:                               '<td class="LC_left_item">'.
 2933:                               '<table><tr>';
 2934:                 my (%current,%currentcap);
 2935:                 if (ref($settings) eq 'HASH') {
 2936:                     if (ref($settings->{$key}) eq 'HASH') {
 2937:                         foreach my $type (@types) {
 2938:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2939:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 2940:                             }
 2941:                             if (($row eq 'limit') && ($key eq 'default')) {
 2942:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2943:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 2944:                                 }
 2945:                             }
 2946:                         }
 2947:                     }
 2948:                 }
 2949:                 my %roles = (
 2950:                              '0' => &Apache::lonnet::plaintext('dc'),
 2951:                             ); 
 2952:             
 2953:                 foreach my $type (@types) {
 2954:                     unless (($row eq 'registered') && ($key eq 'default')) {
 2955:                         $datatable .= '<th>'.&mt($type).'</th>';
 2956:                     }
 2957:                 }
 2958:                 unless (($row eq 'registered') && ($key eq 'default')) {
 2959:                     $datatable .= '</tr><tr>';
 2960:                 }
 2961:                 foreach my $type (@types) {
 2962:                     if ($type eq 'community') {
 2963:                         $roles{'1'} = &mt('Community personnel');
 2964:                     } else {
 2965:                         $roles{'1'} = &mt('Course personnel');
 2966:                     }
 2967:                     $datatable .= '<td style="vertical-align: top">';
 2968:                     if ($position eq 'top') {
 2969:                         my %checked;
 2970:                         if ($current{$type} eq '0') {
 2971:                             $checked{'0'} = ' checked="checked"';
 2972:                         } else {
 2973:                             $checked{'1'} = ' checked="checked"';
 2974:                         }
 2975:                         foreach my $role ('1','0') {
 2976:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2977:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 2978:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 2979:                                           $roles{$role}.'</label></span> ';
 2980:                         }
 2981:                     } else {
 2982:                         if ($row eq 'types') {
 2983:                             my %checked;
 2984:                             if ($current{$type} =~ /^(all|dom)$/) {
 2985:                                 $checked{$1} = ' checked="checked"';
 2986:                             } else {
 2987:                                 $checked{''} = ' checked="checked"';
 2988:                             }
 2989:                             foreach my $val ('','dom','all') {
 2990:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2991:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2992:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2993:                             }
 2994:                         } elsif ($row eq 'registered') {
 2995:                             my %checked;
 2996:                             if ($current{$type} eq '1') {
 2997:                                 $checked{'1'} = ' checked="checked"';
 2998:                             } else {
 2999:                                 $checked{'0'} = ' checked="checked"';
 3000:                             }
 3001:                             foreach my $val ('0','1') {
 3002:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3003:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3004:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3005:                             }
 3006:                         } elsif ($row eq 'approval') {
 3007:                             my %checked;
 3008:                             if ($current{$type} =~ /^([12])$/) {
 3009:                                 $checked{$1} = ' checked="checked"';
 3010:                             } else {
 3011:                                 $checked{'0'} = ' checked="checked"';
 3012:                             }
 3013:                             for my $val (0..2) {
 3014:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3015:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3016:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3017:                             }
 3018:                         } elsif ($row eq 'limit') {
 3019:                             my %checked;
 3020:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3021:                                 $checked{$1} = ' checked="checked"';
 3022:                             } else {
 3023:                                 $checked{'none'} = ' checked="checked"';
 3024:                             }
 3025:                             my $cap;
 3026:                             if ($currentcap{$type} =~ /^\d+$/) {
 3027:                                 $cap = $currentcap{$type};
 3028:                             }
 3029:                             foreach my $val ('none','allstudents','selfenrolled') {
 3030:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3031:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3032:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3033:                             }
 3034:                             $datatable .= '<br />'.
 3035:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3036:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3037:                                           '</span>'; 
 3038:                         }
 3039:                     }
 3040:                     $datatable .= '</td>';
 3041:                 }
 3042:                 $datatable .= '</tr>';
 3043:             }
 3044:             $datatable .= '</table></td></tr>';
 3045:         }
 3046:     } elsif ($position eq 'bottom') {
 3047:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3048:     }
 3049:     $$rowtotal += $itemcount;
 3050:     return $datatable;
 3051: }
 3052: 
 3053: sub print_validation_rows {
 3054:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3055:     my ($itemsref,$namesref,$fieldsref);
 3056:     if ($caller eq 'selfenroll') { 
 3057:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3058:     } elsif ($caller eq 'requestcourses') {
 3059:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3060:     }
 3061:     my %currvalidation;
 3062:     if (ref($settings) eq 'HASH') {
 3063:         if (ref($settings->{'validation'}) eq 'HASH') {
 3064:             %currvalidation = %{$settings->{'validation'}};
 3065:         }
 3066:     }
 3067:     my $datatable;
 3068:     my $itemcount = 0;
 3069:     foreach my $item (@{$itemsref}) {
 3070:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3071:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3072:                       $namesref->{$item}.
 3073:                       '</span></td>'.
 3074:                       '<td class="LC_left_item">';
 3075:         if (($item eq 'url') || ($item eq 'button')) {
 3076:             $datatable .= '<span class="LC_nobreak">'.
 3077:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3078:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3079:         } elsif ($item eq 'fields') {
 3080:             my @currfields;
 3081:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3082:                 @currfields = @{$currvalidation{$item}};
 3083:             }
 3084:             foreach my $field (@{$fieldsref}) {
 3085:                 my $check = '';
 3086:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3087:                     $check = ' checked="checked"';
 3088:                 }
 3089:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3090:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3091:                               ' value="'.$field.'"'.$check.' />'.$field.
 3092:                               '</label></span> ';
 3093:             }
 3094:         } elsif ($item eq 'markup') {
 3095:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3096:                            $currvalidation{$item}.
 3097:                               '</textarea>';
 3098:         }
 3099:         $datatable .= '</td></tr>'."\n";
 3100:         if (ref($rowtotal)) {
 3101:             $itemcount ++;
 3102:         }
 3103:     }
 3104:     if ($caller eq 'requestcourses') {
 3105:         my %currhash;
 3106:         if (ref($settings) eq 'HASH') {
 3107:             if (ref($settings->{'validation'}) eq 'HASH') {
 3108:                 if ($settings->{'validation'}{'dc'} ne '') {
 3109:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 3110:                 }
 3111:             }
 3112:         }
 3113:         my $numinrow = 2;
 3114:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3115:                                                        'validationdc',%currhash);
 3116:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3117:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 3118:         if ($numdc > 1) {
 3119:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 3120:         } else {
 3121:             $datatable .=  &mt('Course creation processed as: ');
 3122:         }
 3123:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3124:         $itemcount ++;
 3125:     }
 3126:     if (ref($rowtotal)) {
 3127:         $$rowtotal += $itemcount;
 3128:     }
 3129:     return $datatable;
 3130: }
 3131: 
 3132: sub print_usersessions {
 3133:     my ($position,$dom,$settings,$rowtotal) = @_;
 3134:     my ($css_class,$datatable,%checked,%choices);
 3135:     my (%by_ip,%by_location,@intdoms);
 3136:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3137: 
 3138:     my @alldoms = &Apache::lonnet::all_domains();
 3139:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3140:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3141:     my %altids = &id_for_thisdom(%servers);
 3142:     my $itemcount = 1;
 3143:     if ($position eq 'top') {
 3144:         if (keys(%serverhomes) > 1) {
 3145:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3146:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 3147:         } else {
 3148:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3149:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3150:         }
 3151:     } else {
 3152:         if (keys(%by_location) == 0) {
 3153:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3154:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3155:         } else {
 3156:             my %lt = &usersession_titles();
 3157:             my $numinrow = 5;
 3158:             my $prefix;
 3159:             my @types;
 3160:             if ($position eq 'bottom') {
 3161:                 $prefix = 'remote';
 3162:                 @types = ('version','excludedomain','includedomain');
 3163:             } else {
 3164:                 $prefix = 'hosted';
 3165:                 @types = ('excludedomain','includedomain');
 3166:             }
 3167:             my (%current,%checkedon,%checkedoff);
 3168:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3169:             my @locations = sort(keys(%by_location));
 3170:             foreach my $type (@types) {
 3171:                 $checkedon{$type} = '';
 3172:                 $checkedoff{$type} = ' checked="checked"';
 3173:             }
 3174:             if (ref($settings) eq 'HASH') {
 3175:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3176:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3177:                         $current{$key} = $settings->{$prefix}{$key};
 3178:                         if ($key eq 'version') {
 3179:                             if ($current{$key} ne '') {
 3180:                                 $checkedon{$key} = ' checked="checked"';
 3181:                                 $checkedoff{$key} = '';
 3182:                             }
 3183:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3184:                             $checkedon{$key} = ' checked="checked"';
 3185:                             $checkedoff{$key} = '';
 3186:                         }
 3187:                     }
 3188:                 }
 3189:             }
 3190:             foreach my $type (@types) {
 3191:                 next if ($type ne 'version' && !@locations);
 3192:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3193:                 $datatable .= '<tr'.$css_class.'>
 3194:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3195:                                <span class="LC_nobreak">&nbsp;
 3196:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3197:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3198:                 if ($type eq 'version') {
 3199:                     my $selector = '<select name="'.$prefix.'_version">';
 3200:                     foreach my $version (@lcversions) {
 3201:                         my $selected = '';
 3202:                         if ($current{'version'} eq $version) {
 3203:                             $selected = ' selected="selected"';
 3204:                         }
 3205:                         $selector .= ' <option value="'.$version.'"'.
 3206:                                      $selected.'>'.$version.'</option>';
 3207:                     }
 3208:                     $selector .= '</select> ';
 3209:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3210:                 } else {
 3211:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3212:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3213:                                  ' />'.('&nbsp;'x2).
 3214:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3215:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3216:                                  "\n".
 3217:                                  '</div><div><table>';
 3218:                     my $rem;
 3219:                     for (my $i=0; $i<@locations; $i++) {
 3220:                         my ($showloc,$value,$checkedtype);
 3221:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3222:                             my $ip = $by_location{$locations[$i]}->[0];
 3223:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3224:                                  $value = join(':',@{$by_ip{$ip}});
 3225:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3226:                                 if (ref($current{$type}) eq 'ARRAY') {
 3227:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3228:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3229:                                             $checkedtype = ' checked="checked"';
 3230:                                             last;
 3231:                                         }
 3232:                                     }
 3233:                                 }
 3234:                             }
 3235:                         }
 3236:                         $rem = $i%($numinrow);
 3237:                         if ($rem == 0) {
 3238:                             if ($i > 0) {
 3239:                                 $datatable .= '</tr>';
 3240:                             }
 3241:                             $datatable .= '<tr>';
 3242:                         }
 3243:                         $datatable .= '<td class="LC_left_item">'.
 3244:                                       '<span class="LC_nobreak"><label>'.
 3245:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3246:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3247:                                       '</label></span></td>';
 3248:                     }
 3249:                     $rem = @locations%($numinrow);
 3250:                     my $colsleft = $numinrow - $rem;
 3251:                     if ($colsleft > 1 ) {
 3252:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3253:                                       '&nbsp;</td>';
 3254:                     } elsif ($colsleft == 1) {
 3255:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3256:                     }
 3257:                     $datatable .= '</tr></table>';
 3258:                 }
 3259:                 $datatable .= '</td></tr>';
 3260:                 $itemcount ++;
 3261:             }
 3262:         }
 3263:     }
 3264:     $$rowtotal += $itemcount;
 3265:     return $datatable;
 3266: }
 3267: 
 3268: sub build_location_hashes {
 3269:     my ($intdoms,$by_ip,$by_location) = @_;
 3270:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3271:                   (ref($by_location) eq 'HASH')); 
 3272:     my %iphost = &Apache::lonnet::get_iphost();
 3273:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3274:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3275:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3276:         foreach my $id (@{$iphost{$primary_ip}}) {
 3277:             my $intdom = &Apache::lonnet::internet_dom($id);
 3278:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3279:                 push(@{$intdoms},$intdom);
 3280:             }
 3281:         }
 3282:     }
 3283:     foreach my $ip (keys(%iphost)) {
 3284:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3285:             foreach my $id (@{$iphost{$ip}}) {
 3286:                 my $location = &Apache::lonnet::internet_dom($id);
 3287:                 if ($location) {
 3288:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3289:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3290:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3291:                             push(@{$by_ip->{$ip}},$location);
 3292:                         }
 3293:                     } else {
 3294:                         $by_ip->{$ip} = [$location];
 3295:                     }
 3296:                 }
 3297:             }
 3298:         }
 3299:     }
 3300:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3301:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3302:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3303:             my $first = $by_ip->{$ip}->[0];
 3304:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3305:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3306:                     push(@{$by_location->{$first}},$ip);
 3307:                 }
 3308:             } else {
 3309:                 $by_location->{$first} = [$ip];
 3310:             }
 3311:         }
 3312:     }
 3313:     return;
 3314: }
 3315: 
 3316: sub current_offloads_to {
 3317:     my ($dom,$settings,$servers) = @_;
 3318:     my (%spareid,%otherdomconfigs);
 3319:     if (ref($servers) eq 'HASH') {
 3320:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3321:             my $gotspares;
 3322:             if (ref($settings) eq 'HASH') {
 3323:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3324:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3325:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3326:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3327:                         $gotspares = 1;
 3328:                     }
 3329:                 }
 3330:             }
 3331:             unless ($gotspares) {
 3332:                 my $gotspares;
 3333:                 my $serverhomeID =
 3334:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3335:                 my $serverhomedom =
 3336:                     &Apache::lonnet::host_domain($serverhomeID);
 3337:                 if ($serverhomedom ne $dom) {
 3338:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3339:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3340:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3341:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3342:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3343:                                 $gotspares = 1;
 3344:                             }
 3345:                         }
 3346:                     } else {
 3347:                         $otherdomconfigs{$serverhomedom} =
 3348:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3349:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3350:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3351:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3352:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3353:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3354:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3355:                                         $gotspares = 1;
 3356:                                     }
 3357:                                 }
 3358:                             }
 3359:                         }
 3360:                     }
 3361:                 }
 3362:             }
 3363:             unless ($gotspares) {
 3364:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3365:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3366:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3367:                } else {
 3368:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3369:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3370:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3371:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3372:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3373:                     } else {
 3374:                         my %what = (
 3375:                              spareid => 1,
 3376:                         );
 3377:                         my ($result,$returnhash) = 
 3378:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3379:                         if ($result eq 'ok') { 
 3380:                             if (ref($returnhash) eq 'HASH') {
 3381:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3382:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3383:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3384:                                 }
 3385:                             }
 3386:                         }
 3387:                     }
 3388:                 }
 3389:             }
 3390:         }
 3391:     }
 3392:     return %spareid;
 3393: }
 3394: 
 3395: sub spares_row {
 3396:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3397:     my $css_class;
 3398:     my $numinrow = 4;
 3399:     my $itemcount = 1;
 3400:     my $datatable;
 3401:     my %typetitles = &sparestype_titles();
 3402:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3403:         foreach my $server (sort(keys(%{$servers}))) {
 3404:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3405:             my ($othercontrol,$serverdom);
 3406:             if ($serverhome ne $server) {
 3407:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3408:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3409:             } else {
 3410:                 $serverdom = &Apache::lonnet::host_domain($server);
 3411:                 if ($serverdom ne $dom) {
 3412:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3413:                 }
 3414:             }
 3415:             next unless (ref($spareid->{$server}) eq 'HASH');
 3416:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3417:             $datatable .= '<tr'.$css_class.'>
 3418:                            <td rowspan="2">
 3419:                             <span class="LC_nobreak">'.
 3420:                           &mt('[_1] when busy, offloads to:'
 3421:                               ,'<b>'.$server.'</b>').
 3422:                           "\n";
 3423:             my (%current,%canselect);
 3424:             my @choices = 
 3425:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3426:             foreach my $type ('primary','default') {
 3427:                 if (ref($spareid->{$server}) eq 'HASH') {
 3428:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3429:                         my @spares = @{$spareid->{$server}{$type}};
 3430:                         if (@spares > 0) {
 3431:                             if ($othercontrol) {
 3432:                                 $current{$type} = join(', ',@spares);
 3433:                             } else {
 3434:                                 $current{$type} .= '<table>';
 3435:                                 my $numspares = scalar(@spares);
 3436:                                 for (my $i=0;  $i<@spares; $i++) {
 3437:                                     my $rem = $i%($numinrow);
 3438:                                     if ($rem == 0) {
 3439:                                         if ($i > 0) {
 3440:                                             $current{$type} .= '</tr>';
 3441:                                         }
 3442:                                         $current{$type} .= '<tr>';
 3443:                                     }
 3444:                                     $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;'.
 3445:                                                        $spareid->{$server}{$type}[$i].
 3446:                                                        '</label></td>'."\n";
 3447:                                 }
 3448:                                 my $rem = @spares%($numinrow);
 3449:                                 my $colsleft = $numinrow - $rem;
 3450:                                 if ($colsleft > 1 ) {
 3451:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3452:                                                        '" class="LC_left_item">'.
 3453:                                                        '&nbsp;</td>';
 3454:                                 } elsif ($colsleft == 1) {
 3455:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3456:                                 }
 3457:                                 $current{$type} .= '</tr></table>';
 3458:                             }
 3459:                         }
 3460:                     }
 3461:                     if ($current{$type} eq '') {
 3462:                         $current{$type} = &mt('None specified');
 3463:                     }
 3464:                     if ($othercontrol) {
 3465:                         if ($type eq 'primary') {
 3466:                             $canselect{$type} = $othercontrol;
 3467:                         }
 3468:                     } else {
 3469:                         $canselect{$type} = 
 3470:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3471:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3472:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3473:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3474:                         if (@choices > 0) {
 3475:                             foreach my $lonhost (@choices) {
 3476:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3477:                             }
 3478:                         }
 3479:                         $canselect{$type} .= '</select>'."\n";
 3480:                     }
 3481:                 } else {
 3482:                     $current{$type} = &mt('Could not be determined');
 3483:                     if ($type eq 'primary') {
 3484:                         $canselect{$type} =  $othercontrol;
 3485:                     }
 3486:                 }
 3487:                 if ($type eq 'default') {
 3488:                     $datatable .= '<tr'.$css_class.'>';
 3489:                 }
 3490:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3491:                               '<td>'.$current{$type}.'</td>'."\n".
 3492:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3493:             }
 3494:             $itemcount ++;
 3495:         }
 3496:     }
 3497:     $$rowtotal += $itemcount;
 3498:     return $datatable;
 3499: }
 3500: 
 3501: sub possible_newspares {
 3502:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3503:     my $serverhostname = &Apache::lonnet::hostname($server);
 3504:     my %excluded;
 3505:     if ($serverhostname ne '') {
 3506:         %excluded = (
 3507:                        $serverhostname => 1,
 3508:                     );
 3509:     }
 3510:     if (ref($currspares) eq 'HASH') {
 3511:         foreach my $type (keys(%{$currspares})) {
 3512:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3513:                 if (@{$currspares->{$type}} > 0) {
 3514:                     foreach my $curr (@{$currspares->{$type}}) {
 3515:                         my $hostname = &Apache::lonnet::hostname($curr);
 3516:                         $excluded{$hostname} = 1;
 3517:                     }
 3518:                 }
 3519:             }
 3520:         }
 3521:     }
 3522:     my @choices;
 3523:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3524:         if (keys(%{$serverhomes}) > 1) {
 3525:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3526:                 unless ($excluded{$name}) {
 3527:                     if (exists($altids->{$serverhomes->{$name}})) {
 3528:                         push(@choices,$altids->{$serverhomes->{$name}});
 3529:                     } else {
 3530:                         push(@choices,$serverhomes->{$name});
 3531:                     }
 3532:                 }
 3533:             }
 3534:         }
 3535:     }
 3536:     return sort(@choices);
 3537: }
 3538: 
 3539: sub print_loadbalancing {
 3540:     my ($dom,$settings,$rowtotal) = @_;
 3541:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3542:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3543:     my $numinrow = 1;
 3544:     my $datatable;
 3545:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3546:     my (%currbalancer,%currtargets,%currrules,%existing);
 3547:     if (ref($settings) eq 'HASH') {
 3548:         %existing = %{$settings};
 3549:     }
 3550:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3551:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3552:                                   \%currtargets,\%currrules);
 3553:     } else {
 3554:         return;
 3555:     }
 3556:     my ($othertitle,$usertypes,$types) =
 3557:         &Apache::loncommon::sorted_inst_types($dom);
 3558:     my $rownum = 8;
 3559:     if (ref($types) eq 'ARRAY') {
 3560:         $rownum += scalar(@{$types});
 3561:     }
 3562:     my @css_class = ('LC_odd_row','LC_even_row');
 3563:     my $balnum = 0;
 3564:     my $islast;
 3565:     my (@toshow,$disabledtext);
 3566:     if (keys(%currbalancer) > 0) {
 3567:         @toshow = sort(keys(%currbalancer));
 3568:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3569:             push(@toshow,'');
 3570:         }
 3571:     } else {
 3572:         @toshow = ('');
 3573:         $disabledtext = &mt('No existing load balancer');
 3574:     }
 3575:     foreach my $lonhost (@toshow) {
 3576:         if ($balnum == scalar(@toshow)-1) {
 3577:             $islast = 1;
 3578:         } else {
 3579:             $islast = 0;
 3580:         }
 3581:         my $cssidx = $balnum%2;
 3582:         my $targets_div_style = 'display: none';
 3583:         my $disabled_div_style = 'display: block';
 3584:         my $homedom_div_style = 'display: none';
 3585:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3586:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3587:                       '<p>';
 3588:         if ($lonhost eq '') {
 3589:             $datatable .= '<span class="LC_nobreak">';
 3590:             if (keys(%currbalancer) > 0) {
 3591:                 $datatable .= &mt('Add balancer:');
 3592:             } else {
 3593:                 $datatable .= &mt('Enable balancer:');
 3594:             }
 3595:             $datatable .= '&nbsp;'.
 3596:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3597:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3598:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3599:                           '<option value="" selected="selected">'.&mt('None').
 3600:                           '</option>'."\n";
 3601:             foreach my $server (sort(keys(%servers))) {
 3602:                 next if ($currbalancer{$server});
 3603:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3604:             }
 3605:             $datatable .=
 3606:                 '</select>'."\n".
 3607:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3608:         } else {
 3609:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3610:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3611:                            &mt('Stop balancing').'</label>'.
 3612:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3613:             $targets_div_style = 'display: block';
 3614:             $disabled_div_style = 'display: none';
 3615:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3616:                 $homedom_div_style = 'display: block';
 3617:             }
 3618:         }
 3619:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3620:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3621:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3622:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3623:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3624:         my @sparestypes = ('primary','default');
 3625:         my %typetitles = &sparestype_titles();
 3626:         foreach my $sparetype (@sparestypes) {
 3627:             my $targettable;
 3628:             for (my $i=0; $i<$numspares; $i++) {
 3629:                 my $checked;
 3630:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3631:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3632:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3633:                             $checked = ' checked="checked"';
 3634:                         }
 3635:                     }
 3636:                 }
 3637:                 my ($chkboxval,$disabled);
 3638:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3639:                     $chkboxval = $spares[$i];
 3640:                 }
 3641:                 if (exists($currbalancer{$spares[$i]})) {
 3642:                     $disabled = ' disabled="disabled"';
 3643:                 }
 3644:                 $targettable .=
 3645:                     '<td><span class="LC_nobreak"><label>'.
 3646:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3647:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3648:                     '</span></label></span></td>';
 3649:                 my $rem = $i%($numinrow);
 3650:                 if ($rem == 0) {
 3651:                     if (($i > 0) && ($i < $numspares-1)) {
 3652:                         $targettable .= '</tr>';
 3653:                     }
 3654:                     if ($i < $numspares-1) {
 3655:                         $targettable .= '<tr>';
 3656:                     }
 3657:                 }
 3658:             }
 3659:             if ($targettable ne '') {
 3660:                 my $rem = $numspares%($numinrow);
 3661:                 my $colsleft = $numinrow - $rem;
 3662:                 if ($colsleft > 1 ) {
 3663:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3664:                                     '&nbsp;</td>';
 3665:                 } elsif ($colsleft == 1) {
 3666:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3667:                 }
 3668:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3669:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3670:             }
 3671:         }
 3672:         $datatable .= '</div></td></tr>'.
 3673:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3674:                                            $othertitle,$usertypes,$types,\%servers,
 3675:                                            \%currbalancer,$lonhost,
 3676:                                            $targets_div_style,$homedom_div_style,
 3677:                                            $css_class[$cssidx],$balnum,$islast);
 3678:         $$rowtotal += $rownum;
 3679:         $balnum ++;
 3680:     }
 3681:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3682:     return $datatable;
 3683: }
 3684: 
 3685: sub get_loadbalancers_config {
 3686:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3687:     return unless ((ref($servers) eq 'HASH') &&
 3688:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3689:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3690:     if (keys(%{$existing}) > 0) {
 3691:         my $oldlonhost;
 3692:         foreach my $key (sort(keys(%{$existing}))) {
 3693:             if ($key eq 'lonhost') {
 3694:                 $oldlonhost = $existing->{'lonhost'};
 3695:                 $currbalancer->{$oldlonhost} = 1;
 3696:             } elsif ($key eq 'targets') {
 3697:                 if ($oldlonhost) {
 3698:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3699:                 }
 3700:             } elsif ($key eq 'rules') {
 3701:                 if ($oldlonhost) {
 3702:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3703:                 }
 3704:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3705:                 $currbalancer->{$key} = 1;
 3706:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3707:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3708:             }
 3709:         }
 3710:     } else {
 3711:         my ($balancerref,$targetsref) =
 3712:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3713:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3714:             foreach my $server (sort(keys(%{$balancerref}))) {
 3715:                 $currbalancer->{$server} = 1;
 3716:                 $currtargets->{$server} = $targetsref->{$server};
 3717:             }
 3718:         }
 3719:     }
 3720:     return;
 3721: }
 3722: 
 3723: sub loadbalancing_rules {
 3724:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3725:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3726:         $css_class,$balnum,$islast) = @_;
 3727:     my $output;
 3728:     my $num = 0;
 3729:     my ($alltypes,$othertypes,$titles) =
 3730:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3731:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3732:         foreach my $type (@{$alltypes}) {
 3733:             $num ++;
 3734:             my $current;
 3735:             if (ref($currrules) eq 'HASH') {
 3736:                 $current = $currrules->{$type};
 3737:             }
 3738:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3739:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3740:                     $current = '';
 3741:                 }
 3742:             }
 3743:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3744:                                              $servers,$currbalancer,$lonhost,$dom,
 3745:                                              $targets_div_style,$homedom_div_style,
 3746:                                              $css_class,$balnum,$num,$islast);
 3747:         }
 3748:     }
 3749:     return $output;
 3750: }
 3751: 
 3752: sub loadbalancing_titles {
 3753:     my ($dom,$intdom,$usertypes,$types) = @_;
 3754:     my %othertypes = (
 3755:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3756:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3757:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3758:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3759:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3760:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3761:                      );
 3762:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3763:     if (ref($types) eq 'ARRAY') {
 3764:         unshift(@alltypes,@{$types},'default');
 3765:     }
 3766:     my %titles;
 3767:     foreach my $type (@alltypes) {
 3768:         if ($type =~ /^_LC_/) {
 3769:             $titles{$type} = $othertypes{$type};
 3770:         } elsif ($type eq 'default') {
 3771:             $titles{$type} = &mt('All users from [_1]',$dom);
 3772:             if (ref($types) eq 'ARRAY') {
 3773:                 if (@{$types} > 0) {
 3774:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3775:                 }
 3776:             }
 3777:         } elsif (ref($usertypes) eq 'HASH') {
 3778:             $titles{$type} = $usertypes->{$type};
 3779:         }
 3780:     }
 3781:     return (\@alltypes,\%othertypes,\%titles);
 3782: }
 3783: 
 3784: sub loadbalance_rule_row {
 3785:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3786:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3787:     my @rulenames;
 3788:     my %ruletitles = &offloadtype_text();
 3789:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3790:         @rulenames = ('balancer','offloadedto','specific');
 3791:     } else {
 3792:         @rulenames = ('default','homeserver');
 3793:         if ($type eq '_LC_external') {
 3794:             push(@rulenames,'externalbalancer');
 3795:         } else {
 3796:             push(@rulenames,'specific');
 3797:         }
 3798:         push(@rulenames,'none');
 3799:     }
 3800:     my $style = $targets_div_style;
 3801:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3802:         $style = $homedom_div_style;
 3803:     }
 3804:     my $space;
 3805:     if ($islast && $num == 1) {
 3806:         $space = '<div display="inline-block">&nbsp;</div>';
 3807:     }
 3808:     my $output =
 3809:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3810:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3811:         '<td valaign="top">'.$space.
 3812:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3813:     for (my $i=0; $i<@rulenames; $i++) {
 3814:         my $rule = $rulenames[$i];
 3815:         my ($checked,$extra);
 3816:         if ($rulenames[$i] eq 'default') {
 3817:             $rule = '';
 3818:         }
 3819:         if ($rulenames[$i] eq 'specific') {
 3820:             if (ref($servers) eq 'HASH') {
 3821:                 my $default;
 3822:                 if (($current ne '') && (exists($servers->{$current}))) {
 3823:                     $checked = ' checked="checked"';
 3824:                 }
 3825:                 unless ($checked) {
 3826:                     $default = ' selected="selected"';
 3827:                 }
 3828:                 $extra =
 3829:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3830:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3831:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3832:                     '<option value=""'.$default.'></option>'."\n";
 3833:                 foreach my $server (sort(keys(%{$servers}))) {
 3834:                     if (ref($currbalancer) eq 'HASH') {
 3835:                         next if (exists($currbalancer->{$server}));
 3836:                     }
 3837:                     my $selected;
 3838:                     if ($server eq $current) {
 3839:                         $selected = ' selected="selected"';
 3840:                     }
 3841:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3842:                 }
 3843:                 $extra .= '</select>';
 3844:             }
 3845:         } elsif ($rule eq $current) {
 3846:             $checked = ' checked="checked"';
 3847:         }
 3848:         $output .= '<span class="LC_nobreak"><label>'.
 3849:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3850:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3851:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3852:                    ')"'.$checked.' />&nbsp;';
 3853:         if (($rulenames[$i] eq 'specific') && ($type =~ /_LC_ipchange/)) {
 3854:             $output .= $ruletitles{'particular'};
 3855:         } else {
 3856:             $output .= $ruletitles{$rulenames[$i]};
 3857:         }
 3858:         $output .= '</label>'.$extra.'</span><br />'."\n";
 3859:     }
 3860:     $output .= '</div></td></tr>'."\n";
 3861:     return $output;
 3862: }
 3863: 
 3864: sub offloadtype_text {
 3865:     my %ruletitles = &Apache::lonlocal::texthash (
 3866:            'default'          => 'Offloads to default destinations',
 3867:            'homeserver'       => "Offloads to user's home server",
 3868:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3869:            'specific'         => 'Offloads to specific server',
 3870:            'none'             => 'No offload',
 3871:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3872:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3873:            'particular'       => 'Session hosted (after re-auth) on server:',
 3874:     );
 3875:     return %ruletitles;
 3876: }
 3877: 
 3878: sub sparestype_titles {
 3879:     my %typestitles = &Apache::lonlocal::texthash (
 3880:                           'primary' => 'primary',
 3881:                           'default' => 'default',
 3882:                       );
 3883:     return %typestitles;
 3884: }
 3885: 
 3886: sub contact_titles {
 3887:     my %titles = &Apache::lonlocal::texthash (
 3888:                    'supportemail' => 'Support E-mail address',
 3889:                    'adminemail'   => 'Default Server Admin E-mail address',
 3890:                    'errormail'    => 'Error reports to be e-mailed to',
 3891:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3892:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3893:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3894:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3895:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3896:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3897:                  );
 3898:     my %short_titles = &Apache::lonlocal::texthash (
 3899:                            adminemail   => 'Admin E-mail address',
 3900:                            supportemail => 'Support E-mail',
 3901:                        );   
 3902:     return (\%titles,\%short_titles);
 3903: }
 3904: 
 3905: sub tool_titles {
 3906:     my %titles = &Apache::lonlocal::texthash (
 3907:                      aboutme    => 'Personal web page',
 3908:                      blog       => 'Blog',
 3909:                      webdav     => 'WebDAV',
 3910:                      portfolio  => 'Portfolio',
 3911:                      official   => 'Official courses (with institutional codes)',
 3912:                      unofficial => 'Unofficial courses',
 3913:                      community  => 'Communities',
 3914:                      textbook   => 'Textbook courses',
 3915:                  );
 3916:     return %titles;
 3917: }
 3918: 
 3919: sub courserequest_titles {
 3920:     my %titles = &Apache::lonlocal::texthash (
 3921:                                    official   => 'Official',
 3922:                                    unofficial => 'Unofficial',
 3923:                                    community  => 'Communities',
 3924:                                    textbook   => 'Textbook',
 3925:                                    norequest  => 'Not allowed',
 3926:                                    approval   => 'Approval by Dom. Coord.',
 3927:                                    validate   => 'With validation',
 3928:                                    autolimit  => 'Numerical limit',
 3929:                                    unlimited  => '(blank for unlimited)',
 3930:                  );
 3931:     return %titles;
 3932: }
 3933: 
 3934: sub authorrequest_titles {
 3935:     my %titles = &Apache::lonlocal::texthash (
 3936:                                    norequest  => 'Not allowed',
 3937:                                    approval   => 'Approval by Dom. Coord.',
 3938:                                    automatic  => 'Automatic approval',
 3939:                  );
 3940:     return %titles;
 3941: }
 3942: 
 3943: sub courserequest_conditions {
 3944:     my %conditions = &Apache::lonlocal::texthash (
 3945:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3946:        validate   => '(Processing of request subject to institutional validation).',
 3947:                  );
 3948:     return %conditions;
 3949: }
 3950: 
 3951: 
 3952: sub print_usercreation {
 3953:     my ($position,$dom,$settings,$rowtotal) = @_;
 3954:     my $numinrow = 4;
 3955:     my $datatable;
 3956:     if ($position eq 'top') {
 3957:         $$rowtotal ++;
 3958:         my $rowcount = 0;
 3959:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3960:         if (ref($rules) eq 'HASH') {
 3961:             if (keys(%{$rules}) > 0) {
 3962:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3963:                                                 $ruleorder,$numinrow,$rowcount);
 3964:                 $$rowtotal ++;
 3965:                 $rowcount ++;
 3966:             }
 3967:         }
 3968:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3969:         if (ref($idrules) eq 'HASH') {
 3970:             if (keys(%{$idrules}) > 0) {
 3971:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3972:                                                 $idruleorder,$numinrow,$rowcount);
 3973:                 $$rowtotal ++;
 3974:                 $rowcount ++;
 3975:             }
 3976:         }
 3977:         if ($rowcount == 0) {
 3978:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3979:             $$rowtotal ++;
 3980:             $rowcount ++;
 3981:         }
 3982:     } elsif ($position eq 'middle') {
 3983:         my @creators = ('author','course','requestcrs');
 3984:         my ($rules,$ruleorder) =
 3985:             &Apache::lonnet::inst_userrules($dom,'username');
 3986:         my %lt = &usercreation_types();
 3987:         my %checked;
 3988:         if (ref($settings) eq 'HASH') {
 3989:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3990:                 foreach my $item (@creators) {
 3991:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3992:                 }
 3993:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3994:                 foreach my $item (@creators) {
 3995:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3996:                         $checked{$item} = 'none';
 3997:                     }
 3998:                 }
 3999:             }
 4000:         }
 4001:         my $rownum = 0;
 4002:         foreach my $item (@creators) {
 4003:             $rownum ++;
 4004:             if ($checked{$item} eq '') {
 4005:                 $checked{$item} = 'any';
 4006:             }
 4007:             my $css_class;
 4008:             if ($rownum%2) {
 4009:                 $css_class = '';
 4010:             } else {
 4011:                 $css_class = ' class="LC_odd_row" ';
 4012:             }
 4013:             $datatable .= '<tr'.$css_class.'>'.
 4014:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 4015:                          '</span></td><td align="right">';
 4016:             my @options = ('any');
 4017:             if (ref($rules) eq 'HASH') {
 4018:                 if (keys(%{$rules}) > 0) {
 4019:                     push(@options,('official','unofficial'));
 4020:                 }
 4021:             }
 4022:             push(@options,'none');
 4023:             foreach my $option (@options) {
 4024:                 my $type = 'radio';
 4025:                 my $check = ' ';
 4026:                 if ($checked{$item} eq $option) {
 4027:                     $check = ' checked="checked" ';
 4028:                 } 
 4029:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4030:                               '<input type="'.$type.'" name="can_createuser_'.
 4031:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 4032:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 4033:             }
 4034:             $datatable .= '</td></tr>';
 4035:         }
 4036:     } else {
 4037:         my @contexts = ('author','course','domain');
 4038:         my @authtypes = ('int','krb4','krb5','loc');
 4039:         my %checked;
 4040:         if (ref($settings) eq 'HASH') {
 4041:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 4042:                 foreach my $item (@contexts) {
 4043:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 4044:                         foreach my $auth (@authtypes) {
 4045:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 4046:                                 $checked{$item}{$auth} = ' checked="checked" ';
 4047:                             }
 4048:                         }
 4049:                     }
 4050:                 }
 4051:             }
 4052:         } else {
 4053:             foreach my $item (@contexts) {
 4054:                 foreach my $auth (@authtypes) {
 4055:                     $checked{$item}{$auth} = ' checked="checked" ';
 4056:                 }
 4057:             }
 4058:         }
 4059:         my %title = &context_names();
 4060:         my %authname = &authtype_names();
 4061:         my $rownum = 0;
 4062:         my $css_class; 
 4063:         foreach my $item (@contexts) {
 4064:             if ($rownum%2) {
 4065:                 $css_class = '';
 4066:             } else {
 4067:                 $css_class = ' class="LC_odd_row" ';
 4068:             }
 4069:             $datatable .=   '<tr'.$css_class.'>'.
 4070:                             '<td>'.$title{$item}.
 4071:                             '</td><td class="LC_left_item">'.
 4072:                             '<span class="LC_nobreak">';
 4073:             foreach my $auth (@authtypes) {
 4074:                 $datatable .= '<label>'. 
 4075:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4076:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4077:                               $authname{$auth}.'</label>&nbsp;';
 4078:             }
 4079:             $datatable .= '</span></td></tr>';
 4080:             $rownum ++;
 4081:         }
 4082:         $$rowtotal += $rownum;
 4083:     }
 4084:     return $datatable;
 4085: }
 4086: 
 4087: sub print_selfcreation {
 4088:     my ($position,$dom,$settings,$rowtotal) = @_;
 4089:     my (@selfcreate,$createsettings,$processing,$datatable);
 4090:     if (ref($settings) eq 'HASH') {
 4091:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4092:             $createsettings = $settings->{'cancreate'};
 4093:             if (ref($createsettings) eq 'HASH') {
 4094:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4095:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4096:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4097:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4098:                         @selfcreate = ('email','login','sso');
 4099:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4100:                         @selfcreate = ($createsettings->{'selfcreate'});
 4101:                     }
 4102:                 }
 4103:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4104:                     $processing = $createsettings->{'selfcreateprocessing'};
 4105:                 }
 4106:             }
 4107:         }
 4108:     }
 4109:     my %radiohash;
 4110:     my $numinrow = 4;
 4111:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4112:     if ($position eq 'top') {
 4113:         my %choices = &Apache::lonlocal::texthash (
 4114:                                                       cancreate_login      => 'Institutional Login',
 4115:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4116:                                                   );
 4117:         my @toggles = sort(keys(%choices));
 4118:         my %defaultchecked = (
 4119:                                'cancreate_login' => 'off',
 4120:                                'cancreate_sso'   => 'off',
 4121:                              );
 4122:         my ($onclick,$itemcount);
 4123:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4124:                                                      \%choices,$itemcount,$onclick);
 4125:         $$rowtotal += $itemcount;
 4126:         
 4127:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4128: 
 4129:         if (ref($usertypes) eq 'HASH') {
 4130:             if (keys(%{$usertypes}) > 0) {
 4131:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4132:                                              $dom,$numinrow,$othertitle,
 4133:                                              'statustocreate',$$rowtotal);
 4134:                 $$rowtotal ++;
 4135:             }
 4136:         }
 4137:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 4138:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4139:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 4140:         my $rem;
 4141:         my $numperrow = 2;
 4142:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4143:         $datatable .= '<tr'.$css_class.'>'.
 4144:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 4145:                      '<td class="LC_left_item">'."\n".
 4146:                      '<table><tr><td>'."\n";
 4147:         for (my $i=0; $i<@fields; $i++) {
 4148:             $rem = $i%($numperrow);
 4149:             if ($rem == 0) {
 4150:                 if ($i > 0) {
 4151:                     $datatable .= '</tr>';
 4152:                 }
 4153:                 $datatable .= '<tr>';
 4154:             }
 4155:             my $currval;
 4156:             if (ref($createsettings) eq 'HASH') {
 4157:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 4158:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 4159:                 }
 4160:             }
 4161:             $datatable .= '<td class="LC_left_item">'.
 4162:                           '<span class="LC_nobreak">'.
 4163:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 4164:                           'value="'.$currval.'" size="10" />&nbsp;'.
 4165:                           $fieldtitles{$fields[$i]}.'</span></td>';
 4166:         }
 4167:         my $colsleft = $numperrow - $rem;
 4168:         if ($colsleft > 1 ) {
 4169:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4170:                          '&nbsp;</td>';
 4171:         } elsif ($colsleft == 1) {
 4172:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4173:         }
 4174:         $datatable .= '</tr></table></td></tr>';
 4175:         $$rowtotal ++;
 4176:     } elsif ($position eq 'middle') {
 4177:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4178:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4179:         $usertypes->{'default'} = $othertitle;
 4180:         if (ref($types) eq 'ARRAY') {
 4181:             push(@{$types},'default');
 4182:             $usertypes->{'default'} = $othertitle;
 4183:             foreach my $status (@{$types}) {
 4184:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4185:                                                        $numinrow,$$rowtotal,$usertypes);
 4186:                 $$rowtotal ++;
 4187:             }
 4188:         }
 4189:     } else {
 4190:         my %choices = &Apache::lonlocal::texthash (
 4191:                                                       cancreate_email => 'E-mail address as username',
 4192:                                                   );
 4193:         my @toggles = sort(keys(%choices));
 4194:         my %defaultchecked = (
 4195:                                'cancreate_email' => 'off',
 4196:                              );
 4197:         my $itemcount = 0;
 4198:         my $display = 'none';
 4199:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 4200:             $display = 'block';
 4201:         }
 4202:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 4203:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 4204:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4205:         my $usertypes = {};
 4206:         my $order = [];
 4207:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 4208:             $usertypes = $domdefaults{'inststatustypes'};
 4209:             $order = $domdefaults{'inststatusguest'};
 4210:         }
 4211:         if (ref($order) eq 'ARRAY') {
 4212:             push(@{$order},'default');
 4213:             if (@{$order} > 1) {
 4214:                 $usertypes->{'default'} = &mt('Other users');
 4215:                 $additional .= '<table><tr>';
 4216:                 foreach my $status (@{$order}) {
 4217:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 4218:                 }
 4219:                 $additional .= '</tr><tr>';
 4220:                 foreach my $status (@{$order}) {
 4221:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 4222:                 }
 4223:                 $additional .= '</tr></table>';
 4224:             } else {
 4225:                 $usertypes->{'default'} = &mt('All users');
 4226:                 $additional .= &email_as_username($rowtotal,$processing);
 4227:             }
 4228:         }
 4229:         $additional .= '</div>'."\n";
 4230: 
 4231:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4232:                                                      \%choices,$$rowtotal,$onclick,$additional);
 4233:         $$rowtotal ++;
 4234:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4235:         $$rowtotal ++;
 4236:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4237:         $numinrow = 1;
 4238:         if (ref($order) eq 'ARRAY') {
 4239:             foreach my $status (@{$order}) {
 4240:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4241:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4242:                 $$rowtotal ++;
 4243:             }
 4244:         }
 4245:         my ($emailrules,$emailruleorder) =
 4246:             &Apache::lonnet::inst_userrules($dom,'email');
 4247:         if (ref($emailrules) eq 'HASH') {
 4248:             if (keys(%{$emailrules}) > 0) {
 4249:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4250:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4251:                 $$rowtotal ++;
 4252:             }
 4253:         }
 4254:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4255:     }
 4256:     return $datatable;
 4257: }
 4258: 
 4259: sub email_as_username {
 4260:     my ($rowtotal,$processing,$type) = @_;
 4261:     my %choices =
 4262:         &Apache::lonlocal::texthash (
 4263:                                       automatic => 'Automatic approval',
 4264:                                       approval  => 'Queued for approval',
 4265:                                     );
 4266:     my $output;
 4267:     foreach my $option ('automatic','approval') {
 4268:         my $checked;
 4269:         if (ref($processing) eq 'HASH') {
 4270:             if ($type eq '') {   
 4271:                 if (!exists($processing->{'default'})) {
 4272:                     if ($option eq 'automatic') {
 4273:                         $checked = ' checked="checked"';
 4274:                     }
 4275:                 } else {
 4276:                     if ($processing->{'default'} eq $option) {
 4277:                         $checked = ' checked="checked"';
 4278:                     }
 4279:                 }
 4280:             } else {
 4281:                 if (!exists($processing->{$type})) {
 4282:                     if ($option eq 'automatic') {
 4283:                         $checked = ' checked="checked"';
 4284:                     }
 4285:                 } else {
 4286:                     if ($processing->{$type} eq $option) {
 4287:                         $checked = ' checked="checked"';
 4288:                     }
 4289:                 }
 4290:             }
 4291:         } elsif ($option eq 'automatic') {
 4292:             $checked = ' checked="checked"'; 
 4293:         }
 4294:         my $name = 'cancreate_emailprocess';
 4295:         if (($type ne '') && ($type ne 'default')) {
 4296:             $name .= '_'.$type;
 4297:         }
 4298:         $output .= '<span class="LC_nobreak"><label>'.
 4299:                    '<input type="radio" name="'.$name.'"'.
 4300:                    $checked.' value="'.$option.'" />'.
 4301:                    $choices{$option}.'</label></span>';
 4302:         if ($type eq '') {
 4303:             $output .= '&nbsp;';
 4304:         } else {
 4305:             $output .= '<br />';
 4306:         }
 4307:     }
 4308:     $$rowtotal ++;
 4309:     return $output;
 4310: }
 4311: 
 4312: sub captcha_choice {
 4313:     my ($context,$settings,$itemcount) = @_;
 4314:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4315:     my %lt = &captcha_phrases();
 4316:     $keyentry = 'hidden';
 4317:     if ($context eq 'cancreate') {
 4318:         $rowname = &mt('CAPTCHA validation');
 4319:     } elsif ($context eq 'login') {
 4320:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4321:     }
 4322:     if (ref($settings) eq 'HASH') {
 4323:         if ($settings->{'captcha'}) {
 4324:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4325:         } else {
 4326:             $checked{'original'} = ' checked="checked"';
 4327:         }
 4328:         if ($settings->{'captcha'} eq 'recaptcha') {
 4329:             $pubtext = $lt{'pub'};
 4330:             $privtext = $lt{'priv'};
 4331:             $keyentry = 'text';
 4332:         }
 4333:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4334:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4335:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4336:         }
 4337:     } else {
 4338:         $checked{'original'} = ' checked="checked"';
 4339:     }
 4340:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4341:     my $output = '<tr'.$css_class.'>'.
 4342:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4343:                  '<table><tr><td>'."\n";
 4344:     foreach my $option ('original','recaptcha','notused') {
 4345:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4346:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4347:                    $lt{$option}.'</label></span>';
 4348:         unless ($option eq 'notused') {
 4349:             $output .= ('&nbsp;'x2)."\n";
 4350:         }
 4351:     }
 4352: #
 4353: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4354: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4355: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4356: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4357: #
 4358:     $output .= '</td></tr>'."\n".
 4359:                '<tr><td>'."\n".
 4360:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4361:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4362:                $currpub.'" size="40" /></span><br />'."\n".
 4363:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4364:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4365:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4366:                '</td></tr>';
 4367:     return $output;
 4368: }
 4369: 
 4370: sub user_formats_row {
 4371:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4372:     my $output;
 4373:     my %text = (
 4374:                    'username' => 'new usernames',
 4375:                    'id'       => 'IDs',
 4376:                    'email'    => 'self-created accounts (e-mail)',
 4377:                );
 4378:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4379:     $output = '<tr '.$css_class.'>'.
 4380:               '<td><span class="LC_nobreak">';
 4381:     if ($type eq 'email') {
 4382:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4383:     } else {
 4384:         $output .= &mt("Format rules to check for $text{$type}: ");
 4385:     }
 4386:     $output .= '</span></td>'.
 4387:                '<td class="LC_left_item" colspan="2"><table>';
 4388:     my $rem;
 4389:     if (ref($ruleorder) eq 'ARRAY') {
 4390:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4391:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4392:                 my $rem = $i%($numinrow);
 4393:                 if ($rem == 0) {
 4394:                     if ($i > 0) {
 4395:                         $output .= '</tr>';
 4396:                     }
 4397:                     $output .= '<tr>';
 4398:                 }
 4399:                 my $check = ' ';
 4400:                 if (ref($settings) eq 'HASH') {
 4401:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4402:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4403:                             $check = ' checked="checked" ';
 4404:                         }
 4405:                     }
 4406:                 }
 4407:                 $output .= '<td class="LC_left_item">'.
 4408:                            '<span class="LC_nobreak"><label>'.
 4409:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4410:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4411:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4412:             }
 4413:         }
 4414:         $rem = @{$ruleorder}%($numinrow);
 4415:     }
 4416:     my $colsleft = $numinrow - $rem;
 4417:     if ($colsleft > 1 ) {
 4418:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4419:                    '&nbsp;</td>';
 4420:     } elsif ($colsleft == 1) {
 4421:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4422:     }
 4423:     $output .= '</tr></table></td></tr>';
 4424:     return $output;
 4425: }
 4426: 
 4427: sub usercreation_types {
 4428:     my %lt = &Apache::lonlocal::texthash (
 4429:                     author     => 'When adding a co-author',
 4430:                     course     => 'When adding a user to a course',
 4431:                     requestcrs => 'When requesting a course',
 4432:                     any        => 'Any',
 4433:                     official   => 'Institutional only ',
 4434:                     unofficial => 'Non-institutional only',
 4435:                     none       => 'None',
 4436:     );
 4437:     return %lt;
 4438: }
 4439: 
 4440: sub selfcreation_types {
 4441:     my %lt = &Apache::lonlocal::texthash (
 4442:                     selfcreate => 'User creates own account',
 4443:                     any        => 'Any',
 4444:                     official   => 'Institutional only ',
 4445:                     unofficial => 'Non-institutional only',
 4446:                     email      => 'E-mail address',
 4447:                     login      => 'Institutional Login',
 4448:                     sso        => 'SSO',
 4449:              );
 4450: }
 4451: 
 4452: sub authtype_names {
 4453:     my %lt = &Apache::lonlocal::texthash(
 4454:                       int    => 'Internal',
 4455:                       krb4   => 'Kerberos 4',
 4456:                       krb5   => 'Kerberos 5',
 4457:                       loc    => 'Local',
 4458:                   );
 4459:     return %lt;
 4460: }
 4461: 
 4462: sub context_names {
 4463:     my %context_title = &Apache::lonlocal::texthash(
 4464:        author => 'Creating users when an Author',
 4465:        course => 'Creating users when in a course',
 4466:        domain => 'Creating users when a Domain Coordinator',
 4467:     );
 4468:     return %context_title;
 4469: }
 4470: 
 4471: sub print_usermodification {
 4472:     my ($position,$dom,$settings,$rowtotal) = @_;
 4473:     my $numinrow = 4;
 4474:     my ($context,$datatable,$rowcount);
 4475:     if ($position eq 'top') {
 4476:         $rowcount = 0;
 4477:         $context = 'author'; 
 4478:         foreach my $role ('ca','aa') {
 4479:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4480:                                                    $numinrow,$rowcount);
 4481:             $$rowtotal ++;
 4482:             $rowcount ++;
 4483:         }
 4484:     } elsif ($position eq 'bottom') {
 4485:         $context = 'course';
 4486:         $rowcount = 0;
 4487:         foreach my $role ('st','ep','ta','in','cr') {
 4488:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4489:                                                    $numinrow,$rowcount);
 4490:             $$rowtotal ++;
 4491:             $rowcount ++;
 4492:         }
 4493:     }
 4494:     return $datatable;
 4495: }
 4496: 
 4497: sub print_defaults {
 4498:     my ($position,$dom,$settings,$rowtotal) = @_;
 4499:     my $rownum = 0;
 4500:     my ($datatable,$css_class);
 4501:     if ($position eq 'top') {
 4502:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4503:                      'datelocale_def','portal_def');
 4504:         my %defaults;
 4505:         if (ref($settings) eq 'HASH') {
 4506:             %defaults = %{$settings};
 4507:         } else {
 4508:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4509:             foreach my $item (@items) {
 4510:                 $defaults{$item} = $domdefaults{$item};
 4511:             }
 4512:         }
 4513:         my $titles = &defaults_titles($dom);
 4514:         foreach my $item (@items) {
 4515:             if ($rownum%2) {
 4516:                 $css_class = '';
 4517:             } else {
 4518:                 $css_class = ' class="LC_odd_row" ';
 4519:             }
 4520:             $datatable .= '<tr'.$css_class.'>'.
 4521:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4522:                           '</span></td><td class="LC_right_item" colspan="3">';
 4523:             if ($item eq 'auth_def') {
 4524:                 my @authtypes = ('internal','krb4','krb5','localauth');
 4525:                 my %shortauth = (
 4526:                                  internal => 'int',
 4527:                                  krb4 => 'krb4',
 4528:                                  krb5 => 'krb5',
 4529:                                  localauth  => 'loc'
 4530:                                 );
 4531:                 my %authnames = &authtype_names();
 4532:                 foreach my $auth (@authtypes) {
 4533:                     my $checked = ' ';
 4534:                     if ($defaults{$item} eq $auth) {
 4535:                         $checked = ' checked="checked" ';
 4536:                     }
 4537:                     $datatable .= '<label><input type="radio" name="'.$item.
 4538:                                   '" value="'.$auth.'"'.$checked.'/>'.
 4539:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4540:                 }
 4541:             } elsif ($item eq 'timezone_def') {
 4542:                 my $includeempty = 1;
 4543:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4544:             } elsif ($item eq 'datelocale_def') {
 4545:                 my $includeempty = 1;
 4546:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4547:             } elsif ($item eq 'lang_def') {
 4548:                 my %langchoices = &get_languages_hash();
 4549:                 $langchoices{''} = 'No language preference';
 4550:                 %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4551:                 $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4552:                                                               \%langchoices);
 4553:             } else {
 4554:                 my $size;
 4555:                 if ($item eq 'portal_def') {
 4556:                     $size = ' size="25"';
 4557:                 }
 4558:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4559:                               $defaults{$item}.'"'.$size.' />';
 4560:             }
 4561:             $datatable .= '</td></tr>';
 4562:             $rownum ++;
 4563:         }
 4564:     } else {
 4565:         my (%defaults);
 4566:         if (ref($settings) eq 'HASH') {
 4567:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 4568:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 4569:                 my $maxnum = @{$settings->{'inststatusorder'}};
 4570:                 for (my $i=0; $i<$maxnum; $i++) {
 4571:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 4572:                     my $item = $settings->{'inststatusorder'}->[$i];
 4573:                     my $title = $settings->{'inststatustypes'}->{$item};
 4574:                     my $guestok;
 4575:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 4576:                         $guestok = 1;
 4577:                     }
 4578:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 4579:                     $datatable .= '<tr'.$css_class.'>'.
 4580:                                   '<td><span class="LC_nobreak">'.
 4581:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 4582:                     for (my $k=0; $k<=$maxnum; $k++) {
 4583:                         my $vpos = $k+1;
 4584:                         my $selstr;
 4585:                         if ($k == $i) {
 4586:                             $selstr = ' selected="selected" ';
 4587:                         }
 4588:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4589:                     }
 4590:                     my ($checkedon,$checkedoff);
 4591:                     $checkedoff = ' checked="checked"';
 4592:                     if ($guestok) {
 4593:                         $checkedon = $checkedoff;
 4594:                         $checkedoff = ''; 
 4595:                     }
 4596:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 4597:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 4598:                                   &mt('delete').'</span></td>'.
 4599:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 4600:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 4601:                                   '</span></td>'.
 4602:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4603:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 4604:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 4605:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 4606:                                   &mt('No').'</label></span></td></tr>';
 4607:                 }
 4608:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 4609:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 4610:                 $datatable .= '<tr '.$css_class.'>'.
 4611:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 4612:                 for (my $k=0; $k<=$maxnum; $k++) {
 4613:                     my $vpos = $k+1;
 4614:                     my $selstr;
 4615:                     if ($k == $maxnum) {
 4616:                         $selstr = ' selected="selected" ';
 4617:                     }
 4618:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4619:                 }
 4620:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 4621:                               '<input type="text" size="10" name="addinststatus" value="" /></span>'.
 4622:                               '&nbsp;'.&mt('(new)').
 4623:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 4624:                               &mt('Name displayed:').
 4625:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 4626:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 4627:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 4628:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 4629:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 4630:                               &mt('No').'</label></span></td></tr>';
 4631:                               '</tr>'."\n";
 4632:                 $rownum ++;
 4633:             }
 4634:         }
 4635:     }
 4636:     $$rowtotal += $rownum;
 4637:     return $datatable;
 4638: }
 4639: 
 4640: sub get_languages_hash {
 4641:     my %langchoices;
 4642:     foreach my $id (&Apache::loncommon::languageids()) {
 4643:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4644:         if ($code ne '') {
 4645:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4646:         }
 4647:     }
 4648:     return %langchoices;
 4649: }
 4650: 
 4651: sub defaults_titles {
 4652:     my ($dom) = @_;
 4653:     my %titles = &Apache::lonlocal::texthash (
 4654:                    'auth_def'      => 'Default authentication type',
 4655:                    'auth_arg_def'  => 'Default authentication argument',
 4656:                    'lang_def'      => 'Default language',
 4657:                    'timezone_def'  => 'Default timezone',
 4658:                    'datelocale_def' => 'Default locale for dates',
 4659:                    'portal_def'     => 'Portal/Default URL',
 4660:                  );
 4661:     if ($dom) {
 4662:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4663:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4664:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4665:         $protocol = 'http' if ($protocol ne 'https');
 4666:         if ($uint_dom) {
 4667:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4668:                                          $uint_dom);
 4669:         }
 4670:     }
 4671:     return (\%titles);
 4672: }
 4673: 
 4674: sub print_scantronformat {
 4675:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4676:     my $itemcount = 1;
 4677:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4678:         %confhash);
 4679:     my $switchserver = &check_switchserver($dom,$confname);
 4680:     my %lt = &Apache::lonlocal::texthash (
 4681:                 default => 'Default bubblesheet format file error',
 4682:                 custom  => 'Custom bubblesheet format file error',
 4683:              );
 4684:     my %scantronfiles = (
 4685:         default => 'default.tab',
 4686:         custom => 'custom.tab',
 4687:     );
 4688:     foreach my $key (keys(%scantronfiles)) {
 4689:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4690:                               .$scantronfiles{$key};
 4691:     }
 4692:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4693:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4694:         if (!$switchserver) {
 4695:             my $servadm = $r->dir_config('lonAdmEMail');
 4696:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4697:             if ($configuserok eq 'ok') {
 4698:                 if ($author_ok eq 'ok') {
 4699:                     my %legacyfile = (
 4700:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4701:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4702:                     );
 4703:                     my %md5chk;
 4704:                     foreach my $type (keys(%legacyfile)) {
 4705:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4706:                         chomp($md5chk{$type});
 4707:                     }
 4708:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4709:                         foreach my $type (keys(%legacyfile)) {
 4710:                             ($scantronurls{$type},my $error) = 
 4711:                                 &legacy_scantronformat($r,$dom,$confname,
 4712:                                                  $type,$legacyfile{$type},
 4713:                                                  $scantronurls{$type},
 4714:                                                  $scantronfiles{$type});
 4715:                             if ($error ne '') {
 4716:                                 $error{$type} = $error;
 4717:                             }
 4718:                         }
 4719:                         if (keys(%error) == 0) {
 4720:                             $is_custom = 1;
 4721:                             $confhash{'scantron'}{'scantronformat'} = 
 4722:                                 $scantronurls{'custom'};
 4723:                             my $putresult = 
 4724:                                 &Apache::lonnet::put_dom('configuration',
 4725:                                                          \%confhash,$dom);
 4726:                             if ($putresult ne 'ok') {
 4727:                                 $error{'custom'} = 
 4728:                                     '<span class="LC_error">'.
 4729:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4730:                             }
 4731:                         }
 4732:                     } else {
 4733:                         ($scantronurls{'default'},my $error) =
 4734:                             &legacy_scantronformat($r,$dom,$confname,
 4735:                                           'default',$legacyfile{'default'},
 4736:                                           $scantronurls{'default'},
 4737:                                           $scantronfiles{'default'});
 4738:                         if ($error eq '') {
 4739:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4740:                             my $putresult =
 4741:                                 &Apache::lonnet::put_dom('configuration',
 4742:                                                          \%confhash,$dom);
 4743:                             if ($putresult ne 'ok') {
 4744:                                 $error{'default'} =
 4745:                                     '<span class="LC_error">'.
 4746:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4747:                             }
 4748:                         } else {
 4749:                             $error{'default'} = $error;
 4750:                         }
 4751:                     }
 4752:                 }
 4753:             }
 4754:         } else {
 4755:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4756:         }
 4757:     }
 4758:     if (ref($settings) eq 'HASH') {
 4759:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4760:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4761:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4762:                 $scantronurl = '';
 4763:             } else {
 4764:                 $scantronurl = $settings->{'scantronformat'};
 4765:             }
 4766:             $is_custom = 1;
 4767:         } else {
 4768:             $scantronurl = $scantronurls{'default'};
 4769:         }
 4770:     } else {
 4771:         if ($is_custom) {
 4772:             $scantronurl = $scantronurls{'custom'};
 4773:         } else {
 4774:             $scantronurl = $scantronurls{'default'};
 4775:         }
 4776:     }
 4777:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4778:     $datatable .= '<tr'.$css_class.'>';
 4779:     if (!$is_custom) {
 4780:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4781:                       '<span class="LC_nobreak">';
 4782:         if ($scantronurl) {
 4783:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4784:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4785:         } else {
 4786:             $datatable = &mt('File unavailable for display');
 4787:         }
 4788:         $datatable .= '</span></td>';
 4789:         if (keys(%error) == 0) { 
 4790:             $datatable .= '<td valign="bottom">';
 4791:             if (!$switchserver) {
 4792:                 $datatable .= &mt('Upload:').'<br />';
 4793:             }
 4794:         } else {
 4795:             my $errorstr;
 4796:             foreach my $key (sort(keys(%error))) {
 4797:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4798:             }
 4799:             $datatable .= '<td>'.$errorstr;
 4800:         }
 4801:     } else {
 4802:         if (keys(%error) > 0) {
 4803:             my $errorstr;
 4804:             foreach my $key (sort(keys(%error))) {
 4805:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4806:             } 
 4807:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4808:         } elsif ($scantronurl) {
 4809:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4810:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4811:             $datatable .= '<td><span class="LC_nobreak">'.
 4812:                           $link.
 4813:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4814:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4815:                           '<td><span class="LC_nobreak">&nbsp;'.
 4816:                           &mt('Replace:').'</span><br />';
 4817:         }
 4818:     }
 4819:     if (keys(%error) == 0) {
 4820:         if ($switchserver) {
 4821:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4822:         } else {
 4823:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4824:                          '<input type="file" name="scantronformat" /></span>';
 4825:         }
 4826:     }
 4827:     $datatable .= '</td></tr>';
 4828:     $$rowtotal ++;
 4829:     return $datatable;
 4830: }
 4831: 
 4832: sub legacy_scantronformat {
 4833:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4834:     my ($url,$error);
 4835:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4836:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4837:         (my $result,$url) =
 4838:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4839:                          '','',$newfile);
 4840:         if ($result ne 'ok') {
 4841:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4842:         }
 4843:     }
 4844:     return ($url,$error);
 4845: }
 4846: 
 4847: sub print_coursecategories {
 4848:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4849:     my $datatable;
 4850:     if ($position eq 'top') {
 4851:         my (%checked);
 4852:         my @catitems = ('unauth','auth');
 4853:         my @cattypes = ('std','domonly','codesrch','none');
 4854:         $checked{'unauth'} = 'std';
 4855:         $checked{'auth'} = 'std';
 4856:         if (ref($settings) eq 'HASH') {
 4857:             foreach my $type (@cattypes) {
 4858:                 if ($type eq $settings->{'unauth'}) {
 4859:                     $checked{'unauth'} = $type;
 4860:                 }
 4861:                 if ($type eq $settings->{'auth'}) {
 4862:                     $checked{'auth'} = $type;
 4863:                 }
 4864:             }
 4865:         }
 4866:         my %lt = &Apache::lonlocal::texthash (
 4867:                                                unauth   => 'Catalog type for unauthenticated users',
 4868:                                                auth     => 'Catalog type for authenticated users',
 4869:                                                none     => 'No catalog',
 4870:                                                std      => 'Standard catalog',
 4871:                                                domonly  => 'Domain-only catalog',
 4872:                                                codesrch => "Code search form",
 4873:                                              );
 4874:        my $itemcount = 0;
 4875:        foreach my $item (@catitems) {
 4876:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 4877:            $datatable .= '<tr '.$css_class.'>'.
 4878:                          '<td>'.$lt{$item}.'</td>'.
 4879:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 4880:            foreach my $type (@cattypes) {
 4881:                my $ischecked;
 4882:                if ($checked{$item} eq $type) {
 4883:                    $ischecked=' checked="checked"';
 4884:                }
 4885:                $datatable .= '<label>'.
 4886:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 4887:                              ' />'.$lt{$type}.'</label>&nbsp;';
 4888:            }
 4889:            $datatable .= '</td></tr>';
 4890:            $itemcount ++;
 4891:         }
 4892:         $$rowtotal += $itemcount;
 4893:     } elsif ($position eq 'middle') {
 4894:         my $toggle_cats_crs = ' ';
 4895:         my $toggle_cats_dom = ' checked="checked" ';
 4896:         my $can_cat_crs = ' ';
 4897:         my $can_cat_dom = ' checked="checked" ';
 4898:         my $toggle_catscomm_comm = ' ';
 4899:         my $toggle_catscomm_dom = ' checked="checked" ';
 4900:         my $can_catcomm_comm = ' ';
 4901:         my $can_catcomm_dom = ' checked="checked" ';
 4902: 
 4903:         if (ref($settings) eq 'HASH') {
 4904:             if ($settings->{'togglecats'} eq 'crs') {
 4905:                 $toggle_cats_crs = $toggle_cats_dom;
 4906:                 $toggle_cats_dom = ' ';
 4907:             }
 4908:             if ($settings->{'categorize'} eq 'crs') {
 4909:                 $can_cat_crs = $can_cat_dom;
 4910:                 $can_cat_dom = ' ';
 4911:             }
 4912:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4913:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4914:                 $toggle_catscomm_dom = ' ';
 4915:             }
 4916:             if ($settings->{'categorizecomm'} eq 'comm') {
 4917:                 $can_catcomm_comm = $can_catcomm_dom;
 4918:                 $can_catcomm_dom = ' ';
 4919:             }
 4920:         }
 4921:         my %title = &Apache::lonlocal::texthash (
 4922:                      togglecats     => 'Show/Hide a course in catalog',
 4923:                      togglecatscomm => 'Show/Hide a community in catalog',
 4924:                      categorize     => 'Assign a category to a course',
 4925:                      categorizecomm => 'Assign a category to a community',
 4926:                     );
 4927:         my %level = &Apache::lonlocal::texthash (
 4928:                      dom  => 'Set in Domain',
 4929:                      crs  => 'Set in Course',
 4930:                      comm => 'Set in Community',
 4931:                     );
 4932:         $datatable = '<tr class="LC_odd_row">'.
 4933:                   '<td>'.$title{'togglecats'}.'</td>'.
 4934:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4935:                   '<input type="radio" name="togglecats"'.
 4936:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4937:                   '<label><input type="radio" name="togglecats"'.
 4938:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4939:                   '</tr><tr>'.
 4940:                   '<td>'.$title{'categorize'}.'</td>'.
 4941:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4942:                   '<label><input type="radio" name="categorize"'.
 4943:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4944:                   '<label><input type="radio" name="categorize"'.
 4945:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4946:                   '</tr><tr class="LC_odd_row">'.
 4947:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4948:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4949:                   '<input type="radio" name="togglecatscomm"'.
 4950:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4951:                   '<label><input type="radio" name="togglecatscomm"'.
 4952:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4953:                   '</tr><tr>'.
 4954:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4955:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4956:                   '<label><input type="radio" name="categorizecomm"'.
 4957:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4958:                   '<label><input type="radio" name="categorizecomm"'.
 4959:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4960:                   '</tr>';
 4961:         $$rowtotal += 4;
 4962:     } else {
 4963:         my $css_class;
 4964:         my $itemcount = 1;
 4965:         my $cathash; 
 4966:         if (ref($settings) eq 'HASH') {
 4967:             $cathash = $settings->{'cats'};
 4968:         }
 4969:         if (ref($cathash) eq 'HASH') {
 4970:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4971:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4972:                                                    \%allitems,\%idx,\@jsarray);
 4973:             my $maxdepth = scalar(@cats);
 4974:             my $colattrib = '';
 4975:             if ($maxdepth > 2) {
 4976:                 $colattrib = ' colspan="2" ';
 4977:             }
 4978:             my @path;
 4979:             if (@cats > 0) {
 4980:                 if (ref($cats[0]) eq 'ARRAY') {
 4981:                     my $numtop = @{$cats[0]};
 4982:                     my $maxnum = $numtop;
 4983:                     my %default_names = (
 4984:                           instcode    => &mt('Official courses'),
 4985:                           communities => &mt('Communities'),
 4986:                     );
 4987: 
 4988:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4989:                         ($cathash->{'instcode::0'} eq '') ||
 4990:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4991:                         ($cathash->{'communities::0'} eq '')) {
 4992:                         $maxnum ++;
 4993:                     }
 4994:                     my $lastidx;
 4995:                     for (my $i=0; $i<$numtop; $i++) {
 4996:                         my $parent = $cats[0][$i];
 4997:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4998:                         my $item = &escape($parent).'::0';
 4999:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 5000:                         $lastidx = $idx{$item};
 5001:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5002:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 5003:                         for (my $k=0; $k<=$maxnum; $k++) {
 5004:                             my $vpos = $k+1;
 5005:                             my $selstr;
 5006:                             if ($k == $i) {
 5007:                                 $selstr = ' selected="selected" ';
 5008:                             }
 5009:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5010:                         }
 5011:                         $datatable .= '</select></span></td><td>';
 5012:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 5013:                             $datatable .=  '<span class="LC_nobreak">'
 5014:                                            .$default_names{$parent}.'</span>';
 5015:                             if ($parent eq 'instcode') {
 5016:                                 $datatable .= '<br /><span class="LC_nobreak">('
 5017:                                               .&mt('with institutional codes')
 5018:                                               .')</span></td><td'.$colattrib.'>';
 5019:                             } else {
 5020:                                 $datatable .= '<table><tr><td>';
 5021:                             }
 5022:                             $datatable .= '<span class="LC_nobreak">'
 5023:                                           .'<label><input type="radio" name="'
 5024:                                           .$parent.'" value="1" checked="checked" />'
 5025:                                           .&mt('Display').'</label>';
 5026:                             if ($parent eq 'instcode') {
 5027:                                 $datatable .= '&nbsp;';
 5028:                             } else {
 5029:                                 $datatable .= '</span></td></tr><tr><td>'
 5030:                                               .'<span class="LC_nobreak">';
 5031:                             }
 5032:                             $datatable .= '<label><input type="radio" name="'
 5033:                                           .$parent.'" value="0" />'
 5034:                                           .&mt('Do not display').'</label></span>';
 5035:                             if ($parent eq 'communities') {
 5036:                                 $datatable .= '</td></tr></table>';
 5037:                             }
 5038:                             $datatable .= '</td>';
 5039:                         } else {
 5040:                             $datatable .= $parent
 5041:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 5042:                                           .'<input type="checkbox" name="deletecategory" '
 5043:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 5044:                         }
 5045:                         my $depth = 1;
 5046:                         push(@path,$parent);
 5047:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 5048:                         pop(@path);
 5049:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 5050:                         $itemcount ++;
 5051:                     }
 5052:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5053:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 5054:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 5055:                     for (my $k=0; $k<=$maxnum; $k++) {
 5056:                         my $vpos = $k+1;
 5057:                         my $selstr;
 5058:                         if ($k == $numtop) {
 5059:                             $selstr = ' selected="selected" ';
 5060:                         }
 5061:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5062:                     }
 5063:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 5064:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 5065:                                   .'</tr>'."\n";
 5066:                     $itemcount ++;
 5067:                     foreach my $default ('instcode','communities') {
 5068:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 5069:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5070:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 5071:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 5072:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5073:                             for (my $k=0; $k<=$maxnum; $k++) {
 5074:                                 my $vpos = $k+1;
 5075:                                 my $selstr;
 5076:                                 if ($k == $maxnum) {
 5077:                                     $selstr = ' selected="selected" ';
 5078:                                 }
 5079:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5080:                             }
 5081:                             $datatable .= '</select></span></td>'.
 5082:                                           '<td><span class="LC_nobreak">'.
 5083:                                           $default_names{$default}.'</span>';
 5084:                             if ($default eq 'instcode') {
 5085:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5086:                                               .&mt('with institutional codes').')</span>';
 5087:                             }
 5088:                             $datatable .= '</td>'
 5089:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5090:                                           .&mt('Display').'</label>&nbsp;'
 5091:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5092:                                           .&mt('Do not display').'</label></span></td></tr>';
 5093:                         }
 5094:                     }
 5095:                 }
 5096:             } else {
 5097:                 $datatable .= &initialize_categories($itemcount);
 5098:             }
 5099:         } else {
 5100:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 5101:                           .&initialize_categories($itemcount);
 5102:         }
 5103:         $$rowtotal += $itemcount;
 5104:     }
 5105:     return $datatable;
 5106: }
 5107: 
 5108: sub print_serverstatuses {
 5109:     my ($dom,$settings,$rowtotal) = @_;
 5110:     my $datatable;
 5111:     my @pages = &serverstatus_pages();
 5112:     my (%namedaccess,%machineaccess);
 5113:     foreach my $type (@pages) {
 5114:         $namedaccess{$type} = '';
 5115:         $machineaccess{$type}= '';
 5116:     }
 5117:     if (ref($settings) eq 'HASH') {
 5118:         foreach my $type (@pages) {
 5119:             if (exists($settings->{$type})) {
 5120:                 if (ref($settings->{$type}) eq 'HASH') {
 5121:                     foreach my $key (keys(%{$settings->{$type}})) {
 5122:                         if ($key eq 'namedusers') {
 5123:                             $namedaccess{$type} = $settings->{$type}->{$key};
 5124:                         } elsif ($key eq 'machines') {
 5125:                             $machineaccess{$type} = $settings->{$type}->{$key};
 5126:                         }
 5127:                     }
 5128:                 }
 5129:             }
 5130:         }
 5131:     }
 5132:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 5133:     my $rownum = 0;
 5134:     my $css_class;
 5135:     foreach my $type (@pages) {
 5136:         $rownum ++;
 5137:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 5138:         $datatable .= '<tr'.$css_class.'>'.
 5139:                       '<td><span class="LC_nobreak">'.
 5140:                       $titles->{$type}.'</span></td>'.
 5141:                       '<td class="LC_left_item">'.
 5142:                       '<input type="text" name="'.$type.'_namedusers" '.
 5143:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 5144:                       '<td class="LC_right_item">'.
 5145:                       '<span class="LC_nobreak">'.
 5146:                       '<input type="text" name="'.$type.'_machines" '.
 5147:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 5148:                       '</td></tr>'."\n";
 5149:     }
 5150:     $$rowtotal += $rownum;
 5151:     return $datatable;
 5152: }
 5153: 
 5154: sub serverstatus_pages {
 5155:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 5156:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 5157:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 5158:             'uniquecodes','diskusage');
 5159: }
 5160: 
 5161: sub defaults_javascript {
 5162:     my ($settings) = @_;
 5163:     return unless (ref($settings) eq 'HASH');
 5164:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5165:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 5166:         if ($maxnum eq '') {
 5167:             $maxnum = 0;
 5168:         }
 5169:         $maxnum ++;
 5170:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 5171:         return <<"ENDSCRIPT";
 5172: <script type="text/javascript">
 5173: // <![CDATA[
 5174: function reorderTypes(form,caller) {
 5175:     var changedVal;
 5176: $jstext 
 5177:     var newpos = 'addinststatus_pos';
 5178:     var current = new Array;
 5179:     var maxh = $maxnum;
 5180:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5181:     var oldVal;
 5182:     if (caller == newpos) {
 5183:         changedVal = newitemVal;
 5184:     } else {
 5185:         var curritem = 'inststatus_pos_'+caller;
 5186:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 5187:         current[newitemVal] = newpos;
 5188:     }
 5189:     for (var i=0; i<inststatuses.length; i++) {
 5190:         if (inststatuses[i] != caller) {
 5191:             var elementName = 'inststatus_pos_'+inststatuses[i];
 5192:             if (form.elements[elementName]) {
 5193:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5194:                 current[currVal] = elementName;
 5195:             }
 5196:         }
 5197:     }
 5198:     for (var j=0; j<maxh; j++) {
 5199:         if (current[j] == undefined) {
 5200:             oldVal = j;
 5201:         }
 5202:     }
 5203:     if (oldVal < changedVal) {
 5204:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5205:            var elementName = current[k];
 5206:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5207:         }
 5208:     } else {
 5209:         for (var k=changedVal; k<oldVal; k++) {
 5210:             var elementName = current[k];
 5211:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5212:         }
 5213:     }
 5214:     return;
 5215: }
 5216: 
 5217: // ]]>
 5218: </script>
 5219: 
 5220: ENDSCRIPT
 5221:     }
 5222: }
 5223: 
 5224: sub coursecategories_javascript {
 5225:     my ($settings) = @_;
 5226:     my ($output,$jstext,$cathash);
 5227:     if (ref($settings) eq 'HASH') {
 5228:         $cathash = $settings->{'cats'};
 5229:     }
 5230:     if (ref($cathash) eq 'HASH') {
 5231:         my (@cats,@jsarray,%idx);
 5232:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 5233:         if (@jsarray > 0) {
 5234:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 5235:             for (my $i=0; $i<@jsarray; $i++) {
 5236:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 5237:                     my $catstr = join('","',@{$jsarray[$i]});
 5238:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 5239:                 }
 5240:             }
 5241:         }
 5242:     } else {
 5243:         $jstext  = '    var categories = Array(1);'."\n".
 5244:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 5245:     }
 5246:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 5247:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 5248:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category.'); 
 5249:     $output = <<"ENDSCRIPT";
 5250: <script type="text/javascript">
 5251: // <![CDATA[
 5252: function reorderCats(form,parent,item,idx) {
 5253:     var changedVal;
 5254: $jstext
 5255:     var newpos = 'addcategory_pos';
 5256:     if (parent == '') {
 5257:         var has_instcode = 0;
 5258:         var maxtop = categories[idx].length;
 5259:         for (var j=0; j<maxtop; j++) {
 5260:             if (categories[idx][j] == 'instcode::0') {
 5261:                 has_instcode == 1;
 5262:             }
 5263:         }
 5264:         if (has_instcode == 0) {
 5265:             categories[idx][maxtop] = 'instcode_pos';
 5266:         }
 5267:     } else {
 5268:         newpos += '_'+parent;
 5269:     }
 5270:     var maxh = 1 + categories[idx].length;
 5271:     var current = new Array;
 5272:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5273:     if (item == newpos) {
 5274:         changedVal = newitemVal;
 5275:     } else {
 5276:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5277:         current[newitemVal] = newpos;
 5278:     }
 5279:     for (var i=0; i<categories[idx].length; i++) {
 5280:         var elementName = categories[idx][i];
 5281:         if (elementName != item) {
 5282:             if (form.elements[elementName]) {
 5283:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5284:                 current[currVal] = elementName;
 5285:             }
 5286:         }
 5287:     }
 5288:     var oldVal;
 5289:     for (var j=0; j<maxh; j++) {
 5290:         if (current[j] == undefined) {
 5291:             oldVal = j;
 5292:         }
 5293:     }
 5294:     if (oldVal < changedVal) {
 5295:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5296:            var elementName = current[k];
 5297:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5298:         }
 5299:     } else {
 5300:         for (var k=changedVal; k<oldVal; k++) {
 5301:             var elementName = current[k];
 5302:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5303:         }
 5304:     }
 5305:     return;
 5306: }
 5307: 
 5308: function categoryCheck(form) {
 5309:     if (form.elements['addcategory_name'].value == 'instcode') {
 5310:         alert('$instcode_reserved\\n$choose_again');
 5311:         return false;
 5312:     }
 5313:     if (form.elements['addcategory_name'].value == 'communities') {
 5314:         alert('$communities_reserved\\n$choose_again');
 5315:         return false;
 5316:     }
 5317:     return true;
 5318: }
 5319: 
 5320: // ]]>
 5321: </script>
 5322: 
 5323: ENDSCRIPT
 5324:     return $output;
 5325: }
 5326: 
 5327: sub initialize_categories {
 5328:     my ($itemcount) = @_;
 5329:     my ($datatable,$css_class,$chgstr);
 5330:     my %default_names = (
 5331:                       instcode    => 'Official courses (with institutional codes)',
 5332:                       communities => 'Communities',
 5333:                         );
 5334:     my $select0 = ' selected="selected"';
 5335:     my $select1 = '';
 5336:     foreach my $default ('instcode','communities') {
 5337:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5338:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 5339:         if ($default eq 'communities') {
 5340:             $select1 = $select0;
 5341:             $select0 = '';
 5342:         }
 5343:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5344:                      .'<select name="'.$default.'_pos">'
 5345:                      .'<option value="0"'.$select0.'>1</option>'
 5346:                      .'<option value="1"'.$select1.'>2</option>'
 5347:                      .'<option value="2">3</option></select>&nbsp;'
 5348:                      .$default_names{$default}
 5349:                      .'</span></td><td><span class="LC_nobreak">'
 5350:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5351:                      .&mt('Display').'</label>&nbsp;<label>'
 5352:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5353:                  .'</label></span></td></tr>';
 5354:         $itemcount ++;
 5355:     }
 5356:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5357:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5358:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5359:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5360:                   .'<option value="0">1</option>'
 5361:                   .'<option value="1">2</option>'
 5362:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5363:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5364:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5365:     return $datatable;
 5366: }
 5367: 
 5368: sub build_category_rows {
 5369:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5370:     my ($text,$name,$item,$chgstr);
 5371:     if (ref($cats) eq 'ARRAY') {
 5372:         my $maxdepth = scalar(@{$cats});
 5373:         if (ref($cats->[$depth]) eq 'HASH') {
 5374:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5375:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5376:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5377:                 $text .= '<td><table class="LC_data_table">';
 5378:                 my ($idxnum,$parent_name,$parent_item);
 5379:                 my $higher = $depth - 1;
 5380:                 if ($higher == 0) {
 5381:                     $parent_name = &escape($parent).'::'.$higher;
 5382:                 } else {
 5383:                     if (ref($path) eq 'ARRAY') {
 5384:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5385:                     }
 5386:                 }
 5387:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5388:                 for (my $j=0; $j<=$numchildren; $j++) {
 5389:                     if ($j < $numchildren) {
 5390:                         $name = $cats->[$depth]{$parent}[$j];
 5391:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5392:                         $idxnum = $idx->{$item};
 5393:                     } else {
 5394:                         $name = $parent_name;
 5395:                         $item = $parent_item;
 5396:                     }
 5397:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5398:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5399:                     for (my $i=0; $i<=$numchildren; $i++) {
 5400:                         my $vpos = $i+1;
 5401:                         my $selstr;
 5402:                         if ($j == $i) {
 5403:                             $selstr = ' selected="selected" ';
 5404:                         }
 5405:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5406:                     }
 5407:                     $text .= '</select>&nbsp;';
 5408:                     if ($j < $numchildren) {
 5409:                         my $deeper = $depth+1;
 5410:                         $text .= $name.'&nbsp;'
 5411:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5412:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5413:                         if(ref($path) eq 'ARRAY') {
 5414:                             push(@{$path},$name);
 5415:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5416:                             pop(@{$path});
 5417:                         }
 5418:                     } else {
 5419:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5420:                         if ($j == $numchildren) {
 5421:                             $text .= $name;
 5422:                         } else {
 5423:                             $text .= $item;
 5424:                         }
 5425:                         $text .= '" value="" />';
 5426:                     }
 5427:                     $text .= '</td></tr>';
 5428:                 }
 5429:                 $text .= '</table></td>';
 5430:             } else {
 5431:                 my $higher = $depth-1;
 5432:                 if ($higher == 0) {
 5433:                     $name = &escape($parent).'::'.$higher;
 5434:                 } else {
 5435:                     if (ref($path) eq 'ARRAY') {
 5436:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5437:                     }
 5438:                 }
 5439:                 my $colspan;
 5440:                 if ($parent ne 'instcode') {
 5441:                     $colspan = $maxdepth - $depth - 1;
 5442:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5443:                 }
 5444:             }
 5445:         }
 5446:     }
 5447:     return $text;
 5448: }
 5449: 
 5450: sub modifiable_userdata_row {
 5451:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5452:     my ($role,$rolename,$statustype);
 5453:     $role = $item;
 5454:     if ($context eq 'cancreate') {
 5455:         if ($item =~ /^emailusername_(.+)$/) {
 5456:             $statustype = $1;
 5457:             $role = 'emailusername';
 5458:             if (ref($usertypes) eq 'HASH') {
 5459:                 if ($usertypes->{$statustype}) {
 5460:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5461:                 } else {
 5462:                     $rolename = &mt('Data provided by user');
 5463:                 }
 5464:             }
 5465:         }
 5466:     } elsif ($context eq 'selfcreate') {
 5467:         if (ref($usertypes) eq 'HASH') {
 5468:             $rolename = $usertypes->{$role};
 5469:         } else {
 5470:             $rolename = $role;
 5471:         }
 5472:     } else {
 5473:         if ($role eq 'cr') {
 5474:             $rolename = &mt('Custom role');
 5475:         } else {
 5476:             $rolename = &Apache::lonnet::plaintext($role);
 5477:         }
 5478:     }
 5479:     my (@fields,%fieldtitles);
 5480:     if (ref($fieldsref) eq 'ARRAY') {
 5481:         @fields = @{$fieldsref};
 5482:     } else {
 5483:         @fields = ('lastname','firstname','middlename','generation',
 5484:                    'permanentemail','id');
 5485:     }
 5486:     if ((ref($titlesref) eq 'HASH')) {
 5487:         %fieldtitles = %{$titlesref};
 5488:     } else {
 5489:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5490:     }
 5491:     my $output;
 5492:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5493:     $output = '<tr '.$css_class.'>'.
 5494:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5495:               '<td class="LC_left_item" colspan="2"><table>';
 5496:     my $rem;
 5497:     my %checks;
 5498:     if (ref($settings) eq 'HASH') {
 5499:         if (ref($settings->{$context}) eq 'HASH') {
 5500:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5501:                 my $hashref = $settings->{$context}->{$role};
 5502:                 if ($role eq 'emailusername') {
 5503:                     if ($statustype) {
 5504:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5505:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5506:                             if (ref($hashref) eq 'HASH') { 
 5507:                                 foreach my $field (@fields) {
 5508:                                     if ($hashref->{$field}) {
 5509:                                         $checks{$field} = $hashref->{$field};
 5510:                                     }
 5511:                                 }
 5512:                             }
 5513:                         }
 5514:                     }
 5515:                 } else {
 5516:                     if (ref($hashref) eq 'HASH') {
 5517:                         foreach my $field (@fields) {
 5518:                             if ($hashref->{$field}) {
 5519:                                 $checks{$field} = ' checked="checked" ';
 5520:                             }
 5521:                         }
 5522:                     }
 5523:                 }
 5524:             }
 5525:         }
 5526:     }
 5527:      
 5528:     for (my $i=0; $i<@fields; $i++) {
 5529:         my $rem = $i%($numinrow);
 5530:         if ($rem == 0) {
 5531:             if ($i > 0) {
 5532:                 $output .= '</tr>';
 5533:             }
 5534:             $output .= '<tr>';
 5535:         }
 5536:         my $check = ' ';
 5537:         unless ($role eq 'emailusername') {
 5538:             if (exists($checks{$fields[$i]})) {
 5539:                 $check = $checks{$fields[$i]}
 5540:             } else {
 5541:                 if ($role eq 'st') {
 5542:                     if (ref($settings) ne 'HASH') {
 5543:                         $check = ' checked="checked" '; 
 5544:                     }
 5545:                 }
 5546:             }
 5547:         }
 5548:         $output .= '<td class="LC_left_item">'.
 5549:                    '<span class="LC_nobreak">';
 5550:         if ($role eq 'emailusername') {
 5551:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5552:                 $checks{$fields[$i]} = 'omit';
 5553:             }
 5554:             foreach my $option ('required','optional','omit') {
 5555:                 my $checked='';
 5556:                 if ($checks{$fields[$i]} eq $option) {
 5557:                     $checked='checked="checked" ';
 5558:                 }
 5559:                 $output .= '<label>'.
 5560:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5561:                            &mt($option).'</label>'.('&nbsp;' x2);
 5562:             }
 5563:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5564:         } else {
 5565:             $output .= '<label>'.
 5566:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5567:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5568:                        '</label>';
 5569:         }
 5570:         $output .= '</span></td>';
 5571:         $rem = @fields%($numinrow);
 5572:     }
 5573:     my $colsleft = $numinrow - $rem;
 5574:     if ($colsleft > 1 ) {
 5575:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5576:                    '&nbsp;</td>';
 5577:     } elsif ($colsleft == 1) {
 5578:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5579:     }
 5580:     $output .= '</tr></table></td></tr>';
 5581:     return $output;
 5582: }
 5583: 
 5584: sub insttypes_row {
 5585:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5586:     my %lt = &Apache::lonlocal::texthash (
 5587:                       cansearch => 'Users allowed to search',
 5588:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5589:                       lockablenames => 'User preference to lock name',
 5590:              );
 5591:     my $showdom;
 5592:     if ($context eq 'cansearch') {
 5593:         $showdom = ' ('.$dom.')';
 5594:     }
 5595:     my $class = 'LC_left_item';
 5596:     if ($context eq 'statustocreate') {
 5597:         $class = 'LC_right_item';
 5598:     }
 5599:     my $css_class = ' class="LC_odd_row"';
 5600:     if ($rownum ne '') { 
 5601:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5602:     }
 5603:     my $output = '<tr'.$css_class.'>'.
 5604:                  '<td>'.$lt{$context}.$showdom.
 5605:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5606:     my $rem;
 5607:     if (ref($types) eq 'ARRAY') {
 5608:         for (my $i=0; $i<@{$types}; $i++) {
 5609:             if (defined($usertypes->{$types->[$i]})) {
 5610:                 my $rem = $i%($numinrow);
 5611:                 if ($rem == 0) {
 5612:                     if ($i > 0) {
 5613:                         $output .= '</tr>';
 5614:                     }
 5615:                     $output .= '<tr>';
 5616:                 }
 5617:                 my $check = ' ';
 5618:                 if (ref($settings) eq 'HASH') {
 5619:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5620:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5621:                             $check = ' checked="checked" ';
 5622:                         }
 5623:                     } elsif ($context eq 'statustocreate') {
 5624:                         $check = ' checked="checked" ';
 5625:                     }
 5626:                 }
 5627:                 $output .= '<td class="LC_left_item">'.
 5628:                            '<span class="LC_nobreak"><label>'.
 5629:                            '<input type="checkbox" name="'.$context.'" '.
 5630:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5631:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5632:             }
 5633:         }
 5634:         $rem = @{$types}%($numinrow);
 5635:     }
 5636:     my $colsleft = $numinrow - $rem;
 5637:     if (($rem == 0) && (@{$types} > 0)) {
 5638:         $output .= '<tr>';
 5639:     }
 5640:     if ($colsleft > 1) {
 5641:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5642:     } else {
 5643:         $output .= '<td class="LC_left_item">';
 5644:     }
 5645:     my $defcheck = ' ';
 5646:     if (ref($settings) eq 'HASH') {  
 5647:         if (ref($settings->{$context}) eq 'ARRAY') {
 5648:             if (grep(/^default$/,@{$settings->{$context}})) {
 5649:                 $defcheck = ' checked="checked" ';
 5650:             }
 5651:         } elsif ($context eq 'statustocreate') {
 5652:             $defcheck = ' checked="checked" ';
 5653:         }
 5654:     }
 5655:     $output .= '<span class="LC_nobreak"><label>'.
 5656:                '<input type="checkbox" name="'.$context.'" '.
 5657:                'value="default"'.$defcheck.'/>'.
 5658:                $othertitle.'</label></span></td>'.
 5659:                '</tr></table></td></tr>';
 5660:     return $output;
 5661: }
 5662: 
 5663: sub sorted_searchtitles {
 5664:     my %searchtitles = &Apache::lonlocal::texthash(
 5665:                          'uname' => 'username',
 5666:                          'lastname' => 'last name',
 5667:                          'lastfirst' => 'last name, first name',
 5668:                      );
 5669:     my @titleorder = ('uname','lastname','lastfirst');
 5670:     return (\%searchtitles,\@titleorder);
 5671: }
 5672: 
 5673: sub sorted_searchtypes {
 5674:     my %srchtypes_desc = (
 5675:                            exact    => 'is exact match',
 5676:                            contains => 'contains ..',
 5677:                            begins   => 'begins with ..',
 5678:                          );
 5679:     my @srchtypeorder = ('exact','begins','contains');
 5680:     return (\%srchtypes_desc,\@srchtypeorder);
 5681: }
 5682: 
 5683: sub usertype_update_row {
 5684:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5685:     my $datatable;
 5686:     my $numinrow = 4;
 5687:     foreach my $type (@{$types}) {
 5688:         if (defined($usertypes->{$type})) {
 5689:             $$rownums ++;
 5690:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5691:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5692:                           '</td><td class="LC_left_item"><table>';
 5693:             for (my $i=0; $i<@{$fields}; $i++) {
 5694:                 my $rem = $i%($numinrow);
 5695:                 if ($rem == 0) {
 5696:                     if ($i > 0) {
 5697:                         $datatable .= '</tr>';
 5698:                     }
 5699:                     $datatable .= '<tr>';
 5700:                 }
 5701:                 my $check = ' ';
 5702:                 if (ref($settings) eq 'HASH') {
 5703:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5704:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5705:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5706:                                 $check = ' checked="checked" ';
 5707:                             }
 5708:                         }
 5709:                     }
 5710:                 }
 5711: 
 5712:                 if ($i == @{$fields}-1) {
 5713:                     my $colsleft = $numinrow - $rem;
 5714:                     if ($colsleft > 1) {
 5715:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5716:                     } else {
 5717:                         $datatable .= '<td>';
 5718:                     }
 5719:                 } else {
 5720:                     $datatable .= '<td>';
 5721:                 }
 5722:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5723:                               '<input type="checkbox" name="updateable_'.$type.
 5724:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5725:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5726:             }
 5727:             $datatable .= '</tr></table></td></tr>';
 5728:         }
 5729:     }
 5730:     return $datatable;
 5731: }
 5732: 
 5733: sub modify_login {
 5734:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5735:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5736:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5737:     %title = ( coursecatalog => 'Display course catalog',
 5738:                adminmail => 'Display administrator E-mail address',
 5739:                helpdesk  => 'Display "Contact Helpdesk" link',
 5740:                newuser => 'Link for visitors to create a user account',
 5741:                loginheader => 'Log-in box header');
 5742:     @offon = ('off','on');
 5743:     if (ref($domconfig{login}) eq 'HASH') {
 5744:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5745:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5746:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5747:             }
 5748:         }
 5749:     }
 5750:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5751:                                            \%domconfig,\%loginhash);
 5752:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5753:     foreach my $item (@toggles) {
 5754:         $loginhash{login}{$item} = $env{'form.'.$item};
 5755:     }
 5756:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5757:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5758:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5759:                                          \%loginhash);
 5760:     }
 5761: 
 5762:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5763:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5764:     if (keys(%servers) > 1) {
 5765:         foreach my $lonhost (keys(%servers)) {
 5766:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5767:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5768:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5769:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5770:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5771:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5772:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5773:                         $changes{'loginvia'}{$lonhost} = 1;
 5774:                     } else {
 5775:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5776:                         $changes{'loginvia'}{$lonhost} = 1;
 5777:                     }
 5778:                 } else {
 5779:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5780:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5781:                         $changes{'loginvia'}{$lonhost} = 1;
 5782:                     }
 5783:                 }
 5784:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5785:                     foreach my $item (@loginvia_attribs) {
 5786:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5787:                     }
 5788:                 } else {
 5789:                     foreach my $item (@loginvia_attribs) {
 5790:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5791:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5792:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5793:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5794:                                 $new = '/';
 5795:                             }
 5796:                         }
 5797:                         if (($item eq 'custompath') && 
 5798:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5799:                             $new = '';
 5800:                         }
 5801:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5802:                             $changes{'loginvia'}{$lonhost} = 1;
 5803:                         }
 5804:                         if ($item eq 'exempt') {
 5805:                             $new =~ s/^\s+//;
 5806:                             $new =~ s/\s+$//;
 5807:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5808:                             my @okips;
 5809:                             foreach my $ip (@poss_ips) {
 5810:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5811:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5812:                                         push(@okips,$ip); 
 5813:                                     }
 5814:                                 }
 5815:                             }
 5816:                             if (@okips > 0) {
 5817:                                 $new = join(',',@okips); 
 5818:                             } else {
 5819:                                 $new = ''; 
 5820:                             }
 5821:                         }
 5822:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5823:                     }
 5824:                 }
 5825:             } else {
 5826:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5827:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5828:                     $changes{'loginvia'}{$lonhost} = 1;
 5829:                     foreach my $item (@loginvia_attribs) {
 5830:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5831:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5832:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5833:                                 $new = '/';
 5834:                             }
 5835:                         }
 5836:                         if (($item eq 'custompath') && 
 5837:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5838:                             $new = '';
 5839:                         }
 5840:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5841:                     }
 5842:                 }
 5843:             }
 5844:         }
 5845:     }
 5846: 
 5847:     my $servadm = $r->dir_config('lonAdmEMail');
 5848:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5849:     if (ref($domconfig{'login'}) eq 'HASH') {
 5850:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5851:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5852:                 if ($lang eq 'nolang') {
 5853:                     push(@currlangs,$lang);
 5854:                 } elsif (defined($langchoices{$lang})) {
 5855:                     push(@currlangs,$lang);
 5856:                 } else {
 5857:                     next;
 5858:                 }
 5859:             }
 5860:         }
 5861:     }
 5862:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5863:     if (@currlangs > 0) {
 5864:         foreach my $lang (@currlangs) {
 5865:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5866:                 $changes{'helpurl'}{$lang} = 1;
 5867:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5868:                 $changes{'helpurl'}{$lang} = 1;
 5869:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5870:                 push(@newlangs,$lang);
 5871:             } else {
 5872:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5873:             }
 5874:         }
 5875:     }
 5876:     unless (grep(/^nolang$/,@currlangs)) {
 5877:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5878:             $changes{'helpurl'}{'nolang'} = 1;
 5879:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5880:             push(@newlangs,'nolang');
 5881:         }
 5882:     }
 5883:     if ($env{'form.loginhelpurl_add_lang'}) {
 5884:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5885:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5886:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5887:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5888:         }
 5889:     }
 5890:     if ((@newlangs > 0) || ($addedfile)) {
 5891:         my $error;
 5892:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5893:         if ($configuserok eq 'ok') {
 5894:             if ($switchserver) {
 5895:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5896:             } elsif ($author_ok eq 'ok') {
 5897:                 my @allnew = @newlangs;
 5898:                 if ($addedfile ne '') {
 5899:                     push(@allnew,$addedfile);
 5900:                 }
 5901:                 foreach my $lang (@allnew) {
 5902:                     my $formelem = 'loginhelpurl_'.$lang;
 5903:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5904:                         $formelem = 'loginhelpurl_add_file';
 5905:                     }
 5906:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5907:                                                                "help/$lang",'','',$newfile{$lang});
 5908:                     if ($result eq 'ok') {
 5909:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5910:                         $changes{'helpurl'}{$lang} = 1;
 5911:                     } else {
 5912:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5913:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5914:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5915:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5916: 
 5917:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5918:                         }
 5919:                     }
 5920:                 }
 5921:             } else {
 5922:                 $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);
 5923:             }
 5924:         } else {
 5925:             $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);
 5926:         }
 5927:         if ($error) {
 5928:             &Apache::lonnet::logthis($error);
 5929:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5930:         }
 5931:     }
 5932:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5933: 
 5934:     my $defaulthelpfile = '/adm/loginproblems.html';
 5935:     my $defaulttext = &mt('Default in use');
 5936: 
 5937:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5938:                                              $dom);
 5939:     if ($putresult eq 'ok') {
 5940:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5941:         my %defaultchecked = (
 5942:                     'coursecatalog' => 'on',
 5943:                     'helpdesk'      => 'on',
 5944:                     'adminmail'     => 'off',
 5945:                     'newuser'       => 'off',
 5946:         );
 5947:         if (ref($domconfig{'login'}) eq 'HASH') {
 5948:             foreach my $item (@toggles) {
 5949:                 if ($defaultchecked{$item} eq 'on') { 
 5950:                     if (($domconfig{'login'}{$item} eq '0') &&
 5951:                         ($env{'form.'.$item} eq '1')) {
 5952:                         $changes{$item} = 1;
 5953:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5954:                               $domconfig{'login'}{$item} eq '1') &&
 5955:                              ($env{'form.'.$item} eq '0')) {
 5956:                         $changes{$item} = 1;
 5957:                     }
 5958:                 } elsif ($defaultchecked{$item} eq 'off') {
 5959:                     if (($domconfig{'login'}{$item} eq '1') &&
 5960:                         ($env{'form.'.$item} eq '0')) {
 5961:                         $changes{$item} = 1;
 5962:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5963:                               $domconfig{'login'}{$item} eq '0') &&
 5964:                              ($env{'form.'.$item} eq '1')) {
 5965:                         $changes{$item} = 1;
 5966:                     }
 5967:                 }
 5968:             }
 5969:         }
 5970:         if (keys(%changes) > 0 || $colchgtext) {
 5971:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5972:             if (ref($lastactref) eq 'HASH') {
 5973:                 $lastactref->{'domainconfig'} = 1;
 5974:             }
 5975:             $resulttext = &mt('Changes made:').'<ul>';
 5976:             foreach my $item (sort(keys(%changes))) {
 5977:                 if ($item eq 'loginvia') {
 5978:                     if (ref($changes{$item}) eq 'HASH') {
 5979:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5980:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5981:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5982:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5983:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5984:                                     $protocol = 'http' if ($protocol ne 'https');
 5985:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5986: 
 5987:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5988:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5989:                                     } else {
 5990:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5991:                                     }
 5992:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5993:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5994:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5995:                                     }
 5996:                                     $resulttext .= '</li>';
 5997:                                 } else {
 5998:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5999:                                 }
 6000:                             } else {
 6001:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 6002:                             }
 6003:                         }
 6004:                         $resulttext .= '</ul></li>';
 6005:                     }
 6006:                 } elsif ($item eq 'helpurl') {
 6007:                     if (ref($changes{$item}) eq 'HASH') {
 6008:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 6009:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 6010:                                 my ($chg,$link);
 6011:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 6012:                                 if ($lang eq 'nolang') {
 6013:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 6014:                                 } else {
 6015:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 6016:                                 }
 6017:                                 $resulttext .= '<li>'.$chg.'</li>';
 6018:                             } else {
 6019:                                 my $chg;
 6020:                                 if ($lang eq 'nolang') {
 6021:                                     $chg = &mt('custom log-in help file for no preferred language');
 6022:                                 } else {
 6023:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 6024:                                 }
 6025:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 6026:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 6027:                                                       '?inhibitmenu=yes',$chg,600,500).
 6028:                                                '</li>';
 6029:                             }
 6030:                         }
 6031:                     }
 6032:                 } elsif ($item eq 'captcha') {
 6033:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6034:                         my $chgtxt;
 6035:                         if ($loginhash{'login'}{$item} eq 'notused') {
 6036:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 6037:                         } else {
 6038:                             my %captchas = &captcha_phrases();
 6039:                             if ($captchas{$loginhash{'login'}{$item}}) {
 6040:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 6041:                             } else {
 6042:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 6043:                             }
 6044:                         }
 6045:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6046:                     }
 6047:                 } elsif ($item eq 'recaptchakeys') {
 6048:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6049:                         my ($privkey,$pubkey);
 6050:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 6051:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 6052:                             $privkey = $loginhash{'login'}{$item}{'private'};
 6053:                         }
 6054:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 6055:                         if (!$pubkey) {
 6056:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 6057:                         } else {
 6058:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6059:                         }
 6060:                         if (!$privkey) {
 6061:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 6062:                         } else {
 6063:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 6064:                         }
 6065:                         $chgtxt .= '</ul>';
 6066:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6067:                     }
 6068:                 } else {
 6069:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 6070:                 }
 6071:             }
 6072:             $resulttext .= $colchgtext.'</ul>';
 6073:         } else {
 6074:             $resulttext = &mt('No changes made to log-in page settings');
 6075:         }
 6076:     } else {
 6077:         $resulttext = '<span class="LC_error">'.
 6078: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6079:     }
 6080:     if ($errors) {
 6081:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 6082:                        $errors.'</ul>';
 6083:     }
 6084:     return $resulttext;
 6085: }
 6086: 
 6087: sub color_font_choices {
 6088:     my %choices =
 6089:         &Apache::lonlocal::texthash (
 6090:             img => "Header",
 6091:             bgs => "Background colors",
 6092:             links => "Link colors",
 6093:             images => "Images",
 6094:             font => "Font color",
 6095:             fontmenu => "Font menu",
 6096:             pgbg => "Page",
 6097:             tabbg => "Header",
 6098:             sidebg => "Border",
 6099:             link => "Link",
 6100:             alink => "Active link",
 6101:             vlink => "Visited link",
 6102:         );
 6103:     return %choices;
 6104: }
 6105: 
 6106: sub modify_rolecolors {
 6107:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 6108:     my ($resulttext,%rolehash);
 6109:     $rolehash{'rolecolors'} = {};
 6110:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 6111:         if ($domconfig{'rolecolors'} eq '') {
 6112:             $domconfig{'rolecolors'} = {};
 6113:         }
 6114:     }
 6115:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 6116:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 6117:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 6118:                                              $dom);
 6119:     if ($putresult eq 'ok') {
 6120:         if (keys(%changes) > 0) {
 6121:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6122:             if (ref($lastactref) eq 'HASH') {
 6123:                 $lastactref->{'domainconfig'} = 1;
 6124:             }
 6125:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 6126:                                              $rolehash{'rolecolors'});
 6127:         } else {
 6128:             $resulttext = &mt('No changes made to default color schemes');
 6129:         }
 6130:     } else {
 6131:         $resulttext = '<span class="LC_error">'.
 6132: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6133:     }
 6134:     if ($errors) {
 6135:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6136:                        $errors.'</ul>';
 6137:     }
 6138:     return $resulttext;
 6139: }
 6140: 
 6141: sub modify_colors {
 6142:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 6143:     my (%changes,%choices);
 6144:     my @bgs;
 6145:     my @links = ('link','alink','vlink');
 6146:     my @logintext;
 6147:     my @images;
 6148:     my $servadm = $r->dir_config('lonAdmEMail');
 6149:     my $errors;
 6150:     my %defaults;
 6151:     foreach my $role (@{$roles}) {
 6152:         if ($role eq 'login') {
 6153:             %choices = &login_choices();
 6154:             @logintext = ('textcol','bgcol');
 6155:         } else {
 6156:             %choices = &color_font_choices();
 6157:         }
 6158:         if ($role eq 'login') {
 6159:             @images = ('img','logo','domlogo','login');
 6160:             @bgs = ('pgbg','mainbg','sidebg');
 6161:         } else {
 6162:             @images = ('img');
 6163:             @bgs = ('pgbg','tabbg','sidebg');
 6164:         }
 6165:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 6166:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 6167:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 6168:         }
 6169:         if ($role eq 'login') {
 6170:             foreach my $item (@logintext) {
 6171:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6172:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6173:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6174:                 }
 6175:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 6176:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6177:                 }
 6178:             }
 6179:         } else {
 6180:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 6181:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 6182:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 6183:             }
 6184:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 6185:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 6186:             }
 6187:         }
 6188:         foreach my $item (@bgs) {
 6189:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6190:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6191:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6192:             }
 6193:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 6194:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6195:             }
 6196:         }
 6197:         foreach my $item (@links) {
 6198:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6199:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6200:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6201:             }
 6202:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 6203:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6204:             }
 6205:         }
 6206:         my ($configuserok,$author_ok,$switchserver) = 
 6207:             &config_check($dom,$confname,$servadm);
 6208:         my ($width,$height) = &thumb_dimensions();
 6209:         if (ref($domconfig->{$role}) ne 'HASH') {
 6210:             $domconfig->{$role} = {};
 6211:         }
 6212:         foreach my $img (@images) {
 6213:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 6214:                 if (defined($env{'form.login_showlogo_'.$img})) {
 6215:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 6216:                 } else { 
 6217:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 6218:                 }
 6219:             } 
 6220: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 6221: 		 && !defined($domconfig->{$role}{$img})
 6222: 		 && !$env{'form.'.$role.'_del_'.$img}
 6223: 		 && $env{'form.'.$role.'_import_'.$img}) {
 6224: 		# import the old configured image from the .tab setting
 6225: 		# if they haven't provided a new one 
 6226: 		$domconfig->{$role}{$img} = 
 6227: 		    $env{'form.'.$role.'_import_'.$img};
 6228: 	    }
 6229:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 6230:                 my $error;
 6231:                 if ($configuserok eq 'ok') {
 6232:                     if ($switchserver) {
 6233:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 6234:                     } else {
 6235:                         if ($author_ok eq 'ok') {
 6236:                             my ($result,$logourl) = 
 6237:                                 &publishlogo($r,'upload',$role.'_'.$img,
 6238:                                            $dom,$confname,$img,$width,$height);
 6239:                             if ($result eq 'ok') {
 6240:                                 $confhash->{$role}{$img} = $logourl;
 6241:                                 $changes{$role}{'images'}{$img} = 1;
 6242:                             } else {
 6243:                                 $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);
 6244:                             }
 6245:                         } else {
 6246:                             $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);
 6247:                         }
 6248:                     }
 6249:                 } else {
 6250:                     $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);
 6251:                 }
 6252:                 if ($error) {
 6253:                     &Apache::lonnet::logthis($error);
 6254:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6255:                 }
 6256:             } elsif ($domconfig->{$role}{$img} ne '') {
 6257:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 6258:                     my $error;
 6259:                     if ($configuserok eq 'ok') {
 6260: # is confname an author?
 6261:                         if ($switchserver eq '') {
 6262:                             if ($author_ok eq 'ok') {
 6263:                                 my ($result,$logourl) = 
 6264:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 6265:                                             $dom,$confname,$img,$width,$height);
 6266:                                 if ($result eq 'ok') {
 6267:                                     $confhash->{$role}{$img} = $logourl;
 6268: 				    $changes{$role}{'images'}{$img} = 1;
 6269:                                 }
 6270:                             }
 6271:                         }
 6272:                     }
 6273:                 }
 6274:             }
 6275:         }
 6276:         if (ref($domconfig) eq 'HASH') {
 6277:             if (ref($domconfig->{$role}) eq 'HASH') {
 6278:                 foreach my $img (@images) {
 6279:                     if ($domconfig->{$role}{$img} ne '') {
 6280:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6281:                             $confhash->{$role}{$img} = '';
 6282:                             $changes{$role}{'images'}{$img} = 1;
 6283:                         } else {
 6284:                             if ($confhash->{$role}{$img} eq '') {
 6285:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 6286:                             }
 6287:                         }
 6288:                     } else {
 6289:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6290:                             $confhash->{$role}{$img} = '';
 6291:                             $changes{$role}{'images'}{$img} = 1;
 6292:                         } 
 6293:                     }
 6294:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 6295:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 6296:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 6297:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 6298:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 6299:                             }
 6300:                         } else {
 6301:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6302:                                 $changes{$role}{'showlogo'}{$img} = 1;
 6303:                             }
 6304:                         }
 6305:                     }
 6306:                 }
 6307:                 if ($domconfig->{$role}{'font'} ne '') {
 6308:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 6309:                         $changes{$role}{'font'} = 1;
 6310:                     }
 6311:                 } else {
 6312:                     if ($confhash->{$role}{'font'}) {
 6313:                         $changes{$role}{'font'} = 1;
 6314:                     }
 6315:                 }
 6316:                 if ($role ne 'login') {
 6317:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 6318:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 6319:                             $changes{$role}{'fontmenu'} = 1;
 6320:                         }
 6321:                     } else {
 6322:                         if ($confhash->{$role}{'fontmenu'}) {
 6323:                             $changes{$role}{'fontmenu'} = 1;
 6324:                         }
 6325:                     }
 6326:                 }
 6327:                 foreach my $item (@bgs) {
 6328:                     if ($domconfig->{$role}{$item} ne '') {
 6329:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6330:                             $changes{$role}{'bgs'}{$item} = 1;
 6331:                         } 
 6332:                     } else {
 6333:                         if ($confhash->{$role}{$item}) {
 6334:                             $changes{$role}{'bgs'}{$item} = 1;
 6335:                         }
 6336:                     }
 6337:                 }
 6338:                 foreach my $item (@links) {
 6339:                     if ($domconfig->{$role}{$item} ne '') {
 6340:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6341:                             $changes{$role}{'links'}{$item} = 1;
 6342:                         }
 6343:                     } else {
 6344:                         if ($confhash->{$role}{$item}) {
 6345:                             $changes{$role}{'links'}{$item} = 1;
 6346:                         }
 6347:                     }
 6348:                 }
 6349:                 foreach my $item (@logintext) {
 6350:                     if ($domconfig->{$role}{$item} ne '') {
 6351:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6352:                             $changes{$role}{'logintext'}{$item} = 1;
 6353:                         }
 6354:                     } else {
 6355:                         if ($confhash->{$role}{$item}) {
 6356:                             $changes{$role}{'logintext'}{$item} = 1;
 6357:                         }
 6358:                     }
 6359:                 }
 6360:             } else {
 6361:                 &default_change_checker($role,\@images,\@links,\@bgs,
 6362:                                         \@logintext,$confhash,\%changes); 
 6363:             }
 6364:         } else {
 6365:             &default_change_checker($role,\@images,\@links,\@bgs,
 6366:                                     \@logintext,$confhash,\%changes); 
 6367:         }
 6368:     }
 6369:     return ($errors,%changes);
 6370: }
 6371: 
 6372: sub config_check {
 6373:     my ($dom,$confname,$servadm) = @_;
 6374:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6375:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6376:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6377:                                                    $confname,$servadm);
 6378:     if ($configuserok eq 'ok') {
 6379:         $switchserver = &check_switchserver($dom,$confname);
 6380:         if ($switchserver eq '') {
 6381:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6382:         }
 6383:     }
 6384:     return ($configuserok,$author_ok,$switchserver);
 6385: }
 6386: 
 6387: sub default_change_checker {
 6388:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6389:     foreach my $item (@{$links}) {
 6390:         if ($confhash->{$role}{$item}) {
 6391:             $changes->{$role}{'links'}{$item} = 1;
 6392:         }
 6393:     }
 6394:     foreach my $item (@{$bgs}) {
 6395:         if ($confhash->{$role}{$item}) {
 6396:             $changes->{$role}{'bgs'}{$item} = 1;
 6397:         }
 6398:     }
 6399:     foreach my $item (@{$logintext}) {
 6400:         if ($confhash->{$role}{$item}) {
 6401:             $changes->{$role}{'logintext'}{$item} = 1;
 6402:         }
 6403:     }
 6404:     foreach my $img (@{$images}) {
 6405:         if ($env{'form.'.$role.'_del_'.$img}) {
 6406:             $confhash->{$role}{$img} = '';
 6407:             $changes->{$role}{'images'}{$img} = 1;
 6408:         }
 6409:         if ($role eq 'login') {
 6410:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6411:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6412:             }
 6413:         }
 6414:     }
 6415:     if ($confhash->{$role}{'font'}) {
 6416:         $changes->{$role}{'font'} = 1;
 6417:     }
 6418: }
 6419: 
 6420: sub display_colorchgs {
 6421:     my ($dom,$changes,$roles,$confhash) = @_;
 6422:     my (%choices,$resulttext);
 6423:     if (!grep(/^login$/,@{$roles})) {
 6424:         $resulttext = &mt('Changes made:').'<br />';
 6425:     }
 6426:     foreach my $role (@{$roles}) {
 6427:         if ($role eq 'login') {
 6428:             %choices = &login_choices();
 6429:         } else {
 6430:             %choices = &color_font_choices();
 6431:         }
 6432:         if (ref($changes->{$role}) eq 'HASH') {
 6433:             if ($role ne 'login') {
 6434:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6435:             }
 6436:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6437:                 if ($role ne 'login') {
 6438:                     $resulttext .= '<ul>';
 6439:                 }
 6440:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6441:                     if ($role ne 'login') {
 6442:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6443:                     }
 6444:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6445:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6446:                             if ($confhash->{$role}{$key}{$item}) {
 6447:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6448:                             } else {
 6449:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6450:                             }
 6451:                         } elsif ($confhash->{$role}{$item} eq '') {
 6452:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6453:                         } else {
 6454:                             my $newitem = $confhash->{$role}{$item};
 6455:                             if ($key eq 'images') {
 6456:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6457:                             }
 6458:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6459:                         }
 6460:                     }
 6461:                     if ($role ne 'login') {
 6462:                         $resulttext .= '</ul></li>';
 6463:                     }
 6464:                 } else {
 6465:                     if ($confhash->{$role}{$key} eq '') {
 6466:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6467:                     } else {
 6468:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6469:                     }
 6470:                 }
 6471:                 if ($role ne 'login') {
 6472:                     $resulttext .= '</ul>';
 6473:                 }
 6474:             }
 6475:         }
 6476:     }
 6477:     return $resulttext;
 6478: }
 6479: 
 6480: sub thumb_dimensions {
 6481:     return ('200','50');
 6482: }
 6483: 
 6484: sub check_dimensions {
 6485:     my ($inputfile) = @_;
 6486:     my ($fullwidth,$fullheight);
 6487:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6488:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6489:             my $imageinfo = <PIPE>;
 6490:             if (!close(PIPE)) {
 6491:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6492:             }
 6493:             chomp($imageinfo);
 6494:             my ($fullsize) = 
 6495:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6496:             if ($fullsize) {
 6497:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6498:             }
 6499:         }
 6500:     }
 6501:     return ($fullwidth,$fullheight);
 6502: }
 6503: 
 6504: sub check_configuser {
 6505:     my ($uhome,$dom,$confname,$servadm) = @_;
 6506:     my ($configuserok,%currroles);
 6507:     if ($uhome eq 'no_host') {
 6508:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6509:         my $configpass = &LONCAPA::Enrollment::create_password();
 6510:         $configuserok = 
 6511:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6512:                              $configpass,'','','','','',undef,$servadm);
 6513:     } else {
 6514:         $configuserok = 'ok';
 6515:         %currroles = 
 6516:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6517:     }
 6518:     return ($configuserok,%currroles);
 6519: }
 6520: 
 6521: sub check_authorstatus {
 6522:     my ($dom,$confname,%currroles) = @_;
 6523:     my $author_ok;
 6524:     if (!$currroles{':'.$dom.':au'}) {
 6525:         my $start = time;
 6526:         my $end = 0;
 6527:         $author_ok = 
 6528:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6529:                                         'au',$end,$start,'','','domconfig');
 6530:     } else {
 6531:         $author_ok = 'ok';
 6532:     }
 6533:     return $author_ok;
 6534: }
 6535: 
 6536: sub publishlogo {
 6537:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6538:     my ($output,$fname,$logourl);
 6539:     if ($action eq 'upload') {
 6540:         $fname=$env{'form.'.$formname.'.filename'};
 6541:         chop($env{'form.'.$formname});
 6542:     } else {
 6543:         ($fname) = ($formname =~ /([^\/]+)$/);
 6544:     }
 6545:     if ($savefileas ne '') {
 6546:         $fname = $savefileas;
 6547:     }
 6548:     $fname=&Apache::lonnet::clean_filename($fname);
 6549: # See if there is anything left
 6550:     unless ($fname) { return ('error: no uploaded file'); }
 6551:     $fname="$subdir/$fname";
 6552:     my $docroot=$r->dir_config('lonDocRoot');
 6553:     my $filepath="$docroot/priv";
 6554:     my $relpath = "$dom/$confname";
 6555:     my ($fnamepath,$file,$fetchthumb);
 6556:     $file=$fname;
 6557:     if ($fname=~m|/|) {
 6558:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6559:     }
 6560:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6561:     my $count;
 6562:     for ($count=5;$count<=$#parts;$count++) {
 6563:         $filepath.="/$parts[$count]";
 6564:         if ((-e $filepath)!=1) {
 6565:             mkdir($filepath,02770);
 6566:         }
 6567:     }
 6568:     # Check for bad extension and disallow upload
 6569:     if ($file=~/\.(\w+)$/ &&
 6570:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6571:         $output = 
 6572:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6573:     } elsif ($file=~/\.(\w+)$/ &&
 6574:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6575:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6576:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6577:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6578:     } elsif (-d "$filepath/$file") {
 6579:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6580:     } else {
 6581:         my $source = $filepath.'/'.$file;
 6582:         my $logfile;
 6583:         if (!open($logfile,">>$source".'.log')) {
 6584:             return (&mt('No write permission to Authoring Space'));
 6585:         }
 6586:         print $logfile
 6587: "\n================= Publish ".localtime()." ================\n".
 6588: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6589: # Save the file
 6590:         if (!open(FH,'>'.$source)) {
 6591:             &Apache::lonnet::logthis('Failed to create '.$source);
 6592:             return (&mt('Failed to create file'));
 6593:         }
 6594:         if ($action eq 'upload') {
 6595:             if (!print FH ($env{'form.'.$formname})) {
 6596:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6597:                 return (&mt('Failed to write file'));
 6598:             }
 6599:         } else {
 6600:             my $original = &Apache::lonnet::filelocation('',$formname);
 6601:             if(!copy($original,$source)) {
 6602:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6603:                 return (&mt('Failed to write file'));
 6604:             }
 6605:         }
 6606:         close(FH);
 6607:         chmod(0660, $source); # Permissions to rw-rw---.
 6608: 
 6609:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6610:         my $copyfile=$targetdir.'/'.$file;
 6611: 
 6612:         my @parts=split(/\//,$targetdir);
 6613:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6614:         for (my $count=5;$count<=$#parts;$count++) {
 6615:             $path.="/$parts[$count]";
 6616:             if (!-e $path) {
 6617:                 print $logfile "\nCreating directory ".$path;
 6618:                 mkdir($path,02770);
 6619:             }
 6620:         }
 6621:         my $versionresult;
 6622:         if (-e $copyfile) {
 6623:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6624:         } else {
 6625:             $versionresult = 'ok';
 6626:         }
 6627:         if ($versionresult eq 'ok') {
 6628:             if (copy($source,$copyfile)) {
 6629:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6630:                 $output = 'ok';
 6631:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6632:                 push(@{$modified_urls},[$copyfile,$source]);
 6633:                 my $metaoutput = 
 6634:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6635:                 unless ($registered_cleanup) {
 6636:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6637:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6638:                     $registered_cleanup=1;
 6639:                 }
 6640:             } else {
 6641:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6642:                 $output = &mt('Failed to copy file to RES space').", $!";
 6643:             }
 6644:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6645:                 my $inputfile = $filepath.'/'.$file;
 6646:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6647:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6648:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6649:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6650:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6651:                         system("convert -sample $thumbsize $inputfile $outfile");
 6652:                         chmod(0660, $filepath.'/tn-'.$file);
 6653:                         if (-e $outfile) {
 6654:                             my $copyfile=$targetdir.'/tn-'.$file;
 6655:                             if (copy($outfile,$copyfile)) {
 6656:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6657:                                 my $thumb_metaoutput = 
 6658:                                     &write_metadata($dom,$confname,$formname,
 6659:                                                     $targetdir,'tn-'.$file,$logfile);
 6660:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6661:                                 unless ($registered_cleanup) {
 6662:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6663:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6664:                                     $registered_cleanup=1;
 6665:                                 }
 6666:                             } else {
 6667:                                 print $logfile "\nUnable to write ".$copyfile.
 6668:                                                ':'.$!."\n";
 6669:                             }
 6670:                         }
 6671:                     }
 6672:                 }
 6673:             }
 6674:         } else {
 6675:             $output = $versionresult;
 6676:         }
 6677:     }
 6678:     return ($output,$logourl);
 6679: }
 6680: 
 6681: sub logo_versioning {
 6682:     my ($targetdir,$file,$logfile) = @_;
 6683:     my $target = $targetdir.'/'.$file;
 6684:     my ($maxversion,$fn,$extn,$output);
 6685:     $maxversion = 0;
 6686:     if ($file =~ /^(.+)\.(\w+)$/) {
 6687:         $fn=$1;
 6688:         $extn=$2;
 6689:     }
 6690:     opendir(DIR,$targetdir);
 6691:     while (my $filename=readdir(DIR)) {
 6692:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6693:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6694:         }
 6695:     }
 6696:     $maxversion++;
 6697:     print $logfile "\nCreating old version ".$maxversion."\n";
 6698:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6699:     if (copy($target,$copyfile)) {
 6700:         print $logfile "Copied old target to ".$copyfile."\n";
 6701:         $copyfile=$copyfile.'.meta';
 6702:         if (copy($target.'.meta',$copyfile)) {
 6703:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6704:             $output = 'ok';
 6705:         } else {
 6706:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6707:             $output = &mt('Failed to copy old meta').", $!, ";
 6708:         }
 6709:     } else {
 6710:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6711:         $output = &mt('Failed to copy old target').", $!, ";
 6712:     }
 6713:     return $output;
 6714: }
 6715: 
 6716: sub write_metadata {
 6717:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6718:     my (%metadatafields,%metadatakeys,$output);
 6719:     $metadatafields{'title'}=$formname;
 6720:     $metadatafields{'creationdate'}=time;
 6721:     $metadatafields{'lastrevisiondate'}=time;
 6722:     $metadatafields{'copyright'}='public';
 6723:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6724:                                          $env{'user.domain'};
 6725:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6726:     $metadatafields{'domain'}=$dom;
 6727:     {
 6728:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6729:         my $mfh;
 6730:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6731:             foreach (sort(keys(%metadatafields))) {
 6732:                 unless ($_=~/\./) {
 6733:                     my $unikey=$_;
 6734:                     $unikey=~/^([A-Za-z]+)/;
 6735:                     my $tag=$1;
 6736:                     $tag=~tr/A-Z/a-z/;
 6737:                     print $mfh "\n\<$tag";
 6738:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6739:                         my $value=$metadatafields{$unikey.'.'.$_};
 6740:                         $value=~s/\"/\'\'/g;
 6741:                         print $mfh ' '.$_.'="'.$value.'"';
 6742:                     }
 6743:                     print $mfh '>'.
 6744:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6745:                             .'</'.$tag.'>';
 6746:                 }
 6747:             }
 6748:             $output = 'ok';
 6749:             print $logfile "\nWrote metadata";
 6750:             close($mfh);
 6751:         } else {
 6752:             print $logfile "\nFailed to open metadata file";
 6753:             $output = &mt('Could not write metadata');
 6754:         }
 6755:     }
 6756:     return $output;
 6757: }
 6758: 
 6759: sub notifysubscribed {
 6760:     foreach my $targetsource (@{$modified_urls}){
 6761:         next unless (ref($targetsource) eq 'ARRAY');
 6762:         my ($target,$source)=@{$targetsource};
 6763:         if ($source ne '') {
 6764:             if (open(my $logfh,'>>'.$source.'.log')) {
 6765:                 print $logfh "\nCleanup phase: Notifications\n";
 6766:                 my @subscribed=&subscribed_hosts($target);
 6767:                 foreach my $subhost (@subscribed) {
 6768:                     print $logfh "\nNotifying host ".$subhost.':';
 6769:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6770:                     print $logfh $reply;
 6771:                 }
 6772:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6773:                 foreach my $subhost (@subscribedmeta) {
 6774:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6775:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6776:                                                         $subhost);
 6777:                     print $logfh $reply;
 6778:                 }
 6779:                 print $logfh "\n============ Done ============\n";
 6780:                 close($logfh);
 6781:             }
 6782:         }
 6783:     }
 6784:     return OK;
 6785: }
 6786: 
 6787: sub subscribed_hosts {
 6788:     my ($target) = @_;
 6789:     my @subscribed;
 6790:     if (open(my $fh,"<$target.subscription")) {
 6791:         while (my $subline=<$fh>) {
 6792:             if ($subline =~ /^($match_lonid):/) {
 6793:                 my $host = $1;
 6794:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6795:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6796:                         push(@subscribed,$host);
 6797:                     }
 6798:                 }
 6799:             }
 6800:         }
 6801:     }
 6802:     return @subscribed;
 6803: }
 6804: 
 6805: sub check_switchserver {
 6806:     my ($dom,$confname) = @_;
 6807:     my ($allowed,$switchserver);
 6808:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6809:     if ($home eq 'no_host') {
 6810:         $home = &Apache::lonnet::domain($dom,'primary');
 6811:     }
 6812:     my @ids=&Apache::lonnet::current_machine_ids();
 6813:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6814:     if (!$allowed) {
 6815: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6816:     }
 6817:     return $switchserver;
 6818: }
 6819: 
 6820: sub modify_quotas {
 6821:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6822:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6823:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6824:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 6825:         $validationfieldsref);
 6826:     if ($action eq 'quotas') {
 6827:         $context = 'tools'; 
 6828:     } else {
 6829:         $context = $action;
 6830:     }
 6831:     if ($context eq 'requestcourses') {
 6832:         @usertools = ('official','unofficial','community','textbook');
 6833:         @options =('norequest','approval','validate','autolimit');
 6834:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6835:         %titles = &courserequest_titles();
 6836:         $toolregexp = join('|',@usertools);
 6837:         %conditions = &courserequest_conditions();
 6838:         $confname = $dom.'-domainconfig';
 6839:         my $servadm = $r->dir_config('lonAdmEMail');
 6840:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6841:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 6842:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6843:     } elsif ($context eq 'requestauthor') {
 6844:         @usertools = ('author');
 6845:         %titles = &authorrequest_titles();
 6846:     } else {
 6847:         @usertools = ('aboutme','blog','webdav','portfolio');
 6848:         %titles = &tool_titles();
 6849:     }
 6850:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6851:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6852:     foreach my $key (keys(%env)) {
 6853:         if ($context eq 'requestcourses') {
 6854:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6855:                 my $item = $1;
 6856:                 my $type = $2;
 6857:                 if ($type =~ /^limit_(.+)/) {
 6858:                     $limithash{$item}{$1} = $env{$key};
 6859:                 } else {
 6860:                     $confhash{$item}{$type} = $env{$key};
 6861:                 }
 6862:             }
 6863:         } elsif ($context eq 'requestauthor') {
 6864:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6865:                 $confhash{$1} = $env{$key};
 6866:             }
 6867:         } else {
 6868:             if ($key =~ /^form\.quota_(.+)$/) {
 6869:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6870:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6871:                 $confhash{'authorquota'}{$1} = $env{$key};
 6872:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6873:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6874:             }
 6875:         }
 6876:     }
 6877:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6878:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6879:         @approvalnotify = sort(@approvalnotify);
 6880:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6881:         my @crstypes = ('official','unofficial','community','textbook');
 6882:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6883:         foreach my $type (@hasuniquecode) {
 6884:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6885:                 $confhash{'uniquecode'}{$type} = 1;
 6886:             }
 6887:         }
 6888:         my (%newbook,%allpos);
 6889:         if ($context eq 'requestcourses') {
 6890:             foreach my $type ('textbooks','templates') {
 6891:                 @{$allpos{$type}} = (); 
 6892:                 my $invalid;
 6893:                 if ($type eq 'textbooks') {
 6894:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 6895:                 } else {
 6896:                     $invalid = &mt('Invalid LON-CAPA course for template');
 6897:                 }
 6898:                 if ($env{'form.'.$type.'_addbook'}) {
 6899:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 6900:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 6901:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 6902:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 6903:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 6904:                         } else {
 6905:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 6906:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 6907:                             $position =~ s/\D+//g;
 6908:                             if ($position ne '') {
 6909:                                 $allpos{$type}[$position] = $newbook{$type};
 6910:                             }
 6911:                         }
 6912:                     } else {
 6913:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 6914:                     }
 6915:                 }
 6916:             } 
 6917:         }
 6918:         if (ref($domconfig{$action}) eq 'HASH') {
 6919:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6920:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6921:                     $changes{'notify'}{'approval'} = 1;
 6922:                 }
 6923:             } else {
 6924:                 if ($confhash{'notify'}{'approval'}) {
 6925:                     $changes{'notify'}{'approval'} = 1;
 6926:                 }
 6927:             }
 6928:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6929:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6930:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6931:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6932:                             $changes{'uniquecode'} = 1;
 6933:                         }
 6934:                     }
 6935:                     unless ($changes{'uniquecode'}) {
 6936:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6937:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6938:                                 $changes{'uniquecode'} = 1;
 6939:                             }
 6940:                         }
 6941:                     }
 6942:                } else {
 6943:                    $changes{'uniquecode'} = 1;
 6944:                }
 6945:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6946:                 $changes{'uniquecode'} = 1;
 6947:             }
 6948:             if ($context eq 'requestcourses') {
 6949:                 foreach my $type ('textbooks','templates') {
 6950:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 6951:                         my %deletions;
 6952:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 6953:                         if (@todelete) {
 6954:                             map { $deletions{$_} = 1; } @todelete;
 6955:                         }
 6956:                         my %imgdeletions;
 6957:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 6958:                         if (@todeleteimages) {
 6959:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 6960:                         }
 6961:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 6962:                         for (my $i=0; $i<=$maxnum; $i++) {
 6963:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 6964:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 6965:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 6966:                                 if ($deletions{$key}) {
 6967:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 6968:                                         #FIXME need to obsolete item in RES space
 6969:                                     }
 6970:                                     next;
 6971:                                 } else {
 6972:                                     my $newpos = $env{'form.'.$itemid};
 6973:                                     $newpos =~ s/\D+//g;
 6974:                                     foreach my $item ('subject','title','publisher','author') {
 6975:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 6976:                                                  ($type eq 'templates'));
 6977:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 6978:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 6979:                                             $changes{$type}{$key} = 1;
 6980:                                         }
 6981:                                     }
 6982:                                     $allpos{$type}[$newpos] = $key;
 6983:                                 }
 6984:                                 if ($imgdeletions{$key}) {
 6985:                                     $changes{$type}{$key} = 1;
 6986:                                     #FIXME need to obsolete item in RES space
 6987:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 6988:                                     my ($cdom,$cnum) = split(/_/,$key);
 6989:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 6990:                                                                                   $cdom,$cnum,$type,$configuserok,
 6991:                                                                                   $switchserver,$author_ok);
 6992:                                     if ($imgurl) {
 6993:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 6994:                                         $changes{$type}{$key} = 1; 
 6995:                                     }
 6996:                                     if ($error) {
 6997:                                         &Apache::lonnet::logthis($error);
 6998:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6999:                                     } 
 7000:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 7001:                                     $confhash{$type}{$key}{'image'} = 
 7002:                                         $domconfig{$action}{$type}{$key}{'image'};
 7003:                                 }
 7004:                             }
 7005:                         }
 7006:                     }
 7007:                 }
 7008:             }
 7009:         } else {
 7010:             if ($confhash{'notify'}{'approval'}) {
 7011:                 $changes{'notify'}{'approval'} = 1;
 7012:             }
 7013:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 7014:                 $changes{'uniquecode'} = 1;
 7015:             }
 7016:         }
 7017:         if ($context eq 'requestcourses') {
 7018:             foreach my $type ('textbooks','templates') {
 7019:                 if ($newbook{$type}) {
 7020:                     $changes{$type}{$newbook{$type}} = 1;
 7021:                     foreach my $item ('subject','title','publisher','author') {
 7022:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7023:                                  ($type eq 'template'));
 7024:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 7025:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 7026:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 7027:                         }
 7028:                     }
 7029:                     if ($type eq 'textbooks') {
 7030:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 7031:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 7032:                             my ($imageurl,$error) =
 7033:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 7034:                                                         $configuserok,$switchserver,$author_ok);
 7035:                             if ($imageurl) {
 7036:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 7037:                             }
 7038:                             if ($error) {
 7039:                                 &Apache::lonnet::logthis($error);
 7040:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7041:                             }
 7042:                         }
 7043:                     }
 7044:                 }
 7045:                 if (@{$allpos{$type}} > 0) {
 7046:                     my $idx = 0;
 7047:                     foreach my $item (@{$allpos{$type}}) {
 7048:                         if ($item ne '') {
 7049:                             $confhash{$type}{$item}{'order'} = $idx;
 7050:                             if (ref($domconfig{$action}) eq 'HASH') {
 7051:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7052:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 7053:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 7054:                                             $changes{$type}{$item} = 1;
 7055:                                         }
 7056:                                     }
 7057:                                 }
 7058:                             }
 7059:                             $idx ++;
 7060:                         }
 7061:                     }
 7062:                 }
 7063:             }
 7064:             if (ref($validationitemsref) eq 'ARRAY') {
 7065:                 foreach my $item (@{$validationitemsref}) {
 7066:                     if ($item eq 'fields') {
 7067:                         my @changed;
 7068:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 7069:                         if (@{$confhash{'validation'}{$item}} > 0) {
 7070:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 7071:                         }
 7072:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7073:                             if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 7074:                                 @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 7075:                                                                               $domconfig{'requestcourses'}{'validation'}{$item});
 7076:                             } else {
 7077:                                 @changed = @{$confhash{'validation'}{$item}};
 7078:                             }
 7079:                         } else {
 7080:                             @changed = @{$confhash{'validation'}{$item}};
 7081:                         }
 7082:                         if (@changed) {
 7083:                             if ($confhash{'validation'}{$item}) {
 7084:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 7085:                             } else {
 7086:                                 $changes{'validation'}{$item} = &mt('None');
 7087:                             }
 7088:                         }
 7089:                     } else {
 7090:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 7091:                         if ($item eq 'markup') {
 7092:                             if ($env{'form.requestcourses_validation_'.$item}) {
 7093:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 7094:                             }
 7095:                         }
 7096:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7097:                             if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 7098:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7099:                             }
 7100:                         } else {
 7101:                             if ($confhash{'validation'}{$item} ne '') {
 7102:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7103:                             }
 7104:                         }
 7105:                     }
 7106:                 }
 7107:             }
 7108:             if ($env{'form.validationdc'}) {
 7109:                 my $newval = $env{'form.validationdc'};
 7110:                 my %domcoords = &get_active_dcs($dom);
 7111:                 if (exists($domcoords{$newval})) {
 7112:                     $confhash{'validation'}{'dc'} = $newval;
 7113:                 }
 7114:             }
 7115:             if (ref($confhash{'validation'}) eq 'HASH') {
 7116:                 if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7117:                     if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7118:                         unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7119:                             if ($confhash{'validation'}{'dc'} eq '') {
 7120:                                 $changes{'validation'}{'dc'} = &mt('None');
 7121:                             } else {
 7122:                                 $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7123:                             }
 7124:                         }
 7125:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 7126:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7127:                     }
 7128:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 7129:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7130:                 }
 7131:             } elsif (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7132:                 if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7133:                     $changes{'validation'}{'dc'} = &mt('None');
 7134:                 }
 7135:             }
 7136:         }
 7137:     } else {
 7138:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 7139:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 7140:     }
 7141:     foreach my $item (@usertools) {
 7142:         foreach my $type (@{$types},'default','_LC_adv') {
 7143:             my $unset; 
 7144:             if ($context eq 'requestcourses') {
 7145:                 $unset = '0';
 7146:                 if ($type eq '_LC_adv') {
 7147:                     $unset = '';
 7148:                 }
 7149:                 if ($confhash{$item}{$type} eq 'autolimit') {
 7150:                     $confhash{$item}{$type} .= '=';
 7151:                     unless ($limithash{$item}{$type} =~ /\D/) {
 7152:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 7153:                     }
 7154:                 }
 7155:             } elsif ($context eq 'requestauthor') {
 7156:                 $unset = '0';
 7157:                 if ($type eq '_LC_adv') {
 7158:                     $unset = '';
 7159:                 }
 7160:             } else {
 7161:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 7162:                     $confhash{$item}{$type} = 1;
 7163:                 } else {
 7164:                     $confhash{$item}{$type} = 0;
 7165:                 }
 7166:             }
 7167:             if (ref($domconfig{$action}) eq 'HASH') {
 7168:                 if ($action eq 'requestauthor') {
 7169:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 7170:                         $changes{$type} = 1;
 7171:                     }
 7172:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 7173:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 7174:                         $changes{$item}{$type} = 1;
 7175:                     }
 7176:                 } else {
 7177:                     if ($context eq 'requestcourses') {
 7178:                         if ($confhash{$item}{$type} ne $unset) {
 7179:                             $changes{$item}{$type} = 1;
 7180:                         }
 7181:                     } else {
 7182:                         if (!$confhash{$item}{$type}) {
 7183:                             $changes{$item}{$type} = 1;
 7184:                         }
 7185:                     }
 7186:                 }
 7187:             } else {
 7188:                 if ($context eq 'requestcourses') {
 7189:                     if ($confhash{$item}{$type} ne $unset) {
 7190:                         $changes{$item}{$type} = 1;
 7191:                     }
 7192:                 } elsif ($context eq 'requestauthor') {
 7193:                     if ($confhash{$type} ne $unset) {
 7194:                         $changes{$type} = 1;
 7195:                     }
 7196:                 } else {
 7197:                     if (!$confhash{$item}{$type}) {
 7198:                         $changes{$item}{$type} = 1;
 7199:                     }
 7200:                 }
 7201:             }
 7202:         }
 7203:     }
 7204:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7205:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 7206:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7207:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 7208:                     if (exists($confhash{'defaultquota'}{$key})) {
 7209:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 7210:                             $changes{'defaultquota'}{$key} = 1;
 7211:                         }
 7212:                     } else {
 7213:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 7214:                     }
 7215:                 }
 7216:             } else {
 7217:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 7218:                     if (exists($confhash{'defaultquota'}{$key})) {
 7219:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 7220:                             $changes{'defaultquota'}{$key} = 1;
 7221:                         }
 7222:                     } else {
 7223:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 7224:                     }
 7225:                 }
 7226:             }
 7227:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7228:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 7229:                     if (exists($confhash{'authorquota'}{$key})) {
 7230:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 7231:                             $changes{'authorquota'}{$key} = 1;
 7232:                         }
 7233:                     } else {
 7234:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 7235:                     }
 7236:                 }
 7237:             }
 7238:         }
 7239:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 7240:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 7241:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7242:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7243:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 7244:                             $changes{'defaultquota'}{$key} = 1;
 7245:                         }
 7246:                     } else {
 7247:                         if (!exists($domconfig{'quotas'}{$key})) {
 7248:                             $changes{'defaultquota'}{$key} = 1;
 7249:                         }
 7250:                     }
 7251:                 } else {
 7252:                     $changes{'defaultquota'}{$key} = 1;
 7253:                 }
 7254:             }
 7255:         }
 7256:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 7257:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 7258:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7259:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7260:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 7261:                             $changes{'authorquota'}{$key} = 1;
 7262:                         }
 7263:                     } else {
 7264:                         $changes{'authorquota'}{$key} = 1;
 7265:                     }
 7266:                 } else {
 7267:                     $changes{'authorquota'}{$key} = 1;
 7268:                 }
 7269:             }
 7270:         }
 7271:     }
 7272: 
 7273:     if ($context eq 'requestauthor') {
 7274:         $domdefaults{'requestauthor'} = \%confhash;
 7275:     } else {
 7276:         foreach my $key (keys(%confhash)) {
 7277:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 7278:                 $domdefaults{$key} = $confhash{$key};
 7279:             }
 7280:         }
 7281:     }
 7282: 
 7283:     my %quotahash = (
 7284:                       $action => { %confhash }
 7285:                     );
 7286:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 7287:                                              $dom);
 7288:     if ($putresult eq 'ok') {
 7289:         if (keys(%changes) > 0) {
 7290:             my $cachetime = 24*60*60;
 7291:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7292:             if (ref($lastactref) eq 'HASH') {
 7293:                 $lastactref->{'domdefaults'} = 1;
 7294:             }
 7295:             $resulttext = &mt('Changes made:').'<ul>';
 7296:             unless (($context eq 'requestcourses') ||
 7297:                     ($context eq 'requestauthor')) {
 7298:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 7299:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 7300:                     foreach my $type (@{$types},'default') {
 7301:                         if (defined($changes{'defaultquota'}{$type})) {
 7302:                             my $typetitle = $usertypes->{$type};
 7303:                             if ($type eq 'default') {
 7304:                                 $typetitle = $othertitle;
 7305:                             }
 7306:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 7307:                         }
 7308:                     }
 7309:                     $resulttext .= '</ul></li>';
 7310:                 }
 7311:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 7312:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 7313:                     foreach my $type (@{$types},'default') {
 7314:                         if (defined($changes{'authorquota'}{$type})) {
 7315:                             my $typetitle = $usertypes->{$type};
 7316:                             if ($type eq 'default') {
 7317:                                 $typetitle = $othertitle;
 7318:                             }
 7319:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 7320:                         }
 7321:                     }
 7322:                     $resulttext .= '</ul></li>';
 7323:                 }
 7324:             }
 7325:             my %newenv;
 7326:             foreach my $item (@usertools) {
 7327:                 my (%haschgs,%inconf);
 7328:                 if ($context eq 'requestauthor') {
 7329:                     %haschgs = %changes;
 7330:                     %inconf = %confhash;
 7331:                 } else {
 7332:                     if (ref($changes{$item}) eq 'HASH') {
 7333:                         %haschgs = %{$changes{$item}};
 7334:                     }
 7335:                     if (ref($confhash{$item}) eq 'HASH') {
 7336:                         %inconf = %{$confhash{$item}};
 7337:                     }
 7338:                 }
 7339:                 if (keys(%haschgs) > 0) {
 7340:                     my $newacc = 
 7341:                         &Apache::lonnet::usertools_access($env{'user.name'},
 7342:                                                           $env{'user.domain'},
 7343:                                                           $item,'reload',$context);
 7344:                     if (($context eq 'requestcourses') ||
 7345:                         ($context eq 'requestauthor')) {
 7346:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 7347:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 7348:                         }
 7349:                     } else {
 7350:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 7351:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 7352:                         }
 7353:                     }
 7354:                     unless ($context eq 'requestauthor') {
 7355:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 7356:                     }
 7357:                     foreach my $type (@{$types},'default','_LC_adv') {
 7358:                         if ($haschgs{$type}) {
 7359:                             my $typetitle = $usertypes->{$type};
 7360:                             if ($type eq 'default') {
 7361:                                 $typetitle = $othertitle;
 7362:                             } elsif ($type eq '_LC_adv') {
 7363:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 7364:                             }
 7365:                             if ($inconf{$type}) {
 7366:                                 if ($context eq 'requestcourses') {
 7367:                                     my $cond;
 7368:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 7369:                                         if ($1 eq '') {
 7370:                                             $cond = &mt('(Automatic processing of any request).');
 7371:                                         } else {
 7372:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 7373:                                         }
 7374:                                     } else { 
 7375:                                         $cond = $conditions{$inconf{$type}};
 7376:                                     }
 7377:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 7378:                                 } elsif ($context eq 'requestauthor') {
 7379:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 7380:                                                              $titles{$inconf{$type}},$typetitle);
 7381: 
 7382:                                 } else {
 7383:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7384:                                 }
 7385:                             } else {
 7386:                                 if ($type eq '_LC_adv') {
 7387:                                     if ($inconf{$type} eq '0') {
 7388:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7389:                                     } else { 
 7390:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7391:                                     }
 7392:                                 } else {
 7393:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7394:                                 }
 7395:                             }
 7396:                         }
 7397:                     }
 7398:                     unless ($context eq 'requestauthor') {
 7399:                         $resulttext .= '</ul></li>';
 7400:                     }
 7401:                 }
 7402:             }
 7403:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7404:                 if (ref($changes{'notify'}) eq 'HASH') {
 7405:                     if ($changes{'notify'}{'approval'}) {
 7406:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7407:                             if ($confhash{'notify'}{'approval'}) {
 7408:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7409:                             } else {
 7410:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7411:                             }
 7412:                         }
 7413:                     }
 7414:                 }
 7415:             }
 7416:             if ($action eq 'requestcourses') {
 7417:                 my @offon = ('off','on');
 7418:                 if ($changes{'uniquecode'}) {
 7419:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7420:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7421:                         $resulttext .= '<li>'.
 7422:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7423:                                        '</li>';
 7424:                     } else {
 7425:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7426:                                        '</li>';
 7427:                     }
 7428:                 }
 7429:                 foreach my $type ('textbooks','templates') {
 7430:                     if (ref($changes{$type}) eq 'HASH') {
 7431:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 7432:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 7433:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 7434:                             my $coursetitle = $coursehash{'description'};
 7435:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 7436:                             $resulttext .= '<li>';
 7437:                             foreach my $item ('subject','title','publisher','author') {
 7438:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7439:                                          ($type eq 'templates'));
 7440:                                 my $name = $item.':';
 7441:                                 $name =~ s/^(\w)/\U$1/;
 7442:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 7443:                             }
 7444:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7445:                             if ($type eq 'textbooks') {
 7446:                                 if ($confhash{$type}{$key}{'image'}) {
 7447:                                     $resulttext .= ' '.&mt('Image: [_1]',
 7448:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 7449:                                                    ' alt="Textbook cover" />').'<br />';
 7450:                                 }
 7451:                             }
 7452:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7453:                         }
 7454:                         $resulttext .= '</ul></li>';
 7455:                     }
 7456:                 }
 7457:                 if (ref($changes{'validation'}) eq 'HASH') {
 7458:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7459:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7460:                         foreach my $item (@{$validationitemsref}) {
 7461:                             if (exists($changes{'validation'}{$item})) {
 7462:                                 if ($item eq 'markup') {
 7463:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7464:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7465:                                 } else {
 7466:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7467:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7468:                                 }
 7469:                             }
 7470:                         }
 7471:                         if (exists($changes{'validation'}{'dc'})) {
 7472:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7473:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7474:                         }
 7475:                     }
 7476:                 }
 7477:             }
 7478:             $resulttext .= '</ul>';
 7479:             if (keys(%newenv)) {
 7480:                 &Apache::lonnet::appenv(\%newenv);
 7481:             }
 7482:         } else {
 7483:             if ($context eq 'requestcourses') {
 7484:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7485:             } elsif ($context eq 'requestauthor') {
 7486:                 $resulttext = &mt('No changes made to rights to request author space.');
 7487:             } else {
 7488:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7489:             }
 7490:         }
 7491:     } else {
 7492:         $resulttext = '<span class="LC_error">'.
 7493: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7494:     }
 7495:     if ($errors) {
 7496:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7497:                        '<ul>'.$errors.'</ul></p>';
 7498:     }
 7499:     return $resulttext;
 7500: }
 7501: 
 7502: sub process_textbook_image {
 7503:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 7504:     my $filename = $env{'form.'.$caller.'.filename'};
 7505:     my ($error,$url);
 7506:     my ($width,$height) = (50,50);
 7507:     if ($configuserok eq 'ok') {
 7508:         if ($switchserver) {
 7509:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7510:                          $switchserver);
 7511:         } elsif ($author_ok eq 'ok') {
 7512:             my ($result,$imageurl) =
 7513:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7514:                              "$type/$dom/$cnum/cover",$width,$height);
 7515:             if ($result eq 'ok') {
 7516:                 $url = $imageurl;
 7517:             } else {
 7518:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7519:             }
 7520:         } else {
 7521:             $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);
 7522:         }
 7523:     } else {
 7524:         $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);
 7525:     }
 7526:     return ($url,$error);
 7527: }
 7528: 
 7529: sub modify_autoenroll {
 7530:     my ($dom,$lastactref,%domconfig) = @_;
 7531:     my ($resulttext,%changes);
 7532:     my %currautoenroll;
 7533:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7534:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7535:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7536:         }
 7537:     }
 7538:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7539:     my %title = ( run => 'Auto-enrollment active',
 7540:                   sender => 'Sender for notification messages',
 7541:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7542:     my @offon = ('off','on');
 7543:     my $sender_uname = $env{'form.sender_uname'};
 7544:     my $sender_domain = $env{'form.sender_domain'};
 7545:     if ($sender_domain eq '') {
 7546:         $sender_uname = '';
 7547:     } elsif ($sender_uname eq '') {
 7548:         $sender_domain = '';
 7549:     }
 7550:     my $coowners = $env{'form.autoassign_coowners'};
 7551:     my %autoenrollhash =  (
 7552:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7553:                                        'sender_uname' => $sender_uname,
 7554:                                        'sender_domain' => $sender_domain,
 7555:                                        'co-owners' => $coowners,
 7556:                                 }
 7557:                      );
 7558:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7559:                                              $dom);
 7560:     if ($putresult eq 'ok') {
 7561:         if (exists($currautoenroll{'run'})) {
 7562:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7563:                  $changes{'run'} = 1;
 7564:              }
 7565:         } elsif ($autorun) {
 7566:             if ($env{'form.autoenroll_run'} ne '1') {
 7567:                  $changes{'run'} = 1;
 7568:             }
 7569:         }
 7570:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7571:             $changes{'sender'} = 1;
 7572:         }
 7573:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7574:             $changes{'sender'} = 1;
 7575:         }
 7576:         if ($currautoenroll{'co-owners'} ne '') {
 7577:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7578:                 $changes{'coowners'} = 1;
 7579:             }
 7580:         } elsif ($coowners) {
 7581:             $changes{'coowners'} = 1;
 7582:         }      
 7583:         if (keys(%changes) > 0) {
 7584:             $resulttext = &mt('Changes made:').'<ul>';
 7585:             if ($changes{'run'}) {
 7586:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7587:             }
 7588:             if ($changes{'sender'}) {
 7589:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7590:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7591:                 } else {
 7592:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7593:                 }
 7594:             }
 7595:             if ($changes{'coowners'}) {
 7596:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7597:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7598:                 if (ref($lastactref) eq 'HASH') {
 7599:                     $lastactref->{'domainconfig'} = 1;
 7600:                 }
 7601:             }
 7602:             $resulttext .= '</ul>';
 7603:         } else {
 7604:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7605:         }
 7606:     } else {
 7607:         $resulttext = '<span class="LC_error">'.
 7608: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7609:     }
 7610:     return $resulttext;
 7611: }
 7612: 
 7613: sub modify_autoupdate {
 7614:     my ($dom,%domconfig) = @_;
 7615:     my ($resulttext,%currautoupdate,%fields,%changes);
 7616:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7617:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7618:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7619:         }
 7620:     }
 7621:     my @offon = ('off','on');
 7622:     my %title = &Apache::lonlocal::texthash (
 7623:                    run => 'Auto-update:',
 7624:                    classlists => 'Updates to user information in classlists?'
 7625:                 );
 7626:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7627:     my %fieldtitles = &Apache::lonlocal::texthash (
 7628:                         id => 'Student/Employee ID',
 7629:                         permanentemail => 'E-mail address',
 7630:                         lastname => 'Last Name',
 7631:                         firstname => 'First Name',
 7632:                         middlename => 'Middle Name',
 7633:                         generation => 'Generation',
 7634:                       );
 7635:     $othertitle = &mt('All users');
 7636:     if (keys(%{$usertypes}) >  0) {
 7637:         $othertitle = &mt('Other users');
 7638:     }
 7639:     foreach my $key (keys(%env)) {
 7640:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7641:             my ($usertype,$item) = ($1,$2);
 7642:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7643:                 if ($usertype eq 'default') {   
 7644:                     push(@{$fields{$1}},$2);
 7645:                 } elsif (ref($types) eq 'ARRAY') {
 7646:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7647:                         push(@{$fields{$1}},$2);
 7648:                     }
 7649:                 }
 7650:             }
 7651:         }
 7652:     }
 7653:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 7654:     @lockablenames = sort(@lockablenames);
 7655:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 7656:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7657:         if (@changed) {
 7658:             $changes{'lockablenames'} = 1;
 7659:         }
 7660:     } else {
 7661:         if (@lockablenames) {
 7662:             $changes{'lockablenames'} = 1;
 7663:         }
 7664:     }
 7665:     my %updatehash = (
 7666:                       autoupdate => { run => $env{'form.autoupdate_run'},
 7667:                                       classlists => $env{'form.classlists'},
 7668:                                       fields => {%fields},
 7669:                                       lockablenames => \@lockablenames,
 7670:                                     }
 7671:                      );
 7672:     foreach my $key (keys(%currautoupdate)) {
 7673:         if (($key eq 'run') || ($key eq 'classlists')) {
 7674:             if (exists($updatehash{autoupdate}{$key})) {
 7675:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 7676:                     $changes{$key} = 1;
 7677:                 }
 7678:             }
 7679:         } elsif ($key eq 'fields') {
 7680:             if (ref($currautoupdate{$key}) eq 'HASH') {
 7681:                 foreach my $item (@{$types},'default') {
 7682:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 7683:                         my $change = 0;
 7684:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 7685:                             if (!exists($fields{$item})) {
 7686:                                 $change = 1;
 7687:                                 last;
 7688:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 7689:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 7690:                                     $change = 1;
 7691:                                     last;
 7692:                                 }
 7693:                             }
 7694:                         }
 7695:                         if ($change) {
 7696:                             push(@{$changes{$key}},$item);
 7697:                         }
 7698:                     } 
 7699:                 }
 7700:             }
 7701:         } elsif ($key eq 'lockablenames') {
 7702:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 7703:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7704:                 if (@changed) {
 7705:                     $changes{'lockablenames'} = 1;
 7706:                 }
 7707:             } else {
 7708:                 if (@lockablenames) {
 7709:                     $changes{'lockablenames'} = 1;
 7710:                 }
 7711:             }
 7712:         }
 7713:     }
 7714:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 7715:         if (@lockablenames) {
 7716:             $changes{'lockablenames'} = 1;
 7717:         }
 7718:     }
 7719:     foreach my $item (@{$types},'default') {
 7720:         if (defined($fields{$item})) {
 7721:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 7722:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 7723:                     my $change = 0;
 7724:                     if (ref($fields{$item}) eq 'ARRAY') {
 7725:                         foreach my $type (@{$fields{$item}}) {
 7726:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 7727:                                 $change = 1;
 7728:                                 last;
 7729:                             }
 7730:                         }
 7731:                     }
 7732:                     if ($change) {
 7733:                         push(@{$changes{'fields'}},$item);
 7734:                     }
 7735:                 } else {
 7736:                     push(@{$changes{'fields'}},$item);
 7737:                 }
 7738:             } else {
 7739:                 push(@{$changes{'fields'}},$item);
 7740:             }
 7741:         }
 7742:     }
 7743:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7744:                                              $dom);
 7745:     if ($putresult eq 'ok') {
 7746:         if (keys(%changes) > 0) {
 7747:             $resulttext = &mt('Changes made:').'<ul>';
 7748:             foreach my $key (sort(keys(%changes))) {
 7749:                 if ($key eq 'lockablenames') {
 7750:                     $resulttext .= '<li>';
 7751:                     if (@lockablenames) {
 7752:                         $usertypes->{'default'} = $othertitle;
 7753:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7754:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7755:                     } else {
 7756:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7757:                     }
 7758:                     $resulttext .= '</li>';
 7759:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7760:                     foreach my $item (@{$changes{$key}}) {
 7761:                         my @newvalues;
 7762:                         foreach my $type (@{$fields{$item}}) {
 7763:                             push(@newvalues,$fieldtitles{$type});
 7764:                         }
 7765:                         my $newvaluestr;
 7766:                         if (@newvalues > 0) {
 7767:                             $newvaluestr = join(', ',@newvalues);
 7768:                         } else {
 7769:                             $newvaluestr = &mt('none');
 7770:                         }
 7771:                         if ($item eq 'default') {
 7772:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7773:                         } else {
 7774:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7775:                         }
 7776:                     }
 7777:                 } else {
 7778:                     my $newvalue;
 7779:                     if ($key eq 'run') {
 7780:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7781:                     } else {
 7782:                         $newvalue = $offon[$env{'form.'.$key}];
 7783:                     }
 7784:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7785:                 }
 7786:             }
 7787:             $resulttext .= '</ul>';
 7788:         } else {
 7789:             $resulttext = &mt('No changes made to autoupdates');
 7790:         }
 7791:     } else {
 7792:         $resulttext = '<span class="LC_error">'.
 7793: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7794:     }
 7795:     return $resulttext;
 7796: }
 7797: 
 7798: sub modify_autocreate {
 7799:     my ($dom,%domconfig) = @_;
 7800:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7801:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7802:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7803:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7804:         }
 7805:     }
 7806:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7807:                  req => 'Auto-creation of validated requests for official courses',
 7808:                  xmldc => 'Identity of course creator of courses from XML files',
 7809:                );
 7810:     my @types = ('xml','req');
 7811:     foreach my $item (@types) {
 7812:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7813:         $newvals{$item} =~ s/\D//g;
 7814:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7815:     }
 7816:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7817:     my %domcoords = &get_active_dcs($dom);
 7818:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7819:         $newvals{'xmldc'} = '';
 7820:     } 
 7821:     %autocreatehash =  (
 7822:                         autocreate => { xml => $newvals{'xml'},
 7823:                                         req => $newvals{'req'},
 7824:                                       }
 7825:                        );
 7826:     if ($newvals{'xmldc'} ne '') {
 7827:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7828:     }
 7829:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7830:                                              $dom);
 7831:     if ($putresult eq 'ok') {
 7832:         my @items = @types;
 7833:         if ($newvals{'xml'}) {
 7834:             push(@items,'xmldc');
 7835:         }
 7836:         foreach my $item (@items) {
 7837:             if (exists($currautocreate{$item})) {
 7838:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7839:                     $changes{$item} = 1;
 7840:                 }
 7841:             } elsif ($newvals{$item}) {
 7842:                 $changes{$item} = 1;
 7843:             }
 7844:         }
 7845:         if (keys(%changes) > 0) {
 7846:             my @offon = ('off','on'); 
 7847:             $resulttext = &mt('Changes made:').'<ul>';
 7848:             foreach my $item (@types) {
 7849:                 if ($changes{$item}) {
 7850:                     my $newtxt = $offon[$newvals{$item}];
 7851:                     $resulttext .= '<li>'.
 7852:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7853:                                        '<b>','</b>').
 7854:                                    '</li>';
 7855:                 }
 7856:             }
 7857:             if ($changes{'xmldc'}) {
 7858:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7859:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7860:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7861:             }
 7862:             $resulttext .= '</ul>';
 7863:         } else {
 7864:             $resulttext = &mt('No changes made to auto-creation settings');
 7865:         }
 7866:     } else {
 7867:         $resulttext = '<span class="LC_error">'.
 7868:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7869:     }
 7870:     return $resulttext;
 7871: }
 7872: 
 7873: sub modify_directorysrch {
 7874:     my ($dom,%domconfig) = @_;
 7875:     my ($resulttext,%changes);
 7876:     my %currdirsrch;
 7877:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7878:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7879:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7880:         }
 7881:     }
 7882:     my %title = ( available => 'Directory search available',
 7883:                   localonly => 'Other domains can search',
 7884:                   searchby => 'Search types',
 7885:                   searchtypes => 'Search latitude');
 7886:     my @offon = ('off','on');
 7887:     my @otherdoms = ('Yes','No');
 7888: 
 7889:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7890:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7891:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7892: 
 7893:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7894:     if (keys(%{$usertypes}) == 0) {
 7895:         @cansearch = ('default');
 7896:     } else {
 7897:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7898:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7899:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7900:                     push(@{$changes{'cansearch'}},$type);
 7901:                 }
 7902:             }
 7903:             foreach my $type (@cansearch) {
 7904:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7905:                     push(@{$changes{'cansearch'}},$type);
 7906:                 }
 7907:             }
 7908:         } else {
 7909:             push(@{$changes{'cansearch'}},@cansearch);
 7910:         }
 7911:     }
 7912: 
 7913:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7914:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7915:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7916:                 push(@{$changes{'searchby'}},$by);
 7917:             }
 7918:         }
 7919:         foreach my $by (@searchby) {
 7920:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7921:                 push(@{$changes{'searchby'}},$by);
 7922:             }
 7923:         }
 7924:     } else {
 7925:         push(@{$changes{'searchby'}},@searchby);
 7926:     }
 7927: 
 7928:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7929:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7930:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7931:                 push(@{$changes{'searchtypes'}},$type);
 7932:             }
 7933:         }
 7934:         foreach my $type (@searchtypes) {
 7935:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7936:                 push(@{$changes{'searchtypes'}},$type);
 7937:             }
 7938:         }
 7939:     } else {
 7940:         if (exists($currdirsrch{'searchtypes'})) {
 7941:             foreach my $type (@searchtypes) {  
 7942:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7943:                     push(@{$changes{'searchtypes'}},$type);
 7944:                 }
 7945:             }
 7946:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7947:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7948:             }   
 7949:         } else {
 7950:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7951:         }
 7952:     }
 7953: 
 7954:     my %dirsrch_hash =  (
 7955:             directorysrch => { available => $env{'form.dirsrch_available'},
 7956:                                cansearch => \@cansearch,
 7957:                                localonly => $env{'form.dirsrch_localonly'},
 7958:                                searchby => \@searchby,
 7959:                                searchtypes => \@searchtypes,
 7960:                              }
 7961:             );
 7962:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7963:                                              $dom);
 7964:     if ($putresult eq 'ok') {
 7965:         if (exists($currdirsrch{'available'})) {
 7966:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7967:                  $changes{'available'} = 1;
 7968:              }
 7969:         } else {
 7970:             if ($env{'form.dirsrch_available'} eq '1') {
 7971:                 $changes{'available'} = 1;
 7972:             }
 7973:         }
 7974:         if (exists($currdirsrch{'localonly'})) {
 7975:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7976:                  $changes{'localonly'} = 1;
 7977:              }
 7978:         } else {
 7979:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7980:                 $changes{'localonly'} = 1;
 7981:             }
 7982:         }
 7983:         if (keys(%changes) > 0) {
 7984:             $resulttext = &mt('Changes made:').'<ul>';
 7985:             if ($changes{'available'}) {
 7986:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7987:             }
 7988:             if ($changes{'localonly'}) {
 7989:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7990:             }
 7991: 
 7992:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7993:                 my $chgtext;
 7994:                 if (ref($usertypes) eq 'HASH') {
 7995:                     if (keys(%{$usertypes}) > 0) {
 7996:                         foreach my $type (@{$types}) {
 7997:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7998:                                 $chgtext .= $usertypes->{$type}.'; ';
 7999:                             }
 8000:                         }
 8001:                         if (grep(/^default$/,@cansearch)) {
 8002:                             $chgtext .= $othertitle;
 8003:                         } else {
 8004:                             $chgtext =~ s/\; $//;
 8005:                         }
 8006:                         $resulttext .=
 8007:                             '<li>'.
 8008:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 8009:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 8010:                             '</li>';
 8011:                     }
 8012:                 }
 8013:             }
 8014:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 8015:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 8016:                 my $chgtext;
 8017:                 foreach my $type (@{$titleorder}) {
 8018:                     if (grep(/^\Q$type\E$/,@searchby)) {
 8019:                         if (defined($searchtitles->{$type})) {
 8020:                             $chgtext .= $searchtitles->{$type}.'; ';
 8021:                         }
 8022:                     }
 8023:                 }
 8024:                 $chgtext =~ s/\; $//;
 8025:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 8026:             }
 8027:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 8028:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 8029:                 my $chgtext;
 8030:                 foreach my $type (@{$srchtypeorder}) {
 8031:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 8032:                         if (defined($srchtypes_desc->{$type})) {
 8033:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 8034:                         }
 8035:                     }
 8036:                 }
 8037:                 $chgtext =~ s/\; $//;
 8038:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 8039:             }
 8040:             $resulttext .= '</ul>';
 8041:         } else {
 8042:             $resulttext = &mt('No changes made to institution directory search settings');
 8043:         }
 8044:     } else {
 8045:         $resulttext = '<span class="LC_error">'.
 8046:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 8047:     }
 8048:     return $resulttext;
 8049: }
 8050: 
 8051: sub modify_contacts {
 8052:     my ($dom,$lastactref,%domconfig) = @_;
 8053:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 8054:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 8055:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 8056:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 8057:         }
 8058:     }
 8059:     my (%others,%to,%bcc);
 8060:     my @contacts = ('supportemail','adminemail');
 8061:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 8062:                     'requestsmail','updatesmail','idconflictsmail');
 8063:     my @toggles = ('reporterrors','reportupdates');
 8064:     foreach my $type (@mailings) {
 8065:         @{$newsetting{$type}} = 
 8066:             &Apache::loncommon::get_env_multiple('form.'.$type);
 8067:         foreach my $item (@contacts) {
 8068:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 8069:                 $contacts_hash{contacts}{$type}{$item} = 1;
 8070:             } else {
 8071:                 $contacts_hash{contacts}{$type}{$item} = 0;
 8072:             }
 8073:         }  
 8074:         $others{$type} = $env{'form.'.$type.'_others'};
 8075:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 8076:         if ($type eq 'helpdeskmail') {
 8077:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 8078:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 8079:         }
 8080:     }
 8081:     foreach my $item (@contacts) {
 8082:         $to{$item} = $env{'form.'.$item};
 8083:         $contacts_hash{'contacts'}{$item} = $to{$item};
 8084:     }
 8085:     foreach my $item (@toggles) {
 8086:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 8087:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 8088:         }
 8089:     }
 8090:     if (keys(%currsetting) > 0) {
 8091:         foreach my $item (@contacts) {
 8092:             if ($to{$item} ne $currsetting{$item}) {
 8093:                 $changes{$item} = 1;
 8094:             }
 8095:         }
 8096:         foreach my $type (@mailings) {
 8097:             foreach my $item (@contacts) {
 8098:                 if (ref($currsetting{$type}) eq 'HASH') {
 8099:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 8100:                         push(@{$changes{$type}},$item);
 8101:                     }
 8102:                 } else {
 8103:                     push(@{$changes{$type}},@{$newsetting{$type}});
 8104:                 }
 8105:             }
 8106:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 8107:                 push(@{$changes{$type}},'others');
 8108:             }
 8109:             if ($type eq 'helpdeskmail') {   
 8110:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 8111:                     push(@{$changes{$type}},'bcc'); 
 8112:                 }
 8113:             }
 8114:         }
 8115:     } else {
 8116:         my %default;
 8117:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 8118:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 8119:         $default{'errormail'} = 'adminemail';
 8120:         $default{'packagesmail'} = 'adminemail';
 8121:         $default{'helpdeskmail'} = 'supportemail';
 8122:         $default{'lonstatusmail'} = 'adminemail';
 8123:         $default{'requestsmail'} = 'adminemail';
 8124:         $default{'updatesmail'} = 'adminemail';
 8125:         foreach my $item (@contacts) {
 8126:            if ($to{$item} ne $default{$item}) {
 8127:               $changes{$item} = 1;
 8128:            }
 8129:         }
 8130:         foreach my $type (@mailings) {
 8131:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 8132:                
 8133:                 push(@{$changes{$type}},@{$newsetting{$type}});
 8134:             }
 8135:             if ($others{$type} ne '') {
 8136:                 push(@{$changes{$type}},'others');
 8137:             }
 8138:             if ($type eq 'helpdeskmail') {
 8139:                 if ($bcc{$type} ne '') {
 8140:                     push(@{$changes{$type}},'bcc');
 8141:                 }
 8142:             }
 8143:         }
 8144:     }
 8145:     foreach my $item (@toggles) {
 8146:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 8147:             $changes{$item} = 1;
 8148:         } elsif ((!$env{'form.'.$item}) &&
 8149:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 8150:             $changes{$item} = 1;
 8151:         }
 8152:     }
 8153:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 8154:                                              $dom);
 8155:     if ($putresult eq 'ok') {
 8156:         if (keys(%changes) > 0) {
 8157:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8158:             if (ref($lastactref) eq 'HASH') {
 8159:                 $lastactref->{'domainconfig'} = 1;
 8160:             }
 8161:             my ($titles,$short_titles)  = &contact_titles();
 8162:             $resulttext = &mt('Changes made:').'<ul>';
 8163:             foreach my $item (@contacts) {
 8164:                 if ($changes{$item}) {
 8165:                     $resulttext .= '<li>'.$titles->{$item}.
 8166:                                     &mt(' set to: ').
 8167:                                     '<span class="LC_cusr_emph">'.
 8168:                                     $to{$item}.'</span></li>';
 8169:                 }
 8170:             }
 8171:             foreach my $type (@mailings) {
 8172:                 if (ref($changes{$type}) eq 'ARRAY') {
 8173:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 8174:                     my @text;
 8175:                     foreach my $item (@{$newsetting{$type}}) {
 8176:                         push(@text,$short_titles->{$item});
 8177:                     }
 8178:                     if ($others{$type} ne '') {
 8179:                         push(@text,$others{$type});
 8180:                     }
 8181:                     $resulttext .= '<span class="LC_cusr_emph">'.
 8182:                                    join(', ',@text).'</span>';
 8183:                     if ($type eq 'helpdeskmail') {
 8184:                         if ($bcc{$type} ne '') {
 8185:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 8186:                         }
 8187:                     }
 8188:                     $resulttext .= '</li>';
 8189:                 }
 8190:             }
 8191:             my @offon = ('off','on');
 8192:             if ($changes{'reporterrors'}) {
 8193:                 $resulttext .= '<li>'.
 8194:                                &mt('E-mail error reports to [_1] set to "'.
 8195:                                    $offon[$env{'form.reporterrors'}].'".',
 8196:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8197:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 8198:                                '</li>';
 8199:             }
 8200:             if ($changes{'reportupdates'}) {
 8201:                 $resulttext .= '<li>'.
 8202:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 8203:                                     $offon[$env{'form.reportupdates'}].'".',
 8204:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8205:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 8206:                                 '</li>';
 8207:             }
 8208:             $resulttext .= '</ul>';
 8209:         } else {
 8210:             $resulttext = &mt('No changes made to contact information');
 8211:         }
 8212:     } else {
 8213:         $resulttext = '<span class="LC_error">'.
 8214:             &mt('An error occurred: [_1].',$putresult).'</span>';
 8215:     }
 8216:     return $resulttext;
 8217: }
 8218: 
 8219: sub modify_usercreation {
 8220:     my ($dom,%domconfig) = @_;
 8221:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 8222:     my $warningmsg;
 8223:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8224:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8225:             if ($key eq 'cancreate') {
 8226:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8227:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8228:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8229:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 8230:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8231:                         } else {
 8232:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8233:                         }
 8234:                     }
 8235:                 }
 8236:             } elsif ($key eq 'email_rule') {
 8237:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8238:             } else {
 8239:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8240:             }
 8241:         }
 8242:     }
 8243:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 8244:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 8245:     my @contexts = ('author','course','requestcrs');
 8246:     foreach my $item(@contexts) {
 8247:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 8248:     }
 8249:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8250:         foreach my $item (@contexts) {
 8251:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 8252:                 push(@{$changes{'cancreate'}},$item);
 8253:             }
 8254:         }
 8255:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 8256:         foreach my $item (@contexts) {
 8257:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 8258:                 if ($cancreate{$item} ne 'any') {
 8259:                     push(@{$changes{'cancreate'}},$item);
 8260:                 }
 8261:             } else {
 8262:                 if ($cancreate{$item} ne 'none') {
 8263:                     push(@{$changes{'cancreate'}},$item);
 8264:                 }
 8265:             }
 8266:         }
 8267:     } else {
 8268:         foreach my $item (@contexts)  {
 8269:             push(@{$changes{'cancreate'}},$item);
 8270:         }
 8271:     }
 8272: 
 8273:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 8274:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 8275:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 8276:                 push(@{$changes{'username_rule'}},$type);
 8277:             }
 8278:         }
 8279:         foreach my $type (@username_rule) {
 8280:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 8281:                 push(@{$changes{'username_rule'}},$type);
 8282:             }
 8283:         }
 8284:     } else {
 8285:         push(@{$changes{'username_rule'}},@username_rule);
 8286:     }
 8287: 
 8288:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 8289:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 8290:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 8291:                 push(@{$changes{'id_rule'}},$type);
 8292:             }
 8293:         }
 8294:         foreach my $type (@id_rule) {
 8295:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 8296:                 push(@{$changes{'id_rule'}},$type);
 8297:             }
 8298:         }
 8299:     } else {
 8300:         push(@{$changes{'id_rule'}},@id_rule);
 8301:     }
 8302: 
 8303:     my @authen_contexts = ('author','course','domain');
 8304:     my @authtypes = ('int','krb4','krb5','loc');
 8305:     my %authhash;
 8306:     foreach my $item (@authen_contexts) {
 8307:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 8308:         foreach my $auth (@authtypes) {
 8309:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 8310:                 $authhash{$item}{$auth} = 1;
 8311:             } else {
 8312:                 $authhash{$item}{$auth} = 0;
 8313:             }
 8314:         }
 8315:     }
 8316:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 8317:         foreach my $item (@authen_contexts) {
 8318:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 8319:                 foreach my $auth (@authtypes) {
 8320:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 8321:                         push(@{$changes{'authtypes'}},$item);
 8322:                         last;
 8323:                     }
 8324:                 }
 8325:             }
 8326:         }
 8327:     } else {
 8328:         foreach my $item (@authen_contexts) {
 8329:             push(@{$changes{'authtypes'}},$item);
 8330:         }
 8331:     }
 8332: 
 8333:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 8334:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 8335:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 8336:     $save_usercreate{'id_rule'} = \@id_rule;
 8337:     $save_usercreate{'username_rule'} = \@username_rule,
 8338:     $save_usercreate{'authtypes'} = \%authhash;
 8339: 
 8340:     my %usercreation_hash =  (
 8341:         usercreation     => \%save_usercreate,
 8342:     );
 8343: 
 8344:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 8345:                                              $dom);
 8346: 
 8347:     if ($putresult eq 'ok') {
 8348:         if (keys(%changes) > 0) {
 8349:             $resulttext = &mt('Changes made:').'<ul>';
 8350:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8351:                 my %lt = &usercreation_types();
 8352:                 foreach my $type (@{$changes{'cancreate'}}) {
 8353:                     my $chgtext = $lt{$type}.', ';
 8354:                     if ($cancreate{$type} eq 'none') {
 8355:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 8356:                     } elsif ($cancreate{$type} eq 'any') {
 8357:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 8358:                     } elsif ($cancreate{$type} eq 'official') {
 8359:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 8360:                     } elsif ($cancreate{$type} eq 'unofficial') {
 8361:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 8362:                     }
 8363:                     $resulttext .= '<li>'.$chgtext.'</li>';
 8364:                 }
 8365:             }
 8366:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 8367:                 my ($rules,$ruleorder) = 
 8368:                     &Apache::lonnet::inst_userrules($dom,'username');
 8369:                 my $chgtext = '<ul>';
 8370:                 foreach my $type (@username_rule) {
 8371:                     if (ref($rules->{$type}) eq 'HASH') {
 8372:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 8373:                     }
 8374:                 }
 8375:                 $chgtext .= '</ul>';
 8376:                 if (@username_rule > 0) {
 8377:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 8378:                 } else {
 8379:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 8380:                 }
 8381:             }
 8382:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 8383:                 my ($idrules,$idruleorder) = 
 8384:                     &Apache::lonnet::inst_userrules($dom,'id');
 8385:                 my $chgtext = '<ul>';
 8386:                 foreach my $type (@id_rule) {
 8387:                     if (ref($idrules->{$type}) eq 'HASH') {
 8388:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8389:                     }
 8390:                 }
 8391:                 $chgtext .= '</ul>';
 8392:                 if (@id_rule > 0) {
 8393:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8394:                 } else {
 8395:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8396:                 }
 8397:             }
 8398:             my %authname = &authtype_names();
 8399:             my %context_title = &context_names();
 8400:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8401:                 my $chgtext = '<ul>';
 8402:                 foreach my $type (@{$changes{'authtypes'}}) {
 8403:                     my @allowed;
 8404:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8405:                     foreach my $auth (@authtypes) {
 8406:                         if ($authhash{$type}{$auth}) {
 8407:                             push(@allowed,$authname{$auth});
 8408:                         }
 8409:                     }
 8410:                     if (@allowed > 0) {
 8411:                         $chgtext .= join(', ',@allowed).'</li>';
 8412:                     } else {
 8413:                         $chgtext .= &mt('none').'</li>';
 8414:                     }
 8415:                 }
 8416:                 $chgtext .= '</ul>';
 8417:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8418:                 $resulttext .= '</li>';
 8419:             }
 8420:             $resulttext .= '</ul>';
 8421:         } else {
 8422:             $resulttext = &mt('No changes made to user creation settings');
 8423:         }
 8424:     } else {
 8425:         $resulttext = '<span class="LC_error">'.
 8426:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8427:     }
 8428:     if ($warningmsg ne '') {
 8429:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8430:     }
 8431:     return $resulttext;
 8432: }
 8433: 
 8434: sub modify_selfcreation {
 8435:     my ($dom,%domconfig) = @_;
 8436:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8437:     my (%save_usercreate,%save_usermodify);
 8438:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8439:     if (ref($types) eq 'ARRAY') {
 8440:         $usertypes->{'default'} = $othertitle;
 8441:         push(@{$types},'default');
 8442:     }
 8443: #
 8444: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8445: #
 8446:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8447:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8448:             if ($key eq 'cancreate') {
 8449:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8450:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8451:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8452:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8453:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 8454:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 8455:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8456:                         } else {
 8457:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8458:                         }
 8459:                     }
 8460:                 }
 8461:             } elsif ($key eq 'email_rule') {
 8462:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8463:             } else {
 8464:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8465:             }
 8466:         }
 8467:     }
 8468: #
 8469: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8470: #
 8471:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8472:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8473:             if ($key eq 'selfcreate') {
 8474:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8475:             } else {
 8476:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8477:             }
 8478:         }
 8479:     }
 8480: 
 8481:     my @contexts = ('selfcreate');
 8482:     @{$cancreate{'selfcreate'}} = ();
 8483:     %{$cancreate{'emailusername'}} = ();
 8484:     @{$cancreate{'statustocreate'}} = ();
 8485:     %{$cancreate{'selfcreateprocessing'}} = ();
 8486:     %{$cancreate{'shibenv'}} = ();
 8487:     my %selfcreatetypes = (
 8488:                              sso   => 'users authenticated by institutional single sign on',
 8489:                              login => 'users authenticated by institutional log-in',
 8490:                              email => 'users who provide a valid e-mail address for use as username',
 8491:                           );
 8492: #
 8493: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8494: # is permitted.
 8495: #
 8496: 
 8497:     my @statuses;
 8498:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 8499:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 8500:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 8501:         }
 8502:     }
 8503:     push(@statuses,'default');
 8504: 
 8505:     foreach my $item ('login','sso','email') {
 8506:         if ($item eq 'email') {
 8507:             if ($env{'form.cancreate_email'}) {
 8508:                 push(@{$cancreate{'selfcreate'}},'email');
 8509:                 push(@contexts,'selfcreateprocessing');
 8510:                 foreach my $type (@statuses) {
 8511:                     if ($type eq 'default') {
 8512:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 8513:                     } else { 
 8514:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 8515:                     }
 8516:                 }
 8517:             }
 8518:         } else {
 8519:             if ($env{'form.cancreate_'.$item}) {
 8520:                 push(@{$cancreate{'selfcreate'}},$item);
 8521:             }
 8522:         }
 8523:     }
 8524:     my (@email_rule,%userinfo,%savecaptcha);
 8525:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8526: #
 8527: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8528: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8529: #
 8530: 
 8531:     if ($env{'form.cancreate_email'}) {
 8532:         push(@contexts,'emailusername');
 8533:         if (ref($types) eq 'ARRAY') {
 8534:             foreach my $type (@{$types}) {
 8535:                 if (ref($infofields) eq 'ARRAY') {
 8536:                     foreach my $field (@{$infofields}) {
 8537:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8538:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8539:                         }
 8540:                     }
 8541:                 }
 8542:             }
 8543:         }
 8544: #
 8545: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8546: # queued requests for self-creation of account using e-mail address as username
 8547: #
 8548: 
 8549:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8550:         @approvalnotify = sort(@approvalnotify);
 8551:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8552:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8553:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8554:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8555:                     push(@{$changes{'cancreate'}},'notify');
 8556:                 }
 8557:             } else {
 8558:                 if ($cancreate{'notify'}{'approval'}) {
 8559:                     push(@{$changes{'cancreate'}},'notify');
 8560:                 }
 8561:             }
 8562:         } elsif ($cancreate{'notify'}{'approval'}) {
 8563:             push(@{$changes{'cancreate'}},'notify');
 8564:         }
 8565: 
 8566: #
 8567: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8568: #
 8569:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8570:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8571:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8572:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8573:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8574:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8575:                         push(@{$changes{'email_rule'}},$type);
 8576:                     }
 8577:                 }
 8578:             }
 8579:             if (@email_rule > 0) {
 8580:                 foreach my $type (@email_rule) {
 8581:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8582:                         push(@{$changes{'email_rule'}},$type);
 8583:                     }
 8584:                 }
 8585:             }
 8586:         } elsif (@email_rule > 0) {
 8587:             push(@{$changes{'email_rule'}},@email_rule);
 8588:         }
 8589:     }
 8590: #  
 8591: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 8592: # institutional log-in.
 8593: #
 8594:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8595:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8596:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8597:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8598:             $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.').' '.
 8599:                           &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.');
 8600:         }
 8601:     }
 8602:     my @fields = ('lastname','firstname','middlename','generation',
 8603:                   'permanentemail','id');
 8604:     my @shibfields = (@fields,'inststatus');
 8605:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8606: #
 8607: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8608: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8609: # may self-create accounts 
 8610: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8611: # which the user may supply, if institutional data is unavailable.
 8612: #
 8613:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8614:         if (ref($types) eq 'ARRAY') {
 8615:             if (@{$types} > 1) {
 8616:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8617:                 push(@contexts,'statustocreate');
 8618:             } else {
 8619:                 undef($cancreate{'statustocreate'});
 8620:             } 
 8621:             foreach my $type (@{$types}) {
 8622:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8623:                 foreach my $field (@fields) {
 8624:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8625:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8626:                     } else {
 8627:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8628:                     }
 8629:                 }
 8630:             }
 8631:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8632:                 foreach my $type (@{$types}) {
 8633:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8634:                         foreach my $field (@fields) {
 8635:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8636:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8637:                                 push(@{$changes{'selfcreate'}},$type);
 8638:                                 last;
 8639:                             }
 8640:                         }
 8641:                     }
 8642:                 }
 8643:             } else {
 8644:                 foreach my $type (@{$types}) {
 8645:                     push(@{$changes{'selfcreate'}},$type);
 8646:                 }
 8647:             }
 8648:         }
 8649:         foreach my $field (@shibfields) {
 8650:             if ($env{'form.shibenv_'.$field} ne '') {
 8651:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 8652:             }
 8653:         }
 8654:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8655:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 8656:                 foreach my $field (@shibfields) {
 8657:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 8658:                         push(@{$changes{'cancreate'}},'shibenv');
 8659:                     }
 8660:                 }
 8661:             } else {
 8662:                 foreach my $field (@shibfields) {
 8663:                     if ($env{'form.shibenv_'.$field}) {
 8664:                         push(@{$changes{'cancreate'}},'shibenv');
 8665:                         last;
 8666:                     }
 8667:                 }
 8668:             }
 8669:         }
 8670:     }
 8671:     foreach my $item (@contexts) {
 8672:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 8673:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 8674:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 8675:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 8676:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8677:                             push(@{$changes{'cancreate'}},$item);
 8678:                         }
 8679:                     }
 8680:                 }
 8681:             }
 8682:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8683:                 foreach my $type (@{$cancreate{$item}}) {
 8684:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 8685:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8686:                             push(@{$changes{'cancreate'}},$item);
 8687:                         }
 8688:                     }
 8689:                 }
 8690:             }
 8691:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 8692:             if (ref($cancreate{$item}) eq 'HASH') {
 8693:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 8694:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 8695:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 8696:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 8697:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8698:                                     push(@{$changes{'cancreate'}},$item);
 8699:                                 }
 8700:                             }
 8701:                         }
 8702:                     } elsif ($item eq 'selfcreateprocessing') {
 8703:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 8704:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8705:                                 push(@{$changes{'cancreate'}},$item);
 8706:                             }
 8707:                         }
 8708:                     } else {
 8709:                         if (!$cancreate{$item}{$curr}) {
 8710:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8711:                                 push(@{$changes{'cancreate'}},$item);
 8712:                             }
 8713:                         }
 8714:                     }
 8715:                 }
 8716:                 foreach my $field (keys(%{$cancreate{$item}})) {
 8717:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 8718:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 8719:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 8720:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 8721:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8722:                                         push(@{$changes{'cancreate'}},$item);
 8723:                                     }
 8724:                                 }
 8725:                             } else {
 8726:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8727:                                     push(@{$changes{'cancreate'}},$item);
 8728:                                 }
 8729:                             }
 8730:                         }
 8731:                     } elsif ($item eq 'selfcreateprocessing') {
 8732:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 8733:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8734:                                 push(@{$changes{'cancreate'}},$item);
 8735:                             }
 8736:                         }
 8737:                     } else {
 8738:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 8739:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8740:                                 push(@{$changes{'cancreate'}},$item);
 8741:                             }
 8742:                         }
 8743:                     }
 8744:                 }
 8745:             }
 8746:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 8747:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8748:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 8749:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8750:                         push(@{$changes{'cancreate'}},$item);
 8751:                     }
 8752:                 }
 8753:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 8754:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 8755:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8756:                         push(@{$changes{'cancreate'}},$item);
 8757:                     }
 8758:                 }
 8759:             }
 8760:         } elsif ($item eq 'emailusername') {
 8761:             if (ref($cancreate{$item}) eq 'HASH') {
 8762:                 foreach my $type (keys(%{$cancreate{$item}})) {
 8763:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 8764:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 8765:                             if ($cancreate{$item}{$type}{$field}) {
 8766:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8767:                                     push(@{$changes{'cancreate'}},$item);
 8768:                                 }
 8769:                                 last;
 8770:                             }
 8771:                         }
 8772:                     }
 8773:                 }
 8774:             }
 8775:         }
 8776:     }
 8777: #
 8778: # Populate %save_usercreate hash with updates to self-creation configuration.
 8779: #
 8780:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 8781:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 8782:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 8783:     if (ref($cancreate{'notify'}) eq 'HASH') {
 8784:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 8785:     }
 8786:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
 8787:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
 8788:     }
 8789:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8790:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 8791:     }
 8792:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
 8793:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
 8794:     }
 8795:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 8796:     $save_usercreate{'emailrule'} = \@email_rule;
 8797: 
 8798:     my %userconfig_hash = (
 8799:             usercreation     => \%save_usercreate,
 8800:             usermodification => \%save_usermodify,
 8801:     );
 8802:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8803:                                              $dom);
 8804: #
 8805: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8806: #
 8807:     if ($putresult eq 'ok') {
 8808:         if (keys(%changes) > 0) {
 8809:             $resulttext = &mt('Changes made:').'<ul>';
 8810:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8811:                 my %lt = &selfcreation_types();
 8812:                 foreach my $type (@{$changes{'cancreate'}}) {
 8813:                     my $chgtext;
 8814:                     if ($type eq 'selfcreate') {
 8815:                         if (@{$cancreate{$type}} == 0) {
 8816:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 8817:                         } else {
 8818:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 8819:                                         '<ul>';
 8820:                             foreach my $case (@{$cancreate{$type}}) {
 8821:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 8822:                             }
 8823:                             $chgtext .= '</ul>';
 8824:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 8825:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 8826:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8827:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 8828:                                             $chgtext .= '<br />'.
 8829:                                                         '<span class="LC_warning">'.
 8830:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8831:                                                         '</span>';
 8832:                                         }
 8833:                                     }
 8834:                                 }
 8835:                             }
 8836:                         }
 8837:                     } elsif ($type eq 'shibenv') {
 8838:                         if (keys(%{$cancreate{$type}}) == 0) {
 8839:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
 8840:                         } else {
 8841:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
 8842:                                         '<ul>';
 8843:                             foreach my $field (@shibfields) {
 8844:                                 next if ($cancreate{$type}{$field} eq '');
 8845:                                 if ($field eq 'inststatus') {
 8846:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
 8847:                                 } else {
 8848:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
 8849:                                 }
 8850:                             }
 8851:                             $chgtext .= '</ul>';
 8852:                         }  
 8853:                     } elsif ($type eq 'statustocreate') {
 8854:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 8855:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 8856:                             if (@{$cancreate{'selfcreate'}} > 0) {
 8857:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 8858:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 8859:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 8860:                                         $chgtext .= '<br />'.
 8861:                                                     '<span class="LC_warning">'.
 8862:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8863:                                                     '</span>';
 8864:                                     }
 8865:                                 } elsif (ref($usertypes) eq 'HASH') {
 8866:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8867:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 8868:                                     } else {
 8869:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 8870:                                     }
 8871:                                     $chgtext .= '<ul>';
 8872:                                     foreach my $case (@{$cancreate{$type}}) {
 8873:                                         if ($case eq 'default') {
 8874:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 8875:                                         } else {
 8876:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 8877:                                         }
 8878:                                     }
 8879:                                     $chgtext .= '</ul>';
 8880:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8881:                                         $chgtext .= '<br /><span class="LC_warning">'.
 8882:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 8883:                                                     '</span>';
 8884:                                     }
 8885:                                 }
 8886:                             } else {
 8887:                                 if (@{$cancreate{$type}} == 0) {
 8888:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8889:                                 } else {
 8890:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8891:                                 }
 8892:                             }
 8893:                         }
 8894:                     } elsif ($type eq 'selfcreateprocessing') {
 8895:                         my %choices = &Apache::lonlocal::texthash (
 8896:                                                                     automatic => 'Automatic approval',
 8897:                                                                     approval  => 'Queued for approval',
 8898:                                                                   );
 8899:                         if (@statuses > 1) {
 8900:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
 8901:                                         '<ul>';
 8902:                            foreach my $type (@statuses) {
 8903:                                if ($type eq 'default') {
 8904:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8905:                                } else {
 8906:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8907:                                }
 8908:                            }
 8909:                            $chgtext .= '</ul>';
 8910:                         } else {
 8911:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
 8912:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
 8913:                         }
 8914:                     } elsif ($type eq 'captcha') {
 8915:                         if ($savecaptcha{$type} eq 'notused') {
 8916:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8917:                         } else {
 8918:                             my %captchas = &captcha_phrases();
 8919:                             if ($captchas{$savecaptcha{$type}}) {
 8920:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8921:                             } else {
 8922:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8923:                             }
 8924:                         }
 8925:                     } elsif ($type eq 'recaptchakeys') {
 8926:                         my ($privkey,$pubkey);
 8927:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8928:                             $pubkey = $savecaptcha{$type}{'public'};
 8929:                             $privkey = $savecaptcha{$type}{'private'};
 8930:                         }
 8931:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8932:                         if (!$pubkey) {
 8933:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8934:                         } else {
 8935:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8936:                         }
 8937:                         if (!$privkey) {
 8938:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8939:                         } else {
 8940:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8941:                         }
 8942:                         $chgtext .= '</ul>';
 8943:                     } elsif ($type eq 'emailusername') {
 8944:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8945:                             if (ref($types) eq 'ARRAY') {
 8946:                                 foreach my $type (@{$types}) {
 8947:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 8948:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 8949:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
 8950:                                                     '<ul>';
 8951:                                             foreach my $field (@{$infofields}) {
 8952:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 8953:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8954:                                                 }
 8955:                                             }
 8956:                                             $chgtext .= '</ul>';
 8957:                                         } else {
 8958:                                             $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 />';
 8959:                                         }
 8960:                                     } else {
 8961:                                         $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 />';
 8962:                                     }
 8963:                                 }
 8964:                             }
 8965:                         }
 8966:                     } elsif ($type eq 'notify') {
 8967:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8968:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8969:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8970:                                 if ($cancreate{'notify'}{'approval'}) {
 8971:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8972:                                 }
 8973:                             }
 8974:                         }
 8975:                     }
 8976:                     if ($chgtext) {
 8977:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8978:                     }
 8979:                 }
 8980:             }
 8981:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8982:                 my ($emailrules,$emailruleorder) =
 8983:                     &Apache::lonnet::inst_userrules($dom,'email');
 8984:                 my $chgtext = '<ul>';
 8985:                 foreach my $type (@email_rule) {
 8986:                     if (ref($emailrules->{$type}) eq 'HASH') {
 8987:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 8988:                     }
 8989:                 }
 8990:                 $chgtext .= '</ul>';
 8991:                 if (@email_rule > 0) {
 8992:                     $resulttext .= '<li>'.
 8993:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 8994:                                        $chgtext.
 8995:                                    '</li>';
 8996:                 } else {
 8997:                     $resulttext .= '<li>'.
 8998:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 8999:                                    '</li>';
 9000:                 }
 9001:             }
 9002:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 9003:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 9004:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9005:                 foreach my $type (@{$changes{'selfcreate'}}) {
 9006:                     my $typename = $type;
 9007:                     if (ref($usertypes) eq 'HASH') {
 9008:                         if ($usertypes->{$type} ne '') {
 9009:                             $typename = $usertypes->{$type};
 9010:                         }
 9011:                     }
 9012:                     my @modifiable;
 9013:                     $resulttext .= '<li>'.
 9014:                                     &mt('Self-creation of account by users with status: [_1]',
 9015:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 9016:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 9017:                     foreach my $field (@fields) {
 9018:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 9019:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 9020:                         }
 9021:                     }
 9022:                     if (@modifiable > 0) {
 9023:                         $resulttext .= join(', ',@modifiable);
 9024:                     } else {
 9025:                         $resulttext .= &mt('none');
 9026:                     }
 9027:                     $resulttext .= '</li>';
 9028:                 }
 9029:                 $resulttext .= '</ul></li>';
 9030:             }
 9031:             $resulttext .= '</ul>';
 9032:         } else {
 9033:             $resulttext = &mt('No changes made to self-creation settings');
 9034:         }
 9035:     } else {
 9036:         $resulttext = '<span class="LC_error">'.
 9037:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9038:     }
 9039:     if ($warningmsg ne '') {
 9040:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9041:     }
 9042:     return $resulttext;
 9043: }
 9044: 
 9045: sub process_captcha {
 9046:     my ($container,$changes,$newsettings,$current) = @_;
 9047:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 9048:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 9049:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 9050:         $newsettings->{'captcha'} = 'original';
 9051:     }
 9052:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 9053:         if ($container eq 'cancreate') {
 9054:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9055:                 push(@{$changes->{'cancreate'}},'captcha');
 9056:             } elsif (!defined($changes->{'cancreate'})) {
 9057:                 $changes->{'cancreate'} = ['captcha'];
 9058:             }
 9059:         } else {
 9060:             $changes->{'captcha'} = 1;
 9061:         }
 9062:     }
 9063:     my ($newpub,$newpriv,$currpub,$currpriv);
 9064:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 9065:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 9066:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 9067:         $newpub =~ s/[^\w\-]//g;
 9068:         $newpriv =~ s/[^\w\-]//g;
 9069:         $newsettings->{'recaptchakeys'} = {
 9070:                                              public  => $newpub,
 9071:                                              private => $newpriv,
 9072:                                           };
 9073:     }
 9074:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 9075:         $currpub = $current->{'recaptchakeys'}{'public'};
 9076:         $currpriv = $current->{'recaptchakeys'}{'private'};
 9077:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 9078:             $newsettings->{'recaptchakeys'} = {
 9079:                                                  public  => '',
 9080:                                                  private => '',
 9081:                                               }
 9082:         }
 9083:     }
 9084:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 9085:         if ($container eq 'cancreate') {
 9086:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9087:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 9088:             } elsif (!defined($changes->{'cancreate'})) {
 9089:                 $changes->{'cancreate'} = ['recaptchakeys'];
 9090:             }
 9091:         } else {
 9092:             $changes->{'recaptchakeys'} = 1;
 9093:         }
 9094:     }
 9095:     return;
 9096: }
 9097: 
 9098: sub modify_usermodification {
 9099:     my ($dom,%domconfig) = @_;
 9100:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 9101:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9102:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9103:             if ($key eq 'selfcreate') {
 9104:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 9105:             } else {  
 9106:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 9107:             }
 9108:         }
 9109:     }
 9110:     my @contexts = ('author','course');
 9111:     my %context_title = (
 9112:                            author => 'In author context',
 9113:                            course => 'In course context',
 9114:                         );
 9115:     my @fields = ('lastname','firstname','middlename','generation',
 9116:                   'permanentemail','id');
 9117:     my %roles = (
 9118:                   author => ['ca','aa'],
 9119:                   course => ['st','ep','ta','in','cr'],
 9120:                 );
 9121:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9122:     foreach my $context (@contexts) {
 9123:         foreach my $role (@{$roles{$context}}) {
 9124:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 9125:             foreach my $item (@fields) {
 9126:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 9127:                     $modifyhash{$context}{$role}{$item} = 1;
 9128:                 } else {
 9129:                     $modifyhash{$context}{$role}{$item} = 0;
 9130:                 }
 9131:             }
 9132:         }
 9133:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 9134:             foreach my $role (@{$roles{$context}}) {
 9135:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 9136:                     foreach my $field (@fields) {
 9137:                         if ($modifyhash{$context}{$role}{$field} ne 
 9138:                                 $curr_usermodification{$context}{$role}{$field}) {
 9139:                             push(@{$changes{$context}},$role);
 9140:                             last;
 9141:                         }
 9142:                     }
 9143:                 }
 9144:             }
 9145:         } else {
 9146:             foreach my $context (@contexts) {
 9147:                 foreach my $role (@{$roles{$context}}) {
 9148:                     push(@{$changes{$context}},$role);
 9149:                 }
 9150:             }
 9151:         }
 9152:     }
 9153:     my %usermodification_hash =  (
 9154:                                    usermodification => \%modifyhash,
 9155:                                  );
 9156:     my $putresult = &Apache::lonnet::put_dom('configuration',
 9157:                                              \%usermodification_hash,$dom);
 9158:     if ($putresult eq 'ok') {
 9159:         if (keys(%changes) > 0) {
 9160:             $resulttext = &mt('Changes made: ').'<ul>';
 9161:             foreach my $context (@contexts) {
 9162:                 if (ref($changes{$context}) eq 'ARRAY') {
 9163:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 9164:                     if (ref($changes{$context}) eq 'ARRAY') {
 9165:                         foreach my $role (@{$changes{$context}}) {
 9166:                             my $rolename;
 9167:                             if ($role eq 'cr') {
 9168:                                 $rolename = &mt('Custom');
 9169:                             } else {
 9170:                                 $rolename = &Apache::lonnet::plaintext($role);
 9171:                             }
 9172:                             my @modifiable;
 9173:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 9174:                             foreach my $field (@fields) {
 9175:                                 if ($modifyhash{$context}{$role}{$field}) {
 9176:                                     push(@modifiable,$fieldtitles{$field});
 9177:                                 }
 9178:                             }
 9179:                             if (@modifiable > 0) {
 9180:                                 $resulttext .= join(', ',@modifiable);
 9181:                             } else {
 9182:                                 $resulttext .= &mt('none'); 
 9183:                             }
 9184:                             $resulttext .= '</li>';
 9185:                         }
 9186:                         $resulttext .= '</ul></li>';
 9187:                     }
 9188:                 }
 9189:             }
 9190:             $resulttext .= '</ul>';
 9191:         } else {
 9192:             $resulttext = &mt('No changes made to user modification settings');
 9193:         }
 9194:     } else {
 9195:         $resulttext = '<span class="LC_error">'.
 9196:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9197:     }
 9198:     return $resulttext;
 9199: }
 9200: 
 9201: sub modify_defaults {
 9202:     my ($dom,$lastactref,%domconfig) = @_;
 9203:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 9204:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9205:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 9206:     my @authtypes = ('internal','krb4','krb5','localauth');
 9207:     foreach my $item (@items) {
 9208:         $newvalues{$item} = $env{'form.'.$item};
 9209:         if ($item eq 'auth_def') {
 9210:             if ($newvalues{$item} ne '') {
 9211:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 9212:                     push(@errors,$item);
 9213:                 }
 9214:             }
 9215:         } elsif ($item eq 'lang_def') {
 9216:             if ($newvalues{$item} ne '') {
 9217:                 if ($newvalues{$item} =~ /^(\w+)/) {
 9218:                     my $langcode = $1;
 9219:                     if ($langcode ne 'x_chef') {
 9220:                         if (code2language($langcode) eq '') {
 9221:                             push(@errors,$item);
 9222:                         }
 9223:                     }
 9224:                 } else {
 9225:                     push(@errors,$item);
 9226:                 }
 9227:             }
 9228:         } elsif ($item eq 'timezone_def') {
 9229:             if ($newvalues{$item} ne '') {
 9230:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 9231:                     push(@errors,$item);   
 9232:                 }
 9233:             }
 9234:         } elsif ($item eq 'datelocale_def') {
 9235:             if ($newvalues{$item} ne '') {
 9236:                 my @datelocale_ids = DateTime::Locale->ids();
 9237:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 9238:                     push(@errors,$item);
 9239:                 }
 9240:             }
 9241:         } elsif ($item eq 'portal_def') {
 9242:             if ($newvalues{$item} ne '') {
 9243:                 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])\/?$/) {
 9244:                     push(@errors,$item);
 9245:                 }
 9246:             }
 9247:         }
 9248:         if (grep(/^\Q$item\E$/,@errors)) {
 9249:             $newvalues{$item} = $domdefaults{$item};
 9250:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 9251:             $changes{$item} = 1;
 9252:         }
 9253:         $domdefaults{$item} = $newvalues{$item};
 9254:     }
 9255:     my %defaults_hash = (
 9256:                          defaults => \%newvalues,
 9257:                         );
 9258:     my $title = &defaults_titles();
 9259: 
 9260:     my $currinststatus;
 9261:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9262:         $currinststatus = $domconfig{'inststatus'};
 9263:     } else {
 9264:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9265:         $currinststatus = {
 9266:                              inststatustypes => $usertypes,
 9267:                              inststatusorder => $types,
 9268:                              inststatusguest => [],
 9269:                           };
 9270:     }
 9271:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
 9272:     my @allpos;
 9273:     my %guests;
 9274:     my %alltypes;
 9275:     my ($currtitles,$currguests,$currorder);
 9276:     if (ref($currinststatus) eq 'HASH') {
 9277:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
 9278:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
 9279:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
 9280:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
 9281:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
 9282:                     }
 9283:                 }
 9284:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
 9285:                     my $position = $env{'form.inststatus_pos_'.$type};
 9286:                     $position =~ s/\D+//g;
 9287:                     $allpos[$position] = $type;
 9288:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
 9289:                     $alltypes{$type} =~ s/`//g;
 9290:                     if ($env{'form.inststatus_guest_'.$type}) {
 9291:                         $guests{$type} = 1;
 9292:                     }
 9293:                 }
 9294:             }
 9295:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
 9296:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
 9297:             }
 9298:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
 9299:             $currtitles =~ s/,$//;
 9300:         }
 9301:     }
 9302:     if ($env{'form.addinststatus'}) {
 9303:         my $newtype = $env{'form.addinststatus'};
 9304:         $newtype =~ s/\W//g;
 9305:         unless (exists($alltypes{$newtype})) {
 9306:             if ($env{'form.addinststatus_guest'}) {
 9307:                 $guests{$newtype} = 1;
 9308:             }
 9309:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
 9310:             $alltypes{$newtype} =~ s/`//g; 
 9311:             my $position = $env{'form.addinststatus_pos'};
 9312:             $position =~ s/\D+//g;
 9313:             if ($position ne '') {
 9314:                 $allpos[$position] = $newtype;
 9315:             }
 9316:         }
 9317:     }
 9318:     my (@orderedstatus,@orderedguests);
 9319:     foreach my $type (@allpos) {
 9320:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
 9321:             push(@orderedstatus,$type);
 9322:             if ($guests{$type}) {
 9323:                 push(@orderedguests,$type);
 9324:             }
 9325:         }
 9326:     }
 9327:     foreach my $type (keys(%alltypes)) {
 9328:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
 9329:             delete($alltypes{$type});
 9330:         }
 9331:     }
 9332:     $defaults_hash{'inststatus'} = {
 9333:                                      inststatustypes => \%alltypes,
 9334:                                      inststatusorder => \@orderedstatus,
 9335:                                      inststatusguest => \@orderedguests,
 9336:                                    };
 9337:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
 9338:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 9339:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
 9340:         }
 9341:     }
 9342:     if ($currorder ne join(',',@orderedstatus)) {
 9343:         $changes{'inststatus'}{'inststatusorder'} = 1;
 9344:     }
 9345:     if ($currguests ne join(',',@orderedguests)) {
 9346:         $changes{'inststatus'}{'inststatusguest'} = 1;
 9347:     }
 9348:     my $newtitles;
 9349:     foreach my $item (@orderedstatus) {
 9350:         $newtitles .= $alltypes{$item}.',';
 9351:     }
 9352:     $newtitles =~ s/,$//;
 9353:     if ($currtitles ne $newtitles) {
 9354:         $changes{'inststatus'}{'inststatustypes'} = 1;
 9355:     }
 9356:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 9357:                                              $dom);
 9358:     if ($putresult eq 'ok') {
 9359:         if (keys(%changes) > 0) {
 9360:             $resulttext = &mt('Changes made:').'<ul>';
 9361:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 9362:             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";
 9363:             foreach my $item (sort(keys(%changes))) {
 9364:                 if ($item eq 'inststatus') {
 9365:                     if (ref($changes{'inststatus'}) eq 'HASH') {
 9366:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
 9367:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
 9368:                             foreach my $type (@orderedstatus) { 
 9369:                                 $resulttext .= $alltypes{$type}.', ';
 9370:                             }
 9371:                             $resulttext =~ s/, $//;
 9372:                             $resulttext .= '</li>';
 9373:                         }
 9374:                         if ($changes{'inststatus'}{'inststatusguest'}) {
 9375:                             $resulttext .= '<li>'; 
 9376:                             if (@orderedguests) {
 9377:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
 9378:                                 foreach my $type (@orderedguests) {
 9379:                                     $resulttext .= $alltypes{$type}.', ';
 9380:                                 }
 9381:                                 $resulttext =~ s/, $//;
 9382:                             } else {
 9383:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
 9384:                             }
 9385:                             $resulttext .= '</li>';
 9386:                         }
 9387:                     }
 9388:                 } else {
 9389:                     my $value = $env{'form.'.$item};
 9390:                     if ($value eq '') {
 9391:                         $value = &mt('none');
 9392:                     } elsif ($item eq 'auth_def') {
 9393:                         my %authnames = &authtype_names();
 9394:                         my %shortauth = (
 9395:                                           internal   => 'int',
 9396:                                           krb4       => 'krb4',
 9397:                                           krb5       => 'krb5',
 9398:                                           localauth  => 'loc',
 9399:                         );
 9400:                         $value = $authnames{$shortauth{$value}};
 9401:                     }
 9402:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 9403:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
 9404:                 }
 9405:             }
 9406:             $resulttext .= '</ul>';
 9407:             $mailmsgtext .= "\n";
 9408:             my $cachetime = 24*60*60;
 9409:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9410:             if (ref($lastactref) eq 'HASH') {
 9411:                 $lastactref->{'domdefaults'} = 1;
 9412:             }
 9413:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 9414:                 my $notify = 1;
 9415:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 9416:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 9417:                         $notify = 0;
 9418:                     }
 9419:                 }
 9420:                 if ($notify) {
 9421:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 9422:                                                "LON-CAPA Domain Settings Change - $dom",
 9423:                                                $mailmsgtext);
 9424:                 }
 9425:             }
 9426:         } else {
 9427:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 9428:         }
 9429:     } else {
 9430:         $resulttext = '<span class="LC_error">'.
 9431:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9432:     }
 9433:     if (@errors > 0) {
 9434:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 9435:         foreach my $item (@errors) {
 9436:             $resulttext .= ' "'.$title->{$item}.'",';
 9437:         }
 9438:         $resulttext =~ s/,$//;
 9439:     }
 9440:     return $resulttext;
 9441: }
 9442: 
 9443: sub modify_scantron {
 9444:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9445:     my ($resulttext,%confhash,%changes,$errors);
 9446:     my $custom = 'custom.tab';
 9447:     my $default = 'default.tab';
 9448:     my $servadm = $r->dir_config('lonAdmEMail');
 9449:     my ($configuserok,$author_ok,$switchserver) = 
 9450:         &config_check($dom,$confname,$servadm);
 9451:     if ($env{'form.scantronformat.filename'} ne '') {
 9452:         my $error;
 9453:         if ($configuserok eq 'ok') {
 9454:             if ($switchserver) {
 9455:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 9456:             } else {
 9457:                 if ($author_ok eq 'ok') {
 9458:                     my ($result,$scantronurl) =
 9459:                         &publishlogo($r,'upload','scantronformat',$dom,
 9460:                                      $confname,'scantron','','',$custom);
 9461:                     if ($result eq 'ok') {
 9462:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 9463:                         $changes{'scantronformat'} = 1;
 9464:                     } else {
 9465:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 9466:                     }
 9467:                 } else {
 9468:                     $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);
 9469:                 }
 9470:             }
 9471:         } else {
 9472:             $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);
 9473:         }
 9474:         if ($error) {
 9475:             &Apache::lonnet::logthis($error);
 9476:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9477:         }
 9478:     }
 9479:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 9480:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 9481:             if ($env{'form.scantronformat_del'}) {
 9482:                 $confhash{'scantron'}{'scantronformat'} = '';
 9483:                 $changes{'scantronformat'} = 1;
 9484:             }
 9485:         }
 9486:     }
 9487:     if (keys(%confhash) > 0) {
 9488:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 9489:                                                  $dom);
 9490:         if ($putresult eq 'ok') {
 9491:             if (keys(%changes) > 0) {
 9492:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 9493:                     $resulttext = &mt('Changes made:').'<ul>';
 9494:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 9495:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 9496:                     } else {
 9497:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 9498:                     }
 9499:                     $resulttext .= '</ul>';
 9500:                 } else {
 9501:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 9502:                 }
 9503:                 $resulttext .= '</ul>';
 9504:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9505:                 if (ref($lastactref) eq 'HASH') {
 9506:                     $lastactref->{'domainconfig'} = 1;
 9507:                 }
 9508:             } else {
 9509:                 $resulttext = &mt('No changes made to bubblesheet format file');
 9510:             }
 9511:         } else {
 9512:             $resulttext = '<span class="LC_error">'.
 9513:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 9514:         }
 9515:     } else {
 9516:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 9517:     }
 9518:     if ($errors) {
 9519:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9520:                        $errors.'</ul>';
 9521:     }
 9522:     return $resulttext;
 9523: }
 9524: 
 9525: sub modify_coursecategories {
 9526:     my ($dom,$lastactref,%domconfig) = @_;
 9527:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 9528:         $cathash);
 9529:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 9530:     my @catitems = ('unauth','auth');
 9531:     my @cattypes = ('std','domonly','codesrch','none');
 9532:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9533:         $cathash = $domconfig{'coursecategories'}{'cats'};
 9534:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 9535:             $changes{'togglecats'} = 1;
 9536:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 9537:         }
 9538:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 9539:             $changes{'categorize'} = 1;
 9540:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 9541:         }
 9542:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 9543:             $changes{'togglecatscomm'} = 1;
 9544:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 9545:         }
 9546:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 9547:             $changes{'categorizecomm'} = 1;
 9548:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 9549:         }
 9550:         foreach my $item (@catitems) {
 9551:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9552:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
 9553:                     $changes{$item} = 1;
 9554:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9555:                 }
 9556:             }
 9557:         }
 9558:     } else {
 9559:         $changes{'togglecats'} = 1;
 9560:         $changes{'categorize'} = 1;
 9561:         $changes{'togglecatscomm'} = 1;
 9562:         $changes{'categorizecomm'} = 1;
 9563:         $domconfig{'coursecategories'} = {
 9564:                                              togglecats => $env{'form.togglecats'},
 9565:                                              categorize => $env{'form.categorize'},
 9566:                                              togglecatscomm => $env{'form.togglecatscomm'},
 9567:                                              categorizecomm => $env{'form.categorizecomm'},
 9568:                                          };
 9569:         foreach my $item (@catitems) {
 9570:             if ($env{'form.coursecat_'.$item} ne 'std') {
 9571:                 $changes{$item} = 1;
 9572:             }
 9573:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9574:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9575:             }
 9576:         }
 9577:     }
 9578:     if (ref($cathash) eq 'HASH') {
 9579:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 9580:             push (@deletecategory,'instcode::0');
 9581:         }
 9582:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 9583:             push(@deletecategory,'communities::0');
 9584:         }
 9585:     }
 9586:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 9587:     if (ref($cathash) eq 'HASH') {
 9588:         if (@deletecategory > 0) {
 9589:             #FIXME Need to remove category from all courses using a deleted category 
 9590:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 9591:             foreach my $item (@deletecategory) {
 9592:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 9593:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 9594:                     $deletions{$item} = 1;
 9595:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 9596:                 }
 9597:             }
 9598:         }
 9599:         foreach my $item (keys(%{$cathash})) {
 9600:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9601:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 9602:                 $reorderings{$item} = 1;
 9603:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 9604:             }
 9605:             if ($env{'form.addcategory_name_'.$item} ne '') {
 9606:                 my $newcat = $env{'form.addcategory_name_'.$item};
 9607:                 my $newdepth = $depth+1;
 9608:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9609:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 9610:                 $adds{$newitem} = 1; 
 9611:             }
 9612:             if ($env{'form.subcat_'.$item} ne '') {
 9613:                 my $newcat = $env{'form.subcat_'.$item};
 9614:                 my $newdepth = $depth+1;
 9615:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9616:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 9617:                 $adds{$newitem} = 1;
 9618:             }
 9619:         }
 9620:     }
 9621:     if ($env{'form.instcode'} eq '1') {
 9622:         if (ref($cathash) eq 'HASH') {
 9623:             my $newitem = 'instcode::0';
 9624:             if ($cathash->{$newitem} eq '') {  
 9625:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9626:                 $adds{$newitem} = 1;
 9627:             }
 9628:         } else {
 9629:             my $newitem = 'instcode::0';
 9630:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9631:             $adds{$newitem} = 1;
 9632:         }
 9633:     }
 9634:     if ($env{'form.communities'} eq '1') {
 9635:         if (ref($cathash) eq 'HASH') {
 9636:             my $newitem = 'communities::0';
 9637:             if ($cathash->{$newitem} eq '') {
 9638:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9639:                 $adds{$newitem} = 1;
 9640:             }
 9641:         } else {
 9642:             my $newitem = 'communities::0';
 9643:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9644:             $adds{$newitem} = 1;
 9645:         }
 9646:     }
 9647:     if ($env{'form.addcategory_name'} ne '') {
 9648:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 9649:             ($env{'form.addcategory_name'} ne 'communities')) {
 9650:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 9651:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 9652:             $adds{$newitem} = 1;
 9653:         }
 9654:     }
 9655:     my $putresult;
 9656:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9657:         if (keys(%deletions) > 0) {
 9658:             foreach my $key (keys(%deletions)) {
 9659:                 if ($predelallitems{$key} ne '') {
 9660:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 9661:                 }
 9662:             }
 9663:         }
 9664:         my (@chkcats,@chktrails,%chkallitems);
 9665:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 9666:         if (ref($chkcats[0]) eq 'ARRAY') {
 9667:             my $depth = 0;
 9668:             my $chg = 0;
 9669:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 9670:                 my $name = $chkcats[0][$i];
 9671:                 my $item;
 9672:                 if ($name eq '') {
 9673:                     $chg ++;
 9674:                 } else {
 9675:                     $item = &escape($name).'::0';
 9676:                     if ($chg) {
 9677:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 9678:                     }
 9679:                     $depth ++; 
 9680:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 9681:                     $depth --;
 9682:                 }
 9683:             }
 9684:         }
 9685:     }
 9686:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9687:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 9688:         if ($putresult eq 'ok') {
 9689:             my %title = (
 9690:                          togglecats     => 'Show/Hide a course in catalog',
 9691:                          categorize     => 'Assign a category to a course',
 9692:                          togglecatscomm => 'Show/Hide a community in catalog',
 9693:                          categorizecomm => 'Assign a category to a community',
 9694:                         );
 9695:             my %level = (
 9696:                          dom  => 'set in Domain ("Modify Course/Community")',
 9697:                          crs  => 'set in Course ("Course Configuration")',
 9698:                          comm => 'set in Community ("Community Configuration")',
 9699:                          none     => 'No catalog',
 9700:                          std      => 'Standard catalog',
 9701:                          domonly  => 'Domain-only catalog',
 9702:                          codesrch => 'Code search form',
 9703:                         );
 9704:             $resulttext = &mt('Changes made:').'<ul>';
 9705:             if ($changes{'togglecats'}) {
 9706:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 9707:             }
 9708:             if ($changes{'categorize'}) {
 9709:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 9710:             }
 9711:             if ($changes{'togglecatscomm'}) {
 9712:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 9713:             }
 9714:             if ($changes{'categorizecomm'}) {
 9715:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 9716:             }
 9717:             if ($changes{'unauth'}) {
 9718:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
 9719:             }
 9720:             if ($changes{'auth'}) {
 9721:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
 9722:             }
 9723:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9724:                 my $cathash;
 9725:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9726:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 9727:                 } else {
 9728:                     $cathash = {};
 9729:                 } 
 9730:                 my (@cats,@trails,%allitems);
 9731:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 9732:                 if (keys(%deletions) > 0) {
 9733:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 9734:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 9735:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 9736:                     }
 9737:                     $resulttext .= '</ul></li>';
 9738:                 }
 9739:                 if (keys(%reorderings) > 0) {
 9740:                     my %sort_by_trail;
 9741:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 9742:                     foreach my $key (keys(%reorderings)) {
 9743:                         if ($allitems{$key} ne '') {
 9744:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9745:                         }
 9746:                     }
 9747:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9748:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9749:                     }
 9750:                     $resulttext .= '</ul></li>';
 9751:                 }
 9752:                 if (keys(%adds) > 0) {
 9753:                     my %sort_by_trail;
 9754:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 9755:                     foreach my $key (keys(%adds)) {
 9756:                         if ($allitems{$key} ne '') {
 9757:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9758:                         }
 9759:                     }
 9760:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9761:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9762:                     }
 9763:                     $resulttext .= '</ul></li>';
 9764:                 }
 9765:             }
 9766:             $resulttext .= '</ul>';
 9767:             if ($changes{'unauth'} || $changes{'auth'}) {
 9768:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 9769:                 if ($changes{'auth'}) {
 9770:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 9771:                 }
 9772:                 if ($changes{'unauth'}) {
 9773:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 9774:                 }
 9775:                 my $cachetime = 24*60*60;
 9776:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9777:                 if (ref($lastactref) eq 'HASH') {
 9778:                     $lastactref->{'domdefaults'} = 1;
 9779:                 }
 9780:             }
 9781:         } else {
 9782:             $resulttext = '<span class="LC_error">'.
 9783:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9784:         }
 9785:     } else {
 9786:         $resulttext = &mt('No changes made to course and community categories');
 9787:     }
 9788:     return $resulttext;
 9789: }
 9790: 
 9791: sub modify_serverstatuses {
 9792:     my ($dom,%domconfig) = @_;
 9793:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 9794:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 9795:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 9796:     }
 9797:     my @pages = &serverstatus_pages();
 9798:     foreach my $type (@pages) {
 9799:         $newserverstatus{$type}{'namedusers'} = '';
 9800:         $newserverstatus{$type}{'machines'} = '';
 9801:         if (defined($env{'form.'.$type.'_namedusers'})) {
 9802:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 9803:             my @okusers;
 9804:             foreach my $user (@users) {
 9805:                 my ($uname,$udom) = split(/:/,$user);
 9806:                 if (($udom =~ /^$match_domain$/) &&   
 9807:                     (&Apache::lonnet::domain($udom)) &&
 9808:                     ($uname =~ /^$match_username$/)) {
 9809:                     if (!grep(/^\Q$user\E/,@okusers)) {
 9810:                         push(@okusers,$user);
 9811:                     }
 9812:                 }
 9813:             }
 9814:             if (@okusers > 0) {
 9815:                  @okusers = sort(@okusers);
 9816:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 9817:             }
 9818:         }
 9819:         if (defined($env{'form.'.$type.'_machines'})) {
 9820:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 9821:             my @okmachines;
 9822:             foreach my $ip (@machines) {
 9823:                 my @parts = split(/\./,$ip);
 9824:                 next if (@parts < 4);
 9825:                 my $badip = 0;
 9826:                 for (my $i=0; $i<4; $i++) {
 9827:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 9828:                         $badip = 1;
 9829:                         last;
 9830:                     }
 9831:                 }
 9832:                 if (!$badip) {
 9833:                     push(@okmachines,$ip);     
 9834:                 }
 9835:             }
 9836:             @okmachines = sort(@okmachines);
 9837:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 9838:         }
 9839:     }
 9840:     my %serverstatushash =  (
 9841:                                 serverstatuses => \%newserverstatus,
 9842:                             );
 9843:     foreach my $type (@pages) {
 9844:         foreach my $setting ('namedusers','machines') {
 9845:             my (@current,@new);
 9846:             if (ref($currserverstatus{$type}) eq 'HASH') {
 9847:                 if ($currserverstatus{$type}{$setting} ne '') { 
 9848:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 9849:                 }
 9850:             }
 9851:             if ($newserverstatus{$type}{$setting} ne '') {
 9852:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 9853:             }
 9854:             if (@current > 0) {
 9855:                 if (@new > 0) {
 9856:                     foreach my $item (@current) {
 9857:                         if (!grep(/^\Q$item\E$/,@new)) {
 9858:                             $changes{$type}{$setting} = 1;
 9859:                             last;
 9860:                         }
 9861:                     }
 9862:                     foreach my $item (@new) {
 9863:                         if (!grep(/^\Q$item\E$/,@current)) {
 9864:                             $changes{$type}{$setting} = 1;
 9865:                             last;
 9866:                         }
 9867:                     }
 9868:                 } else {
 9869:                     $changes{$type}{$setting} = 1;
 9870:                 }
 9871:             } elsif (@new > 0) {
 9872:                 $changes{$type}{$setting} = 1;
 9873:             }
 9874:         }
 9875:     }
 9876:     if (keys(%changes) > 0) {
 9877:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9878:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9879:                                                  \%serverstatushash,$dom);
 9880:         if ($putresult eq 'ok') {
 9881:             $resulttext .= &mt('Changes made:').'<ul>';
 9882:             foreach my $type (@pages) {
 9883:                 if (ref($changes{$type}) eq 'HASH') {
 9884:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 9885:                     if ($changes{$type}{'namedusers'}) {
 9886:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 9887:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 9888:                         } else {
 9889:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 9890:                         }
 9891:                     }
 9892:                     if ($changes{$type}{'machines'}) {
 9893:                         if ($newserverstatus{$type}{'machines'} eq '') {
 9894:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 9895:                         } else {
 9896:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 9897:                         }
 9898: 
 9899:                     }
 9900:                     $resulttext .= '</ul></li>';
 9901:                 }
 9902:             }
 9903:             $resulttext .= '</ul>';
 9904:         } else {
 9905:             $resulttext = '<span class="LC_error">'.
 9906:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 9907: 
 9908:         }
 9909:     } else {
 9910:         $resulttext = &mt('No changes made to access to server status pages');
 9911:     }
 9912:     return $resulttext;
 9913: }
 9914: 
 9915: sub modify_helpsettings {
 9916:     my ($r,$dom,$confname,%domconfig) = @_;
 9917:     my ($resulttext,$errors,%changes,%helphash);
 9918:     my %defaultchecked = ('submitbugs' => 'on');
 9919:     my @offon = ('off','on');
 9920:     my @toggles = ('submitbugs');
 9921:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 9922:         foreach my $item (@toggles) {
 9923:             if ($defaultchecked{$item} eq 'on') { 
 9924:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9925:                     if ($env{'form.'.$item} eq '0') {
 9926:                         $changes{$item} = 1;
 9927:                     }
 9928:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9929:                     $changes{$item} = 1;
 9930:                 }
 9931:             } elsif ($defaultchecked{$item} eq 'off') {
 9932:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9933:                     if ($env{'form.'.$item} eq '1') {
 9934:                         $changes{$item} = 1;
 9935:                     }
 9936:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9937:                     $changes{$item} = 1;
 9938:                 }
 9939:             }
 9940:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 9941:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 9942:             }
 9943:         }
 9944:     }
 9945:     my $putresult;
 9946:     if (keys(%changes) > 0) {
 9947:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 9948:         if ($putresult eq 'ok') {
 9949:             $resulttext = &mt('Changes made:').'<ul>';
 9950:             foreach my $item (sort(keys(%changes))) {
 9951:                 if ($item eq 'submitbugs') {
 9952:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 9953:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 9954:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 9955:                 }
 9956:             }
 9957:             $resulttext .= '</ul>';
 9958:         } else {
 9959:             $resulttext = &mt('No changes made to help settings');
 9960:             $errors .= '<li><span class="LC_error">'.
 9961:                        &mt('An error occurred storing the settings: [_1]',
 9962:                            $putresult).'</span></li>';
 9963:         }
 9964:     }
 9965:     if ($errors) {
 9966:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9967:                        $errors.'</ul>';
 9968:     }
 9969:     return $resulttext;
 9970: }
 9971: 
 9972: sub modify_coursedefaults {
 9973:     my ($dom,$lastactref,%domconfig) = @_;
 9974:     my ($resulttext,$errors,%changes,%defaultshash);
 9975:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 9976:     my @toggles = ('canuse_pdfforms');
 9977:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 9978:                    'uploadquota_community','uploadquota_textbook');
 9979:     my @types = ('official','unofficial','community','textbook');
 9980:     my %staticdefaults = (
 9981:                            anonsurvey_threshold => 10,
 9982:                            uploadquota          => 500,
 9983:                          );
 9984: 
 9985:     $defaultshash{'coursedefaults'} = {};
 9986: 
 9987:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 9988:         if ($domconfig{'coursedefaults'} eq '') {
 9989:             $domconfig{'coursedefaults'} = {};
 9990:         }
 9991:     }
 9992: 
 9993:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 9994:         foreach my $item (@toggles) {
 9995:             if ($defaultchecked{$item} eq 'on') {
 9996:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9997:                     ($env{'form.'.$item} eq '0')) {
 9998:                     $changes{$item} = 1;
 9999:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10000:                     $changes{$item} = 1;
10001:                 }
10002:             } elsif ($defaultchecked{$item} eq 'off') {
10003:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
10004:                     ($env{'form.'.$item} eq '1')) {
10005:                     $changes{$item} = 1;
10006:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10007:                     $changes{$item} = 1;
10008:                 }
10009:             }
10010:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
10011:         }
10012:         foreach my $item (@numbers) {
10013:             my ($currdef,$newdef);
10014:             $newdef = $env{'form.'.$item};
10015:             if ($item eq 'anonsurvey_threshold') {
10016:                 $currdef = $domconfig{'coursedefaults'}{$item};
10017:                 $newdef =~ s/\D//g;
10018:                 if ($newdef eq '' || $newdef < 1) {
10019:                     $newdef = 1;
10020:                 }
10021:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
10022:             } else {
10023:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
10024:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10025:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
10026:                 }
10027:                 $newdef =~ s/[^\w.\-]//g;
10028:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
10029:             }
10030:             if ($currdef ne $newdef) {
10031:                 my $staticdef;
10032:                 if ($item eq 'anonsurvey_threshold') {
10033:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
10034:                         $changes{$item} = 1;
10035:                     }
10036:                 } else {
10037:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
10038:                         $changes{'uploadquota'} = 1;
10039:                     }
10040:                 }
10041:             }
10042:         }
10043: 
10044:         my $officialcreds = $env{'form.official_credits'};
10045:         $officialcreds =~ s/[^\d.]+//g;
10046:         my $unofficialcreds = $env{'form.unofficial_credits'};
10047:         $unofficialcreds =~ s/[^\d.]+//g;
10048:         my $textbookcreds = $env{'form.textbook_credits'};
10049:         $textbookcreds =~ s/[^\d.]+//g;
10050:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
10051:                 ($env{'form.coursecredits'} eq '1')) {
10052:                 $changes{'coursecredits'} = 1;
10053:         } else {
10054:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
10055:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
10056:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
10057:                 $changes{'coursecredits'} = 1;
10058:             }
10059:         }
10060:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
10061:             official   => $officialcreds,
10062:             unofficial => $unofficialcreds,
10063:             textbook   => $textbookcreds,
10064:         }
10065:     }
10066:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10067:                                              $dom);
10068:     if ($putresult eq 'ok') {
10069:         if (keys(%changes) > 0) {
10070:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10071:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || 
10072:                 ($changes{'uploadquota'})) { 
10073:                 if ($changes{'canuse_pdfforms'}) {
10074:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
10075:                 }
10076:                 if ($changes{'coursecredits'}) {
10077:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10078:                         $domdefaults{'officialcredits'} =
10079:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
10080:                         $domdefaults{'unofficialcredits'} =
10081:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
10082:                         $domdefaults{'textbookcredits'} =
10083:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
10084:                     }
10085:                 }
10086:                 if ($changes{'uploadquota'}) {
10087:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10088:                         foreach my $type (@types) {
10089:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
10090:                         }
10091:                     }
10092:                 }
10093:                 my $cachetime = 24*60*60;
10094:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10095:                 if (ref($lastactref) eq 'HASH') {
10096:                     $lastactref->{'domdefaults'} = 1;
10097:                 }
10098:             }
10099:             $resulttext = &mt('Changes made:').'<ul>';
10100:             foreach my $item (sort(keys(%changes))) {
10101:                 if ($item eq 'canuse_pdfforms') {
10102:                     if ($env{'form.'.$item} eq '1') {
10103:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
10104:                     } else {
10105:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
10106:                     }
10107:                 } elsif ($item eq 'anonsurvey_threshold') {
10108:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
10109:                 } elsif ($item eq 'uploadquota') {
10110:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10111:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
10112:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
10113:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
10114:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
10115: 
10116:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
10117:                                        '</ul>'.
10118:                                        '</li>';
10119:                     } else {
10120:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
10121:                     }
10122:                 } elsif ($item eq 'coursecredits') {
10123:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10124:                         if (($domdefaults{'officialcredits'} eq '') &&
10125:                             ($domdefaults{'unofficialcredits'} eq '') &&
10126:                             ($domdefaults{'textbookcredits'} eq '')) {
10127:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10128:                         } else {
10129:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
10130:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
10131:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
10132:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
10133:                                            '</ul>'.
10134:                                            '</li>';
10135:                         }
10136:                     } else {
10137:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10138:                     }
10139:                 }
10140:             }
10141:             $resulttext .= '</ul>';
10142:         } else {
10143:             $resulttext = &mt('No changes made to course defaults');
10144:         }
10145:     } else {
10146:         $resulttext = '<span class="LC_error">'.
10147:             &mt('An error occurred: [_1]',$putresult).'</span>';
10148:     }
10149:     return $resulttext;
10150: }
10151: 
10152: sub modify_selfenrollment {
10153:     my ($dom,$lastactref,%domconfig) = @_;
10154:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
10155:     my @types = ('official','unofficial','community','textbook');
10156:     my %titles = &tool_titles();
10157:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
10158:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
10159:     $ordered{'default'} = ['types','registered','approval','limit'];
10160: 
10161:     my (%roles,%shown,%toplevel);
10162:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
10163: 
10164:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
10165:         if ($domconfig{'selfenrollment'} eq '') {
10166:             $domconfig{'selfenrollment'} = {};
10167:         }
10168:     }
10169:     %toplevel = (
10170:                   admin      => 'Configuration Rights',
10171:                   default    => 'Default settings',
10172:                   validation => 'Validation of self-enrollment requests',
10173:                 );
10174:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
10175: 
10176:     if (ref($ordered{'admin'}) eq 'ARRAY') {
10177:         foreach my $item (@{$ordered{'admin'}}) {
10178:             foreach my $type (@types) {
10179:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
10180:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
10181:                 } else {
10182:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
10183:                 }
10184:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
10185:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
10186:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
10187:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
10188:                             push(@{$changes{'admin'}{$type}},$item);
10189:                         }
10190:                     } else {
10191:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
10192:                             push(@{$changes{'admin'}{$type}},$item);
10193:                         }
10194:                     }
10195:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
10196:                     push(@{$changes{'admin'}{$type}},$item);
10197:                 }
10198:             }
10199:         }
10200:     }
10201: 
10202:     foreach my $item (@{$ordered{'default'}}) {
10203:         foreach my $type (@types) {
10204:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
10205:             if ($item eq 'types') {
10206:                 unless (($value eq 'all') || ($value eq 'dom')) {
10207:                     $value = '';
10208:                 }
10209:             } elsif ($item eq 'registered') {
10210:                 unless ($value eq '1') {
10211:                     $value = 0;
10212:                 }
10213:             } elsif ($item eq 'approval') {
10214:                 unless ($value =~ /^[012]$/) {
10215:                     $value = 0;
10216:                 }
10217:             } else {
10218:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10219:                     $value = 'none';
10220:                 }
10221:             }
10222:             $selfenrollhash{'default'}{$type}{$item} = $value;
10223:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
10224:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10225:                     if ($selfenrollhash{'default'}{$type}{$item} ne
10226:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
10227:                          push(@{$changes{'default'}{$type}},$item);
10228:                     }
10229:                 } else {
10230:                     push(@{$changes{'default'}{$type}},$item);
10231:                 }
10232:             } else {
10233:                 push(@{$changes{'default'}{$type}},$item);
10234:             }
10235:             if ($item eq 'limit') {
10236:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10237:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
10238:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
10239:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
10240:                     }
10241:                 } else {
10242:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
10243:                 }
10244:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10245:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
10246:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
10247:                          push(@{$changes{'default'}{$type}},'cap');
10248:                     }
10249:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
10250:                     push(@{$changes{'default'}{$type}},'cap');
10251:                 }
10252:             }
10253:         }
10254:     }
10255: 
10256:     foreach my $item (@{$itemsref}) {
10257:         if ($item eq 'fields') {
10258:             my @changed;
10259:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
10260:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
10261:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
10262:             }
10263:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10264:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
10265:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
10266:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
10267:                 } else {
10268:                     @changed = @{$selfenrollhash{'validation'}{$item}};
10269:                 }
10270:             } else {
10271:                 @changed = @{$selfenrollhash{'validation'}{$item}};
10272:             }
10273:             if (@changed) {
10274:                 if ($selfenrollhash{'validation'}{$item}) { 
10275:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
10276:                 } else {
10277:                     $changes{'validation'}{$item} = &mt('None');
10278:                 }
10279:             }
10280:         } else {
10281:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
10282:             if ($item eq 'markup') {
10283:                if ($env{'form.selfenroll_validation_'.$item}) {
10284:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10285:                }
10286:             }
10287:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10288:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
10289:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
10290:                 }
10291:             }
10292:         }
10293:     }
10294: 
10295:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
10296:                                              $dom);
10297:     if ($putresult eq 'ok') {
10298:         if (keys(%changes) > 0) {
10299:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10300:             $resulttext = &mt('Changes made:').'<ul>';
10301:             foreach my $key ('admin','default','validation') {
10302:                 if (ref($changes{$key}) eq 'HASH') {
10303:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
10304:                     if ($key eq 'validation') {
10305:                         foreach my $item (@{$itemsref}) {
10306:                             if (exists($changes{$key}{$item})) {
10307:                                 if ($item eq 'markup') {
10308:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10309:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
10310:                                 } else {  
10311:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10312:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
10313:                                 }
10314:                             }
10315:                         }
10316:                     } else {
10317:                         foreach my $type (@types) {
10318:                             if ($type eq 'community') {
10319:                                 $roles{'1'} = &mt('Community personnel');
10320:                             } else {
10321:                                 $roles{'1'} = &mt('Course personnel');
10322:                             }
10323:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
10324:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
10325:                                     if ($key eq 'admin') {
10326:                                         my @mgrdc = ();
10327:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10328:                                             foreach my $item (@{$ordered{'admin'}}) {
10329:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
10330:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
10331:                                                         push(@mgrdc,$item);
10332:                                                     }
10333:                                                 }
10334:                                             }
10335:                                             if (@mgrdc) {
10336:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
10337:                                             } else {
10338:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
10339:                                             }
10340:                                         }
10341:                                     } else {
10342:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10343:                                             foreach my $item (@{$ordered{$key}}) {
10344:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10345:                                                     $domdefaults{$type.'selfenroll'.$item} =
10346:                                                         $selfenrollhash{$key}{$type}{$item};
10347:                                                 }
10348:                                             }
10349:                                         }
10350:                                     }
10351:                                 }
10352:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
10353:                                 foreach my $item (@{$ordered{$key}}) {
10354:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10355:                                         $resulttext .= '<li>';
10356:                                         if ($key eq 'admin') {
10357:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
10358:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
10359:                                         } else {
10360:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
10361:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
10362:                                         }
10363:                                         $resulttext .= '</li>';
10364:                                     }
10365:                                 }
10366:                                 $resulttext .= '</ul></li>';
10367:                             }
10368:                         }
10369:                         $resulttext .= '</ul></li>'; 
10370:                     }
10371:                 }
10372:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
10373:                     my $cachetime = 24*60*60;
10374:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10375:                     if (ref($lastactref) eq 'HASH') {
10376:                         $lastactref->{'domdefaults'} = 1;
10377:                     }
10378:                 }
10379:             }
10380:             $resulttext .= '</ul>';
10381:         } else {
10382:             $resulttext = &mt('No changes made to self-enrollment settings');
10383:         }
10384:     } else {
10385:         $resulttext = '<span class="LC_error">'.
10386:             &mt('An error occurred: [_1]',$putresult).'</span>';
10387:     }
10388:     return $resulttext;
10389: }
10390: 
10391: sub modify_usersessions {
10392:     my ($dom,$lastactref,%domconfig) = @_;
10393:     my @hostingtypes = ('version','excludedomain','includedomain');
10394:     my @offloadtypes = ('primary','default');
10395:     my %types = (
10396:                   remote => \@hostingtypes,
10397:                   hosted => \@hostingtypes,
10398:                   spares => \@offloadtypes,
10399:                 );
10400:     my @prefixes = ('remote','hosted','spares');
10401:     my @lcversions = &Apache::lonnet::all_loncaparevs();
10402:     my (%by_ip,%by_location,@intdoms);
10403:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
10404:     my @locations = sort(keys(%by_location));
10405:     my (%defaultshash,%changes);
10406:     foreach my $prefix (@prefixes) {
10407:         $defaultshash{'usersessions'}{$prefix} = {};
10408:     }
10409:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10410:     my $resulttext;
10411:     my %iphost = &Apache::lonnet::get_iphost();
10412:     foreach my $prefix (@prefixes) {
10413:         next if ($prefix eq 'spares');
10414:         foreach my $type (@{$types{$prefix}}) {
10415:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
10416:             if ($type eq 'version') {
10417:                 my $value = $env{'form.'.$prefix.'_'.$type};
10418:                 my $okvalue;
10419:                 if ($value ne '') {
10420:                     if (grep(/^\Q$value\E$/,@lcversions)) {
10421:                         $okvalue = $value;
10422:                     }
10423:                 }
10424:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10425:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10426:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
10427:                             if ($inuse == 0) {
10428:                                 $changes{$prefix}{$type} = 1;
10429:                             } else {
10430:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
10431:                                     $changes{$prefix}{$type} = 1;
10432:                                 }
10433:                                 if ($okvalue ne '') {
10434:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10435:                                 } 
10436:                             }
10437:                         } else {
10438:                             if (($inuse == 1) && ($okvalue ne '')) {
10439:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10440:                                 $changes{$prefix}{$type} = 1;
10441:                             }
10442:                         }
10443:                     } else {
10444:                         if (($inuse == 1) && ($okvalue ne '')) {
10445:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10446:                             $changes{$prefix}{$type} = 1;
10447:                         }
10448:                     }
10449:                 } else {
10450:                     if (($inuse == 1) && ($okvalue ne '')) {
10451:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10452:                         $changes{$prefix}{$type} = 1;
10453:                     }
10454:                 }
10455:             } else {
10456:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
10457:                 my @okvals;
10458:                 foreach my $val (@vals) {
10459:                     if ($val =~ /:/) {
10460:                         my @items = split(/:/,$val);
10461:                         foreach my $item (@items) {
10462:                             if (ref($by_location{$item}) eq 'ARRAY') {
10463:                                 push(@okvals,$item);
10464:                             }
10465:                         }
10466:                     } else {
10467:                         if (ref($by_location{$val}) eq 'ARRAY') {
10468:                             push(@okvals,$val);
10469:                         }
10470:                     }
10471:                 }
10472:                 @okvals = sort(@okvals);
10473:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10474:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10475:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10476:                             if ($inuse == 0) {
10477:                                 $changes{$prefix}{$type} = 1; 
10478:                             } else {
10479:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10480:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
10481:                                 if (@changed > 0) {
10482:                                     $changes{$prefix}{$type} = 1;
10483:                                 }
10484:                             }
10485:                         } else {
10486:                             if ($inuse == 1) {
10487:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10488:                                 $changes{$prefix}{$type} = 1;
10489:                             }
10490:                         } 
10491:                     } else {
10492:                         if ($inuse == 1) {
10493:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10494:                             $changes{$prefix}{$type} = 1;
10495:                         }
10496:                     }
10497:                 } else {
10498:                     if ($inuse == 1) {
10499:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10500:                         $changes{$prefix}{$type} = 1;
10501:                     }
10502:                 }
10503:             }
10504:         }
10505:     }
10506: 
10507:     my @alldoms = &Apache::lonnet::all_domains();
10508:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10509:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
10510:     my $savespares;
10511: 
10512:     foreach my $lonhost (sort(keys(%servers))) {
10513:         my $serverhomeID =
10514:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
10515:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
10516:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
10517:         my %spareschg;
10518:         foreach my $type (@{$types{'spares'}}) {
10519:             my @okspares;
10520:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
10521:             foreach my $server (@checked) {
10522:                 if (&Apache::lonnet::hostname($server) ne '') {
10523:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
10524:                         unless (grep(/^\Q$server\E$/,@okspares)) {
10525:                             push(@okspares,$server);
10526:                         }
10527:                     }
10528:                 }
10529:             }
10530:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
10531:             my $newspare;
10532:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
10533:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
10534:                     $newspare = $new;
10535:                 }
10536:             }
10537:             my @spares;
10538:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
10539:                 @spares = sort(@okspares,$newspare);
10540:             } else {
10541:                 @spares = sort(@okspares);
10542:             }
10543:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
10544:             if (ref($spareid{$lonhost}) eq 'HASH') {
10545:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
10546:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
10547:                     if (@diffs > 0) {
10548:                         $spareschg{$type} = 1;
10549:                     }
10550:                 }
10551:             }
10552:         }
10553:         if (keys(%spareschg) > 0) {
10554:             $changes{'spares'}{$lonhost} = \%spareschg;
10555:         }
10556:     }
10557: 
10558:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
10559:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
10560:             if (ref($changes{'spares'}) eq 'HASH') {
10561:                 if (keys(%{$changes{'spares'}}) > 0) {
10562:                     $savespares = 1;
10563:                 }
10564:             }
10565:         } else {
10566:             $savespares = 1;
10567:         }
10568:     }
10569: 
10570:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
10571:     if ((keys(%changes) > 0) || ($savespares)) {
10572:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10573:                                                  $dom);
10574:         if ($putresult eq 'ok') {
10575:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10576:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
10577:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
10578:                 }
10579:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
10580:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
10581:                 }
10582:             }
10583:             my $cachetime = 24*60*60;
10584:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10585:             if (ref($lastactref) eq 'HASH') {
10586:                 $lastactref->{'domdefaults'} = 1;
10587:             }
10588:             if (keys(%changes) > 0) {
10589:                 my %lt = &usersession_titles();
10590:                 $resulttext = &mt('Changes made:').'<ul>';
10591:                 foreach my $prefix (@prefixes) {
10592:                     if (ref($changes{$prefix}) eq 'HASH') {
10593:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
10594:                         if ($prefix eq 'spares') {
10595:                             if (ref($changes{$prefix}) eq 'HASH') {
10596:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
10597:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
10598:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
10599:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
10600:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
10601:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
10602:                                         foreach my $type (@{$types{$prefix}}) {
10603:                                             if ($changes{$prefix}{$lonhost}{$type}) {
10604:                                                 my $offloadto = &mt('None');
10605:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
10606:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
10607:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
10608:                                                     }
10609:                                                 }
10610:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
10611:                                             }
10612:                                         }
10613:                                     }
10614:                                     $resulttext .= '</li>';
10615:                                 }
10616:                             }
10617:                         } else {
10618:                             foreach my $type (@{$types{$prefix}}) {
10619:                                 if (defined($changes{$prefix}{$type})) {
10620:                                     my $newvalue;
10621:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10622:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
10623:                                             if ($type eq 'version') {
10624:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
10625:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10626:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
10627:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
10628:                                                 }
10629:                                             }
10630:                                         }
10631:                                     }
10632:                                     if ($newvalue eq '') {
10633:                                         if ($type eq 'version') {
10634:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
10635:                                         } else {
10636:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
10637:                                         }
10638:                                     } else {
10639:                                         if ($type eq 'version') {
10640:                                             $newvalue .= ' '.&mt('(or later)'); 
10641:                                         }
10642:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
10643:                                     }
10644:                                 }
10645:                             }
10646:                         }
10647:                         $resulttext .= '</ul>';
10648:                     }
10649:                 }
10650:                 $resulttext .= '</ul>';
10651:             } else {
10652:                 $resulttext = $nochgmsg;
10653:             }
10654:         } else {
10655:             $resulttext = '<span class="LC_error">'.
10656:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10657:         }
10658:     } else {
10659:         $resulttext = $nochgmsg;
10660:     }
10661:     return $resulttext;
10662: }
10663: 
10664: sub modify_loadbalancing {
10665:     my ($dom,%domconfig) = @_;
10666:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
10667:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
10668:     my ($othertitle,$usertypes,$types) =
10669:         &Apache::loncommon::sorted_inst_types($dom);
10670:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10671:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
10672:     my @sparestypes = ('primary','default');
10673:     my %typetitles = &sparestype_titles();
10674:     my $resulttext;
10675:     my (%currbalancer,%currtargets,%currrules,%existing);
10676:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10677:         %existing = %{$domconfig{'loadbalancing'}};
10678:     }
10679:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
10680:                               \%currtargets,\%currrules);
10681:     my ($saveloadbalancing,%defaultshash,%changes);
10682:     my ($alltypes,$othertypes,$titles) =
10683:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
10684:     my %ruletitles = &offloadtype_text();
10685:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
10686:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
10687:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
10688:         if ($balancer eq '') {
10689:             next;
10690:         }
10691:         if (!exists($servers{$balancer})) {
10692:             if (exists($currbalancer{$balancer})) {
10693:                 push(@{$changes{'delete'}},$balancer);
10694:             }
10695:             next;
10696:         }
10697:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
10698:             push(@{$changes{'delete'}},$balancer);
10699:             next;
10700:         }
10701:         if (!exists($currbalancer{$balancer})) {
10702:             push(@{$changes{'add'}},$balancer);
10703:         }
10704:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
10705:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
10706:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
10707:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10708:             $saveloadbalancing = 1;
10709:         }
10710:         foreach my $sparetype (@sparestypes) {
10711:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
10712:             my @offloadto;
10713:             foreach my $target (@targets) {
10714:                 if (($servers{$target}) && ($target ne $balancer)) {
10715:                     if ($sparetype eq 'default') {
10716:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
10717:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
10718:                         }
10719:                     }
10720:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
10721:                         push(@offloadto,$target);
10722:                     }
10723:                 }
10724:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
10725:             }
10726:         }
10727:         if (ref($currtargets{$balancer}) eq 'HASH') {
10728:             foreach my $sparetype (@sparestypes) {
10729:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
10730:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
10731:                     if (@targetdiffs > 0) {
10732:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10733:                     }
10734:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10735:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10736:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10737:                     }
10738:                 }
10739:             }
10740:         } else {
10741:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
10742:                 foreach my $sparetype (@sparestypes) {
10743:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10744:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10745:                             $changes{'curr'}{$balancer}{'targets'} = 1;
10746:                         }
10747:                     }
10748:                 }
10749:             }
10750:         }
10751:         my $ishomedom;
10752:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
10753:             $ishomedom = 1;
10754:         }
10755:         if (ref($alltypes) eq 'ARRAY') {
10756:             foreach my $type (@{$alltypes}) {
10757:                 my $rule;
10758:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
10759:                          (!$ishomedom)) {
10760:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
10761:                 }
10762:                 if ($rule eq 'specific') {
10763:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
10764:                     if (exists($servers{$specifiedhost})) {
10765:                         $rule = $specifiedhost;
10766:                     }
10767:                 }
10768:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
10769:                 if (ref($currrules{$balancer}) eq 'HASH') {
10770:                     if ($rule ne $currrules{$balancer}{$type}) {
10771:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10772:                     }
10773:                 } elsif ($rule ne '') {
10774:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10775:                 }
10776:             }
10777:         }
10778:     }
10779:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
10780:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
10781:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
10782:             $defaultshash{'loadbalancing'} = {};
10783:         }
10784:         my $putresult = &Apache::lonnet::put_dom('configuration',
10785:                                                  \%defaultshash,$dom);
10786:         if ($putresult eq 'ok') {
10787:             if (keys(%changes) > 0) {
10788:                 my %toupdate;
10789:                 if (ref($changes{'delete'}) eq 'ARRAY') {
10790:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
10791:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
10792:                         $toupdate{$balancer} = 1;
10793:                     }
10794:                 }
10795:                 if (ref($changes{'add'}) eq 'ARRAY') {
10796:                     foreach my $balancer (sort(@{$changes{'add'}})) {
10797:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
10798:                         $toupdate{$balancer} = 1;
10799:                     }
10800:                 }
10801:                 if (ref($changes{'curr'}) eq 'HASH') {
10802:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
10803:                         $toupdate{$balancer} = 1;
10804:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
10805:                             if ($changes{'curr'}{$balancer}{'targets'}) {
10806:                                 my %offloadstr;
10807:                                 foreach my $sparetype (@sparestypes) {
10808:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10809:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10810:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10811:                                         }
10812:                                     }
10813:                                 }
10814:                                 if (keys(%offloadstr) == 0) {
10815:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
10816:                                 } else {
10817:                                     my $showoffload;
10818:                                     foreach my $sparetype (@sparestypes) {
10819:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
10820:                                         if (defined($offloadstr{$sparetype})) {
10821:                                             $showoffload .= $offloadstr{$sparetype};
10822:                                         } else {
10823:                                             $showoffload .= &mt('None');
10824:                                         }
10825:                                         $showoffload .= ('&nbsp;'x3);
10826:                                     }
10827:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
10828:                                 }
10829:                             }
10830:                         }
10831:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
10832:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
10833:                                 foreach my $type (@{$alltypes}) {
10834:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
10835:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
10836:                                         my $balancetext;
10837:                                         if ($rule eq '') {
10838:                                             $balancetext =  $ruletitles{'default'};
10839:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
10840:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
10841:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
10842:                                                 foreach my $sparetype (@sparestypes) {
10843:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10844:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10845:                                                     }
10846:                                                 }
10847:                                                 foreach my $item (@{$alltypes}) {
10848:                                                     next if ($item =~  /^_LC_ipchange/);
10849:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
10850:                                                     if ($hasrule eq 'homeserver') {
10851:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
10852:                                                     } else {
10853:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
10854:                                                             if ($servers{$hasrule}) {
10855:                                                                 $toupdate{$hasrule} = 1;
10856:                                                             }
10857:                                                         }
10858:                                                     }
10859:                                                 }
10860:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
10861:                                                     $balancetext =  $ruletitles{$rule};
10862:                                                 } else {
10863:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
10864:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
10865:                                                     if ($receiver) {
10866:                                                         $toupdate{$receiver};
10867:                                                     }
10868:                                                 }
10869:                                             } else {
10870:                                                 $balancetext =  $ruletitles{$rule};
10871:                                             }
10872:                                         } else {
10873:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
10874:                                         }
10875:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
10876:                                     }
10877:                                 }
10878:                             }
10879:                         }
10880:                         if (keys(%toupdate)) {
10881:                             my %thismachine;
10882:                             my $updatedhere;
10883:                             my $cachetime = 60*60*24;
10884:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
10885:                             foreach my $lonhost (keys(%toupdate)) {
10886:                                 if ($thismachine{$lonhost}) {
10887:                                     unless ($updatedhere) {
10888:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
10889:                                                                       $defaultshash{'loadbalancing'},
10890:                                                                       $cachetime);
10891:                                         $updatedhere = 1;
10892:                                     }
10893:                                 } else {
10894:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10895:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
10896:                                 }
10897:                             }
10898:                         }
10899:                     }
10900:                 }
10901:                 if ($resulttext ne '') {
10902:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
10903:                 } else {
10904:                     $resulttext = $nochgmsg;
10905:                 }
10906:             } else {
10907:                 $resulttext = $nochgmsg;
10908:             }
10909:         } else {
10910:             $resulttext = '<span class="LC_error">'.
10911:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10912:         }
10913:     } else {
10914:         $resulttext = $nochgmsg;
10915:     }
10916:     return $resulttext;
10917: }
10918: 
10919: sub recurse_check {
10920:     my ($chkcats,$categories,$depth,$name) = @_;
10921:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
10922:         my $chg = 0;
10923:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
10924:             my $category = $chkcats->[$depth]{$name}[$j];
10925:             my $item;
10926:             if ($category eq '') {
10927:                 $chg ++;
10928:             } else {
10929:                 my $deeper = $depth + 1;
10930:                 $item = &escape($category).':'.&escape($name).':'.$depth;
10931:                 if ($chg) {
10932:                     $categories->{$item} -= $chg;
10933:                 }
10934:                 &recurse_check($chkcats,$categories,$deeper,$category);
10935:                 $deeper --;
10936:             }
10937:         }
10938:     }
10939:     return;
10940: }
10941: 
10942: sub recurse_cat_deletes {
10943:     my ($item,$coursecategories,$deletions) = @_;
10944:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10945:     my $subdepth = $depth + 1;
10946:     if (ref($coursecategories) eq 'HASH') {
10947:         foreach my $subitem (keys(%{$coursecategories})) {
10948:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
10949:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
10950:                 delete($coursecategories->{$subitem});
10951:                 $deletions->{$subitem} = 1;
10952:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
10953:             }
10954:         }
10955:     }
10956:     return;
10957: }
10958: 
10959: sub get_active_dcs {
10960:     my ($dom) = @_;
10961:     my $now = time;
10962:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
10963:     my %domcoords;
10964:     my $numdcs = 0;
10965:     foreach my $server (keys(%dompersonnel)) {
10966:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
10967:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
10968:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
10969:         }
10970:     }
10971:     return %domcoords;
10972: }
10973: 
10974: sub active_dc_picker {
10975:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
10976:     my %domcoords = &get_active_dcs($dom);
10977:     my @domcoord = keys(%domcoords);
10978:     if (keys(%currhash)) {
10979:         foreach my $dc (keys(%currhash)) {
10980:             unless (exists($domcoords{$dc})) {
10981:                 push(@domcoord,$dc);
10982:             }
10983:         }
10984:     }
10985:     @domcoord = sort(@domcoord);
10986:     my $numdcs = scalar(@domcoord);
10987:     my $rows = 0;
10988:     my $table;
10989:     if ($numdcs > 1) {
10990:         $table = '<table>';
10991:         for (my $i=0; $i<@domcoord; $i++) {
10992:             my $rem = $i%($numinrow);
10993:             if ($rem == 0) {
10994:                 if ($i > 0) {
10995:                     $table .= '</tr>';
10996:                 }
10997:                 $table .= '<tr>';
10998:                 $rows ++;
10999:             }
11000:             my $check = '';
11001:             if ($inputtype eq 'radio') {
11002:                 if (keys(%currhash) == 0) {
11003:                     if (!$i) {
11004:                         $check = ' checked="checked"';
11005:                     }
11006:                 } elsif (exists($currhash{$domcoord[$i]})) {
11007:                     $check = ' checked="checked"';
11008:                 }
11009:             } else {
11010:                 if (exists($currhash{$domcoord[$i]})) {
11011:                     $check = ' checked="checked"';
11012:                 }
11013:             }
11014:             if ($i == @domcoord - 1) {
11015:                 my $colsleft = $numinrow - $rem;
11016:                 if ($colsleft > 1) {
11017:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
11018:                 } else {
11019:                     $table .= '<td class="LC_left_item">';
11020:                 }
11021:             } else {
11022:                 $table .= '<td class="LC_left_item">';
11023:             }
11024:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
11025:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
11026:             $table .= '<span class="LC_nobreak"><label>'.
11027:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
11028:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
11029:             if ($user ne $dcname.':'.$dcdom) {
11030:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11031:             }
11032:             $table .= '</label></span></td>';
11033:         }
11034:         $table .= '</tr></table>';
11035:     } elsif ($numdcs == 1) {
11036:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
11037:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
11038:         if ($inputtype eq 'radio') {
11039:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
11040:             if ($user ne $dcname.':'.$dcdom) {
11041:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11042:             }
11043:         } else {
11044:             my $check;
11045:             if (exists($currhash{$domcoord[0]})) {
11046:                 $check = ' checked="checked"';
11047:             }
11048:             $table = '<span class="LC_nobreak"><label>'.
11049:                      '<input type="checkbox" name="'.$name.'" '.
11050:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
11051:             if ($user ne $dcname.':'.$dcdom) {
11052:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11053:             }
11054:             $table .= '</label></span>';
11055:             $rows ++;
11056:         }
11057:     }
11058:     return ($numdcs,$table,$rows);
11059: }
11060: 
11061: sub usersession_titles {
11062:     return &Apache::lonlocal::texthash(
11063:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
11064:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
11065:                spares => 'Servers offloaded to, when busy',
11066:                version => 'LON-CAPA version requirement',
11067:                excludedomain => 'Allow all, but exclude specific domains',
11068:                includedomain => 'Deny all, but include specific domains',
11069:                primary => 'Primary (checked first)',
11070:                default => 'Default',
11071:            );
11072: }
11073: 
11074: sub id_for_thisdom {
11075:     my (%servers) = @_;
11076:     my %altids;
11077:     foreach my $server (keys(%servers)) {
11078:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
11079:         if ($serverhome ne $server) {
11080:             $altids{$serverhome} = $server;
11081:         }
11082:     }
11083:     return %altids;
11084: }
11085: 
11086: sub count_servers {
11087:     my ($currbalancer,%servers) = @_;
11088:     my (@spares,$numspares);
11089:     foreach my $lonhost (sort(keys(%servers))) {
11090:         next if ($currbalancer eq $lonhost);
11091:         push(@spares,$lonhost);
11092:     }
11093:     if ($currbalancer) {
11094:         $numspares = scalar(@spares);
11095:     } else {
11096:         $numspares = scalar(@spares) - 1;
11097:     }
11098:     return ($numspares,@spares);
11099: }
11100: 
11101: sub lonbalance_targets_js {
11102:     my ($dom,$types,$servers,$settings) = @_;
11103:     my $select = &mt('Select');
11104:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
11105:     if (ref($servers) eq 'HASH') {
11106:         $alltargets = join("','",sort(keys(%{$servers})));
11107:         my @homedoms;
11108:         foreach my $server (sort(keys(%{$servers}))) {
11109:             if (&Apache::lonnet::host_domain($server) eq $dom) {
11110:                 push(@homedoms,'1');
11111:             } else {
11112:                 push(@homedoms,'0');
11113:             }
11114:         }
11115:         $allishome = join("','",@homedoms);
11116:     }
11117:     if (ref($types) eq 'ARRAY') {
11118:         if (@{$types} > 0) {
11119:             @alltypes = @{$types};
11120:         }
11121:     }
11122:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
11123:     $allinsttypes = join("','",@alltypes);
11124:     my (%currbalancer,%currtargets,%currrules,%existing);
11125:     if (ref($settings) eq 'HASH') {
11126:         %existing = %{$settings};
11127:     }
11128:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
11129:                               \%currtargets,\%currrules);
11130:     my $balancers = join("','",sort(keys(%currbalancer)));
11131:     return <<"END";
11132: 
11133: <script type="text/javascript">
11134: // <![CDATA[
11135: 
11136: currBalancers = new Array('$balancers');
11137: 
11138: function toggleTargets(balnum) {
11139:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11140:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
11141:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
11142:     var prevbalancer = prevhostitem.value;
11143:     var baltotal = document.getElementById('loadbalancing_total').value;
11144:     prevhostitem.value = balancer;
11145:     if (prevbalancer != '') {
11146:         var prevIdx = currBalancers.indexOf(prevbalancer);
11147:         if (prevIdx != -1) {
11148:             currBalancers.splice(prevIdx,1);
11149:         }
11150:     }
11151:     if (balancer == '') {
11152:         hideSpares(balnum);
11153:     } else {
11154:         var currIdx = currBalancers.indexOf(balancer);
11155:         if (currIdx == -1) {
11156:             currBalancers.push(balancer);
11157:         }
11158:         var homedoms = new Array('$allishome');
11159:         var ishomedom = homedoms[lonhostitem.selectedIndex];
11160:         showSpares(balancer,ishomedom,balnum);
11161:     }
11162:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
11163:     return;
11164: }
11165: 
11166: function showSpares(balancer,ishomedom,balnum) {
11167:     var alltargets = new Array('$alltargets');
11168:     var insttypes = new Array('$allinsttypes');
11169:     var offloadtypes = new Array('primary','default');
11170: 
11171:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
11172:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
11173:  
11174:     for (var i=0; i<offloadtypes.length; i++) {
11175:         var count = 0;
11176:         for (var j=0; j<alltargets.length; j++) {
11177:             if (alltargets[j] != balancer) {
11178:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
11179:                 item.value = alltargets[j];
11180:                 item.style.textAlign='left';
11181:                 item.style.textFace='normal';
11182:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
11183:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
11184:                     item.disabled = '';
11185:                 } else {
11186:                     item.disabled = 'disabled';
11187:                     item.checked = false;
11188:                 }
11189:                 count ++;
11190:             }
11191:         }
11192:     }
11193:     for (var k=0; k<insttypes.length; k++) {
11194:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
11195:             if (ishomedom == 1) {
11196:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11197:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11198:             } else {
11199:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11200:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11201:             }
11202:         } else {
11203:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11204:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11205:         }
11206:         if ((insttypes[k] != '_LC_external') && 
11207:             ((insttypes[k] != '_LC_internetdom') ||
11208:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
11209:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
11210:             item.options.length = 0;
11211:             item.options[0] = new Option("","",true,true);
11212:             var idx = 0;
11213:             for (var m=0; m<alltargets.length; m++) {
11214:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
11215:                     idx ++;
11216:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11217:                 }
11218:             }
11219:         }
11220:     }
11221:     return;
11222: }
11223: 
11224: function hideSpares(balnum) {
11225:     var alltargets = new Array('$alltargets');
11226:     var insttypes = new Array('$allinsttypes');
11227:     var offloadtypes = new Array('primary','default');
11228: 
11229:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
11230:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
11231: 
11232:     var total = alltargets.length - 1;
11233:     for (var i=0; i<offloadtypes; i++) {
11234:         for (var j=0; j<total; j++) {
11235:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
11236:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
11237:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
11238:         }
11239:     }
11240:     for (var k=0; k<insttypes.length; k++) {
11241:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11242:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11243:         if (insttypes[k] != '_LC_external') {
11244:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
11245:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
11246:         }
11247:     }
11248:     return;
11249: }
11250: 
11251: function checkOffloads(item,balnum,type) {
11252:     var alltargets = new Array('$alltargets');
11253:     var offloadtypes = new Array('primary','default');
11254:     if (item.checked) {
11255:         var total = alltargets.length - 1;
11256:         var other;
11257:         if (type == offloadtypes[0]) {
11258:             other = offloadtypes[1];
11259:         } else {
11260:             other = offloadtypes[0];
11261:         }
11262:         for (var i=0; i<total; i++) {
11263:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
11264:             if (server == item.value) {
11265:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
11266:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
11267:                 }
11268:             }
11269:         }
11270:     }
11271:     return;
11272: }
11273: 
11274: function singleServerToggle(balnum,type) {
11275:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
11276:     if (offloadtoSelIdx == 0) {
11277:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
11278:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11279: 
11280:     } else {
11281:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
11282:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11283:     }
11284:     return;
11285: }
11286: 
11287: function balanceruleChange(formname,balnum,type) {
11288:     if (type == '_LC_external') {
11289:         return;
11290:     }
11291:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
11292:     for (var i=0; i<typesRules.length; i++) {
11293:         if (formname.elements[typesRules[i]].checked) {
11294:             if (formname.elements[typesRules[i]].value != 'specific') {
11295:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
11296:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11297:             } else {
11298:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11299:             }
11300:         }
11301:     }
11302:     return;
11303: }
11304: 
11305: function balancerDeleteChange(balnum) {
11306:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11307:     var baltotal = document.getElementById('loadbalancing_total').value;
11308:     var addtarget;
11309:     var removetarget;
11310:     var action = 'delete';
11311:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
11312:         var lonhost = hostitem.value;
11313:         var currIdx = currBalancers.indexOf(lonhost);
11314:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
11315:             if (currIdx != -1) {
11316:                 currBalancers.splice(currIdx,1);
11317:             }
11318:             addtarget = lonhost;
11319:         } else {
11320:             if (currIdx == -1) {
11321:                 currBalancers.push(lonhost);
11322:             }
11323:             removetarget = lonhost;
11324:             action = 'undelete';
11325:         }
11326:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
11327:     }
11328:     return;
11329: }
11330: 
11331: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
11332:     if (baltotal > 1) {
11333:         var offloadtypes = new Array('primary','default');
11334:         var alltargets = new Array('$alltargets');
11335:         var insttypes = new Array('$allinsttypes');
11336:         for (var i=0; i<baltotal; i++) {
11337:             if (i != balnum) {
11338:                 for (var j=0; j<offloadtypes.length; j++) {
11339:                     var total = alltargets.length - 1;
11340:                     for (var k=0; k<total; k++) {
11341:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
11342:                         var server = serveritem.value;
11343:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
11344:                             if (server == addtarget) {
11345:                                 serveritem.disabled = '';
11346:                             }
11347:                         }
11348:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11349:                             if (server == removetarget) {
11350:                                 serveritem.disabled = 'disabled';
11351:                                 serveritem.checked = false;
11352:                             }
11353:                         }
11354:                     }
11355:                 }
11356:                 for (var j=0; j<insttypes.length; j++) {
11357:                     if (insttypes[j] != '_LC_external') {
11358:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
11359:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
11360:                             var currSel = singleserver.selectedIndex;
11361:                             var currVal = singleserver.options[currSel].value;
11362:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
11363:                                 var numoptions = singleserver.options.length;
11364:                                 var needsnew = 1;
11365:                                 for (var k=0; k<numoptions; k++) {
11366:                                     if (singleserver.options[k] == addtarget) {
11367:                                         needsnew = 0;
11368:                                         break;
11369:                                     }
11370:                                 }
11371:                                 if (needsnew == 1) {
11372:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
11373:                                 }
11374:                             }
11375:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11376:                                 singleserver.options.length = 0;
11377:                                 if ((currVal) && (currVal != removetarget)) {
11378:                                     singleserver.options[0] = new Option("","",false,false);
11379:                                 } else {
11380:                                     singleserver.options[0] = new Option("","",true,true);
11381:                                 }
11382:                                 var idx = 0;
11383:                                 for (var m=0; m<alltargets.length; m++) {
11384:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
11385:                                         idx ++;
11386:                                         if (currVal == alltargets[m]) {
11387:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
11388:                                         } else {
11389:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11390:                                         }
11391:                                     }
11392:                                 }
11393:                             }
11394:                         }
11395:                     }
11396:                 }
11397:             }
11398:         }
11399:     }
11400:     return;
11401: }
11402: 
11403: // ]]>
11404: </script>
11405: 
11406: END
11407: }
11408: 
11409: sub new_spares_js {
11410:     my @sparestypes = ('primary','default');
11411:     my $types = join("','",@sparestypes);
11412:     my $select = &mt('Select');
11413:     return <<"END";
11414: 
11415: <script type="text/javascript">
11416: // <![CDATA[
11417: 
11418: function updateNewSpares(formname,lonhost) {
11419:     var types = new Array('$types');
11420:     var include = new Array();
11421:     var exclude = new Array();
11422:     for (var i=0; i<types.length; i++) {
11423:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
11424:         for (var j=0; j<spareboxes.length; j++) {
11425:             if (formname.elements[spareboxes[j]].checked) {
11426:                 exclude.push(formname.elements[spareboxes[j]].value);
11427:             } else {
11428:                 include.push(formname.elements[spareboxes[j]].value);
11429:             }
11430:         }
11431:     }
11432:     for (var i=0; i<types.length; i++) {
11433:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
11434:         var selIdx = newSpare.selectedIndex;
11435:         var currnew = newSpare.options[selIdx].value;
11436:         var okSpares = new Array();
11437:         for (var j=0; j<newSpare.options.length; j++) {
11438:             var possible = newSpare.options[j].value;
11439:             if (possible != '') {
11440:                 if (exclude.indexOf(possible) == -1) {
11441:                     okSpares.push(possible);
11442:                 } else {
11443:                     if (currnew == possible) {
11444:                         selIdx = 0;
11445:                     }
11446:                 }
11447:             }
11448:         }
11449:         for (var k=0; k<include.length; k++) {
11450:             if (okSpares.indexOf(include[k]) == -1) {
11451:                 okSpares.push(include[k]);
11452:             }
11453:         }
11454:         okSpares.sort();
11455:         newSpare.options.length = 0;
11456:         if (selIdx == 0) {
11457:             newSpare.options[0] = new Option("$select","",true,true);
11458:         } else {
11459:             newSpare.options[0] = new Option("$select","",false,false);
11460:         }
11461:         for (var m=0; m<okSpares.length; m++) {
11462:             var idx = m+1;
11463:             var selThis = 0;
11464:             if (selIdx != 0) {
11465:                 if (okSpares[m] == currnew) {
11466:                     selThis = 1;
11467:                 }
11468:             }
11469:             if (selThis == 1) {
11470:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
11471:             } else {
11472:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
11473:             }
11474:         }
11475:     }
11476:     return;
11477: }
11478: 
11479: function checkNewSpares(lonhost,type) {
11480:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
11481:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
11482:     if (chosen != '') { 
11483:         var othertype;
11484:         var othernewSpare;
11485:         if (type == 'primary') {
11486:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
11487:         }
11488:         if (type == 'default') {
11489:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
11490:         }
11491:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
11492:             othernewSpare.selectedIndex = 0;
11493:         }
11494:     }
11495:     return;
11496: }
11497: 
11498: // ]]>
11499: </script>
11500: 
11501: END
11502: 
11503: }
11504: 
11505: sub common_domprefs_js {
11506:     return <<"END";
11507: 
11508: <script type="text/javascript">
11509: // <![CDATA[
11510: 
11511: function getIndicesByName(formname,item) {
11512:     var group = new Array();
11513:     for (var i=0;i<formname.elements.length;i++) {
11514:         if (formname.elements[i].name == item) {
11515:             group.push(formname.elements[i].id);
11516:         }
11517:     }
11518:     return group;
11519: }
11520: 
11521: // ]]>
11522: </script>
11523: 
11524: END
11525: 
11526: }
11527: 
11528: sub recaptcha_js {
11529:     my %lt = &captcha_phrases();
11530:     return <<"END";
11531: 
11532: <script type="text/javascript">
11533: // <![CDATA[
11534: 
11535: function updateCaptcha(caller,context) {
11536:     var privitem;
11537:     var pubitem;
11538:     var privtext;
11539:     var pubtext;
11540:     if (document.getElementById(context+'_recaptchapub')) {
11541:         pubitem = document.getElementById(context+'_recaptchapub');
11542:     } else {
11543:         return;
11544:     }
11545:     if (document.getElementById(context+'_recaptchapriv')) {
11546:         privitem = document.getElementById(context+'_recaptchapriv');
11547:     } else {
11548:         return;
11549:     }
11550:     if (document.getElementById(context+'_recaptchapubtxt')) {
11551:         pubtext = document.getElementById(context+'_recaptchapubtxt');
11552:     } else {
11553:         return;
11554:     }
11555:     if (document.getElementById(context+'_recaptchaprivtxt')) {
11556:         privtext = document.getElementById(context+'_recaptchaprivtxt');
11557:     } else {
11558:         return;
11559:     }
11560:     if (caller.checked) {
11561:         if (caller.value == 'recaptcha') {
11562:             pubitem.type = 'text';
11563:             privitem.type = 'text';
11564:             pubitem.size = '40';
11565:             privitem.size = '40';
11566:             pubtext.innerHTML = "$lt{'pub'}";
11567:             privtext.innerHTML = "$lt{'priv'}";
11568:         } else {
11569:             pubitem.type = 'hidden';
11570:             privitem.type = 'hidden';
11571:             pubtext.innerHTML = '';
11572:             privtext.innerHTML = '';
11573:         }
11574:     }
11575:     return;
11576: }
11577: 
11578: // ]]>
11579: </script>
11580: 
11581: END
11582: 
11583: }
11584: 
11585: sub toggle_display_js {
11586:     return <<"END";
11587: 
11588: <script type="text/javascript">
11589: // <![CDATA[
11590: 
11591: function toggleDisplay(domForm,caller) {
11592:     if (document.getElementById(caller)) {
11593:         var divitem = document.getElementById(caller);
11594:         var optionsElement = domForm.coursecredits;
11595:         if (caller == 'emailoptions') {
11596:             optionsElement = domForm.cancreate_email; 
11597:         }
11598:         if (optionsElement.length) {
11599:             var currval;
11600:             for (var i=0; i<optionsElement.length; i++) {
11601:                 if (optionsElement[i].checked) {
11602:                    currval = optionsElement[i].value;
11603:                 }
11604:             }
11605:             if (currval == 1) {
11606:                 divitem.style.display = 'block';
11607:             } else {
11608:                 divitem.style.display = 'none';
11609:             }
11610:         }
11611:     }
11612:     return;
11613: }
11614: 
11615: // ]]>
11616: </script>
11617: 
11618: END
11619: 
11620: }
11621: 
11622: sub captcha_phrases {
11623:     return &Apache::lonlocal::texthash (
11624:                  priv => 'Private key',
11625:                  pub  => 'Public key',
11626:                  original  => 'original (CAPTCHA)',
11627:                  recaptcha => 'successor (ReCAPTCHA)',
11628:                  notused   => 'unused',
11629:     );
11630: }
11631: 
11632: sub devalidate_remote_domconfs {
11633:     my ($dom,$cachekeys) = @_;
11634:     return unless (ref($cachekeys) eq 'HASH');
11635:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11636:     my %thismachine;
11637:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
11638:     my @posscached = ('domainconfig','domdefaults');
11639:     if (keys(%servers) > 1) {
11640:         foreach my $server (keys(%servers)) {
11641:             next if ($thismachine{$server});
11642:             my @cached;
11643:             foreach my $name (@posscached) {
11644:                 if ($cachekeys->{$name}) {
11645:                     push(@cached,&escape($name).':'.&escape($dom));
11646:                 }
11647:             }
11648:             if (@cached) {
11649:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
11650:             }
11651:         }
11652:     }
11653:     return;
11654: }
11655: 
11656: 1;

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