File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.253: download - view: text, annotated - select for diffs
Sun Oct 5 13:34:01 2014 UTC (9 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Bug 6675
- Add domain configuration setting on balancer server for non-SSO users
  with IP mismatch during offload for case where balancer server's default
  domain is not domain being configured.
- If loadbalancing configuration change affects behavior of servers
  which host offloaded sessions (e.g., ip change detected in migration)
  expire cached loadbalancing config on load balancer, and servers which
  might be set as "specific" offloaded session recipient.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.253 2014/10/05 13:34:01 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','helpsettings',
  217:                 'coursedefaults','usersessions','loadbalancing',
  218:                 'requestauthor','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','helpsettings',
  224:                        'coursedefaults','selfenrollment','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:         'helpsettings' =>
  413:                  {text   => 'Help page settings',
  414:                   help   => 'Domain_Configuration_Help_Settings',
  415:                   header => [{col1 => 'Help Settings (logged-in users)',
  416:                               col2 => 'Value'}],
  417:                   print  => \&print_helpsettings,
  418:                   modify => \&modify_helpsettings,
  419:                  },
  420:         'coursedefaults' => 
  421:                  {text => 'Course/Community defaults',
  422:                   help => 'Domain_Configuration_Course_Defaults',
  423:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  424:                               col2 => 'Value',},
  425:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  426:                               col2 => 'Value',},],
  427:                   print => \&print_coursedefaults,
  428:                   modify => \&modify_coursedefaults,
  429:                  },
  430:         'selfenrollment' => 
  431:                  {text   => 'Self-enrollment in Course/Community',
  432:                   help   => 'Domain_Configuration_Selfenrollment',
  433:                   header => [{col1 => 'Configuration Rights',
  434:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  435:                              {col1 => 'Defaults',
  436:                               col2 => 'Value'},
  437:                              {col1 => 'Self-enrollment validation (optional)',
  438:                               col2 => 'Value'},],
  439:                   print => \&print_selfenrollment,
  440:                   modify => \&modify_selfenrollment,
  441:                  },
  442:         'privacy' => 
  443:                  {text   => 'User Privacy',
  444:                   help   => 'Domain_Configuration_User_Privacy',
  445:                   header => [{col1 => 'Setting',
  446:                               col2 => 'Value',}],
  447:                   print => \&print_privacy,
  448:                   modify => \&modify_privacy,
  449:                  },
  450:         'usersessions' =>
  451:                  {text  => 'User session hosting/offloading',
  452:                   help  => 'Domain_Configuration_User_Sessions',
  453:                   header => [{col1 => 'Domain server',
  454:                               col2 => 'Servers to offload sessions to when busy'},
  455:                              {col1 => 'Hosting of users from other domains',
  456:                               col2 => 'Rules'},
  457:                              {col1 => "Hosting domain's own users elsewhere",
  458:                               col2 => 'Rules'}],
  459:                   print => \&print_usersessions,
  460:                   modify => \&modify_usersessions,
  461:                  },
  462:          'loadbalancing' =>
  463:                  {text  => 'Dedicated Load Balancer(s)',
  464:                   help  => 'Domain_Configuration_Load_Balancing',
  465:                   header => [{col1 => 'Balancers',
  466:                               col2 => 'Default destinations',
  467:                               col3 => 'User affiliation',
  468:                               col4 => 'Overrides'},
  469:                             ],
  470:                   print => \&print_loadbalancing,
  471:                   modify => \&modify_loadbalancing,
  472:                  },
  473:     );
  474:     if (keys(%servers) > 1) {
  475:         $prefs{'login'}  = { text   => 'Log-in page options',
  476:                              help   => 'Domain_Configuration_Login_Page',
  477:                             header => [{col1 => 'Log-in Service',
  478:                                         col2 => 'Server Setting',},
  479:                                        {col1 => 'Log-in Page Items',
  480:                                         col2 => ''},
  481:                                        {col1 => 'Log-in Help',
  482:                                         col2 => 'Value'}],
  483:                             print => \&print_login,
  484:                             modify => \&modify_login,
  485:                            };
  486:     }
  487: 
  488:     my @roles = ('student','coordinator','author','admin');
  489:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  490:     &Apache::lonhtmlcommon::add_breadcrumb
  491:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  492:       text=>"Settings to display/modify"});
  493:     my $confname = $dom.'-domainconfig';
  494: 
  495:     if ($phase eq 'process') {
  496:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  497:                                                               \%prefs,\%domconfig,$confname,\@roles);
  498:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  499:             $r->rflush();
  500:             &devalidate_remote_domconfs($dom,$result);
  501:         }
  502:     } elsif ($phase eq 'display') {
  503:         my $js = &recaptcha_js().
  504:                  &toggle_display_js();
  505:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  506:             my ($othertitle,$usertypes,$types) =
  507:                 &Apache::loncommon::sorted_inst_types($dom);
  508:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  509:                                           $domconfig{'loadbalancing'}).
  510:                    &new_spares_js().
  511:                    &common_domprefs_js().
  512:                    &Apache::loncommon::javascript_array_indexof();
  513:         }
  514:         if (grep(/^requestcourses$/,@actions)) {
  515:             my $javascript_validations;
  516:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  517:             $js .= <<END;
  518: <script type="text/javascript">
  519: $javascript_validations
  520: </script>
  521: $coursebrowserjs
  522: END
  523:         }
  524:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  525:     } else {
  526: # check if domconfig user exists for the domain.
  527:         my $servadm = $r->dir_config('lonAdmEMail');
  528:         my ($configuserok,$author_ok,$switchserver) =
  529:             &config_check($dom,$confname,$servadm);
  530:         unless ($configuserok eq 'ok') {
  531:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  532:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  533:                           $confname).
  534:                       '<br />'
  535:             );
  536:             if ($switchserver) {
  537:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  538:                           '<br />'.
  539:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  540:                           '<br />'.
  541:                           &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).
  542:                           '<br />'.
  543:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  544:                 );
  545:             } else {
  546:                 $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.').
  547:                           '<br />'.
  548:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  549:                 );
  550:             }
  551:             $r->print(&Apache::loncommon::end_page());
  552:             return OK;
  553:         }
  554:         if (keys(%domconfig) == 0) {
  555:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  556:             my @ids=&Apache::lonnet::current_machine_ids();
  557:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  558:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  559:                 my @loginimages = ('img','logo','domlogo','login');
  560:                 my $custom_img_count = 0;
  561:                 foreach my $img (@loginimages) {
  562:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  563:                         $custom_img_count ++;
  564:                     }
  565:                 }
  566:                 foreach my $role (@roles) {
  567:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  568:                         $custom_img_count ++;
  569:                     }
  570:                 }
  571:                 if ($custom_img_count > 0) {
  572:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  573:                     my $switch_server = &check_switchserver($dom,$confname);
  574:                     $r->print(
  575:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  576:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  577:     &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 />'.
  578:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  579:                     if ($switch_server) {
  580:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  581:                     }
  582:                     $r->print(&Apache::loncommon::end_page());
  583:                     return OK;
  584:                 }
  585:             }
  586:         }
  587:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  588:     }
  589:     return OK;
  590: }
  591: 
  592: sub process_changes {
  593:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  594:     my %domconfig;
  595:     if (ref($values) eq 'HASH') {
  596:         %domconfig = %{$values};
  597:     }
  598:     my $output;
  599:     if ($action eq 'login') {
  600:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  601:     } elsif ($action eq 'rolecolors') {
  602:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  603:                                      $lastactref,%domconfig);
  604:     } elsif ($action eq 'quotas') {
  605:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  606:     } elsif ($action eq 'autoenroll') {
  607:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  608:     } elsif ($action eq 'autoupdate') {
  609:         $output = &modify_autoupdate($dom,%domconfig);
  610:     } elsif ($action eq 'autocreate') {
  611:         $output = &modify_autocreate($dom,%domconfig);
  612:     } elsif ($action eq 'directorysrch') {
  613:         $output = &modify_directorysrch($dom,%domconfig);
  614:     } elsif ($action eq 'usercreation') {
  615:         $output = &modify_usercreation($dom,%domconfig);
  616:     } elsif ($action eq 'selfcreation') {
  617:         $output = &modify_selfcreation($dom,%domconfig);
  618:     } elsif ($action eq 'usermodification') {
  619:         $output = &modify_usermodification($dom,%domconfig);
  620:     } elsif ($action eq 'contacts') {
  621:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  622:     } elsif ($action eq 'defaults') {
  623:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  624:     } elsif ($action eq 'scantron') {
  625:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  626:     } elsif ($action eq 'coursecategories') {
  627:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  628:     } elsif ($action eq 'serverstatuses') {
  629:         $output = &modify_serverstatuses($dom,%domconfig);
  630:     } elsif ($action eq 'requestcourses') {
  631:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  632:     } elsif ($action eq 'requestauthor') {
  633:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  634:     } elsif ($action eq 'helpsettings') {
  635:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  636:     } elsif ($action eq 'coursedefaults') {
  637:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  638:     } elsif ($action eq 'selfenrollment') {
  639:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  640:     } elsif ($action eq 'usersessions') {
  641:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  642:     } elsif ($action eq 'loadbalancing') {
  643:         $output = &modify_loadbalancing($dom,%domconfig);
  644:     }
  645:     return $output;
  646: }
  647: 
  648: sub print_config_box {
  649:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  650:     my $rowtotal = 0;
  651:     my $output;
  652:     if ($action eq 'coursecategories') {
  653:         $output = &coursecategories_javascript($settings);
  654:     } elsif ($action eq 'defaults') {
  655:         $output = &defaults_javascript($settings); 
  656:     }
  657:     $output .=
  658:          '<table class="LC_nested_outer">
  659:           <tr>
  660:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  661:            &mt($item->{text}).'&nbsp;'.
  662:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  663:           '</tr>';
  664:     $rowtotal ++;
  665:     my $numheaders = 1;
  666:     if (ref($item->{'header'}) eq 'ARRAY') {
  667:         $numheaders = scalar(@{$item->{'header'}});
  668:     }
  669:     if ($numheaders > 1) {
  670:         my $colspan = '';
  671:         my $rightcolspan = '';
  672:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  673:             (($action eq 'login') && ($numheaders < 3))) {
  674:             $colspan = ' colspan="2"';
  675:         }
  676:         if ($action eq 'usersessions') {
  677:             $rightcolspan = ' colspan="3"'; 
  678:         }
  679:         $output .= '
  680:           <tr>
  681:            <td>
  682:             <table class="LC_nested">
  683:              <tr class="LC_info_row">
  684:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  685:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  686:              </tr>';
  687:         $rowtotal ++;
  688:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  689:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  690:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
  691:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  692:         } elsif ($action eq 'coursecategories') {
  693:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  694:         } elsif ($action eq 'login') {
  695:             if ($numheaders == 3) {
  696:                 $colspan = ' colspan="2"';
  697:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  698:             } else {
  699:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  700:             }
  701:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  702:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  703:         } elsif ($action eq 'rolecolors') {
  704:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  705:         }
  706:         $output .= '
  707:            </table>
  708:           </td>
  709:          </tr>
  710:          <tr>
  711:            <td>
  712:             <table class="LC_nested">
  713:              <tr class="LC_info_row">
  714:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  715:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  716:              </tr>';
  717:             $rowtotal ++;
  718:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  719:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  720:             ($action eq 'usersessions') || ($action eq 'coursecategories')) {
  721:             if ($action eq 'coursecategories') {
  722:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  723:                 $colspan = ' colspan="2"';
  724:             } else {
  725:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  726:             }
  727:             $output .= '
  728:            </table>
  729:           </td>
  730:          </tr>
  731:          <tr>
  732:            <td>
  733:             <table class="LC_nested">
  734:              <tr class="LC_info_row">
  735:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  736:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  737:              </tr>'."\n";
  738:             if ($action eq 'coursecategories') {
  739:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  740:             } else {
  741:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  742:             }
  743:             $rowtotal ++;
  744:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  745:                   ($action eq 'defaults')) {
  746:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  747:         } elsif ($action eq 'login') {
  748:             if ($numheaders == 3) {
  749:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  750:            </table>
  751:           </td>
  752:          </tr>
  753:          <tr>
  754:            <td>
  755:             <table class="LC_nested">
  756:              <tr class="LC_info_row">
  757:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  758:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  759:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  760:                 $rowtotal ++;
  761:             } else {
  762:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  763:             }
  764:         } elsif ($action eq 'requestcourses') {
  765:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  766:             $rowtotal ++;
  767:             $output .= &print_studentcode($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.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  776:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  777:                        &textbookcourses_javascript($settings).
  778:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  779:             </table>
  780:            </td>
  781:           </tr>
  782:          <tr>
  783:            <td>
  784:             <table class="LC_nested">
  785:              <tr class="LC_info_row">
  786:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  787:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  788:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  789:             </table>
  790:            </td>
  791:           </tr>
  792:           <tr>
  793:            <td>
  794:             <table class="LC_nested">
  795:              <tr class="LC_info_row">
  796:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  797:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  798:              </tr>'.
  799:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  800:         } elsif ($action eq 'requestauthor') {
  801:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  802:             $rowtotal ++;
  803:         } elsif ($action eq 'rolecolors') {
  804:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  805:            </table>
  806:           </td>
  807:          </tr>
  808:          <tr>
  809:            <td>
  810:             <table class="LC_nested">
  811:              <tr class="LC_info_row">
  812:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  813:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  814:               <td class="LC_right_item" valign="top">'.
  815:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  816:              </tr>'.
  817:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  818:            </table>
  819:           </td>
  820:          </tr>
  821:          <tr>
  822:            <td>
  823:             <table class="LC_nested">
  824:              <tr class="LC_info_row">
  825:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  826:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  827:              </tr>'.
  828:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  829:             $rowtotal += 2;
  830:         }
  831:     } else {
  832:         $output .= '
  833:           <tr>
  834:            <td>
  835:             <table class="LC_nested">
  836:              <tr class="LC_info_row">';
  837:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  838:             $output .= '  
  839:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  840:         } elsif ($action eq 'serverstatuses') {
  841:             $output .= '
  842:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  843:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  844: 
  845:         } else {
  846:             $output .= '
  847:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  848:         }
  849:         if (defined($item->{'header'}->[0]->{'col3'})) {
  850:             $output .= '<td class="LC_left_item" valign="top">'.
  851:                        &mt($item->{'header'}->[0]->{'col2'});
  852:             if ($action eq 'serverstatuses') {
  853:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  854:             } 
  855:         } else {
  856:             $output .= '<td class="LC_right_item" valign="top">'.
  857:                        &mt($item->{'header'}->[0]->{'col2'});
  858:         }
  859:         $output .= '</td>';
  860:         if ($item->{'header'}->[0]->{'col3'}) {
  861:             if (defined($item->{'header'}->[0]->{'col4'})) {
  862:                 $output .= '<td class="LC_left_item" valign="top">'.
  863:                             &mt($item->{'header'}->[0]->{'col3'});
  864:             } else {
  865:                 $output .= '<td class="LC_right_item" valign="top">'.
  866:                            &mt($item->{'header'}->[0]->{'col3'});
  867:             }
  868:             if ($action eq 'serverstatuses') {
  869:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  870:             }
  871:             $output .= '</td>';
  872:         }
  873:         if ($item->{'header'}->[0]->{'col4'}) {
  874:             $output .= '<td class="LC_right_item" valign="top">'.
  875:                        &mt($item->{'header'}->[0]->{'col4'});
  876:         }
  877:         $output .= '</tr>';
  878:         $rowtotal ++;
  879:         if ($action eq 'quotas') {
  880:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  881:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  882:                  ($action eq 'contacts') || ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  883:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  884:         } elsif ($action eq 'scantron') {
  885:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  886:         } elsif ($action eq 'helpsettings') {
  887:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  888:         }
  889:     }
  890:     $output .= '
  891:    </table>
  892:   </td>
  893:  </tr>
  894: </table><br />';
  895:     return ($output,$rowtotal);
  896: }
  897: 
  898: sub print_login {
  899:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  900:     my ($css_class,$datatable);
  901:     my %choices = &login_choices();
  902: 
  903:     if ($caller eq 'service') {
  904:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  905:         my $choice = $choices{'disallowlogin'};
  906:         $css_class = ' class="LC_odd_row"';
  907:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  908:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  909:                       '<th>'.$choices{'server'}.'</th>'.
  910:                       '<th>'.$choices{'serverpath'}.'</th>'.
  911:                       '<th>'.$choices{'custompath'}.'</th>'.
  912:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  913:         my %disallowed;
  914:         if (ref($settings) eq 'HASH') {
  915:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  916:                %disallowed = %{$settings->{'loginvia'}};
  917:             }
  918:         }
  919:         foreach my $lonhost (sort(keys(%servers))) {
  920:             my $direct = 'selected="selected"';
  921:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  922:                 if ($disallowed{$lonhost}{'server'} ne '') {
  923:                     $direct = '';
  924:                 }
  925:             }
  926:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  927:                           '<td><select name="'.$lonhost.'_server">'.
  928:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  929:                           '</option>';
  930:             foreach my $hostid (sort(keys(%servers))) {
  931:                 next if ($servers{$hostid} eq $servers{$lonhost});
  932:                 my $selected = '';
  933:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  934:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  935:                         $selected = 'selected="selected"';
  936:                     }
  937:                 }
  938:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  939:                               $servers{$hostid}.'</option>';
  940:             }
  941:             $datatable .= '</select></td>'.
  942:                           '<td><select name="'.$lonhost.'_serverpath">';
  943:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  944:                 my $pathname = $path;
  945:                 if ($path eq 'custom') {
  946:                     $pathname = &mt('Custom Path').' ->';
  947:                 }
  948:                 my $selected = '';
  949:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  950:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  951:                         $selected = 'selected="selected"';
  952:                     }
  953:                 } elsif ($path eq '') {
  954:                     $selected = 'selected="selected"';
  955:                 }
  956:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  957:             }
  958:             $datatable .= '</select></td>';
  959:             my ($custom,$exempt);
  960:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  961:                 $custom = $disallowed{$lonhost}{'custompath'};
  962:                 $exempt = $disallowed{$lonhost}{'exempt'};
  963:             }
  964:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  965:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  966:                           '</tr>';
  967:         }
  968:         $datatable .= '</table></td></tr>';
  969:         return $datatable;
  970:     } elsif ($caller eq 'page') {
  971:         my %defaultchecked = ( 
  972:                                'coursecatalog' => 'on',
  973:                                'helpdesk'      => 'on',
  974:                                'adminmail'     => 'off',
  975:                                'newuser'       => 'off',
  976:                              );
  977:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  978:         my (%checkedon,%checkedoff);
  979:         foreach my $item (@toggles) {
  980:             if ($defaultchecked{$item} eq 'on') { 
  981:                 $checkedon{$item} = ' checked="checked" ';
  982:                 $checkedoff{$item} = ' ';
  983:             } elsif ($defaultchecked{$item} eq 'off') {
  984:                 $checkedoff{$item} = ' checked="checked" ';
  985:                 $checkedon{$item} = ' ';
  986:             }
  987:         }
  988:         my @images = ('img','logo','domlogo','login');
  989:         my @logintext = ('textcol','bgcol');
  990:         my @bgs = ('pgbg','mainbg','sidebg');
  991:         my @links = ('link','alink','vlink');
  992:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  993:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  994:         my (%is_custom,%designs);
  995:         my %defaults = (
  996:                        font => $defaultdesign{'login.font'},
  997:                        );
  998:         foreach my $item (@images) {
  999:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1000:             $defaults{'showlogo'}{$item} = 1;
 1001:         }
 1002:         foreach my $item (@bgs) {
 1003:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1004:         }
 1005:         foreach my $item (@logintext) {
 1006:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1007:         }
 1008:         foreach my $item (@links) {
 1009:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1010:         }
 1011:         if (ref($settings) eq 'HASH') {
 1012:             foreach my $item (@toggles) {
 1013:                 if ($settings->{$item} eq '1') {
 1014:                     $checkedon{$item} =  ' checked="checked" ';
 1015:                     $checkedoff{$item} = ' ';
 1016:                 } elsif ($settings->{$item} eq '0') {
 1017:                     $checkedoff{$item} =  ' checked="checked" ';
 1018:                     $checkedon{$item} = ' ';
 1019:                 }
 1020:             }
 1021:             foreach my $item (@images) {
 1022:                 if (defined($settings->{$item})) {
 1023:                     $designs{$item} = $settings->{$item};
 1024:                     $is_custom{$item} = 1;
 1025:                 }
 1026:                 if (defined($settings->{'showlogo'}{$item})) {
 1027:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1028:                 }
 1029:             }
 1030:             foreach my $item (@logintext) {
 1031:                 if ($settings->{$item} ne '') {
 1032:                     $designs{'logintext'}{$item} = $settings->{$item};
 1033:                     $is_custom{$item} = 1;
 1034:                 }
 1035:             }
 1036:             if ($settings->{'font'} ne '') {
 1037:                 $designs{'font'} = $settings->{'font'};
 1038:                 $is_custom{'font'} = 1;
 1039:             }
 1040:             foreach my $item (@bgs) {
 1041:                 if ($settings->{$item} ne '') {
 1042:                     $designs{'bgs'}{$item} = $settings->{$item};
 1043:                     $is_custom{$item} = 1;
 1044:                 }
 1045:             }
 1046:             foreach my $item (@links) {
 1047:                 if ($settings->{$item} ne '') {
 1048:                     $designs{'links'}{$item} = $settings->{$item};
 1049:                     $is_custom{$item} = 1;
 1050:                 }
 1051:             }
 1052:         } else {
 1053:             if ($designhash{$dom.'.login.font'} ne '') {
 1054:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1055:                 $is_custom{'font'} = 1;
 1056:             }
 1057:             foreach my $item (@images) {
 1058:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1059:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1060:                     $is_custom{$item} = 1;
 1061:                 }
 1062:             }
 1063:             foreach my $item (@bgs) {
 1064:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1065:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1066:                     $is_custom{$item} = 1;
 1067:                 }
 1068:             }
 1069:             foreach my $item (@links) {
 1070:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1071:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1072:                     $is_custom{$item} = 1;
 1073:                 }
 1074:             }
 1075:         }
 1076:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1077:                                                       logo => 'Institution Logo',
 1078:                                                       domlogo => 'Domain Logo',
 1079:                                                       login => 'Login box');
 1080:         my $itemcount = 1;
 1081:         foreach my $item (@toggles) {
 1082:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1083:             $datatable .=  
 1084:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1085:                 '</td><td>'.
 1086:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1087:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1088:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1089:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1090:                 '</tr>';
 1091:             $itemcount ++;
 1092:         }
 1093:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1094:         $datatable .= '</tr></table></td></tr>';
 1095:     } elsif ($caller eq 'help') {
 1096:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1097:         my $switchserver = &check_switchserver($dom,$confname);
 1098:         my $itemcount = 1;
 1099:         $defaulturl = '/adm/loginproblems.html';
 1100:         $defaulttype = 'default';
 1101:         %lt = &Apache::lonlocal::texthash (
 1102:                      del     => 'Delete?',
 1103:                      rep     => 'Replace:',
 1104:                      upl     => 'Upload:',
 1105:                      default => 'Default',
 1106:                      custom  => 'Custom',
 1107:                                              );
 1108:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1109:         my @currlangs;
 1110:         if (ref($settings) eq 'HASH') {
 1111:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1112:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1113:                     next if ($settings->{'helpurl'}{$key} eq '');
 1114:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1115:                     $type{$key} = 'custom';
 1116:                     unless ($key eq 'nolang') {
 1117:                         push(@currlangs,$key);
 1118:                     }
 1119:                 }
 1120:             } elsif ($settings->{'helpurl'} ne '') {
 1121:                 $type{'nolang'} = 'custom';
 1122:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1123:             }
 1124:         }
 1125:         foreach my $lang ('nolang',sort(@currlangs)) {
 1126:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1127:             $datatable .= '<tr'.$css_class.'>';
 1128:             if ($url{$lang} eq '') {
 1129:                 $url{$lang} = $defaulturl;
 1130:             }
 1131:             if ($type{$lang} eq '') {
 1132:                 $type{$lang} = $defaulttype;
 1133:             }
 1134:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1135:             if ($lang eq 'nolang') {
 1136:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1137:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1138:             } else {
 1139:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1140:                                   $langchoices{$lang},
 1141:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1142:             }
 1143:             $datatable .= '</span></td>'."\n".
 1144:                           '<td class="LC_left_item">';
 1145:             if ($type{$lang} eq 'custom') {
 1146:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1147:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1148:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1149:             } else {
 1150:                 $datatable .= $lt{'upl'};
 1151:             }
 1152:             $datatable .='<br />';
 1153:             if ($switchserver) {
 1154:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1155:             } else {
 1156:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1157:             }
 1158:             $datatable .= '</td></tr>';
 1159:             $itemcount ++;
 1160:         }
 1161:         my @addlangs;
 1162:         foreach my $lang (sort(keys(%langchoices))) {
 1163:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1164:             push(@addlangs,$lang);
 1165:         }
 1166:         if (@addlangs > 0) {
 1167:             my %toadd;
 1168:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1169:             $toadd{''} = &mt('Select');
 1170:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1171:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1172:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1173:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1174:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1175:             if ($switchserver) {
 1176:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1177:             } else {
 1178:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1179:             }
 1180:             $datatable .= '</td></tr>';
 1181:             $itemcount ++;
 1182:         }
 1183:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1184:     }
 1185:     return $datatable;
 1186: }
 1187: 
 1188: sub login_choices {
 1189:     my %choices =
 1190:         &Apache::lonlocal::texthash (
 1191:             coursecatalog => 'Display Course/Community Catalog link?',
 1192:             adminmail     => "Display Administrator's E-mail Address?",
 1193:             helpdesk      => 'Display "Contact Helpdesk" link',
 1194:             disallowlogin => "Login page requests redirected",
 1195:             hostid        => "Server",
 1196:             server        => "Redirect to:",
 1197:             serverpath    => "Path",
 1198:             custompath    => "Custom", 
 1199:             exempt        => "Exempt IP(s)",
 1200:             directlogin   => "No redirect",
 1201:             newuser       => "Link to create a user account",
 1202:             img           => "Header",
 1203:             logo          => "Main Logo",
 1204:             domlogo       => "Domain Logo",
 1205:             login         => "Log-in Header", 
 1206:             textcol       => "Text color",
 1207:             bgcol         => "Box color",
 1208:             bgs           => "Background colors",
 1209:             links         => "Link colors",
 1210:             font          => "Font color",
 1211:             pgbg          => "Header",
 1212:             mainbg        => "Page",
 1213:             sidebg        => "Login box",
 1214:             link          => "Link",
 1215:             alink         => "Active link",
 1216:             vlink         => "Visited link",
 1217:         );
 1218:     return %choices;
 1219: }
 1220: 
 1221: sub print_rolecolors {
 1222:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1223:     my %choices = &color_font_choices();
 1224:     my @bgs = ('pgbg','tabbg','sidebg');
 1225:     my @links = ('link','alink','vlink');
 1226:     my @images = ('img');
 1227:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1228:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1229:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1230:     my (%is_custom,%designs);
 1231:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1232:     if (ref($settings) eq 'HASH') {
 1233:         if (ref($settings->{$role}) eq 'HASH') {
 1234:             if ($settings->{$role}->{'img'} ne '') {
 1235:                 $designs{'img'} = $settings->{$role}->{'img'};
 1236:                 $is_custom{'img'} = 1;
 1237:             }
 1238:             if ($settings->{$role}->{'font'} ne '') {
 1239:                 $designs{'font'} = $settings->{$role}->{'font'};
 1240:                 $is_custom{'font'} = 1;
 1241:             }
 1242:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1243:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1244:                 $is_custom{'fontmenu'} = 1;
 1245:             }
 1246:             foreach my $item (@bgs) {
 1247:                 if ($settings->{$role}->{$item} ne '') {
 1248:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1249:                     $is_custom{$item} = 1;
 1250:                 }
 1251:             }
 1252:             foreach my $item (@links) {
 1253:                 if ($settings->{$role}->{$item} ne '') {
 1254:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1255:                     $is_custom{$item} = 1;
 1256:                 }
 1257:             }
 1258:         }
 1259:     } else {
 1260:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1261:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1262:             $is_custom{'img'} = 1;
 1263:         }
 1264:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1265:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1266:             $is_custom{'fontmenu'} = 1; 
 1267:         }
 1268:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1269:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1270:             $is_custom{'font'} = 1;
 1271:         }
 1272:         foreach my $item (@bgs) {
 1273:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1274:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1275:                 $is_custom{$item} = 1;
 1276:             
 1277:             }
 1278:         }
 1279:         foreach my $item (@links) {
 1280:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1281:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1282:                 $is_custom{$item} = 1;
 1283:             }
 1284:         }
 1285:     }
 1286:     my $itemcount = 1;
 1287:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1288:     $datatable .= '</tr></table></td></tr>';
 1289:     return $datatable;
 1290: }
 1291: 
 1292: sub role_defaults {
 1293:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1294:     my %defaults;
 1295:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1296:         return %defaults;
 1297:     }
 1298:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1299:     if ($role eq 'login') {
 1300:         %defaults = (
 1301:                        font => $defaultdesign{$role.'.font'},
 1302:                     );
 1303:         if (ref($logintext) eq 'ARRAY') {
 1304:             foreach my $item (@{$logintext}) {
 1305:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1306:             }
 1307:         }
 1308:         foreach my $item (@{$images}) {
 1309:             $defaults{'showlogo'}{$item} = 1;
 1310:         }
 1311:     } else {
 1312:         %defaults = (
 1313:                        img => $defaultdesign{$role.'.img'},
 1314:                        font => $defaultdesign{$role.'.font'},
 1315:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1316:                     );
 1317:     }
 1318:     foreach my $item (@{$bgs}) {
 1319:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1320:     }
 1321:     foreach my $item (@{$links}) {
 1322:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1323:     }
 1324:     foreach my $item (@{$images}) {
 1325:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1326:     }
 1327:     return %defaults;
 1328: }
 1329: 
 1330: sub display_color_options {
 1331:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1332:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1333:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1334:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1335:     my $datatable = '<tr'.$css_class.'>'.
 1336:         '<td>'.$choices->{'font'}.'</td>';
 1337:     if (!$is_custom->{'font'}) {
 1338:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1339:     } else {
 1340:         $datatable .= '<td>&nbsp;</td>';
 1341:     }
 1342:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1343: 
 1344:     $datatable .= '<td><span class="LC_nobreak">'.
 1345:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1346:                   ' value="'.$current_color.'" />&nbsp;'.
 1347:                   '&nbsp;</td></tr>';
 1348:     unless ($role eq 'login') { 
 1349:         $datatable .= '<tr'.$css_class.'>'.
 1350:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1351:         if (!$is_custom->{'fontmenu'}) {
 1352:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1353:         } else {
 1354:             $datatable .= '<td>&nbsp;</td>';
 1355:         }
 1356: 	$current_color = $designs->{'fontmenu'} ?
 1357: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1358:         $datatable .= '<td><span class="LC_nobreak">'.
 1359:                       '<input class="colorchooser" type="text" size="10" name="'
 1360: 		      .$role.'_fontmenu"'.
 1361:                       ' value="'.$current_color.'" />&nbsp;'.
 1362:                       '&nbsp;</td></tr>';
 1363:     }
 1364:     my $switchserver = &check_switchserver($dom,$confname);
 1365:     foreach my $img (@{$images}) {
 1366: 	$itemcount ++;
 1367:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1368:         $datatable .= '<tr'.$css_class.'>'.
 1369:                       '<td>'.$choices->{$img};
 1370:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1371:         if ($role eq 'login') {
 1372:             if ($img eq 'login') {
 1373:                 $login_hdr_pick =
 1374:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1375:                 $logincolors =
 1376:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1377:                                        $designs,$defaults);
 1378:             } elsif ($img ne 'domlogo') {
 1379:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1380:             }
 1381:         }
 1382:         $datatable .= '</td>';
 1383:         if ($designs->{$img} ne '') {
 1384:             $imgfile = $designs->{$img};
 1385: 	    $img_import = ($imgfile =~ m{^/adm/});
 1386:         } else {
 1387:             $imgfile = $defaults->{$img};
 1388:         }
 1389:         if ($imgfile) {
 1390:             my ($showfile,$fullsize);
 1391:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1392:                 my $urldir = $1;
 1393:                 my $filename = $2;
 1394:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1395:                 if (@info) {
 1396:                     my $thumbfile = 'tn-'.$filename;
 1397:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1398:                     if (@thumb) {
 1399:                         $showfile = $urldir.'/'.$thumbfile;
 1400:                     } else {
 1401:                         $showfile = $imgfile;
 1402:                     }
 1403:                 } else {
 1404:                     $showfile = '';
 1405:                 }
 1406:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1407:                 $showfile = $imgfile;
 1408:                 my $imgdir = $1;
 1409:                 my $filename = $2;
 1410:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1411:                     $showfile = "/$imgdir/tn-".$filename;
 1412:                 } else {
 1413:                     my $input = $londocroot.$imgfile;
 1414:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1415:                     if (!-e $output) {
 1416:                         my ($width,$height) = &thumb_dimensions();
 1417:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1418:                         if ($fullwidth ne '' && $fullheight ne '') {
 1419:                             if ($fullwidth > $width && $fullheight > $height) { 
 1420:                                 my $size = $width.'x'.$height;
 1421:                                 system("convert -sample $size $input $output");
 1422:                                 $showfile = "/$imgdir/tn-".$filename;
 1423:                             }
 1424:                         }
 1425:                     }
 1426:                 }
 1427:             }
 1428:             if ($showfile) {
 1429:                 if ($showfile =~ m{^/(adm|res)/}) {
 1430:                     if ($showfile =~ m{^/res/}) {
 1431:                         my $local_showfile =
 1432:                             &Apache::lonnet::filelocation('',$showfile);
 1433:                         &Apache::lonnet::repcopy($local_showfile);
 1434:                     }
 1435:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1436:                 }
 1437:                 if ($imgfile) {
 1438:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1439:                         if ($imgfile =~ m{^/res/}) {
 1440:                             my $local_imgfile =
 1441:                                 &Apache::lonnet::filelocation('',$imgfile);
 1442:                             &Apache::lonnet::repcopy($local_imgfile);
 1443:                         }
 1444:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1445:                     } else {
 1446:                         $fullsize = $imgfile;
 1447:                     }
 1448:                 }
 1449:                 $datatable .= '<td>';
 1450:                 if ($img eq 'login') {
 1451:                     $datatable .= $login_hdr_pick;
 1452:                 } 
 1453:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1454:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1455:             } else {
 1456:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1457:                               &mt('Upload:').'<br />';
 1458:             }
 1459:         } else {
 1460:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1461:                           &mt('Upload:').'<br />';
 1462:         }
 1463:         if ($switchserver) {
 1464:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1465:         } else {
 1466:             if ($img ne 'login') { # suppress file selection for Log-in header
 1467:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1468:             }
 1469:         }
 1470:         $datatable .= '</td></tr>';
 1471:     }
 1472:     $itemcount ++;
 1473:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1474:     $datatable .= '<tr'.$css_class.'>'.
 1475:                   '<td>'.$choices->{'bgs'}.'</td>';
 1476:     my $bgs_def;
 1477:     foreach my $item (@{$bgs}) {
 1478:         if (!$is_custom->{$item}) {
 1479:             $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>';
 1480:         }
 1481:     }
 1482:     if ($bgs_def) {
 1483:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1484:     } else {
 1485:         $datatable .= '<td>&nbsp;</td>';
 1486:     }
 1487:     $datatable .= '<td class="LC_right_item">'.
 1488:                   '<table border="0"><tr>';
 1489: 
 1490:     foreach my $item (@{$bgs}) {
 1491:         $datatable .= '<td align="center">'.$choices->{$item};
 1492: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1493:         if ($designs->{'bgs'}{$item}) {
 1494:             $datatable .= '&nbsp;';
 1495:         }
 1496:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1497:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1498:     }
 1499:     $datatable .= '</tr></table></td></tr>';
 1500:     $itemcount ++;
 1501:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1502:     $datatable .= '<tr'.$css_class.'>'.
 1503:                   '<td>'.$choices->{'links'}.'</td>';
 1504:     my $links_def;
 1505:     foreach my $item (@{$links}) {
 1506:         if (!$is_custom->{$item}) {
 1507:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1508:         }
 1509:     }
 1510:     if ($links_def) {
 1511:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1512:     } else {
 1513:         $datatable .= '<td>&nbsp;</td>';
 1514:     }
 1515:     $datatable .= '<td class="LC_right_item">'.
 1516:                   '<table border="0"><tr>';
 1517:     foreach my $item (@{$links}) {
 1518: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1519:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1520:         if ($designs->{'links'}{$item}) {
 1521:             $datatable.='&nbsp;';
 1522:         }
 1523:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1524:                       '" /></td>';
 1525:     }
 1526:     $$rowtotal += $itemcount;
 1527:     return $datatable;
 1528: }
 1529: 
 1530: sub logo_display_options {
 1531:     my ($img,$defaults,$designs) = @_;
 1532:     my $checkedon;
 1533:     if (ref($defaults) eq 'HASH') {
 1534:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1535:             if ($defaults->{'showlogo'}{$img}) {
 1536:                 $checkedon = 'checked="checked" ';     
 1537:             }
 1538:         } 
 1539:     }
 1540:     if (ref($designs) eq 'HASH') {
 1541:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1542:             if (defined($designs->{'showlogo'}{$img})) {
 1543:                 if ($designs->{'showlogo'}{$img} == 0) {
 1544:                     $checkedon = '';
 1545:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1546:                     $checkedon = 'checked="checked" ';
 1547:                 }
 1548:             }
 1549:         }
 1550:     }
 1551:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1552:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1553:            &mt('show').'</label>'."\n";
 1554: }
 1555: 
 1556: sub login_header_options  {
 1557:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1558:     my $output = '';
 1559:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1560:         $output .= &mt('Text default(s):').'<br />';
 1561:         if (!$is_custom->{'textcol'}) {
 1562:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1563:                        '&nbsp;&nbsp;&nbsp;';
 1564:         }
 1565:         if (!$is_custom->{'bgcol'}) {
 1566:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1567:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1568:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1569:         }
 1570:         $output .= '<br />';
 1571:     }
 1572:     $output .='<br />';
 1573:     return $output;
 1574: }
 1575: 
 1576: sub login_text_colors {
 1577:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1578:     my $color_menu = '<table border="0"><tr>';
 1579:     foreach my $item (@{$logintext}) {
 1580:         $color_menu .= '<td align="center">'.$choices->{$item};
 1581:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1582:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1583:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1584:     }
 1585:     $color_menu .= '</tr></table><br />';
 1586:     return $color_menu;
 1587: }
 1588: 
 1589: sub image_changes {
 1590:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1591:     my $output;
 1592:     if ($img eq 'login') {
 1593:             # suppress image for Log-in header
 1594:     } elsif (!$is_custom) {
 1595:         if ($img ne 'domlogo') {
 1596:             $output .= &mt('Default image:').'<br />';
 1597:         } else {
 1598:             $output .= &mt('Default in use:').'<br />';
 1599:         }
 1600:     }
 1601:     if ($img eq 'login') { # suppress image for Log-in header
 1602:         $output .= '<td>'.$logincolors;
 1603:     } else {
 1604:         if ($img_import) {
 1605:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1606:         }
 1607:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1608:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1609:         if ($is_custom) {
 1610:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1611:                        '<input type="checkbox" name="'.
 1612:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1613:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1614:         } else {
 1615:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1616:         }
 1617:     }
 1618:     return $output;
 1619: }
 1620: 
 1621: sub print_quotas {
 1622:     my ($dom,$settings,$rowtotal,$action) = @_;
 1623:     my $context;
 1624:     if ($action eq 'quotas') {
 1625:         $context = 'tools';
 1626:     } else {
 1627:         $context = $action;
 1628:     }
 1629:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1630:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1631:     my $typecount = 0;
 1632:     my ($css_class,%titles);
 1633:     if ($context eq 'requestcourses') {
 1634:         @usertools = ('official','unofficial','community','textbook');
 1635:         @options =('norequest','approval','validate','autolimit');
 1636:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1637:         %titles = &courserequest_titles();
 1638:     } elsif ($context eq 'requestauthor') {
 1639:         @usertools = ('author');
 1640:         @options = ('norequest','approval','automatic');
 1641:         %titles = &authorrequest_titles();
 1642:     } else {
 1643:         @usertools = ('aboutme','blog','webdav','portfolio');
 1644:         %titles = &tool_titles();
 1645:     }
 1646:     if (ref($types) eq 'ARRAY') {
 1647:         foreach my $type (@{$types}) {
 1648:             my ($currdefquota,$currauthorquota);
 1649:             unless (($context eq 'requestcourses') ||
 1650:                     ($context eq 'requestauthor')) {
 1651:                 if (ref($settings) eq 'HASH') {
 1652:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1653:                         $currdefquota = $settings->{defaultquota}->{$type};
 1654:                     } else {
 1655:                         $currdefquota = $settings->{$type};
 1656:                     }
 1657:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1658:                         $currauthorquota = $settings->{authorquota}->{$type};
 1659:                     }
 1660:                 }
 1661:             }
 1662:             if (defined($usertypes->{$type})) {
 1663:                 $typecount ++;
 1664:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1665:                 $datatable .= '<tr'.$css_class.'>'.
 1666:                               '<td>'.$usertypes->{$type}.'</td>'.
 1667:                               '<td class="LC_left_item">';
 1668:                 if ($context eq 'requestcourses') {
 1669:                     $datatable .= '<table><tr>';
 1670:                 }
 1671:                 my %cell;  
 1672:                 foreach my $item (@usertools) {
 1673:                     if ($context eq 'requestcourses') {
 1674:                         my ($curroption,$currlimit);
 1675:                         if (ref($settings) eq 'HASH') {
 1676:                             if (ref($settings->{$item}) eq 'HASH') {
 1677:                                 $curroption = $settings->{$item}->{$type};
 1678:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1679:                                     $currlimit = $1; 
 1680:                                 }
 1681:                             }
 1682:                         }
 1683:                         if (!$curroption) {
 1684:                             $curroption = 'norequest';
 1685:                         }
 1686:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1687:                         foreach my $option (@options) {
 1688:                             my $val = $option;
 1689:                             if ($option eq 'norequest') {
 1690:                                 $val = 0;  
 1691:                             }
 1692:                             if ($option eq 'validate') {
 1693:                                 my $canvalidate = 0;
 1694:                                 if (ref($validations{$item}) eq 'HASH') { 
 1695:                                     if ($validations{$item}{$type}) {
 1696:                                         $canvalidate = 1;
 1697:                                     }
 1698:                                 }
 1699:                                 next if (!$canvalidate);
 1700:                             }
 1701:                             my $checked = '';
 1702:                             if ($option eq $curroption) {
 1703:                                 $checked = ' checked="checked"';
 1704:                             } elsif ($option eq 'autolimit') {
 1705:                                 if ($curroption =~ /^autolimit/) {
 1706:                                     $checked = ' checked="checked"';
 1707:                                 }                       
 1708:                             } 
 1709:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1710:                                   '<input type="radio" name="crsreq_'.$item.
 1711:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1712:                                   $titles{$option}.'</label>';
 1713:                             if ($option eq 'autolimit') {
 1714:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1715:                                                 $item.'_limit_'.$type.'" size="1" '.
 1716:                                                 'value="'.$currlimit.'" />';
 1717:                             }
 1718:                             $cell{$item} .= '</span> ';
 1719:                             if ($option eq 'autolimit') {
 1720:                                 $cell{$item} .= $titles{'unlimited'};
 1721:                             }
 1722:                         }
 1723:                     } elsif ($context eq 'requestauthor') {
 1724:                         my $curroption;
 1725:                         if (ref($settings) eq 'HASH') {
 1726:                             $curroption = $settings->{$type};
 1727:                         }
 1728:                         if (!$curroption) {
 1729:                             $curroption = 'norequest';
 1730:                         }
 1731:                         foreach my $option (@options) {
 1732:                             my $val = $option;
 1733:                             if ($option eq 'norequest') {
 1734:                                 $val = 0;
 1735:                             }
 1736:                             my $checked = '';
 1737:                             if ($option eq $curroption) {
 1738:                                 $checked = ' checked="checked"';
 1739:                             }
 1740:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1741:                                   '<input type="radio" name="authorreq_'.$type.
 1742:                                   '" value="'.$val.'"'.$checked.' />'.
 1743:                                   $titles{$option}.'</label></span>&nbsp; ';
 1744:                         }
 1745:                     } else {
 1746:                         my $checked = 'checked="checked" ';
 1747:                         if (ref($settings) eq 'HASH') {
 1748:                             if (ref($settings->{$item}) eq 'HASH') {
 1749:                                 if ($settings->{$item}->{$type} == 0) {
 1750:                                     $checked = '';
 1751:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1752:                                     $checked =  'checked="checked" ';
 1753:                                 }
 1754:                             }
 1755:                         }
 1756:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1757:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1758:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1759:                                       '</label></span>&nbsp; ';
 1760:                     }
 1761:                 }
 1762:                 if ($context eq 'requestcourses') {
 1763:                     $datatable .= '</tr><tr>';
 1764:                     foreach my $item (@usertools) {
 1765:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1766:                     }
 1767:                     $datatable .= '</tr></table>';
 1768:                 }
 1769:                 $datatable .= '</td>';
 1770:                 unless (($context eq 'requestcourses') ||
 1771:                         ($context eq 'requestauthor')) {
 1772:                     $datatable .= 
 1773:                               '<td class="LC_right_item">'.
 1774:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1775:                               '<input type="text" name="quota_'.$type.
 1776:                               '" value="'.$currdefquota.
 1777:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1778:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1779:                               '<input type="text" name="authorquota_'.$type.
 1780:                               '" value="'.$currauthorquota.
 1781:                               '" size="5" /></span></td>';
 1782:                 }
 1783:                 $datatable .= '</tr>';
 1784:             }
 1785:         }
 1786:     }
 1787:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1788:         $defaultquota = '20';
 1789:         $authorquota = '500';
 1790:         if (ref($settings) eq 'HASH') {
 1791:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1792:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1793:             } elsif (defined($settings->{'default'})) {
 1794:                 $defaultquota = $settings->{'default'};
 1795:             }
 1796:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1797:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1798:             }
 1799:         }
 1800:     }
 1801:     $typecount ++;
 1802:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1803:     $datatable .= '<tr'.$css_class.'>'.
 1804:                   '<td>'.$othertitle.'</td>'.
 1805:                   '<td class="LC_left_item">';
 1806:     if ($context eq 'requestcourses') {
 1807:         $datatable .= '<table><tr>';
 1808:     }
 1809:     my %defcell;
 1810:     foreach my $item (@usertools) {
 1811:         if ($context eq 'requestcourses') {
 1812:             my ($curroption,$currlimit);
 1813:             if (ref($settings) eq 'HASH') {
 1814:                 if (ref($settings->{$item}) eq 'HASH') {
 1815:                     $curroption = $settings->{$item}->{'default'};
 1816:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1817:                         $currlimit = $1;
 1818:                     }
 1819:                 }
 1820:             }
 1821:             if (!$curroption) {
 1822:                 $curroption = 'norequest';
 1823:             }
 1824:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1825:             foreach my $option (@options) {
 1826:                 my $val = $option;
 1827:                 if ($option eq 'norequest') {
 1828:                     $val = 0;
 1829:                 }
 1830:                 if ($option eq 'validate') {
 1831:                     my $canvalidate = 0;
 1832:                     if (ref($validations{$item}) eq 'HASH') {
 1833:                         if ($validations{$item}{'default'}) {
 1834:                             $canvalidate = 1;
 1835:                         }
 1836:                     }
 1837:                     next if (!$canvalidate);
 1838:                 }
 1839:                 my $checked = '';
 1840:                 if ($option eq $curroption) {
 1841:                     $checked = ' checked="checked"';
 1842:                 } elsif ($option eq 'autolimit') {
 1843:                     if ($curroption =~ /^autolimit/) {
 1844:                         $checked = ' checked="checked"';
 1845:                     }
 1846:                 }
 1847:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1848:                                   '<input type="radio" name="crsreq_'.$item.
 1849:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1850:                                   $titles{$option}.'</label>';
 1851:                 if ($option eq 'autolimit') {
 1852:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1853:                                        $item.'_limit_default" size="1" '.
 1854:                                        'value="'.$currlimit.'" />';
 1855:                 }
 1856:                 $defcell{$item} .= '</span> ';
 1857:                 if ($option eq 'autolimit') {
 1858:                     $defcell{$item} .= $titles{'unlimited'};
 1859:                 }
 1860:             }
 1861:         } elsif ($context eq 'requestauthor') {
 1862:             my $curroption;
 1863:             if (ref($settings) eq 'HASH') {
 1864:                 $curroption = $settings->{'default'};
 1865:             }
 1866:             if (!$curroption) {
 1867:                 $curroption = 'norequest';
 1868:             }
 1869:             foreach my $option (@options) {
 1870:                 my $val = $option;
 1871:                 if ($option eq 'norequest') {
 1872:                     $val = 0;
 1873:                 }
 1874:                 my $checked = '';
 1875:                 if ($option eq $curroption) {
 1876:                     $checked = ' checked="checked"';
 1877:                 }
 1878:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1879:                               '<input type="radio" name="authorreq_default"'.
 1880:                               ' value="'.$val.'"'.$checked.' />'.
 1881:                               $titles{$option}.'</label></span>&nbsp; ';
 1882:             }
 1883:         } else {
 1884:             my $checked = 'checked="checked" ';
 1885:             if (ref($settings) eq 'HASH') {
 1886:                 if (ref($settings->{$item}) eq 'HASH') {
 1887:                     if ($settings->{$item}->{'default'} == 0) {
 1888:                         $checked = '';
 1889:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1890:                         $checked = 'checked="checked" ';
 1891:                     }
 1892:                 }
 1893:             }
 1894:             $datatable .= '<span class="LC_nobreak"><label>'.
 1895:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1896:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1897:                           '</label></span>&nbsp; ';
 1898:         }
 1899:     }
 1900:     if ($context eq 'requestcourses') {
 1901:         $datatable .= '</tr><tr>';
 1902:         foreach my $item (@usertools) {
 1903:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1904:         }
 1905:         $datatable .= '</tr></table>';
 1906:     }
 1907:     $datatable .= '</td>';
 1908:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1909:         $datatable .= '<td class="LC_right_item">'.
 1910:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1911:                       '<input type="text" name="defaultquota" value="'.
 1912:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1913:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1914:                       '<input type="text" name="authorquota" value="'.
 1915:                       $authorquota.'" size="5" /></span></td>';
 1916:     }
 1917:     $datatable .= '</tr>';
 1918:     $typecount ++;
 1919:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1920:     $datatable .= '<tr'.$css_class.'>'.
 1921:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1922:     if ($context eq 'requestcourses') {
 1923:         $datatable .= &mt('(overrides affiliation, if set)').
 1924:                       '</td>'.
 1925:                       '<td class="LC_left_item">'.
 1926:                       '<table><tr>';
 1927:     } else {
 1928:         $datatable .= &mt('(overrides affiliation, if checked)').
 1929:                       '</td>'.
 1930:                       '<td class="LC_left_item" colspan="2">'.
 1931:                       '<br />';
 1932:     }
 1933:     my %advcell;
 1934:     foreach my $item (@usertools) {
 1935:         if ($context eq 'requestcourses') {
 1936:             my ($curroption,$currlimit);
 1937:             if (ref($settings) eq 'HASH') {
 1938:                 if (ref($settings->{$item}) eq 'HASH') {
 1939:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1940:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1941:                         $currlimit = $1;
 1942:                     }
 1943:                 }
 1944:             }
 1945:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1946:             my $checked = '';
 1947:             if ($curroption eq '') {
 1948:                 $checked = ' checked="checked"';
 1949:             }
 1950:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1951:                                '<input type="radio" name="crsreq_'.$item.
 1952:                                '__LC_adv" value=""'.$checked.' />'.
 1953:                                &mt('No override set').'</label></span>&nbsp; ';
 1954:             foreach my $option (@options) {
 1955:                 my $val = $option;
 1956:                 if ($option eq 'norequest') {
 1957:                     $val = 0;
 1958:                 }
 1959:                 if ($option eq 'validate') {
 1960:                     my $canvalidate = 0;
 1961:                     if (ref($validations{$item}) eq 'HASH') {
 1962:                         if ($validations{$item}{'_LC_adv'}) {
 1963:                             $canvalidate = 1;
 1964:                         }
 1965:                     }
 1966:                     next if (!$canvalidate);
 1967:                 }
 1968:                 my $checked = '';
 1969:                 if ($val eq $curroption) {
 1970:                     $checked = ' checked="checked"';
 1971:                 } elsif ($option eq 'autolimit') {
 1972:                     if ($curroption =~ /^autolimit/) {
 1973:                         $checked = ' checked="checked"';
 1974:                     }
 1975:                 }
 1976:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1977:                                   '<input type="radio" name="crsreq_'.$item.
 1978:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1979:                                   $titles{$option}.'</label>';
 1980:                 if ($option eq 'autolimit') {
 1981:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1982:                                        $item.'_limit__LC_adv" size="1" '.
 1983:                                        'value="'.$currlimit.'" />';
 1984:                 }
 1985:                 $advcell{$item} .= '</span> ';
 1986:                 if ($option eq 'autolimit') {
 1987:                     $advcell{$item} .= $titles{'unlimited'};
 1988:                 }
 1989:             }
 1990:         } elsif ($context eq 'requestauthor') {
 1991:             my $curroption;
 1992:             if (ref($settings) eq 'HASH') {
 1993:                 $curroption = $settings->{'_LC_adv'};
 1994:             }
 1995:             my $checked = '';
 1996:             if ($curroption eq '') {
 1997:                 $checked = ' checked="checked"';
 1998:             }
 1999:             $datatable .= '<span class="LC_nobreak"><label>'.
 2000:                           '<input type="radio" name="authorreq__LC_adv"'.
 2001:                           ' value=""'.$checked.' />'.
 2002:                           &mt('No override set').'</label></span>&nbsp; ';
 2003:             foreach my $option (@options) {
 2004:                 my $val = $option;
 2005:                 if ($option eq 'norequest') {
 2006:                     $val = 0;
 2007:                 }
 2008:                 my $checked = '';
 2009:                 if ($val eq $curroption) {
 2010:                     $checked = ' checked="checked"';
 2011:                 }
 2012:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2013:                               '<input type="radio" name="authorreq__LC_adv"'.
 2014:                               ' value="'.$val.'"'.$checked.' />'.
 2015:                               $titles{$option}.'</label></span>&nbsp; ';
 2016:             }
 2017:         } else {
 2018:             my $checked = 'checked="checked" ';
 2019:             if (ref($settings) eq 'HASH') {
 2020:                 if (ref($settings->{$item}) eq 'HASH') {
 2021:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2022:                         $checked = '';
 2023:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2024:                         $checked = 'checked="checked" ';
 2025:                     }
 2026:                 }
 2027:             }
 2028:             $datatable .= '<span class="LC_nobreak"><label>'.
 2029:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2030:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2031:                           '</label></span>&nbsp; ';
 2032:         }
 2033:     }
 2034:     if ($context eq 'requestcourses') {
 2035:         $datatable .= '</tr><tr>';
 2036:         foreach my $item (@usertools) {
 2037:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2038:         }
 2039:         $datatable .= '</tr></table>';
 2040:     }
 2041:     $datatable .= '</td></tr>';
 2042:     $$rowtotal += $typecount;
 2043:     return $datatable;
 2044: }
 2045: 
 2046: sub print_requestmail {
 2047:     my ($dom,$action,$settings,$rowtotal) = @_;
 2048:     my ($now,$datatable,%currapp);
 2049:     $now = time;
 2050:     if (ref($settings) eq 'HASH') {
 2051:         if (ref($settings->{'notify'}) eq 'HASH') {
 2052:             if ($settings->{'notify'}{'approval'} ne '') {
 2053:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2054:             }
 2055:         }
 2056:     }
 2057:     my $numinrow = 2;
 2058:     my $css_class;
 2059:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2060:     my $text;
 2061:     if ($action eq 'requestcourses') {
 2062:         $text = &mt('Receive notification of course requests requiring approval');
 2063:     } elsif ($action eq 'requestauthor') {
 2064:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2065:     } else {
 2066:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2067:     }
 2068:     $datatable = '<tr'.$css_class.'>'.
 2069:                  ' <td>'.$text.'</td>'.
 2070:                  ' <td class="LC_left_item">';
 2071:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2072:                                                  $action.'notifyapproval',%currapp);
 2073:     if ($numdc > 0) {
 2074:         $datatable .= $table;
 2075:     } else {
 2076:         $datatable .= &mt('There are no active Domain Coordinators');
 2077:     }
 2078:     $datatable .='</td></tr>';
 2079:     return $datatable;
 2080: }
 2081: 
 2082: sub print_studentcode {
 2083:     my ($settings,$rowtotal) = @_;
 2084:     my $rownum = 0; 
 2085:     my ($output,%current);
 2086:     my @crstypes = ('official','unofficial','community','textbook');
 2087:     if (ref($settings) eq 'HASH') {
 2088:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2089:             foreach my $type (@crstypes) {
 2090:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2091:             }
 2092:         }
 2093:     }
 2094:     $output .= '<tr>'.
 2095:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2096:                '<td class="LC_left_item">';
 2097:     foreach my $type (@crstypes) {
 2098:         my $check = ' ';
 2099:         if ($current{$type}) {
 2100:             $check = ' checked="checked" ';
 2101:         }
 2102:         $output .= '<span class="LC_nobreak"><label>'.
 2103:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2104:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2105:     }
 2106:     $output .= '</td></tr>';
 2107:     $$rowtotal ++;
 2108:     return $output;
 2109: }
 2110: 
 2111: sub print_textbookcourses {
 2112:     my ($dom,$type,$settings,$rowtotal) = @_;
 2113:     my $rownum = 0;
 2114:     my $css_class;
 2115:     my $itemcount = 1;
 2116:     my $maxnum = 0;
 2117:     my $bookshash;
 2118:     if (ref($settings) eq 'HASH') {
 2119:         $bookshash = $settings->{$type};
 2120:     }
 2121:     my %ordered;
 2122:     if (ref($bookshash) eq 'HASH') {
 2123:         foreach my $item (keys(%{$bookshash})) {
 2124:             if (ref($bookshash->{$item}) eq 'HASH') {
 2125:                 my $num = $bookshash->{$item}{'order'};
 2126:                 $ordered{$num} = $item;
 2127:             }
 2128:         }
 2129:     }
 2130:     my $confname = $dom.'-domainconfig';
 2131:     my $switchserver = &check_switchserver($dom,$confname);
 2132:     my $maxnum = scalar(keys(%ordered));
 2133:     my $datatable;
 2134:     if (keys(%ordered)) {
 2135:         my @items = sort { $a <=> $b } keys(%ordered);
 2136:         for (my $i=0; $i<@items; $i++) {
 2137:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2138:             my $key = $ordered{$items[$i]};
 2139:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2140:             my $coursetitle = $coursehash{'description'};
 2141:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2142:             if (ref($bookshash->{$key}) eq 'HASH') {
 2143:                 $subject = $bookshash->{$key}->{'subject'};
 2144:                 $title = $bookshash->{$key}->{'title'};
 2145:                 if ($type eq 'textbooks') {
 2146:                     $publisher = $bookshash->{$key}->{'publisher'};
 2147:                     $author = $bookshash->{$key}->{'author'};
 2148:                     $image = $bookshash->{$key}->{'image'};
 2149:                     if ($image ne '') {
 2150:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2151:                         my $imagethumb = "$path/tn-".$imagefile;
 2152:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2153:                     }
 2154:                 }
 2155:             }
 2156:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2157:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2158:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2159:             for (my $k=0; $k<=$maxnum; $k++) {
 2160:                 my $vpos = $k+1;
 2161:                 my $selstr;
 2162:                 if ($k == $i) {
 2163:                     $selstr = ' selected="selected" ';
 2164:                 }
 2165:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2166:             }
 2167:             $datatable .= '</select>'.('&nbsp;'x2).
 2168:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2169:                 &mt('Delete?').'</label></span></td>'.
 2170:                 '<td colspan="2">'.
 2171:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2172:                 ('&nbsp;'x2).
 2173:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2174:             if ($type eq 'textbooks') {
 2175:                 $datatable .= ('&nbsp;'x2).
 2176:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2177:                               ('&nbsp;'x2).
 2178:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2179:                               ('&nbsp;'x2).
 2180:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2181:                 if ($image) {
 2182:                     $datatable .= '<span class="LC_nobreak">'.
 2183:                                   $imgsrc.
 2184:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2185:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2186:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2187:                 }
 2188:                 if ($switchserver) {
 2189:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2190:                 } else {
 2191:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2192:                 }
 2193:             }
 2194:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2195:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2196:                           $coursetitle.'</span></td></tr>'."\n";
 2197:             $itemcount ++;
 2198:         }
 2199:     }
 2200:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2201:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2202:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2203:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2204:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2205:     for (my $k=0; $k<$maxnum+1; $k++) {
 2206:         my $vpos = $k+1;
 2207:         my $selstr;
 2208:         if ($k == $maxnum) {
 2209:             $selstr = ' selected="selected" ';
 2210:         }
 2211:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2212:     }
 2213:     $datatable .= '</select>&nbsp;'."\n".
 2214:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2215:                   '<td colspan="2">'.
 2216:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2217:                   ('&nbsp;'x2).
 2218:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2219:                   ('&nbsp;'x2);
 2220:     if ($type eq 'textbooks') {
 2221:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2222:                       ('&nbsp;'x2).
 2223:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2224:                       ('&nbsp;'x2).
 2225:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2226:         if ($switchserver) {
 2227:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2228:         } else {
 2229:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2230:         }
 2231:     }
 2232:     $datatable .= '</span>'."\n".
 2233:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2234:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2235:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2236:                   &Apache::loncommon::selectcourse_link
 2237:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2238:                   '</span></td>'."\n".
 2239:                   '</tr>'."\n";
 2240:     $itemcount ++;
 2241:     return $datatable;
 2242: }
 2243: 
 2244: sub textbookcourses_javascript {
 2245:     my ($settings) = @_;
 2246:     return unless(ref($settings) eq 'HASH');
 2247:     my (%ordered,%total,%jstext);
 2248:     foreach my $type ('textbooks','templates') {
 2249:         $total{$type} = 0;
 2250:         if (ref($settings->{$type}) eq 'HASH') {
 2251:             foreach my $item (keys(%{$settings->{$type}})) {
 2252:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2253:                     my $num = $settings->{$type}->{$item}{'order'};
 2254:                     $ordered{$type}{$num} = $item;
 2255:                 }
 2256:             }
 2257:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2258:         }
 2259:         my @jsarray = ();
 2260:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2261:             push(@jsarray,$ordered{$type}{$item});
 2262:         }
 2263:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2264:     }
 2265:     return <<"ENDSCRIPT";
 2266: <script type="text/javascript">
 2267: // <![CDATA[
 2268: function reorderBooks(form,item,caller) {
 2269:     var changedVal;
 2270: $jstext{'textbooks'};
 2271: $jstext{'templates'};
 2272:     var newpos;
 2273:     var maxh;
 2274:     if (caller == 'textbooks') {  
 2275:         newpos = 'textbooks_addbook_pos';
 2276:         maxh = 1 + $total{'textbooks'};
 2277:     } else {
 2278:         newpos = 'templates_addbook_pos';
 2279:         maxh = 1 + $total{'templates'};
 2280:     }
 2281:     var current = new Array;
 2282:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2283:     if (item == newpos) {
 2284:         changedVal = newitemVal;
 2285:     } else {
 2286:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2287:         current[newitemVal] = newpos;
 2288:     }
 2289:     if (caller == 'textbooks') {
 2290:         for (var i=0; i<textbooks.length; i++) {
 2291:             var elementName = 'textbooks_'+textbooks[i];
 2292:             if (elementName != item) {
 2293:                 if (form.elements[elementName]) {
 2294:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2295:                     current[currVal] = elementName;
 2296:                 }
 2297:             }
 2298:         }
 2299:     }
 2300:     if (caller == 'templates') {
 2301:         for (var i=0; i<templates.length; i++) {
 2302:             var elementName = 'templates_'+templates[i];
 2303:             if (elementName != item) {
 2304:                 if (form.elements[elementName]) {
 2305:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2306:                     current[currVal] = elementName;
 2307:                 }
 2308:             }
 2309:         }
 2310:     }
 2311:     var oldVal;
 2312:     for (var j=0; j<maxh; j++) {
 2313:         if (current[j] == undefined) {
 2314:             oldVal = j;
 2315:         }
 2316:     }
 2317:     if (oldVal < changedVal) {
 2318:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2319:            var elementName = current[k];
 2320:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2321:         }
 2322:     } else {
 2323:         for (var k=changedVal; k<oldVal; k++) {
 2324:             var elementName = current[k];
 2325:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2326:         }
 2327:     }
 2328:     return;
 2329: }
 2330: 
 2331: // ]]>
 2332: </script>
 2333: 
 2334: ENDSCRIPT
 2335: }
 2336: 
 2337: sub print_autoenroll {
 2338:     my ($dom,$settings,$rowtotal) = @_;
 2339:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2340:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2341:     if (ref($settings) eq 'HASH') {
 2342:         if (exists($settings->{'run'})) {
 2343:             if ($settings->{'run'} eq '0') {
 2344:                 $runoff = ' checked="checked" ';
 2345:                 $runon = ' ';
 2346:             } else {
 2347:                 $runon = ' checked="checked" ';
 2348:                 $runoff = ' ';
 2349:             }
 2350:         } else {
 2351:             if ($autorun) {
 2352:                 $runon = ' checked="checked" ';
 2353:                 $runoff = ' ';
 2354:             } else {
 2355:                 $runoff = ' checked="checked" ';
 2356:                 $runon = ' ';
 2357:             }
 2358:         }
 2359:         if (exists($settings->{'co-owners'})) {
 2360:             if ($settings->{'co-owners'} eq '0') {
 2361:                 $coownersoff = ' checked="checked" ';
 2362:                 $coownerson = ' ';
 2363:             } else {
 2364:                 $coownerson = ' checked="checked" ';
 2365:                 $coownersoff = ' ';
 2366:             }
 2367:         } else {
 2368:             $coownersoff = ' checked="checked" ';
 2369:             $coownerson = ' ';
 2370:         }
 2371:         if (exists($settings->{'sender_domain'})) {
 2372:             $defdom = $settings->{'sender_domain'};
 2373:         }
 2374:     } else {
 2375:         if ($autorun) {
 2376:             $runon = ' checked="checked" ';
 2377:             $runoff = ' ';
 2378:         } else {
 2379:             $runoff = ' checked="checked" ';
 2380:             $runon = ' ';
 2381:         }
 2382:     }
 2383:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2384:     my $notif_sender;
 2385:     if (ref($settings) eq 'HASH') {
 2386:         $notif_sender = $settings->{'sender_uname'};
 2387:     }
 2388:     my $datatable='<tr class="LC_odd_row">'.
 2389:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2390:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2391:                   '<input type="radio" name="autoenroll_run"'.
 2392:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2393:                   '<label><input type="radio" name="autoenroll_run"'.
 2394:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2395:                   '</tr><tr>'.
 2396:                   '<td>'.&mt('Notification messages - sender').
 2397:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2398:                   &mt('username').':&nbsp;'.
 2399:                   '<input type="text" name="sender_uname" value="'.
 2400:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2401:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2402:                   '<tr class="LC_odd_row">'.
 2403:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2404:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2405:                   '<input type="radio" name="autoassign_coowners"'.
 2406:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2407:                   '<label><input type="radio" name="autoassign_coowners"'.
 2408:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2409:                   '</tr>';
 2410:     $$rowtotal += 3;
 2411:     return $datatable;
 2412: }
 2413: 
 2414: sub print_autoupdate {
 2415:     my ($position,$dom,$settings,$rowtotal) = @_;
 2416:     my $datatable;
 2417:     if ($position eq 'top') {
 2418:         my $updateon = ' ';
 2419:         my $updateoff = ' checked="checked" ';
 2420:         my $classlistson = ' ';
 2421:         my $classlistsoff = ' checked="checked" ';
 2422:         if (ref($settings) eq 'HASH') {
 2423:             if ($settings->{'run'} eq '1') {
 2424:                 $updateon = $updateoff;
 2425:                 $updateoff = ' ';
 2426:             }
 2427:             if ($settings->{'classlists'} eq '1') {
 2428:                 $classlistson = $classlistsoff;
 2429:                 $classlistsoff = ' ';
 2430:             }
 2431:         }
 2432:         my %title = (
 2433:                    run => 'Auto-update active?',
 2434:                    classlists => 'Update information in classlists?',
 2435:                     );
 2436:         $datatable = '<tr class="LC_odd_row">'. 
 2437:                   '<td>'.&mt($title{'run'}).'</td>'.
 2438:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2439:                   '<input type="radio" name="autoupdate_run"'.
 2440:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2441:                   '<label><input type="radio" name="autoupdate_run"'.
 2442:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2443:                   '</tr><tr>'.
 2444:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2445:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2446:                   '<label><input type="radio" name="classlists"'.
 2447:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2448:                   '<label><input type="radio" name="classlists"'.
 2449:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2450:                   '</tr>';
 2451:         $$rowtotal += 2;
 2452:     } elsif ($position eq 'middle') {
 2453:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2454:         my $numinrow = 3;
 2455:         my $locknamesettings;
 2456:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2457:                                      $dom,$numinrow,$othertitle,
 2458:                                     'lockablenames');
 2459:         $$rowtotal ++;
 2460:     } else {
 2461:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2462:         my @fields = ('lastname','firstname','middlename','generation',
 2463:                       'permanentemail','id');
 2464:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2465:         my $numrows = 0;
 2466:         if (ref($types) eq 'ARRAY') {
 2467:             if (@{$types} > 0) {
 2468:                 $datatable = 
 2469:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2470:                                          \@fields,$types,\$numrows);
 2471:                     $$rowtotal += @{$types}; 
 2472:             }
 2473:         }
 2474:         $datatable .= 
 2475:             &usertype_update_row($settings,{'default' => $othertitle},
 2476:                                  \%fieldtitles,\@fields,['default'],
 2477:                                  \$numrows);
 2478:         $$rowtotal ++;     
 2479:     }
 2480:     return $datatable;
 2481: }
 2482: 
 2483: sub print_autocreate {
 2484:     my ($dom,$settings,$rowtotal) = @_;
 2485:     my (%createon,%createoff,%currhash);
 2486:     my @types = ('xml','req');
 2487:     if (ref($settings) eq 'HASH') {
 2488:         foreach my $item (@types) {
 2489:             $createoff{$item} = ' checked="checked" ';
 2490:             $createon{$item} = ' ';
 2491:             if (exists($settings->{$item})) {
 2492:                 if ($settings->{$item}) {
 2493:                     $createon{$item} = ' checked="checked" ';
 2494:                     $createoff{$item} = ' ';
 2495:                 }
 2496:             }
 2497:         }
 2498:         if ($settings->{'xmldc'} ne '') {
 2499:             $currhash{$settings->{'xmldc'}} = 1;
 2500:         }
 2501:     } else {
 2502:         foreach my $item (@types) {
 2503:             $createoff{$item} = ' checked="checked" ';
 2504:             $createon{$item} = ' ';
 2505:         }
 2506:     }
 2507:     $$rowtotal += 2;
 2508:     my $numinrow = 2;
 2509:     my $datatable='<tr class="LC_odd_row">'.
 2510:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2511:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2512:                   '<input type="radio" name="autocreate_xml"'.
 2513:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2514:                   '<label><input type="radio" name="autocreate_xml"'.
 2515:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2516:                   '</td></tr><tr>'.
 2517:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2518:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2519:                   '<input type="radio" name="autocreate_req"'.
 2520:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2521:                   '<label><input type="radio" name="autocreate_req"'.
 2522:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2523:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2524:                                                    'autocreate_xmldc',%currhash);
 2525:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2526:     if ($numdc > 1) {
 2527:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2528:                       '</td><td class="LC_left_item">';
 2529:     } else {
 2530:         $datatable .= &mt('Course creation processed as:').
 2531:                       '</td><td class="LC_right_item">';
 2532:     }
 2533:     $datatable .= $dctable.'</td></tr>';
 2534:     $$rowtotal += $rows;
 2535:     return $datatable;
 2536: }
 2537: 
 2538: sub print_directorysrch {
 2539:     my ($dom,$settings,$rowtotal) = @_;
 2540:     my $srchon = ' ';
 2541:     my $srchoff = ' checked="checked" ';
 2542:     my ($exacton,$containson,$beginson);
 2543:     my $localon = ' ';
 2544:     my $localoff = ' checked="checked" ';
 2545:     if (ref($settings) eq 'HASH') {
 2546:         if ($settings->{'available'} eq '1') {
 2547:             $srchon = $srchoff;
 2548:             $srchoff = ' ';
 2549:         }
 2550:         if ($settings->{'localonly'} eq '1') {
 2551:             $localon = $localoff;
 2552:             $localoff = ' ';
 2553:         }
 2554:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2555:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2556:                 if ($type eq 'exact') {
 2557:                     $exacton = ' checked="checked" ';
 2558:                 } elsif ($type eq 'contains') {
 2559:                     $containson = ' checked="checked" ';
 2560:                 } elsif ($type eq 'begins') {
 2561:                     $beginson = ' checked="checked" ';
 2562:                 }
 2563:             }
 2564:         } else {
 2565:             if ($settings->{'searchtypes'} eq 'exact') {
 2566:                 $exacton = ' checked="checked" ';
 2567:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2568:                 $containson = ' checked="checked" ';
 2569:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2570:                 $exacton = ' checked="checked" ';
 2571:                 $containson = ' checked="checked" ';
 2572:             }
 2573:         }
 2574:     }
 2575:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2576:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2577: 
 2578:     my $numinrow = 4;
 2579:     my $cansrchrow = 0;
 2580:     my $datatable='<tr class="LC_odd_row">'.
 2581:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2582:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2583:                   '<input type="radio" name="dirsrch_available"'.
 2584:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2585:                   '<label><input type="radio" name="dirsrch_available"'.
 2586:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2587:                   '</tr><tr>'.
 2588:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2589:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2590:                   '<input type="radio" name="dirsrch_localonly"'.
 2591:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2592:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2593:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2594:                   '</tr>';
 2595:     $$rowtotal += 2;
 2596:     if (ref($usertypes) eq 'HASH') {
 2597:         if (keys(%{$usertypes}) > 0) {
 2598:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2599:                                          $numinrow,$othertitle,'cansearch');
 2600:             $cansrchrow = 1;
 2601:         }
 2602:     }
 2603:     if ($cansrchrow) {
 2604:         $$rowtotal ++;
 2605:         $datatable .= '<tr>';
 2606:     } else {
 2607:         $datatable .= '<tr class="LC_odd_row">';
 2608:     }
 2609:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2610:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2611:     foreach my $title (@{$titleorder}) {
 2612:         if (defined($searchtitles->{$title})) {
 2613:             my $check = ' ';
 2614:             if (ref($settings) eq 'HASH') {
 2615:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2616:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2617:                         $check = ' checked="checked" ';
 2618:                     }
 2619:                 }
 2620:             }
 2621:             $datatable .= '<td class="LC_left_item">'.
 2622:                           '<span class="LC_nobreak"><label>'.
 2623:                           '<input type="checkbox" name="searchby" '.
 2624:                           'value="'.$title.'"'.$check.'/>'.
 2625:                           $searchtitles->{$title}.'</label></span></td>';
 2626:         }
 2627:     }
 2628:     $datatable .= '</tr></table></td></tr>';
 2629:     $$rowtotal ++;
 2630:     if ($cansrchrow) {
 2631:         $datatable .= '<tr class="LC_odd_row">';
 2632:     } else {
 2633:         $datatable .= '<tr>';
 2634:     }
 2635:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2636:                   '<td class="LC_left_item" colspan="2">'.
 2637:                   '<span class="LC_nobreak"><label>'.
 2638:                   '<input type="checkbox" name="searchtypes" '.
 2639:                   $exacton.' value="exact" />'.&mt('Exact match').
 2640:                   '</label>&nbsp;'.
 2641:                   '<label><input type="checkbox" name="searchtypes" '.
 2642:                   $beginson.' value="begins" />'.&mt('Begins with').
 2643:                   '</label>&nbsp;'.
 2644:                   '<label><input type="checkbox" name="searchtypes" '.
 2645:                   $containson.' value="contains" />'.&mt('Contains').
 2646:                   '</label></span></td></tr>';
 2647:     $$rowtotal ++;
 2648:     return $datatable;
 2649: }
 2650: 
 2651: sub print_contacts {
 2652:     my ($dom,$settings,$rowtotal) = @_;
 2653:     my $datatable;
 2654:     my @contacts = ('adminemail','supportemail');
 2655:     my (%checked,%to,%otheremails,%bccemails);
 2656:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2657:                     'requestsmail','updatesmail','idconflictsmail');
 2658:     foreach my $type (@mailings) {
 2659:         $otheremails{$type} = '';
 2660:     }
 2661:     $bccemails{'helpdeskmail'} = '';
 2662:     if (ref($settings) eq 'HASH') {
 2663:         foreach my $item (@contacts) {
 2664:             if (exists($settings->{$item})) {
 2665:                 $to{$item} = $settings->{$item};
 2666:             }
 2667:         }
 2668:         foreach my $type (@mailings) {
 2669:             if (exists($settings->{$type})) {
 2670:                 if (ref($settings->{$type}) eq 'HASH') {
 2671:                     foreach my $item (@contacts) {
 2672:                         if ($settings->{$type}{$item}) {
 2673:                             $checked{$type}{$item} = ' checked="checked" ';
 2674:                         }
 2675:                     }
 2676:                     $otheremails{$type} = $settings->{$type}{'others'};
 2677:                     if ($type eq 'helpdeskmail') {
 2678:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2679:                     }
 2680:                 }
 2681:             } elsif ($type eq 'lonstatusmail') {
 2682:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2683:             }
 2684:         }
 2685:     } else {
 2686:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2687:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2688:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2689:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2690:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2691:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2692:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2693:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2694:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2695:     }
 2696:     my ($titles,$short_titles) = &contact_titles();
 2697:     my $rownum = 0;
 2698:     my $css_class;
 2699:     foreach my $item (@contacts) {
 2700:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2701:         $datatable .= '<tr'.$css_class.'>'. 
 2702:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2703:                   '</span></td><td class="LC_right_item">'.
 2704:                   '<input type="text" name="'.$item.'" value="'.
 2705:                   $to{$item}.'" /></td></tr>';
 2706:         $rownum ++;
 2707:     }
 2708:     foreach my $type (@mailings) {
 2709:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2710:         $datatable .= '<tr'.$css_class.'>'.
 2711:                       '<td><span class="LC_nobreak">'.
 2712:                       $titles->{$type}.': </span></td>'.
 2713:                       '<td class="LC_left_item">'.
 2714:                       '<span class="LC_nobreak">';
 2715:         foreach my $item (@contacts) {
 2716:             $datatable .= '<label>'.
 2717:                           '<input type="checkbox" name="'.$type.'"'.
 2718:                           $checked{$type}{$item}.
 2719:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2720:                           '</label>&nbsp;';
 2721:         }
 2722:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2723:                       '<input type="text" name="'.$type.'_others" '.
 2724:                       'value="'.$otheremails{$type}.'"  />';
 2725:         if ($type eq 'helpdeskmail') {
 2726:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2727:                           '<input type="text" name="'.$type.'_bcc" '.
 2728:                           'value="'.$bccemails{$type}.'"  />';
 2729:         }
 2730:         $datatable .= '</td></tr>'."\n";
 2731:         $rownum ++;
 2732:     }
 2733:     my %choices;
 2734:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2735:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2736:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2737:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2738:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2739:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2740:     my @toggles = ('reporterrors','reportupdates');
 2741:     my %defaultchecked = ('reporterrors'  => 'on',
 2742:                           'reportupdates' => 'on');
 2743:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2744:                                                \%choices,$rownum);
 2745:     $datatable .= $reports;
 2746:     $$rowtotal += $rownum;
 2747:     return $datatable;
 2748: }
 2749: 
 2750: sub print_helpsettings {
 2751:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2752:     my ($datatable,$itemcount);
 2753:     $itemcount = 1;
 2754:     my (%choices,%defaultchecked,@toggles);
 2755:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2756:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2757:                                  &mt('LON-CAPA bug tracker'),600,500));
 2758:     %defaultchecked = ('submitbugs' => 'on');
 2759:     @toggles = ('submitbugs',);
 2760: 
 2761:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2762:                                                  \%choices,$itemcount);
 2763:     return $datatable;
 2764: }
 2765: 
 2766: sub radiobutton_prefs {
 2767:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2768:         $additional) = @_;
 2769:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2770:                    (ref($choices) eq 'HASH'));
 2771: 
 2772:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2773: 
 2774:     foreach my $item (@{$toggles}) {
 2775:         if ($defaultchecked->{$item} eq 'on') {
 2776:             $checkedon{$item} = ' checked="checked" ';
 2777:             $checkedoff{$item} = ' ';
 2778:         } elsif ($defaultchecked->{$item} eq 'off') {
 2779:             $checkedoff{$item} = ' checked="checked" ';
 2780:             $checkedon{$item} = ' ';
 2781:         }
 2782:     }
 2783:     if (ref($settings) eq 'HASH') {
 2784:         foreach my $item (@{$toggles}) {
 2785:             if ($settings->{$item} eq '1') {
 2786:                 $checkedon{$item} =  ' checked="checked" ';
 2787:                 $checkedoff{$item} = ' ';
 2788:             } elsif ($settings->{$item} eq '0') {
 2789:                 $checkedoff{$item} =  ' checked="checked" ';
 2790:                 $checkedon{$item} = ' ';
 2791:             }
 2792:         }
 2793:     }
 2794:     if ($onclick) {
 2795:         $onclick = ' onclick="'.$onclick.'"';
 2796:     }
 2797:     foreach my $item (@{$toggles}) {
 2798:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2799:         $datatable .=
 2800:             '<tr'.$css_class.'><td valign="top">'.
 2801:             '<span class="LC_nobreak">'.$choices->{$item}.
 2802:             '</span></td>'.
 2803:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2804:             '<label><input type="radio" name="'.
 2805:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2806:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2807:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2808:             '</span>'.$additional.
 2809:             '</td>'.
 2810:             '</tr>';
 2811:         $itemcount ++;
 2812:     }
 2813:     return ($datatable,$itemcount);
 2814: }
 2815: 
 2816: sub print_coursedefaults {
 2817:     my ($position,$dom,$settings,$rowtotal) = @_;
 2818:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2819:     my $itemcount = 1;
 2820:     my %choices =  &Apache::lonlocal::texthash (
 2821:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2822:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2823:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2824:         coursecredits        => 'Credits can be specified for courses',
 2825:     );
 2826:     my %staticdefaults = (
 2827:                            anonsurvey_threshold => 10,
 2828:                            uploadquota          => 500,
 2829:                          );
 2830:     if ($position eq 'top') {
 2831:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2832:         @toggles = ('canuse_pdfforms');
 2833:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2834:                                                  \%choices,$itemcount);
 2835:     } else {
 2836:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2837:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2838:             %curruploadquota);
 2839:         my $currusecredits = 0;
 2840:         my @types = ('official','unofficial','community','textbook');
 2841:         if (ref($settings) eq 'HASH') {
 2842:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2843:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2844:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2845:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2846:                 }
 2847:             }
 2848:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2849:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2850:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2851:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2852:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2853:                     ($def_textbook_credits ne '')) {
 2854:                     $currusecredits = 1;
 2855:                 }
 2856:             }
 2857:         }
 2858:         if (!$currdefresponder) {
 2859:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2860:         } elsif ($currdefresponder < 1) {
 2861:             $currdefresponder = 1;
 2862:         }
 2863:         foreach my $type (@types) {
 2864:             if ($curruploadquota{$type} eq '') {
 2865:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2866:             }
 2867:         }
 2868:         $datatable .=
 2869:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2870:                 $choices{'anonsurvey_threshold'}.
 2871:                 '</span></td>'.
 2872:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2873:                 '<input type="text" name="anonsurvey_threshold"'.
 2874:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2875:                 '</td></tr>'."\n";
 2876:         $itemcount ++;
 2877:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2878:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2879:                       $choices{'uploadquota'}.
 2880:                       '</span></td>'.
 2881:                       '<td align="right" class="LC_right_item">'.
 2882:                       '<table><tr>';
 2883:         foreach my $type (@types) {
 2884:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2885:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2886:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2887:         }
 2888:         $datatable .= '</tr></table></td></tr>'."\n";
 2889:         $itemcount ++;
 2890:         my $onclick = "toggleDisplay(this.form,'credits');";
 2891:         my $display = 'none';
 2892:         if ($currusecredits) {
 2893:             $display = 'block';
 2894:         }
 2895:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2896:                          '<span class="LC_nobreak">'.
 2897:                          &mt('Default credits for official courses [_1]',
 2898:                          '<input type="text" name="official_credits" value="'.
 2899:                          $def_official_credits.'" size="3" />').
 2900:                          '</span><br />'.
 2901:                          '<span class="LC_nobreak">'.
 2902:                          &mt('Default credits for unofficial courses [_1]',
 2903:                          '<input type="text" name="unofficial_credits" value="'.
 2904:                          $def_unofficial_credits.'" size="3" />').
 2905:                          '</span><br />'.
 2906:                          '<span class="LC_nobreak">'.
 2907:                          &mt('Default credits for textbook courses [_1]',
 2908:                          '<input type="text" name="textbook_credits" value="'.
 2909:                          $def_textbook_credits.'" size="3" />').
 2910:                          '</span></div>'."\n";
 2911:         %defaultchecked = ('coursecredits' => 'off');
 2912:         @toggles = ('coursecredits');
 2913:         my $current = {
 2914:                         'coursecredits' => $currusecredits,
 2915:                       };
 2916:         (my $table,$itemcount) =
 2917:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2918:                                \%choices,$itemcount,$onclick,$additional);
 2919:         $datatable .= $table;
 2920:         $itemcount ++;
 2921:     }
 2922:     $$rowtotal += $itemcount;
 2923:     return $datatable;
 2924: }
 2925: 
 2926: sub print_selfenrollment {
 2927:     my ($position,$dom,$settings,$rowtotal) = @_;
 2928:     my ($css_class,$datatable);
 2929:     my $itemcount = 1;
 2930:     my @types = ('official','unofficial','community','textbook');
 2931:     if (($position eq 'top') || ($position eq 'middle')) {
 2932:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 2933:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 2934:         my @rows;
 2935:         my $key;
 2936:         if ($position eq 'top') {
 2937:             $key = 'admin'; 
 2938:             if (ref($rowsref) eq 'ARRAY') {
 2939:                 @rows = @{$rowsref};
 2940:             }
 2941:         } elsif ($position eq 'middle') {
 2942:             $key = 'default';
 2943:             @rows = ('types','registered','approval','limit');
 2944:         }
 2945:         foreach my $row (@rows) {
 2946:             if (defined($titlesref->{$row})) {
 2947:                 $itemcount ++;
 2948:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2949:                 $datatable .= '<tr'.$css_class.'>'.
 2950:                               '<td>'.$titlesref->{$row}.'</td>'.
 2951:                               '<td class="LC_left_item">'.
 2952:                               '<table><tr>';
 2953:                 my (%current,%currentcap);
 2954:                 if (ref($settings) eq 'HASH') {
 2955:                     if (ref($settings->{$key}) eq 'HASH') {
 2956:                         foreach my $type (@types) {
 2957:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2958:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 2959:                             }
 2960:                             if (($row eq 'limit') && ($key eq 'default')) {
 2961:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2962:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 2963:                                 }
 2964:                             }
 2965:                         }
 2966:                     }
 2967:                 }
 2968:                 my %roles = (
 2969:                              '0' => &Apache::lonnet::plaintext('dc'),
 2970:                             ); 
 2971:             
 2972:                 foreach my $type (@types) {
 2973:                     unless (($row eq 'registered') && ($key eq 'default')) {
 2974:                         $datatable .= '<th>'.&mt($type).'</th>';
 2975:                     }
 2976:                 }
 2977:                 unless (($row eq 'registered') && ($key eq 'default')) {
 2978:                     $datatable .= '</tr><tr>';
 2979:                 }
 2980:                 foreach my $type (@types) {
 2981:                     if ($type eq 'community') {
 2982:                         $roles{'1'} = &mt('Community personnel');
 2983:                     } else {
 2984:                         $roles{'1'} = &mt('Course personnel');
 2985:                     }
 2986:                     $datatable .= '<td style="vertical-align: top">';
 2987:                     if ($position eq 'top') {
 2988:                         my %checked;
 2989:                         if ($current{$type} eq '0') {
 2990:                             $checked{'0'} = ' checked="checked"';
 2991:                         } else {
 2992:                             $checked{'1'} = ' checked="checked"';
 2993:                         }
 2994:                         foreach my $role ('1','0') {
 2995:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2996:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 2997:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 2998:                                           $roles{$role}.'</label></span> ';
 2999:                         }
 3000:                     } else {
 3001:                         if ($row eq 'types') {
 3002:                             my %checked;
 3003:                             if ($current{$type} =~ /^(all|dom)$/) {
 3004:                                 $checked{$1} = ' checked="checked"';
 3005:                             } else {
 3006:                                 $checked{''} = ' checked="checked"';
 3007:                             }
 3008:                             foreach my $val ('','dom','all') {
 3009:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3010:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3011:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3012:                             }
 3013:                         } elsif ($row eq 'registered') {
 3014:                             my %checked;
 3015:                             if ($current{$type} eq '1') {
 3016:                                 $checked{'1'} = ' checked="checked"';
 3017:                             } else {
 3018:                                 $checked{'0'} = ' checked="checked"';
 3019:                             }
 3020:                             foreach my $val ('0','1') {
 3021:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3022:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3023:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3024:                             }
 3025:                         } elsif ($row eq 'approval') {
 3026:                             my %checked;
 3027:                             if ($current{$type} =~ /^([12])$/) {
 3028:                                 $checked{$1} = ' checked="checked"';
 3029:                             } else {
 3030:                                 $checked{'0'} = ' checked="checked"';
 3031:                             }
 3032:                             for my $val (0..2) {
 3033:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3034:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3035:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3036:                             }
 3037:                         } elsif ($row eq 'limit') {
 3038:                             my %checked;
 3039:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3040:                                 $checked{$1} = ' checked="checked"';
 3041:                             } else {
 3042:                                 $checked{'none'} = ' checked="checked"';
 3043:                             }
 3044:                             my $cap;
 3045:                             if ($currentcap{$type} =~ /^\d+$/) {
 3046:                                 $cap = $currentcap{$type};
 3047:                             }
 3048:                             foreach my $val ('none','allstudents','selfenrolled') {
 3049:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3050:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3051:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3052:                             }
 3053:                             $datatable .= '<br />'.
 3054:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3055:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3056:                                           '</span>'; 
 3057:                         }
 3058:                     }
 3059:                     $datatable .= '</td>';
 3060:                 }
 3061:                 $datatable .= '</tr>';
 3062:             }
 3063:             $datatable .= '</table></td></tr>';
 3064:         }
 3065:     } elsif ($position eq 'bottom') {
 3066:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3067:     }
 3068:     $$rowtotal += $itemcount;
 3069:     return $datatable;
 3070: }
 3071: 
 3072: sub print_validation_rows {
 3073:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3074:     my ($itemsref,$namesref,$fieldsref);
 3075:     if ($caller eq 'selfenroll') { 
 3076:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3077:     } elsif ($caller eq 'requestcourses') {
 3078:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3079:     }
 3080:     my %currvalidation;
 3081:     if (ref($settings) eq 'HASH') {
 3082:         if (ref($settings->{'validation'}) eq 'HASH') {
 3083:             %currvalidation = %{$settings->{'validation'}};
 3084:         }
 3085:     }
 3086:     my $datatable;
 3087:     my $itemcount = 0;
 3088:     foreach my $item (@{$itemsref}) {
 3089:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3090:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3091:                       $namesref->{$item}.
 3092:                       '</span></td>'.
 3093:                       '<td class="LC_left_item">';
 3094:         if (($item eq 'url') || ($item eq 'button')) {
 3095:             $datatable .= '<span class="LC_nobreak">'.
 3096:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3097:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3098:         } elsif ($item eq 'fields') {
 3099:             my @currfields;
 3100:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3101:                 @currfields = @{$currvalidation{$item}};
 3102:             }
 3103:             foreach my $field (@{$fieldsref}) {
 3104:                 my $check = '';
 3105:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3106:                     $check = ' checked="checked"';
 3107:                 }
 3108:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3109:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3110:                               ' value="'.$field.'"'.$check.' />'.$field.
 3111:                               '</label></span> ';
 3112:             }
 3113:         } elsif ($item eq 'markup') {
 3114:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3115:                            $currvalidation{$item}.
 3116:                               '</textarea>';
 3117:         }
 3118:         $datatable .= '</td></tr>'."\n";
 3119:         if (ref($rowtotal)) {
 3120:             $itemcount ++;
 3121:         }
 3122:     }
 3123:     if ($caller eq 'requestcourses') {
 3124:         my %currhash;
 3125:         if (ref($settings) eq 'HASH') {
 3126:             if (ref($settings->{'validation'}) eq 'HASH') {
 3127:                 if ($settings->{'validation'}{'dc'} ne '') {
 3128:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 3129:                 }
 3130:             }
 3131:         }
 3132:         my $numinrow = 2;
 3133:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3134:                                                        'validationdc',%currhash);
 3135:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3136:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 3137:         if ($numdc > 1) {
 3138:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 3139:         } else {
 3140:             $datatable .=  &mt('Course creation processed as: ');
 3141:         }
 3142:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3143:         $itemcount ++;
 3144:     }
 3145:     if (ref($rowtotal)) {
 3146:         $$rowtotal += $itemcount;
 3147:     }
 3148:     return $datatable;
 3149: }
 3150: 
 3151: sub print_usersessions {
 3152:     my ($position,$dom,$settings,$rowtotal) = @_;
 3153:     my ($css_class,$datatable,%checked,%choices);
 3154:     my (%by_ip,%by_location,@intdoms);
 3155:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3156: 
 3157:     my @alldoms = &Apache::lonnet::all_domains();
 3158:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3159:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3160:     my %altids = &id_for_thisdom(%servers);
 3161:     my $itemcount = 1;
 3162:     if ($position eq 'top') {
 3163:         if (keys(%serverhomes) > 1) {
 3164:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3165:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 3166:         } else {
 3167:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3168:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3169:         }
 3170:     } else {
 3171:         if (keys(%by_location) == 0) {
 3172:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3173:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3174:         } else {
 3175:             my %lt = &usersession_titles();
 3176:             my $numinrow = 5;
 3177:             my $prefix;
 3178:             my @types;
 3179:             if ($position eq 'bottom') {
 3180:                 $prefix = 'remote';
 3181:                 @types = ('version','excludedomain','includedomain');
 3182:             } else {
 3183:                 $prefix = 'hosted';
 3184:                 @types = ('excludedomain','includedomain');
 3185:             }
 3186:             my (%current,%checkedon,%checkedoff);
 3187:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3188:             my @locations = sort(keys(%by_location));
 3189:             foreach my $type (@types) {
 3190:                 $checkedon{$type} = '';
 3191:                 $checkedoff{$type} = ' checked="checked"';
 3192:             }
 3193:             if (ref($settings) eq 'HASH') {
 3194:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3195:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3196:                         $current{$key} = $settings->{$prefix}{$key};
 3197:                         if ($key eq 'version') {
 3198:                             if ($current{$key} ne '') {
 3199:                                 $checkedon{$key} = ' checked="checked"';
 3200:                                 $checkedoff{$key} = '';
 3201:                             }
 3202:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3203:                             $checkedon{$key} = ' checked="checked"';
 3204:                             $checkedoff{$key} = '';
 3205:                         }
 3206:                     }
 3207:                 }
 3208:             }
 3209:             foreach my $type (@types) {
 3210:                 next if ($type ne 'version' && !@locations);
 3211:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3212:                 $datatable .= '<tr'.$css_class.'>
 3213:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3214:                                <span class="LC_nobreak">&nbsp;
 3215:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3216:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3217:                 if ($type eq 'version') {
 3218:                     my $selector = '<select name="'.$prefix.'_version">';
 3219:                     foreach my $version (@lcversions) {
 3220:                         my $selected = '';
 3221:                         if ($current{'version'} eq $version) {
 3222:                             $selected = ' selected="selected"';
 3223:                         }
 3224:                         $selector .= ' <option value="'.$version.'"'.
 3225:                                      $selected.'>'.$version.'</option>';
 3226:                     }
 3227:                     $selector .= '</select> ';
 3228:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3229:                 } else {
 3230:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3231:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3232:                                  ' />'.('&nbsp;'x2).
 3233:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3234:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3235:                                  "\n".
 3236:                                  '</div><div><table>';
 3237:                     my $rem;
 3238:                     for (my $i=0; $i<@locations; $i++) {
 3239:                         my ($showloc,$value,$checkedtype);
 3240:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3241:                             my $ip = $by_location{$locations[$i]}->[0];
 3242:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3243:                                  $value = join(':',@{$by_ip{$ip}});
 3244:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3245:                                 if (ref($current{$type}) eq 'ARRAY') {
 3246:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3247:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3248:                                             $checkedtype = ' checked="checked"';
 3249:                                             last;
 3250:                                         }
 3251:                                     }
 3252:                                 }
 3253:                             }
 3254:                         }
 3255:                         $rem = $i%($numinrow);
 3256:                         if ($rem == 0) {
 3257:                             if ($i > 0) {
 3258:                                 $datatable .= '</tr>';
 3259:                             }
 3260:                             $datatable .= '<tr>';
 3261:                         }
 3262:                         $datatable .= '<td class="LC_left_item">'.
 3263:                                       '<span class="LC_nobreak"><label>'.
 3264:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3265:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3266:                                       '</label></span></td>';
 3267:                     }
 3268:                     $rem = @locations%($numinrow);
 3269:                     my $colsleft = $numinrow - $rem;
 3270:                     if ($colsleft > 1 ) {
 3271:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3272:                                       '&nbsp;</td>';
 3273:                     } elsif ($colsleft == 1) {
 3274:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3275:                     }
 3276:                     $datatable .= '</tr></table>';
 3277:                 }
 3278:                 $datatable .= '</td></tr>';
 3279:                 $itemcount ++;
 3280:             }
 3281:         }
 3282:     }
 3283:     $$rowtotal += $itemcount;
 3284:     return $datatable;
 3285: }
 3286: 
 3287: sub build_location_hashes {
 3288:     my ($intdoms,$by_ip,$by_location) = @_;
 3289:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3290:                   (ref($by_location) eq 'HASH')); 
 3291:     my %iphost = &Apache::lonnet::get_iphost();
 3292:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3293:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3294:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3295:         foreach my $id (@{$iphost{$primary_ip}}) {
 3296:             my $intdom = &Apache::lonnet::internet_dom($id);
 3297:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3298:                 push(@{$intdoms},$intdom);
 3299:             }
 3300:         }
 3301:     }
 3302:     foreach my $ip (keys(%iphost)) {
 3303:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3304:             foreach my $id (@{$iphost{$ip}}) {
 3305:                 my $location = &Apache::lonnet::internet_dom($id);
 3306:                 if ($location) {
 3307:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3308:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3309:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3310:                             push(@{$by_ip->{$ip}},$location);
 3311:                         }
 3312:                     } else {
 3313:                         $by_ip->{$ip} = [$location];
 3314:                     }
 3315:                 }
 3316:             }
 3317:         }
 3318:     }
 3319:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3320:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3321:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3322:             my $first = $by_ip->{$ip}->[0];
 3323:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3324:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3325:                     push(@{$by_location->{$first}},$ip);
 3326:                 }
 3327:             } else {
 3328:                 $by_location->{$first} = [$ip];
 3329:             }
 3330:         }
 3331:     }
 3332:     return;
 3333: }
 3334: 
 3335: sub current_offloads_to {
 3336:     my ($dom,$settings,$servers) = @_;
 3337:     my (%spareid,%otherdomconfigs);
 3338:     if (ref($servers) eq 'HASH') {
 3339:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3340:             my $gotspares;
 3341:             if (ref($settings) eq 'HASH') {
 3342:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3343:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3344:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3345:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3346:                         $gotspares = 1;
 3347:                     }
 3348:                 }
 3349:             }
 3350:             unless ($gotspares) {
 3351:                 my $gotspares;
 3352:                 my $serverhomeID =
 3353:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3354:                 my $serverhomedom =
 3355:                     &Apache::lonnet::host_domain($serverhomeID);
 3356:                 if ($serverhomedom ne $dom) {
 3357:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3358:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3359:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3360:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3361:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3362:                                 $gotspares = 1;
 3363:                             }
 3364:                         }
 3365:                     } else {
 3366:                         $otherdomconfigs{$serverhomedom} =
 3367:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3368:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3369:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3370:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3371:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3372:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3373:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3374:                                         $gotspares = 1;
 3375:                                     }
 3376:                                 }
 3377:                             }
 3378:                         }
 3379:                     }
 3380:                 }
 3381:             }
 3382:             unless ($gotspares) {
 3383:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3384:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3385:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3386:                } else {
 3387:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3388:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3389:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3390:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3391:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3392:                     } else {
 3393:                         my %what = (
 3394:                              spareid => 1,
 3395:                         );
 3396:                         my ($result,$returnhash) = 
 3397:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3398:                         if ($result eq 'ok') { 
 3399:                             if (ref($returnhash) eq 'HASH') {
 3400:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3401:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3402:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3403:                                 }
 3404:                             }
 3405:                         }
 3406:                     }
 3407:                 }
 3408:             }
 3409:         }
 3410:     }
 3411:     return %spareid;
 3412: }
 3413: 
 3414: sub spares_row {
 3415:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3416:     my $css_class;
 3417:     my $numinrow = 4;
 3418:     my $itemcount = 1;
 3419:     my $datatable;
 3420:     my %typetitles = &sparestype_titles();
 3421:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3422:         foreach my $server (sort(keys(%{$servers}))) {
 3423:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3424:             my ($othercontrol,$serverdom);
 3425:             if ($serverhome ne $server) {
 3426:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3427:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3428:             } else {
 3429:                 $serverdom = &Apache::lonnet::host_domain($server);
 3430:                 if ($serverdom ne $dom) {
 3431:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3432:                 }
 3433:             }
 3434:             next unless (ref($spareid->{$server}) eq 'HASH');
 3435:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3436:             $datatable .= '<tr'.$css_class.'>
 3437:                            <td rowspan="2">
 3438:                             <span class="LC_nobreak">'.
 3439:                           &mt('[_1] when busy, offloads to:'
 3440:                               ,'<b>'.$server.'</b>').
 3441:                           "\n";
 3442:             my (%current,%canselect);
 3443:             my @choices = 
 3444:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3445:             foreach my $type ('primary','default') {
 3446:                 if (ref($spareid->{$server}) eq 'HASH') {
 3447:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3448:                         my @spares = @{$spareid->{$server}{$type}};
 3449:                         if (@spares > 0) {
 3450:                             if ($othercontrol) {
 3451:                                 $current{$type} = join(', ',@spares);
 3452:                             } else {
 3453:                                 $current{$type} .= '<table>';
 3454:                                 my $numspares = scalar(@spares);
 3455:                                 for (my $i=0;  $i<@spares; $i++) {
 3456:                                     my $rem = $i%($numinrow);
 3457:                                     if ($rem == 0) {
 3458:                                         if ($i > 0) {
 3459:                                             $current{$type} .= '</tr>';
 3460:                                         }
 3461:                                         $current{$type} .= '<tr>';
 3462:                                     }
 3463:                                     $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;'.
 3464:                                                        $spareid->{$server}{$type}[$i].
 3465:                                                        '</label></td>'."\n";
 3466:                                 }
 3467:                                 my $rem = @spares%($numinrow);
 3468:                                 my $colsleft = $numinrow - $rem;
 3469:                                 if ($colsleft > 1 ) {
 3470:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3471:                                                        '" class="LC_left_item">'.
 3472:                                                        '&nbsp;</td>';
 3473:                                 } elsif ($colsleft == 1) {
 3474:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3475:                                 }
 3476:                                 $current{$type} .= '</tr></table>';
 3477:                             }
 3478:                         }
 3479:                     }
 3480:                     if ($current{$type} eq '') {
 3481:                         $current{$type} = &mt('None specified');
 3482:                     }
 3483:                     if ($othercontrol) {
 3484:                         if ($type eq 'primary') {
 3485:                             $canselect{$type} = $othercontrol;
 3486:                         }
 3487:                     } else {
 3488:                         $canselect{$type} = 
 3489:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3490:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3491:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3492:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3493:                         if (@choices > 0) {
 3494:                             foreach my $lonhost (@choices) {
 3495:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3496:                             }
 3497:                         }
 3498:                         $canselect{$type} .= '</select>'."\n";
 3499:                     }
 3500:                 } else {
 3501:                     $current{$type} = &mt('Could not be determined');
 3502:                     if ($type eq 'primary') {
 3503:                         $canselect{$type} =  $othercontrol;
 3504:                     }
 3505:                 }
 3506:                 if ($type eq 'default') {
 3507:                     $datatable .= '<tr'.$css_class.'>';
 3508:                 }
 3509:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3510:                               '<td>'.$current{$type}.'</td>'."\n".
 3511:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3512:             }
 3513:             $itemcount ++;
 3514:         }
 3515:     }
 3516:     $$rowtotal += $itemcount;
 3517:     return $datatable;
 3518: }
 3519: 
 3520: sub possible_newspares {
 3521:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3522:     my $serverhostname = &Apache::lonnet::hostname($server);
 3523:     my %excluded;
 3524:     if ($serverhostname ne '') {
 3525:         %excluded = (
 3526:                        $serverhostname => 1,
 3527:                     );
 3528:     }
 3529:     if (ref($currspares) eq 'HASH') {
 3530:         foreach my $type (keys(%{$currspares})) {
 3531:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3532:                 if (@{$currspares->{$type}} > 0) {
 3533:                     foreach my $curr (@{$currspares->{$type}}) {
 3534:                         my $hostname = &Apache::lonnet::hostname($curr);
 3535:                         $excluded{$hostname} = 1;
 3536:                     }
 3537:                 }
 3538:             }
 3539:         }
 3540:     }
 3541:     my @choices;
 3542:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3543:         if (keys(%{$serverhomes}) > 1) {
 3544:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3545:                 unless ($excluded{$name}) {
 3546:                     if (exists($altids->{$serverhomes->{$name}})) {
 3547:                         push(@choices,$altids->{$serverhomes->{$name}});
 3548:                     } else {
 3549:                         push(@choices,$serverhomes->{$name});
 3550:                     }
 3551:                 }
 3552:             }
 3553:         }
 3554:     }
 3555:     return sort(@choices);
 3556: }
 3557: 
 3558: sub print_loadbalancing {
 3559:     my ($dom,$settings,$rowtotal) = @_;
 3560:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3561:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3562:     my $numinrow = 1;
 3563:     my $datatable;
 3564:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3565:     my (%currbalancer,%currtargets,%currrules,%existing);
 3566:     if (ref($settings) eq 'HASH') {
 3567:         %existing = %{$settings};
 3568:     }
 3569:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3570:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3571:                                   \%currtargets,\%currrules);
 3572:     } else {
 3573:         return;
 3574:     }
 3575:     my ($othertitle,$usertypes,$types) =
 3576:         &Apache::loncommon::sorted_inst_types($dom);
 3577:     my $rownum = 8;
 3578:     if (ref($types) eq 'ARRAY') {
 3579:         $rownum += scalar(@{$types});
 3580:     }
 3581:     my @css_class = ('LC_odd_row','LC_even_row');
 3582:     my $balnum = 0;
 3583:     my $islast;
 3584:     my (@toshow,$disabledtext);
 3585:     if (keys(%currbalancer) > 0) {
 3586:         @toshow = sort(keys(%currbalancer));
 3587:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3588:             push(@toshow,'');
 3589:         }
 3590:     } else {
 3591:         @toshow = ('');
 3592:         $disabledtext = &mt('No existing load balancer');
 3593:     }
 3594:     foreach my $lonhost (@toshow) {
 3595:         if ($balnum == scalar(@toshow)-1) {
 3596:             $islast = 1;
 3597:         } else {
 3598:             $islast = 0;
 3599:         }
 3600:         my $cssidx = $balnum%2;
 3601:         my $targets_div_style = 'display: none';
 3602:         my $disabled_div_style = 'display: block';
 3603:         my $homedom_div_style = 'display: none';
 3604:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3605:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3606:                       '<p>';
 3607:         if ($lonhost eq '') {
 3608:             $datatable .= '<span class="LC_nobreak">';
 3609:             if (keys(%currbalancer) > 0) {
 3610:                 $datatable .= &mt('Add balancer:');
 3611:             } else {
 3612:                 $datatable .= &mt('Enable balancer:');
 3613:             }
 3614:             $datatable .= '&nbsp;'.
 3615:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3616:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3617:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3618:                           '<option value="" selected="selected">'.&mt('None').
 3619:                           '</option>'."\n";
 3620:             foreach my $server (sort(keys(%servers))) {
 3621:                 next if ($currbalancer{$server});
 3622:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3623:             }
 3624:             $datatable .=
 3625:                 '</select>'."\n".
 3626:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3627:         } else {
 3628:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3629:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3630:                            &mt('Stop balancing').'</label>'.
 3631:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3632:             $targets_div_style = 'display: block';
 3633:             $disabled_div_style = 'display: none';
 3634:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3635:                 $homedom_div_style = 'display: block';
 3636:             }
 3637:         }
 3638:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3639:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3640:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3641:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3642:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3643:         my @sparestypes = ('primary','default');
 3644:         my %typetitles = &sparestype_titles();
 3645:         foreach my $sparetype (@sparestypes) {
 3646:             my $targettable;
 3647:             for (my $i=0; $i<$numspares; $i++) {
 3648:                 my $checked;
 3649:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3650:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3651:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3652:                             $checked = ' checked="checked"';
 3653:                         }
 3654:                     }
 3655:                 }
 3656:                 my ($chkboxval,$disabled);
 3657:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3658:                     $chkboxval = $spares[$i];
 3659:                 }
 3660:                 if (exists($currbalancer{$spares[$i]})) {
 3661:                     $disabled = ' disabled="disabled"';
 3662:                 }
 3663:                 $targettable .=
 3664:                     '<td><span class="LC_nobreak"><label>'.
 3665:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3666:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3667:                     '</span></label></span></td>';
 3668:                 my $rem = $i%($numinrow);
 3669:                 if ($rem == 0) {
 3670:                     if (($i > 0) && ($i < $numspares-1)) {
 3671:                         $targettable .= '</tr>';
 3672:                     }
 3673:                     if ($i < $numspares-1) {
 3674:                         $targettable .= '<tr>';
 3675:                     }
 3676:                 }
 3677:             }
 3678:             if ($targettable ne '') {
 3679:                 my $rem = $numspares%($numinrow);
 3680:                 my $colsleft = $numinrow - $rem;
 3681:                 if ($colsleft > 1 ) {
 3682:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3683:                                     '&nbsp;</td>';
 3684:                 } elsif ($colsleft == 1) {
 3685:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3686:                 }
 3687:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3688:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3689:             }
 3690:         }
 3691:         $datatable .= '</div></td></tr>'.
 3692:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3693:                                            $othertitle,$usertypes,$types,\%servers,
 3694:                                            \%currbalancer,$lonhost,
 3695:                                            $targets_div_style,$homedom_div_style,
 3696:                                            $css_class[$cssidx],$balnum,$islast);
 3697:         $$rowtotal += $rownum;
 3698:         $balnum ++;
 3699:     }
 3700:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3701:     return $datatable;
 3702: }
 3703: 
 3704: sub get_loadbalancers_config {
 3705:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3706:     return unless ((ref($servers) eq 'HASH') &&
 3707:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3708:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3709:     if (keys(%{$existing}) > 0) {
 3710:         my $oldlonhost;
 3711:         foreach my $key (sort(keys(%{$existing}))) {
 3712:             if ($key eq 'lonhost') {
 3713:                 $oldlonhost = $existing->{'lonhost'};
 3714:                 $currbalancer->{$oldlonhost} = 1;
 3715:             } elsif ($key eq 'targets') {
 3716:                 if ($oldlonhost) {
 3717:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3718:                 }
 3719:             } elsif ($key eq 'rules') {
 3720:                 if ($oldlonhost) {
 3721:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3722:                 }
 3723:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3724:                 $currbalancer->{$key} = 1;
 3725:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3726:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3727:             }
 3728:         }
 3729:     } else {
 3730:         my ($balancerref,$targetsref) =
 3731:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3732:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3733:             foreach my $server (sort(keys(%{$balancerref}))) {
 3734:                 $currbalancer->{$server} = 1;
 3735:                 $currtargets->{$server} = $targetsref->{$server};
 3736:             }
 3737:         }
 3738:     }
 3739:     return;
 3740: }
 3741: 
 3742: sub loadbalancing_rules {
 3743:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3744:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3745:         $css_class,$balnum,$islast) = @_;
 3746:     my $output;
 3747:     my $num = 0;
 3748:     my ($alltypes,$othertypes,$titles) =
 3749:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3750:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3751:         foreach my $type (@{$alltypes}) {
 3752:             $num ++;
 3753:             my $current;
 3754:             if (ref($currrules) eq 'HASH') {
 3755:                 $current = $currrules->{$type};
 3756:             }
 3757:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3758:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3759:                     $current = '';
 3760:                 }
 3761:             }
 3762:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3763:                                              $servers,$currbalancer,$lonhost,$dom,
 3764:                                              $targets_div_style,$homedom_div_style,
 3765:                                              $css_class,$balnum,$num,$islast);
 3766:         }
 3767:     }
 3768:     return $output;
 3769: }
 3770: 
 3771: sub loadbalancing_titles {
 3772:     my ($dom,$intdom,$usertypes,$types) = @_;
 3773:     my %othertypes = (
 3774:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3775:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3776:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3777:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3778:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3779:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3780:                      );
 3781:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3782:     if (ref($types) eq 'ARRAY') {
 3783:         unshift(@alltypes,@{$types},'default');
 3784:     }
 3785:     my %titles;
 3786:     foreach my $type (@alltypes) {
 3787:         if ($type =~ /^_LC_/) {
 3788:             $titles{$type} = $othertypes{$type};
 3789:         } elsif ($type eq 'default') {
 3790:             $titles{$type} = &mt('All users from [_1]',$dom);
 3791:             if (ref($types) eq 'ARRAY') {
 3792:                 if (@{$types} > 0) {
 3793:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3794:                 }
 3795:             }
 3796:         } elsif (ref($usertypes) eq 'HASH') {
 3797:             $titles{$type} = $usertypes->{$type};
 3798:         }
 3799:     }
 3800:     return (\@alltypes,\%othertypes,\%titles);
 3801: }
 3802: 
 3803: sub loadbalance_rule_row {
 3804:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3805:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3806:     my @rulenames;
 3807:     my %ruletitles = &offloadtype_text();
 3808:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3809:         @rulenames = ('balancer','offloadedto');
 3810:     } else {
 3811:         @rulenames = ('default','homeserver');
 3812:         if ($type eq '_LC_external') {
 3813:             push(@rulenames,'externalbalancer');
 3814:         } else {
 3815:             push(@rulenames,'specific');
 3816:         }
 3817:         push(@rulenames,'none');
 3818:     }
 3819:     my $style = $targets_div_style;
 3820:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3821:         $style = $homedom_div_style;
 3822:     }
 3823:     my $space;
 3824:     if ($islast && $num == 1) {
 3825:         $space = '<div display="inline-block">&nbsp;</div>';
 3826:     }
 3827:     my $output =
 3828:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3829:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3830:         '<td valaign="top">'.$space.
 3831:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3832:     for (my $i=0; $i<@rulenames; $i++) {
 3833:         my $rule = $rulenames[$i];
 3834:         my ($checked,$extra);
 3835:         if ($rulenames[$i] eq 'default') {
 3836:             $rule = '';
 3837:         }
 3838:         if ($rulenames[$i] eq 'specific') {
 3839:             if (ref($servers) eq 'HASH') {
 3840:                 my $default;
 3841:                 if (($current ne '') && (exists($servers->{$current}))) {
 3842:                     $checked = ' checked="checked"';
 3843:                 }
 3844:                 unless ($checked) {
 3845:                     $default = ' selected="selected"';
 3846:                 }
 3847:                 $extra =
 3848:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3849:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3850:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3851:                     '<option value=""'.$default.'></option>'."\n";
 3852:                 foreach my $server (sort(keys(%{$servers}))) {
 3853:                     if (ref($currbalancer) eq 'HASH') {
 3854:                         next if (exists($currbalancer->{$server}));
 3855:                     }
 3856:                     my $selected;
 3857:                     if ($server eq $current) {
 3858:                         $selected = ' selected="selected"';
 3859:                     }
 3860:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3861:                 }
 3862:                 $extra .= '</select>';
 3863:             }
 3864:         } elsif ($rule eq $current) {
 3865:             $checked = ' checked="checked"';
 3866:         }
 3867:         $output .= '<span class="LC_nobreak"><label>'.
 3868:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3869:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3870:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3871:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3872:                    '</label>'.$extra.'</span><br />'."\n";
 3873:     }
 3874:     $output .= '</div></td></tr>'."\n";
 3875:     return $output;
 3876: }
 3877: 
 3878: sub offloadtype_text {
 3879:     my %ruletitles = &Apache::lonlocal::texthash (
 3880:            'default'          => 'Offloads to default destinations',
 3881:            'homeserver'       => "Offloads to user's home server",
 3882:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3883:            'specific'         => 'Offloads to specific server',
 3884:            'none'             => 'No offload',
 3885:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3886:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3887:     );
 3888:     return %ruletitles;
 3889: }
 3890: 
 3891: sub sparestype_titles {
 3892:     my %typestitles = &Apache::lonlocal::texthash (
 3893:                           'primary' => 'primary',
 3894:                           'default' => 'default',
 3895:                       );
 3896:     return %typestitles;
 3897: }
 3898: 
 3899: sub contact_titles {
 3900:     my %titles = &Apache::lonlocal::texthash (
 3901:                    'supportemail' => 'Support E-mail address',
 3902:                    'adminemail'   => 'Default Server Admin E-mail address',
 3903:                    'errormail'    => 'Error reports to be e-mailed to',
 3904:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3905:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3906:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3907:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3908:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3909:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3910:                  );
 3911:     my %short_titles = &Apache::lonlocal::texthash (
 3912:                            adminemail   => 'Admin E-mail address',
 3913:                            supportemail => 'Support E-mail',
 3914:                        );   
 3915:     return (\%titles,\%short_titles);
 3916: }
 3917: 
 3918: sub tool_titles {
 3919:     my %titles = &Apache::lonlocal::texthash (
 3920:                      aboutme    => 'Personal web page',
 3921:                      blog       => 'Blog',
 3922:                      webdav     => 'WebDAV',
 3923:                      portfolio  => 'Portfolio',
 3924:                      official   => 'Official courses (with institutional codes)',
 3925:                      unofficial => 'Unofficial courses',
 3926:                      community  => 'Communities',
 3927:                      textbook   => 'Textbook courses',
 3928:                  );
 3929:     return %titles;
 3930: }
 3931: 
 3932: sub courserequest_titles {
 3933:     my %titles = &Apache::lonlocal::texthash (
 3934:                                    official   => 'Official',
 3935:                                    unofficial => 'Unofficial',
 3936:                                    community  => 'Communities',
 3937:                                    textbook   => 'Textbook',
 3938:                                    norequest  => 'Not allowed',
 3939:                                    approval   => 'Approval by Dom. Coord.',
 3940:                                    validate   => 'With validation',
 3941:                                    autolimit  => 'Numerical limit',
 3942:                                    unlimited  => '(blank for unlimited)',
 3943:                  );
 3944:     return %titles;
 3945: }
 3946: 
 3947: sub authorrequest_titles {
 3948:     my %titles = &Apache::lonlocal::texthash (
 3949:                                    norequest  => 'Not allowed',
 3950:                                    approval   => 'Approval by Dom. Coord.',
 3951:                                    automatic  => 'Automatic approval',
 3952:                  );
 3953:     return %titles;
 3954: }
 3955: 
 3956: sub courserequest_conditions {
 3957:     my %conditions = &Apache::lonlocal::texthash (
 3958:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3959:        validate   => '(Processing of request subject to institutional validation).',
 3960:                  );
 3961:     return %conditions;
 3962: }
 3963: 
 3964: 
 3965: sub print_usercreation {
 3966:     my ($position,$dom,$settings,$rowtotal) = @_;
 3967:     my $numinrow = 4;
 3968:     my $datatable;
 3969:     if ($position eq 'top') {
 3970:         $$rowtotal ++;
 3971:         my $rowcount = 0;
 3972:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3973:         if (ref($rules) eq 'HASH') {
 3974:             if (keys(%{$rules}) > 0) {
 3975:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3976:                                                 $ruleorder,$numinrow,$rowcount);
 3977:                 $$rowtotal ++;
 3978:                 $rowcount ++;
 3979:             }
 3980:         }
 3981:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3982:         if (ref($idrules) eq 'HASH') {
 3983:             if (keys(%{$idrules}) > 0) {
 3984:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3985:                                                 $idruleorder,$numinrow,$rowcount);
 3986:                 $$rowtotal ++;
 3987:                 $rowcount ++;
 3988:             }
 3989:         }
 3990:         if ($rowcount == 0) {
 3991:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3992:             $$rowtotal ++;
 3993:             $rowcount ++;
 3994:         }
 3995:     } elsif ($position eq 'middle') {
 3996:         my @creators = ('author','course','requestcrs');
 3997:         my ($rules,$ruleorder) =
 3998:             &Apache::lonnet::inst_userrules($dom,'username');
 3999:         my %lt = &usercreation_types();
 4000:         my %checked;
 4001:         if (ref($settings) eq 'HASH') {
 4002:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 4003:                 foreach my $item (@creators) {
 4004:                     $checked{$item} = $settings->{'cancreate'}{$item};
 4005:                 }
 4006:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 4007:                 foreach my $item (@creators) {
 4008:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 4009:                         $checked{$item} = 'none';
 4010:                     }
 4011:                 }
 4012:             }
 4013:         }
 4014:         my $rownum = 0;
 4015:         foreach my $item (@creators) {
 4016:             $rownum ++;
 4017:             if ($checked{$item} eq '') {
 4018:                 $checked{$item} = 'any';
 4019:             }
 4020:             my $css_class;
 4021:             if ($rownum%2) {
 4022:                 $css_class = '';
 4023:             } else {
 4024:                 $css_class = ' class="LC_odd_row" ';
 4025:             }
 4026:             $datatable .= '<tr'.$css_class.'>'.
 4027:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 4028:                          '</span></td><td align="right">';
 4029:             my @options = ('any');
 4030:             if (ref($rules) eq 'HASH') {
 4031:                 if (keys(%{$rules}) > 0) {
 4032:                     push(@options,('official','unofficial'));
 4033:                 }
 4034:             }
 4035:             push(@options,'none');
 4036:             foreach my $option (@options) {
 4037:                 my $type = 'radio';
 4038:                 my $check = ' ';
 4039:                 if ($checked{$item} eq $option) {
 4040:                     $check = ' checked="checked" ';
 4041:                 } 
 4042:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4043:                               '<input type="'.$type.'" name="can_createuser_'.
 4044:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 4045:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 4046:             }
 4047:             $datatable .= '</td></tr>';
 4048:         }
 4049:     } else {
 4050:         my @contexts = ('author','course','domain');
 4051:         my @authtypes = ('int','krb4','krb5','loc');
 4052:         my %checked;
 4053:         if (ref($settings) eq 'HASH') {
 4054:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 4055:                 foreach my $item (@contexts) {
 4056:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 4057:                         foreach my $auth (@authtypes) {
 4058:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 4059:                                 $checked{$item}{$auth} = ' checked="checked" ';
 4060:                             }
 4061:                         }
 4062:                     }
 4063:                 }
 4064:             }
 4065:         } else {
 4066:             foreach my $item (@contexts) {
 4067:                 foreach my $auth (@authtypes) {
 4068:                     $checked{$item}{$auth} = ' checked="checked" ';
 4069:                 }
 4070:             }
 4071:         }
 4072:         my %title = &context_names();
 4073:         my %authname = &authtype_names();
 4074:         my $rownum = 0;
 4075:         my $css_class; 
 4076:         foreach my $item (@contexts) {
 4077:             if ($rownum%2) {
 4078:                 $css_class = '';
 4079:             } else {
 4080:                 $css_class = ' class="LC_odd_row" ';
 4081:             }
 4082:             $datatable .=   '<tr'.$css_class.'>'.
 4083:                             '<td>'.$title{$item}.
 4084:                             '</td><td class="LC_left_item">'.
 4085:                             '<span class="LC_nobreak">';
 4086:             foreach my $auth (@authtypes) {
 4087:                 $datatable .= '<label>'. 
 4088:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4089:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4090:                               $authname{$auth}.'</label>&nbsp;';
 4091:             }
 4092:             $datatable .= '</span></td></tr>';
 4093:             $rownum ++;
 4094:         }
 4095:         $$rowtotal += $rownum;
 4096:     }
 4097:     return $datatable;
 4098: }
 4099: 
 4100: sub print_selfcreation {
 4101:     my ($position,$dom,$settings,$rowtotal) = @_;
 4102:     my (@selfcreate,$createsettings,$processing,$datatable);
 4103:     if (ref($settings) eq 'HASH') {
 4104:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4105:             $createsettings = $settings->{'cancreate'};
 4106:             if (ref($createsettings) eq 'HASH') {
 4107:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4108:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4109:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4110:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4111:                         @selfcreate = ('email','login','sso');
 4112:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4113:                         @selfcreate = ($createsettings->{'selfcreate'});
 4114:                     }
 4115:                 }
 4116:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4117:                     $processing = $createsettings->{'selfcreateprocessing'};
 4118:                 }
 4119:             }
 4120:         }
 4121:     }
 4122:     my %radiohash;
 4123:     my $numinrow = 4;
 4124:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4125:     if ($position eq 'top') {
 4126:         my %choices = &Apache::lonlocal::texthash (
 4127:                                                       cancreate_login      => 'Institutional Login',
 4128:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4129:                                                   );
 4130:         my @toggles = sort(keys(%choices));
 4131:         my %defaultchecked = (
 4132:                                'cancreate_login' => 'off',
 4133:                                'cancreate_sso'   => 'off',
 4134:                              );
 4135:         my ($onclick,$itemcount);
 4136:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4137:                                                      \%choices,$itemcount,$onclick);
 4138:         $$rowtotal += $itemcount;
 4139:         
 4140:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4141: 
 4142:         if (ref($usertypes) eq 'HASH') {
 4143:             if (keys(%{$usertypes}) > 0) {
 4144:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4145:                                              $dom,$numinrow,$othertitle,
 4146:                                              'statustocreate',$$rowtotal);
 4147:                 $$rowtotal ++;
 4148:             }
 4149:         }
 4150:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 4151:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4152:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 4153:         my $rem;
 4154:         my $numperrow = 2;
 4155:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4156:         $datatable .= '<tr'.$css_class.'>'.
 4157:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 4158:                      '<td class="LC_left_item">'."\n".
 4159:                      '<table><tr><td>'."\n";
 4160:         for (my $i=0; $i<@fields; $i++) {
 4161:             $rem = $i%($numperrow);
 4162:             if ($rem == 0) {
 4163:                 if ($i > 0) {
 4164:                     $datatable .= '</tr>';
 4165:                 }
 4166:                 $datatable .= '<tr>';
 4167:             }
 4168:             my $currval;
 4169:             if (ref($createsettings) eq 'HASH') {
 4170:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 4171:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 4172:                 }
 4173:             }
 4174:             $datatable .= '<td class="LC_left_item">'.
 4175:                           '<span class="LC_nobreak">'.
 4176:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 4177:                           'value="'.$currval.'" size="10" />&nbsp;'.
 4178:                           $fieldtitles{$fields[$i]}.'</span></td>';
 4179:         }
 4180:         my $colsleft = $numperrow - $rem;
 4181:         if ($colsleft > 1 ) {
 4182:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4183:                          '&nbsp;</td>';
 4184:         } elsif ($colsleft == 1) {
 4185:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4186:         }
 4187:         $datatable .= '</tr></table></td></tr>';
 4188:         $$rowtotal ++;
 4189:     } elsif ($position eq 'middle') {
 4190:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4191:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4192:         $usertypes->{'default'} = $othertitle;
 4193:         if (ref($types) eq 'ARRAY') {
 4194:             push(@{$types},'default');
 4195:             $usertypes->{'default'} = $othertitle;
 4196:             foreach my $status (@{$types}) {
 4197:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4198:                                                        $numinrow,$$rowtotal,$usertypes);
 4199:                 $$rowtotal ++;
 4200:             }
 4201:         }
 4202:     } else {
 4203:         my %choices = &Apache::lonlocal::texthash (
 4204:                                                       cancreate_email => 'E-mail address as username',
 4205:                                                   );
 4206:         my @toggles = sort(keys(%choices));
 4207:         my %defaultchecked = (
 4208:                                'cancreate_email' => 'off',
 4209:                              );
 4210:         my $itemcount = 0;
 4211:         my $display = 'none';
 4212:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 4213:             $display = 'block';
 4214:         }
 4215:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 4216:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 4217:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4218:         my $usertypes = {};
 4219:         my $order = [];
 4220:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 4221:             $usertypes = $domdefaults{'inststatustypes'};
 4222:             $order = $domdefaults{'inststatusguest'};
 4223:         }
 4224:         if (ref($order) eq 'ARRAY') {
 4225:             push(@{$order},'default');
 4226:             if (@{$order} > 1) {
 4227:                 $usertypes->{'default'} = &mt('Other users');
 4228:                 $additional .= '<table><tr>';
 4229:                 foreach my $status (@{$order}) {
 4230:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 4231:                 }
 4232:                 $additional .= '</tr><tr>';
 4233:                 foreach my $status (@{$order}) {
 4234:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 4235:                 }
 4236:                 $additional .= '</tr></table>';
 4237:             } else {
 4238:                 $usertypes->{'default'} = &mt('All users');
 4239:                 $additional .= &email_as_username($rowtotal,$processing);
 4240:             }
 4241:         }
 4242:         $additional .= '</div>'."\n";
 4243: 
 4244:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4245:                                                      \%choices,$$rowtotal,$onclick,$additional);
 4246:         $$rowtotal ++;
 4247:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4248:         $$rowtotal ++;
 4249:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4250:         $numinrow = 1;
 4251:         if (ref($order) eq 'ARRAY') {
 4252:             foreach my $status (@{$order}) {
 4253:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4254:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4255:                 $$rowtotal ++;
 4256:             }
 4257:         }
 4258:         my ($emailrules,$emailruleorder) =
 4259:             &Apache::lonnet::inst_userrules($dom,'email');
 4260:         if (ref($emailrules) eq 'HASH') {
 4261:             if (keys(%{$emailrules}) > 0) {
 4262:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4263:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4264:                 $$rowtotal ++;
 4265:             }
 4266:         }
 4267:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4268:     }
 4269:     return $datatable;
 4270: }
 4271: 
 4272: sub email_as_username {
 4273:     my ($rowtotal,$processing,$type) = @_;
 4274:     my %choices =
 4275:         &Apache::lonlocal::texthash (
 4276:                                       automatic => 'Automatic approval',
 4277:                                       approval  => 'Queued for approval',
 4278:                                     );
 4279:     my $output;
 4280:     foreach my $option ('automatic','approval') {
 4281:         my $checked;
 4282:         if (ref($processing) eq 'HASH') {
 4283:             if ($type eq '') {   
 4284:                 if (!exists($processing->{'default'})) {
 4285:                     if ($option eq 'automatic') {
 4286:                         $checked = ' checked="checked"';
 4287:                     }
 4288:                 } else {
 4289:                     if ($processing->{'default'} eq $option) {
 4290:                         $checked = ' checked="checked"';
 4291:                     }
 4292:                 }
 4293:             } else {
 4294:                 if (!exists($processing->{$type})) {
 4295:                     if ($option eq 'automatic') {
 4296:                         $checked = ' checked="checked"';
 4297:                     }
 4298:                 } else {
 4299:                     if ($processing->{$type} eq $option) {
 4300:                         $checked = ' checked="checked"';
 4301:                     }
 4302:                 }
 4303:             }
 4304:         } elsif ($option eq 'automatic') {
 4305:             $checked = ' checked="checked"'; 
 4306:         }
 4307:         my $name = 'cancreate_emailprocess';
 4308:         if (($type ne '') && ($type ne 'default')) {
 4309:             $name .= '_'.$type;
 4310:         }
 4311:         $output .= '<span class="LC_nobreak"><label>'.
 4312:                    '<input type="radio" name="'.$name.'"'.
 4313:                    $checked.' value="'.$option.'" />'.
 4314:                    $choices{$option}.'</label></span>';
 4315:         if ($type eq '') {
 4316:             $output .= '&nbsp;';
 4317:         } else {
 4318:             $output .= '<br />';
 4319:         }
 4320:     }
 4321:     $$rowtotal ++;
 4322:     return $output;
 4323: }
 4324: 
 4325: sub captcha_choice {
 4326:     my ($context,$settings,$itemcount) = @_;
 4327:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4328:     my %lt = &captcha_phrases();
 4329:     $keyentry = 'hidden';
 4330:     if ($context eq 'cancreate') {
 4331:         $rowname = &mt('CAPTCHA validation');
 4332:     } elsif ($context eq 'login') {
 4333:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4334:     }
 4335:     if (ref($settings) eq 'HASH') {
 4336:         if ($settings->{'captcha'}) {
 4337:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4338:         } else {
 4339:             $checked{'original'} = ' checked="checked"';
 4340:         }
 4341:         if ($settings->{'captcha'} eq 'recaptcha') {
 4342:             $pubtext = $lt{'pub'};
 4343:             $privtext = $lt{'priv'};
 4344:             $keyentry = 'text';
 4345:         }
 4346:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4347:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4348:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4349:         }
 4350:     } else {
 4351:         $checked{'original'} = ' checked="checked"';
 4352:     }
 4353:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4354:     my $output = '<tr'.$css_class.'>'.
 4355:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4356:                  '<table><tr><td>'."\n";
 4357:     foreach my $option ('original','recaptcha','notused') {
 4358:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4359:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4360:                    $lt{$option}.'</label></span>';
 4361:         unless ($option eq 'notused') {
 4362:             $output .= ('&nbsp;'x2)."\n";
 4363:         }
 4364:     }
 4365: #
 4366: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4367: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4368: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4369: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4370: #
 4371:     $output .= '</td></tr>'."\n".
 4372:                '<tr><td>'."\n".
 4373:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4374:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4375:                $currpub.'" size="40" /></span><br />'."\n".
 4376:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4377:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4378:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4379:                '</td></tr>';
 4380:     return $output;
 4381: }
 4382: 
 4383: sub user_formats_row {
 4384:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4385:     my $output;
 4386:     my %text = (
 4387:                    'username' => 'new usernames',
 4388:                    'id'       => 'IDs',
 4389:                    'email'    => 'self-created accounts (e-mail)',
 4390:                );
 4391:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4392:     $output = '<tr '.$css_class.'>'.
 4393:               '<td><span class="LC_nobreak">';
 4394:     if ($type eq 'email') {
 4395:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4396:     } else {
 4397:         $output .= &mt("Format rules to check for $text{$type}: ");
 4398:     }
 4399:     $output .= '</span></td>'.
 4400:                '<td class="LC_left_item" colspan="2"><table>';
 4401:     my $rem;
 4402:     if (ref($ruleorder) eq 'ARRAY') {
 4403:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4404:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4405:                 my $rem = $i%($numinrow);
 4406:                 if ($rem == 0) {
 4407:                     if ($i > 0) {
 4408:                         $output .= '</tr>';
 4409:                     }
 4410:                     $output .= '<tr>';
 4411:                 }
 4412:                 my $check = ' ';
 4413:                 if (ref($settings) eq 'HASH') {
 4414:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4415:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4416:                             $check = ' checked="checked" ';
 4417:                         }
 4418:                     }
 4419:                 }
 4420:                 $output .= '<td class="LC_left_item">'.
 4421:                            '<span class="LC_nobreak"><label>'.
 4422:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4423:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4424:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4425:             }
 4426:         }
 4427:         $rem = @{$ruleorder}%($numinrow);
 4428:     }
 4429:     my $colsleft = $numinrow - $rem;
 4430:     if ($colsleft > 1 ) {
 4431:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4432:                    '&nbsp;</td>';
 4433:     } elsif ($colsleft == 1) {
 4434:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4435:     }
 4436:     $output .= '</tr></table></td></tr>';
 4437:     return $output;
 4438: }
 4439: 
 4440: sub usercreation_types {
 4441:     my %lt = &Apache::lonlocal::texthash (
 4442:                     author     => 'When adding a co-author',
 4443:                     course     => 'When adding a user to a course',
 4444:                     requestcrs => 'When requesting a course',
 4445:                     any        => 'Any',
 4446:                     official   => 'Institutional only ',
 4447:                     unofficial => 'Non-institutional only',
 4448:                     none       => 'None',
 4449:     );
 4450:     return %lt;
 4451: }
 4452: 
 4453: sub selfcreation_types {
 4454:     my %lt = &Apache::lonlocal::texthash (
 4455:                     selfcreate => 'User creates own account',
 4456:                     any        => 'Any',
 4457:                     official   => 'Institutional only ',
 4458:                     unofficial => 'Non-institutional only',
 4459:                     email      => 'E-mail address',
 4460:                     login      => 'Institutional Login',
 4461:                     sso        => 'SSO',
 4462:              );
 4463: }
 4464: 
 4465: sub authtype_names {
 4466:     my %lt = &Apache::lonlocal::texthash(
 4467:                       int    => 'Internal',
 4468:                       krb4   => 'Kerberos 4',
 4469:                       krb5   => 'Kerberos 5',
 4470:                       loc    => 'Local',
 4471:                   );
 4472:     return %lt;
 4473: }
 4474: 
 4475: sub context_names {
 4476:     my %context_title = &Apache::lonlocal::texthash(
 4477:        author => 'Creating users when an Author',
 4478:        course => 'Creating users when in a course',
 4479:        domain => 'Creating users when a Domain Coordinator',
 4480:     );
 4481:     return %context_title;
 4482: }
 4483: 
 4484: sub print_usermodification {
 4485:     my ($position,$dom,$settings,$rowtotal) = @_;
 4486:     my $numinrow = 4;
 4487:     my ($context,$datatable,$rowcount);
 4488:     if ($position eq 'top') {
 4489:         $rowcount = 0;
 4490:         $context = 'author'; 
 4491:         foreach my $role ('ca','aa') {
 4492:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4493:                                                    $numinrow,$rowcount);
 4494:             $$rowtotal ++;
 4495:             $rowcount ++;
 4496:         }
 4497:     } elsif ($position eq 'bottom') {
 4498:         $context = 'course';
 4499:         $rowcount = 0;
 4500:         foreach my $role ('st','ep','ta','in','cr') {
 4501:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4502:                                                    $numinrow,$rowcount);
 4503:             $$rowtotal ++;
 4504:             $rowcount ++;
 4505:         }
 4506:     }
 4507:     return $datatable;
 4508: }
 4509: 
 4510: sub print_defaults {
 4511:     my ($position,$dom,$settings,$rowtotal) = @_;
 4512:     my $rownum = 0;
 4513:     my ($datatable,$css_class);
 4514:     if ($position eq 'top') {
 4515:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4516:                      'datelocale_def','portal_def');
 4517:         my %defaults;
 4518:         if (ref($settings) eq 'HASH') {
 4519:             %defaults = %{$settings};
 4520:         } else {
 4521:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4522:             foreach my $item (@items) {
 4523:                 $defaults{$item} = $domdefaults{$item};
 4524:             }
 4525:         }
 4526:         my $titles = &defaults_titles($dom);
 4527:         foreach my $item (@items) {
 4528:             if ($rownum%2) {
 4529:                 $css_class = '';
 4530:             } else {
 4531:                 $css_class = ' class="LC_odd_row" ';
 4532:             }
 4533:             $datatable .= '<tr'.$css_class.'>'.
 4534:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4535:                           '</span></td><td class="LC_right_item" colspan="3">';
 4536:             if ($item eq 'auth_def') {
 4537:                 my @authtypes = ('internal','krb4','krb5','localauth');
 4538:                 my %shortauth = (
 4539:                                  internal => 'int',
 4540:                                  krb4 => 'krb4',
 4541:                                  krb5 => 'krb5',
 4542:                                  localauth  => 'loc'
 4543:                                 );
 4544:                 my %authnames = &authtype_names();
 4545:                 foreach my $auth (@authtypes) {
 4546:                     my $checked = ' ';
 4547:                     if ($defaults{$item} eq $auth) {
 4548:                         $checked = ' checked="checked" ';
 4549:                     }
 4550:                     $datatable .= '<label><input type="radio" name="'.$item.
 4551:                                   '" value="'.$auth.'"'.$checked.'/>'.
 4552:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4553:                 }
 4554:             } elsif ($item eq 'timezone_def') {
 4555:                 my $includeempty = 1;
 4556:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4557:             } elsif ($item eq 'datelocale_def') {
 4558:                 my $includeempty = 1;
 4559:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4560:             } elsif ($item eq 'lang_def') {
 4561:                 my %langchoices = &get_languages_hash();
 4562:                 $langchoices{''} = 'No language preference';
 4563:                 %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4564:                 $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4565:                                                               \%langchoices);
 4566:             } else {
 4567:                 my $size;
 4568:                 if ($item eq 'portal_def') {
 4569:                     $size = ' size="25"';
 4570:                 }
 4571:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4572:                               $defaults{$item}.'"'.$size.' />';
 4573:             }
 4574:             $datatable .= '</td></tr>';
 4575:             $rownum ++;
 4576:         }
 4577:     } else {
 4578:         my (%defaults);
 4579:         if (ref($settings) eq 'HASH') {
 4580:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 4581:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 4582:                 my $maxnum = @{$settings->{'inststatusorder'}};
 4583:                 for (my $i=0; $i<$maxnum; $i++) {
 4584:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 4585:                     my $item = $settings->{'inststatusorder'}->[$i];
 4586:                     my $title = $settings->{'inststatustypes'}->{$item};
 4587:                     my $guestok;
 4588:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 4589:                         $guestok = 1;
 4590:                     }
 4591:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 4592:                     $datatable .= '<tr'.$css_class.'>'.
 4593:                                   '<td><span class="LC_nobreak">'.
 4594:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 4595:                     for (my $k=0; $k<=$maxnum; $k++) {
 4596:                         my $vpos = $k+1;
 4597:                         my $selstr;
 4598:                         if ($k == $i) {
 4599:                             $selstr = ' selected="selected" ';
 4600:                         }
 4601:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4602:                     }
 4603:                     my ($checkedon,$checkedoff);
 4604:                     $checkedoff = ' checked="checked"';
 4605:                     if ($guestok) {
 4606:                         $checkedon = $checkedoff;
 4607:                         $checkedoff = ''; 
 4608:                     }
 4609:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 4610:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 4611:                                   &mt('delete').'</span></td>'.
 4612:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 4613:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 4614:                                   '</span></td>'.
 4615:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4616:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 4617:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 4618:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 4619:                                   &mt('No').'</label></span></td></tr>';
 4620:                 }
 4621:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 4622:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 4623:                 $datatable .= '<tr '.$css_class.'>'.
 4624:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 4625:                 for (my $k=0; $k<=$maxnum; $k++) {
 4626:                     my $vpos = $k+1;
 4627:                     my $selstr;
 4628:                     if ($k == $maxnum) {
 4629:                         $selstr = ' selected="selected" ';
 4630:                     }
 4631:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4632:                 }
 4633:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 4634:                               '<input type="text" size="10" name="addinststatus" value="" /></span>'.
 4635:                               '&nbsp;'.&mt('(new)').
 4636:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 4637:                               &mt('Name displayed:').
 4638:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 4639:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 4640:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 4641:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 4642:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 4643:                               &mt('No').'</label></span></td></tr>';
 4644:                               '</tr>'."\n";
 4645:                 $rownum ++;
 4646:             }
 4647:         }
 4648:     }
 4649:     $$rowtotal += $rownum;
 4650:     return $datatable;
 4651: }
 4652: 
 4653: sub get_languages_hash {
 4654:     my %langchoices;
 4655:     foreach my $id (&Apache::loncommon::languageids()) {
 4656:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4657:         if ($code ne '') {
 4658:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4659:         }
 4660:     }
 4661:     return %langchoices;
 4662: }
 4663: 
 4664: sub defaults_titles {
 4665:     my ($dom) = @_;
 4666:     my %titles = &Apache::lonlocal::texthash (
 4667:                    'auth_def'      => 'Default authentication type',
 4668:                    'auth_arg_def'  => 'Default authentication argument',
 4669:                    'lang_def'      => 'Default language',
 4670:                    'timezone_def'  => 'Default timezone',
 4671:                    'datelocale_def' => 'Default locale for dates',
 4672:                    'portal_def'     => 'Portal/Default URL',
 4673:                  );
 4674:     if ($dom) {
 4675:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4676:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4677:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4678:         $protocol = 'http' if ($protocol ne 'https');
 4679:         if ($uint_dom) {
 4680:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4681:                                          $uint_dom);
 4682:         }
 4683:     }
 4684:     return (\%titles);
 4685: }
 4686: 
 4687: sub print_scantronformat {
 4688:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4689:     my $itemcount = 1;
 4690:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4691:         %confhash);
 4692:     my $switchserver = &check_switchserver($dom,$confname);
 4693:     my %lt = &Apache::lonlocal::texthash (
 4694:                 default => 'Default bubblesheet format file error',
 4695:                 custom  => 'Custom bubblesheet format file error',
 4696:              );
 4697:     my %scantronfiles = (
 4698:         default => 'default.tab',
 4699:         custom => 'custom.tab',
 4700:     );
 4701:     foreach my $key (keys(%scantronfiles)) {
 4702:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4703:                               .$scantronfiles{$key};
 4704:     }
 4705:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4706:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4707:         if (!$switchserver) {
 4708:             my $servadm = $r->dir_config('lonAdmEMail');
 4709:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4710:             if ($configuserok eq 'ok') {
 4711:                 if ($author_ok eq 'ok') {
 4712:                     my %legacyfile = (
 4713:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4714:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4715:                     );
 4716:                     my %md5chk;
 4717:                     foreach my $type (keys(%legacyfile)) {
 4718:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4719:                         chomp($md5chk{$type});
 4720:                     }
 4721:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4722:                         foreach my $type (keys(%legacyfile)) {
 4723:                             ($scantronurls{$type},my $error) = 
 4724:                                 &legacy_scantronformat($r,$dom,$confname,
 4725:                                                  $type,$legacyfile{$type},
 4726:                                                  $scantronurls{$type},
 4727:                                                  $scantronfiles{$type});
 4728:                             if ($error ne '') {
 4729:                                 $error{$type} = $error;
 4730:                             }
 4731:                         }
 4732:                         if (keys(%error) == 0) {
 4733:                             $is_custom = 1;
 4734:                             $confhash{'scantron'}{'scantronformat'} = 
 4735:                                 $scantronurls{'custom'};
 4736:                             my $putresult = 
 4737:                                 &Apache::lonnet::put_dom('configuration',
 4738:                                                          \%confhash,$dom);
 4739:                             if ($putresult ne 'ok') {
 4740:                                 $error{'custom'} = 
 4741:                                     '<span class="LC_error">'.
 4742:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4743:                             }
 4744:                         }
 4745:                     } else {
 4746:                         ($scantronurls{'default'},my $error) =
 4747:                             &legacy_scantronformat($r,$dom,$confname,
 4748:                                           'default',$legacyfile{'default'},
 4749:                                           $scantronurls{'default'},
 4750:                                           $scantronfiles{'default'});
 4751:                         if ($error eq '') {
 4752:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4753:                             my $putresult =
 4754:                                 &Apache::lonnet::put_dom('configuration',
 4755:                                                          \%confhash,$dom);
 4756:                             if ($putresult ne 'ok') {
 4757:                                 $error{'default'} =
 4758:                                     '<span class="LC_error">'.
 4759:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4760:                             }
 4761:                         } else {
 4762:                             $error{'default'} = $error;
 4763:                         }
 4764:                     }
 4765:                 }
 4766:             }
 4767:         } else {
 4768:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4769:         }
 4770:     }
 4771:     if (ref($settings) eq 'HASH') {
 4772:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4773:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4774:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4775:                 $scantronurl = '';
 4776:             } else {
 4777:                 $scantronurl = $settings->{'scantronformat'};
 4778:             }
 4779:             $is_custom = 1;
 4780:         } else {
 4781:             $scantronurl = $scantronurls{'default'};
 4782:         }
 4783:     } else {
 4784:         if ($is_custom) {
 4785:             $scantronurl = $scantronurls{'custom'};
 4786:         } else {
 4787:             $scantronurl = $scantronurls{'default'};
 4788:         }
 4789:     }
 4790:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4791:     $datatable .= '<tr'.$css_class.'>';
 4792:     if (!$is_custom) {
 4793:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4794:                       '<span class="LC_nobreak">';
 4795:         if ($scantronurl) {
 4796:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4797:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4798:         } else {
 4799:             $datatable = &mt('File unavailable for display');
 4800:         }
 4801:         $datatable .= '</span></td>';
 4802:         if (keys(%error) == 0) { 
 4803:             $datatable .= '<td valign="bottom">';
 4804:             if (!$switchserver) {
 4805:                 $datatable .= &mt('Upload:').'<br />';
 4806:             }
 4807:         } else {
 4808:             my $errorstr;
 4809:             foreach my $key (sort(keys(%error))) {
 4810:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4811:             }
 4812:             $datatable .= '<td>'.$errorstr;
 4813:         }
 4814:     } else {
 4815:         if (keys(%error) > 0) {
 4816:             my $errorstr;
 4817:             foreach my $key (sort(keys(%error))) {
 4818:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4819:             } 
 4820:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4821:         } elsif ($scantronurl) {
 4822:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4823:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4824:             $datatable .= '<td><span class="LC_nobreak">'.
 4825:                           $link.
 4826:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4827:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4828:                           '<td><span class="LC_nobreak">&nbsp;'.
 4829:                           &mt('Replace:').'</span><br />';
 4830:         }
 4831:     }
 4832:     if (keys(%error) == 0) {
 4833:         if ($switchserver) {
 4834:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4835:         } else {
 4836:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4837:                          '<input type="file" name="scantronformat" /></span>';
 4838:         }
 4839:     }
 4840:     $datatable .= '</td></tr>';
 4841:     $$rowtotal ++;
 4842:     return $datatable;
 4843: }
 4844: 
 4845: sub legacy_scantronformat {
 4846:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4847:     my ($url,$error);
 4848:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4849:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4850:         (my $result,$url) =
 4851:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4852:                          '','',$newfile);
 4853:         if ($result ne 'ok') {
 4854:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4855:         }
 4856:     }
 4857:     return ($url,$error);
 4858: }
 4859: 
 4860: sub print_coursecategories {
 4861:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4862:     my $datatable;
 4863:     if ($position eq 'top') {
 4864:         my (%checked);
 4865:         my @catitems = ('unauth','auth');
 4866:         my @cattypes = ('std','domonly','codesrch','none');
 4867:         $checked{'unauth'} = 'std';
 4868:         $checked{'auth'} = 'std';
 4869:         if (ref($settings) eq 'HASH') {
 4870:             foreach my $type (@cattypes) {
 4871:                 if ($type eq $settings->{'unauth'}) {
 4872:                     $checked{'unauth'} = $type;
 4873:                 }
 4874:                 if ($type eq $settings->{'auth'}) {
 4875:                     $checked{'auth'} = $type;
 4876:                 }
 4877:             }
 4878:         }
 4879:         my %lt = &Apache::lonlocal::texthash (
 4880:                                                unauth   => 'Catalog type for unauthenticated users',
 4881:                                                auth     => 'Catalog type for authenticated users',
 4882:                                                none     => 'No catalog',
 4883:                                                std      => 'Standard catalog',
 4884:                                                domonly  => 'Domain-only catalog',
 4885:                                                codesrch => "Code search form",
 4886:                                              );
 4887:        my $itemcount = 0;
 4888:        foreach my $item (@catitems) {
 4889:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 4890:            $datatable .= '<tr '.$css_class.'>'.
 4891:                          '<td>'.$lt{$item}.'</td>'.
 4892:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 4893:            foreach my $type (@cattypes) {
 4894:                my $ischecked;
 4895:                if ($checked{$item} eq $type) {
 4896:                    $ischecked=' checked="checked"';
 4897:                }
 4898:                $datatable .= '<label>'.
 4899:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 4900:                              ' />'.$lt{$type}.'</label>&nbsp;';
 4901:            }
 4902:            $datatable .= '</td></tr>';
 4903:            $itemcount ++;
 4904:         }
 4905:         $$rowtotal += $itemcount;
 4906:     } elsif ($position eq 'middle') {
 4907:         my $toggle_cats_crs = ' ';
 4908:         my $toggle_cats_dom = ' checked="checked" ';
 4909:         my $can_cat_crs = ' ';
 4910:         my $can_cat_dom = ' checked="checked" ';
 4911:         my $toggle_catscomm_comm = ' ';
 4912:         my $toggle_catscomm_dom = ' checked="checked" ';
 4913:         my $can_catcomm_comm = ' ';
 4914:         my $can_catcomm_dom = ' checked="checked" ';
 4915: 
 4916:         if (ref($settings) eq 'HASH') {
 4917:             if ($settings->{'togglecats'} eq 'crs') {
 4918:                 $toggle_cats_crs = $toggle_cats_dom;
 4919:                 $toggle_cats_dom = ' ';
 4920:             }
 4921:             if ($settings->{'categorize'} eq 'crs') {
 4922:                 $can_cat_crs = $can_cat_dom;
 4923:                 $can_cat_dom = ' ';
 4924:             }
 4925:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4926:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4927:                 $toggle_catscomm_dom = ' ';
 4928:             }
 4929:             if ($settings->{'categorizecomm'} eq 'comm') {
 4930:                 $can_catcomm_comm = $can_catcomm_dom;
 4931:                 $can_catcomm_dom = ' ';
 4932:             }
 4933:         }
 4934:         my %title = &Apache::lonlocal::texthash (
 4935:                      togglecats     => 'Show/Hide a course in catalog',
 4936:                      togglecatscomm => 'Show/Hide a community in catalog',
 4937:                      categorize     => 'Assign a category to a course',
 4938:                      categorizecomm => 'Assign a category to a community',
 4939:                     );
 4940:         my %level = &Apache::lonlocal::texthash (
 4941:                      dom  => 'Set in Domain',
 4942:                      crs  => 'Set in Course',
 4943:                      comm => 'Set in Community',
 4944:                     );
 4945:         $datatable = '<tr class="LC_odd_row">'.
 4946:                   '<td>'.$title{'togglecats'}.'</td>'.
 4947:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4948:                   '<input type="radio" name="togglecats"'.
 4949:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4950:                   '<label><input type="radio" name="togglecats"'.
 4951:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4952:                   '</tr><tr>'.
 4953:                   '<td>'.$title{'categorize'}.'</td>'.
 4954:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4955:                   '<label><input type="radio" name="categorize"'.
 4956:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4957:                   '<label><input type="radio" name="categorize"'.
 4958:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4959:                   '</tr><tr class="LC_odd_row">'.
 4960:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4961:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4962:                   '<input type="radio" name="togglecatscomm"'.
 4963:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4964:                   '<label><input type="radio" name="togglecatscomm"'.
 4965:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4966:                   '</tr><tr>'.
 4967:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4968:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4969:                   '<label><input type="radio" name="categorizecomm"'.
 4970:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4971:                   '<label><input type="radio" name="categorizecomm"'.
 4972:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4973:                   '</tr>';
 4974:         $$rowtotal += 4;
 4975:     } else {
 4976:         my $css_class;
 4977:         my $itemcount = 1;
 4978:         my $cathash; 
 4979:         if (ref($settings) eq 'HASH') {
 4980:             $cathash = $settings->{'cats'};
 4981:         }
 4982:         if (ref($cathash) eq 'HASH') {
 4983:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4984:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4985:                                                    \%allitems,\%idx,\@jsarray);
 4986:             my $maxdepth = scalar(@cats);
 4987:             my $colattrib = '';
 4988:             if ($maxdepth > 2) {
 4989:                 $colattrib = ' colspan="2" ';
 4990:             }
 4991:             my @path;
 4992:             if (@cats > 0) {
 4993:                 if (ref($cats[0]) eq 'ARRAY') {
 4994:                     my $numtop = @{$cats[0]};
 4995:                     my $maxnum = $numtop;
 4996:                     my %default_names = (
 4997:                           instcode    => &mt('Official courses'),
 4998:                           communities => &mt('Communities'),
 4999:                     );
 5000: 
 5001:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 5002:                         ($cathash->{'instcode::0'} eq '') ||
 5003:                         (!grep(/^communities$/,@{$cats[0]})) || 
 5004:                         ($cathash->{'communities::0'} eq '')) {
 5005:                         $maxnum ++;
 5006:                     }
 5007:                     my $lastidx;
 5008:                     for (my $i=0; $i<$numtop; $i++) {
 5009:                         my $parent = $cats[0][$i];
 5010:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5011:                         my $item = &escape($parent).'::0';
 5012:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 5013:                         $lastidx = $idx{$item};
 5014:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5015:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 5016:                         for (my $k=0; $k<=$maxnum; $k++) {
 5017:                             my $vpos = $k+1;
 5018:                             my $selstr;
 5019:                             if ($k == $i) {
 5020:                                 $selstr = ' selected="selected" ';
 5021:                             }
 5022:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5023:                         }
 5024:                         $datatable .= '</select></span></td><td>';
 5025:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 5026:                             $datatable .=  '<span class="LC_nobreak">'
 5027:                                            .$default_names{$parent}.'</span>';
 5028:                             if ($parent eq 'instcode') {
 5029:                                 $datatable .= '<br /><span class="LC_nobreak">('
 5030:                                               .&mt('with institutional codes')
 5031:                                               .')</span></td><td'.$colattrib.'>';
 5032:                             } else {
 5033:                                 $datatable .= '<table><tr><td>';
 5034:                             }
 5035:                             $datatable .= '<span class="LC_nobreak">'
 5036:                                           .'<label><input type="radio" name="'
 5037:                                           .$parent.'" value="1" checked="checked" />'
 5038:                                           .&mt('Display').'</label>';
 5039:                             if ($parent eq 'instcode') {
 5040:                                 $datatable .= '&nbsp;';
 5041:                             } else {
 5042:                                 $datatable .= '</span></td></tr><tr><td>'
 5043:                                               .'<span class="LC_nobreak">';
 5044:                             }
 5045:                             $datatable .= '<label><input type="radio" name="'
 5046:                                           .$parent.'" value="0" />'
 5047:                                           .&mt('Do not display').'</label></span>';
 5048:                             if ($parent eq 'communities') {
 5049:                                 $datatable .= '</td></tr></table>';
 5050:                             }
 5051:                             $datatable .= '</td>';
 5052:                         } else {
 5053:                             $datatable .= $parent
 5054:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 5055:                                           .'<input type="checkbox" name="deletecategory" '
 5056:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 5057:                         }
 5058:                         my $depth = 1;
 5059:                         push(@path,$parent);
 5060:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 5061:                         pop(@path);
 5062:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 5063:                         $itemcount ++;
 5064:                     }
 5065:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5066:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 5067:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 5068:                     for (my $k=0; $k<=$maxnum; $k++) {
 5069:                         my $vpos = $k+1;
 5070:                         my $selstr;
 5071:                         if ($k == $numtop) {
 5072:                             $selstr = ' selected="selected" ';
 5073:                         }
 5074:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5075:                     }
 5076:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 5077:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 5078:                                   .'</tr>'."\n";
 5079:                     $itemcount ++;
 5080:                     foreach my $default ('instcode','communities') {
 5081:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 5082:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5083:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 5084:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 5085:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5086:                             for (my $k=0; $k<=$maxnum; $k++) {
 5087:                                 my $vpos = $k+1;
 5088:                                 my $selstr;
 5089:                                 if ($k == $maxnum) {
 5090:                                     $selstr = ' selected="selected" ';
 5091:                                 }
 5092:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5093:                             }
 5094:                             $datatable .= '</select></span></td>'.
 5095:                                           '<td><span class="LC_nobreak">'.
 5096:                                           $default_names{$default}.'</span>';
 5097:                             if ($default eq 'instcode') {
 5098:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5099:                                               .&mt('with institutional codes').')</span>';
 5100:                             }
 5101:                             $datatable .= '</td>'
 5102:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5103:                                           .&mt('Display').'</label>&nbsp;'
 5104:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5105:                                           .&mt('Do not display').'</label></span></td></tr>';
 5106:                         }
 5107:                     }
 5108:                 }
 5109:             } else {
 5110:                 $datatable .= &initialize_categories($itemcount);
 5111:             }
 5112:         } else {
 5113:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 5114:                           .&initialize_categories($itemcount);
 5115:         }
 5116:         $$rowtotal += $itemcount;
 5117:     }
 5118:     return $datatable;
 5119: }
 5120: 
 5121: sub print_serverstatuses {
 5122:     my ($dom,$settings,$rowtotal) = @_;
 5123:     my $datatable;
 5124:     my @pages = &serverstatus_pages();
 5125:     my (%namedaccess,%machineaccess);
 5126:     foreach my $type (@pages) {
 5127:         $namedaccess{$type} = '';
 5128:         $machineaccess{$type}= '';
 5129:     }
 5130:     if (ref($settings) eq 'HASH') {
 5131:         foreach my $type (@pages) {
 5132:             if (exists($settings->{$type})) {
 5133:                 if (ref($settings->{$type}) eq 'HASH') {
 5134:                     foreach my $key (keys(%{$settings->{$type}})) {
 5135:                         if ($key eq 'namedusers') {
 5136:                             $namedaccess{$type} = $settings->{$type}->{$key};
 5137:                         } elsif ($key eq 'machines') {
 5138:                             $machineaccess{$type} = $settings->{$type}->{$key};
 5139:                         }
 5140:                     }
 5141:                 }
 5142:             }
 5143:         }
 5144:     }
 5145:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 5146:     my $rownum = 0;
 5147:     my $css_class;
 5148:     foreach my $type (@pages) {
 5149:         $rownum ++;
 5150:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 5151:         $datatable .= '<tr'.$css_class.'>'.
 5152:                       '<td><span class="LC_nobreak">'.
 5153:                       $titles->{$type}.'</span></td>'.
 5154:                       '<td class="LC_left_item">'.
 5155:                       '<input type="text" name="'.$type.'_namedusers" '.
 5156:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 5157:                       '<td class="LC_right_item">'.
 5158:                       '<span class="LC_nobreak">'.
 5159:                       '<input type="text" name="'.$type.'_machines" '.
 5160:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 5161:                       '</td></tr>'."\n";
 5162:     }
 5163:     $$rowtotal += $rownum;
 5164:     return $datatable;
 5165: }
 5166: 
 5167: sub serverstatus_pages {
 5168:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 5169:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 5170:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 5171:             'uniquecodes','diskusage');
 5172: }
 5173: 
 5174: sub defaults_javascript {
 5175:     my ($settings) = @_;
 5176:     return unless (ref($settings) eq 'HASH'); 
 5177:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5178:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 5179:         if ($maxnum eq '') {
 5180:             $maxnum = 0;
 5181:         }
 5182:         $maxnum ++;
 5183:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 5184:         return <<"ENDSCRIPT";
 5185: <script type="text/javascript">
 5186: // <![CDATA[
 5187: function reorderTypes(form,caller) {
 5188:     var changedVal;
 5189: $jstext 
 5190:     var newpos = 'addinststatus_pos';
 5191:     var current = new Array;
 5192:     var maxh = $maxnum;
 5193:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5194:     var oldVal;
 5195:     if (caller == newpos) {
 5196:         changedVal = newitemVal;
 5197:     } else {
 5198:         var curritem = 'inststatus_pos_'+caller;
 5199:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 5200:         current[newitemVal] = newpos;
 5201:     }
 5202:     for (var i=0; i<inststatuses.length; i++) {
 5203:         if (inststatuses[i] != caller) {
 5204:             var elementName = 'inststatus_pos_'+inststatuses[i];
 5205:             if (form.elements[elementName]) {
 5206:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5207:                 current[currVal] = elementName;
 5208:             }
 5209:         }
 5210:     }
 5211:     for (var j=0; j<maxh; j++) {
 5212:         if (current[j] == undefined) {
 5213:             oldVal = j;
 5214:         }
 5215:     }
 5216:     if (oldVal < changedVal) {
 5217:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5218:            var elementName = current[k];
 5219:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5220:         }
 5221:     } else {
 5222:         for (var k=changedVal; k<oldVal; k++) {
 5223:             var elementName = current[k];
 5224:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5225:         }
 5226:     }
 5227:     return;
 5228: }
 5229: 
 5230: // ]]>
 5231: </script>
 5232: 
 5233: ENDSCRIPT
 5234:     }
 5235: }
 5236: 
 5237: sub coursecategories_javascript {
 5238:     my ($settings) = @_;
 5239:     my ($output,$jstext,$cathash);
 5240:     if (ref($settings) eq 'HASH') {
 5241:         $cathash = $settings->{'cats'};
 5242:     }
 5243:     if (ref($cathash) eq 'HASH') {
 5244:         my (@cats,@jsarray,%idx);
 5245:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 5246:         if (@jsarray > 0) {
 5247:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 5248:             for (my $i=0; $i<@jsarray; $i++) {
 5249:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 5250:                     my $catstr = join('","',@{$jsarray[$i]});
 5251:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 5252:                 }
 5253:             }
 5254:         }
 5255:     } else {
 5256:         $jstext  = '    var categories = Array(1);'."\n".
 5257:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 5258:     }
 5259:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 5260:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 5261:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category.'); 
 5262:     $output = <<"ENDSCRIPT";
 5263: <script type="text/javascript">
 5264: // <![CDATA[
 5265: function reorderCats(form,parent,item,idx) {
 5266:     var changedVal;
 5267: $jstext
 5268:     var newpos = 'addcategory_pos';
 5269:     if (parent == '') {
 5270:         var has_instcode = 0;
 5271:         var maxtop = categories[idx].length;
 5272:         for (var j=0; j<maxtop; j++) {
 5273:             if (categories[idx][j] == 'instcode::0') {
 5274:                 has_instcode == 1;
 5275:             }
 5276:         }
 5277:         if (has_instcode == 0) {
 5278:             categories[idx][maxtop] = 'instcode_pos';
 5279:         }
 5280:     } else {
 5281:         newpos += '_'+parent;
 5282:     }
 5283:     var maxh = 1 + categories[idx].length;
 5284:     var current = new Array;
 5285:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5286:     if (item == newpos) {
 5287:         changedVal = newitemVal;
 5288:     } else {
 5289:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5290:         current[newitemVal] = newpos;
 5291:     }
 5292:     for (var i=0; i<categories[idx].length; i++) {
 5293:         var elementName = categories[idx][i];
 5294:         if (elementName != item) {
 5295:             if (form.elements[elementName]) {
 5296:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5297:                 current[currVal] = elementName;
 5298:             }
 5299:         }
 5300:     }
 5301:     var oldVal;
 5302:     for (var j=0; j<maxh; j++) {
 5303:         if (current[j] == undefined) {
 5304:             oldVal = j;
 5305:         }
 5306:     }
 5307:     if (oldVal < changedVal) {
 5308:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5309:            var elementName = current[k];
 5310:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5311:         }
 5312:     } else {
 5313:         for (var k=changedVal; k<oldVal; k++) {
 5314:             var elementName = current[k];
 5315:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5316:         }
 5317:     }
 5318:     return;
 5319: }
 5320: 
 5321: function categoryCheck(form) {
 5322:     if (form.elements['addcategory_name'].value == 'instcode') {
 5323:         alert('$instcode_reserved\\n$choose_again');
 5324:         return false;
 5325:     }
 5326:     if (form.elements['addcategory_name'].value == 'communities') {
 5327:         alert('$communities_reserved\\n$choose_again');
 5328:         return false;
 5329:     }
 5330:     return true;
 5331: }
 5332: 
 5333: // ]]>
 5334: </script>
 5335: 
 5336: ENDSCRIPT
 5337:     return $output;
 5338: }
 5339: 
 5340: sub initialize_categories {
 5341:     my ($itemcount) = @_;
 5342:     my ($datatable,$css_class,$chgstr);
 5343:     my %default_names = (
 5344:                       instcode    => 'Official courses (with institutional codes)',
 5345:                       communities => 'Communities',
 5346:                         );
 5347:     my $select0 = ' selected="selected"';
 5348:     my $select1 = '';
 5349:     foreach my $default ('instcode','communities') {
 5350:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5351:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 5352:         if ($default eq 'communities') {
 5353:             $select1 = $select0;
 5354:             $select0 = '';
 5355:         }
 5356:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5357:                      .'<select name="'.$default.'_pos">'
 5358:                      .'<option value="0"'.$select0.'>1</option>'
 5359:                      .'<option value="1"'.$select1.'>2</option>'
 5360:                      .'<option value="2">3</option></select>&nbsp;'
 5361:                      .$default_names{$default}
 5362:                      .'</span></td><td><span class="LC_nobreak">'
 5363:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5364:                      .&mt('Display').'</label>&nbsp;<label>'
 5365:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5366:                  .'</label></span></td></tr>';
 5367:         $itemcount ++;
 5368:     }
 5369:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5370:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5371:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5372:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5373:                   .'<option value="0">1</option>'
 5374:                   .'<option value="1">2</option>'
 5375:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5376:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5377:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5378:     return $datatable;
 5379: }
 5380: 
 5381: sub build_category_rows {
 5382:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5383:     my ($text,$name,$item,$chgstr);
 5384:     if (ref($cats) eq 'ARRAY') {
 5385:         my $maxdepth = scalar(@{$cats});
 5386:         if (ref($cats->[$depth]) eq 'HASH') {
 5387:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5388:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5389:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5390:                 $text .= '<td><table class="LC_data_table">';
 5391:                 my ($idxnum,$parent_name,$parent_item);
 5392:                 my $higher = $depth - 1;
 5393:                 if ($higher == 0) {
 5394:                     $parent_name = &escape($parent).'::'.$higher;
 5395:                 } else {
 5396:                     if (ref($path) eq 'ARRAY') {
 5397:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5398:                     }
 5399:                 }
 5400:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5401:                 for (my $j=0; $j<=$numchildren; $j++) {
 5402:                     if ($j < $numchildren) {
 5403:                         $name = $cats->[$depth]{$parent}[$j];
 5404:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5405:                         $idxnum = $idx->{$item};
 5406:                     } else {
 5407:                         $name = $parent_name;
 5408:                         $item = $parent_item;
 5409:                     }
 5410:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5411:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5412:                     for (my $i=0; $i<=$numchildren; $i++) {
 5413:                         my $vpos = $i+1;
 5414:                         my $selstr;
 5415:                         if ($j == $i) {
 5416:                             $selstr = ' selected="selected" ';
 5417:                         }
 5418:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5419:                     }
 5420:                     $text .= '</select>&nbsp;';
 5421:                     if ($j < $numchildren) {
 5422:                         my $deeper = $depth+1;
 5423:                         $text .= $name.'&nbsp;'
 5424:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5425:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5426:                         if(ref($path) eq 'ARRAY') {
 5427:                             push(@{$path},$name);
 5428:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5429:                             pop(@{$path});
 5430:                         }
 5431:                     } else {
 5432:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5433:                         if ($j == $numchildren) {
 5434:                             $text .= $name;
 5435:                         } else {
 5436:                             $text .= $item;
 5437:                         }
 5438:                         $text .= '" value="" />';
 5439:                     }
 5440:                     $text .= '</td></tr>';
 5441:                 }
 5442:                 $text .= '</table></td>';
 5443:             } else {
 5444:                 my $higher = $depth-1;
 5445:                 if ($higher == 0) {
 5446:                     $name = &escape($parent).'::'.$higher;
 5447:                 } else {
 5448:                     if (ref($path) eq 'ARRAY') {
 5449:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5450:                     }
 5451:                 }
 5452:                 my $colspan;
 5453:                 if ($parent ne 'instcode') {
 5454:                     $colspan = $maxdepth - $depth - 1;
 5455:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5456:                 }
 5457:             }
 5458:         }
 5459:     }
 5460:     return $text;
 5461: }
 5462: 
 5463: sub modifiable_userdata_row {
 5464:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5465:     my ($role,$rolename,$statustype);
 5466:     $role = $item;
 5467:     if ($context eq 'cancreate') {
 5468:         if ($item =~ /^emailusername_(.+)$/) {
 5469:             $statustype = $1;
 5470:             $role = 'emailusername';
 5471:             if (ref($usertypes) eq 'HASH') {
 5472:                 if ($usertypes->{$statustype}) {
 5473:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5474:                 } else {
 5475:                     $rolename = &mt('Data provided by user');
 5476:                 }
 5477:             }
 5478:         }
 5479:     } elsif ($context eq 'selfcreate') {
 5480:         if (ref($usertypes) eq 'HASH') {
 5481:             $rolename = $usertypes->{$role};
 5482:         } else {
 5483:             $rolename = $role;
 5484:         }
 5485:     } else {
 5486:         if ($role eq 'cr') {
 5487:             $rolename = &mt('Custom role');
 5488:         } else {
 5489:             $rolename = &Apache::lonnet::plaintext($role);
 5490:         }
 5491:     }
 5492:     my (@fields,%fieldtitles);
 5493:     if (ref($fieldsref) eq 'ARRAY') {
 5494:         @fields = @{$fieldsref};
 5495:     } else {
 5496:         @fields = ('lastname','firstname','middlename','generation',
 5497:                    'permanentemail','id');
 5498:     }
 5499:     if ((ref($titlesref) eq 'HASH')) {
 5500:         %fieldtitles = %{$titlesref};
 5501:     } else {
 5502:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5503:     }
 5504:     my $output;
 5505:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5506:     $output = '<tr '.$css_class.'>'.
 5507:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5508:               '<td class="LC_left_item" colspan="2"><table>';
 5509:     my $rem;
 5510:     my %checks;
 5511:     if (ref($settings) eq 'HASH') {
 5512:         if (ref($settings->{$context}) eq 'HASH') {
 5513:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5514:                 my $hashref = $settings->{$context}->{$role};
 5515:                 if ($role eq 'emailusername') {
 5516:                     if ($statustype) {
 5517:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5518:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5519:                             if (ref($hashref) eq 'HASH') { 
 5520:                                 foreach my $field (@fields) {
 5521:                                     if ($hashref->{$field}) {
 5522:                                         $checks{$field} = $hashref->{$field};
 5523:                                     }
 5524:                                 }
 5525:                             }
 5526:                         }
 5527:                     }
 5528:                 } else {
 5529:                     if (ref($hashref) eq 'HASH') {
 5530:                         foreach my $field (@fields) {
 5531:                             if ($hashref->{$field}) {
 5532:                                 $checks{$field} = ' checked="checked" ';
 5533:                             }
 5534:                         }
 5535:                     }
 5536:                 }
 5537:             }
 5538:         }
 5539:     }
 5540:      
 5541:     for (my $i=0; $i<@fields; $i++) {
 5542:         my $rem = $i%($numinrow);
 5543:         if ($rem == 0) {
 5544:             if ($i > 0) {
 5545:                 $output .= '</tr>';
 5546:             }
 5547:             $output .= '<tr>';
 5548:         }
 5549:         my $check = ' ';
 5550:         unless ($role eq 'emailusername') {
 5551:             if (exists($checks{$fields[$i]})) {
 5552:                 $check = $checks{$fields[$i]}
 5553:             } else {
 5554:                 if ($role eq 'st') {
 5555:                     if (ref($settings) ne 'HASH') {
 5556:                         $check = ' checked="checked" '; 
 5557:                     }
 5558:                 }
 5559:             }
 5560:         }
 5561:         $output .= '<td class="LC_left_item">'.
 5562:                    '<span class="LC_nobreak">';
 5563:         if ($role eq 'emailusername') {
 5564:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5565:                 $checks{$fields[$i]} = 'omit';
 5566:             }
 5567:             foreach my $option ('required','optional','omit') {
 5568:                 my $checked='';
 5569:                 if ($checks{$fields[$i]} eq $option) {
 5570:                     $checked='checked="checked" ';
 5571:                 }
 5572:                 $output .= '<label>'.
 5573:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5574:                            &mt($option).'</label>'.('&nbsp;' x2);
 5575:             }
 5576:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5577:         } else {
 5578:             $output .= '<label>'.
 5579:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5580:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5581:                        '</label>';
 5582:         }
 5583:         $output .= '</span></td>';
 5584:         $rem = @fields%($numinrow);
 5585:     }
 5586:     my $colsleft = $numinrow - $rem;
 5587:     if ($colsleft > 1 ) {
 5588:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5589:                    '&nbsp;</td>';
 5590:     } elsif ($colsleft == 1) {
 5591:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5592:     }
 5593:     $output .= '</tr></table></td></tr>';
 5594:     return $output;
 5595: }
 5596: 
 5597: sub insttypes_row {
 5598:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5599:     my %lt = &Apache::lonlocal::texthash (
 5600:                       cansearch => 'Users allowed to search',
 5601:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5602:                       lockablenames => 'User preference to lock name',
 5603:              );
 5604:     my $showdom;
 5605:     if ($context eq 'cansearch') {
 5606:         $showdom = ' ('.$dom.')';
 5607:     }
 5608:     my $class = 'LC_left_item';
 5609:     if ($context eq 'statustocreate') {
 5610:         $class = 'LC_right_item';
 5611:     }
 5612:     my $css_class = ' class="LC_odd_row"';
 5613:     if ($rownum ne '') { 
 5614:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5615:     }
 5616:     my $output = '<tr'.$css_class.'>'.
 5617:                  '<td>'.$lt{$context}.$showdom.
 5618:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5619:     my $rem;
 5620:     if (ref($types) eq 'ARRAY') {
 5621:         for (my $i=0; $i<@{$types}; $i++) {
 5622:             if (defined($usertypes->{$types->[$i]})) {
 5623:                 my $rem = $i%($numinrow);
 5624:                 if ($rem == 0) {
 5625:                     if ($i > 0) {
 5626:                         $output .= '</tr>';
 5627:                     }
 5628:                     $output .= '<tr>';
 5629:                 }
 5630:                 my $check = ' ';
 5631:                 if (ref($settings) eq 'HASH') {
 5632:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5633:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5634:                             $check = ' checked="checked" ';
 5635:                         }
 5636:                     } elsif ($context eq 'statustocreate') {
 5637:                         $check = ' checked="checked" ';
 5638:                     }
 5639:                 }
 5640:                 $output .= '<td class="LC_left_item">'.
 5641:                            '<span class="LC_nobreak"><label>'.
 5642:                            '<input type="checkbox" name="'.$context.'" '.
 5643:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5644:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5645:             }
 5646:         }
 5647:         $rem = @{$types}%($numinrow);
 5648:     }
 5649:     my $colsleft = $numinrow - $rem;
 5650:     if (($rem == 0) && (@{$types} > 0)) {
 5651:         $output .= '<tr>';
 5652:     }
 5653:     if ($colsleft > 1) {
 5654:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5655:     } else {
 5656:         $output .= '<td class="LC_left_item">';
 5657:     }
 5658:     my $defcheck = ' ';
 5659:     if (ref($settings) eq 'HASH') {  
 5660:         if (ref($settings->{$context}) eq 'ARRAY') {
 5661:             if (grep(/^default$/,@{$settings->{$context}})) {
 5662:                 $defcheck = ' checked="checked" ';
 5663:             }
 5664:         } elsif ($context eq 'statustocreate') {
 5665:             $defcheck = ' checked="checked" ';
 5666:         }
 5667:     }
 5668:     $output .= '<span class="LC_nobreak"><label>'.
 5669:                '<input type="checkbox" name="'.$context.'" '.
 5670:                'value="default"'.$defcheck.'/>'.
 5671:                $othertitle.'</label></span></td>'.
 5672:                '</tr></table></td></tr>';
 5673:     return $output;
 5674: }
 5675: 
 5676: sub sorted_searchtitles {
 5677:     my %searchtitles = &Apache::lonlocal::texthash(
 5678:                          'uname' => 'username',
 5679:                          'lastname' => 'last name',
 5680:                          'lastfirst' => 'last name, first name',
 5681:                      );
 5682:     my @titleorder = ('uname','lastname','lastfirst');
 5683:     return (\%searchtitles,\@titleorder);
 5684: }
 5685: 
 5686: sub sorted_searchtypes {
 5687:     my %srchtypes_desc = (
 5688:                            exact    => 'is exact match',
 5689:                            contains => 'contains ..',
 5690:                            begins   => 'begins with ..',
 5691:                          );
 5692:     my @srchtypeorder = ('exact','begins','contains');
 5693:     return (\%srchtypes_desc,\@srchtypeorder);
 5694: }
 5695: 
 5696: sub usertype_update_row {
 5697:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5698:     my $datatable;
 5699:     my $numinrow = 4;
 5700:     foreach my $type (@{$types}) {
 5701:         if (defined($usertypes->{$type})) {
 5702:             $$rownums ++;
 5703:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5704:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5705:                           '</td><td class="LC_left_item"><table>';
 5706:             for (my $i=0; $i<@{$fields}; $i++) {
 5707:                 my $rem = $i%($numinrow);
 5708:                 if ($rem == 0) {
 5709:                     if ($i > 0) {
 5710:                         $datatable .= '</tr>';
 5711:                     }
 5712:                     $datatable .= '<tr>';
 5713:                 }
 5714:                 my $check = ' ';
 5715:                 if (ref($settings) eq 'HASH') {
 5716:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5717:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5718:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5719:                                 $check = ' checked="checked" ';
 5720:                             }
 5721:                         }
 5722:                     }
 5723:                 }
 5724: 
 5725:                 if ($i == @{$fields}-1) {
 5726:                     my $colsleft = $numinrow - $rem;
 5727:                     if ($colsleft > 1) {
 5728:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5729:                     } else {
 5730:                         $datatable .= '<td>';
 5731:                     }
 5732:                 } else {
 5733:                     $datatable .= '<td>';
 5734:                 }
 5735:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5736:                               '<input type="checkbox" name="updateable_'.$type.
 5737:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5738:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5739:             }
 5740:             $datatable .= '</tr></table></td></tr>';
 5741:         }
 5742:     }
 5743:     return $datatable;
 5744: }
 5745: 
 5746: sub modify_login {
 5747:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5748:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5749:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5750:     %title = ( coursecatalog => 'Display course catalog',
 5751:                adminmail => 'Display administrator E-mail address',
 5752:                helpdesk  => 'Display "Contact Helpdesk" link',
 5753:                newuser => 'Link for visitors to create a user account',
 5754:                loginheader => 'Log-in box header');
 5755:     @offon = ('off','on');
 5756:     if (ref($domconfig{login}) eq 'HASH') {
 5757:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5758:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5759:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5760:             }
 5761:         }
 5762:     }
 5763:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5764:                                            \%domconfig,\%loginhash);
 5765:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5766:     foreach my $item (@toggles) {
 5767:         $loginhash{login}{$item} = $env{'form.'.$item};
 5768:     }
 5769:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5770:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5771:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5772:                                          \%loginhash);
 5773:     }
 5774: 
 5775:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5776:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5777:     if (keys(%servers) > 1) {
 5778:         foreach my $lonhost (keys(%servers)) {
 5779:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5780:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5781:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5782:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5783:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5784:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5785:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5786:                         $changes{'loginvia'}{$lonhost} = 1;
 5787:                     } else {
 5788:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5789:                         $changes{'loginvia'}{$lonhost} = 1;
 5790:                     }
 5791:                 } else {
 5792:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5793:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5794:                         $changes{'loginvia'}{$lonhost} = 1;
 5795:                     }
 5796:                 }
 5797:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5798:                     foreach my $item (@loginvia_attribs) {
 5799:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5800:                     }
 5801:                 } else {
 5802:                     foreach my $item (@loginvia_attribs) {
 5803:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5804:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5805:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5806:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5807:                                 $new = '/';
 5808:                             }
 5809:                         }
 5810:                         if (($item eq 'custompath') && 
 5811:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5812:                             $new = '';
 5813:                         }
 5814:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5815:                             $changes{'loginvia'}{$lonhost} = 1;
 5816:                         }
 5817:                         if ($item eq 'exempt') {
 5818:                             $new =~ s/^\s+//;
 5819:                             $new =~ s/\s+$//;
 5820:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5821:                             my @okips;
 5822:                             foreach my $ip (@poss_ips) {
 5823:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5824:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5825:                                         push(@okips,$ip); 
 5826:                                     }
 5827:                                 }
 5828:                             }
 5829:                             if (@okips > 0) {
 5830:                                 $new = join(',',@okips); 
 5831:                             } else {
 5832:                                 $new = ''; 
 5833:                             }
 5834:                         }
 5835:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5836:                     }
 5837:                 }
 5838:             } else {
 5839:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5840:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5841:                     $changes{'loginvia'}{$lonhost} = 1;
 5842:                     foreach my $item (@loginvia_attribs) {
 5843:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5844:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5845:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5846:                                 $new = '/';
 5847:                             }
 5848:                         }
 5849:                         if (($item eq 'custompath') && 
 5850:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5851:                             $new = '';
 5852:                         }
 5853:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5854:                     }
 5855:                 }
 5856:             }
 5857:         }
 5858:     }
 5859: 
 5860:     my $servadm = $r->dir_config('lonAdmEMail');
 5861:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5862:     if (ref($domconfig{'login'}) eq 'HASH') {
 5863:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5864:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5865:                 if ($lang eq 'nolang') {
 5866:                     push(@currlangs,$lang);
 5867:                 } elsif (defined($langchoices{$lang})) {
 5868:                     push(@currlangs,$lang);
 5869:                 } else {
 5870:                     next;
 5871:                 }
 5872:             }
 5873:         }
 5874:     }
 5875:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5876:     if (@currlangs > 0) {
 5877:         foreach my $lang (@currlangs) {
 5878:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5879:                 $changes{'helpurl'}{$lang} = 1;
 5880:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5881:                 $changes{'helpurl'}{$lang} = 1;
 5882:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5883:                 push(@newlangs,$lang);
 5884:             } else {
 5885:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5886:             }
 5887:         }
 5888:     }
 5889:     unless (grep(/^nolang$/,@currlangs)) {
 5890:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5891:             $changes{'helpurl'}{'nolang'} = 1;
 5892:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5893:             push(@newlangs,'nolang');
 5894:         }
 5895:     }
 5896:     if ($env{'form.loginhelpurl_add_lang'}) {
 5897:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5898:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5899:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5900:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5901:         }
 5902:     }
 5903:     if ((@newlangs > 0) || ($addedfile)) {
 5904:         my $error;
 5905:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5906:         if ($configuserok eq 'ok') {
 5907:             if ($switchserver) {
 5908:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5909:             } elsif ($author_ok eq 'ok') {
 5910:                 my @allnew = @newlangs;
 5911:                 if ($addedfile ne '') {
 5912:                     push(@allnew,$addedfile);
 5913:                 }
 5914:                 foreach my $lang (@allnew) {
 5915:                     my $formelem = 'loginhelpurl_'.$lang;
 5916:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5917:                         $formelem = 'loginhelpurl_add_file';
 5918:                     }
 5919:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5920:                                                                "help/$lang",'','',$newfile{$lang});
 5921:                     if ($result eq 'ok') {
 5922:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5923:                         $changes{'helpurl'}{$lang} = 1;
 5924:                     } else {
 5925:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5926:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5927:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5928:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5929: 
 5930:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5931:                         }
 5932:                     }
 5933:                 }
 5934:             } else {
 5935:                 $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);
 5936:             }
 5937:         } else {
 5938:             $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);
 5939:         }
 5940:         if ($error) {
 5941:             &Apache::lonnet::logthis($error);
 5942:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5943:         }
 5944:     }
 5945:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5946: 
 5947:     my $defaulthelpfile = '/adm/loginproblems.html';
 5948:     my $defaulttext = &mt('Default in use');
 5949: 
 5950:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5951:                                              $dom);
 5952:     if ($putresult eq 'ok') {
 5953:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5954:         my %defaultchecked = (
 5955:                     'coursecatalog' => 'on',
 5956:                     'helpdesk'      => 'on',
 5957:                     'adminmail'     => 'off',
 5958:                     'newuser'       => 'off',
 5959:         );
 5960:         if (ref($domconfig{'login'}) eq 'HASH') {
 5961:             foreach my $item (@toggles) {
 5962:                 if ($defaultchecked{$item} eq 'on') { 
 5963:                     if (($domconfig{'login'}{$item} eq '0') &&
 5964:                         ($env{'form.'.$item} eq '1')) {
 5965:                         $changes{$item} = 1;
 5966:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5967:                               $domconfig{'login'}{$item} eq '1') &&
 5968:                              ($env{'form.'.$item} eq '0')) {
 5969:                         $changes{$item} = 1;
 5970:                     }
 5971:                 } elsif ($defaultchecked{$item} eq 'off') {
 5972:                     if (($domconfig{'login'}{$item} eq '1') &&
 5973:                         ($env{'form.'.$item} eq '0')) {
 5974:                         $changes{$item} = 1;
 5975:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5976:                               $domconfig{'login'}{$item} eq '0') &&
 5977:                              ($env{'form.'.$item} eq '1')) {
 5978:                         $changes{$item} = 1;
 5979:                     }
 5980:                 }
 5981:             }
 5982:         }
 5983:         if (keys(%changes) > 0 || $colchgtext) {
 5984:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5985:             if (ref($lastactref) eq 'HASH') {
 5986:                 $lastactref->{'domainconfig'} = 1;
 5987:             }
 5988:             $resulttext = &mt('Changes made:').'<ul>';
 5989:             foreach my $item (sort(keys(%changes))) {
 5990:                 if ($item eq 'loginvia') {
 5991:                     if (ref($changes{$item}) eq 'HASH') {
 5992:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5993:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5994:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5995:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5996:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5997:                                     $protocol = 'http' if ($protocol ne 'https');
 5998:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5999: 
 6000:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 6001:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 6002:                                     } else {
 6003:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 6004:                                     }
 6005:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 6006:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 6007:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 6008:                                     }
 6009:                                     $resulttext .= '</li>';
 6010:                                 } else {
 6011:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 6012:                                 }
 6013:                             } else {
 6014:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 6015:                             }
 6016:                         }
 6017:                         $resulttext .= '</ul></li>';
 6018:                     }
 6019:                 } elsif ($item eq 'helpurl') {
 6020:                     if (ref($changes{$item}) eq 'HASH') {
 6021:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 6022:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 6023:                                 my ($chg,$link);
 6024:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 6025:                                 if ($lang eq 'nolang') {
 6026:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 6027:                                 } else {
 6028:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 6029:                                 }
 6030:                                 $resulttext .= '<li>'.$chg.'</li>';
 6031:                             } else {
 6032:                                 my $chg;
 6033:                                 if ($lang eq 'nolang') {
 6034:                                     $chg = &mt('custom log-in help file for no preferred language');
 6035:                                 } else {
 6036:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 6037:                                 }
 6038:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 6039:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 6040:                                                       '?inhibitmenu=yes',$chg,600,500).
 6041:                                                '</li>';
 6042:                             }
 6043:                         }
 6044:                     }
 6045:                 } elsif ($item eq 'captcha') {
 6046:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6047:                         my $chgtxt;
 6048:                         if ($loginhash{'login'}{$item} eq 'notused') {
 6049:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 6050:                         } else {
 6051:                             my %captchas = &captcha_phrases();
 6052:                             if ($captchas{$loginhash{'login'}{$item}}) {
 6053:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 6054:                             } else {
 6055:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 6056:                             }
 6057:                         }
 6058:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6059:                     }
 6060:                 } elsif ($item eq 'recaptchakeys') {
 6061:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6062:                         my ($privkey,$pubkey);
 6063:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 6064:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 6065:                             $privkey = $loginhash{'login'}{$item}{'private'};
 6066:                         }
 6067:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 6068:                         if (!$pubkey) {
 6069:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 6070:                         } else {
 6071:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6072:                         }
 6073:                         if (!$privkey) {
 6074:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 6075:                         } else {
 6076:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 6077:                         }
 6078:                         $chgtxt .= '</ul>';
 6079:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6080:                     }
 6081:                 } else {
 6082:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 6083:                 }
 6084:             }
 6085:             $resulttext .= $colchgtext.'</ul>';
 6086:         } else {
 6087:             $resulttext = &mt('No changes made to log-in page settings');
 6088:         }
 6089:     } else {
 6090:         $resulttext = '<span class="LC_error">'.
 6091: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6092:     }
 6093:     if ($errors) {
 6094:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 6095:                        $errors.'</ul>';
 6096:     }
 6097:     return $resulttext;
 6098: }
 6099: 
 6100: sub color_font_choices {
 6101:     my %choices =
 6102:         &Apache::lonlocal::texthash (
 6103:             img => "Header",
 6104:             bgs => "Background colors",
 6105:             links => "Link colors",
 6106:             images => "Images",
 6107:             font => "Font color",
 6108:             fontmenu => "Font menu",
 6109:             pgbg => "Page",
 6110:             tabbg => "Header",
 6111:             sidebg => "Border",
 6112:             link => "Link",
 6113:             alink => "Active link",
 6114:             vlink => "Visited link",
 6115:         );
 6116:     return %choices;
 6117: }
 6118: 
 6119: sub modify_rolecolors {
 6120:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 6121:     my ($resulttext,%rolehash);
 6122:     $rolehash{'rolecolors'} = {};
 6123:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 6124:         if ($domconfig{'rolecolors'} eq '') {
 6125:             $domconfig{'rolecolors'} = {};
 6126:         }
 6127:     }
 6128:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 6129:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 6130:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 6131:                                              $dom);
 6132:     if ($putresult eq 'ok') {
 6133:         if (keys(%changes) > 0) {
 6134:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6135:             if (ref($lastactref) eq 'HASH') {
 6136:                 $lastactref->{'domainconfig'} = 1;
 6137:             }
 6138:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 6139:                                              $rolehash{'rolecolors'});
 6140:         } else {
 6141:             $resulttext = &mt('No changes made to default color schemes');
 6142:         }
 6143:     } else {
 6144:         $resulttext = '<span class="LC_error">'.
 6145: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6146:     }
 6147:     if ($errors) {
 6148:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6149:                        $errors.'</ul>';
 6150:     }
 6151:     return $resulttext;
 6152: }
 6153: 
 6154: sub modify_colors {
 6155:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 6156:     my (%changes,%choices);
 6157:     my @bgs;
 6158:     my @links = ('link','alink','vlink');
 6159:     my @logintext;
 6160:     my @images;
 6161:     my $servadm = $r->dir_config('lonAdmEMail');
 6162:     my $errors;
 6163:     my %defaults;
 6164:     foreach my $role (@{$roles}) {
 6165:         if ($role eq 'login') {
 6166:             %choices = &login_choices();
 6167:             @logintext = ('textcol','bgcol');
 6168:         } else {
 6169:             %choices = &color_font_choices();
 6170:         }
 6171:         if ($role eq 'login') {
 6172:             @images = ('img','logo','domlogo','login');
 6173:             @bgs = ('pgbg','mainbg','sidebg');
 6174:         } else {
 6175:             @images = ('img');
 6176:             @bgs = ('pgbg','tabbg','sidebg');
 6177:         }
 6178:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 6179:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 6180:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 6181:         }
 6182:         if ($role eq 'login') {
 6183:             foreach my $item (@logintext) {
 6184:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6185:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6186:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6187:                 }
 6188:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 6189:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6190:                 }
 6191:             }
 6192:         } else {
 6193:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 6194:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 6195:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 6196:             }
 6197:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 6198:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 6199:             }
 6200:         }
 6201:         foreach my $item (@bgs) {
 6202:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6203:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6204:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6205:             }
 6206:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 6207:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6208:             }
 6209:         }
 6210:         foreach my $item (@links) {
 6211:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6212:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6213:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6214:             }
 6215:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 6216:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6217:             }
 6218:         }
 6219:         my ($configuserok,$author_ok,$switchserver) = 
 6220:             &config_check($dom,$confname,$servadm);
 6221:         my ($width,$height) = &thumb_dimensions();
 6222:         if (ref($domconfig->{$role}) ne 'HASH') {
 6223:             $domconfig->{$role} = {};
 6224:         }
 6225:         foreach my $img (@images) {
 6226:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 6227:                 if (defined($env{'form.login_showlogo_'.$img})) {
 6228:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 6229:                 } else { 
 6230:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 6231:                 }
 6232:             } 
 6233: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 6234: 		 && !defined($domconfig->{$role}{$img})
 6235: 		 && !$env{'form.'.$role.'_del_'.$img}
 6236: 		 && $env{'form.'.$role.'_import_'.$img}) {
 6237: 		# import the old configured image from the .tab setting
 6238: 		# if they haven't provided a new one 
 6239: 		$domconfig->{$role}{$img} = 
 6240: 		    $env{'form.'.$role.'_import_'.$img};
 6241: 	    }
 6242:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 6243:                 my $error;
 6244:                 if ($configuserok eq 'ok') {
 6245:                     if ($switchserver) {
 6246:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 6247:                     } else {
 6248:                         if ($author_ok eq 'ok') {
 6249:                             my ($result,$logourl) = 
 6250:                                 &publishlogo($r,'upload',$role.'_'.$img,
 6251:                                            $dom,$confname,$img,$width,$height);
 6252:                             if ($result eq 'ok') {
 6253:                                 $confhash->{$role}{$img} = $logourl;
 6254:                                 $changes{$role}{'images'}{$img} = 1;
 6255:                             } else {
 6256:                                 $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);
 6257:                             }
 6258:                         } else {
 6259:                             $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);
 6260:                         }
 6261:                     }
 6262:                 } else {
 6263:                     $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);
 6264:                 }
 6265:                 if ($error) {
 6266:                     &Apache::lonnet::logthis($error);
 6267:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6268:                 }
 6269:             } elsif ($domconfig->{$role}{$img} ne '') {
 6270:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 6271:                     my $error;
 6272:                     if ($configuserok eq 'ok') {
 6273: # is confname an author?
 6274:                         if ($switchserver eq '') {
 6275:                             if ($author_ok eq 'ok') {
 6276:                                 my ($result,$logourl) = 
 6277:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 6278:                                             $dom,$confname,$img,$width,$height);
 6279:                                 if ($result eq 'ok') {
 6280:                                     $confhash->{$role}{$img} = $logourl;
 6281: 				    $changes{$role}{'images'}{$img} = 1;
 6282:                                 }
 6283:                             }
 6284:                         }
 6285:                     }
 6286:                 }
 6287:             }
 6288:         }
 6289:         if (ref($domconfig) eq 'HASH') {
 6290:             if (ref($domconfig->{$role}) eq 'HASH') {
 6291:                 foreach my $img (@images) {
 6292:                     if ($domconfig->{$role}{$img} ne '') {
 6293:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6294:                             $confhash->{$role}{$img} = '';
 6295:                             $changes{$role}{'images'}{$img} = 1;
 6296:                         } else {
 6297:                             if ($confhash->{$role}{$img} eq '') {
 6298:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 6299:                             }
 6300:                         }
 6301:                     } else {
 6302:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6303:                             $confhash->{$role}{$img} = '';
 6304:                             $changes{$role}{'images'}{$img} = 1;
 6305:                         } 
 6306:                     }
 6307:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 6308:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 6309:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 6310:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 6311:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 6312:                             }
 6313:                         } else {
 6314:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6315:                                 $changes{$role}{'showlogo'}{$img} = 1;
 6316:                             }
 6317:                         }
 6318:                     }
 6319:                 }
 6320:                 if ($domconfig->{$role}{'font'} ne '') {
 6321:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 6322:                         $changes{$role}{'font'} = 1;
 6323:                     }
 6324:                 } else {
 6325:                     if ($confhash->{$role}{'font'}) {
 6326:                         $changes{$role}{'font'} = 1;
 6327:                     }
 6328:                 }
 6329:                 if ($role ne 'login') {
 6330:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 6331:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 6332:                             $changes{$role}{'fontmenu'} = 1;
 6333:                         }
 6334:                     } else {
 6335:                         if ($confhash->{$role}{'fontmenu'}) {
 6336:                             $changes{$role}{'fontmenu'} = 1;
 6337:                         }
 6338:                     }
 6339:                 }
 6340:                 foreach my $item (@bgs) {
 6341:                     if ($domconfig->{$role}{$item} ne '') {
 6342:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6343:                             $changes{$role}{'bgs'}{$item} = 1;
 6344:                         } 
 6345:                     } else {
 6346:                         if ($confhash->{$role}{$item}) {
 6347:                             $changes{$role}{'bgs'}{$item} = 1;
 6348:                         }
 6349:                     }
 6350:                 }
 6351:                 foreach my $item (@links) {
 6352:                     if ($domconfig->{$role}{$item} ne '') {
 6353:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6354:                             $changes{$role}{'links'}{$item} = 1;
 6355:                         }
 6356:                     } else {
 6357:                         if ($confhash->{$role}{$item}) {
 6358:                             $changes{$role}{'links'}{$item} = 1;
 6359:                         }
 6360:                     }
 6361:                 }
 6362:                 foreach my $item (@logintext) {
 6363:                     if ($domconfig->{$role}{$item} ne '') {
 6364:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6365:                             $changes{$role}{'logintext'}{$item} = 1;
 6366:                         }
 6367:                     } else {
 6368:                         if ($confhash->{$role}{$item}) {
 6369:                             $changes{$role}{'logintext'}{$item} = 1;
 6370:                         }
 6371:                     }
 6372:                 }
 6373:             } else {
 6374:                 &default_change_checker($role,\@images,\@links,\@bgs,
 6375:                                         \@logintext,$confhash,\%changes); 
 6376:             }
 6377:         } else {
 6378:             &default_change_checker($role,\@images,\@links,\@bgs,
 6379:                                     \@logintext,$confhash,\%changes); 
 6380:         }
 6381:     }
 6382:     return ($errors,%changes);
 6383: }
 6384: 
 6385: sub config_check {
 6386:     my ($dom,$confname,$servadm) = @_;
 6387:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6388:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6389:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6390:                                                    $confname,$servadm);
 6391:     if ($configuserok eq 'ok') {
 6392:         $switchserver = &check_switchserver($dom,$confname);
 6393:         if ($switchserver eq '') {
 6394:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6395:         }
 6396:     }
 6397:     return ($configuserok,$author_ok,$switchserver);
 6398: }
 6399: 
 6400: sub default_change_checker {
 6401:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6402:     foreach my $item (@{$links}) {
 6403:         if ($confhash->{$role}{$item}) {
 6404:             $changes->{$role}{'links'}{$item} = 1;
 6405:         }
 6406:     }
 6407:     foreach my $item (@{$bgs}) {
 6408:         if ($confhash->{$role}{$item}) {
 6409:             $changes->{$role}{'bgs'}{$item} = 1;
 6410:         }
 6411:     }
 6412:     foreach my $item (@{$logintext}) {
 6413:         if ($confhash->{$role}{$item}) {
 6414:             $changes->{$role}{'logintext'}{$item} = 1;
 6415:         }
 6416:     }
 6417:     foreach my $img (@{$images}) {
 6418:         if ($env{'form.'.$role.'_del_'.$img}) {
 6419:             $confhash->{$role}{$img} = '';
 6420:             $changes->{$role}{'images'}{$img} = 1;
 6421:         }
 6422:         if ($role eq 'login') {
 6423:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6424:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6425:             }
 6426:         }
 6427:     }
 6428:     if ($confhash->{$role}{'font'}) {
 6429:         $changes->{$role}{'font'} = 1;
 6430:     }
 6431: }
 6432: 
 6433: sub display_colorchgs {
 6434:     my ($dom,$changes,$roles,$confhash) = @_;
 6435:     my (%choices,$resulttext);
 6436:     if (!grep(/^login$/,@{$roles})) {
 6437:         $resulttext = &mt('Changes made:').'<br />';
 6438:     }
 6439:     foreach my $role (@{$roles}) {
 6440:         if ($role eq 'login') {
 6441:             %choices = &login_choices();
 6442:         } else {
 6443:             %choices = &color_font_choices();
 6444:         }
 6445:         if (ref($changes->{$role}) eq 'HASH') {
 6446:             if ($role ne 'login') {
 6447:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6448:             }
 6449:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6450:                 if ($role ne 'login') {
 6451:                     $resulttext .= '<ul>';
 6452:                 }
 6453:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6454:                     if ($role ne 'login') {
 6455:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6456:                     }
 6457:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6458:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6459:                             if ($confhash->{$role}{$key}{$item}) {
 6460:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6461:                             } else {
 6462:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6463:                             }
 6464:                         } elsif ($confhash->{$role}{$item} eq '') {
 6465:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6466:                         } else {
 6467:                             my $newitem = $confhash->{$role}{$item};
 6468:                             if ($key eq 'images') {
 6469:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6470:                             }
 6471:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6472:                         }
 6473:                     }
 6474:                     if ($role ne 'login') {
 6475:                         $resulttext .= '</ul></li>';
 6476:                     }
 6477:                 } else {
 6478:                     if ($confhash->{$role}{$key} eq '') {
 6479:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6480:                     } else {
 6481:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6482:                     }
 6483:                 }
 6484:                 if ($role ne 'login') {
 6485:                     $resulttext .= '</ul>';
 6486:                 }
 6487:             }
 6488:         }
 6489:     }
 6490:     return $resulttext;
 6491: }
 6492: 
 6493: sub thumb_dimensions {
 6494:     return ('200','50');
 6495: }
 6496: 
 6497: sub check_dimensions {
 6498:     my ($inputfile) = @_;
 6499:     my ($fullwidth,$fullheight);
 6500:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6501:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6502:             my $imageinfo = <PIPE>;
 6503:             if (!close(PIPE)) {
 6504:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6505:             }
 6506:             chomp($imageinfo);
 6507:             my ($fullsize) = 
 6508:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6509:             if ($fullsize) {
 6510:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6511:             }
 6512:         }
 6513:     }
 6514:     return ($fullwidth,$fullheight);
 6515: }
 6516: 
 6517: sub check_configuser {
 6518:     my ($uhome,$dom,$confname,$servadm) = @_;
 6519:     my ($configuserok,%currroles);
 6520:     if ($uhome eq 'no_host') {
 6521:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6522:         my $configpass = &LONCAPA::Enrollment::create_password();
 6523:         $configuserok = 
 6524:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6525:                              $configpass,'','','','','',undef,$servadm);
 6526:     } else {
 6527:         $configuserok = 'ok';
 6528:         %currroles = 
 6529:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6530:     }
 6531:     return ($configuserok,%currroles);
 6532: }
 6533: 
 6534: sub check_authorstatus {
 6535:     my ($dom,$confname,%currroles) = @_;
 6536:     my $author_ok;
 6537:     if (!$currroles{':'.$dom.':au'}) {
 6538:         my $start = time;
 6539:         my $end = 0;
 6540:         $author_ok = 
 6541:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6542:                                         'au',$end,$start,'','','domconfig');
 6543:     } else {
 6544:         $author_ok = 'ok';
 6545:     }
 6546:     return $author_ok;
 6547: }
 6548: 
 6549: sub publishlogo {
 6550:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6551:     my ($output,$fname,$logourl);
 6552:     if ($action eq 'upload') {
 6553:         $fname=$env{'form.'.$formname.'.filename'};
 6554:         chop($env{'form.'.$formname});
 6555:     } else {
 6556:         ($fname) = ($formname =~ /([^\/]+)$/);
 6557:     }
 6558:     if ($savefileas ne '') {
 6559:         $fname = $savefileas;
 6560:     }
 6561:     $fname=&Apache::lonnet::clean_filename($fname);
 6562: # See if there is anything left
 6563:     unless ($fname) { return ('error: no uploaded file'); }
 6564:     $fname="$subdir/$fname";
 6565:     my $docroot=$r->dir_config('lonDocRoot');
 6566:     my $filepath="$docroot/priv";
 6567:     my $relpath = "$dom/$confname";
 6568:     my ($fnamepath,$file,$fetchthumb);
 6569:     $file=$fname;
 6570:     if ($fname=~m|/|) {
 6571:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6572:     }
 6573:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6574:     my $count;
 6575:     for ($count=5;$count<=$#parts;$count++) {
 6576:         $filepath.="/$parts[$count]";
 6577:         if ((-e $filepath)!=1) {
 6578:             mkdir($filepath,02770);
 6579:         }
 6580:     }
 6581:     # Check for bad extension and disallow upload
 6582:     if ($file=~/\.(\w+)$/ &&
 6583:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6584:         $output = 
 6585:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6586:     } elsif ($file=~/\.(\w+)$/ &&
 6587:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6588:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6589:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6590:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6591:     } elsif (-d "$filepath/$file") {
 6592:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6593:     } else {
 6594:         my $source = $filepath.'/'.$file;
 6595:         my $logfile;
 6596:         if (!open($logfile,">>$source".'.log')) {
 6597:             return (&mt('No write permission to Authoring Space'));
 6598:         }
 6599:         print $logfile
 6600: "\n================= Publish ".localtime()." ================\n".
 6601: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6602: # Save the file
 6603:         if (!open(FH,'>'.$source)) {
 6604:             &Apache::lonnet::logthis('Failed to create '.$source);
 6605:             return (&mt('Failed to create file'));
 6606:         }
 6607:         if ($action eq 'upload') {
 6608:             if (!print FH ($env{'form.'.$formname})) {
 6609:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6610:                 return (&mt('Failed to write file'));
 6611:             }
 6612:         } else {
 6613:             my $original = &Apache::lonnet::filelocation('',$formname);
 6614:             if(!copy($original,$source)) {
 6615:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6616:                 return (&mt('Failed to write file'));
 6617:             }
 6618:         }
 6619:         close(FH);
 6620:         chmod(0660, $source); # Permissions to rw-rw---.
 6621: 
 6622:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6623:         my $copyfile=$targetdir.'/'.$file;
 6624: 
 6625:         my @parts=split(/\//,$targetdir);
 6626:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6627:         for (my $count=5;$count<=$#parts;$count++) {
 6628:             $path.="/$parts[$count]";
 6629:             if (!-e $path) {
 6630:                 print $logfile "\nCreating directory ".$path;
 6631:                 mkdir($path,02770);
 6632:             }
 6633:         }
 6634:         my $versionresult;
 6635:         if (-e $copyfile) {
 6636:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6637:         } else {
 6638:             $versionresult = 'ok';
 6639:         }
 6640:         if ($versionresult eq 'ok') {
 6641:             if (copy($source,$copyfile)) {
 6642:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6643:                 $output = 'ok';
 6644:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6645:                 push(@{$modified_urls},[$copyfile,$source]);
 6646:                 my $metaoutput = 
 6647:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6648:                 unless ($registered_cleanup) {
 6649:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6650:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6651:                     $registered_cleanup=1;
 6652:                 }
 6653:             } else {
 6654:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6655:                 $output = &mt('Failed to copy file to RES space').", $!";
 6656:             }
 6657:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6658:                 my $inputfile = $filepath.'/'.$file;
 6659:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6660:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6661:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6662:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6663:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6664:                         system("convert -sample $thumbsize $inputfile $outfile");
 6665:                         chmod(0660, $filepath.'/tn-'.$file);
 6666:                         if (-e $outfile) {
 6667:                             my $copyfile=$targetdir.'/tn-'.$file;
 6668:                             if (copy($outfile,$copyfile)) {
 6669:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6670:                                 my $thumb_metaoutput = 
 6671:                                     &write_metadata($dom,$confname,$formname,
 6672:                                                     $targetdir,'tn-'.$file,$logfile);
 6673:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6674:                                 unless ($registered_cleanup) {
 6675:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6676:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6677:                                     $registered_cleanup=1;
 6678:                                 }
 6679:                             } else {
 6680:                                 print $logfile "\nUnable to write ".$copyfile.
 6681:                                                ':'.$!."\n";
 6682:                             }
 6683:                         }
 6684:                     }
 6685:                 }
 6686:             }
 6687:         } else {
 6688:             $output = $versionresult;
 6689:         }
 6690:     }
 6691:     return ($output,$logourl);
 6692: }
 6693: 
 6694: sub logo_versioning {
 6695:     my ($targetdir,$file,$logfile) = @_;
 6696:     my $target = $targetdir.'/'.$file;
 6697:     my ($maxversion,$fn,$extn,$output);
 6698:     $maxversion = 0;
 6699:     if ($file =~ /^(.+)\.(\w+)$/) {
 6700:         $fn=$1;
 6701:         $extn=$2;
 6702:     }
 6703:     opendir(DIR,$targetdir);
 6704:     while (my $filename=readdir(DIR)) {
 6705:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6706:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6707:         }
 6708:     }
 6709:     $maxversion++;
 6710:     print $logfile "\nCreating old version ".$maxversion."\n";
 6711:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6712:     if (copy($target,$copyfile)) {
 6713:         print $logfile "Copied old target to ".$copyfile."\n";
 6714:         $copyfile=$copyfile.'.meta';
 6715:         if (copy($target.'.meta',$copyfile)) {
 6716:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6717:             $output = 'ok';
 6718:         } else {
 6719:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6720:             $output = &mt('Failed to copy old meta').", $!, ";
 6721:         }
 6722:     } else {
 6723:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6724:         $output = &mt('Failed to copy old target').", $!, ";
 6725:     }
 6726:     return $output;
 6727: }
 6728: 
 6729: sub write_metadata {
 6730:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6731:     my (%metadatafields,%metadatakeys,$output);
 6732:     $metadatafields{'title'}=$formname;
 6733:     $metadatafields{'creationdate'}=time;
 6734:     $metadatafields{'lastrevisiondate'}=time;
 6735:     $metadatafields{'copyright'}='public';
 6736:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6737:                                          $env{'user.domain'};
 6738:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6739:     $metadatafields{'domain'}=$dom;
 6740:     {
 6741:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6742:         my $mfh;
 6743:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6744:             foreach (sort(keys(%metadatafields))) {
 6745:                 unless ($_=~/\./) {
 6746:                     my $unikey=$_;
 6747:                     $unikey=~/^([A-Za-z]+)/;
 6748:                     my $tag=$1;
 6749:                     $tag=~tr/A-Z/a-z/;
 6750:                     print $mfh "\n\<$tag";
 6751:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6752:                         my $value=$metadatafields{$unikey.'.'.$_};
 6753:                         $value=~s/\"/\'\'/g;
 6754:                         print $mfh ' '.$_.'="'.$value.'"';
 6755:                     }
 6756:                     print $mfh '>'.
 6757:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6758:                             .'</'.$tag.'>';
 6759:                 }
 6760:             }
 6761:             $output = 'ok';
 6762:             print $logfile "\nWrote metadata";
 6763:             close($mfh);
 6764:         } else {
 6765:             print $logfile "\nFailed to open metadata file";
 6766:             $output = &mt('Could not write metadata');
 6767:         }
 6768:     }
 6769:     return $output;
 6770: }
 6771: 
 6772: sub notifysubscribed {
 6773:     foreach my $targetsource (@{$modified_urls}){
 6774:         next unless (ref($targetsource) eq 'ARRAY');
 6775:         my ($target,$source)=@{$targetsource};
 6776:         if ($source ne '') {
 6777:             if (open(my $logfh,'>>'.$source.'.log')) {
 6778:                 print $logfh "\nCleanup phase: Notifications\n";
 6779:                 my @subscribed=&subscribed_hosts($target);
 6780:                 foreach my $subhost (@subscribed) {
 6781:                     print $logfh "\nNotifying host ".$subhost.':';
 6782:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6783:                     print $logfh $reply;
 6784:                 }
 6785:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6786:                 foreach my $subhost (@subscribedmeta) {
 6787:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6788:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6789:                                                         $subhost);
 6790:                     print $logfh $reply;
 6791:                 }
 6792:                 print $logfh "\n============ Done ============\n";
 6793:                 close($logfh);
 6794:             }
 6795:         }
 6796:     }
 6797:     return OK;
 6798: }
 6799: 
 6800: sub subscribed_hosts {
 6801:     my ($target) = @_;
 6802:     my @subscribed;
 6803:     if (open(my $fh,"<$target.subscription")) {
 6804:         while (my $subline=<$fh>) {
 6805:             if ($subline =~ /^($match_lonid):/) {
 6806:                 my $host = $1;
 6807:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6808:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6809:                         push(@subscribed,$host);
 6810:                     }
 6811:                 }
 6812:             }
 6813:         }
 6814:     }
 6815:     return @subscribed;
 6816: }
 6817: 
 6818: sub check_switchserver {
 6819:     my ($dom,$confname) = @_;
 6820:     my ($allowed,$switchserver);
 6821:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6822:     if ($home eq 'no_host') {
 6823:         $home = &Apache::lonnet::domain($dom,'primary');
 6824:     }
 6825:     my @ids=&Apache::lonnet::current_machine_ids();
 6826:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6827:     if (!$allowed) {
 6828: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6829:     }
 6830:     return $switchserver;
 6831: }
 6832: 
 6833: sub modify_quotas {
 6834:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6835:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6836:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6837:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 6838:         $validationfieldsref);
 6839:     if ($action eq 'quotas') {
 6840:         $context = 'tools'; 
 6841:     } else {
 6842:         $context = $action;
 6843:     }
 6844:     if ($context eq 'requestcourses') {
 6845:         @usertools = ('official','unofficial','community','textbook');
 6846:         @options =('norequest','approval','validate','autolimit');
 6847:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6848:         %titles = &courserequest_titles();
 6849:         $toolregexp = join('|',@usertools);
 6850:         %conditions = &courserequest_conditions();
 6851:         $confname = $dom.'-domainconfig';
 6852:         my $servadm = $r->dir_config('lonAdmEMail');
 6853:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6854:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 6855:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6856:     } elsif ($context eq 'requestauthor') {
 6857:         @usertools = ('author');
 6858:         %titles = &authorrequest_titles();
 6859:     } else {
 6860:         @usertools = ('aboutme','blog','webdav','portfolio');
 6861:         %titles = &tool_titles();
 6862:     }
 6863:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6864:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6865:     foreach my $key (keys(%env)) {
 6866:         if ($context eq 'requestcourses') {
 6867:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6868:                 my $item = $1;
 6869:                 my $type = $2;
 6870:                 if ($type =~ /^limit_(.+)/) {
 6871:                     $limithash{$item}{$1} = $env{$key};
 6872:                 } else {
 6873:                     $confhash{$item}{$type} = $env{$key};
 6874:                 }
 6875:             }
 6876:         } elsif ($context eq 'requestauthor') {
 6877:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6878:                 $confhash{$1} = $env{$key};
 6879:             }
 6880:         } else {
 6881:             if ($key =~ /^form\.quota_(.+)$/) {
 6882:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6883:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6884:                 $confhash{'authorquota'}{$1} = $env{$key};
 6885:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6886:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6887:             }
 6888:         }
 6889:     }
 6890:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6891:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6892:         @approvalnotify = sort(@approvalnotify);
 6893:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6894:         my @crstypes = ('official','unofficial','community','textbook');
 6895:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6896:         foreach my $type (@hasuniquecode) {
 6897:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6898:                 $confhash{'uniquecode'}{$type} = 1;
 6899:             }
 6900:         }
 6901:         my (%newbook,%allpos);
 6902:         if ($context eq 'requestcourses') {
 6903:             foreach my $type ('textbooks','templates') {
 6904:                 @{$allpos{$type}} = (); 
 6905:                 my $invalid;
 6906:                 if ($type eq 'textbooks') {
 6907:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 6908:                 } else {
 6909:                     $invalid = &mt('Invalid LON-CAPA course for template');
 6910:                 }
 6911:                 if ($env{'form.'.$type.'_addbook'}) {
 6912:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 6913:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 6914:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 6915:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 6916:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 6917:                         } else {
 6918:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 6919:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 6920:                             $position =~ s/\D+//g;
 6921:                             if ($position ne '') {
 6922:                                 $allpos{$type}[$position] = $newbook{$type};
 6923:                             }
 6924:                         }
 6925:                     } else {
 6926:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 6927:                     }
 6928:                 }
 6929:             } 
 6930:         }
 6931:         if (ref($domconfig{$action}) eq 'HASH') {
 6932:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6933:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6934:                     $changes{'notify'}{'approval'} = 1;
 6935:                 }
 6936:             } else {
 6937:                 if ($confhash{'notify'}{'approval'}) {
 6938:                     $changes{'notify'}{'approval'} = 1;
 6939:                 }
 6940:             }
 6941:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6942:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6943:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6944:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6945:                             $changes{'uniquecode'} = 1;
 6946:                         }
 6947:                     }
 6948:                     unless ($changes{'uniquecode'}) {
 6949:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6950:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6951:                                 $changes{'uniquecode'} = 1;
 6952:                             }
 6953:                         }
 6954:                     }
 6955:                } else {
 6956:                    $changes{'uniquecode'} = 1;
 6957:                }
 6958:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6959:                 $changes{'uniquecode'} = 1;
 6960:             }
 6961:             if ($context eq 'requestcourses') {
 6962:                 foreach my $type ('textbooks','templates') {
 6963:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 6964:                         my %deletions;
 6965:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 6966:                         if (@todelete) {
 6967:                             map { $deletions{$_} = 1; } @todelete;
 6968:                         }
 6969:                         my %imgdeletions;
 6970:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 6971:                         if (@todeleteimages) {
 6972:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 6973:                         }
 6974:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 6975:                         for (my $i=0; $i<=$maxnum; $i++) {
 6976:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 6977:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 6978:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 6979:                                 if ($deletions{$key}) {
 6980:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 6981:                                         #FIXME need to obsolete item in RES space
 6982:                                     }
 6983:                                     next;
 6984:                                 } else {
 6985:                                     my $newpos = $env{'form.'.$itemid};
 6986:                                     $newpos =~ s/\D+//g;
 6987:                                     foreach my $item ('subject','title','publisher','author') {
 6988:                                         next if ((($item eq 'author') || ($item eq 'publisher')) && 
 6989:                                                  ($type eq 'templates'));
 6990:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 6991:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 6992:                                             $changes{$type}{$key} = 1;
 6993:                                         }
 6994:                                     }
 6995:                                     $allpos{$type}[$newpos] = $key;
 6996:                                 }
 6997:                                 if ($imgdeletions{$key}) {
 6998:                                     $changes{$type}{$key} = 1;
 6999:                                     #FIXME need to obsolete item in RES space
 7000:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 7001:                                     my ($cdom,$cnum) = split(/_/,$key);
 7002:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 7003:                                                                                   $cdom,$cnum,$type,$configuserok,
 7004:                                                                                   $switchserver,$author_ok);
 7005:                                     if ($imgurl) {
 7006:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 7007:                                         $changes{$type}{$key} = 1; 
 7008:                                     }
 7009:                                     if ($error) {
 7010:                                         &Apache::lonnet::logthis($error);
 7011:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7012:                                     } 
 7013:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 7014:                                     $confhash{$type}{$key}{'image'} = 
 7015:                                         $domconfig{$action}{$type}{$key}{'image'};
 7016:                                 }
 7017:                             }
 7018:                         }
 7019:                     }
 7020:                 }
 7021:             }
 7022:         } else {
 7023:             if ($confhash{'notify'}{'approval'}) {
 7024:                 $changes{'notify'}{'approval'} = 1;
 7025:             }
 7026:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 7027:                 $changes{'uniquecode'} = 1;
 7028:             }
 7029:         }
 7030:         if ($context eq 'requestcourses') {
 7031:             foreach my $type ('textbooks','templates') {
 7032:                 if ($newbook{$type}) {
 7033:                     $changes{$type}{$newbook{$type}} = 1;
 7034:                     foreach my $item ('subject','title','publisher','author') {
 7035:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7036:                                  ($type eq 'template'));
 7037:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 7038:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 7039:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 7040:                         }
 7041:                     }
 7042:                     if ($type eq 'textbooks') {
 7043:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 7044:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 7045:                             my ($imageurl,$error) =
 7046:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 7047:                                                         $configuserok,$switchserver,$author_ok);
 7048:                             if ($imageurl) {
 7049:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 7050:                             }
 7051:                             if ($error) {
 7052:                                 &Apache::lonnet::logthis($error);
 7053:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7054:                             }
 7055:                         }
 7056:                     }
 7057:                 }
 7058:                 if (@{$allpos{$type}} > 0) {
 7059:                     my $idx = 0;
 7060:                     foreach my $item (@{$allpos{$type}}) {
 7061:                         if ($item ne '') {
 7062:                             $confhash{$type}{$item}{'order'} = $idx;
 7063:                             if (ref($domconfig{$action}) eq 'HASH') {
 7064:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7065:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 7066:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 7067:                                             $changes{$type}{$item} = 1;
 7068:                                         }
 7069:                                     }
 7070:                                 }
 7071:                             }
 7072:                             $idx ++;
 7073:                         }
 7074:                     }
 7075:                 }
 7076:             }
 7077:             if (ref($validationitemsref) eq 'ARRAY') {
 7078:                 foreach my $item (@{$validationitemsref}) {
 7079:                     if ($item eq 'fields') {
 7080:                         my @changed;
 7081:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 7082:                         if (@{$confhash{'validation'}{$item}} > 0) {
 7083:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 7084:                         }
 7085:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7086:                             if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 7087:                                 @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 7088:                                                                               $domconfig{'requestcourses'}{'validation'}{$item});
 7089:                             } else {
 7090:                                 @changed = @{$confhash{'validation'}{$item}};
 7091:                             }
 7092:                         } else {
 7093:                             @changed = @{$confhash{'validation'}{$item}};
 7094:                         }
 7095:                         if (@changed) {
 7096:                             if ($confhash{'validation'}{$item}) {
 7097:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 7098:                             } else {
 7099:                                 $changes{'validation'}{$item} = &mt('None');
 7100:                             }
 7101:                         }
 7102:                     } else {
 7103:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 7104:                         if ($item eq 'markup') {
 7105:                             if ($env{'form.requestcourses_validation_'.$item}) {
 7106:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 7107:                             }
 7108:                         }
 7109:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7110:                             if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 7111:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7112:                             }
 7113:                         } else {
 7114:                             if ($confhash{'validation'}{$item} ne '') {
 7115:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7116:                             }
 7117:                         }
 7118:                     }
 7119:                 }
 7120:             }
 7121:             if ($env{'form.validationdc'}) {
 7122:                 my $newval = $env{'form.validationdc'};
 7123:                 my %domcoords = &get_active_dcs($dom);
 7124:                 if (exists($domcoords{$newval})) {
 7125:                     $confhash{'validation'}{'dc'} = $newval;
 7126:                 }
 7127:             }
 7128:             if (ref($confhash{'validation'}) eq 'HASH') {
 7129:                 if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7130:                     if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7131:                         unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7132:                             if ($confhash{'validation'}{'dc'} eq '') {
 7133:                                 $changes{'validation'}{'dc'} = &mt('None');
 7134:                             } else {
 7135:                                 $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7136:                             }
 7137:                         }
 7138:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 7139:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7140:                     }
 7141:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 7142:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7143:                 }
 7144:             } elsif (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7145:                 if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7146:                     $changes{'validation'}{'dc'} = &mt('None');
 7147:                 }
 7148:             }
 7149:         }
 7150:     } else {
 7151:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 7152:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 7153:     }
 7154:     foreach my $item (@usertools) {
 7155:         foreach my $type (@{$types},'default','_LC_adv') {
 7156:             my $unset; 
 7157:             if ($context eq 'requestcourses') {
 7158:                 $unset = '0';
 7159:                 if ($type eq '_LC_adv') {
 7160:                     $unset = '';
 7161:                 }
 7162:                 if ($confhash{$item}{$type} eq 'autolimit') {
 7163:                     $confhash{$item}{$type} .= '=';
 7164:                     unless ($limithash{$item}{$type} =~ /\D/) {
 7165:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 7166:                     }
 7167:                 }
 7168:             } elsif ($context eq 'requestauthor') {
 7169:                 $unset = '0';
 7170:                 if ($type eq '_LC_adv') {
 7171:                     $unset = '';
 7172:                 }
 7173:             } else {
 7174:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 7175:                     $confhash{$item}{$type} = 1;
 7176:                 } else {
 7177:                     $confhash{$item}{$type} = 0;
 7178:                 }
 7179:             }
 7180:             if (ref($domconfig{$action}) eq 'HASH') {
 7181:                 if ($action eq 'requestauthor') {
 7182:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 7183:                         $changes{$type} = 1;
 7184:                     }
 7185:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 7186:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 7187:                         $changes{$item}{$type} = 1;
 7188:                     }
 7189:                 } else {
 7190:                     if ($context eq 'requestcourses') {
 7191:                         if ($confhash{$item}{$type} ne $unset) {
 7192:                             $changes{$item}{$type} = 1;
 7193:                         }
 7194:                     } else {
 7195:                         if (!$confhash{$item}{$type}) {
 7196:                             $changes{$item}{$type} = 1;
 7197:                         }
 7198:                     }
 7199:                 }
 7200:             } else {
 7201:                 if ($context eq 'requestcourses') {
 7202:                     if ($confhash{$item}{$type} ne $unset) {
 7203:                         $changes{$item}{$type} = 1;
 7204:                     }
 7205:                 } elsif ($context eq 'requestauthor') {
 7206:                     if ($confhash{$type} ne $unset) {
 7207:                         $changes{$type} = 1;
 7208:                     }
 7209:                 } else {
 7210:                     if (!$confhash{$item}{$type}) {
 7211:                         $changes{$item}{$type} = 1;
 7212:                     }
 7213:                 }
 7214:             }
 7215:         }
 7216:     }
 7217:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7218:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 7219:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7220:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 7221:                     if (exists($confhash{'defaultquota'}{$key})) {
 7222:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 7223:                             $changes{'defaultquota'}{$key} = 1;
 7224:                         }
 7225:                     } else {
 7226:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 7227:                     }
 7228:                 }
 7229:             } else {
 7230:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 7231:                     if (exists($confhash{'defaultquota'}{$key})) {
 7232:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 7233:                             $changes{'defaultquota'}{$key} = 1;
 7234:                         }
 7235:                     } else {
 7236:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 7237:                     }
 7238:                 }
 7239:             }
 7240:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7241:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 7242:                     if (exists($confhash{'authorquota'}{$key})) {
 7243:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 7244:                             $changes{'authorquota'}{$key} = 1;
 7245:                         }
 7246:                     } else {
 7247:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 7248:                     }
 7249:                 }
 7250:             }
 7251:         }
 7252:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 7253:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 7254:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7255:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7256:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 7257:                             $changes{'defaultquota'}{$key} = 1;
 7258:                         }
 7259:                     } else {
 7260:                         if (!exists($domconfig{'quotas'}{$key})) {
 7261:                             $changes{'defaultquota'}{$key} = 1;
 7262:                         }
 7263:                     }
 7264:                 } else {
 7265:                     $changes{'defaultquota'}{$key} = 1;
 7266:                 }
 7267:             }
 7268:         }
 7269:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 7270:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 7271:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7272:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7273:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 7274:                             $changes{'authorquota'}{$key} = 1;
 7275:                         }
 7276:                     } else {
 7277:                         $changes{'authorquota'}{$key} = 1;
 7278:                     }
 7279:                 } else {
 7280:                     $changes{'authorquota'}{$key} = 1;
 7281:                 }
 7282:             }
 7283:         }
 7284:     }
 7285: 
 7286:     if ($context eq 'requestauthor') {
 7287:         $domdefaults{'requestauthor'} = \%confhash;
 7288:     } else {
 7289:         foreach my $key (keys(%confhash)) {
 7290:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 7291:                 $domdefaults{$key} = $confhash{$key};
 7292:             }
 7293:         }
 7294:     }
 7295: 
 7296:     my %quotahash = (
 7297:                       $action => { %confhash }
 7298:                     );
 7299:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 7300:                                              $dom);
 7301:     if ($putresult eq 'ok') {
 7302:         if (keys(%changes) > 0) {
 7303:             my $cachetime = 24*60*60;
 7304:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7305:             if (ref($lastactref) eq 'HASH') {
 7306:                 $lastactref->{'domdefaults'} = 1;
 7307:             }
 7308:             $resulttext = &mt('Changes made:').'<ul>';
 7309:             unless (($context eq 'requestcourses') ||
 7310:                     ($context eq 'requestauthor')) {
 7311:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 7312:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 7313:                     foreach my $type (@{$types},'default') {
 7314:                         if (defined($changes{'defaultquota'}{$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{'defaultquota'}{$type}).'</li>';
 7320:                         }
 7321:                     }
 7322:                     $resulttext .= '</ul></li>';
 7323:                 }
 7324:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 7325:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 7326:                     foreach my $type (@{$types},'default') {
 7327:                         if (defined($changes{'authorquota'}{$type})) {
 7328:                             my $typetitle = $usertypes->{$type};
 7329:                             if ($type eq 'default') {
 7330:                                 $typetitle = $othertitle;
 7331:                             }
 7332:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 7333:                         }
 7334:                     }
 7335:                     $resulttext .= '</ul></li>';
 7336:                 }
 7337:             }
 7338:             my %newenv;
 7339:             foreach my $item (@usertools) {
 7340:                 my (%haschgs,%inconf);
 7341:                 if ($context eq 'requestauthor') {
 7342:                     %haschgs = %changes;
 7343:                     %inconf = %confhash;
 7344:                 } else {
 7345:                     if (ref($changes{$item}) eq 'HASH') {
 7346:                         %haschgs = %{$changes{$item}};
 7347:                     }
 7348:                     if (ref($confhash{$item}) eq 'HASH') {
 7349:                         %inconf = %{$confhash{$item}};
 7350:                     }
 7351:                 }
 7352:                 if (keys(%haschgs) > 0) {
 7353:                     my $newacc = 
 7354:                         &Apache::lonnet::usertools_access($env{'user.name'},
 7355:                                                           $env{'user.domain'},
 7356:                                                           $item,'reload',$context);
 7357:                     if (($context eq 'requestcourses') ||
 7358:                         ($context eq 'requestauthor')) {
 7359:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 7360:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 7361:                         }
 7362:                     } else {
 7363:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 7364:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 7365:                         }
 7366:                     }
 7367:                     unless ($context eq 'requestauthor') {
 7368:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 7369:                     }
 7370:                     foreach my $type (@{$types},'default','_LC_adv') {
 7371:                         if ($haschgs{$type}) {
 7372:                             my $typetitle = $usertypes->{$type};
 7373:                             if ($type eq 'default') {
 7374:                                 $typetitle = $othertitle;
 7375:                             } elsif ($type eq '_LC_adv') {
 7376:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 7377:                             }
 7378:                             if ($inconf{$type}) {
 7379:                                 if ($context eq 'requestcourses') {
 7380:                                     my $cond;
 7381:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 7382:                                         if ($1 eq '') {
 7383:                                             $cond = &mt('(Automatic processing of any request).');
 7384:                                         } else {
 7385:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 7386:                                         }
 7387:                                     } else { 
 7388:                                         $cond = $conditions{$inconf{$type}};
 7389:                                     }
 7390:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 7391:                                 } elsif ($context eq 'requestauthor') {
 7392:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 7393:                                                              $titles{$inconf{$type}},$typetitle);
 7394: 
 7395:                                 } else {
 7396:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7397:                                 }
 7398:                             } else {
 7399:                                 if ($type eq '_LC_adv') {
 7400:                                     if ($inconf{$type} eq '0') {
 7401:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7402:                                     } else { 
 7403:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7404:                                     }
 7405:                                 } else {
 7406:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7407:                                 }
 7408:                             }
 7409:                         }
 7410:                     }
 7411:                     unless ($context eq 'requestauthor') {
 7412:                         $resulttext .= '</ul></li>';
 7413:                     }
 7414:                 }
 7415:             }
 7416:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7417:                 if (ref($changes{'notify'}) eq 'HASH') {
 7418:                     if ($changes{'notify'}{'approval'}) {
 7419:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7420:                             if ($confhash{'notify'}{'approval'}) {
 7421:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7422:                             } else {
 7423:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7424:                             }
 7425:                         }
 7426:                     }
 7427:                 }
 7428:             }
 7429:             if ($action eq 'requestcourses') {
 7430:                 my @offon = ('off','on');
 7431:                 if ($changes{'uniquecode'}) {
 7432:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7433:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7434:                         $resulttext .= '<li>'.
 7435:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7436:                                        '</li>';
 7437:                     } else {
 7438:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7439:                                        '</li>';
 7440:                     }
 7441:                 }
 7442:                 foreach my $type ('textbooks','templates') {
 7443:                     if (ref($changes{$type}) eq 'HASH') {
 7444:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 7445:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 7446:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 7447:                             my $coursetitle = $coursehash{'description'};
 7448:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 7449:                             $resulttext .= '<li>';
 7450:                             foreach my $item ('subject','title','publisher','author') {
 7451:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7452:                                          ($type eq 'templates'));
 7453:                                 my $name = $item.':';
 7454:                                 $name =~ s/^(\w)/\U$1/;
 7455:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 7456:                             }
 7457:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7458:                             if ($type eq 'textbooks') {
 7459:                                 if ($confhash{$type}{$key}{'image'}) {
 7460:                                     $resulttext .= ' '.&mt('Image: [_1]',
 7461:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 7462:                                                    ' alt="Textbook cover" />').'<br />';
 7463:                                 }
 7464:                             }
 7465:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7466:                         }
 7467:                         $resulttext .= '</ul></li>';
 7468:                     }
 7469:                 }
 7470:                 if (ref($changes{'validation'}) eq 'HASH') {
 7471:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7472:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7473:                         foreach my $item (@{$validationitemsref}) {
 7474:                             if (exists($changes{'validation'}{$item})) {
 7475:                                 if ($item eq 'markup') {
 7476:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7477:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7478:                                 } else {
 7479:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7480:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7481:                                 }
 7482:                             }
 7483:                         }
 7484:                         if (exists($changes{'validation'}{'dc'})) {
 7485:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7486:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7487:                         }
 7488:                     }
 7489:                 }
 7490:             }
 7491:             $resulttext .= '</ul>';
 7492:             if (keys(%newenv)) {
 7493:                 &Apache::lonnet::appenv(\%newenv);
 7494:             }
 7495:         } else {
 7496:             if ($context eq 'requestcourses') {
 7497:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7498:             } elsif ($context eq 'requestauthor') {
 7499:                 $resulttext = &mt('No changes made to rights to request author space.');
 7500:             } else {
 7501:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7502:             }
 7503:         }
 7504:     } else {
 7505:         $resulttext = '<span class="LC_error">'.
 7506: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7507:     }
 7508:     if ($errors) {
 7509:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7510:                        '<ul>'.$errors.'</ul></p>';
 7511:     }
 7512:     return $resulttext;
 7513: }
 7514: 
 7515: sub process_textbook_image {
 7516:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 7517:     my $filename = $env{'form.'.$caller.'.filename'};
 7518:     my ($error,$url);
 7519:     my ($width,$height) = (50,50);
 7520:     if ($configuserok eq 'ok') {
 7521:         if ($switchserver) {
 7522:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7523:                          $switchserver);
 7524:         } elsif ($author_ok eq 'ok') {
 7525:             my ($result,$imageurl) =
 7526:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7527:                              "$type/$dom/$cnum/cover",$width,$height);
 7528:             if ($result eq 'ok') {
 7529:                 $url = $imageurl;
 7530:             } else {
 7531:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7532:             }
 7533:         } else {
 7534:             $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);
 7535:         }
 7536:     } else {
 7537:         $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);
 7538:     }
 7539:     return ($url,$error);
 7540: }
 7541: 
 7542: sub modify_autoenroll {
 7543:     my ($dom,$lastactref,%domconfig) = @_;
 7544:     my ($resulttext,%changes);
 7545:     my %currautoenroll;
 7546:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7547:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7548:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7549:         }
 7550:     }
 7551:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7552:     my %title = ( run => 'Auto-enrollment active',
 7553:                   sender => 'Sender for notification messages',
 7554:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7555:     my @offon = ('off','on');
 7556:     my $sender_uname = $env{'form.sender_uname'};
 7557:     my $sender_domain = $env{'form.sender_domain'};
 7558:     if ($sender_domain eq '') {
 7559:         $sender_uname = '';
 7560:     } elsif ($sender_uname eq '') {
 7561:         $sender_domain = '';
 7562:     }
 7563:     my $coowners = $env{'form.autoassign_coowners'};
 7564:     my %autoenrollhash =  (
 7565:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7566:                                        'sender_uname' => $sender_uname,
 7567:                                        'sender_domain' => $sender_domain,
 7568:                                        'co-owners' => $coowners,
 7569:                                 }
 7570:                      );
 7571:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7572:                                              $dom);
 7573:     if ($putresult eq 'ok') {
 7574:         if (exists($currautoenroll{'run'})) {
 7575:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7576:                  $changes{'run'} = 1;
 7577:              }
 7578:         } elsif ($autorun) {
 7579:             if ($env{'form.autoenroll_run'} ne '1') {
 7580:                  $changes{'run'} = 1;
 7581:             }
 7582:         }
 7583:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7584:             $changes{'sender'} = 1;
 7585:         }
 7586:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7587:             $changes{'sender'} = 1;
 7588:         }
 7589:         if ($currautoenroll{'co-owners'} ne '') {
 7590:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7591:                 $changes{'coowners'} = 1;
 7592:             }
 7593:         } elsif ($coowners) {
 7594:             $changes{'coowners'} = 1;
 7595:         }      
 7596:         if (keys(%changes) > 0) {
 7597:             $resulttext = &mt('Changes made:').'<ul>';
 7598:             if ($changes{'run'}) {
 7599:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7600:             }
 7601:             if ($changes{'sender'}) {
 7602:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7603:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7604:                 } else {
 7605:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7606:                 }
 7607:             }
 7608:             if ($changes{'coowners'}) {
 7609:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7610:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7611:                 if (ref($lastactref) eq 'HASH') {
 7612:                     $lastactref->{'domainconfig'} = 1;
 7613:                 }
 7614:             }
 7615:             $resulttext .= '</ul>';
 7616:         } else {
 7617:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7618:         }
 7619:     } else {
 7620:         $resulttext = '<span class="LC_error">'.
 7621: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7622:     }
 7623:     return $resulttext;
 7624: }
 7625: 
 7626: sub modify_autoupdate {
 7627:     my ($dom,%domconfig) = @_;
 7628:     my ($resulttext,%currautoupdate,%fields,%changes);
 7629:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7630:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7631:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7632:         }
 7633:     }
 7634:     my @offon = ('off','on');
 7635:     my %title = &Apache::lonlocal::texthash (
 7636:                    run => 'Auto-update:',
 7637:                    classlists => 'Updates to user information in classlists?'
 7638:                 );
 7639:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7640:     my %fieldtitles = &Apache::lonlocal::texthash (
 7641:                         id => 'Student/Employee ID',
 7642:                         permanentemail => 'E-mail address',
 7643:                         lastname => 'Last Name',
 7644:                         firstname => 'First Name',
 7645:                         middlename => 'Middle Name',
 7646:                         generation => 'Generation',
 7647:                       );
 7648:     $othertitle = &mt('All users');
 7649:     if (keys(%{$usertypes}) >  0) {
 7650:         $othertitle = &mt('Other users');
 7651:     }
 7652:     foreach my $key (keys(%env)) {
 7653:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7654:             my ($usertype,$item) = ($1,$2);
 7655:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7656:                 if ($usertype eq 'default') {   
 7657:                     push(@{$fields{$1}},$2);
 7658:                 } elsif (ref($types) eq 'ARRAY') {
 7659:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7660:                         push(@{$fields{$1}},$2);
 7661:                     }
 7662:                 }
 7663:             }
 7664:         }
 7665:     }
 7666:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 7667:     @lockablenames = sort(@lockablenames);
 7668:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 7669:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7670:         if (@changed) {
 7671:             $changes{'lockablenames'} = 1;
 7672:         }
 7673:     } else {
 7674:         if (@lockablenames) {
 7675:             $changes{'lockablenames'} = 1;
 7676:         }
 7677:     }
 7678:     my %updatehash = (
 7679:                       autoupdate => { run => $env{'form.autoupdate_run'},
 7680:                                       classlists => $env{'form.classlists'},
 7681:                                       fields => {%fields},
 7682:                                       lockablenames => \@lockablenames,
 7683:                                     }
 7684:                      );
 7685:     foreach my $key (keys(%currautoupdate)) {
 7686:         if (($key eq 'run') || ($key eq 'classlists')) {
 7687:             if (exists($updatehash{autoupdate}{$key})) {
 7688:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 7689:                     $changes{$key} = 1;
 7690:                 }
 7691:             }
 7692:         } elsif ($key eq 'fields') {
 7693:             if (ref($currautoupdate{$key}) eq 'HASH') {
 7694:                 foreach my $item (@{$types},'default') {
 7695:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 7696:                         my $change = 0;
 7697:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 7698:                             if (!exists($fields{$item})) {
 7699:                                 $change = 1;
 7700:                                 last;
 7701:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 7702:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 7703:                                     $change = 1;
 7704:                                     last;
 7705:                                 }
 7706:                             }
 7707:                         }
 7708:                         if ($change) {
 7709:                             push(@{$changes{$key}},$item);
 7710:                         }
 7711:                     } 
 7712:                 }
 7713:             }
 7714:         } elsif ($key eq 'lockablenames') {
 7715:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 7716:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7717:                 if (@changed) {
 7718:                     $changes{'lockablenames'} = 1;
 7719:                 }
 7720:             } else {
 7721:                 if (@lockablenames) {
 7722:                     $changes{'lockablenames'} = 1;
 7723:                 }
 7724:             }
 7725:         }
 7726:     }
 7727:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 7728:         if (@lockablenames) {
 7729:             $changes{'lockablenames'} = 1;
 7730:         }
 7731:     }
 7732:     foreach my $item (@{$types},'default') {
 7733:         if (defined($fields{$item})) {
 7734:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 7735:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 7736:                     my $change = 0;
 7737:                     if (ref($fields{$item}) eq 'ARRAY') {
 7738:                         foreach my $type (@{$fields{$item}}) {
 7739:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 7740:                                 $change = 1;
 7741:                                 last;
 7742:                             }
 7743:                         }
 7744:                     }
 7745:                     if ($change) {
 7746:                         push(@{$changes{'fields'}},$item);
 7747:                     }
 7748:                 } else {
 7749:                     push(@{$changes{'fields'}},$item);
 7750:                 }
 7751:             } else {
 7752:                 push(@{$changes{'fields'}},$item);
 7753:             }
 7754:         }
 7755:     }
 7756:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7757:                                              $dom);
 7758:     if ($putresult eq 'ok') {
 7759:         if (keys(%changes) > 0) {
 7760:             $resulttext = &mt('Changes made:').'<ul>';
 7761:             foreach my $key (sort(keys(%changes))) {
 7762:                 if ($key eq 'lockablenames') {
 7763:                     $resulttext .= '<li>';
 7764:                     if (@lockablenames) {
 7765:                         $usertypes->{'default'} = $othertitle;
 7766:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7767:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7768:                     } else {
 7769:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7770:                     }
 7771:                     $resulttext .= '</li>';
 7772:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7773:                     foreach my $item (@{$changes{$key}}) {
 7774:                         my @newvalues;
 7775:                         foreach my $type (@{$fields{$item}}) {
 7776:                             push(@newvalues,$fieldtitles{$type});
 7777:                         }
 7778:                         my $newvaluestr;
 7779:                         if (@newvalues > 0) {
 7780:                             $newvaluestr = join(', ',@newvalues);
 7781:                         } else {
 7782:                             $newvaluestr = &mt('none');
 7783:                         }
 7784:                         if ($item eq 'default') {
 7785:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7786:                         } else {
 7787:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7788:                         }
 7789:                     }
 7790:                 } else {
 7791:                     my $newvalue;
 7792:                     if ($key eq 'run') {
 7793:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7794:                     } else {
 7795:                         $newvalue = $offon[$env{'form.'.$key}];
 7796:                     }
 7797:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7798:                 }
 7799:             }
 7800:             $resulttext .= '</ul>';
 7801:         } else {
 7802:             $resulttext = &mt('No changes made to autoupdates');
 7803:         }
 7804:     } else {
 7805:         $resulttext = '<span class="LC_error">'.
 7806: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7807:     }
 7808:     return $resulttext;
 7809: }
 7810: 
 7811: sub modify_autocreate {
 7812:     my ($dom,%domconfig) = @_;
 7813:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7814:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7815:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7816:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7817:         }
 7818:     }
 7819:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7820:                  req => 'Auto-creation of validated requests for official courses',
 7821:                  xmldc => 'Identity of course creator of courses from XML files',
 7822:                );
 7823:     my @types = ('xml','req');
 7824:     foreach my $item (@types) {
 7825:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7826:         $newvals{$item} =~ s/\D//g;
 7827:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7828:     }
 7829:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7830:     my %domcoords = &get_active_dcs($dom);
 7831:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7832:         $newvals{'xmldc'} = '';
 7833:     } 
 7834:     %autocreatehash =  (
 7835:                         autocreate => { xml => $newvals{'xml'},
 7836:                                         req => $newvals{'req'},
 7837:                                       }
 7838:                        );
 7839:     if ($newvals{'xmldc'} ne '') {
 7840:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7841:     }
 7842:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7843:                                              $dom);
 7844:     if ($putresult eq 'ok') {
 7845:         my @items = @types;
 7846:         if ($newvals{'xml'}) {
 7847:             push(@items,'xmldc');
 7848:         }
 7849:         foreach my $item (@items) {
 7850:             if (exists($currautocreate{$item})) {
 7851:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7852:                     $changes{$item} = 1;
 7853:                 }
 7854:             } elsif ($newvals{$item}) {
 7855:                 $changes{$item} = 1;
 7856:             }
 7857:         }
 7858:         if (keys(%changes) > 0) {
 7859:             my @offon = ('off','on'); 
 7860:             $resulttext = &mt('Changes made:').'<ul>';
 7861:             foreach my $item (@types) {
 7862:                 if ($changes{$item}) {
 7863:                     my $newtxt = $offon[$newvals{$item}];
 7864:                     $resulttext .= '<li>'.
 7865:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7866:                                        '<b>','</b>').
 7867:                                    '</li>';
 7868:                 }
 7869:             }
 7870:             if ($changes{'xmldc'}) {
 7871:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7872:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7873:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7874:             }
 7875:             $resulttext .= '</ul>';
 7876:         } else {
 7877:             $resulttext = &mt('No changes made to auto-creation settings');
 7878:         }
 7879:     } else {
 7880:         $resulttext = '<span class="LC_error">'.
 7881:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7882:     }
 7883:     return $resulttext;
 7884: }
 7885: 
 7886: sub modify_directorysrch {
 7887:     my ($dom,%domconfig) = @_;
 7888:     my ($resulttext,%changes);
 7889:     my %currdirsrch;
 7890:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7891:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7892:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7893:         }
 7894:     }
 7895:     my %title = ( available => 'Directory search available',
 7896:                   localonly => 'Other domains can search',
 7897:                   searchby => 'Search types',
 7898:                   searchtypes => 'Search latitude');
 7899:     my @offon = ('off','on');
 7900:     my @otherdoms = ('Yes','No');
 7901: 
 7902:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7903:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7904:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7905: 
 7906:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7907:     if (keys(%{$usertypes}) == 0) {
 7908:         @cansearch = ('default');
 7909:     } else {
 7910:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7911:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7912:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7913:                     push(@{$changes{'cansearch'}},$type);
 7914:                 }
 7915:             }
 7916:             foreach my $type (@cansearch) {
 7917:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7918:                     push(@{$changes{'cansearch'}},$type);
 7919:                 }
 7920:             }
 7921:         } else {
 7922:             push(@{$changes{'cansearch'}},@cansearch);
 7923:         }
 7924:     }
 7925: 
 7926:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7927:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7928:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7929:                 push(@{$changes{'searchby'}},$by);
 7930:             }
 7931:         }
 7932:         foreach my $by (@searchby) {
 7933:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7934:                 push(@{$changes{'searchby'}},$by);
 7935:             }
 7936:         }
 7937:     } else {
 7938:         push(@{$changes{'searchby'}},@searchby);
 7939:     }
 7940: 
 7941:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7942:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7943:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7944:                 push(@{$changes{'searchtypes'}},$type);
 7945:             }
 7946:         }
 7947:         foreach my $type (@searchtypes) {
 7948:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7949:                 push(@{$changes{'searchtypes'}},$type);
 7950:             }
 7951:         }
 7952:     } else {
 7953:         if (exists($currdirsrch{'searchtypes'})) {
 7954:             foreach my $type (@searchtypes) {  
 7955:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7956:                     push(@{$changes{'searchtypes'}},$type);
 7957:                 }
 7958:             }
 7959:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7960:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7961:             }   
 7962:         } else {
 7963:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7964:         }
 7965:     }
 7966: 
 7967:     my %dirsrch_hash =  (
 7968:             directorysrch => { available => $env{'form.dirsrch_available'},
 7969:                                cansearch => \@cansearch,
 7970:                                localonly => $env{'form.dirsrch_localonly'},
 7971:                                searchby => \@searchby,
 7972:                                searchtypes => \@searchtypes,
 7973:                              }
 7974:             );
 7975:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7976:                                              $dom);
 7977:     if ($putresult eq 'ok') {
 7978:         if (exists($currdirsrch{'available'})) {
 7979:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7980:                  $changes{'available'} = 1;
 7981:              }
 7982:         } else {
 7983:             if ($env{'form.dirsrch_available'} eq '1') {
 7984:                 $changes{'available'} = 1;
 7985:             }
 7986:         }
 7987:         if (exists($currdirsrch{'localonly'})) {
 7988:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7989:                  $changes{'localonly'} = 1;
 7990:              }
 7991:         } else {
 7992:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7993:                 $changes{'localonly'} = 1;
 7994:             }
 7995:         }
 7996:         if (keys(%changes) > 0) {
 7997:             $resulttext = &mt('Changes made:').'<ul>';
 7998:             if ($changes{'available'}) {
 7999:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 8000:             }
 8001:             if ($changes{'localonly'}) {
 8002:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 8003:             }
 8004: 
 8005:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 8006:                 my $chgtext;
 8007:                 if (ref($usertypes) eq 'HASH') {
 8008:                     if (keys(%{$usertypes}) > 0) {
 8009:                         foreach my $type (@{$types}) {
 8010:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 8011:                                 $chgtext .= $usertypes->{$type}.'; ';
 8012:                             }
 8013:                         }
 8014:                         if (grep(/^default$/,@cansearch)) {
 8015:                             $chgtext .= $othertitle;
 8016:                         } else {
 8017:                             $chgtext =~ s/\; $//;
 8018:                         }
 8019:                         $resulttext .=
 8020:                             '<li>'.
 8021:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 8022:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 8023:                             '</li>';
 8024:                     }
 8025:                 }
 8026:             }
 8027:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 8028:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 8029:                 my $chgtext;
 8030:                 foreach my $type (@{$titleorder}) {
 8031:                     if (grep(/^\Q$type\E$/,@searchby)) {
 8032:                         if (defined($searchtitles->{$type})) {
 8033:                             $chgtext .= $searchtitles->{$type}.'; ';
 8034:                         }
 8035:                     }
 8036:                 }
 8037:                 $chgtext =~ s/\; $//;
 8038:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 8039:             }
 8040:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 8041:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 8042:                 my $chgtext;
 8043:                 foreach my $type (@{$srchtypeorder}) {
 8044:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 8045:                         if (defined($srchtypes_desc->{$type})) {
 8046:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 8047:                         }
 8048:                     }
 8049:                 }
 8050:                 $chgtext =~ s/\; $//;
 8051:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 8052:             }
 8053:             $resulttext .= '</ul>';
 8054:         } else {
 8055:             $resulttext = &mt('No changes made to institution directory search settings');
 8056:         }
 8057:     } else {
 8058:         $resulttext = '<span class="LC_error">'.
 8059:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 8060:     }
 8061:     return $resulttext;
 8062: }
 8063: 
 8064: sub modify_contacts {
 8065:     my ($dom,$lastactref,%domconfig) = @_;
 8066:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 8067:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 8068:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 8069:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 8070:         }
 8071:     }
 8072:     my (%others,%to,%bcc);
 8073:     my @contacts = ('supportemail','adminemail');
 8074:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 8075:                     'requestsmail','updatesmail','idconflictsmail');
 8076:     my @toggles = ('reporterrors','reportupdates');
 8077:     foreach my $type (@mailings) {
 8078:         @{$newsetting{$type}} = 
 8079:             &Apache::loncommon::get_env_multiple('form.'.$type);
 8080:         foreach my $item (@contacts) {
 8081:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 8082:                 $contacts_hash{contacts}{$type}{$item} = 1;
 8083:             } else {
 8084:                 $contacts_hash{contacts}{$type}{$item} = 0;
 8085:             }
 8086:         }  
 8087:         $others{$type} = $env{'form.'.$type.'_others'};
 8088:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 8089:         if ($type eq 'helpdeskmail') {
 8090:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 8091:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 8092:         }
 8093:     }
 8094:     foreach my $item (@contacts) {
 8095:         $to{$item} = $env{'form.'.$item};
 8096:         $contacts_hash{'contacts'}{$item} = $to{$item};
 8097:     }
 8098:     foreach my $item (@toggles) {
 8099:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 8100:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 8101:         }
 8102:     }
 8103:     if (keys(%currsetting) > 0) {
 8104:         foreach my $item (@contacts) {
 8105:             if ($to{$item} ne $currsetting{$item}) {
 8106:                 $changes{$item} = 1;
 8107:             }
 8108:         }
 8109:         foreach my $type (@mailings) {
 8110:             foreach my $item (@contacts) {
 8111:                 if (ref($currsetting{$type}) eq 'HASH') {
 8112:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 8113:                         push(@{$changes{$type}},$item);
 8114:                     }
 8115:                 } else {
 8116:                     push(@{$changes{$type}},@{$newsetting{$type}});
 8117:                 }
 8118:             }
 8119:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 8120:                 push(@{$changes{$type}},'others');
 8121:             }
 8122:             if ($type eq 'helpdeskmail') {   
 8123:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 8124:                     push(@{$changes{$type}},'bcc'); 
 8125:                 }
 8126:             }
 8127:         }
 8128:     } else {
 8129:         my %default;
 8130:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 8131:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 8132:         $default{'errormail'} = 'adminemail';
 8133:         $default{'packagesmail'} = 'adminemail';
 8134:         $default{'helpdeskmail'} = 'supportemail';
 8135:         $default{'lonstatusmail'} = 'adminemail';
 8136:         $default{'requestsmail'} = 'adminemail';
 8137:         $default{'updatesmail'} = 'adminemail';
 8138:         foreach my $item (@contacts) {
 8139:            if ($to{$item} ne $default{$item}) {
 8140:               $changes{$item} = 1;
 8141:            }
 8142:         }
 8143:         foreach my $type (@mailings) {
 8144:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 8145:                
 8146:                 push(@{$changes{$type}},@{$newsetting{$type}});
 8147:             }
 8148:             if ($others{$type} ne '') {
 8149:                 push(@{$changes{$type}},'others');
 8150:             }
 8151:             if ($type eq 'helpdeskmail') {
 8152:                 if ($bcc{$type} ne '') {
 8153:                     push(@{$changes{$type}},'bcc');
 8154:                 }
 8155:             }
 8156:         }
 8157:     }
 8158:     foreach my $item (@toggles) {
 8159:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 8160:             $changes{$item} = 1;
 8161:         } elsif ((!$env{'form.'.$item}) &&
 8162:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 8163:             $changes{$item} = 1;
 8164:         }
 8165:     }
 8166:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 8167:                                              $dom);
 8168:     if ($putresult eq 'ok') {
 8169:         if (keys(%changes) > 0) {
 8170:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8171:             if (ref($lastactref) eq 'HASH') {
 8172:                 $lastactref->{'domainconfig'} = 1;
 8173:             }
 8174:             my ($titles,$short_titles)  = &contact_titles();
 8175:             $resulttext = &mt('Changes made:').'<ul>';
 8176:             foreach my $item (@contacts) {
 8177:                 if ($changes{$item}) {
 8178:                     $resulttext .= '<li>'.$titles->{$item}.
 8179:                                     &mt(' set to: ').
 8180:                                     '<span class="LC_cusr_emph">'.
 8181:                                     $to{$item}.'</span></li>';
 8182:                 }
 8183:             }
 8184:             foreach my $type (@mailings) {
 8185:                 if (ref($changes{$type}) eq 'ARRAY') {
 8186:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 8187:                     my @text;
 8188:                     foreach my $item (@{$newsetting{$type}}) {
 8189:                         push(@text,$short_titles->{$item});
 8190:                     }
 8191:                     if ($others{$type} ne '') {
 8192:                         push(@text,$others{$type});
 8193:                     }
 8194:                     $resulttext .= '<span class="LC_cusr_emph">'.
 8195:                                    join(', ',@text).'</span>';
 8196:                     if ($type eq 'helpdeskmail') {
 8197:                         if ($bcc{$type} ne '') {
 8198:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 8199:                         }
 8200:                     }
 8201:                     $resulttext .= '</li>';
 8202:                 }
 8203:             }
 8204:             my @offon = ('off','on');
 8205:             if ($changes{'reporterrors'}) {
 8206:                 $resulttext .= '<li>'.
 8207:                                &mt('E-mail error reports to [_1] set to "'.
 8208:                                    $offon[$env{'form.reporterrors'}].'".',
 8209:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8210:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 8211:                                '</li>';
 8212:             }
 8213:             if ($changes{'reportupdates'}) {
 8214:                 $resulttext .= '<li>'.
 8215:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 8216:                                     $offon[$env{'form.reportupdates'}].'".',
 8217:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8218:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 8219:                                 '</li>';
 8220:             }
 8221:             $resulttext .= '</ul>';
 8222:         } else {
 8223:             $resulttext = &mt('No changes made to contact information');
 8224:         }
 8225:     } else {
 8226:         $resulttext = '<span class="LC_error">'.
 8227:             &mt('An error occurred: [_1].',$putresult).'</span>';
 8228:     }
 8229:     return $resulttext;
 8230: }
 8231: 
 8232: sub modify_usercreation {
 8233:     my ($dom,%domconfig) = @_;
 8234:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 8235:     my $warningmsg;
 8236:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8237:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8238:             if ($key eq 'cancreate') {
 8239:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8240:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8241:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8242:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 8243:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8244:                         } else {
 8245:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8246:                         }
 8247:                     }
 8248:                 }
 8249:             } elsif ($key eq 'email_rule') {
 8250:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8251:             } else {
 8252:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8253:             }
 8254:         }
 8255:     }
 8256:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 8257:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 8258:     my @contexts = ('author','course','requestcrs');
 8259:     foreach my $item(@contexts) {
 8260:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 8261:     }
 8262:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8263:         foreach my $item (@contexts) {
 8264:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 8265:                 push(@{$changes{'cancreate'}},$item);
 8266:             }
 8267:         }
 8268:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 8269:         foreach my $item (@contexts) {
 8270:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 8271:                 if ($cancreate{$item} ne 'any') {
 8272:                     push(@{$changes{'cancreate'}},$item);
 8273:                 }
 8274:             } else {
 8275:                 if ($cancreate{$item} ne 'none') {
 8276:                     push(@{$changes{'cancreate'}},$item);
 8277:                 }
 8278:             }
 8279:         }
 8280:     } else {
 8281:         foreach my $item (@contexts)  {
 8282:             push(@{$changes{'cancreate'}},$item);
 8283:         }
 8284:     }
 8285: 
 8286:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 8287:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 8288:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 8289:                 push(@{$changes{'username_rule'}},$type);
 8290:             }
 8291:         }
 8292:         foreach my $type (@username_rule) {
 8293:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 8294:                 push(@{$changes{'username_rule'}},$type);
 8295:             }
 8296:         }
 8297:     } else {
 8298:         push(@{$changes{'username_rule'}},@username_rule);
 8299:     }
 8300: 
 8301:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 8302:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 8303:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 8304:                 push(@{$changes{'id_rule'}},$type);
 8305:             }
 8306:         }
 8307:         foreach my $type (@id_rule) {
 8308:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 8309:                 push(@{$changes{'id_rule'}},$type);
 8310:             }
 8311:         }
 8312:     } else {
 8313:         push(@{$changes{'id_rule'}},@id_rule);
 8314:     }
 8315: 
 8316:     my @authen_contexts = ('author','course','domain');
 8317:     my @authtypes = ('int','krb4','krb5','loc');
 8318:     my %authhash;
 8319:     foreach my $item (@authen_contexts) {
 8320:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 8321:         foreach my $auth (@authtypes) {
 8322:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 8323:                 $authhash{$item}{$auth} = 1;
 8324:             } else {
 8325:                 $authhash{$item}{$auth} = 0;
 8326:             }
 8327:         }
 8328:     }
 8329:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 8330:         foreach my $item (@authen_contexts) {
 8331:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 8332:                 foreach my $auth (@authtypes) {
 8333:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 8334:                         push(@{$changes{'authtypes'}},$item);
 8335:                         last;
 8336:                     }
 8337:                 }
 8338:             }
 8339:         }
 8340:     } else {
 8341:         foreach my $item (@authen_contexts) {
 8342:             push(@{$changes{'authtypes'}},$item);
 8343:         }
 8344:     }
 8345: 
 8346:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 8347:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 8348:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 8349:     $save_usercreate{'id_rule'} = \@id_rule;
 8350:     $save_usercreate{'username_rule'} = \@username_rule,
 8351:     $save_usercreate{'authtypes'} = \%authhash;
 8352: 
 8353:     my %usercreation_hash =  (
 8354:         usercreation     => \%save_usercreate,
 8355:     );
 8356: 
 8357:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 8358:                                              $dom);
 8359: 
 8360:     if ($putresult eq 'ok') {
 8361:         if (keys(%changes) > 0) {
 8362:             $resulttext = &mt('Changes made:').'<ul>';
 8363:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8364:                 my %lt = &usercreation_types();
 8365:                 foreach my $type (@{$changes{'cancreate'}}) {
 8366:                     my $chgtext = $lt{$type}.', ';
 8367:                     if ($cancreate{$type} eq 'none') {
 8368:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 8369:                     } elsif ($cancreate{$type} eq 'any') {
 8370:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 8371:                     } elsif ($cancreate{$type} eq 'official') {
 8372:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 8373:                     } elsif ($cancreate{$type} eq 'unofficial') {
 8374:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 8375:                     }
 8376:                     $resulttext .= '<li>'.$chgtext.'</li>';
 8377:                 }
 8378:             }
 8379:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 8380:                 my ($rules,$ruleorder) = 
 8381:                     &Apache::lonnet::inst_userrules($dom,'username');
 8382:                 my $chgtext = '<ul>';
 8383:                 foreach my $type (@username_rule) {
 8384:                     if (ref($rules->{$type}) eq 'HASH') {
 8385:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 8386:                     }
 8387:                 }
 8388:                 $chgtext .= '</ul>';
 8389:                 if (@username_rule > 0) {
 8390:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 8391:                 } else {
 8392:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 8393:                 }
 8394:             }
 8395:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 8396:                 my ($idrules,$idruleorder) = 
 8397:                     &Apache::lonnet::inst_userrules($dom,'id');
 8398:                 my $chgtext = '<ul>';
 8399:                 foreach my $type (@id_rule) {
 8400:                     if (ref($idrules->{$type}) eq 'HASH') {
 8401:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8402:                     }
 8403:                 }
 8404:                 $chgtext .= '</ul>';
 8405:                 if (@id_rule > 0) {
 8406:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8407:                 } else {
 8408:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8409:                 }
 8410:             }
 8411:             my %authname = &authtype_names();
 8412:             my %context_title = &context_names();
 8413:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8414:                 my $chgtext = '<ul>';
 8415:                 foreach my $type (@{$changes{'authtypes'}}) {
 8416:                     my @allowed;
 8417:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8418:                     foreach my $auth (@authtypes) {
 8419:                         if ($authhash{$type}{$auth}) {
 8420:                             push(@allowed,$authname{$auth});
 8421:                         }
 8422:                     }
 8423:                     if (@allowed > 0) {
 8424:                         $chgtext .= join(', ',@allowed).'</li>';
 8425:                     } else {
 8426:                         $chgtext .= &mt('none').'</li>';
 8427:                     }
 8428:                 }
 8429:                 $chgtext .= '</ul>';
 8430:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8431:                 $resulttext .= '</li>';
 8432:             }
 8433:             $resulttext .= '</ul>';
 8434:         } else {
 8435:             $resulttext = &mt('No changes made to user creation settings');
 8436:         }
 8437:     } else {
 8438:         $resulttext = '<span class="LC_error">'.
 8439:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8440:     }
 8441:     if ($warningmsg ne '') {
 8442:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8443:     }
 8444:     return $resulttext;
 8445: }
 8446: 
 8447: sub modify_selfcreation {
 8448:     my ($dom,%domconfig) = @_;
 8449:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8450:     my (%save_usercreate,%save_usermodify);
 8451:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8452:     if (ref($types) eq 'ARRAY') {
 8453:         $usertypes->{'default'} = $othertitle;
 8454:         push(@{$types},'default');
 8455:     }
 8456: #
 8457: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8458: #
 8459:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8460:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8461:             if ($key eq 'cancreate') {
 8462:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8463:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8464:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8465:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8466:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 8467:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 8468:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8469:                         } else {
 8470:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8471:                         }
 8472:                     }
 8473:                 }
 8474:             } elsif ($key eq 'email_rule') {
 8475:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8476:             } else {
 8477:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8478:             }
 8479:         }
 8480:     }
 8481: #
 8482: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8483: #
 8484:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8485:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8486:             if ($key eq 'selfcreate') {
 8487:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8488:             } else {
 8489:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8490:             }
 8491:         }
 8492:     }
 8493: 
 8494:     my @contexts = ('selfcreate');
 8495:     @{$cancreate{'selfcreate'}} = ();
 8496:     %{$cancreate{'emailusername'}} = ();
 8497:     @{$cancreate{'statustocreate'}} = ();
 8498:     %{$cancreate{'selfcreateprocessing'}} = ();
 8499:     %{$cancreate{'shibenv'}} = ();
 8500:     my %selfcreatetypes = (
 8501:                              sso   => 'users authenticated by institutional single sign on',
 8502:                              login => 'users authenticated by institutional log-in',
 8503:                              email => 'users who provide a valid e-mail address for use as username',
 8504:                           );
 8505: #
 8506: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8507: # is permitted.
 8508: #
 8509: 
 8510:     my @statuses;
 8511:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 8512:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 8513:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 8514:         }
 8515:     }
 8516:     push(@statuses,'default');
 8517: 
 8518:     foreach my $item ('login','sso','email') {
 8519:         if ($item eq 'email') {
 8520:             if ($env{'form.cancreate_email'}) {
 8521:                 push(@{$cancreate{'selfcreate'}},'email');
 8522:                 push(@contexts,'selfcreateprocessing');
 8523:                 foreach my $type (@statuses) {
 8524:                     if ($type eq 'default') {
 8525:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 8526:                     } else { 
 8527:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 8528:                     }
 8529:                 }
 8530:             }
 8531:         } else {
 8532:             if ($env{'form.cancreate_'.$item}) {
 8533:                 push(@{$cancreate{'selfcreate'}},$item);
 8534:             }
 8535:         }
 8536:     }
 8537:     my (@email_rule,%userinfo,%savecaptcha);
 8538:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8539: #
 8540: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8541: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8542: #
 8543: 
 8544:     if ($env{'form.cancreate_email'}) {
 8545:         push(@contexts,'emailusername');
 8546:         if (ref($types) eq 'ARRAY') {
 8547:             foreach my $type (@{$types}) {
 8548:                 if (ref($infofields) eq 'ARRAY') {
 8549:                     foreach my $field (@{$infofields}) {
 8550:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8551:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8552:                         }
 8553:                     }
 8554:                 }
 8555:             }
 8556:         }
 8557: #
 8558: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8559: # queued requests for self-creation of account using e-mail address as username
 8560: #
 8561: 
 8562:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8563:         @approvalnotify = sort(@approvalnotify);
 8564:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8565:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8566:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8567:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8568:                     push(@{$changes{'cancreate'}},'notify');
 8569:                 }
 8570:             } else {
 8571:                 if ($cancreate{'notify'}{'approval'}) {
 8572:                     push(@{$changes{'cancreate'}},'notify');
 8573:                 }
 8574:             }
 8575:         } elsif ($cancreate{'notify'}{'approval'}) {
 8576:             push(@{$changes{'cancreate'}},'notify');
 8577:         }
 8578: 
 8579: #
 8580: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8581: #
 8582:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8583:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8584:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8585:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8586:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8587:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8588:                         push(@{$changes{'email_rule'}},$type);
 8589:                     }
 8590:                 }
 8591:             }
 8592:             if (@email_rule > 0) {
 8593:                 foreach my $type (@email_rule) {
 8594:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8595:                         push(@{$changes{'email_rule'}},$type);
 8596:                     }
 8597:                 }
 8598:             }
 8599:         } elsif (@email_rule > 0) {
 8600:             push(@{$changes{'email_rule'}},@email_rule);
 8601:         }
 8602:     }
 8603: #  
 8604: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 8605: # institutional log-in.
 8606: #
 8607:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8608:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8609:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8610:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8611:             $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.').' '.
 8612:                           &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.');
 8613:         }
 8614:     }
 8615:     my @fields = ('lastname','firstname','middlename','generation',
 8616:                   'permanentemail','id');
 8617:     my @shibfields = (@fields,'inststatus');
 8618:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8619: #
 8620: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8621: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8622: # may self-create accounts 
 8623: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8624: # which the user may supply, if institutional data is unavailable.
 8625: #
 8626:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8627:         if (ref($types) eq 'ARRAY') {
 8628:             if (@{$types} > 1) {
 8629:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8630:                 push(@contexts,'statustocreate');
 8631:             } else {
 8632:                 undef($cancreate{'statustocreate'});
 8633:             } 
 8634:             foreach my $type (@{$types}) {
 8635:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8636:                 foreach my $field (@fields) {
 8637:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8638:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8639:                     } else {
 8640:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8641:                     }
 8642:                 }
 8643:             }
 8644:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8645:                 foreach my $type (@{$types}) {
 8646:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8647:                         foreach my $field (@fields) {
 8648:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8649:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8650:                                 push(@{$changes{'selfcreate'}},$type);
 8651:                                 last;
 8652:                             }
 8653:                         }
 8654:                     }
 8655:                 }
 8656:             } else {
 8657:                 foreach my $type (@{$types}) {
 8658:                     push(@{$changes{'selfcreate'}},$type);
 8659:                 }
 8660:             }
 8661:         }
 8662:         foreach my $field (@shibfields) {
 8663:             if ($env{'form.shibenv_'.$field} ne '') {
 8664:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 8665:             }
 8666:         }
 8667:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8668:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 8669:                 foreach my $field (@shibfields) {
 8670:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 8671:                         push(@{$changes{'cancreate'}},'shibenv');
 8672:                     }
 8673:                 }
 8674:             } else {
 8675:                 foreach my $field (@shibfields) {
 8676:                     if ($env{'form.shibenv_'.$field}) {
 8677:                         push(@{$changes{'cancreate'}},'shibenv');
 8678:                         last;
 8679:                     }
 8680:                 }
 8681:             }
 8682:         }
 8683:     }
 8684:     foreach my $item (@contexts) {
 8685:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 8686:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 8687:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 8688:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 8689:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8690:                             push(@{$changes{'cancreate'}},$item);
 8691:                         }
 8692:                     }
 8693:                 }
 8694:             }
 8695:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8696:                 foreach my $type (@{$cancreate{$item}}) {
 8697:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 8698:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8699:                             push(@{$changes{'cancreate'}},$item);
 8700:                         }
 8701:                     }
 8702:                 }
 8703:             }
 8704:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 8705:             if (ref($cancreate{$item}) eq 'HASH') {
 8706:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 8707:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 8708:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 8709:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 8710:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8711:                                     push(@{$changes{'cancreate'}},$item);
 8712:                                 }
 8713:                             }
 8714:                         }
 8715:                     } elsif ($item eq 'selfcreateprocessing') {
 8716:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 8717:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8718:                                 push(@{$changes{'cancreate'}},$item);
 8719:                             }
 8720:                         }
 8721:                     } else {
 8722:                         if (!$cancreate{$item}{$curr}) {
 8723:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8724:                                 push(@{$changes{'cancreate'}},$item);
 8725:                             }
 8726:                         }
 8727:                     }
 8728:                 }
 8729:                 foreach my $field (keys(%{$cancreate{$item}})) {
 8730:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 8731:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 8732:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 8733:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 8734:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8735:                                         push(@{$changes{'cancreate'}},$item);
 8736:                                     }
 8737:                                 }
 8738:                             } else {
 8739:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8740:                                     push(@{$changes{'cancreate'}},$item);
 8741:                                 }
 8742:                             }
 8743:                         }
 8744:                     } elsif ($item eq 'selfcreateprocessing') {
 8745:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 8746:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8747:                                 push(@{$changes{'cancreate'}},$item);
 8748:                             }
 8749:                         }
 8750:                     } else {
 8751:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 8752:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8753:                                 push(@{$changes{'cancreate'}},$item);
 8754:                             }
 8755:                         }
 8756:                     }
 8757:                 }
 8758:             }
 8759:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 8760:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8761:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 8762:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8763:                         push(@{$changes{'cancreate'}},$item);
 8764:                     }
 8765:                 }
 8766:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 8767:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 8768:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8769:                         push(@{$changes{'cancreate'}},$item);
 8770:                     }
 8771:                 }
 8772:             }
 8773:         } elsif ($item eq 'emailusername') {
 8774:             if (ref($cancreate{$item}) eq 'HASH') {
 8775:                 foreach my $type (keys(%{$cancreate{$item}})) {
 8776:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 8777:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 8778:                             if ($cancreate{$item}{$type}{$field}) {
 8779:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8780:                                     push(@{$changes{'cancreate'}},$item);
 8781:                                 }
 8782:                                 last;
 8783:                             }
 8784:                         }
 8785:                     }
 8786:                 }
 8787:             }
 8788:         }
 8789:     }
 8790: #
 8791: # Populate %save_usercreate hash with updates to self-creation configuration.
 8792: #
 8793:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 8794:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 8795:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 8796:     if (ref($cancreate{'notify'}) eq 'HASH') {
 8797:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 8798:     }
 8799:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
 8800:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
 8801:     }
 8802:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8803:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 8804:     }
 8805:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
 8806:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
 8807:     }
 8808:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 8809:     $save_usercreate{'emailrule'} = \@email_rule;
 8810: 
 8811:     my %userconfig_hash = (
 8812:             usercreation     => \%save_usercreate,
 8813:             usermodification => \%save_usermodify,
 8814:     );
 8815:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8816:                                              $dom);
 8817: #
 8818: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8819: #
 8820:     if ($putresult eq 'ok') {
 8821:         if (keys(%changes) > 0) {
 8822:             $resulttext = &mt('Changes made:').'<ul>';
 8823:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8824:                 my %lt = &selfcreation_types();
 8825:                 foreach my $type (@{$changes{'cancreate'}}) {
 8826:                     my $chgtext;
 8827:                     if ($type eq 'selfcreate') {
 8828:                         if (@{$cancreate{$type}} == 0) {
 8829:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 8830:                         } else {
 8831:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 8832:                                         '<ul>';
 8833:                             foreach my $case (@{$cancreate{$type}}) {
 8834:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 8835:                             }
 8836:                             $chgtext .= '</ul>';
 8837:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 8838:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 8839:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8840:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 8841:                                             $chgtext .= '<br />'.
 8842:                                                         '<span class="LC_warning">'.
 8843:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8844:                                                         '</span>';
 8845:                                         }
 8846:                                     }
 8847:                                 }
 8848:                             }
 8849:                         }
 8850:                     } elsif ($type eq 'shibenv') {
 8851:                         if (keys(%{$cancreate{$type}}) == 0) {
 8852:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
 8853:                         } else {
 8854:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
 8855:                                         '<ul>';
 8856:                             foreach my $field (@shibfields) {
 8857:                                 next if ($cancreate{$type}{$field} eq '');
 8858:                                 if ($field eq 'inststatus') {
 8859:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
 8860:                                 } else {
 8861:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
 8862:                                 }
 8863:                             }
 8864:                             $chgtext .= '</ul>';
 8865:                         }  
 8866:                     } elsif ($type eq 'statustocreate') {
 8867:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 8868:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 8869:                             if (@{$cancreate{'selfcreate'}} > 0) {
 8870:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 8871:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 8872:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 8873:                                         $chgtext .= '<br />'.
 8874:                                                     '<span class="LC_warning">'.
 8875:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8876:                                                     '</span>';
 8877:                                     }
 8878:                                 } elsif (ref($usertypes) eq 'HASH') {
 8879:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8880:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 8881:                                     } else {
 8882:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 8883:                                     }
 8884:                                     $chgtext .= '<ul>';
 8885:                                     foreach my $case (@{$cancreate{$type}}) {
 8886:                                         if ($case eq 'default') {
 8887:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 8888:                                         } else {
 8889:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 8890:                                         }
 8891:                                     }
 8892:                                     $chgtext .= '</ul>';
 8893:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8894:                                         $chgtext .= '<br /><span class="LC_warning">'.
 8895:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 8896:                                                     '</span>';
 8897:                                     }
 8898:                                 }
 8899:                             } else {
 8900:                                 if (@{$cancreate{$type}} == 0) {
 8901:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8902:                                 } else {
 8903:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8904:                                 }
 8905:                             }
 8906:                         }
 8907:                     } elsif ($type eq 'selfcreateprocessing') {
 8908:                         my %choices = &Apache::lonlocal::texthash (
 8909:                                                                     automatic => 'Automatic approval',
 8910:                                                                     approval  => 'Queued for approval',
 8911:                                                                   );
 8912:                         if (@statuses > 1) {
 8913:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
 8914:                                         '<ul>';
 8915:                            foreach my $type (@statuses) {
 8916:                                if ($type eq 'default') {
 8917:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8918:                                } else {
 8919:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8920:                                }
 8921:                            }
 8922:                            $chgtext .= '</ul>';
 8923:                         } else {
 8924:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
 8925:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
 8926:                         }
 8927:                     } elsif ($type eq 'captcha') {
 8928:                         if ($savecaptcha{$type} eq 'notused') {
 8929:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8930:                         } else {
 8931:                             my %captchas = &captcha_phrases();
 8932:                             if ($captchas{$savecaptcha{$type}}) {
 8933:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8934:                             } else {
 8935:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8936:                             }
 8937:                         }
 8938:                     } elsif ($type eq 'recaptchakeys') {
 8939:                         my ($privkey,$pubkey);
 8940:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8941:                             $pubkey = $savecaptcha{$type}{'public'};
 8942:                             $privkey = $savecaptcha{$type}{'private'};
 8943:                         }
 8944:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8945:                         if (!$pubkey) {
 8946:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8947:                         } else {
 8948:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8949:                         }
 8950:                         if (!$privkey) {
 8951:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8952:                         } else {
 8953:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8954:                         }
 8955:                         $chgtext .= '</ul>';
 8956:                     } elsif ($type eq 'emailusername') {
 8957:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8958:                             if (ref($types) eq 'ARRAY') {
 8959:                                 foreach my $type (@{$types}) {
 8960:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 8961:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 8962:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
 8963:                                                     '<ul>';
 8964:                                             foreach my $field (@{$infofields}) {
 8965:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 8966:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8967:                                                 }
 8968:                                             }
 8969:                                             $chgtext .= '</ul>';
 8970:                                         } else {
 8971:                                             $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 />';
 8972:                                         }
 8973:                                     } else {
 8974:                                         $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 />';
 8975:                                     }
 8976:                                 }
 8977:                             }
 8978:                         }
 8979:                     } elsif ($type eq 'notify') {
 8980:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8981:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8982:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8983:                                 if ($cancreate{'notify'}{'approval'}) {
 8984:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8985:                                 }
 8986:                             }
 8987:                         }
 8988:                     }
 8989:                     if ($chgtext) {
 8990:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8991:                     }
 8992:                 }
 8993:             }
 8994:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8995:                 my ($emailrules,$emailruleorder) =
 8996:                     &Apache::lonnet::inst_userrules($dom,'email');
 8997:                 my $chgtext = '<ul>';
 8998:                 foreach my $type (@email_rule) {
 8999:                     if (ref($emailrules->{$type}) eq 'HASH') {
 9000:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 9001:                     }
 9002:                 }
 9003:                 $chgtext .= '</ul>';
 9004:                 if (@email_rule > 0) {
 9005:                     $resulttext .= '<li>'.
 9006:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 9007:                                        $chgtext.
 9008:                                    '</li>';
 9009:                 } else {
 9010:                     $resulttext .= '<li>'.
 9011:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 9012:                                    '</li>';
 9013:                 }
 9014:             }
 9015:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 9016:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 9017:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9018:                 foreach my $type (@{$changes{'selfcreate'}}) {
 9019:                     my $typename = $type;
 9020:                     if (ref($usertypes) eq 'HASH') {
 9021:                         if ($usertypes->{$type} ne '') {
 9022:                             $typename = $usertypes->{$type};
 9023:                         }
 9024:                     }
 9025:                     my @modifiable;
 9026:                     $resulttext .= '<li>'.
 9027:                                     &mt('Self-creation of account by users with status: [_1]',
 9028:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 9029:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 9030:                     foreach my $field (@fields) {
 9031:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 9032:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 9033:                         }
 9034:                     }
 9035:                     if (@modifiable > 0) {
 9036:                         $resulttext .= join(', ',@modifiable);
 9037:                     } else {
 9038:                         $resulttext .= &mt('none');
 9039:                     }
 9040:                     $resulttext .= '</li>';
 9041:                 }
 9042:                 $resulttext .= '</ul></li>';
 9043:             }
 9044:             $resulttext .= '</ul>';
 9045:         } else {
 9046:             $resulttext = &mt('No changes made to self-creation settings');
 9047:         }
 9048:     } else {
 9049:         $resulttext = '<span class="LC_error">'.
 9050:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9051:     }
 9052:     if ($warningmsg ne '') {
 9053:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9054:     }
 9055:     return $resulttext;
 9056: }
 9057: 
 9058: sub process_captcha {
 9059:     my ($container,$changes,$newsettings,$current) = @_;
 9060:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 9061:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 9062:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 9063:         $newsettings->{'captcha'} = 'original';
 9064:     }
 9065:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 9066:         if ($container eq 'cancreate') {
 9067:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9068:                 push(@{$changes->{'cancreate'}},'captcha');
 9069:             } elsif (!defined($changes->{'cancreate'})) {
 9070:                 $changes->{'cancreate'} = ['captcha'];
 9071:             }
 9072:         } else {
 9073:             $changes->{'captcha'} = 1;
 9074:         }
 9075:     }
 9076:     my ($newpub,$newpriv,$currpub,$currpriv);
 9077:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 9078:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 9079:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 9080:         $newpub =~ s/[^\w\-]//g;
 9081:         $newpriv =~ s/[^\w\-]//g;
 9082:         $newsettings->{'recaptchakeys'} = {
 9083:                                              public  => $newpub,
 9084:                                              private => $newpriv,
 9085:                                           };
 9086:     }
 9087:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 9088:         $currpub = $current->{'recaptchakeys'}{'public'};
 9089:         $currpriv = $current->{'recaptchakeys'}{'private'};
 9090:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 9091:             $newsettings->{'recaptchakeys'} = {
 9092:                                                  public  => '',
 9093:                                                  private => '',
 9094:                                               }
 9095:         }
 9096:     }
 9097:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 9098:         if ($container eq 'cancreate') {
 9099:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9100:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 9101:             } elsif (!defined($changes->{'cancreate'})) {
 9102:                 $changes->{'cancreate'} = ['recaptchakeys'];
 9103:             }
 9104:         } else {
 9105:             $changes->{'recaptchakeys'} = 1;
 9106:         }
 9107:     }
 9108:     return;
 9109: }
 9110: 
 9111: sub modify_usermodification {
 9112:     my ($dom,%domconfig) = @_;
 9113:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 9114:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9115:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9116:             if ($key eq 'selfcreate') {
 9117:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 9118:             } else {  
 9119:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 9120:             }
 9121:         }
 9122:     }
 9123:     my @contexts = ('author','course');
 9124:     my %context_title = (
 9125:                            author => 'In author context',
 9126:                            course => 'In course context',
 9127:                         );
 9128:     my @fields = ('lastname','firstname','middlename','generation',
 9129:                   'permanentemail','id');
 9130:     my %roles = (
 9131:                   author => ['ca','aa'],
 9132:                   course => ['st','ep','ta','in','cr'],
 9133:                 );
 9134:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9135:     foreach my $context (@contexts) {
 9136:         foreach my $role (@{$roles{$context}}) {
 9137:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 9138:             foreach my $item (@fields) {
 9139:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 9140:                     $modifyhash{$context}{$role}{$item} = 1;
 9141:                 } else {
 9142:                     $modifyhash{$context}{$role}{$item} = 0;
 9143:                 }
 9144:             }
 9145:         }
 9146:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 9147:             foreach my $role (@{$roles{$context}}) {
 9148:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 9149:                     foreach my $field (@fields) {
 9150:                         if ($modifyhash{$context}{$role}{$field} ne 
 9151:                                 $curr_usermodification{$context}{$role}{$field}) {
 9152:                             push(@{$changes{$context}},$role);
 9153:                             last;
 9154:                         }
 9155:                     }
 9156:                 }
 9157:             }
 9158:         } else {
 9159:             foreach my $context (@contexts) {
 9160:                 foreach my $role (@{$roles{$context}}) {
 9161:                     push(@{$changes{$context}},$role);
 9162:                 }
 9163:             }
 9164:         }
 9165:     }
 9166:     my %usermodification_hash =  (
 9167:                                    usermodification => \%modifyhash,
 9168:                                  );
 9169:     my $putresult = &Apache::lonnet::put_dom('configuration',
 9170:                                              \%usermodification_hash,$dom);
 9171:     if ($putresult eq 'ok') {
 9172:         if (keys(%changes) > 0) {
 9173:             $resulttext = &mt('Changes made: ').'<ul>';
 9174:             foreach my $context (@contexts) {
 9175:                 if (ref($changes{$context}) eq 'ARRAY') {
 9176:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 9177:                     if (ref($changes{$context}) eq 'ARRAY') {
 9178:                         foreach my $role (@{$changes{$context}}) {
 9179:                             my $rolename;
 9180:                             if ($role eq 'cr') {
 9181:                                 $rolename = &mt('Custom');
 9182:                             } else {
 9183:                                 $rolename = &Apache::lonnet::plaintext($role);
 9184:                             }
 9185:                             my @modifiable;
 9186:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 9187:                             foreach my $field (@fields) {
 9188:                                 if ($modifyhash{$context}{$role}{$field}) {
 9189:                                     push(@modifiable,$fieldtitles{$field});
 9190:                                 }
 9191:                             }
 9192:                             if (@modifiable > 0) {
 9193:                                 $resulttext .= join(', ',@modifiable);
 9194:                             } else {
 9195:                                 $resulttext .= &mt('none'); 
 9196:                             }
 9197:                             $resulttext .= '</li>';
 9198:                         }
 9199:                         $resulttext .= '</ul></li>';
 9200:                     }
 9201:                 }
 9202:             }
 9203:             $resulttext .= '</ul>';
 9204:         } else {
 9205:             $resulttext = &mt('No changes made to user modification settings');
 9206:         }
 9207:     } else {
 9208:         $resulttext = '<span class="LC_error">'.
 9209:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9210:     }
 9211:     return $resulttext;
 9212: }
 9213: 
 9214: sub modify_defaults {
 9215:     my ($dom,$lastactref,%domconfig) = @_;
 9216:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 9217:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9218:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 9219:     my @authtypes = ('internal','krb4','krb5','localauth');
 9220:     foreach my $item (@items) {
 9221:         $newvalues{$item} = $env{'form.'.$item};
 9222:         if ($item eq 'auth_def') {
 9223:             if ($newvalues{$item} ne '') {
 9224:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 9225:                     push(@errors,$item);
 9226:                 }
 9227:             }
 9228:         } elsif ($item eq 'lang_def') {
 9229:             if ($newvalues{$item} ne '') {
 9230:                 if ($newvalues{$item} =~ /^(\w+)/) {
 9231:                     my $langcode = $1;
 9232:                     if ($langcode ne 'x_chef') {
 9233:                         if (code2language($langcode) eq '') {
 9234:                             push(@errors,$item);
 9235:                         }
 9236:                     }
 9237:                 } else {
 9238:                     push(@errors,$item);
 9239:                 }
 9240:             }
 9241:         } elsif ($item eq 'timezone_def') {
 9242:             if ($newvalues{$item} ne '') {
 9243:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 9244:                     push(@errors,$item);   
 9245:                 }
 9246:             }
 9247:         } elsif ($item eq 'datelocale_def') {
 9248:             if ($newvalues{$item} ne '') {
 9249:                 my @datelocale_ids = DateTime::Locale->ids();
 9250:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 9251:                     push(@errors,$item);
 9252:                 }
 9253:             }
 9254:         } elsif ($item eq 'portal_def') {
 9255:             if ($newvalues{$item} ne '') {
 9256:                 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])\/?$/) {
 9257:                     push(@errors,$item);
 9258:                 }
 9259:             }
 9260:         }
 9261:         if (grep(/^\Q$item\E$/,@errors)) {
 9262:             $newvalues{$item} = $domdefaults{$item};
 9263:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 9264:             $changes{$item} = 1;
 9265:         }
 9266:         $domdefaults{$item} = $newvalues{$item};
 9267:     }
 9268:     my %defaults_hash = (
 9269:                          defaults => \%newvalues,
 9270:                         );
 9271:     my $title = &defaults_titles();
 9272: 
 9273:     my $currinststatus;
 9274:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9275:         $currinststatus = $domconfig{'inststatus'};
 9276:     } else {
 9277:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9278:         $currinststatus = {
 9279:                              inststatustypes => $usertypes,
 9280:                              inststatusorder => $types,
 9281:                              inststatusguest => [],
 9282:                           };
 9283:     }
 9284:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
 9285:     my @allpos;
 9286:     my %guests;
 9287:     my %alltypes;
 9288:     my ($currtitles,$currguests,$currorder);
 9289:     if (ref($currinststatus) eq 'HASH') {
 9290:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
 9291:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
 9292:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
 9293:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
 9294:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
 9295:                     }
 9296:                 }
 9297:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
 9298:                     my $position = $env{'form.inststatus_pos_'.$type};
 9299:                     $position =~ s/\D+//g;
 9300:                     $allpos[$position] = $type;
 9301:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
 9302:                     $alltypes{$type} =~ s/`//g;
 9303:                     if ($env{'form.inststatus_guest_'.$type}) {
 9304:                         $guests{$type} = 1;
 9305:                     }
 9306:                 }
 9307:             }
 9308:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
 9309:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
 9310:             }
 9311:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
 9312:             $currtitles =~ s/,$//;
 9313:         }
 9314:     }
 9315:     if ($env{'form.addinststatus'}) {
 9316:         my $newtype = $env{'form.addinststatus'};
 9317:         $newtype =~ s/\W//g;
 9318:         unless (exists($alltypes{$newtype})) {
 9319:             if ($env{'form.addinststatus_guest'}) {
 9320:                 $guests{$newtype} = 1;
 9321:             }
 9322:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
 9323:             $alltypes{$newtype} =~ s/`//g; 
 9324:             my $position = $env{'form.addinststatus_pos'};
 9325:             $position =~ s/\D+//g;
 9326:             if ($position ne '') {
 9327:                 $allpos[$position] = $newtype;
 9328:             }
 9329:         }
 9330:     }
 9331:     my (@orderedstatus,@orderedguests);
 9332:     foreach my $type (@allpos) {
 9333:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
 9334:             push(@orderedstatus,$type);
 9335:             if ($guests{$type}) {
 9336:                 push(@orderedguests,$type);
 9337:             }
 9338:         }
 9339:     }
 9340:     foreach my $type (keys(%alltypes)) {
 9341:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
 9342:             delete($alltypes{$type});
 9343:         }
 9344:     }
 9345:     $defaults_hash{'inststatus'} = {
 9346:                                      inststatustypes => \%alltypes,
 9347:                                      inststatusorder => \@orderedstatus,
 9348:                                      inststatusguest => \@orderedguests,
 9349:                                    };
 9350:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
 9351:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 9352:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
 9353:         }
 9354:     }
 9355:     if ($currorder ne join(',',@orderedstatus)) {
 9356:         $changes{'inststatus'}{'inststatusorder'} = 1;
 9357:     }
 9358:     if ($currguests ne join(',',@orderedguests)) {
 9359:         $changes{'inststatus'}{'inststatusguest'} = 1;
 9360:     }
 9361:     my $newtitles;
 9362:     foreach my $item (@orderedstatus) {
 9363:         $newtitles .= $alltypes{$item}.',';
 9364:     }
 9365:     $newtitles =~ s/,$//;
 9366:     if ($currtitles ne $newtitles) {
 9367:         $changes{'inststatus'}{'inststatustypes'} = 1;
 9368:     }
 9369:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 9370:                                              $dom);
 9371:     if ($putresult eq 'ok') {
 9372:         if (keys(%changes) > 0) {
 9373:             $resulttext = &mt('Changes made:').'<ul>';
 9374:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 9375:             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";
 9376:             foreach my $item (sort(keys(%changes))) {
 9377:                 if ($item eq 'inststatus') {
 9378:                     if (ref($changes{'inststatus'}) eq 'HASH') {
 9379:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
 9380:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
 9381:                             foreach my $type (@orderedstatus) { 
 9382:                                 $resulttext .= $alltypes{$type}.', ';
 9383:                             }
 9384:                             $resulttext =~ s/, $//;
 9385:                             $resulttext .= '</li>';
 9386:                         }
 9387:                         if ($changes{'inststatus'}{'inststatusguest'}) {
 9388:                             $resulttext .= '<li>'; 
 9389:                             if (@orderedguests) {
 9390:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
 9391:                                 foreach my $type (@orderedguests) {
 9392:                                     $resulttext .= $alltypes{$type}.', ';
 9393:                                 }
 9394:                                 $resulttext =~ s/, $//;
 9395:                             } else {
 9396:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
 9397:                             }
 9398:                             $resulttext .= '</li>';
 9399:                         }
 9400:                     }
 9401:                 } else {
 9402:                     my $value = $env{'form.'.$item};
 9403:                     if ($value eq '') {
 9404:                         $value = &mt('none');
 9405:                     } elsif ($item eq 'auth_def') {
 9406:                         my %authnames = &authtype_names();
 9407:                         my %shortauth = (
 9408:                                           internal   => 'int',
 9409:                                           krb4       => 'krb4',
 9410:                                           krb5       => 'krb5',
 9411:                                           localauth  => 'loc',
 9412:                         );
 9413:                         $value = $authnames{$shortauth{$value}};
 9414:                     }
 9415:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 9416:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
 9417:                 }
 9418:             }
 9419:             $resulttext .= '</ul>';
 9420:             $mailmsgtext .= "\n";
 9421:             my $cachetime = 24*60*60;
 9422:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9423:             if (ref($lastactref) eq 'HASH') {
 9424:                 $lastactref->{'domdefaults'} = 1;
 9425:             }
 9426:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 9427:                 my $notify = 1;
 9428:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 9429:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 9430:                         $notify = 0;
 9431:                     }
 9432:                 }
 9433:                 if ($notify) {
 9434:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 9435:                                                "LON-CAPA Domain Settings Change - $dom",
 9436:                                                $mailmsgtext);
 9437:                 }
 9438:             }
 9439:         } else {
 9440:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 9441:         }
 9442:     } else {
 9443:         $resulttext = '<span class="LC_error">'.
 9444:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9445:     }
 9446:     if (@errors > 0) {
 9447:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 9448:         foreach my $item (@errors) {
 9449:             $resulttext .= ' "'.$title->{$item}.'",';
 9450:         }
 9451:         $resulttext =~ s/,$//;
 9452:     }
 9453:     return $resulttext;
 9454: }
 9455: 
 9456: sub modify_scantron {
 9457:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9458:     my ($resulttext,%confhash,%changes,$errors);
 9459:     my $custom = 'custom.tab';
 9460:     my $default = 'default.tab';
 9461:     my $servadm = $r->dir_config('lonAdmEMail');
 9462:     my ($configuserok,$author_ok,$switchserver) = 
 9463:         &config_check($dom,$confname,$servadm);
 9464:     if ($env{'form.scantronformat.filename'} ne '') {
 9465:         my $error;
 9466:         if ($configuserok eq 'ok') {
 9467:             if ($switchserver) {
 9468:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 9469:             } else {
 9470:                 if ($author_ok eq 'ok') {
 9471:                     my ($result,$scantronurl) =
 9472:                         &publishlogo($r,'upload','scantronformat',$dom,
 9473:                                      $confname,'scantron','','',$custom);
 9474:                     if ($result eq 'ok') {
 9475:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 9476:                         $changes{'scantronformat'} = 1;
 9477:                     } else {
 9478:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 9479:                     }
 9480:                 } else {
 9481:                     $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);
 9482:                 }
 9483:             }
 9484:         } else {
 9485:             $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);
 9486:         }
 9487:         if ($error) {
 9488:             &Apache::lonnet::logthis($error);
 9489:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9490:         }
 9491:     }
 9492:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 9493:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 9494:             if ($env{'form.scantronformat_del'}) {
 9495:                 $confhash{'scantron'}{'scantronformat'} = '';
 9496:                 $changes{'scantronformat'} = 1;
 9497:             }
 9498:         }
 9499:     }
 9500:     if (keys(%confhash) > 0) {
 9501:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 9502:                                                  $dom);
 9503:         if ($putresult eq 'ok') {
 9504:             if (keys(%changes) > 0) {
 9505:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 9506:                     $resulttext = &mt('Changes made:').'<ul>';
 9507:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 9508:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 9509:                     } else {
 9510:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 9511:                     }
 9512:                     $resulttext .= '</ul>';
 9513:                 } else {
 9514:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 9515:                 }
 9516:                 $resulttext .= '</ul>';
 9517:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9518:                 if (ref($lastactref) eq 'HASH') {
 9519:                     $lastactref->{'domainconfig'} = 1;
 9520:                 }
 9521:             } else {
 9522:                 $resulttext = &mt('No changes made to bubblesheet format file');
 9523:             }
 9524:         } else {
 9525:             $resulttext = '<span class="LC_error">'.
 9526:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 9527:         }
 9528:     } else {
 9529:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 9530:     }
 9531:     if ($errors) {
 9532:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9533:                        $errors.'</ul>';
 9534:     }
 9535:     return $resulttext;
 9536: }
 9537: 
 9538: sub modify_coursecategories {
 9539:     my ($dom,$lastactref,%domconfig) = @_;
 9540:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 9541:         $cathash);
 9542:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 9543:     my @catitems = ('unauth','auth');
 9544:     my @cattypes = ('std','domonly','codesrch','none');
 9545:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9546:         $cathash = $domconfig{'coursecategories'}{'cats'};
 9547:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 9548:             $changes{'togglecats'} = 1;
 9549:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 9550:         }
 9551:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 9552:             $changes{'categorize'} = 1;
 9553:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 9554:         }
 9555:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 9556:             $changes{'togglecatscomm'} = 1;
 9557:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 9558:         }
 9559:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 9560:             $changes{'categorizecomm'} = 1;
 9561:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 9562:         }
 9563:         foreach my $item (@catitems) {
 9564:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9565:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
 9566:                     $changes{$item} = 1;
 9567:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9568:                 }
 9569:             }
 9570:         }
 9571:     } else {
 9572:         $changes{'togglecats'} = 1;
 9573:         $changes{'categorize'} = 1;
 9574:         $changes{'togglecatscomm'} = 1;
 9575:         $changes{'categorizecomm'} = 1;
 9576:         $domconfig{'coursecategories'} = {
 9577:                                              togglecats => $env{'form.togglecats'},
 9578:                                              categorize => $env{'form.categorize'},
 9579:                                              togglecatscomm => $env{'form.togglecatscomm'},
 9580:                                              categorizecomm => $env{'form.categorizecomm'},
 9581:                                          };
 9582:         foreach my $item (@catitems) {
 9583:             if ($env{'form.coursecat_'.$item} ne 'std') {
 9584:                 $changes{$item} = 1;
 9585:             }
 9586:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9587:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9588:             }
 9589:         }
 9590:     }
 9591:     if (ref($cathash) eq 'HASH') {
 9592:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 9593:             push (@deletecategory,'instcode::0');
 9594:         }
 9595:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 9596:             push(@deletecategory,'communities::0');
 9597:         }
 9598:     }
 9599:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 9600:     if (ref($cathash) eq 'HASH') {
 9601:         if (@deletecategory > 0) {
 9602:             #FIXME Need to remove category from all courses using a deleted category 
 9603:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 9604:             foreach my $item (@deletecategory) {
 9605:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 9606:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 9607:                     $deletions{$item} = 1;
 9608:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 9609:                 }
 9610:             }
 9611:         }
 9612:         foreach my $item (keys(%{$cathash})) {
 9613:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9614:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 9615:                 $reorderings{$item} = 1;
 9616:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 9617:             }
 9618:             if ($env{'form.addcategory_name_'.$item} ne '') {
 9619:                 my $newcat = $env{'form.addcategory_name_'.$item};
 9620:                 my $newdepth = $depth+1;
 9621:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9622:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 9623:                 $adds{$newitem} = 1; 
 9624:             }
 9625:             if ($env{'form.subcat_'.$item} ne '') {
 9626:                 my $newcat = $env{'form.subcat_'.$item};
 9627:                 my $newdepth = $depth+1;
 9628:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9629:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 9630:                 $adds{$newitem} = 1;
 9631:             }
 9632:         }
 9633:     }
 9634:     if ($env{'form.instcode'} eq '1') {
 9635:         if (ref($cathash) eq 'HASH') {
 9636:             my $newitem = 'instcode::0';
 9637:             if ($cathash->{$newitem} eq '') {  
 9638:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9639:                 $adds{$newitem} = 1;
 9640:             }
 9641:         } else {
 9642:             my $newitem = 'instcode::0';
 9643:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9644:             $adds{$newitem} = 1;
 9645:         }
 9646:     }
 9647:     if ($env{'form.communities'} eq '1') {
 9648:         if (ref($cathash) eq 'HASH') {
 9649:             my $newitem = 'communities::0';
 9650:             if ($cathash->{$newitem} eq '') {
 9651:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9652:                 $adds{$newitem} = 1;
 9653:             }
 9654:         } else {
 9655:             my $newitem = 'communities::0';
 9656:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9657:             $adds{$newitem} = 1;
 9658:         }
 9659:     }
 9660:     if ($env{'form.addcategory_name'} ne '') {
 9661:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 9662:             ($env{'form.addcategory_name'} ne 'communities')) {
 9663:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 9664:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 9665:             $adds{$newitem} = 1;
 9666:         }
 9667:     }
 9668:     my $putresult;
 9669:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9670:         if (keys(%deletions) > 0) {
 9671:             foreach my $key (keys(%deletions)) {
 9672:                 if ($predelallitems{$key} ne '') {
 9673:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 9674:                 }
 9675:             }
 9676:         }
 9677:         my (@chkcats,@chktrails,%chkallitems);
 9678:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 9679:         if (ref($chkcats[0]) eq 'ARRAY') {
 9680:             my $depth = 0;
 9681:             my $chg = 0;
 9682:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 9683:                 my $name = $chkcats[0][$i];
 9684:                 my $item;
 9685:                 if ($name eq '') {
 9686:                     $chg ++;
 9687:                 } else {
 9688:                     $item = &escape($name).'::0';
 9689:                     if ($chg) {
 9690:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 9691:                     }
 9692:                     $depth ++; 
 9693:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 9694:                     $depth --;
 9695:                 }
 9696:             }
 9697:         }
 9698:     }
 9699:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9700:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 9701:         if ($putresult eq 'ok') {
 9702:             my %title = (
 9703:                          togglecats     => 'Show/Hide a course in catalog',
 9704:                          categorize     => 'Assign a category to a course',
 9705:                          togglecatscomm => 'Show/Hide a community in catalog',
 9706:                          categorizecomm => 'Assign a category to a community',
 9707:                         );
 9708:             my %level = (
 9709:                          dom  => 'set in Domain ("Modify Course/Community")',
 9710:                          crs  => 'set in Course ("Course Configuration")',
 9711:                          comm => 'set in Community ("Community Configuration")',
 9712:                          none     => 'No catalog',
 9713:                          std      => 'Standard catalog',
 9714:                          domonly  => 'Domain-only catalog',
 9715:                          codesrch => 'Code search form',
 9716:                         );
 9717:             $resulttext = &mt('Changes made:').'<ul>';
 9718:             if ($changes{'togglecats'}) {
 9719:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 9720:             }
 9721:             if ($changes{'categorize'}) {
 9722:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 9723:             }
 9724:             if ($changes{'togglecatscomm'}) {
 9725:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 9726:             }
 9727:             if ($changes{'categorizecomm'}) {
 9728:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 9729:             }
 9730:             if ($changes{'unauth'}) {
 9731:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
 9732:             }
 9733:             if ($changes{'auth'}) {
 9734:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
 9735:             }
 9736:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9737:                 my $cathash;
 9738:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9739:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 9740:                 } else {
 9741:                     $cathash = {};
 9742:                 } 
 9743:                 my (@cats,@trails,%allitems);
 9744:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 9745:                 if (keys(%deletions) > 0) {
 9746:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 9747:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 9748:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 9749:                     }
 9750:                     $resulttext .= '</ul></li>';
 9751:                 }
 9752:                 if (keys(%reorderings) > 0) {
 9753:                     my %sort_by_trail;
 9754:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 9755:                     foreach my $key (keys(%reorderings)) {
 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:                 if (keys(%adds) > 0) {
 9766:                     my %sort_by_trail;
 9767:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 9768:                     foreach my $key (keys(%adds)) {
 9769:                         if ($allitems{$key} ne '') {
 9770:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9771:                         }
 9772:                     }
 9773:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9774:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9775:                     }
 9776:                     $resulttext .= '</ul></li>';
 9777:                 }
 9778:             }
 9779:             $resulttext .= '</ul>';
 9780:             if ($changes{'unauth'} || $changes{'auth'}) {
 9781:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 9782:                 if ($changes{'auth'}) {
 9783:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 9784:                 }
 9785:                 if ($changes{'unauth'}) {
 9786:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 9787:                 }
 9788:                 my $cachetime = 24*60*60;
 9789:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9790:                 if (ref($lastactref) eq 'HASH') {
 9791:                     $lastactref->{'domdefaults'} = 1;
 9792:                 }
 9793:             }
 9794:         } else {
 9795:             $resulttext = '<span class="LC_error">'.
 9796:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9797:         }
 9798:     } else {
 9799:         $resulttext = &mt('No changes made to course and community categories');
 9800:     }
 9801:     return $resulttext;
 9802: }
 9803: 
 9804: sub modify_serverstatuses {
 9805:     my ($dom,%domconfig) = @_;
 9806:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 9807:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 9808:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 9809:     }
 9810:     my @pages = &serverstatus_pages();
 9811:     foreach my $type (@pages) {
 9812:         $newserverstatus{$type}{'namedusers'} = '';
 9813:         $newserverstatus{$type}{'machines'} = '';
 9814:         if (defined($env{'form.'.$type.'_namedusers'})) {
 9815:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 9816:             my @okusers;
 9817:             foreach my $user (@users) {
 9818:                 my ($uname,$udom) = split(/:/,$user);
 9819:                 if (($udom =~ /^$match_domain$/) &&   
 9820:                     (&Apache::lonnet::domain($udom)) &&
 9821:                     ($uname =~ /^$match_username$/)) {
 9822:                     if (!grep(/^\Q$user\E/,@okusers)) {
 9823:                         push(@okusers,$user);
 9824:                     }
 9825:                 }
 9826:             }
 9827:             if (@okusers > 0) {
 9828:                  @okusers = sort(@okusers);
 9829:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 9830:             }
 9831:         }
 9832:         if (defined($env{'form.'.$type.'_machines'})) {
 9833:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 9834:             my @okmachines;
 9835:             foreach my $ip (@machines) {
 9836:                 my @parts = split(/\./,$ip);
 9837:                 next if (@parts < 4);
 9838:                 my $badip = 0;
 9839:                 for (my $i=0; $i<4; $i++) {
 9840:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 9841:                         $badip = 1;
 9842:                         last;
 9843:                     }
 9844:                 }
 9845:                 if (!$badip) {
 9846:                     push(@okmachines,$ip);     
 9847:                 }
 9848:             }
 9849:             @okmachines = sort(@okmachines);
 9850:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 9851:         }
 9852:     }
 9853:     my %serverstatushash =  (
 9854:                                 serverstatuses => \%newserverstatus,
 9855:                             );
 9856:     foreach my $type (@pages) {
 9857:         foreach my $setting ('namedusers','machines') {
 9858:             my (@current,@new);
 9859:             if (ref($currserverstatus{$type}) eq 'HASH') {
 9860:                 if ($currserverstatus{$type}{$setting} ne '') { 
 9861:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 9862:                 }
 9863:             }
 9864:             if ($newserverstatus{$type}{$setting} ne '') {
 9865:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 9866:             }
 9867:             if (@current > 0) {
 9868:                 if (@new > 0) {
 9869:                     foreach my $item (@current) {
 9870:                         if (!grep(/^\Q$item\E$/,@new)) {
 9871:                             $changes{$type}{$setting} = 1;
 9872:                             last;
 9873:                         }
 9874:                     }
 9875:                     foreach my $item (@new) {
 9876:                         if (!grep(/^\Q$item\E$/,@current)) {
 9877:                             $changes{$type}{$setting} = 1;
 9878:                             last;
 9879:                         }
 9880:                     }
 9881:                 } else {
 9882:                     $changes{$type}{$setting} = 1;
 9883:                 }
 9884:             } elsif (@new > 0) {
 9885:                 $changes{$type}{$setting} = 1;
 9886:             }
 9887:         }
 9888:     }
 9889:     if (keys(%changes) > 0) {
 9890:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9891:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9892:                                                  \%serverstatushash,$dom);
 9893:         if ($putresult eq 'ok') {
 9894:             $resulttext .= &mt('Changes made:').'<ul>';
 9895:             foreach my $type (@pages) {
 9896:                 if (ref($changes{$type}) eq 'HASH') {
 9897:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 9898:                     if ($changes{$type}{'namedusers'}) {
 9899:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 9900:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 9901:                         } else {
 9902:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 9903:                         }
 9904:                     }
 9905:                     if ($changes{$type}{'machines'}) {
 9906:                         if ($newserverstatus{$type}{'machines'} eq '') {
 9907:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 9908:                         } else {
 9909:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 9910:                         }
 9911: 
 9912:                     }
 9913:                     $resulttext .= '</ul></li>';
 9914:                 }
 9915:             }
 9916:             $resulttext .= '</ul>';
 9917:         } else {
 9918:             $resulttext = '<span class="LC_error">'.
 9919:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 9920: 
 9921:         }
 9922:     } else {
 9923:         $resulttext = &mt('No changes made to access to server status pages');
 9924:     }
 9925:     return $resulttext;
 9926: }
 9927: 
 9928: sub modify_helpsettings {
 9929:     my ($r,$dom,$confname,%domconfig) = @_;
 9930:     my ($resulttext,$errors,%changes,%helphash);
 9931:     my %defaultchecked = ('submitbugs' => 'on');
 9932:     my @offon = ('off','on');
 9933:     my @toggles = ('submitbugs');
 9934:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 9935:         foreach my $item (@toggles) {
 9936:             if ($defaultchecked{$item} eq 'on') { 
 9937:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9938:                     if ($env{'form.'.$item} eq '0') {
 9939:                         $changes{$item} = 1;
 9940:                     }
 9941:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9942:                     $changes{$item} = 1;
 9943:                 }
 9944:             } elsif ($defaultchecked{$item} eq 'off') {
 9945:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9946:                     if ($env{'form.'.$item} eq '1') {
 9947:                         $changes{$item} = 1;
 9948:                     }
 9949:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9950:                     $changes{$item} = 1;
 9951:                 }
 9952:             }
 9953:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 9954:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 9955:             }
 9956:         }
 9957:     }
 9958:     my $putresult;
 9959:     if (keys(%changes) > 0) {
 9960:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 9961:         if ($putresult eq 'ok') {
 9962:             $resulttext = &mt('Changes made:').'<ul>';
 9963:             foreach my $item (sort(keys(%changes))) {
 9964:                 if ($item eq 'submitbugs') {
 9965:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 9966:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 9967:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 9968:                 }
 9969:             }
 9970:             $resulttext .= '</ul>';
 9971:         } else {
 9972:             $resulttext = &mt('No changes made to help settings');
 9973:             $errors .= '<li><span class="LC_error">'.
 9974:                        &mt('An error occurred storing the settings: [_1]',
 9975:                            $putresult).'</span></li>';
 9976:         }
 9977:     }
 9978:     if ($errors) {
 9979:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9980:                        $errors.'</ul>';
 9981:     }
 9982:     return $resulttext;
 9983: }
 9984: 
 9985: sub modify_coursedefaults {
 9986:     my ($dom,$lastactref,%domconfig) = @_;
 9987:     my ($resulttext,$errors,%changes,%defaultshash);
 9988:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 9989:     my @toggles = ('canuse_pdfforms');
 9990:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 9991:                    'uploadquota_community','uploadquota_textbook');
 9992:     my @types = ('official','unofficial','community','textbook');
 9993:     my %staticdefaults = (
 9994:                            anonsurvey_threshold => 10,
 9995:                            uploadquota          => 500,
 9996:                          );
 9997: 
 9998:     $defaultshash{'coursedefaults'} = {};
 9999: 
10000:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
10001:         if ($domconfig{'coursedefaults'} eq '') {
10002:             $domconfig{'coursedefaults'} = {};
10003:         }
10004:     }
10005: 
10006:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
10007:         foreach my $item (@toggles) {
10008:             if ($defaultchecked{$item} eq 'on') {
10009:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
10010:                     ($env{'form.'.$item} eq '0')) {
10011:                     $changes{$item} = 1;
10012:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10013:                     $changes{$item} = 1;
10014:                 }
10015:             } elsif ($defaultchecked{$item} eq 'off') {
10016:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
10017:                     ($env{'form.'.$item} eq '1')) {
10018:                     $changes{$item} = 1;
10019:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10020:                     $changes{$item} = 1;
10021:                 }
10022:             }
10023:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
10024:         }
10025:         foreach my $item (@numbers) {
10026:             my ($currdef,$newdef);
10027:             $newdef = $env{'form.'.$item};
10028:             if ($item eq 'anonsurvey_threshold') {
10029:                 $currdef = $domconfig{'coursedefaults'}{$item};
10030:                 $newdef =~ s/\D//g;
10031:                 if ($newdef eq '' || $newdef < 1) {
10032:                     $newdef = 1;
10033:                 }
10034:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
10035:             } else {
10036:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
10037:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10038:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
10039:                 }
10040:                 $newdef =~ s/[^\w.\-]//g;
10041:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
10042:             }
10043:             if ($currdef ne $newdef) {
10044:                 my $staticdef;
10045:                 if ($item eq 'anonsurvey_threshold') {
10046:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
10047:                         $changes{$item} = 1;
10048:                     }
10049:                 } else {
10050:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
10051:                         $changes{'uploadquota'} = 1;
10052:                     }
10053:                 }
10054:             }
10055:         }
10056: 
10057:         my $officialcreds = $env{'form.official_credits'};
10058:         $officialcreds =~ s/[^\d.]+//g;
10059:         my $unofficialcreds = $env{'form.unofficial_credits'};
10060:         $unofficialcreds =~ s/[^\d.]+//g;
10061:         my $textbookcreds = $env{'form.textbook_credits'};
10062:         $textbookcreds =~ s/[^\d.]+//g;
10063:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
10064:                 ($env{'form.coursecredits'} eq '1')) {
10065:                 $changes{'coursecredits'} = 1;
10066:         } else {
10067:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
10068:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
10069:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
10070:                 $changes{'coursecredits'} = 1;
10071:             }
10072:         }
10073:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
10074:             official   => $officialcreds,
10075:             unofficial => $unofficialcreds,
10076:             textbook   => $textbookcreds,
10077:         }
10078:     }
10079:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10080:                                              $dom);
10081:     if ($putresult eq 'ok') {
10082:         if (keys(%changes) > 0) {
10083:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10084:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || 
10085:                 ($changes{'uploadquota'})) { 
10086:                 if ($changes{'canuse_pdfforms'}) {
10087:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
10088:                 }
10089:                 if ($changes{'coursecredits'}) {
10090:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10091:                         $domdefaults{'officialcredits'} =
10092:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
10093:                         $domdefaults{'unofficialcredits'} =
10094:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
10095:                         $domdefaults{'textbookcredits'} =
10096:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
10097:                     }
10098:                 }
10099:                 if ($changes{'uploadquota'}) {
10100:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10101:                         foreach my $type (@types) {
10102:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
10103:                         }
10104:                     }
10105:                 }
10106:                 my $cachetime = 24*60*60;
10107:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10108:                 if (ref($lastactref) eq 'HASH') {
10109:                     $lastactref->{'domdefaults'} = 1;
10110:                 }
10111:             }
10112:             $resulttext = &mt('Changes made:').'<ul>';
10113:             foreach my $item (sort(keys(%changes))) {
10114:                 if ($item eq 'canuse_pdfforms') {
10115:                     if ($env{'form.'.$item} eq '1') {
10116:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
10117:                     } else {
10118:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
10119:                     }
10120:                 } elsif ($item eq 'anonsurvey_threshold') {
10121:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
10122:                 } elsif ($item eq 'uploadquota') {
10123:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10124:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
10125:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
10126:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
10127:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
10128: 
10129:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
10130:                                        '</ul>'.
10131:                                        '</li>';
10132:                     } else {
10133:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
10134:                     }
10135:                 } elsif ($item eq 'coursecredits') {
10136:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10137:                         if (($domdefaults{'officialcredits'} eq '') &&
10138:                             ($domdefaults{'unofficialcredits'} eq '') &&
10139:                             ($domdefaults{'textbookcredits'} eq '')) {
10140:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10141:                         } else {
10142:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
10143:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
10144:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
10145:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
10146:                                            '</ul>'.
10147:                                            '</li>';
10148:                         }
10149:                     } else {
10150:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10151:                     }
10152:                 }
10153:             }
10154:             $resulttext .= '</ul>';
10155:         } else {
10156:             $resulttext = &mt('No changes made to course defaults');
10157:         }
10158:     } else {
10159:         $resulttext = '<span class="LC_error">'.
10160:             &mt('An error occurred: [_1]',$putresult).'</span>';
10161:     }
10162:     return $resulttext;
10163: }
10164: 
10165: sub modify_selfenrollment {
10166:     my ($dom,$lastactref,%domconfig) = @_;
10167:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
10168:     my @types = ('official','unofficial','community','textbook');
10169:     my %titles = &tool_titles();
10170:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
10171:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
10172:     $ordered{'default'} = ['types','registered','approval','limit'];
10173: 
10174:     my (%roles,%shown,%toplevel);
10175:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
10176: 
10177:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
10178:         if ($domconfig{'selfenrollment'} eq '') {
10179:             $domconfig{'selfenrollment'} = {};
10180:         }
10181:     }
10182:     %toplevel = (
10183:                   admin      => 'Configuration Rights',
10184:                   default    => 'Default settings',
10185:                   validation => 'Validation of self-enrollment requests',
10186:                 );
10187:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
10188: 
10189:     if (ref($ordered{'admin'}) eq 'ARRAY') {
10190:         foreach my $item (@{$ordered{'admin'}}) {
10191:             foreach my $type (@types) {
10192:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
10193:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
10194:                 } else {
10195:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
10196:                 }
10197:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
10198:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
10199:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
10200:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
10201:                             push(@{$changes{'admin'}{$type}},$item);
10202:                         }
10203:                     } else {
10204:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
10205:                             push(@{$changes{'admin'}{$type}},$item);
10206:                         }
10207:                     }
10208:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
10209:                     push(@{$changes{'admin'}{$type}},$item);
10210:                 }
10211:             }
10212:         }
10213:     }
10214: 
10215:     foreach my $item (@{$ordered{'default'}}) {
10216:         foreach my $type (@types) {
10217:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
10218:             if ($item eq 'types') {
10219:                 unless (($value eq 'all') || ($value eq 'dom')) {
10220:                     $value = '';
10221:                 }
10222:             } elsif ($item eq 'registered') {
10223:                 unless ($value eq '1') {
10224:                     $value = 0;
10225:                 }
10226:             } elsif ($item eq 'approval') {
10227:                 unless ($value =~ /^[012]$/) {
10228:                     $value = 0;
10229:                 }
10230:             } else {
10231:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10232:                     $value = 'none';
10233:                 }
10234:             }
10235:             $selfenrollhash{'default'}{$type}{$item} = $value;
10236:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
10237:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10238:                     if ($selfenrollhash{'default'}{$type}{$item} ne
10239:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
10240:                          push(@{$changes{'default'}{$type}},$item);
10241:                     }
10242:                 } else {
10243:                     push(@{$changes{'default'}{$type}},$item);
10244:                 }
10245:             } else {
10246:                 push(@{$changes{'default'}{$type}},$item);
10247:             }
10248:             if ($item eq 'limit') {
10249:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10250:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
10251:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
10252:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
10253:                     }
10254:                 } else {
10255:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
10256:                 }
10257:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10258:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
10259:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
10260:                          push(@{$changes{'default'}{$type}},'cap');
10261:                     }
10262:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
10263:                     push(@{$changes{'default'}{$type}},'cap');
10264:                 }
10265:             }
10266:         }
10267:     }
10268: 
10269:     foreach my $item (@{$itemsref}) {
10270:         if ($item eq 'fields') {
10271:             my @changed;
10272:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
10273:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
10274:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
10275:             }
10276:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10277:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
10278:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
10279:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
10280:                 } else {
10281:                     @changed = @{$selfenrollhash{'validation'}{$item}};
10282:                 }
10283:             } else {
10284:                 @changed = @{$selfenrollhash{'validation'}{$item}};
10285:             }
10286:             if (@changed) {
10287:                 if ($selfenrollhash{'validation'}{$item}) { 
10288:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
10289:                 } else {
10290:                     $changes{'validation'}{$item} = &mt('None');
10291:                 }
10292:             }
10293:         } else {
10294:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
10295:             if ($item eq 'markup') {
10296:                if ($env{'form.selfenroll_validation_'.$item}) {
10297:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10298:                }
10299:             }
10300:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10301:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
10302:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
10303:                 }
10304:             }
10305:         }
10306:     }
10307: 
10308:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
10309:                                              $dom);
10310:     if ($putresult eq 'ok') {
10311:         if (keys(%changes) > 0) {
10312:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10313:             $resulttext = &mt('Changes made:').'<ul>';
10314:             foreach my $key ('admin','default','validation') {
10315:                 if (ref($changes{$key}) eq 'HASH') {
10316:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
10317:                     if ($key eq 'validation') {
10318:                         foreach my $item (@{$itemsref}) {
10319:                             if (exists($changes{$key}{$item})) {
10320:                                 if ($item eq 'markup') {
10321:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10322:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
10323:                                 } else {  
10324:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10325:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
10326:                                 }
10327:                             }
10328:                         }
10329:                     } else {
10330:                         foreach my $type (@types) {
10331:                             if ($type eq 'community') {
10332:                                 $roles{'1'} = &mt('Community personnel');
10333:                             } else {
10334:                                 $roles{'1'} = &mt('Course personnel');
10335:                             }
10336:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
10337:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
10338:                                     if ($key eq 'admin') {
10339:                                         my @mgrdc = ();
10340:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10341:                                             foreach my $item (@{$ordered{'admin'}}) {
10342:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
10343:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
10344:                                                         push(@mgrdc,$item);
10345:                                                     }
10346:                                                 }
10347:                                             }
10348:                                             if (@mgrdc) {
10349:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
10350:                                             } else {
10351:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
10352:                                             }
10353:                                         }
10354:                                     } else {
10355:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10356:                                             foreach my $item (@{$ordered{$key}}) {
10357:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10358:                                                     $domdefaults{$type.'selfenroll'.$item} =
10359:                                                         $selfenrollhash{$key}{$type}{$item};
10360:                                                 }
10361:                                             }
10362:                                         }
10363:                                     }
10364:                                 }
10365:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
10366:                                 foreach my $item (@{$ordered{$key}}) {
10367:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10368:                                         $resulttext .= '<li>';
10369:                                         if ($key eq 'admin') {
10370:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
10371:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
10372:                                         } else {
10373:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
10374:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
10375:                                         }
10376:                                         $resulttext .= '</li>';
10377:                                     }
10378:                                 }
10379:                                 $resulttext .= '</ul></li>';
10380:                             }
10381:                         }
10382:                         $resulttext .= '</ul></li>'; 
10383:                     }
10384:                 }
10385:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
10386:                     my $cachetime = 24*60*60;
10387:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10388:                     if (ref($lastactref) eq 'HASH') {
10389:                         $lastactref->{'domdefaults'} = 1;
10390:                     }
10391:                 }
10392:             }
10393:             $resulttext .= '</ul>';
10394:         } else {
10395:             $resulttext = &mt('No changes made to self-enrollment settings');
10396:         }
10397:     } else {
10398:         $resulttext = '<span class="LC_error">'.
10399:             &mt('An error occurred: [_1]',$putresult).'</span>';
10400:     }
10401:     return $resulttext;
10402: }
10403: 
10404: sub modify_usersessions {
10405:     my ($dom,$lastactref,%domconfig) = @_;
10406:     my @hostingtypes = ('version','excludedomain','includedomain');
10407:     my @offloadtypes = ('primary','default');
10408:     my %types = (
10409:                   remote => \@hostingtypes,
10410:                   hosted => \@hostingtypes,
10411:                   spares => \@offloadtypes,
10412:                 );
10413:     my @prefixes = ('remote','hosted','spares');
10414:     my @lcversions = &Apache::lonnet::all_loncaparevs();
10415:     my (%by_ip,%by_location,@intdoms);
10416:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
10417:     my @locations = sort(keys(%by_location));
10418:     my (%defaultshash,%changes);
10419:     foreach my $prefix (@prefixes) {
10420:         $defaultshash{'usersessions'}{$prefix} = {};
10421:     }
10422:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10423:     my $resulttext;
10424:     my %iphost = &Apache::lonnet::get_iphost();
10425:     foreach my $prefix (@prefixes) {
10426:         next if ($prefix eq 'spares');
10427:         foreach my $type (@{$types{$prefix}}) {
10428:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
10429:             if ($type eq 'version') {
10430:                 my $value = $env{'form.'.$prefix.'_'.$type};
10431:                 my $okvalue;
10432:                 if ($value ne '') {
10433:                     if (grep(/^\Q$value\E$/,@lcversions)) {
10434:                         $okvalue = $value;
10435:                     }
10436:                 }
10437:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10438:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10439:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
10440:                             if ($inuse == 0) {
10441:                                 $changes{$prefix}{$type} = 1;
10442:                             } else {
10443:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
10444:                                     $changes{$prefix}{$type} = 1;
10445:                                 }
10446:                                 if ($okvalue ne '') {
10447:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10448:                                 } 
10449:                             }
10450:                         } else {
10451:                             if (($inuse == 1) && ($okvalue ne '')) {
10452:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10453:                                 $changes{$prefix}{$type} = 1;
10454:                             }
10455:                         }
10456:                     } else {
10457:                         if (($inuse == 1) && ($okvalue ne '')) {
10458:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10459:                             $changes{$prefix}{$type} = 1;
10460:                         }
10461:                     }
10462:                 } else {
10463:                     if (($inuse == 1) && ($okvalue ne '')) {
10464:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10465:                         $changes{$prefix}{$type} = 1;
10466:                     }
10467:                 }
10468:             } else {
10469:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
10470:                 my @okvals;
10471:                 foreach my $val (@vals) {
10472:                     if ($val =~ /:/) {
10473:                         my @items = split(/:/,$val);
10474:                         foreach my $item (@items) {
10475:                             if (ref($by_location{$item}) eq 'ARRAY') {
10476:                                 push(@okvals,$item);
10477:                             }
10478:                         }
10479:                     } else {
10480:                         if (ref($by_location{$val}) eq 'ARRAY') {
10481:                             push(@okvals,$val);
10482:                         }
10483:                     }
10484:                 }
10485:                 @okvals = sort(@okvals);
10486:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10487:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10488:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10489:                             if ($inuse == 0) {
10490:                                 $changes{$prefix}{$type} = 1; 
10491:                             } else {
10492:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10493:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
10494:                                 if (@changed > 0) {
10495:                                     $changes{$prefix}{$type} = 1;
10496:                                 }
10497:                             }
10498:                         } else {
10499:                             if ($inuse == 1) {
10500:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10501:                                 $changes{$prefix}{$type} = 1;
10502:                             }
10503:                         } 
10504:                     } else {
10505:                         if ($inuse == 1) {
10506:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10507:                             $changes{$prefix}{$type} = 1;
10508:                         }
10509:                     }
10510:                 } else {
10511:                     if ($inuse == 1) {
10512:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10513:                         $changes{$prefix}{$type} = 1;
10514:                     }
10515:                 }
10516:             }
10517:         }
10518:     }
10519: 
10520:     my @alldoms = &Apache::lonnet::all_domains();
10521:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10522:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
10523:     my $savespares;
10524: 
10525:     foreach my $lonhost (sort(keys(%servers))) {
10526:         my $serverhomeID =
10527:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
10528:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
10529:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
10530:         my %spareschg;
10531:         foreach my $type (@{$types{'spares'}}) {
10532:             my @okspares;
10533:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
10534:             foreach my $server (@checked) {
10535:                 if (&Apache::lonnet::hostname($server) ne '') {
10536:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
10537:                         unless (grep(/^\Q$server\E$/,@okspares)) {
10538:                             push(@okspares,$server);
10539:                         }
10540:                     }
10541:                 }
10542:             }
10543:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
10544:             my $newspare;
10545:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
10546:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
10547:                     $newspare = $new;
10548:                 }
10549:             }
10550:             my @spares;
10551:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
10552:                 @spares = sort(@okspares,$newspare);
10553:             } else {
10554:                 @spares = sort(@okspares);
10555:             }
10556:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
10557:             if (ref($spareid{$lonhost}) eq 'HASH') {
10558:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
10559:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
10560:                     if (@diffs > 0) {
10561:                         $spareschg{$type} = 1;
10562:                     }
10563:                 }
10564:             }
10565:         }
10566:         if (keys(%spareschg) > 0) {
10567:             $changes{'spares'}{$lonhost} = \%spareschg;
10568:         }
10569:     }
10570: 
10571:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
10572:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
10573:             if (ref($changes{'spares'}) eq 'HASH') {
10574:                 if (keys(%{$changes{'spares'}}) > 0) {
10575:                     $savespares = 1;
10576:                 }
10577:             }
10578:         } else {
10579:             $savespares = 1;
10580:         }
10581:     }
10582: 
10583:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
10584:     if ((keys(%changes) > 0) || ($savespares)) {
10585:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10586:                                                  $dom);
10587:         if ($putresult eq 'ok') {
10588:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10589:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
10590:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
10591:                 }
10592:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
10593:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
10594:                 }
10595:             }
10596:             my $cachetime = 24*60*60;
10597:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10598:             if (ref($lastactref) eq 'HASH') {
10599:                 $lastactref->{'domdefaults'} = 1;
10600:             }
10601:             if (keys(%changes) > 0) {
10602:                 my %lt = &usersession_titles();
10603:                 $resulttext = &mt('Changes made:').'<ul>';
10604:                 foreach my $prefix (@prefixes) {
10605:                     if (ref($changes{$prefix}) eq 'HASH') {
10606:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
10607:                         if ($prefix eq 'spares') {
10608:                             if (ref($changes{$prefix}) eq 'HASH') {
10609:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
10610:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
10611:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
10612:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
10613:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
10614:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
10615:                                         foreach my $type (@{$types{$prefix}}) {
10616:                                             if ($changes{$prefix}{$lonhost}{$type}) {
10617:                                                 my $offloadto = &mt('None');
10618:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
10619:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
10620:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
10621:                                                     }
10622:                                                 }
10623:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
10624:                                             }
10625:                                         }
10626:                                     }
10627:                                     $resulttext .= '</li>';
10628:                                 }
10629:                             }
10630:                         } else {
10631:                             foreach my $type (@{$types{$prefix}}) {
10632:                                 if (defined($changes{$prefix}{$type})) {
10633:                                     my $newvalue;
10634:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10635:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
10636:                                             if ($type eq 'version') {
10637:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
10638:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10639:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
10640:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
10641:                                                 }
10642:                                             }
10643:                                         }
10644:                                     }
10645:                                     if ($newvalue eq '') {
10646:                                         if ($type eq 'version') {
10647:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
10648:                                         } else {
10649:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
10650:                                         }
10651:                                     } else {
10652:                                         if ($type eq 'version') {
10653:                                             $newvalue .= ' '.&mt('(or later)'); 
10654:                                         }
10655:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
10656:                                     }
10657:                                 }
10658:                             }
10659:                         }
10660:                         $resulttext .= '</ul>';
10661:                     }
10662:                 }
10663:                 $resulttext .= '</ul>';
10664:             } else {
10665:                 $resulttext = $nochgmsg;
10666:             }
10667:         } else {
10668:             $resulttext = '<span class="LC_error">'.
10669:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10670:         }
10671:     } else {
10672:         $resulttext = $nochgmsg;
10673:     }
10674:     return $resulttext;
10675: }
10676: 
10677: sub modify_loadbalancing {
10678:     my ($dom,%domconfig) = @_;
10679:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
10680:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
10681:     my ($othertitle,$usertypes,$types) =
10682:         &Apache::loncommon::sorted_inst_types($dom);
10683:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10684:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
10685:     my @sparestypes = ('primary','default');
10686:     my %typetitles = &sparestype_titles();
10687:     my $resulttext;
10688:     my (%currbalancer,%currtargets,%currrules,%existing);
10689:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10690:         %existing = %{$domconfig{'loadbalancing'}};
10691:     }
10692:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
10693:                               \%currtargets,\%currrules);
10694:     my ($saveloadbalancing,%defaultshash,%changes);
10695:     my ($alltypes,$othertypes,$titles) =
10696:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
10697:     my %ruletitles = &offloadtype_text();
10698:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
10699:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
10700:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
10701:         if ($balancer eq '') {
10702:             next;
10703:         }
10704:         if (!exists($servers{$balancer})) {
10705:             if (exists($currbalancer{$balancer})) {
10706:                 push(@{$changes{'delete'}},$balancer);
10707:             }
10708:             next;
10709:         }
10710:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
10711:             push(@{$changes{'delete'}},$balancer);
10712:             next;
10713:         }
10714:         if (!exists($currbalancer{$balancer})) {
10715:             push(@{$changes{'add'}},$balancer);
10716:         }
10717:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
10718:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
10719:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
10720:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10721:             $saveloadbalancing = 1;
10722:         }
10723:         foreach my $sparetype (@sparestypes) {
10724:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
10725:             my @offloadto;
10726:             foreach my $target (@targets) {
10727:                 if (($servers{$target}) && ($target ne $balancer)) {
10728:                     if ($sparetype eq 'default') {
10729:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
10730:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
10731:                         }
10732:                     }
10733:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
10734:                         push(@offloadto,$target);
10735:                     }
10736:                 }
10737:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
10738:             }
10739:         }
10740:         if (ref($currtargets{$balancer}) eq 'HASH') {
10741:             foreach my $sparetype (@sparestypes) {
10742:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
10743:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
10744:                     if (@targetdiffs > 0) {
10745:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10746:                     }
10747:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10748:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10749:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10750:                     }
10751:                 }
10752:             }
10753:         } else {
10754:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
10755:                 foreach my $sparetype (@sparestypes) {
10756:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10757:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10758:                             $changes{'curr'}{$balancer}{'targets'} = 1;
10759:                         }
10760:                     }
10761:                 }
10762:             }
10763:         }
10764:         my $ishomedom;
10765:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
10766:             $ishomedom = 1;
10767:         }
10768:         if (ref($alltypes) eq 'ARRAY') {
10769:             foreach my $type (@{$alltypes}) {
10770:                 my $rule;
10771:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
10772:                          (!$ishomedom)) {
10773:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
10774:                 }
10775:                 if ($rule eq 'specific') {
10776:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
10777:                 }
10778:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
10779:                 if (ref($currrules{$balancer}) eq 'HASH') {
10780:                     if ($rule ne $currrules{$balancer}{$type}) {
10781:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10782:                     }
10783:                 } elsif ($rule ne '') {
10784:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10785:                 }
10786:             }
10787:         }
10788:     }
10789:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
10790:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
10791:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
10792:             $defaultshash{'loadbalancing'} = {};
10793:         }
10794:         my $putresult = &Apache::lonnet::put_dom('configuration',
10795:                                                  \%defaultshash,$dom);
10796:         if ($putresult eq 'ok') {
10797:             if (keys(%changes) > 0) {
10798:                 my %toupdate;
10799:                 if (ref($changes{'delete'}) eq 'ARRAY') {
10800:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
10801:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
10802:                         $toupdate{$balancer} = 1;
10803:                     }
10804:                 }
10805:                 if (ref($changes{'add'}) eq 'ARRAY') {
10806:                     foreach my $balancer (sort(@{$changes{'add'}})) {
10807:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
10808:                         $toupdate{$balancer} = 1;
10809:                     }
10810:                 }
10811:                 if (ref($changes{'curr'}) eq 'HASH') {
10812:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
10813:                         $toupdate{$balancer} = 1;
10814:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
10815:                             if ($changes{'curr'}{$balancer}{'targets'}) {
10816:                                 my %offloadstr;
10817:                                 foreach my $sparetype (@sparestypes) {
10818:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10819:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10820:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10821:                                         }
10822:                                     }
10823:                                 }
10824:                                 if (keys(%offloadstr) == 0) {
10825:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
10826:                                 } else {
10827:                                     my $showoffload;
10828:                                     foreach my $sparetype (@sparestypes) {
10829:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
10830:                                         if (defined($offloadstr{$sparetype})) {
10831:                                             $showoffload .= $offloadstr{$sparetype};
10832:                                         } else {
10833:                                             $showoffload .= &mt('None');
10834:                                         }
10835:                                         $showoffload .= ('&nbsp;'x3);
10836:                                     }
10837:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
10838:                                 }
10839:                             }
10840:                         }
10841:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
10842:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
10843:                                 foreach my $type (@{$alltypes}) {
10844:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
10845:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
10846:                                         my $balancetext;
10847:                                         if ($rule eq '') {
10848:                                             $balancetext =  $ruletitles{'default'};
10849:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
10850:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
10851:                                             $balancetext =  $ruletitles{$rule};
10852:                                             if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
10853:                                                 foreach my $sparetype (@sparestypes) {
10854:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10855:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10856:                                                     }
10857:                                                 }
10858:                                                 foreach my $item (@{$alltypes}) {
10859:                                                     next if ($item =~  /^_LC_ipchange/);
10860:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
10861:                                                     if ($hasrule eq 'homeserver') {
10862:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
10863:                                                     } else {
10864:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
10865:                                                             if ($servers{$hasrule}) {
10866:                                                                 $toupdate{$hasrule} = 1;
10867:                                                             }
10868:                                                         }
10869:                                                     }
10870:                                                 }
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>