File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.116: download - view: text, annotated - select for diffs
Tue Dec 28 03:35:34 2021 UTC (2 years, 4 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  Backport 1.399, 1.400

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.116 2021/12/28 03:35:34 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: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ##############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, and textbook).  In each case the radio buttons 
  108: allow the selection of one of four values:
  109: 
  110: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  111: which have the following effects:
  112: 
  113: 0
  114: 
  115: =over
  116: 
  117: - course requests are not allowed for this course types/affiliation
  118: 
  119: =back
  120: 
  121: approval 
  122: 
  123: =over 
  124: 
  125: - course requests must be approved by a Doman Coordinator in the 
  126: course's domain
  127: 
  128: =back
  129: 
  130: validate 
  131: 
  132: =over
  133: 
  134: - an institutional validation (e.g., check requestor is instructor
  135: of record) needs to be passed before the course will be created.  The required
  136: validation is in localenroll.pm on the primary library server for the course 
  137: domain.
  138: 
  139: =back
  140: 
  141: autolimit 
  142: 
  143: =over
  144:  
  145: - course requests will be processed automatically up to a limit of
  146: N requests for the course type for the particular requestor.
  147: If N is undefined, there is no limit to the number of course requests
  148: which a course owner may submit and have processed automatically. 
  149: 
  150: =back
  151: 
  152: =item modify_quotas() 
  153: 
  154: =back
  155: 
  156: =cut
  157: 
  158: package Apache::domainprefs;
  159: 
  160: use strict;
  161: use Apache::Constants qw(:common :http);
  162: use Apache::lonnet;
  163: use Apache::loncommon();
  164: use Apache::lonhtmlcommon();
  165: use Apache::lonlocal;
  166: use Apache::lonmsg();
  167: use Apache::lonconfigsettings;
  168: use Apache::lonuserutils();
  169: use Apache::loncoursequeueadmin();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use File::Copy;
  174: use Locale::Language;
  175: use DateTime::TimeZone;
  176: use DateTime::Locale;
  177: use Net::CIDR;
  178: 
  179: my $registered_cleanup;
  180: my $modified_urls;
  181: 
  182: sub handler {
  183:     my $r=shift;
  184:     if ($r->header_only) {
  185:         &Apache::loncommon::content_type($r,'text/html');
  186:         $r->send_http_header;
  187:         return OK;
  188:     }
  189: 
  190:     my $context = 'domain';
  191:     my $dom = $env{'request.role.domain'};
  192:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  193:     if (&Apache::lonnet::allowed('mau',$dom)) {
  194:         &Apache::loncommon::content_type($r,'text/html');
  195:         $r->send_http_header;
  196:     } else {
  197:         $env{'user.error.msg'}=
  198:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  199:         return HTTP_NOT_ACCEPTABLE;
  200:     }
  201: 
  202:     $registered_cleanup=0;
  203:     @{$modified_urls}=();
  204: 
  205:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  206:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  207:                                             ['phase','actions']);
  208:     my $phase = 'pickactions';
  209:     if ( exists($env{'form.phase'}) ) {
  210:         $phase = $env{'form.phase'};
  211:     }
  212:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  213:     my %domconfig =
  214:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  215:                 'quotas','autoenroll','autoupdate','autocreate',
  216:                 'directorysrch','usercreation','usermodification',
  217:                 'contacts','defaults','scantron','coursecategories',
  218:                 'serverstatuses','requestcourses','helpsettings',
  219:                 'coursedefaults','usersessions','loadbalancing',
  220:                 'requestauthor','selfenrollment','inststatus',
  221:                 'passwords','wafproxy','ipaccess'],$dom);
  222:     my @prefs_order = ('rolecolors','login','ipaccess','defaults','wafproxy','passwords',
  223:                        'quotas','autoenroll','autoupdate','autocreate','directorysrch',
  224:                        'contacts','usercreation','selfcreation','usermodification',
  225:                        'scantron','requestcourses','requestauthor','coursecategories',
  226:                        'serverstatuses','helpsettings','coursedefaults',
  227:                        'selfenrollment','usersessions');
  228:     my %existing;
  229:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  230:         %existing = %{$domconfig{'loadbalancing'}};
  231:     }
  232:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  233:         push(@prefs_order,'loadbalancing');
  234:     }
  235:     my %prefs = (
  236:         'rolecolors' =>
  237:                    { text => 'Default color schemes',
  238:                      help => 'Domain_Configuration_Color_Schemes',
  239:                      header => [{col1 => 'Student Settings',
  240:                                  col2 => '',},
  241:                                 {col1 => 'Coordinator Settings',
  242:                                  col2 => '',},
  243:                                 {col1 => 'Author Settings',
  244:                                  col2 => '',},
  245:                                 {col1 => 'Administrator Settings',
  246:                                  col2 => '',}],
  247:                       print => \&print_rolecolors,
  248:                       modify => \&modify_rolecolors,
  249:                     },
  250:         'login' =>
  251:                     { text => 'Log-in page options',
  252:                       help => 'Domain_Configuration_Login_Page',
  253:                       header => [{col1 => 'Log-in Page Items',
  254:                                   col2 => '',},
  255:                                  {col1 => 'Log-in Help',
  256:                                   col2 => 'Value'},
  257:                                  {col1 => 'Custom HTML in document head',
  258:                                   col2 => 'Value'},
  259:                                  {col1 => 'SSO',
  260:                                   col2 => 'Dual login: SSO and non-SSO options'},
  261:                                 ],
  262:                       print => \&print_login,
  263:                       modify => \&modify_login,
  264:                     },
  265:         'defaults' => 
  266:                     { text => 'Default authentication/language/timezone/portal/types',
  267:                       help => 'Domain_Configuration_LangTZAuth',
  268:                       header => [{col1 => 'Setting',
  269:                                   col2 => 'Value'},
  270:                                  {col1 => 'Institutional user types',
  271:                                   col2 => 'Name displayed'}],
  272:                       print => \&print_defaults,
  273:                       modify => \&modify_defaults,
  274:                     },
  275:         'wafproxy' =>
  276:                     { text => 'Web Application Firewall/Reverse Proxy',
  277:                       help => 'Domain_Configuration_WAF_Proxy',
  278:                       header => [{col1 => 'Domain(s)',
  279:                                   col2 => 'Servers and WAF/Reverse Proxy alias(es)',
  280:                                  },
  281:                                  {col1 => 'Domain(s)',
  282:                                   col2 => 'WAF Configuration',}],
  283:                       print => \&print_wafproxy,
  284:                       modify => \&modify_wafproxy,
  285:                     },
  286:         'passwords' =>
  287:                     { text => 'Passwords (Internal authentication)',
  288:                       help => 'Domain_Configuration_Passwords',
  289:                       header => [{col1 => 'Resetting Forgotten Password',
  290:                                   col2 => 'Settings'},
  291:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  292:                                   col2 => 'Settings'},
  293:                                  {col1 => 'Rules for LON-CAPA Passwords',
  294:                                   col2 => 'Settings'},
  295:                                  {col1 => 'Course Owner Changing Student Passwords',
  296:                                   col2 => 'Settings'}],
  297:                       print => \&print_passwords,
  298:                       modify => \&modify_passwords,
  299:                     },
  300:         'quotas' => 
  301:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  302:                       help => 'Domain_Configuration_Quotas',
  303:                       header => [{col1 => 'User affiliation',
  304:                                   col2 => 'Available tools',
  305:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  306:                       print => \&print_quotas,
  307:                       modify => \&modify_quotas,
  308:                     },
  309:         'autoenroll' =>
  310:                    { text => 'Auto-enrollment settings',
  311:                      help => 'Domain_Configuration_Auto_Enrollment',
  312:                      header => [{col1 => 'Configuration setting',
  313:                                  col2 => 'Value(s)'}],
  314:                      print => \&print_autoenroll,
  315:                      modify => \&modify_autoenroll,
  316:                    },
  317:         'autoupdate' => 
  318:                    { text => 'Auto-update settings',
  319:                      help => 'Domain_Configuration_Auto_Updates',
  320:                      header => [{col1 => 'Setting',
  321:                                  col2 => 'Value',},
  322:                                 {col1 => 'Setting',
  323:                                  col2 => 'Affiliation'},
  324:                                 {col1 => 'User population',
  325:                                  col2 => 'Updatable user data'}],
  326:                      print => \&print_autoupdate,
  327:                      modify => \&modify_autoupdate,
  328:                   },
  329:         'autocreate' => 
  330:                   { text => 'Auto-course creation settings',
  331:                      help => 'Domain_Configuration_Auto_Creation',
  332:                      header => [{col1 => 'Configuration Setting',
  333:                                  col2 => 'Value',}],
  334:                      print => \&print_autocreate,
  335:                      modify => \&modify_autocreate,
  336:                   },
  337:         'directorysrch' => 
  338:                   { text => 'Directory searches',
  339:                     help => 'Domain_Configuration_InstDirectory_Search',
  340:                     header => [{col1 => 'Institutional Directory Setting',
  341:                                 col2 => 'Value',},
  342:                                {col1 => 'LON-CAPA Directory Setting',
  343:                                 col2 => 'Value',}],
  344:                     print => \&print_directorysrch,
  345:                     modify => \&modify_directorysrch,
  346:                   },
  347:         'contacts' =>
  348:                   { text => 'E-mail addresses and helpform',
  349:                     help => 'Domain_Configuration_Contact_Info',
  350:                     header => [{col1 => 'Default e-mail addresses',
  351:                                 col2 => 'Value',},
  352:                                {col1 => 'Recipient(s) for notifications',
  353:                                 col2 => 'Value',},
  354:                                {col1 => 'Nightly status check e-mail',
  355:                                 col2 => 'Settings',},
  356:                                {col1 => 'Ask helpdesk form settings',
  357:                                 col2 => 'Value',},],
  358:                     print => \&print_contacts,
  359:                     modify => \&modify_contacts,
  360:                   },
  361:         'usercreation' => 
  362:                   { text => 'User creation',
  363:                     help => 'Domain_Configuration_User_Creation',
  364:                     header => [{col1 => 'Format rule type',
  365:                                 col2 => 'Format rules in force'},
  366:                                {col1 => 'User account creation',
  367:                                 col2 => 'Usernames which may be created',},
  368:                                {col1 => 'Context',
  369:                                 col2 => 'Assignable authentication types'}],
  370:                     print => \&print_usercreation,
  371:                     modify => \&modify_usercreation,
  372:                   },
  373:         'selfcreation' => 
  374:                   { text => 'Users self-creating accounts',
  375:                     help => 'Domain_Configuration_Self_Creation', 
  376:                     header => [{col1 => 'Self-creation with institutional username',
  377:                                 col2 => 'Enabled?'},
  378:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  379:                                 col2 => 'Information user can enter'},
  380:                                {col1 => 'Self-creation with e-mail verification',
  381:                                 col2 => 'Settings'}],
  382:                     print => \&print_selfcreation,
  383:                     modify => \&modify_selfcreation,
  384:                   },
  385:         'usermodification' =>
  386:                   { text => 'User modification',
  387:                     help => 'Domain_Configuration_User_Modification',
  388:                     header => [{col1 => 'Target user has role',
  389:                                 col2 => 'User information updatable in author context'},
  390:                                {col1 => 'Target user has role',
  391:                                 col2 => 'User information updatable in course context'}],
  392:                     print => \&print_usermodification,
  393:                     modify => \&modify_usermodification,
  394:                   },
  395:         'scantron' =>
  396:                   { text => 'Bubblesheet format',
  397:                     help => 'Domain_Configuration_Scantron_Format',
  398:                     header => [ {col1 => 'Bubblesheet format file',
  399:                                  col2 => ''},
  400:                                 {col1 => 'Bubblesheet data upload formats',
  401:                                  col2 => 'Settings'}],
  402:                     print => \&print_scantron,
  403:                     modify => \&modify_scantron,
  404:                   },
  405:         'requestcourses' => 
  406:                  {text => 'Request creation of courses',
  407:                   help => 'Domain_Configuration_Request_Courses',
  408:                   header => [{col1 => 'User affiliation',
  409:                               col2 => 'Availability/Processing of requests',},
  410:                              {col1 => 'Setting',
  411:                               col2 => 'Value'},
  412:                              {col1 => 'Available textbooks',
  413:                               col2 => ''},
  414:                              {col1 => 'Available templates',
  415:                               col2 => ''},
  416:                              {col1 => 'Validation (not official courses)',
  417:                               col2 => 'Value'},],
  418:                   print => \&print_quotas,
  419:                   modify => \&modify_quotas,
  420:                  },
  421:         'requestauthor' =>
  422:                  {text => 'Request Authoring Space',
  423:                   help => 'Domain_Configuration_Request_Author',
  424:                   header => [{col1 => 'User affiliation',
  425:                               col2 => 'Availability/Processing of requests',},
  426:                              {col1 => 'Setting',
  427:                               col2 => 'Value'}],
  428:                   print => \&print_quotas,
  429:                   modify => \&modify_quotas,
  430:                  },
  431:         'coursecategories' =>
  432:                   { text => 'Cataloging of courses/communities',
  433:                     help => 'Domain_Configuration_Cataloging_Courses',
  434:                     header => [{col1 => 'Catalog type/availability',
  435:                                 col2 => '',},
  436:                                {col1 => 'Category settings for standard catalog',
  437:                                 col2 => '',},
  438:                                {col1 => 'Categories',
  439:                                 col2 => '',
  440:                                }],
  441:                     print => \&print_coursecategories,
  442:                     modify => \&modify_coursecategories,
  443:                   },
  444:         'serverstatuses' =>
  445:                  {text   => 'Access to server status pages',
  446:                   help   => 'Domain_Configuration_Server_Status',
  447:                   header => [{col1 => 'Status Page',
  448:                               col2 => 'Other named users',
  449:                               col3 => 'Specific IPs',
  450:                             }],
  451:                   print => \&print_serverstatuses,
  452:                   modify => \&modify_serverstatuses,
  453:                  },
  454:         'helpsettings' =>
  455:                  {text   => 'Support settings',
  456:                   help   => 'Domain_Configuration_Help_Settings',
  457:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  458:                               col2 => 'Value'},
  459:                              {col1 => 'Helpdesk Roles',
  460:                               col2 => 'Settings'},],
  461:                   print  => \&print_helpsettings,
  462:                   modify => \&modify_helpsettings,
  463:                  },
  464:         'coursedefaults' => 
  465:                  {text => 'Course/Community defaults',
  466:                   help => 'Domain_Configuration_Course_Defaults',
  467:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  468:                               col2 => 'Value',},
  469:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  470:                               col2 => 'Value',},],
  471:                   print => \&print_coursedefaults,
  472:                   modify => \&modify_coursedefaults,
  473:                  },
  474:         'selfenrollment' => 
  475:                  {text   => 'Self-enrollment in Course/Community',
  476:                   help   => 'Domain_Configuration_Selfenrollment',
  477:                   header => [{col1 => 'Configuration Rights',
  478:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  479:                              {col1 => 'Defaults',
  480:                               col2 => 'Value'},
  481:                              {col1 => 'Self-enrollment validation (optional)',
  482:                               col2 => 'Value'},],
  483:                   print => \&print_selfenrollment,
  484:                   modify => \&modify_selfenrollment,
  485:                  },
  486:         'usersessions' =>
  487:                  {text  => 'User session hosting/offloading',
  488:                   help  => 'Domain_Configuration_User_Sessions',
  489:                   header => [{col1 => 'Domain server',
  490:                               col2 => 'Servers to offload sessions to when busy'},
  491:                              {col1 => 'Hosting of users from other domains',
  492:                               col2 => 'Rules'},
  493:                              {col1 => "Hosting domain's own users elsewhere",
  494:                               col2 => 'Rules'}],
  495:                   print => \&print_usersessions,
  496:                   modify => \&modify_usersessions,
  497:                  },
  498:         'loadbalancing' =>
  499:                  {text  => 'Dedicated Load Balancer(s)',
  500:                   help  => 'Domain_Configuration_Load_Balancing',
  501:                   header => [{col1 => 'Balancers',
  502:                               col2 => 'Default destinations',
  503:                               col3 => 'User affiliation',
  504:                               col4 => 'Overrides'},
  505:                             ],
  506:                   print => \&print_loadbalancing,
  507:                   modify => \&modify_loadbalancing,
  508:                  },
  509:          'ipaccess' =>
  510:                        {text => 'IP-based access control',
  511:                         help => 'Domain_Configuration_IP_Access',
  512:                         header => [{col1 => 'Setting',
  513:                                     col2 => 'Value'},],
  514:                         print  => \&print_ipaccess,
  515:                         modify => \&modify_ipaccess,
  516:                        },
  517:     );
  518:     if (keys(%servers) > 1) {
  519:         $prefs{'login'}  = { text   => 'Log-in page options',
  520:                              help   => 'Domain_Configuration_Login_Page',
  521:                             header => [{col1 => 'Log-in Service',
  522:                                         col2 => 'Server Setting',},
  523:                                        {col1 => 'Log-in Page Items',
  524:                                         col2 => ''},
  525:                                        {col1 => 'Log-in Help',
  526:                                         col2 => 'Value'},
  527:                                        {col1 => 'Custom HTML in document head',
  528:                                         col2 => 'Value'},
  529:                                        {col1 => 'SSO',
  530:                                         col2 => 'Dual login: SSO and non-SSO options'},
  531:                                       ],
  532:                             print => \&print_login,
  533:                             modify => \&modify_login,
  534:                            };
  535:     }
  536: 
  537:     my @roles = ('student','coordinator','author','admin');
  538:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  539:     &Apache::lonhtmlcommon::add_breadcrumb
  540:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  541:       text=>"Settings to display/modify"});
  542:     my $confname = $dom.'-domainconfig';
  543: 
  544:     if ($phase eq 'process') {
  545:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  546:                                                               \%prefs,\%domconfig,$confname,\@roles);
  547:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  548:             $r->rflush();
  549:             &devalidate_remote_domconfs($dom,$result);
  550:         }
  551:     } elsif ($phase eq 'display') {
  552:         my $js = &recaptcha_js().
  553:                  &toggle_display_js();
  554:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  555:             my ($othertitle,$usertypes,$types) =
  556:                 &Apache::loncommon::sorted_inst_types($dom);
  557:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  558:                                           $domconfig{'loadbalancing'}).
  559:                    &new_spares_js().
  560:                    &common_domprefs_js().
  561:                    &Apache::loncommon::javascript_array_indexof();
  562:         }
  563:         if (grep(/^requestcourses$/,@actions)) {
  564:             my $javascript_validations;
  565:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  566:             $js .= <<END;
  567: <script type="text/javascript">
  568: $javascript_validations
  569: </script>
  570: $coursebrowserjs
  571: END
  572:         } elsif (grep(/^ipaccess$/,@actions)) {
  573:             $js .= &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
  574:         }
  575:         if (grep(/^selfcreation$/,@actions)) {
  576:             $js .= &selfcreate_javascript();
  577:         }
  578:         if (grep(/^contacts$/,@actions)) {
  579:             $js .= &contacts_javascript();
  580:         }
  581:         if (grep(/^scantron$/,@actions)) {
  582:             $js .= &scantron_javascript();
  583:         }
  584:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  585:     } else {
  586: # check if domconfig user exists for the domain.
  587:         my $servadm = $r->dir_config('lonAdmEMail');
  588:         my ($configuserok,$author_ok,$switchserver) =
  589:             &config_check($dom,$confname,$servadm);
  590:         unless ($configuserok eq 'ok') {
  591:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  592:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  593:                           $confname).
  594:                       '<br />'
  595:             );
  596:             if ($switchserver) {
  597:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  598:                           '<br />'.
  599:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  600:                           '<br />'.
  601:                           &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).
  602:                           '<br />'.
  603:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  604:                 );
  605:             } else {
  606:                 $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.').
  607:                           '<br />'.
  608:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  609:                 );
  610:             }
  611:             $r->print(&Apache::loncommon::end_page());
  612:             return OK;
  613:         }
  614:         if (keys(%domconfig) == 0) {
  615:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  616:             my @ids=&Apache::lonnet::current_machine_ids();
  617:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  618:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  619:                 my @loginimages = ('img','logo','domlogo','login');
  620:                 my $custom_img_count = 0;
  621:                 foreach my $img (@loginimages) {
  622:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  623:                         $custom_img_count ++;
  624:                     }
  625:                 }
  626:                 foreach my $role (@roles) {
  627:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  628:                         $custom_img_count ++;
  629:                     }
  630:                 }
  631:                 if ($custom_img_count > 0) {
  632:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  633:                     my $switch_server = &check_switchserver($dom,$confname);
  634:                     $r->print(
  635:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  636:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  637:     &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 />'.
  638:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  639:                     if ($switch_server) {
  640:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  641:                     }
  642:                     $r->print(&Apache::loncommon::end_page());
  643:                     return OK;
  644:                 }
  645:             }
  646:         }
  647:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  648:     }
  649:     return OK;
  650: }
  651: 
  652: sub process_changes {
  653:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  654:     my %domconfig;
  655:     if (ref($values) eq 'HASH') {
  656:         %domconfig = %{$values};
  657:     }
  658:     my $output;
  659:     if ($action eq 'login') {
  660:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  661:     } elsif ($action eq 'rolecolors') {
  662:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  663:                                      $lastactref,%domconfig);
  664:     } elsif ($action eq 'quotas') {
  665:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  666:     } elsif ($action eq 'autoenroll') {
  667:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  668:     } elsif ($action eq 'autoupdate') {
  669:         $output = &modify_autoupdate($dom,%domconfig);
  670:     } elsif ($action eq 'autocreate') {
  671:         $output = &modify_autocreate($dom,%domconfig);
  672:     } elsif ($action eq 'directorysrch') {
  673:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  674:     } elsif ($action eq 'usercreation') {
  675:         $output = &modify_usercreation($dom,%domconfig);
  676:     } elsif ($action eq 'selfcreation') {
  677:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  678:     } elsif ($action eq 'usermodification') {
  679:         $output = &modify_usermodification($dom,%domconfig);
  680:     } elsif ($action eq 'contacts') {
  681:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  682:     } elsif ($action eq 'defaults') {
  683:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  684:     } elsif ($action eq 'scantron') {
  685:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  686:     } elsif ($action eq 'coursecategories') {
  687:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  688:     } elsif ($action eq 'serverstatuses') {
  689:         $output = &modify_serverstatuses($dom,%domconfig);
  690:     } elsif ($action eq 'requestcourses') {
  691:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  692:     } elsif ($action eq 'requestauthor') {
  693:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  694:     } elsif ($action eq 'helpsettings') {
  695:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  696:     } elsif ($action eq 'coursedefaults') {
  697:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  698:     } elsif ($action eq 'selfenrollment') {
  699:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  700:     } elsif ($action eq 'usersessions') {
  701:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  702:     } elsif ($action eq 'loadbalancing') {
  703:         $output = &modify_loadbalancing($dom,%domconfig);
  704:     } elsif ($action eq 'passwords') {
  705:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  706:     } elsif ($action eq 'wafproxy') {
  707:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  708:     } elsif ($action eq 'ipaccess') {
  709:         $output = &modify_ipaccess($dom,$lastactref,%domconfig);
  710:     }
  711:     return $output;
  712: }
  713: 
  714: sub print_config_box {
  715:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  716:     my $rowtotal = 0;
  717:     my $output;
  718:     if ($action eq 'coursecategories') {
  719:         $output = &coursecategories_javascript($settings);
  720:     } elsif ($action eq 'defaults') {
  721:         $output = &defaults_javascript($settings); 
  722:     } elsif ($action eq 'passwords') {
  723:         $output = &passwords_javascript();
  724:     } elsif ($action eq 'helpsettings') {
  725:         my (%privs,%levelscurrent);
  726:         my %full=();
  727:         my %levels=(
  728:                      course => {},
  729:                      domain => {},
  730:                      system => {},
  731:                    );
  732:         my $context = 'domain';
  733:         my $crstype = 'Course';
  734:         my $formname = 'display';
  735:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  736:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  737:         $output =
  738:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  739:                                                       \@templateroles);
  740:     } elsif ($action eq 'wafproxy') {
  741:         $output .= &wafproxy_javascript($dom);
  742:     } elsif ($action eq 'autoupdate') {
  743:         $output .= &autoupdate_javascript();
  744:     } elsif ($action eq 'autoenroll') {
  745:         $output .= &autoenroll_javascript();
  746:     } elsif ($action eq 'login') {
  747:         $output .= &saml_javascript();
  748:     } elsif ($action eq 'ipaccess') {
  749:         $output .= &ipaccess_javascript($settings);
  750:     }
  751:     $output .=
  752:          '<table class="LC_nested_outer">
  753:           <tr>
  754:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  755:            &mt($item->{text}).'&nbsp;'.
  756:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  757:           '</tr>';
  758:     $rowtotal ++;
  759:     my $numheaders = 1;
  760:     if (ref($item->{'header'}) eq 'ARRAY') {
  761:         $numheaders = scalar(@{$item->{'header'}});
  762:     }
  763:     if ($numheaders > 1) {
  764:         my $colspan = '';
  765:         my $rightcolspan = '';
  766:         my $leftnobr = '';  
  767:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  768:             ($action eq 'directorysrch') ||
  769:             (($action eq 'login') && ($numheaders < 5))) {
  770:             $colspan = ' colspan="2"';
  771:         }
  772:         if ($action eq 'usersessions') {
  773:             $rightcolspan = ' colspan="3"'; 
  774:         }
  775:         if ($action eq 'passwords') {
  776:             $leftnobr = ' LC_nobreak';
  777:         }
  778:         $output .= '
  779:           <tr>
  780:            <td>
  781:             <table class="LC_nested">
  782:              <tr class="LC_info_row">
  783:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  784:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  785:              </tr>';
  786:         $rowtotal ++;
  787:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  788:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  789:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  790:             ($action eq 'helpsettings') || ($action eq 'contacts') || ($action eq 'wafproxy')) {
  791:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  792:         } elsif ($action eq 'passwords') {
  793:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  794:         } elsif ($action eq 'coursecategories') {
  795:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  796:         } elsif ($action eq 'scantron') {
  797:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  798:         } elsif ($action eq 'login') {
  799:             if ($numheaders == 5) {
  800:                 $colspan = ' colspan="2"';
  801:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  802:             } else {
  803:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  804:             }
  805:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  806:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  807:         } elsif ($action eq 'rolecolors') {
  808:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  809:         }
  810:         $output .= '
  811:            </table>
  812:           </td>
  813:          </tr>
  814:          <tr>
  815:            <td>
  816:             <table class="LC_nested">
  817:              <tr class="LC_info_row">
  818:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  819:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  820:              </tr>';
  821:             $rowtotal ++;
  822:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  823:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  824:             ($action eq 'usersessions') || ($action eq 'coursecategories') ||
  825:             ($action eq 'contacts') || ($action eq 'passwords')) {
  826:             if ($action eq 'coursecategories') {
  827:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  828:                 $colspan = ' colspan="2"';
  829:             } elsif ($action eq 'passwords') {
  830:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  831:             } else {
  832:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  833:             }
  834:             $output .= '
  835:            </table>
  836:           </td>
  837:          </tr>
  838:          <tr>
  839:            <td>
  840:             <table class="LC_nested">
  841:              <tr class="LC_info_row">
  842:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  843:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  844:              </tr>'."\n";
  845:             if ($action eq 'coursecategories') {
  846:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  847:             } elsif (($action eq 'contacts') || ($action eq 'passwords')) {
  848:                 if ($action eq 'passwords') {
  849:                     $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
  850:                 } else {
  851:                     $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
  852:                 }
  853:                 $output .= '
  854:              </tr>
  855:             </table>
  856:            </td>
  857:           </tr>
  858:           <tr>
  859:            <td>
  860:             <table class="LC_nested">
  861:              <tr class="LC_info_row">
  862:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  863:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n";
  864:                 if ($action eq 'passwords') {
  865:                     $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
  866:                 } else {
  867:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  868:                 }
  869:                 $output .= '
  870:             </table>
  871:           </td>
  872:          </tr>
  873:          <tr>';
  874:             } else {
  875:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  876:             }
  877:             $rowtotal ++;
  878:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  879:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  880:                  ($action eq 'helpsettings') || ($action eq 'wafproxy')) {
  881:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  882:         } elsif ($action eq 'scantron') {
  883:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
  884:         } elsif ($action eq 'login') {
  885:             if ($numheaders == 5) {
  886:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  887:            </table>
  888:           </td>
  889:          </tr>
  890:          <tr>
  891:            <td>
  892:             <table class="LC_nested">
  893:              <tr class="LC_info_row">
  894:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  895:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  896:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  897:                 $rowtotal ++;
  898:             } else {
  899:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  900:             }
  901:             $output .= '
  902:            </table>
  903:           </td>
  904:          </tr>
  905:          <tr>
  906:            <td>
  907:             <table class="LC_nested">
  908:              <tr class="LC_info_row">';
  909:             if ($numheaders == 5) {
  910:                 $output .= '
  911:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  912:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  913:              </tr>';
  914:             } else {
  915:                 $output .= '
  916:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  917:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  918:              </tr>';
  919:             }
  920:             $rowtotal ++;
  921:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal).'
  922:            </table>
  923:           </td>
  924:          </tr>
  925:          <tr>
  926:            <td>
  927:             <table class="LC_nested">
  928:              <tr class="LC_info_row">';
  929:             if ($numheaders == 5) {
  930:                 $output .= '
  931:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  932:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  933:              </tr>';
  934:             } else {
  935:                 $output .= '
  936:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  937:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  938:              </tr>';
  939:             }
  940:             $rowtotal ++;
  941:             $output .= &print_login('saml',$dom,$confname,$phase,$settings,\$rowtotal);
  942:         } elsif ($action eq 'requestcourses') {
  943:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  944:             $rowtotal ++;
  945:             $output .= &print_studentcode($settings,\$rowtotal).'
  946:            </table>
  947:           </td>
  948:          </tr>
  949:          <tr>
  950:            <td>
  951:             <table class="LC_nested">
  952:              <tr class="LC_info_row">
  953:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  954:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  955:                        &textbookcourses_javascript($settings).
  956:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  957:             </table>
  958:            </td>
  959:           </tr>
  960:          <tr>
  961:            <td>
  962:             <table class="LC_nested">
  963:              <tr class="LC_info_row">
  964:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  965:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  966:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  967:             </table>
  968:            </td>
  969:           </tr>
  970:           <tr>
  971:            <td>
  972:             <table class="LC_nested">
  973:              <tr class="LC_info_row">
  974:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  975:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  976:              </tr>'.
  977:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  978:         } elsif ($action eq 'requestauthor') {
  979:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  980:             $rowtotal ++;
  981:         } elsif ($action eq 'rolecolors') {
  982:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  983:            </table>
  984:           </td>
  985:          </tr>
  986:          <tr>
  987:            <td>
  988:             <table class="LC_nested">
  989:              <tr class="LC_info_row">
  990:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  991:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  992:               <td class="LC_right_item" valign="top">'.
  993:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  994:              </tr>'.
  995:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  996:            </table>
  997:           </td>
  998:          </tr>
  999:          <tr>
 1000:            <td>
 1001:             <table class="LC_nested">
 1002:              <tr class="LC_info_row">
 1003:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1004:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1005:              </tr>'.
 1006:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1007:             $rowtotal += 2;
 1008:         }
 1009:     } else {
 1010:         $output .= '
 1011:           <tr>
 1012:            <td>
 1013:             <table class="LC_nested">
 1014:              <tr class="LC_info_row">';
 1015:         if ($action eq 'login') {
 1016:             $output .= '  
 1017:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1018:         } elsif ($action eq 'serverstatuses') {
 1019:             $output .= '
 1020:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1021:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1022: 
 1023:         } else {
 1024:             $output .= '
 1025:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1026:         }
 1027:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1028:             $output .= '<td class="LC_left_item" valign="top">'.
 1029:                        &mt($item->{'header'}->[0]->{'col2'});
 1030:             if ($action eq 'serverstatuses') {
 1031:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1032:             } 
 1033:         } else {
 1034:             $output .= '<td class="LC_right_item" valign="top">'.
 1035:                        &mt($item->{'header'}->[0]->{'col2'});
 1036:         }
 1037:         $output .= '</td>';
 1038:         if ($item->{'header'}->[0]->{'col3'}) {
 1039:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1040:                 $output .= '<td class="LC_left_item" valign="top">'.
 1041:                             &mt($item->{'header'}->[0]->{'col3'});
 1042:             } else {
 1043:                 $output .= '<td class="LC_right_item" valign="top">'.
 1044:                            &mt($item->{'header'}->[0]->{'col3'});
 1045:             }
 1046:             if ($action eq 'serverstatuses') {
 1047:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1048:             }
 1049:             $output .= '</td>';
 1050:         }
 1051:         if ($item->{'header'}->[0]->{'col4'}) {
 1052:             $output .= '<td class="LC_right_item" valign="top">'.
 1053:                        &mt($item->{'header'}->[0]->{'col4'});
 1054:         }
 1055:         $output .= '</tr>';
 1056:         $rowtotal ++;
 1057:         if ($action eq 'quotas') {
 1058:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1059:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1060:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') ||
 1061:                  ($action eq 'ipaccess')) {
 1062:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1063:         }
 1064:     }
 1065:     $output .= '
 1066:    </table>
 1067:   </td>
 1068:  </tr>
 1069: </table><br />';
 1070:     return ($output,$rowtotal);
 1071: }
 1072: 
 1073: sub print_login {
 1074:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1075:     my ($css_class,$datatable,$switchserver,%lt);
 1076:     my %choices = &login_choices();
 1077:     if (($caller eq 'help') || ($caller eq 'headtag') || ($caller eq 'saml')) {
 1078:         %lt = &login_file_options();
 1079:         $switchserver = &check_switchserver($dom,$confname);
 1080:     }
 1081: 
 1082:     if ($caller eq 'service') {
 1083:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1084:         my $choice = $choices{'disallowlogin'};
 1085:         $css_class = ' class="LC_odd_row"';
 1086:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1087:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1088:                       '<th>'.$choices{'server'}.'</th>'.
 1089:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1090:                       '<th>'.$choices{'custompath'}.'</th>'.
 1091:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1092:         my %disallowed;
 1093:         if (ref($settings) eq 'HASH') {
 1094:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1095:                %disallowed = %{$settings->{'loginvia'}};
 1096:             }
 1097:         }
 1098:         foreach my $lonhost (sort(keys(%servers))) {
 1099:             my $direct = 'selected="selected"';
 1100:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1101:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1102:                     $direct = '';
 1103:                 }
 1104:             }
 1105:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1106:                           '<td><select name="'.$lonhost.'_server">'.
 1107:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1108:                           '</option>';
 1109:             foreach my $hostid (sort(keys(%servers))) {
 1110:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1111:                 my $selected = '';
 1112:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1113:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1114:                         $selected = 'selected="selected"';
 1115:                     }
 1116:                 }
 1117:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1118:                               $servers{$hostid}.'</option>';
 1119:             }
 1120:             $datatable .= '</select></td>'.
 1121:                           '<td><select name="'.$lonhost.'_serverpath">';
 1122:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1123:                 my $pathname = $path;
 1124:                 if ($path eq 'custom') {
 1125:                     $pathname = &mt('Custom Path').' ->';
 1126:                 }
 1127:                 my $selected = '';
 1128:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1129:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1130:                         $selected = 'selected="selected"';
 1131:                     }
 1132:                 } elsif ($path eq '') {
 1133:                     $selected = 'selected="selected"';
 1134:                 }
 1135:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1136:             }
 1137:             $datatable .= '</select></td>';
 1138:             my ($custom,$exempt);
 1139:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1140:                 $custom = $disallowed{$lonhost}{'custompath'};
 1141:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1142:             }
 1143:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1144:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1145:                           '</tr>';
 1146:         }
 1147:         $datatable .= '</table></td></tr>';
 1148:         return $datatable;
 1149:     } elsif ($caller eq 'page') {
 1150:         my %defaultchecked = ( 
 1151:                                'coursecatalog' => 'on',
 1152:                                'helpdesk'      => 'on',
 1153:                                'adminmail'     => 'off',
 1154:                                'newuser'       => 'off',
 1155:                              );
 1156:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1157:         my (%checkedon,%checkedoff);
 1158:         foreach my $item (@toggles) {
 1159:             if ($defaultchecked{$item} eq 'on') { 
 1160:                 $checkedon{$item} = ' checked="checked" ';
 1161:                 $checkedoff{$item} = ' ';
 1162:             } elsif ($defaultchecked{$item} eq 'off') {
 1163:                 $checkedoff{$item} = ' checked="checked" ';
 1164:                 $checkedon{$item} = ' ';
 1165:             }
 1166:         }
 1167:         my @images = ('img','logo','domlogo','login');
 1168:         my @logintext = ('textcol','bgcol');
 1169:         my @bgs = ('pgbg','mainbg','sidebg');
 1170:         my @links = ('link','alink','vlink');
 1171:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1172:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1173:         my (%is_custom,%designs);
 1174:         my %defaults = (
 1175:                        font => $defaultdesign{'login.font'},
 1176:                        );
 1177:         foreach my $item (@images) {
 1178:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1179:             $defaults{'showlogo'}{$item} = 1;
 1180:         }
 1181:         foreach my $item (@bgs) {
 1182:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1183:         }
 1184:         foreach my $item (@logintext) {
 1185:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1186:         }
 1187:         foreach my $item (@links) {
 1188:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1189:         }
 1190:         if (ref($settings) eq 'HASH') {
 1191:             foreach my $item (@toggles) {
 1192:                 if ($settings->{$item} eq '1') {
 1193:                     $checkedon{$item} =  ' checked="checked" ';
 1194:                     $checkedoff{$item} = ' ';
 1195:                 } elsif ($settings->{$item} eq '0') {
 1196:                     $checkedoff{$item} =  ' checked="checked" ';
 1197:                     $checkedon{$item} = ' ';
 1198:                 }
 1199:             }
 1200:             foreach my $item (@images) {
 1201:                 if (defined($settings->{$item})) {
 1202:                     $designs{$item} = $settings->{$item};
 1203:                     $is_custom{$item} = 1;
 1204:                 }
 1205:                 if (defined($settings->{'showlogo'}{$item})) {
 1206:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1207:                 }
 1208:             }
 1209:             foreach my $item (@logintext) {
 1210:                 if ($settings->{$item} ne '') {
 1211:                     $designs{'logintext'}{$item} = $settings->{$item};
 1212:                     $is_custom{$item} = 1;
 1213:                 }
 1214:             }
 1215:             if ($settings->{'font'} ne '') {
 1216:                 $designs{'font'} = $settings->{'font'};
 1217:                 $is_custom{'font'} = 1;
 1218:             }
 1219:             foreach my $item (@bgs) {
 1220:                 if ($settings->{$item} ne '') {
 1221:                     $designs{'bgs'}{$item} = $settings->{$item};
 1222:                     $is_custom{$item} = 1;
 1223:                 }
 1224:             }
 1225:             foreach my $item (@links) {
 1226:                 if ($settings->{$item} ne '') {
 1227:                     $designs{'links'}{$item} = $settings->{$item};
 1228:                     $is_custom{$item} = 1;
 1229:                 }
 1230:             }
 1231:         } else {
 1232:             if ($designhash{$dom.'.login.font'} ne '') {
 1233:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1234:                 $is_custom{'font'} = 1;
 1235:             }
 1236:             foreach my $item (@images) {
 1237:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1238:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1239:                     $is_custom{$item} = 1;
 1240:                 }
 1241:             }
 1242:             foreach my $item (@bgs) {
 1243:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1244:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1245:                     $is_custom{$item} = 1;
 1246:                 }
 1247:             }
 1248:             foreach my $item (@links) {
 1249:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1250:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1251:                     $is_custom{$item} = 1;
 1252:                 }
 1253:             }
 1254:         }
 1255:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1256:                                                       logo => 'Institution Logo',
 1257:                                                       domlogo => 'Domain Logo',
 1258:                                                       login => 'Login box');
 1259:         my $itemcount = 1;
 1260:         foreach my $item (@toggles) {
 1261:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1262:             $datatable .=  
 1263:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1264:                 '</td><td>'.
 1265:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1266:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1267:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1268:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1269:                 '</tr>';
 1270:             $itemcount ++;
 1271:         }
 1272:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1273:         $datatable .= '</tr></table></td></tr>';
 1274:     } elsif ($caller eq 'help') {
 1275:         my ($defaulturl,$defaulttype,%url,%type,%langchoices);
 1276:         my $itemcount = 1;
 1277:         $defaulturl = '/adm/loginproblems.html';
 1278:         $defaulttype = 'default';
 1279:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1280:         my @currlangs;
 1281:         if (ref($settings) eq 'HASH') {
 1282:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1283:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1284:                     next if ($settings->{'helpurl'}{$key} eq '');
 1285:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1286:                     $type{$key} = 'custom';
 1287:                     unless ($key eq 'nolang') {
 1288:                         push(@currlangs,$key);
 1289:                     }
 1290:                 }
 1291:             } elsif ($settings->{'helpurl'} ne '') {
 1292:                 $type{'nolang'} = 'custom';
 1293:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1294:             }
 1295:         }
 1296:         foreach my $lang ('nolang',sort(@currlangs)) {
 1297:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1298:             $datatable .= '<tr'.$css_class.'>';
 1299:             if ($url{$lang} eq '') {
 1300:                 $url{$lang} = $defaulturl;
 1301:             }
 1302:             if ($type{$lang} eq '') {
 1303:                 $type{$lang} = $defaulttype;
 1304:             }
 1305:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1306:             if ($lang eq 'nolang') {
 1307:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1308:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1309:             } else {
 1310:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1311:                                   $langchoices{$lang},
 1312:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1313:             }
 1314:             $datatable .= '</span></td>'."\n".
 1315:                           '<td class="LC_left_item">';
 1316:             if ($type{$lang} eq 'custom') {
 1317:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1318:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1319:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1320:             } else {
 1321:                 $datatable .= $lt{'upl'};
 1322:             }
 1323:             $datatable .='<br />';
 1324:             if ($switchserver) {
 1325:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1326:             } else {
 1327:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1328:             }
 1329:             $datatable .= '</td></tr>';
 1330:             $itemcount ++;
 1331:         }
 1332:         my @addlangs;
 1333:         foreach my $lang (sort(keys(%langchoices))) {
 1334:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1335:             push(@addlangs,$lang);
 1336:         }
 1337:         if (@addlangs > 0) {
 1338:             my %toadd;
 1339:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1340:             $toadd{''} = &mt('Select');
 1341:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1342:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1343:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1344:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1345:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1346:             if ($switchserver) {
 1347:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1348:             } else {
 1349:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1350:             }
 1351:             $datatable .= '</td></tr>';
 1352:             $itemcount ++;
 1353:         }
 1354:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1355:     } elsif ($caller eq 'headtag') {
 1356:         my %domservers = &Apache::lonnet::get_servers($dom);
 1357:         my $choice = $choices{'headtag'};
 1358:         $css_class = ' class="LC_odd_row"';
 1359:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1360:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1361:                       '<th>'.$choices{'current'}.'</th>'.
 1362:                       '<th>'.$choices{'action'}.'</th>'.
 1363:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1364:         my (%currurls,%currexempt);
 1365:         if (ref($settings) eq 'HASH') {
 1366:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1367:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1368:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1369:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1370:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1371:                     }
 1372:                 }
 1373:             }
 1374:         }
 1375:         foreach my $lonhost (sort(keys(%domservers))) {
 1376:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1377:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1378:             if ($currurls{$lonhost}) {
 1379:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1380:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1381:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1382:                               '">'.$lt{'curr'}.'</a></td>'.
 1383:                               '<td><span class="LC_nobreak"><label>'.
 1384:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1385:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1386:             } else {
 1387:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1388:             }
 1389:             $datatable .='<br />';
 1390:             if ($switchserver) {
 1391:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1392:             } else {
 1393:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1394:             }
 1395:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1396:         }
 1397:         $datatable .= '</table></td></tr>';
 1398:     } elsif ($caller eq 'saml') {
 1399:         my %domservers = &Apache::lonnet::get_servers($dom);
 1400:         $datatable .= '<tr><td colspan="3" style="text-align: left">'.
 1401:                       '<table><tr><th>'.$choices{'hostid'}.'</th>'.
 1402:                       '<th>'.$choices{'samllanding'}.'</th>'.
 1403:                       '<th>'.$choices{'samloptions'}.'</th></tr>'."\n";
 1404:         my (%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlnotsso,%styleon,%styleoff);
 1405:         foreach my $lonhost (keys(%domservers)) {
 1406:             $samlurl{$lonhost} = '/adm/sso';
 1407:             $styleon{$lonhost} = 'display:none';
 1408:             $styleoff{$lonhost} = '';
 1409:         }
 1410:         if (ref($settings->{'saml'}) eq 'HASH') {
 1411:             foreach my $lonhost (keys(%{$settings->{'saml'}})) {
 1412:                 if (ref($settings->{'saml'}{$lonhost}) eq 'HASH') {
 1413:                     $saml{$lonhost} = 1;
 1414:                     $samltext{$lonhost} = $settings->{'saml'}{$lonhost}{'text'};
 1415:                     $samlimg{$lonhost} = $settings->{'saml'}{$lonhost}{'img'};
 1416:                     $samlalt{$lonhost} = $settings->{'saml'}{$lonhost}{'alt'};
 1417:                     $samlurl{$lonhost} = $settings->{'saml'}{$lonhost}{'url'};
 1418:                     $samltitle{$lonhost} = $settings->{'saml'}{$lonhost}{'title'};
 1419:                     $samlnotsso{$lonhost} = $settings->{'saml'}{$lonhost}{'notsso'};
 1420:                     $styleon{$lonhost} = '';
 1421:                     $styleoff{$lonhost} = 'display:none';
 1422:                 } else {
 1423:                     $styleon{$lonhost} = 'display:none';
 1424:                     $styleoff{$lonhost} = '';
 1425:                 }
 1426:             }
 1427:         }
 1428:         my $itemcount = 1;
 1429:         foreach my $lonhost (sort(keys(%domservers))) {
 1430:             my $samlon = ' ';
 1431:             my $samloff = ' checked="checked" ';
 1432:             if ($saml{$lonhost}) {
 1433:                 $samlon = $samloff;
 1434:                 $samloff = ' ';
 1435:             }
 1436:             my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1437:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.$domservers{$lonhost}.'</span></td>'.
 1438:                           '<td><span class="LC_nobreak"><label><input type="radio" name="saml_'.$lonhost.'"'.$samloff.
 1439:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="0" />'.
 1440:                           &mt('No').'</label>'.('&nbsp;'x2).
 1441:                           '<label><input type="radio" name="saml_'.$lonhost.'"'.$samlon.
 1442:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="1" />'.
 1443:                           &mt('Yes').'</label></span></td>'.
 1444:                           '<td id="samloptionson_'.$lonhost.'" style="'.$styleon{$lonhost}.'" width="100%">'.
 1445:                           '<table><tr><th colspan="5" align="center">'.&mt('SSO').'</th><th align="center">'.
 1446:                           '<span class="LC_nobreak">'.&mt('Non-SSO').'</span></th></tr>'.
 1447:                           '<tr><th>'.&mt('Text').'</th><th>'.&mt('Image').'</th>'.
 1448:                           '<th>'.&mt('Alt Text').'</th><th>'.&mt('URL').'</th>'.
 1449:                           '<th>'.&mt('Tool Tip').'</th><th>'.&mt('Text').'</th></tr>'.
 1450:                           '<tr'.$css_class.'><td><input type="text" name="saml_text_'.$lonhost.'" size="8" value="'.
 1451:                           $samltext{$lonhost}.'" /></td><td>';
 1452:             if ($samlimg{$lonhost}) {
 1453:                 $datatable .= '<img src="'.$samlimg{$lonhost}.'" /><br />'.
 1454:                               '<span class="LC_nobreak"><label>'.
 1455:                               '<input type="checkbox" name="saml_img_del" value="'.$lonhost.'" />'.
 1456:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1457:             } else {
 1458:                 $datatable .= $lt{'upl'};
 1459:             }
 1460:             $datatable .='<br />';
 1461:             if ($switchserver) {
 1462:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1463:             } else {
 1464:                 $datatable .= '<input type="file" name="saml_img_'.$lonhost.'" />';
 1465:             }
 1466:             $datatable .= '</td>'.
 1467:                           '<td><input type="text" name="saml_alt_'.$lonhost.'" size="20" '.
 1468:                           'value="'.$samlalt{$lonhost}.'" /></td>'.
 1469:                           '<td><input type="text" name="saml_url_'.$lonhost.'" size="8" '.
 1470:                           'value="'.$samlurl{$lonhost}.'" /></td>'.
 1471:                           '<td><textarea name="saml_title_'.$lonhost.'" rows="3" cols="15">'.
 1472:                           $samltitle{$lonhost}.'</textarea></td>'.
 1473:                           '<td><input type="text" name="saml_notsso_'.$lonhost.'" size="8" '.
 1474:                           'value="'.$samlnotsso{$lonhost}.'" /></td></tr>'.
 1475:                           '</table></td>'.
 1476:                           '<td id="samloptionsoff_'.$lonhost.'" style="'.$styleoff{$lonhost}.'" width="100%">&nbsp;</td></tr>';
 1477:            $itemcount ++;
 1478:         }
 1479:         $datatable .= '</table></td></tr>';
 1480:     }
 1481:     return $datatable;
 1482: }
 1483: 
 1484: sub login_choices {
 1485:     my %choices =
 1486:         &Apache::lonlocal::texthash (
 1487:             coursecatalog => 'Display Course/Community Catalog link?',
 1488:             adminmail     => "Display Administrator's E-mail Address?",
 1489:             helpdesk      => 'Display "Contact Helpdesk" link',
 1490:             disallowlogin => "Login page requests redirected",
 1491:             hostid        => "Server",
 1492:             server        => "Redirect to:",
 1493:             serverpath    => "Path",
 1494:             custompath    => "Custom", 
 1495:             exempt        => "Exempt IP(s)",
 1496:             directlogin   => "No redirect",
 1497:             newuser       => "Link to create a user account",
 1498:             img           => "Header",
 1499:             logo          => "Main Logo",
 1500:             domlogo       => "Domain Logo",
 1501:             login         => "Log-in Header", 
 1502:             textcol       => "Text color",
 1503:             bgcol         => "Box color",
 1504:             bgs           => "Background colors",
 1505:             links         => "Link colors",
 1506:             font          => "Font color",
 1507:             pgbg          => "Header",
 1508:             mainbg        => "Page",
 1509:             sidebg        => "Login box",
 1510:             link          => "Link",
 1511:             alink         => "Active link",
 1512:             vlink         => "Visited link",
 1513:             headtag       => "Custom markup",
 1514:             action        => "Action",
 1515:             current       => "Current",
 1516:             samllanding   => "Dual login?",
 1517:             samloptions   => "Options",
 1518:         );
 1519:     return %choices;
 1520: }
 1521: 
 1522: sub login_file_options {
 1523:       return &Apache::lonlocal::texthash(
 1524:                                            del     => 'Delete?',
 1525:                                            rep     => 'Replace:',
 1526:                                            upl     => 'Upload:',
 1527:                                            curr    => 'View contents',
 1528:                                            default => 'Default',
 1529:                                            custom  => 'Custom',
 1530:                                            none    => 'None',
 1531:       );
 1532: }
 1533: 
 1534: sub print_ipaccess {
 1535:     my ($dom,$settings,$rowtotal) = @_;
 1536:     my $css_class;
 1537:     my $itemcount = 0;
 1538:     my $datatable;
 1539:     my %ordered;
 1540:     if (ref($settings) eq 'HASH') {
 1541:         foreach my $item (keys(%{$settings})) {
 1542:             if (ref($settings->{$item}) eq 'HASH') {
 1543:                 my $num = $settings->{$item}{'order'};
 1544:                 if ($num eq '') {
 1545:                     $num = scalar(keys(%{$settings}));
 1546:                 }
 1547:                 $ordered{$num} = $item;
 1548:             }
 1549:         }
 1550:     }
 1551:     my $maxnum = scalar(keys(%ordered));
 1552:     if (keys(%ordered)) {
 1553:         my @items = sort { $a <=> $b } keys(%ordered);
 1554:         for (my $i=0; $i<@items; $i++) {
 1555:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1556:             my $item = $ordered{$items[$i]};
 1557:             my ($name,$ipranges,%commblocks,%courses);
 1558:             if (ref($settings->{$item}) eq 'HASH') {
 1559:                 $name = $settings->{$item}->{'name'};
 1560:                 $ipranges = $settings->{$item}->{'ip'};
 1561:                 if (ref($settings->{$item}->{'commblocks'}) eq 'HASH') {
 1562:                     %commblocks = %{$settings->{$item}->{'commblocks'}};
 1563:                 }
 1564:                 if (ref($settings->{$item}->{'courses'}) eq 'HASH') {
 1565:                     %courses = %{$settings->{$item}->{'courses'}};
 1566:                 }
 1567:             }
 1568:             my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_".$item."'".');"';
 1569:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 1570:                          .'<select name="ipaccess_pos_'.$item.'"'.$chgstr.'>';
 1571:             for (my $k=0; $k<=$maxnum; $k++) {
 1572:                 my $vpos = $k+1;
 1573:                 my $selstr;
 1574:                 if ($k == $i) {
 1575:                     $selstr = ' selected="selected" ';
 1576:                 }
 1577:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1578:             }
 1579:             $datatable .= '</select>'.('&nbsp;'x2).
 1580:                 '<label><input type="checkbox" name="ipaccess_del" value="'.$item.'" />'.
 1581:                 &mt('Delete?').'</label></span></td>'.
 1582:                 '<td colspan="2"><input type="hidden" name="ipaccess_id_'.$i.'" value="'.$item.'" />'.
 1583:                 &ipaccess_options($i,$itemcount,$dom,$name,$ipranges,\%commblocks,\%courses).
 1584:                 '</td></tr>';
 1585:             $itemcount ++;
 1586:         }
 1587:     }
 1588:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1589:     my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_add'".');"';
 1590:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 1591:                   '<input type="hidden" name="ipaccess_maxnum" value="'.$maxnum.'" />'."\n".
 1592:                   '<select name="ipaccess_pos_add"'.$chgstr.'>';
 1593:     for (my $k=0; $k<$maxnum+1; $k++) {
 1594:         my $vpos = $k+1;
 1595:         my $selstr;
 1596:         if ($k == $maxnum) {
 1597:             $selstr = ' selected="selected" ';
 1598:         }
 1599:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1600:     }
 1601:     $datatable .= '</select>&nbsp;'."\n".
 1602:                   '<input type="checkbox" name="ipaccess_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 1603:                   '<td colspan="2">'.
 1604:                   &ipaccess_options('add',$itemcount,$dom).
 1605:                   '</td>'."\n".
 1606:                   '</tr>'."\n";
 1607:     $$rowtotal ++;
 1608:     return $datatable;
 1609: }
 1610: 
 1611: sub ipaccess_options {
 1612:     my ($num,$itemcount,$dom,$name,$ipranges,$blocksref,$coursesref) = @_;
 1613:     my (%currblocks,%currcourses,$output);
 1614:     if (ref($blocksref) eq 'HASH') {
 1615:         %currblocks = %{$blocksref};
 1616:     }
 1617:     if (ref($coursesref) eq 'HASH') {
 1618:         %currcourses = %{$coursesref};
 1619:     }
 1620:     $output = '<fieldset><legend>'.&mt('Location(s)').'</legend>'.
 1621:               '<span class="LC_nobreak">'.&mt('Name').':&nbsp;'.
 1622:               '<input type="text" name="ipaccess_name_'.$num.'" value="'.$name.'" />'.
 1623:               '</span></fieldset>'.
 1624:               '<fieldset><legend>'.&mt('IP Range(s)').'</legend>'.
 1625:               &mt('Format for each IP range').': '.&mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 1626:               &mt('Range(s) will be stored as IP netblock(s) in CIDR notation (comma separated)').'<br />'.
 1627:               '<textarea name="ipaccess_range_'.$num.'" rows="3" cols="80">'.
 1628:               $ipranges.'</textarea></fieldset>'.
 1629:               '<fieldset><legend>'.&mt('Functionality Blocked?').'</legend>'.
 1630:               &blocker_checkboxes($num,$blocksref).'</fieldset>'.
 1631:               '<fieldset><legend>'.&mt('Courses/Communities allowed').'</legend>'.
 1632:               '<table>';
 1633:     foreach my $cid (sort(keys(%currcourses))) {
 1634:         my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
 1635:         $output .= '<tr><td><span class="LC_nobreak">'.
 1636:                    '<label><input type="checkbox" name="ipaccess_course_delete_'.$num.'" value="'.$cid.'" />'.
 1637:                    &mt('Delete?').'&nbsp;<span class="LC_cusr_emph">'.$courseinfo{'description'}.'</span></label></span>'.
 1638:                    ' <span class="LC_fontsize_medium">('.$cid.')</span></td></tr>';
 1639:     }
 1640:     $output .= '<tr><td><span class="LC_nobreak">'.&mt('Add').':&nbsp;'.
 1641:                '<input type="text" name="ipaccess_cdesc_'.$num.'" value="" onfocus="this.blur();opencrsbrowser('."'display','ipaccess_cnum_$num','ipaccess_cdom_$num','ipaccess_cdesc_$num'".');" />'.
 1642:                 &Apache::loncommon::selectcourse_link('display','ipaccess_cnum_'.$num,'ipaccess_cdom_'.$num,'ipaccess_cdesc_'.$num,$dom,undef,'Course/Community').
 1643:                '<input type="hidden" name="ipaccess_cnum_'.$num.'" value="" />'.
 1644:                '<input type="hidden" name="ipaccess_cdom_'.$num.'" value="" />'.
 1645:                '</span></td></tr></table>'."\n".
 1646:                '</fieldset>';
 1647:     return $output;
 1648: }
 1649: 
 1650: sub blocker_checkboxes {
 1651:     my ($num,$blocks) = @_;
 1652:     my ($typeorder,$types) = &commblocktype_text();
 1653:     my $numinrow = 6;
 1654:     my $output = '<table>';
 1655:     for (my $i=0; $i<@{$typeorder}; $i++) {
 1656:         my $block = $typeorder->[$i];
 1657:         my $blockstatus;
 1658:         if (ref($blocks) eq 'HASH') {
 1659:             if ($blocks->{$block} eq 'on') {
 1660:                 $blockstatus = 'checked="checked"';
 1661:             }
 1662:         }
 1663:         my $rem = $i%($numinrow);
 1664:         if ($rem == 0) {
 1665:             if ($i > 0) {
 1666:                 $output .= '</tr>';
 1667:             }
 1668:             $output .= '<tr>';
 1669:         }
 1670:         if ($i == scalar(@{$typeorder})-1) {
 1671:             my $colsleft = $numinrow-$rem;
 1672:             if ($colsleft > 1) {
 1673:                 $output .= '<td colspan="'.$colsleft.'">';
 1674:             } else {
 1675:                 $output .= '<td>';
 1676:             }
 1677:         } else {
 1678:             $output .= '<td>';
 1679:         }
 1680:         my $item = 'ipaccess_block_'.$num;
 1681:         if ($blockstatus) {
 1682:             $blockstatus = ' '.$blockstatus;
 1683:         }
 1684:         $output .= '<span class="LC_nobreak"><label>'."\n".
 1685:                    '<input type="checkbox" name="'.$item.'"'.
 1686:                    $blockstatus.' value="'.$block.'"'.' />'.
 1687:                    $types->{$block}.'</label></span>'."\n".
 1688:                    '<br /></td>';
 1689:     }
 1690:     $output .= '</tr></table>';
 1691:     return $output;
 1692: }
 1693: 
 1694: sub commblocktype_text {
 1695:     my %types = &Apache::lonlocal::texthash(
 1696:         'com' => 'Messaging',
 1697:         'chat' => 'Chat Room',
 1698:         'boards' => 'Discussion',
 1699:         'port' => 'Portfolio',
 1700:         'groups' => 'Groups',
 1701:         'blogs' => 'Blogs',
 1702:         'about' => 'User Information',
 1703:         'printout' => 'Printouts',
 1704:         'passwd' => 'Change Password',
 1705:         'grades' => 'Gradebook',
 1706:         'search' => 'Course search',
 1707:         'wishlist' => 'Stored links',
 1708:         'annotate' => 'Annotations',
 1709:     );
 1710:     my $typeorder = ['com','chat','boards','port','groups','blogs','about','wishlist','printout','grades','search','annotate','passwd'];
 1711:     return ($typeorder,\%types);
 1712: }
 1713: 
 1714: sub print_rolecolors {
 1715:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1716:     my %choices = &color_font_choices();
 1717:     my @bgs = ('pgbg','tabbg','sidebg');
 1718:     my @links = ('link','alink','vlink');
 1719:     my @images = ('img');
 1720:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1721:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1722:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1723:     my (%is_custom,%designs);
 1724:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1725:     if (ref($settings) eq 'HASH') {
 1726:         if (ref($settings->{$role}) eq 'HASH') {
 1727:             if ($settings->{$role}->{'img'} ne '') {
 1728:                 $designs{'img'} = $settings->{$role}->{'img'};
 1729:                 $is_custom{'img'} = 1;
 1730:             }
 1731:             if ($settings->{$role}->{'font'} ne '') {
 1732:                 $designs{'font'} = $settings->{$role}->{'font'};
 1733:                 $is_custom{'font'} = 1;
 1734:             }
 1735:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1736:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1737:                 $is_custom{'fontmenu'} = 1;
 1738:             }
 1739:             foreach my $item (@bgs) {
 1740:                 if ($settings->{$role}->{$item} ne '') {
 1741:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1742:                     $is_custom{$item} = 1;
 1743:                 }
 1744:             }
 1745:             foreach my $item (@links) {
 1746:                 if ($settings->{$role}->{$item} ne '') {
 1747:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1748:                     $is_custom{$item} = 1;
 1749:                 }
 1750:             }
 1751:         }
 1752:     } else {
 1753:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1754:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1755:             $is_custom{'img'} = 1;
 1756:         }
 1757:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1758:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1759:             $is_custom{'fontmenu'} = 1; 
 1760:         }
 1761:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1762:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1763:             $is_custom{'font'} = 1;
 1764:         }
 1765:         foreach my $item (@bgs) {
 1766:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1767:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1768:                 $is_custom{$item} = 1;
 1769:             
 1770:             }
 1771:         }
 1772:         foreach my $item (@links) {
 1773:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1774:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1775:                 $is_custom{$item} = 1;
 1776:             }
 1777:         }
 1778:     }
 1779:     my $itemcount = 1;
 1780:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1781:     $datatable .= '</tr></table></td></tr>';
 1782:     return $datatable;
 1783: }
 1784: 
 1785: sub role_defaults {
 1786:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1787:     my %defaults;
 1788:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1789:         return %defaults;
 1790:     }
 1791:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1792:     if ($role eq 'login') {
 1793:         %defaults = (
 1794:                        font => $defaultdesign{$role.'.font'},
 1795:                     );
 1796:         if (ref($logintext) eq 'ARRAY') {
 1797:             foreach my $item (@{$logintext}) {
 1798:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1799:             }
 1800:         }
 1801:         foreach my $item (@{$images}) {
 1802:             $defaults{'showlogo'}{$item} = 1;
 1803:         }
 1804:     } else {
 1805:         %defaults = (
 1806:                        img => $defaultdesign{$role.'.img'},
 1807:                        font => $defaultdesign{$role.'.font'},
 1808:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1809:                     );
 1810:     }
 1811:     foreach my $item (@{$bgs}) {
 1812:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1813:     }
 1814:     foreach my $item (@{$links}) {
 1815:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1816:     }
 1817:     foreach my $item (@{$images}) {
 1818:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1819:     }
 1820:     return %defaults;
 1821: }
 1822: 
 1823: sub display_color_options {
 1824:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1825:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1826:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1827:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1828:     my $datatable = '<tr'.$css_class.'>'.
 1829:         '<td>'.$choices->{'font'}.'</td>';
 1830:     if (!$is_custom->{'font'}) {
 1831:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1832:     } else {
 1833:         $datatable .= '<td>&nbsp;</td>';
 1834:     }
 1835:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1836: 
 1837:     $datatable .= '<td><span class="LC_nobreak">'.
 1838:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1839:                   ' value="'.$current_color.'" />&nbsp;'.
 1840:                   '&nbsp;</span></td></tr>';
 1841:     unless ($role eq 'login') { 
 1842:         $datatable .= '<tr'.$css_class.'>'.
 1843:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1844:         if (!$is_custom->{'fontmenu'}) {
 1845:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1846:         } else {
 1847:             $datatable .= '<td>&nbsp;</td>';
 1848:         }
 1849: 	$current_color = $designs->{'fontmenu'} ?
 1850: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1851:         $datatable .= '<td><span class="LC_nobreak">'.
 1852:                       '<input class="colorchooser" type="text" size="10" name="'
 1853: 		      .$role.'_fontmenu"'.
 1854:                       ' value="'.$current_color.'" />&nbsp;'.
 1855:                       '&nbsp;</span></td></tr>';
 1856:     }
 1857:     my $switchserver = &check_switchserver($dom,$confname);
 1858:     foreach my $img (@{$images}) {
 1859: 	$itemcount ++;
 1860:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1861:         $datatable .= '<tr'.$css_class.'>'.
 1862:                       '<td>'.$choices->{$img};
 1863:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1864:         if ($role eq 'login') {
 1865:             if ($img eq 'login') {
 1866:                 $login_hdr_pick =
 1867:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1868:                 $logincolors =
 1869:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1870:                                        $designs,$defaults);
 1871:             } elsif ($img ne 'domlogo') {
 1872:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1873:             }
 1874:         }
 1875:         $datatable .= '</td>';
 1876:         if ($designs->{$img} ne '') {
 1877:             $imgfile = $designs->{$img};
 1878: 	    $img_import = ($imgfile =~ m{^/adm/});
 1879:         } else {
 1880:             $imgfile = $defaults->{$img};
 1881:         }
 1882:         if ($imgfile) {
 1883:             my ($showfile,$fullsize);
 1884:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1885:                 my $urldir = $1;
 1886:                 my $filename = $2;
 1887:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1888:                 if (@info) {
 1889:                     my $thumbfile = 'tn-'.$filename;
 1890:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1891:                     if (@thumb) {
 1892:                         $showfile = $urldir.'/'.$thumbfile;
 1893:                     } else {
 1894:                         $showfile = $imgfile;
 1895:                     }
 1896:                 } else {
 1897:                     $showfile = '';
 1898:                 }
 1899:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1900:                 $showfile = $imgfile;
 1901:                 my $imgdir = $1;
 1902:                 my $filename = $2;
 1903:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1904:                     $showfile = "/$imgdir/tn-".$filename;
 1905:                 } else {
 1906:                     my $input = $londocroot.$imgfile;
 1907:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1908:                     if (!-e $output) {
 1909:                         my ($width,$height) = &thumb_dimensions();
 1910:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1911:                         if ($fullwidth ne '' && $fullheight ne '') {
 1912:                             if ($fullwidth > $width && $fullheight > $height) { 
 1913:                                 my $size = $width.'x'.$height;
 1914:                                 my @args = ('convert','-sample',$size,$input,$output);
 1915:                                 system({$args[0]} @args);
 1916:                                 $showfile = "/$imgdir/tn-".$filename;
 1917:                             }
 1918:                         }
 1919:                     }
 1920:                 }
 1921:             }
 1922:             if ($showfile) {
 1923:                 if ($showfile =~ m{^/(adm|res)/}) {
 1924:                     if ($showfile =~ m{^/res/}) {
 1925:                         my $local_showfile =
 1926:                             &Apache::lonnet::filelocation('',$showfile);
 1927:                         &Apache::lonnet::repcopy($local_showfile);
 1928:                     }
 1929:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1930:                 }
 1931:                 if ($imgfile) {
 1932:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1933:                         if ($imgfile =~ m{^/res/}) {
 1934:                             my $local_imgfile =
 1935:                                 &Apache::lonnet::filelocation('',$imgfile);
 1936:                             &Apache::lonnet::repcopy($local_imgfile);
 1937:                         }
 1938:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1939:                     } else {
 1940:                         $fullsize = $imgfile;
 1941:                     }
 1942:                 }
 1943:                 $datatable .= '<td>';
 1944:                 if ($img eq 'login') {
 1945:                     $datatable .= $login_hdr_pick;
 1946:                 } 
 1947:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1948:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1949:             } else {
 1950:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1951:                               &mt('Upload:').'<br />';
 1952:             }
 1953:         } else {
 1954:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1955:                           &mt('Upload:').'<br />';
 1956:         }
 1957:         if ($switchserver) {
 1958:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1959:         } else {
 1960:             if ($img ne 'login') { # suppress file selection for Log-in header
 1961:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1962:             }
 1963:         }
 1964:         $datatable .= '</td></tr>';
 1965:     }
 1966:     $itemcount ++;
 1967:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1968:     $datatable .= '<tr'.$css_class.'>'.
 1969:                   '<td>'.$choices->{'bgs'}.'</td>';
 1970:     my $bgs_def;
 1971:     foreach my $item (@{$bgs}) {
 1972:         if (!$is_custom->{$item}) {
 1973:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span class="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 1974:         }
 1975:     }
 1976:     if ($bgs_def) {
 1977:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1978:     } else {
 1979:         $datatable .= '<td>&nbsp;</td>';
 1980:     }
 1981:     $datatable .= '<td class="LC_right_item">'.
 1982:                   '<table border="0"><tr>';
 1983: 
 1984:     foreach my $item (@{$bgs}) {
 1985:         $datatable .= '<td align="center">'.$choices->{$item};
 1986: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1987:         if ($designs->{'bgs'}{$item}) {
 1988:             $datatable .= '&nbsp;';
 1989:         }
 1990:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1991:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1992:     }
 1993:     $datatable .= '</tr></table></td></tr>';
 1994:     $itemcount ++;
 1995:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1996:     $datatable .= '<tr'.$css_class.'>'.
 1997:                   '<td>'.$choices->{'links'}.'</td>';
 1998:     my $links_def;
 1999:     foreach my $item (@{$links}) {
 2000:         if (!$is_custom->{$item}) {
 2001:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 2002:         }
 2003:     }
 2004:     if ($links_def) {
 2005:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 2006:     } else {
 2007:         $datatable .= '<td>&nbsp;</td>';
 2008:     }
 2009:     $datatable .= '<td class="LC_right_item">'.
 2010:                   '<table border="0"><tr>';
 2011:     foreach my $item (@{$links}) {
 2012: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 2013:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 2014:         if ($designs->{'links'}{$item}) {
 2015:             $datatable.='&nbsp;';
 2016:         }
 2017:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 2018:                       '" /></td>';
 2019:     }
 2020:     $$rowtotal += $itemcount;
 2021:     return $datatable;
 2022: }
 2023: 
 2024: sub logo_display_options {
 2025:     my ($img,$defaults,$designs) = @_;
 2026:     my $checkedon;
 2027:     if (ref($defaults) eq 'HASH') {
 2028:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 2029:             if ($defaults->{'showlogo'}{$img}) {
 2030:                 $checkedon = 'checked="checked" ';     
 2031:             }
 2032:         } 
 2033:     }
 2034:     if (ref($designs) eq 'HASH') {
 2035:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 2036:             if (defined($designs->{'showlogo'}{$img})) {
 2037:                 if ($designs->{'showlogo'}{$img} == 0) {
 2038:                     $checkedon = '';
 2039:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 2040:                     $checkedon = 'checked="checked" ';
 2041:                 }
 2042:             }
 2043:         }
 2044:     }
 2045:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 2046:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 2047:            &mt('show').'</label>'."\n";
 2048: }
 2049: 
 2050: sub login_header_options  {
 2051:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 2052:     my $output = '';
 2053:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 2054:         $output .= &mt('Text default(s):').'<br />';
 2055:         if (!$is_custom->{'textcol'}) {
 2056:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 2057:                        '&nbsp;&nbsp;&nbsp;';
 2058:         }
 2059:         if (!$is_custom->{'bgcol'}) {
 2060:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 2061:                        '<span id="css_'.$role.'_font" style="background-color: '.
 2062:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 2063:         }
 2064:         $output .= '<br />';
 2065:     }
 2066:     $output .='<br />';
 2067:     return $output;
 2068: }
 2069: 
 2070: sub login_text_colors {
 2071:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 2072:     my $color_menu = '<table border="0"><tr>';
 2073:     foreach my $item (@{$logintext}) {
 2074:         $color_menu .= '<td align="center">'.$choices->{$item};
 2075:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 2076:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2077:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2078:     }
 2079:     $color_menu .= '</tr></table><br />';
 2080:     return $color_menu;
 2081: }
 2082: 
 2083: sub image_changes {
 2084:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 2085:     my $output;
 2086:     if ($img eq 'login') {
 2087:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 2088:     } elsif (!$is_custom) {
 2089:         if ($img ne 'domlogo') {
 2090:             $output = &mt('Default image:').'<br />';
 2091:         } else {
 2092:             $output = &mt('Default in use:').'<br />';
 2093:         }
 2094:     }
 2095:     if ($img ne 'login') {
 2096:         if ($img_import) {
 2097:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 2098:         }
 2099:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 2100:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 2101:         if ($is_custom) {
 2102:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 2103:                        '<input type="checkbox" name="'.
 2104:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 2105:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 2106:         } else {
 2107:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 2108:         }
 2109:     }
 2110:     return $output;
 2111: }
 2112: 
 2113: sub print_quotas {
 2114:     my ($dom,$settings,$rowtotal,$action) = @_;
 2115:     my $context;
 2116:     if ($action eq 'quotas') {
 2117:         $context = 'tools';
 2118:     } else {
 2119:         $context = $action;
 2120:     }
 2121:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 2122:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2123:     my $typecount = 0;
 2124:     my ($css_class,%titles);
 2125:     if ($context eq 'requestcourses') {
 2126:         @usertools = ('official','unofficial','community','textbook');
 2127:         @options =('norequest','approval','validate','autolimit');
 2128:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2129:         %titles = &courserequest_titles();
 2130:     } elsif ($context eq 'requestauthor') {
 2131:         @usertools = ('author');
 2132:         @options = ('norequest','approval','automatic');
 2133:         %titles = &authorrequest_titles();
 2134:     } else {
 2135:         @usertools = ('aboutme','blog','webdav','portfolio');
 2136:         %titles = &tool_titles();
 2137:     }
 2138:     if (ref($types) eq 'ARRAY') {
 2139:         foreach my $type (@{$types}) {
 2140:             my ($currdefquota,$currauthorquota);
 2141:             unless (($context eq 'requestcourses') ||
 2142:                     ($context eq 'requestauthor')) {
 2143:                 if (ref($settings) eq 'HASH') {
 2144:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2145:                         $currdefquota = $settings->{defaultquota}->{$type};
 2146:                     } else {
 2147:                         $currdefquota = $settings->{$type};
 2148:                     }
 2149:                     if (ref($settings->{authorquota}) eq 'HASH') {
 2150:                         $currauthorquota = $settings->{authorquota}->{$type};
 2151:                     }
 2152:                 }
 2153:             }
 2154:             if (defined($usertypes->{$type})) {
 2155:                 $typecount ++;
 2156:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2157:                 $datatable .= '<tr'.$css_class.'>'.
 2158:                               '<td>'.$usertypes->{$type}.'</td>'.
 2159:                               '<td class="LC_left_item">';
 2160:                 if ($context eq 'requestcourses') {
 2161:                     $datatable .= '<table><tr>';
 2162:                 }
 2163:                 my %cell;  
 2164:                 foreach my $item (@usertools) {
 2165:                     if ($context eq 'requestcourses') {
 2166:                         my ($curroption,$currlimit);
 2167:                         if (ref($settings) eq 'HASH') {
 2168:                             if (ref($settings->{$item}) eq 'HASH') {
 2169:                                 $curroption = $settings->{$item}->{$type};
 2170:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2171:                                     $currlimit = $1; 
 2172:                                 }
 2173:                             }
 2174:                         }
 2175:                         if (!$curroption) {
 2176:                             $curroption = 'norequest';
 2177:                         }
 2178:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2179:                         foreach my $option (@options) {
 2180:                             my $val = $option;
 2181:                             if ($option eq 'norequest') {
 2182:                                 $val = 0;  
 2183:                             }
 2184:                             if ($option eq 'validate') {
 2185:                                 my $canvalidate = 0;
 2186:                                 if (ref($validations{$item}) eq 'HASH') { 
 2187:                                     if ($validations{$item}{$type}) {
 2188:                                         $canvalidate = 1;
 2189:                                     }
 2190:                                 }
 2191:                                 next if (!$canvalidate);
 2192:                             }
 2193:                             my $checked = '';
 2194:                             if ($option eq $curroption) {
 2195:                                 $checked = ' checked="checked"';
 2196:                             } elsif ($option eq 'autolimit') {
 2197:                                 if ($curroption =~ /^autolimit/) {
 2198:                                     $checked = ' checked="checked"';
 2199:                                 }                       
 2200:                             } 
 2201:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2202:                                   '<input type="radio" name="crsreq_'.$item.
 2203:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2204:                                   $titles{$option}.'</label>';
 2205:                             if ($option eq 'autolimit') {
 2206:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2207:                                                 $item.'_limit_'.$type.'" size="1" '.
 2208:                                                 'value="'.$currlimit.'" />';
 2209:                             }
 2210:                             $cell{$item} .= '</span> ';
 2211:                             if ($option eq 'autolimit') {
 2212:                                 $cell{$item} .= $titles{'unlimited'};
 2213:                             }
 2214:                         }
 2215:                     } elsif ($context eq 'requestauthor') {
 2216:                         my $curroption;
 2217:                         if (ref($settings) eq 'HASH') {
 2218:                             $curroption = $settings->{$type};
 2219:                         }
 2220:                         if (!$curroption) {
 2221:                             $curroption = 'norequest';
 2222:                         }
 2223:                         foreach my $option (@options) {
 2224:                             my $val = $option;
 2225:                             if ($option eq 'norequest') {
 2226:                                 $val = 0;
 2227:                             }
 2228:                             my $checked = '';
 2229:                             if ($option eq $curroption) {
 2230:                                 $checked = ' checked="checked"';
 2231:                             }
 2232:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2233:                                   '<input type="radio" name="authorreq_'.$type.
 2234:                                   '" value="'.$val.'"'.$checked.' />'.
 2235:                                   $titles{$option}.'</label></span>&nbsp; ';
 2236:                         }
 2237:                     } else {
 2238:                         my $checked = 'checked="checked" ';
 2239:                         if (ref($settings) eq 'HASH') {
 2240:                             if (ref($settings->{$item}) eq 'HASH') {
 2241:                                 if ($settings->{$item}->{$type} == 0) {
 2242:                                     $checked = '';
 2243:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2244:                                     $checked =  'checked="checked" ';
 2245:                                 }
 2246:                             }
 2247:                         }
 2248:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2249:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2250:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2251:                                       '</label></span>&nbsp; ';
 2252:                     }
 2253:                 }
 2254:                 if ($context eq 'requestcourses') {
 2255:                     $datatable .= '</tr><tr>';
 2256:                     foreach my $item (@usertools) {
 2257:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2258:                     }
 2259:                     $datatable .= '</tr></table>';
 2260:                 }
 2261:                 $datatable .= '</td>';
 2262:                 unless (($context eq 'requestcourses') ||
 2263:                         ($context eq 'requestauthor')) {
 2264:                     $datatable .= 
 2265:                               '<td class="LC_right_item">'.
 2266:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2267:                               '<input type="text" name="quota_'.$type.
 2268:                               '" value="'.$currdefquota.
 2269:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2270:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2271:                               '<input type="text" name="authorquota_'.$type.
 2272:                               '" value="'.$currauthorquota.
 2273:                               '" size="5" /></span></td>';
 2274:                 }
 2275:                 $datatable .= '</tr>';
 2276:             }
 2277:         }
 2278:     }
 2279:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2280:         $defaultquota = '20';
 2281:         $authorquota = '500';
 2282:         if (ref($settings) eq 'HASH') {
 2283:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2284:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2285:             } elsif (defined($settings->{'default'})) {
 2286:                 $defaultquota = $settings->{'default'};
 2287:             }
 2288:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2289:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2290:             }
 2291:         }
 2292:     }
 2293:     $typecount ++;
 2294:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2295:     $datatable .= '<tr'.$css_class.'>'.
 2296:                   '<td>'.$othertitle.'</td>'.
 2297:                   '<td class="LC_left_item">';
 2298:     if ($context eq 'requestcourses') {
 2299:         $datatable .= '<table><tr>';
 2300:     }
 2301:     my %defcell;
 2302:     foreach my $item (@usertools) {
 2303:         if ($context eq 'requestcourses') {
 2304:             my ($curroption,$currlimit);
 2305:             if (ref($settings) eq 'HASH') {
 2306:                 if (ref($settings->{$item}) eq 'HASH') {
 2307:                     $curroption = $settings->{$item}->{'default'};
 2308:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2309:                         $currlimit = $1;
 2310:                     }
 2311:                 }
 2312:             }
 2313:             if (!$curroption) {
 2314:                 $curroption = 'norequest';
 2315:             }
 2316:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2317:             foreach my $option (@options) {
 2318:                 my $val = $option;
 2319:                 if ($option eq 'norequest') {
 2320:                     $val = 0;
 2321:                 }
 2322:                 if ($option eq 'validate') {
 2323:                     my $canvalidate = 0;
 2324:                     if (ref($validations{$item}) eq 'HASH') {
 2325:                         if ($validations{$item}{'default'}) {
 2326:                             $canvalidate = 1;
 2327:                         }
 2328:                     }
 2329:                     next if (!$canvalidate);
 2330:                 }
 2331:                 my $checked = '';
 2332:                 if ($option eq $curroption) {
 2333:                     $checked = ' checked="checked"';
 2334:                 } elsif ($option eq 'autolimit') {
 2335:                     if ($curroption =~ /^autolimit/) {
 2336:                         $checked = ' checked="checked"';
 2337:                     }
 2338:                 }
 2339:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2340:                                   '<input type="radio" name="crsreq_'.$item.
 2341:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2342:                                   $titles{$option}.'</label>';
 2343:                 if ($option eq 'autolimit') {
 2344:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2345:                                        $item.'_limit_default" size="1" '.
 2346:                                        'value="'.$currlimit.'" />';
 2347:                 }
 2348:                 $defcell{$item} .= '</span> ';
 2349:                 if ($option eq 'autolimit') {
 2350:                     $defcell{$item} .= $titles{'unlimited'};
 2351:                 }
 2352:             }
 2353:         } elsif ($context eq 'requestauthor') {
 2354:             my $curroption;
 2355:             if (ref($settings) eq 'HASH') {
 2356:                 $curroption = $settings->{'default'};
 2357:             }
 2358:             if (!$curroption) {
 2359:                 $curroption = 'norequest';
 2360:             }
 2361:             foreach my $option (@options) {
 2362:                 my $val = $option;
 2363:                 if ($option eq 'norequest') {
 2364:                     $val = 0;
 2365:                 }
 2366:                 my $checked = '';
 2367:                 if ($option eq $curroption) {
 2368:                     $checked = ' checked="checked"';
 2369:                 }
 2370:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2371:                               '<input type="radio" name="authorreq_default"'.
 2372:                               ' value="'.$val.'"'.$checked.' />'.
 2373:                               $titles{$option}.'</label></span>&nbsp; ';
 2374:             }
 2375:         } else {
 2376:             my $checked = 'checked="checked" ';
 2377:             if (ref($settings) eq 'HASH') {
 2378:                 if (ref($settings->{$item}) eq 'HASH') {
 2379:                     if ($settings->{$item}->{'default'} == 0) {
 2380:                         $checked = '';
 2381:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2382:                         $checked = 'checked="checked" ';
 2383:                     }
 2384:                 }
 2385:             }
 2386:             $datatable .= '<span class="LC_nobreak"><label>'.
 2387:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2388:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2389:                           '</label></span>&nbsp; ';
 2390:         }
 2391:     }
 2392:     if ($context eq 'requestcourses') {
 2393:         $datatable .= '</tr><tr>';
 2394:         foreach my $item (@usertools) {
 2395:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2396:         }
 2397:         $datatable .= '</tr></table>';
 2398:     }
 2399:     $datatable .= '</td>';
 2400:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2401:         $datatable .= '<td class="LC_right_item">'.
 2402:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2403:                       '<input type="text" name="defaultquota" value="'.
 2404:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2405:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2406:                       '<input type="text" name="authorquota" value="'.
 2407:                       $authorquota.'" size="5" /></span></td>';
 2408:     }
 2409:     $datatable .= '</tr>';
 2410:     $typecount ++;
 2411:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2412:     $datatable .= '<tr'.$css_class.'>'.
 2413:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2414:     if ($context eq 'requestcourses') {
 2415:         $datatable .= &mt('(overrides affiliation, if set)').
 2416:                       '</td>'.
 2417:                       '<td class="LC_left_item">'.
 2418:                       '<table><tr>';
 2419:     } else {
 2420:         $datatable .= &mt('(overrides affiliation, if checked)').
 2421:                       '</td>'.
 2422:                       '<td class="LC_left_item" colspan="2">'.
 2423:                       '<br />';
 2424:     }
 2425:     my %advcell;
 2426:     foreach my $item (@usertools) {
 2427:         if ($context eq 'requestcourses') {
 2428:             my ($curroption,$currlimit);
 2429:             if (ref($settings) eq 'HASH') {
 2430:                 if (ref($settings->{$item}) eq 'HASH') {
 2431:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2432:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2433:                         $currlimit = $1;
 2434:                     }
 2435:                 }
 2436:             }
 2437:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2438:             my $checked = '';
 2439:             if ($curroption eq '') {
 2440:                 $checked = ' checked="checked"';
 2441:             }
 2442:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2443:                                '<input type="radio" name="crsreq_'.$item.
 2444:                                '__LC_adv" value=""'.$checked.' />'.
 2445:                                &mt('No override set').'</label></span>&nbsp; ';
 2446:             foreach my $option (@options) {
 2447:                 my $val = $option;
 2448:                 if ($option eq 'norequest') {
 2449:                     $val = 0;
 2450:                 }
 2451:                 if ($option eq 'validate') {
 2452:                     my $canvalidate = 0;
 2453:                     if (ref($validations{$item}) eq 'HASH') {
 2454:                         if ($validations{$item}{'_LC_adv'}) {
 2455:                             $canvalidate = 1;
 2456:                         }
 2457:                     }
 2458:                     next if (!$canvalidate);
 2459:                 }
 2460:                 my $checked = '';
 2461:                 if ($val eq $curroption) {
 2462:                     $checked = ' checked="checked"';
 2463:                 } elsif ($option eq 'autolimit') {
 2464:                     if ($curroption =~ /^autolimit/) {
 2465:                         $checked = ' checked="checked"';
 2466:                     }
 2467:                 }
 2468:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2469:                                   '<input type="radio" name="crsreq_'.$item.
 2470:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2471:                                   $titles{$option}.'</label>';
 2472:                 if ($option eq 'autolimit') {
 2473:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2474:                                        $item.'_limit__LC_adv" size="1" '.
 2475:                                        'value="'.$currlimit.'" />';
 2476:                 }
 2477:                 $advcell{$item} .= '</span> ';
 2478:                 if ($option eq 'autolimit') {
 2479:                     $advcell{$item} .= $titles{'unlimited'};
 2480:                 }
 2481:             }
 2482:         } elsif ($context eq 'requestauthor') {
 2483:             my $curroption;
 2484:             if (ref($settings) eq 'HASH') {
 2485:                 $curroption = $settings->{'_LC_adv'};
 2486:             }
 2487:             my $checked = '';
 2488:             if ($curroption eq '') {
 2489:                 $checked = ' checked="checked"';
 2490:             }
 2491:             $datatable .= '<span class="LC_nobreak"><label>'.
 2492:                           '<input type="radio" name="authorreq__LC_adv"'.
 2493:                           ' value=""'.$checked.' />'.
 2494:                           &mt('No override set').'</label></span>&nbsp; ';
 2495:             foreach my $option (@options) {
 2496:                 my $val = $option;
 2497:                 if ($option eq 'norequest') {
 2498:                     $val = 0;
 2499:                 }
 2500:                 my $checked = '';
 2501:                 if ($val eq $curroption) {
 2502:                     $checked = ' checked="checked"';
 2503:                 }
 2504:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2505:                               '<input type="radio" name="authorreq__LC_adv"'.
 2506:                               ' value="'.$val.'"'.$checked.' />'.
 2507:                               $titles{$option}.'</label></span>&nbsp; ';
 2508:             }
 2509:         } else {
 2510:             my $checked = 'checked="checked" ';
 2511:             if (ref($settings) eq 'HASH') {
 2512:                 if (ref($settings->{$item}) eq 'HASH') {
 2513:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2514:                         $checked = '';
 2515:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2516:                         $checked = 'checked="checked" ';
 2517:                     }
 2518:                 }
 2519:             }
 2520:             $datatable .= '<span class="LC_nobreak"><label>'.
 2521:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2522:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2523:                           '</label></span>&nbsp; ';
 2524:         }
 2525:     }
 2526:     if ($context eq 'requestcourses') {
 2527:         $datatable .= '</tr><tr>';
 2528:         foreach my $item (@usertools) {
 2529:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2530:         }
 2531:         $datatable .= '</tr></table>';
 2532:     }
 2533:     $datatable .= '</td></tr>';
 2534:     $$rowtotal += $typecount;
 2535:     return $datatable;
 2536: }
 2537: 
 2538: sub print_requestmail {
 2539:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2540:     my ($now,$datatable,%currapp);
 2541:     $now = time;
 2542:     if (ref($settings) eq 'HASH') {
 2543:         if (ref($settings->{'notify'}) eq 'HASH') {
 2544:             if ($settings->{'notify'}{'approval'} ne '') {
 2545:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2546:             }
 2547:         }
 2548:     }
 2549:     my $numinrow = 2;
 2550:     my $css_class;
 2551:     if ($$rowtotal%2) {
 2552:         $css_class = 'LC_odd_row';
 2553:     }
 2554:     if ($customcss) {
 2555:         $css_class .= " $customcss";
 2556:     }
 2557:     $css_class =~ s/^\s+//;
 2558:     if ($css_class) {
 2559:         $css_class = ' class="'.$css_class.'"';
 2560:     }
 2561:     if ($rowstyle) {
 2562:         $css_class .= ' style="'.$rowstyle.'"';
 2563:     }
 2564:     my $text;
 2565:     if ($action eq 'requestcourses') {
 2566:         $text = &mt('Receive notification of course requests requiring approval');
 2567:     } elsif ($action eq 'requestauthor') {
 2568:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2569:     } else {
 2570:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2571:     }
 2572:     $datatable = '<tr'.$css_class.'>'.
 2573:                  ' <td>'.$text.'</td>'.
 2574:                  ' <td class="LC_left_item">';
 2575:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2576:                                                  $action.'notifyapproval',%currapp);
 2577:     if ($numdc > 0) {
 2578:         $datatable .= $table;
 2579:     } else {
 2580:         $datatable .= &mt('There are no active Domain Coordinators');
 2581:     }
 2582:     $datatable .='</td></tr>';
 2583:     return $datatable;
 2584: }
 2585: 
 2586: sub print_studentcode {
 2587:     my ($settings,$rowtotal) = @_;
 2588:     my $rownum = 0; 
 2589:     my ($output,%current);
 2590:     my @crstypes = ('official','unofficial','community','textbook');
 2591:     if (ref($settings) eq 'HASH') {
 2592:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2593:             foreach my $type (@crstypes) {
 2594:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2595:             }
 2596:         }
 2597:     }
 2598:     $output .= '<tr>'.
 2599:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2600:                '<td class="LC_left_item">';
 2601:     foreach my $type (@crstypes) {
 2602:         my $check = ' ';
 2603:         if ($current{$type}) {
 2604:             $check = ' checked="checked" ';
 2605:         }
 2606:         $output .= '<span class="LC_nobreak"><label>'.
 2607:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2608:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2609:     }
 2610:     $output .= '</td></tr>';
 2611:     $$rowtotal ++;
 2612:     return $output;
 2613: }
 2614: 
 2615: sub print_textbookcourses {
 2616:     my ($dom,$type,$settings,$rowtotal) = @_;
 2617:     my $rownum = 0;
 2618:     my $css_class;
 2619:     my $itemcount = 1;
 2620:     my $maxnum = 0;
 2621:     my $bookshash;
 2622:     if (ref($settings) eq 'HASH') {
 2623:         $bookshash = $settings->{$type};
 2624:     }
 2625:     my %ordered;
 2626:     if (ref($bookshash) eq 'HASH') {
 2627:         foreach my $item (keys(%{$bookshash})) {
 2628:             if (ref($bookshash->{$item}) eq 'HASH') {
 2629:                 my $num = $bookshash->{$item}{'order'};
 2630:                 $ordered{$num} = $item;
 2631:             }
 2632:         }
 2633:     }
 2634:     my $confname = $dom.'-domainconfig';
 2635:     my $switchserver = &check_switchserver($dom,$confname);
 2636:     my $maxnum = scalar(keys(%ordered));
 2637:     my $datatable;
 2638:     if (keys(%ordered)) {
 2639:         my @items = sort { $a <=> $b } keys(%ordered);
 2640:         for (my $i=0; $i<@items; $i++) {
 2641:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2642:             my $key = $ordered{$items[$i]};
 2643:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2644:             my $coursetitle = $coursehash{'description'};
 2645:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2646:             if (ref($bookshash->{$key}) eq 'HASH') {
 2647:                 $subject = $bookshash->{$key}->{'subject'};
 2648:                 $title = $bookshash->{$key}->{'title'};
 2649:                 if ($type eq 'textbooks') {
 2650:                     $publisher = $bookshash->{$key}->{'publisher'};
 2651:                     $author = $bookshash->{$key}->{'author'};
 2652:                     $image = $bookshash->{$key}->{'image'};
 2653:                     if ($image ne '') {
 2654:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2655:                         my $imagethumb = "$path/tn-".$imagefile;
 2656:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2657:                     }
 2658:                 }
 2659:             }
 2660:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2661:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2662:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2663:             for (my $k=0; $k<=$maxnum; $k++) {
 2664:                 my $vpos = $k+1;
 2665:                 my $selstr;
 2666:                 if ($k == $i) {
 2667:                     $selstr = ' selected="selected" ';
 2668:                 }
 2669:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2670:             }
 2671:             $datatable .= '</select>'.('&nbsp;'x2).
 2672:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2673:                 &mt('Delete?').'</label></span></td>'.
 2674:                 '<td colspan="2">'.
 2675:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2676:                 ('&nbsp;'x2).
 2677:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2678:             if ($type eq 'textbooks') {
 2679:                 $datatable .= ('&nbsp;'x2).
 2680:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2681:                               ('&nbsp;'x2).
 2682:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2683:                               ('&nbsp;'x2).
 2684:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2685:                 if ($image) {
 2686:                     $datatable .= $imgsrc.
 2687:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2688:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2689:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2690:                 }
 2691:                 if ($switchserver) {
 2692:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2693:                 } else {
 2694:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2695:                 }
 2696:             }
 2697:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2698:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2699:                           $coursetitle.'</span></td></tr>'."\n";
 2700:             $itemcount ++;
 2701:         }
 2702:     }
 2703:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2704:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2705:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2706:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2707:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2708:     for (my $k=0; $k<$maxnum+1; $k++) {
 2709:         my $vpos = $k+1;
 2710:         my $selstr;
 2711:         if ($k == $maxnum) {
 2712:             $selstr = ' selected="selected" ';
 2713:         }
 2714:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2715:     }
 2716:     $datatable .= '</select>&nbsp;'."\n".
 2717:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2718:                   '<td colspan="2">'.
 2719:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2720:                   ('&nbsp;'x2).
 2721:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2722:                   ('&nbsp;'x2);
 2723:     if ($type eq 'textbooks') {
 2724:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2725:                       ('&nbsp;'x2).
 2726:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2727:                       ('&nbsp;'x2).
 2728:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2729:         if ($switchserver) {
 2730:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2731:         } else {
 2732:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2733:         }
 2734:         $datatable .= '</span>'."\n";
 2735:     }
 2736:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2737:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2738:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2739:                   &Apache::loncommon::selectcourse_link
 2740:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2741:                   '</span></td>'."\n".
 2742:                   '</tr>'."\n";
 2743:     $itemcount ++;
 2744:     return $datatable;
 2745: }
 2746: 
 2747: sub textbookcourses_javascript {
 2748:     my ($settings) = @_;
 2749:     return unless(ref($settings) eq 'HASH');
 2750:     my (%ordered,%total,%jstext);
 2751:     foreach my $type ('textbooks','templates') {
 2752:         $total{$type} = 0;
 2753:         if (ref($settings->{$type}) eq 'HASH') {
 2754:             foreach my $item (keys(%{$settings->{$type}})) {
 2755:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2756:                     my $num = $settings->{$type}->{$item}{'order'};
 2757:                     $ordered{$type}{$num} = $item;
 2758:                 }
 2759:             }
 2760:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2761:         }
 2762:         my @jsarray = ();
 2763:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2764:             push(@jsarray,$ordered{$type}{$item});
 2765:         }
 2766:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2767:     }
 2768:     return <<"ENDSCRIPT";
 2769: <script type="text/javascript">
 2770: // <![CDATA[
 2771: function reorderBooks(form,item,caller) {
 2772:     var changedVal;
 2773: $jstext{'textbooks'};
 2774: $jstext{'templates'};
 2775:     var newpos;
 2776:     var maxh;
 2777:     if (caller == 'textbooks') {  
 2778:         newpos = 'textbooks_addbook_pos';
 2779:         maxh = 1 + $total{'textbooks'};
 2780:     } else {
 2781:         newpos = 'templates_addbook_pos';
 2782:         maxh = 1 + $total{'templates'};
 2783:     }
 2784:     var current = new Array;
 2785:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2786:     if (item == newpos) {
 2787:         changedVal = newitemVal;
 2788:     } else {
 2789:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2790:         current[newitemVal] = newpos;
 2791:     }
 2792:     if (caller == 'textbooks') {
 2793:         for (var i=0; i<textbooks.length; i++) {
 2794:             var elementName = 'textbooks_'+textbooks[i];
 2795:             if (elementName != item) {
 2796:                 if (form.elements[elementName]) {
 2797:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2798:                     current[currVal] = elementName;
 2799:                 }
 2800:             }
 2801:         }
 2802:     }
 2803:     if (caller == 'templates') {
 2804:         for (var i=0; i<templates.length; i++) {
 2805:             var elementName = 'templates_'+templates[i];
 2806:             if (elementName != item) {
 2807:                 if (form.elements[elementName]) {
 2808:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2809:                     current[currVal] = elementName;
 2810:                 }
 2811:             }
 2812:         }
 2813:     }
 2814:     var oldVal;
 2815:     for (var j=0; j<maxh; j++) {
 2816:         if (current[j] == undefined) {
 2817:             oldVal = j;
 2818:         }
 2819:     }
 2820:     if (oldVal < changedVal) {
 2821:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2822:            var elementName = current[k];
 2823:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2824:         }
 2825:     } else {
 2826:         for (var k=changedVal; k<oldVal; k++) {
 2827:             var elementName = current[k];
 2828:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2829:         }
 2830:     }
 2831:     return;
 2832: }
 2833: 
 2834: // ]]>
 2835: </script>
 2836: 
 2837: ENDSCRIPT
 2838: }
 2839: 
 2840: sub wafproxy_javascript {
 2841:     my ($dom) = @_;
 2842:     return <<"ENDSCRIPT";
 2843: <script type="text/javascript">
 2844: // <![CDATA[
 2845: function updateWAF() {
 2846:     if (document.getElementById('wafproxy_remoteip')) {
 2847:         var wafremote = 0;
 2848:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 2849:             wafremote = 1;
 2850:         }
 2851:         var fields = new Array('header','trust');
 2852:         for (var i=0; i<fields.length; i++) {
 2853:             if (document.getElementById('wafproxy_'+fields[i])) {
 2854:                 if (wafremote == 1) {
 2855:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 2856:                 }
 2857:                 else {
 2858:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 2859:                 }
 2860:             }
 2861:         }
 2862:         if (document.getElementById('wafproxyranges_$dom')) {
 2863:             if (wafremote == 1) {
 2864:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 2865:             } else {
 2866:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 2867:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 2868:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 2869:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 2870:                         }
 2871:                     }
 2872:                 }
 2873:             }
 2874:         }
 2875:     }
 2876:     return;
 2877: }
 2878: 
 2879: function checkWAF() {
 2880:     if (document.getElementById('wafproxy_remoteip')) {
 2881:         var wafvpn = 0;
 2882:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 2883:             if (document.display.wafproxy_vpnaccess[i].checked) {
 2884:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 2885:                     wafvpn = 1;
 2886:                 }
 2887:                 break;
 2888:             }
 2889:         }
 2890:         var vpn = new Array('vpnint','vpnext');
 2891:         for (var i=0; i<vpn.length; i++) {
 2892:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 2893:                 if (wafvpn == 1) {
 2894:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 2895:                 }
 2896:                 else {
 2897:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 2898:                 }
 2899:             }
 2900:         }
 2901:         if (document.getElementById('wafproxyranges_$dom')) {
 2902:             if (wafvpn == 1) {
 2903:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 2904:             }
 2905:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 2906:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 2907:             }
 2908:         }
 2909:     }
 2910:     return;
 2911: }
 2912: 
 2913: function toggleWAF() {
 2914:     if (document.getElementById('wafproxy_table')) {
 2915:         var wafproxy = 0;
 2916:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 2917:              if (document.display.wafproxy_${dom}[i].checked) {
 2918:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 2919:                      wafproxy = 1;
 2920:                      break;
 2921:                 }
 2922:             }
 2923:         }
 2924:         if (wafproxy == 1) {
 2925:             document.getElementById('wafproxy_table').style.display='inline';
 2926:         }
 2927:         else {
 2928:            document.getElementById('wafproxy_table').style.display='none';
 2929:         }
 2930:         if (document.getElementById('wafproxyrow_${dom}')) {
 2931:             if (wafproxy == 1) {
 2932:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 2933:             }
 2934:             else {
 2935:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 2936:             }
 2937:         }
 2938:         if (document.getElementById('nowafproxyrow_$dom')) {
 2939:             if (wafproxy == 1) {
 2940:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 2941:             }
 2942:             else {
 2943:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 2944:             }
 2945:         }
 2946:     }
 2947:     return;
 2948: }
 2949: // ]]>
 2950: </script>
 2951: 
 2952: ENDSCRIPT
 2953: }
 2954: 
 2955: sub autoupdate_javascript {
 2956:     return <<"ENDSCRIPT";
 2957: <script type="text/javascript">
 2958: // <![CDATA[
 2959: function toggleLastActiveDays(form) {
 2960:     var radioname = 'lastactive';
 2961:     var divid = 'lastactive_div';
 2962:     var num = form.elements[radioname].length;
 2963:     if (num) {
 2964:         var setvis = '';
 2965:         for (var i=0; i<num; i++) {
 2966:             if (form.elements[radioname][i].checked) {
 2967:                 if (form.elements[radioname][i].value == '1') {
 2968:                     if (document.getElementById(divid)) {
 2969:                         document.getElementById(divid).style.display = 'inline-block';
 2970:                     }
 2971:                     setvis = 1;
 2972:                 }
 2973:                 break;
 2974:             }
 2975:         }
 2976:         if (!setvis) {
 2977:             if (document.getElementById(divid)) {
 2978:                 document.getElementById(divid).style.display = 'none';
 2979:             }
 2980:         }
 2981:     }
 2982:     return;
 2983: }
 2984: // ]]>
 2985: </script>
 2986: 
 2987: ENDSCRIPT
 2988: }
 2989: 
 2990: sub autoenroll_javascript {
 2991:     return <<"ENDSCRIPT";
 2992: <script type="text/javascript">
 2993: // <![CDATA[
 2994: function toggleFailsafe(form) {
 2995:     var radioname = 'autoenroll_failsafe';
 2996:     var divid = 'autoenroll_failsafe_div';
 2997:     var num = form.elements[radioname].length;
 2998:     if (num) {
 2999:         var setvis = '';
 3000:         for (var i=0; i<num; i++) {
 3001:             if (form.elements[radioname][i].checked) {
 3002:                 if ((form.elements[radioname][i].value == 'zero') || (form.elements[radioname][i].value == 'any')) {
 3003:                     if (document.getElementById(divid)) {
 3004:                         document.getElementById(divid).style.display = 'inline-block';
 3005:                     }
 3006:                     setvis = 1;
 3007:                 }
 3008:                 break;
 3009:             }
 3010:         }
 3011:         if (!setvis) {
 3012:             if (document.getElementById(divid)) {
 3013:                 document.getElementById(divid).style.display = 'none';
 3014:             }
 3015:         }
 3016:     }
 3017:     return;
 3018: }
 3019: // ]]>
 3020: </script>
 3021: 
 3022: ENDSCRIPT
 3023: }
 3024: 
 3025: sub saml_javascript {
 3026:     return <<"ENDSCRIPT";
 3027: <script type="text/javascript">
 3028: // <![CDATA[
 3029: function toggleSamlOptions(form,hostid) {
 3030:     var radioname = 'saml_'+hostid;
 3031:     var tablecellon = 'samloptionson_'+hostid;
 3032:     var tablecelloff = 'samloptionsoff_'+hostid;
 3033:     var num = form.elements[radioname].length;
 3034:     if (num) {
 3035:         var setvis = '';
 3036:         for (var i=0; i<num; i++) {
 3037:             if (form.elements[radioname][i].checked) {
 3038:                 if (form.elements[radioname][i].value == '1') {
 3039:                     if (document.getElementById(tablecellon)) {
 3040:                         document.getElementById(tablecellon).style.display='';
 3041:                     }
 3042:                     if (document.getElementById(tablecelloff)) {
 3043:                         document.getElementById(tablecelloff).style.display='none';
 3044:                     }
 3045:                     setvis = 1;
 3046:                 }
 3047:                 break;
 3048:             }
 3049:         }
 3050:         if (!setvis) {
 3051:             if (document.getElementById(tablecellon)) {
 3052:                 document.getElementById(tablecellon).style.display='none';
 3053:             }
 3054:             if (document.getElementById(tablecelloff)) {
 3055:                 document.getElementById(tablecelloff).style.display='';
 3056:             }
 3057:         }
 3058:     }
 3059:     return;
 3060: }
 3061: // ]]>
 3062: </script>
 3063: 
 3064: ENDSCRIPT
 3065: }
 3066: 
 3067: sub ipaccess_javascript {
 3068:     my ($settings) = @_;
 3069:     my (%ordered,$total,%jstext);
 3070:     $total = 0;
 3071:     if (ref($settings) eq 'HASH') {
 3072:         foreach my $item (keys(%{$settings})) {
 3073:             if (ref($settings->{$item}) eq 'HASH') {
 3074:                 my $num = $settings->{$item}{'order'};
 3075:                 $ordered{$num} = $item;
 3076:             }
 3077:         }
 3078:         $total = scalar(keys(%{$settings}));
 3079:     }
 3080:     my @jsarray = ();
 3081:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3082:         push(@jsarray,$ordered{$item});
 3083:     }
 3084:     my $jstext = '    var ipaccess = Array('."'".join("','",@jsarray)."'".');'."\n";
 3085:     return <<"ENDSCRIPT";
 3086: <script type="text/javascript">
 3087: // <![CDATA[
 3088: function reorderIPaccess(form,item) {
 3089:     var changedVal;
 3090: $jstext
 3091:     var newpos = 'ipaccess_pos_add';
 3092:     var maxh = 1 + $total;
 3093:     var current = new Array;
 3094:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3095:     if (item == newpos) {
 3096:         changedVal = newitemVal;
 3097:     } else {
 3098:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3099:         current[newitemVal] = newpos;
 3100:     }
 3101:     for (var i=0; i<ipaccess.length; i++) {
 3102:         var elementName = 'ipaccess_pos_'+ipaccess[i];
 3103:         if (elementName != item) {
 3104:             if (form.elements[elementName]) {
 3105:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3106:                 current[currVal] = elementName;
 3107:             }
 3108:         }
 3109:     }
 3110:     var oldVal;
 3111:     for (var j=0; j<maxh; j++) {
 3112:         if (current[j] == undefined) {
 3113:             oldVal = j;
 3114:         }
 3115:     }
 3116:     if (oldVal < changedVal) {
 3117:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3118:            var elementName = current[k];
 3119:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3120:         }
 3121:     } else {
 3122:         for (var k=changedVal; k<oldVal; k++) {
 3123:             var elementName = current[k];
 3124:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3125:         }
 3126:     }
 3127:     return;
 3128: }
 3129: // ]]>
 3130: </script>
 3131: 
 3132: ENDSCRIPT
 3133: }
 3134: 
 3135: sub print_autoenroll {
 3136:     my ($dom,$settings,$rowtotal) = @_;
 3137:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3138:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,
 3139:         $failsafe,$autofailsafe,$failsafesty,%failsafechecked);
 3140:     $failsafesty = 'none';
 3141:     %failsafechecked = (
 3142:         off => ' checked="checked"',
 3143:     );
 3144:     if (ref($settings) eq 'HASH') {
 3145:         if (exists($settings->{'run'})) {
 3146:             if ($settings->{'run'} eq '0') {
 3147:                 $runoff = ' checked="checked" ';
 3148:                 $runon = ' ';
 3149:             } else {
 3150:                 $runon = ' checked="checked" ';
 3151:                 $runoff = ' ';
 3152:             }
 3153:         } else {
 3154:             if ($autorun) {
 3155:                 $runon = ' checked="checked" ';
 3156:                 $runoff = ' ';
 3157:             } else {
 3158:                 $runoff = ' checked="checked" ';
 3159:                 $runon = ' ';
 3160:             }
 3161:         }
 3162:         if (exists($settings->{'co-owners'})) {
 3163:             if ($settings->{'co-owners'} eq '0') {
 3164:                 $coownersoff = ' checked="checked" ';
 3165:                 $coownerson = ' ';
 3166:             } else {
 3167:                 $coownerson = ' checked="checked" ';
 3168:                 $coownersoff = ' ';
 3169:             }
 3170:         } else {
 3171:             $coownersoff = ' checked="checked" ';
 3172:             $coownerson = ' ';
 3173:         }
 3174:         if (exists($settings->{'sender_domain'})) {
 3175:             $defdom = $settings->{'sender_domain'};
 3176:         }
 3177:         if (exists($settings->{'failsafe'})) {
 3178:             $failsafe = $settings->{'failsafe'};
 3179:             if ($failsafe eq 'zero') {
 3180:                 $failsafechecked{'zero'} = ' checked="checked"';
 3181:                 $failsafechecked{'off'} = '';
 3182:                 $failsafesty = 'inline-block';
 3183:             } elsif ($failsafe eq 'any') {
 3184:                 $failsafechecked{'any'} = ' checked="checked"';
 3185:                 $failsafechecked{'off'} = '';
 3186:             }
 3187:             $autofailsafe = $settings->{'autofailsafe'};
 3188:         } elsif (exists($settings->{'autofailsafe'})) {
 3189:             $autofailsafe = $settings->{'autofailsafe'};
 3190:             if ($autofailsafe ne '') {
 3191:                 $failsafechecked{'zero'} = ' checked="checked"';
 3192:                 $failsafe = 'zero';
 3193:                 $failsafechecked{'off'} = '';
 3194:             }
 3195:         }
 3196:     } else {
 3197:         if ($autorun) {
 3198:             $runon = ' checked="checked" ';
 3199:             $runoff = ' ';
 3200:         } else {
 3201:             $runoff = ' checked="checked" ';
 3202:             $runon = ' ';
 3203:         }
 3204:     }
 3205:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3206:     my $notif_sender;
 3207:     if (ref($settings) eq 'HASH') {
 3208:         $notif_sender = $settings->{'sender_uname'};
 3209:     }
 3210:     my $datatable='<tr class="LC_odd_row">'.
 3211:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3212:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3213:                   '<input type="radio" name="autoenroll_run"'.
 3214:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3215:                   '<label><input type="radio" name="autoenroll_run"'.
 3216:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3217:                   '</tr><tr>'.
 3218:                   '<td>'.&mt('Notification messages - sender').
 3219:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 3220:                   &mt('username').':&nbsp;'.
 3221:                   '<input type="text" name="sender_uname" value="'.
 3222:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 3223:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 3224:                   '<tr class="LC_odd_row">'.
 3225:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 3226:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3227:                   '<input type="radio" name="autoassign_coowners"'.
 3228:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3229:                   '<label><input type="radio" name="autoassign_coowners"'.
 3230:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3231:                   '</tr><tr>'.
 3232:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 3233:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 3234:                   '<span class="LC_nobreak"><label><input type="radio" name="autoenroll_failsafe" value="off" onclick="toggleFailsafe(this.form)"'.$failsafechecked{'off'}.' />'.&mt('Not in use').'</label></span>&nbsp;&nbsp;&nbsp; '.
 3235:                   '<span class="LC_nobreak"><label><input type="radio" name="autoenroll_failsafe" value="zero" onclick="toggleFailsafe(this.form)"'.$failsafechecked{'zero'}.' />'.&mt('Retrieved section enrollment is zero').'</label></span><br />'.
 3236:                   '<span class="LC_nobreak"><label><input type="radio" name="autoenroll_failsafe" value="any" onclick="toggleFailsafe(this.form)"'.$failsafechecked{'any'}.' />'.&mt('Retrieved section enrollment is zero or greater').'</label></span>'.
 3237:                   '<div class="LC_floatleft" style="display:'.$failsafesty.';" id="autoenroll_failsafe_div">'.
 3238:                   '<span class="LC_nobreak">'.
 3239:                   &mt('Threshold for number of students in section to drop: [_1]',
 3240:                       '<input type="text" name="autoenroll_autofailsafe" value="'.$autofailsafe.'" size="4" />').
 3241:                   '</span></div></td></tr>';
 3242:     $$rowtotal += 4;
 3243:     return $datatable;
 3244: }
 3245: 
 3246: sub print_autoupdate {
 3247:     my ($position,$dom,$settings,$rowtotal) = @_;
 3248:     my ($enable,$datatable);
 3249:     if ($position eq 'top') {
 3250:         my %choices = &Apache::lonlocal::texthash (
 3251:                           run        => 'Auto-update active?',
 3252:                           classlists => 'Update information in classlists?',
 3253:                           unexpired  => 'Skip updates for users without active or future roles?',
 3254:                           lastactive => 'Skip updates for inactive users?',
 3255:         );
 3256:         my $itemcount = 0;
 3257:         my $updateon = ' ';
 3258:         my $updateoff = ' checked="checked" ';
 3259:         if (ref($settings) eq 'HASH') {
 3260:             if ($settings->{'run'} eq '1') {
 3261:                 $updateon = $updateoff;
 3262:                 $updateoff = ' ';
 3263:             }
 3264:         }
 3265:         $enable = '<tr class="LC_odd_row">'. 
 3266:                   '<td>'.&mt($choices{'run'}).'</td>'.
 3267:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3268:                   '<input type="radio" name="autoupdate_run"'.
 3269:                   $updateoff.'value="0" />'.&mt('No').'</label>&nbsp;'.
 3270:                   '<label><input type="radio" name="autoupdate_run"'.
 3271:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 3272:                   '</tr>';
 3273:         my @toggles = ('classlists','unexpired');
 3274:         my %defaultchecked = ('classlists' => 'off',
 3275:                               'unexpired'  => 'off'
 3276:                               );
 3277:         $$rowtotal ++;
 3278:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3279:                                                      \%choices,$itemcount,'','','left','no');
 3280:         $datatable = $enable.$datatable;
 3281:         $$rowtotal += $itemcount;
 3282:         my $lastactiveon = ' ';
 3283:         my $lastactiveoff = ' checked="checked" ';
 3284:         my $lastactivestyle = 'none';
 3285:         my $lastactivedays;
 3286:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 3287:         if (ref($settings) eq 'HASH') {
 3288:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 3289:                 $lastactiveon = $lastactiveoff;
 3290:                 $lastactiveoff = ' ';
 3291:                 $lastactivestyle = 'inline-block';
 3292:                 $lastactivedays = $settings->{'lastactive'};
 3293:             }
 3294:         }
 3295:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3296:         $datatable .= '<tr'.$css_class.'>'.
 3297:                       '<td>'.$choices{'lastactive'}.'</td>'.
 3298:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3299:                       '<input type="radio" name="lastactive"'.
 3300:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3301:                       '&nbsp;<label>'.
 3302:                       '<input type="radio" name="lastactive"'.
 3303:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 3304:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 3305:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 3306:                           '<input type="text" size="5" name="lastactivedays" value="'.
 3307:                           $lastactivedays.'" />').
 3308:                       '</span></td>'.
 3309:                       '</tr>';
 3310:         $$rowtotal ++;
 3311:     } elsif ($position eq 'middle') {
 3312:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3313:         my $numinrow = 3;
 3314:         my $locknamesettings;
 3315:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3316:                                      $dom,$numinrow,$othertitle,
 3317:                                     'lockablenames',$rowtotal);
 3318:         $$rowtotal ++;
 3319:     } else {
 3320:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3321:         my @fields = ('lastname','firstname','middlename','generation',
 3322:                       'permanentemail','id');
 3323:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3324:         my $numrows = 0;
 3325:         if (ref($types) eq 'ARRAY') {
 3326:             if (@{$types} > 0) {
 3327:                 $datatable = 
 3328:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3329:                                          \@fields,$types,\$numrows);
 3330:                     $$rowtotal += @{$types}; 
 3331:             }
 3332:         }
 3333:         $datatable .= 
 3334:             &usertype_update_row($settings,{'default' => $othertitle},
 3335:                                  \%fieldtitles,\@fields,['default'],
 3336:                                  \$numrows);
 3337:         $$rowtotal ++;     
 3338:     }
 3339:     return $datatable;
 3340: }
 3341: 
 3342: sub print_autocreate {
 3343:     my ($dom,$settings,$rowtotal) = @_;
 3344:     my (%createon,%createoff,%currhash);
 3345:     my @types = ('xml','req');
 3346:     if (ref($settings) eq 'HASH') {
 3347:         foreach my $item (@types) {
 3348:             $createoff{$item} = ' checked="checked" ';
 3349:             $createon{$item} = ' ';
 3350:             if (exists($settings->{$item})) {
 3351:                 if ($settings->{$item}) {
 3352:                     $createon{$item} = ' checked="checked" ';
 3353:                     $createoff{$item} = ' ';
 3354:                 }
 3355:             }
 3356:         }
 3357:         if ($settings->{'xmldc'} ne '') {
 3358:             $currhash{$settings->{'xmldc'}} = 1;
 3359:         }
 3360:     } else {
 3361:         foreach my $item (@types) {
 3362:             $createoff{$item} = ' checked="checked" ';
 3363:             $createon{$item} = ' ';
 3364:         }
 3365:     }
 3366:     $$rowtotal += 2;
 3367:     my $numinrow = 2;
 3368:     my $datatable='<tr class="LC_odd_row">'.
 3369:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 3370:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3371:                   '<input type="radio" name="autocreate_xml"'.
 3372:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3373:                   '<label><input type="radio" name="autocreate_xml"'.
 3374:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 3375:                   '</td></tr><tr>'.
 3376:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 3377:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3378:                   '<input type="radio" name="autocreate_req"'.
 3379:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3380:                   '<label><input type="radio" name="autocreate_req"'.
 3381:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 3382:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3383:                                                    'autocreate_xmldc',%currhash);
 3384:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 3385:     if ($numdc > 1) {
 3386:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 3387:                       '</td><td class="LC_left_item">';
 3388:     } else {
 3389:         $datatable .= &mt('Course creation processed as:').
 3390:                       '</td><td class="LC_right_item">';
 3391:     }
 3392:     $datatable .= $dctable.'</td></tr>';
 3393:     $$rowtotal += $rows;
 3394:     return $datatable;
 3395: }
 3396: 
 3397: sub print_directorysrch {
 3398:     my ($position,$dom,$settings,$rowtotal) = @_;
 3399:     my $datatable;
 3400:     if ($position eq 'top') {
 3401:         my $instsrchon = ' ';
 3402:         my $instsrchoff = ' checked="checked" ';
 3403:         my ($exacton,$containson,$beginson);
 3404:         my $instlocalon = ' ';
 3405:         my $instlocaloff = ' checked="checked" ';
 3406:         if (ref($settings) eq 'HASH') {
 3407:             if ($settings->{'available'} eq '1') {
 3408:                 $instsrchon = $instsrchoff;
 3409:                 $instsrchoff = ' ';
 3410:             }
 3411:             if ($settings->{'localonly'} eq '1') {
 3412:                 $instlocalon = $instlocaloff;
 3413:                 $instlocaloff = ' ';
 3414:             }
 3415:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 3416:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 3417:                     if ($type eq 'exact') {
 3418:                         $exacton = ' checked="checked" ';
 3419:                     } elsif ($type eq 'contains') {
 3420:                         $containson = ' checked="checked" ';
 3421:                     } elsif ($type eq 'begins') {
 3422:                         $beginson = ' checked="checked" ';
 3423:                     }
 3424:                 }
 3425:             } else {
 3426:                 if ($settings->{'searchtypes'} eq 'exact') {
 3427:                     $exacton = ' checked="checked" ';
 3428:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 3429:                     $containson = ' checked="checked" ';
 3430:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 3431:                     $exacton = ' checked="checked" ';
 3432:                     $containson = ' checked="checked" ';
 3433:                 }
 3434:             }
 3435:         }
 3436:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 3437:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3438: 
 3439:         my $numinrow = 4;
 3440:         my $cansrchrow = 0;
 3441:         $datatable='<tr class="LC_odd_row">'.
 3442:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 3443:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3444:                    '<input type="radio" name="dirsrch_available"'.
 3445:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3446:                    '<label><input type="radio" name="dirsrch_available"'.
 3447:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3448:                    '</tr><tr>'.
 3449:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 3450:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3451:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 3452:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3453:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 3454:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3455:                    '</tr>';
 3456:         $$rowtotal += 2;
 3457:         if (ref($usertypes) eq 'HASH') {
 3458:             if (keys(%{$usertypes}) > 0) {
 3459:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3460:                                              $numinrow,$othertitle,'cansearch',
 3461:                                              $rowtotal);
 3462:                 $cansrchrow = 1;
 3463:             }
 3464:         }
 3465:         if ($cansrchrow) {
 3466:             $$rowtotal ++;
 3467:             $datatable .= '<tr>';
 3468:         } else {
 3469:             $datatable .= '<tr class="LC_odd_row">';
 3470:         }
 3471:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 3472:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 3473:         foreach my $title (@{$titleorder}) {
 3474:             if (defined($searchtitles->{$title})) {
 3475:                 my $check = ' ';
 3476:                 if (ref($settings) eq 'HASH') {
 3477:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 3478:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 3479:                             $check = ' checked="checked" ';
 3480:                         }
 3481:                     }
 3482:                 }
 3483:                 $datatable .= '<td class="LC_left_item">'.
 3484:                               '<span class="LC_nobreak"><label>'.
 3485:                               '<input type="checkbox" name="searchby" '.
 3486:                               'value="'.$title.'"'.$check.'/>'.
 3487:                               $searchtitles->{$title}.'</label></span></td>';
 3488:             }
 3489:         }
 3490:         $datatable .= '</tr></table></td></tr>';
 3491:         $$rowtotal ++;
 3492:         if ($cansrchrow) {
 3493:             $datatable .= '<tr class="LC_odd_row">';
 3494:         } else {
 3495:             $datatable .= '<tr>';
 3496:         }
 3497:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 3498:                       '<td class="LC_left_item" colspan="2">'.
 3499:                       '<span class="LC_nobreak"><label>'.
 3500:                       '<input type="checkbox" name="searchtypes" '.
 3501:                       $exacton.' value="exact" />'.&mt('Exact match').
 3502:                       '</label>&nbsp;'.
 3503:                       '<label><input type="checkbox" name="searchtypes" '.
 3504:                       $beginson.' value="begins" />'.&mt('Begins with').
 3505:                       '</label>&nbsp;'.
 3506:                       '<label><input type="checkbox" name="searchtypes" '.
 3507:                       $containson.' value="contains" />'.&mt('Contains').
 3508:                       '</label></span></td></tr>';
 3509:         $$rowtotal ++;
 3510:     } else {
 3511:         my $domsrchon = ' checked="checked" ';
 3512:         my $domsrchoff = ' ';
 3513:         my $domlocalon = ' ';
 3514:         my $domlocaloff = ' checked="checked" ';
 3515:         if (ref($settings) eq 'HASH') {
 3516:             if ($settings->{'lclocalonly'} eq '1') {
 3517:                 $domlocalon = $domlocaloff;
 3518:                 $domlocaloff = ' ';
 3519:             }
 3520:             if ($settings->{'lcavailable'} eq '0') {
 3521:                 $domsrchoff = $domsrchon;
 3522:                 $domsrchon = ' ';
 3523:             }
 3524:         }
 3525:         $datatable='<tr class="LC_odd_row">'.
 3526:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 3527:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3528:                       '<input type="radio" name="dirsrch_domavailable"'.
 3529:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3530:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 3531:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3532:                       '</tr><tr>'.
 3533:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 3534:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3535:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 3536:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3537:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 3538:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3539:                       '</tr>';
 3540:         $$rowtotal += 2;
 3541:     }
 3542:     return $datatable;
 3543: }
 3544: 
 3545: sub print_contacts {
 3546:     my ($position,$dom,$settings,$rowtotal) = @_;
 3547:     my $datatable;
 3548:     my @contacts = ('adminemail','supportemail');
 3549:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3550:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 3551:     if ($position eq 'top') {
 3552:         if (ref($settings) eq 'HASH') {
 3553:             foreach my $item (@contacts) {
 3554:                 if (exists($settings->{$item})) {
 3555:                     $to{$item} = $settings->{$item};
 3556:                 }
 3557:             }
 3558:         }
 3559:     } elsif ($position eq 'middle') {
 3560:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3561:                      'updatesmail','idconflictsmail','hostipmail');
 3562:         foreach my $type (@mailings) {
 3563:             $otheremails{$type} = '';
 3564:         }
 3565:     } elsif ($position eq 'lower') {
 3566:         if (ref($settings) eq 'HASH') {
 3567:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 3568:                 %lonstatus = %{$settings->{'lonstatus'}};
 3569:             }
 3570:         }
 3571:     } else {
 3572:         @mailings = ('helpdeskmail','otherdomsmail');
 3573:         foreach my $type (@mailings) {
 3574:             $otheremails{$type} = '';
 3575:         }
 3576:         $bccemails{'helpdeskmail'} = '';
 3577:         $bccemails{'otherdomsmail'} = '';
 3578:         $includestr{'helpdeskmail'} = '';
 3579:         $includestr{'otherdomsmail'} = '';
 3580:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3581:     }
 3582:     if (ref($settings) eq 'HASH') {
 3583:         unless (($position eq 'top') || ($position eq 'lower')) {
 3584:             foreach my $type (@mailings) {
 3585:                 if (exists($settings->{$type})) {
 3586:                     if (ref($settings->{$type}) eq 'HASH') {
 3587:                         foreach my $item (@contacts) {
 3588:                             if ($settings->{$type}{$item}) {
 3589:                                 $checked{$type}{$item} = ' checked="checked" ';
 3590:                             }
 3591:                         }
 3592:                         $otheremails{$type} = $settings->{$type}{'others'};
 3593:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3594:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3595:                             if ($settings->{$type}{'include'} ne '') {
 3596:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3597:                                 $includestr{$type} = &unescape($includestr{$type});
 3598:                             }
 3599:                         }
 3600:                     }
 3601:                 } elsif ($type eq 'lonstatusmail') {
 3602:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3603:                 }
 3604:             }
 3605:         }
 3606:         if ($position eq 'bottom') {
 3607:             foreach my $type (@mailings) {
 3608:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3609:                 if ($settings->{$type}{'include'} ne '') {
 3610:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3611:                     $includestr{$type} = &unescape($includestr{$type});
 3612:                 }
 3613:             }
 3614:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3615:                 if (ref($fields) eq 'ARRAY') {
 3616:                     foreach my $field (@{$fields}) {
 3617:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3618:                     }
 3619:                 }
 3620:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3621:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3622:                 } else {
 3623:                     $maxsize = '1.0';
 3624:                 }
 3625:             } else {
 3626:                 if (ref($fields) eq 'ARRAY') {
 3627:                     foreach my $field (@{$fields}) {
 3628:                         $currfield{$field} = 'yes';
 3629:                     }
 3630:                 }
 3631:                 $maxsize = '1.0';
 3632:             }
 3633:         }
 3634:     } else {
 3635:         if ($position eq 'top') {
 3636:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3637:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3638:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3639:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3640:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3641:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3642:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3643:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3644:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 3645:         } elsif ($position eq 'bottom') {
 3646:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3647:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3648:             if (ref($fields) eq 'ARRAY') {
 3649:                 foreach my $field (@{$fields}) {
 3650:                     $currfield{$field} = 'yes';
 3651:                 }
 3652:             }
 3653:             $maxsize = '1.0';
 3654:         }
 3655:     }
 3656:     my ($titles,$short_titles) = &contact_titles();
 3657:     my $rownum = 0;
 3658:     my $css_class;
 3659:     if ($position eq 'top') {
 3660:         foreach my $item (@contacts) {
 3661:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3662:             $datatable .= '<tr'.$css_class.'>'. 
 3663:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3664:                           '</span></td><td class="LC_right_item">'.
 3665:                           '<input type="text" name="'.$item.'" value="'.
 3666:                           $to{$item}.'" /></td></tr>';
 3667:             $rownum ++;
 3668:         }
 3669:     } elsif ($position eq 'bottom') {
 3670:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3671:         $datatable .= '<tr'.$css_class.'>'.
 3672:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3673:                       &mt('(e-mail, subject, and description always shown)').
 3674:                       '</td><td class="LC_left_item">';
 3675:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3676:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3677:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3678:             foreach my $field (@{$fields}) {
 3679:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3680:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3681:                     $datatable .= ' '.&mt('(logged-in users)');
 3682:                 }
 3683:                 $datatable .='</td><td>';
 3684:                 my $clickaction;
 3685:                 if ($field eq 'screenshot') {
 3686:                     $clickaction = ' onclick="screenshotSize(this);"';
 3687:                 }
 3688:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3689:                     foreach my $option (@{$possoptions->{$field}}) {
 3690:                         my $checked;
 3691:                         if ($currfield{$field} eq $option) {
 3692:                             $checked = ' checked="checked"';
 3693:                         }
 3694:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3695:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3696:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3697:                                       '</label></span>'.('&nbsp;'x2);
 3698:                     }
 3699:                 }
 3700:                 if ($field eq 'screenshot') {
 3701:                     my $display;
 3702:                     if ($currfield{$field} eq 'no') {
 3703:                         $display = ' style="display:none"';
 3704:                     }
 3705:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3706:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3707:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3708:                 }
 3709:                 $datatable .= '</td></tr>';
 3710:             }
 3711:             $datatable .= '</table>';
 3712:         }
 3713:         $datatable .= '</td></tr>'."\n";
 3714:         $rownum ++;
 3715:     }
 3716:     unless (($position eq 'top') || ($position eq 'lower')) {
 3717:         foreach my $type (@mailings) {
 3718:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3719:             $datatable .= '<tr'.$css_class.'>'.
 3720:                           '<td><span class="LC_nobreak">'.
 3721:                           $titles->{$type}.': </span></td>'.
 3722:                           '<td class="LC_left_item">';
 3723:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3724:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3725:             }
 3726:             $datatable .= '<span class="LC_nobreak">';
 3727:             foreach my $item (@contacts) {
 3728:                 $datatable .= '<label>'.
 3729:                               '<input type="checkbox" name="'.$type.'"'.
 3730:                               $checked{$type}{$item}.
 3731:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3732:                               '</label>&nbsp;';
 3733:             }
 3734:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3735:                           '<input type="text" name="'.$type.'_others" '.
 3736:                           'value="'.$otheremails{$type}.'"  />';
 3737:             my %locchecked;
 3738:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3739:                 foreach my $loc ('s','b') {
 3740:                     if ($includeloc{$type} eq $loc) {
 3741:                         $locchecked{$loc} = ' checked="checked"';
 3742:                         last;
 3743:                     }
 3744:                 }
 3745:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3746:                               '<input type="text" name="'.$type.'_bcc" '.
 3747:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3748:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3749:                               &mt('Text automatically added to e-mail:').' '.
 3750:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3751:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3752:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3753:                               ('&nbsp;'x2).
 3754:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3755:                               '</span></fieldset>';
 3756:             }
 3757:             $datatable .= '</td></tr>'."\n";
 3758:             $rownum ++;
 3759:         }
 3760:     }
 3761:     if ($position eq 'middle') {
 3762:         my %choices;
 3763:         my $corelink = &core_link_msu();
 3764:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 3765:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3766:                                         $corelink);
 3767:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 3768:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 3769:         my %defaultchecked = ('reporterrors'  => 'on',
 3770:                               'reportupdates' => 'on',
 3771:                               'reportstatus'  => 'on');
 3772:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3773:                                                    \%choices,$rownum);
 3774:         $datatable .= $reports;
 3775:     } elsif ($position eq 'lower') {
 3776:         my (%current,%excluded,%weights);
 3777:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 3778:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 3779:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 3780:         } else {
 3781:             $current{'errorthreshold'} = $defaults->{'threshold'};
 3782:         }
 3783:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 3784:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 3785:         } else {
 3786:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 3787:         }
 3788:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 3789:             foreach my $type ('E','W','N','U') {
 3790:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 3791:                     $weights{$type} = $lonstatus{'weights'}{$type};
 3792:                 } else {
 3793:                     $weights{$type} = $defaults->{$type};
 3794:                 }
 3795:             }
 3796:         } else {
 3797:             foreach my $type ('E','W','N','U') {
 3798:                 $weights{$type} = $defaults->{$type};
 3799:             }
 3800:         }
 3801:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 3802:             if (@{$lonstatus{'excluded'}} > 0) {
 3803:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 3804:             }
 3805:         }
 3806:         foreach my $item ('errorthreshold','errorsysmail') {
 3807:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3808:             $datatable .= '<tr'.$css_class.'>'.
 3809:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 3810:                           $titles->{$item}.
 3811:                           '</span></td><td class="LC_left_item">'.
 3812:                           '<input type="text" name="'.$item.'" value="'.
 3813:                           $current{$item}.'" size="5" /></td></tr>';
 3814:             $rownum ++;
 3815:         }
 3816:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3817:         $datatable .= '<tr'.$css_class.'>'.
 3818:                       '<td class="LC_left_item">'.
 3819:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 3820:                       '</span></td><td class="LC_left_item"><table><tr>';
 3821:         foreach my $type ('E','W','N','U') {
 3822:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 3823:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 3824:                           $weights{$type}.'" size="5" /></td>';
 3825:         }
 3826:         $datatable .= '</tr></table></tr>';
 3827:         $rownum ++;
 3828:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3829:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 3830:                       $titles->{'errorexcluded'}.'</td>'.
 3831:                       '<td class="LC_left_item"><table>';
 3832:         my $numinrow = 4;
 3833:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 3834:         for (my $i=0; $i<@ids; $i++) {
 3835:             my $rem = $i%($numinrow);
 3836:             if ($rem == 0) {
 3837:                 if ($i > 0) {
 3838:                     $datatable .= '</tr>';
 3839:                 }
 3840:                 $datatable .= '<tr>';
 3841:             }
 3842:             my $check;
 3843:             if ($excluded{$ids[$i]}) {
 3844:                 $check = ' checked="checked" ';
 3845:             }
 3846:             $datatable .= '<td class="LC_left_item">'.
 3847:                           '<span class="LC_nobreak"><label>'.
 3848:                           '<input type="checkbox" name="errorexcluded" '.
 3849:                           'value="'.$ids[$i].'"'.$check.' />'.
 3850:                           $ids[$i].'</label></span></td>';
 3851:         }
 3852:         my $colsleft = $numinrow - @ids%($numinrow);
 3853:         if ($colsleft > 1 ) {
 3854:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3855:                           '&nbsp;</td>';
 3856:         } elsif ($colsleft == 1) {
 3857:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3858:         }
 3859:         $datatable .= '</tr></table></td></tr>';
 3860:         $rownum ++;
 3861:     } elsif ($position eq 'bottom') {
 3862:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3863:         my (@posstypes,%usertypeshash);
 3864:         if (ref($types) eq 'ARRAY') {
 3865:             @posstypes = @{$types};
 3866:         }
 3867:         if (@posstypes) {
 3868:             if (ref($usertypes) eq 'HASH') {
 3869:                 %usertypeshash = %{$usertypes};
 3870:             }
 3871:             my @overridden;
 3872:             my $numinrow = 4;
 3873:             if (ref($settings) eq 'HASH') {
 3874:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3875:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3876:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3877:                             push(@overridden,$key);
 3878:                             foreach my $item (@contacts) {
 3879:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3880:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3881:                                 }
 3882:                             }
 3883:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3884:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3885:                             $includeloc{'override_'.$key} = '';
 3886:                             $includestr{'override_'.$key} = '';
 3887:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3888:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 3889:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3890:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3891:                             }
 3892:                         }
 3893:                     }
 3894:                 }
 3895:             }
 3896:             my $customclass = 'LC_helpdesk_override';
 3897:             my $optionsprefix = 'LC_options_helpdesk_';
 3898: 
 3899:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3900: 
 3901:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3902:                                          $numinrow,$othertitle,'overrides',
 3903:                                          \$rownum,$onclicktypes,$customclass);
 3904:             $rownum ++;
 3905:             $usertypeshash{'default'} = $othertitle;
 3906:             foreach my $status (@posstypes) {
 3907:                 my $css_class;
 3908:                 if ($rownum%2) {
 3909:                     $css_class = 'LC_odd_row ';
 3910:                 }
 3911:                 $css_class .= $customclass;
 3912:                 my $rowid = $optionsprefix.$status;
 3913:                 my $hidden = 1;
 3914:                 my $currstyle = 'display:none';
 3915:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3916:                     $currstyle = 'display:table-row';
 3917:                     $hidden = 0;
 3918:                 }
 3919:                 my $key = 'override_'.$status;
 3920:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3921:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3922:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3923:                                                   \@contacts,$short_titles);
 3924:                 unless ($hidden) {
 3925:                     $rownum ++;
 3926:                 }
 3927:             }
 3928:         }
 3929:     }
 3930:     $$rowtotal += $rownum;
 3931:     return $datatable;
 3932: }
 3933: 
 3934: sub core_link_msu {
 3935:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3936:                                           &mt('LON-CAPA core group - MSU'),600,500);
 3937: }
 3938: 
 3939: sub overridden_helpdesk {
 3940:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3941:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3942:     my $class = 'LC_left_item';
 3943:     if ($css_class) {
 3944:         $css_class = ' class="'.$css_class.'"';
 3945:     }
 3946:     if ($rowid) {
 3947:         $rowid = ' id="'.$rowid.'"';
 3948:     }
 3949:     if ($rowstyle) {
 3950:         $rowstyle = ' style="'.$rowstyle.'"';
 3951:     }
 3952:     my ($output,$description);
 3953:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3954:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3955:               "<td>$description</td>\n".
 3956:               '<td class="'.$class.'" colspan="2">'.
 3957:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3958:               '<span class="LC_nobreak">';
 3959:     if (ref($contacts) eq 'ARRAY') {
 3960:         foreach my $item (@{$contacts}) {
 3961:             my $check;
 3962:             if (ref($checked) eq 'HASH') {
 3963:                $check = $checked->{$item};
 3964:             }
 3965:             my $title;
 3966:             if (ref($short_titles) eq 'HASH') {
 3967:                 $title = $short_titles->{$item};
 3968:             }
 3969:             $output .= '<label>'.
 3970:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3971:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3972:         }
 3973:     }
 3974:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3975:                '<input type="text" name="override_'.$type.'_others" '.
 3976:                'value="'.$otheremails.'"  />';
 3977:     my %locchecked;
 3978:     foreach my $loc ('s','b') {
 3979:         if ($includeloc eq $loc) {
 3980:             $locchecked{$loc} = ' checked="checked"';
 3981:             last;
 3982:         }
 3983:     }
 3984:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3985:                '<input type="text" name="override_'.$type.'_bcc" '.
 3986:                'value="'.$bccemails.'"  /></fieldset>'.
 3987:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3988:                &mt('Text automatically added to e-mail:').' '.
 3989:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 3990:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3991:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3992:                ('&nbsp;'x2).
 3993:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3994:                '</span></fieldset>'.
 3995:                '</td></tr>'."\n";
 3996:     return $output;
 3997: }
 3998: 
 3999: sub contacts_javascript {
 4000:     return <<"ENDSCRIPT";
 4001: 
 4002: <script type="text/javascript">
 4003: // <![CDATA[
 4004: 
 4005: function screenshotSize(field) {
 4006:     if (document.getElementById('help_screenshotsize')) {
 4007:         if (field.value == 'no') {
 4008:             document.getElementById('help_screenshotsize').style.display="none";
 4009:         } else {
 4010:             document.getElementById('help_screenshotsize').style.display="";
 4011:         }
 4012:     }
 4013:     return;
 4014: }
 4015: 
 4016: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4017:     if (form.elements[checkbox].length != undefined) {
 4018:         var count = 0;
 4019:         if (docount) {
 4020:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4021:                 if (form.elements[checkbox][i].checked) {
 4022:                     count ++;
 4023:                 }
 4024:             }
 4025:         }
 4026:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4027:             var type = form.elements[checkbox][i].value;
 4028:             if (document.getElementById(prefix+type)) {
 4029:                 if (form.elements[checkbox][i].checked) {
 4030:                     document.getElementById(prefix+type).style.display = 'table-row';
 4031:                     if (count % 2 == 1) {
 4032:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4033:                     } else {
 4034:                         document.getElementById(prefix+type).className = target;
 4035:                     }
 4036:                     count ++;
 4037:                 } else {
 4038:                     document.getElementById(prefix+type).style.display = 'none';
 4039:                 }
 4040:             }
 4041:         }
 4042:     }
 4043:     return;
 4044: }
 4045: 
 4046: // ]]>
 4047: </script>
 4048: 
 4049: ENDSCRIPT
 4050: }
 4051: 
 4052: sub print_helpsettings {
 4053:     my ($position,$dom,$settings,$rowtotal) = @_;
 4054:     my $confname = $dom.'-domainconfig';
 4055:     my $formname = 'display';
 4056:     my ($datatable,$itemcount);
 4057:     if ($position eq 'top') {
 4058:         $itemcount = 1;
 4059:         my (%choices,%defaultchecked,@toggles);
 4060:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4061:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4062:                                      &mt('LON-CAPA bug tracker'),600,500));
 4063:         %defaultchecked = ('submitbugs' => 'on');
 4064:         @toggles = ('submitbugs');
 4065:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4066:                                                      \%choices,$itemcount);
 4067:         $$rowtotal ++;
 4068:     } else {
 4069:         my $css_class;
 4070:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4071:         my (%customroles,%ordered,%current);
 4072:         if (ref($settings) eq 'HASH') {
 4073:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4074:                 %current = %{$settings->{'adhoc'}};
 4075:             }
 4076:         }
 4077:         my $count = 0;
 4078:         foreach my $key (sort(keys(%existing))) {
 4079:             if ($key=~/^rolesdef\_(\w+)$/) {
 4080:                 my $rolename = $1;
 4081:                 my (%privs,$order);
 4082:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4083:                 $customroles{$rolename} = \%privs;
 4084:                 if (ref($current{$rolename}) eq 'HASH') {
 4085:                     $order = $current{$rolename}{'order'};
 4086:                 }
 4087:                 if ($order eq '') {
 4088:                     $order = $count;
 4089:                 }
 4090:                 $ordered{$order} = $rolename;
 4091:                 $count++;
 4092:             }
 4093:         }
 4094:         my $maxnum = scalar(keys(%ordered));
 4095:         my @roles_by_num = ();
 4096:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4097:             push(@roles_by_num,$item);
 4098:         }
 4099:         my $context = 'domprefs';
 4100:         my $crstype = 'Course';
 4101:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4102:         my @accesstypes = ('all','dh','da','none');
 4103:         my ($numstatustypes,@jsarray);
 4104:         if (ref($types) eq 'ARRAY') {
 4105:             if (@{$types} > 0) {
 4106:                 $numstatustypes = scalar(@{$types});
 4107:                 push(@accesstypes,'status');
 4108:                 @jsarray = ('bystatus');
 4109:             }
 4110:         }
 4111:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4112:         if (keys(%domhelpdesk)) {
 4113:             push(@accesstypes,('inc','exc'));
 4114:             push(@jsarray,('notinc','notexc'));
 4115:         }
 4116:         my $hiddenstr = join("','",@jsarray);
 4117:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 4118:         my $context = 'domprefs';
 4119:         my $crstype = 'Course';
 4120:         my $prefix = 'helproles_';
 4121:         my $add_class = 'LC_hidden';
 4122:         foreach my $num (@roles_by_num) {
 4123:             my $role = $ordered{$num};
 4124:             my ($desc,$access,@statuses);
 4125:             if (ref($current{$role}) eq 'HASH') {
 4126:                 $desc = $current{$role}{'desc'};
 4127:                 $access = $current{$role}{'access'};
 4128:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4129:                     @statuses = @{$current{$role}{'insttypes'}};
 4130:                 }
 4131:             }
 4132:             if ($desc eq '') {
 4133:                 $desc = $role;
 4134:             }
 4135:             my $identifier = 'custhelp'.$num;
 4136:             my %full=();
 4137:             my %levels= (
 4138:                          course => {},
 4139:                          domain => {},
 4140:                          system => {},
 4141:                         );
 4142:             my %levelscurrent=(
 4143:                                course => {},
 4144:                                domain => {},
 4145:                                system => {},
 4146:                               );
 4147:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4148:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4149:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4150:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4151:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 4152:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4153:             for (my $k=0; $k<=$maxnum; $k++) {
 4154:                 my $vpos = $k+1;
 4155:                 my $selstr;
 4156:                 if ($k == $num) {
 4157:                     $selstr = ' selected="selected" ';
 4158:                 }
 4159:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4160:             }
 4161:             $datatable .= '</select>'.('&nbsp;'x2).
 4162:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 4163:                           '</td>'.
 4164:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4165:                           &mt('Name shown to users:').
 4166:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 4167:                           '</fieldset>'.
 4168:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4169:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4170:                           '<fieldset>'.
 4171:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4172:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4173:                                                                    \%levelscurrent,$identifier,
 4174:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4175:                           '</fieldset></td>';
 4176:             $itemcount ++;
 4177:         }
 4178:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4179:         my $newcust = 'custhelp'.$count;
 4180:         my (%privs,%levelscurrent);
 4181:         my %full=();
 4182:         my %levels= (
 4183:                      course => {},
 4184:                      domain => {},
 4185:                      system => {},
 4186:                     );
 4187:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4188:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4189:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4190:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 4191:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4192:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4193:         for (my $k=0; $k<$maxnum+1; $k++) {
 4194:             my $vpos = $k+1;
 4195:             my $selstr;
 4196:             if ($k == $maxnum) {
 4197:                 $selstr = ' selected="selected" ';
 4198:             }
 4199:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4200:         }
 4201:         $datatable .= '</select>&nbsp;'."\n".
 4202:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4203:                       '</label></span></td>'.
 4204:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4205:                       '<span class="LC_nobreak">'.
 4206:                       &mt('Internal name:').
 4207:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4208:                       '</span>'.('&nbsp;'x4).
 4209:                       '<span class="LC_nobreak">'.
 4210:                       &mt('Name shown to users:').
 4211:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4212:                       '</span></fieldset>'.
 4213:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4214:                                              $usertypes,$types,\%domhelpdesk).
 4215:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4216:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4217:                                                                 \@templateroles,$newcust).
 4218:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4219:                                                                \%levelscurrent,$newcust).
 4220:                       '</fieldset>'.
 4221:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4222:                       '</td></tr>';
 4223:         $count ++;
 4224:         $$rowtotal += $count;
 4225:     }
 4226:     return $datatable;
 4227: }
 4228: 
 4229: sub adhocbutton {
 4230:     my ($prefix,$num,$field,$visibility) = @_;
 4231:     my %lt = &Apache::lonlocal::texthash(
 4232:                                           show => 'Show details',
 4233:                                           hide => 'Hide details',
 4234:                                         );
 4235:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4236:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4237:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4238:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4239: }
 4240: 
 4241: sub helpsettings_javascript {
 4242:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4243:     return unless(ref($roles_by_num) eq 'ARRAY');
 4244:     my %html_js_lt = &Apache::lonlocal::texthash(
 4245:                                           show => 'Show details',
 4246:                                           hide => 'Hide details',
 4247:                                         );
 4248:     &html_escape(\%html_js_lt);
 4249:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4250:     return <<"ENDSCRIPT";
 4251: <script type="text/javascript">
 4252: // <![CDATA[
 4253: 
 4254: function reorderHelpRoles(form,item) {
 4255:     var changedVal;
 4256: $jstext
 4257:     var newpos = 'helproles_${total}_pos';
 4258:     var maxh = 1 + $total;
 4259:     var current = new Array();
 4260:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4261:     if (item == newpos) {
 4262:         changedVal = newitemVal;
 4263:     } else {
 4264:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4265:         current[newitemVal] = newpos;
 4266:     }
 4267:     for (var i=0; i<helproles.length; i++) {
 4268:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4269:         if (elementName != item) {
 4270:             if (form.elements[elementName]) {
 4271:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4272:                 current[currVal] = elementName;
 4273:             }
 4274:         }
 4275:     }
 4276:     var oldVal;
 4277:     for (var j=0; j<maxh; j++) {
 4278:         if (current[j] == undefined) {
 4279:             oldVal = j;
 4280:         }
 4281:     }
 4282:     if (oldVal < changedVal) {
 4283:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4284:            var elementName = current[k];
 4285:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4286:         }
 4287:     } else {
 4288:         for (var k=changedVal; k<oldVal; k++) {
 4289:             var elementName = current[k];
 4290:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4291:         }
 4292:     }
 4293:     return;
 4294: }
 4295: 
 4296: function helpdeskAccess(num) {
 4297:     var curraccess = null;
 4298:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4299:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4300:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4301:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4302:             }
 4303:         }
 4304:     }
 4305:     var shown = Array();
 4306:     var hidden = Array();
 4307:     if (curraccess == 'none') {
 4308:         hidden = Array('$hiddenstr');
 4309:     } else {
 4310:         if (curraccess == 'status') {
 4311:             shown = Array('bystatus');
 4312:             hidden = Array('notinc','notexc');
 4313:         } else {
 4314:             if (curraccess == 'exc') {
 4315:                 shown = Array('notexc');
 4316:                 hidden = Array('notinc','bystatus');
 4317:             }
 4318:             if (curraccess == 'inc') {
 4319:                 shown = Array('notinc');
 4320:                 hidden = Array('notexc','bystatus');
 4321:             }
 4322:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4323:                 hidden = Array('notinc','notexc','bystatus');
 4324:             }
 4325:         }
 4326:     }
 4327:     if (hidden.length > 0) {
 4328:         for (var i=0; i<hidden.length; i++) {
 4329:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4330:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4331:             }
 4332:         }
 4333:     }
 4334:     if (shown.length > 0) {
 4335:         for (var i=0; i<shown.length; i++) {
 4336:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4337:                 if (shown[i] == 'privs') {
 4338:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4339:                 } else {
 4340:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4341:                 }
 4342:             }
 4343:         }
 4344:     }
 4345:     return;
 4346: }
 4347: 
 4348: function toggleHelpdeskItem(num,field) {
 4349:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4350:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4351:             document.getElementById('helproles_'+num+'_'+field).className =
 4352:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4353:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4354:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4355:             }
 4356:         } else {
 4357:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4358:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4359:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4360:             }
 4361:         }
 4362:     }
 4363:     return;
 4364: }
 4365: 
 4366: // ]]>
 4367: </script>
 4368: 
 4369: ENDSCRIPT
 4370: }
 4371: 
 4372: sub helpdeskroles_access {
 4373:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 4374:         $usertypes,$types,$domhelpdesk) = @_;
 4375:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 4376:     my %lt = &Apache::lonlocal::texthash(
 4377:                     'rou'    => 'Role usage',
 4378:                     'whi'    => 'Which helpdesk personnel may use this role?',
 4379:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 4380:                     'dh'     => 'All with domain helpdesk role',
 4381:                     'da'     => 'All with domain helpdesk assistant role',
 4382:                     'none'   => 'None',
 4383:                     'status' => 'Determined based on institutional status',
 4384:                     'inc'    => 'Include all, but exclude specific personnel',
 4385:                     'exc'    => 'Exclude all, but include specific personnel',
 4386:                   );
 4387:     my %usecheck = (
 4388:                      all => ' checked="checked"',
 4389:                    );
 4390:     my %displaydiv = (
 4391:                       status => 'none',
 4392:                       inc    => 'none',
 4393:                       exc    => 'none',
 4394:                       priv   => 'block',
 4395:                      );
 4396:     my $output;
 4397:     if (ref($current) eq 'HASH') {
 4398:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 4399:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 4400:                 $usecheck{$current->{access}} = $usecheck{'all'};
 4401:                 delete($usecheck{'all'});
 4402:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 4403:                     my $access = $1;
 4404:                     $displaydiv{$access} = 'inline';
 4405:                 } elsif ($current->{access} eq 'none') {
 4406:                     $displaydiv{'priv'} = 'none';
 4407:                 }
 4408:             }
 4409:         }
 4410:     }
 4411:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 4412:               '<p>'.$lt{'whi'}.'</p>';
 4413:     foreach my $access (@{$accesstypes}) {
 4414:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 4415:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 4416:                    $lt{$access}.'</label>';
 4417:         if ($access eq 'status') {
 4418:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 4419:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 4420:                                                                  $othertitle,$usertypes,$types).
 4421:                        '</div>';
 4422:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 4423:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 4424:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4425:                        '</div>';
 4426:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 4427:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 4428:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4429:                        '</div>';
 4430:         }
 4431:         $output .= '</p>';
 4432:     }
 4433:     $output .= '</fieldset>';
 4434:     return $output;
 4435: }
 4436: 
 4437: sub radiobutton_prefs {
 4438:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 4439:         $additional,$align,$firstval) = @_;
 4440:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 4441:                    (ref($choices) eq 'HASH'));
 4442: 
 4443:     my (%checkedon,%checkedoff,$datatable,$css_class);
 4444: 
 4445:     foreach my $item (@{$toggles}) {
 4446:         if ($defaultchecked->{$item} eq 'on') {
 4447:             $checkedon{$item} = ' checked="checked" ';
 4448:             $checkedoff{$item} = ' ';
 4449:         } elsif ($defaultchecked->{$item} eq 'off') {
 4450:             $checkedoff{$item} = ' checked="checked" ';
 4451:             $checkedon{$item} = ' ';
 4452:         }
 4453:     }
 4454:     if (ref($settings) eq 'HASH') {
 4455:         foreach my $item (@{$toggles}) {
 4456:             if ($settings->{$item} eq '1') {
 4457:                 $checkedon{$item} =  ' checked="checked" ';
 4458:                 $checkedoff{$item} = ' ';
 4459:             } elsif ($settings->{$item} eq '0') {
 4460:                 $checkedoff{$item} =  ' checked="checked" ';
 4461:                 $checkedon{$item} = ' ';
 4462:             }
 4463:         }
 4464:     }
 4465:     if ($onclick) {
 4466:         $onclick = ' onclick="'.$onclick.'"';
 4467:     }
 4468:     foreach my $item (@{$toggles}) {
 4469:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4470:         $datatable .=
 4471:             '<tr'.$css_class.'><td valign="top">'.
 4472:             '<span class="LC_nobreak">'.$choices->{$item}.
 4473:             '</span></td>';
 4474:         if ($align eq 'left') {
 4475:             $datatable .= '<td class="LC_left_item">';
 4476:         } else {
 4477:             $datatable .= '<td class="LC_right_item">';
 4478:         }
 4479:         $datatable .= '<span class="LC_nobreak">';
 4480:         if ($firstval eq 'no') {
 4481:             $datatable .=
 4482:                 '<label><input type="radio" name="'.
 4483:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 4484:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4485:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 4486:         } else {
 4487:             $datatable .=
 4488:             '<label><input type="radio" name="'.
 4489:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 4490:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4491:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 4492:         }
 4493:         $datatable .= '</span>'.$additional.'</td></tr>';
 4494:         $itemcount ++;
 4495:     }
 4496:     return ($datatable,$itemcount);
 4497: }
 4498: 
 4499: sub print_coursedefaults {
 4500:     my ($position,$dom,$settings,$rowtotal) = @_;
 4501:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4502:     my $itemcount = 1;
 4503:     my %choices =  &Apache::lonlocal::texthash (
 4504:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4505:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4506:         coursecredits        => 'Credits can be specified for courses',
 4507:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4508:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4509:         inline_chem          => 'Use inline previewer for chemical reaction response in place of pop-up',
 4510:         texengine            => 'Default method to display mathematics',
 4511:         postsubmit           => 'Disable submit button/keypress following student submission',
 4512:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4513:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4514:     );
 4515:     my %staticdefaults = (
 4516:                            anonsurvey_threshold => 10,
 4517:                            uploadquota          => 500,
 4518:                            postsubmit           => 60,
 4519:                            mysqltables          => 172800,
 4520:                          );
 4521:     if ($position eq 'top') {
 4522:         %defaultchecked = (
 4523:                             'uselcmath'       => 'on',
 4524:                             'usejsme'         => 'on',
 4525:                             'inline_chem'     => 'on',
 4526:                             'canclone'        => 'none',
 4527:                           );
 4528:         @toggles = ('uselcmath','usejsme','inline_chem');
 4529:         my $deftex = $Apache::lonnet::deftex;
 4530:         if (ref($settings) eq 'HASH') {
 4531:             if ($settings->{'texengine'}) {
 4532:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 4533:                     $deftex = $settings->{'texengine'};
 4534:                 }
 4535:             }
 4536:         }
 4537:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4538:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 4539:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 4540:                        '</span></td><td class="LC_right_item">'.
 4541:                        '<select name="texengine">'."\n";
 4542:         my %texoptions = (
 4543:                             MathJax  => 'MathJax',
 4544:                             mimetex  => &mt('Convert to Images'),
 4545:                             tth      => &mt('TeX to HTML'),
 4546:                          );
 4547:         foreach my $renderer ('MathJax','mimetex','tth') {
 4548:             my $selected = '';
 4549:             if ($renderer eq $deftex) {
 4550:                 $selected = ' selected="selected"';
 4551:             }
 4552:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 4553:         }
 4554:         $mathdisp .= '</select></td></tr>'."\n";
 4555:         $itemcount ++;
 4556:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4557:                                                      \%choices,$itemcount);
 4558:         $datatable = $mathdisp.$datatable;
 4559:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4560:         $datatable .=
 4561:             '<tr'.$css_class.'><td valign="top">'.
 4562:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4563:             '</span></td><td class="LC_left_item">';
 4564:         my $currcanclone = 'none';
 4565:         my $onclick;
 4566:         my @cloneoptions = ('none','domain');
 4567:         my %clonetitles = &Apache::lonlocal::texthash (
 4568:                              none     => 'No additional course requesters',
 4569:                              domain   => "Any course requester in course's domain",
 4570:                              instcode => 'Course requests for official courses ...',
 4571:                           );
 4572:         my (%codedefaults,@code_order,@posscodes);
 4573:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4574:                                                     \@code_order) eq 'ok') {
 4575:             if (@code_order > 0) {
 4576:                 push(@cloneoptions,'instcode');
 4577:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4578:             }
 4579:         }
 4580:         if (ref($settings) eq 'HASH') {
 4581:             if ($settings->{'canclone'}) {
 4582:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4583:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4584:                         if (@code_order > 0) {
 4585:                             $currcanclone = 'instcode';
 4586:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4587:                         }
 4588:                     }
 4589:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4590:                     $currcanclone = $settings->{'canclone'};
 4591:                 }
 4592:             }
 4593:         }
 4594:         foreach my $option (@cloneoptions) {
 4595:             my ($checked,$additional);
 4596:             if ($currcanclone eq $option) {
 4597:                 $checked = ' checked="checked"';
 4598:             }
 4599:             if ($option eq 'instcode') {
 4600:                 if (@code_order) {
 4601:                     my $show = 'none';
 4602:                     if ($checked) {
 4603:                         $show = 'block';
 4604:                     }
 4605:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4606:                                   &mt('Institutional codes for new and cloned course have identical:').
 4607:                                   '<br />';
 4608:                     foreach my $item (@code_order) {
 4609:                         my $codechk;
 4610:                         if ($checked) {
 4611:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4612:                                 $codechk = ' checked="checked"';
 4613:                             }
 4614:                         }
 4615:                         $additional .= '<label>'.
 4616:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4617:                                        $item.'</label>';
 4618:                     }
 4619:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4620:                 }
 4621:             }
 4622:             $datatable .=
 4623:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4624:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4625:                 '</label>&nbsp;'.$additional.'</span><br />';
 4626:         }
 4627:         $datatable .= '</td>'.
 4628:                       '</tr>';
 4629:         $itemcount ++;
 4630:     } else {
 4631:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4632:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4633:         my $currusecredits = 0;
 4634:         my $postsubmitclient = 1;
 4635:         my @types = ('official','unofficial','community','textbook');
 4636:         if (ref($settings) eq 'HASH') {
 4637:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4638:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4639:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4640:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4641:                 }
 4642:             }
 4643:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4644:                 foreach my $type (@types) {
 4645:                     next if ($type eq 'community');
 4646:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4647:                     if ($defcredits{$type} ne '') {
 4648:                         $currusecredits = 1;
 4649:                     }
 4650:                 }
 4651:             }
 4652:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4653:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4654:                     $postsubmitclient = 0;
 4655:                     foreach my $type (@types) {
 4656:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4657:                     }
 4658:                 } else {
 4659:                     foreach my $type (@types) {
 4660:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4661:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4662:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4663:                             } else {
 4664:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4665:                             }
 4666:                         } else {
 4667:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4668:                         }
 4669:                     }
 4670:                 }
 4671:             } else {
 4672:                 foreach my $type (@types) {
 4673:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4674:                 }
 4675:             }
 4676:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4677:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4678:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4679:                 }
 4680:             } else {
 4681:                 foreach my $type (@types) {
 4682:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4683:                 }
 4684:             }
 4685:         } else {
 4686:             foreach my $type (@types) {
 4687:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4688:             }
 4689:         }
 4690:         if (!$currdefresponder) {
 4691:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4692:         } elsif ($currdefresponder < 1) {
 4693:             $currdefresponder = 1;
 4694:         }
 4695:         foreach my $type (@types) {
 4696:             if ($curruploadquota{$type} eq '') {
 4697:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4698:             }
 4699:         }
 4700:         $datatable .=
 4701:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4702:                 $choices{'anonsurvey_threshold'}.
 4703:                 '</span></td>'.
 4704:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4705:                 '<input type="text" name="anonsurvey_threshold"'.
 4706:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4707:                 '</td></tr>'."\n";
 4708:         $itemcount ++;
 4709:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4710:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4711:                       $choices{'uploadquota'}.
 4712:                       '</span></td>'.
 4713:                       '<td align="right" class="LC_right_item">'.
 4714:                       '<table><tr>';
 4715:         foreach my $type (@types) {
 4716:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4717:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4718:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4719:         }
 4720:         $datatable .= '</tr></table></td></tr>'."\n";
 4721:         $itemcount ++;
 4722:         my $onclick = "toggleDisplay(this.form,'credits');";
 4723:         my $display = 'none';
 4724:         if ($currusecredits) {
 4725:             $display = 'block';
 4726:         }
 4727:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4728:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4729:         foreach my $type (@types) {
 4730:             next if ($type eq 'community');
 4731:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4732:                            '<input type="text" name="'.$type.'_credits"'.
 4733:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4734:         }
 4735:         $additional .= '</tr></table></div>'."\n";
 4736:         %defaultchecked = ('coursecredits' => 'off');
 4737:         @toggles = ('coursecredits');
 4738:         my $current = {
 4739:                         'coursecredits' => $currusecredits,
 4740:                       };
 4741:         (my $table,$itemcount) =
 4742:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4743:                                \%choices,$itemcount,$onclick,$additional,'left');
 4744:         $datatable .= $table;
 4745:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4746:         my $display = 'none';
 4747:         if ($postsubmitclient) {
 4748:             $display = 'block';
 4749:         }
 4750:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4751:                       &mt('Number of seconds submit is disabled').'<br />'.
 4752:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4753:                       '<table><tr>';
 4754:         foreach my $type (@types) {
 4755:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4756:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4757:                            $deftimeout{$type}.'" size="5" /></td>';
 4758:         }
 4759:         $additional .= '</tr></table></div>'."\n";
 4760:         %defaultchecked = ('postsubmit' => 'on');
 4761:         @toggles = ('postsubmit');
 4762:         $current = {
 4763:                        'postsubmit' => $postsubmitclient,
 4764:                    };
 4765:         ($table,$itemcount) =
 4766:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4767:                                \%choices,$itemcount,$onclick,$additional,'left');
 4768:         $datatable .= $table;
 4769:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4770:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4771:                       $choices{'mysqltables'}.
 4772:                       '</span></td>'.
 4773:                       '<td align="right" class="LC_right_item">'.
 4774:                       '<table><tr>';
 4775:         foreach my $type (@types) {
 4776:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4777:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4778:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 4779:         }
 4780:         $datatable .= '</tr></table></td></tr>'."\n";
 4781:         $itemcount ++;
 4782: 
 4783:     }
 4784:     $$rowtotal += $itemcount;
 4785:     return $datatable;
 4786: }
 4787: 
 4788: sub print_selfenrollment {
 4789:     my ($position,$dom,$settings,$rowtotal) = @_;
 4790:     my ($css_class,$datatable);
 4791:     my $itemcount = 1;
 4792:     my @types = ('official','unofficial','community','textbook');
 4793:     if (($position eq 'top') || ($position eq 'middle')) {
 4794:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4795:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4796:         my @rows;
 4797:         my $key;
 4798:         if ($position eq 'top') {
 4799:             $key = 'admin'; 
 4800:             if (ref($rowsref) eq 'ARRAY') {
 4801:                 @rows = @{$rowsref};
 4802:             }
 4803:         } elsif ($position eq 'middle') {
 4804:             $key = 'default';
 4805:             @rows = ('types','registered','approval','limit');
 4806:         }
 4807:         foreach my $row (@rows) {
 4808:             if (defined($titlesref->{$row})) {
 4809:                 $itemcount ++;
 4810:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4811:                 $datatable .= '<tr'.$css_class.'>'.
 4812:                               '<td>'.$titlesref->{$row}.'</td>'.
 4813:                               '<td class="LC_left_item">'.
 4814:                               '<table><tr>';
 4815:                 my (%current,%currentcap);
 4816:                 if (ref($settings) eq 'HASH') {
 4817:                     if (ref($settings->{$key}) eq 'HASH') {
 4818:                         foreach my $type (@types) {
 4819:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4820:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4821:                             }
 4822:                             if (($row eq 'limit') && ($key eq 'default')) {
 4823:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4824:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4825:                                 }
 4826:                             }
 4827:                         }
 4828:                     }
 4829:                 }
 4830:                 my %roles = (
 4831:                              '0' => &Apache::lonnet::plaintext('dc'),
 4832:                             ); 
 4833:             
 4834:                 foreach my $type (@types) {
 4835:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4836:                         $datatable .= '<th>'.&mt($type).'</th>';
 4837:                     }
 4838:                 }
 4839:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4840:                     $datatable .= '</tr><tr>';
 4841:                 }
 4842:                 foreach my $type (@types) {
 4843:                     if ($type eq 'community') {
 4844:                         $roles{'1'} = &mt('Community personnel');
 4845:                     } else {
 4846:                         $roles{'1'} = &mt('Course personnel');
 4847:                     }
 4848:                     $datatable .= '<td style="vertical-align: top">';
 4849:                     if ($position eq 'top') {
 4850:                         my %checked;
 4851:                         if ($current{$type} eq '0') {
 4852:                             $checked{'0'} = ' checked="checked"';
 4853:                         } else {
 4854:                             $checked{'1'} = ' checked="checked"';
 4855:                         }
 4856:                         foreach my $role ('1','0') {
 4857:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4858:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4859:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4860:                                           $roles{$role}.'</label></span> ';
 4861:                         }
 4862:                     } else {
 4863:                         if ($row eq 'types') {
 4864:                             my %checked;
 4865:                             if ($current{$type} =~ /^(all|dom)$/) {
 4866:                                 $checked{$1} = ' checked="checked"';
 4867:                             } else {
 4868:                                 $checked{''} = ' checked="checked"';
 4869:                             }
 4870:                             foreach my $val ('','dom','all') {
 4871:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4872:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4873:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4874:                             }
 4875:                         } elsif ($row eq 'registered') {
 4876:                             my %checked;
 4877:                             if ($current{$type} eq '1') {
 4878:                                 $checked{'1'} = ' checked="checked"';
 4879:                             } else {
 4880:                                 $checked{'0'} = ' checked="checked"';
 4881:                             }
 4882:                             foreach my $val ('0','1') {
 4883:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4884:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4885:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4886:                             }
 4887:                         } elsif ($row eq 'approval') {
 4888:                             my %checked;
 4889:                             if ($current{$type} =~ /^([12])$/) {
 4890:                                 $checked{$1} = ' checked="checked"';
 4891:                             } else {
 4892:                                 $checked{'0'} = ' checked="checked"';
 4893:                             }
 4894:                             for my $val (0..2) {
 4895:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4896:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4897:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4898:                             }
 4899:                         } elsif ($row eq 'limit') {
 4900:                             my %checked;
 4901:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4902:                                 $checked{$1} = ' checked="checked"';
 4903:                             } else {
 4904:                                 $checked{'none'} = ' checked="checked"';
 4905:                             }
 4906:                             my $cap;
 4907:                             if ($currentcap{$type} =~ /^\d+$/) {
 4908:                                 $cap = $currentcap{$type};
 4909:                             }
 4910:                             foreach my $val ('none','allstudents','selfenrolled') {
 4911:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4912:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4913:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4914:                             }
 4915:                             $datatable .= '<br />'.
 4916:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4917:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4918:                                           '</span>'; 
 4919:                         }
 4920:                     }
 4921:                     $datatable .= '</td>';
 4922:                 }
 4923:                 $datatable .= '</tr>';
 4924:             }
 4925:             $datatable .= '</table></td></tr>';
 4926:         }
 4927:     } elsif ($position eq 'bottom') {
 4928:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4929:     }
 4930:     $$rowtotal += $itemcount;
 4931:     return $datatable;
 4932: }
 4933: 
 4934: sub print_validation_rows {
 4935:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4936:     my ($itemsref,$namesref,$fieldsref);
 4937:     if ($caller eq 'selfenroll') { 
 4938:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4939:     } elsif ($caller eq 'requestcourses') {
 4940:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4941:     }
 4942:     my %currvalidation;
 4943:     if (ref($settings) eq 'HASH') {
 4944:         if (ref($settings->{'validation'}) eq 'HASH') {
 4945:             %currvalidation = %{$settings->{'validation'}};
 4946:         }
 4947:     }
 4948:     my $datatable;
 4949:     my $itemcount = 0;
 4950:     foreach my $item (@{$itemsref}) {
 4951:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4952:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4953:                       $namesref->{$item}.
 4954:                       '</span></td>'.
 4955:                       '<td class="LC_left_item">';
 4956:         if (($item eq 'url') || ($item eq 'button')) {
 4957:             $datatable .= '<span class="LC_nobreak">'.
 4958:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4959:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4960:         } elsif ($item eq 'fields') {
 4961:             my @currfields;
 4962:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4963:                 @currfields = @{$currvalidation{$item}};
 4964:             }
 4965:             foreach my $field (@{$fieldsref}) {
 4966:                 my $check = '';
 4967:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4968:                     $check = ' checked="checked"';
 4969:                 }
 4970:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4971:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4972:                               ' value="'.$field.'"'.$check.' />'.$field.
 4973:                               '</label></span> ';
 4974:             }
 4975:         } elsif ($item eq 'markup') {
 4976:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 4977:                            $currvalidation{$item}.
 4978:                               '</textarea>';
 4979:         }
 4980:         $datatable .= '</td></tr>'."\n";
 4981:         if (ref($rowtotal)) {
 4982:             $itemcount ++;
 4983:         }
 4984:     }
 4985:     if ($caller eq 'requestcourses') {
 4986:         my %currhash;
 4987:         if (ref($settings) eq 'HASH') {
 4988:             if (ref($settings->{'validation'}) eq 'HASH') {
 4989:                 if ($settings->{'validation'}{'dc'} ne '') {
 4990:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4991:                 }
 4992:             }
 4993:         }
 4994:         my $numinrow = 2;
 4995:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4996:                                                        'validationdc',%currhash);
 4997:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4998:         $datatable .= '<tr'.$css_class.'><td>';
 4999:         if ($numdc > 1) {
 5000:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 5001:         } else {
 5002:             $datatable .=  &mt('Course creation processed as: ');
 5003:         }
 5004:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 5005:         $itemcount ++;
 5006:     }
 5007:     if (ref($rowtotal)) {
 5008:         $$rowtotal += $itemcount;
 5009:     }
 5010:     return $datatable;
 5011: }
 5012: 
 5013: sub print_passwords {
 5014:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 5015:     my ($datatable,$css_class);
 5016:     my $itemcount = 0;
 5017:     my %titles = &Apache::lonlocal::texthash (
 5018:         captcha        => '"Forgot Password" CAPTCHA validation',
 5019:         link           => 'Reset link expiration (hours)',
 5020:         case           => 'Case-sensitive usernames/e-mail',
 5021:         prelink        => 'Information required (form 1)',
 5022:         postlink       => 'Information required (form 2)',
 5023:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 5024:         customtext     => 'Domain specific text (HTML)',
 5025:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 5026:         intauth_check  => 'Check bcrypt cost if authenticated',
 5027:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 5028:         permanent      => 'Permanent e-mail address',
 5029:         critical       => 'Critical notification address',
 5030:         notify         => 'Notification address',
 5031:         min            => 'Minimum password length',
 5032:         max            => 'Maximum password length',
 5033:         chars          => 'Required characters',
 5034:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 5035:     );
 5036:     if ($position eq 'top') {
 5037:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5038:         my $shownlinklife = 2;
 5039:         my $prelink = 'both';
 5040:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 5041:         if (ref($settings) eq 'HASH') {
 5042:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 5043:                 $shownlinklife = $settings->{resetlink};
 5044:             }
 5045:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 5046:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 5047:             }
 5048:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 5049:                 $prelink = $settings->{resetprelink};
 5050:             }
 5051:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 5052:                 %postlink = %{$settings->{resetpostlink}};
 5053:             }
 5054:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 5055:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 5056:             }
 5057:             if ($settings->{resetremove}) {
 5058:                 $nostdtext = 1;
 5059:             }
 5060:             if ($settings->{resetcustom}) {
 5061:                 $customurl = $settings->{resetcustom};
 5062:             }
 5063:         } else {
 5064:             if (ref($types) eq 'ARRAY') {
 5065:                 foreach my $item (@{$types}) {
 5066:                     $casesens{$item} = 1;
 5067:                     $postlink{$item} = ['username','email'];
 5068:                 }
 5069:             }
 5070:             $casesens{'default'} = 1;
 5071:             $postlink{'default'} = ['username','email'];
 5072:             $prelink = 'both';
 5073:             %emailsrc = (
 5074:                           permanent => 1,
 5075:                           critical  => 1,
 5076:                           notify    => 1,
 5077:             );
 5078:         }
 5079:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 5080:         $itemcount ++;
 5081:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5082:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 5083:                       '<td class="LC_left_item">'.
 5084:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 5085:                       'name="passwords_link" size="3" /></td></tr>';
 5086:         $itemcount ++;
 5087:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5088:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 5089:                       '<td class="LC_left_item">';
 5090:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 5091:             foreach my $item (@{$types}) {
 5092:                 my $checkedcase;
 5093:                 if ($casesens{$item}) {
 5094:                     $checkedcase = ' checked="checked"';
 5095:                 }
 5096:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5097:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 5098:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 5099:                               '</span>&nbsp;&nbsp; ';
 5100:             }
 5101:         }
 5102:         my $checkedcase;
 5103:         if ($casesens{'default'}) {
 5104:             $checkedcase = ' checked="checked"';
 5105:         }
 5106:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 5107:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 5108:                       $othertitle.'</label></span></td>';
 5109:         $itemcount ++;
 5110:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5111:         my %checkedpre = (
 5112:                              both => ' checked="checked"',
 5113:                              either => '',
 5114:                          );
 5115:         if ($prelink eq 'either') {
 5116:             $checkedpre{either} = ' checked="checked"';
 5117:             $checkedpre{both} = '';
 5118:         }
 5119:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 5120:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 5121:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 5122:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 5123:                       '<span class="LC_nobreak"><label>'.
 5124:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 5125:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 5126:         $itemcount ++;
 5127:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5128:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 5129:                       '<td class="LC_left_item">';
 5130:         my %postlinked;
 5131:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 5132:             foreach my $item (@{$types}) {
 5133:                 undef(%postlinked);
 5134:                 $datatable .= '<fieldset style="display: inline-block;">'.
 5135:                               '<legend>'.$usertypes->{$item}.'</legend>';
 5136:                 if (ref($postlink{$item}) eq 'ARRAY') {
 5137:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 5138:                 }
 5139:                 foreach my $field ('email','username') {
 5140:                     my $checked;
 5141:                     if ($postlinked{$field}) {
 5142:                         $checked = ' checked="checked"';
 5143:                     }
 5144:                     $datatable .= '<span class="LC_nobreak"><label>'.
 5145:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 5146:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 5147:                                   '<span>&nbsp;&nbsp; ';
 5148:                 }
 5149:                 $datatable .= '</fieldset>';
 5150:             }
 5151:         }
 5152:         if (ref($postlink{'default'}) eq 'ARRAY') {
 5153:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 5154:         }
 5155:         $datatable .= '<fieldset style="display: inline-block;">'.
 5156:                       '<legend>'.$othertitle.'</legend>';
 5157:         foreach my $field ('email','username') {
 5158:             my $checked;
 5159:             if ($postlinked{$field}) {
 5160:                 $checked = ' checked="checked"';
 5161:             }
 5162:             $datatable .= '<span class="LC_nobreak"><label>'.
 5163:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 5164:                           $field.'"'.$checked.' />'.$field.'</label>'.
 5165:                           '<span>&nbsp;&nbsp; ';
 5166:         }
 5167:         $datatable .= '</fieldset></td></tr>';
 5168:         $itemcount ++;
 5169:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5170:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 5171:                       '<td class="LC_left_item">';
 5172:         foreach my $type ('permanent','critical','notify') {
 5173:             my $checkedemail;
 5174:             if ($emailsrc{$type}) {
 5175:                 $checkedemail = ' checked="checked"';
 5176:             }
 5177:             $datatable .= '<span class="LC_nobreak"><label>'.
 5178:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 5179:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 5180:                           '<span>&nbsp;&nbsp; ';
 5181:         }
 5182:         $datatable .= '</td></tr>';
 5183:         $itemcount ++;
 5184:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5185:         my $switchserver = &check_switchserver($dom,$confname);
 5186:         my ($showstd,$noshowstd);
 5187:         if ($nostdtext) {
 5188:             $noshowstd = ' checked="checked"';
 5189:         } else {
 5190:             $showstd = ' checked="checked"';
 5191:         }
 5192:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 5193:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 5194:                       &mt('Retain standard text:').
 5195:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 5196:                       &mt('Yes').'</label>'.'&nbsp;'.
 5197:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 5198:                       &mt('No').'</label></span><br />'.
 5199:                       '<span class="LC_fontsize_small">'.
 5200:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 5201:                       &mt('Include custom text:');
 5202:         if ($customurl) {
 5203:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 5204:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 5205:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 5206:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 5207:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 5208:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 5209:         }
 5210:         if ($switchserver) {
 5211:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 5212:         } else {
 5213:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 5214:                          '<input type="file" name="passwords_customfile" /></span>';
 5215:         }
 5216:         $datatable .= '</td></tr>';
 5217:     } elsif ($position eq 'middle') {
 5218:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 5219:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 5220:         my %defaults;
 5221:         if (ref($domconf{'defaults'}) eq 'HASH') {
 5222:             %defaults = %{$domconf{'defaults'}};
 5223:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 5224:                 $defaults{'intauth_cost'} = 10;
 5225:             }
 5226:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 5227:                 $defaults{'intauth_check'} = 0;
 5228:             }
 5229:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 5230:                 $defaults{'intauth_switch'} = 0;
 5231:             }
 5232:         } else {
 5233:             %defaults = (
 5234:                           'intauth_cost'   => 10,
 5235:                           'intauth_check'  => 0,
 5236:                           'intauth_switch' => 0,
 5237:                         );
 5238:         }
 5239:         foreach my $item (@items) {
 5240:             if ($itemcount%2) {
 5241:                 $css_class = '';
 5242:             } else {
 5243:                 $css_class = ' class="LC_odd_row" ';
 5244:             }
 5245:             $datatable .= '<tr'.$css_class.'>'.
 5246:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 5247:                           '</span></td><td class="LC_left_item" colspan="3">';
 5248:             if ($item eq 'intauth_switch') {
 5249:                 my @options = (0,1,2);
 5250:                 my %optiondesc = &Apache::lonlocal::texthash (
 5251:                                    0 => 'No',
 5252:                                    1 => 'Yes',
 5253:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 5254:                                  );
 5255:                 $datatable .= '<table width="100%">';
 5256:                 foreach my $option (@options) {
 5257:                     my $checked = ' ';
 5258:                     if ($defaults{$item} eq $option) {
 5259:                         $checked = ' checked="checked"';
 5260:                     }
 5261:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 5262:                                   '<label><input type="radio" name="'.$item.
 5263:                                   '" value="'.$option.'"'.$checked.' />'.
 5264:                                   $optiondesc{$option}.'</label></span></td></tr>';
 5265:                 }
 5266:                 $datatable .= '</table>';
 5267:             } elsif ($item eq 'intauth_check') {
 5268:                 my @options = (0,1,2);
 5269:                 my %optiondesc = &Apache::lonlocal::texthash (
 5270:                                    0 => 'No',
 5271:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 5272:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 5273:                                  );
 5274:                 $datatable .= '<table width="100%">';
 5275:                 foreach my $option (@options) {
 5276:                     my $checked = ' ';
 5277:                     my $onclick;
 5278:                     if ($defaults{$item} eq $option) {
 5279:                         $checked = ' checked="checked"';
 5280:                     }
 5281:                     if ($option == 2) {
 5282:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 5283:                     }
 5284:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 5285:                                   '<label><input type="radio" name="'.$item.
 5286:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 5287:                                   $optiondesc{$option}.'</label></span></td></tr>';
 5288:                 }
 5289:                 $datatable .= '</table>';
 5290:             } else {
 5291:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 5292:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 5293:             }
 5294:             $datatable .= '</td></tr>';
 5295:             $itemcount ++;
 5296:         }
 5297:     } elsif ($position eq 'lower') {
 5298:         my ($min,$max,%chars,$numsaved);
 5299:         $min = $Apache::lonnet::passwdmin;
 5300:         if (ref($settings) eq 'HASH') {
 5301:             if ($settings->{min}) {
 5302:                 $min = $settings->{min};
 5303:             }
 5304:             if ($settings->{max}) {
 5305:                 $max = $settings->{max};
 5306:             }
 5307:             if (ref($settings->{chars}) eq 'ARRAY') {
 5308:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 5309:             }
 5310:             if ($settings->{numsaved}) {
 5311:                 $numsaved = $settings->{numsaved};
 5312:             }
 5313:         }
 5314:         my %rulenames = &Apache::lonlocal::texthash(
 5315:                                                      uc => 'At least one upper case letter',
 5316:                                                      lc => 'At least one lower case letter',
 5317:                                                      num => 'At least one number',
 5318:                                                      spec => 'At least one non-alphanumeric',
 5319:                                                    );
 5320:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5321:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 5322:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 5323:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 5324:                       'onblur="javascript:warnIntPass(this);" />'.
 5325:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 5326:                       '</span></td></tr>';
 5327:         $itemcount ++;
 5328:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5329:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 5330:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 5331:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 5332:                       'onblur="javascript:warnIntPass(this);" />'.
 5333:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 5334:                       '</span></td></tr>';
 5335:         $itemcount ++;
 5336:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5337:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 5338:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 5339:                       '</span></td>';
 5340:         my $numinrow = 2;
 5341:         my @possrules = ('uc','lc','num','spec');
 5342:         $datatable .= '<td class="LC_left_item"><table>';
 5343:         for (my $i=0; $i<@possrules; $i++) {
 5344:             my ($rem,$checked);
 5345:             if ($chars{$possrules[$i]}) {
 5346:                 $checked = ' checked="checked"';
 5347:             }
 5348:             $rem = $i%($numinrow);
 5349:             if ($rem == 0) {
 5350:                 if ($i > 0) {
 5351:                     $datatable .= '</tr>';
 5352:                 }
 5353:                 $datatable .= '<tr>';
 5354:             }
 5355:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 5356:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 5357:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 5358:         }
 5359:         my $rem = @possrules%($numinrow);
 5360:         my $colsleft = $numinrow - $rem;
 5361:         if ($colsleft > 1 ) {
 5362:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5363:                           '&nbsp;</td>';
 5364:         } elsif ($colsleft == 1) {
 5365:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5366:         }
 5367:         $datatable .='</table></td></tr>';
 5368:         $itemcount ++;
 5369:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5370:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 5371:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 5372:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" '.
 5373:                       'onblur="javascript:warnIntPass(this);" />'.
 5374:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 5375:                       '</span></td></tr>';
 5376:     } else {
 5377:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5378:         my %ownerchg = (
 5379:                           by  => {},
 5380:                           for => {},
 5381:                        );
 5382:         my %ownertitles = &Apache::lonlocal::texthash (
 5383:                             by  => 'Course owner status(es) allowed',
 5384:                             for => 'Student status(es) allowed',
 5385:                           );
 5386:         if (ref($settings) eq 'HASH') {
 5387:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 5388:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 5389:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 5390:                 }
 5391:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 5392:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 5393:                 }
 5394:             }
 5395:         }
 5396:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5397:         $datatable .= '<tr '.$css_class.'>'.
 5398:                       '<td>'.
 5399:                       &mt('Requirements').'<ul>'.
 5400:                       '<li>'.&mt("Course 'type' is not a Community").'</li>'.
 5401:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 5402:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 5403:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 5404:                       '</ul>'.
 5405:                       '</td>'.
 5406:                       '<td class="LC_left_item">';
 5407:         foreach my $item ('by','for') {
 5408:             $datatable .= '<fieldset style="display: inline-block;">'.
 5409:                           '<legend>'.$ownertitles{$item}.'</legend>';
 5410:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 5411:                 foreach my $type (@{$types}) {
 5412:                     my $checked;
 5413:                     if ($ownerchg{$item}{$type}) {
 5414:                         $checked = ' checked="checked"';
 5415:                     }
 5416:                     $datatable .= '<span class="LC_nobreak"><label>'.
 5417:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 5418:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 5419:                                   '</span>&nbsp;&nbsp; ';
 5420:                 }
 5421:             }
 5422:             my $checked;
 5423:             if ($ownerchg{$item}{'default'}) {
 5424:                 $checked = ' checked="checked"';
 5425:             }
 5426:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 5427:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 5428:                           $othertitle.'</label></span></fieldset>';
 5429:         }
 5430:         $datatable .= '</td></tr>';
 5431:     }
 5432:     return $datatable;
 5433: }
 5434: 
 5435: sub print_wafproxy {
 5436:     my ($position,$dom,$settings,$rowtotal) = @_;
 5437:     my $css_class;
 5438:     my $itemcount = 0;
 5439:     my $datatable;
 5440:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5441:     my (%othercontrol,%otherdoms,%aliases,%saml,%values,$setdom,$showdom);
 5442:     my %lt = &wafproxy_titles();
 5443:     foreach my $server (sort(keys(%servers))) {
 5444:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 5445:         next if ($serverhome eq '');
 5446:         my $serverdom;
 5447:         if ($serverhome ne $server) {
 5448:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 5449:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 5450:                 $othercontrol{$server} = $serverdom;
 5451:             }
 5452:         } else {
 5453:             $serverdom = &Apache::lonnet::host_domain($server);
 5454:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 5455:             if ($serverdom ne $dom) {
 5456:                 $othercontrol{$server} = $serverdom;
 5457:             } else {
 5458:                 $setdom = 1;
 5459:                 if (ref($settings) eq 'HASH') {
 5460:                     if (ref($settings->{'alias'}) eq 'HASH') {
 5461:                         $aliases{$dom} = $settings->{'alias'};
 5462:                         if ($aliases{$dom} ne '') {
 5463:                             $showdom = 1;
 5464:                         }
 5465:                     }
 5466:                     if (ref($settings->{'saml'}) eq 'HASH') {
 5467:                         $saml{$dom} = $settings->{'saml'};
 5468:                     }
 5469:                 }
 5470:             }
 5471:         }
 5472:     }
 5473:     if ($setdom) {
 5474:         %{$values{$dom}} = ();
 5475:         if (ref($settings) eq 'HASH') {
 5476:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 5477:                 $values{$dom}{$item} = $settings->{$item};
 5478:             }
 5479:         }
 5480:     }
 5481:     if (keys(%othercontrol)) {
 5482:         %otherdoms = reverse(%othercontrol);
 5483:         foreach my $domain (keys(%otherdoms)) {
 5484:             %{$values{$domain}} = ();
 5485:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 5486:             if (ref($config{'wafproxy'}) eq 'HASH') {
 5487:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 5488:                 if (exists($config{'wafproxy'}{'saml'})) {
 5489:                     $saml{$domain} = $config{'wafproxy'}{'saml'};
 5490:                 }
 5491:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 5492:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 5493:                 }
 5494:             }
 5495:         }
 5496:     }
 5497:     if ($position eq 'top') {
 5498:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5499:         my %aliasinfo;
 5500:         foreach my $server (sort(keys(%servers))) {
 5501:             $itemcount ++;
 5502:             my $dom_in_effect;
 5503:             my $aliasrows = '<tr>'.
 5504:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 5505:                             &mt('Hostname').':&nbsp;'.
 5506:                             '<i>'.&Apache::lonnet::hostname($server).'</i></td><td>&nbsp;</td>';
 5507:             if ($othercontrol{$server}) {
 5508:                 $dom_in_effect = $othercontrol{$server};
 5509:                 my ($current,$forsaml);
 5510:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 5511:                     $current = $aliases{$dom_in_effect}{$server};
 5512:                 }
 5513:                 if (ref($saml{$dom_in_effect}) eq 'HASH') {
 5514:                     if ($saml{$dom_in_effect}{$server}) {
 5515:                         $forsaml = 1;
 5516:                     }
 5517:                 }
 5518:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 5519:                               &mt('Alias').':&nbsp';
 5520:                 if ($current) {
 5521:                     $aliasrows .= $current;
 5522:                     if ($forsaml) {
 5523:                          $aliasrows .= '&nbsp;('.&mt('also for SSO Auth').')';
 5524:                     }
 5525:                 } else {
 5526:                     $aliasrows .= &mt('None');
 5527:                 }
 5528:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 5529:                               &mt('controlled by domain: [_1]',
 5530:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 5531:             } else {
 5532:                 $dom_in_effect = $dom;
 5533:                 my ($current,$samlon,$samloff);
 5534:                 $samloff = ' checked="checked"';
 5535:                 if (ref($aliases{$dom}) eq 'HASH') {
 5536:                     if ($aliases{$dom}{$server}) {
 5537:                         $current = $aliases{$dom}{$server};
 5538:                     }
 5539:                 }
 5540:                 if (ref($saml{$dom}) eq 'HASH') {
 5541:                     if ($saml{$dom}{$server}) {
 5542:                         $samlon = $samloff;
 5543:                         undef($samloff);
 5544:                     }
 5545:                 }
 5546:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 5547:                               &mt('Alias').':&nbsp;'.
 5548:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 5549:                               'value="'.$current.'" size="30" />'.
 5550:                               ('&nbsp;'x2).'<span class="LC_nobreak">'.
 5551:                               &mt('Alias used for SSO Auth').':&nbsp;<label>'.
 5552:                               '<input type="radio" value="0"'.$samloff.' name="wafproxy_alias_saml_'.$server.'" />'.
 5553:                               &mt('No').'</label>&nbsp;<label>'.
 5554:                               '<input type="radio" value="1"'.$samlon.' name="wafproxy_alias_saml_'.$server.'" />'.
 5555:                               &mt('Yes').'</label></span>'.
 5556:                               '</td>';
 5557:             }
 5558:             $aliasrows .= '</tr>';
 5559:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 5560:         }
 5561:         if ($aliasinfo{$dom}) {
 5562:             my ($onclick,$wafon,$wafoff,$showtable);
 5563:             $onclick = ' onclick="javascript:toggleWAF();"';
 5564:             $wafoff = ' checked="checked"';
 5565:             $showtable = ' style="display:none";';
 5566:             if ($showdom) {
 5567:                 $wafon = $wafoff;
 5568:                 $wafoff = '';
 5569:                 $showtable = ' style="display:inline;"';
 5570:             }
 5571:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5572:             $datatable = '<tr'.$css_class.'>'.
 5573:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 5574:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 5575:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 5576:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 5577:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 5578:                          &mt('No').'</label></span></td>'.
 5579:                          '<td class="LC_left_item">'.
 5580:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 5581:                          '</table></td></tr>';
 5582:             $itemcount++;
 5583:         }
 5584:         if (keys(%otherdoms)) {
 5585:             foreach my $key (sort(keys(%otherdoms))) {
 5586:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5587:                 $datatable .= '<tr'.$css_class.'>'.
 5588:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 5589:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 5590:                               '</table></td></tr>';
 5591:                 $itemcount++;
 5592:             }
 5593:         }
 5594:     } else {
 5595:         my %ip_methods = &remoteip_methods();
 5596:         if ($setdom) {
 5597:             $itemcount ++;
 5598:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5599:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 5600:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 5601:             $wafstyle = ' style="display:none;"';
 5602:             $nowafstyle = ' style="display:table-row;"';
 5603:             $currwafdisplay = ' style="display: none"';
 5604:             $wafrangestyle = ' style="display: none"';
 5605:             $curr_remotip = 'n';
 5606:             $ssltossl = ' checked="checked"';
 5607:             if ($showdom) {
 5608:                 $wafstyle = ' style="display:table-row;"';
 5609:                 $nowafstyle =  ' style="display:none;"';
 5610:                 if (keys(%{$values{$dom}})) {
 5611:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 5612:                         $curr_remotip = $values{$dom}{remoteip};
 5613:                     }
 5614:                     if ($curr_remotip eq 'h') {
 5615:                         $currwafdisplay = ' style="display:table-row"';
 5616:                         $wafrangestyle = ' style="display:inline-block;"';
 5617:                     }
 5618:                     if ($values{$dom}{'sslopt'}) {
 5619:                         $alltossl = ' checked="checked"';
 5620:                         $ssltossl = '';
 5621:                     }
 5622:                 }
 5623:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 5624:                     $vpndircheck = ' checked="checked"';
 5625:                     $currwafvpn = ' style="display:table-row;"';
 5626:                     $wafrangestyle = ' style="display:inline-block;"';
 5627:                 } else {
 5628:                     $vpnaliascheck = ' checked="checked"';
 5629:                     $currwafvpn = ' style="display:none;"';
 5630:                 }
 5631:             }
 5632:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 5633:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 5634:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 5635:                           '</tr>'.
 5636:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 5637:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 5638:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 5639:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 5640:                           &mt('Range(s) stored in CIDR notation').'</div></td>'.
 5641:                           '<td class="LC_left_item"><table>'.
 5642:                           '<tr>'.
 5643:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 5644:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 5645:             foreach my $option ('m','h','n') {
 5646:                 my $sel;
 5647:                 if ($option eq $curr_remotip) {
 5648:                    $sel = ' selected="selected"';
 5649:                 }
 5650:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 5651:                               $ip_methods{$option}.'</option>';
 5652:             }
 5653:             $datatable .= '</select></td></tr>'."\n".
 5654:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 5655:                           $lt{'ipheader'}.':&nbsp;'.
 5656:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 5657:                           'name="wafproxy_ipheader" />'.
 5658:                           '</td></tr>'."\n".
 5659:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 5660:                           $lt{'trusted'}.':<br />'.
 5661:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 5662:                           $values{$dom}{'trusted'}.'</textarea>'.
 5663:                           '</td></tr>'."\n".
 5664:                           '<tr><td><hr /></td></tr>'."\n".
 5665:                           '<tr>'.
 5666:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 5667:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 5668:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 5669:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 5670:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 5671:             foreach my $item ('vpnint','vpnext') {
 5672:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 5673:                               '<td valign="top">'.$lt{$item}.':<br />'.
 5674:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 5675:                               $values{$dom}{$item}.'</textarea>'.
 5676:                               '</td></tr>'."\n";
 5677:             }
 5678:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 5679:                           '<tr>'.
 5680:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 5681:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 5682:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 5683:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 5684:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 5685:                           '</table></td></tr>';
 5686:         }
 5687:         if (keys(%otherdoms)) {
 5688:             foreach my $domain (sort(keys(%otherdoms))) {
 5689:                 $itemcount ++;
 5690:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5691:                 $datatable .= '<tr'.$css_class.'>'.
 5692:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 5693:                               '<td class="LC_left_item"><table>';
 5694:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 5695:                     my $showval = &mt('None');
 5696:                     if ($item eq 'ssl') {
 5697:                         $showval = $lt{'ssltossl'};
 5698:                     }
 5699:                     if ($values{$domain}{$item}) {
 5700:                         $showval = $values{$domain}{$item};
 5701:                         if ($item eq 'ssl') {
 5702:                             $showval = $lt{'alltossl'};
 5703:                         } elsif ($item eq 'remoteip') {
 5704:                             $showval = $ip_methods{$values{$domain}{$item}};
 5705:                         }
 5706:                     }
 5707:                     $datatable .= '<tr>'.
 5708:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 5709:                 }
 5710:                 $datatable .= '</table></td></tr>';
 5711:             }
 5712:         }
 5713:     }
 5714:     $$rowtotal += $itemcount;
 5715:     return $datatable;
 5716: }
 5717: 
 5718: sub wafproxy_titles {
 5719:     return &Apache::lonlocal::texthash(
 5720:                remoteip   => "Method for determining user's IP",
 5721:                ipheader   => 'Request header containing remote IP',
 5722:                trusted    => 'Trusted IP range(s)',
 5723:                vpnaccess  => 'Access from institutional VPN',
 5724:                vpndirect  => 'via regular hostname (no WAF)',
 5725:                vpnaliased => 'via aliased hostname (WAF)',
 5726:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 5727:                vpnext     => 'IP Range(s) for backend WAF connections',
 5728:                sslopt     => 'Forwarding http/https',
 5729:                alltossl   => 'WAF forwards both http and https requests to https',
 5730:                ssltossl   => 'WAF forwards http requests to http and https to https',
 5731:            );
 5732: }
 5733: 
 5734: sub remoteip_methods {
 5735:     return &Apache::lonlocal::texthash(
 5736:               m => 'Use Apache mod_remoteip',
 5737:               h => 'Use headers parsed by LON-CAPA',
 5738:               n => 'Not in use',
 5739:            );
 5740: }
 5741: 
 5742: sub print_usersessions {
 5743:     my ($position,$dom,$settings,$rowtotal) = @_;
 5744:     my ($css_class,$datatable,%checked,%choices);
 5745:     my (%by_ip,%by_location,@intdoms);
 5746:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 5747: 
 5748:     my @alldoms = &Apache::lonnet::all_domains();
 5749:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 5750:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5751:     my %altids = &id_for_thisdom(%servers);
 5752:     my $itemcount = 1;
 5753:     if ($position eq 'top') {
 5754:         if (keys(%serverhomes) > 1) {
 5755:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 5756:             my ($curroffloadnow,$curroffloadoth);
 5757:             if (ref($settings) eq 'HASH') {
 5758:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 5759:                     $curroffloadnow = $settings->{'offloadnow'};
 5760:                 }
 5761:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 5762:                     $curroffloadoth = $settings->{'offloadoth'};
 5763:                 }
 5764:             }
 5765:             my $other_insts = scalar(keys(%by_location));
 5766:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 5767:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 5768:         } else {
 5769:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 5770:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 5771:         }
 5772:     } else {
 5773:         if (keys(%by_location) == 0) {
 5774:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 5775:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 5776:         } else {
 5777:             my %lt = &usersession_titles();
 5778:             my $numinrow = 5;
 5779:             my $prefix;
 5780:             my @types;
 5781:             if ($position eq 'bottom') {
 5782:                 $prefix = 'remote';
 5783:                 @types = ('version','excludedomain','includedomain');
 5784:             } else {
 5785:                 $prefix = 'hosted';
 5786:                 @types = ('excludedomain','includedomain');
 5787:             }
 5788:             my (%current,%checkedon,%checkedoff);
 5789:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 5790:             my @locations = sort(keys(%by_location));
 5791:             foreach my $type (@types) {
 5792:                 $checkedon{$type} = '';
 5793:                 $checkedoff{$type} = ' checked="checked"';
 5794:             }
 5795:             if (ref($settings) eq 'HASH') {
 5796:                 if (ref($settings->{$prefix}) eq 'HASH') {
 5797:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 5798:                         $current{$key} = $settings->{$prefix}{$key};
 5799:                         if ($key eq 'version') {
 5800:                             if ($current{$key} ne '') {
 5801:                                 $checkedon{$key} = ' checked="checked"';
 5802:                                 $checkedoff{$key} = '';
 5803:                             }
 5804:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 5805:                             $checkedon{$key} = ' checked="checked"';
 5806:                             $checkedoff{$key} = '';
 5807:                         }
 5808:                     }
 5809:                 }
 5810:             }
 5811:             foreach my $type (@types) {
 5812:                 next if ($type ne 'version' && !@locations);
 5813:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5814:                 $datatable .= '<tr'.$css_class.'>
 5815:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 5816:                                <span class="LC_nobreak">&nbsp;
 5817:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 5818:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 5819:                 if ($type eq 'version') {
 5820:                     my $selector = '<select name="'.$prefix.'_version">';
 5821:                     foreach my $version (@lcversions) {
 5822:                         my $selected = '';
 5823:                         if ($current{'version'} eq $version) {
 5824:                             $selected = ' selected="selected"';
 5825:                         }
 5826:                         $selector .= ' <option value="'.$version.'"'.
 5827:                                      $selected.'>'.$version.'</option>';
 5828:                     }
 5829:                     $selector .= '</select> ';
 5830:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 5831:                 } else {
 5832:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 5833:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 5834:                                  ' />'.('&nbsp;'x2).
 5835:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 5836:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 5837:                                  "\n".
 5838:                                  '</div><div><table>';
 5839:                     my $rem;
 5840:                     for (my $i=0; $i<@locations; $i++) {
 5841:                         my ($showloc,$value,$checkedtype);
 5842:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 5843:                             my $ip = $by_location{$locations[$i]}->[0];
 5844:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 5845:                                  $value = join(':',@{$by_ip{$ip}});
 5846:                                 $showloc = join(', ',@{$by_ip{$ip}});
 5847:                                 if (ref($current{$type}) eq 'ARRAY') {
 5848:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 5849:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 5850:                                             $checkedtype = ' checked="checked"';
 5851:                                             last;
 5852:                                         }
 5853:                                     }
 5854:                                 }
 5855:                             }
 5856:                         }
 5857:                         $rem = $i%($numinrow);
 5858:                         if ($rem == 0) {
 5859:                             if ($i > 0) {
 5860:                                 $datatable .= '</tr>';
 5861:                             }
 5862:                             $datatable .= '<tr>';
 5863:                         }
 5864:                         $datatable .= '<td class="LC_left_item">'.
 5865:                                       '<span class="LC_nobreak"><label>'.
 5866:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 5867:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 5868:                                       '</label></span></td>';
 5869:                     }
 5870:                     $rem = @locations%($numinrow);
 5871:                     my $colsleft = $numinrow - $rem;
 5872:                     if ($colsleft > 1 ) {
 5873:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5874:                                       '&nbsp;</td>';
 5875:                     } elsif ($colsleft == 1) {
 5876:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5877:                     }
 5878:                     $datatable .= '</tr></table>';
 5879:                 }
 5880:                 $datatable .= '</td></tr>';
 5881:                 $itemcount ++;
 5882:             }
 5883:         }
 5884:     }
 5885:     $$rowtotal += $itemcount;
 5886:     return $datatable;
 5887: }
 5888: 
 5889: sub build_location_hashes {
 5890:     my ($intdoms,$by_ip,$by_location) = @_;
 5891:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 5892:                   (ref($by_location) eq 'HASH')); 
 5893:     my %iphost = &Apache::lonnet::get_iphost();
 5894:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 5895:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 5896:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 5897:         foreach my $id (@{$iphost{$primary_ip}}) {
 5898:             my $intdom = &Apache::lonnet::internet_dom($id);
 5899:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 5900:                 push(@{$intdoms},$intdom);
 5901:             }
 5902:         }
 5903:     }
 5904:     foreach my $ip (keys(%iphost)) {
 5905:         if (ref($iphost{$ip}) eq 'ARRAY') {
 5906:             foreach my $id (@{$iphost{$ip}}) {
 5907:                 my $location = &Apache::lonnet::internet_dom($id);
 5908:                 if ($location) {
 5909:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 5910:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5911:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 5912:                             push(@{$by_ip->{$ip}},$location);
 5913:                         }
 5914:                     } else {
 5915:                         $by_ip->{$ip} = [$location];
 5916:                     }
 5917:                 }
 5918:             }
 5919:         }
 5920:     }
 5921:     foreach my $ip (sort(keys(%{$by_ip}))) {
 5922:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5923:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 5924:             my $first = $by_ip->{$ip}->[0];
 5925:             if (ref($by_location->{$first}) eq 'ARRAY') {
 5926:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 5927:                     push(@{$by_location->{$first}},$ip);
 5928:                 }
 5929:             } else {
 5930:                 $by_location->{$first} = [$ip];
 5931:             }
 5932:         }
 5933:     }
 5934:     return;
 5935: }
 5936: 
 5937: sub current_offloads_to {
 5938:     my ($dom,$settings,$servers) = @_;
 5939:     my (%spareid,%otherdomconfigs);
 5940:     if (ref($servers) eq 'HASH') {
 5941:         foreach my $lonhost (sort(keys(%{$servers}))) {
 5942:             my $gotspares;
 5943:             if (ref($settings) eq 'HASH') {
 5944:                 if (ref($settings->{'spares'}) eq 'HASH') {
 5945:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 5946:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 5947:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 5948:                         $gotspares = 1;
 5949:                     }
 5950:                 }
 5951:             }
 5952:             unless ($gotspares) {
 5953:                 my $gotspares;
 5954:                 my $serverhomeID =
 5955:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 5956:                 my $serverhomedom =
 5957:                     &Apache::lonnet::host_domain($serverhomeID);
 5958:                 if ($serverhomedom ne $dom) {
 5959:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 5960:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 5961:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 5962:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 5963:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 5964:                                 $gotspares = 1;
 5965:                             }
 5966:                         }
 5967:                     } else {
 5968:                         $otherdomconfigs{$serverhomedom} =
 5969:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 5970:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 5971:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 5972:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 5973:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 5974:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 5975:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 5976:                                         $gotspares = 1;
 5977:                                     }
 5978:                                 }
 5979:                             }
 5980:                         }
 5981:                     }
 5982:                 }
 5983:             }
 5984:             unless ($gotspares) {
 5985:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5986:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5987:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5988:                } else {
 5989:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 5990:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 5991:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5992:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5993:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5994:                     } else {
 5995:                         my %what = (
 5996:                              spareid => 1,
 5997:                         );
 5998:                         my ($result,$returnhash) = 
 5999:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 6000:                         if ($result eq 'ok') { 
 6001:                             if (ref($returnhash) eq 'HASH') {
 6002:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 6003:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 6004:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 6005:                                 }
 6006:                             }
 6007:                         }
 6008:                     }
 6009:                 }
 6010:             }
 6011:         }
 6012:     }
 6013:     return %spareid;
 6014: }
 6015: 
 6016: sub spares_row {
 6017:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 6018:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 6019:     my $css_class;
 6020:     my $numinrow = 4;
 6021:     my $itemcount = 1;
 6022:     my $datatable;
 6023:     my %typetitles = &sparestype_titles();
 6024:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 6025:         foreach my $server (sort(keys(%{$servers}))) {
 6026:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 6027:             my ($othercontrol,$serverdom);
 6028:             if ($serverhome ne $server) {
 6029:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 6030:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 6031:             } else {
 6032:                 $serverdom = &Apache::lonnet::host_domain($server);
 6033:                 if ($serverdom ne $dom) {
 6034:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 6035:                 }
 6036:             }
 6037:             next unless (ref($spareid->{$server}) eq 'HASH');
 6038:             my ($checkednow,$checkedoth);
 6039:             if (ref($curroffloadnow) eq 'HASH') {
 6040:                 if ($curroffloadnow->{$server}) {
 6041:                     $checkednow = ' checked="checked"';
 6042:                 }
 6043:             }
 6044:             if (ref($curroffloadoth) eq 'HASH') {
 6045:                 if ($curroffloadoth->{$server}) {
 6046:                     $checkedoth = ' checked="checked"';
 6047:                 }
 6048:             }
 6049:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6050:             $datatable .= '<tr'.$css_class.'>
 6051:                            <td rowspan="2">
 6052:                             <span class="LC_nobreak">'.
 6053:                           &mt('[_1] when busy, offloads to:'
 6054:                               ,'<b>'.$server.'</b>').'</span><br />'.
 6055:                           '<span class="LC_nobreak">'."\n".
 6056:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 6057:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 6058:                           "\n";
 6059:             if ($other_insts) {
 6060:                 $datatable .= '<br />'.
 6061:                               '<span class="LC_nobreak">'."\n".
 6062:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 6063:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 6064:                           "\n";
 6065:             }
 6066:             my (%current,%canselect);
 6067:             my @choices = 
 6068:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 6069:             foreach my $type ('primary','default') {
 6070:                 if (ref($spareid->{$server}) eq 'HASH') {
 6071:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 6072:                         my @spares = @{$spareid->{$server}{$type}};
 6073:                         if (@spares > 0) {
 6074:                             if ($othercontrol) {
 6075:                                 $current{$type} = join(', ',@spares);
 6076:                             } else {
 6077:                                 $current{$type} .= '<table>';
 6078:                                 my $numspares = scalar(@spares);
 6079:                                 for (my $i=0;  $i<@spares; $i++) {
 6080:                                     my $rem = $i%($numinrow);
 6081:                                     if ($rem == 0) {
 6082:                                         if ($i > 0) {
 6083:                                             $current{$type} .= '</tr>';
 6084:                                         }
 6085:                                         $current{$type} .= '<tr>';
 6086:                                     }
 6087:                                     $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;'.
 6088:                                                        $spareid->{$server}{$type}[$i].
 6089:                                                        '</label></td>'."\n";
 6090:                                 }
 6091:                                 my $rem = @spares%($numinrow);
 6092:                                 my $colsleft = $numinrow - $rem;
 6093:                                 if ($colsleft > 1 ) {
 6094:                                     $current{$type} .= '<td colspan="'.$colsleft.
 6095:                                                        '" class="LC_left_item">'.
 6096:                                                        '&nbsp;</td>';
 6097:                                 } elsif ($colsleft == 1) {
 6098:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 6099:                                 }
 6100:                                 $current{$type} .= '</tr></table>';
 6101:                             }
 6102:                         }
 6103:                     }
 6104:                     if ($current{$type} eq '') {
 6105:                         $current{$type} = &mt('None specified');
 6106:                     }
 6107:                     if ($othercontrol) {
 6108:                         if ($type eq 'primary') {
 6109:                             $canselect{$type} = $othercontrol;
 6110:                         }
 6111:                     } else {
 6112:                         $canselect{$type} = 
 6113:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 6114:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 6115:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 6116:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 6117:                         if (@choices > 0) {
 6118:                             foreach my $lonhost (@choices) {
 6119:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 6120:                             }
 6121:                         }
 6122:                         $canselect{$type} .= '</select>'."\n";
 6123:                     }
 6124:                 } else {
 6125:                     $current{$type} = &mt('Could not be determined');
 6126:                     if ($type eq 'primary') {
 6127:                         $canselect{$type} =  $othercontrol;
 6128:                     }
 6129:                 }
 6130:                 if ($type eq 'default') {
 6131:                     $datatable .= '<tr'.$css_class.'>';
 6132:                 }
 6133:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 6134:                               '<td>'.$current{$type}.'</td>'."\n".
 6135:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 6136:             }
 6137:             $itemcount ++;
 6138:         }
 6139:     }
 6140:     $$rowtotal += $itemcount;
 6141:     return $datatable;
 6142: }
 6143: 
 6144: sub possible_newspares {
 6145:     my ($server,$currspares,$serverhomes,$altids) = @_;
 6146:     my $serverhostname = &Apache::lonnet::hostname($server);
 6147:     my %excluded;
 6148:     if ($serverhostname ne '') {
 6149:         %excluded = (
 6150:                        $serverhostname => 1,
 6151:                     );
 6152:     }
 6153:     if (ref($currspares) eq 'HASH') {
 6154:         foreach my $type (keys(%{$currspares})) {
 6155:             if (ref($currspares->{$type}) eq 'ARRAY') {
 6156:                 if (@{$currspares->{$type}} > 0) {
 6157:                     foreach my $curr (@{$currspares->{$type}}) {
 6158:                         my $hostname = &Apache::lonnet::hostname($curr);
 6159:                         $excluded{$hostname} = 1;
 6160:                     }
 6161:                 }
 6162:             }
 6163:         }
 6164:     }
 6165:     my @choices;
 6166:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 6167:         if (keys(%{$serverhomes}) > 1) {
 6168:             foreach my $name (sort(keys(%{$serverhomes}))) {
 6169:                 unless ($excluded{$name}) {
 6170:                     if (exists($altids->{$serverhomes->{$name}})) {
 6171:                         push(@choices,$altids->{$serverhomes->{$name}});
 6172:                     } else {
 6173:                         push(@choices,$serverhomes->{$name});
 6174:                     }
 6175:                 }
 6176:             }
 6177:         }
 6178:     }
 6179:     return sort(@choices);
 6180: }
 6181: 
 6182: sub print_loadbalancing {
 6183:     my ($dom,$settings,$rowtotal) = @_;
 6184:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6185:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6186:     my $numinrow = 1;
 6187:     my $datatable;
 6188:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6189:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 6190:     if (ref($settings) eq 'HASH') {
 6191:         %existing = %{$settings};
 6192:     }
 6193:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 6194:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 6195:                                   \%currtargets,\%currrules,\%currcookies);
 6196:     } else {
 6197:         return;
 6198:     }
 6199:     my ($othertitle,$usertypes,$types) =
 6200:         &Apache::loncommon::sorted_inst_types($dom);
 6201:     my $rownum = 8;
 6202:     if (ref($types) eq 'ARRAY') {
 6203:         $rownum += scalar(@{$types});
 6204:     }
 6205:     my @css_class = ('LC_odd_row','LC_even_row');
 6206:     my $balnum = 0;
 6207:     my $islast;
 6208:     my (@toshow,$disabledtext);
 6209:     if (keys(%currbalancer) > 0) {
 6210:         @toshow = sort(keys(%currbalancer));
 6211:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 6212:             push(@toshow,'');
 6213:         }
 6214:     } else {
 6215:         @toshow = ('');
 6216:         $disabledtext = &mt('No existing load balancer');
 6217:     }
 6218:     foreach my $lonhost (@toshow) {
 6219:         if ($balnum == scalar(@toshow)-1) {
 6220:             $islast = 1;
 6221:         } else {
 6222:             $islast = 0;
 6223:         }
 6224:         my $cssidx = $balnum%2;
 6225:         my $targets_div_style = 'display: none';
 6226:         my $disabled_div_style = 'display: block';
 6227:         my $homedom_div_style = 'display: none';
 6228:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 6229:                       '<td rowspan="'.$rownum.'" valign="top">'.
 6230:                       '<p>';
 6231:         if ($lonhost eq '') {
 6232:             $datatable .= '<span class="LC_nobreak">';
 6233:             if (keys(%currbalancer) > 0) {
 6234:                 $datatable .= &mt('Add balancer:');
 6235:             } else {
 6236:                 $datatable .= &mt('Enable balancer:');
 6237:             }
 6238:             $datatable .= '&nbsp;'.
 6239:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 6240:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 6241:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 6242:                           '<option value="" selected="selected">'.&mt('None').
 6243:                           '</option>'."\n";
 6244:             foreach my $server (sort(keys(%servers))) {
 6245:                 next if ($currbalancer{$server});
 6246:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 6247:             }
 6248:             $datatable .=
 6249:                 '</select>'."\n".
 6250:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 6251:         } else {
 6252:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 6253:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 6254:                            &mt('Stop balancing').'</label>'.
 6255:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 6256:             $targets_div_style = 'display: block';
 6257:             $disabled_div_style = 'display: none';
 6258:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 6259:                 $homedom_div_style = 'display: block';
 6260:             }
 6261:         }
 6262:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 6263:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 6264:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 6265:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 6266:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 6267:         my @sparestypes = ('primary','default');
 6268:         my %typetitles = &sparestype_titles();
 6269:         my %hostherechecked = (
 6270:                                   no => ' checked="checked"',
 6271:                               );
 6272:         my %balcookiechecked = (
 6273:                                   no => ' checked="checked"',
 6274:                                );
 6275:         foreach my $sparetype (@sparestypes) {
 6276:             my $targettable;
 6277:             for (my $i=0; $i<$numspares; $i++) {
 6278:                 my $checked;
 6279:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 6280:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 6281:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 6282:                             $checked = ' checked="checked"';
 6283:                         }
 6284:                     }
 6285:                 }
 6286:                 my ($chkboxval,$disabled);
 6287:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 6288:                     $chkboxval = $spares[$i];
 6289:                 }
 6290:                 if (exists($currbalancer{$spares[$i]})) {
 6291:                     $disabled = ' disabled="disabled"';
 6292:                 }
 6293:                 $targettable .=
 6294:                     '<td><span class="LC_nobreak"><label>'.
 6295:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 6296:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 6297:                     '</span></label></span></td>';
 6298:                 my $rem = $i%($numinrow);
 6299:                 if ($rem == 0) {
 6300:                     if (($i > 0) && ($i < $numspares-1)) {
 6301:                         $targettable .= '</tr>';
 6302:                     }
 6303:                     if ($i < $numspares-1) {
 6304:                         $targettable .= '<tr>';
 6305:                     }
 6306:                 }
 6307:             }
 6308:             if ($targettable ne '') {
 6309:                 my $rem = $numspares%($numinrow);
 6310:                 my $colsleft = $numinrow - $rem;
 6311:                 if ($colsleft > 1 ) {
 6312:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6313:                                     '&nbsp;</td>';
 6314:                 } elsif ($colsleft == 1) {
 6315:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 6316:                 }
 6317:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 6318:                                '<table><tr>'.$targettable.'</tr></table><br />';
 6319:             }
 6320:             $hostherechecked{$sparetype} = '';
 6321:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 6322:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 6323:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 6324:                         $hostherechecked{$sparetype} = ' checked="checked"';
 6325:                         $hostherechecked{'no'} = '';
 6326:                     }
 6327:                 }
 6328:             }
 6329:         }
 6330:         if ($currcookies{$lonhost}) {
 6331:             %balcookiechecked = (
 6332:                                     yes => ' checked="checked"',
 6333:                                 );
 6334:         }
 6335:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 6336:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 6337:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 6338:         foreach my $sparetype (@sparestypes) {
 6339:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 6340:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 6341:                           '</i></label><br />';
 6342:         }
 6343:         $datatable .= &mt('Use balancer cookie').'<br />'.
 6344:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 6345:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 6346:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 6347:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 6348:                       '</div></td></tr>'.
 6349:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 6350:                                            $othertitle,$usertypes,$types,\%servers,
 6351:                                            \%currbalancer,$lonhost,
 6352:                                            $targets_div_style,$homedom_div_style,
 6353:                                            $css_class[$cssidx],$balnum,$islast);
 6354:         $$rowtotal += $rownum;
 6355:         $balnum ++;
 6356:     }
 6357:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 6358:     return $datatable;
 6359: }
 6360: 
 6361: sub get_loadbalancers_config {
 6362:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 6363:     return unless ((ref($servers) eq 'HASH') &&
 6364:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 6365:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 6366:                    (ref($currcookies) eq 'HASH'));
 6367:     if (keys(%{$existing}) > 0) {
 6368:         my $oldlonhost;
 6369:         foreach my $key (sort(keys(%{$existing}))) {
 6370:             if ($key eq 'lonhost') {
 6371:                 $oldlonhost = $existing->{'lonhost'};
 6372:                 $currbalancer->{$oldlonhost} = 1;
 6373:             } elsif ($key eq 'targets') {
 6374:                 if ($oldlonhost) {
 6375:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 6376:                 }
 6377:             } elsif ($key eq 'rules') {
 6378:                 if ($oldlonhost) {
 6379:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 6380:                 }
 6381:             } elsif (ref($existing->{$key}) eq 'HASH') {
 6382:                 $currbalancer->{$key} = 1;
 6383:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 6384:                 $currrules->{$key} = $existing->{$key}{'rules'};
 6385:                 if ($existing->{$key}{'cookie'}) {
 6386:                     $currcookies->{$key} = 1;
 6387:                 }
 6388:             }
 6389:         }
 6390:     } else {
 6391:         my ($balancerref,$targetsref) =
 6392:                 &Apache::lonnet::get_lonbalancer_config($servers);
 6393:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 6394:             foreach my $server (sort(keys(%{$balancerref}))) {
 6395:                 $currbalancer->{$server} = 1;
 6396:                 $currtargets->{$server} = $targetsref->{$server};
 6397:             }
 6398:         }
 6399:     }
 6400:     return;
 6401: }
 6402: 
 6403: sub loadbalancing_rules {
 6404:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 6405:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 6406:         $css_class,$balnum,$islast) = @_;
 6407:     my $output;
 6408:     my $num = 0;
 6409:     my ($alltypes,$othertypes,$titles) =
 6410:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 6411:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 6412:         foreach my $type (@{$alltypes}) {
 6413:             $num ++;
 6414:             my $current;
 6415:             if (ref($currrules) eq 'HASH') {
 6416:                 $current = $currrules->{$type};
 6417:             }
 6418:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 6419:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 6420:                     $current = '';
 6421:                 }
 6422:             }
 6423:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 6424:                                              $servers,$currbalancer,$lonhost,$dom,
 6425:                                              $targets_div_style,$homedom_div_style,
 6426:                                              $css_class,$balnum,$num,$islast);
 6427:         }
 6428:     }
 6429:     return $output;
 6430: }
 6431: 
 6432: sub loadbalancing_titles {
 6433:     my ($dom,$intdom,$usertypes,$types) = @_;
 6434:     my %othertypes = (
 6435:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 6436:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 6437:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 6438:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 6439:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 6440:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 6441:                      );
 6442:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 6443:     my @available;
 6444:     if (ref($types) eq 'ARRAY') {
 6445:         @available = @{$types};
 6446:     }
 6447:     unless (grep(/^default$/,@available)) {
 6448:         push(@available,'default');
 6449:     }
 6450:     unshift(@alltypes,@available);
 6451:     my %titles;
 6452:     foreach my $type (@alltypes) {
 6453:         if ($type =~ /^_LC_/) {
 6454:             $titles{$type} = $othertypes{$type};
 6455:         } elsif ($type eq 'default') {
 6456:             $titles{$type} = &mt('All users from [_1]',$dom);
 6457:             if (ref($types) eq 'ARRAY') {
 6458:                 if (@{$types} > 0) {
 6459:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 6460:                 }
 6461:             }
 6462:         } elsif (ref($usertypes) eq 'HASH') {
 6463:             $titles{$type} = $usertypes->{$type};
 6464:         }
 6465:     }
 6466:     return (\@alltypes,\%othertypes,\%titles);
 6467: }
 6468: 
 6469: sub loadbalance_rule_row {
 6470:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 6471:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 6472:     my @rulenames;
 6473:     my %ruletitles = &offloadtype_text();
 6474:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 6475:         @rulenames = ('balancer','offloadedto','specific');
 6476:     } else {
 6477:         @rulenames = ('default','homeserver');
 6478:         if ($type eq '_LC_external') {
 6479:             push(@rulenames,'externalbalancer');
 6480:         } else {
 6481:             push(@rulenames,'specific');
 6482:         }
 6483:         push(@rulenames,'none');
 6484:     }
 6485:     my $style = $targets_div_style;
 6486:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 6487:         $style = $homedom_div_style;
 6488:     }
 6489:     my $space;
 6490:     if ($islast && $num == 1) {
 6491:         $space = '<div display="inline-block">&nbsp;</div>';
 6492:     }
 6493:     my $output =
 6494:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 6495:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 6496:         '<td valaign="top">'.$space.
 6497:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 6498:     for (my $i=0; $i<@rulenames; $i++) {
 6499:         my $rule = $rulenames[$i];
 6500:         my ($checked,$extra);
 6501:         if ($rulenames[$i] eq 'default') {
 6502:             $rule = '';
 6503:         }
 6504:         if ($rulenames[$i] eq 'specific') {
 6505:             if (ref($servers) eq 'HASH') {
 6506:                 my $default;
 6507:                 if (($current ne '') && (exists($servers->{$current}))) {
 6508:                     $checked = ' checked="checked"';
 6509:                 }
 6510:                 unless ($checked) {
 6511:                     $default = ' selected="selected"';
 6512:                 }
 6513:                 $extra =
 6514:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 6515:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 6516:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 6517:                     '<option value=""'.$default.'></option>'."\n";
 6518:                 foreach my $server (sort(keys(%{$servers}))) {
 6519:                     if (ref($currbalancer) eq 'HASH') {
 6520:                         next if (exists($currbalancer->{$server}));
 6521:                     }
 6522:                     my $selected;
 6523:                     if ($server eq $current) {
 6524:                         $selected = ' selected="selected"';
 6525:                     }
 6526:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 6527:                 }
 6528:                 $extra .= '</select>';
 6529:             }
 6530:         } elsif ($rule eq $current) {
 6531:             $checked = ' checked="checked"';
 6532:         }
 6533:         $output .= '<span class="LC_nobreak"><label>'.
 6534:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 6535:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 6536:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 6537:                    ')"'.$checked.' />&nbsp;';
 6538:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 6539:             $output .= $ruletitles{'particular'};
 6540:         } else {
 6541:             $output .= $ruletitles{$rulenames[$i]};
 6542:         }
 6543:         $output .= '</label>'.$extra.'</span><br />'."\n";
 6544:     }
 6545:     $output .= '</div></td></tr>'."\n";
 6546:     return $output;
 6547: }
 6548: 
 6549: sub offloadtype_text {
 6550:     my %ruletitles = &Apache::lonlocal::texthash (
 6551:            'default'          => 'Offloads to default destinations',
 6552:            'homeserver'       => "Offloads to user's home server",
 6553:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 6554:            'specific'         => 'Offloads to specific server',
 6555:            'none'             => 'No offload',
 6556:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 6557:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 6558:            'particular'       => 'Session hosted (after re-auth) on server:',
 6559:     );
 6560:     return %ruletitles;
 6561: }
 6562: 
 6563: sub sparestype_titles {
 6564:     my %typestitles = &Apache::lonlocal::texthash (
 6565:                           'primary' => 'primary',
 6566:                           'default' => 'default',
 6567:                       );
 6568:     return %typestitles;
 6569: }
 6570: 
 6571: sub contact_titles {
 6572:     my %titles = &Apache::lonlocal::texthash (
 6573:                    'supportemail'    => 'Support E-mail address',
 6574:                    'adminemail'      => 'Default Server Admin E-mail address',
 6575:                    'errormail'       => 'Error reports to be e-mailed to',
 6576:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 6577:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 6578:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 6579:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 6580:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 6581:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 6582:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 6583:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 6584:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 6585:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 6586:                    'errorweights'    => 'Weights used to compute error count',
 6587:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 6588:                  );
 6589:     my %short_titles = &Apache::lonlocal::texthash (
 6590:                            adminemail   => 'Admin E-mail address',
 6591:                            supportemail => 'Support E-mail',
 6592:                        );   
 6593:     return (\%titles,\%short_titles);
 6594: }
 6595: 
 6596: sub helpform_fields {
 6597:     my %titles =  &Apache::lonlocal::texthash (
 6598:                        'username'   => 'Name',
 6599:                        'user'       => 'Username/domain',
 6600:                        'phone'      => 'Phone',
 6601:                        'cc'         => 'Cc e-mail',
 6602:                        'course'     => 'Course Details',
 6603:                        'section'    => 'Sections',
 6604:                        'screenshot' => 'File upload',
 6605:     );
 6606:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 6607:     my %possoptions = (
 6608:                         username     => ['yes','no','req'],
 6609:                         phone        => ['yes','no','req'],
 6610:                         user         => ['yes','no'],
 6611:                         cc           => ['yes','no'],
 6612:                         course       => ['yes','no'],
 6613:                         section      => ['yes','no'],
 6614:                         screenshot   => ['yes','no'],
 6615:                       );
 6616:     my %fieldoptions = &Apache::lonlocal::texthash (
 6617:                          'yes'  => 'Optional',
 6618:                          'req'  => 'Required',
 6619:                          'no'   => "Not shown",
 6620:     );
 6621:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 6622: }
 6623: 
 6624: sub tool_titles {
 6625:     my %titles = &Apache::lonlocal::texthash (
 6626:                      aboutme    => 'Personal web page',
 6627:                      blog       => 'Blog',
 6628:                      webdav     => 'WebDAV',
 6629:                      portfolio  => 'Portfolio',
 6630:                      official   => 'Official courses (with institutional codes)',
 6631:                      unofficial => 'Unofficial courses',
 6632:                      community  => 'Communities',
 6633:                      textbook   => 'Textbook courses',
 6634:                  );
 6635:     return %titles;
 6636: }
 6637: 
 6638: sub courserequest_titles {
 6639:     my %titles = &Apache::lonlocal::texthash (
 6640:                                    official   => 'Official',
 6641:                                    unofficial => 'Unofficial',
 6642:                                    community  => 'Communities',
 6643:                                    textbook   => 'Textbook',
 6644:                                    norequest  => 'Not allowed',
 6645:                                    approval   => 'Approval by Dom. Coord.',
 6646:                                    validate   => 'With validation',
 6647:                                    autolimit  => 'Numerical limit',
 6648:                                    unlimited  => '(blank for unlimited)',
 6649:                  );
 6650:     return %titles;
 6651: }
 6652: 
 6653: sub authorrequest_titles {
 6654:     my %titles = &Apache::lonlocal::texthash (
 6655:                                    norequest  => 'Not allowed',
 6656:                                    approval   => 'Approval by Dom. Coord.',
 6657:                                    automatic  => 'Automatic approval',
 6658:                  );
 6659:     return %titles;
 6660: }
 6661: 
 6662: sub courserequest_conditions {
 6663:     my %conditions = &Apache::lonlocal::texthash (
 6664:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 6665:        validate   => '(Processing of request subject to institutional validation).',
 6666:                  );
 6667:     return %conditions;
 6668: }
 6669: 
 6670: 
 6671: sub print_usercreation {
 6672:     my ($position,$dom,$settings,$rowtotal) = @_;
 6673:     my $numinrow = 4;
 6674:     my $datatable;
 6675:     if ($position eq 'top') {
 6676:         $$rowtotal ++;
 6677:         my $rowcount = 0;
 6678:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 6679:         if (ref($rules) eq 'HASH') {
 6680:             if (keys(%{$rules}) > 0) {
 6681:                 $datatable .= &user_formats_row('username',$settings,$rules,
 6682:                                                 $ruleorder,$numinrow,$rowcount);
 6683:                 $$rowtotal ++;
 6684:                 $rowcount ++;
 6685:             }
 6686:         }
 6687:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 6688:         if (ref($idrules) eq 'HASH') {
 6689:             if (keys(%{$idrules}) > 0) {
 6690:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 6691:                                                 $idruleorder,$numinrow,$rowcount);
 6692:                 $$rowtotal ++;
 6693:                 $rowcount ++;
 6694:             }
 6695:         }
 6696:         if ($rowcount == 0) {
 6697:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 6698:             $$rowtotal ++;
 6699:             $rowcount ++;
 6700:         }
 6701:     } elsif ($position eq 'middle') {
 6702:         my @creators = ('author','course','requestcrs');
 6703:         my ($rules,$ruleorder) =
 6704:             &Apache::lonnet::inst_userrules($dom,'username');
 6705:         my %lt = &usercreation_types();
 6706:         my %checked;
 6707:         if (ref($settings) eq 'HASH') {
 6708:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 6709:                 foreach my $item (@creators) {
 6710:                     $checked{$item} = $settings->{'cancreate'}{$item};
 6711:                 }
 6712:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 6713:                 foreach my $item (@creators) {
 6714:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 6715:                         $checked{$item} = 'none';
 6716:                     }
 6717:                 }
 6718:             }
 6719:         }
 6720:         my $rownum = 0;
 6721:         foreach my $item (@creators) {
 6722:             $rownum ++;
 6723:             if ($checked{$item} eq '') {
 6724:                 $checked{$item} = 'any';
 6725:             }
 6726:             my $css_class;
 6727:             if ($rownum%2) {
 6728:                 $css_class = '';
 6729:             } else {
 6730:                 $css_class = ' class="LC_odd_row" ';
 6731:             }
 6732:             $datatable .= '<tr'.$css_class.'>'.
 6733:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 6734:                          '</span></td><td align="right">';
 6735:             my @options = ('any');
 6736:             if (ref($rules) eq 'HASH') {
 6737:                 if (keys(%{$rules}) > 0) {
 6738:                     push(@options,('official','unofficial'));
 6739:                 }
 6740:             }
 6741:             push(@options,'none');
 6742:             foreach my $option (@options) {
 6743:                 my $type = 'radio';
 6744:                 my $check = ' ';
 6745:                 if ($checked{$item} eq $option) {
 6746:                     $check = ' checked="checked" ';
 6747:                 } 
 6748:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6749:                               '<input type="'.$type.'" name="can_createuser_'.
 6750:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 6751:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 6752:             }
 6753:             $datatable .= '</td></tr>';
 6754:         }
 6755:     } else {
 6756:         my @contexts = ('author','course','domain');
 6757:         my @authtypes = ('int','krb4','krb5','loc');
 6758:         my %checked;
 6759:         if (ref($settings) eq 'HASH') {
 6760:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 6761:                 foreach my $item (@contexts) {
 6762:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 6763:                         foreach my $auth (@authtypes) {
 6764:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 6765:                                 $checked{$item}{$auth} = ' checked="checked" ';
 6766:                             }
 6767:                         }
 6768:                     }
 6769:                 }
 6770:             }
 6771:         } else {
 6772:             foreach my $item (@contexts) {
 6773:                 foreach my $auth (@authtypes) {
 6774:                     $checked{$item}{$auth} = ' checked="checked" ';
 6775:                 }
 6776:             }
 6777:         }
 6778:         my %title = &context_names();
 6779:         my %authname = &authtype_names();
 6780:         my $rownum = 0;
 6781:         my $css_class; 
 6782:         foreach my $item (@contexts) {
 6783:             if ($rownum%2) {
 6784:                 $css_class = '';
 6785:             } else {
 6786:                 $css_class = ' class="LC_odd_row" ';
 6787:             }
 6788:             $datatable .=   '<tr'.$css_class.'>'.
 6789:                             '<td>'.$title{$item}.
 6790:                             '</td><td class="LC_left_item">'.
 6791:                             '<span class="LC_nobreak">';
 6792:             foreach my $auth (@authtypes) {
 6793:                 $datatable .= '<label>'. 
 6794:                               '<input type="checkbox" name="'.$item.'_auth" '.
 6795:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 6796:                               $authname{$auth}.'</label>&nbsp;';
 6797:             }
 6798:             $datatable .= '</span></td></tr>';
 6799:             $rownum ++;
 6800:         }
 6801:         $$rowtotal += $rownum;
 6802:     }
 6803:     return $datatable;
 6804: }
 6805: 
 6806: sub print_selfcreation {
 6807:     my ($position,$dom,$settings,$rowtotal) = @_;
 6808:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 6809:         $emaildomain,$datatable);
 6810:     if (ref($settings) eq 'HASH') {
 6811:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 6812:             $createsettings = $settings->{'cancreate'};
 6813:             if (ref($createsettings) eq 'HASH') {
 6814:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 6815:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 6816:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 6817:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 6818:                         @selfcreate = ('email','login','sso');
 6819:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 6820:                         @selfcreate = ($createsettings->{'selfcreate'});
 6821:                     }
 6822:                 }
 6823:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 6824:                     $processing = $createsettings->{'selfcreateprocessing'};
 6825:                 }
 6826:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 6827:                     $emailoptions = $createsettings->{'emailoptions'};
 6828:                 }
 6829:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 6830:                     $emailverified = $createsettings->{'emailverified'};
 6831:                 }
 6832:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 6833:                     $emaildomain = $createsettings->{'emaildomain'};
 6834:                 }
 6835:             }
 6836:         }
 6837:     }
 6838:     my %radiohash;
 6839:     my $numinrow = 4;
 6840:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 6841:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6842:     if ($position eq 'top') {
 6843:         my %choices = &Apache::lonlocal::texthash (
 6844:                                                       cancreate_login      => 'Institutional Login',
 6845:                                                       cancreate_sso        => 'Institutional Single Sign On',
 6846:                                                   );
 6847:         my @toggles = sort(keys(%choices));
 6848:         my %defaultchecked = (
 6849:                                'cancreate_login' => 'off',
 6850:                                'cancreate_sso'   => 'off',
 6851:                              );
 6852:         my ($onclick,$itemcount);
 6853:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 6854:                                                      \%choices,$itemcount,$onclick);
 6855:         $$rowtotal += $itemcount;
 6856: 
 6857:         if (ref($usertypes) eq 'HASH') {
 6858:             if (keys(%{$usertypes}) > 0) {
 6859:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 6860:                                              $dom,$numinrow,$othertitle,
 6861:                                              'statustocreate',$rowtotal);
 6862:                 $$rowtotal ++;
 6863:             }
 6864:         }
 6865:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 6866:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6867:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 6868:         my $rem;
 6869:         my $numperrow = 2;
 6870:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 6871:         $datatable .= '<tr'.$css_class.'>'.
 6872:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 6873:                      '<td class="LC_left_item">'."\n".
 6874:                      '<table>'."\n";
 6875:         for (my $i=0; $i<@fields; $i++) {
 6876:             $rem = $i%($numperrow);
 6877:             if ($rem == 0) {
 6878:                 if ($i > 0) {
 6879:                     $datatable .= '</tr>';
 6880:                 }
 6881:                 $datatable .= '<tr>';
 6882:             }
 6883:             my $currval;
 6884:             if (ref($createsettings) eq 'HASH') {
 6885:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 6886:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 6887:                 }
 6888:             }
 6889:             $datatable .= '<td class="LC_left_item">'.
 6890:                           '<span class="LC_nobreak">'.
 6891:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 6892:                           'value="'.$currval.'" size="10" />&nbsp;'.
 6893:                           $fieldtitles{$fields[$i]}.'</span></td>';
 6894:         }
 6895:         my $colsleft = $numperrow - $rem;
 6896:         if ($colsleft > 1 ) {
 6897:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6898:                          '&nbsp;</td>';
 6899:         } elsif ($colsleft == 1) {
 6900:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 6901:         }
 6902:         $datatable .= '</tr></table></td></tr>';
 6903:         $$rowtotal ++;
 6904:     } elsif ($position eq 'middle') {
 6905:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 6906:         my @posstypes;
 6907:         if (ref($types) eq 'ARRAY') {
 6908:             @posstypes = @{$types};
 6909:         }
 6910:         unless (grep(/^default$/,@posstypes)) {
 6911:             push(@posstypes,'default');
 6912:         }
 6913:         my %usertypeshash;
 6914:         if (ref($usertypes) eq 'HASH') {
 6915:             %usertypeshash = %{$usertypes};
 6916:         }
 6917:         $usertypeshash{'default'} = $othertitle;
 6918:         foreach my $status (@posstypes) {
 6919:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 6920:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 6921:             $$rowtotal ++;
 6922:         }
 6923:     } else {
 6924:         my %choices = &Apache::lonlocal::texthash (
 6925:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 6926:                                                   );
 6927:         my @toggles = sort(keys(%choices));
 6928:         my %defaultchecked = (
 6929:                                'cancreate_email' => 'off',
 6930:                              );
 6931:         my $customclass = 'LC_selfcreate_email';
 6932:         my $classprefix = 'LC_canmodify_emailusername_';
 6933:         my $optionsprefix = 'LC_options_emailusername_';
 6934:         my $display = 'none';
 6935:         my $rowstyle = 'display:none';
 6936:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 6937:             $display = 'block';
 6938:             $rowstyle = 'display:table-row';
 6939:         }
 6940:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 6941:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 6942:                                                      \%choices,$$rowtotal,$onclick);
 6943:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 6944:                                          $rowstyle);
 6945:         $$rowtotal ++;
 6946:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 6947:                                       $rowstyle);
 6948:         $$rowtotal ++;
 6949:         my (@ordered,@posstypes,%usertypeshash);
 6950:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6951:         my ($emailrules,$emailruleorder) =
 6952:             &Apache::lonnet::inst_userrules($dom,'email');
 6953:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6954:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6955:         if (ref($types) eq 'ARRAY') {
 6956:             @posstypes = @{$types};
 6957:         }
 6958:         if (@posstypes) {
 6959:             unless (grep(/^default$/,@posstypes)) {
 6960:                 push(@posstypes,'default');
 6961:             }
 6962:             if (ref($usertypes) eq 'HASH') {
 6963:                 %usertypeshash = %{$usertypes};
 6964:             }
 6965:             my $currassign;
 6966:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 6967:                 $currassign = {
 6968:                                   selfassign => $domdefaults{'inststatusguest'},
 6969:                               };
 6970:                 @ordered = @{$domdefaults{'inststatusguest'}};
 6971:             } else {
 6972:                 $currassign = { selfassign => [] };
 6973:             }
 6974:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 6975:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 6976:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 6977:                                          $numinrow,$othertitle,'selfassign',
 6978:                                          $rowtotal,$onclicktypes,$customclass,
 6979:                                          $rowstyle);
 6980:             $$rowtotal ++;
 6981:             $usertypeshash{'default'} = $othertitle;
 6982:             foreach my $status (@posstypes) {
 6983:                 my $css_class;
 6984:                 if ($$rowtotal%2) {
 6985:                     $css_class = 'LC_odd_row ';
 6986:                 }
 6987:                 $css_class .= $customclass;
 6988:                 my $rowid = $optionsprefix.$status;
 6989:                 my $hidden = 1;
 6990:                 my $currstyle = 'display:none';
 6991:                 if (grep(/^\Q$status\E$/,@ordered)) {
 6992:                     $currstyle = $rowstyle;
 6993:                     $hidden = 0;
 6994:                 }
 6995:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 6996:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 6997:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 6998:                 unless ($hidden) {
 6999:                     $$rowtotal ++;
 7000:                 }
 7001:             }
 7002:         } else {
 7003:             my $css_class;
 7004:             if ($$rowtotal%2) {
 7005:                 $css_class = 'LC_odd_row ';
 7006:             }
 7007:             $css_class .= $customclass;
 7008:             $usertypeshash{'default'} = $othertitle;
 7009:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 7010:                                          $emailrules,$emailruleorder,$settings,'default','',
 7011:                                          $othertitle,$css_class,$rowstyle,$intdom);
 7012:             $$rowtotal ++;
 7013:         }
 7014:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 7015:         $numinrow = 1;
 7016:         if (@posstypes) {
 7017:             foreach my $status (@posstypes) {
 7018:                 my $rowid = $classprefix.$status;
 7019:                 my $datarowstyle = 'display:none';
 7020:                 if (grep(/^\Q$status\E$/,@ordered)) {
 7021:                     $datarowstyle = $rowstyle;
 7022:                 }
 7023:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 7024:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 7025:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 7026:                 unless ($datarowstyle eq 'display:none') {
 7027:                     $$rowtotal ++;
 7028:                 }
 7029:             }
 7030:         } else {
 7031:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 7032:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 7033:                                                    $infotitles,'',$customclass,$rowstyle);
 7034:         }
 7035:     }
 7036:     return $datatable;
 7037: }
 7038: 
 7039: sub selfcreate_javascript {
 7040:     return <<"ENDSCRIPT";
 7041: 
 7042: <script type="text/javascript">
 7043: // <![CDATA[
 7044: 
 7045: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 7046:     var x = document.getElementsByClassName(target);
 7047:     var insttypes = 0;
 7048:     var insttypeRegExp = new RegExp(prefix);
 7049:     if ((x.length != undefined) && (x.length > 0)) {
 7050:         if (form.elements[radio].length != undefined) {
 7051:             for (var i=0; i<form.elements[radio].length; i++) {
 7052:                 if (form.elements[radio][i].checked) {
 7053:                     if (form.elements[radio][i].value == 1) {
 7054:                         for (var j=0; j<x.length; j++) {
 7055:                             if (x[j].id == 'undefined') {
 7056:                                 x[j].style.display = 'table-row';
 7057:                             } else if (insttypeRegExp.test(x[j].id)) {
 7058:                                 insttypes ++;
 7059:                             } else {
 7060:                                 x[j].style.display = 'table-row';
 7061:                             }
 7062:                         }
 7063:                     } else {
 7064:                         for (var j=0; j<x.length; j++) {
 7065:                             x[j].style.display = 'none';
 7066:                         }
 7067:                     }
 7068:                     break;
 7069:                 }
 7070:             }
 7071:             if (insttypes > 0) {
 7072:                 toggleDataRow(form,checkbox,target,altprefix);
 7073:                 toggleDataRow(form,checkbox,target,prefix,1);
 7074:             }
 7075:         }
 7076:     }
 7077:     return;
 7078: }
 7079: 
 7080: function toggleDataRow(form,checkbox,target,prefix,docount) {
 7081:     if (form.elements[checkbox].length != undefined) {
 7082:         var count = 0;
 7083:         if (docount) {
 7084:             for (var i=0; i<form.elements[checkbox].length; i++) {
 7085:                 if (form.elements[checkbox][i].checked) {
 7086:                     count ++;
 7087:                 }
 7088:             }
 7089:         }
 7090:         for (var i=0; i<form.elements[checkbox].length; i++) {
 7091:             var type = form.elements[checkbox][i].value;
 7092:             if (document.getElementById(prefix+type)) {
 7093:                 if (form.elements[checkbox][i].checked) {
 7094:                     document.getElementById(prefix+type).style.display = 'table-row';
 7095:                     if (count % 2 == 1) {
 7096:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 7097:                     } else {
 7098:                         document.getElementById(prefix+type).className = target;
 7099:                     }
 7100:                     count ++;
 7101:                 } else {
 7102:                     document.getElementById(prefix+type).style.display = 'none';
 7103:                 }
 7104:             }
 7105:         }
 7106:     }
 7107:     return;
 7108: }
 7109: 
 7110: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 7111:     var caller = radio+'_'+status;
 7112:     if (form.elements[caller].length != undefined) {
 7113:         for (var i=0; i<form.elements[caller].length; i++) {
 7114:             if (form.elements[caller][i].checked) {
 7115:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 7116:                     var curr = form.elements[caller][i].value;
 7117:                     if (prefix) {
 7118:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 7119:                     }
 7120:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 7121:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 7122:                     if (curr == 'custom') {
 7123:                         if (prefix) {
 7124:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 7125:                         }
 7126:                     } else if (curr == 'inst') {
 7127:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 7128:                     } else if (curr == 'noninst') {
 7129:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 7130:                     }
 7131:                     break;
 7132:                 }
 7133:             }
 7134:         }
 7135:     }
 7136: }
 7137: 
 7138: // ]]>
 7139: </script>
 7140: 
 7141: ENDSCRIPT
 7142: }
 7143: 
 7144: sub noninst_users {
 7145:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 7146:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
 7147:     my $class = 'LC_left_item';
 7148:     if ($css_class) {
 7149:         $css_class = ' class="'.$css_class.'"';
 7150:     }
 7151:     if ($rowid) {
 7152:         $rowid = ' id="'.$rowid.'"';
 7153:     }
 7154:     if ($rowstyle) {
 7155:         $rowstyle = ' style="'.$rowstyle.'"';
 7156:     }
 7157:     my ($output,$description);
 7158:     if ($type eq 'default') {
 7159:         $description = &mt('Requests for: [_1]',$typetitle);
 7160:     } else {
 7161:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 7162:     }
 7163:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 7164:               "<td>$description</td>\n".
 7165:               '<td class="'.$class.'" colspan="2">'.
 7166:               '<table><tr>';
 7167:     my %headers = &Apache::lonlocal::texthash(
 7168:               approve  => 'Processing',
 7169:               email    => 'E-mail',
 7170:               username => 'Username',
 7171:     );
 7172:     foreach my $item ('approve','email','username') {
 7173:         $output .= '<th>'.$headers{$item}.'</th>';
 7174:     }
 7175:     $output .= '</tr><tr>';
 7176:     foreach my $item ('approve','email','username') {
 7177:         $output .= '<td valign="top">';
 7178:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 7179:         if ($item eq 'approve') {
 7180:             %choices = &Apache::lonlocal::texthash (
 7181:                                                      automatic => 'Automatically approved',
 7182:                                                      approval  => 'Queued for approval',
 7183:                                                    );
 7184:             @options = ('automatic','approval');
 7185:             $hashref = $processing;
 7186:             $defoption = 'automatic';
 7187:             $name = 'cancreate_emailprocess_'.$type;
 7188:         } elsif ($item eq 'email') {
 7189:             %choices = &Apache::lonlocal::texthash (
 7190:                                                      any     => 'Any e-mail',
 7191:                                                      inst    => 'Institutional only',
 7192:                                                      noninst => 'Non-institutional only',
 7193:                                                      custom  => 'Custom restrictions',
 7194:                                                    );
 7195:             @options = ('any','inst','noninst');
 7196:             my $showcustom;
 7197:             if (ref($emailrules) eq 'HASH') {
 7198:                 if (keys(%{$emailrules}) > 0) {
 7199:                     push(@options,'custom');
 7200:                     $showcustom = 'cancreate_emailrule';
 7201:                     if (ref($settings) eq 'HASH') {
 7202:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 7203:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 7204:                                 if (exists($emailrules->{$rule})) {
 7205:                                     $hascustom ++;
 7206:                                 }
 7207:                             }
 7208:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 7209:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 7210:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 7211:                                     if (exists($emailrules->{$rule})) {
 7212:                                         $hascustom ++;
 7213:                                     }
 7214:                                 }
 7215:                             }
 7216:                         }
 7217:                     }
 7218:                 }
 7219:             }
 7220:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 7221:                                                      "'cancreate_emaildomain','$type'".');"';
 7222:             $hashref = $emailoptions;
 7223:             $defoption = 'any';
 7224:             $name = 'cancreate_emailoptions_'.$type;
 7225:         } elsif ($item eq 'username') {
 7226:             %choices = &Apache::lonlocal::texthash (
 7227:                                                      all    => 'Same as e-mail',
 7228:                                                      first  => 'Omit @domain',
 7229:                                                      free   => 'Free to choose',
 7230:                                                    );
 7231:             @options = ('all','first','free');
 7232:             $hashref = $emailverified;
 7233:             $defoption = 'all';
 7234:             $name = 'cancreate_usernameoptions_'.$type;
 7235:         }
 7236:         foreach my $option (@options) {
 7237:             my $checked;
 7238:             if (ref($hashref) eq 'HASH') {
 7239:                 if ($type eq '') {
 7240:                     if (!exists($hashref->{'default'})) {
 7241:                         if ($option eq $defoption) {
 7242:                             $checked = ' checked="checked"';
 7243:                         }
 7244:                     } else {
 7245:                         if ($hashref->{'default'} eq $option) {
 7246:                             $checked = ' checked="checked"';
 7247:                         }
 7248:                     }
 7249:                 } else {
 7250:                     if (!exists($hashref->{$type})) {
 7251:                         if ($option eq $defoption) {
 7252:                             $checked = ' checked="checked"';
 7253:                         }
 7254:                     } else {
 7255:                         if ($hashref->{$type} eq $option) {
 7256:                             $checked = ' checked="checked"';
 7257:                         }
 7258:                     }
 7259:                 }
 7260:             } elsif (($item eq 'email') && ($hascustom)) {
 7261:                 if ($option eq 'custom') {
 7262:                     $checked = ' checked="checked"';
 7263:                 }
 7264:             } elsif ($option eq $defoption) {
 7265:                 $checked = ' checked="checked"';
 7266:             }
 7267:             $output .= '<span class="LC_nobreak"><label>'.
 7268:                        '<input type="radio" name="'.$name.'"'.
 7269:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 7270:                        $choices{$option}.'</label></span><br />';
 7271:             if ($item eq 'email') {
 7272:                 if ($option eq 'custom') {
 7273:                     my $id = 'cancreate_emailrule_'.$type;
 7274:                     my $display = 'none';
 7275:                     if ($checked) {
 7276:                         $display = 'inline';
 7277:                     }
 7278:                     my $numinrow = 2;
 7279:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 7280:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 7281:                                &user_formats_row('email',$settings,$emailrules,
 7282:                                                  $emailruleorder,$numinrow,'',$type);
 7283:                               '</table></fieldset>';
 7284:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 7285:                     my %text = &Apache::lonlocal::texthash (
 7286:                                                              inst    => 'must end:',
 7287:                                                              noninst => 'cannot end:',
 7288:                                                            );
 7289:                     my $value;
 7290:                     if (ref($emaildomain) eq 'HASH') {
 7291:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 7292:                             $value = $emaildomain->{$type}->{$option};
 7293:                         }
 7294:                     }
 7295:                     if ($value eq '') {
 7296:                         $value = '@'.$intdom;
 7297:                     }
 7298:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 7299:                     my $display = 'none';
 7300:                     if ($checked) {
 7301:                         $display = 'inline';
 7302:                     }
 7303:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 7304:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 7305:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 7306:                                '</div>';
 7307:                 }
 7308:             }
 7309:         }
 7310:         $output .= '</td>'."\n";
 7311:     }
 7312:     $output .= "</tr></table></td></tr>\n";
 7313:     return $output;
 7314: }
 7315: 
 7316: sub captcha_choice {
 7317:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 7318:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 7319:         $vertext,$currver); 
 7320:     my %lt = &captcha_phrases();
 7321:     $keyentry = 'hidden';
 7322:     my $colspan=2;
 7323:     if ($context eq 'cancreate') {
 7324:         $rowname = &mt('CAPTCHA validation');
 7325:     } elsif ($context eq 'login') {
 7326:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 7327:     } elsif ($context eq 'passwords') {
 7328:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 7329:         $colspan=1;
 7330:     }
 7331:     if (ref($settings) eq 'HASH') {
 7332:         if ($settings->{'captcha'}) {
 7333:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 7334:         } else {
 7335:             $checked{'original'} = ' checked="checked"';
 7336:         }
 7337:         if ($settings->{'captcha'} eq 'recaptcha') {
 7338:             $pubtext = $lt{'pub'};
 7339:             $privtext = $lt{'priv'};
 7340:             $keyentry = 'text';
 7341:             $vertext = $lt{'ver'};
 7342:             $currver = $settings->{'recaptchaversion'};
 7343:             if ($currver ne '2') {
 7344:                 $currver = 1;
 7345:             }
 7346:         }
 7347:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 7348:             $currpub = $settings->{'recaptchakeys'}{'public'};
 7349:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 7350:         }
 7351:     } else {
 7352:         $checked{'original'} = ' checked="checked"';
 7353:     }
 7354:     my $css_class;
 7355:     if ($itemcount%2) {
 7356:         $css_class = 'LC_odd_row';
 7357:     }
 7358:     if ($customcss) {
 7359:         $css_class .= " $customcss";
 7360:     }
 7361:     $css_class =~ s/^\s+//;
 7362:     if ($css_class) {
 7363:         $css_class = ' class="'.$css_class.'"';
 7364:     }
 7365:     if ($rowstyle) {
 7366:         $css_class .= ' style="'.$rowstyle.'"';
 7367:     }
 7368:     my $output = '<tr'.$css_class.'>'.
 7369:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 7370:                  '<table><tr><td>'."\n";
 7371:     foreach my $option ('original','recaptcha','notused') {
 7372:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 7373:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 7374:                    $lt{$option}.'</label></span>';
 7375:         unless ($option eq 'notused') {
 7376:             $output .= ('&nbsp;'x2)."\n";
 7377:         }
 7378:     }
 7379: #
 7380: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 7381: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 7382: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 7383: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 7384: #
 7385:     $output .= '</td></tr>'."\n".
 7386:                '<tr><td class="LC_zero_height">'."\n".
 7387:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 7388:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 7389:                $currpub.'" size="40" /></span><br />'."\n".
 7390:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 7391:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 7392:                $currpriv.'" size="40" /></span><br />'.
 7393:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 7394:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 7395:                $currver.'" size="3" /></span><br />'.
 7396:                '</td></tr></table>'."\n".
 7397:                '</td></tr>';
 7398:     return $output;
 7399: }
 7400: 
 7401: sub user_formats_row {
 7402:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 7403:     my $output;
 7404:     my %text = (
 7405:                    'username' => 'new usernames',
 7406:                    'id'       => 'IDs',
 7407:                );
 7408:     unless ($type eq 'email') {
 7409:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7410:         $output = '<tr '.$css_class.'>'.
 7411:                   '<td><span class="LC_nobreak">'.
 7412:                   &mt("Format rules to check for $text{$type}: ").
 7413:                   '</td><td class="LC_left_item" colspan="2"><table>';
 7414:     }
 7415:     my $rem;
 7416:     if (ref($ruleorder) eq 'ARRAY') {
 7417:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 7418:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 7419:                 my $rem = $i%($numinrow);
 7420:                 if ($rem == 0) {
 7421:                     if ($i > 0) {
 7422:                         $output .= '</tr>';
 7423:                     }
 7424:                     $output .= '<tr>';
 7425:                 }
 7426:                 my $check = ' ';
 7427:                 if (ref($settings) eq 'HASH') {
 7428:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 7429:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 7430:                             $check = ' checked="checked" ';
 7431:                         }
 7432:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 7433:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 7434:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 7435:                                 $check = ' checked="checked" ';
 7436:                             }
 7437:                         }
 7438:                     }
 7439:                 }
 7440:                 my $name = $type.'_rule';
 7441:                 if ($type eq 'email') {
 7442:                     $name .= '_'.$status;
 7443:                 }
 7444:                 $output .= '<td class="LC_left_item">'.
 7445:                            '<span class="LC_nobreak"><label>'.
 7446:                            '<input type="checkbox" name="'.$name.'" '.
 7447:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 7448:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 7449:             }
 7450:         }
 7451:         $rem = @{$ruleorder}%($numinrow);
 7452:     }
 7453:     my $colsleft;
 7454:     if ($rem) {
 7455:         $colsleft = $numinrow - $rem;
 7456:     }
 7457:     if ($colsleft > 1 ) {
 7458:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7459:                    '&nbsp;</td>';
 7460:     } elsif ($colsleft == 1) {
 7461:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7462:     }
 7463:     $output .= '</tr></table>';
 7464:     unless ($type eq 'email') {
 7465:         $output .= '</td></tr>';
 7466:     }
 7467:     return $output;
 7468: }
 7469: 
 7470: sub usercreation_types {
 7471:     my %lt = &Apache::lonlocal::texthash (
 7472:                     author     => 'When adding a co-author',
 7473:                     course     => 'When adding a user to a course',
 7474:                     requestcrs => 'When requesting a course',
 7475:                     any        => 'Any',
 7476:                     official   => 'Institutional only ',
 7477:                     unofficial => 'Non-institutional only',
 7478:                     none       => 'None',
 7479:     );
 7480:     return %lt;
 7481: }
 7482: 
 7483: sub selfcreation_types {
 7484:     my %lt = &Apache::lonlocal::texthash (
 7485:                     selfcreate => 'User creates own account',
 7486:                     any        => 'Any',
 7487:                     official   => 'Institutional only ',
 7488:                     unofficial => 'Non-institutional only',
 7489:                     email      => 'E-mail address',
 7490:                     login      => 'Institutional Login',
 7491:                     sso        => 'SSO',
 7492:              );
 7493: }
 7494: 
 7495: sub authtype_names {
 7496:     my %lt = &Apache::lonlocal::texthash(
 7497:                       int    => 'Internal',
 7498:                       krb4   => 'Kerberos 4',
 7499:                       krb5   => 'Kerberos 5',
 7500:                       loc    => 'Local',
 7501:                   );
 7502:     return %lt;
 7503: }
 7504: 
 7505: sub context_names {
 7506:     my %context_title = &Apache::lonlocal::texthash(
 7507:        author => 'Creating users when an Author',
 7508:        course => 'Creating users when in a course',
 7509:        domain => 'Creating users when a Domain Coordinator',
 7510:     );
 7511:     return %context_title;
 7512: }
 7513: 
 7514: sub print_usermodification {
 7515:     my ($position,$dom,$settings,$rowtotal) = @_;
 7516:     my $numinrow = 4;
 7517:     my ($context,$datatable,$rowcount);
 7518:     if ($position eq 'top') {
 7519:         $rowcount = 0;
 7520:         $context = 'author'; 
 7521:         foreach my $role ('ca','aa') {
 7522:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 7523:                                                    $numinrow,$rowcount);
 7524:             $$rowtotal ++;
 7525:             $rowcount ++;
 7526:         }
 7527:     } elsif ($position eq 'bottom') {
 7528:         $context = 'course';
 7529:         $rowcount = 0;
 7530:         foreach my $role ('st','ep','ta','in','cr') {
 7531:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 7532:                                                    $numinrow,$rowcount);
 7533:             $$rowtotal ++;
 7534:             $rowcount ++;
 7535:         }
 7536:     }
 7537:     return $datatable;
 7538: }
 7539: 
 7540: sub print_defaults {
 7541:     my ($position,$dom,$settings,$rowtotal) = @_;
 7542:     my $rownum = 0;
 7543:     my ($datatable,$css_class,$titles);
 7544:     unless ($position eq 'bottom') {
 7545:         $titles = &defaults_titles($dom);
 7546:     }
 7547:     if ($position eq 'top') {
 7548:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 7549:                      'datelocale_def','portal_def');
 7550:         my %defaults;
 7551:         if (ref($settings) eq 'HASH') {
 7552:             %defaults = %{$settings};
 7553:         } else {
 7554:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7555:             foreach my $item (@items) {
 7556:                 $defaults{$item} = $domdefaults{$item};
 7557:             }
 7558:         }
 7559:         foreach my $item (@items) {
 7560:             if ($rownum%2) {
 7561:                 $css_class = '';
 7562:             } else {
 7563:                 $css_class = ' class="LC_odd_row" ';
 7564:             }
 7565:             $datatable .= '<tr'.$css_class.'>'.
 7566:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 7567:                           '</span></td><td class="LC_right_item" colspan="3">';
 7568:             if ($item eq 'auth_def') {
 7569:                 my @authtypes = ('internal','krb4','krb5','localauth');
 7570:                 my %shortauth = (
 7571:                                  internal => 'int',
 7572:                                  krb4 => 'krb4',
 7573:                                  krb5 => 'krb5',
 7574:                                  localauth  => 'loc'
 7575:                                 );
 7576:                 my %authnames = &authtype_names();
 7577:                 foreach my $auth (@authtypes) {
 7578:                     my $checked = ' ';
 7579:                     if ($defaults{$item} eq $auth) {
 7580:                         $checked = ' checked="checked" ';
 7581:                     }
 7582:                     $datatable .= '<label><input type="radio" name="'.$item.
 7583:                                   '" value="'.$auth.'"'.$checked.'/>'.
 7584:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 7585:                 }
 7586:             } elsif ($item eq 'timezone_def') {
 7587:                 my $includeempty = 1;
 7588:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 7589:             } elsif ($item eq 'datelocale_def') {
 7590:                 my $includeempty = 1;
 7591:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 7592:             } elsif ($item eq 'lang_def') {
 7593:                 my $includeempty = 1;
 7594:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 7595:             } else {
 7596:                 my $size;
 7597:                 if ($item eq 'portal_def') {
 7598:                     $size = ' size="25"';
 7599:                 }
 7600:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7601:                               $defaults{$item}.'"'.$size.' />';
 7602:             }
 7603:             $datatable .= '</td></tr>';
 7604:             $rownum ++;
 7605:         }
 7606:     } else {
 7607:         my %defaults;
 7608:         if (ref($settings) eq 'HASH') {
 7609:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7610:                 my $maxnum = @{$settings->{'inststatusorder'}};
 7611:                 for (my $i=0; $i<$maxnum; $i++) {
 7612:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 7613:                     my $item = $settings->{'inststatusorder'}->[$i];
 7614:                     my $title = $settings->{'inststatustypes'}->{$item};
 7615:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 7616:                     $datatable .= '<tr'.$css_class.'>'.
 7617:                                   '<td><span class="LC_nobreak">'.
 7618:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 7619:                     for (my $k=0; $k<=$maxnum; $k++) {
 7620:                         my $vpos = $k+1;
 7621:                         my $selstr;
 7622:                         if ($k == $i) {
 7623:                             $selstr = ' selected="selected" ';
 7624:                         }
 7625:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7626:                     }
 7627:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 7628:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 7629:                                   &mt('delete').'</span></td>'.
 7630:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed').':'.
 7631:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 7632:                                   '</span></td></tr>';
 7633:                 }
 7634:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 7635:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 7636:                 $datatable .= '<tr '.$css_class.'>'.
 7637:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 7638:                 for (my $k=0; $k<=$maxnum; $k++) {
 7639:                     my $vpos = $k+1;
 7640:                     my $selstr;
 7641:                     if ($k == $maxnum) {
 7642:                         $selstr = ' selected="selected" ';
 7643:                     }
 7644:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7645:                 }
 7646:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 7647:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 7648:                               '&nbsp;'.&mt('(new)').
 7649:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 7650:                               &mt('Name displayed').':'.
 7651:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 7652:                               '</tr>'."\n";
 7653:                 $rownum ++;
 7654:             }
 7655:         }
 7656:     }
 7657:     $$rowtotal += $rownum;
 7658:     return $datatable;
 7659: }
 7660: 
 7661: sub get_languages_hash {
 7662:     my %langchoices;
 7663:     foreach my $id (&Apache::loncommon::languageids()) {
 7664:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 7665:         if ($code ne '') {
 7666:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 7667:         }
 7668:     }
 7669:     return %langchoices;
 7670: }
 7671: 
 7672: sub defaults_titles {
 7673:     my ($dom) = @_;
 7674:     my %titles = &Apache::lonlocal::texthash (
 7675:                    'auth_def'      => 'Default authentication type',
 7676:                    'auth_arg_def'  => 'Default authentication argument',
 7677:                    'lang_def'      => 'Default language',
 7678:                    'timezone_def'  => 'Default timezone',
 7679:                    'datelocale_def' => 'Default locale for dates',
 7680:                    'portal_def'     => 'Portal/Default URL',
 7681:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 7682:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 7683:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 7684:                  );
 7685:     if ($dom) {
 7686:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 7687:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 7688:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 7689:         $protocol = 'http' if ($protocol ne 'https');
 7690:         if ($uint_dom) {
 7691:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 7692:                                          $uint_dom);
 7693:         }
 7694:     }
 7695:     return (\%titles);
 7696: }
 7697: 
 7698: sub print_scantron {
 7699:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 7700:     if ($position eq 'top') {
 7701:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 7702:     } else {
 7703:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 7704:     }
 7705: }
 7706: 
 7707: sub scantron_javascript {
 7708:     return <<"ENDSCRIPT";
 7709: 
 7710: <script type="text/javascript">
 7711: // <![CDATA[
 7712: 
 7713: function toggleScantron(form) {
 7714:     var csvfieldset = new Array();
 7715:     if (document.getElementById('scantroncsv_cols')) {
 7716:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 7717:     }
 7718:     if (document.getElementById('scantroncsv_options')) {
 7719:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 7720:     }
 7721:     if (csvfieldset.length) {
 7722:         if (document.getElementById('scantronconfcsv')) {
 7723:             var scantroncsv = document.getElementById('scantronconfcsv');
 7724:             if (scantroncsv.checked) {
 7725:                 for (var i=0; i<csvfieldset.length; i++) {
 7726:                     csvfieldset[i].style.display = 'block';
 7727:                 }
 7728:             } else {
 7729:                 for (var i=0; i<csvfieldset.length; i++) {
 7730:                     csvfieldset[i].style.display = 'none';
 7731:                 }
 7732:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 7733:                 if (csvselects.length) {
 7734:                     for (var j=0; j<csvselects.length; j++) {
 7735:                         csvselects[j].selectedIndex = 0;
 7736:                     }
 7737:                 }
 7738:             }
 7739:         }
 7740:     }
 7741:     return;
 7742: }
 7743: // ]]>
 7744: </script>
 7745: 
 7746: ENDSCRIPT
 7747: 
 7748: }
 7749: 
 7750: sub print_scantronformat {
 7751:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 7752:     my $itemcount = 1;
 7753:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 7754:         %confhash);
 7755:     my $switchserver = &check_switchserver($dom,$confname);
 7756:     my %lt = &Apache::lonlocal::texthash (
 7757:                 default => 'Default bubblesheet format file error',
 7758:                 custom  => 'Custom bubblesheet format file error',
 7759:              );
 7760:     my %scantronfiles = (
 7761:         default => 'default.tab',
 7762:         custom => 'custom.tab',
 7763:     );
 7764:     foreach my $key (keys(%scantronfiles)) {
 7765:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 7766:                               .$scantronfiles{$key};
 7767:     }
 7768:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 7769:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 7770:         if (!$switchserver) {
 7771:             my $servadm = $r->dir_config('lonAdmEMail');
 7772:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 7773:             if ($configuserok eq 'ok') {
 7774:                 if ($author_ok eq 'ok') {
 7775:                     my %legacyfile = (
 7776:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 7777:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 7778:                     );
 7779:                     my %md5chk;
 7780:                     foreach my $type (keys(%legacyfile)) {
 7781:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 7782:                         chomp($md5chk{$type});
 7783:                     }
 7784:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 7785:                         foreach my $type (keys(%legacyfile)) {
 7786:                             ($scantronurls{$type},my $error) =
 7787:                                 &legacy_scantronformat($r,$dom,$confname,
 7788:                                                  $type,$legacyfile{$type},
 7789:                                                  $scantronurls{$type},
 7790:                                                  $scantronfiles{$type});
 7791:                             if ($error ne '') {
 7792:                                 $error{$type} = $error;
 7793:                             }
 7794:                         }
 7795:                         if (keys(%error) == 0) {
 7796:                             $is_custom = 1;
 7797:                             $confhash{'scantron'}{'scantronformat'} =
 7798:                                 $scantronurls{'custom'};
 7799:                             my $putresult =
 7800:                                 &Apache::lonnet::put_dom('configuration',
 7801:                                                          \%confhash,$dom);
 7802:                             if ($putresult ne 'ok') {
 7803:                                 $error{'custom'} =
 7804:                                     '<span class="LC_error">'.
 7805:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 7806:                             }
 7807:                         }
 7808:                     } else {
 7809:                         ($scantronurls{'default'},my $error) =
 7810:                             &legacy_scantronformat($r,$dom,$confname,
 7811:                                           'default',$legacyfile{'default'},
 7812:                                           $scantronurls{'default'},
 7813:                                           $scantronfiles{'default'});
 7814:                         if ($error eq '') {
 7815:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 7816:                             my $putresult =
 7817:                                 &Apache::lonnet::put_dom('configuration',
 7818:                                                          \%confhash,$dom);
 7819:                             if ($putresult ne 'ok') {
 7820:                                 $error{'default'} =
 7821:                                     '<span class="LC_error">'.
 7822:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 7823:                             }
 7824:                         } else {
 7825:                             $error{'default'} = $error;
 7826:                         }
 7827:                     }
 7828:                 }
 7829:             }
 7830:         } else {
 7831:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 7832:         }
 7833:     }
 7834:     if (ref($settings) eq 'HASH') {
 7835:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 7836:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 7837:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 7838:                 $scantronurl = '';
 7839:             } else {
 7840:                 $scantronurl = $settings->{'scantronformat'};
 7841:             }
 7842:             $is_custom = 1;
 7843:         } else {
 7844:             $scantronurl = $scantronurls{'default'};
 7845:         }
 7846:     } else {
 7847:         if ($is_custom) {
 7848:             $scantronurl = $scantronurls{'custom'};
 7849:         } else {
 7850:             $scantronurl = $scantronurls{'default'};
 7851:         }
 7852:     }
 7853:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7854:     $datatable .= '<tr'.$css_class.'>';
 7855:     if (!$is_custom) {
 7856:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 7857:                       '<span class="LC_nobreak">';
 7858:         if ($scantronurl) {
 7859:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 7860:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 7861:         } else {
 7862:             $datatable = &mt('File unavailable for display');
 7863:         }
 7864:         $datatable .= '</span></td>';
 7865:         if (keys(%error) == 0) { 
 7866:             $datatable .= '<td valign="bottom">';
 7867:             if (!$switchserver) {
 7868:                 $datatable .= &mt('Upload:').'<br />';
 7869:             }
 7870:         } else {
 7871:             my $errorstr;
 7872:             foreach my $key (sort(keys(%error))) {
 7873:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 7874:             }
 7875:             $datatable .= '<td>'.$errorstr;
 7876:         }
 7877:     } else {
 7878:         if (keys(%error) > 0) {
 7879:             my $errorstr;
 7880:             foreach my $key (sort(keys(%error))) {
 7881:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 7882:             } 
 7883:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 7884:         } elsif ($scantronurl) {
 7885:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 7886:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7887:             $datatable .= '<td><span class="LC_nobreak">'.
 7888:                           $link.
 7889:                           '<label><input type="checkbox" name="scantronformat_del"'.
 7890:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 7891:                           '<td><span class="LC_nobreak">&nbsp;'.
 7892:                           &mt('Replace:').'</span><br />';
 7893:         }
 7894:     }
 7895:     if (keys(%error) == 0) {
 7896:         if ($switchserver) {
 7897:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 7898:         } else {
 7899:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 7900:                          '<input type="file" name="scantronformat" /></span>';
 7901:         }
 7902:     }
 7903:     $datatable .= '</td></tr>';
 7904:     $$rowtotal ++;
 7905:     return $datatable;
 7906: }
 7907: 
 7908: sub legacy_scantronformat {
 7909:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 7910:     my ($url,$error);
 7911:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 7912:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 7913:         (my $result,$url) =
 7914:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 7915:                          '','',$newfile);
 7916:         if ($result ne 'ok') {
 7917:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 7918:         }
 7919:     }
 7920:     return ($url,$error);
 7921: }
 7922: 
 7923: sub print_scantronconfig {
 7924:     my ($dom,$settings,$rowtotal) = @_;
 7925:     my $itemcount = 2;
 7926:     my $is_checked = ' checked="checked"';
 7927:     my %optionson = (
 7928:                      hdr => ' checked="checked"',
 7929:                      pad => ' checked="checked"',
 7930:                      rem => ' checked="checked"',
 7931:                     );
 7932:     my %optionsoff = (
 7933:                       hdr => '',
 7934:                       pad => '',
 7935:                       rem => '',
 7936:                      );
 7937:     my $currcsvsty = 'none';
 7938:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 7939:     my @fields = &scantroncsv_fields();
 7940:     my %titles = &scantronconfig_titles();
 7941:     if (ref($settings) eq 'HASH') {
 7942:         if (ref($settings->{config}) eq 'HASH') {
 7943:             if ($settings->{config}->{dat}) {
 7944:                 $checked{'dat'} = $is_checked;
 7945:             }
 7946:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 7947:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 7948:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 7949:                     if (keys(%csvfields) > 0) {
 7950:                         $checked{'csv'} = $is_checked;
 7951:                         $currcsvsty = 'block';
 7952:                     }
 7953:                 }
 7954:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 7955:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 7956:                     foreach my $option (keys(%optionson)) {
 7957:                         unless ($csvoptions{$option}) {
 7958:                             $optionsoff{$option} = $optionson{$option};
 7959:                             $optionson{$option} = '';
 7960:                         }
 7961:                     }
 7962:                 }
 7963:             }
 7964:         } else {
 7965:             $checked{'dat'} = $is_checked;
 7966:         }
 7967:     } else {
 7968:         $checked{'dat'} = $is_checked;
 7969:     }
 7970:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 7971:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 7972:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 7973:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 7974:     foreach my $item ('dat','csv') {
 7975:         my $id;
 7976:         if ($item eq 'csv') {
 7977:             $id = 'id="scantronconfcsv" ';
 7978:         }
 7979:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 7980:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 7981:         if ($item eq 'csv') {
 7982:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 7983:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 7984:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 7985:             foreach my $col (@fields) {
 7986:                 my $selnone;
 7987:                 if ($csvfields{$col} eq '') {
 7988:                     $selnone = ' selected="selected"';
 7989:                 }
 7990:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 7991:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 7992:                               '<option value=""'.$selnone.'></option>';
 7993:                 for (my $i=0; $i<20; $i++) {
 7994:                     my $shown = $i+1;
 7995:                     my $sel;
 7996:                     unless ($selnone) {
 7997:                         if (exists($csvfields{$col})) {
 7998:                             if ($csvfields{$col} == $i) {
 7999:                                 $sel = ' selected="selected"';
 8000:                             }
 8001:                         }
 8002:                     }
 8003:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 8004:                 }
 8005:                 $datatable .= '</select></td></tr>';
 8006:            }
 8007:            $datatable .= '</table></fieldset>'.
 8008:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 8009:                          '<legend>'.&mt('CSV Options').'</legend>';
 8010:            foreach my $option ('hdr','pad','rem') {
 8011:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 8012:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 8013:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 8014:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 8015:            }
 8016:            $datatable .= '</fieldset>';
 8017:            $itemcount ++;
 8018:         }
 8019:     }
 8020:     $datatable .= '</td></tr>';
 8021:     $$rowtotal ++;
 8022:     return $datatable;
 8023: }
 8024: 
 8025: sub scantronconfig_titles {
 8026:     return &Apache::lonlocal::texthash(
 8027:                                           dat => 'Standard format (.dat)',
 8028:                                           csv => 'Comma separated values (.csv)',
 8029:                                           hdr => 'Remove first line in file (contains column titles)',
 8030:                                           pad => 'Prepend 0s to PaperID',
 8031:                                           rem => 'Remove leading spaces (except Question Response columns)',
 8032:                                           CODE => 'CODE',
 8033:                                           ID   => 'Student ID',
 8034:                                           PaperID => 'Paper ID',
 8035:                                           FirstName => 'First Name',
 8036:                                           LastName => 'Last Name',
 8037:                                           FirstQuestion => 'First Question Response',
 8038:                                           Section => 'Section',
 8039:     );
 8040: }
 8041: 
 8042: sub scantroncsv_fields {
 8043:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 8044: }
 8045: 
 8046: sub print_coursecategories {
 8047:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 8048:     my $datatable;
 8049:     if ($position eq 'top') {
 8050:         my (%checked);
 8051:         my @catitems = ('unauth','auth');
 8052:         my @cattypes = ('std','domonly','codesrch','none');
 8053:         $checked{'unauth'} = 'std';
 8054:         $checked{'auth'} = 'std';
 8055:         if (ref($settings) eq 'HASH') {
 8056:             foreach my $type (@cattypes) {
 8057:                 if ($type eq $settings->{'unauth'}) {
 8058:                     $checked{'unauth'} = $type;
 8059:                 }
 8060:                 if ($type eq $settings->{'auth'}) {
 8061:                     $checked{'auth'} = $type;
 8062:                 }
 8063:             }
 8064:         }
 8065:         my %lt = &Apache::lonlocal::texthash (
 8066:                                                unauth   => 'Catalog type for unauthenticated users',
 8067:                                                auth     => 'Catalog type for authenticated users',
 8068:                                                none     => 'No catalog',
 8069:                                                std      => 'Standard catalog',
 8070:                                                domonly  => 'Domain-only catalog',
 8071:                                                codesrch => "Code search form",
 8072:                                              );
 8073:        my $itemcount = 0;
 8074:        foreach my $item (@catitems) {
 8075:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 8076:            $datatable .= '<tr '.$css_class.'>'.
 8077:                          '<td>'.$lt{$item}.'</td>'.
 8078:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 8079:            foreach my $type (@cattypes) {
 8080:                my $ischecked;
 8081:                if ($checked{$item} eq $type) {
 8082:                    $ischecked=' checked="checked"';
 8083:                }
 8084:                $datatable .= '<label>'.
 8085:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 8086:                              ' />'.$lt{$type}.'</label>&nbsp;';
 8087:            }
 8088:            $datatable .= '</span></td></tr>';
 8089:            $itemcount ++;
 8090:         }
 8091:         $$rowtotal += $itemcount;
 8092:     } elsif ($position eq 'middle') {
 8093:         my $toggle_cats_crs = ' ';
 8094:         my $toggle_cats_dom = ' checked="checked" ';
 8095:         my $can_cat_crs = ' ';
 8096:         my $can_cat_dom = ' checked="checked" ';
 8097:         my $toggle_catscomm_comm = ' ';
 8098:         my $toggle_catscomm_dom = ' checked="checked" ';
 8099:         my $can_catcomm_comm = ' ';
 8100:         my $can_catcomm_dom = ' checked="checked" ';
 8101: 
 8102:         if (ref($settings) eq 'HASH') {
 8103:             if ($settings->{'togglecats'} eq 'crs') {
 8104:                 $toggle_cats_crs = $toggle_cats_dom;
 8105:                 $toggle_cats_dom = ' ';
 8106:             }
 8107:             if ($settings->{'categorize'} eq 'crs') {
 8108:                 $can_cat_crs = $can_cat_dom;
 8109:                 $can_cat_dom = ' ';
 8110:             }
 8111:             if ($settings->{'togglecatscomm'} eq 'comm') {
 8112:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 8113:                 $toggle_catscomm_dom = ' ';
 8114:             }
 8115:             if ($settings->{'categorizecomm'} eq 'comm') {
 8116:                 $can_catcomm_comm = $can_catcomm_dom;
 8117:                 $can_catcomm_dom = ' ';
 8118:             }
 8119:         }
 8120:         my %title = &Apache::lonlocal::texthash (
 8121:                      togglecats     => 'Show/Hide a course in catalog',
 8122:                      togglecatscomm => 'Show/Hide a community in catalog',
 8123:                      categorize     => 'Assign a category to a course',
 8124:                      categorizecomm => 'Assign a category to a community',
 8125:                     );
 8126:         my %level = &Apache::lonlocal::texthash (
 8127:                      dom  => 'Set in Domain',
 8128:                      crs  => 'Set in Course',
 8129:                      comm => 'Set in Community',
 8130:                     );
 8131:         $datatable = '<tr class="LC_odd_row">'.
 8132:                   '<td>'.$title{'togglecats'}.'</td>'.
 8133:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8134:                   '<input type="radio" name="togglecats"'.
 8135:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8136:                   '<label><input type="radio" name="togglecats"'.
 8137:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 8138:                   '</tr><tr>'.
 8139:                   '<td>'.$title{'categorize'}.'</td>'.
 8140:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8141:                   '<label><input type="radio" name="categorize"'.
 8142:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8143:                   '<label><input type="radio" name="categorize"'.
 8144:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 8145:                   '</tr><tr class="LC_odd_row">'.
 8146:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 8147:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8148:                   '<input type="radio" name="togglecatscomm"'.
 8149:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8150:                   '<label><input type="radio" name="togglecatscomm"'.
 8151:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 8152:                   '</tr><tr>'.
 8153:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 8154:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 8155:                   '<label><input type="radio" name="categorizecomm"'.
 8156:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8157:                   '<label><input type="radio" name="categorizecomm"'.
 8158:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 8159:                   '</tr>';
 8160:         $$rowtotal += 4;
 8161:     } else {
 8162:         my $css_class;
 8163:         my $itemcount = 1;
 8164:         my $cathash; 
 8165:         if (ref($settings) eq 'HASH') {
 8166:             $cathash = $settings->{'cats'};
 8167:         }
 8168:         if (ref($cathash) eq 'HASH') {
 8169:             my (@cats,@trails,%allitems,%idx,@jsarray);
 8170:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 8171:                                                    \%allitems,\%idx,\@jsarray);
 8172:             my $maxdepth = scalar(@cats);
 8173:             my $colattrib = '';
 8174:             if ($maxdepth > 2) {
 8175:                 $colattrib = ' colspan="2" ';
 8176:             }
 8177:             my @path;
 8178:             if (@cats > 0) {
 8179:                 if (ref($cats[0]) eq 'ARRAY') {
 8180:                     my $numtop = @{$cats[0]};
 8181:                     my $maxnum = $numtop;
 8182:                     my %default_names = (
 8183:                           instcode    => &mt('Official courses'),
 8184:                           communities => &mt('Communities'),
 8185:                     );
 8186: 
 8187:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 8188:                         ($cathash->{'instcode::0'} eq '') ||
 8189:                         (!grep(/^communities$/,@{$cats[0]})) || 
 8190:                         ($cathash->{'communities::0'} eq '')) {
 8191:                         $maxnum ++;
 8192:                     }
 8193:                     my $lastidx;
 8194:                     for (my $i=0; $i<$numtop; $i++) {
 8195:                         my $parent = $cats[0][$i];
 8196:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8197:                         my $item = &escape($parent).'::0';
 8198:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 8199:                         $lastidx = $idx{$item};
 8200:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8201:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 8202:                         for (my $k=0; $k<=$maxnum; $k++) {
 8203:                             my $vpos = $k+1;
 8204:                             my $selstr;
 8205:                             if ($k == $i) {
 8206:                                 $selstr = ' selected="selected" ';
 8207:                             }
 8208:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8209:                         }
 8210:                         $datatable .= '</select></span></td><td>';
 8211:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 8212:                             $datatable .=  '<span class="LC_nobreak">'
 8213:                                            .$default_names{$parent}.'</span>';
 8214:                             if ($parent eq 'instcode') {
 8215:                                 $datatable .= '<br /><span class="LC_nobreak">('
 8216:                                               .&mt('with institutional codes')
 8217:                                               .')</span></td><td'.$colattrib.'>';
 8218:                             } else {
 8219:                                 $datatable .= '<table><tr><td>';
 8220:                             }
 8221:                             $datatable .= '<span class="LC_nobreak">'
 8222:                                           .'<label><input type="radio" name="'
 8223:                                           .$parent.'" value="1" checked="checked" />'
 8224:                                           .&mt('Display').'</label>';
 8225:                             if ($parent eq 'instcode') {
 8226:                                 $datatable .= '&nbsp;';
 8227:                             } else {
 8228:                                 $datatable .= '</span></td></tr><tr><td>'
 8229:                                               .'<span class="LC_nobreak">';
 8230:                             }
 8231:                             $datatable .= '<label><input type="radio" name="'
 8232:                                           .$parent.'" value="0" />'
 8233:                                           .&mt('Do not display').'</label></span>';
 8234:                             if ($parent eq 'communities') {
 8235:                                 $datatable .= '</td></tr></table>';
 8236:                             }
 8237:                             $datatable .= '</td>';
 8238:                         } else {
 8239:                             $datatable .= $parent
 8240:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 8241:                                           .'<input type="checkbox" name="deletecategory" '
 8242:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 8243:                         }
 8244:                         my $depth = 1;
 8245:                         push(@path,$parent);
 8246:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 8247:                         pop(@path);
 8248:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 8249:                         $itemcount ++;
 8250:                     }
 8251:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8252:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 8253:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 8254:                     for (my $k=0; $k<=$maxnum; $k++) {
 8255:                         my $vpos = $k+1;
 8256:                         my $selstr;
 8257:                         if ($k == $numtop) {
 8258:                             $selstr = ' selected="selected" ';
 8259:                         }
 8260:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8261:                     }
 8262:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 8263:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 8264:                                   .'</tr>'."\n";
 8265:                     $itemcount ++;
 8266:                     foreach my $default ('instcode','communities') {
 8267:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 8268:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8269:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 8270:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 8271:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 8272:                             for (my $k=0; $k<=$maxnum; $k++) {
 8273:                                 my $vpos = $k+1;
 8274:                                 my $selstr;
 8275:                                 if ($k == $maxnum) {
 8276:                                     $selstr = ' selected="selected" ';
 8277:                                 }
 8278:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8279:                             }
 8280:                             $datatable .= '</select></span></td>'.
 8281:                                           '<td><span class="LC_nobreak">'.
 8282:                                           $default_names{$default}.'</span>';
 8283:                             if ($default eq 'instcode') {
 8284:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 8285:                                               .&mt('with institutional codes').')</span>';
 8286:                             }
 8287:                             $datatable .= '</td>'
 8288:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 8289:                                           .&mt('Display').'</label>&nbsp;'
 8290:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 8291:                                           .&mt('Do not display').'</label></span></td></tr>';
 8292:                         }
 8293:                     }
 8294:                 }
 8295:             } else {
 8296:                 $datatable .= &initialize_categories($itemcount);
 8297:             }
 8298:         } else {
 8299:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 8300:                           .&initialize_categories($itemcount);
 8301:         }
 8302:         $$rowtotal += $itemcount;
 8303:     }
 8304:     return $datatable;
 8305: }
 8306: 
 8307: sub print_serverstatuses {
 8308:     my ($dom,$settings,$rowtotal) = @_;
 8309:     my $datatable;
 8310:     my @pages = &serverstatus_pages();
 8311:     my (%namedaccess,%machineaccess);
 8312:     foreach my $type (@pages) {
 8313:         $namedaccess{$type} = '';
 8314:         $machineaccess{$type}= '';
 8315:     }
 8316:     if (ref($settings) eq 'HASH') {
 8317:         foreach my $type (@pages) {
 8318:             if (exists($settings->{$type})) {
 8319:                 if (ref($settings->{$type}) eq 'HASH') {
 8320:                     foreach my $key (keys(%{$settings->{$type}})) {
 8321:                         if ($key eq 'namedusers') {
 8322:                             $namedaccess{$type} = $settings->{$type}->{$key};
 8323:                         } elsif ($key eq 'machines') {
 8324:                             $machineaccess{$type} = $settings->{$type}->{$key};
 8325:                         }
 8326:                     }
 8327:                 }
 8328:             }
 8329:         }
 8330:     }
 8331:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 8332:     my $rownum = 0;
 8333:     my $css_class;
 8334:     foreach my $type (@pages) {
 8335:         $rownum ++;
 8336:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 8337:         $datatable .= '<tr'.$css_class.'>'.
 8338:                       '<td><span class="LC_nobreak">'.
 8339:                       $titles->{$type}.'</span></td>'.
 8340:                       '<td class="LC_left_item">'.
 8341:                       '<input type="text" name="'.$type.'_namedusers" '.
 8342:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 8343:                       '<td class="LC_right_item">'.
 8344:                       '<span class="LC_nobreak">'.
 8345:                       '<input type="text" name="'.$type.'_machines" '.
 8346:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 8347:                       '</span></td></tr>'."\n";
 8348:     }
 8349:     $$rowtotal += $rownum;
 8350:     return $datatable;
 8351: }
 8352: 
 8353: sub serverstatus_pages {
 8354:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 8355:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 8356:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 8357:             'uniquecodes','diskusage','coursecatalog');
 8358: }
 8359: 
 8360: sub defaults_javascript {
 8361:     my ($settings) = @_;
 8362:     return unless (ref($settings) eq 'HASH');
 8363:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 8364:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 8365:         if ($maxnum eq '') {
 8366:             $maxnum = 0;
 8367:         }
 8368:         $maxnum ++;
 8369:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 8370:         return <<"ENDSCRIPT";
 8371: <script type="text/javascript">
 8372: // <![CDATA[
 8373: function reorderTypes(form,caller) {
 8374:     var changedVal;
 8375: $jstext 
 8376:     var newpos = 'addinststatus_pos';
 8377:     var current = new Array;
 8378:     var maxh = $maxnum;
 8379:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 8380:     var oldVal;
 8381:     if (caller == newpos) {
 8382:         changedVal = newitemVal;
 8383:     } else {
 8384:         var curritem = 'inststatus_pos_'+caller;
 8385:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 8386:         current[newitemVal] = newpos;
 8387:     }
 8388:     for (var i=0; i<inststatuses.length; i++) {
 8389:         if (inststatuses[i] != caller) {
 8390:             var elementName = 'inststatus_pos_'+inststatuses[i];
 8391:             if (form.elements[elementName]) {
 8392:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 8393:                 current[currVal] = elementName;
 8394:             }
 8395:         }
 8396:     }
 8397:     for (var j=0; j<maxh; j++) {
 8398:         if (current[j] == undefined) {
 8399:             oldVal = j;
 8400:         }
 8401:     }
 8402:     if (oldVal < changedVal) {
 8403:         for (var k=oldVal+1; k<=changedVal ; k++) {
 8404:            var elementName = current[k];
 8405:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 8406:         }
 8407:     } else {
 8408:         for (var k=changedVal; k<oldVal; k++) {
 8409:             var elementName = current[k];
 8410:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 8411:         }
 8412:     }
 8413:     return;
 8414: }
 8415: 
 8416: // ]]>
 8417: </script>
 8418: 
 8419: ENDSCRIPT
 8420:     }
 8421: }
 8422: 
 8423: sub passwords_javascript {
 8424:     my %intalert = &Apache::lonlocal::texthash (
 8425:         authcheck => 'Warning: disallowing login for an authenticated user if the stored cost is less than the default will require a password reset by/for the user.',
 8426:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
 8427:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
 8428:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
 8429:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
 8430:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
 8431:     );
 8432:     &js_escape(\%intalert);
 8433:     my $defmin = $Apache::lonnet::passwdmin;
 8434:     my $intauthjs = <<"ENDSCRIPT";
 8435: 
 8436: function warnIntAuth(field) {
 8437:     if (field.name == 'intauth_check') {
 8438:         if (field.value == '2') {
 8439:             alert('$intalert{authcheck}');
 8440:         }
 8441:     }
 8442:     if (field.name == 'intauth_cost') {
 8443:         field.value.replace(/\s/g,'');
 8444:         if (field.value != '') {
 8445:             var regexdigit=/^\\d+\$/;
 8446:             if (!regexdigit.test(field.value)) {
 8447:                 alert('$intalert{authcost}');
 8448:             }
 8449:         }
 8450:     }
 8451:     return;
 8452: }
 8453: 
 8454: function warnIntPass(field) {
 8455:     field.value.replace(/^\s+/,'');
 8456:     field.value.replace(/\s+\$/,'');
 8457:     var regexdigit=/^\\d+\$/;
 8458:     if (field.name == 'passwords_min') {
 8459:         if (field.value == '') {
 8460:             alert('$intalert{passmin}');
 8461:             field.value = '$defmin';
 8462:         } else {
 8463:             if (!regexdigit.test(field.value)) {
 8464:                 alert('$intalert{passmin}');
 8465:                 field.value = '$defmin';
 8466:             }
 8467:             var minval = parseInt(field.value,10);
 8468:             if (minval < $defmin) {
 8469:                 alert('$intalert{passmin}');
 8470:                 field.value = '$defmin';
 8471:             }
 8472:         }
 8473:     } else {
 8474:         if (field.value == '0') {
 8475:             field.value = '';
 8476:         }
 8477:         if (field.value != '') {
 8478:             if (field.name == 'passwords_expire') {
 8479:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/;
 8480:                 if (!regexpposnum.test(field.value)) {
 8481:                     alert('$intalert{passexp}');
 8482:                     field.value = '';
 8483:                 } else {
 8484:                     var expval = parseFloat(field.value);
 8485:                     if (expval == 0) {
 8486:                         alert('$intalert{passexp}');
 8487:                         field.value = '';
 8488:                     }
 8489:                 }
 8490:             } else {
 8491:                 if (!regexdigit.test(field.value)) {
 8492:                     if (field.name == 'passwords_max') {
 8493:                         alert('$intalert{passmax}');
 8494:                     } else {
 8495:                         if (field.name == 'passwords_numsaved') {
 8496:                             alert('$intalert{passnum}');
 8497:                         }
 8498:                     }
 8499:                     field.value = '';
 8500:                 }
 8501:             }
 8502:         }
 8503:     }
 8504:     return;
 8505: }
 8506: 
 8507: ENDSCRIPT
 8508:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
 8509: }
 8510: 
 8511: sub coursecategories_javascript {
 8512:     my ($settings) = @_;
 8513:     my ($output,$jstext,$cathash);
 8514:     if (ref($settings) eq 'HASH') {
 8515:         $cathash = $settings->{'cats'};
 8516:     }
 8517:     if (ref($cathash) eq 'HASH') {
 8518:         my (@cats,@jsarray,%idx);
 8519:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 8520:         if (@jsarray > 0) {
 8521:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 8522:             for (my $i=0; $i<@jsarray; $i++) {
 8523:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 8524:                     my $catstr = join('","',@{$jsarray[$i]});
 8525:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 8526:                 }
 8527:             }
 8528:         }
 8529:     } else {
 8530:         $jstext  = '    var categories = Array(1);'."\n".
 8531:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 8532:     }
 8533:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 8534:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 8535:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 8536:     &js_escape(\$instcode_reserved);
 8537:     &js_escape(\$communities_reserved);
 8538:     &js_escape(\$choose_again);
 8539:     $output = <<"ENDSCRIPT";
 8540: <script type="text/javascript">
 8541: // <![CDATA[
 8542: function reorderCats(form,parent,item,idx) {
 8543:     var changedVal;
 8544: $jstext
 8545:     var newpos = 'addcategory_pos';
 8546:     if (parent == '') {
 8547:         var has_instcode = 0;
 8548:         var maxtop = categories[idx].length;
 8549:         for (var j=0; j<maxtop; j++) {
 8550:             if (categories[idx][j] == 'instcode::0') {
 8551:                 has_instcode == 1;
 8552:             }
 8553:         }
 8554:         if (has_instcode == 0) {
 8555:             categories[idx][maxtop] = 'instcode_pos';
 8556:         }
 8557:     } else {
 8558:         newpos += '_'+parent;
 8559:     }
 8560:     var maxh = 1 + categories[idx].length;
 8561:     var current = new Array;
 8562:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 8563:     if (item == newpos) {
 8564:         changedVal = newitemVal;
 8565:     } else {
 8566:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 8567:         current[newitemVal] = newpos;
 8568:     }
 8569:     for (var i=0; i<categories[idx].length; i++) {
 8570:         var elementName = categories[idx][i];
 8571:         if (elementName != item) {
 8572:             if (form.elements[elementName]) {
 8573:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 8574:                 current[currVal] = elementName;
 8575:             }
 8576:         }
 8577:     }
 8578:     var oldVal;
 8579:     for (var j=0; j<maxh; j++) {
 8580:         if (current[j] == undefined) {
 8581:             oldVal = j;
 8582:         }
 8583:     }
 8584:     if (oldVal < changedVal) {
 8585:         for (var k=oldVal+1; k<=changedVal ; k++) {
 8586:            var elementName = current[k];
 8587:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 8588:         }
 8589:     } else {
 8590:         for (var k=changedVal; k<oldVal; k++) {
 8591:             var elementName = current[k];
 8592:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 8593:         }
 8594:     }
 8595:     return;
 8596: }
 8597: 
 8598: function categoryCheck(form) {
 8599:     if (form.elements['addcategory_name'].value == 'instcode') {
 8600:         alert('$instcode_reserved\\n$choose_again');
 8601:         return false;
 8602:     }
 8603:     if (form.elements['addcategory_name'].value == 'communities') {
 8604:         alert('$communities_reserved\\n$choose_again');
 8605:         return false;
 8606:     }
 8607:     return true;
 8608: }
 8609: 
 8610: // ]]>
 8611: </script>
 8612: 
 8613: ENDSCRIPT
 8614:     return $output;
 8615: }
 8616: 
 8617: sub initialize_categories {
 8618:     my ($itemcount) = @_;
 8619:     my ($datatable,$css_class,$chgstr);
 8620:     my %default_names = &Apache::lonlocal::texthash (
 8621:                       instcode    => 'Official courses (with institutional codes)',
 8622:                       communities => 'Communities',
 8623:                         );
 8624:     my $select0 = ' selected="selected"';
 8625:     my $select1 = '';
 8626:     foreach my $default ('instcode','communities') {
 8627:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8628:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
 8629:         if ($default eq 'communities') {
 8630:             $select1 = $select0;
 8631:             $select0 = '';
 8632:         }
 8633:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8634:                      .'<select name="'.$default.'_pos">'
 8635:                      .'<option value="0"'.$select0.'>1</option>'
 8636:                      .'<option value="1"'.$select1.'>2</option>'
 8637:                      .'<option value="2">3</option></select>&nbsp;'
 8638:                      .$default_names{$default}
 8639:                      .'</span></td><td><span class="LC_nobreak">'
 8640:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 8641:                      .&mt('Display').'</label>&nbsp;<label>'
 8642:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 8643:                  .'</label></span></td></tr>';
 8644:         $itemcount ++;
 8645:     }
 8646:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8647:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 8648:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8649:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 8650:                   .'<option value="0">1</option>'
 8651:                   .'<option value="1">2</option>'
 8652:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 8653:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
 8654:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
 8655:                   .'</td></tr>';
 8656:     return $datatable;
 8657: }
 8658: 
 8659: sub build_category_rows {
 8660:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 8661:     my ($text,$name,$item,$chgstr);
 8662:     if (ref($cats) eq 'ARRAY') {
 8663:         my $maxdepth = scalar(@{$cats});
 8664:         if (ref($cats->[$depth]) eq 'HASH') {
 8665:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 8666:                 my $numchildren = @{$cats->[$depth]{$parent}};
 8667:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8668:                 $text .= '<td><table class="LC_data_table">';
 8669:                 my ($idxnum,$parent_name,$parent_item);
 8670:                 my $higher = $depth - 1;
 8671:                 if ($higher == 0) {
 8672:                     $parent_name = &escape($parent).'::'.$higher;
 8673:                 } else {
 8674:                     if (ref($path) eq 'ARRAY') {
 8675:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8676:                     }
 8677:                 }
 8678:                 $parent_item = 'addcategory_pos_'.$parent_name;
 8679:                 for (my $j=0; $j<=$numchildren; $j++) {
 8680:                     if ($j < $numchildren) {
 8681:                         $name = $cats->[$depth]{$parent}[$j];
 8682:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 8683:                         $idxnum = $idx->{$item};
 8684:                     } else {
 8685:                         $name = $parent_name;
 8686:                         $item = $parent_item;
 8687:                     }
 8688:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 8689:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 8690:                     for (my $i=0; $i<=$numchildren; $i++) {
 8691:                         my $vpos = $i+1;
 8692:                         my $selstr;
 8693:                         if ($j == $i) {
 8694:                             $selstr = ' selected="selected" ';
 8695:                         }
 8696:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 8697:                     }
 8698:                     $text .= '</select>&nbsp;';
 8699:                     if ($j < $numchildren) {
 8700:                         my $deeper = $depth+1;
 8701:                         $text .= $name.'&nbsp;'
 8702:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 8703:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 8704:                         if(ref($path) eq 'ARRAY') {
 8705:                             push(@{$path},$name);
 8706:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 8707:                             pop(@{$path});
 8708:                         }
 8709:                     } else {
 8710:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
 8711:                         if ($j == $numchildren) {
 8712:                             $text .= $name;
 8713:                         } else {
 8714:                             $text .= $item;
 8715:                         }
 8716:                         $text .= '" value="" />';
 8717:                     }
 8718:                     $text .= '</td></tr>';
 8719:                 }
 8720:                 $text .= '</table></td>';
 8721:             } else {
 8722:                 my $higher = $depth-1;
 8723:                 if ($higher == 0) {
 8724:                     $name = &escape($parent).'::'.$higher;
 8725:                 } else {
 8726:                     if (ref($path) eq 'ARRAY') {
 8727:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8728:                     }
 8729:                 }
 8730:                 my $colspan;
 8731:                 if ($parent ne 'instcode') {
 8732:                     $colspan = $maxdepth - $depth - 1;
 8733:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
 8734:                 }
 8735:             }
 8736:         }
 8737:     }
 8738:     return $text;
 8739: }
 8740: 
 8741: sub modifiable_userdata_row {
 8742:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 8743:         $rowid,$customcss,$rowstyle) = @_;
 8744:     my ($role,$rolename,$statustype);
 8745:     $role = $item;
 8746:     if ($context eq 'cancreate') {
 8747:         if ($item =~ /^(emailusername)_(.+)$/) {
 8748:             $role = $1;
 8749:             $statustype = $2;
 8750:             if (ref($usertypes) eq 'HASH') {
 8751:                 if ($usertypes->{$statustype}) {
 8752:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 8753:                 } else {
 8754:                     $rolename = &mt('Data provided by user');
 8755:                 }
 8756:             }
 8757:         }
 8758:     } elsif ($context eq 'selfcreate') {
 8759:         if (ref($usertypes) eq 'HASH') {
 8760:             $rolename = $usertypes->{$role};
 8761:         } else {
 8762:             $rolename = $role;
 8763:         }
 8764:     } else {
 8765:         if ($role eq 'cr') {
 8766:             $rolename = &mt('Custom role');
 8767:         } else {
 8768:             $rolename = &Apache::lonnet::plaintext($role);
 8769:         }
 8770:     }
 8771:     my (@fields,%fieldtitles);
 8772:     if (ref($fieldsref) eq 'ARRAY') {
 8773:         @fields = @{$fieldsref};
 8774:     } else {
 8775:         @fields = ('lastname','firstname','middlename','generation',
 8776:                    'permanentemail','id');
 8777:     }
 8778:     if ((ref($titlesref) eq 'HASH')) {
 8779:         %fieldtitles = %{$titlesref};
 8780:     } else {
 8781:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8782:     }
 8783:     my $output;
 8784:     my $css_class;
 8785:     if ($rowcount%2) {
 8786:         $css_class = 'LC_odd_row';
 8787:     }
 8788:     if ($customcss) {
 8789:         $css_class .= " $customcss";
 8790:     }
 8791:     $css_class =~ s/^\s+//;
 8792:     if ($css_class) {
 8793:         $css_class = ' class="'.$css_class.'"';
 8794:     }
 8795:     if ($rowstyle) {
 8796:         $css_class .= ' style="'.$rowstyle.'"';
 8797:     }
 8798:     if ($rowid) {
 8799:         $rowid = ' id="'.$rowid.'"';
 8800:     }
 8801: 
 8802:     $output = '<tr '.$css_class.$rowid.'>'.
 8803:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 8804:               '<td class="LC_left_item" colspan="2"><table>';
 8805:     my $rem;
 8806:     my %checks;
 8807:     if (ref($settings) eq 'HASH') {
 8808:         if (ref($settings->{$context}) eq 'HASH') {
 8809:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 8810:                 my $hashref = $settings->{$context}->{$role};
 8811:                 if ($role eq 'emailusername') {
 8812:                     if ($statustype) {
 8813:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 8814:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 8815:                             if (ref($hashref) eq 'HASH') { 
 8816:                                 foreach my $field (@fields) {
 8817:                                     if ($hashref->{$field}) {
 8818:                                         $checks{$field} = $hashref->{$field};
 8819:                                     }
 8820:                                 }
 8821:                             }
 8822:                         }
 8823:                     }
 8824:                 } else {
 8825:                     if (ref($hashref) eq 'HASH') {
 8826:                         foreach my $field (@fields) {
 8827:                             if ($hashref->{$field}) {
 8828:                                 $checks{$field} = ' checked="checked" ';
 8829:                             }
 8830:                         }
 8831:                     }
 8832:                 }
 8833:             }
 8834:         }
 8835:     }
 8836: 
 8837:     my $total = scalar(@fields);
 8838:     for (my $i=0; $i<$total; $i++) {
 8839:         $rem = $i%($numinrow);
 8840:         if ($rem == 0) {
 8841:             if ($i > 0) {
 8842:                 $output .= '</tr>';
 8843:             }
 8844:             $output .= '<tr>';
 8845:         }
 8846:         my $check = ' ';
 8847:         unless ($role eq 'emailusername') {
 8848:             if (exists($checks{$fields[$i]})) {
 8849:                 $check = $checks{$fields[$i]};
 8850:             } else {
 8851:                 if ($role eq 'st') {
 8852:                     if (ref($settings) ne 'HASH') {
 8853:                         $check = ' checked="checked" '; 
 8854:                     }
 8855:                 }
 8856:             }
 8857:         }
 8858:         $output .= '<td class="LC_left_item">'.
 8859:                    '<span class="LC_nobreak">';
 8860:         if ($role eq 'emailusername') {
 8861:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 8862:                 $checks{$fields[$i]} = 'omit';
 8863:             }
 8864:             foreach my $option ('required','optional','omit') {
 8865:                 my $checked='';
 8866:                 if ($checks{$fields[$i]} eq $option) {
 8867:                     $checked='checked="checked" ';
 8868:                 }
 8869:                 $output .= '<label>'.
 8870:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 8871:                            &mt($option).'</label>'.('&nbsp;' x2);
 8872:             }
 8873:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 8874:         } else {
 8875:             $output .= '<label>'.
 8876:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 8877:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 8878:                        '</label>';
 8879:         }
 8880:         $output .= '</span></td>';
 8881:     }
 8882:     $rem = $total%$numinrow;
 8883:     my $colsleft;
 8884:     if ($rem) {
 8885:         $colsleft = $numinrow - $rem;
 8886:     }
 8887:     if ($colsleft > 1) {
 8888:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8889:                    '&nbsp;</td>';
 8890:     } elsif ($colsleft == 1) {
 8891:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 8892:     }
 8893:     $output .= '</tr></table></td></tr>';
 8894:     return $output;
 8895: }
 8896: 
 8897: sub insttypes_row {
 8898:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 8899:         $customcss,$rowstyle) = @_;
 8900:     my %lt = &Apache::lonlocal::texthash (
 8901:                       cansearch => 'Users allowed to search',
 8902:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 8903:                       lockablenames => 'User preference to lock name',
 8904:                       selfassign    => 'Self-reportable affiliations',
 8905:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 8906:              );
 8907:     my $showdom;
 8908:     if ($context eq 'cansearch') {
 8909:         $showdom = ' ('.$dom.')';
 8910:     }
 8911:     my $class = 'LC_left_item';
 8912:     if ($context eq 'statustocreate') {
 8913:         $class = 'LC_right_item';
 8914:     }
 8915:     my $css_class;
 8916:     if ($$rowtotal%2) {
 8917:         $css_class = 'LC_odd_row';
 8918:     }
 8919:     if ($customcss) {
 8920:         $css_class .= ' '.$customcss;
 8921:     }
 8922:     $css_class =~ s/^\s+//;
 8923:     if ($css_class) {
 8924:         $css_class = ' class="'.$css_class.'"';
 8925:     }
 8926:     if ($rowstyle) {
 8927:         $css_class .= ' style="'.$rowstyle.'"';
 8928:     }
 8929:     if ($onclick) {
 8930:         $onclick = 'onclick="'.$onclick.'" ';
 8931:     }
 8932:     my $output = '<tr'.$css_class.'>'.
 8933:                  '<td>'.$lt{$context}.$showdom.
 8934:                  '</td><td class="'.$class.'" colspan="2"><table>';
 8935:     my $rem;
 8936:     if (ref($types) eq 'ARRAY') {
 8937:         for (my $i=0; $i<@{$types}; $i++) {
 8938:             if (defined($usertypes->{$types->[$i]})) {
 8939:                 my $rem = $i%($numinrow);
 8940:                 if ($rem == 0) {
 8941:                     if ($i > 0) {
 8942:                         $output .= '</tr>';
 8943:                     }
 8944:                     $output .= '<tr>';
 8945:                 }
 8946:                 my $check = ' ';
 8947:                 if (ref($settings) eq 'HASH') {
 8948:                     if (ref($settings->{$context}) eq 'ARRAY') {
 8949:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 8950:                             $check = ' checked="checked" ';
 8951:                         }
 8952:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 8953:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 8954:                             $check = ' checked="checked" ';
 8955:                         }
 8956:                     } elsif ($context eq 'statustocreate') {
 8957:                         $check = ' checked="checked" ';
 8958:                     }
 8959:                 }
 8960:                 $output .= '<td class="LC_left_item">'.
 8961:                            '<span class="LC_nobreak"><label>'.
 8962:                            '<input type="checkbox" name="'.$context.'" '.
 8963:                            'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
 8964:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 8965:             }
 8966:         }
 8967:         $rem = @{$types}%($numinrow);
 8968:     }
 8969:     my $colsleft = $numinrow - $rem;
 8970:     if ($context eq 'overrides') {
 8971:         if ($colsleft > 1) {
 8972:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 8973:         } else {
 8974:             $output .= '<td class="LC_left_item">';
 8975:         }
 8976:         $output .= '&nbsp;';
 8977:     } else {
 8978:         if ($rem == 0) {
 8979:             $output .= '<tr>';
 8980:         }
 8981:         if ($colsleft > 1) {
 8982:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 8983:         } else {
 8984:             $output .= '<td class="LC_left_item">';
 8985:         }
 8986:         my $defcheck = ' ';
 8987:         if (ref($settings) eq 'HASH') {  
 8988:             if (ref($settings->{$context}) eq 'ARRAY') {
 8989:                 if (grep(/^default$/,@{$settings->{$context}})) {
 8990:                     $defcheck = ' checked="checked" ';
 8991:                 }
 8992:             } elsif ($context eq 'statustocreate') {
 8993:                 $defcheck = ' checked="checked" ';
 8994:             }
 8995:         }
 8996:         $output .= '<span class="LC_nobreak"><label>'.
 8997:                    '<input type="checkbox" name="'.$context.'" '.
 8998:                    'value="default"'.$defcheck.$onclick.' />'.
 8999:                    $othertitle.'</label></span>';
 9000:     }
 9001:     $output .= '</td></tr></table></td></tr>';
 9002:     return $output;
 9003: }
 9004: 
 9005: sub sorted_searchtitles {
 9006:     my %searchtitles = &Apache::lonlocal::texthash(
 9007:                          'uname' => 'username',
 9008:                          'lastname' => 'last name',
 9009:                          'lastfirst' => 'last name, first name',
 9010:                      );
 9011:     my @titleorder = ('uname','lastname','lastfirst');
 9012:     return (\%searchtitles,\@titleorder);
 9013: }
 9014: 
 9015: sub sorted_searchtypes {
 9016:     my %srchtypes_desc = (
 9017:                            exact    => 'is exact match',
 9018:                            contains => 'contains ..',
 9019:                            begins   => 'begins with ..',
 9020:                          );
 9021:     my @srchtypeorder = ('exact','begins','contains');
 9022:     return (\%srchtypes_desc,\@srchtypeorder);
 9023: }
 9024: 
 9025: sub usertype_update_row {
 9026:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 9027:     my $datatable;
 9028:     my $numinrow = 4;
 9029:     foreach my $type (@{$types}) {
 9030:         if (defined($usertypes->{$type})) {
 9031:             $$rownums ++;
 9032:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 9033:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 9034:                           '</td><td class="LC_left_item"><table>';
 9035:             for (my $i=0; $i<@{$fields}; $i++) {
 9036:                 my $rem = $i%($numinrow);
 9037:                 if ($rem == 0) {
 9038:                     if ($i > 0) {
 9039:                         $datatable .= '</tr>';
 9040:                     }
 9041:                     $datatable .= '<tr>';
 9042:                 }
 9043:                 my $check = ' ';
 9044:                 if (ref($settings) eq 'HASH') {
 9045:                     if (ref($settings->{'fields'}) eq 'HASH') {
 9046:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 9047:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 9048:                                 $check = ' checked="checked" ';
 9049:                             }
 9050:                         }
 9051:                     }
 9052:                 }
 9053: 
 9054:                 if ($i == @{$fields}-1) {
 9055:                     my $colsleft = $numinrow - $rem;
 9056:                     if ($colsleft > 1) {
 9057:                         $datatable .= '<td colspan="'.$colsleft.'">';
 9058:                     } else {
 9059:                         $datatable .= '<td>';
 9060:                     }
 9061:                 } else {
 9062:                     $datatable .= '<td>';
 9063:                 }
 9064:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9065:                               '<input type="checkbox" name="updateable_'.$type.
 9066:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 9067:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 9068:             }
 9069:             $datatable .= '</tr></table></td></tr>';
 9070:         }
 9071:     }
 9072:     return $datatable;
 9073: }
 9074: 
 9075: sub modify_login {
 9076:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9077:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 9078:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon,
 9079:         %currsaml,%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlnotsso);
 9080:     %title = ( coursecatalog => 'Display course catalog',
 9081:                adminmail => 'Display administrator E-mail address',
 9082:                helpdesk  => 'Display "Contact Helpdesk" link',
 9083:                newuser => 'Link for visitors to create a user account',
 9084:                loginheader => 'Log-in box header',
 9085:                saml => 'Dual SSO and non-SSO login');
 9086:     @offon = ('off','on');
 9087:     if (ref($domconfig{login}) eq 'HASH') {
 9088:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 9089:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 9090:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 9091:             }
 9092:         }
 9093:         if (ref($domconfig{login}{'saml'}) eq 'HASH') {
 9094:             foreach my $lonhost (keys(%{$domconfig{login}{'saml'}})) {
 9095:                 if (ref($domconfig{login}{'saml'}{$lonhost}) eq 'HASH') {
 9096:                     $currsaml{$lonhost} = $domconfig{login}{'saml'}{$lonhost};
 9097:                     $saml{$lonhost} = 1;
 9098:                     $samltext{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'text'};
 9099:                     $samlurl{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'url'};
 9100:                     $samlalt{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'alt'};
 9101:                     $samlimg{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'img'};
 9102:                     $samltitle{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'title'};
 9103:                     $samlnotsso{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'notsso'};
 9104:                 }
 9105:             }
 9106:         }
 9107:     }
 9108:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 9109:                                            \%domconfig,\%loginhash);
 9110:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 9111:     foreach my $item (@toggles) {
 9112:         $loginhash{login}{$item} = $env{'form.'.$item};
 9113:     }
 9114:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 9115:     if (ref($colchanges{'login'}) eq 'HASH') {  
 9116:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 9117:                                          \%loginhash);
 9118:     }
 9119: 
 9120:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9121:     my %domservers = &Apache::lonnet::get_servers($dom);
 9122:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 9123:     if (keys(%servers) > 1) {
 9124:         foreach my $lonhost (keys(%servers)) {
 9125:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 9126:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 9127:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 9128:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 9129:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 9130:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9131:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9132:                         $changes{'loginvia'}{$lonhost} = 1;
 9133:                     } else {
 9134:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 9135:                         $changes{'loginvia'}{$lonhost} = 1;
 9136:                     }
 9137:                 } else {
 9138:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9139:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9140:                         $changes{'loginvia'}{$lonhost} = 1;
 9141:                     }
 9142:                 }
 9143:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 9144:                     foreach my $item (@loginvia_attribs) {
 9145:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 9146:                     }
 9147:                 } else {
 9148:                     foreach my $item (@loginvia_attribs) {
 9149:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 9150:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 9151:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 9152:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 9153:                                 $new = '/';
 9154:                             }
 9155:                         }
 9156:                         if (($item eq 'custompath') && 
 9157:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 9158:                             $new = '';
 9159:                         }
 9160:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 9161:                             $changes{'loginvia'}{$lonhost} = 1;
 9162:                         }
 9163:                         if ($item eq 'exempt') {
 9164:                             $new = &check_exempt_addresses($new);
 9165:                         }
 9166:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 9167:                     }
 9168:                 }
 9169:             } else {
 9170:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 9171:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 9172:                     $changes{'loginvia'}{$lonhost} = 1;
 9173:                     foreach my $item (@loginvia_attribs) {
 9174:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 9175:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 9176:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 9177:                                 $new = '/';
 9178:                             }
 9179:                         }
 9180:                         if (($item eq 'custompath') && 
 9181:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 9182:                             $new = '';
 9183:                         }
 9184:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 9185:                     }
 9186:                 }
 9187:             }
 9188:         }
 9189:     }
 9190: 
 9191:     my $servadm = $r->dir_config('lonAdmEMail');
 9192:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 9193:     if (ref($domconfig{'login'}) eq 'HASH') {
 9194:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 9195:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 9196:                 if ($lang eq 'nolang') {
 9197:                     push(@currlangs,$lang);
 9198:                 } elsif (defined($langchoices{$lang})) {
 9199:                     push(@currlangs,$lang);
 9200:                 } else {
 9201:                     next;
 9202:                 }
 9203:             }
 9204:         }
 9205:     }
 9206:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 9207:     if (@currlangs > 0) {
 9208:         foreach my $lang (@currlangs) {
 9209:             if (grep(/^\Q$lang\E$/,@delurls)) {
 9210:                 $changes{'helpurl'}{$lang} = 1;
 9211:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 9212:                 $changes{'helpurl'}{$lang} = 1;
 9213:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 9214:                 push(@newlangs,$lang);
 9215:             } else {
 9216:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 9217:             }
 9218:         }
 9219:     }
 9220:     unless (grep(/^nolang$/,@currlangs)) {
 9221:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 9222:             $changes{'helpurl'}{'nolang'} = 1;
 9223:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 9224:             push(@newlangs,'nolang');
 9225:         }
 9226:     }
 9227:     if ($env{'form.loginhelpurl_add_lang'}) {
 9228:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 9229:             ($env{'form.loginhelpurl_add_file.filename'})) {
 9230:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 9231:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 9232:         }
 9233:     }
 9234:     if ((@newlangs > 0) || ($addedfile)) {
 9235:         my $error;
 9236:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9237:         if ($configuserok eq 'ok') {
 9238:             if ($switchserver) {
 9239:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 9240:             } elsif ($author_ok eq 'ok') {
 9241:                 my @allnew = @newlangs;
 9242:                 if ($addedfile ne '') {
 9243:                     push(@allnew,$addedfile);
 9244:                 }
 9245:                 foreach my $lang (@allnew) {
 9246:                     my $formelem = 'loginhelpurl_'.$lang;
 9247:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 9248:                         $formelem = 'loginhelpurl_add_file';
 9249:                     }
 9250:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 9251:                                                                "help/$lang",'','',$newfile{$lang});
 9252:                     if ($result eq 'ok') {
 9253:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 9254:                         $changes{'helpurl'}{$lang} = 1;
 9255:                     } else {
 9256:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 9257:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 9258:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 9259:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 9260:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 9261:                         }
 9262:                     }
 9263:                 }
 9264:             } else {
 9265:                 $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);
 9266:             }
 9267:         } else {
 9268:             $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);
 9269:         }
 9270:         if ($error) {
 9271:             &Apache::lonnet::logthis($error);
 9272:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9273:         }
 9274:     }
 9275: 
 9276:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 9277:     if (ref($domconfig{'login'}) eq 'HASH') {
 9278:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 9279:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 9280:                 if ($domservers{$lonhost}) {
 9281:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 9282:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 9283:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 9284:                     }
 9285:                 }
 9286:             }
 9287:         }
 9288:     }
 9289:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 9290:     foreach my $lonhost (sort(keys(%domservers))) {
 9291:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 9292:             $changes{'headtag'}{$lonhost} = 1;
 9293:         } else {
 9294:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 9295:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 9296:             }
 9297:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 9298:                 push(@newhosts,$lonhost);
 9299:             } elsif ($currheadtagurls{$lonhost}) {
 9300:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 9301:                 if ($currexempt{$lonhost}) {
 9302:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 9303:                         $changes{'headtag'}{$lonhost} = 1;
 9304:                     }
 9305:                 } elsif ($possexempt{$lonhost}) {
 9306:                     $changes{'headtag'}{$lonhost} = 1;
 9307:                 }
 9308:                 if ($possexempt{$lonhost}) {
 9309:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 9310:                 }
 9311:             }
 9312:         }
 9313:     }
 9314:     if (@newhosts) {
 9315:         my $error;
 9316:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9317:         if ($configuserok eq 'ok') {
 9318:             if ($switchserver) {
 9319:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 9320:             } elsif ($author_ok eq 'ok') {
 9321:                 foreach my $lonhost (@newhosts) {
 9322:                     my $formelem = 'loginheadtag_'.$lonhost;
 9323:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 9324:                                                                           "login/headtag/$lonhost",'','',
 9325:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 9326:                     if ($result eq 'ok') {
 9327:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 9328:                         $changes{'headtag'}{$lonhost} = 1;
 9329:                         if ($possexempt{$lonhost}) {
 9330:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 9331:                         }
 9332:                     } else {
 9333:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 9334:                                            $newheadtagurls{$lonhost},$result);
 9335:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 9336:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 9337:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 9338:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 9339:                         }
 9340:                     }
 9341:                 }
 9342:             } else {
 9343:                 $error = &mt("Upload of custom markup file(s) failed because an author role could not be assigned to a Domain Configuration user ([_1]) in domain: [_2].  Error was: [_3].",$confname,$dom,$author_ok);
 9344:             }
 9345:         } else {
 9346:             $error = &mt("Upload of custom markup file(s) failed because a Domain Configuration user ([_1]) could not be created in domain: [_2].  Error was: [_3].",$confname,$dom,$configuserok);
 9347:         }
 9348:         if ($error) {
 9349:             &Apache::lonnet::logthis($error);
 9350:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9351:         }
 9352:     }
 9353:     my @delsamlimg = &Apache::loncommon::get_env_multiple('form.saml_img_del');
 9354:     my @newsamlimgs;
 9355:     foreach my $lonhost (keys(%domservers)) {
 9356:         if ($env{'form.saml_'.$lonhost}) {
 9357:             if ($env{'form.saml_img_'.$lonhost.'.filename'}) {
 9358:                 push(@newsamlimgs,$lonhost);
 9359:             }
 9360:             foreach my $item ('text','alt','url','title','notsso') {
 9361:                 $env{'form.saml_'.$item.'_'.$lonhost} =~ s/^\s+|\s+$//g;
 9362:             }
 9363:             if ($saml{$lonhost}) {
 9364:                 if (grep(/^\Q$lonhost\E$/,@delsamlimg)) {
 9365: #FIXME Need to obsolete published image
 9366:                     delete($currsaml{$lonhost}{'img'});
 9367:                     $changes{'saml'}{$lonhost} = 1;
 9368:                 }
 9369:                 if ($env{'form.saml_alt_'.$lonhost} ne $samlalt{$lonhost}) {
 9370:                     $changes{'saml'}{$lonhost} = 1;
 9371:                 }
 9372:                 if ($env{'form.saml_text_'.$lonhost} ne $samltext{$lonhost}) {
 9373:                     $changes{'saml'}{$lonhost} = 1;
 9374:                 }
 9375:                 if ($env{'form.saml_url_'.$lonhost} ne $samlurl{$lonhost}) {
 9376:                     $changes{'saml'}{$lonhost} = 1;
 9377:                 }
 9378:                 if ($env{'form.saml_title_'.$lonhost} ne $samltitle{$lonhost}) {
 9379:                     $changes{'saml'}{$lonhost} = 1;
 9380:                 }
 9381:                 if ($env{'form.saml_notsso_'.$lonhost} ne $samlnotsso{$lonhost}) {
 9382:                     $changes{'saml'}{$lonhost} = 1;
 9383:                 }
 9384:             } else {
 9385:                 $changes{'saml'}{$lonhost} = 1;
 9386:             }
 9387:             foreach my $item ('text','alt','url','title','notsso') {
 9388:                 $currsaml{$lonhost}{$item} = $env{'form.saml_'.$item.'_'.$lonhost};
 9389:             }
 9390:         } else {
 9391:             if ($saml{$lonhost}) {
 9392:                 $changes{'saml'}{$lonhost} = 1;
 9393:                 delete($currsaml{$lonhost});
 9394:             }
 9395:         }
 9396:     }
 9397:     foreach my $posshost (keys(%currsaml)) {
 9398:         unless (exists($domservers{$posshost})) {
 9399:             delete($currsaml{$posshost});
 9400:         }
 9401:     }
 9402:     %{$loginhash{'login'}{'saml'}} = %currsaml;
 9403:     if (@newsamlimgs) {
 9404:         my $error;
 9405:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9406:         if ($configuserok eq 'ok') {
 9407:             if ($switchserver) {
 9408:                 $error = &mt("Upload of SSO Button Image is not permitted to this server: [_1].",$switchserver);
 9409:             } elsif ($author_ok eq 'ok') {
 9410:                 foreach my $lonhost (@newsamlimgs) {
 9411:                     my $formelem = 'saml_img_'.$lonhost;
 9412:                     my ($result,$imgurl) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 9413:                                                         "login/saml/$lonhost",'','',
 9414:                                                         $env{'form.saml_img_'.$lonhost.'.filename'});
 9415:                     if ($result eq 'ok') {
 9416:                         $currsaml{$lonhost}{'img'} = $imgurl;
 9417:                         $loginhash{'login'}{'saml'}{$lonhost}{'img'} = $imgurl;
 9418:                         $changes{'saml'}{$lonhost} = 1;
 9419:                     } else {
 9420:                         my $puberror = &mt("Upload of SSO button image failed for [_1] because an error occurred publishing the file in RES space. Error was: [_2].",
 9421:                                            $lonhost,$result);
 9422:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 9423:                     }
 9424:                 }
 9425:             } else {
 9426:                 $error = &mt("Upload of SSO button image 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);
 9427:             }
 9428:         } else {
 9429:             $error = &mt("Upload of SSO button image file(s) failed because a Domain Configuration user ([_1]) could not be created in domain: [_2].  Error was: [_3].",$confname,$dom,$configuserok);
 9430:         }
 9431:         if ($error) {
 9432:             &Apache::lonnet::logthis($error);
 9433:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9434:         }
 9435:     }
 9436:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 9437: 
 9438:     my $defaulthelpfile = '/adm/loginproblems.html';
 9439:     my $defaulttext = &mt('Default in use');
 9440: 
 9441:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 9442:                                              $dom);
 9443:     if ($putresult eq 'ok') {
 9444:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 9445:         my %defaultchecked = (
 9446:                     'coursecatalog' => 'on',
 9447:                     'helpdesk'      => 'on',
 9448:                     'adminmail'     => 'off',
 9449:                     'newuser'       => 'off',
 9450:         );
 9451:         if (ref($domconfig{'login'}) eq 'HASH') {
 9452:             foreach my $item (@toggles) {
 9453:                 if ($defaultchecked{$item} eq 'on') { 
 9454:                     if (($domconfig{'login'}{$item} eq '0') &&
 9455:                         ($env{'form.'.$item} eq '1')) {
 9456:                         $changes{$item} = 1;
 9457:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 9458:                               $domconfig{'login'}{$item} eq '1') &&
 9459:                              ($env{'form.'.$item} eq '0')) {
 9460:                         $changes{$item} = 1;
 9461:                     }
 9462:                 } elsif ($defaultchecked{$item} eq 'off') {
 9463:                     if (($domconfig{'login'}{$item} eq '1') &&
 9464:                         ($env{'form.'.$item} eq '0')) {
 9465:                         $changes{$item} = 1;
 9466:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 9467:                               $domconfig{'login'}{$item} eq '0') &&
 9468:                              ($env{'form.'.$item} eq '1')) {
 9469:                         $changes{$item} = 1;
 9470:                     }
 9471:                 }
 9472:             }
 9473:         }
 9474:         if (keys(%changes) > 0 || $colchgtext) {
 9475:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9476:             if (exists($changes{'saml'})) {
 9477:                 my $hostid_in_use;
 9478:                 my @hosts = &Apache::lonnet::current_machine_ids();
 9479:                 if (@hosts > 1) {
 9480:                     foreach my $hostid (@hosts) {
 9481:                         if (&Apache::lonnet::host_domain($hostid) eq $dom) {
 9482:                             $hostid_in_use = $hostid;
 9483:                             last;
 9484:                         }
 9485:                     }
 9486:                 } else {
 9487:                     $hostid_in_use = $r->dir_config('lonHostID');
 9488:                 }
 9489:                 if (($hostid_in_use) &&
 9490:                     (&Apache::lonnet::host_domain($hostid_in_use) eq $dom)) {
 9491:                     &Apache::lonnet::devalidate_cache_new('samllanding',$hostid_in_use);
 9492:                 }
 9493:                 if (ref($lastactref) eq 'HASH') {
 9494:                     if (ref($changes{'saml'}) eq 'HASH') {
 9495:                         my %updates;
 9496:                         map { $updates{$_} = 1; } keys(%{$changes{'saml'}});
 9497:                         $lastactref->{'samllanding'} = \%updates;
 9498:                     }
 9499:                 }
 9500:             }
 9501:             if (ref($lastactref) eq 'HASH') {
 9502:                 $lastactref->{'domainconfig'} = 1;
 9503:             }
 9504:             $resulttext = &mt('Changes made:').'<ul>';
 9505:             foreach my $item (sort(keys(%changes))) {
 9506:                 if ($item eq 'loginvia') {
 9507:                     if (ref($changes{$item}) eq 'HASH') {
 9508:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 9509:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 9510:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 9511:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 9512:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 9513:                                     $protocol = 'http' if ($protocol ne 'https');
 9514:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 9515: 
 9516:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 9517:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 9518:                                     } else {
 9519:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 9520:                                     }
 9521:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 9522:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 9523:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 9524:                                     }
 9525:                                     $resulttext .= '</li>';
 9526:                                 } else {
 9527:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 9528:                                 }
 9529:                             } else {
 9530:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 9531:                             }
 9532:                         }
 9533:                         $resulttext .= '</ul></li>';
 9534:                     }
 9535:                 } elsif ($item eq 'helpurl') {
 9536:                     if (ref($changes{$item}) eq 'HASH') {
 9537:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 9538:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 9539:                                 my ($chg,$link);
 9540:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 9541:                                 if ($lang eq 'nolang') {
 9542:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 9543:                                 } else {
 9544:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 9545:                                 }
 9546:                                 $resulttext .= '<li>'.$chg.'</li>';
 9547:                             } else {
 9548:                                 my $chg;
 9549:                                 if ($lang eq 'nolang') {
 9550:                                     $chg = &mt('custom log-in help file for no preferred language');
 9551:                                 } else {
 9552:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 9553:                                 }
 9554:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 9555:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 9556:                                                       '?inhibitmenu=yes',$chg,600,500).
 9557:                                                '</li>';
 9558:                             }
 9559:                         }
 9560:                     }
 9561:                 } elsif ($item eq 'headtag') {
 9562:                     if (ref($changes{$item}) eq 'HASH') {
 9563:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 9564:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 9565:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 9566:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 9567:                                 $resulttext .= '<li><a href="'.
 9568:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 9569:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 9570:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 9571:                                 if ($possexempt{$lonhost}) {
 9572:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 9573:                                 } else {
 9574:                                     $resulttext .= &mt('included for any client IP');
 9575:                                 }
 9576:                                 $resulttext .= '</li>';
 9577:                             }
 9578:                         }
 9579:                     }
 9580:                 } elsif ($item eq 'saml') {
 9581:                     if (ref($changes{$item}) eq 'HASH') {
 9582:                         my %notlt = (
 9583:                                        text   => 'Text for log-in by SSO',
 9584:                                        img    => 'SSO button image',
 9585:                                        alt    => 'Alt text for button image',
 9586:                                        url    => 'SSO URL',
 9587:                                        title  => 'Tooltip for SSO link',
 9588:                                        notsso => 'Text for non-SSO log-in',
 9589:                                     );
 9590:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 9591:                             if (ref($currsaml{$lonhost}) eq 'HASH') {
 9592:                                 $resulttext .= '<li>'.&mt("$title{$item} in use for [_1]","<b>$lonhost</b>").
 9593:                                                '<ul>';
 9594:                                 foreach my $key ('text','img','alt','url','title','notsso') {
 9595:                                     if ($currsaml{$lonhost}{$key} eq '') {
 9596:                                         $resulttext .= '<li>'.&mt("$notlt{$key} not in use").'</li>';
 9597:                                     } else {
 9598:                                         my $value = "'$currsaml{$lonhost}{$key}'";
 9599:                                         if ($key eq 'img') {
 9600:                                             $value = '<img src="'.$currsaml{$lonhost}{$key}.'" />';
 9601:                                         }
 9602:                                         $resulttext .= '<li>'.&mt("$notlt{$key} set to: [_1]",
 9603:                                                                   $value).'</li>';
 9604:                                     }
 9605:                                 }
 9606:                                 $resulttext .= '</ul></li>';
 9607:                             } else {
 9608:                                 $resulttext .= '<li>'.&mt("$title{$item} not in use for [_1]",$lonhost).'</li>';
 9609:                             }
 9610:                         }
 9611:                     }
 9612:                 } elsif ($item eq 'captcha') {
 9613:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9614:                         my $chgtxt;
 9615:                         if ($loginhash{'login'}{$item} eq 'notused') {
 9616:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 9617:                         } else {
 9618:                             my %captchas = &captcha_phrases();
 9619:                             if ($captchas{$loginhash{'login'}{$item}}) {
 9620:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 9621:                             } else {
 9622:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 9623:                             }
 9624:                         }
 9625:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 9626:                     }
 9627:                 } elsif ($item eq 'recaptchakeys') {
 9628:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9629:                         my ($privkey,$pubkey);
 9630:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 9631:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 9632:                             $privkey = $loginhash{'login'}{$item}{'private'};
 9633:                         }
 9634:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 9635:                         if (!$pubkey) {
 9636:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 9637:                         } else {
 9638:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 9639:                         }
 9640:                         if (!$privkey) {
 9641:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 9642:                         } else {
 9643:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 9644:                         }
 9645:                         $chgtxt .= '</ul>';
 9646:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 9647:                     }
 9648:                 } elsif ($item eq 'recaptchaversion') {
 9649:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9650:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 9651:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 9652:                                            '</li>';
 9653:                         }
 9654:                     }
 9655:                 } else {
 9656:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 9657:                 }
 9658:             }
 9659:             $resulttext .= $colchgtext.'</ul>';
 9660:         } else {
 9661:             $resulttext = &mt('No changes made to log-in page settings');
 9662:         }
 9663:     } else {
 9664:         $resulttext = '<span class="LC_error">'.
 9665: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9666:     }
 9667:     if ($errors) {
 9668:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9669:                        $errors.'</ul>';
 9670:     }
 9671:     return $resulttext;
 9672: }
 9673: 
 9674: sub check_exempt_addresses {
 9675:     my ($iplist) = @_;
 9676:     $iplist =~ s/^\s+//;
 9677:     $iplist =~ s/\s+$//;
 9678:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 9679:     my (@okips,$new);
 9680:     foreach my $ip (@poss_ips) {
 9681:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 9682:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 9683:                 push(@okips,$ip);
 9684:             }
 9685:         }
 9686:     }
 9687:     if (@okips > 0) {
 9688:         $new = join(',',@okips);
 9689:     } else {
 9690:         $new = '';
 9691:     }
 9692:     return $new;
 9693: }
 9694: 
 9695: sub color_font_choices {
 9696:     my %choices =
 9697:         &Apache::lonlocal::texthash (
 9698:             img => "Header",
 9699:             bgs => "Background colors",
 9700:             links => "Link colors",
 9701:             images => "Images",
 9702:             font => "Font color",
 9703:             fontmenu => "Font menu",
 9704:             pgbg => "Page",
 9705:             tabbg => "Header",
 9706:             sidebg => "Border",
 9707:             link => "Link",
 9708:             alink => "Active link",
 9709:             vlink => "Visited link",
 9710:         );
 9711:     return %choices;
 9712: }
 9713: 
 9714: sub modify_ipaccess {
 9715:     my ($dom,$lastactref,%domconfig) = @_;
 9716:     my (@allpos,%changes,%confhash,$errors,$resulttext);
 9717:     my (@items,%deletions,%itemids,@warnings);
 9718:     my ($typeorder,$types) = &commblocktype_text();
 9719:     if ($env{'form.ipaccess_add'}) {
 9720:         my $name = $env{'form.ipaccess_name_add'};
 9721:         my ($newid,$error) = &get_ipaccess_id($dom,$name);
 9722:         if ($newid) {
 9723:             $itemids{'add'} = $newid;
 9724:             push(@items,'add');
 9725:             $changes{$newid} = 1;
 9726:         } else {
 9727:             $error = &mt('Failed to acquire unique ID for new IP access control item');
 9728:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9729:         }
 9730:     }
 9731:     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
 9732:         my @todelete = &Apache::loncommon::get_env_multiple('form.ipaccess_del');
 9733:         if (@todelete) {
 9734:             map { $deletions{$_} = 1; } @todelete;
 9735:         }
 9736:         my $maxnum = $env{'form.ipaccess_maxnum'};
 9737:         for (my $i=0; $i<$maxnum; $i++) {
 9738:             my $itemid = $env{'form.ipaccess_id_'.$i};
 9739:             $itemid =~ s/\D+//g;
 9740:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
 9741:                 if ($deletions{$itemid}) {
 9742:                     $changes{$itemid} = $domconfig{'ipaccess'}{$itemid}{'name'};
 9743:                 } else {
 9744:                     push(@items,$i);
 9745:                     $itemids{$i} = $itemid;
 9746:                 }
 9747:             }
 9748:         }
 9749:     }
 9750:     foreach my $idx (@items) {
 9751:         my $itemid = $itemids{$idx};
 9752:         next unless ($itemid);
 9753:         my %current;
 9754:         unless ($idx eq 'add') {
 9755:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
 9756:                 %current = %{$domconfig{'ipaccess'}{$itemid}};
 9757:             }
 9758:         }
 9759:         my $position = $env{'form.ipaccess_pos_'.$itemid};
 9760:         $position =~ s/\D+//g;
 9761:         if ($position ne '') {
 9762:             $allpos[$position] = $itemid;
 9763:         }
 9764:         my $name = $env{'form.ipaccess_name_'.$idx};
 9765:         $name =~ s/^\s+|\s+$//g;
 9766:         $confhash{$itemid}{'name'} = $name;
 9767:         my $possrange = $env{'form.ipaccess_range_'.$idx};
 9768:         $possrange =~ s/^\s+|\s+$//g;
 9769:         unless ($possrange eq '') {
 9770:             $possrange =~ s/[\r\n]+/\s/g;
 9771:             $possrange =~ s/\s*-\s*/-/g;
 9772:             $possrange =~ s/\s+/,/g;
 9773:             $possrange =~ s/,+/,/g;
 9774:             if ($possrange ne '') {
 9775:                 my (@ok,$count);
 9776:                 $count = 0;
 9777:                 foreach my $poss (split(/\,/,$possrange)) {
 9778:                     $count ++;
 9779:                     $poss = &validate_ip_pattern($poss);
 9780:                     if ($poss ne '') {
 9781:                         push(@ok,$poss);
 9782:                     }
 9783:                 }
 9784:                 my $diff = $count - scalar(@ok);
 9785:                 if ($diff) {
 9786:                     $errors .= '<li><span class="LC_error">'.
 9787:                                &mt('[quant,_1,IP] invalid and excluded from saved value for IP range(s) for [_2]',
 9788:                                    $diff,$name).
 9789:                                '</span></li>';
 9790:                 }
 9791:                 if (@ok) {
 9792:                     my @cidr_list;
 9793:                     foreach my $item (@ok) {
 9794:                         @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
 9795:                     }
 9796:                     $confhash{$itemid}{'ip'} = join(',',@cidr_list);
 9797:                 }
 9798:             }
 9799:         }
 9800:         foreach my $field ('name','ip') {
 9801:             unless (($idx eq 'add') || ($changes{$itemid})) {
 9802:                 if ($current{$field} ne $confhash{$itemid}{$field}) {
 9803:                     $changes{$itemid} = 1;
 9804:                     last;
 9805:                 }
 9806:             }
 9807:         }
 9808:         $confhash{$itemid}{'commblocks'} = {};
 9809: 
 9810:         my %commblocks;
 9811:         map { $commblocks{$_} = 1; } &Apache::loncommon::get_env_multiple('form.ipaccess_block_'.$idx);
 9812:         foreach my $type (@{$typeorder}) {
 9813:             if ($commblocks{$type}) {
 9814:                 $confhash{$itemid}{'commblocks'}{$type} = 'on';
 9815:             }
 9816:             unless (($idx eq 'add') || ($changes{$itemid})) {
 9817:                 if (ref($current{'commblocks'}) eq 'HASH') {
 9818:                     if ($confhash{$itemid}{'commblocks'}{$type} ne $current{'commblocks'}{$type}) {
 9819:                         $changes{$itemid} = 1;
 9820:                     }
 9821:                 } elsif ($confhash{$itemid}{'commblocks'}{$type}) {
 9822:                     $changes{$itemid} = 1;
 9823:                 }
 9824:             }
 9825:         }
 9826:         $confhash{$itemid}{'courses'} = {};
 9827:         my %crsdeletions;
 9828:         my @delcrs = &Apache::loncommon::get_env_multiple('form.ipaccess_course_delete_'.$idx);
 9829:         if (@delcrs) {
 9830:             map { $crsdeletions{$_} = 1; } @delcrs;
 9831:         }
 9832:         if (ref($current{'courses'}) eq 'HASH') {
 9833:             foreach my $cid (sort(keys(%{$current{'courses'}}))) {
 9834:                 if ($crsdeletions{$cid}) {
 9835:                     $changes{$itemid} = 1;
 9836:                 } else {
 9837:                     $confhash{$itemid}{'courses'}{$cid} = 1;
 9838:                 }
 9839:             }
 9840:         }
 9841:         $env{'form.ipaccess_cnum_'.$idx} =~ s/^\s+|\s+$//g;
 9842:         $env{'form.ipaccess_cdom_'.$idx} =~ s/^\s+|\s+$//g;
 9843:         if (($env{'form.ipaccess_cnum_'.$idx} =~ /^$match_courseid$/) &&
 9844:             ($env{'form.ipaccess_cdom_'.$idx} =~ /^$match_domain$/)) {
 9845:             if (&Apache::lonnet::homeserver($env{'form.ipaccess_cnum_'.$idx},
 9846:                                             $env{'form.ipaccess_cdom_'.$idx}) eq 'no_host') {
 9847:                 $errors .= '<li><span class="LC_error">'.
 9848:                            &mt('Invalid courseID [_1] omitted from list of allowed courses',
 9849:                                $env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}).
 9850:                            '</span></li>';
 9851:             } else {
 9852:                 $confhash{$itemid}{'courses'}{$env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}} = 1;
 9853:                 $changes{$itemid} = 1;
 9854:             }
 9855:         }
 9856:     }
 9857:     if (@allpos > 0) {
 9858:         my $idx = 0;
 9859:         foreach my $itemid (@allpos) {
 9860:             if ($itemid ne '') {
 9861:                 $confhash{$itemid}{'order'} = $idx;
 9862:                 unless ($changes{$itemid}) {
 9863:                     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
 9864:                         if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
 9865:                             if ($domconfig{'ipaccess'}{$itemid}{'order'} ne $idx) {
 9866:                                 $changes{$itemid} = 1;
 9867:                             }
 9868:                         }
 9869:                     }
 9870:                 }
 9871:                 $idx ++;
 9872:             }
 9873:         }
 9874:     }
 9875:     if (keys(%changes)) {
 9876:         my %defaultshash = (
 9877:                               ipaccess => \%confhash,
 9878:                            );
 9879:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9880:                                                  $dom);
 9881:         if ($putresult eq 'ok') {
 9882:             my $cachetime = 1800;
 9883:             &Apache::lonnet::do_cache_new('ipaccess',$dom,\%confhash,$cachetime);
 9884:             if (ref($lastactref) eq 'HASH') {
 9885:                 $lastactref->{'ipaccess'} = 1;
 9886:             }
 9887:             $resulttext = &mt('Changes made:').'<ul>';
 9888:             my %bynum;
 9889:             foreach my $itemid (sort(keys(%changes))) {
 9890:                 if (ref($confhash{$itemid}) eq 'HASH') {
 9891:                     my $position = $confhash{$itemid}{'order'};
 9892:                     if ($position =~ /^\d+$/) {
 9893:                         $bynum{$position} = $itemid;
 9894:                     }
 9895:                 }
 9896:             }
 9897:             if (keys(%deletions)) {
 9898:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
 9899:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
 9900:                 }
 9901:             }
 9902:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
 9903:                 my $itemid = $bynum{$pos};
 9904:                 if (ref($confhash{$itemid}) eq 'HASH') {
 9905:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'name'}.'</b><ul>';
 9906:                     my $position = $pos + 1;
 9907:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
 9908:                     if ($confhash{$itemid}{'ip'} eq '') {
 9909:                         $resulttext .= '<li>'.&mt('No IP Range(s) set').'</li>';
 9910:                     } else {
 9911:                         $resulttext .= '<li>'.&mt('IP Range(s): [_1]',$confhash{$itemid}{'ip'}).'</li>';
 9912:                     }
 9913:                     if (keys(%{$confhash{$itemid}{'commblocks'}})) {
 9914:                         $resulttext .= '<li>'.&mt('Functionality Blocked: [_1]',
 9915:                                                   join(', ', map { $types->{$_}; } sort(keys(%{$confhash{$itemid}{'commblocks'}})))).
 9916:                                        '</li>';
 9917:                     } else {
 9918:                         $resulttext .= '<li>'.&mt('No functionality blocked').'</li>';
 9919:                     }
 9920:                     if (keys(%{$confhash{$itemid}{'courses'}})) {
 9921:                         my @courses;
 9922:                         foreach my $cid (sort(keys(%{$confhash{$itemid}{'courses'}}))) {
 9923:                             my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
 9924:                             push(@courses,$courseinfo{'description'}.' ('.$cid.')');
 9925:                         }
 9926:                         $resulttext .= '<li>'.&mt('Courses/Communities allowed').':<ul><li>'.
 9927:                                              join('</li><li>',@courses).'</li></ul>';
 9928:                     } else {
 9929:                         $resulttext .= '<li>'.&mt('No courses allowed').'</li>';
 9930:                     }
 9931:                     $resulttext .= '</ul></li>';
 9932:                 }
 9933:             }
 9934:             $resulttext .= '</ul>';
 9935:         } else {
 9936:             $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
 9937:         }
 9938:     } else {
 9939:         $resulttext = &mt('No changes made');
 9940:     }
 9941:     if ($errors) {
 9942:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
 9943:                        $errors.'</ul></p>';
 9944:     }
 9945:     return $resulttext;
 9946: }
 9947: 
 9948: sub get_ipaccess_id {
 9949:     my ($domain,$location) = @_;
 9950:     # get lock on ipaccess db
 9951:     my $lockhash = {
 9952:                       lock => $env{'user.name'}.
 9953:                               ':'.$env{'user.domain'},
 9954:                    };
 9955:     my $tries = 0;
 9956:     my $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
 9957:     my ($id,$error);
 9958: 
 9959:     while (($gotlock ne 'ok') && ($tries<10)) {
 9960:         $tries ++;
 9961:         sleep (0.1);
 9962:         $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
 9963:     }
 9964:     if ($gotlock eq 'ok') {
 9965:         my %currids = &Apache::lonnet::dump_dom('ipaccess',$domain);
 9966:         if ($currids{'lock'}) {
 9967:             delete($currids{'lock'});
 9968:             if (keys(%currids)) {
 9969:                 my @curr = sort { $a <=> $b } keys(%currids);
 9970:                 if ($curr[-1] =~ /^\d+$/) {
 9971:                     $id = 1 + $curr[-1];
 9972:                 }
 9973:             } else {
 9974:                 $id = 1;
 9975:             }
 9976:             if ($id) {
 9977:                 unless (&Apache::lonnet::newput_dom('ipaccess',{ $id => $location },$domain) eq 'ok') {
 9978:                     $error = 'nostore';
 9979:                 }
 9980:             } else {
 9981:                 $error = 'nonumber';
 9982:             }
 9983:         }
 9984:         my $dellockoutcome = &Apache::lonnet::del_dom('ipaccess',['lock'],$domain);
 9985:     } else {
 9986:         $error = 'nolock';
 9987:     }
 9988:     return ($id,$error);
 9989: }
 9990: 
 9991: sub modify_rolecolors {
 9992:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 9993:     my ($resulttext,%rolehash);
 9994:     $rolehash{'rolecolors'} = {};
 9995:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 9996:         if ($domconfig{'rolecolors'} eq '') {
 9997:             $domconfig{'rolecolors'} = {};
 9998:         }
 9999:     }
10000:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
10001:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
10002:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
10003:                                              $dom);
10004:     if ($putresult eq 'ok') {
10005:         if (keys(%changes) > 0) {
10006:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10007:             if (ref($lastactref) eq 'HASH') {
10008:                 $lastactref->{'domainconfig'} = 1;
10009:             }
10010:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
10011:                                              $rolehash{'rolecolors'});
10012:         } else {
10013:             $resulttext = &mt('No changes made to default color schemes');
10014:         }
10015:     } else {
10016:         $resulttext = '<span class="LC_error">'.
10017: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10018:     }
10019:     if ($errors) {
10020:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
10021:                        $errors.'</ul>';
10022:     }
10023:     return $resulttext;
10024: }
10025: 
10026: sub modify_colors {
10027:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
10028:     my (%changes,%choices);
10029:     my @bgs;
10030:     my @links = ('link','alink','vlink');
10031:     my @logintext;
10032:     my @images;
10033:     my $servadm = $r->dir_config('lonAdmEMail');
10034:     my $errors;
10035:     my %defaults;
10036:     foreach my $role (@{$roles}) {
10037:         if ($role eq 'login') {
10038:             %choices = &login_choices();
10039:             @logintext = ('textcol','bgcol');
10040:         } else {
10041:             %choices = &color_font_choices();
10042:         }
10043:         if ($role eq 'login') {
10044:             @images = ('img','logo','domlogo','login');
10045:             @bgs = ('pgbg','mainbg','sidebg');
10046:         } else {
10047:             @images = ('img');
10048:             @bgs = ('pgbg','tabbg','sidebg');
10049:         }
10050:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
10051:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
10052:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
10053:         }
10054:         if ($role eq 'login') {
10055:             foreach my $item (@logintext) {
10056:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
10057:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
10058:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
10059:                 }
10060:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
10061:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
10062:                 }
10063:             }
10064:         } else {
10065:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
10066:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
10067:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
10068:             }
10069:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
10070:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
10071:             }
10072:         }
10073:         foreach my $item (@bgs) {
10074:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
10075:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
10076:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
10077:             }
10078:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
10079:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
10080:             }
10081:         }
10082:         foreach my $item (@links) {
10083:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
10084:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
10085:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
10086:             }
10087:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
10088:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
10089:             }
10090:         }
10091:         my ($configuserok,$author_ok,$switchserver) = 
10092:             &config_check($dom,$confname,$servadm);
10093:         my ($width,$height) = &thumb_dimensions();
10094:         if (ref($domconfig->{$role}) ne 'HASH') {
10095:             $domconfig->{$role} = {};
10096:         }
10097:         foreach my $img (@images) {
10098:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
10099:                 if (defined($env{'form.login_showlogo_'.$img})) {
10100:                     $confhash->{$role}{'showlogo'}{$img} = 1;
10101:                 } else { 
10102:                     $confhash->{$role}{'showlogo'}{$img} = 0;
10103:                 }
10104:             } 
10105: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
10106: 		 && !defined($domconfig->{$role}{$img})
10107: 		 && !$env{'form.'.$role.'_del_'.$img}
10108: 		 && $env{'form.'.$role.'_import_'.$img}) {
10109: 		# import the old configured image from the .tab setting
10110: 		# if they haven't provided a new one 
10111: 		$domconfig->{$role}{$img} = 
10112: 		    $env{'form.'.$role.'_import_'.$img};
10113: 	    }
10114:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
10115:                 my $error;
10116:                 if ($configuserok eq 'ok') {
10117:                     if ($switchserver) {
10118:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
10119:                     } else {
10120:                         if ($author_ok eq 'ok') {
10121:                             my ($result,$logourl) = 
10122:                                 &publishlogo($r,'upload',$role.'_'.$img,
10123:                                            $dom,$confname,$img,$width,$height);
10124:                             if ($result eq 'ok') {
10125:                                 $confhash->{$role}{$img} = $logourl;
10126:                                 $changes{$role}{'images'}{$img} = 1;
10127:                             } else {
10128:                                 $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);
10129:                             }
10130:                         } else {
10131:                             $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);
10132:                         }
10133:                     }
10134:                 } else {
10135:                     $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);
10136:                 }
10137:                 if ($error) {
10138:                     &Apache::lonnet::logthis($error);
10139:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10140:                 }
10141:             } elsif ($domconfig->{$role}{$img} ne '') {
10142:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
10143:                     my $error;
10144:                     if ($configuserok eq 'ok') {
10145: # is confname an author?
10146:                         if ($switchserver eq '') {
10147:                             if ($author_ok eq 'ok') {
10148:                                 my ($result,$logourl) = 
10149:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
10150:                                             $dom,$confname,$img,$width,$height);
10151:                                 if ($result eq 'ok') {
10152:                                     $confhash->{$role}{$img} = $logourl;
10153: 				    $changes{$role}{'images'}{$img} = 1;
10154:                                 }
10155:                             }
10156:                         }
10157:                     }
10158:                 }
10159:             }
10160:         }
10161:         if (ref($domconfig) eq 'HASH') {
10162:             if (ref($domconfig->{$role}) eq 'HASH') {
10163:                 foreach my $img (@images) {
10164:                     if ($domconfig->{$role}{$img} ne '') {
10165:                         if ($env{'form.'.$role.'_del_'.$img}) {
10166:                             $confhash->{$role}{$img} = '';
10167:                             $changes{$role}{'images'}{$img} = 1;
10168:                         } else {
10169:                             if ($confhash->{$role}{$img} eq '') {
10170:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
10171:                             }
10172:                         }
10173:                     } else {
10174:                         if ($env{'form.'.$role.'_del_'.$img}) {
10175:                             $confhash->{$role}{$img} = '';
10176:                             $changes{$role}{'images'}{$img} = 1;
10177:                         } 
10178:                     }
10179:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
10180:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
10181:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
10182:                                 $domconfig->{$role}{'showlogo'}{$img}) {
10183:                                 $changes{$role}{'showlogo'}{$img} = 1; 
10184:                             }
10185:                         } else {
10186:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
10187:                                 $changes{$role}{'showlogo'}{$img} = 1;
10188:                             }
10189:                         }
10190:                     }
10191:                 }
10192:                 if ($domconfig->{$role}{'font'} ne '') {
10193:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
10194:                         $changes{$role}{'font'} = 1;
10195:                     }
10196:                 } else {
10197:                     if ($confhash->{$role}{'font'}) {
10198:                         $changes{$role}{'font'} = 1;
10199:                     }
10200:                 }
10201:                 if ($role ne 'login') {
10202:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
10203:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
10204:                             $changes{$role}{'fontmenu'} = 1;
10205:                         }
10206:                     } else {
10207:                         if ($confhash->{$role}{'fontmenu'}) {
10208:                             $changes{$role}{'fontmenu'} = 1;
10209:                         }
10210:                     }
10211:                 }
10212:                 foreach my $item (@bgs) {
10213:                     if ($domconfig->{$role}{$item} ne '') {
10214:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10215:                             $changes{$role}{'bgs'}{$item} = 1;
10216:                         } 
10217:                     } else {
10218:                         if ($confhash->{$role}{$item}) {
10219:                             $changes{$role}{'bgs'}{$item} = 1;
10220:                         }
10221:                     }
10222:                 }
10223:                 foreach my $item (@links) {
10224:                     if ($domconfig->{$role}{$item} ne '') {
10225:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10226:                             $changes{$role}{'links'}{$item} = 1;
10227:                         }
10228:                     } else {
10229:                         if ($confhash->{$role}{$item}) {
10230:                             $changes{$role}{'links'}{$item} = 1;
10231:                         }
10232:                     }
10233:                 }
10234:                 foreach my $item (@logintext) {
10235:                     if ($domconfig->{$role}{$item} ne '') {
10236:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10237:                             $changes{$role}{'logintext'}{$item} = 1;
10238:                         }
10239:                     } else {
10240:                         if ($confhash->{$role}{$item}) {
10241:                             $changes{$role}{'logintext'}{$item} = 1;
10242:                         }
10243:                     }
10244:                 }
10245:             } else {
10246:                 &default_change_checker($role,\@images,\@links,\@bgs,
10247:                                         \@logintext,$confhash,\%changes); 
10248:             }
10249:         } else {
10250:             &default_change_checker($role,\@images,\@links,\@bgs,
10251:                                     \@logintext,$confhash,\%changes); 
10252:         }
10253:     }
10254:     return ($errors,%changes);
10255: }
10256: 
10257: sub config_check {
10258:     my ($dom,$confname,$servadm) = @_;
10259:     my ($configuserok,$author_ok,$switchserver,%currroles);
10260:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
10261:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
10262:                                                    $confname,$servadm);
10263:     if ($configuserok eq 'ok') {
10264:         $switchserver = &check_switchserver($dom,$confname);
10265:         if ($switchserver eq '') {
10266:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
10267:         }
10268:     }
10269:     return ($configuserok,$author_ok,$switchserver);
10270: }
10271: 
10272: sub default_change_checker {
10273:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
10274:     foreach my $item (@{$links}) {
10275:         if ($confhash->{$role}{$item}) {
10276:             $changes->{$role}{'links'}{$item} = 1;
10277:         }
10278:     }
10279:     foreach my $item (@{$bgs}) {
10280:         if ($confhash->{$role}{$item}) {
10281:             $changes->{$role}{'bgs'}{$item} = 1;
10282:         }
10283:     }
10284:     foreach my $item (@{$logintext}) {
10285:         if ($confhash->{$role}{$item}) {
10286:             $changes->{$role}{'logintext'}{$item} = 1;
10287:         }
10288:     }
10289:     foreach my $img (@{$images}) {
10290:         if ($env{'form.'.$role.'_del_'.$img}) {
10291:             $confhash->{$role}{$img} = '';
10292:             $changes->{$role}{'images'}{$img} = 1;
10293:         }
10294:         if ($role eq 'login') {
10295:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
10296:                 $changes->{$role}{'showlogo'}{$img} = 1;
10297:             }
10298:         }
10299:     }
10300:     if ($confhash->{$role}{'font'}) {
10301:         $changes->{$role}{'font'} = 1;
10302:     }
10303: }
10304: 
10305: sub display_colorchgs {
10306:     my ($dom,$changes,$roles,$confhash) = @_;
10307:     my (%choices,$resulttext);
10308:     if (!grep(/^login$/,@{$roles})) {
10309:         $resulttext = &mt('Changes made:').'<br />';
10310:     }
10311:     foreach my $role (@{$roles}) {
10312:         if ($role eq 'login') {
10313:             %choices = &login_choices();
10314:         } else {
10315:             %choices = &color_font_choices();
10316:         }
10317:         if (ref($changes->{$role}) eq 'HASH') {
10318:             if ($role ne 'login') {
10319:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
10320:             }
10321:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
10322:                 if ($role ne 'login') {
10323:                     $resulttext .= '<ul>';
10324:                 }
10325:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
10326:                     if ($role ne 'login') {
10327:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
10328:                     }
10329:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
10330:                         if (($role eq 'login') && ($key eq 'showlogo')) {
10331:                             if ($confhash->{$role}{$key}{$item}) {
10332:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
10333:                             } else {
10334:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
10335:                             }
10336:                         } elsif ($confhash->{$role}{$item} eq '') {
10337:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
10338:                         } else {
10339:                             my $newitem = $confhash->{$role}{$item};
10340:                             if ($key eq 'images') {
10341:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
10342:                             }
10343:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
10344:                         }
10345:                     }
10346:                     if ($role ne 'login') {
10347:                         $resulttext .= '</ul></li>';
10348:                     }
10349:                 } else {
10350:                     if ($confhash->{$role}{$key} eq '') {
10351:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
10352:                     } else {
10353:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
10354:                     }
10355:                 }
10356:                 if ($role ne 'login') {
10357:                     $resulttext .= '</ul>';
10358:                 }
10359:             }
10360:         }
10361:     }
10362:     return $resulttext;
10363: }
10364: 
10365: sub thumb_dimensions {
10366:     return ('200','50');
10367: }
10368: 
10369: sub check_dimensions {
10370:     my ($inputfile) = @_;
10371:     my ($fullwidth,$fullheight);
10372:     if ($inputfile =~ m|^[/\w.\-]+$|) {
10373:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
10374:             my $imageinfo = <PIPE>;
10375:             if (!close(PIPE)) {
10376:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
10377:             }
10378:             chomp($imageinfo);
10379:             my ($fullsize) = 
10380:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
10381:             if ($fullsize) {
10382:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
10383:             }
10384:         }
10385:     }
10386:     return ($fullwidth,$fullheight);
10387: }
10388: 
10389: sub check_configuser {
10390:     my ($uhome,$dom,$confname,$servadm) = @_;
10391:     my ($configuserok,%currroles);
10392:     if ($uhome eq 'no_host') {
10393:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
10394:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
10395:         $configuserok = 
10396:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
10397:                              $configpass,'','','','','',undef,$servadm);
10398:     } else {
10399:         $configuserok = 'ok';
10400:         %currroles = 
10401:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
10402:     }
10403:     return ($configuserok,%currroles);
10404: }
10405: 
10406: sub check_authorstatus {
10407:     my ($dom,$confname,%currroles) = @_;
10408:     my $author_ok;
10409:     if (!$currroles{':'.$dom.':au'}) {
10410:         my $start = time;
10411:         my $end = 0;
10412:         $author_ok = 
10413:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
10414:                                         'au',$end,$start,'','','domconfig');
10415:     } else {
10416:         $author_ok = 'ok';
10417:     }
10418:     return $author_ok;
10419: }
10420: 
10421: sub publishlogo {
10422:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
10423:     my ($output,$fname,$logourl);
10424:     if ($action eq 'upload') {
10425:         $fname=$env{'form.'.$formname.'.filename'};
10426:         chop($env{'form.'.$formname});
10427:     } else {
10428:         ($fname) = ($formname =~ /([^\/]+)$/);
10429:     }
10430:     if ($savefileas ne '') {
10431:         $fname = $savefileas;
10432:     }
10433:     $fname=&Apache::lonnet::clean_filename($fname);
10434: # See if there is anything left
10435:     unless ($fname) { return ('error: no uploaded file'); }
10436:     $fname="$subdir/$fname";
10437:     my $docroot=$r->dir_config('lonDocRoot');
10438:     my $filepath="$docroot/priv";
10439:     my $relpath = "$dom/$confname";
10440:     my ($fnamepath,$file,$fetchthumb);
10441:     $file=$fname;
10442:     if ($fname=~m|/|) {
10443:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
10444:     }
10445:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
10446:     my $count;
10447:     for ($count=5;$count<=$#parts;$count++) {
10448:         $filepath.="/$parts[$count]";
10449:         if ((-e $filepath)!=1) {
10450:             mkdir($filepath,02770);
10451:         }
10452:     }
10453:     # Check for bad extension and disallow upload
10454:     if ($file=~/\.(\w+)$/ &&
10455:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
10456:         $output = 
10457:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
10458:     } elsif ($file=~/\.(\w+)$/ &&
10459:         !defined(&Apache::loncommon::fileembstyle($1))) {
10460:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
10461:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
10462:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
10463:     } elsif (-d "$filepath/$file") {
10464:         $output = &mt('Filename is a directory name - rename the file and re-upload');
10465:     } else {
10466:         my $source = $filepath.'/'.$file;
10467:         my $logfile;
10468:         if (!open($logfile,">>",$source.'.log')) {
10469:             return (&mt('No write permission to Authoring Space'));
10470:         }
10471:         print $logfile
10472: "\n================= Publish ".localtime()." ================\n".
10473: $env{'user.name'}.':'.$env{'user.domain'}."\n";
10474: # Save the file
10475:         if (!open(FH,">",$source)) {
10476:             &Apache::lonnet::logthis('Failed to create '.$source);
10477:             return (&mt('Failed to create file'));
10478:         }
10479:         if ($action eq 'upload') {
10480:             if (!print FH ($env{'form.'.$formname})) {
10481:                 &Apache::lonnet::logthis('Failed to write to '.$source);
10482:                 return (&mt('Failed to write file'));
10483:             }
10484:         } else {
10485:             my $original = &Apache::lonnet::filelocation('',$formname);
10486:             if(!copy($original,$source)) {
10487:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
10488:                 return (&mt('Failed to write file'));
10489:             }
10490:         }
10491:         close(FH);
10492:         chmod(0660, $source); # Permissions to rw-rw---.
10493: 
10494:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
10495:         my $copyfile=$targetdir.'/'.$file;
10496: 
10497:         my @parts=split(/\//,$targetdir);
10498:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
10499:         for (my $count=5;$count<=$#parts;$count++) {
10500:             $path.="/$parts[$count]";
10501:             if (!-e $path) {
10502:                 print $logfile "\nCreating directory ".$path;
10503:                 mkdir($path,02770);
10504:             }
10505:         }
10506:         my $versionresult;
10507:         if (-e $copyfile) {
10508:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
10509:         } else {
10510:             $versionresult = 'ok';
10511:         }
10512:         if ($versionresult eq 'ok') {
10513:             if (copy($source,$copyfile)) {
10514:                 print $logfile "\nCopied original source to ".$copyfile."\n";
10515:                 $output = 'ok';
10516:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
10517:                 push(@{$modified_urls},[$copyfile,$source]);
10518:                 my $metaoutput = 
10519:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
10520:                 unless ($registered_cleanup) {
10521:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
10522:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
10523:                     $registered_cleanup=1;
10524:                 }
10525:             } else {
10526:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
10527:                 $output = &mt('Failed to copy file to RES space').", $!";
10528:             }
10529:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
10530:                 my $inputfile = $filepath.'/'.$file;
10531:                 my $outfile = $filepath.'/'.'tn-'.$file;
10532:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
10533:                 if ($fullwidth ne '' && $fullheight ne '') { 
10534:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
10535:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
10536:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
10537:                         system({$args[0]} @args);
10538:                         chmod(0660, $filepath.'/tn-'.$file);
10539:                         if (-e $outfile) {
10540:                             my $copyfile=$targetdir.'/tn-'.$file;
10541:                             if (copy($outfile,$copyfile)) {
10542:                                 print $logfile "\nCopied source to ".$copyfile."\n";
10543:                                 my $thumb_metaoutput = 
10544:                                     &write_metadata($dom,$confname,$formname,
10545:                                                     $targetdir,'tn-'.$file,$logfile);
10546:                                 push(@{$modified_urls},[$copyfile,$outfile]);
10547:                                 unless ($registered_cleanup) {
10548:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
10549:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
10550:                                     $registered_cleanup=1;
10551:                                 }
10552:                             } else {
10553:                                 print $logfile "\nUnable to write ".$copyfile.
10554:                                                ':'.$!."\n";
10555:                             }
10556:                         }
10557:                     }
10558:                 }
10559:             }
10560:         } else {
10561:             $output = $versionresult;
10562:         }
10563:     }
10564:     return ($output,$logourl);
10565: }
10566: 
10567: sub logo_versioning {
10568:     my ($targetdir,$file,$logfile) = @_;
10569:     my $target = $targetdir.'/'.$file;
10570:     my ($maxversion,$fn,$extn,$output);
10571:     $maxversion = 0;
10572:     if ($file =~ /^(.+)\.(\w+)$/) {
10573:         $fn=$1;
10574:         $extn=$2;
10575:     }
10576:     opendir(DIR,$targetdir);
10577:     while (my $filename=readdir(DIR)) {
10578:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
10579:             $maxversion=($1>$maxversion)?$1:$maxversion;
10580:         }
10581:     }
10582:     $maxversion++;
10583:     print $logfile "\nCreating old version ".$maxversion."\n";
10584:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
10585:     if (copy($target,$copyfile)) {
10586:         print $logfile "Copied old target to ".$copyfile."\n";
10587:         $copyfile=$copyfile.'.meta';
10588:         if (copy($target.'.meta',$copyfile)) {
10589:             print $logfile "Copied old target metadata to ".$copyfile."\n";
10590:             $output = 'ok';
10591:         } else {
10592:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
10593:             $output = &mt('Failed to copy old meta').", $!, ";
10594:         }
10595:     } else {
10596:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
10597:         $output = &mt('Failed to copy old target').", $!, ";
10598:     }
10599:     return $output;
10600: }
10601: 
10602: sub write_metadata {
10603:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
10604:     my (%metadatafields,%metadatakeys,$output);
10605:     $metadatafields{'title'}=$formname;
10606:     $metadatafields{'creationdate'}=time;
10607:     $metadatafields{'lastrevisiondate'}=time;
10608:     $metadatafields{'copyright'}='public';
10609:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
10610:                                          $env{'user.domain'};
10611:     $metadatafields{'authorspace'}=$confname.':'.$dom;
10612:     $metadatafields{'domain'}=$dom;
10613:     {
10614:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
10615:         my $mfh;
10616:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
10617:             foreach (sort(keys(%metadatafields))) {
10618:                 unless ($_=~/\./) {
10619:                     my $unikey=$_;
10620:                     $unikey=~/^([A-Za-z]+)/;
10621:                     my $tag=$1;
10622:                     $tag=~tr/A-Z/a-z/;
10623:                     print $mfh "\n\<$tag";
10624:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
10625:                         my $value=$metadatafields{$unikey.'.'.$_};
10626:                         $value=~s/\"/\'\'/g;
10627:                         print $mfh ' '.$_.'="'.$value.'"';
10628:                     }
10629:                     print $mfh '>'.
10630:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
10631:                             .'</'.$tag.'>';
10632:                 }
10633:             }
10634:             $output = 'ok';
10635:             print $logfile "\nWrote metadata";
10636:             close($mfh);
10637:         } else {
10638:             print $logfile "\nFailed to open metadata file";
10639:             $output = &mt('Could not write metadata');
10640:         }
10641:     }
10642:     return $output;
10643: }
10644: 
10645: sub notifysubscribed {
10646:     foreach my $targetsource (@{$modified_urls}){
10647:         next unless (ref($targetsource) eq 'ARRAY');
10648:         my ($target,$source)=@{$targetsource};
10649:         if ($source ne '') {
10650:             if (open(my $logfh,">>",$source.'.log')) {
10651:                 print $logfh "\nCleanup phase: Notifications\n";
10652:                 my @subscribed=&subscribed_hosts($target);
10653:                 foreach my $subhost (@subscribed) {
10654:                     print $logfh "\nNotifying host ".$subhost.':';
10655:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
10656:                     print $logfh $reply;
10657:                 }
10658:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
10659:                 foreach my $subhost (@subscribedmeta) {
10660:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
10661:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
10662:                                                         $subhost);
10663:                     print $logfh $reply;
10664:                 }
10665:                 print $logfh "\n============ Done ============\n";
10666:                 close($logfh);
10667:             }
10668:         }
10669:     }
10670:     return OK;
10671: }
10672: 
10673: sub subscribed_hosts {
10674:     my ($target) = @_;
10675:     my @subscribed;
10676:     if (open(my $fh,"<","$target.subscription")) {
10677:         while (my $subline=<$fh>) {
10678:             if ($subline =~ /^($match_lonid):/) {
10679:                 my $host = $1;
10680:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
10681:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
10682:                         push(@subscribed,$host);
10683:                     }
10684:                 }
10685:             }
10686:         }
10687:     }
10688:     return @subscribed;
10689: }
10690: 
10691: sub check_switchserver {
10692:     my ($dom,$confname) = @_;
10693:     my ($allowed,$switchserver);
10694:     my $home = &Apache::lonnet::homeserver($confname,$dom);
10695:     if ($home eq 'no_host') {
10696:         $home = &Apache::lonnet::domain($dom,'primary');
10697:     }
10698:     my @ids=&Apache::lonnet::current_machine_ids();
10699:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
10700:     if (!$allowed) {
10701: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
10702:     }
10703:     return $switchserver;
10704: }
10705: 
10706: sub modify_quotas {
10707:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
10708:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
10709:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
10710:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
10711:         $validationfieldsref);
10712:     if ($action eq 'quotas') {
10713:         $context = 'tools'; 
10714:     } else {
10715:         $context = $action;
10716:     }
10717:     if ($context eq 'requestcourses') {
10718:         @usertools = ('official','unofficial','community','textbook');
10719:         @options =('norequest','approval','validate','autolimit');
10720:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
10721:         %titles = &courserequest_titles();
10722:         $toolregexp = join('|',@usertools);
10723:         %conditions = &courserequest_conditions();
10724:         $confname = $dom.'-domainconfig';
10725:         my $servadm = $r->dir_config('lonAdmEMail');
10726:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10727:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
10728:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
10729:     } elsif ($context eq 'requestauthor') {
10730:         @usertools = ('author');
10731:         %titles = &authorrequest_titles();
10732:     } else {
10733:         @usertools = ('aboutme','blog','webdav','portfolio');
10734:         %titles = &tool_titles();
10735:     }
10736:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10737:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10738:     foreach my $key (keys(%env)) {
10739:         if ($context eq 'requestcourses') {
10740:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
10741:                 my $item = $1;
10742:                 my $type = $2;
10743:                 if ($type =~ /^limit_(.+)/) {
10744:                     $limithash{$item}{$1} = $env{$key};
10745:                 } else {
10746:                     $confhash{$item}{$type} = $env{$key};
10747:                 }
10748:             }
10749:         } elsif ($context eq 'requestauthor') {
10750:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
10751:                 $confhash{$1} = $env{$key};
10752:             }
10753:         } else {
10754:             if ($key =~ /^form\.quota_(.+)$/) {
10755:                 $confhash{'defaultquota'}{$1} = $env{$key};
10756:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
10757:                 $confhash{'authorquota'}{$1} = $env{$key};
10758:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
10759:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
10760:             }
10761:         }
10762:     }
10763:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
10764:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
10765:         @approvalnotify = sort(@approvalnotify);
10766:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
10767:         my @crstypes = ('official','unofficial','community','textbook');
10768:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
10769:         foreach my $type (@hasuniquecode) {
10770:             if (grep(/^\Q$type\E$/,@crstypes)) {
10771:                 $confhash{'uniquecode'}{$type} = 1;
10772:             }
10773:         }
10774:         my (%newbook,%allpos);
10775:         if ($context eq 'requestcourses') {
10776:             foreach my $type ('textbooks','templates') {
10777:                 @{$allpos{$type}} = (); 
10778:                 my $invalid;
10779:                 if ($type eq 'textbooks') {
10780:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
10781:                 } else {
10782:                     $invalid = &mt('Invalid LON-CAPA course for template');
10783:                 }
10784:                 if ($env{'form.'.$type.'_addbook'}) {
10785:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
10786:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
10787:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
10788:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
10789:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
10790:                         } else {
10791:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
10792:                             my $position = $env{'form.'.$type.'_addbook_pos'};
10793:                             $position =~ s/\D+//g;
10794:                             if ($position ne '') {
10795:                                 $allpos{$type}[$position] = $newbook{$type};
10796:                             }
10797:                         }
10798:                     } else {
10799:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
10800:                     }
10801:                 }
10802:             } 
10803:         }
10804:         if (ref($domconfig{$action}) eq 'HASH') {
10805:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
10806:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
10807:                     $changes{'notify'}{'approval'} = 1;
10808:                 }
10809:             } else {
10810:                 if ($confhash{'notify'}{'approval'}) {
10811:                     $changes{'notify'}{'approval'} = 1;
10812:                 }
10813:             }
10814:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
10815:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
10816:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
10817:                         unless ($confhash{'uniquecode'}{$crstype}) {
10818:                             $changes{'uniquecode'} = 1;
10819:                         }
10820:                     }
10821:                     unless ($changes{'uniquecode'}) {
10822:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
10823:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
10824:                                 $changes{'uniquecode'} = 1;
10825:                             }
10826:                         }
10827:                     }
10828:                } else {
10829:                    $changes{'uniquecode'} = 1;
10830:                }
10831:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
10832:                 $changes{'uniquecode'} = 1;
10833:             }
10834:             if ($context eq 'requestcourses') {
10835:                 foreach my $type ('textbooks','templates') {
10836:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
10837:                         my %deletions;
10838:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
10839:                         if (@todelete) {
10840:                             map { $deletions{$_} = 1; } @todelete;
10841:                         }
10842:                         my %imgdeletions;
10843:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
10844:                         if (@todeleteimages) {
10845:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
10846:                         }
10847:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
10848:                         for (my $i=0; $i<=$maxnum; $i++) {
10849:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
10850:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
10851:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
10852:                                 if ($deletions{$key}) {
10853:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
10854:                                         #FIXME need to obsolete item in RES space
10855:                                     }
10856:                                     next;
10857:                                 } else {
10858:                                     my $newpos = $env{'form.'.$itemid};
10859:                                     $newpos =~ s/\D+//g;
10860:                                     foreach my $item ('subject','title','publisher','author') {
10861:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
10862:                                                  ($type eq 'templates'));
10863:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
10864:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
10865:                                             $changes{$type}{$key} = 1;
10866:                                         }
10867:                                     }
10868:                                     $allpos{$type}[$newpos] = $key;
10869:                                 }
10870:                                 if ($imgdeletions{$key}) {
10871:                                     $changes{$type}{$key} = 1;
10872:                                     #FIXME need to obsolete item in RES space
10873:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
10874:                                     my ($cdom,$cnum) = split(/_/,$key);
10875:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
10876:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
10877:                                     } else {
10878:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
10879:                                                                                       $cdom,$cnum,$type,$configuserok,
10880:                                                                                       $switchserver,$author_ok);
10881:                                         if ($imgurl) {
10882:                                             $confhash{$type}{$key}{'image'} = $imgurl;
10883:                                             $changes{$type}{$key} = 1; 
10884:                                         }
10885:                                         if ($error) {
10886:                                             &Apache::lonnet::logthis($error);
10887:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10888:                                         }
10889:                                     } 
10890:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
10891:                                     $confhash{$type}{$key}{'image'} = 
10892:                                         $domconfig{$action}{$type}{$key}{'image'};
10893:                                 }
10894:                             }
10895:                         }
10896:                     }
10897:                 }
10898:             }
10899:         } else {
10900:             if ($confhash{'notify'}{'approval'}) {
10901:                 $changes{'notify'}{'approval'} = 1;
10902:             }
10903:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
10904:                 $changes{'uniquecode'} = 1;
10905:             }
10906:         }
10907:         if ($context eq 'requestcourses') {
10908:             foreach my $type ('textbooks','templates') {
10909:                 if ($newbook{$type}) {
10910:                     $changes{$type}{$newbook{$type}} = 1;
10911:                     foreach my $item ('subject','title','publisher','author') {
10912:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
10913:                                  ($type eq 'template'));
10914:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
10915:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
10916:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
10917:                         }
10918:                     }
10919:                     if ($type eq 'textbooks') {
10920:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
10921:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
10922:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
10923:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
10924:                             } else {
10925:                                 my ($imageurl,$error) =
10926:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
10927:                                                             $configuserok,$switchserver,$author_ok);
10928:                                 if ($imageurl) {
10929:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
10930:                                 }
10931:                                 if ($error) {
10932:                                     &Apache::lonnet::logthis($error);
10933:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10934:                                 }
10935:                             }
10936:                         }
10937:                     }
10938:                 }
10939:                 if (@{$allpos{$type}} > 0) {
10940:                     my $idx = 0;
10941:                     foreach my $item (@{$allpos{$type}}) {
10942:                         if ($item ne '') {
10943:                             $confhash{$type}{$item}{'order'} = $idx;
10944:                             if (ref($domconfig{$action}) eq 'HASH') {
10945:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
10946:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
10947:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
10948:                                             $changes{$type}{$item} = 1;
10949:                                         }
10950:                                     }
10951:                                 }
10952:                             }
10953:                             $idx ++;
10954:                         }
10955:                     }
10956:                 }
10957:             }
10958:             if (ref($validationitemsref) eq 'ARRAY') {
10959:                 foreach my $item (@{$validationitemsref}) {
10960:                     if ($item eq 'fields') {
10961:                         my @changed;
10962:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
10963:                         if (@{$confhash{'validation'}{$item}} > 0) {
10964:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
10965:                         }
10966:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10967:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10968:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
10969:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
10970:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
10971:                                 } else {
10972:                                     @changed = @{$confhash{'validation'}{$item}};
10973:                                 }
10974:                             } else {
10975:                                 @changed = @{$confhash{'validation'}{$item}};
10976:                             }
10977:                         } else {
10978:                             @changed = @{$confhash{'validation'}{$item}};
10979:                         }
10980:                         if (@changed) {
10981:                             if ($confhash{'validation'}{$item}) {
10982:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
10983:                             } else {
10984:                                 $changes{'validation'}{$item} = &mt('None');
10985:                             }
10986:                         }
10987:                     } else {
10988:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
10989:                         if ($item eq 'markup') {
10990:                             if ($env{'form.requestcourses_validation_'.$item}) {
10991:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10992:                             }
10993:                         }
10994:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10995:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10996:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
10997:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10998:                                 }
10999:                             } else {
11000:                                 if ($confhash{'validation'}{$item} ne '') {
11001:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
11002:                                 }
11003:                             }
11004:                         } else {
11005:                             if ($confhash{'validation'}{$item} ne '') {
11006:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
11007:                             }
11008:                         }
11009:                     }
11010:                 }
11011:             }
11012:             if ($env{'form.validationdc'}) {
11013:                 my $newval = $env{'form.validationdc'};
11014:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
11015:                 if (exists($domcoords{$newval})) {
11016:                     $confhash{'validation'}{'dc'} = $newval;
11017:                 }
11018:             }
11019:             if (ref($confhash{'validation'}) eq 'HASH') {
11020:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
11021:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
11022:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
11023:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
11024:                                 if ($confhash{'validation'}{'dc'} eq '') {
11025:                                     $changes{'validation'}{'dc'} = &mt('None');
11026:                                 } else {
11027:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
11028:                                 }
11029:                             }
11030:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
11031:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
11032:                         }
11033:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
11034:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
11035:                     }
11036:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
11037:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
11038:                 }
11039:             } else {
11040:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
11041:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
11042:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
11043:                             $changes{'validation'}{'dc'} = &mt('None');
11044:                         }
11045:                     }
11046:                 }
11047:             }
11048:         }
11049:     } else {
11050:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
11051:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
11052:     }
11053:     foreach my $item (@usertools) {
11054:         foreach my $type (@{$types},'default','_LC_adv') {
11055:             my $unset; 
11056:             if ($context eq 'requestcourses') {
11057:                 $unset = '0';
11058:                 if ($type eq '_LC_adv') {
11059:                     $unset = '';
11060:                 }
11061:                 if ($confhash{$item}{$type} eq 'autolimit') {
11062:                     $confhash{$item}{$type} .= '=';
11063:                     unless ($limithash{$item}{$type} =~ /\D/) {
11064:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
11065:                     }
11066:                 }
11067:             } elsif ($context eq 'requestauthor') {
11068:                 $unset = '0';
11069:                 if ($type eq '_LC_adv') {
11070:                     $unset = '';
11071:                 }
11072:             } else {
11073:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
11074:                     $confhash{$item}{$type} = 1;
11075:                 } else {
11076:                     $confhash{$item}{$type} = 0;
11077:                 }
11078:             }
11079:             if (ref($domconfig{$action}) eq 'HASH') {
11080:                 if ($action eq 'requestauthor') {
11081:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
11082:                         $changes{$type} = 1;
11083:                     }
11084:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
11085:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
11086:                         $changes{$item}{$type} = 1;
11087:                     }
11088:                 } else {
11089:                     if ($context eq 'requestcourses') {
11090:                         if ($confhash{$item}{$type} ne $unset) {
11091:                             $changes{$item}{$type} = 1;
11092:                         }
11093:                     } else {
11094:                         if (!$confhash{$item}{$type}) {
11095:                             $changes{$item}{$type} = 1;
11096:                         }
11097:                     }
11098:                 }
11099:             } else {
11100:                 if ($context eq 'requestcourses') {
11101:                     if ($confhash{$item}{$type} ne $unset) {
11102:                         $changes{$item}{$type} = 1;
11103:                     }
11104:                 } elsif ($context eq 'requestauthor') {
11105:                     if ($confhash{$type} ne $unset) {
11106:                         $changes{$type} = 1;
11107:                     }
11108:                 } else {
11109:                     if (!$confhash{$item}{$type}) {
11110:                         $changes{$item}{$type} = 1;
11111:                     }
11112:                 }
11113:             }
11114:         }
11115:     }
11116:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
11117:         if (ref($domconfig{'quotas'}) eq 'HASH') {
11118:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
11119:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
11120:                     if (exists($confhash{'defaultquota'}{$key})) {
11121:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
11122:                             $changes{'defaultquota'}{$key} = 1;
11123:                         }
11124:                     } else {
11125:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
11126:                     }
11127:                 }
11128:             } else {
11129:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
11130:                     if (exists($confhash{'defaultquota'}{$key})) {
11131:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
11132:                             $changes{'defaultquota'}{$key} = 1;
11133:                         }
11134:                     } else {
11135:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
11136:                     }
11137:                 }
11138:             }
11139:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
11140:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
11141:                     if (exists($confhash{'authorquota'}{$key})) {
11142:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
11143:                             $changes{'authorquota'}{$key} = 1;
11144:                         }
11145:                     } else {
11146:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
11147:                     }
11148:                 }
11149:             }
11150:         }
11151:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
11152:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
11153:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
11154:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
11155:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
11156:                             $changes{'defaultquota'}{$key} = 1;
11157:                         }
11158:                     } else {
11159:                         if (!exists($domconfig{'quotas'}{$key})) {
11160:                             $changes{'defaultquota'}{$key} = 1;
11161:                         }
11162:                     }
11163:                 } else {
11164:                     $changes{'defaultquota'}{$key} = 1;
11165:                 }
11166:             }
11167:         }
11168:         if (ref($confhash{'authorquota'}) eq 'HASH') {
11169:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
11170:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
11171:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
11172:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
11173:                             $changes{'authorquota'}{$key} = 1;
11174:                         }
11175:                     } else {
11176:                         $changes{'authorquota'}{$key} = 1;
11177:                     }
11178:                 } else {
11179:                     $changes{'authorquota'}{$key} = 1;
11180:                 }
11181:             }
11182:         }
11183:     }
11184: 
11185:     if ($context eq 'requestauthor') {
11186:         $domdefaults{'requestauthor'} = \%confhash;
11187:     } else {
11188:         foreach my $key (keys(%confhash)) {
11189:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
11190:                 $domdefaults{$key} = $confhash{$key};
11191:             }
11192:         }
11193:     }
11194: 
11195:     my %quotahash = (
11196:                       $action => { %confhash }
11197:                     );
11198:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
11199:                                              $dom);
11200:     if ($putresult eq 'ok') {
11201:         if (keys(%changes) > 0) {
11202:             my $cachetime = 24*60*60;
11203:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11204:             if (ref($lastactref) eq 'HASH') {
11205:                 $lastactref->{'domdefaults'} = 1;
11206:             }
11207:             $resulttext = &mt('Changes made:').'<ul>';
11208:             unless (($context eq 'requestcourses') ||
11209:                     ($context eq 'requestauthor')) {
11210:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
11211:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
11212:                     foreach my $type (@{$types},'default') {
11213:                         if (defined($changes{'defaultquota'}{$type})) {
11214:                             my $typetitle = $usertypes->{$type};
11215:                             if ($type eq 'default') {
11216:                                 $typetitle = $othertitle;
11217:                             }
11218:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
11219:                         }
11220:                     }
11221:                     $resulttext .= '</ul></li>';
11222:                 }
11223:                 if (ref($changes{'authorquota'}) eq 'HASH') {
11224:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
11225:                     foreach my $type (@{$types},'default') {
11226:                         if (defined($changes{'authorquota'}{$type})) {
11227:                             my $typetitle = $usertypes->{$type};
11228:                             if ($type eq 'default') {
11229:                                 $typetitle = $othertitle;
11230:                             }
11231:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
11232:                         }
11233:                     }
11234:                     $resulttext .= '</ul></li>';
11235:                 }
11236:             }
11237:             my %newenv;
11238:             foreach my $item (@usertools) {
11239:                 my (%haschgs,%inconf);
11240:                 if ($context eq 'requestauthor') {
11241:                     %haschgs = %changes;
11242:                     %inconf = %confhash;
11243:                 } else {
11244:                     if (ref($changes{$item}) eq 'HASH') {
11245:                         %haschgs = %{$changes{$item}};
11246:                     }
11247:                     if (ref($confhash{$item}) eq 'HASH') {
11248:                         %inconf = %{$confhash{$item}};
11249:                     }
11250:                 }
11251:                 if (keys(%haschgs) > 0) {
11252:                     my $newacc = 
11253:                         &Apache::lonnet::usertools_access($env{'user.name'},
11254:                                                           $env{'user.domain'},
11255:                                                           $item,'reload',$context);
11256:                     if (($context eq 'requestcourses') ||
11257:                         ($context eq 'requestauthor')) {
11258:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
11259:                             $newenv{'environment.canrequest.'.$item} = $newacc;
11260:                         }
11261:                     } else {
11262:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
11263:                             $newenv{'environment.availabletools.'.$item} = $newacc;
11264:                         }
11265:                     }
11266:                     unless ($context eq 'requestauthor') {
11267:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
11268:                     }
11269:                     foreach my $type (@{$types},'default','_LC_adv') {
11270:                         if ($haschgs{$type}) {
11271:                             my $typetitle = $usertypes->{$type};
11272:                             if ($type eq 'default') {
11273:                                 $typetitle = $othertitle;
11274:                             } elsif ($type eq '_LC_adv') {
11275:                                 $typetitle = 'LON-CAPA Advanced Users'; 
11276:                             }
11277:                             if ($inconf{$type}) {
11278:                                 if ($context eq 'requestcourses') {
11279:                                     my $cond;
11280:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
11281:                                         if ($1 eq '') {
11282:                                             $cond = &mt('(Automatic processing of any request).');
11283:                                         } else {
11284:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
11285:                                         }
11286:                                     } else { 
11287:                                         $cond = $conditions{$inconf{$type}};
11288:                                     }
11289:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
11290:                                 } elsif ($context eq 'requestauthor') {
11291:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
11292:                                                              $titles{$inconf{$type}},$typetitle);
11293: 
11294:                                 } else {
11295:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
11296:                                 }
11297:                             } else {
11298:                                 if ($type eq '_LC_adv') {
11299:                                     if ($inconf{$type} eq '0') {
11300:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
11301:                                     } else { 
11302:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
11303:                                     }
11304:                                 } else {
11305:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
11306:                                 }
11307:                             }
11308:                         }
11309:                     }
11310:                     unless ($context eq 'requestauthor') {
11311:                         $resulttext .= '</ul></li>';
11312:                     }
11313:                 }
11314:             }
11315:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
11316:                 if (ref($changes{'notify'}) eq 'HASH') {
11317:                     if ($changes{'notify'}{'approval'}) {
11318:                         if (ref($confhash{'notify'}) eq 'HASH') {
11319:                             if ($confhash{'notify'}{'approval'}) {
11320:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
11321:                             } else {
11322:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
11323:                             }
11324:                         }
11325:                     }
11326:                 }
11327:             }
11328:             if ($action eq 'requestcourses') {
11329:                 my @offon = ('off','on');
11330:                 if ($changes{'uniquecode'}) {
11331:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
11332:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
11333:                         $resulttext .= '<li>'.
11334:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
11335:                                        '</li>';
11336:                     } else {
11337:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
11338:                                        '</li>';
11339:                     }
11340:                 }
11341:                 foreach my $type ('textbooks','templates') {
11342:                     if (ref($changes{$type}) eq 'HASH') {
11343:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
11344:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
11345:                             my %coursehash = &Apache::lonnet::coursedescription($key);
11346:                             my $coursetitle = $coursehash{'description'};
11347:                             my $position = $confhash{$type}{$key}{'order'} + 1;
11348:                             $resulttext .= '<li>';
11349:                             foreach my $item ('subject','title','publisher','author') {
11350:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
11351:                                          ($type eq 'templates'));
11352:                                 my $name = $item.':';
11353:                                 $name =~ s/^(\w)/\U$1/;
11354:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
11355:                             }
11356:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
11357:                             if ($type eq 'textbooks') {
11358:                                 if ($confhash{$type}{$key}{'image'}) {
11359:                                     $resulttext .= ' '.&mt('Image: [_1]',
11360:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
11361:                                                    ' alt="Textbook cover" />').'<br />';
11362:                                 }
11363:                             }
11364:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
11365:                         }
11366:                         $resulttext .= '</ul></li>';
11367:                     }
11368:                 }
11369:                 if (ref($changes{'validation'}) eq 'HASH') {
11370:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
11371:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
11372:                         foreach my $item (@{$validationitemsref}) {
11373:                             if (exists($changes{'validation'}{$item})) {
11374:                                 if ($item eq 'markup') {
11375:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
11376:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
11377:                                 } else {
11378:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
11379:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
11380:                                 }
11381:                             }
11382:                         }
11383:                         if (exists($changes{'validation'}{'dc'})) {
11384:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
11385:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
11386:                         }
11387:                     }
11388:                 }
11389:             }
11390:             $resulttext .= '</ul>';
11391:             if (keys(%newenv)) {
11392:                 &Apache::lonnet::appenv(\%newenv);
11393:             }
11394:         } else {
11395:             if ($context eq 'requestcourses') {
11396:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
11397:             } elsif ($context eq 'requestauthor') {
11398:                 $resulttext = &mt('No changes made to rights to request author space.');
11399:             } else {
11400:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
11401:             }
11402:         }
11403:     } else {
11404:         $resulttext = '<span class="LC_error">'.
11405: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11406:     }
11407:     if ($errors) {
11408:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
11409:                        '<ul>'.$errors.'</ul></p>';
11410:     }
11411:     return $resulttext;
11412: }
11413: 
11414: sub process_textbook_image {
11415:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
11416:     my $filename = $env{'form.'.$caller.'.filename'};
11417:     my ($error,$url);
11418:     my ($width,$height) = (50,50);
11419:     if ($configuserok eq 'ok') {
11420:         if ($switchserver) {
11421:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
11422:                          $switchserver);
11423:         } elsif ($author_ok eq 'ok') {
11424:             my ($result,$imageurl) =
11425:                 &publishlogo($r,'upload',$caller,$dom,$confname,
11426:                              "$type/$cdom/$cnum/cover",$width,$height);
11427:             if ($result eq 'ok') {
11428:                 $url = $imageurl;
11429:             } else {
11430:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
11431:             }
11432:         } else {
11433:             $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);
11434:         }
11435:     } else {
11436:         $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);
11437:     }
11438:     return ($url,$error);
11439: }
11440: 
11441: sub modify_autoenroll {
11442:     my ($dom,$lastactref,%domconfig) = @_;
11443:     my ($resulttext,%changes);
11444:     my %currautoenroll;
11445:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
11446:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
11447:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
11448:         }
11449:     }
11450:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
11451:     my %title = ( run => 'Auto-enrollment active',
11452:                   sender => 'Sender for notification messages',
11453:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
11454:                   autofailsafe => 'Failsafe for no drops if institutional data missing for a section');
11455:     my @offon = ('off','on');
11456:     my $sender_uname = $env{'form.sender_uname'};
11457:     my $sender_domain = $env{'form.sender_domain'};
11458:     if ($sender_domain eq '') {
11459:         $sender_uname = '';
11460:     } elsif ($sender_uname eq '') {
11461:         $sender_domain = '';
11462:     }
11463:     my $coowners = $env{'form.autoassign_coowners'};
11464:     my $autofailsafe = $env{'form.autoenroll_autofailsafe'};
11465:     $autofailsafe =~ s{^\s+|\s+$}{}g;
11466:     if ($autofailsafe =~ /\D/) {
11467:         undef($autofailsafe);
11468:     }
11469:     my $failsafe = $env{'form.autoenroll_failsafe'};
11470:     unless (($failsafe eq 'zero') || ($failsafe eq 'any')) {
11471:         $failsafe = 'off';
11472:         undef($autofailsafe);
11473:     }
11474:     my %autoenrollhash =  (
11475:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
11476:                                        'sender_uname' => $sender_uname,
11477:                                        'sender_domain' => $sender_domain,
11478:                                        'co-owners' => $coowners,
11479:                                        'autofailsafe' => $autofailsafe,
11480:                                        'failsafe' => $failsafe,
11481:                                 }
11482:                      );
11483:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
11484:                                              $dom);
11485:     if ($putresult eq 'ok') {
11486:         if (exists($currautoenroll{'run'})) {
11487:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
11488:                  $changes{'run'} = 1;
11489:              }
11490:         } elsif ($autorun) {
11491:             if ($env{'form.autoenroll_run'} ne '1') {
11492:                  $changes{'run'} = 1;
11493:             }
11494:         }
11495:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
11496:             $changes{'sender'} = 1;
11497:         }
11498:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
11499:             $changes{'sender'} = 1;
11500:         }
11501:         if ($currautoenroll{'co-owners'} ne '') {
11502:             if ($currautoenroll{'co-owners'} ne $coowners) {
11503:                 $changes{'coowners'} = 1;
11504:             }
11505:         } elsif ($coowners) {
11506:             $changes{'coowners'} = 1;
11507:         }
11508:         if ($currautoenroll{'autofailsafe'} ne $autofailsafe) {
11509:             $changes{'autofailsafe'} = 1;
11510:         }
11511:         if ($currautoenroll{'failsafe'} ne $failsafe) {
11512:             $changes{'failsafe'} = 1;
11513:         }
11514:         if (keys(%changes) > 0) {
11515:             $resulttext = &mt('Changes made:').'<ul>';
11516:             if ($changes{'run'}) {
11517:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
11518:             }
11519:             if ($changes{'sender'}) {
11520:                 if ($sender_uname eq '' || $sender_domain eq '') {
11521:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
11522:                 } else {
11523:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
11524:                 }
11525:             }
11526:             if ($changes{'coowners'}) {
11527:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
11528:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
11529:                 if (ref($lastactref) eq 'HASH') {
11530:                     $lastactref->{'domainconfig'} = 1;
11531:                 }
11532:             }
11533:             if ($changes{'autofailsafe'}) {
11534:                 if ($autofailsafe ne '') {
11535:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$autofailsafe).'</li>';
11536:                 } else {
11537:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
11538:                 }
11539:             }
11540:             if ($changes{'failsafe'}) {
11541:                 if ($failsafe eq 'off') {
11542:                     unless ($changes{'autofailsafe'}) {
11543:                         $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
11544:                     }
11545:                 } elsif ($failsafe eq 'zero') {
11546:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero').'</li>';
11547:                 } else {
11548:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero or greater').'</li>';
11549:                 }
11550:             }
11551:             if (($changes{'autofailsafe'}) || ($changes{'failsafe'})) {
11552:                 &Apache::lonnet::get_domain_defaults($dom,1);
11553:                 if (ref($lastactref) eq 'HASH') {
11554:                     $lastactref->{'domdefaults'} = 1;
11555:                 }
11556:             }
11557:             $resulttext .= '</ul>';
11558:         } else {
11559:             $resulttext = &mt('No changes made to auto-enrollment settings');
11560:         }
11561:     } else {
11562:         $resulttext = '<span class="LC_error">'.
11563: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11564:     }
11565:     return $resulttext;
11566: }
11567: 
11568: sub modify_autoupdate {
11569:     my ($dom,%domconfig) = @_;
11570:     my ($resulttext,%currautoupdate,%fields,%changes);
11571:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
11572:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
11573:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
11574:         }
11575:     }
11576:     my @offon = ('off','on');
11577:     my %title = &Apache::lonlocal::texthash (
11578:                     run        => 'Auto-update:',
11579:                     classlists => 'Updates to user information in classlists?',
11580:                     unexpired  => 'Skip updates for users without active or future roles?',
11581:                     lastactive => 'Skip updates for inactive users?',
11582:                 );
11583:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11584:     my %fieldtitles = &Apache::lonlocal::texthash (
11585:                         id => 'Student/Employee ID',
11586:                         permanentemail => 'E-mail address',
11587:                         lastname => 'Last Name',
11588:                         firstname => 'First Name',
11589:                         middlename => 'Middle Name',
11590:                         generation => 'Generation',
11591:                       );
11592:     $othertitle = &mt('All users');
11593:     if (keys(%{$usertypes}) >  0) {
11594:         $othertitle = &mt('Other users');
11595:     }
11596:     foreach my $key (keys(%env)) {
11597:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
11598:             my ($usertype,$item) = ($1,$2);
11599:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
11600:                 if ($usertype eq 'default') {   
11601:                     push(@{$fields{$1}},$2);
11602:                 } elsif (ref($types) eq 'ARRAY') {
11603:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
11604:                         push(@{$fields{$1}},$2);
11605:                     }
11606:                 }
11607:             }
11608:         }
11609:     }
11610:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
11611:     @lockablenames = sort(@lockablenames);
11612:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
11613:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
11614:         if (@changed) {
11615:             $changes{'lockablenames'} = 1;
11616:         }
11617:     } else {
11618:         if (@lockablenames) {
11619:             $changes{'lockablenames'} = 1;
11620:         }
11621:     }
11622:     my %updatehash = (
11623:                       autoupdate => { run => $env{'form.autoupdate_run'},
11624:                                       classlists => $env{'form.classlists'},
11625:                                       unexpired  => $env{'form.unexpired'},
11626:                                       fields => {%fields},
11627:                                       lockablenames => \@lockablenames,
11628:                                     }
11629:                      );
11630:     my $lastactivedays;
11631:     if ($env{'form.lastactive'}) {
11632:         $lastactivedays = $env{'form.lastactivedays'};
11633:         $lastactivedays =~ s/^\s+|\s+$//g;
11634:         unless ($lastactivedays =~ /^\d+$/) {
11635:             undef($lastactivedays);
11636:             $env{'form.lastactive'} = 0;
11637:         }
11638:     }
11639:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
11640:     foreach my $key (keys(%currautoupdate)) {
11641:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
11642:             if (exists($updatehash{autoupdate}{$key})) {
11643:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
11644:                     $changes{$key} = 1;
11645:                 }
11646:             }
11647:         } elsif ($key eq 'fields') {
11648:             if (ref($currautoupdate{$key}) eq 'HASH') {
11649:                 foreach my $item (@{$types},'default') {
11650:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
11651:                         my $change = 0;
11652:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
11653:                             if (!exists($fields{$item})) {
11654:                                 $change = 1;
11655:                                 last;
11656:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
11657:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
11658:                                     $change = 1;
11659:                                     last;
11660:                                 }
11661:                             }
11662:                         }
11663:                         if ($change) {
11664:                             push(@{$changes{$key}},$item);
11665:                         }
11666:                     } 
11667:                 }
11668:             }
11669:         } elsif ($key eq 'lockablenames') {
11670:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
11671:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
11672:                 if (@changed) {
11673:                     $changes{'lockablenames'} = 1;
11674:                 }
11675:             } else {
11676:                 if (@lockablenames) {
11677:                     $changes{'lockablenames'} = 1;
11678:                 }
11679:             }
11680:         }
11681:     }
11682:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
11683:         if (@lockablenames) {
11684:             $changes{'lockablenames'} = 1;
11685:         }
11686:     }
11687:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
11688:         if ($updatehash{'autoupdate'}{'unexpired'}) {
11689:             $changes{'unexpired'} = 1;
11690:         }
11691:     }
11692:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
11693:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
11694:             $changes{'lastactive'} = 1;
11695:         }
11696:     }
11697:     foreach my $item (@{$types},'default') {
11698:         if (defined($fields{$item})) {
11699:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
11700:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
11701:                     my $change = 0;
11702:                     if (ref($fields{$item}) eq 'ARRAY') {
11703:                         foreach my $type (@{$fields{$item}}) {
11704:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
11705:                                 $change = 1;
11706:                                 last;
11707:                             }
11708:                         }
11709:                     }
11710:                     if ($change) {
11711:                         push(@{$changes{'fields'}},$item);
11712:                     }
11713:                 } else {
11714:                     push(@{$changes{'fields'}},$item);
11715:                 }
11716:             } else {
11717:                 push(@{$changes{'fields'}},$item);
11718:             }
11719:         }
11720:     }
11721:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
11722:                                              $dom);
11723:     if ($putresult eq 'ok') {
11724:         if (keys(%changes) > 0) {
11725:             $resulttext = &mt('Changes made:').'<ul>';
11726:             foreach my $key (sort(keys(%changes))) {
11727:                 if ($key eq 'lockablenames') {
11728:                     $resulttext .= '<li>';
11729:                     if (@lockablenames) {
11730:                         $usertypes->{'default'} = $othertitle;
11731:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
11732:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
11733:                     } else {
11734:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
11735:                     }
11736:                     $resulttext .= '</li>';
11737:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
11738:                     foreach my $item (@{$changes{$key}}) {
11739:                         my @newvalues;
11740:                         foreach my $type (@{$fields{$item}}) {
11741:                             push(@newvalues,$fieldtitles{$type});
11742:                         }
11743:                         my $newvaluestr;
11744:                         if (@newvalues > 0) {
11745:                             $newvaluestr = join(', ',@newvalues);
11746:                         } else {
11747:                             $newvaluestr = &mt('none');
11748:                         }
11749:                         if ($item eq 'default') {
11750:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
11751:                         } else {
11752:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
11753:                         }
11754:                     }
11755:                 } else {
11756:                     my $newvalue;
11757:                     if ($key eq 'run') {
11758:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
11759:                     } elsif ($key eq 'lastactive') {
11760:                         $newvalue = $offon[$env{'form.lastactive'}];
11761:                         unless ($lastactivedays eq '') {
11762:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
11763:                         }
11764:                     } else {
11765:                         $newvalue = $offon[$env{'form.'.$key}];
11766:                     }
11767:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
11768:                 }
11769:             }
11770:             $resulttext .= '</ul>';
11771:         } else {
11772:             $resulttext = &mt('No changes made to autoupdates');
11773:         }
11774:     } else {
11775:         $resulttext = '<span class="LC_error">'.
11776: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11777:     }
11778:     return $resulttext;
11779: }
11780: 
11781: sub modify_autocreate {
11782:     my ($dom,%domconfig) = @_;
11783:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
11784:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
11785:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
11786:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
11787:         }
11788:     }
11789:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
11790:                  req => 'Auto-creation of validated requests for official courses',
11791:                  xmldc => 'Identity of course creator of courses from XML files',
11792:                );
11793:     my @types = ('xml','req');
11794:     foreach my $item (@types) {
11795:         $newvals{$item} = $env{'form.autocreate_'.$item};
11796:         $newvals{$item} =~ s/\D//g;
11797:         $newvals{$item} = 0 if ($newvals{$item} eq '');
11798:     }
11799:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
11800:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
11801:     unless (exists($domcoords{$newvals{'xmldc'}})) {
11802:         $newvals{'xmldc'} = '';
11803:     } 
11804:     %autocreatehash =  (
11805:                         autocreate => { xml => $newvals{'xml'},
11806:                                         req => $newvals{'req'},
11807:                                       }
11808:                        );
11809:     if ($newvals{'xmldc'} ne '') {
11810:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
11811:     }
11812:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
11813:                                              $dom);
11814:     if ($putresult eq 'ok') {
11815:         my @items = @types;
11816:         if ($newvals{'xml'}) {
11817:             push(@items,'xmldc');
11818:         }
11819:         foreach my $item (@items) {
11820:             if (exists($currautocreate{$item})) {
11821:                 if ($currautocreate{$item} ne $newvals{$item}) {
11822:                     $changes{$item} = 1;
11823:                 }
11824:             } elsif ($newvals{$item}) {
11825:                 $changes{$item} = 1;
11826:             }
11827:         }
11828:         if (keys(%changes) > 0) {
11829:             my @offon = ('off','on'); 
11830:             $resulttext = &mt('Changes made:').'<ul>';
11831:             foreach my $item (@types) {
11832:                 if ($changes{$item}) {
11833:                     my $newtxt = $offon[$newvals{$item}];
11834:                     $resulttext .= '<li>'.
11835:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
11836:                                        '<b>','</b>').
11837:                                    '</li>';
11838:                 }
11839:             }
11840:             if ($changes{'xmldc'}) {
11841:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
11842:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
11843:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
11844:             }
11845:             $resulttext .= '</ul>';
11846:         } else {
11847:             $resulttext = &mt('No changes made to auto-creation settings');
11848:         }
11849:     } else {
11850:         $resulttext = '<span class="LC_error">'.
11851:             &mt('An error occurred: [_1]',$putresult).'</span>';
11852:     }
11853:     return $resulttext;
11854: }
11855: 
11856: sub modify_directorysrch {
11857:     my ($dom,$lastactref,%domconfig) = @_;
11858:     my ($resulttext,%changes);
11859:     my %currdirsrch;
11860:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
11861:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
11862:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
11863:         }
11864:     }
11865:     my %title = ( available => 'Institutional directory search available',
11866:                   localonly => 'Other domains can search institution',
11867:                   lcavailable => 'LON-CAPA directory search available',
11868:                   lclocalonly => 'Other domains can search LON-CAPA domain',
11869:                   searchby => 'Search types',
11870:                   searchtypes => 'Search latitude');
11871:     my @offon = ('off','on');
11872:     my @otherdoms = ('Yes','No');
11873: 
11874:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
11875:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
11876:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
11877: 
11878:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11879:     if (keys(%{$usertypes}) == 0) {
11880:         @cansearch = ('default');
11881:     } else {
11882:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
11883:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
11884:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
11885:                     push(@{$changes{'cansearch'}},$type);
11886:                 }
11887:             }
11888:             foreach my $type (@cansearch) {
11889:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
11890:                     push(@{$changes{'cansearch'}},$type);
11891:                 }
11892:             }
11893:         } else {
11894:             push(@{$changes{'cansearch'}},@cansearch);
11895:         }
11896:     }
11897: 
11898:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
11899:         foreach my $by (@{$currdirsrch{'searchby'}}) {
11900:             if (!grep(/^\Q$by\E$/,@searchby)) {
11901:                 push(@{$changes{'searchby'}},$by);
11902:             }
11903:         }
11904:         foreach my $by (@searchby) {
11905:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
11906:                 push(@{$changes{'searchby'}},$by);
11907:             }
11908:         }
11909:     } else {
11910:         push(@{$changes{'searchby'}},@searchby);
11911:     }
11912: 
11913:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
11914:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
11915:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
11916:                 push(@{$changes{'searchtypes'}},$type);
11917:             }
11918:         }
11919:         foreach my $type (@searchtypes) {
11920:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
11921:                 push(@{$changes{'searchtypes'}},$type);
11922:             }
11923:         }
11924:     } else {
11925:         if (exists($currdirsrch{'searchtypes'})) {
11926:             foreach my $type (@searchtypes) {  
11927:                 if ($type ne $currdirsrch{'searchtypes'}) { 
11928:                     push(@{$changes{'searchtypes'}},$type);
11929:                 }
11930:             }
11931:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
11932:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
11933:             }   
11934:         } else {
11935:             push(@{$changes{'searchtypes'}},@searchtypes); 
11936:         }
11937:     }
11938: 
11939:     my %dirsrch_hash =  (
11940:             directorysrch => { available => $env{'form.dirsrch_available'},
11941:                                cansearch => \@cansearch,
11942:                                localonly => $env{'form.dirsrch_instlocalonly'},
11943:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
11944:                                lcavailable => $env{'form.dirsrch_domavailable'},
11945:                                searchby => \@searchby,
11946:                                searchtypes => \@searchtypes,
11947:                              }
11948:             );
11949:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
11950:                                              $dom);
11951:     if ($putresult eq 'ok') {
11952:         if (exists($currdirsrch{'available'})) {
11953:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
11954:                  $changes{'available'} = 1;
11955:              }
11956:         } else {
11957:             if ($env{'form.dirsrch_available'} eq '1') {
11958:                 $changes{'available'} = 1;
11959:             }
11960:         }
11961:         if (exists($currdirsrch{'lcavailable'})) {
11962:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
11963:                 $changes{'lcavailable'} = 1;
11964:             }
11965:         } else {
11966:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
11967:                 $changes{'lcavailable'} = 1;
11968:             }
11969:         }
11970:         if (exists($currdirsrch{'localonly'})) {
11971:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
11972:                 $changes{'localonly'} = 1;
11973:             }
11974:         } else {
11975:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
11976:                 $changes{'localonly'} = 1;
11977:             }
11978:         }
11979:         if (exists($currdirsrch{'lclocalonly'})) {
11980:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
11981:                 $changes{'lclocalonly'} = 1;
11982:             }
11983:         } else {
11984:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
11985:                 $changes{'lclocalonly'} = 1;
11986:             }
11987:         }
11988:         if (keys(%changes) > 0) {
11989:             $resulttext = &mt('Changes made:').'<ul>';
11990:             if ($changes{'available'}) {
11991:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
11992:             }
11993:             if ($changes{'lcavailable'}) {
11994:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
11995:             }
11996:             if ($changes{'localonly'}) {
11997:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
11998:             }
11999:             if ($changes{'lclocalonly'}) {
12000:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
12001:             }
12002:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
12003:                 my $chgtext;
12004:                 if (ref($usertypes) eq 'HASH') {
12005:                     if (keys(%{$usertypes}) > 0) {
12006:                         foreach my $type (@{$types}) {
12007:                             if (grep(/^\Q$type\E$/,@cansearch)) {
12008:                                 $chgtext .= $usertypes->{$type}.'; ';
12009:                             }
12010:                         }
12011:                         if (grep(/^default$/,@cansearch)) {
12012:                             $chgtext .= $othertitle;
12013:                         } else {
12014:                             $chgtext =~ s/\; $//;
12015:                         }
12016:                         $resulttext .=
12017:                             '<li>'.
12018:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
12019:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
12020:                             '</li>';
12021:                     }
12022:                 }
12023:             }
12024:             if (ref($changes{'searchby'}) eq 'ARRAY') {
12025:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
12026:                 my $chgtext;
12027:                 foreach my $type (@{$titleorder}) {
12028:                     if (grep(/^\Q$type\E$/,@searchby)) {
12029:                         if (defined($searchtitles->{$type})) {
12030:                             $chgtext .= $searchtitles->{$type}.'; ';
12031:                         }
12032:                     }
12033:                 }
12034:                 $chgtext =~ s/\; $//;
12035:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
12036:             }
12037:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
12038:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
12039:                 my $chgtext;
12040:                 foreach my $type (@{$srchtypeorder}) {
12041:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
12042:                         if (defined($srchtypes_desc->{$type})) {
12043:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
12044:                         }
12045:                     }
12046:                 }
12047:                 $chgtext =~ s/\; $//;
12048:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
12049:             }
12050:             $resulttext .= '</ul>';
12051:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
12052:             if (ref($lastactref) eq 'HASH') {
12053:                 $lastactref->{'directorysrch'} = 1;
12054:             }
12055:         } else {
12056:             $resulttext = &mt('No changes made to directory search settings');
12057:         }
12058:     } else {
12059:         $resulttext = '<span class="LC_error">'.
12060:                       &mt('An error occurred: [_1]',$putresult).'</span>';
12061:     }
12062:     return $resulttext;
12063: }
12064: 
12065: sub modify_contacts {
12066:     my ($dom,$lastactref,%domconfig) = @_;
12067:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
12068:     if (ref($domconfig{'contacts'}) eq 'HASH') {
12069:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
12070:             $currsetting{$key} = $domconfig{'contacts'}{$key};
12071:         }
12072:     }
12073:     my (%others,%to,%bcc,%includestr,%includeloc);
12074:     my @contacts = ('supportemail','adminemail');
12075:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
12076:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
12077:     my @toggles = ('reporterrors','reportupdates','reportstatus');
12078:     my @lonstatus = ('threshold','sysmail','weights','excluded');
12079:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
12080:     foreach my $type (@mailings) {
12081:         @{$newsetting{$type}} = 
12082:             &Apache::loncommon::get_env_multiple('form.'.$type);
12083:         foreach my $item (@contacts) {
12084:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
12085:                 $contacts_hash{contacts}{$type}{$item} = 1;
12086:             } else {
12087:                 $contacts_hash{contacts}{$type}{$item} = 0;
12088:             }
12089:         }
12090:         $others{$type} = $env{'form.'.$type.'_others'};
12091:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
12092:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12093:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
12094:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
12095:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
12096:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
12097:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
12098:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
12099:             }
12100:         }
12101:     }
12102:     foreach my $item (@contacts) {
12103:         $to{$item} = $env{'form.'.$item};
12104:         $contacts_hash{'contacts'}{$item} = $to{$item};
12105:     }
12106:     foreach my $item (@toggles) {
12107:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
12108:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
12109:         }
12110:     }
12111:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
12112:     foreach my $item (@lonstatus) {
12113:         if ($item eq 'excluded') {
12114:             my (%serverhomes,@excluded);
12115:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
12116:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
12117:             if (@possexcluded) {
12118:                 foreach my $id (sort(@possexcluded)) {
12119:                     if ($serverhomes{$id}) {
12120:                         push(@excluded,$id);
12121:                     }
12122:                 }
12123:             }
12124:             if (@excluded) {
12125:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
12126:             }
12127:         } elsif ($item eq 'weights') {
12128:             foreach my $type ('E','W','N','U') {
12129:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
12130:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
12131:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
12132:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
12133:                             $env{'form.error'.$item.'_'.$type};
12134:                     }
12135:                 }
12136:             }
12137:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
12138:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
12139:             if ($env{'form.error'.$item} =~ /^\d+$/) {
12140:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
12141:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
12142:                 }
12143:             }
12144:         }
12145:     }
12146:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
12147:         foreach my $field (@{$fields}) {
12148:             if (ref($possoptions->{$field}) eq 'ARRAY') {
12149:                 my $value = $env{'form.helpform_'.$field};
12150:                 $value =~ s/^\s+|\s+$//g;
12151:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
12152:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
12153:                     if ($field eq 'screenshot') {
12154:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
12155:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
12156:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
12157:                         }
12158:                     }
12159:                 }
12160:             }
12161:         }
12162:     }
12163:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12164:     my (@statuses,%usertypeshash,@overrides);
12165:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
12166:         @statuses = @{$types};
12167:         if (ref($usertypes) eq 'HASH') {
12168:             %usertypeshash = %{$usertypes};
12169:         }
12170:     }
12171:     if (@statuses) {
12172:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
12173:         foreach my $type (@possoverrides) {
12174:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
12175:                 push(@overrides,$type);
12176:             }
12177:         }
12178:         if (@overrides) {
12179:             foreach my $type (@overrides) {
12180:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
12181:                 foreach my $item (@contacts) {
12182:                     if (grep(/^\Q$item\E$/,@standard)) {
12183:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
12184:                         $newsetting{'override_'.$type}{$item} = 1;
12185:                     } else {
12186:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
12187:                         $newsetting{'override_'.$type}{$item} = 0;
12188:                     }
12189:                 }
12190:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
12191:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
12192:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
12193:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
12194:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
12195:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
12196:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
12197:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
12198:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
12199:                 }
12200:             }    
12201:         }
12202:     }
12203:     if (keys(%currsetting) > 0) {
12204:         foreach my $item (@contacts) {
12205:             if ($to{$item} ne $currsetting{$item}) {
12206:                 $changes{$item} = 1;
12207:             }
12208:         }
12209:         foreach my $type (@mailings) {
12210:             foreach my $item (@contacts) {
12211:                 if (ref($currsetting{$type}) eq 'HASH') {
12212:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
12213:                         push(@{$changes{$type}},$item);
12214:                     }
12215:                 } else {
12216:                     push(@{$changes{$type}},@{$newsetting{$type}});
12217:                 }
12218:             }
12219:             if ($others{$type} ne $currsetting{$type}{'others'}) {
12220:                 push(@{$changes{$type}},'others');
12221:             }
12222:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12223:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
12224:                     push(@{$changes{$type}},'bcc'); 
12225:                 }
12226:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
12227:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
12228:                     push(@{$changes{$type}},'include');
12229:                 }
12230:             }
12231:         }
12232:         if (ref($fields) eq 'ARRAY') {
12233:             if (ref($currsetting{'helpform'}) eq 'HASH') {
12234:                 foreach my $field (@{$fields}) {
12235:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
12236:                         push(@{$changes{'helpform'}},$field);
12237:                     }
12238:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
12239:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
12240:                             push(@{$changes{'helpform'}},'maxsize');
12241:                         }
12242:                     }
12243:                 }
12244:             } else {
12245:                 foreach my $field (@{$fields}) {
12246:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
12247:                         push(@{$changes{'helpform'}},$field);
12248:                     }
12249:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
12250:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
12251:                             push(@{$changes{'helpform'}},'maxsize');
12252:                         }
12253:                     }
12254:                 }
12255:             }
12256:         }
12257:         if (@statuses) {
12258:             if (ref($currsetting{'overrides'}) eq 'HASH') {
12259:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
12260:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
12261:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
12262:                             foreach my $item (@contacts,'bcc','others','include') {
12263:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
12264:                                     push(@{$changes{'overrides'}},$key);
12265:                                     last;
12266:                                 }
12267:                             }
12268:                         } else {
12269:                             push(@{$changes{'overrides'}},$key);
12270:                         }
12271:                     }
12272:                 }
12273:                 foreach my $key (@overrides) {
12274:                     unless (exists($currsetting{'overrides'}{$key})) {
12275:                         push(@{$changes{'overrides'}},$key);
12276:                     }
12277:                 }
12278:             } else {
12279:                 foreach my $key (@overrides) {
12280:                     push(@{$changes{'overrides'}},$key);
12281:                 }
12282:             }
12283:         }
12284:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
12285:             foreach my $key ('excluded','weights','threshold','sysmail') {
12286:                 if ($key eq 'excluded') {
12287:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
12288:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
12289:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
12290:                             (@{$currsetting{'lonstatus'}{$key}})) {
12291:                             my @diffs =
12292:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
12293:                                                                    $currsetting{'lonstatus'}{$key});
12294:                             if (@diffs) {
12295:                                 push(@{$changes{'lonstatus'}},$key);
12296:                             }
12297:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
12298:                             push(@{$changes{'lonstatus'}},$key);
12299:                         }
12300:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
12301:                              (@{$currsetting{'lonstatus'}{$key}})) {
12302:                         push(@{$changes{'lonstatus'}},$key);
12303:                     }
12304:                 } elsif ($key eq 'weights') {
12305:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
12306:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
12307:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
12308:                             foreach my $type ('E','W','N','U') {
12309:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
12310:                                         $currsetting{'lonstatus'}{$key}{$type}) {
12311:                                     push(@{$changes{'lonstatus'}},$key);
12312:                                     last;
12313:                                 }
12314:                             }
12315:                         } else {
12316:                             foreach my $type ('E','W','N','U') {
12317:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
12318:                                     push(@{$changes{'lonstatus'}},$key);
12319:                                     last;
12320:                                 }
12321:                             }
12322:                         }
12323:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
12324:                         foreach my $type ('E','W','N','U') {
12325:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
12326:                                 push(@{$changes{'lonstatus'}},$key);
12327:                                 last;
12328:                             }
12329:                         }
12330:                     }
12331:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
12332:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
12333:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
12334:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
12335:                                 push(@{$changes{'lonstatus'}},$key);
12336:                             }
12337:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
12338:                             push(@{$changes{'lonstatus'}},$key);
12339:                         }
12340:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
12341:                         push(@{$changes{'lonstatus'}},$key);
12342:                     }
12343:                 }
12344:             }
12345:         } else {
12346:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
12347:                 foreach my $key ('excluded','weights','threshold','sysmail') {
12348:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
12349:                         push(@{$changes{'lonstatus'}},$key);
12350:                     }
12351:                 }
12352:             }
12353:         }
12354:     } else {
12355:         my %default;
12356:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
12357:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
12358:         $default{'errormail'} = 'adminemail';
12359:         $default{'packagesmail'} = 'adminemail';
12360:         $default{'helpdeskmail'} = 'supportemail';
12361:         $default{'otherdomsmail'} = 'supportemail';
12362:         $default{'lonstatusmail'} = 'adminemail';
12363:         $default{'requestsmail'} = 'adminemail';
12364:         $default{'updatesmail'} = 'adminemail';
12365:         $default{'hostipmail'} = 'adminemail';
12366:         foreach my $item (@contacts) {
12367:            if ($to{$item} ne $default{$item}) {
12368:                $changes{$item} = 1;
12369:            }
12370:         }
12371:         foreach my $type (@mailings) {
12372:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
12373:                 push(@{$changes{$type}},@{$newsetting{$type}});
12374:             }
12375:             if ($others{$type} ne '') {
12376:                 push(@{$changes{$type}},'others');
12377:             }
12378:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12379:                 if ($bcc{$type} ne '') {
12380:                     push(@{$changes{$type}},'bcc');
12381:                 }
12382:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
12383:                     push(@{$changes{$type}},'include');
12384:                 }
12385:             }
12386:         }
12387:         if (ref($fields) eq 'ARRAY') {
12388:             foreach my $field (@{$fields}) {
12389:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
12390:                     push(@{$changes{'helpform'}},$field);
12391:                 }
12392:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
12393:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
12394:                         push(@{$changes{'helpform'}},'maxsize');
12395:                     }
12396:                 }
12397:             }
12398:         }
12399:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
12400:             foreach my $key ('excluded','weights','threshold','sysmail') {
12401:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
12402:                     push(@{$changes{'lonstatus'}},$key);
12403:                 }
12404:             }
12405:         }
12406:     }
12407:     foreach my $item (@toggles) {
12408:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
12409:             $changes{$item} = 1;
12410:         } elsif ((!$env{'form.'.$item}) &&
12411:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
12412:             $changes{$item} = 1;
12413:         }
12414:     }
12415:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
12416:                                              $dom);
12417:     if ($putresult eq 'ok') {
12418:         if (keys(%changes) > 0) {
12419:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12420:             if (ref($lastactref) eq 'HASH') {
12421:                 $lastactref->{'domainconfig'} = 1;
12422:             }
12423:             my ($titles,$short_titles)  = &contact_titles();
12424:             $resulttext = &mt('Changes made:').'<ul>';
12425:             foreach my $item (@contacts) {
12426:                 if ($changes{$item}) {
12427:                     $resulttext .= '<li>'.$titles->{$item}.
12428:                                     &mt(' set to: ').
12429:                                     '<span class="LC_cusr_emph">'.
12430:                                     $to{$item}.'</span></li>';
12431:                 }
12432:             }
12433:             foreach my $type (@mailings) {
12434:                 if (ref($changes{$type}) eq 'ARRAY') {
12435:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12436:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
12437:                     } else {
12438:                         $resulttext .= '<li>'.$titles->{$type}.': ';
12439:                     }
12440:                     my @text;
12441:                     foreach my $item (@{$newsetting{$type}}) {
12442:                         push(@text,$short_titles->{$item});
12443:                     }
12444:                     if ($others{$type} ne '') {
12445:                         push(@text,$others{$type});
12446:                     }
12447:                     if (@text) {
12448:                         $resulttext .= '<span class="LC_cusr_emph">'.
12449:                                        join(', ',@text).'</span>';
12450:                     }
12451:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12452:                         if ($bcc{$type} ne '') {
12453:                             my $bcctext;
12454:                             if (@text) {
12455:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
12456:                             } else {
12457:                                 $bcctext = '(Bcc)';
12458:                             }
12459:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
12460:                         } elsif (!@text) {
12461:                             $resulttext .= &mt('No one');
12462:                         }
12463:                         if ($includestr{$type} ne '') {
12464:                             if ($includeloc{$type} eq 'b') {
12465:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
12466:                             } elsif ($includeloc{$type} eq 's') {
12467:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
12468:                             }
12469:                         }
12470:                     } elsif (!@text) {
12471:                         $resulttext .= &mt('No recipients');
12472:                     }
12473:                     $resulttext .= '</li>';
12474:                 }
12475:             }
12476:             if (ref($changes{'overrides'}) eq 'ARRAY') {
12477:                 my @deletions;
12478:                 foreach my $type (@{$changes{'overrides'}}) {
12479:                     if ($usertypeshash{$type}) {
12480:                         if (grep(/^\Q$type\E/,@overrides)) {
12481:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
12482:                                                       $usertypeshash{$type}).'<ul><li>';
12483:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
12484:                                 my @text;
12485:                                 foreach my $item (@contacts) {
12486:                                     if ($newsetting{'override_'.$type}{$item}) {
12487:                                         push(@text,$short_titles->{$item});
12488:                                     }
12489:                                 }
12490:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
12491:                                     push(@text,$newsetting{'override_'.$type}{'others'});
12492:                                 }
12493: 
12494:                                 if (@text) {
12495:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
12496:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
12497:                                 }
12498:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
12499:                                     my $bcctext;
12500:                                     if (@text) {
12501:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
12502:                                     } else {
12503:                                         $bcctext = '(Bcc)';
12504:                                     }
12505:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
12506:                                 } elsif (!@text) {
12507:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
12508:                                 }
12509:                                 $resulttext .= '</li>';
12510:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
12511:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
12512:                                     if ($loc eq 'b') {
12513:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
12514:                                     } elsif ($loc eq 's') {
12515:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
12516:                                     }
12517:                                 }
12518:                             }
12519:                             $resulttext .= '</li></ul></li>';
12520:                         } else {
12521:                             push(@deletions,$usertypeshash{$type});
12522:                         }
12523:                     }
12524:                 }
12525:                 if (@deletions) {
12526:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
12527:                                               join(', ',@deletions)).'</li>';
12528:                 }
12529:             }
12530:             my @offon = ('off','on');
12531:             my $corelink = &core_link_msu();
12532:             if ($changes{'reporterrors'}) {
12533:                 $resulttext .= '<li>'.
12534:                                &mt('E-mail error reports to [_1] set to "'.
12535:                                    $offon[$env{'form.reporterrors'}].'".',
12536:                                    $corelink).
12537:                                '</li>';
12538:             }
12539:             if ($changes{'reportupdates'}) {
12540:                 $resulttext .= '<li>'.
12541:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
12542:                                     $offon[$env{'form.reportupdates'}].'".',
12543:                                     $corelink).
12544:                                 '</li>';
12545:             }
12546:             if ($changes{'reportstatus'}) {
12547:                 $resulttext .= '<li>'.
12548:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
12549:                                     $offon[$env{'form.reportstatus'}].'".',
12550:                                     $corelink).
12551:                                 '</li>';
12552:             }
12553:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
12554:                 $resulttext .= '<li>'.
12555:                                &mt('Nightly status check e-mail settings').':<ul>';
12556:                 my (%defval,%use_def,%shown);
12557:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
12558:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
12559:                 $defval{'weights'} =
12560:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
12561:                 $defval{'excluded'} = &mt('None');
12562:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
12563:                     foreach my $item ('threshold','sysmail','weights','excluded') {
12564:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
12565:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
12566:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
12567:                             } elsif ($item eq 'weights') {
12568:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
12569:                                     foreach my $type ('E','W','N','U') {
12570:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
12571:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
12572:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
12573:                                         } else {
12574:                                             $shown{$item} .= $lonstatus_defs->{$type};
12575:                                         }
12576:                                         $shown{$item} .= ', ';
12577:                                     }
12578:                                     $shown{$item} =~ s/, $//;
12579:                                 } else {
12580:                                     $shown{$item} = $defval{$item};
12581:                                 }
12582:                             } elsif ($item eq 'excluded') {
12583:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
12584:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
12585:                                 } else {
12586:                                     $shown{$item} = $defval{$item};
12587:                                 }
12588:                             }
12589:                         } else {
12590:                             $shown{$item} = $defval{$item};
12591:                         }
12592:                     }
12593:                 } else {
12594:                     foreach my $item ('threshold','weights','excluded','sysmail') {
12595:                         $shown{$item} = $defval{$item};
12596:                     }
12597:                 }
12598:                 foreach my $item ('threshold','weights','excluded','sysmail') {
12599:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
12600:                                           $shown{$item}).'</li>';
12601:                 }
12602:                 $resulttext .= '</ul></li>';
12603:             }
12604:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
12605:                 my (@optional,@required,@unused,$maxsizechg);
12606:                 foreach my $field (@{$changes{'helpform'}}) {
12607:                     if ($field eq 'maxsize') {
12608:                         $maxsizechg = 1;
12609:                         next;
12610:                     }
12611:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
12612:                         push(@optional,$field);
12613:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
12614:                         push(@unused,$field);
12615:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
12616:                         push(@required,$field);
12617:                     }
12618:                 }
12619:                 if (@optional) {
12620:                     $resulttext .= '<li>'.
12621:                                    &mt('Help form fields changed to "Optional": [_1].',
12622:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
12623:                                    '</li>';
12624:                 }
12625:                 if (@required) {
12626:                     $resulttext .= '<li>'.
12627:                                    &mt('Help form fields changed to "Required": [_1].',
12628:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
12629:                                    '</li>';
12630:                 }
12631:                 if (@unused) {
12632:                     $resulttext .= '<li>'.
12633:                                    &mt('Help form fields changed to "Not shown": [_1].',
12634:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
12635:                                    '</li>';
12636:                 }
12637:                 if ($maxsizechg) {
12638:                     $resulttext .= '<li>'.
12639:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
12640:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
12641:                                    '</li>';
12642:                 }
12643:             }
12644:             $resulttext .= '</ul>';
12645:         } else {
12646:             $resulttext = &mt('No changes made to contacts and form settings');
12647:         }
12648:     } else {
12649:         $resulttext = '<span class="LC_error">'.
12650:             &mt('An error occurred: [_1].',$putresult).'</span>';
12651:     }
12652:     return $resulttext;
12653: }
12654: 
12655: sub modify_passwords {
12656:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12657:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
12658:         $updatedefaults,$updateconf);
12659:     my $customfn = 'resetpw.html';
12660:     if (ref($domconfig{'passwords'}) eq 'HASH') {
12661:         %current = %{$domconfig{'passwords'}};
12662:     }
12663:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12664:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12665:     if (ref($types) eq 'ARRAY') {
12666:         @oktypes = @{$types};
12667:     }
12668:     push(@oktypes,'default');
12669: 
12670:     my %titles = &Apache::lonlocal::texthash (
12671:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
12672:         intauth_check  => 'Check bcrypt cost if authenticated',
12673:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
12674:         permanent      => 'Permanent e-mail address',
12675:         critical       => 'Critical notification address',
12676:         notify         => 'Notification address',
12677:         min            => 'Minimum password length',
12678:         max            => 'Maximum password length',
12679:         chars          => 'Required characters',
12680:         numsaved       => 'Number of previous passwords to save',
12681:         reset          => 'Resetting Forgotten Password',
12682:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
12683:         rules          => 'Rules for LON-CAPA Passwords',
12684:         crsownerchg    => 'Course Owner Changing Student Passwords',
12685:         username       => 'Username',
12686:         email          => 'E-mail address',
12687:     );
12688: 
12689: #
12690: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
12691: #
12692:     my (%curr_defaults,%save_defaults);
12693:     if (ref($domconfig{'defaults'}) eq 'HASH') {
12694:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
12695:             if ($key =~ /^intauth_(cost|check|switch)$/) {
12696:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
12697:             } else {
12698:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
12699:             }
12700:         }
12701:     }
12702:     my %staticdefaults = (
12703:         'resetlink'      => 2,
12704:         'resetcase'      => \@oktypes,
12705:         'resetprelink'   => 'both',
12706:         'resetemail'     => ['critical','notify','permanent'],
12707:         'intauth_cost'   => 10,
12708:         'intauth_check'  => 0,
12709:         'intauth_switch' => 0,
12710:     );
12711:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
12712:     foreach my $type (@oktypes) {
12713:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
12714:     }
12715:     my $linklife = $env{'form.passwords_link'};
12716:     $linklife =~ s/^\s+|\s+$//g;
12717:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
12718:         $newvalues{'resetlink'} = $linklife;
12719:         if ($current{'resetlink'}) {
12720:             if ($current{'resetlink'} ne $linklife) {
12721:                 $changes{'reset'} = 1;
12722:             }
12723:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12724:             if ($staticdefaults{'resetlink'} ne $linklife) {
12725:                 $changes{'reset'} = 1;
12726:             }
12727:         }
12728:     } elsif ($current{'resetlink'}) {
12729:         $changes{'reset'} = 1;
12730:     }
12731:     my @casesens;
12732:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
12733:     foreach my $case (sort(@posscase)) {
12734:         if (grep(/^\Q$case\E$/,@oktypes)) {
12735:             push(@casesens,$case);
12736:         }
12737:     }
12738:     $newvalues{'resetcase'} = \@casesens;
12739:     if (ref($current{'resetcase'}) eq 'ARRAY') {
12740:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
12741:         if (@diffs > 0) {
12742:             $changes{'reset'} = 1;
12743:         }
12744:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12745:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
12746:         if (@diffs > 0) {
12747:             $changes{'reset'} = 1;
12748:         }
12749:     }
12750:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
12751:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
12752:         if (exists($current{'resetprelink'})) {
12753:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
12754:                 $changes{'reset'} = 1;
12755:             }
12756:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12757:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
12758:                 $changes{'reset'} = 1;
12759:             }
12760:         }
12761:     } elsif ($current{'resetprelink'}) {
12762:         $changes{'reset'} = 1;
12763:     }
12764:     foreach my $type (@oktypes) {
12765:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
12766:         my @postlink;
12767:         foreach my $item (sort(@possplink)) {
12768:             if ($item =~ /^(email|username)$/) {
12769:                 push(@postlink,$item);
12770:             }
12771:         }
12772:         $newvalues{'resetpostlink'}{$type} = \@postlink;
12773:         unless ($changes{'reset'}) {
12774:             if (ref($current{'resetpostlink'}) eq 'HASH') {
12775:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
12776:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
12777:                     if (@diffs > 0) {
12778:                         $changes{'reset'} = 1;
12779:                     }
12780:                 } else {
12781:                     $changes{'reset'} = 1;
12782:                 }
12783:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12784:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
12785:                 if (@diffs > 0) {
12786:                     $changes{'reset'} = 1;
12787:                 }
12788:             }
12789:         }
12790:     }
12791:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
12792:     my @resetemail;
12793:     foreach my $item (sort(@possemailsrc)) {
12794:         if ($item =~ /^(permanent|critical|notify)$/) {
12795:             push(@resetemail,$item);
12796:         }
12797:     }
12798:     $newvalues{'resetemail'} = \@resetemail;
12799:     unless ($changes{'reset'}) {
12800:         if (ref($current{'resetemail'}) eq 'ARRAY') {
12801:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
12802:             if (@diffs > 0) {
12803:                 $changes{'reset'} = 1;
12804:             }
12805:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12806:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
12807:             if (@diffs > 0) {
12808:                 $changes{'reset'} = 1;
12809:             }
12810:         }
12811:     }
12812:     if ($env{'form.passwords_stdtext'} == 0) {
12813:         $newvalues{'resetremove'} = 1;
12814:         unless ($current{'resetremove'}) {
12815:             $changes{'reset'} = 1;
12816:         }
12817:     } elsif ($current{'resetremove'}) {
12818:         $changes{'reset'} = 1;
12819:     }
12820:     if ($env{'form.passwords_customfile.filename'} ne '') {
12821:         my $servadm = $r->dir_config('lonAdmEMail');
12822:         my $servadm = $r->dir_config('lonAdmEMail');
12823:         my ($configuserok,$author_ok,$switchserver) =
12824:             &config_check($dom,$confname,$servadm);
12825:         my $error;
12826:         if ($configuserok eq 'ok') {
12827:             if ($switchserver) {
12828:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
12829:             } else {
12830:                 if ($author_ok eq 'ok') {
12831:                     my ($result,$customurl) =
12832:                         &publishlogo($r,'upload','passwords_customfile',$dom,
12833:                                      $confname,'customtext/resetpw','','',$customfn);
12834:                     if ($result eq 'ok') {
12835:                         $newvalues{'resetcustom'} = $customurl;
12836:                         $changes{'reset'} = 1;
12837:                     } else {
12838:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
12839:                     }
12840:                 } else {
12841:                     $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].",$customfn,$confname,$dom,$author_ok);
12842:                 }
12843:             }
12844:         } else {
12845:             $error = &mt("Upload of [_1] failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$customfn,$confname,$dom,$configuserok);
12846:         }
12847:         if ($error) {
12848:             &Apache::lonnet::logthis($error);
12849:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12850:         }
12851:     } elsif ($current{'resetcustom'}) {
12852:         if ($env{'form.passwords_custom_del'}) {
12853:             $changes{'reset'} = 1;
12854:         } else {
12855:             $newvalues{'resetcustom'} = $current{'resetcustom'};
12856:         }
12857:     }
12858:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
12859:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
12860:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
12861:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
12862:             $changes{'intauth'} = 1;
12863:         }
12864:     } else {
12865:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
12866:     }
12867:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
12868:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
12869:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
12870:             $changes{'intauth'} = 1;
12871:         }
12872:     } else {
12873:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
12874:     }
12875:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
12876:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
12877:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
12878:             $changes{'intauth'} = 1;
12879:         }
12880:     } else {
12881:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
12882:     }
12883:     foreach my $item ('cost','check','switch') {
12884:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
12885:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
12886:             $updatedefaults = 1;
12887:         }
12888:     }
12889:     foreach my $rule ('min','max','numsaved') {
12890:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
12891:         my $ruleok;
12892:         if ($rule eq 'min') {
12893:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
12894:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
12895:                     $ruleok = 1;
12896:                 }
12897:             }
12898:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
12899:                  ($env{'form.passwords_'.$rule} ne '0')) {
12900:             $ruleok = 1;
12901:         }
12902:         if ($ruleok) {
12903:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
12904:             if (exists($current{$rule})) {
12905:                 if ($newvalues{$rule} ne $current{$rule}) {
12906:                     $changes{'rules'} = 1;
12907:                 }
12908:             } elsif ($rule eq 'min') {
12909:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
12910:                     $changes{'rules'} = 1;
12911:                 }
12912:             } else {
12913:                 $changes{'rules'} = 1;
12914:             }
12915:         } elsif (exists($current{$rule})) {
12916:             $changes{'rules'} = 1;
12917:         }
12918:     }
12919:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
12920:     my @chars;
12921:     foreach my $item (sort(@posschars)) {
12922:         if ($item =~ /^(uc|lc|num|spec)$/) {
12923:             push(@chars,$item);
12924:         }
12925:     }
12926:     $newvalues{'chars'} = \@chars;
12927:     unless ($changes{'rules'}) {
12928:         if (ref($current{'chars'}) eq 'ARRAY') {
12929:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
12930:             if (@diffs > 0) {
12931:                 $changes{'rules'} = 1;
12932:             }
12933:         } else {
12934:             if (@chars > 0) {
12935:                 $changes{'rules'} = 1;
12936:             }
12937:         }
12938:     }
12939:     my %crsownerchg = (
12940:                         by => [],
12941:                         for => [],
12942:                       );
12943:     foreach my $item ('by','for') {
12944:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
12945:         foreach my $type (sort(@posstypes)) {
12946:             if (grep(/^\Q$type\E$/,@oktypes)) {
12947:                 push(@{$crsownerchg{$item}},$type);
12948:             }
12949:         }
12950:     }
12951:     $newvalues{'crsownerchg'} = \%crsownerchg;
12952:     if (ref($current{'crsownerchg'}) eq 'HASH') {
12953:         foreach my $item ('by','for') {
12954:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
12955:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
12956:                 if (@diffs > 0) {
12957:                     $changes{'crsownerchg'} = 1;
12958:                     last;
12959:                 }
12960:             }
12961:         }
12962:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
12963:         foreach my $item ('by','for') {
12964:             if (@{$crsownerchg{$item}} > 0) {
12965:                 $changes{'crsownerchg'} = 1;
12966:                 last;
12967:             }
12968:         }
12969:     }
12970: 
12971:     my %confighash = (
12972:                         defaults  => \%save_defaults,
12973:                         passwords => \%newvalues,
12974:                      );
12975:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
12976: 
12977:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
12978:     if ($putresult eq 'ok') {
12979:         if (keys(%changes) > 0) {
12980:             $resulttext = &mt('Changes made: ').'<ul>';
12981:             foreach my $key ('reset','intauth','rules','crsownerchg') {
12982:                 if ($changes{$key}) {
12983:                     unless ($key eq 'intauth') {
12984:                         $updateconf = 1;
12985:                     }
12986:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
12987:                     if ($key eq 'reset') {
12988:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
12989:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
12990:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
12991:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
12992:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
12993:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
12994:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
12995:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
12996:                             }
12997:                         } else {
12998:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
12999:                         }
13000:                         if ($confighash{'passwords'}{'resetlink'}) {
13001:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
13002:                         } else {
13003:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
13004:                                                   &mt('Will default to 2 hours').'</li>';
13005:                         }
13006:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
13007:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
13008:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
13009:                             } else {
13010:                                 my $casesens;
13011:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
13012:                                     if ($type eq 'default') {
13013:                                         $casesens .= $othertitle.', ';
13014:                                     } elsif ($usertypes->{$type} ne '') {
13015:                                         $casesens .= $usertypes->{$type}.', ';
13016:                                     }
13017:                                 }
13018:                                 $casesens =~ s/\Q, \E$//;
13019:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
13020:                             }
13021:                         } else {
13022:                             $resulttext .= '<li>'.&mt('Case-sensitivity not set for "Forgot Password" web form').' '.&mt('Will default to case-sensitive for username and/or e-mail address for all').'</li>';
13023:                         }
13024:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
13025:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
13026:                         } else {
13027:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
13028:                         }
13029:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
13030:                             my $output;
13031:                             if (ref($types) eq 'ARRAY') {
13032:                                 foreach my $type (@{$types}) {
13033:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
13034:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
13035:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
13036:                                         } else {
13037:                                             $output .= $usertypes->{$type}.' -- '.
13038:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
13039:                                         }
13040:                                     }
13041:                                 }
13042:                             }
13043:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
13044:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
13045:                                     $output .= $othertitle.' -- '.&mt('none');
13046:                                 } else {
13047:                                     $output .= $othertitle.' -- '.
13048:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
13049:                                 }
13050:                             }
13051:                             if ($output) {
13052:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
13053:                             } else {
13054:                                 $resulttext .= '<li>'.&mt('Information required for new password form not set.').' '.&mt('Will default to requiring both the username and an e-mail address').'</li>';
13055:                             }
13056:                         } else {
13057:                             $resulttext .= '<li>'.&mt('Information required for new password form not set.').' '.&mt('Will default to requiring both the username and an e-mail address').'</li>';
13058:                         }
13059:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
13060:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
13061:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
13062:                             } else {
13063:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
13064:                             }
13065:                         } else {
13066:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
13067:                         }
13068:                         if ($confighash{'passwords'}{'resetremove'}) {
13069:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
13070:                         } else {
13071:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
13072:                         }
13073:                         if ($confighash{'passwords'}{'resetcustom'}) {
13074:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
13075:                                                                             &mt('custom text'),600,500,undef,undef,
13076:                                                                             undef,undef,'background-color:#ffffff');
13077:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
13078:                         } else {
13079:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
13080:                         }
13081:                     } elsif ($key eq 'intauth') {
13082:                         foreach my $item ('cost','switch','check') {
13083:                             my $value = $save_defaults{$key.'_'.$item};
13084:                             if ($item eq 'switch') {
13085:                                 my %optiondesc = &Apache::lonlocal::texthash (
13086:                                                      0 => 'No',
13087:                                                      1 => 'Yes',
13088:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
13089:                                                  );
13090:                                 if ($value =~ /^(0|1|2)$/) {
13091:                                     $value = $optiondesc{$value};
13092:                                 } else {
13093:                                     $value = &mt('none -- defaults to No');
13094:                                 }
13095:                             } elsif ($item eq 'check') {
13096:                                 my %optiondesc = &Apache::lonlocal::texthash (
13097:                                                      0 => 'No',
13098:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
13099:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
13100:                                                  );
13101:                                 if ($value =~ /^(0|1|2)$/) {
13102:                                     $value = $optiondesc{$value};
13103:                                 } else {
13104:                                     $value = &mt('none -- defaults to No');
13105:                                 }
13106:                             }
13107:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
13108:                         }
13109:                     } elsif ($key eq 'rules') {
13110:                         foreach my $rule ('min','max','numsaved') {
13111:                             if ($confighash{'passwords'}{$rule} eq '') {
13112:                                 if ($rule eq 'min') {
13113:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
13114:                                                    ' '.&mt('Default of [_1] will be used',
13115:                                                            $Apache::lonnet::passwdmin).'</li>';
13116:                                 } else {
13117:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
13118:                                 }
13119:                             } else {
13120:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
13121:                             }
13122:                         }
13123:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
13124:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
13125:                                 my %rulenames = &Apache::lonlocal::texthash(
13126:                                                      uc => 'At least one upper case letter',
13127:                                                      lc => 'At least one lower case letter',
13128:                                                      num => 'At least one number',
13129:                                                      spec => 'At least one non-alphanumeric',
13130:                                                    );
13131:                                 my $needed = '<ul><li>'.
13132:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
13133:                                              '</li></ul>';
13134:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
13135:                             } else {
13136:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
13137:                             }
13138:                         } else {
13139:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
13140:                         }
13141:                     } elsif ($key eq 'crsownerchg') {
13142:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
13143:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
13144:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
13145:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
13146:                             } else {
13147:                                 my %crsownerstr;
13148:                                 foreach my $item ('by','for') {
13149:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
13150:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
13151:                                             if ($type eq 'default') {
13152:                                                 $crsownerstr{$item} .= $othertitle.', ';
13153:                                             } elsif ($usertypes->{$type} ne '') {
13154:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
13155:                                             }
13156:                                         }
13157:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
13158:                                     }
13159:                                 }
13160:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
13161:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
13162:                             }
13163:                         } else {
13164:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
13165:                         }
13166:                     }
13167:                     $resulttext .= '</ul></li>';
13168:                 }
13169:             }
13170:             $resulttext .= '</ul>';
13171:         } else {
13172:             $resulttext = &mt('No changes made to password settings');
13173:         }
13174:         my $cachetime = 24*60*60;
13175:         if ($updatedefaults) {
13176:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13177:             if (ref($lastactref) eq 'HASH') {
13178:                 $lastactref->{'domdefaults'} = 1;
13179:             }
13180:         }
13181:         if ($updateconf) {
13182:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
13183:             if (ref($lastactref) eq 'HASH') {
13184:                 $lastactref->{'passwdconf'} = 1;
13185:             }
13186:         }
13187:     } else {
13188:         $resulttext = '<span class="LC_error">'.
13189:             &mt('An error occurred: [_1]',$putresult).'</span>';
13190:     }
13191:     if ($errors) {
13192:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
13193:                        $errors.'</ul></p>';
13194:     }
13195:     return $resulttext;
13196: }
13197: 
13198: sub modify_usercreation {
13199:     my ($dom,%domconfig) = @_;
13200:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
13201:     my $warningmsg;
13202:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
13203:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
13204:             if ($key eq 'cancreate') {
13205:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
13206:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
13207:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
13208:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13209:                         } else {
13210:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13211:                         }
13212:                     }
13213:                 }
13214:             } elsif ($key eq 'email_rule') {
13215:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
13216:             } else {
13217:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
13218:             }
13219:         }
13220:     }
13221:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
13222:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
13223:     my @contexts = ('author','course','requestcrs');
13224:     foreach my $item(@contexts) {
13225:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
13226:     }
13227:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13228:         foreach my $item (@contexts) {
13229:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
13230:                 push(@{$changes{'cancreate'}},$item);
13231:             }
13232:         }
13233:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
13234:         foreach my $item (@contexts) {
13235:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
13236:                 if ($cancreate{$item} ne 'any') {
13237:                     push(@{$changes{'cancreate'}},$item);
13238:                 }
13239:             } else {
13240:                 if ($cancreate{$item} ne 'none') {
13241:                     push(@{$changes{'cancreate'}},$item);
13242:                 }
13243:             }
13244:         }
13245:     } else {
13246:         foreach my $item (@contexts)  {
13247:             push(@{$changes{'cancreate'}},$item);
13248:         }
13249:     }
13250: 
13251:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
13252:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
13253:             if (!grep(/^\Q$type\E$/,@username_rule)) {
13254:                 push(@{$changes{'username_rule'}},$type);
13255:             }
13256:         }
13257:         foreach my $type (@username_rule) {
13258:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
13259:                 push(@{$changes{'username_rule'}},$type);
13260:             }
13261:         }
13262:     } else {
13263:         push(@{$changes{'username_rule'}},@username_rule);
13264:     }
13265: 
13266:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
13267:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
13268:             if (!grep(/^\Q$type\E$/,@id_rule)) {
13269:                 push(@{$changes{'id_rule'}},$type);
13270:             }
13271:         }
13272:         foreach my $type (@id_rule) {
13273:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
13274:                 push(@{$changes{'id_rule'}},$type);
13275:             }
13276:         }
13277:     } else {
13278:         push(@{$changes{'id_rule'}},@id_rule);
13279:     }
13280: 
13281:     my @authen_contexts = ('author','course','domain');
13282:     my @authtypes = ('int','krb4','krb5','loc');
13283:     my %authhash;
13284:     foreach my $item (@authen_contexts) {
13285:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
13286:         foreach my $auth (@authtypes) {
13287:             if (grep(/^\Q$auth\E$/,@authallowed)) {
13288:                 $authhash{$item}{$auth} = 1;
13289:             } else {
13290:                 $authhash{$item}{$auth} = 0;
13291:             }
13292:         }
13293:     }
13294:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
13295:         foreach my $item (@authen_contexts) {
13296:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
13297:                 foreach my $auth (@authtypes) {
13298:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
13299:                         push(@{$changes{'authtypes'}},$item);
13300:                         last;
13301:                     }
13302:                 }
13303:             }
13304:         }
13305:     } else {
13306:         foreach my $item (@authen_contexts) {
13307:             push(@{$changes{'authtypes'}},$item);
13308:         }
13309:     }
13310: 
13311:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
13312:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
13313:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
13314:     $save_usercreate{'id_rule'} = \@id_rule;
13315:     $save_usercreate{'username_rule'} = \@username_rule,
13316:     $save_usercreate{'authtypes'} = \%authhash;
13317: 
13318:     my %usercreation_hash =  (
13319:         usercreation     => \%save_usercreate,
13320:     );
13321: 
13322:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
13323:                                              $dom);
13324: 
13325:     if ($putresult eq 'ok') {
13326:         if (keys(%changes) > 0) {
13327:             $resulttext = &mt('Changes made:').'<ul>';
13328:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
13329:                 my %lt = &usercreation_types();
13330:                 foreach my $type (@{$changes{'cancreate'}}) {
13331:                     my $chgtext = $lt{$type}.', ';
13332:                     if ($cancreate{$type} eq 'none') {
13333:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
13334:                     } elsif ($cancreate{$type} eq 'any') {
13335:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
13336:                     } elsif ($cancreate{$type} eq 'official') {
13337:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
13338:                     } elsif ($cancreate{$type} eq 'unofficial') {
13339:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
13340:                     }
13341:                     $resulttext .= '<li>'.$chgtext.'</li>';
13342:                 }
13343:             }
13344:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
13345:                 my ($rules,$ruleorder) = 
13346:                     &Apache::lonnet::inst_userrules($dom,'username');
13347:                 my $chgtext = '<ul>';
13348:                 foreach my $type (@username_rule) {
13349:                     if (ref($rules->{$type}) eq 'HASH') {
13350:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
13351:                     }
13352:                 }
13353:                 $chgtext .= '</ul>';
13354:                 if (@username_rule > 0) {
13355:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
13356:                 } else {
13357:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
13358:                 }
13359:             }
13360:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
13361:                 my ($idrules,$idruleorder) = 
13362:                     &Apache::lonnet::inst_userrules($dom,'id');
13363:                 my $chgtext = '<ul>';
13364:                 foreach my $type (@id_rule) {
13365:                     if (ref($idrules->{$type}) eq 'HASH') {
13366:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
13367:                     }
13368:                 }
13369:                 $chgtext .= '</ul>';
13370:                 if (@id_rule > 0) {
13371:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
13372:                 } else {
13373:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
13374:                 }
13375:             }
13376:             my %authname = &authtype_names();
13377:             my %context_title = &context_names();
13378:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
13379:                 my $chgtext = '<ul>';
13380:                 foreach my $type (@{$changes{'authtypes'}}) {
13381:                     my @allowed;
13382:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
13383:                     foreach my $auth (@authtypes) {
13384:                         if ($authhash{$type}{$auth}) {
13385:                             push(@allowed,$authname{$auth});
13386:                         }
13387:                     }
13388:                     if (@allowed > 0) {
13389:                         $chgtext .= join(', ',@allowed).'</li>';
13390:                     } else {
13391:                         $chgtext .= &mt('none').'</li>';
13392:                     }
13393:                 }
13394:                 $chgtext .= '</ul>';
13395:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
13396:                 $resulttext .= '</li>';
13397:             }
13398:             $resulttext .= '</ul>';
13399:         } else {
13400:             $resulttext = &mt('No changes made to user creation settings');
13401:         }
13402:     } else {
13403:         $resulttext = '<span class="LC_error">'.
13404:             &mt('An error occurred: [_1]',$putresult).'</span>';
13405:     }
13406:     if ($warningmsg ne '') {
13407:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
13408:     }
13409:     return $resulttext;
13410: }
13411: 
13412: sub modify_selfcreation {
13413:     my ($dom,$lastactref,%domconfig) = @_;
13414:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
13415:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
13416:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13417:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
13418:     if (ref($typesref) eq 'ARRAY') {
13419:         @types = @{$typesref};
13420:     }
13421:     if (ref($usertypesref) eq 'HASH') {
13422:         %usertypes = %{$usertypesref};
13423:     }
13424:     $usertypes{'default'} = $othertitle;
13425: #
13426: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
13427: #
13428:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
13429:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
13430:             if ($key eq 'cancreate') {
13431:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
13432:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
13433:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
13434:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
13435:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
13436:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
13437:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
13438:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
13439:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13440:                         } else {
13441:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13442:                         }
13443:                     }
13444:                 }
13445:             } elsif ($key eq 'email_rule') {
13446:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
13447:             } else {
13448:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
13449:             }
13450:         }
13451:     }
13452: #
13453: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
13454: #
13455:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
13456:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
13457:             if ($key eq 'selfcreate') {
13458:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
13459:             } else {
13460:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
13461:             }
13462:         }
13463:     }
13464: #
13465: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
13466: #
13467:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
13468:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
13469:             if ($key eq 'inststatusguest') {
13470:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
13471:             } else {
13472:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
13473:             }
13474:         }
13475:     }
13476: 
13477:     my @contexts = ('selfcreate');
13478:     @{$cancreate{'selfcreate'}} = ();
13479:     %{$cancreate{'emailusername'}} = ();
13480:     if (@types) {
13481:         @{$cancreate{'statustocreate'}} = ();
13482:     }
13483:     %{$cancreate{'selfcreateprocessing'}} = ();
13484:     %{$cancreate{'shibenv'}} = ();
13485:     %{$cancreate{'emailverified'}} = ();
13486:     %{$cancreate{'emailoptions'}} = ();
13487:     %{$cancreate{'emaildomain'}} = ();
13488:     my %selfcreatetypes = (
13489:                              sso   => 'users authenticated by institutional single sign on',
13490:                              login => 'users authenticated by institutional log-in',
13491:                              email => 'users verified by e-mail',
13492:                           );
13493: #
13494: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
13495: # is permitted.
13496: #
13497: 
13498:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
13499: 
13500:     my (@statuses,%email_rule);
13501:     foreach my $item ('login','sso','email') {
13502:         if ($item eq 'email') {
13503:             if ($env{'form.cancreate_email'}) {
13504:                 if (@types) {
13505:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
13506:                     foreach my $status (@poss_statuses) {
13507:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
13508:                             push(@statuses,$status);
13509:                         }
13510:                     }
13511:                     $save_inststatus{'inststatusguest'} = \@statuses;
13512:                 } else {
13513:                     push(@statuses,'default');
13514:                 }
13515:                 if (@statuses) {
13516:                     my %curr_rule;
13517:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
13518:                         foreach my $type (@statuses) {
13519:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
13520:                         }
13521:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
13522:                         foreach my $type (@statuses) {
13523:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
13524:                         }
13525:                     }
13526:                     push(@{$cancreate{'selfcreate'}},'email');
13527:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
13528:                     my %curremaildom;
13529:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
13530:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
13531:                     }
13532:                     foreach my $type (@statuses) {
13533:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
13534:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
13535:                         }
13536:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
13537:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
13538:                         }
13539:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
13540: #
13541: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
13542: #
13543:                             my $chosen = $1;
13544:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
13545:                                 my $emaildom;
13546:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
13547:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
13548:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
13549:                                     if (ref($curremaildom{$type}) eq 'HASH') {
13550:                                         if (exists($curremaildom{$type}{$chosen})) {
13551:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
13552:                                                 push(@{$changes{'cancreate'}},'emaildomain');
13553:                                             }
13554:                                         } elsif ($emaildom ne '') {
13555:                                             push(@{$changes{'cancreate'}},'emaildomain');
13556:                                         }
13557:                                     } elsif ($emaildom ne '') {
13558:                                         push(@{$changes{'cancreate'}},'emaildomain');
13559:                                     }
13560:                                 }
13561:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
13562:                             } elsif ($chosen eq 'custom') {
13563:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
13564:                                 $email_rule{$type} = [];
13565:                                 if (ref($emailrules) eq 'HASH') {
13566:                                     foreach my $rule (@possemail_rules) {
13567:                                         if (exists($emailrules->{$rule})) {
13568:                                             push(@{$email_rule{$type}},$rule);
13569:                                         }
13570:                                     }
13571:                                 }
13572:                                 if (@{$email_rule{$type}}) {
13573:                                     $cancreate{'emailoptions'}{$type} = 'custom';
13574:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
13575:                                         if (@{$curr_rule{$type}} > 0) {
13576:                                             foreach my $rule (@{$curr_rule{$type}}) {
13577:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
13578:                                                     push(@{$changes{'email_rule'}},$type);
13579:                                                 }
13580:                                             }
13581:                                         }
13582:                                         foreach my $type (@{$email_rule{$type}}) {
13583:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
13584:                                                 push(@{$changes{'email_rule'}},$type);
13585:                                             }
13586:                                         }
13587:                                     } else {
13588:                                         push(@{$changes{'email_rule'}},$type);
13589:                                     }
13590:                                 }
13591:                             } else {
13592:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
13593:                             }
13594:                         }
13595:                     }
13596:                     if (@types) {
13597:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13598:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
13599:                             if (@changed) {
13600:                                 push(@{$changes{'inststatus'}},'inststatusguest');
13601:                             }
13602:                         } else {
13603:                             push(@{$changes{'inststatus'}},'inststatusguest');
13604:                         }
13605:                     }
13606:                 } else {
13607:                     delete($env{'form.cancreate_email'});
13608:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13609:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
13610:                             push(@{$changes{'inststatus'}},'inststatusguest');
13611:                         }
13612:                     }
13613:                 }
13614:             } else {
13615:                 $save_inststatus{'inststatusguest'} = [];
13616:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13617:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
13618:                         push(@{$changes{'inststatus'}},'inststatusguest');
13619:                     }
13620:                 }
13621:             }
13622:         } else {
13623:             if ($env{'form.cancreate_'.$item}) {
13624:                 push(@{$cancreate{'selfcreate'}},$item);
13625:             }
13626:         }
13627:     }
13628:     my (%userinfo,%savecaptcha);
13629:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
13630: #
13631: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
13632: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
13633: #
13634: 
13635:     if ($env{'form.cancreate_email'}) {
13636:         push(@contexts,'emailusername');
13637:         if (@statuses) {
13638:             foreach my $type (@statuses) {
13639:                 if (ref($infofields) eq 'ARRAY') {
13640:                     foreach my $field (@{$infofields}) {
13641:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
13642:                             $cancreate{'emailusername'}{$type}{$field} = $1;
13643:                         }
13644:                     }
13645:                 }
13646:             }
13647:         }
13648: #
13649: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
13650: # queued requests for self-creation of account verified by e-mail.
13651: #
13652: 
13653:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
13654:         @approvalnotify = sort(@approvalnotify);
13655:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
13656:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13657:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
13658:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
13659:                     push(@{$changes{'cancreate'}},'notify');
13660:                 }
13661:             } else {
13662:                 if ($cancreate{'notify'}{'approval'}) {
13663:                     push(@{$changes{'cancreate'}},'notify');
13664:                 }
13665:             }
13666:         } elsif ($cancreate{'notify'}{'approval'}) {
13667:             push(@{$changes{'cancreate'}},'notify');
13668:         }
13669: 
13670:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
13671:     }
13672: #  
13673: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
13674: # institutional log-in.
13675: #
13676:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
13677:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
13678:                ($domdefaults{'auth_def'} eq 'localauth'))) {
13679:             $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.').' '.
13680:                           &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.');
13681:         }
13682:     }
13683:     my @fields = ('lastname','firstname','middlename','generation',
13684:                   'permanentemail','id');
13685:     my @shibfields = (@fields,'inststatus');
13686:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13687: #
13688: # Where usernames may created for institutional log-in and/or institutional single sign on:
13689: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
13690: # may self-create accounts 
13691: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
13692: # which the user may supply, if institutional data is unavailable.
13693: #
13694:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
13695:         if (@types) {
13696:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
13697:             push(@contexts,'statustocreate');
13698:             foreach my $type (@types) {
13699:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
13700:                 foreach my $field (@fields) {
13701:                     if (grep(/^\Q$field\E$/,@modifiable)) {
13702:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
13703:                     } else {
13704:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
13705:                     }
13706:                 }
13707:             }
13708:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
13709:                 foreach my $type (@types) {
13710:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
13711:                         foreach my $field (@fields) {
13712:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
13713:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
13714:                                 push(@{$changes{'selfcreate'}},$type);
13715:                                 last;
13716:                             }
13717:                         }
13718:                     }
13719:                 }
13720:             } else {
13721:                 foreach my $type (@types) {
13722:                     push(@{$changes{'selfcreate'}},$type);
13723:                 }
13724:             }
13725:         }
13726:         foreach my $field (@shibfields) {
13727:             if ($env{'form.shibenv_'.$field} ne '') {
13728:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
13729:             }
13730:         }
13731:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13732:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
13733:                 foreach my $field (@shibfields) {
13734:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
13735:                         push(@{$changes{'cancreate'}},'shibenv');
13736:                     }
13737:                 }
13738:             } else {
13739:                 foreach my $field (@shibfields) {
13740:                     if ($env{'form.shibenv_'.$field}) {
13741:                         push(@{$changes{'cancreate'}},'shibenv');
13742:                         last;
13743:                     }
13744:                 }
13745:             }
13746:         }
13747:     }
13748:     foreach my $item (@contexts) {
13749:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
13750:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
13751:                 if (ref($cancreate{$item}) eq 'ARRAY') {
13752:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
13753:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13754:                             push(@{$changes{'cancreate'}},$item);
13755:                         }
13756:                     }
13757:                 }
13758:             }
13759:             if (ref($cancreate{$item}) eq 'ARRAY') {
13760:                 foreach my $type (@{$cancreate{$item}}) {
13761:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
13762:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13763:                             push(@{$changes{'cancreate'}},$item);
13764:                         }
13765:                     }
13766:                 }
13767:             }
13768:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
13769:             if (ref($cancreate{$item}) eq 'HASH') {
13770:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
13771:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
13772:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
13773:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
13774:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13775:                                     push(@{$changes{'cancreate'}},$item);
13776:                                 }
13777:                             }
13778:                         }
13779:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13780:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
13781:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13782:                                 push(@{$changes{'cancreate'}},$item);
13783:                             }
13784:                         }
13785:                     }
13786:                 }
13787:                 foreach my $type (keys(%{$cancreate{$item}})) {
13788:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
13789:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
13790:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
13791:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
13792:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13793:                                         push(@{$changes{'cancreate'}},$item);
13794:                                     }
13795:                                 }
13796:                             } else {
13797:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13798:                                     push(@{$changes{'cancreate'}},$item);
13799:                                 }
13800:                             }
13801:                         }
13802:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13803:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
13804:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13805:                                 push(@{$changes{'cancreate'}},$item);
13806:                             }
13807:                         }
13808:                     }
13809:                 }
13810:             }
13811:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
13812:             if (ref($cancreate{$item}) eq 'ARRAY') {
13813:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
13814:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13815:                         push(@{$changes{'cancreate'}},$item);
13816:                     }
13817:                 }
13818:             }
13819:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13820:             if (ref($cancreate{$item}) eq 'HASH') {
13821:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13822:                     push(@{$changes{'cancreate'}},$item);
13823:                 }
13824:             }
13825:         } elsif ($item eq 'emailusername') {
13826:             if (ref($cancreate{$item}) eq 'HASH') {
13827:                 foreach my $type (keys(%{$cancreate{$item}})) {
13828:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
13829:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
13830:                             if ($cancreate{$item}{$type}{$field}) {
13831:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13832:                                     push(@{$changes{'cancreate'}},$item);
13833:                                 }
13834:                                 last;
13835:                             }
13836:                         }
13837:                     }
13838:                 }
13839:             }
13840:         }
13841:     }
13842: #
13843: # Populate %save_usercreate hash with updates to self-creation configuration.
13844: #
13845:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
13846:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
13847:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
13848:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
13849:     if (ref($cancreate{'notify'}) eq 'HASH') {
13850:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
13851:     }
13852:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
13853:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
13854:     }
13855:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
13856:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
13857:     }
13858:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
13859:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
13860:     }
13861:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
13862:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
13863:     }
13864:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
13865:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
13866:     }
13867:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
13868:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
13869:     }
13870:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
13871:     $save_usercreate{'email_rule'} = \%email_rule;
13872: 
13873:     my %userconfig_hash = (
13874:             usercreation     => \%save_usercreate,
13875:             usermodification => \%save_usermodify,
13876:             inststatus       => \%save_inststatus,
13877:     );
13878: 
13879:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
13880:                                              $dom);
13881: #
13882: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
13883: #
13884:     if ($putresult eq 'ok') {
13885:         if (keys(%changes) > 0) {
13886:             $resulttext = &mt('Changes made:').'<ul>';
13887:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
13888:                 my %lt = &selfcreation_types();
13889:                 foreach my $type (@{$changes{'cancreate'}}) {
13890:                     my $chgtext = '';
13891:                     if ($type eq 'selfcreate') {
13892:                         if (@{$cancreate{$type}} == 0) {
13893:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
13894:                         } else {
13895:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
13896:                                         '<ul>';
13897:                             foreach my $case (@{$cancreate{$type}}) {
13898:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
13899:                             }
13900:                             $chgtext .= '</ul>';
13901:                             if (ref($cancreate{$type}) eq 'ARRAY') {
13902:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
13903:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
13904:                                         if (@{$cancreate{'statustocreate'}} == 0) {
13905:                                             $chgtext .= '<span class="LC_warning">'.
13906:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
13907:                                                         '</span><br />';
13908:                                         }
13909:                                     }
13910:                                 }
13911:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
13912:                                     if (!@statuses) {
13913:                                         $chgtext .= '<span class="LC_warning">'.
13914:                                                     &mt("However, e-mail verification is currently set to 'unavailable' for all user types (including 'other'), so self-creation of accounts is not possible for non-institutional log-in.").
13915:                                                     '</span><br />';
13916: 
13917:                                     }
13918:                                 }
13919:                             }
13920:                         }
13921:                     } elsif ($type eq 'shibenv') {
13922:                         if (keys(%{$cancreate{$type}}) == 0) {
13923:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
13924:                         } else {
13925:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
13926:                                         '<ul>';
13927:                             foreach my $field (@shibfields) {
13928:                                 next if ($cancreate{$type}{$field} eq '');
13929:                                 if ($field eq 'inststatus') {
13930:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
13931:                                 } else {
13932:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
13933:                                 }
13934:                             }
13935:                             $chgtext .= '</ul>';
13936:                         }
13937:                     } elsif ($type eq 'statustocreate') {
13938:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
13939:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
13940:                             if (@{$cancreate{'selfcreate'}} > 0) {
13941:                                 if (@{$cancreate{'statustocreate'}} == 0) {
13942:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
13943:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
13944:                                         $chgtext .= '<br />'.
13945:                                                     '<span class="LC_warning">'.
13946:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
13947:                                                     '</span>';
13948:                                     }
13949:                                 } elsif (keys(%usertypes) > 0) {
13950:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
13951:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
13952:                                     } else {
13953:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
13954:                                     }
13955:                                     $chgtext .= '<ul>';
13956:                                     foreach my $case (@{$cancreate{$type}}) {
13957:                                         if ($case eq 'default') {
13958:                                             $chgtext .= '<li>'.$othertitle.'</li>';
13959:                                         } else {
13960:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
13961:                                         }
13962:                                     }
13963:                                     $chgtext .= '</ul>';
13964:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
13965:                                         $chgtext .= '<span class="LC_warning">'.
13966:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
13967:                                                     '</span>';
13968:                                     }
13969:                                 }
13970:                             } else {
13971:                                 if (@{$cancreate{$type}} == 0) {
13972:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
13973:                                 } else {
13974:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
13975:                                 }
13976:                             }
13977:                             $chgtext .= '<br />';
13978:                         }
13979:                     } elsif ($type eq 'selfcreateprocessing') {
13980:                         my %choices = &Apache::lonlocal::texthash (
13981:                                                                     automatic => 'Automatic approval',
13982:                                                                     approval  => 'Queued for approval',
13983:                                                                   );
13984:                         if (@types) {
13985:                             if (@statuses) {
13986:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
13987:                                             '<ul>';
13988:                                 foreach my $status (@statuses) {
13989:                                     if ($status eq 'default') {
13990:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
13991:                                     } else {
13992:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
13993:                                     }
13994:                                 }
13995:                                 $chgtext .= '</ul>';
13996:                             }
13997:                         } else {
13998:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
13999:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
14000:                         }
14001:                     } elsif ($type eq 'emailverified') {
14002:                         my %options = &Apache::lonlocal::texthash (
14003:                                                                     all   => 'Same as e-mail',
14004:                                                                     first => 'Omit @domain',
14005:                                                                     free  => 'Free to choose',
14006:                                                                   );
14007:                         if (@types) {
14008:                             if (@statuses) {
14009:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
14010:                                             '<ul>';
14011:                                 foreach my $status (@statuses) {
14012:                                     if ($status eq 'default') {
14013:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
14014:                                     } else {
14015:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
14016:                                     }
14017:                                 }
14018:                                 $chgtext .= '</ul>';
14019:                             }
14020:                         } else {
14021:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
14022:                                             $options{$cancreate{'emailverified'}{'default'}});
14023:                         }
14024:                     } elsif ($type eq 'emailoptions') {
14025:                         my %options = &Apache::lonlocal::texthash (
14026:                                                                     any     => 'Any e-mail',
14027:                                                                     inst    => 'Institutional only',
14028:                                                                     noninst => 'Non-institutional only',
14029:                                                                     custom  => 'Custom restrictions',
14030:                                                                   );
14031:                         if (@types) {
14032:                             if (@statuses) {
14033:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
14034:                                             '<ul>';
14035:                                 foreach my $status (@statuses) {
14036:                                     if ($type eq 'default') {
14037:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
14038:                                     } else {
14039:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
14040:                                     }
14041:                                 }
14042:                                 $chgtext .= '</ul>';
14043:                             }
14044:                         } else {
14045:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
14046:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
14047:                             } else {
14048:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
14049:                                                 $options{$cancreate{'emailoptions'}{'default'}});
14050:                             }
14051:                         }
14052:                     } elsif ($type eq 'emaildomain') {
14053:                         my $output;
14054:                         if (@statuses) {
14055:                             foreach my $type (@statuses) {
14056:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
14057:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
14058:                                         if ($type eq 'default') {
14059:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14060:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
14061:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
14062:                                             } else {
14063:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
14064:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
14065:                                             }
14066:                                         } else {
14067:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14068:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
14069:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
14070:                                             } else {
14071:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
14072:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
14073:                                             }
14074:                                         }
14075:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
14076:                                         if ($type eq 'default') {
14077:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14078:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
14079:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
14080:                                             } else {
14081:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
14082:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
14083:                                             }
14084:                                         } else {
14085:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
14086:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
14087:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
14088:                                             } else {
14089:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
14090:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
14091:                                             }
14092:                                         }
14093:                                     }
14094:                                 }
14095:                             }
14096:                         }
14097:                         if ($output ne '') {
14098:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
14099:                                         '<ul>'.$output.'</ul>';
14100:                         }
14101:                     } elsif ($type eq 'captcha') {
14102:                         if ($savecaptcha{$type} eq 'notused') {
14103:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
14104:                         } else {
14105:                             my %captchas = &captcha_phrases();
14106:                             if ($captchas{$savecaptcha{$type}}) {
14107:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
14108:                             } else {
14109:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
14110:                             }
14111:                         }
14112:                     } elsif ($type eq 'recaptchakeys') {
14113:                         my ($privkey,$pubkey);
14114:                         if (ref($savecaptcha{$type}) eq 'HASH') {
14115:                             $pubkey = $savecaptcha{$type}{'public'};
14116:                             $privkey = $savecaptcha{$type}{'private'};
14117:                         }
14118:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
14119:                         if (!$pubkey) {
14120:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
14121:                         } else {
14122:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
14123:                         }
14124:                         if (!$privkey) {
14125:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
14126:                         } else {
14127:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
14128:                         }
14129:                         $chgtext .= '</ul>';
14130:                     } elsif ($type eq 'recaptchaversion') {
14131:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
14132:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
14133:                         }
14134:                     } elsif ($type eq 'emailusername') {
14135:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
14136:                             if (@statuses) {
14137:                                 foreach my $type (@statuses) {
14138:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
14139:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
14140:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
14141:                                                     '<ul>';
14142:                                             foreach my $field (@{$infofields}) {
14143:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
14144:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
14145:                                                 }
14146:                                             }
14147:                                             $chgtext .= '</ul>';
14148:                                         } else {
14149:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
14150:                                         }
14151:                                     } else {
14152:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
14153:                                     }
14154:                                 }
14155:                             }
14156:                         }
14157:                     } elsif ($type eq 'notify') {
14158:                         my $numapprove = 0;
14159:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
14160:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
14161:                                 if ($cancreate{'notify'}{'approval'}) {
14162:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
14163:                                     $numapprove ++;
14164:                                 }
14165:                             }
14166:                         }
14167:                         unless ($numapprove) {
14168:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
14169:                         }
14170:                     }
14171:                     if ($chgtext) {
14172:                         $resulttext .= '<li>'.$chgtext.'</li>';
14173:                     }
14174:                 }
14175:             }
14176:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
14177:                 my ($emailrules,$emailruleorder) =
14178:                     &Apache::lonnet::inst_userrules($dom,'email');
14179:                 foreach my $type (@{$changes{'email_rule'}}) {
14180:                     if (ref($email_rule{$type}) eq 'ARRAY') {
14181:                         my $chgtext = '<ul>';
14182:                         foreach my $rule (@{$email_rule{$type}}) {
14183:                             if (ref($emailrules->{$rule}) eq 'HASH') {
14184:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
14185:                             }
14186:                         }
14187:                         $chgtext .= '</ul>';
14188:                         my $typename;
14189:                         if (@types) {
14190:                             if ($type eq 'default') {
14191:                                 $typename = $othertitle;
14192:                             } else {
14193:                                 $typename = $usertypes{$type};
14194:                             }
14195:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
14196:                         }
14197:                         if (@{$email_rule{$type}} > 0) {
14198:                             $resulttext .= '<li>'.
14199:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
14200:                                                $usertypes{$type}).
14201:                                            $chgtext.
14202:                                            '</li>';
14203:                         } else {
14204:                             $resulttext .= '<li>'.
14205:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
14206:                                            '</li>'.
14207:                                            &mt('(Affiliation: [_1])',$typename);
14208:                         }
14209:                     }
14210:                 }
14211:             }
14212:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
14213:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
14214:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
14215:                         my $chgtext = '<ul>';
14216:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
14217:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
14218:                         }
14219:                         $chgtext .= '</ul>';
14220:                         $resulttext .= '<li>'.
14221:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
14222:                                           $chgtext.
14223:                                        '</li>';
14224:                     } else {
14225:                         $resulttext .= '<li>'.
14226:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
14227:                                        '</li>';
14228:                     }
14229:                 }
14230:             }
14231:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
14232:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
14233:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14234:                 foreach my $type (@{$changes{'selfcreate'}}) {
14235:                     my $typename = $type;
14236:                     if (keys(%usertypes) > 0) {
14237:                         if ($usertypes{$type} ne '') {
14238:                             $typename = $usertypes{$type};
14239:                         }
14240:                     }
14241:                     my @modifiable;
14242:                     $resulttext .= '<li>'.
14243:                                     &mt('Self-creation of account by users with status: [_1]',
14244:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
14245:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
14246:                     foreach my $field (@fields) {
14247:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
14248:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
14249:                         }
14250:                     }
14251:                     if (@modifiable > 0) {
14252:                         $resulttext .= join(', ',@modifiable);
14253:                     } else {
14254:                         $resulttext .= &mt('none');
14255:                     }
14256:                     $resulttext .= '</li>';
14257:                 }
14258:                 $resulttext .= '</ul></li>';
14259:             }
14260:             $resulttext .= '</ul>';
14261:             my $cachetime = 24*60*60;
14262:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
14263:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14264:             if (ref($lastactref) eq 'HASH') {
14265:                 $lastactref->{'domdefaults'} = 1;
14266:             }
14267:         } else {
14268:             $resulttext = &mt('No changes made to self-creation settings');
14269:         }
14270:     } else {
14271:         $resulttext = '<span class="LC_error">'.
14272:             &mt('An error occurred: [_1]',$putresult).'</span>';
14273:     }
14274:     if ($warningmsg ne '') {
14275:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
14276:     }
14277:     return $resulttext;
14278: }
14279: 
14280: sub process_captcha {
14281:     my ($container,$changes,$newsettings,$currsettings) = @_;
14282:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
14283:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
14284:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
14285:         $newsettings->{'captcha'} = 'original';
14286:     }
14287:     my %current;
14288:     if (ref($currsettings) eq 'HASH') {
14289:         %current = %{$currsettings};
14290:     }
14291:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
14292:         if ($container eq 'cancreate') {
14293:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14294:                 push(@{$changes->{'cancreate'}},'captcha');
14295:             } elsif (!defined($changes->{'cancreate'})) {
14296:                 $changes->{'cancreate'} = ['captcha'];
14297:             }
14298:         } elsif ($container eq 'passwords') {
14299:             $changes->{'reset'} = 1;
14300:         } else {
14301:             $changes->{'captcha'} = 1;
14302:         }
14303:     }
14304:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
14305:     if ($newsettings->{'captcha'} eq 'recaptcha') {
14306:         $newpub = $env{'form.'.$container.'_recaptchapub'};
14307:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
14308:         $newpub =~ s/[^\w\-]//g;
14309:         $newpriv =~ s/[^\w\-]//g;
14310:         $newsettings->{'recaptchakeys'} = {
14311:                                              public  => $newpub,
14312:                                              private => $newpriv,
14313:                                           };
14314:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
14315:         $newversion =~ s/\D//g;
14316:         if ($newversion ne '2') {
14317:             $newversion = 1;
14318:         }
14319:         $newsettings->{'recaptchaversion'} = $newversion;
14320:     }
14321:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
14322:         $currpub = $current{'recaptchakeys'}{'public'};
14323:         $currpriv = $current{'recaptchakeys'}{'private'};
14324:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
14325:             $newsettings->{'recaptchakeys'} = {
14326:                                                  public  => '',
14327:                                                  private => '',
14328:                                               }
14329:         }
14330:     }
14331:     if ($current{'captcha'} eq 'recaptcha') {
14332:         $currversion = $current{'recaptchaversion'};
14333:         if ($currversion ne '2') {
14334:             $currversion = 1;
14335:         }
14336:     }
14337:     if ($currversion ne $newversion) {
14338:         if ($container eq 'cancreate') {
14339:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14340:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
14341:             } elsif (!defined($changes->{'cancreate'})) {
14342:                 $changes->{'cancreate'} = ['recaptchaversion'];
14343:             }
14344:         } elsif ($container eq 'passwords') {
14345:             $changes->{'reset'} = 1;
14346:         } else {
14347:             $changes->{'recaptchaversion'} = 1;
14348:         }
14349:     }
14350:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
14351:         if ($container eq 'cancreate') {
14352:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14353:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
14354:             } elsif (!defined($changes->{'cancreate'})) {
14355:                 $changes->{'cancreate'} = ['recaptchakeys'];
14356:             }
14357:         } elsif ($container eq 'passwords') {
14358:             $changes->{'reset'} = 1;
14359:         } else {
14360:             $changes->{'recaptchakeys'} = 1;
14361:         }
14362:     }
14363:     return;
14364: }
14365: 
14366: sub modify_usermodification {
14367:     my ($dom,%domconfig) = @_;
14368:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
14369:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
14370:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
14371:             if ($key eq 'selfcreate') {
14372:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
14373:             } else {  
14374:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
14375:             }
14376:         }
14377:     }
14378:     my @contexts = ('author','course');
14379:     my %context_title = (
14380:                            author => 'In author context',
14381:                            course => 'In course context',
14382:                         );
14383:     my @fields = ('lastname','firstname','middlename','generation',
14384:                   'permanentemail','id');
14385:     my %roles = (
14386:                   author => ['ca','aa'],
14387:                   course => ['st','ep','ta','in','cr'],
14388:                 );
14389:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14390:     foreach my $context (@contexts) {
14391:         foreach my $role (@{$roles{$context}}) {
14392:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
14393:             foreach my $item (@fields) {
14394:                 if (grep(/^\Q$item\E$/,@modifiable)) {
14395:                     $modifyhash{$context}{$role}{$item} = 1;
14396:                 } else {
14397:                     $modifyhash{$context}{$role}{$item} = 0;
14398:                 }
14399:             }
14400:         }
14401:         if (ref($curr_usermodification{$context}) eq 'HASH') {
14402:             foreach my $role (@{$roles{$context}}) {
14403:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
14404:                     foreach my $field (@fields) {
14405:                         if ($modifyhash{$context}{$role}{$field} ne 
14406:                                 $curr_usermodification{$context}{$role}{$field}) {
14407:                             push(@{$changes{$context}},$role);
14408:                             last;
14409:                         }
14410:                     }
14411:                 }
14412:             }
14413:         } else {
14414:             foreach my $context (@contexts) {
14415:                 foreach my $role (@{$roles{$context}}) {
14416:                     push(@{$changes{$context}},$role);
14417:                 }
14418:             }
14419:         }
14420:     }
14421:     my %usermodification_hash =  (
14422:                                    usermodification => \%modifyhash,
14423:                                  );
14424:     my $putresult = &Apache::lonnet::put_dom('configuration',
14425:                                              \%usermodification_hash,$dom);
14426:     if ($putresult eq 'ok') {
14427:         if (keys(%changes) > 0) {
14428:             $resulttext = &mt('Changes made: ').'<ul>';
14429:             foreach my $context (@contexts) {
14430:                 if (ref($changes{$context}) eq 'ARRAY') {
14431:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
14432:                     if (ref($changes{$context}) eq 'ARRAY') {
14433:                         foreach my $role (@{$changes{$context}}) {
14434:                             my $rolename;
14435:                             if ($role eq 'cr') {
14436:                                 $rolename = &mt('Custom');
14437:                             } else {
14438:                                 $rolename = &Apache::lonnet::plaintext($role);
14439:                             }
14440:                             my @modifiable;
14441:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
14442:                             foreach my $field (@fields) {
14443:                                 if ($modifyhash{$context}{$role}{$field}) {
14444:                                     push(@modifiable,$fieldtitles{$field});
14445:                                 }
14446:                             }
14447:                             if (@modifiable > 0) {
14448:                                 $resulttext .= join(', ',@modifiable);
14449:                             } else {
14450:                                 $resulttext .= &mt('none'); 
14451:                             }
14452:                             $resulttext .= '</li>';
14453:                         }
14454:                         $resulttext .= '</ul></li>';
14455:                     }
14456:                 }
14457:             }
14458:             $resulttext .= '</ul>';
14459:         } else {
14460:             $resulttext = &mt('No changes made to user modification settings');
14461:         }
14462:     } else {
14463:         $resulttext = '<span class="LC_error">'.
14464:             &mt('An error occurred: [_1]',$putresult).'</span>';
14465:     }
14466:     return $resulttext;
14467: }
14468: 
14469: sub modify_defaults {
14470:     my ($dom,$lastactref,%domconfig) = @_;
14471:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
14472:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14473:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
14474:                  'portal_def');
14475:     my @authtypes = ('internal','krb4','krb5','localauth');
14476:     foreach my $item (@items) {
14477:         $newvalues{$item} = $env{'form.'.$item};
14478:         if ($item eq 'auth_def') {
14479:             if ($newvalues{$item} ne '') {
14480:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
14481:                     push(@errors,$item);
14482:                 }
14483:             }
14484:         } elsif ($item eq 'lang_def') {
14485:             if ($newvalues{$item} ne '') {
14486:                 if ($newvalues{$item} =~ /^(\w+)/) {
14487:                     my $langcode = $1;
14488:                     if ($langcode ne 'x_chef') {
14489:                         if (code2language($langcode) eq '') {
14490:                             push(@errors,$item);
14491:                         }
14492:                     }
14493:                 } else {
14494:                     push(@errors,$item);
14495:                 }
14496:             }
14497:         } elsif ($item eq 'timezone_def') {
14498:             if ($newvalues{$item} ne '') {
14499:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
14500:                     push(@errors,$item);   
14501:                 }
14502:             }
14503:         } elsif ($item eq 'datelocale_def') {
14504:             if ($newvalues{$item} ne '') {
14505:                 my @datelocale_ids = DateTime::Locale->ids();
14506:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
14507:                     push(@errors,$item);
14508:                 }
14509:             }
14510:         } elsif ($item eq 'portal_def') {
14511:             if ($newvalues{$item} ne '') {
14512:                 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])\/?$/) {
14513:                     push(@errors,$item);
14514:                 }
14515:             }
14516:         }
14517:         if (grep(/^\Q$item\E$/,@errors)) {
14518:             $newvalues{$item} = $domdefaults{$item};
14519:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
14520:             $changes{$item} = 1;
14521:         }
14522:         $domdefaults{$item} = $newvalues{$item};
14523:     }
14524:     my %staticdefaults = (
14525:                            'intauth_cost'   => 10,
14526:                            'intauth_check'  => 0,
14527:                            'intauth_switch' => 0,
14528:                          );
14529:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
14530:         if (exists($domdefaults{$item})) {
14531:             $newvalues{$item} = $domdefaults{$item};
14532:         } else {
14533:             $newvalues{$item} = $staticdefaults{$item};
14534:         }
14535:     }
14536:     my %defaults_hash = (
14537:                          defaults => \%newvalues,
14538:                         );
14539:     my $title = &defaults_titles();
14540: 
14541:     my $currinststatus;
14542:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
14543:         $currinststatus = $domconfig{'inststatus'};
14544:     } else {
14545:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14546:         $currinststatus = {
14547:                              inststatustypes => $usertypes,
14548:                              inststatusorder => $types,
14549:                              inststatusguest => [],
14550:                           };
14551:     }
14552:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
14553:     my @allpos;
14554:     my %alltypes;
14555:     my @inststatusguest;
14556:     if (ref($currinststatus) eq 'HASH') {
14557:         if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
14558:             foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
14559:                 unless (grep(/^\Q$type\E$/,@todelete)) {
14560:                     push(@inststatusguest,$type);
14561:                 }
14562:             }
14563:         }
14564:     }
14565:     my ($currtitles,$currorder);
14566:     if (ref($currinststatus) eq 'HASH') {
14567:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
14568:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
14569:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
14570:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
14571:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
14572:                     }
14573:                 }
14574:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
14575:                     my $position = $env{'form.inststatus_pos_'.$type};
14576:                     $position =~ s/\D+//g;
14577:                     $allpos[$position] = $type;
14578:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
14579:                     $alltypes{$type} =~ s/`//g;
14580:                 }
14581:             }
14582:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
14583:             $currtitles =~ s/,$//;
14584:         }
14585:     }
14586:     if ($env{'form.addinststatus'}) {
14587:         my $newtype = $env{'form.addinststatus'};
14588:         $newtype =~ s/\W//g;
14589:         unless (exists($alltypes{$newtype})) {
14590:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
14591:             $alltypes{$newtype} =~ s/`//g; 
14592:             my $position = $env{'form.addinststatus_pos'};
14593:             $position =~ s/\D+//g;
14594:             if ($position ne '') {
14595:                 $allpos[$position] = $newtype;
14596:             }
14597:         }
14598:     }
14599:     my @orderedstatus;
14600:     foreach my $type (@allpos) {
14601:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
14602:             push(@orderedstatus,$type);
14603:         }
14604:     }
14605:     foreach my $type (keys(%alltypes)) {
14606:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
14607:             delete($alltypes{$type});
14608:         }
14609:     }
14610:     $defaults_hash{'inststatus'} = {
14611:                                      inststatustypes => \%alltypes,
14612:                                      inststatusorder => \@orderedstatus,
14613:                                      inststatusguest => \@inststatusguest,
14614:                                    };
14615:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
14616:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
14617:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
14618:         }
14619:     }
14620:     if ($currorder ne join(',',@orderedstatus)) {
14621:         $changes{'inststatus'}{'inststatusorder'} = 1;
14622:     }
14623:     my $newtitles;
14624:     foreach my $item (@orderedstatus) {
14625:         $newtitles .= $alltypes{$item}.',';
14626:     }
14627:     $newtitles =~ s/,$//;
14628:     if ($currtitles ne $newtitles) {
14629:         $changes{'inststatus'}{'inststatustypes'} = 1;
14630:     }
14631:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
14632:                                              $dom);
14633:     if ($putresult eq 'ok') {
14634:         if (keys(%changes) > 0) {
14635:             $resulttext = &mt('Changes made:').'<ul>';
14636:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
14637:             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";
14638:             foreach my $item (sort(keys(%changes))) {
14639:                 if ($item eq 'inststatus') {
14640:                     if (ref($changes{'inststatus'}) eq 'HASH') {
14641:                         if (@orderedstatus) {
14642:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
14643:                             foreach my $type (@orderedstatus) { 
14644:                                 $resulttext .= $alltypes{$type}.', ';
14645:                             }
14646:                             $resulttext =~ s/, $//;
14647:                             $resulttext .= '</li>';
14648:                         } else {
14649:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
14650:                         }
14651:                     }
14652:                 } else {
14653:                     my $value = $env{'form.'.$item};
14654:                     if ($value eq '') {
14655:                         $value = &mt('none');
14656:                     } elsif ($item eq 'auth_def') {
14657:                         my %authnames = &authtype_names();
14658:                         my %shortauth = (
14659:                                           internal   => 'int',
14660:                                           krb4       => 'krb4',
14661:                                           krb5       => 'krb5',
14662:                                           localauth  => 'loc',
14663:                         );
14664:                         $value = $authnames{$shortauth{$value}};
14665:                     }
14666:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
14667:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
14668:                 }
14669:             }
14670:             $resulttext .= '</ul>';
14671:             $mailmsgtext .= "\n";
14672:             my $cachetime = 24*60*60;
14673:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14674:             if (ref($lastactref) eq 'HASH') {
14675:                 $lastactref->{'domdefaults'} = 1;
14676:             }
14677:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
14678:                 my $notify = 1;
14679:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
14680:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
14681:                         $notify = 0;
14682:                     }
14683:                 }
14684:                 if ($notify) {
14685:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
14686:                                                "LON-CAPA Domain Settings Change - $dom",
14687:                                                $mailmsgtext);
14688:                 }
14689:             }
14690:         } else {
14691:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
14692:         }
14693:     } else {
14694:         $resulttext = '<span class="LC_error">'.
14695:             &mt('An error occurred: [_1]',$putresult).'</span>';
14696:     }
14697:     if (@errors > 0) {
14698:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
14699:         foreach my $item (@errors) {
14700:             $resulttext .= ' "'.$title->{$item}.'",';
14701:         }
14702:         $resulttext =~ s/,$//;
14703:     }
14704:     return $resulttext;
14705: }
14706: 
14707: sub modify_scantron {
14708:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
14709:     my ($resulttext,%confhash,%changes,$errors);
14710:     my $custom = 'custom.tab';
14711:     my $default = 'default.tab';
14712:     my $servadm = $r->dir_config('lonAdmEMail');
14713:     my ($configuserok,$author_ok,$switchserver) =
14714:         &config_check($dom,$confname,$servadm);
14715:     if ($env{'form.scantronformat.filename'} ne '') {
14716:         my $error;
14717:         if ($configuserok eq 'ok') {
14718:             if ($switchserver) {
14719:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
14720:             } else {
14721:                 if ($author_ok eq 'ok') {
14722:                     my ($result,$scantronurl) =
14723:                         &publishlogo($r,'upload','scantronformat',$dom,
14724:                                      $confname,'scantron','','',$custom);
14725:                     if ($result eq 'ok') {
14726:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
14727:                         $changes{'scantronformat'} = 1;
14728:                     } else {
14729:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
14730:                     }
14731:                 } else {
14732:                     $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);
14733:                 }
14734:             }
14735:         } else {
14736:             $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);
14737:         }
14738:         if ($error) {
14739:             &Apache::lonnet::logthis($error);
14740:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14741:         }
14742:     }
14743:     if (ref($domconfig{'scantron'}) eq 'HASH') {
14744:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
14745:             if ($env{'form.scantronformat_del'}) {
14746:                 $confhash{'scantron'}{'scantronformat'} = '';
14747:                 $changes{'scantronformat'} = 1;
14748:             } else {
14749:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
14750:             }
14751:         }
14752:     }
14753:     my @options = ('hdr','pad','rem');
14754:     my @fields = &scantroncsv_fields();
14755:     my %titles = &scantronconfig_titles();
14756:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
14757:     my ($newdat,$currdat,%newcol,%currcol);
14758:     if (grep(/^dat$/,@formats)) {
14759:         $confhash{'scantron'}{config}{dat} = 1;
14760:         $newdat = 1;
14761:     } else {
14762:         $newdat = 0;
14763:     }
14764:     if (grep(/^csv$/,@formats)) {
14765:         my %bynum;
14766:         foreach my $field (@fields) {
14767:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
14768:                 my $posscol = $1;
14769:                 if (($posscol < 20) && (!$bynum{$posscol})) {
14770:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
14771:                     $bynum{$posscol} = $field;
14772:                     $newcol{$field} = $posscol;
14773:                 }
14774:             }
14775:         }
14776:         if (keys(%newcol)) {
14777:             foreach my $option (@options) {
14778:                 if ($env{'form.scantroncsv_'.$option}) {
14779:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
14780:                 }
14781:             }
14782:         }
14783:     }
14784:     $currdat = 1;
14785:     if (ref($domconfig{'scantron'}) eq 'HASH') {
14786:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
14787:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
14788:                 $currdat = 0;
14789:             }
14790:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
14791:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
14792:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
14793:                 }
14794:             }
14795:         }
14796:     }
14797:     if ($currdat != $newdat) {
14798:         $changes{'config'} = 1;
14799:     } else {
14800:         foreach my $field (@fields) {
14801:             if ($currcol{$field} ne '') {
14802:                 if ($currcol{$field} ne $newcol{$field}) {
14803:                     $changes{'config'} = 1;
14804:                     last;
14805:                 }
14806:             } elsif ($newcol{$field} ne '') {
14807:                 $changes{'config'} = 1;
14808:                 last;
14809:             }
14810:         }
14811:     }
14812:     if (keys(%confhash) > 0) {
14813:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
14814:                                                  $dom);
14815:         if ($putresult eq 'ok') {
14816:             if (keys(%changes) > 0) {
14817:                 if (ref($confhash{'scantron'}) eq 'HASH') {
14818:                     $resulttext = &mt('Changes made:').'<ul>';
14819:                     if ($changes{'scantronformat'}) {
14820:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
14821:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
14822:                         } else {
14823:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
14824:                         }
14825:                     }
14826:                     if ($changes{'config'}) {
14827:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
14828:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
14829:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
14830:                             }
14831:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
14832:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
14833:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
14834:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
14835:                                         foreach my $field (@fields) {
14836:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
14837:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
14838:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
14839:                                             }
14840:                                         }
14841:                                         $resulttext .= '</ul></li>';
14842:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
14843:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
14844:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
14845:                                                 foreach my $option (@options) {
14846:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
14847:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
14848:                                                     }
14849:                                                 }
14850:                                                 $resulttext .= '</ul></li>';
14851:                                             }
14852:                                         }
14853:                                     }
14854:                                 }
14855:                             }
14856:                         } else {
14857:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
14858:                         }
14859:                     }
14860:                     $resulttext .= '</ul>';
14861:                 } else {
14862:                     $resulttext = &mt('Changes made to bubblesheet format file.');
14863:                 }
14864:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
14865:                 if (ref($lastactref) eq 'HASH') {
14866:                     $lastactref->{'domainconfig'} = 1;
14867:                 }
14868:             } else {
14869:                 $resulttext = &mt('No changes made to bubblesheet format settings');
14870:             }
14871:         } else {
14872:             $resulttext = '<span class="LC_error">'.
14873:                 &mt('An error occurred: [_1]',$putresult).'</span>';
14874:         }
14875:     } else {
14876:         $resulttext = &mt('No changes made to bubblesheet format settings');
14877:     }
14878:     if ($errors) {
14879:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14880:                        $errors.'</ul>';
14881:     }
14882:     return $resulttext;
14883: }
14884: 
14885: sub modify_coursecategories {
14886:     my ($dom,$lastactref,%domconfig) = @_;
14887:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
14888:         $cathash);
14889:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
14890:     my @catitems = ('unauth','auth');
14891:     my @cattypes = ('std','domonly','codesrch','none');
14892:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
14893:         $cathash = $domconfig{'coursecategories'}{'cats'};
14894:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
14895:             $changes{'togglecats'} = 1;
14896:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
14897:         }
14898:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
14899:             $changes{'categorize'} = 1;
14900:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
14901:         }
14902:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
14903:             $changes{'togglecatscomm'} = 1;
14904:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
14905:         }
14906:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
14907:             $changes{'categorizecomm'} = 1;
14908:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
14909:         }
14910:         foreach my $item (@catitems) {
14911:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
14912:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
14913:                     $changes{$item} = 1;
14914:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
14915:                 }
14916:             }
14917:         }
14918:     } else {
14919:         $changes{'togglecats'} = 1;
14920:         $changes{'categorize'} = 1;
14921:         $changes{'togglecatscomm'} = 1;
14922:         $changes{'categorizecomm'} = 1;
14923:         $domconfig{'coursecategories'} = {
14924:                                              togglecats => $env{'form.togglecats'},
14925:                                              categorize => $env{'form.categorize'},
14926:                                              togglecatscomm => $env{'form.togglecatscomm'},
14927:                                              categorizecomm => $env{'form.categorizecomm'},
14928:                                          };
14929:         foreach my $item (@catitems) {
14930:             if ($env{'form.coursecat_'.$item} ne 'std') {
14931:                 $changes{$item} = 1;
14932:             }
14933:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
14934:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
14935:             }
14936:         }
14937:     }
14938:     if (ref($cathash) eq 'HASH') {
14939:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
14940:             push (@deletecategory,'instcode::0');
14941:         }
14942:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
14943:             push(@deletecategory,'communities::0');
14944:         }
14945:     }
14946:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
14947:     if (ref($cathash) eq 'HASH') {
14948:         if (@deletecategory > 0) {
14949:             #FIXME Need to remove category from all courses using a deleted category 
14950:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
14951:             foreach my $item (@deletecategory) {
14952:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
14953:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
14954:                     $deletions{$item} = 1;
14955:                     &recurse_cat_deletes($item,$cathash,\%deletions);
14956:                 }
14957:             }
14958:         }
14959:         foreach my $item (keys(%{$cathash})) {
14960:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14961:             if ($cathash->{$item} ne $env{'form.'.$item}) {
14962:                 $reorderings{$item} = 1;
14963:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
14964:             }
14965:             if ($env{'form.addcategory_name_'.$item} ne '') {
14966:                 my $newcat = $env{'form.addcategory_name_'.$item};
14967:                 my $newdepth = $depth+1;
14968:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
14969:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
14970:                 $adds{$newitem} = 1; 
14971:             }
14972:             if ($env{'form.subcat_'.$item} ne '') {
14973:                 my $newcat = $env{'form.subcat_'.$item};
14974:                 my $newdepth = $depth+1;
14975:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
14976:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
14977:                 $adds{$newitem} = 1;
14978:             }
14979:         }
14980:     }
14981:     if ($env{'form.instcode'} eq '1') {
14982:         if (ref($cathash) eq 'HASH') {
14983:             my $newitem = 'instcode::0';
14984:             if ($cathash->{$newitem} eq '') {  
14985:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
14986:                 $adds{$newitem} = 1;
14987:             }
14988:         } else {
14989:             my $newitem = 'instcode::0';
14990:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
14991:             $adds{$newitem} = 1;
14992:         }
14993:     }
14994:     if ($env{'form.communities'} eq '1') {
14995:         if (ref($cathash) eq 'HASH') {
14996:             my $newitem = 'communities::0';
14997:             if ($cathash->{$newitem} eq '') {
14998:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
14999:                 $adds{$newitem} = 1;
15000:             }
15001:         } else {
15002:             my $newitem = 'communities::0';
15003:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
15004:             $adds{$newitem} = 1;
15005:         }
15006:     }
15007:     if ($env{'form.addcategory_name'} ne '') {
15008:         if (($env{'form.addcategory_name'} ne 'instcode') &&
15009:             ($env{'form.addcategory_name'} ne 'communities')) {
15010:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
15011:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
15012:             $adds{$newitem} = 1;
15013:         }
15014:     }
15015:     my $putresult;
15016:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
15017:         if (keys(%deletions) > 0) {
15018:             foreach my $key (keys(%deletions)) {
15019:                 if ($predelallitems{$key} ne '') {
15020:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
15021:                 }
15022:             }
15023:         }
15024:         my (@chkcats,@chktrails,%chkallitems);
15025:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
15026:         if (ref($chkcats[0]) eq 'ARRAY') {
15027:             my $depth = 0;
15028:             my $chg = 0;
15029:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
15030:                 my $name = $chkcats[0][$i];
15031:                 my $item;
15032:                 if ($name eq '') {
15033:                     $chg ++;
15034:                 } else {
15035:                     $item = &escape($name).'::0';
15036:                     if ($chg) {
15037:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
15038:                     }
15039:                     $depth ++; 
15040:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
15041:                     $depth --;
15042:                 }
15043:             }
15044:         }
15045:     }
15046:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
15047:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
15048:         if ($putresult eq 'ok') {
15049:             my %title = (
15050:                          togglecats     => 'Show/Hide a course in catalog',
15051:                          categorize     => 'Assign a category to a course',
15052:                          togglecatscomm => 'Show/Hide a community in catalog',
15053:                          categorizecomm => 'Assign a category to a community',
15054:                         );
15055:             my %level = (
15056:                          dom  => 'set in Domain ("Modify Course/Community")',
15057:                          crs  => 'set in Course ("Course Configuration")',
15058:                          comm => 'set in Community ("Community Configuration")',
15059:                          none     => 'No catalog',
15060:                          std      => 'Standard catalog',
15061:                          domonly  => 'Domain-only catalog',
15062:                          codesrch => 'Code search form',
15063:                         );
15064:             $resulttext = &mt('Changes made:').'<ul>';
15065:             if ($changes{'togglecats'}) {
15066:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
15067:             }
15068:             if ($changes{'categorize'}) {
15069:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
15070:             }
15071:             if ($changes{'togglecatscomm'}) {
15072:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
15073:             }
15074:             if ($changes{'categorizecomm'}) {
15075:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
15076:             }
15077:             if ($changes{'unauth'}) {
15078:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
15079:             }
15080:             if ($changes{'auth'}) {
15081:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
15082:             }
15083:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
15084:                 my $cathash;
15085:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
15086:                     $cathash = $domconfig{'coursecategories'}{'cats'};
15087:                 } else {
15088:                     $cathash = {};
15089:                 } 
15090:                 my (@cats,@trails,%allitems);
15091:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
15092:                 if (keys(%deletions) > 0) {
15093:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
15094:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
15095:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
15096:                     }
15097:                     $resulttext .= '</ul></li>';
15098:                 }
15099:                 if (keys(%reorderings) > 0) {
15100:                     my %sort_by_trail;
15101:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
15102:                     foreach my $key (keys(%reorderings)) {
15103:                         if ($allitems{$key} ne '') {
15104:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
15105:                         }
15106:                     }
15107:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
15108:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
15109:                     }
15110:                     $resulttext .= '</ul></li>';
15111:                 }
15112:                 if (keys(%adds) > 0) {
15113:                     my %sort_by_trail;
15114:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
15115:                     foreach my $key (keys(%adds)) {
15116:                         if ($allitems{$key} ne '') {
15117:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
15118:                         }
15119:                     }
15120:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
15121:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
15122:                     }
15123:                     $resulttext .= '</ul></li>';
15124:                 }
15125:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
15126:                 if (ref($lastactref) eq 'HASH') {
15127:                     $lastactref->{'cats'} = 1;
15128:                 }
15129:             }
15130:             $resulttext .= '</ul>';
15131:             if ($changes{'unauth'} || $changes{'auth'}) {
15132:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
15133:                 if ($changes{'auth'}) {
15134:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
15135:                 }
15136:                 if ($changes{'unauth'}) {
15137:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
15138:                 }
15139:                 my $cachetime = 24*60*60;
15140:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15141:                 if (ref($lastactref) eq 'HASH') {
15142:                     $lastactref->{'domdefaults'} = 1;
15143:                 }
15144:             }
15145:         } else {
15146:             $resulttext = '<span class="LC_error">'.
15147:                           &mt('An error occurred: [_1]',$putresult).'</span>';
15148:         }
15149:     } else {
15150:         $resulttext = &mt('No changes made to course and community categories');
15151:     }
15152:     return $resulttext;
15153: }
15154: 
15155: sub modify_serverstatuses {
15156:     my ($dom,%domconfig) = @_;
15157:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
15158:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
15159:         %currserverstatus = %{$domconfig{'serverstatuses'}};
15160:     }
15161:     my @pages = &serverstatus_pages();
15162:     foreach my $type (@pages) {
15163:         $newserverstatus{$type}{'namedusers'} = '';
15164:         $newserverstatus{$type}{'machines'} = '';
15165:         if (defined($env{'form.'.$type.'_namedusers'})) {
15166:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
15167:             my @okusers;
15168:             foreach my $user (@users) {
15169:                 my ($uname,$udom) = split(/:/,$user);
15170:                 if (($udom =~ /^$match_domain$/) &&   
15171:                     (&Apache::lonnet::domain($udom)) &&
15172:                     ($uname =~ /^$match_username$/)) {
15173:                     if (!grep(/^\Q$user\E/,@okusers)) {
15174:                         push(@okusers,$user);
15175:                     }
15176:                 }
15177:             }
15178:             if (@okusers > 0) {
15179:                  @okusers = sort(@okusers);
15180:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
15181:             }
15182:         }
15183:         if (defined($env{'form.'.$type.'_machines'})) {
15184:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
15185:             my @okmachines;
15186:             foreach my $ip (@machines) {
15187:                 my @parts = split(/\./,$ip);
15188:                 next if (@parts < 4);
15189:                 my $badip = 0;
15190:                 for (my $i=0; $i<4; $i++) {
15191:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
15192:                         $badip = 1;
15193:                         last;
15194:                     }
15195:                 }
15196:                 if (!$badip) {
15197:                     push(@okmachines,$ip);     
15198:                 }
15199:             }
15200:             @okmachines = sort(@okmachines);
15201:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
15202:         }
15203:     }
15204:     my %serverstatushash =  (
15205:                                 serverstatuses => \%newserverstatus,
15206:                             );
15207:     foreach my $type (@pages) {
15208:         foreach my $setting ('namedusers','machines') {
15209:             my (@current,@new);
15210:             if (ref($currserverstatus{$type}) eq 'HASH') {
15211:                 if ($currserverstatus{$type}{$setting} ne '') { 
15212:                     @current = split(/,/,$currserverstatus{$type}{$setting});
15213:                 }
15214:             }
15215:             if ($newserverstatus{$type}{$setting} ne '') {
15216:                 @new = split(/,/,$newserverstatus{$type}{$setting});
15217:             }
15218:             if (@current > 0) {
15219:                 if (@new > 0) {
15220:                     foreach my $item (@current) {
15221:                         if (!grep(/^\Q$item\E$/,@new)) {
15222:                             $changes{$type}{$setting} = 1;
15223:                             last;
15224:                         }
15225:                     }
15226:                     foreach my $item (@new) {
15227:                         if (!grep(/^\Q$item\E$/,@current)) {
15228:                             $changes{$type}{$setting} = 1;
15229:                             last;
15230:                         }
15231:                     }
15232:                 } else {
15233:                     $changes{$type}{$setting} = 1;
15234:                 }
15235:             } elsif (@new > 0) {
15236:                 $changes{$type}{$setting} = 1;
15237:             }
15238:         }
15239:     }
15240:     if (keys(%changes) > 0) {
15241:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
15242:         my $putresult = &Apache::lonnet::put_dom('configuration',
15243:                                                  \%serverstatushash,$dom);
15244:         if ($putresult eq 'ok') {
15245:             $resulttext .= &mt('Changes made:').'<ul>';
15246:             foreach my $type (@pages) {
15247:                 if (ref($changes{$type}) eq 'HASH') {
15248:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
15249:                     if ($changes{$type}{'namedusers'}) {
15250:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
15251:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
15252:                         } else {
15253:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
15254:                         }
15255:                     }
15256:                     if ($changes{$type}{'machines'}) {
15257:                         if ($newserverstatus{$type}{'machines'} eq '') {
15258:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
15259:                         } else {
15260:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
15261:                         }
15262: 
15263:                     }
15264:                     $resulttext .= '</ul></li>';
15265:                 }
15266:             }
15267:             $resulttext .= '</ul>';
15268:         } else {
15269:             $resulttext = '<span class="LC_error">'.
15270:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
15271: 
15272:         }
15273:     } else {
15274:         $resulttext = &mt('No changes made to access to server status pages');
15275:     }
15276:     return $resulttext;
15277: }
15278: 
15279: sub modify_helpsettings {
15280:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
15281:     my ($resulttext,$errors,%changes,%helphash);
15282:     my %defaultchecked = ('submitbugs' => 'on');
15283:     my @offon = ('off','on');
15284:     my @toggles = ('submitbugs');
15285:     my %current = ('submitbugs' => '',
15286:                    'adhoc'      => {},
15287:                   );
15288:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
15289:         %current = %{$domconfig{'helpsettings'}};
15290:     }
15291:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15292:     foreach my $item (@toggles) {
15293:         if ($defaultchecked{$item} eq 'on') { 
15294:             if ($current{$item} eq '') {
15295:                 if ($env{'form.'.$item} eq '0') {
15296:                     $changes{$item} = 1;
15297:                 }
15298:             } elsif ($current{$item} ne $env{'form.'.$item}) {
15299:                 $changes{$item} = 1;
15300:             }
15301:         } elsif ($defaultchecked{$item} eq 'off') {
15302:             if ($current{$item} eq '') {
15303:                 if ($env{'form.'.$item} eq '1') {
15304:                     $changes{$item} = 1;
15305:                 }
15306:             } elsif ($current{$item} ne $env{'form.'.$item}) {
15307:                 $changes{$item} = 1;
15308:             }
15309:         }
15310:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
15311:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
15312:         }
15313:     }
15314:     my $maxnum = $env{'form.helproles_maxnum'};
15315:     my $confname = $dom.'-domainconfig';
15316:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
15317:     my (@allpos,%newsettings,%changedprivs,$newrole);
15318:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15319:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
15320:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
15321:     my %lt = &Apache::lonlocal::texthash(
15322:                     s      => 'system',
15323:                     d      => 'domain',
15324:                     order  => 'Display order',
15325:                     access => 'Role usage',
15326:                     all    => 'All with domain helpdesk or helpdesk assistant role',
15327:                     dh     => 'All with domain helpdesk role',
15328:                     da     => 'All with domain helpdesk assistant role',
15329:                     none   => 'None',
15330:                     status => 'Determined based on institutional status',
15331:                     inc    => 'Include all, but exclude specific personnel',
15332:                     exc    => 'Exclude all, but include specific personnel',
15333:     );
15334:     for (my $num=0; $num<=$maxnum; $num++) {
15335:         my ($prefix,$identifier,$rolename,%curr);
15336:         if ($num == $maxnum) {
15337:             next unless ($env{'form.newcusthelp'} == $maxnum);
15338:             $identifier = 'custhelp'.$num;
15339:             $prefix = 'helproles_'.$num;
15340:             $rolename = $env{'form.custhelpname'.$num};
15341:             $rolename=~s/[^A-Za-z0-9]//gs;
15342:             next if ($rolename eq '');
15343:             next if (exists($existing{'rolesdef_'.$rolename}));
15344:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
15345:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
15346:                                                      $newprivs{'c'},$confname,$dom);
15347:             if ($result ne 'ok') {
15348:                 $errors .= '<li><span class="LC_error">'.
15349:                            &mt('An error occurred storing the new custom role: [_1]',
15350:                            $result).'</span></li>';
15351:                 next;
15352:             } else {
15353:                 $changedprivs{$rolename} = \%newprivs;
15354:                 $newrole = $rolename;
15355:             }
15356:         } else {
15357:             $prefix = 'helproles_'.$num;
15358:             $rolename = $env{'form.'.$prefix};
15359:             next if ($rolename eq '');
15360:             next unless (exists($existing{'rolesdef_'.$rolename}));
15361:             $identifier = 'custhelp'.$num;
15362:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
15363:             my %currprivs;
15364:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
15365:                 split(/\_/,$existing{'rolesdef_'.$rolename});
15366:             foreach my $level ('c','d','s') {
15367:                 if ($newprivs{$level} ne $currprivs{$level}) {
15368:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
15369:                                                              $newprivs{'c'},$confname,$dom);
15370:                     if ($result ne 'ok') {
15371:                         $errors .= '<li><span class="LC_error">'.
15372:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
15373:                                        $rolename,$result).'</span></li>';
15374:                     } else {
15375:                         $changedprivs{$rolename} = \%newprivs;
15376:                     }
15377:                     last;
15378:                 }
15379:             }
15380:             if (ref($current{'adhoc'}) eq 'HASH') {
15381:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
15382:                     %curr = %{$current{'adhoc'}{$rolename}};
15383:                 }
15384:             }
15385:         }
15386:         my $newpos = $env{'form.'.$prefix.'_pos'};
15387:         $newpos =~ s/\D+//g;
15388:         $allpos[$newpos] = $rolename;
15389:         my $newdesc = $env{'form.'.$prefix.'_desc'};
15390:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
15391:         if ($curr{'desc'}) {
15392:             if ($curr{'desc'} ne $newdesc) {
15393:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
15394:                 $newsettings{$rolename}{'desc'} = $newdesc;
15395:             }
15396:         } elsif ($newdesc ne '') {
15397:             $changes{'customrole'}{$rolename}{'desc'} = 1;
15398:             $newsettings{$rolename}{'desc'} = $newdesc;
15399:         }
15400:         my $access = $env{'form.'.$prefix.'_access'};
15401:         if (grep(/^\Q$access\E$/,@accesstypes)) {
15402:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
15403:             if ($access eq 'status') {
15404:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
15405:                 if (scalar(@statuses) == 0) {
15406:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
15407:                 } else {
15408:                     my (@shownstatus,$numtypes);
15409:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
15410:                     if (ref($types) eq 'ARRAY') {
15411:                         $numtypes = scalar(@{$types});
15412:                         foreach my $type (sort(@statuses)) {
15413:                             if ($type eq 'default') {
15414:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
15415:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
15416:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
15417:                                 push(@shownstatus,$usertypes->{$type});
15418:                             }
15419:                         }
15420:                     }
15421:                     if (grep(/^default$/,@statuses)) {
15422:                         push(@shownstatus,$othertitle);
15423:                     }
15424:                     if (scalar(@shownstatus) == 1+$numtypes) {
15425:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
15426:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
15427:                     } else {
15428:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
15429:                         if (ref($curr{'status'}) eq 'ARRAY') {
15430:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
15431:                             if (@diffs) {
15432:                                 $changes{'customrole'}{$rolename}{$access} = 1;
15433:                             }
15434:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15435:                             $changes{'customrole'}{$rolename}{$access} = 1;
15436:                         }
15437:                     }
15438:                 }
15439:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
15440:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
15441:                 my @newspecstaff;
15442:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
15443:                 foreach my $person (sort(@personnel)) {
15444:                     if ($domhelpdesk{$person}) {
15445:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
15446:                     }
15447:                 }
15448:                 if (ref($curr{$access}) eq 'ARRAY') {
15449:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
15450:                     if (@diffs) {
15451:                         $changes{'customrole'}{$rolename}{$access} = 1;
15452:                     }
15453:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15454:                     $changes{'customrole'}{$rolename}{$access} = 1;
15455:                 }
15456:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15457:                     my ($uname,$udom) = split(/:/,$person);
15458:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
15459:                 }
15460:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
15461:             }
15462:         } else {
15463:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
15464:         }
15465:         unless ($curr{'access'} eq $access) {
15466:             $changes{'customrole'}{$rolename}{'access'} = 1;
15467:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
15468:         }
15469:     }
15470:     if (@allpos > 0) {
15471:         my $idx = 0;
15472:         foreach my $rolename (@allpos) {
15473:             if ($rolename ne '') {
15474:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
15475:                 if (ref($current{'adhoc'}) eq 'HASH') {
15476:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
15477:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
15478:                             $changes{'customrole'}{$rolename}{'order'} = 1;
15479:                             $newsettings{$rolename}{'order'} = $idx+1;
15480:                         }
15481:                     }
15482:                 }
15483:                 $idx ++;
15484:             }
15485:         }
15486:     }
15487:     my $putresult;
15488:     if (keys(%changes) > 0) {
15489:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
15490:         if ($putresult eq 'ok') {
15491:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
15492:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
15493:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
15494:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
15495:                 }
15496:             }
15497:             my $cachetime = 24*60*60;
15498:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15499:             if (ref($lastactref) eq 'HASH') {
15500:                 $lastactref->{'domdefaults'} = 1;
15501:             }
15502:         } else {
15503:             $errors .= '<li><span class="LC_error">'.
15504:                        &mt('An error occurred storing the settings: [_1]',
15505:                            $putresult).'</span></li>';
15506:         }
15507:     }
15508:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
15509:         $resulttext = &mt('Changes made:').'<ul>';
15510:         my (%shownprivs,@levelorder);
15511:         @levelorder = ('c','d','s');
15512:         if ((keys(%changes)) && ($putresult eq 'ok')) {
15513:             foreach my $item (sort(keys(%changes))) {
15514:                 if ($item eq 'submitbugs') {
15515:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
15516:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
15517:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
15518:                 } elsif ($item eq 'customrole') {
15519:                     if (ref($changes{'customrole'}) eq 'HASH') {
15520:                         my @keyorder = ('order','desc','access','status','exc','inc');
15521:                         my %keytext = &Apache::lonlocal::texthash(
15522:                                                                    order  => 'Order',
15523:                                                                    desc   => 'Role description',
15524:                                                                    access => 'Role usage',
15525:                                                                    status => 'Allowed institutional types',
15526:                                                                    exc    => 'Allowed personnel',
15527:                                                                    inc    => 'Disallowed personnel',
15528:                         );
15529:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
15530:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
15531:                                 if ($role eq $newrole) {
15532:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
15533:                                                               $role).'<ul>';
15534:                                 } else {
15535:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
15536:                                                               $role).'<ul>';
15537:                                 }
15538:                                 foreach my $key (@keyorder) {
15539:                                     if ($changes{'customrole'}{$role}{$key}) {
15540:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
15541:                                                                   $keytext{$key},$newsettings{$role}{$key}).
15542:                                                        '</li>';
15543:                                     }
15544:                                 }
15545:                                 if (ref($changedprivs{$role}) eq 'HASH') {
15546:                                     $shownprivs{$role} = 1;
15547:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
15548:                                     foreach my $level (@levelorder) {
15549:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
15550:                                             next if ($item eq '');
15551:                                             my ($priv) = split(/\&/,$item,2);
15552:                                             if (&Apache::lonnet::plaintext($priv)) {
15553:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
15554:                                                 unless ($level eq 'c') {
15555:                                                     $resulttext .= ' ('.$lt{$level}.')';
15556:                                                 }
15557:                                                 $resulttext .= '</li>';
15558:                                             }
15559:                                         }
15560:                                     }
15561:                                     $resulttext .= '</ul>';
15562:                                 }
15563:                                 $resulttext .= '</ul></li>';
15564:                             }
15565:                         }
15566:                     }
15567:                 }
15568:             }
15569:         }
15570:         if (keys(%changedprivs)) {
15571:             foreach my $role (sort(keys(%changedprivs))) {
15572:                 unless ($shownprivs{$role}) {
15573:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
15574:                                               $role).'<ul>'.
15575:                                    '<li>'.&mt('Privileges set to :').'<ul>';
15576:                     foreach my $level (@levelorder) {
15577:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
15578:                             next if ($item eq '');
15579:                             my ($priv) = split(/\&/,$item,2);
15580:                             if (&Apache::lonnet::plaintext($priv)) {
15581:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
15582:                                 unless ($level eq 'c') {
15583:                                     $resulttext .= ' ('.$lt{$level}.')';
15584:                                 }
15585:                                 $resulttext .= '</li>';
15586:                             }
15587:                         }
15588:                     }
15589:                     $resulttext .= '</ul></li></ul></li>';
15590:                 }
15591:             }
15592:         }
15593:         $resulttext .= '</ul>';
15594:     } else {
15595:         $resulttext = &mt('No changes made to help settings');
15596:     }
15597:     if ($errors) {
15598:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
15599:                                     $errors.'</ul>';
15600:     }
15601:     return $resulttext;
15602: }
15603: 
15604: sub modify_coursedefaults {
15605:     my ($dom,$lastactref,%domconfig) = @_;
15606:     my ($resulttext,$errors,%changes,%defaultshash);
15607:     my %defaultchecked = (
15608:                            'uselcmath'       => 'on',
15609:                            'usejsme'         => 'on',
15610:                            'inline_chem'     => 'on',
15611:                          );
15612:     my @toggles = ('uselcmath','usejsme','inline_chem');
15613:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
15614:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
15615:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
15616:     my @types = ('official','unofficial','community','textbook');
15617:     my %staticdefaults = (
15618:                            anonsurvey_threshold => 10,
15619:                            uploadquota          => 500,
15620:                            postsubmit           => 60,
15621:                            mysqltables          => 172800,
15622:                          );
15623:     my %texoptions = (
15624:                         MathJax  => 'MathJax',
15625:                         mimetex  => &mt('Convert to Images'),
15626:                         tth      => &mt('TeX to HTML'),
15627:                      );
15628:     $defaultshash{'coursedefaults'} = {};
15629: 
15630:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
15631:         if ($domconfig{'coursedefaults'} eq '') {
15632:             $domconfig{'coursedefaults'} = {};
15633:         }
15634:     }
15635: 
15636:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
15637:         foreach my $item (@toggles) {
15638:             if ($defaultchecked{$item} eq 'on') {
15639:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
15640:                     ($env{'form.'.$item} eq '0')) {
15641:                     $changes{$item} = 1;
15642:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
15643:                     $changes{$item} = 1;
15644:                 }
15645:             } elsif ($defaultchecked{$item} eq 'off') {
15646:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
15647:                     ($env{'form.'.$item} eq '1')) {
15648:                     $changes{$item} = 1;
15649:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
15650:                     $changes{$item} = 1;
15651:                 }
15652:             }
15653:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
15654:         }
15655:         foreach my $item (@numbers) {
15656:             my ($currdef,$newdef);
15657:             $newdef = $env{'form.'.$item};
15658:             if ($item eq 'anonsurvey_threshold') {
15659:                 $currdef = $domconfig{'coursedefaults'}{$item};
15660:                 $newdef =~ s/\D//g;
15661:                 if ($newdef eq '' || $newdef < 1) {
15662:                     $newdef = 1;
15663:                 }
15664:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
15665:             } else {
15666:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
15667:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
15668:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
15669:                 }
15670:                 $newdef =~ s/[^\w.\-]//g;
15671:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
15672:             }
15673:             if ($currdef ne $newdef) {
15674:                 if ($item eq 'anonsurvey_threshold') {
15675:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
15676:                         $changes{$item} = 1;
15677:                     }
15678:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
15679:                     my $setting = $1;
15680:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
15681:                         $changes{$setting} = 1;
15682:                     }
15683:                 }
15684:             }
15685:         }
15686:         my $texengine;
15687:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
15688:             $texengine = $env{'form.texengine'};
15689:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
15690:             if ($currdef eq '') {
15691:                 unless ($texengine eq $Apache::lonnet::deftex) {
15692:                     $changes{'texengine'} = 1;
15693:                 }
15694:             } elsif ($currdef ne $texengine) {
15695:                 $changes{'texengine'} = 1;
15696:             }
15697:         }
15698:         if ($texengine ne '') {
15699:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
15700:         }
15701:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
15702:         my @currclonecode;
15703:         if (ref($currclone) eq 'HASH') {
15704:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
15705:                 @currclonecode = @{$currclone->{'instcode'}};
15706:             }
15707:         }
15708:         my $newclone;
15709:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
15710:             $newclone = $env{'form.canclone'};
15711:         }
15712:         if ($newclone eq 'instcode') {
15713:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
15714:             my (%codedefaults,@code_order,@clonecode);
15715:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
15716:                                                     \@code_order);
15717:             foreach my $item (@code_order) {
15718:                 if (grep(/^\Q$item\E$/,@newcodes)) {
15719:                     push(@clonecode,$item);
15720:                 }
15721:             }
15722:             if (@clonecode) {
15723:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
15724:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
15725:                 if (@diffs) {
15726:                     $changes{'canclone'} = 1;
15727:                 }
15728:             } else {
15729:                 $newclone eq '';
15730:             }
15731:         } elsif ($newclone ne '') {
15732:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
15733:         }
15734:         if ($newclone ne $currclone) {
15735:             $changes{'canclone'} = 1;
15736:         }
15737:         my %credits;
15738:         foreach my $type (@types) {
15739:             unless ($type eq 'community') {
15740:                 $credits{$type} = $env{'form.'.$type.'_credits'};
15741:                 $credits{$type} =~ s/[^\d.]+//g;
15742:             }
15743:         }
15744:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
15745:             ($env{'form.coursecredits'} eq '1')) {
15746:             $changes{'coursecredits'} = 1;
15747:             foreach my $type (keys(%credits)) {
15748:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
15749:             }
15750:         } else {
15751:             if ($env{'form.coursecredits'} eq '1') {
15752:                 foreach my $type (@types) {
15753:                     unless ($type eq 'community') {
15754:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
15755:                             $changes{'coursecredits'} = 1;
15756:                         }
15757:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
15758:                     }
15759:                 }
15760:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15761:                 foreach my $type (@types) {
15762:                     unless ($type eq 'community') {
15763:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
15764:                             $changes{'coursecredits'} = 1;
15765:                             last;
15766:                         }
15767:                     }
15768:                 }
15769:             }
15770:         }
15771:         if ($env{'form.postsubmit'} eq '1') {
15772:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
15773:             my %currtimeout;
15774:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15775:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
15776:                     $changes{'postsubmit'} = 1;
15777:                 }
15778:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15779:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
15780:                 }
15781:             } else {
15782:                 $changes{'postsubmit'} = 1;
15783:             }
15784:             foreach my $type (@types) {
15785:                 my $timeout = $env{'form.'.$type.'_timeout'};
15786:                 $timeout =~ s/\D//g;
15787:                 if ($timeout == $staticdefaults{'postsubmit'}) {
15788:                     $timeout = '';
15789:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
15790:                     $timeout = '0';
15791:                 }
15792:                 unless ($timeout eq '') {
15793:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
15794:                 }
15795:                 if (exists($currtimeout{$type})) {
15796:                     if ($timeout ne $currtimeout{$type}) {
15797:                         $changes{'postsubmit'} = 1;
15798:                     }
15799:                 } elsif ($timeout ne '') {
15800:                     $changes{'postsubmit'} = 1;
15801:                 }
15802:             }
15803:         } else {
15804:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
15805:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15806:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
15807:                     $changes{'postsubmit'} = 1;
15808:                 }
15809:             } else {
15810:                 $changes{'postsubmit'} = 1;
15811:             }
15812:         }
15813:     }
15814:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
15815:                                              $dom);
15816:     if ($putresult eq 'ok') {
15817:         if (keys(%changes) > 0) {
15818:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15819:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
15820:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
15821:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'}) ||
15822:                 ($changes{'inline_chem'})) {
15823:                 foreach my $item ('uselcmath','usejsme','inline_chem','texengine') {
15824:                     if ($changes{$item}) {
15825:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
15826:                     }
15827:                 }
15828:                 if ($changes{'coursecredits'}) {
15829:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15830:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
15831:                             $domdefaults{$type.'credits'} =
15832:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
15833:                         }
15834:                     }
15835:                 }
15836:                 if ($changes{'postsubmit'}) {
15837:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15838:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
15839:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15840:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
15841:                                 $domdefaults{$type.'postsubtimeout'} =
15842:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
15843:                             }
15844:                         }
15845:                     }
15846:                 }
15847:                 if ($changes{'uploadquota'}) {
15848:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
15849:                         foreach my $type (@types) {
15850:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
15851:                         }
15852:                     }
15853:                 }
15854:                 if ($changes{'canclone'}) {
15855:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
15856:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
15857:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
15858:                             if (@clonecodes) {
15859:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
15860:                             }
15861:                         }
15862:                     } else {
15863:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
15864:                     }
15865:                 }
15866:                 my $cachetime = 24*60*60;
15867:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15868:                 if (ref($lastactref) eq 'HASH') {
15869:                     $lastactref->{'domdefaults'} = 1;
15870:                 }
15871:             }
15872:             $resulttext = &mt('Changes made:').'<ul>';
15873:             foreach my $item (sort(keys(%changes))) {
15874:                 if ($item eq 'uselcmath') {
15875:                     if ($env{'form.'.$item} eq '1') {
15876:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
15877:                     } else {
15878:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
15879:                     }
15880:                 } elsif ($item eq 'usejsme') {
15881:                     if ($env{'form.'.$item} eq '1') {
15882:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
15883:                     } else {
15884:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
15885:                     }
15886:                 } elsif ($item eq 'inline_chem') {
15887:                     if ($env{'form.'.$item} eq '1') {
15888:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses inline previewer').'</li>';
15889:                     } else {
15890:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses pop-up previewer').'</li>';
15891:                     }
15892:                 } elsif ($item eq 'texengine') {
15893:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
15894:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
15895:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
15896:                     }
15897:                 } elsif ($item eq 'anonsurvey_threshold') {
15898:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
15899:                 } elsif ($item eq 'uploadquota') {
15900:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
15901:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
15902:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
15903:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
15904:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
15905: 
15906:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
15907:                                        '</ul>'.
15908:                                        '</li>';
15909:                     } else {
15910:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
15911:                     }
15912:                 } elsif ($item eq 'mysqltables') {
15913:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
15914:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
15915:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
15916:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
15917:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
15918:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
15919:                                        '</ul>'.
15920:                                        '</li>';
15921:                     } else {
15922:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
15923:                     }
15924:                 } elsif ($item eq 'postsubmit') {
15925:                     if ($domdefaults{'postsubmit'} eq 'off') {
15926:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
15927:                     } else {
15928:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
15929:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15930:                             $resulttext .= &mt('durations:').'<ul>';
15931:                             foreach my $type (@types) {
15932:                                 $resulttext .= '<li>';
15933:                                 my $timeout;
15934:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15935:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
15936:                                 }
15937:                                 my $display;
15938:                                 if ($timeout eq '0') {
15939:                                     $display = &mt('unlimited');
15940:                                 } elsif ($timeout eq '') {
15941:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
15942:                                 } else {
15943:                                     $display = &mt('[quant,_1,second]',$timeout);
15944:                                 }
15945:                                 if ($type eq 'community') {
15946:                                     $resulttext .= &mt('Communities');
15947:                                 } elsif ($type eq 'official') {
15948:                                     $resulttext .= &mt('Official courses');
15949:                                 } elsif ($type eq 'unofficial') {
15950:                                     $resulttext .= &mt('Unofficial courses');
15951:                                 } elsif ($type eq 'textbook') {
15952:                                     $resulttext .= &mt('Textbook courses');
15953:                                 }
15954:                                 $resulttext .= ' -- '.$display.'</li>';
15955:                             }
15956:                             $resulttext .= '</ul>';
15957:                         }
15958:                         $resulttext .= '</li>';
15959:                     }
15960:                 } elsif ($item eq 'coursecredits') {
15961:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15962:                         if (($domdefaults{'officialcredits'} eq '') &&
15963:                             ($domdefaults{'unofficialcredits'} eq '') &&
15964:                             ($domdefaults{'textbookcredits'} eq '')) {
15965:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
15966:                         } else {
15967:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
15968:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
15969:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
15970:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
15971:                                            '</ul>'.
15972:                                            '</li>';
15973:                         }
15974:                     } else {
15975:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
15976:                     }
15977:                 } elsif ($item eq 'canclone') {
15978:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
15979:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
15980:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
15981:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
15982:                         }
15983:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
15984:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
15985:                     } else {
15986:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
15987:                     }
15988:                 }
15989:             }
15990:             $resulttext .= '</ul>';
15991:         } else {
15992:             $resulttext = &mt('No changes made to course defaults');
15993:         }
15994:     } else {
15995:         $resulttext = '<span class="LC_error">'.
15996:             &mt('An error occurred: [_1]',$putresult).'</span>';
15997:     }
15998:     return $resulttext;
15999: }
16000: 
16001: sub modify_selfenrollment {
16002:     my ($dom,$lastactref,%domconfig) = @_;
16003:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
16004:     my @types = ('official','unofficial','community','textbook');
16005:     my %titles = &tool_titles();
16006:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
16007:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
16008:     $ordered{'default'} = ['types','registered','approval','limit'];
16009: 
16010:     my (%roles,%shown,%toplevel);
16011:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
16012: 
16013:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
16014:         if ($domconfig{'selfenrollment'} eq '') {
16015:             $domconfig{'selfenrollment'} = {};
16016:         }
16017:     }
16018:     %toplevel = (
16019:                   admin      => 'Configuration Rights',
16020:                   default    => 'Default settings',
16021:                   validation => 'Validation of self-enrollment requests',
16022:                 );
16023:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
16024: 
16025:     if (ref($ordered{'admin'}) eq 'ARRAY') {
16026:         foreach my $item (@{$ordered{'admin'}}) {
16027:             foreach my $type (@types) {
16028:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
16029:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
16030:                 } else {
16031:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
16032:                 }
16033:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
16034:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
16035:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
16036:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
16037:                             push(@{$changes{'admin'}{$type}},$item);
16038:                         }
16039:                     } else {
16040:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
16041:                             push(@{$changes{'admin'}{$type}},$item);
16042:                         }
16043:                     }
16044:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
16045:                     push(@{$changes{'admin'}{$type}},$item);
16046:                 }
16047:             }
16048:         }
16049:     }
16050: 
16051:     foreach my $item (@{$ordered{'default'}}) {
16052:         foreach my $type (@types) {
16053:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
16054:             if ($item eq 'types') {
16055:                 unless (($value eq 'all') || ($value eq 'dom')) {
16056:                     $value = '';
16057:                 }
16058:             } elsif ($item eq 'registered') {
16059:                 unless ($value eq '1') {
16060:                     $value = 0;
16061:                 }
16062:             } elsif ($item eq 'approval') {
16063:                 unless ($value =~ /^[012]$/) {
16064:                     $value = 0;
16065:                 }
16066:             } else {
16067:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
16068:                     $value = 'none';
16069:                 }
16070:             }
16071:             $selfenrollhash{'default'}{$type}{$item} = $value;
16072:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
16073:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
16074:                     if ($selfenrollhash{'default'}{$type}{$item} ne
16075:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
16076:                          push(@{$changes{'default'}{$type}},$item);
16077:                     }
16078:                 } else {
16079:                     push(@{$changes{'default'}{$type}},$item);
16080:                 }
16081:             } else {
16082:                 push(@{$changes{'default'}{$type}},$item);
16083:             }
16084:             if ($item eq 'limit') {
16085:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
16086:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
16087:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
16088:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
16089:                     }
16090:                 } else {
16091:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
16092:                 }
16093:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
16094:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
16095:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
16096:                          push(@{$changes{'default'}{$type}},'cap');
16097:                     }
16098:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
16099:                     push(@{$changes{'default'}{$type}},'cap');
16100:                 }
16101:             }
16102:         }
16103:     }
16104: 
16105:     foreach my $item (@{$itemsref}) {
16106:         if ($item eq 'fields') {
16107:             my @changed;
16108:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
16109:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
16110:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
16111:             }
16112:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
16113:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
16114:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
16115:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
16116:                 } else {
16117:                     @changed = @{$selfenrollhash{'validation'}{$item}};
16118:                 }
16119:             } else {
16120:                 @changed = @{$selfenrollhash{'validation'}{$item}};
16121:             }
16122:             if (@changed) {
16123:                 if ($selfenrollhash{'validation'}{$item}) { 
16124:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
16125:                 } else {
16126:                     $changes{'validation'}{$item} = &mt('None');
16127:                 }
16128:             }
16129:         } else {
16130:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
16131:             if ($item eq 'markup') {
16132:                if ($env{'form.selfenroll_validation_'.$item}) {
16133:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
16134:                }
16135:             }
16136:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
16137:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
16138:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
16139:                 }
16140:             }
16141:         }
16142:     }
16143: 
16144:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
16145:                                              $dom);
16146:     if ($putresult eq 'ok') {
16147:         if (keys(%changes) > 0) {
16148:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16149:             $resulttext = &mt('Changes made:').'<ul>';
16150:             foreach my $key ('admin','default','validation') {
16151:                 if (ref($changes{$key}) eq 'HASH') {
16152:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
16153:                     if ($key eq 'validation') {
16154:                         foreach my $item (@{$itemsref}) {
16155:                             if (exists($changes{$key}{$item})) {
16156:                                 if ($item eq 'markup') {
16157:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
16158:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
16159:                                 } else {  
16160:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
16161:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
16162:                                 }
16163:                             }
16164:                         }
16165:                     } else {
16166:                         foreach my $type (@types) {
16167:                             if ($type eq 'community') {
16168:                                 $roles{'1'} = &mt('Community personnel');
16169:                             } else {
16170:                                 $roles{'1'} = &mt('Course personnel');
16171:                             }
16172:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
16173:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
16174:                                     if ($key eq 'admin') {
16175:                                         my @mgrdc = ();
16176:                                         if (ref($ordered{$key}) eq 'ARRAY') {
16177:                                             foreach my $item (@{$ordered{'admin'}}) {
16178:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
16179:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
16180:                                                         push(@mgrdc,$item);
16181:                                                     }
16182:                                                 }
16183:                                             }
16184:                                             if (@mgrdc) {
16185:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
16186:                                             } else {
16187:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
16188:                                             }
16189:                                         }
16190:                                     } else {
16191:                                         if (ref($ordered{$key}) eq 'ARRAY') {
16192:                                             foreach my $item (@{$ordered{$key}}) {
16193:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
16194:                                                     $domdefaults{$type.'selfenroll'.$item} =
16195:                                                         $selfenrollhash{$key}{$type}{$item};
16196:                                                 }
16197:                                             }
16198:                                         }
16199:                                     }
16200:                                 }
16201:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
16202:                                 foreach my $item (@{$ordered{$key}}) {
16203:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
16204:                                         $resulttext .= '<li>';
16205:                                         if ($key eq 'admin') {
16206:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
16207:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
16208:                                         } else {
16209:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
16210:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
16211:                                         }
16212:                                         $resulttext .= '</li>';
16213:                                     }
16214:                                 }
16215:                                 $resulttext .= '</ul></li>';
16216:                             }
16217:                         }
16218:                         $resulttext .= '</ul></li>'; 
16219:                     }
16220:                 }
16221:             }
16222:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
16223:                 my $cachetime = 24*60*60;
16224:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16225:                 if (ref($lastactref) eq 'HASH') {
16226:                     $lastactref->{'domdefaults'} = 1;
16227:                 }
16228:             }
16229:             $resulttext .= '</ul>';
16230:         } else {
16231:             $resulttext = &mt('No changes made to self-enrollment settings');
16232:         }
16233:     } else {
16234:         $resulttext = '<span class="LC_error">'.
16235:             &mt('An error occurred: [_1]',$putresult).'</span>';
16236:     }
16237:     return $resulttext;
16238: }
16239: 
16240: sub modify_wafproxy {
16241:     my ($dom,$action,$lastactref,%domconfig) = @_;
16242:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16243:     my (%othercontrol,%canset,%values,%curralias,%currsaml,%currvalue,@warnings,
16244:         %wafproxy,%changes,%expirecache,%expiresaml);
16245:     foreach my $server (sort(keys(%servers))) {
16246:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
16247:         if ($serverhome eq $server) {
16248:             my $serverdom = &Apache::lonnet::host_domain($server);
16249:             if ($serverdom eq $dom) {
16250:                 $canset{$server} = 1;
16251:             }
16252:         }
16253:     }
16254:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
16255:         %{$values{$dom}} = ();
16256:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
16257:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
16258:         }
16259:         if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
16260:             %currsaml = %{$domconfig{'wafproxy'}{'saml'}};
16261:         }
16262:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
16263:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
16264:         }
16265:     }
16266:     my $output;
16267:     if (keys(%canset)) {
16268:         %{$wafproxy{'alias'}} = ();
16269:         %{$wafproxy{'saml'}} = ();
16270:         foreach my $key (sort(keys(%canset))) {
16271:             if ($env{'form.wafproxy_'.$dom}) {
16272:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
16273:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
16274:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
16275:                     $changes{'alias'} = 1;
16276:                 }
16277:                 if ($env{'form.wafproxy_alias_saml_'.$key}) {
16278:                     $wafproxy{'saml'}{$key} = 1;
16279:                 }
16280:                 if ($wafproxy{'saml'}{$key} ne $currsaml{$key}) {
16281:                     $changes{'saml'} = 1;
16282:                 }
16283:             } else {
16284:                 $wafproxy{'alias'}{$key} = '';
16285:                 $wafproxy{'saml'}{$key} = '';
16286:                 if ($curralias{$key}) {
16287:                     $changes{'alias'} = 1;
16288:                 }
16289:                 if ($currsaml{$key}) {
16290:                     $changes{'saml'} = 1;
16291:                 }
16292:             }
16293:             if ($wafproxy{'alias'}{$key} eq '') {
16294:                 if ($curralias{$key}) {
16295:                     $expirecache{$key} = 1;
16296:                 }
16297:                 delete($wafproxy{'alias'}{$key});
16298:             }
16299:             if ($wafproxy{'saml'}{$key} eq '') {
16300:                 if ($currsaml{$key}) {
16301:                     $expiresaml{$key} = 1;
16302:                 }
16303:                 delete($wafproxy{'saml'}{$key});
16304:             }
16305:         }
16306:         unless (keys(%{$wafproxy{'alias'}})) {
16307:             delete($wafproxy{'alias'});
16308:         }
16309:         unless (keys(%{$wafproxy{'saml'}})) {
16310:             delete($wafproxy{'saml'});
16311:         }
16312:         # Localization for values in %warn occurs in &mt() calls separately.
16313:         my %warn = (
16314:                      trusted => 'trusted IP range(s)',
16315:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
16316:                      vpnext => 'IP range(s) for backend WAF connections',
16317:                    );
16318:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
16319:             my $possible = $env{'form.wafproxy_'.$item};
16320:             $possible =~ s/^\s+|\s+$//g;
16321:             if ($possible ne '') {
16322:                 if ($item eq 'remoteip') {
16323:                     if ($possible =~ /^[mhn]$/) {
16324:                         $wafproxy{$item} = $possible;
16325:                     }
16326:                 } elsif ($item eq 'ipheader') {
16327:                     if ($wafproxy{'remoteip'} eq 'h') {
16328:                         $wafproxy{$item} = $possible;
16329:                     }
16330:                 } elsif ($item eq 'sslopt') {
16331:                     if ($possible =~ /^0|1$/) {
16332:                         $wafproxy{$item} = $possible;
16333:                     }
16334:                 } else {
16335:                     my (@ok,$count);
16336:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
16337:                         unless ($env{'form.wafproxy_vpnaccess'}) {
16338:                             $possible = '';
16339:                         }
16340:                     } elsif ($item eq 'trusted') {
16341:                         unless ($wafproxy{'remoteip'} eq 'h') {
16342:                             $possible = '';
16343:                         }
16344:                     }
16345:                     unless ($possible eq '') {
16346:                         $possible =~ s/[\r\n]+/\s/g;
16347:                         $possible =~ s/\s*-\s*/-/g;
16348:                         $possible =~ s/\s+/,/g;
16349:                         $possible =~ s/,+/,/g;
16350:                     }
16351:                     $count = 0;
16352:                     if ($possible ne '') {
16353:                         foreach my $poss (split(/\,/,$possible)) {
16354:                             $count ++;
16355:                             $poss = &validate_ip_pattern($poss);
16356:                             if ($poss ne '') {
16357:                                 push(@ok,$poss);
16358:                             }
16359:                         }
16360:                         my $diff = $count - scalar(@ok);
16361:                         if ($diff) {
16362:                             push(@warnings,'<li>'.
16363:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
16364:                                      $diff,$warn{$item}).
16365:                                  '</li>');
16366:                         }
16367:                         if (@ok) {
16368:                             my @cidr_list;
16369:                             foreach my $item (@ok) {
16370:                                 @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
16371:                             }
16372:                             $wafproxy{$item} = join(',',@cidr_list);
16373:                         }
16374:                     }
16375:                 }
16376:                 if ($wafproxy{$item} ne $currvalue{$item}) {
16377:                     $changes{$item} = 1;
16378:                 }
16379:             } elsif ($currvalue{$item}) {
16380:                 $changes{$item} = 1;
16381:             }
16382:         }
16383:     } else {
16384:         if (keys(%curralias)) {
16385:             $changes{'alias'} = 1;
16386:         }
16387:         if (keys(%currsaml)) {
16388:             $changes{'saml'} = 1;
16389:         }
16390:         if (keys(%currvalue)) {
16391:             foreach my $key (keys(%currvalue)) {
16392:                 $changes{$key} = 1;
16393:             }
16394:         }
16395:     }
16396:     if (keys(%changes)) {
16397:         my %defaultshash = (
16398:                               wafproxy => \%wafproxy,
16399:                            );
16400:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
16401:                                                  $dom);
16402:         if ($putresult eq 'ok') {
16403:             my $cachetime = 24*60*60;
16404:             my (%domdefaults,$updatedomdefs);
16405:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
16406:                 if ($changes{$item}) {
16407:                     unless ($updatedomdefs) {
16408:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
16409:                         $updatedomdefs = 1;
16410:                     }
16411:                     if ($wafproxy{$item}) {
16412:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
16413:                     } elsif (exists($domdefaults{'waf_'.$item})) {
16414:                         delete($domdefaults{'waf_'.$item});
16415:                     }
16416:                 }
16417:             }
16418:             if ($updatedomdefs) {
16419:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16420:                 if (ref($lastactref) eq 'HASH') {
16421:                     $lastactref->{'domdefaults'} = 1;
16422:                 }
16423:             }
16424:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
16425:                 my %updates = %expirecache;
16426:                 foreach my $key (keys(%expirecache)) {
16427:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
16428:                 }
16429:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
16430:                     my $cachetime = 24*60*60;
16431:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
16432:                         $updates{$key} = 1;
16433:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
16434:                                                       $cachetime);
16435:                     }
16436:                 }
16437:                 if (ref($lastactref) eq 'HASH') {
16438:                     $lastactref->{'proxyalias'} = \%updates;
16439:                 }
16440:             }
16441:             if ((exists($wafproxy{'saml'})) || (keys(%expiresaml))) {
16442:                 my %samlupdates = %expiresaml;
16443:                 foreach my $key (keys(%expiresaml)) {
16444:                     &Apache::lonnet::devalidate_cache_new('proxysaml',$key);
16445:                 }
16446:                 if (ref($wafproxy{'saml'}) eq 'HASH') {
16447:                     my $cachetime = 24*60*60;
16448:                     foreach my $key (keys(%{$wafproxy{'saml'}})) {
16449:                         $samlupdates{$key} = 1;
16450:                         &Apache::lonnet::do_cache_new('proxysaml',$key,$wafproxy{'saml'}{$key},
16451:                                                       $cachetime);
16452:                     }
16453:                 }
16454:                 if (ref($lastactref) eq 'HASH') {
16455:                     $lastactref->{'proxysaml'} = \%samlupdates;
16456:                 }
16457:             }
16458:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
16459:             foreach my $item ('alias','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
16460:                 if ($changes{$item}) {
16461:                     if ($item eq 'alias') {
16462:                         my $numaliased = 0;
16463:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
16464:                             my $shown;
16465:                             if (keys(%{$wafproxy{'alias'}})) {
16466:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
16467:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
16468:                                                          &Apache::lonnet::hostname($server),
16469:                                                          $wafproxy{'alias'}{$server}).'</li>';
16470:                                     $numaliased ++;
16471:                                 }
16472:                                 if ($numaliased) {
16473:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
16474:                                                           '<ul>'.$shown.'</ul>').'</li>';
16475:                                 }
16476:                             }
16477:                         }
16478:                         unless ($numaliased) {
16479:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
16480:                         }
16481:                     } elsif ($item eq 'saml') {
16482:                         my $shown;
16483:                         if (ref($wafproxy{'saml'}) eq 'HASH') {
16484:                             if (keys(%{$wafproxy{'saml'}})) {
16485:                                 $shown = join(', ',sort(keys(%{$wafproxy{'saml'}})));
16486:                             }
16487:                         }
16488:                         if ($shown) {
16489:                             $output .= '<li>'.&mt('Alias used by SSO Auth for: [_1]',
16490:                                                   $shown).'</li>';
16491:                         } else {
16492:                             $output .= '<li>'.&mt('No alias used for SSO Auth').'</li>';
16493:                         }
16494:                     } else {
16495:                         if ($item eq 'remoteip') {
16496:                             my %ip_methods = &remoteip_methods();
16497:                             if ($wafproxy{$item} =~ /^[mh]$/) {
16498:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
16499:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
16500:                             } else {
16501:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
16502:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
16503:                                                '</li>';
16504:                                 } else {
16505:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
16506:                                 }
16507:                             }
16508:                         } elsif ($item eq 'ipheader') {
16509:                             if ($wafproxy{$item}) {
16510:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
16511:                                                       $wafproxy{$item}).'</li>';
16512:                             } else {
16513:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
16514:                             }
16515:                         } elsif ($item eq 'trusted') {
16516:                             if ($wafproxy{$item}) {
16517:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
16518:                                                       $wafproxy{$item}).'</li>';
16519:                             } else {
16520:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
16521:                             }
16522:                         } elsif ($item eq 'vpnint') {
16523:                             if ($wafproxy{$item}) {
16524:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
16525:                                                        $wafproxy{$item}).'</li>';
16526:                             } else {
16527:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
16528:                             }
16529:                         } elsif ($item eq 'vpnext') {
16530:                             if ($wafproxy{$item}) {
16531:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
16532:                                                        $wafproxy{$item}).'</li>';
16533:                             } else {
16534:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
16535:                             }
16536:                         } elsif ($item eq 'sslopt') {
16537:                             if ($wafproxy{$item}) {
16538:                                 $output .= '<li>'.&mt('WAF/Reverse Proxy expected to forward requests to https on LON-CAPA node, regardless of original protocol in web browser (http or https).').'</li>';
16539:                             } else {
16540:                                 $output .= '<li>'.&mt('WAF/Reverse Proxy expected to preserve original protocol in web browser (either http or https) when forwarding to LON-CAPA node.').'</li>';
16541:                             }
16542:                         }
16543:                     }
16544:                 }
16545:             }
16546:         } else {
16547:             $output = '<span class="LC_error">'.
16548:                       &mt('An error occurred: [_1]',$putresult).'</span>';
16549:         }
16550:     } elsif (keys(%canset)) {
16551:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
16552:     }
16553:     if (@warnings) {
16554:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
16555:                        join("\n",@warnings).'</ul>';
16556:     }
16557:     return $output;
16558: }
16559: 
16560: sub validate_ip_pattern {
16561:     my ($pattern) = @_;
16562:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
16563:         my ($start,$end) = ($1,$2);
16564:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
16565:             if (($start !~ m{/}) && ($end !~ m{/})) {
16566:                 return $start.'-'.$end;
16567:             }
16568:         }
16569:     } elsif ($pattern ne '') {
16570:         $pattern = &Net::CIDR::cidrvalidate($pattern);
16571:         if ($pattern ne '') {
16572:             return $pattern;
16573:         }
16574:     }
16575:     return;
16576: }
16577: 
16578: sub modify_usersessions {
16579:     my ($dom,$lastactref,%domconfig) = @_;
16580:     my @hostingtypes = ('version','excludedomain','includedomain');
16581:     my @offloadtypes = ('primary','default');
16582:     my %types = (
16583:                   remote => \@hostingtypes,
16584:                   hosted => \@hostingtypes,
16585:                   spares => \@offloadtypes,
16586:                 );
16587:     my @prefixes = ('remote','hosted','spares');
16588:     my @lcversions = &Apache::lonnet::all_loncaparevs();
16589:     my (%by_ip,%by_location,@intdoms);
16590:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
16591:     my @locations = sort(keys(%by_location));
16592:     my (%defaultshash,%changes);
16593:     foreach my $prefix (@prefixes) {
16594:         $defaultshash{'usersessions'}{$prefix} = {};
16595:     }
16596:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16597:     my $resulttext;
16598:     my %iphost = &Apache::lonnet::get_iphost();
16599:     foreach my $prefix (@prefixes) {
16600:         next if ($prefix eq 'spares');
16601:         foreach my $type (@{$types{$prefix}}) {
16602:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
16603:             if ($type eq 'version') {
16604:                 my $value = $env{'form.'.$prefix.'_'.$type};
16605:                 my $okvalue;
16606:                 if ($value ne '') {
16607:                     if (grep(/^\Q$value\E$/,@lcversions)) {
16608:                         $okvalue = $value;
16609:                     }
16610:                 }
16611:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
16612:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
16613:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
16614:                             if ($inuse == 0) {
16615:                                 $changes{$prefix}{$type} = 1;
16616:                             } else {
16617:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
16618:                                     $changes{$prefix}{$type} = 1;
16619:                                 }
16620:                                 if ($okvalue ne '') {
16621:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16622:                                 } 
16623:                             }
16624:                         } else {
16625:                             if (($inuse == 1) && ($okvalue ne '')) {
16626:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16627:                                 $changes{$prefix}{$type} = 1;
16628:                             }
16629:                         }
16630:                     } else {
16631:                         if (($inuse == 1) && ($okvalue ne '')) {
16632:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16633:                             $changes{$prefix}{$type} = 1;
16634:                         }
16635:                     }
16636:                 } else {
16637:                     if (($inuse == 1) && ($okvalue ne '')) {
16638:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
16639:                         $changes{$prefix}{$type} = 1;
16640:                     }
16641:                 }
16642:             } else {
16643:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
16644:                 my @okvals;
16645:                 foreach my $val (@vals) {
16646:                     if ($val =~ /:/) {
16647:                         my @items = split(/:/,$val);
16648:                         foreach my $item (@items) {
16649:                             if (ref($by_location{$item}) eq 'ARRAY') {
16650:                                 push(@okvals,$item);
16651:                             }
16652:                         }
16653:                     } else {
16654:                         if (ref($by_location{$val}) eq 'ARRAY') {
16655:                             push(@okvals,$val);
16656:                         }
16657:                     }
16658:                 }
16659:                 @okvals = sort(@okvals);
16660:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
16661:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
16662:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
16663:                             if ($inuse == 0) {
16664:                                 $changes{$prefix}{$type} = 1; 
16665:                             } else {
16666:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16667:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
16668:                                 if (@changed > 0) {
16669:                                     $changes{$prefix}{$type} = 1;
16670:                                 }
16671:                             }
16672:                         } else {
16673:                             if ($inuse == 1) {
16674:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16675:                                 $changes{$prefix}{$type} = 1;
16676:                             }
16677:                         } 
16678:                     } else {
16679:                         if ($inuse == 1) {
16680:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16681:                             $changes{$prefix}{$type} = 1;
16682:                         }
16683:                     }
16684:                 } else {
16685:                     if ($inuse == 1) {
16686:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16687:                         $changes{$prefix}{$type} = 1;
16688:                     }
16689:                 }
16690:             }
16691:         }
16692:     }
16693: 
16694:     my @alldoms = &Apache::lonnet::all_domains();
16695:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16696:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
16697:     my $savespares;
16698: 
16699:     foreach my $lonhost (sort(keys(%servers))) {
16700:         my $serverhomeID =
16701:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
16702:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
16703:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
16704:         my %spareschg;
16705:         foreach my $type (@{$types{'spares'}}) {
16706:             my @okspares;
16707:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
16708:             foreach my $server (@checked) {
16709:                 if (&Apache::lonnet::hostname($server) ne '') {
16710:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
16711:                         unless (grep(/^\Q$server\E$/,@okspares)) {
16712:                             push(@okspares,$server);
16713:                         }
16714:                     }
16715:                 }
16716:             }
16717:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
16718:             my $newspare;
16719:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
16720:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
16721:                     $newspare = $new;
16722:                 }
16723:             }
16724:             my @spares;
16725:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
16726:                 @spares = sort(@okspares,$newspare);
16727:             } else {
16728:                 @spares = sort(@okspares);
16729:             }
16730:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
16731:             if (ref($spareid{$lonhost}) eq 'HASH') {
16732:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
16733:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
16734:                     if (@diffs > 0) {
16735:                         $spareschg{$type} = 1;
16736:                     }
16737:                 }
16738:             }
16739:         }
16740:         if (keys(%spareschg) > 0) {
16741:             $changes{'spares'}{$lonhost} = \%spareschg;
16742:         }
16743:     }
16744:     $defaultshash{'usersessions'}{'offloadnow'} = {};
16745:     $defaultshash{'usersessions'}{'offloadoth'} = {};
16746:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
16747:     my @okoffload;
16748:     if (@offloadnow) {
16749:         foreach my $server (@offloadnow) {
16750:             if (&Apache::lonnet::hostname($server) ne '') {
16751:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
16752:                     push(@okoffload,$server);
16753:                 }
16754:             }
16755:         }
16756:         if (@okoffload) {
16757:             foreach my $lonhost (@okoffload) {
16758:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
16759:             }
16760:         }
16761:     }
16762:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
16763:     my @okoffloadoth;
16764:     if (@offloadoth) {
16765:         foreach my $server (@offloadoth) {
16766:             if (&Apache::lonnet::hostname($server) ne '') {
16767:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
16768:                     push(@okoffloadoth,$server);
16769:                 }
16770:             }
16771:         }
16772:         if (@okoffloadoth) {
16773:             foreach my $lonhost (@okoffloadoth) {
16774:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
16775:             }
16776:         }
16777:     }
16778:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
16779:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
16780:             if (ref($changes{'spares'}) eq 'HASH') {
16781:                 if (keys(%{$changes{'spares'}}) > 0) {
16782:                     $savespares = 1;
16783:                 }
16784:             }
16785:         } else {
16786:             $savespares = 1;
16787:         }
16788:         foreach my $offload ('offloadnow','offloadoth') {
16789:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
16790:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
16791:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
16792:                         $changes{$offload} = 1;
16793:                         last;
16794:                     }
16795:                 }
16796:                 unless ($changes{$offload}) {
16797:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
16798:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
16799:                             $changes{$offload} = 1;
16800:                             last;
16801:                         }
16802:                     }
16803:                 }
16804:             } else {
16805:                 if (($offload eq 'offloadnow') && (@okoffload)) {
16806:                      $changes{'offloadnow'} = 1;
16807:                 }
16808:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
16809:                     $changes{'offloadoth'} = 1;
16810:                 }
16811:             }
16812:         }
16813:     } else {
16814:         if (@okoffload) {
16815:             $changes{'offloadnow'} = 1;
16816:         }
16817:         if (@okoffloadoth) {
16818:             $changes{'offloadoth'} = 1;
16819:         }
16820:     }
16821:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
16822:     if ((keys(%changes) > 0) || ($savespares)) {
16823:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
16824:                                                  $dom);
16825:         if ($putresult eq 'ok') {
16826:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
16827:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
16828:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
16829:                 }
16830:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
16831:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
16832:                 }
16833:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
16834:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
16835:                 }
16836:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
16837:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
16838:                 }
16839:             }
16840:             my $cachetime = 24*60*60;
16841:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16842:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
16843:             if (ref($lastactref) eq 'HASH') {
16844:                 $lastactref->{'domdefaults'} = 1;
16845:                 $lastactref->{'usersessions'} = 1;
16846:             }
16847:             if (keys(%changes) > 0) {
16848:                 my %lt = &usersession_titles();
16849:                 $resulttext = &mt('Changes made:').'<ul>';
16850:                 foreach my $prefix (@prefixes) {
16851:                     if (ref($changes{$prefix}) eq 'HASH') {
16852:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
16853:                         if ($prefix eq 'spares') {
16854:                             if (ref($changes{$prefix}) eq 'HASH') {
16855:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
16856:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
16857:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
16858:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
16859:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
16860:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
16861:                                         foreach my $type (@{$types{$prefix}}) {
16862:                                             if ($changes{$prefix}{$lonhost}{$type}) {
16863:                                                 my $offloadto = &mt('None');
16864:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
16865:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
16866:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
16867:                                                     }
16868:                                                 }
16869:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
16870:                                             }
16871:                                         }
16872:                                     }
16873:                                     $resulttext .= '</li>';
16874:                                 }
16875:                             }
16876:                         } else {
16877:                             foreach my $type (@{$types{$prefix}}) {
16878:                                 if (defined($changes{$prefix}{$type})) {
16879:                                     my $newvalue;
16880:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
16881:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
16882:                                             if ($type eq 'version') {
16883:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
16884:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
16885:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
16886:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
16887:                                                 }
16888:                                             }
16889:                                         }
16890:                                     }
16891:                                     if ($newvalue eq '') {
16892:                                         if ($type eq 'version') {
16893:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
16894:                                         } else {
16895:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
16896:                                         }
16897:                                     } else {
16898:                                         if ($type eq 'version') {
16899:                                             $newvalue .= ' '.&mt('(or later)'); 
16900:                                         }
16901:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
16902:                                     }
16903:                                 }
16904:                             }
16905:                         }
16906:                         $resulttext .= '</ul>';
16907:                     }
16908:                 }
16909:                 if ($changes{'offloadnow'}) {
16910:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
16911:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
16912:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
16913:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
16914:                                 $resulttext .= '<li>'.$lonhost.'</li>';
16915:                             }
16916:                             $resulttext .= '</ul>';
16917:                         } else {
16918:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
16919:                         }
16920:                     } else {
16921:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
16922:                     }
16923:                 }
16924:                 if ($changes{'offloadoth'}) {
16925:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
16926:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
16927:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
16928:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
16929:                                 $resulttext .= '<li>'.$lonhost.'</li>';
16930:                             }
16931:                             $resulttext .= '</ul>';
16932:                         } else {
16933:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
16934:                         }
16935:                     } else {
16936:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
16937:                     }
16938:                 }
16939:                 $resulttext .= '</ul>';
16940:             } else {
16941:                 $resulttext = $nochgmsg;
16942:             }
16943:         } else {
16944:             $resulttext = '<span class="LC_error">'.
16945:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16946:         }
16947:     } else {
16948:         $resulttext = $nochgmsg;
16949:     }
16950:     return $resulttext;
16951: }
16952: 
16953: sub modify_loadbalancing {
16954:     my ($dom,%domconfig) = @_;
16955:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
16956:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
16957:     my ($othertitle,$usertypes,$types) =
16958:         &Apache::loncommon::sorted_inst_types($dom);
16959:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16960:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
16961:     my @sparestypes = ('primary','default');
16962:     my %typetitles = &sparestype_titles();
16963:     my $resulttext;
16964:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
16965:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
16966:         %existing = %{$domconfig{'loadbalancing'}};
16967:     }
16968:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
16969:                               \%currtargets,\%currrules,\%currcookies);
16970:     my ($saveloadbalancing,%defaultshash,%changes);
16971:     my ($alltypes,$othertypes,$titles) =
16972:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
16973:     my %ruletitles = &offloadtype_text();
16974:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
16975:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
16976:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
16977:         if ($balancer eq '') {
16978:             next;
16979:         }
16980:         if (!exists($servers{$balancer})) {
16981:             if (exists($currbalancer{$balancer})) {
16982:                 push(@{$changes{'delete'}},$balancer);
16983:             }
16984:             next;
16985:         }
16986:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
16987:             push(@{$changes{'delete'}},$balancer);
16988:             next;
16989:         }
16990:         if (!exists($currbalancer{$balancer})) {
16991:             push(@{$changes{'add'}},$balancer);
16992:         }
16993:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
16994:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
16995:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
16996:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
16997:             $saveloadbalancing = 1;
16998:         }
16999:         foreach my $sparetype (@sparestypes) {
17000:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
17001:             my @offloadto;
17002:             foreach my $target (@targets) {
17003:                 if (($servers{$target}) && ($target ne $balancer)) {
17004:                     if ($sparetype eq 'default') {
17005:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
17006:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
17007:                         }
17008:                     }
17009:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
17010:                         push(@offloadto,$target);
17011:                     }
17012:                 }
17013:             }
17014:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
17015:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
17016:                     push(@offloadto,$balancer);
17017:                 }
17018:             }
17019:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
17020:         }
17021:         if ($env{'form.loadbalancing_cookie_'.$i}) {
17022:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
17023:             if (exists($currbalancer{$balancer})) {
17024:                 unless ($currcookies{$balancer}) {
17025:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
17026:                 }
17027:             }
17028:         } elsif (exists($currbalancer{$balancer})) {
17029:             if ($currcookies{$balancer}) {
17030:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
17031:             }
17032:         }
17033:         if (ref($currtargets{$balancer}) eq 'HASH') {
17034:             foreach my $sparetype (@sparestypes) {
17035:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
17036:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
17037:                     if (@targetdiffs > 0) {
17038:                         $changes{'curr'}{$balancer}{'targets'} = 1;
17039:                     }
17040:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17041:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
17042:                         $changes{'curr'}{$balancer}{'targets'} = 1;
17043:                     }
17044:                 }
17045:             }
17046:         } else {
17047:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
17048:                 foreach my $sparetype (@sparestypes) {
17049:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17050:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
17051:                             $changes{'curr'}{$balancer}{'targets'} = 1;
17052:                         }
17053:                     }
17054:                 }
17055:             }
17056:         }
17057:         my $ishomedom;
17058:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
17059:             $ishomedom = 1;
17060:         }
17061:         if (ref($alltypes) eq 'ARRAY') {
17062:             foreach my $type (@{$alltypes}) {
17063:                 my $rule;
17064:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
17065:                          (!$ishomedom)) {
17066:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
17067:                 }
17068:                 if ($rule eq 'specific') {
17069:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
17070:                     if (exists($servers{$specifiedhost})) {
17071:                         $rule = $specifiedhost;
17072:                     }
17073:                 }
17074:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
17075:                 if (ref($currrules{$balancer}) eq 'HASH') {
17076:                     if ($rule ne $currrules{$balancer}{$type}) {
17077:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
17078:                     }
17079:                 } elsif ($rule ne '') {
17080:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
17081:                 }
17082:             }
17083:         }
17084:     }
17085:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
17086:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
17087:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
17088:             $defaultshash{'loadbalancing'} = {};
17089:         }
17090:         my $putresult = &Apache::lonnet::put_dom('configuration',
17091:                                                  \%defaultshash,$dom);
17092:         if ($putresult eq 'ok') {
17093:             if (keys(%changes) > 0) {
17094:                 my %toupdate;
17095:                 if (ref($changes{'delete'}) eq 'ARRAY') {
17096:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
17097:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
17098:                         $toupdate{$balancer} = 1;
17099:                     }
17100:                 }
17101:                 if (ref($changes{'add'}) eq 'ARRAY') {
17102:                     foreach my $balancer (sort(@{$changes{'add'}})) {
17103:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
17104:                         $toupdate{$balancer} = 1;
17105:                     }
17106:                 }
17107:                 if (ref($changes{'curr'}) eq 'HASH') {
17108:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
17109:                         $toupdate{$balancer} = 1;
17110:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
17111:                             if ($changes{'curr'}{$balancer}{'targets'}) {
17112:                                 my %offloadstr;
17113:                                 foreach my $sparetype (@sparestypes) {
17114:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17115:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
17116:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
17117:                                         }
17118:                                     }
17119:                                 }
17120:                                 if (keys(%offloadstr) == 0) {
17121:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
17122:                                 } else {
17123:                                     my $showoffload;
17124:                                     foreach my $sparetype (@sparestypes) {
17125:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
17126:                                         if (defined($offloadstr{$sparetype})) {
17127:                                             $showoffload .= $offloadstr{$sparetype};
17128:                                         } else {
17129:                                             $showoffload .= &mt('None');
17130:                                         }
17131:                                         $showoffload .= ('&nbsp;'x3);
17132:                                     }
17133:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
17134:                                 }
17135:                             }
17136:                         }
17137:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
17138:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
17139:                                 foreach my $type (@{$alltypes}) {
17140:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
17141:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
17142:                                         my $balancetext;
17143:                                         if ($rule eq '') {
17144:                                             $balancetext =  $ruletitles{'default'};
17145:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
17146:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
17147:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
17148:                                                 foreach my $sparetype (@sparestypes) {
17149:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
17150:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
17151:                                                     }
17152:                                                 }
17153:                                                 foreach my $item (@{$alltypes}) {
17154:                                                     next if ($item =~  /^_LC_ipchange/);
17155:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
17156:                                                     if ($hasrule eq 'homeserver') {
17157:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
17158:                                                     } else {
17159:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
17160:                                                             if ($servers{$hasrule}) {
17161:                                                                 $toupdate{$hasrule} = 1;
17162:                                                             }
17163:                                                         }
17164:                                                     }
17165:                                                 }
17166:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
17167:                                                     $balancetext =  $ruletitles{$rule};
17168:                                                 } else {
17169:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
17170:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
17171:                                                     if ($receiver) {
17172:                                                         $toupdate{$receiver};
17173:                                                     }
17174:                                                 }
17175:                                             } else {
17176:                                                 $balancetext =  $ruletitles{$rule};
17177:                                             }
17178:                                         } else {
17179:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
17180:                                         }
17181:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
17182:                                     }
17183:                                 }
17184:                             }
17185:                         }
17186:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
17187:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
17188:                                                       $balancer).'</li>';
17189:                         }
17190:                     }
17191:                 }
17192:                 if (keys(%toupdate)) {
17193:                     my %thismachine;
17194:                     my $updatedhere;
17195:                     my $cachetime = 60*60*24;
17196:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
17197:                     foreach my $lonhost (keys(%toupdate)) {
17198:                         if ($thismachine{$lonhost}) {
17199:                             unless ($updatedhere) {
17200:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
17201:                                                               $defaultshash{'loadbalancing'},
17202:                                                               $cachetime);
17203:                                 $updatedhere = 1;
17204:                             }
17205:                         } else {
17206:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
17207:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
17208:                         }
17209:                     }
17210:                 }
17211:                 if ($resulttext ne '') {
17212:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
17213:                 } else {
17214:                     $resulttext = $nochgmsg;
17215:                 }
17216:             } else {
17217:                 $resulttext = $nochgmsg;
17218:             }
17219:         } else {
17220:             $resulttext = '<span class="LC_error">'.
17221:                           &mt('An error occurred: [_1]',$putresult).'</span>';
17222:         }
17223:     } else {
17224:         $resulttext = $nochgmsg;
17225:     }
17226:     return $resulttext;
17227: }
17228: 
17229: sub recurse_check {
17230:     my ($chkcats,$categories,$depth,$name) = @_;
17231:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
17232:         my $chg = 0;
17233:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
17234:             my $category = $chkcats->[$depth]{$name}[$j];
17235:             my $item;
17236:             if ($category eq '') {
17237:                 $chg ++;
17238:             } else {
17239:                 my $deeper = $depth + 1;
17240:                 $item = &escape($category).':'.&escape($name).':'.$depth;
17241:                 if ($chg) {
17242:                     $categories->{$item} -= $chg;
17243:                 }
17244:                 &recurse_check($chkcats,$categories,$deeper,$category);
17245:                 $deeper --;
17246:             }
17247:         }
17248:     }
17249:     return;
17250: }
17251: 
17252: sub recurse_cat_deletes {
17253:     my ($item,$coursecategories,$deletions) = @_;
17254:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
17255:     my $subdepth = $depth + 1;
17256:     if (ref($coursecategories) eq 'HASH') {
17257:         foreach my $subitem (keys(%{$coursecategories})) {
17258:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
17259:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
17260:                 delete($coursecategories->{$subitem});
17261:                 $deletions->{$subitem} = 1;
17262:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
17263:             }
17264:         }
17265:     }
17266:     return;
17267: }
17268: 
17269: sub active_dc_picker {
17270:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
17271:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
17272:     my @domcoord = keys(%domcoords);
17273:     if (keys(%currhash)) {
17274:         foreach my $dc (keys(%currhash)) {
17275:             unless (exists($domcoords{$dc})) {
17276:                 push(@domcoord,$dc);
17277:             }
17278:         }
17279:     }
17280:     @domcoord = sort(@domcoord);
17281:     my $numdcs = scalar(@domcoord);
17282:     my $rows = 0;
17283:     my $table;
17284:     if ($numdcs > 1) {
17285:         $table = '<table>';
17286:         for (my $i=0; $i<@domcoord; $i++) {
17287:             my $rem = $i%($numinrow);
17288:             if ($rem == 0) {
17289:                 if ($i > 0) {
17290:                     $table .= '</tr>';
17291:                 }
17292:                 $table .= '<tr>';
17293:                 $rows ++;
17294:             }
17295:             my $check = '';
17296:             if ($inputtype eq 'radio') {
17297:                 if (keys(%currhash) == 0) {
17298:                     if (!$i) {
17299:                         $check = ' checked="checked"';
17300:                     }
17301:                 } elsif (exists($currhash{$domcoord[$i]})) {
17302:                     $check = ' checked="checked"';
17303:                 }
17304:             } else {
17305:                 if (exists($currhash{$domcoord[$i]})) {
17306:                     $check = ' checked="checked"';
17307:                 }
17308:             }
17309:             if ($i == @domcoord - 1) {
17310:                 my $colsleft = $numinrow - $rem;
17311:                 if ($colsleft > 1) {
17312:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
17313:                 } else {
17314:                     $table .= '<td class="LC_left_item">';
17315:                 }
17316:             } else {
17317:                 $table .= '<td class="LC_left_item">';
17318:             }
17319:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
17320:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
17321:             $table .= '<span class="LC_nobreak"><label>'.
17322:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
17323:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
17324:             if ($user ne $dcname.':'.$dcdom) {
17325:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
17326:             }
17327:             $table .= '</label></span></td>';
17328:         }
17329:         $table .= '</tr></table>';
17330:     } elsif ($numdcs == 1) {
17331:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
17332:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
17333:         if ($inputtype eq 'radio') {
17334:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
17335:             if ($user ne $dcname.':'.$dcdom) {
17336:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
17337:             }
17338:         } else {
17339:             my $check;
17340:             if (exists($currhash{$domcoord[0]})) {
17341:                 $check = ' checked="checked"';
17342:             }
17343:             $table = '<span class="LC_nobreak"><label>'.
17344:                      '<input type="checkbox" name="'.$name.'" '.
17345:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
17346:             if ($user ne $dcname.':'.$dcdom) {
17347:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
17348:             }
17349:             $table .= '</label></span>';
17350:             $rows ++;
17351:         }
17352:     }
17353:     return ($numdcs,$table,$rows);
17354: }
17355: 
17356: sub usersession_titles {
17357:     return &Apache::lonlocal::texthash(
17358:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
17359:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
17360:                spares => 'Servers offloaded to, when busy',
17361:                version => 'LON-CAPA version requirement',
17362:                excludedomain => 'Allow all, but exclude specific domains',
17363:                includedomain => 'Deny all, but include specific domains',
17364:                primary => 'Primary (checked first)',
17365:                default => 'Default',
17366:            );
17367: }
17368: 
17369: sub id_for_thisdom {
17370:     my (%servers) = @_;
17371:     my %altids;
17372:     foreach my $server (keys(%servers)) {
17373:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
17374:         if ($serverhome ne $server) {
17375:             $altids{$serverhome} = $server;
17376:         }
17377:     }
17378:     return %altids;
17379: }
17380: 
17381: sub count_servers {
17382:     my ($currbalancer,%servers) = @_;
17383:     my (@spares,$numspares);
17384:     foreach my $lonhost (sort(keys(%servers))) {
17385:         next if ($currbalancer eq $lonhost);
17386:         push(@spares,$lonhost);
17387:     }
17388:     if ($currbalancer) {
17389:         $numspares = scalar(@spares);
17390:     } else {
17391:         $numspares = scalar(@spares) - 1;
17392:     }
17393:     return ($numspares,@spares);
17394: }
17395: 
17396: sub lonbalance_targets_js {
17397:     my ($dom,$types,$servers,$settings) = @_;
17398:     my $select = &mt('Select');
17399:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
17400:     if (ref($servers) eq 'HASH') {
17401:         $alltargets = join("','",sort(keys(%{$servers})));
17402:         my @homedoms;
17403:         foreach my $server (sort(keys(%{$servers}))) {
17404:             if (&Apache::lonnet::host_domain($server) eq $dom) {
17405:                 push(@homedoms,'1');
17406:             } else {
17407:                 push(@homedoms,'0');
17408:             }
17409:         }
17410:         $allishome = join("','",@homedoms);
17411:     }
17412:     if (ref($types) eq 'ARRAY') {
17413:         if (@{$types} > 0) {
17414:             @alltypes = @{$types};
17415:         }
17416:     }
17417:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
17418:     $allinsttypes = join("','",@alltypes);
17419:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
17420:     if (ref($settings) eq 'HASH') {
17421:         %existing = %{$settings};
17422:     }
17423:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
17424:                               \%currtargets,\%currrules,\%currcookies);
17425:     my $balancers = join("','",sort(keys(%currbalancer)));
17426:     return <<"END";
17427: 
17428: <script type="text/javascript">
17429: // <![CDATA[
17430: 
17431: currBalancers = new Array('$balancers');
17432: 
17433: function toggleTargets(balnum) {
17434:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
17435:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
17436:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
17437:     var prevbalancer = prevhostitem.value;
17438:     var baltotal = document.getElementById('loadbalancing_total').value;
17439:     prevhostitem.value = balancer;
17440:     if (prevbalancer != '') {
17441:         var prevIdx = currBalancers.indexOf(prevbalancer);
17442:         if (prevIdx != -1) {
17443:             currBalancers.splice(prevIdx,1);
17444:         }
17445:     }
17446:     if (balancer == '') {
17447:         hideSpares(balnum);
17448:     } else {
17449:         var currIdx = currBalancers.indexOf(balancer);
17450:         if (currIdx == -1) {
17451:             currBalancers.push(balancer);
17452:         }
17453:         var homedoms = new Array('$allishome');
17454:         var ishomedom = homedoms[lonhostitem.selectedIndex];
17455:         showSpares(balancer,ishomedom,balnum);
17456:     }
17457:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
17458:     return;
17459: }
17460: 
17461: function showSpares(balancer,ishomedom,balnum) {
17462:     var alltargets = new Array('$alltargets');
17463:     var insttypes = new Array('$allinsttypes');
17464:     var offloadtypes = new Array('primary','default');
17465: 
17466:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
17467:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
17468:  
17469:     for (var i=0; i<offloadtypes.length; i++) {
17470:         var count = 0;
17471:         for (var j=0; j<alltargets.length; j++) {
17472:             if (alltargets[j] != balancer) {
17473:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
17474:                 item.value = alltargets[j];
17475:                 item.style.textAlign='left';
17476:                 item.style.textFace='normal';
17477:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
17478:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
17479:                     item.disabled = '';
17480:                 } else {
17481:                     item.disabled = 'disabled';
17482:                     item.checked = false;
17483:                 }
17484:                 count ++;
17485:             }
17486:         }
17487:     }
17488:     for (var k=0; k<insttypes.length; k++) {
17489:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
17490:             if (ishomedom == 1) {
17491:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
17492:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
17493:             } else {
17494:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
17495:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
17496:             }
17497:         } else {
17498:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
17499:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
17500:         }
17501:         if ((insttypes[k] != '_LC_external') && 
17502:             ((insttypes[k] != '_LC_internetdom') ||
17503:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
17504:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
17505:             item.options.length = 0;
17506:             item.options[0] = new Option("","",true,true);
17507:             var idx = 0;
17508:             for (var m=0; m<alltargets.length; m++) {
17509:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
17510:                     idx ++;
17511:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
17512:                 }
17513:             }
17514:         }
17515:     }
17516:     return;
17517: }
17518: 
17519: function hideSpares(balnum) {
17520:     var alltargets = new Array('$alltargets');
17521:     var insttypes = new Array('$allinsttypes');
17522:     var offloadtypes = new Array('primary','default');
17523: 
17524:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
17525:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
17526: 
17527:     var total = alltargets.length - 1;
17528:     for (var i=0; i<offloadtypes; i++) {
17529:         for (var j=0; j<total; j++) {
17530:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
17531:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
17532:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
17533:         }
17534:     }
17535:     for (var k=0; k<insttypes.length; k++) {
17536:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
17537:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
17538:         if (insttypes[k] != '_LC_external') {
17539:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
17540:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
17541:         }
17542:     }
17543:     return;
17544: }
17545: 
17546: function checkOffloads(item,balnum,type) {
17547:     var alltargets = new Array('$alltargets');
17548:     var offloadtypes = new Array('primary','default');
17549:     if (item.checked) {
17550:         var total = alltargets.length - 1;
17551:         var other;
17552:         if (type == offloadtypes[0]) {
17553:             other = offloadtypes[1];
17554:         } else {
17555:             other = offloadtypes[0];
17556:         }
17557:         for (var i=0; i<total; i++) {
17558:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
17559:             if (server == item.value) {
17560:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
17561:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
17562:                 }
17563:             }
17564:         }
17565:     }
17566:     return;
17567: }
17568: 
17569: function singleServerToggle(balnum,type) {
17570:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
17571:     if (offloadtoSelIdx == 0) {
17572:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
17573:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
17574: 
17575:     } else {
17576:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
17577:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
17578:     }
17579:     return;
17580: }
17581: 
17582: function balanceruleChange(formname,balnum,type) {
17583:     if (type == '_LC_external') {
17584:         return;
17585:     }
17586:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
17587:     for (var i=0; i<typesRules.length; i++) {
17588:         if (formname.elements[typesRules[i]].checked) {
17589:             if (formname.elements[typesRules[i]].value != 'specific') {
17590:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
17591:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
17592:             } else {
17593:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
17594:             }
17595:         }
17596:     }
17597:     return;
17598: }
17599: 
17600: function balancerDeleteChange(balnum) {
17601:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
17602:     var baltotal = document.getElementById('loadbalancing_total').value;
17603:     var addtarget;
17604:     var removetarget;
17605:     var action = 'delete';
17606:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
17607:         var lonhost = hostitem.value;
17608:         var currIdx = currBalancers.indexOf(lonhost);
17609:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
17610:             if (currIdx != -1) {
17611:                 currBalancers.splice(currIdx,1);
17612:             }
17613:             addtarget = lonhost;
17614:         } else {
17615:             if (currIdx == -1) {
17616:                 currBalancers.push(lonhost);
17617:             }
17618:             removetarget = lonhost;
17619:             action = 'undelete';
17620:         }
17621:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
17622:     }
17623:     return;
17624: }
17625: 
17626: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
17627:     if (baltotal > 1) {
17628:         var offloadtypes = new Array('primary','default');
17629:         var alltargets = new Array('$alltargets');
17630:         var insttypes = new Array('$allinsttypes');
17631:         for (var i=0; i<baltotal; i++) {
17632:             if (i != balnum) {
17633:                 for (var j=0; j<offloadtypes.length; j++) {
17634:                     var total = alltargets.length - 1;
17635:                     for (var k=0; k<total; k++) {
17636:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
17637:                         var server = serveritem.value;
17638:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
17639:                             if (server == addtarget) {
17640:                                 serveritem.disabled = '';
17641:                             }
17642:                         }
17643:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
17644:                             if (server == removetarget) {
17645:                                 serveritem.disabled = 'disabled';
17646:                                 serveritem.checked = false;
17647:                             }
17648:                         }
17649:                     }
17650:                 }
17651:                 for (var j=0; j<insttypes.length; j++) {
17652:                     if (insttypes[j] != '_LC_external') {
17653:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
17654:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
17655:                             var currSel = singleserver.selectedIndex;
17656:                             var currVal = singleserver.options[currSel].value;
17657:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
17658:                                 var numoptions = singleserver.options.length;
17659:                                 var needsnew = 1;
17660:                                 for (var k=0; k<numoptions; k++) {
17661:                                     if (singleserver.options[k] == addtarget) {
17662:                                         needsnew = 0;
17663:                                         break;
17664:                                     }
17665:                                 }
17666:                                 if (needsnew == 1) {
17667:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
17668:                                 }
17669:                             }
17670:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
17671:                                 singleserver.options.length = 0;
17672:                                 if ((currVal) && (currVal != removetarget)) {
17673:                                     singleserver.options[0] = new Option("","",false,false);
17674:                                 } else {
17675:                                     singleserver.options[0] = new Option("","",true,true);
17676:                                 }
17677:                                 var idx = 0;
17678:                                 for (var m=0; m<alltargets.length; m++) {
17679:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
17680:                                         idx ++;
17681:                                         if (currVal == alltargets[m]) {
17682:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
17683:                                         } else {
17684:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
17685:                                         }
17686:                                     }
17687:                                 }
17688:                             }
17689:                         }
17690:                     }
17691:                 }
17692:             }
17693:         }
17694:     }
17695:     return;
17696: }
17697: 
17698: // ]]>
17699: </script>
17700: 
17701: END
17702: }
17703: 
17704: sub new_spares_js {
17705:     my @sparestypes = ('primary','default');
17706:     my $types = join("','",@sparestypes);
17707:     my $select = &mt('Select');
17708:     return <<"END";
17709: 
17710: <script type="text/javascript">
17711: // <![CDATA[
17712: 
17713: function updateNewSpares(formname,lonhost) {
17714:     var types = new Array('$types');
17715:     var include = new Array();
17716:     var exclude = new Array();
17717:     for (var i=0; i<types.length; i++) {
17718:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
17719:         for (var j=0; j<spareboxes.length; j++) {
17720:             if (formname.elements[spareboxes[j]].checked) {
17721:                 exclude.push(formname.elements[spareboxes[j]].value);
17722:             } else {
17723:                 include.push(formname.elements[spareboxes[j]].value);
17724:             }
17725:         }
17726:     }
17727:     for (var i=0; i<types.length; i++) {
17728:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
17729:         var selIdx = newSpare.selectedIndex;
17730:         var currnew = newSpare.options[selIdx].value;
17731:         var okSpares = new Array();
17732:         for (var j=0; j<newSpare.options.length; j++) {
17733:             var possible = newSpare.options[j].value;
17734:             if (possible != '') {
17735:                 if (exclude.indexOf(possible) == -1) {
17736:                     okSpares.push(possible);
17737:                 } else {
17738:                     if (currnew == possible) {
17739:                         selIdx = 0;
17740:                     }
17741:                 }
17742:             }
17743:         }
17744:         for (var k=0; k<include.length; k++) {
17745:             if (okSpares.indexOf(include[k]) == -1) {
17746:                 okSpares.push(include[k]);
17747:             }
17748:         }
17749:         okSpares.sort();
17750:         newSpare.options.length = 0;
17751:         if (selIdx == 0) {
17752:             newSpare.options[0] = new Option("$select","",true,true);
17753:         } else {
17754:             newSpare.options[0] = new Option("$select","",false,false);
17755:         }
17756:         for (var m=0; m<okSpares.length; m++) {
17757:             var idx = m+1;
17758:             var selThis = 0;
17759:             if (selIdx != 0) {
17760:                 if (okSpares[m] == currnew) {
17761:                     selThis = 1;
17762:                 }
17763:             }
17764:             if (selThis == 1) {
17765:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
17766:             } else {
17767:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
17768:             }
17769:         }
17770:     }
17771:     return;
17772: }
17773: 
17774: function checkNewSpares(lonhost,type) {
17775:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
17776:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
17777:     if (chosen != '') { 
17778:         var othertype;
17779:         var othernewSpare;
17780:         if (type == 'primary') {
17781:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
17782:         }
17783:         if (type == 'default') {
17784:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
17785:         }
17786:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
17787:             othernewSpare.selectedIndex = 0;
17788:         }
17789:     }
17790:     return;
17791: }
17792: 
17793: // ]]>
17794: </script>
17795: 
17796: END
17797: 
17798: }
17799: 
17800: sub common_domprefs_js {
17801:     return <<"END";
17802: 
17803: <script type="text/javascript">
17804: // <![CDATA[
17805: 
17806: function getIndicesByName(formname,item) {
17807:     var group = new Array();
17808:     for (var i=0;i<formname.elements.length;i++) {
17809:         if (formname.elements[i].name == item) {
17810:             group.push(formname.elements[i].id);
17811:         }
17812:     }
17813:     return group;
17814: }
17815: 
17816: // ]]>
17817: </script>
17818: 
17819: END
17820: 
17821: }
17822: 
17823: sub recaptcha_js {
17824:     my %lt = &captcha_phrases();
17825:     return <<"END";
17826: 
17827: <script type="text/javascript">
17828: // <![CDATA[
17829: 
17830: function updateCaptcha(caller,context) {
17831:     var privitem;
17832:     var pubitem;
17833:     var privtext;
17834:     var pubtext;
17835:     var versionitem;
17836:     var versiontext;
17837:     if (document.getElementById(context+'_recaptchapub')) {
17838:         pubitem = document.getElementById(context+'_recaptchapub');
17839:     } else {
17840:         return;
17841:     }
17842:     if (document.getElementById(context+'_recaptchapriv')) {
17843:         privitem = document.getElementById(context+'_recaptchapriv');
17844:     } else {
17845:         return;
17846:     }
17847:     if (document.getElementById(context+'_recaptchapubtxt')) {
17848:         pubtext = document.getElementById(context+'_recaptchapubtxt');
17849:     } else {
17850:         return;
17851:     }
17852:     if (document.getElementById(context+'_recaptchaprivtxt')) {
17853:         privtext = document.getElementById(context+'_recaptchaprivtxt');
17854:     } else {
17855:         return;
17856:     }
17857:     if (document.getElementById(context+'_recaptchaversion')) {
17858:         versionitem = document.getElementById(context+'_recaptchaversion');
17859:     } else {
17860:         return;
17861:     }
17862:     if (document.getElementById(context+'_recaptchavertxt')) {
17863:         versiontext = document.getElementById(context+'_recaptchavertxt');
17864:     } else {
17865:         return;
17866:     }
17867:     if (caller.checked) {
17868:         if (caller.value == 'recaptcha') {
17869:             pubitem.type = 'text';
17870:             privitem.type = 'text';
17871:             pubitem.size = '40';
17872:             privitem.size = '40';
17873:             pubtext.innerHTML = "$lt{'pub'}";
17874:             privtext.innerHTML = "$lt{'priv'}";
17875:             versionitem.type = 'text';
17876:             versionitem.size = '3';
17877:             versiontext.innerHTML = "$lt{'ver'}";
17878:         } else {
17879:             pubitem.type = 'hidden';
17880:             privitem.type = 'hidden';
17881:             versionitem.type = 'hidden';
17882:             pubtext.innerHTML = '';
17883:             privtext.innerHTML = '';
17884:             versiontext.innerHTML = '';
17885:         }
17886:     }
17887:     return;
17888: }
17889: 
17890: // ]]>
17891: </script>
17892: 
17893: END
17894: 
17895: }
17896: 
17897: sub toggle_display_js {
17898:     return <<"END";
17899: 
17900: <script type="text/javascript">
17901: // <![CDATA[
17902: 
17903: function toggleDisplay(domForm,caller) {
17904:     if (document.getElementById(caller)) {
17905:         var divitem = document.getElementById(caller);
17906:         var optionsElement = domForm.coursecredits;
17907:         var checkval = 1;
17908:         var dispval = 'block';
17909:         var selfcreateRegExp = /^cancreate_emailverified/;
17910:         if (caller == 'emailoptions') {
17911:             optionsElement = domForm.cancreate_email; 
17912:         }
17913:         if (caller == 'studentsubmission') {
17914:             optionsElement = domForm.postsubmit;
17915:         }
17916:         if (caller == 'cloneinstcode') {
17917:             optionsElement = domForm.canclone;
17918:             checkval = 'instcode';
17919:         }
17920:         if (selfcreateRegExp.test(caller)) {
17921:             optionsElement = domForm.elements[caller];
17922:             checkval = 'other';
17923:             dispval = 'inline'
17924:         }
17925:         if (optionsElement.length) {
17926:             var currval;
17927:             for (var i=0; i<optionsElement.length; i++) {
17928:                 if (optionsElement[i].checked) {
17929:                    currval = optionsElement[i].value;
17930:                 }
17931:             }
17932:             if (currval == checkval) {
17933:                 divitem.style.display = dispval;
17934:             } else {
17935:                 divitem.style.display = 'none';
17936:             }
17937:         }
17938:     }
17939:     return;
17940: }
17941: 
17942: // ]]>
17943: </script>
17944: 
17945: END
17946: 
17947: }
17948: 
17949: sub captcha_phrases {
17950:     return &Apache::lonlocal::texthash (
17951:                  priv => 'Private key',
17952:                  pub  => 'Public key',
17953:                  original  => 'original (CAPTCHA)',
17954:                  recaptcha => 'successor (ReCAPTCHA)',
17955:                  notused   => 'unused',
17956:                  ver => 'ReCAPTCHA version (1 or 2)',
17957:     );
17958: }
17959: 
17960: sub devalidate_remote_domconfs {
17961:     my ($dom,$cachekeys) = @_;
17962:     return unless (ref($cachekeys) eq 'HASH');
17963:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
17964:     my %thismachine;
17965:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
17966:     my @posscached = ('domainconfig','domdefaults','usersessions',
17967:                       'directorysrch','passwdconf','cats','proxyalias','proxysaml',
17968:                       'ipaccess');
17969:     my %cache_by_lonhost;
17970:     if (exists($cachekeys->{'samllanding'})) {
17971:         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
17972:             my %landing = %{$cachekeys->{'samllanding'}};
17973:             my %domservers = &Apache::lonnet::get_servers($dom);
17974:             if (keys(%domservers)) {
17975:                 foreach my $server (keys(%domservers)) {
17976:                     my @cached;
17977:                     next if ($thismachine{$server});
17978:                     if ($landing{$server}) {
17979:                         push(@cached,&escape('samllanding').':'.&escape($server));
17980:                     }
17981:                     if (@cached) {
17982:                         $cache_by_lonhost{$server} = \@cached;
17983:                     }
17984:                 }
17985:             }
17986:         }
17987:     }
17988:     if (keys(%servers)) {
17989:         foreach my $server (keys(%servers)) {
17990:             next if ($thismachine{$server});
17991:             my @cached;
17992:             foreach my $name (@posscached) {
17993:                 if ($cachekeys->{$name}) {
17994:                     if (($name eq 'proxyalias') || ($name eq 'proxysaml')) {
17995:                         if (ref($cachekeys->{$name}) eq 'HASH') {
17996:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
17997:                                 push(@cached,&escape($name).':'.&escape($key));
17998:                             }
17999:                         }
18000:                     } else {
18001:                         push(@cached,&escape($name).':'.&escape($dom));
18002:                     }
18003:                 }
18004:             }
18005:             if ((exists($cache_by_lonhost{$server})) &&
18006:                 (ref($cache_by_lonhost{$server}) eq 'ARRAY')) {
18007:                 push(@cached,@{$cache_by_lonhost{$server}});
18008:             }
18009:             if (@cached) {
18010:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
18011:             }
18012:         }
18013:     }
18014:     return;
18015: }
18016: 
18017: 1;

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