File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.118.2.18: download - view: text, annotated - select for diffs
Sun Feb 25 05:54:21 2024 UTC (3 months, 1 week ago) by raeburn
Branches: version_2_11_4_msu
- For 2.11.4 (modified)
  Include changes in 1.434

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.118.2.18 2024/02/25 05:54:21 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, textbook, and lti).
  108: In each case the radio buttons 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 Apache::courseprefs();
  171: use LONCAPA qw(:DEFAULT :match);
  172: use LONCAPA::Enrollment;
  173: use LONCAPA::lonauthcgi();
  174: use File::Copy;
  175: use Locale::Language;
  176: use DateTime::TimeZone;
  177: use DateTime::Locale;
  178: use Time::HiRes qw( sleep );
  179: use Net::CIDR;
  180: use Crypt::CBC;
  181: 
  182: my $registered_cleanup;
  183: my $modified_urls;
  184: 
  185: sub handler {
  186:     my $r=shift;
  187:     if ($r->header_only) {
  188:         &Apache::loncommon::content_type($r,'text/html');
  189:         $r->send_http_header;
  190:         return OK;
  191:     }
  192: 
  193:     my $context = 'domain';
  194:     my $dom = $env{'request.role.domain'};
  195:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  196:     if (&Apache::lonnet::allowed('mau',$dom)) {
  197:         &Apache::loncommon::content_type($r,'text/html');
  198:         $r->send_http_header;
  199:     } else {
  200:         $env{'user.error.msg'}=
  201:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  202:         return HTTP_NOT_ACCEPTABLE;
  203:     }
  204: 
  205:     $registered_cleanup=0;
  206:     @{$modified_urls}=();
  207: 
  208:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  209:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  210:                                             ['phase','actions']);
  211:     my $phase = 'pickactions';
  212:     if ( exists($env{'form.phase'}) ) {
  213:         $phase = $env{'form.phase'};
  214:     }
  215:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  216:     my %domconfig =
  217:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  218:                 'quotas','autoenroll','autoupdate','autocreate',
  219:                 'directorysrch','usercreation','usermodification',
  220:                 'contacts','defaults','scantron','coursecategories',
  221:                 'serverstatuses','requestcourses','helpsettings',
  222:                 'coursedefaults','usersessions','loadbalancing',
  223:                 'requestauthor','selfenrollment','inststatus',
  224:                 'passwords','ltitools','toolsec','lti','ltisec',
  225:                 'wafproxy','ipaccess'],$dom);
  226:     my %encconfig =
  227:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti','linkprot'],$dom,undef,1);
  228:     my ($checked_is_home,$is_home);
  229:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  230:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  231:             my $home = &Apache::lonnet::domain($dom,'primary');
  232:             unless (($home eq 'no_host') || ($home eq '')) {
  233:                 my @ids=&Apache::lonnet::current_machine_ids();
  234:                 if (grep(/^\Q$home\E$/,@ids)) {
  235:                     $is_home = 1;
  236:                 }
  237:             }
  238:             $checked_is_home = 1;
  239:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  240:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  241:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  242:                     $domconfig{'ltitools'}{$id}{'key'} = $encconfig{'ltitools'}{$id}{'key'};
  243:                     if (($is_home) && ($phase eq 'process')) {
  244:                         $domconfig{'ltitools'}{$id}{'secret'} = $encconfig{'ltitools'}{$id}{'secret'};
  245:                     }
  246:                 }
  247:             }
  248:         }
  249:     }
  250:     if (ref($domconfig{'lti'}) eq 'HASH') {
  251:         if (ref($encconfig{'lti'}) eq 'HASH') {
  252:             unless ($checked_is_home) {
  253:                 my $home = &Apache::lonnet::domain($dom,'primary');
  254:                 unless (($home eq 'no_host') || ($home eq '')) {
  255:                     my @ids=&Apache::lonnet::current_machine_ids();
  256:                     if (grep(/^\Q$home\E$/,@ids)) {
  257:                         $is_home = 1;
  258:                     }
  259:                 }
  260:                 $checked_is_home = 1;
  261:             }
  262:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  263:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  264:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  265:                     $domconfig{'lti'}{$id}{'key'} = $encconfig{'lti'}{$id}{'key'};
  266:                     if (($is_home) && ($phase eq 'process')) {
  267:                         $domconfig{'lti'}{$id}{'secret'} = $encconfig{'lti'}{$id}{'secret'};
  268:                     }
  269:                 }
  270:             }
  271:         }
  272:     }
  273:     if (ref($domconfig{'ltisec'}) eq 'HASH') {
  274:         if (ref($domconfig{'ltisec'}{'linkprot'}) eq 'HASH') {
  275:             if (ref($encconfig{'linkprot'}) eq 'HASH') {
  276:                 foreach my $id (keys(%{$domconfig{'ltisec'}{'linkprot'}})) {
  277:                     unless ($id =~ /^\d+$/) {
  278:                         delete($domconfig{'ltisec'}{'linkprot'}{$id});
  279:                     }
  280:                     if ((ref($domconfig{'ltisec'}{'linkprot'}{$id}) eq 'HASH') &&
  281:                         (ref($encconfig{'linkprot'}{$id}) eq 'HASH')) {
  282:                         foreach my $item ('key','secret') {
  283:                             $domconfig{'ltisec'}{'linkprot'}{$id}{$item} = $encconfig{'linkprot'}{$id}{$item};
  284:                         }
  285:                     }
  286:                 }
  287:             }
  288:         }
  289:     }
  290:     my @prefs_order = ('rolecolors','login','ipaccess','defaults','wafproxy','passwords',
  291:                        'quotas','autoenroll','autoupdate','autocreate','directorysrch',
  292:                        'contacts','usercreation','selfcreation','usermodification',
  293:                        'scantron','requestcourses','requestauthor','coursecategories',
  294:                        'serverstatuses','helpsettings','coursedefaults',
  295:                        'ltitools','selfenrollment','usersessions','lti');
  296:     my %existing;
  297:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  298:         %existing = %{$domconfig{'loadbalancing'}};
  299:     }
  300:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  301:         push(@prefs_order,'loadbalancing');
  302:     }
  303:     my %prefs = (
  304:         'rolecolors' =>
  305:                    { text => 'Default color schemes',
  306:                      help => 'Domain_Configuration_Color_Schemes',
  307:                      header => [{col1 => 'Student Settings',
  308:                                  col2 => '',},
  309:                                 {col1 => 'Coordinator Settings',
  310:                                  col2 => '',},
  311:                                 {col1 => 'Author Settings',
  312:                                  col2 => '',},
  313:                                 {col1 => 'Administrator Settings',
  314:                                  col2 => '',}],
  315:                       print => \&print_rolecolors,
  316:                       modify => \&modify_rolecolors,
  317:                     },
  318:         'login' =>
  319:                     { text => 'Log-in page options',
  320:                       help => 'Domain_Configuration_Login_Page',
  321:                       header => [{col1 => 'Log-in Page Items',
  322:                                   col2 => '',},
  323:                                  {col1 => 'Log-in Help',
  324:                                   col2 => 'Value'},
  325:                                  {col1 => 'Custom HTML in document head',
  326:                                   col2 => 'Value'},
  327:                                  {col1 => 'SSO',
  328:                                   col2 => 'Dual login: SSO and non-SSO options'},
  329:                                 ],
  330:                       print => \&print_login,
  331:                       modify => \&modify_login,
  332:                     },
  333:         'defaults' => 
  334:                     { text => 'Default authentication/language/timezone/portal/types',
  335:                       help => 'Domain_Configuration_LangTZAuth',
  336:                       header => [{col1 => 'Setting',
  337:                                   col2 => 'Value'},
  338:                                  {col1 => 'Institutional user types',
  339:                                   col2 => 'Name displayed'},
  340:                                  {col1 => 'Mapping for missing usernames via standard log-in',
  341:                                   col2 => 'Rules in use'}],
  342:                       print => \&print_defaults,
  343:                       modify => \&modify_defaults,
  344:                     },
  345:         'wafproxy' =>
  346:                     { text => 'Web Application Firewall/Reverse Proxy',
  347:                       help => 'Domain_Configuration_WAF_Proxy',
  348:                       header => [{col1 => 'Domain(s)',
  349:                                   col2 => 'Servers and WAF/Reverse Proxy alias(es)',
  350:                                  },
  351:                                  {col1 => 'Domain(s)',
  352:                                   col2 => 'WAF Configuration',}],
  353:                       print => \&print_wafproxy,
  354:                       modify => \&modify_wafproxy,
  355:                     },
  356:         'passwords' =>
  357:                     { text => 'Passwords (Internal authentication)',
  358:                       help => 'Domain_Configuration_Passwords',
  359:                       header => [{col1 => 'Resetting Forgotten Password',
  360:                                   col2 => 'Settings'},
  361:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  362:                                   col2 => 'Settings'},
  363:                                  {col1 => 'Rules for LON-CAPA Passwords',
  364:                                   col2 => 'Settings'},
  365:                                  {col1 => 'Course Owner Changing Student Passwords',
  366:                                   col2 => 'Settings'}],
  367:                       print => \&print_passwords,
  368:                       modify => \&modify_passwords,
  369:                     },
  370:         'quotas' => 
  371:                     { text => 'Blogs, personal pages/timezones, webDAV/quotas, portfolio',
  372:                       help => 'Domain_Configuration_Quotas',
  373:                       header => [{col1 => 'User affiliation',
  374:                                   col2 => 'Available tools',
  375:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  376:                       print => \&print_quotas,
  377:                       modify => \&modify_quotas,
  378:                     },
  379:         'autoenroll' =>
  380:                    { text => 'Auto-enrollment settings',
  381:                      help => 'Domain_Configuration_Auto_Enrollment',
  382:                      header => [{col1 => 'Configuration setting',
  383:                                  col2 => 'Value(s)'}],
  384:                      print => \&print_autoenroll,
  385:                      modify => \&modify_autoenroll,
  386:                    },
  387:         'autoupdate' => 
  388:                    { text => 'Auto-update settings',
  389:                      help => 'Domain_Configuration_Auto_Updates',
  390:                      header => [{col1 => 'Setting',
  391:                                  col2 => 'Value',},
  392:                                 {col1 => 'Setting',
  393:                                  col2 => 'Affiliation'},
  394:                                 {col1 => 'User population',
  395:                                  col2 => 'Updatable user data'}],
  396:                      print => \&print_autoupdate,
  397:                      modify => \&modify_autoupdate,
  398:                   },
  399:         'autocreate' => 
  400:                   { text => 'Auto-course creation settings',
  401:                      help => 'Domain_Configuration_Auto_Creation',
  402:                      header => [{col1 => 'Configuration Setting',
  403:                                  col2 => 'Value',}],
  404:                      print => \&print_autocreate,
  405:                      modify => \&modify_autocreate,
  406:                   },
  407:         'directorysrch' => 
  408:                   { text => 'Directory searches',
  409:                     help => 'Domain_Configuration_InstDirectory_Search',
  410:                     header => [{col1 => 'Institutional Directory Setting',
  411:                                 col2 => 'Value',},
  412:                                {col1 => 'LON-CAPA Directory Setting',
  413:                                 col2 => 'Value',}],
  414:                     print => \&print_directorysrch,
  415:                     modify => \&modify_directorysrch,
  416:                   },
  417:         'contacts' =>
  418:                   { text => 'E-mail addresses and helpform',
  419:                     help => 'Domain_Configuration_Contact_Info',
  420:                     header => [{col1 => 'Default e-mail addresses',
  421:                                 col2 => 'Value',},
  422:                                {col1 => 'Recipient(s) for notifications',
  423:                                 col2 => 'Value',},
  424:                                {col1 => 'Nightly status check e-mail',
  425:                                 col2 => 'Settings',},
  426:                                {col1 => 'Ask helpdesk form settings',
  427:                                 col2 => 'Value',},],
  428:                     print => \&print_contacts,
  429:                     modify => \&modify_contacts,
  430:                   },
  431:         'usercreation' => 
  432:                   { text => 'User creation',
  433:                     help => 'Domain_Configuration_User_Creation',
  434:                     header => [{col1 => 'Format rule type',
  435:                                 col2 => 'Format rules in force'},
  436:                                {col1 => 'User account creation',
  437:                                 col2 => 'Usernames which may be created',},
  438:                                {col1 => 'Context',
  439:                                 col2 => 'Assignable authentication types'}],
  440:                     print => \&print_usercreation,
  441:                     modify => \&modify_usercreation,
  442:                   },
  443:         'selfcreation' => 
  444:                   { text => 'Users self-creating accounts',
  445:                     help => 'Domain_Configuration_Self_Creation', 
  446:                     header => [{col1 => 'Self-creation with institutional username',
  447:                                 col2 => 'Enabled?'},
  448:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  449:                                 col2 => 'Information user can enter'},
  450:                                {col1 => 'Self-creation with e-mail verification',
  451:                                 col2 => 'Settings'}],
  452:                     print => \&print_selfcreation,
  453:                     modify => \&modify_selfcreation,
  454:                   },
  455:         'usermodification' =>
  456:                   { text => 'User modification',
  457:                     help => 'Domain_Configuration_User_Modification',
  458:                     header => [{col1 => 'Target user has role',
  459:                                 col2 => 'User information updatable in author context'},
  460:                                {col1 => 'Target user has role',
  461:                                 col2 => 'User information updatable in course context'}],
  462:                     print => \&print_usermodification,
  463:                     modify => \&modify_usermodification,
  464:                   },
  465:         'scantron' =>
  466:                   { text => 'Bubblesheet format',
  467:                     help => 'Domain_Configuration_Scantron_Format',
  468:                     header => [ {col1 => 'Bubblesheet format file',
  469:                                  col2 => ''},
  470:                                 {col1 => 'Bubblesheet data upload formats',
  471:                                  col2 => 'Settings'}],
  472:                     print => \&print_scantron,
  473:                     modify => \&modify_scantron,
  474:                   },
  475:         'requestcourses' => 
  476:                  {text => 'Request creation of courses',
  477:                   help => 'Domain_Configuration_Request_Courses',
  478:                   header => [{col1 => 'User affiliation',
  479:                               col2 => 'Availability/Processing of requests',},
  480:                              {col1 => 'Setting',
  481:                               col2 => 'Value'},
  482:                              {col1 => 'Available textbooks',
  483:                               col2 => ''},
  484:                              {col1 => 'Available templates',
  485:                               col2 => ''},
  486:                              {col1 => 'Validation (not official courses)',
  487:                               col2 => 'Value'},],
  488:                   print => \&print_quotas,
  489:                   modify => \&modify_quotas,
  490:                  },
  491:         'requestauthor' =>
  492:                  {text => 'Request Authoring Space',
  493:                   help => 'Domain_Configuration_Request_Author',
  494:                   header => [{col1 => 'User affiliation',
  495:                               col2 => 'Availability/Processing of requests',},
  496:                              {col1 => 'Setting',
  497:                               col2 => 'Value'}],
  498:                   print => \&print_quotas,
  499:                   modify => \&modify_quotas,
  500:                  },
  501:         'coursecategories' =>
  502:                   { text => 'Cataloging of courses/communities',
  503:                     help => 'Domain_Configuration_Cataloging_Courses',
  504:                     header => [{col1 => 'Catalog type/availability',
  505:                                 col2 => '',},
  506:                                {col1 => 'Category settings for standard catalog',
  507:                                 col2 => '',},
  508:                                {col1 => 'Categories',
  509:                                 col2 => '',
  510:                                }],
  511:                     print => \&print_coursecategories,
  512:                     modify => \&modify_coursecategories,
  513:                   },
  514:         'serverstatuses' =>
  515:                  {text   => 'Access to server status pages',
  516:                   help   => 'Domain_Configuration_Server_Status',
  517:                   header => [{col1 => 'Status Page',
  518:                               col2 => 'Other named users',
  519:                               col3 => 'Specific IPs',
  520:                             }],
  521:                   print => \&print_serverstatuses,
  522:                   modify => \&modify_serverstatuses,
  523:                  },
  524:         'helpsettings' =>
  525:                  {text   => 'Support settings',
  526:                   help   => 'Domain_Configuration_Help_Settings',
  527:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  528:                               col2 => 'Value'},
  529:                              {col1 => 'Helpdesk Roles',
  530:                               col2 => 'Settings'},],
  531:                   print  => \&print_helpsettings,
  532:                   modify => \&modify_helpsettings,
  533:                  },
  534:         'coursedefaults' => 
  535:                  {text => 'Course/Community defaults',
  536:                   help => 'Domain_Configuration_Course_Defaults',
  537:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  538:                               col2 => 'Value',},
  539:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  540:                               col2 => 'Value',},],
  541:                   print => \&print_coursedefaults,
  542:                   modify => \&modify_coursedefaults,
  543:                  },
  544:         'selfenrollment' => 
  545:                  {text   => 'Self-enrollment in Course/Community',
  546:                   help   => 'Domain_Configuration_Selfenrollment',
  547:                   header => [{col1 => 'Configuration Rights',
  548:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  549:                              {col1 => 'Defaults',
  550:                               col2 => 'Value'},
  551:                              {col1 => 'Self-enrollment validation (optional)',
  552:                               col2 => 'Value'},],
  553:                   print => \&print_selfenrollment,
  554:                   modify => \&modify_selfenrollment,
  555:                  },
  556:         'usersessions' =>
  557:                  {text  => 'User session hosting/offloading',
  558:                   help  => 'Domain_Configuration_User_Sessions',
  559:                   header => [{col1 => 'Domain server',
  560:                               col2 => 'Servers to offload sessions to when busy'},
  561:                              {col1 => 'Hosting of users from other domains',
  562:                               col2 => 'Rules'},
  563:                              {col1 => "Hosting domain's own users elsewhere",
  564:                               col2 => 'Rules'}],
  565:                   print => \&print_usersessions,
  566:                   modify => \&modify_usersessions,
  567:                  },
  568:         'loadbalancing' =>
  569:                  {text  => 'Dedicated Load Balancer(s)',
  570:                   help  => 'Domain_Configuration_Load_Balancing',
  571:                   header => [{col1 => 'Balancers',
  572:                               col2 => 'Default destinations',
  573:                               col3 => 'User affiliation',
  574:                               col4 => 'Overrides'},
  575:                             ],
  576:                   print => \&print_loadbalancing,
  577:                   modify => \&modify_loadbalancing,
  578:                  },
  579:         'ltitools' =>
  580:                  {text => 'External Tools (LTI)',
  581:                   help => 'Domain_Configuration_LTI_Tools',
  582:                   header => [{col1 => 'Encryption of shared secrets',
  583:                               col2 => 'Settings'},
  584:                              {col1 => 'Rules for shared secrets',
  585:                               col2 => 'Settings'},
  586:                              {col1 => 'Providers',
  587:                               col2 => 'Settings',}],
  588:                   print => \&print_ltitools,
  589:                   modify => \&modify_ltitools,
  590:                  },
  591:         'lti' =>
  592:                  {text => 'LTI Link Protection and LTI Consumers',
  593:                   help => 'Domain_Configuration_LTI_Provider',
  594:                   header => [{col1 => 'Encryption of shared secrets',
  595:                               col2 => 'Settings'},
  596:                              {col1 => 'Rules for shared secrets',
  597:                               col2 => 'Settings'},
  598:                              {col1 => 'Link Protectors in Courses',
  599:                               col2 => 'Values'},
  600:                              {col1 => 'Link Protectors',
  601:                               col2 => 'Settings'},
  602:                              {col1 => 'Consumers',
  603:                               col2 => 'Settings'},],
  604:                   print => \&print_lti,
  605:                   modify => \&modify_lti,
  606:                  },
  607:         'ipaccess' =>
  608:                        {text => 'IP-based access control',
  609:                         help => 'Domain_Configuration_IP_Access',
  610:                         header => [{col1 => 'Setting',
  611:                                     col2 => 'Value'},],
  612:                         print  => \&print_ipaccess,
  613:                         modify => \&modify_ipaccess,
  614:                        },
  615:     );
  616:     if (keys(%servers) > 1) {
  617:         $prefs{'login'}  = { text   => 'Log-in page options',
  618:                              help   => 'Domain_Configuration_Login_Page',
  619:                             header => [{col1 => 'Log-in Service',
  620:                                         col2 => 'Server Setting',},
  621:                                        {col1 => 'Log-in Page Items',
  622:                                         col2 => 'Settings'},
  623:                                        {col1 => 'Log-in Help',
  624:                                         col2 => 'Value'},
  625:                                        {col1 => 'Custom HTML in document head',
  626:                                         col2 => 'Value'},
  627:                                        {col1 => 'SSO',
  628:                                         col2 => 'Dual login: SSO and non-SSO options'},
  629:                                       ],
  630:                             print => \&print_login,
  631:                             modify => \&modify_login,
  632:                            };
  633:     }
  634: 
  635:     my @roles = ('student','coordinator','author','admin');
  636:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  637:     &Apache::lonhtmlcommon::add_breadcrumb
  638:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  639:       text=>"Settings to display/modify"});
  640:     my $confname = $dom.'-domainconfig';
  641: 
  642:     if ($phase eq 'process') {
  643:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  644:                                                               \%prefs,\%domconfig,$confname,\@roles);
  645:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  646:             $r->rflush();
  647:             &devalidate_remote_domconfs($dom,$result);
  648:         }
  649:     } elsif ($phase eq 'display') {
  650:         my $js = &recaptcha_js().
  651:                  &toggle_display_js();
  652:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  653:             my ($othertitle,$usertypes,$types) =
  654:                 &Apache::loncommon::sorted_inst_types($dom);
  655:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  656:                                           $domconfig{'loadbalancing'}).
  657:                    &new_spares_js().
  658:                    &common_domprefs_js().
  659:                    &Apache::loncommon::javascript_array_indexof();
  660:         }
  661:         if (grep(/^requestcourses$/,@actions)) {
  662:             my $javascript_validations;
  663:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  664:             $js .= <<END;
  665: <script type="text/javascript">
  666: $javascript_validations
  667: </script>
  668: $coursebrowserjs
  669: END
  670:         } elsif (grep(/^ipaccess$/,@actions)) {
  671:             $js .= &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
  672:         }
  673:         if (grep(/^selfcreation$/,@actions)) {
  674:             $js .= &selfcreate_javascript();
  675:         }
  676:         if (grep(/^contacts$/,@actions)) {
  677:             $js .= &contacts_javascript();
  678:         }
  679:         if (grep(/^scantron$/,@actions)) {
  680:             $js .= &scantron_javascript();
  681:         }
  682:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  683:     } else {
  684: # check if domconfig user exists for the domain.
  685:         my $servadm = $r->dir_config('lonAdmEMail');
  686:         my ($configuserok,$author_ok,$switchserver) =
  687:             &config_check($dom,$confname,$servadm);
  688:         unless ($configuserok eq 'ok') {
  689:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  690:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  691:                           $confname).
  692:                       '<br />'
  693:             );
  694:             if ($switchserver) {
  695:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  696:                           '<br />'.
  697:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  698:                           '<br />'.
  699:                           &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).
  700:                           '<br />'.
  701:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  702:                 );
  703:             } else {
  704:                 $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.').
  705:                           '<br />'.
  706:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  707:                 );
  708:             }
  709:             $r->print(&Apache::loncommon::end_page());
  710:             return OK;
  711:         }
  712:         if (keys(%domconfig) == 0) {
  713:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  714:             my @ids=&Apache::lonnet::current_machine_ids();
  715:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  716:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  717:                 my @loginimages = ('img','logo','domlogo','login');
  718:                 my $custom_img_count = 0;
  719:                 foreach my $img (@loginimages) {
  720:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  721:                         $custom_img_count ++;
  722:                     }
  723:                 }
  724:                 foreach my $role (@roles) {
  725:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  726:                         $custom_img_count ++;
  727:                     }
  728:                 }
  729:                 if ($custom_img_count > 0) {
  730:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  731:                     my $switch_server = &check_switchserver($dom,$confname);
  732:                     $r->print(
  733:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  734:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  735:     &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 />'.
  736:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  737:                     if ($switch_server) {
  738:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  739:                     }
  740:                     $r->print(&Apache::loncommon::end_page());
  741:                     return OK;
  742:                 }
  743:             }
  744:         }
  745:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  746:     }
  747:     return OK;
  748: }
  749: 
  750: sub process_changes {
  751:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  752:     my %domconfig;
  753:     if (ref($values) eq 'HASH') {
  754:         %domconfig = %{$values};
  755:     }
  756:     my $output;
  757:     if ($action eq 'login') {
  758:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  759:     } elsif ($action eq 'rolecolors') {
  760:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  761:                                      $lastactref,%domconfig);
  762:     } elsif ($action eq 'quotas') {
  763:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  764:     } elsif ($action eq 'autoenroll') {
  765:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  766:     } elsif ($action eq 'autoupdate') {
  767:         $output = &modify_autoupdate($dom,%domconfig);
  768:     } elsif ($action eq 'autocreate') {
  769:         $output = &modify_autocreate($dom,%domconfig);
  770:     } elsif ($action eq 'directorysrch') {
  771:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  772:     } elsif ($action eq 'usercreation') {
  773:         $output = &modify_usercreation($dom,%domconfig);
  774:     } elsif ($action eq 'selfcreation') {
  775:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  776:     } elsif ($action eq 'usermodification') {
  777:         $output = &modify_usermodification($dom,%domconfig);
  778:     } elsif ($action eq 'contacts') {
  779:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  780:     } elsif ($action eq 'defaults') {
  781:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  782:     } elsif ($action eq 'scantron') {
  783:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  784:     } elsif ($action eq 'coursecategories') {
  785:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  786:     } elsif ($action eq 'serverstatuses') {
  787:         $output = &modify_serverstatuses($dom,%domconfig);
  788:     } elsif ($action eq 'requestcourses') {
  789:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  790:     } elsif ($action eq 'requestauthor') {
  791:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  792:     } elsif ($action eq 'helpsettings') {
  793:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  794:     } elsif ($action eq 'coursedefaults') {
  795:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  796:     } elsif ($action eq 'selfenrollment') {
  797:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  798:     } elsif ($action eq 'usersessions') {
  799:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  800:     } elsif ($action eq 'loadbalancing') {
  801:         $output = &modify_loadbalancing($dom,%domconfig);
  802:     } elsif ($action eq 'ltitools') {
  803:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  804:     } elsif ($action eq 'lti') {
  805:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  806:     } elsif ($action eq 'passwords') {
  807:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  808:     } elsif ($action eq 'wafproxy') {
  809:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  810:     } elsif ($action eq 'ipaccess') {
  811:         $output = &modify_ipaccess($dom,$lastactref,%domconfig);
  812:     }
  813:     return $output;
  814: }
  815: 
  816: sub print_config_box {
  817:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  818:     my $rowtotal = 0;
  819:     my $output;
  820:     if ($action eq 'coursecategories') {
  821:         $output = &coursecategories_javascript($settings);
  822:     } elsif ($action eq 'defaults') {
  823:         $output = &defaults_javascript($settings); 
  824:     } elsif ($action eq 'passwords') {
  825:         $output = &passwords_javascript($action);
  826:     } elsif ($action eq 'helpsettings') {
  827:         my (%privs,%levelscurrent);
  828:         my %full=();
  829:         my %levels=(
  830:                      course => {},
  831:                      domain => {},
  832:                      system => {},
  833:                    );
  834:         my $context = 'domain';
  835:         my $crstype = 'Course';
  836:         my $formname = 'display';
  837:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  838:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  839:         $output =
  840:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  841:                                                       \@templateroles);
  842:     } elsif ($action eq 'ltitools') {
  843:         $output .= &Apache::lonconfigsettings::ltitools_javascript($settings);
  844:     } elsif ($action eq 'lti') {
  845:         $output .= &passwords_javascript('ltisecrets')."\n".
  846:                    &lti_javascript($dom,$settings);
  847:     } elsif ($action eq 'wafproxy') {
  848:         $output .= &wafproxy_javascript($dom);
  849:     } elsif ($action eq 'autoupdate') {
  850:         $output .= &autoupdate_javascript();
  851:     } elsif ($action eq 'autoenroll') {
  852:         $output .= &autoenroll_javascript();
  853:     } elsif ($action eq 'login') {
  854:         $output .= &saml_javascript();
  855:     } elsif ($action eq 'ipaccess') {
  856:         $output .= &ipaccess_javascript($settings);
  857:     }
  858:     $output .=
  859:          '<table class="LC_nested_outer">
  860:           <tr>
  861:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  862:            &mt($item->{text}).'&nbsp;'.
  863:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  864:           '</tr>';
  865:     $rowtotal ++;
  866:     my $numheaders = 1;
  867:     if (ref($item->{'header'}) eq 'ARRAY') {
  868:         $numheaders = scalar(@{$item->{'header'}});
  869:     }
  870:     if ($numheaders > 1) {
  871:         my $colspan = '';
  872:         my $rightcolspan = '';
  873:         my $leftnobr = '';
  874:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  875:             ($action eq 'directorysrch') ||
  876:             (($action eq 'login') && ($numheaders < 5))) {
  877:             $colspan = ' colspan="2"';
  878:         }
  879:         if ($action eq 'usersessions') {
  880:             $rightcolspan = ' colspan="3"'; 
  881:         }
  882:         if ($action eq 'passwords') {
  883:             $leftnobr = ' LC_nobreak';
  884:         }
  885:         $output .= '
  886:           <tr>
  887:            <td>
  888:             <table class="LC_nested">
  889:              <tr class="LC_info_row">
  890:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  891:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  892:              </tr>';
  893:         $rowtotal ++;
  894:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  895:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  896:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  897:             ($action eq 'helpsettings') || ($action eq 'contacts') || ($action eq 'wafproxy') ||
  898:             ($action eq 'lti') || ($action eq 'ltitools')) {
  899:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  900:         } elsif ($action eq 'passwords') {
  901:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  902:         } elsif ($action eq 'coursecategories') {
  903:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  904:         } elsif ($action eq 'scantron') {
  905:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  906:         } elsif ($action eq 'login') {
  907:             if ($numheaders == 5) {
  908:                 $colspan = ' colspan="2"';
  909:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  910:             } else {
  911:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  912:             }
  913:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  914:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  915:         } elsif ($action eq 'rolecolors') {
  916:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  917:         }
  918:         $output .= '
  919:            </table>
  920:           </td>
  921:          </tr>
  922:          <tr>
  923:            <td>
  924:             <table class="LC_nested">
  925:              <tr class="LC_info_row">
  926:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  927:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  928:              </tr>';
  929:             $rowtotal ++;
  930:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  931:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  932:             ($action eq 'usersessions') || ($action eq 'coursecategories') ||
  933:             ($action eq 'contacts') || ($action eq 'passwords') || 
  934:             ($action eq 'defaults') || ($action eq 'lti') ||
  935:             ($action eq 'ltitools')) {
  936:             if ($action eq 'coursecategories') {
  937:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  938:                 $colspan = ' colspan="2"';
  939:             } elsif ($action eq 'passwords') {
  940:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  941:             } elsif ($action eq 'lti') {
  942:                 $output .= $item->{'print'}->('upper',$dom,$settings,\$rowtotal).'
  943:                            </table>
  944:                           </td>
  945:                          </tr>
  946:                          <tr>
  947:                           <td>
  948:                           <table class="LC_nested">
  949:                            <tr class="LC_info_row">
  950:                             <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  951:                             <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  952:                            </tr>'."\n".
  953:                            $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  954:             } else {
  955:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  956:             }
  957:             my $hdridx = 2;
  958:             if ($action eq 'lti') {
  959:                 $hdridx = 3;
  960:             }
  961:             $output .= '
  962:            </table>
  963:           </td>
  964:          </tr>
  965:          <tr>
  966:            <td>
  967:             <table class="LC_nested">
  968:              <tr class="LC_info_row">
  969:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col1'}).'</td>
  970:               <td class="LC_right_item">'.&mt($item->{'header'}->[$hdridx]->{'col2'}).'</td>
  971:              </tr>'."\n";
  972:             if ($action eq 'coursecategories') {
  973:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  974:             } elsif (($action eq 'contacts') || ($action eq 'passwords')) {
  975:                 if ($action eq 'passwords') {
  976:                     $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
  977:                 } else {
  978:                     $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
  979:                 }
  980:                 $hdridx ++;
  981:                 $output .= '
  982:              </tr>
  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'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col1'}).'</td>
  991:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col2'}).'</td></tr>'."\n";
  992:                 if ($action eq 'passwords') {
  993:                     $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
  994:                 } else {
  995:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  996:                 }
  997:                 $output .= '
  998:             </table>
  999:           </td>
 1000:          </tr>
 1001:          <tr>';
 1002:             } else {
 1003:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1004:             }
 1005:             $rowtotal ++;
 1006:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
 1007:                  ($action eq 'directorysrch') || ($action eq 'helpsettings') ||
 1008:                  ($action eq 'wafproxy')) {
 1009:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1010:         } elsif ($action eq 'scantron') {
 1011:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1012:         } elsif ($action eq 'login') {
 1013:             if ($numheaders == 5) {
 1014:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1015:            </table>
 1016:           </td>
 1017:          </tr>
 1018:          <tr>
 1019:            <td>
 1020:             <table class="LC_nested">
 1021:              <tr class="LC_info_row">
 1022:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1023:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1024:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1025:                 $rowtotal ++;
 1026:             } else {
 1027:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1028:             }
 1029:             $output .= '
 1030:            </table>
 1031:           </td>
 1032:          </tr>
 1033:          <tr>
 1034:            <td>
 1035:             <table class="LC_nested">
 1036:              <tr class="LC_info_row">';
 1037:             if ($numheaders == 5) {
 1038:                 $output .= '
 1039:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1040:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1041:              </tr>';
 1042:             } else {
 1043:                 $output .= '
 1044:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1045:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1046:              </tr>';
 1047:             }
 1048:             $rowtotal ++;
 1049:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal).'
 1050:            </table>
 1051:           </td>
 1052:          </tr>
 1053:          <tr>
 1054:            <td>
 1055:             <table class="LC_nested">
 1056:              <tr class="LC_info_row">';
 1057:             if ($numheaders == 5) {
 1058:                 $output .= '
 1059:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1060:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1061:              </tr>';
 1062:             } else {
 1063:                 $output .= '
 1064:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1065:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1066:              </tr>';
 1067:             }
 1068:             $rowtotal ++;
 1069:             $output .= &print_login('saml',$dom,$confname,$phase,$settings,\$rowtotal);
 1070:         } elsif ($action eq 'requestcourses') {
 1071:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1072:             $rowtotal ++;
 1073:             $output .= &print_studentcode($settings,\$rowtotal).'
 1074:            </table>
 1075:           </td>
 1076:          </tr>
 1077:          <tr>
 1078:            <td>
 1079:             <table class="LC_nested">
 1080:              <tr class="LC_info_row">
 1081:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1082:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1083:                        &textbookcourses_javascript($settings).
 1084:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1085:             </table>
 1086:            </td>
 1087:           </tr>
 1088:          <tr>
 1089:            <td>
 1090:             <table class="LC_nested">
 1091:              <tr class="LC_info_row">
 1092:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1093:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1094:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1095:             </table>
 1096:            </td>
 1097:           </tr>
 1098:           <tr>
 1099:            <td>
 1100:             <table class="LC_nested">
 1101:              <tr class="LC_info_row">
 1102:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1103:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1104:              </tr>'.
 1105:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1106:         } elsif ($action eq 'requestauthor') {
 1107:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1108:             $rowtotal ++;
 1109:         } elsif ($action eq 'rolecolors') {
 1110:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1111:            </table>
 1112:           </td>
 1113:          </tr>
 1114:          <tr>
 1115:            <td>
 1116:             <table class="LC_nested">
 1117:              <tr class="LC_info_row">
 1118:               <td class="LC_left_item"'.$colspan.' valign="top">'.
 1119:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1120:               <td class="LC_right_item" valign="top">'.
 1121:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1122:              </tr>'.
 1123:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1124:            </table>
 1125:           </td>
 1126:          </tr>
 1127:          <tr>
 1128:            <td>
 1129:             <table class="LC_nested">
 1130:              <tr class="LC_info_row">
 1131:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1132:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1133:              </tr>'.
 1134:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1135:             $rowtotal += 2;
 1136:         }
 1137:     } else {
 1138:         $output .= '
 1139:           <tr>
 1140:            <td>
 1141:             <table class="LC_nested">
 1142:              <tr class="LC_info_row">';
 1143:         if ($action eq 'login') {
 1144:             $output .= '  
 1145:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1146:         } elsif ($action eq 'serverstatuses') {
 1147:             $output .= '
 1148:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1149:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1150: 
 1151:         } else {
 1152:             $output .= '
 1153:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1154:         }
 1155:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1156:             $output .= '<td class="LC_left_item" valign="top">'.
 1157:                        &mt($item->{'header'}->[0]->{'col2'});
 1158:             if ($action eq 'serverstatuses') {
 1159:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1160:             } 
 1161:         } else {
 1162:             $output .= '<td class="LC_right_item" valign="top">'.
 1163:                        &mt($item->{'header'}->[0]->{'col2'});
 1164:         }
 1165:         $output .= '</td>';
 1166:         if ($item->{'header'}->[0]->{'col3'}) {
 1167:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1168:                 $output .= '<td class="LC_left_item" valign="top">'.
 1169:                             &mt($item->{'header'}->[0]->{'col3'});
 1170:             } else {
 1171:                 $output .= '<td class="LC_right_item" valign="top">'.
 1172:                            &mt($item->{'header'}->[0]->{'col3'});
 1173:             }
 1174:             if ($action eq 'serverstatuses') {
 1175:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1176:             }
 1177:             $output .= '</td>';
 1178:         }
 1179:         if ($item->{'header'}->[0]->{'col4'}) {
 1180:             $output .= '<td class="LC_right_item" valign="top">'.
 1181:                        &mt($item->{'header'}->[0]->{'col4'});
 1182:         }
 1183:         $output .= '</tr>';
 1184:         $rowtotal ++;
 1185:         if ($action eq 'quotas') {
 1186:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1187:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1188:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') ||
 1189:                  ($action eq 'ipaccess')) {
 1190:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1191:         }
 1192:     }
 1193:     $output .= '
 1194:    </table>
 1195:   </td>
 1196:  </tr>
 1197: </table><br />';
 1198:     return ($output,$rowtotal);
 1199: }
 1200: 
 1201: sub print_login {
 1202:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1203:     my ($css_class,$datatable,$switchserver,%lt);
 1204:     my %choices = &login_choices();
 1205:     if (($caller eq 'help') || ($caller eq 'headtag') || ($caller eq 'saml')) {
 1206:         %lt = &login_file_options();
 1207:         $switchserver = &check_switchserver($dom,$confname);
 1208:     }
 1209:     if ($caller eq 'service') {
 1210:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1211:         my $choice = $choices{'disallowlogin'};
 1212:         $css_class = ' class="LC_odd_row"';
 1213:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1214:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1215:                       '<th>'.$choices{'server'}.'</th>'.
 1216:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1217:                       '<th>'.$choices{'custompath'}.'</th>'.
 1218:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1219:         my %disallowed;
 1220:         if (ref($settings) eq 'HASH') {
 1221:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1222:                %disallowed = %{$settings->{'loginvia'}};
 1223:             }
 1224:         }
 1225:         foreach my $lonhost (sort(keys(%servers))) {
 1226:             my $direct = 'selected="selected"';
 1227:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1228:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1229:                     $direct = '';
 1230:                 }
 1231:             }
 1232:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1233:                           '<td><select name="'.$lonhost.'_server">'.
 1234:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1235:                           '</option>';
 1236:             foreach my $hostid (sort(keys(%servers))) {
 1237:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1238:                 my $selected = '';
 1239:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1240:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1241:                         $selected = 'selected="selected"';
 1242:                     }
 1243:                 }
 1244:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1245:                               $servers{$hostid}.'</option>';
 1246:             }
 1247:             $datatable .= '</select></td>'.
 1248:                           '<td><select name="'.$lonhost.'_serverpath">';
 1249:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1250:                 my $pathname = $path;
 1251:                 if ($path eq 'custom') {
 1252:                     $pathname = &mt('Custom Path').' ->';
 1253:                 }
 1254:                 my $selected = '';
 1255:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1256:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1257:                         $selected = 'selected="selected"';
 1258:                     }
 1259:                 } elsif ($path eq '') {
 1260:                     $selected = 'selected="selected"';
 1261:                 }
 1262:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1263:             }
 1264:             $datatable .= '</select></td>';
 1265:             my ($custom,$exempt);
 1266:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1267:                 $custom = $disallowed{$lonhost}{'custompath'};
 1268:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1269:             }
 1270:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1271:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1272:                           '</tr>';
 1273:         }
 1274:         $datatable .= '</table></td></tr>';
 1275:         return $datatable;
 1276:     } elsif ($caller eq 'page') {
 1277:         my %defaultchecked = ( 
 1278:                                'coursecatalog' => 'on',
 1279:                                'helpdesk'      => 'on',
 1280:                                'adminmail'     => 'off',
 1281:                                'newuser'       => 'off',
 1282:                              );
 1283:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1284:         my (%checkedon,%checkedoff);
 1285:         foreach my $item (@toggles) {
 1286:             if ($defaultchecked{$item} eq 'on') { 
 1287:                 $checkedon{$item} = ' checked="checked" ';
 1288:                 $checkedoff{$item} = ' ';
 1289:             } elsif ($defaultchecked{$item} eq 'off') {
 1290:                 $checkedoff{$item} = ' checked="checked" ';
 1291:                 $checkedon{$item} = ' ';
 1292:             }
 1293:         }
 1294:         my @images = ('img','logo','domlogo','login');
 1295:         my @alttext = ('img','logo','domlogo');
 1296:         my @logintext = ('textcol','bgcol');
 1297:         my @bgs = ('pgbg','mainbg','sidebg');
 1298:         my @links = ('link','alink','vlink');
 1299:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1300:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1301:         my (%is_custom,%designs);
 1302:         my %defaults = (
 1303:                        font => $defaultdesign{'login.font'},
 1304:                        );
 1305:         foreach my $item (@images) {
 1306:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1307:             $defaults{'showlogo'}{$item} = 1;
 1308:         }
 1309:         foreach my $item (@bgs) {
 1310:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1311:         }
 1312:         foreach my $item (@logintext) {
 1313:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1314:         }
 1315:         foreach my $item (@links) {
 1316:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1317:         }
 1318:         if (ref($settings) eq 'HASH') {
 1319:             foreach my $item (@toggles) {
 1320:                 if ($settings->{$item} eq '1') {
 1321:                     $checkedon{$item} =  ' checked="checked" ';
 1322:                     $checkedoff{$item} = ' ';
 1323:                 } elsif ($settings->{$item} eq '0') {
 1324:                     $checkedoff{$item} =  ' checked="checked" ';
 1325:                     $checkedon{$item} = ' ';
 1326:                 }
 1327:             }
 1328:             foreach my $item (@images) {
 1329:                 if (defined($settings->{$item})) {
 1330:                     $designs{$item} = $settings->{$item};
 1331:                     $is_custom{$item} = 1;
 1332:                 }
 1333:                 if (defined($settings->{'showlogo'}{$item})) {
 1334:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1335:                 }
 1336:             }
 1337:             foreach my $item (@alttext) {
 1338:                 if (ref($settings->{'alttext'}) eq 'HASH') {
 1339:                     if ($settings->{'alttext'}->{$item} ne '') {
 1340:                         $designs{'alttext'}{$item} = $settings->{'alttext'}{$item};
 1341:                     }
 1342:                 }
 1343:             }
 1344:             foreach my $item (@logintext) {
 1345:                 if ($settings->{$item} ne '') {
 1346:                     $designs{'logintext'}{$item} = $settings->{$item};
 1347:                     $is_custom{$item} = 1;
 1348:                 }
 1349:             }
 1350:             if ($settings->{'font'} ne '') {
 1351:                 $designs{'font'} = $settings->{'font'};
 1352:                 $is_custom{'font'} = 1;
 1353:             }
 1354:             foreach my $item (@bgs) {
 1355:                 if ($settings->{$item} ne '') {
 1356:                     $designs{'bgs'}{$item} = $settings->{$item};
 1357:                     $is_custom{$item} = 1;
 1358:                 }
 1359:             }
 1360:             foreach my $item (@links) {
 1361:                 if ($settings->{$item} ne '') {
 1362:                     $designs{'links'}{$item} = $settings->{$item};
 1363:                     $is_custom{$item} = 1;
 1364:                 }
 1365:             }
 1366:         } else {
 1367:             if ($designhash{$dom.'.login.font'} ne '') {
 1368:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1369:                 $is_custom{'font'} = 1;
 1370:             }
 1371:             foreach my $item (@images) {
 1372:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1373:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1374:                     $is_custom{$item} = 1;
 1375:                 }
 1376:             }
 1377:             foreach my $item (@bgs) {
 1378:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1379:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1380:                     $is_custom{$item} = 1;
 1381:                 }
 1382:             }
 1383:             foreach my $item (@links) {
 1384:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1385:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1386:                     $is_custom{$item} = 1;
 1387:                 }
 1388:             }
 1389:         }
 1390:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1391:                                                       logo => 'Institution Logo',
 1392:                                                       domlogo => 'Domain Logo',
 1393:                                                       login => 'Login box');
 1394:         my $itemcount = 1;
 1395:         foreach my $item (@toggles) {
 1396:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1397:             $datatable .=  
 1398:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1399:                 '</td><td>'.
 1400:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1401:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1402:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1403:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1404:                 '</tr>';
 1405:             $itemcount ++;
 1406:         }
 1407:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1408:         $datatable .= '</tr></table></td></tr>';
 1409:     } elsif ($caller eq 'help') {
 1410:         my ($defaulturl,$defaulttype,%url,%type,%langchoices);
 1411:         my $itemcount = 1;
 1412:         $defaulturl = '/adm/loginproblems.html';
 1413:         $defaulttype = 'default';
 1414:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1415:         my @currlangs;
 1416:         if (ref($settings) eq 'HASH') {
 1417:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1418:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1419:                     next if ($settings->{'helpurl'}{$key} eq '');
 1420:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1421:                     $type{$key} = 'custom';
 1422:                     unless ($key eq 'nolang') {
 1423:                         push(@currlangs,$key);
 1424:                     }
 1425:                 }
 1426:             } elsif ($settings->{'helpurl'} ne '') {
 1427:                 $type{'nolang'} = 'custom';
 1428:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1429:             }
 1430:         }
 1431:         foreach my $lang ('nolang',sort(@currlangs)) {
 1432:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1433:             $datatable .= '<tr'.$css_class.'>';
 1434:             if ($url{$lang} eq '') {
 1435:                 $url{$lang} = $defaulturl;
 1436:             }
 1437:             if ($type{$lang} eq '') {
 1438:                 $type{$lang} = $defaulttype;
 1439:             }
 1440:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1441:             if ($lang eq 'nolang') {
 1442:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1443:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1444:             } else {
 1445:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1446:                                   $langchoices{$lang},
 1447:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1448:             }
 1449:             $datatable .= '</span></td>'."\n".
 1450:                           '<td class="LC_left_item">';
 1451:             if ($type{$lang} eq 'custom') {
 1452:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1453:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1454:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1455:             } else {
 1456:                 $datatable .= $lt{'upl'};
 1457:             }
 1458:             $datatable .='<br />';
 1459:             if ($switchserver) {
 1460:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1461:             } else {
 1462:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1463:             }
 1464:             $datatable .= '</td></tr>';
 1465:             $itemcount ++;
 1466:         }
 1467:         my @addlangs;
 1468:         foreach my $lang (sort(keys(%langchoices))) {
 1469:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1470:             push(@addlangs,$lang);
 1471:         }
 1472:         if (@addlangs > 0) {
 1473:             my %toadd;
 1474:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1475:             $toadd{''} = &mt('Select');
 1476:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1477:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1478:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1479:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1480:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1481:             if ($switchserver) {
 1482:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1483:             } else {
 1484:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1485:             }
 1486:             $datatable .= '</td></tr>';
 1487:             $itemcount ++;
 1488:         }
 1489:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1490:     } elsif ($caller eq 'headtag') {
 1491:         my %domservers = &Apache::lonnet::get_servers($dom);
 1492:         my $choice = $choices{'headtag'};
 1493:         $css_class = ' class="LC_odd_row"';
 1494:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1495:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1496:                       '<th>'.$choices{'current'}.'</th>'.
 1497:                       '<th>'.$choices{'action'}.'</th>'.
 1498:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1499:         my (%currurls,%currexempt);
 1500:         if (ref($settings) eq 'HASH') {
 1501:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1502:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1503:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1504:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1505:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1506:                     }
 1507:                 }
 1508:             }
 1509:         }
 1510:         foreach my $lonhost (sort(keys(%domservers))) {
 1511:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1512:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1513:             if ($currurls{$lonhost}) {
 1514:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1515:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1516:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1517:                               '">'.$lt{'curr'}.'</a></td>'.
 1518:                               '<td><span class="LC_nobreak"><label>'.
 1519:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1520:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1521:             } else {
 1522:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1523:             }
 1524:             $datatable .='<br />';
 1525:             if ($switchserver) {
 1526:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1527:             } else {
 1528:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1529:             }
 1530:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1531:         }
 1532:         $datatable .= '</table></td></tr>';
 1533:     } elsif ($caller eq 'saml') {
 1534:         my %domservers = &Apache::lonnet::get_servers($dom);
 1535:         $datatable .= '<tr><td colspan="3" style="text-align: left">'.
 1536:                       '<table><tr><th>'.$choices{'hostid'}.'</th>'.
 1537:                       '<th>'.$choices{'samllanding'}.'</th>'.
 1538:                       '<th>'.$choices{'samloptions'}.'</th></tr>'."\n";
 1539:         my (%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso,%styleon,%styleoff);
 1540:         foreach my $lonhost (keys(%domservers)) {
 1541:             $samlurl{$lonhost} = '/adm/sso';
 1542:             $styleon{$lonhost} = 'display:none';
 1543:             $styleoff{$lonhost} = '';
 1544:         }
 1545:         if ((ref($settings) eq 'HASH') && (ref($settings->{'saml'}) eq 'HASH')) {
 1546:             foreach my $lonhost (keys(%{$settings->{'saml'}})) {
 1547:                 if (ref($settings->{'saml'}{$lonhost}) eq 'HASH') {
 1548:                     $saml{$lonhost} = 1;
 1549:                     $samltext{$lonhost} = $settings->{'saml'}{$lonhost}{'text'};
 1550:                     $samlimg{$lonhost} = $settings->{'saml'}{$lonhost}{'img'};
 1551:                     $samlalt{$lonhost} = $settings->{'saml'}{$lonhost}{'alt'};
 1552:                     $samlurl{$lonhost} = $settings->{'saml'}{$lonhost}{'url'};
 1553:                     $samltitle{$lonhost} = $settings->{'saml'}{$lonhost}{'title'};
 1554:                     $samlwindow{$lonhost} = $settings->{'saml'}{$lonhost}{'window'};
 1555:                     $samlnotsso{$lonhost} = $settings->{'saml'}{$lonhost}{'notsso'};
 1556:                     $styleon{$lonhost} = '';
 1557:                     $styleoff{$lonhost} = 'display:none';
 1558:                 } else {
 1559:                     $styleon{$lonhost} = 'display:none';
 1560:                     $styleoff{$lonhost} = '';
 1561:                 }
 1562:             }
 1563:         }
 1564:         my $itemcount = 1;
 1565:         foreach my $lonhost (sort(keys(%domservers))) {
 1566:             my $samlon = ' ';
 1567:             my $samloff = ' checked="checked" ';
 1568:             if ($saml{$lonhost}) {
 1569:                 $samlon = $samloff;
 1570:                 $samloff = ' ';
 1571:             }
 1572:             my $samlwinon = '';
 1573:             my $samlwinoff = ' checked="checked"';
 1574:             if ($samlwindow{$lonhost}) {
 1575:                 $samlwinon = $samlwinoff;
 1576:                 $samlwinoff = '';
 1577:             }
 1578:             my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1579:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.$domservers{$lonhost}.'</span></td>'.
 1580:                           '<td><span class="LC_nobreak"><label><input type="radio" name="saml_'.$lonhost.'"'.$samloff.
 1581:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="0" />'.
 1582:                           &mt('No').'</label>'.('&nbsp;'x2).
 1583:                           '<label><input type="radio" name="saml_'.$lonhost.'"'.$samlon.
 1584:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="1" />'.
 1585:                           &mt('Yes').'</label></span></td>'.
 1586:                           '<td id="samloptionson_'.$lonhost.'" style="'.$styleon{$lonhost}.'" width="100%">'.
 1587:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th></tr>'.
 1588:                           '<tr><th>'.&mt('Text').'</th><th>'.&mt('Image').'</th>'.
 1589:                           '<th>'.&mt('Alt Text').'</th></tr>'.
 1590:                           '<tr'.$css_class.'><td><input type="text" name="saml_text_'.$lonhost.'" size="20" value="'.
 1591:                           $samltext{$lonhost}.'" /></td><td>';
 1592:             if ($samlimg{$lonhost}) {
 1593:                 $datatable .= '<img src="'.$samlimg{$lonhost}.'" /><br />'.
 1594:                               '<span class="LC_nobreak"><label>'.
 1595:                               '<input type="checkbox" name="saml_img_del" value="'.$lonhost.'" />'.
 1596:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1597:             } else {
 1598:                 $datatable .= $lt{'upl'};
 1599:             }
 1600:             $datatable .='<br />';
 1601:             if ($switchserver) {
 1602:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1603:             } else {
 1604:                 $datatable .= '<input type="file" name="saml_img_'.$lonhost.'" />';
 1605:             }
 1606:             $datatable .= '</td>'.
 1607:                           '<td><input type="text" name="saml_alt_'.$lonhost.'" size="25" '.
 1608:                           'value="'.$samlalt{$lonhost}.'" /></td></tr></table><br />'.
 1609:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th><th align="center">'.
 1610:                           '<span class="LC_nobreak">'.&mt('Non-SSO').'</span></th></tr>'.
 1611:                           '<tr><th>'.&mt('URL').'</th><th>'.&mt('Tool Tip').'</th>'.
 1612:                           '<th>'.&mt('Pop-up if iframe').'</th><th>'.&mt('Text').'</th></tr>'.
 1613:                           '<tr'.$css_class.'>'.
 1614:                           '<td><input type="text" name="saml_url_'.$lonhost.'" size="30" '.
 1615:                           'value="'.$samlurl{$lonhost}.'" /></td>'.
 1616:                           '<td><textarea name="saml_title_'.$lonhost.'" rows="3" cols="20">'.
 1617:                           $samltitle{$lonhost}.'</textarea></td>'.
 1618:                           '<td><label><input type="radio" name="saml_window_'.$lonhost.'" value=""'.$samlwinoff.'>'.
 1619:                           &mt('No').'</label>'.('&nbsp;'x2).'<label><input type="radio" '.
 1620:                           'name="saml_window_'.$lonhost.'" value="1"'.$samlwinon.'>'.&mt('Yes').'</label></td>'.
 1621:                           '<td><input type="text" name="saml_notsso_'.$lonhost.'" size="12" '.
 1622:                           'value="'.$samlnotsso{$lonhost}.'" /></td></tr>'.
 1623:                           '</table></td>'.
 1624:                           '<td id="samloptionsoff_'.$lonhost.'" style="'.$styleoff{$lonhost}.'" width="100%">&nbsp;</td></tr>';
 1625:            $itemcount ++;
 1626:         }
 1627:         $datatable .= '</table></td></tr>';
 1628:     }
 1629:     return $datatable;
 1630: }
 1631: 
 1632: sub login_choices {
 1633:     my %choices =
 1634:         &Apache::lonlocal::texthash (
 1635:             coursecatalog => 'Display Course/Community Catalog link?',
 1636:             adminmail     => "Display Administrator's E-mail Address?",
 1637:             helpdesk      => 'Display "Contact Helpdesk" link',
 1638:             disallowlogin => "Login page requests redirected",
 1639:             hostid        => "Server",
 1640:             server        => "Redirect to:",
 1641:             serverpath    => "Path",
 1642:             custompath    => "Custom", 
 1643:             exempt        => "Exempt IP(s)",
 1644:             directlogin   => "No redirect",
 1645:             newuser       => "Link to create a user account",
 1646:             img           => "Header",
 1647:             logo          => "Main Logo",
 1648:             domlogo       => "Domain Logo",
 1649:             login         => "Log-in Header", 
 1650:             textcol       => "Text color",
 1651:             bgcol         => "Box color",
 1652:             bgs           => "Background colors",
 1653:             links         => "Link colors",
 1654:             font          => "Font color",
 1655:             pgbg          => "Header",
 1656:             mainbg        => "Page",
 1657:             sidebg        => "Login box",
 1658:             link          => "Link",
 1659:             alink         => "Active link",
 1660:             vlink         => "Visited link",
 1661:             headtag       => "Custom markup",
 1662:             action        => "Action",
 1663:             current       => "Current",
 1664:             samllanding   => "Dual login?",
 1665:             samloptions   => "Options",
 1666:             alttext       => "Alt text",
 1667:         );
 1668:     return %choices;
 1669: }
 1670: 
 1671: sub login_file_options {
 1672:       return &Apache::lonlocal::texthash(
 1673:                                            del     => 'Delete?',
 1674:                                            rep     => 'Replace:',
 1675:                                            upl     => 'Upload:',
 1676:                                            curr    => 'View contents',
 1677:                                            default => 'Default',
 1678:                                            custom  => 'Custom',
 1679:                                            none    => 'None',
 1680:       );
 1681: }
 1682: 
 1683: sub print_ipaccess {
 1684:     my ($dom,$settings,$rowtotal) = @_;
 1685:     my $css_class;
 1686:     my $itemcount = 0;
 1687:     my $datatable;
 1688:     my %ordered;
 1689:     if (ref($settings) eq 'HASH') {
 1690:         foreach my $item (keys(%{$settings})) {
 1691:             if (ref($settings->{$item}) eq 'HASH') {
 1692:                 my $num = $settings->{$item}{'order'};
 1693:                 if ($num eq '') {
 1694:                     $num = scalar(keys(%{$settings}));
 1695:                 }
 1696:                 $ordered{$num} = $item;
 1697:             }
 1698:         }
 1699:     }
 1700:     my $maxnum = scalar(keys(%ordered));
 1701:     if (keys(%ordered)) {
 1702:         my @items = sort { $a <=> $b } keys(%ordered);
 1703:         for (my $i=0; $i<@items; $i++) {
 1704:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1705:             my $item = $ordered{$items[$i]};
 1706:             my ($name,$ipranges,%commblocks,%courses);
 1707:             if (ref($settings->{$item}) eq 'HASH') {
 1708:                 $name = $settings->{$item}->{'name'};
 1709:                 $ipranges = $settings->{$item}->{'ip'};
 1710:                 if (ref($settings->{$item}->{'commblocks'}) eq 'HASH') {
 1711:                     %commblocks = %{$settings->{$item}->{'commblocks'}};
 1712:                 }
 1713:                 if (ref($settings->{$item}->{'courses'}) eq 'HASH') {
 1714:                     %courses = %{$settings->{$item}->{'courses'}};
 1715:                 }
 1716:             }
 1717:             my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_".$item."'".');"';
 1718:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 1719:                          .'<select name="ipaccess_pos_'.$item.'"'.$chgstr.'>';
 1720:             for (my $k=0; $k<=$maxnum; $k++) {
 1721:                 my $vpos = $k+1;
 1722:                 my $selstr;
 1723:                 if ($k == $i) {
 1724:                     $selstr = ' selected="selected" ';
 1725:                 }
 1726:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1727:             }
 1728:             $datatable .= '</select>'.('&nbsp;'x2).
 1729:                 '<label><input type="checkbox" name="ipaccess_del" value="'.$item.'" />'.
 1730:                 &mt('Delete?').'</label></span></td>'.
 1731:                 '<td colspan="2"><input type="hidden" name="ipaccess_id_'.$i.'" value="'.$item.'" />'.
 1732:                 &ipaccess_options($i,$itemcount,$dom,$name,$ipranges,\%commblocks,\%courses).
 1733:                 '</td></tr>';
 1734:             $itemcount ++;
 1735:         }
 1736:     }
 1737:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1738:     my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_add'".');"';
 1739:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 1740:                   '<input type="hidden" name="ipaccess_maxnum" value="'.$maxnum.'" />'."\n".
 1741:                   '<select name="ipaccess_pos_add"'.$chgstr.'>';
 1742:     for (my $k=0; $k<$maxnum+1; $k++) {
 1743:         my $vpos = $k+1;
 1744:         my $selstr;
 1745:         if ($k == $maxnum) {
 1746:             $selstr = ' selected="selected" ';
 1747:         }
 1748:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1749:     }
 1750:     $datatable .= '</select>&nbsp;'."\n".
 1751:                   '<input type="checkbox" name="ipaccess_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 1752:                   '<td colspan="2">'.
 1753:                   &ipaccess_options('add',$itemcount,$dom).
 1754:                   '</td>'."\n".
 1755:                   '</tr>'."\n";
 1756:     $$rowtotal ++;
 1757:     return $datatable;
 1758: }
 1759: 
 1760: sub ipaccess_options {
 1761:     my ($num,$itemcount,$dom,$name,$ipranges,$blocksref,$coursesref) = @_;
 1762:     my (%currblocks,%currcourses,$output);
 1763:     if (ref($blocksref) eq 'HASH') {
 1764:         %currblocks = %{$blocksref};
 1765:     }
 1766:     if (ref($coursesref) eq 'HASH') {
 1767:         %currcourses = %{$coursesref};
 1768:     }
 1769:     $output = '<fieldset><legend>'.&mt('Location(s)').'</legend>'.
 1770:               '<span class="LC_nobreak">'.&mt('Name').':&nbsp;'.
 1771:               '<input type="text" name="ipaccess_name_'.$num.'" value="'.$name.'" />'.
 1772:               '</span></fieldset>'.
 1773:               '<fieldset><legend>'.&mt('IP Range(s)').'</legend>'.
 1774:               &mt('Format for each IP range').': '.&mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 1775:               &mt('Range(s) will be stored as IP netblock(s) in CIDR notation (comma separated)').'<br />'.
 1776:               '<textarea name="ipaccess_range_'.$num.'" rows="3" cols="80">'.
 1777:               $ipranges.'</textarea></fieldset>'.
 1778:               '<fieldset><legend>'.&mt('Functionality Blocked?').'</legend>'.
 1779:               &blocker_checkboxes($num,$blocksref).'</fieldset>'.
 1780:               '<fieldset><legend>'.&mt('Courses/Communities allowed').'</legend>'.
 1781:               '<table>';
 1782:     foreach my $cid (sort(keys(%currcourses))) {
 1783:         my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
 1784:         $output .= '<tr><td><span class="LC_nobreak">'.
 1785:                    '<label><input type="checkbox" name="ipaccess_course_delete_'.$num.'" value="'.$cid.'" />'.
 1786:                    &mt('Delete?').'&nbsp;<span class="LC_cusr_emph">'.$courseinfo{'description'}.'</span></label></span>'.
 1787:                    ' <span class="LC_fontsize_medium">('.$cid.')</span></td></tr>';
 1788:     }
 1789:     $output .= '<tr><td><span class="LC_nobreak">'.&mt('Add').':&nbsp;'.
 1790:                '<input type="text" name="ipaccess_cdesc_'.$num.'" value="" onfocus="this.blur();opencrsbrowser('."'display','ipaccess_cnum_$num','ipaccess_cdom_$num','ipaccess_cdesc_$num'".');" />'.
 1791:                 &Apache::loncommon::selectcourse_link('display','ipaccess_cnum_'.$num,'ipaccess_cdom_'.$num,'ipaccess_cdesc_'.$num,$dom,undef,'Course/Community').
 1792:                '<input type="hidden" name="ipaccess_cnum_'.$num.'" value="" />'.
 1793:                '<input type="hidden" name="ipaccess_cdom_'.$num.'" value="" />'.
 1794:                '</span></td></tr></table>'."\n".
 1795:                '</fieldset>';
 1796:     return $output;
 1797: }
 1798: 
 1799: sub blocker_checkboxes {
 1800:     my ($num,$blocks) = @_;
 1801:     my ($typeorder,$types) = &commblocktype_text();
 1802:     my $numinrow = 6;
 1803:     my $output = '<table>';
 1804:     for (my $i=0; $i<@{$typeorder}; $i++) {
 1805:         my $block = $typeorder->[$i];
 1806:         my $blockstatus;
 1807:         if (ref($blocks) eq 'HASH') {
 1808:             if ($blocks->{$block} eq 'on') {
 1809:                 $blockstatus = 'checked="checked"';
 1810:             }
 1811:         }
 1812:         my $rem = $i%($numinrow);
 1813:         if ($rem == 0) {
 1814:             if ($i > 0) {
 1815:                 $output .= '</tr>';
 1816:             }
 1817:             $output .= '<tr>';
 1818:         }
 1819:         if ($i == scalar(@{$typeorder})-1) {
 1820:             my $colsleft = $numinrow-$rem;
 1821:             if ($colsleft > 1) {
 1822:                 $output .= '<td colspan="'.$colsleft.'">';
 1823:             } else {
 1824:                 $output .= '<td>';
 1825:             }
 1826:         } else {
 1827:             $output .= '<td>';
 1828:         }
 1829:         my $item = 'ipaccess_block_'.$num;
 1830:         if ($blockstatus) {
 1831:             $blockstatus = ' '.$blockstatus;
 1832:         }
 1833:         $output .= '<span class="LC_nobreak"><label>'."\n".
 1834:                    '<input type="checkbox" name="'.$item.'"'.
 1835:                    $blockstatus.' value="'.$block.'"'.' />'.
 1836:                    $types->{$block}.'</label></span>'."\n".
 1837:                    '<br /></td>';
 1838:     }
 1839:     $output .= '</tr></table>';
 1840:     return $output;
 1841: }
 1842: 
 1843: sub commblocktype_text {
 1844:     my %types = &Apache::lonlocal::texthash(
 1845:         'com' => 'Messaging',
 1846:         'chat' => 'Chat Room',
 1847:         'boards' => 'Discussion',
 1848:         'port' => 'Portfolio',
 1849:         'groups' => 'Groups',
 1850:         'blogs' => 'Blogs',
 1851:         'about' => 'User Information',
 1852:         'printout' => 'Printouts',
 1853:         'passwd' => 'Change Password',
 1854:         'grades' => 'Gradebook',
 1855:         'search' => 'Course search',
 1856:         'wishlist' => 'Stored links',
 1857:         'annotate' => 'Annotations',
 1858:     );
 1859:     my $typeorder = ['com','chat','boards','port','groups','blogs','about','wishlist','printout','grades','search','annotate','passwd'];
 1860:     return ($typeorder,\%types);
 1861: }
 1862: 
 1863: sub print_rolecolors {
 1864:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1865:     my %choices = &color_font_choices();
 1866:     my @bgs = ('pgbg','tabbg','sidebg');
 1867:     my @links = ('link','alink','vlink');
 1868:     my @images = ('img');
 1869:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1870:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1871:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1872:     my (%is_custom,%designs);
 1873:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1874:     if (ref($settings) eq 'HASH') {
 1875:         if (ref($settings->{$role}) eq 'HASH') {
 1876:             if ($settings->{$role}->{'img'} ne '') {
 1877:                 $designs{'img'} = $settings->{$role}->{'img'};
 1878:                 $is_custom{'img'} = 1;
 1879:             }
 1880:             if ($settings->{$role}->{'font'} ne '') {
 1881:                 $designs{'font'} = $settings->{$role}->{'font'};
 1882:                 $is_custom{'font'} = 1;
 1883:             }
 1884:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1885:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1886:                 $is_custom{'fontmenu'} = 1;
 1887:             }
 1888:             foreach my $item (@bgs) {
 1889:                 if ($settings->{$role}->{$item} ne '') {
 1890:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1891:                     $is_custom{$item} = 1;
 1892:                 }
 1893:             }
 1894:             foreach my $item (@links) {
 1895:                 if ($settings->{$role}->{$item} ne '') {
 1896:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1897:                     $is_custom{$item} = 1;
 1898:                 }
 1899:             }
 1900:         }
 1901:     } else {
 1902:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1903:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1904:             $is_custom{'img'} = 1;
 1905:         }
 1906:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1907:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1908:             $is_custom{'fontmenu'} = 1; 
 1909:         }
 1910:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1911:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1912:             $is_custom{'font'} = 1;
 1913:         }
 1914:         foreach my $item (@bgs) {
 1915:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1916:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1917:                 $is_custom{$item} = 1;
 1918:             
 1919:             }
 1920:         }
 1921:         foreach my $item (@links) {
 1922:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1923:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1924:                 $is_custom{$item} = 1;
 1925:             }
 1926:         }
 1927:     }
 1928:     my $itemcount = 1;
 1929:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1930:     $datatable .= '</tr></table></td></tr>';
 1931:     return $datatable;
 1932: }
 1933: 
 1934: sub role_defaults {
 1935:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1936:     my %defaults;
 1937:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1938:         return %defaults;
 1939:     }
 1940:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1941:     if ($role eq 'login') {
 1942:         %defaults = (
 1943:                        font => $defaultdesign{$role.'.font'},
 1944:                     );
 1945:         if (ref($logintext) eq 'ARRAY') {
 1946:             foreach my $item (@{$logintext}) {
 1947:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1948:             }
 1949:         }
 1950:         foreach my $item (@{$images}) {
 1951:             $defaults{'showlogo'}{$item} = 1;
 1952:         }
 1953:     } else {
 1954:         %defaults = (
 1955:                        img => $defaultdesign{$role.'.img'},
 1956:                        font => $defaultdesign{$role.'.font'},
 1957:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1958:                     );
 1959:     }
 1960:     foreach my $item (@{$bgs}) {
 1961:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1962:     }
 1963:     foreach my $item (@{$links}) {
 1964:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1965:     }
 1966:     foreach my $item (@{$images}) {
 1967:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1968:     }
 1969:     return %defaults;
 1970: }
 1971: 
 1972: sub display_color_options {
 1973:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1974:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1975:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1976:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1977:     my $datatable = '<tr'.$css_class.'>'.
 1978:         '<td>'.$choices->{'font'}.'</td>';
 1979:     if (!$is_custom->{'font'}) {
 1980:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1981:     } else {
 1982:         $datatable .= '<td>&nbsp;</td>';
 1983:     }
 1984:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1985: 
 1986:     $datatable .= '<td><span class="LC_nobreak">'.
 1987:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1988:                   ' value="'.$current_color.'" />&nbsp;'.
 1989:                   '&nbsp;</span></td></tr>';
 1990:     unless ($role eq 'login') { 
 1991:         $datatable .= '<tr'.$css_class.'>'.
 1992:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1993:         if (!$is_custom->{'fontmenu'}) {
 1994:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1995:         } else {
 1996:             $datatable .= '<td>&nbsp;</td>';
 1997:         }
 1998: 	$current_color = $designs->{'fontmenu'} ?
 1999: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 2000:         $datatable .= '<td><span class="LC_nobreak">'.
 2001:                       '<input class="colorchooser" type="text" size="10" name="'
 2002: 		      .$role.'_fontmenu"'.
 2003:                       ' value="'.$current_color.'" />&nbsp;'.
 2004:                       '&nbsp;</span></td></tr>';
 2005:     }
 2006:     my $switchserver = &check_switchserver($dom,$confname);
 2007:     foreach my $img (@{$images}) {
 2008: 	$itemcount ++;
 2009:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2010:         $datatable .= '<tr'.$css_class.'>'.
 2011:                       '<td>'.$choices->{$img};
 2012:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors,$alttext);
 2013:         if ($role eq 'login') {
 2014:             if ($img eq 'login') {
 2015:                 $login_hdr_pick =
 2016:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 2017:                 $logincolors =
 2018:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 2019:                                        $designs,$defaults);
 2020:             } else {
 2021:                 if ($img ne 'domlogo') {
 2022:                     $datatable.= &logo_display_options($img,$defaults,$designs);
 2023:                 }
 2024:                 if (ref($designs->{'alttext'}) eq 'HASH') {
 2025:                     $alttext = $designs->{'alttext'}{$img};
 2026:                 }
 2027:             }
 2028:         }
 2029:         $datatable .= '</td>';
 2030:         if ($designs->{$img} ne '') {
 2031:             $imgfile = $designs->{$img};
 2032: 	    $img_import = ($imgfile =~ m{^/adm/});
 2033:         } else {
 2034:             $imgfile = $defaults->{$img};
 2035:         }
 2036:         if ($imgfile) {
 2037:             my ($showfile,$fullsize);
 2038:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 2039:                 my $urldir = $1;
 2040:                 my $filename = $2;
 2041:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 2042:                 if (@info) {
 2043:                     my $thumbfile = 'tn-'.$filename;
 2044:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 2045:                     if (@thumb) {
 2046:                         $showfile = $urldir.'/'.$thumbfile;
 2047:                     } else {
 2048:                         $showfile = $imgfile;
 2049:                     }
 2050:                 } else {
 2051:                     $showfile = '';
 2052:                 }
 2053:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 2054:                 $showfile = $imgfile;
 2055:                 my $imgdir = $1;
 2056:                 my $filename = $2;
 2057:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 2058:                     $showfile = "/$imgdir/tn-".$filename;
 2059:                 } else {
 2060:                     my $input = $londocroot.$imgfile;
 2061:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 2062:                     if (!-e $output) {
 2063:                         my ($width,$height) = &thumb_dimensions();
 2064:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 2065:                         if ($fullwidth ne '' && $fullheight ne '') {
 2066:                             if ($fullwidth > $width && $fullheight > $height) { 
 2067:                                 my $size = $width.'x'.$height;
 2068:                                 my @args = ('convert','-sample',$size,$input,$output);
 2069:                                 system({$args[0]} @args);
 2070:                                 $showfile = "/$imgdir/tn-".$filename;
 2071:                             }
 2072:                         }
 2073:                     }
 2074:                 }
 2075:             }
 2076:             if ($showfile) {
 2077:                 if ($showfile =~ m{^/(adm|res)/}) {
 2078:                     if ($showfile =~ m{^/res/}) {
 2079:                         my $local_showfile =
 2080:                             &Apache::lonnet::filelocation('',$showfile);
 2081:                         &Apache::lonnet::repcopy($local_showfile);
 2082:                     }
 2083:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 2084:                 }
 2085:                 if ($imgfile) {
 2086:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 2087:                         if ($imgfile =~ m{^/res/}) {
 2088:                             my $local_imgfile =
 2089:                                 &Apache::lonnet::filelocation('',$imgfile);
 2090:                             &Apache::lonnet::repcopy($local_imgfile);
 2091:                         }
 2092:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 2093:                     } else {
 2094:                         $fullsize = $imgfile;
 2095:                     }
 2096:                 }
 2097:                 $datatable .= '<td>';
 2098:                 if ($img eq 'login') {
 2099:                     $datatable .= $login_hdr_pick;
 2100:                 } 
 2101:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 2102:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 2103:             } else {
 2104:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2105:                               &mt('Upload:').'<br />';
 2106:             }
 2107:         } else {
 2108:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2109:                           &mt('Upload:').'<br />';
 2110:         }
 2111:         if ($switchserver) {
 2112:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2113:         } else {
 2114:             if ($img ne 'login') { # suppress file selection for Log-in header
 2115:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 2116:             }
 2117:         }
 2118:         if (($role eq 'login') && ($img ne 'login')) {
 2119:             $datatable .= ('&nbsp;' x2).' <span class="LC_nobreak"><label>'.$choices->{'alttext'}.':'.
 2120:                           '<input type="text" name="'.$role.'_alt_'.$img.'" size="10" value="'.$alttext.'" />'.
 2121:                           '</label></span>';
 2122:         }
 2123:         $datatable .= '</td></tr>';
 2124:     }
 2125:     $itemcount ++;
 2126:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2127:     $datatable .= '<tr'.$css_class.'>'.
 2128:                   '<td>'.$choices->{'bgs'}.'</td>';
 2129:     my $bgs_def;
 2130:     foreach my $item (@{$bgs}) {
 2131:         if (!$is_custom->{$item}) {
 2132:             $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>';
 2133:         }
 2134:     }
 2135:     if ($bgs_def) {
 2136:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 2137:     } else {
 2138:         $datatable .= '<td>&nbsp;</td>';
 2139:     }
 2140:     $datatable .= '<td class="LC_right_item">'.
 2141:                   '<table border="0"><tr>';
 2142: 
 2143:     foreach my $item (@{$bgs}) {
 2144:         $datatable .= '<td align="center">'.$choices->{$item};
 2145: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 2146:         if ($designs->{'bgs'}{$item}) {
 2147:             $datatable .= '&nbsp;';
 2148:         }
 2149:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2150:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2151:     }
 2152:     $datatable .= '</tr></table></td></tr>';
 2153:     $itemcount ++;
 2154:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2155:     $datatable .= '<tr'.$css_class.'>'.
 2156:                   '<td>'.$choices->{'links'}.'</td>';
 2157:     my $links_def;
 2158:     foreach my $item (@{$links}) {
 2159:         if (!$is_custom->{$item}) {
 2160:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 2161:         }
 2162:     }
 2163:     if ($links_def) {
 2164:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 2165:     } else {
 2166:         $datatable .= '<td>&nbsp;</td>';
 2167:     }
 2168:     $datatable .= '<td class="LC_right_item">'.
 2169:                   '<table border="0"><tr>';
 2170:     foreach my $item (@{$links}) {
 2171: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 2172:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 2173:         if ($designs->{'links'}{$item}) {
 2174:             $datatable.='&nbsp;';
 2175:         }
 2176:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 2177:                       '" /></td>';
 2178:     }
 2179:     $$rowtotal += $itemcount;
 2180:     return $datatable;
 2181: }
 2182: 
 2183: sub logo_display_options {
 2184:     my ($img,$defaults,$designs) = @_;
 2185:     my $checkedon;
 2186:     if (ref($defaults) eq 'HASH') {
 2187:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 2188:             if ($defaults->{'showlogo'}{$img}) {
 2189:                 $checkedon = 'checked="checked" ';     
 2190:             }
 2191:         } 
 2192:     }
 2193:     if (ref($designs) eq 'HASH') {
 2194:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 2195:             if (defined($designs->{'showlogo'}{$img})) {
 2196:                 if ($designs->{'showlogo'}{$img} == 0) {
 2197:                     $checkedon = '';
 2198:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 2199:                     $checkedon = 'checked="checked" ';
 2200:                 }
 2201:             }
 2202:         }
 2203:     }
 2204:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 2205:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 2206:            &mt('show').'</label>'."\n";
 2207: }
 2208: 
 2209: sub login_header_options  {
 2210:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 2211:     my $output = '';
 2212:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 2213:         $output .= &mt('Text default(s):').'<br />';
 2214:         if (!$is_custom->{'textcol'}) {
 2215:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 2216:                        '&nbsp;&nbsp;&nbsp;';
 2217:         }
 2218:         if (!$is_custom->{'bgcol'}) {
 2219:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 2220:                        '<span id="css_'.$role.'_font" style="background-color: '.
 2221:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 2222:         }
 2223:         $output .= '<br />';
 2224:     }
 2225:     $output .='<br />';
 2226:     return $output;
 2227: }
 2228: 
 2229: sub login_text_colors {
 2230:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 2231:     my $color_menu = '<table border="0"><tr>';
 2232:     foreach my $item (@{$logintext}) {
 2233:         $color_menu .= '<td align="center">'.$choices->{$item};
 2234:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 2235:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2236:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2237:     }
 2238:     $color_menu .= '</tr></table><br />';
 2239:     return $color_menu;
 2240: }
 2241: 
 2242: sub image_changes {
 2243:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 2244:     my $output;
 2245:     if ($img eq 'login') {
 2246:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 2247:     } elsif (!$is_custom) {
 2248:         if ($img ne 'domlogo') {
 2249:             $output = &mt('Default image:').'<br />';
 2250:         } else {
 2251:             $output = &mt('Default in use:').'<br />';
 2252:         }
 2253:     }
 2254:     if ($img ne 'login') {
 2255:         if ($img_import) {
 2256:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 2257:         }
 2258:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 2259:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 2260:         if ($is_custom) {
 2261:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 2262:                        '<input type="checkbox" name="'.
 2263:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 2264:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 2265:         } else {
 2266:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 2267:         }
 2268:     }
 2269:     return $output;
 2270: }
 2271: 
 2272: sub print_quotas {
 2273:     my ($dom,$settings,$rowtotal,$action) = @_;
 2274:     my $context;
 2275:     if ($action eq 'quotas') {
 2276:         $context = 'tools';
 2277:     } else {
 2278:         $context = $action;
 2279:     }
 2280:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 2281:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2282:     my $typecount = 0;
 2283:     my ($css_class,%titles);
 2284:     if ($context eq 'requestcourses') {
 2285:         @usertools = ('official','unofficial','community','textbook','lti');
 2286:         @options =('norequest','approval','validate','autolimit');
 2287:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2288:         %titles = &courserequest_titles();
 2289:     } elsif ($context eq 'requestauthor') {
 2290:         @usertools = ('author');
 2291:         @options = ('norequest','approval','automatic');
 2292:         %titles = &authorrequest_titles();
 2293:     } else {
 2294:         @usertools = ('aboutme','blog','webdav','portfolio','timezone');
 2295:         %titles = &tool_titles();
 2296:     }
 2297:     if (ref($types) eq 'ARRAY') {
 2298:         foreach my $type (@{$types}) {
 2299:             my ($currdefquota,$currauthorquota);
 2300:             unless (($context eq 'requestcourses') ||
 2301:                     ($context eq 'requestauthor')) {
 2302:                 if (ref($settings) eq 'HASH') {
 2303:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2304:                         $currdefquota = $settings->{defaultquota}->{$type};
 2305:                     } else {
 2306:                         $currdefquota = $settings->{$type};
 2307:                     }
 2308:                     if (ref($settings->{authorquota}) eq 'HASH') {
 2309:                         $currauthorquota = $settings->{authorquota}->{$type};
 2310:                     }
 2311:                 }
 2312:             }
 2313:             if (defined($usertypes->{$type})) {
 2314:                 $typecount ++;
 2315:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2316:                 $datatable .= '<tr'.$css_class.'>'.
 2317:                               '<td>'.$usertypes->{$type}.'</td>'.
 2318:                               '<td class="LC_left_item">';
 2319:                 if ($context eq 'requestcourses') {
 2320:                     $datatable .= '<table><tr>';
 2321:                 }
 2322:                 my %cell;  
 2323:                 foreach my $item (@usertools) {
 2324:                     if ($context eq 'requestcourses') {
 2325:                         my ($curroption,$currlimit);
 2326:                         if (ref($settings) eq 'HASH') {
 2327:                             if (ref($settings->{$item}) eq 'HASH') {
 2328:                                 $curroption = $settings->{$item}->{$type};
 2329:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2330:                                     $currlimit = $1; 
 2331:                                 }
 2332:                             }
 2333:                         }
 2334:                         if (!$curroption) {
 2335:                             $curroption = 'norequest';
 2336:                         }
 2337:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2338:                         foreach my $option (@options) {
 2339:                             my $val = $option;
 2340:                             if ($option eq 'norequest') {
 2341:                                 $val = 0;  
 2342:                             }
 2343:                             if ($option eq 'validate') {
 2344:                                 my $canvalidate = 0;
 2345:                                 if (ref($validations{$item}) eq 'HASH') { 
 2346:                                     if ($validations{$item}{$type}) {
 2347:                                         $canvalidate = 1;
 2348:                                     }
 2349:                                 }
 2350:                                 next if (!$canvalidate);
 2351:                             }
 2352:                             my $checked = '';
 2353:                             if ($option eq $curroption) {
 2354:                                 $checked = ' checked="checked"';
 2355:                             } elsif ($option eq 'autolimit') {
 2356:                                 if ($curroption =~ /^autolimit/) {
 2357:                                     $checked = ' checked="checked"';
 2358:                                 }                       
 2359:                             } 
 2360:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2361:                                   '<input type="radio" name="crsreq_'.$item.
 2362:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2363:                                   $titles{$option}.'</label>';
 2364:                             if ($option eq 'autolimit') {
 2365:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2366:                                                 $item.'_limit_'.$type.'" size="1" '.
 2367:                                                 'value="'.$currlimit.'" />';
 2368:                             }
 2369:                             $cell{$item} .= '</span> ';
 2370:                             if ($option eq 'autolimit') {
 2371:                                 $cell{$item} .= $titles{'unlimited'};
 2372:                             }
 2373:                         }
 2374:                     } elsif ($context eq 'requestauthor') {
 2375:                         my $curroption;
 2376:                         if (ref($settings) eq 'HASH') {
 2377:                             $curroption = $settings->{$type};
 2378:                         }
 2379:                         if (!$curroption) {
 2380:                             $curroption = 'norequest';
 2381:                         }
 2382:                         foreach my $option (@options) {
 2383:                             my $val = $option;
 2384:                             if ($option eq 'norequest') {
 2385:                                 $val = 0;
 2386:                             }
 2387:                             my $checked = '';
 2388:                             if ($option eq $curroption) {
 2389:                                 $checked = ' checked="checked"';
 2390:                             }
 2391:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2392:                                   '<input type="radio" name="authorreq_'.$type.
 2393:                                   '" value="'.$val.'"'.$checked.' />'.
 2394:                                   $titles{$option}.'</label></span>&nbsp; ';
 2395:                         }
 2396:                     } else {
 2397:                         my $checked = 'checked="checked" ';
 2398:                         if ($item eq 'timezone') {
 2399:                             $checked = '';
 2400:                         }
 2401:                         if (ref($settings) eq 'HASH') {
 2402:                             if (ref($settings->{$item}) eq 'HASH') {
 2403:                                 if (!$settings->{$item}->{$type}) {
 2404:                                     $checked = '';
 2405:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2406:                                     $checked =  'checked="checked" ';
 2407:                                 }
 2408:                             }
 2409:                         }
 2410:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2411:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2412:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2413:                                       '</label></span>&nbsp; ';
 2414:                     }
 2415:                 }
 2416:                 if ($context eq 'requestcourses') {
 2417:                     $datatable .= '</tr><tr>';
 2418:                     foreach my $item (@usertools) {
 2419:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2420:                     }
 2421:                     $datatable .= '</tr></table>';
 2422:                 }
 2423:                 $datatable .= '</td>';
 2424:                 unless (($context eq 'requestcourses') ||
 2425:                         ($context eq 'requestauthor')) {
 2426:                     $datatable .= 
 2427:                               '<td class="LC_right_item">'.
 2428:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2429:                               '<input type="text" name="quota_'.$type.
 2430:                               '" value="'.$currdefquota.
 2431:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2432:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2433:                               '<input type="text" name="authorquota_'.$type.
 2434:                               '" value="'.$currauthorquota.
 2435:                               '" size="5" /></span></td>';
 2436:                 }
 2437:                 $datatable .= '</tr>';
 2438:             }
 2439:         }
 2440:     }
 2441:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2442:         $defaultquota = '20';
 2443:         $authorquota = '500';
 2444:         if (ref($settings) eq 'HASH') {
 2445:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2446:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2447:             } elsif (defined($settings->{'default'})) {
 2448:                 $defaultquota = $settings->{'default'};
 2449:             }
 2450:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2451:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2452:             }
 2453:         }
 2454:     }
 2455:     $typecount ++;
 2456:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2457:     $datatable .= '<tr'.$css_class.'>'.
 2458:                   '<td>'.$othertitle.'</td>'.
 2459:                   '<td class="LC_left_item">';
 2460:     if ($context eq 'requestcourses') {
 2461:         $datatable .= '<table><tr>';
 2462:     }
 2463:     my %defcell;
 2464:     foreach my $item (@usertools) {
 2465:         if ($context eq 'requestcourses') {
 2466:             my ($curroption,$currlimit);
 2467:             if (ref($settings) eq 'HASH') {
 2468:                 if (ref($settings->{$item}) eq 'HASH') {
 2469:                     $curroption = $settings->{$item}->{'default'};
 2470:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2471:                         $currlimit = $1;
 2472:                     }
 2473:                 }
 2474:             }
 2475:             if (!$curroption) {
 2476:                 $curroption = 'norequest';
 2477:             }
 2478:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2479:             foreach my $option (@options) {
 2480:                 my $val = $option;
 2481:                 if ($option eq 'norequest') {
 2482:                     $val = 0;
 2483:                 }
 2484:                 if ($option eq 'validate') {
 2485:                     my $canvalidate = 0;
 2486:                     if (ref($validations{$item}) eq 'HASH') {
 2487:                         if ($validations{$item}{'default'}) {
 2488:                             $canvalidate = 1;
 2489:                         }
 2490:                     }
 2491:                     next if (!$canvalidate);
 2492:                 }
 2493:                 my $checked = '';
 2494:                 if ($option eq $curroption) {
 2495:                     $checked = ' checked="checked"';
 2496:                 } elsif ($option eq 'autolimit') {
 2497:                     if ($curroption =~ /^autolimit/) {
 2498:                         $checked = ' checked="checked"';
 2499:                     }
 2500:                 }
 2501:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2502:                                   '<input type="radio" name="crsreq_'.$item.
 2503:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2504:                                   $titles{$option}.'</label>';
 2505:                 if ($option eq 'autolimit') {
 2506:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2507:                                        $item.'_limit_default" size="1" '.
 2508:                                        'value="'.$currlimit.'" />';
 2509:                 }
 2510:                 $defcell{$item} .= '</span> ';
 2511:                 if ($option eq 'autolimit') {
 2512:                     $defcell{$item} .= $titles{'unlimited'};
 2513:                 }
 2514:             }
 2515:         } elsif ($context eq 'requestauthor') {
 2516:             my $curroption;
 2517:             if (ref($settings) eq 'HASH') {
 2518:                 $curroption = $settings->{'default'};
 2519:             }
 2520:             if (!$curroption) {
 2521:                 $curroption = 'norequest';
 2522:             }
 2523:             foreach my $option (@options) {
 2524:                 my $val = $option;
 2525:                 if ($option eq 'norequest') {
 2526:                     $val = 0;
 2527:                 }
 2528:                 my $checked = '';
 2529:                 if ($option eq $curroption) {
 2530:                     $checked = ' checked="checked"';
 2531:                 }
 2532:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2533:                               '<input type="radio" name="authorreq_default"'.
 2534:                               ' value="'.$val.'"'.$checked.' />'.
 2535:                               $titles{$option}.'</label></span>&nbsp; ';
 2536:             }
 2537:         } else {
 2538:             my $checked = 'checked="checked" ';
 2539:             if (ref($settings) eq 'HASH') {
 2540:                 if (ref($settings->{$item}) eq 'HASH') {
 2541:                     if ($settings->{$item}->{'default'} == 0) {
 2542:                         $checked = '';
 2543:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2544:                         $checked = 'checked="checked" ';
 2545:                     }
 2546:                 }
 2547:             }
 2548:             $datatable .= '<span class="LC_nobreak"><label>'.
 2549:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2550:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2551:                           '</label></span>&nbsp; ';
 2552:         }
 2553:     }
 2554:     if ($context eq 'requestcourses') {
 2555:         $datatable .= '</tr><tr>';
 2556:         foreach my $item (@usertools) {
 2557:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2558:         }
 2559:         $datatable .= '</tr></table>';
 2560:     }
 2561:     $datatable .= '</td>';
 2562:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2563:         $datatable .= '<td class="LC_right_item">'.
 2564:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2565:                       '<input type="text" name="defaultquota" value="'.
 2566:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2567:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2568:                       '<input type="text" name="authorquota" value="'.
 2569:                       $authorquota.'" size="5" /></span></td>';
 2570:     }
 2571:     $datatable .= '</tr>';
 2572:     $typecount ++;
 2573:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2574:     $datatable .= '<tr'.$css_class.'>'.
 2575:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2576:     if ($context eq 'requestcourses') {
 2577:         $datatable .= &mt('(overrides affiliation, if set)').
 2578:                       '</td>'.
 2579:                       '<td class="LC_left_item">'.
 2580:                       '<table><tr>';
 2581:     } else {
 2582:         $datatable .= &mt('(overrides affiliation, if checked)').
 2583:                       '</td>'.
 2584:                       '<td class="LC_left_item" colspan="2">'.
 2585:                       '<br />';
 2586:     }
 2587:     my %advcell;
 2588:     foreach my $item (@usertools) {
 2589:         if ($context eq 'requestcourses') {
 2590:             my ($curroption,$currlimit);
 2591:             if (ref($settings) eq 'HASH') {
 2592:                 if (ref($settings->{$item}) eq 'HASH') {
 2593:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2594:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2595:                         $currlimit = $1;
 2596:                     }
 2597:                 }
 2598:             }
 2599:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2600:             my $checked = '';
 2601:             if ($curroption eq '') {
 2602:                 $checked = ' checked="checked"';
 2603:             }
 2604:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2605:                                '<input type="radio" name="crsreq_'.$item.
 2606:                                '__LC_adv" value=""'.$checked.' />'.
 2607:                                &mt('No override set').'</label></span>&nbsp; ';
 2608:             foreach my $option (@options) {
 2609:                 my $val = $option;
 2610:                 if ($option eq 'norequest') {
 2611:                     $val = 0;
 2612:                 }
 2613:                 if ($option eq 'validate') {
 2614:                     my $canvalidate = 0;
 2615:                     if (ref($validations{$item}) eq 'HASH') {
 2616:                         if ($validations{$item}{'_LC_adv'}) {
 2617:                             $canvalidate = 1;
 2618:                         }
 2619:                     }
 2620:                     next if (!$canvalidate);
 2621:                 }
 2622:                 my $checked = '';
 2623:                 if ($val eq $curroption) {
 2624:                     $checked = ' checked="checked"';
 2625:                 } elsif ($option eq 'autolimit') {
 2626:                     if ($curroption =~ /^autolimit/) {
 2627:                         $checked = ' checked="checked"';
 2628:                     }
 2629:                 }
 2630:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2631:                                   '<input type="radio" name="crsreq_'.$item.
 2632:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2633:                                   $titles{$option}.'</label>';
 2634:                 if ($option eq 'autolimit') {
 2635:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2636:                                        $item.'_limit__LC_adv" size="1" '.
 2637:                                        'value="'.$currlimit.'" />';
 2638:                 }
 2639:                 $advcell{$item} .= '</span> ';
 2640:                 if ($option eq 'autolimit') {
 2641:                     $advcell{$item} .= $titles{'unlimited'};
 2642:                 }
 2643:             }
 2644:         } elsif ($context eq 'requestauthor') {
 2645:             my $curroption;
 2646:             if (ref($settings) eq 'HASH') {
 2647:                 $curroption = $settings->{'_LC_adv'};
 2648:             }
 2649:             my $checked = '';
 2650:             if ($curroption eq '') {
 2651:                 $checked = ' checked="checked"';
 2652:             }
 2653:             $datatable .= '<span class="LC_nobreak"><label>'.
 2654:                           '<input type="radio" name="authorreq__LC_adv"'.
 2655:                           ' value=""'.$checked.' />'.
 2656:                           &mt('No override set').'</label></span>&nbsp; ';
 2657:             foreach my $option (@options) {
 2658:                 my $val = $option;
 2659:                 if ($option eq 'norequest') {
 2660:                     $val = 0;
 2661:                 }
 2662:                 my $checked = '';
 2663:                 if ($val eq $curroption) {
 2664:                     $checked = ' checked="checked"';
 2665:                 }
 2666:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2667:                               '<input type="radio" name="authorreq__LC_adv"'.
 2668:                               ' value="'.$val.'"'.$checked.' />'.
 2669:                               $titles{$option}.'</label></span>&nbsp; ';
 2670:             }
 2671:         } else {
 2672:             my $checked = 'checked="checked" ';
 2673:             if (ref($settings) eq 'HASH') {
 2674:                 if (ref($settings->{$item}) eq 'HASH') {
 2675:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2676:                         $checked = '';
 2677:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2678:                         $checked = 'checked="checked" ';
 2679:                     }
 2680:                 }
 2681:             }
 2682:             $datatable .= '<span class="LC_nobreak"><label>'.
 2683:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2684:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2685:                           '</label></span>&nbsp; ';
 2686:         }
 2687:     }
 2688:     if ($context eq 'requestcourses') {
 2689:         $datatable .= '</tr><tr>';
 2690:         foreach my $item (@usertools) {
 2691:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2692:         }
 2693:         $datatable .= '</tr></table>';
 2694:     }
 2695:     $datatable .= '</td></tr>';
 2696:     $$rowtotal += $typecount;
 2697:     return $datatable;
 2698: }
 2699: 
 2700: sub print_requestmail {
 2701:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2702:     my ($now,$datatable,%currapp);
 2703:     $now = time;
 2704:     if (ref($settings) eq 'HASH') {
 2705:         if (ref($settings->{'notify'}) eq 'HASH') {
 2706:             if ($settings->{'notify'}{'approval'} ne '') {
 2707:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2708:             }
 2709:         }
 2710:     }
 2711:     my $numinrow = 2;
 2712:     my $css_class;
 2713:     if ($$rowtotal%2) {
 2714:         $css_class = 'LC_odd_row';
 2715:     }
 2716:     if ($customcss) {
 2717:         $css_class .= " $customcss";
 2718:     }
 2719:     $css_class =~ s/^\s+//;
 2720:     if ($css_class) {
 2721:         $css_class = ' class="'.$css_class.'"';
 2722:     }
 2723:     if ($rowstyle) {
 2724:         $css_class .= ' style="'.$rowstyle.'"';
 2725:     }
 2726:     my $text;
 2727:     if ($action eq 'requestcourses') {
 2728:         $text = &mt('Receive notification of course requests requiring approval');
 2729:     } elsif ($action eq 'requestauthor') {
 2730:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2731:     } else {
 2732:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2733:     }
 2734:     $datatable = '<tr'.$css_class.'>'.
 2735:                  ' <td>'.$text.'</td>'.
 2736:                  ' <td class="LC_left_item">';
 2737:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2738:                                                  $action.'notifyapproval',%currapp);
 2739:     if ($numdc > 0) {
 2740:         $datatable .= $table;
 2741:     } else {
 2742:         $datatable .= &mt('There are no active Domain Coordinators');
 2743:     }
 2744:     $datatable .='</td></tr>';
 2745:     return $datatable;
 2746: }
 2747: 
 2748: sub print_studentcode {
 2749:     my ($settings,$rowtotal) = @_;
 2750:     my $rownum = 0; 
 2751:     my ($output,%current);
 2752:     my @crstypes = ('official','unofficial','community','textbook','lti');
 2753:     if (ref($settings) eq 'HASH') {
 2754:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2755:             foreach my $type (@crstypes) {
 2756:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2757:             }
 2758:         }
 2759:     }
 2760:     $output .= '<tr>'.
 2761:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2762:                '<td class="LC_left_item">';
 2763:     foreach my $type (@crstypes) {
 2764:         my $check = ' ';
 2765:         if ($current{$type}) {
 2766:             $check = ' checked="checked" ';
 2767:         }
 2768:         $output .= '<span class="LC_nobreak"><label>'.
 2769:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2770:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2771:     }
 2772:     $output .= '</td></tr>';
 2773:     $$rowtotal ++;
 2774:     return $output;
 2775: }
 2776: 
 2777: sub print_textbookcourses {
 2778:     my ($dom,$type,$settings,$rowtotal) = @_;
 2779:     my $rownum = 0;
 2780:     my $css_class;
 2781:     my $itemcount = 1;
 2782:     my $maxnum = 0;
 2783:     my $bookshash;
 2784:     if (ref($settings) eq 'HASH') {
 2785:         $bookshash = $settings->{$type};
 2786:     }
 2787:     my %ordered;
 2788:     if (ref($bookshash) eq 'HASH') {
 2789:         foreach my $item (keys(%{$bookshash})) {
 2790:             if (ref($bookshash->{$item}) eq 'HASH') {
 2791:                 my $num = $bookshash->{$item}{'order'};
 2792:                 $ordered{$num} = $item;
 2793:             }
 2794:         }
 2795:     }
 2796:     my $confname = $dom.'-domainconfig';
 2797:     my $switchserver = &check_switchserver($dom,$confname);
 2798:     my $maxnum = scalar(keys(%ordered));
 2799:     my $datatable;
 2800:     if (keys(%ordered)) {
 2801:         my @items = sort { $a <=> $b } keys(%ordered);
 2802:         for (my $i=0; $i<@items; $i++) {
 2803:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2804:             my $key = $ordered{$items[$i]};
 2805:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2806:             my $coursetitle = $coursehash{'description'};
 2807:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2808:             if (ref($bookshash->{$key}) eq 'HASH') {
 2809:                 $subject = $bookshash->{$key}->{'subject'};
 2810:                 $title = $bookshash->{$key}->{'title'};
 2811:                 if ($type eq 'textbooks') {
 2812:                     $publisher = $bookshash->{$key}->{'publisher'};
 2813:                     $author = $bookshash->{$key}->{'author'};
 2814:                     $image = $bookshash->{$key}->{'image'};
 2815:                     if ($image ne '') {
 2816:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2817:                         my $imagethumb = "$path/tn-".$imagefile;
 2818:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2819:                     }
 2820:                 }
 2821:             }
 2822:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2823:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2824:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2825:             for (my $k=0; $k<=$maxnum; $k++) {
 2826:                 my $vpos = $k+1;
 2827:                 my $selstr;
 2828:                 if ($k == $i) {
 2829:                     $selstr = ' selected="selected" ';
 2830:                 }
 2831:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2832:             }
 2833:             $datatable .= '</select>'.('&nbsp;'x2).
 2834:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2835:                 &mt('Delete?').'</label></span></td>'.
 2836:                 '<td colspan="2">'.
 2837:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2838:                 ('&nbsp;'x2).
 2839:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2840:             if ($type eq 'textbooks') {
 2841:                 $datatable .= ('&nbsp;'x2).
 2842:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2843:                               ('&nbsp;'x2).
 2844:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2845:                               ('&nbsp;'x2).
 2846:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2847:                 if ($image) {
 2848:                     $datatable .= $imgsrc.
 2849:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2850:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2851:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2852:                 }
 2853:                 if ($switchserver) {
 2854:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2855:                 } else {
 2856:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2857:                 }
 2858:             }
 2859:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2860:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2861:                           $coursetitle.'</span></td></tr>'."\n";
 2862:             $itemcount ++;
 2863:         }
 2864:     }
 2865:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2866:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2867:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2868:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2869:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2870:     for (my $k=0; $k<$maxnum+1; $k++) {
 2871:         my $vpos = $k+1;
 2872:         my $selstr;
 2873:         if ($k == $maxnum) {
 2874:             $selstr = ' selected="selected" ';
 2875:         }
 2876:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2877:     }
 2878:     $datatable .= '</select>&nbsp;'."\n".
 2879:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2880:                   '<td colspan="2">'.
 2881:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2882:                   ('&nbsp;'x2).
 2883:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2884:                   ('&nbsp;'x2);
 2885:     if ($type eq 'textbooks') {
 2886:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2887:                       ('&nbsp;'x2).
 2888:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2889:                       ('&nbsp;'x2).
 2890:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2891:         if ($switchserver) {
 2892:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2893:         } else {
 2894:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2895:         }
 2896:         $datatable .= '</span>'."\n";
 2897:     }
 2898:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2899:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2900:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2901:                   &Apache::loncommon::selectcourse_link
 2902:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2903:                   '</span></td>'."\n".
 2904:                   '</tr>'."\n";
 2905:     $itemcount ++;
 2906:     return $datatable;
 2907: }
 2908: 
 2909: sub textbookcourses_javascript {
 2910:     my ($settings) = @_;
 2911:     return unless(ref($settings) eq 'HASH');
 2912:     my (%ordered,%total,%jstext);
 2913:     foreach my $type ('textbooks','templates') {
 2914:         $total{$type} = 0;
 2915:         if (ref($settings->{$type}) eq 'HASH') {
 2916:             foreach my $item (keys(%{$settings->{$type}})) {
 2917:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2918:                     my $num = $settings->{$type}->{$item}{'order'};
 2919:                     $ordered{$type}{$num} = $item;
 2920:                 }
 2921:             }
 2922:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2923:         }
 2924:         my @jsarray = ();
 2925:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2926:             push(@jsarray,$ordered{$type}{$item});
 2927:         }
 2928:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2929:     }
 2930:     return <<"ENDSCRIPT";
 2931: <script type="text/javascript">
 2932: // <![CDATA[
 2933: function reorderBooks(form,item,caller) {
 2934:     var changedVal;
 2935: $jstext{'textbooks'};
 2936: $jstext{'templates'};
 2937:     var newpos;
 2938:     var maxh;
 2939:     if (caller == 'textbooks') {  
 2940:         newpos = 'textbooks_addbook_pos';
 2941:         maxh = 1 + $total{'textbooks'};
 2942:     } else {
 2943:         newpos = 'templates_addbook_pos';
 2944:         maxh = 1 + $total{'templates'};
 2945:     }
 2946:     var current = new Array;
 2947:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2948:     if (item == newpos) {
 2949:         changedVal = newitemVal;
 2950:     } else {
 2951:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2952:         current[newitemVal] = newpos;
 2953:     }
 2954:     if (caller == 'textbooks') {
 2955:         for (var i=0; i<textbooks.length; i++) {
 2956:             var elementName = 'textbooks_'+textbooks[i];
 2957:             if (elementName != item) {
 2958:                 if (form.elements[elementName]) {
 2959:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2960:                     current[currVal] = elementName;
 2961:                 }
 2962:             }
 2963:         }
 2964:     }
 2965:     if (caller == 'templates') {
 2966:         for (var i=0; i<templates.length; i++) {
 2967:             var elementName = 'templates_'+templates[i];
 2968:             if (elementName != item) {
 2969:                 if (form.elements[elementName]) {
 2970:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2971:                     current[currVal] = elementName;
 2972:                 }
 2973:             }
 2974:         }
 2975:     }
 2976:     var oldVal;
 2977:     for (var j=0; j<maxh; j++) {
 2978:         if (current[j] == undefined) {
 2979:             oldVal = j;
 2980:         }
 2981:     }
 2982:     if (oldVal < changedVal) {
 2983:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2984:            var elementName = current[k];
 2985:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2986:         }
 2987:     } else {
 2988:         for (var k=changedVal; k<oldVal; k++) {
 2989:             var elementName = current[k];
 2990:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2991:         }
 2992:     }
 2993:     return;
 2994: }
 2995: 
 2996: // ]]>
 2997: </script>
 2998: 
 2999: ENDSCRIPT
 3000: }
 3001: 
 3002: sub ltitools_javascript {
 3003:     my ($settings) = @_;
 3004:     my $togglejs = &ltitools_toggle_js();
 3005:     unless (ref($settings) eq 'HASH') {
 3006:         return $togglejs;
 3007:     }
 3008:     my (%ordered,$total,%jstext);
 3009:     $total = 0;
 3010:     foreach my $item (keys(%{$settings})) {
 3011:         if (ref($settings->{$item}) eq 'HASH') {
 3012:             my $num = $settings->{$item}{'order'};
 3013:             $ordered{$num} = $item;
 3014:         }
 3015:     }
 3016:     $total = scalar(keys(%{$settings}));
 3017:     my @jsarray = ();
 3018:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3019:         push(@jsarray,$ordered{$item});
 3020:     }
 3021:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 3022:     return <<"ENDSCRIPT";
 3023: <script type="text/javascript">
 3024: // <![CDATA[
 3025: function reorderLTITools(form,item) {
 3026:     var changedVal;
 3027: $jstext
 3028:     var newpos = 'ltitools_add_pos';
 3029:     var maxh = 1 + $total;
 3030:     var current = new Array;
 3031:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3032:     if (item == newpos) {
 3033:         changedVal = newitemVal;
 3034:     } else {
 3035:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3036:         current[newitemVal] = newpos;
 3037:     }
 3038:     for (var i=0; i<ltitools.length; i++) {
 3039:         var elementName = 'ltitools_'+ltitools[i];
 3040:         if (elementName != item) {
 3041:             if (form.elements[elementName]) {
 3042:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3043:                 current[currVal] = elementName;
 3044:             }
 3045:         }
 3046:     }
 3047:     var oldVal;
 3048:     for (var j=0; j<maxh; j++) {
 3049:         if (current[j] == undefined) {
 3050:             oldVal = j;
 3051:         }
 3052:     }
 3053:     if (oldVal < changedVal) {
 3054:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3055:            var elementName = current[k];
 3056:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3057:         }
 3058:     } else {
 3059:         for (var k=changedVal; k<oldVal; k++) {
 3060:             var elementName = current[k];
 3061:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3062:         }
 3063:     }
 3064:     return;
 3065: }
 3066: 
 3067: // ]]>
 3068: </script>
 3069: 
 3070: $togglejs
 3071: 
 3072: ENDSCRIPT
 3073: }
 3074: 
 3075: sub ltitools_toggle_js {
 3076:     return <<"ENDSCRIPT";
 3077: <script type="text/javascript">
 3078: // <![CDATA[
 3079: 
 3080: function toggleLTITools(form,setting,item) {
 3081:     var radioname = '';
 3082:     var divid = '';
 3083:     if ((setting == 'passback') || (setting == 'roster')) {
 3084:         radioname = 'ltitools_'+setting+'_'+item;
 3085:         divid = 'ltitools_'+setting+'time_'+item;
 3086:         var num = form.elements[radioname].length;
 3087:         if (num) {
 3088:             var setvis = '';
 3089:             for (var i=0; i<num; i++) {
 3090:                 if (form.elements[radioname][i].checked) {
 3091:                     if (form.elements[radioname][i].value == '1') {
 3092:                         if (document.getElementById(divid)) {
 3093:                             document.getElementById(divid).style.display = 'inline-block';
 3094:                         }
 3095:                         setvis = 1;
 3096:                     }
 3097:                     break;
 3098:                 }
 3099:             }
 3100:         }
 3101:         if (!setvis) {
 3102:             if (document.getElementById(divid)) {
 3103:                 document.getElementById(divid).style.display = 'none';
 3104:             }
 3105:         }
 3106:     }
 3107:     if (setting == 'user') {
 3108:         divid = 'ltitools_'+setting+'_div_'+item;
 3109:         var checkid = 'ltitools_'+setting+'_field_'+item;
 3110:         if (document.getElementById(divid)) {
 3111:             if (document.getElementById(checkid)) {
 3112:                 if (document.getElementById(checkid).checked) {
 3113:                     document.getElementById(divid).style.display = 'inline-block';
 3114:                 } else {
 3115:                     document.getElementById(divid).style.display = 'none';
 3116:                 }
 3117:             }
 3118:         }
 3119:     }
 3120:     return;
 3121: }
 3122: // ]]>
 3123: </script>
 3124: 
 3125: ENDSCRIPT
 3126: }
 3127: 
 3128: sub wafproxy_javascript {
 3129:     my ($dom) = @_;
 3130:     return <<"ENDSCRIPT";
 3131: <script type="text/javascript">
 3132: // <![CDATA[
 3133: function updateWAF() {
 3134:     if (document.getElementById('wafproxy_remoteip')) {
 3135:         var wafremote = 0;
 3136:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 3137:             wafremote = 1;
 3138:         }
 3139:         var fields = new Array('header','trust');
 3140:         for (var i=0; i<fields.length; i++) {
 3141:             if (document.getElementById('wafproxy_'+fields[i])) {
 3142:                 if (wafremote == 1) {
 3143:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 3144:                 }
 3145:                 else {
 3146:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 3147:                 }
 3148:             }
 3149:         }
 3150:         if (document.getElementById('wafproxyranges_$dom')) {
 3151:             if (wafremote == 1) {
 3152:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3153:             } else {
 3154:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3155:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 3156:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 3157:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3158:                         }
 3159:                     }
 3160:                 }
 3161:             }
 3162:         }
 3163:     }
 3164:     return;
 3165: }
 3166: 
 3167: function checkWAF() {
 3168:     if (document.getElementById('wafproxy_remoteip')) {
 3169:         var wafvpn = 0;
 3170:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3171:             if (document.display.wafproxy_vpnaccess[i].checked) {
 3172:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 3173:                     wafvpn = 1;
 3174:                 }
 3175:                 break;
 3176:             }
 3177:         }
 3178:         var vpn = new Array('vpnint','vpnext');
 3179:         for (var i=0; i<vpn.length; i++) {
 3180:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 3181:                 if (wafvpn == 1) {
 3182:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 3183:                 }
 3184:                 else {
 3185:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 3186:                 }
 3187:             }
 3188:         }
 3189:         if (document.getElementById('wafproxyranges_$dom')) {
 3190:             if (wafvpn == 1) {
 3191:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3192:             }
 3193:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 3194:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3195:             }
 3196:         }
 3197:     }
 3198:     return;
 3199: }
 3200: 
 3201: function toggleWAF() {
 3202:     if (document.getElementById('wafproxy_table')) {
 3203:         var wafproxy = 0;
 3204:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 3205:              if (document.display.wafproxy_${dom}[i].checked) {
 3206:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 3207:                      wafproxy = 1;
 3208:                      break;
 3209:                 }
 3210:             }
 3211:         }
 3212:         if (wafproxy == 1) {
 3213:             document.getElementById('wafproxy_table').style.display='inline';
 3214:         }
 3215:         else {
 3216:            document.getElementById('wafproxy_table').style.display='none';
 3217:         }
 3218:         if (document.getElementById('wafproxyrow_${dom}')) {
 3219:             if (wafproxy == 1) {
 3220:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 3221:             }
 3222:             else {
 3223:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 3224:             }
 3225:         }
 3226:         if (document.getElementById('nowafproxyrow_$dom')) {
 3227:             if (wafproxy == 1) {
 3228:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 3229:             }
 3230:             else {
 3231:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 3232:             }
 3233:         }
 3234:     }
 3235:     return;
 3236: }
 3237: // ]]>
 3238: </script>
 3239: 
 3240: ENDSCRIPT
 3241: }
 3242: 
 3243: sub lti_javascript {
 3244:     my ($dom,$settings) = @_;
 3245:     my $togglejs = &lti_toggle_js($dom);
 3246:     my $linkprot_js = &Apache::courseprefs::linkprot_javascript();
 3247:     unless (ref($settings) eq 'HASH') {
 3248:         return $togglejs.'
 3249: <script type="text/javascript">
 3250: // <![CDATA[
 3251: 
 3252: '.$linkprot_js.'
 3253: 
 3254: // ]]>
 3255: </script>
 3256: ';
 3257:     }
 3258:     my (%ordered,$total,%jstext);
 3259:     $total = scalar(keys(%{$settings}));
 3260:     foreach my $item (keys(%{$settings})) {
 3261:         if (ref($settings->{$item}) eq 'HASH') {
 3262:             my $num = $settings->{$item}{'order'};
 3263:             if ($num eq '') {
 3264:                 $num = $total - 1;
 3265:             }
 3266:             $ordered{$num} = $item;
 3267:         }
 3268:     }
 3269:     my @jsarray = ();
 3270:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3271:         push(@jsarray,$ordered{$item});
 3272:     }
 3273:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 3274:     return <<"ENDSCRIPT";
 3275: <script type="text/javascript">
 3276: // <![CDATA[
 3277: function reorderLTI(form,item) {
 3278:     var changedVal;
 3279: $jstext
 3280:     var newpos = 'lti_pos_add';
 3281:     var maxh = 1 + $total;
 3282:     var current = new Array;
 3283:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3284:     if (item == newpos) {
 3285:         changedVal = newitemVal;
 3286:     } else {
 3287:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3288:         current[newitemVal] = newpos;
 3289:     }
 3290:     for (var i=0; i<lti.length; i++) {
 3291:         var elementName = 'lti_pos_'+lti[i];
 3292:         if (elementName != item) {
 3293:             if (form.elements[elementName]) {
 3294:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3295:                 current[currVal] = elementName;
 3296:             }
 3297:         }
 3298:     }
 3299:     var oldVal;
 3300:     for (var j=0; j<maxh; j++) {
 3301:         if (current[j] == undefined) {
 3302:             oldVal = j;
 3303:         }
 3304:     }
 3305:     if (oldVal < changedVal) {
 3306:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3307:            var elementName = current[k];
 3308:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3309:         }
 3310:     } else {
 3311:         for (var k=changedVal; k<oldVal; k++) {
 3312:             var elementName = current[k];
 3313:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3314:         }
 3315:     }
 3316:     return;
 3317: }
 3318: 
 3319: $linkprot_js
 3320: 
 3321: // ]]>
 3322: </script>
 3323: 
 3324: $togglejs
 3325: 
 3326: ENDSCRIPT
 3327: }
 3328: 
 3329: sub lti_toggle_js {
 3330:     my ($dom) = @_;
 3331:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3332:                             localauth => 'Local auth argument',
 3333:                             krb       => 'Kerberos domain',
 3334:                          );
 3335:     my $crsincalert = &mt('"User\'s identity sent" needs to be set to "Yes" first,[_1] before setting "Course\'s identity sent" to "Yes"',"\n");
 3336:     &js_escape(\$crsincalert);
 3337:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 3338:     my $primary = &Apache::lonnet::domain($dom,'primary');
 3339:     my $course_servers = "'".join("','",keys(%servers))."'";
 3340:     return <<"ENDSCRIPT";
 3341: <script type="text/javascript">
 3342: // <![CDATA[
 3343: 
 3344: function toggleLTI(form,setting,item) {
 3345:     if ((setting == 'requser') || (setting == 'crsinc')) {
 3346:         var usrfieldsets = document.getElementsByClassName('ltioption_usr_'+item);
 3347:         var setvis = '';
 3348:         var radioname = 'lti_requser_'+item;
 3349:         var num = form.elements[radioname].length;
 3350:         if (num) {
 3351:             for (var i=0; i<num; i++) {
 3352:                 if (form.elements[radioname][i].checked) {
 3353:                     if (form.elements[radioname][i].value == '1') {
 3354:                         setvis = 1;
 3355:                         break;
 3356:                     }
 3357:                 }
 3358:             }
 3359:         }
 3360:         if (usrfieldsets.length) {
 3361:             for (var j=0; j<usrfieldsets.length; j++) {
 3362:                 if (setvis) {
 3363:                     usrfieldsets[j].style.display = 'block';
 3364:                 } else {
 3365:                     usrfieldsets[j].style.display = 'none';
 3366:                 }
 3367:             }
 3368:         }
 3369:         var crsfieldsets = document.getElementsByClassName('ltioption_crs_'+item);
 3370:         if (crsfieldsets.length) {
 3371:             radioname = 'lti_crsinc_'+item;
 3372:             var num = form.elements[radioname].length;
 3373:             if (num) {
 3374:                 var crsvis = '';
 3375:                 for (var i=0; i<num; i++) {
 3376:                     if (form.elements[radioname][i].checked) {
 3377:                         if (form.elements[radioname][i].value == '1') {
 3378:                             if (setvis == '') {
 3379:                                 if (setting == 'crsinc'){
 3380:                                     alert("$crsincalert");
 3381:                                     form.elements[radioname][0].checked = true;
 3382:                                 }
 3383:                             } else {
 3384:                                 crsvis = 1;
 3385:                             }
 3386:                             break;
 3387:                         }
 3388:                     }
 3389:                 }
 3390:                 setvis = crsvis;
 3391:             }
 3392:             for (var j=0; j<crsfieldsets.length; j++) {
 3393:                 if (setvis) {
 3394:                     crsfieldsets[j].style.display = 'block';
 3395:                 } else {
 3396:                     crsfieldsets[j].style.display = 'none';
 3397:                 }
 3398:             }
 3399:         }
 3400:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3401:         var radioname = '';
 3402:         var divid = '';
 3403:         if (setting == 'user') {
 3404:             radioname = 'lti_mapuser_'+item;
 3405:             divid = 'lti_userfield_'+item;
 3406:         } else if (setting == 'crs') {
 3407:             radioname = 'lti_mapcrs_'+item;
 3408:             divid = 'lti_crsfield_'+item;
 3409:         } else if (setting == 'callback') {
 3410:             radioname = 'lti_callback_'+item;
 3411:             divid = 'lti_callbackfield_'+item;
 3412:         } else {
 3413:             radioname = 'lti_passback_'+item;
 3414:             divid =  'lti_passback_'+item;
 3415:         }
 3416:         var num = form.elements[radioname].length;
 3417:         if (num) {
 3418:             var setvis = '';
 3419:             for (var i=0; i<num; i++) {
 3420:                if (form.elements[radioname][i].checked) {
 3421:                    if ((setting == 'passback') || (setting == 'callback')) {
 3422:                        if (form.elements[radioname][i].value == '1') {
 3423:                            if (document.getElementById(divid)) {
 3424:                                document.getElementById(divid).style.display = 'inline-block';
 3425:                            }
 3426:                            setvis = 1;
 3427:                            break;
 3428:                        }
 3429:                    } else {
 3430:                        if (form.elements[radioname][i].value == 'other') {
 3431:                            if (document.getElementById(divid)) {
 3432:                                document.getElementById(divid).style.display = 'inline-block';
 3433:                            }
 3434:                            setvis = 1;
 3435:                            break;
 3436:                        }
 3437:                    }
 3438:                }
 3439:             }
 3440:             if (!setvis) {
 3441:                 if (document.getElementById(divid)) {
 3442:                     document.getElementById(divid).style.display = 'none';
 3443:                 }
 3444:             }
 3445:         }
 3446:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3447:         var numsec = form.elements['lti_crssec_'+item].length;
 3448:         if (numsec) {
 3449:             var setvis = '';
 3450:             for (var i=0; i<numsec; i++) {
 3451:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3452:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3453:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3454:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3455:                             setvis = 1;
 3456:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3457:                             if (numsrcsec) {
 3458:                                 var setsrcvis = '';
 3459:                                 for (var j=0; j<numsrcsec; j++) {
 3460:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3461:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3462:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3463:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3464:                                                 setsrcvis = 1;
 3465:                                             }
 3466:                                         }
 3467:                                     }
 3468:                                 }
 3469:                                 if (!setsrcvis) {
 3470:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3471:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3472:                                     }
 3473:                                 }
 3474:                             }
 3475:                         }
 3476:                     }
 3477:                 }
 3478:             }
 3479:             if (!setvis) {
 3480:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3481:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3482:                 }
 3483:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3484:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3485:                 }
 3486:             }
 3487:         }
 3488:     } else if (setting == 'lcauth') {
 3489:         var numauth = form.elements['lti_lcauth_'+item].length;
 3490:         if (numauth) {
 3491:             for (var i=0; i<numauth; i++) {
 3492:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3493:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3494:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3495:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3496:                         } else {
 3497:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3498:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3499:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3500:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3501:                                 } else {
 3502:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3503:                                 }
 3504:                             }
 3505:                         }
 3506:                     }
 3507:                 }
 3508:             }
 3509:         }
 3510:     } else if (setting == 'lcmenu') {
 3511:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3512:         var divid = 'lti_menufield_'+item;
 3513:         var setvis = '';
 3514:         for (var i=0; i<menus.length; i++) {
 3515:             var radioname = menus[i];
 3516:             var num = form.elements[radioname].length;
 3517:             if (num) {
 3518:                 for (var j=0; j<num; j++) {
 3519:                     if (form.elements[radioname][j].checked) {
 3520:                         if (form.elements[radioname][j].value == '1') {
 3521:                             if (document.getElementById(divid)) {
 3522:                                 document.getElementById(divid).style.display = 'inline-block';
 3523:                             }
 3524:                             setvis = 1;
 3525:                             break;
 3526:                         }
 3527:                     }
 3528:                 }
 3529:             }
 3530:             if (setvis == 1) {
 3531:                 break;
 3532:             }
 3533:         }
 3534:         if (!setvis) {
 3535:             if (document.getElementById(divid)) {
 3536:                 document.getElementById(divid).style.display = 'none';
 3537:             }
 3538:         }
 3539:     }
 3540:     return;
 3541: }
 3542: 
 3543: // ]]>
 3544: </script>
 3545: 
 3546: ENDSCRIPT
 3547: }
 3548: 
 3549: sub autoupdate_javascript {
 3550:     return <<"ENDSCRIPT";
 3551: <script type="text/javascript">
 3552: // <![CDATA[
 3553: function toggleLastActiveDays(form) {
 3554:     var radioname = 'lastactive';
 3555:     var divid = 'lastactive_div';
 3556:     var num = form.elements[radioname].length;
 3557:     if (num) {
 3558:         var setvis = '';
 3559:         for (var i=0; i<num; i++) {
 3560:             if (form.elements[radioname][i].checked) {
 3561:                 if (form.elements[radioname][i].value == '1') {
 3562:                     if (document.getElementById(divid)) {
 3563:                         document.getElementById(divid).style.display = 'inline-block';
 3564:                     }
 3565:                     setvis = 1;
 3566:                 }
 3567:                 break;
 3568:             }
 3569:         }
 3570:         if (!setvis) {
 3571:             if (document.getElementById(divid)) {
 3572:                 document.getElementById(divid).style.display = 'none';
 3573:             }
 3574:         }
 3575:     }
 3576:     return;
 3577: }
 3578: // ]]>
 3579: </script>
 3580: 
 3581: ENDSCRIPT
 3582: }
 3583: 
 3584: sub autoenroll_javascript {
 3585:     return <<"ENDSCRIPT";
 3586: <script type="text/javascript">
 3587: // <![CDATA[
 3588: function toggleFailsafe(form) {
 3589:     var radioname = 'autoenroll_failsafe';
 3590:     var divid = 'autoenroll_failsafe_div';
 3591:     var num = form.elements[radioname].length;
 3592:     if (num) {
 3593:         var setvis = '';
 3594:         for (var i=0; i<num; i++) {
 3595:             if (form.elements[radioname][i].checked) {
 3596:                 if ((form.elements[radioname][i].value == 'zero') || (form.elements[radioname][i].value == 'any')) {
 3597:                     if (document.getElementById(divid)) {
 3598:                         document.getElementById(divid).style.display = 'inline-block';
 3599:                     }
 3600:                     setvis = 1;
 3601:                 }
 3602:                 break;
 3603:             }
 3604:         }
 3605:         if (!setvis) {
 3606:             if (document.getElementById(divid)) {
 3607:                 document.getElementById(divid).style.display = 'none';
 3608:             }
 3609:         }
 3610:     }
 3611:     return;
 3612: }
 3613: // ]]>
 3614: </script>
 3615: 
 3616: ENDSCRIPT
 3617: }
 3618: 
 3619: sub saml_javascript {
 3620:     return <<"ENDSCRIPT";
 3621: <script type="text/javascript">
 3622: // <![CDATA[
 3623: function toggleSamlOptions(form,hostid) {
 3624:     var radioname = 'saml_'+hostid;
 3625:     var tablecellon = 'samloptionson_'+hostid;
 3626:     var tablecelloff = 'samloptionsoff_'+hostid;
 3627:     var num = form.elements[radioname].length;
 3628:     if (num) {
 3629:         var setvis = '';
 3630:         for (var i=0; i<num; i++) {
 3631:             if (form.elements[radioname][i].checked) {
 3632:                 if (form.elements[radioname][i].value == '1') {
 3633:                     if (document.getElementById(tablecellon)) {
 3634:                         document.getElementById(tablecellon).style.display='';
 3635:                     }
 3636:                     if (document.getElementById(tablecelloff)) {
 3637:                         document.getElementById(tablecelloff).style.display='none';
 3638:                     }
 3639:                     setvis = 1;
 3640:                 }
 3641:                 break;
 3642:             }
 3643:         }
 3644:         if (!setvis) {
 3645:             if (document.getElementById(tablecellon)) {
 3646:                 document.getElementById(tablecellon).style.display='none';
 3647:             }
 3648:             if (document.getElementById(tablecelloff)) {
 3649:                 document.getElementById(tablecelloff).style.display='';
 3650:             }
 3651:         }
 3652:     }
 3653:     return;
 3654: }
 3655: // ]]>
 3656: </script>
 3657: 
 3658: ENDSCRIPT
 3659: }
 3660: 
 3661: sub ipaccess_javascript {
 3662:     my ($settings) = @_;
 3663:     my (%ordered,$total,%jstext);
 3664:     $total = 0;
 3665:     if (ref($settings) eq 'HASH') {
 3666:         foreach my $item (keys(%{$settings})) {
 3667:             if (ref($settings->{$item}) eq 'HASH') {
 3668:                 my $num = $settings->{$item}{'order'};
 3669:                 $ordered{$num} = $item;
 3670:             }
 3671:         }
 3672:         $total = scalar(keys(%{$settings}));
 3673:     }
 3674:     my @jsarray = ();
 3675:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3676:         push(@jsarray,$ordered{$item});
 3677:     }
 3678:     my $jstext = '    var ipaccess = Array('."'".join("','",@jsarray)."'".');'."\n";
 3679:     return <<"ENDSCRIPT";
 3680: <script type="text/javascript">
 3681: // <![CDATA[
 3682: function reorderIPaccess(form,item) {
 3683:     var changedVal;
 3684: $jstext
 3685:     var newpos = 'ipaccess_pos_add';
 3686:     var maxh = 1 + $total;
 3687:     var current = new Array;
 3688:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3689:     if (item == newpos) {
 3690:         changedVal = newitemVal;
 3691:     } else {
 3692:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3693:         current[newitemVal] = newpos;
 3694:     }
 3695:     for (var i=0; i<ipaccess.length; i++) {
 3696:         var elementName = 'ipaccess_pos_'+ipaccess[i];
 3697:         if (elementName != item) {
 3698:             if (form.elements[elementName]) {
 3699:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3700:                 current[currVal] = elementName;
 3701:             }
 3702:         }
 3703:     }
 3704:     var oldVal;
 3705:     for (var j=0; j<maxh; j++) {
 3706:         if (current[j] == undefined) {
 3707:             oldVal = j;
 3708:         }
 3709:     }
 3710:     if (oldVal < changedVal) {
 3711:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3712:            var elementName = current[k];
 3713:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3714:         }
 3715:     } else {
 3716:         for (var k=changedVal; k<oldVal; k++) {
 3717:             var elementName = current[k];
 3718:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3719:         }
 3720:     }
 3721:     return;
 3722: }
 3723: // ]]>
 3724: </script>
 3725: 
 3726: ENDSCRIPT
 3727: }
 3728: 
 3729: sub print_autoenroll {
 3730:     my ($dom,$settings,$rowtotal) = @_;
 3731:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3732:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,
 3733:         $failsafe,$autofailsafe,$failsafesty,%failsafechecked);
 3734:     $failsafesty = 'none';
 3735:     %failsafechecked = (
 3736:         off => ' checked="checked"',
 3737:     );
 3738:     if (ref($settings) eq 'HASH') {
 3739:         if (exists($settings->{'run'})) {
 3740:             if ($settings->{'run'} eq '0') {
 3741:                 $runoff = ' checked="checked" ';
 3742:                 $runon = ' ';
 3743:             } else {
 3744:                 $runon = ' checked="checked" ';
 3745:                 $runoff = ' ';
 3746:             }
 3747:         } else {
 3748:             if ($autorun) {
 3749:                 $runon = ' checked="checked" ';
 3750:                 $runoff = ' ';
 3751:             } else {
 3752:                 $runoff = ' checked="checked" ';
 3753:                 $runon = ' ';
 3754:             }
 3755:         }
 3756:         if (exists($settings->{'co-owners'})) {
 3757:             if ($settings->{'co-owners'} eq '0') {
 3758:                 $coownersoff = ' checked="checked" ';
 3759:                 $coownerson = ' ';
 3760:             } else {
 3761:                 $coownerson = ' checked="checked" ';
 3762:                 $coownersoff = ' ';
 3763:             }
 3764:         } else {
 3765:             $coownersoff = ' checked="checked" ';
 3766:             $coownerson = ' ';
 3767:         }
 3768:         if (exists($settings->{'sender_domain'})) {
 3769:             $defdom = $settings->{'sender_domain'};
 3770:         }
 3771:         if (exists($settings->{'failsafe'})) {
 3772:             $failsafe = $settings->{'failsafe'};
 3773:             if ($failsafe eq 'zero') {
 3774:                 $failsafechecked{'zero'} = ' checked="checked"';
 3775:                 $failsafechecked{'off'} = '';
 3776:                 $failsafesty = 'inline-block';
 3777:             } elsif ($failsafe eq 'any') {
 3778:                 $failsafechecked{'any'} = ' checked="checked"';
 3779:                 $failsafechecked{'off'} = '';
 3780:             }
 3781:             $autofailsafe = $settings->{'autofailsafe'};
 3782:         } elsif (exists($settings->{'autofailsafe'})) {
 3783:             $autofailsafe = $settings->{'autofailsafe'};
 3784:             if ($autofailsafe ne '') {
 3785:                 $failsafechecked{'zero'} = ' checked="checked"';
 3786:                 $failsafe = 'zero';
 3787:                 $failsafechecked{'off'} = '';
 3788:             }
 3789:         }
 3790:     } else {
 3791:         if ($autorun) {
 3792:             $runon = ' checked="checked" ';
 3793:             $runoff = ' ';
 3794:         } else {
 3795:             $runoff = ' checked="checked" ';
 3796:             $runon = ' ';
 3797:         }
 3798:     }
 3799:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3800:     my $notif_sender;
 3801:     if (ref($settings) eq 'HASH') {
 3802:         $notif_sender = $settings->{'sender_uname'};
 3803:     }
 3804:     my $datatable='<tr class="LC_odd_row">'.
 3805:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3806:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3807:                   '<input type="radio" name="autoenroll_run"'.
 3808:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3809:                   '<label><input type="radio" name="autoenroll_run"'.
 3810:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3811:                   '</tr><tr>'.
 3812:                   '<td>'.&mt('Notification messages - sender').
 3813:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 3814:                   &mt('username').':&nbsp;'.
 3815:                   '<input type="text" name="sender_uname" value="'.
 3816:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 3817:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 3818:                   '<tr class="LC_odd_row">'.
 3819:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 3820:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3821:                   '<input type="radio" name="autoassign_coowners"'.
 3822:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3823:                   '<label><input type="radio" name="autoassign_coowners"'.
 3824:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3825:                   '</tr><tr>'.
 3826:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 3827:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 3828:                   '<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; '.
 3829:                   '<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 />'.
 3830:                   '<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>'.
 3831:                   '<div class="LC_floatleft" style="display:'.$failsafesty.';" id="autoenroll_failsafe_div">'.
 3832:                   '<span class="LC_nobreak">'.
 3833:                   &mt('Threshold for number of students in section to drop: [_1]',
 3834:                       '<input type="text" name="autoenroll_autofailsafe" value="'.$autofailsafe.'" size="4" />').
 3835:                   '</span></div></td></tr>';
 3836:     $$rowtotal += 4;
 3837:     return $datatable;
 3838: }
 3839: 
 3840: sub print_autoupdate {
 3841:     my ($position,$dom,$settings,$rowtotal) = @_;
 3842:     my ($enable,$datatable);
 3843:     if ($position eq 'top') {
 3844:         my %choices = &Apache::lonlocal::texthash (
 3845:                           run        => 'Auto-update active?',
 3846:                           classlists => 'Update information in classlists?',
 3847:                           unexpired  => 'Skip updates for users without active or future roles?',
 3848:                           lastactive => 'Skip updates for inactive users?',
 3849:         );
 3850:         my $itemcount = 0;
 3851:         my $updateon = ' ';
 3852:         my $updateoff = ' checked="checked" ';
 3853:         if (ref($settings) eq 'HASH') {
 3854:             if ($settings->{'run'} eq '1') {
 3855:                 $updateon = $updateoff;
 3856:                 $updateoff = ' ';
 3857:             }
 3858:         }
 3859:         $enable = '<tr class="LC_odd_row">'.
 3860:                   '<td>'.$choices{'run'}.'</td>'.
 3861:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3862:                   '<input type="radio" name="autoupdate_run"'.
 3863:                   $updateoff.'value="0" />'.&mt('No').'</label>&nbsp;'.
 3864:                   '<label><input type="radio" name="autoupdate_run"'.
 3865:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 3866:                   '</tr>';
 3867:         my @toggles = ('classlists','unexpired');
 3868:         my %defaultchecked = ('classlists' => 'off',
 3869:                               'unexpired'  => 'off'
 3870:                               );
 3871:         $$rowtotal ++;
 3872:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3873:                                                      \%choices,$itemcount,'','','left','no');
 3874:         $datatable = $enable.$datatable;
 3875:         $$rowtotal += $itemcount;
 3876:         my $lastactiveon = ' ';
 3877:         my $lastactiveoff = ' checked="checked" ';
 3878:         my $lastactivestyle = 'none';
 3879:         my $lastactivedays;
 3880:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 3881:         if (ref($settings) eq 'HASH') {
 3882:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 3883:                 $lastactiveon = $lastactiveoff;
 3884:                 $lastactiveoff = ' ';
 3885:                 $lastactivestyle = 'inline-block';
 3886:                 $lastactivedays = $settings->{'lastactive'};
 3887:             }
 3888:         }
 3889:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3890:         $datatable .= '<tr'.$css_class.'>'.
 3891:                       '<td>'.$choices{'lastactive'}.'</td>'.
 3892:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3893:                       '<input type="radio" name="lastactive"'.
 3894:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3895:                       '&nbsp;<label>'.
 3896:                       '<input type="radio" name="lastactive"'.
 3897:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 3898:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 3899:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 3900:                           '<input type="text" size="5" name="lastactivedays" value="'.
 3901:                           $lastactivedays.'" />').
 3902:                       '</span></td>'.
 3903:                       '</tr>';
 3904:         $$rowtotal ++;
 3905:     } elsif ($position eq 'middle') {
 3906:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3907:         my $numinrow = 3;
 3908:         my $locknamesettings;
 3909:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3910:                                      $dom,$numinrow,$othertitle,
 3911:                                     'lockablenames',$rowtotal);
 3912:         $$rowtotal ++;
 3913:     } else {
 3914:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3915:         my @fields = ('lastname','firstname','middlename','generation',
 3916:                       'permanentemail','id');
 3917:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3918:         my $numrows = 0;
 3919:         if (ref($types) eq 'ARRAY') {
 3920:             if (@{$types} > 0) {
 3921:                 $datatable = 
 3922:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3923:                                          \@fields,$types,\$numrows);
 3924:                     $$rowtotal += @{$types}; 
 3925:             }
 3926:         }
 3927:         $datatable .= 
 3928:             &usertype_update_row($settings,{'default' => $othertitle},
 3929:                                  \%fieldtitles,\@fields,['default'],
 3930:                                  \$numrows);
 3931:         $$rowtotal ++;     
 3932:     }
 3933:     return $datatable;
 3934: }
 3935: 
 3936: sub print_autocreate {
 3937:     my ($dom,$settings,$rowtotal) = @_;
 3938:     my (%createon,%createoff,%currhash);
 3939:     my @types = ('xml','req');
 3940:     if (ref($settings) eq 'HASH') {
 3941:         foreach my $item (@types) {
 3942:             $createoff{$item} = ' checked="checked" ';
 3943:             $createon{$item} = ' ';
 3944:             if (exists($settings->{$item})) {
 3945:                 if ($settings->{$item}) {
 3946:                     $createon{$item} = ' checked="checked" ';
 3947:                     $createoff{$item} = ' ';
 3948:                 }
 3949:             }
 3950:         }
 3951:         if ($settings->{'xmldc'} ne '') {
 3952:             $currhash{$settings->{'xmldc'}} = 1;
 3953:         }
 3954:     } else {
 3955:         foreach my $item (@types) {
 3956:             $createoff{$item} = ' checked="checked" ';
 3957:             $createon{$item} = ' ';
 3958:         }
 3959:     }
 3960:     $$rowtotal += 2;
 3961:     my $numinrow = 2;
 3962:     my $datatable='<tr class="LC_odd_row">'.
 3963:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 3964:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3965:                   '<input type="radio" name="autocreate_xml"'.
 3966:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3967:                   '<label><input type="radio" name="autocreate_xml"'.
 3968:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 3969:                   '</td></tr><tr>'.
 3970:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 3971:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3972:                   '<input type="radio" name="autocreate_req"'.
 3973:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3974:                   '<label><input type="radio" name="autocreate_req"'.
 3975:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 3976:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3977:                                                    'autocreate_xmldc',%currhash);
 3978:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 3979:     if ($numdc > 1) {
 3980:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 3981:                       '</td><td class="LC_left_item">';
 3982:     } else {
 3983:         $datatable .= &mt('Course creation processed as:').
 3984:                       '</td><td class="LC_right_item">';
 3985:     }
 3986:     $datatable .= $dctable.'</td></tr>';
 3987:     $$rowtotal += $rows;
 3988:     return $datatable;
 3989: }
 3990: 
 3991: sub print_directorysrch {
 3992:     my ($position,$dom,$settings,$rowtotal) = @_;
 3993:     my $datatable;
 3994:     if ($position eq 'top') {
 3995:         my $instsrchon = ' ';
 3996:         my $instsrchoff = ' checked="checked" ';
 3997:         my ($exacton,$containson,$beginson);
 3998:         my $instlocalon = ' ';
 3999:         my $instlocaloff = ' checked="checked" ';
 4000:         if (ref($settings) eq 'HASH') {
 4001:             if ($settings->{'available'} eq '1') {
 4002:                 $instsrchon = $instsrchoff;
 4003:                 $instsrchoff = ' ';
 4004:             }
 4005:             if ($settings->{'localonly'} eq '1') {
 4006:                 $instlocalon = $instlocaloff;
 4007:                 $instlocaloff = ' ';
 4008:             }
 4009:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 4010:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 4011:                     if ($type eq 'exact') {
 4012:                         $exacton = ' checked="checked" ';
 4013:                     } elsif ($type eq 'contains') {
 4014:                         $containson = ' checked="checked" ';
 4015:                     } elsif ($type eq 'begins') {
 4016:                         $beginson = ' checked="checked" ';
 4017:                     }
 4018:                 }
 4019:             } else {
 4020:                 if ($settings->{'searchtypes'} eq 'exact') {
 4021:                     $exacton = ' checked="checked" ';
 4022:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 4023:                     $containson = ' checked="checked" ';
 4024:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 4025:                     $exacton = ' checked="checked" ';
 4026:                     $containson = ' checked="checked" ';
 4027:                 }
 4028:             }
 4029:         }
 4030:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 4031:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4032: 
 4033:         my $numinrow = 4;
 4034:         my $cansrchrow = 0;
 4035:         $datatable='<tr class="LC_odd_row">'.
 4036:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 4037:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4038:                    '<input type="radio" name="dirsrch_available"'.
 4039:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4040:                    '<label><input type="radio" name="dirsrch_available"'.
 4041:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4042:                    '</tr><tr>'.
 4043:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 4044:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4045:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 4046:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4047:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 4048:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4049:                    '</tr>';
 4050:         $$rowtotal += 2;
 4051:         if (ref($usertypes) eq 'HASH') {
 4052:             if (keys(%{$usertypes}) > 0) {
 4053:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4054:                                              $numinrow,$othertitle,'cansearch',
 4055:                                              $rowtotal);
 4056:                 $cansrchrow = 1;
 4057:             }
 4058:         }
 4059:         if ($cansrchrow) {
 4060:             $$rowtotal ++;
 4061:             $datatable .= '<tr>';
 4062:         } else {
 4063:             $datatable .= '<tr class="LC_odd_row">';
 4064:         }
 4065:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 4066:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 4067:         foreach my $title (@{$titleorder}) {
 4068:             if (defined($searchtitles->{$title})) {
 4069:                 my $check = ' ';
 4070:                 if (ref($settings) eq 'HASH') {
 4071:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 4072:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 4073:                             $check = ' checked="checked" ';
 4074:                         }
 4075:                     }
 4076:                 }
 4077:                 $datatable .= '<td class="LC_left_item">'.
 4078:                               '<span class="LC_nobreak"><label>'.
 4079:                               '<input type="checkbox" name="searchby" '.
 4080:                               'value="'.$title.'"'.$check.'/>'.
 4081:                               $searchtitles->{$title}.'</label></span></td>';
 4082:             }
 4083:         }
 4084:         $datatable .= '</tr></table></td></tr>';
 4085:         $$rowtotal ++;
 4086:         if ($cansrchrow) {
 4087:             $datatable .= '<tr class="LC_odd_row">';
 4088:         } else {
 4089:             $datatable .= '<tr>';
 4090:         }
 4091:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 4092:                       '<td class="LC_left_item" colspan="2">'.
 4093:                       '<span class="LC_nobreak"><label>'.
 4094:                       '<input type="checkbox" name="searchtypes" '.
 4095:                       $exacton.' value="exact" />'.&mt('Exact match').
 4096:                       '</label>&nbsp;'.
 4097:                       '<label><input type="checkbox" name="searchtypes" '.
 4098:                       $beginson.' value="begins" />'.&mt('Begins with').
 4099:                       '</label>&nbsp;'.
 4100:                       '<label><input type="checkbox" name="searchtypes" '.
 4101:                       $containson.' value="contains" />'.&mt('Contains').
 4102:                       '</label></span></td></tr>';
 4103:         $$rowtotal ++;
 4104:     } else {
 4105:         my $domsrchon = ' checked="checked" ';
 4106:         my $domsrchoff = ' ';
 4107:         my $domlocalon = ' ';
 4108:         my $domlocaloff = ' checked="checked" ';
 4109:         if (ref($settings) eq 'HASH') {
 4110:             if ($settings->{'lclocalonly'} eq '1') {
 4111:                 $domlocalon = $domlocaloff;
 4112:                 $domlocaloff = ' ';
 4113:             }
 4114:             if ($settings->{'lcavailable'} eq '0') {
 4115:                 $domsrchoff = $domsrchon;
 4116:                 $domsrchon = ' ';
 4117:             }
 4118:         }
 4119:         $datatable='<tr class="LC_odd_row">'.
 4120:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 4121:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4122:                       '<input type="radio" name="dirsrch_domavailable"'.
 4123:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4124:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 4125:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4126:                       '</tr><tr>'.
 4127:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 4128:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4129:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 4130:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4131:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 4132:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4133:                       '</tr>';
 4134:         $$rowtotal += 2;
 4135:     }
 4136:     return $datatable;
 4137: }
 4138: 
 4139: sub print_contacts {
 4140:     my ($position,$dom,$settings,$rowtotal) = @_;
 4141:     my $datatable;
 4142:     my @contacts = ('adminemail','supportemail');
 4143:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 4144:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 4145:     if ($position eq 'top') {
 4146:         if (ref($settings) eq 'HASH') {
 4147:             foreach my $item (@contacts) {
 4148:                 if (exists($settings->{$item})) {
 4149:                     $to{$item} = $settings->{$item};
 4150:                 }
 4151:             }
 4152:         }
 4153:     } elsif ($position eq 'middle') {
 4154:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 4155:                      'updatesmail','idconflictsmail','hostipmail');
 4156:         foreach my $type (@mailings) {
 4157:             $otheremails{$type} = '';
 4158:         }
 4159:     } elsif ($position eq 'lower') {
 4160:         if (ref($settings) eq 'HASH') {
 4161:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 4162:                 %lonstatus = %{$settings->{'lonstatus'}};
 4163:             }
 4164:         }
 4165:     } else {
 4166:         @mailings = ('helpdeskmail','otherdomsmail');
 4167:         foreach my $type (@mailings) {
 4168:             $otheremails{$type} = '';
 4169:         }
 4170:         $bccemails{'helpdeskmail'} = '';
 4171:         $bccemails{'otherdomsmail'} = '';
 4172:         $includestr{'helpdeskmail'} = '';
 4173:         $includestr{'otherdomsmail'} = '';
 4174:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 4175:     }
 4176:     if (ref($settings) eq 'HASH') {
 4177:         unless (($position eq 'top') || ($position eq 'lower')) {
 4178:             foreach my $type (@mailings) {
 4179:                 if (exists($settings->{$type})) {
 4180:                     if (ref($settings->{$type}) eq 'HASH') {
 4181:                         foreach my $item (@contacts) {
 4182:                             if ($settings->{$type}{$item}) {
 4183:                                 $checked{$type}{$item} = ' checked="checked" ';
 4184:                             }
 4185:                         }
 4186:                         $otheremails{$type} = $settings->{$type}{'others'};
 4187:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4188:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 4189:                             if ($settings->{$type}{'include'} ne '') {
 4190:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4191:                                 $includestr{$type} = &unescape($includestr{$type});
 4192:                             }
 4193:                         }
 4194:                     }
 4195:                 } elsif ($type eq 'lonstatusmail') {
 4196:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4197:                 }
 4198:             }
 4199:         }
 4200:         if ($position eq 'bottom') {
 4201:             foreach my $type (@mailings) {
 4202:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 4203:                 if ($settings->{$type}{'include'} ne '') {
 4204:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4205:                     $includestr{$type} = &unescape($includestr{$type});
 4206:                 }
 4207:             }
 4208:             if (ref($settings->{'helpform'}) eq 'HASH') {
 4209:                 if (ref($fields) eq 'ARRAY') {
 4210:                     foreach my $field (@{$fields}) {
 4211:                         $currfield{$field} = $settings->{'helpform'}{$field};
 4212:                     }
 4213:                 }
 4214:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 4215:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 4216:                 } else {
 4217:                     $maxsize = '1.0';
 4218:                 }
 4219:             } else {
 4220:                 if (ref($fields) eq 'ARRAY') {
 4221:                     foreach my $field (@{$fields}) {
 4222:                         $currfield{$field} = 'yes';
 4223:                     }
 4224:                 }
 4225:                 $maxsize = '1.0';
 4226:             }
 4227:         }
 4228:     } else {
 4229:         if ($position eq 'top') {
 4230:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 4231:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 4232:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 4233:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 4234:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4235:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 4236:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 4237:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 4238:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 4239:         } elsif ($position eq 'bottom') {
 4240:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 4241:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 4242:             if (ref($fields) eq 'ARRAY') {
 4243:                 foreach my $field (@{$fields}) {
 4244:                     $currfield{$field} = 'yes';
 4245:                 }
 4246:             }
 4247:             $maxsize = '1.0';
 4248:         }
 4249:     }
 4250:     my ($titles,$short_titles) = &contact_titles();
 4251:     my $rownum = 0;
 4252:     my $css_class;
 4253:     if ($position eq 'top') {
 4254:         foreach my $item (@contacts) {
 4255:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4256:             $datatable .= '<tr'.$css_class.'>'. 
 4257:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4258:                           '</span></td><td class="LC_right_item">'.
 4259:                           '<input type="text" name="'.$item.'" value="'.
 4260:                           $to{$item}.'" /></td></tr>';
 4261:             $rownum ++;
 4262:         }
 4263:     } elsif ($position eq 'bottom') {
 4264:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4265:         $datatable .= '<tr'.$css_class.'>'.
 4266:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 4267:                       &mt('(e-mail, subject, and description always shown)').
 4268:                       '</td><td class="LC_left_item">';
 4269:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 4270:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 4271:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 4272:             foreach my $field (@{$fields}) {
 4273:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 4274:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 4275:                     $datatable .= ' '.&mt('(logged-in users)');
 4276:                 }
 4277:                 $datatable .='</td><td>';
 4278:                 my $clickaction;
 4279:                 if ($field eq 'screenshot') {
 4280:                     $clickaction = ' onclick="screenshotSize(this);"';
 4281:                 }
 4282:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 4283:                     foreach my $option (@{$possoptions->{$field}}) {
 4284:                         my $checked;
 4285:                         if ($currfield{$field} eq $option) {
 4286:                             $checked = ' checked="checked"';
 4287:                         }
 4288:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4289:                                       '<input type="radio" name="helpform_'.$field.'" '.
 4290:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 4291:                                       '</label></span>'.('&nbsp;'x2);
 4292:                     }
 4293:                 }
 4294:                 if ($field eq 'screenshot') {
 4295:                     my $display;
 4296:                     if ($currfield{$field} eq 'no') {
 4297:                         $display = ' style="display:none"';
 4298:                     }
 4299:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 4300:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 4301:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 4302:                 }
 4303:                 $datatable .= '</td></tr>';
 4304:             }
 4305:             $datatable .= '</table>';
 4306:         }
 4307:         $datatable .= '</td></tr>'."\n";
 4308:         $rownum ++;
 4309:     }
 4310:     unless (($position eq 'top') || ($position eq 'lower')) {
 4311:         foreach my $type (@mailings) {
 4312:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4313:             $datatable .= '<tr'.$css_class.'>'.
 4314:                           '<td><span class="LC_nobreak">'.
 4315:                           $titles->{$type}.': </span></td>'.
 4316:                           '<td class="LC_left_item">';
 4317:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4318:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 4319:             }
 4320:             $datatable .= '<span class="LC_nobreak">';
 4321:             foreach my $item (@contacts) {
 4322:                 $datatable .= '<label>'.
 4323:                               '<input type="checkbox" name="'.$type.'"'.
 4324:                               $checked{$type}{$item}.
 4325:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 4326:                               '</label>&nbsp;';
 4327:             }
 4328:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4329:                           '<input type="text" name="'.$type.'_others" '.
 4330:                           'value="'.$otheremails{$type}.'"  />';
 4331:             my %locchecked;
 4332:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4333:                 foreach my $loc ('s','b') {
 4334:                     if ($includeloc{$type} eq $loc) {
 4335:                         $locchecked{$loc} = ' checked="checked"';
 4336:                         last;
 4337:                     }
 4338:                 }
 4339:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4340:                               '<input type="text" name="'.$type.'_bcc" '.
 4341:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 4342:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4343:                               &mt('Text automatically added to e-mail:').' '.
 4344:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 4345:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4346:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4347:                               ('&nbsp;'x2).
 4348:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4349:                               '</span></fieldset>';
 4350:             }
 4351:             $datatable .= '</td></tr>'."\n";
 4352:             $rownum ++;
 4353:         }
 4354:     }
 4355:     if ($position eq 'middle') {
 4356:         my %choices;
 4357:         my $corelink = &core_link_msu();
 4358:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 4359:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 4360:                                         $corelink);
 4361:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 4362:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 4363:         my %defaultchecked = ('reporterrors'  => 'on',
 4364:                               'reportupdates' => 'on',
 4365:                               'reportstatus'  => 'on');
 4366:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4367:                                                    \%choices,$rownum);
 4368:         $datatable .= $reports;
 4369:     } elsif ($position eq 'lower') {
 4370:         my (%current,%excluded,%weights);
 4371:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 4372:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 4373:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 4374:         } else {
 4375:             $current{'errorthreshold'} = $defaults->{'threshold'};
 4376:         }
 4377:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 4378:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 4379:         } else {
 4380:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 4381:         }
 4382:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 4383:             foreach my $type ('E','W','N','U') {
 4384:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 4385:                     $weights{$type} = $lonstatus{'weights'}{$type};
 4386:                 } else {
 4387:                     $weights{$type} = $defaults->{$type};
 4388:                 }
 4389:             }
 4390:         } else {
 4391:             foreach my $type ('E','W','N','U') {
 4392:                 $weights{$type} = $defaults->{$type};
 4393:             }
 4394:         }
 4395:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 4396:             if (@{$lonstatus{'excluded'}} > 0) {
 4397:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 4398:             }
 4399:         }
 4400:         foreach my $item ('errorthreshold','errorsysmail') {
 4401:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4402:             $datatable .= '<tr'.$css_class.'>'.
 4403:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 4404:                           $titles->{$item}.
 4405:                           '</span></td><td class="LC_left_item">'.
 4406:                           '<input type="text" name="'.$item.'" value="'.
 4407:                           $current{$item}.'" size="5" /></td></tr>';
 4408:             $rownum ++;
 4409:         }
 4410:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4411:         $datatable .= '<tr'.$css_class.'>'.
 4412:                       '<td class="LC_left_item">'.
 4413:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 4414:                       '</span></td><td class="LC_left_item"><table><tr>';
 4415:         foreach my $type ('E','W','N','U') {
 4416:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 4417:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 4418:                           $weights{$type}.'" size="5" /></td>';
 4419:         }
 4420:         $datatable .= '</tr></table></tr>';
 4421:         $rownum ++;
 4422:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4423:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 4424:                       $titles->{'errorexcluded'}.'</td>'.
 4425:                       '<td class="LC_left_item"><table>';
 4426:         my $numinrow = 4;
 4427:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 4428:         for (my $i=0; $i<@ids; $i++) {
 4429:             my $rem = $i%($numinrow);
 4430:             if ($rem == 0) {
 4431:                 if ($i > 0) {
 4432:                     $datatable .= '</tr>';
 4433:                 }
 4434:                 $datatable .= '<tr>';
 4435:             }
 4436:             my $check;
 4437:             if ($excluded{$ids[$i]}) {
 4438:                 $check = ' checked="checked" ';
 4439:             }
 4440:             $datatable .= '<td class="LC_left_item">'.
 4441:                           '<span class="LC_nobreak"><label>'.
 4442:                           '<input type="checkbox" name="errorexcluded" '.
 4443:                           'value="'.$ids[$i].'"'.$check.' />'.
 4444:                           $ids[$i].'</label></span></td>';
 4445:         }
 4446:         my $colsleft = $numinrow - @ids%($numinrow);
 4447:         if ($colsleft > 1 ) {
 4448:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4449:                           '&nbsp;</td>';
 4450:         } elsif ($colsleft == 1) {
 4451:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4452:         }
 4453:         $datatable .= '</tr></table></td></tr>';
 4454:         $rownum ++;
 4455:     } elsif ($position eq 'bottom') {
 4456:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4457:         my (@posstypes,%usertypeshash);
 4458:         if (ref($types) eq 'ARRAY') {
 4459:             @posstypes = @{$types};
 4460:         }
 4461:         if (@posstypes) {
 4462:             if (ref($usertypes) eq 'HASH') {
 4463:                 %usertypeshash = %{$usertypes};
 4464:             }
 4465:             my @overridden;
 4466:             my $numinrow = 4;
 4467:             if (ref($settings) eq 'HASH') {
 4468:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 4469:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 4470:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 4471:                             push(@overridden,$key);
 4472:                             foreach my $item (@contacts) {
 4473:                                 if ($settings->{'overrides'}{$key}{$item}) {
 4474:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 4475:                                 }
 4476:                             }
 4477:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 4478:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 4479:                             $includeloc{'override_'.$key} = '';
 4480:                             $includestr{'override_'.$key} = '';
 4481:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 4482:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 4483:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 4484:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 4485:                             }
 4486:                         }
 4487:                     }
 4488:                 }
 4489:             }
 4490:             my $customclass = 'LC_helpdesk_override';
 4491:             my $optionsprefix = 'LC_options_helpdesk_';
 4492: 
 4493:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 4494:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4495:                                          $numinrow,$othertitle,'overrides',
 4496:                                          \$rownum,$onclicktypes,$customclass);
 4497:             $rownum ++;
 4498:             $usertypeshash{'default'} = $othertitle;
 4499:             foreach my $status (@posstypes) {
 4500:                 my $css_class;
 4501:                 if ($rownum%2) {
 4502:                     $css_class = 'LC_odd_row ';
 4503:                 }
 4504:                 $css_class .= $customclass;
 4505:                 my $rowid = $optionsprefix.$status;
 4506:                 my $hidden = 1;
 4507:                 my $currstyle = 'display:none';
 4508:                 if (grep(/^\Q$status\E$/,@overridden)) {
 4509:                     $currstyle = 'display:table-row';
 4510:                     $hidden = 0;
 4511:                 }
 4512:                 my $key = 'override_'.$status;
 4513:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 4514:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 4515:                                                   $usertypeshash{$status},$css_class,$currstyle,
 4516:                                                   \@contacts,$short_titles);
 4517:                 unless ($hidden) {
 4518:                     $rownum ++;
 4519:                 }
 4520:             }
 4521:         }
 4522:     }
 4523:     $$rowtotal += $rownum;
 4524:     return $datatable;
 4525: }
 4526: 
 4527: sub core_link_msu {
 4528:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 4529:                                           &mt('LON-CAPA core group - MSU'),600,500);
 4530: }
 4531: 
 4532: sub overridden_helpdesk {
 4533:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 4534:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 4535:     my $class = 'LC_left_item';
 4536:     if ($css_class) {
 4537:         $css_class = ' class="'.$css_class.'"';
 4538:     }
 4539:     if ($rowid) {
 4540:         $rowid = ' id="'.$rowid.'"';
 4541:     }
 4542:     if ($rowstyle) {
 4543:         $rowstyle = ' style="'.$rowstyle.'"';
 4544:     }
 4545:     my ($output,$description);
 4546:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 4547:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 4548:               "<td>$description</td>\n".
 4549:               '<td class="'.$class.'" colspan="2">'.
 4550:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 4551:               '<span class="LC_nobreak">';
 4552:     if (ref($contacts) eq 'ARRAY') {
 4553:         foreach my $item (@{$contacts}) {
 4554:             my $check;
 4555:             if (ref($checked) eq 'HASH') {
 4556:                $check = $checked->{$item};
 4557:             }
 4558:             my $title;
 4559:             if (ref($short_titles) eq 'HASH') {
 4560:                 $title = $short_titles->{$item};
 4561:             }
 4562:             $output .= '<label>'.
 4563:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 4564:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 4565:         }
 4566:     }
 4567:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4568:                '<input type="text" name="override_'.$type.'_others" '.
 4569:                'value="'.$otheremails.'"  />';
 4570:     my %locchecked;
 4571:     foreach my $loc ('s','b') {
 4572:         if ($includeloc eq $loc) {
 4573:             $locchecked{$loc} = ' checked="checked"';
 4574:             last;
 4575:         }
 4576:     }
 4577:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4578:                '<input type="text" name="override_'.$type.'_bcc" '.
 4579:                'value="'.$bccemails.'"  /></fieldset>'.
 4580:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4581:                &mt('Text automatically added to e-mail:').' '.
 4582:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4583:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4584:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4585:                ('&nbsp;'x2).
 4586:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4587:                '</span></fieldset>'.
 4588:                '</td></tr>'."\n";
 4589:     return $output;
 4590: }
 4591: 
 4592: sub contacts_javascript {
 4593:     return <<"ENDSCRIPT";
 4594: 
 4595: <script type="text/javascript">
 4596: // <![CDATA[
 4597: 
 4598: function screenshotSize(field) {
 4599:     if (document.getElementById('help_screenshotsize')) {
 4600:         if (field.value == 'no') {
 4601:             document.getElementById('help_screenshotsize').style.display="none";
 4602:         } else {
 4603:             document.getElementById('help_screenshotsize').style.display="";
 4604:         }
 4605:     }
 4606:     return;
 4607: }
 4608: 
 4609: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4610:     if (form.elements[checkbox].length != undefined) {
 4611:         var count = 0;
 4612:         if (docount) {
 4613:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4614:                 if (form.elements[checkbox][i].checked) {
 4615:                     count ++;
 4616:                 }
 4617:             }
 4618:         }
 4619:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4620:             var type = form.elements[checkbox][i].value;
 4621:             if (document.getElementById(prefix+type)) {
 4622:                 if (form.elements[checkbox][i].checked) {
 4623:                     document.getElementById(prefix+type).style.display = 'table-row';
 4624:                     if (count % 2 == 1) {
 4625:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4626:                     } else {
 4627:                         document.getElementById(prefix+type).className = target;
 4628:                     }
 4629:                     count ++;
 4630:                 } else {
 4631:                     document.getElementById(prefix+type).style.display = 'none';
 4632:                 }
 4633:             }
 4634:         }
 4635:     }
 4636:     return;
 4637: }
 4638: 
 4639: // ]]>
 4640: </script>
 4641: 
 4642: ENDSCRIPT
 4643: }
 4644: 
 4645: sub print_helpsettings {
 4646:     my ($position,$dom,$settings,$rowtotal) = @_;
 4647:     my $confname = $dom.'-domainconfig';
 4648:     my $formname = 'display';
 4649:     my ($datatable,$itemcount);
 4650:     if ($position eq 'top') {
 4651:         $itemcount = 1;
 4652:         my (%choices,%defaultchecked,@toggles);
 4653:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4654:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4655:                                      &mt('LON-CAPA bug tracker'),600,500));
 4656:         %defaultchecked = ('submitbugs' => 'on');
 4657:         @toggles = ('submitbugs');
 4658:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4659:                                                      \%choices,$itemcount);
 4660:         $$rowtotal ++;
 4661:     } else {
 4662:         my $css_class;
 4663:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4664:         my (%customroles,%ordered,%current);
 4665:         if (ref($settings) eq 'HASH') {
 4666:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4667:                 %current = %{$settings->{'adhoc'}};
 4668:             }
 4669:         }
 4670:         my $count = 0;
 4671:         foreach my $key (sort(keys(%existing))) {
 4672:             if ($key=~/^rolesdef\_(\w+)$/) {
 4673:                 my $rolename = $1;
 4674:                 my (%privs,$order);
 4675:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4676:                 $customroles{$rolename} = \%privs;
 4677:                 if (ref($current{$rolename}) eq 'HASH') {
 4678:                     $order = $current{$rolename}{'order'};
 4679:                 }
 4680:                 if ($order eq '') {
 4681:                     $order = $count;
 4682:                 }
 4683:                 $ordered{$order} = $rolename;
 4684:                 $count++;
 4685:             }
 4686:         }
 4687:         my $maxnum = scalar(keys(%ordered));
 4688:         my @roles_by_num = ();
 4689:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4690:             push(@roles_by_num,$item);
 4691:         }
 4692:         my $context = 'domprefs';
 4693:         my $crstype = 'Course';
 4694:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4695:         my @accesstypes = ('all','dh','da','none');
 4696:         my ($numstatustypes,@jsarray);
 4697:         if (ref($types) eq 'ARRAY') {
 4698:             if (@{$types} > 0) {
 4699:                 $numstatustypes = scalar(@{$types});
 4700:                 push(@accesstypes,'status');
 4701:                 @jsarray = ('bystatus');
 4702:             }
 4703:         }
 4704:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4705:         if (keys(%domhelpdesk)) {
 4706:             push(@accesstypes,('inc','exc'));
 4707:             push(@jsarray,('notinc','notexc'));
 4708:         }
 4709:         my $hiddenstr = join("','",@jsarray);
 4710:         my $context = 'domprefs';
 4711:         my $crstype = 'Course';
 4712:         my $prefix = 'helproles_';
 4713:         my $add_class = 'LC_hidden';
 4714:         foreach my $num (@roles_by_num) {
 4715:             my $role = $ordered{$num};
 4716:             my ($desc,$access,@statuses);
 4717:             if (ref($current{$role}) eq 'HASH') {
 4718:                 $desc = $current{$role}{'desc'};
 4719:                 $access = $current{$role}{'access'};
 4720:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4721:                     @statuses = @{$current{$role}{'insttypes'}};
 4722:                 }
 4723:             }
 4724:             if ($desc eq '') {
 4725:                 $desc = $role;
 4726:             }
 4727:             my $identifier = 'custhelp'.$num;
 4728:             my %full=();
 4729:             my %levels= (
 4730:                          course => {},
 4731:                          domain => {},
 4732:                          system => {},
 4733:                         );
 4734:             my %levelscurrent=(
 4735:                                course => {},
 4736:                                domain => {},
 4737:                                system => {},
 4738:                               );
 4739:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4740:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4741:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4742:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4743:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 4744:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4745:             for (my $k=0; $k<=$maxnum; $k++) {
 4746:                 my $vpos = $k+1;
 4747:                 my $selstr;
 4748:                 if ($k == $num) {
 4749:                     $selstr = ' selected="selected" ';
 4750:                 }
 4751:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4752:             }
 4753:             $datatable .= '</select>'.('&nbsp;'x2).
 4754:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 4755:                           '</td>'.
 4756:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4757:                           &mt('Name shown to users:').
 4758:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 4759:                           '</fieldset>'.
 4760:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4761:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4762:                           '<fieldset>'.
 4763:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4764:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4765:                                                                    \%levelscurrent,$identifier,
 4766:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4767:                           '</fieldset></td>';
 4768:             $itemcount ++;
 4769:         }
 4770:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4771:         my $newcust = 'custhelp'.$count;
 4772:         my (%privs,%levelscurrent);
 4773:         my %full=();
 4774:         my %levels= (
 4775:                      course => {},
 4776:                      domain => {},
 4777:                      system => {},
 4778:                     );
 4779:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4780:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4781:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4782:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 4783:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4784:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4785:         for (my $k=0; $k<$maxnum+1; $k++) {
 4786:             my $vpos = $k+1;
 4787:             my $selstr;
 4788:             if ($k == $maxnum) {
 4789:                 $selstr = ' selected="selected" ';
 4790:             }
 4791:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4792:         }
 4793:         $datatable .= '</select>&nbsp;'."\n".
 4794:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4795:                       '</label></span></td>'.
 4796:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4797:                       '<span class="LC_nobreak">'.
 4798:                       &mt('Internal name:').
 4799:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4800:                       '</span>'.('&nbsp;'x4).
 4801:                       '<span class="LC_nobreak">'.
 4802:                       &mt('Name shown to users:').
 4803:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4804:                       '</span></fieldset>'.
 4805:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4806:                                              $usertypes,$types,\%domhelpdesk).
 4807:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4808:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4809:                                                                 \@templateroles,$newcust).
 4810:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4811:                                                                \%levelscurrent,$newcust).
 4812:                       '</fieldset>'.
 4813:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4814:                       '</td></tr>';
 4815:         $count ++;
 4816:         $$rowtotal += $count;
 4817:     }
 4818:     return $datatable;
 4819: }
 4820: 
 4821: sub adhocbutton {
 4822:     my ($prefix,$num,$field,$visibility) = @_;
 4823:     my %lt = &Apache::lonlocal::texthash(
 4824:                                           show => 'Show details',
 4825:                                           hide => 'Hide details',
 4826:                                         );
 4827:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4828:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4829:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4830:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4831: }
 4832: 
 4833: sub helpsettings_javascript {
 4834:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4835:     return unless(ref($roles_by_num) eq 'ARRAY');
 4836:     my %html_js_lt = &Apache::lonlocal::texthash(
 4837:                                           show => 'Show details',
 4838:                                           hide => 'Hide details',
 4839:                                         );
 4840:     &html_escape(\%html_js_lt);
 4841:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4842:     return <<"ENDSCRIPT";
 4843: <script type="text/javascript">
 4844: // <![CDATA[
 4845: 
 4846: function reorderHelpRoles(form,item) {
 4847:     var changedVal;
 4848: $jstext
 4849:     var newpos = 'helproles_${total}_pos';
 4850:     var maxh = 1 + $total;
 4851:     var current = new Array();
 4852:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4853:     if (item == newpos) {
 4854:         changedVal = newitemVal;
 4855:     } else {
 4856:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4857:         current[newitemVal] = newpos;
 4858:     }
 4859:     for (var i=0; i<helproles.length; i++) {
 4860:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4861:         if (elementName != item) {
 4862:             if (form.elements[elementName]) {
 4863:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4864:                 current[currVal] = elementName;
 4865:             }
 4866:         }
 4867:     }
 4868:     var oldVal;
 4869:     for (var j=0; j<maxh; j++) {
 4870:         if (current[j] == undefined) {
 4871:             oldVal = j;
 4872:         }
 4873:     }
 4874:     if (oldVal < changedVal) {
 4875:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4876:            var elementName = current[k];
 4877:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4878:         }
 4879:     } else {
 4880:         for (var k=changedVal; k<oldVal; k++) {
 4881:             var elementName = current[k];
 4882:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4883:         }
 4884:     }
 4885:     return;
 4886: }
 4887: 
 4888: function helpdeskAccess(num) {
 4889:     var curraccess = null;
 4890:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4891:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4892:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4893:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4894:             }
 4895:         }
 4896:     }
 4897:     var shown = Array();
 4898:     var hidden = Array();
 4899:     if (curraccess == 'none') {
 4900:         hidden = Array('$hiddenstr');
 4901:     } else {
 4902:         if (curraccess == 'status') {
 4903:             shown = Array('bystatus');
 4904:             hidden = Array('notinc','notexc');
 4905:         } else {
 4906:             if (curraccess == 'exc') {
 4907:                 shown = Array('notexc');
 4908:                 hidden = Array('notinc','bystatus');
 4909:             }
 4910:             if (curraccess == 'inc') {
 4911:                 shown = Array('notinc');
 4912:                 hidden = Array('notexc','bystatus');
 4913:             }
 4914:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4915:                 hidden = Array('notinc','notexc','bystatus');
 4916:             }
 4917:         }
 4918:     }
 4919:     if (hidden.length > 0) {
 4920:         for (var i=0; i<hidden.length; i++) {
 4921:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4922:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4923:             }
 4924:         }
 4925:     }
 4926:     if (shown.length > 0) {
 4927:         for (var i=0; i<shown.length; i++) {
 4928:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4929:                 if (shown[i] == 'privs') {
 4930:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4931:                 } else {
 4932:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4933:                 }
 4934:             }
 4935:         }
 4936:     }
 4937:     return;
 4938: }
 4939: 
 4940: function toggleHelpdeskItem(num,field) {
 4941:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4942:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4943:             document.getElementById('helproles_'+num+'_'+field).className =
 4944:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4945:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4946:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4947:             }
 4948:         } else {
 4949:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4950:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4951:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4952:             }
 4953:         }
 4954:     }
 4955:     return;
 4956: }
 4957: 
 4958: // ]]>
 4959: </script>
 4960: 
 4961: ENDSCRIPT
 4962: }
 4963: 
 4964: sub helpdeskroles_access {
 4965:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 4966:         $usertypes,$types,$domhelpdesk) = @_;
 4967:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 4968:     my %lt = &Apache::lonlocal::texthash(
 4969:                     'rou'    => 'Role usage',
 4970:                     'whi'    => 'Which helpdesk personnel may use this role?',
 4971:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 4972:                     'dh'     => 'All with domain helpdesk role',
 4973:                     'da'     => 'All with domain helpdesk assistant role',
 4974:                     'none'   => 'None',
 4975:                     'status' => 'Determined based on institutional status',
 4976:                     'inc'    => 'Include all, but exclude specific personnel',
 4977:                     'exc'    => 'Exclude all, but include specific personnel',
 4978:                   );
 4979:     my %usecheck = (
 4980:                      all => ' checked="checked"',
 4981:                    );
 4982:     my %displaydiv = (
 4983:                       status => 'none',
 4984:                       inc    => 'none',
 4985:                       exc    => 'none',
 4986:                       priv   => 'block',
 4987:                      );
 4988:     my $output;
 4989:     if (ref($current) eq 'HASH') {
 4990:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 4991:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 4992:                 $usecheck{$current->{access}} = $usecheck{'all'};
 4993:                 delete($usecheck{'all'});
 4994:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 4995:                     my $access = $1;
 4996:                     $displaydiv{$access} = 'inline';
 4997:                 } elsif ($current->{access} eq 'none') {
 4998:                     $displaydiv{'priv'} = 'none';
 4999:                 }
 5000:             }
 5001:         }
 5002:     }
 5003:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 5004:               '<p>'.$lt{'whi'}.'</p>';
 5005:     foreach my $access (@{$accesstypes}) {
 5006:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 5007:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 5008:                    $lt{$access}.'</label>';
 5009:         if ($access eq 'status') {
 5010:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 5011:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 5012:                                                                  $othertitle,$usertypes,$types).
 5013:                        '</div>';
 5014:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 5015:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 5016:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5017:                        '</div>';
 5018:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 5019:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 5020:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5021:                        '</div>';
 5022:         }
 5023:         $output .= '</p>';
 5024:     }
 5025:     $output .= '</fieldset>';
 5026:     return $output;
 5027: }
 5028: 
 5029: sub radiobutton_prefs {
 5030:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 5031:         $additional,$align,$firstval) = @_;
 5032:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 5033:                    (ref($choices) eq 'HASH'));
 5034: 
 5035:     my (%checkedon,%checkedoff,$datatable,$css_class);
 5036: 
 5037:     foreach my $item (@{$toggles}) {
 5038:         if ($defaultchecked->{$item} eq 'on') {
 5039:             $checkedon{$item} = ' checked="checked" ';
 5040:             $checkedoff{$item} = ' ';
 5041:         } elsif ($defaultchecked->{$item} eq 'off') {
 5042:             $checkedoff{$item} = ' checked="checked" ';
 5043:             $checkedon{$item} = ' ';
 5044:         }
 5045:     }
 5046:     if (ref($settings) eq 'HASH') {
 5047:         foreach my $item (@{$toggles}) {
 5048:             if ($settings->{$item} eq '1') {
 5049:                 $checkedon{$item} =  ' checked="checked" ';
 5050:                 $checkedoff{$item} = ' ';
 5051:             } elsif ($settings->{$item} eq '0') {
 5052:                 $checkedoff{$item} =  ' checked="checked" ';
 5053:                 $checkedon{$item} = ' ';
 5054:             }
 5055:         }
 5056:     }
 5057:     if ($onclick) {
 5058:         $onclick = ' onclick="'.$onclick.'"';
 5059:     }
 5060:     foreach my $item (@{$toggles}) {
 5061:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5062:         $datatable .=
 5063:             '<tr'.$css_class.'><td valign="top">'.
 5064:             '<span class="LC_nobreak">'.$choices->{$item}.
 5065:             '</span></td>';
 5066:         if ($align eq 'left') {
 5067:             $datatable .= '<td class="LC_left_item">';
 5068:         } else {
 5069:             $datatable .= '<td class="LC_right_item">';
 5070:         }
 5071:         $datatable .= '<span class="LC_nobreak">';
 5072:         if ($firstval eq 'no') {
 5073:             $datatable .=
 5074:                 '<label><input type="radio" name="'.
 5075:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 5076:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5077:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 5078:         } else {
 5079:             $datatable .=
 5080:                 '<label><input type="radio" name="'.
 5081:                 $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 5082:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5083:                 $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 5084:         }
 5085:         $datatable .= '</span>'.$additional.'</td></tr>';
 5086:         $itemcount ++;
 5087:     }
 5088:     return ($datatable,$itemcount);
 5089: }
 5090: 
 5091: sub print_ltitools {
 5092:     my ($position,$dom,$settings,$rowtotal) = @_;
 5093:     my (%rules,%encrypt,%privkeys,%linkprot);
 5094:     if (ref($settings) eq 'HASH') {
 5095:         if ($position eq 'top') {
 5096:             if (exists($settings->{'encrypt'})) {
 5097:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 5098:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 5099:                         $encrypt{'toolsec_'.$key} = $settings->{'encrypt'}{$key};
 5100:                     }
 5101:                 }
 5102:             }
 5103:             if (exists($settings->{'private'})) {
 5104:                 if (ref($settings->{'private'}) eq 'HASH') {
 5105:                     if (ref($settings->{'private'}) eq 'HASH') {
 5106:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 5107:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 5108:                         }
 5109:                     }
 5110:                 }
 5111:             }
 5112:         } elsif ($position eq 'middle') {
 5113:             if (exists($settings->{'rules'})) {
 5114:                 if (ref($settings->{'rules'}) eq 'HASH') {
 5115:                     %rules = %{$settings->{'rules'}};
 5116:                 }
 5117:             }
 5118:         } else {
 5119:             foreach my $key ('encrypt','private','rules') {
 5120:                 if (exists($settings->{$key})) {
 5121:                     delete($settings->{$key});
 5122:                 }
 5123:             }
 5124:         }
 5125:     }
 5126:     my $datatable;
 5127:     my $itemcount = 1;
 5128:     if ($position eq 'top') {
 5129:         $datatable = &secrets_form($dom,'toolsec',\%encrypt,\%privkeys,$rowtotal);
 5130:     } elsif ($position eq 'middle') {
 5131:         $datatable = &password_rules('toolsecrets',\$itemcount,\%rules);
 5132:         $$rowtotal += $itemcount;
 5133:     } else {
 5134:         $datatable = &Apache::courseprefs::print_ltitools($dom,'',$settings,\$rowtotal,'','','domain');
 5135:     }
 5136:     return $datatable;
 5137: }
 5138: 
 5139: sub ltitools_names {
 5140:     my %lt = &Apache::lonlocal::texthash(
 5141:                                           'title'          => 'Title',
 5142:                                           'version'        => 'Version',
 5143:                                           'msgtype'        => 'Message Type',
 5144:                                           'sigmethod'      => 'Signature Method',
 5145:                                           'url'            => 'URL',
 5146:                                           'key'            => 'Key',
 5147:                                           'lifetime'       => 'Nonce lifetime (s)',
 5148:                                           'secret'         => 'Secret',
 5149:                                           'icon'           => 'Icon',
 5150:                                           'user'           => 'User',
 5151:                                           'fullname'       => 'Full Name',
 5152:                                           'firstname'      => 'First Name',
 5153:                                           'lastname'       => 'Last Name',
 5154:                                           'email'          => 'E-mail',
 5155:                                           'roles'          => 'Role',
 5156:                                           'window'         => 'Window',
 5157:                                           'tab'            => 'Tab',
 5158:                                           'iframe'         => 'iFrame',
 5159:                                           'height'         => 'Height',
 5160:                                           'width'          => 'Width',
 5161:                                           'linktext'       => 'Default Link Text',
 5162:                                           'explanation'    => 'Default Explanation',
 5163:                                           'passback'       => 'Tool can return grades:',
 5164:                                           'roster'         => 'Tool can retrieve roster:',
 5165:                                           'crstarget'      => 'Display target',
 5166:                                           'crslabel'       => 'Course label',
 5167:                                           'crstitle'       => 'Course title',
 5168:                                           'crslinktext'    => 'Link Text',
 5169:                                           'crsexplanation' => 'Explanation',
 5170:                                           'crsappend'      => 'Provider URL',
 5171:                                         );
 5172:     return %lt;
 5173: }
 5174: 
 5175: sub secrets_form {
 5176:     my ($dom,$context,$encrypt,$privkeys,$rowtotal) = @_;
 5177:     my @ids=&Apache::lonnet::current_machine_ids();
 5178:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 5179:     my $primary = &Apache::lonnet::domain($dom,'primary');
 5180:     my ($css_class,$extra,$numshown,$itemcount,$output);
 5181:     $itemcount = 0;
 5182:     foreach my $hostid (sort(keys(%servers))) {
 5183:         my ($showextra,$divsty,$switch);
 5184:         if ($hostid eq $primary) {
 5185:             if ($context eq 'ltisec') {
 5186:                 if (($encrypt->{'ltisec_consumers'}) || ($encrypt->{'ltisec_domlinkprot'})) {
 5187:                     $showextra = 1;
 5188:                 }
 5189:                 if ($encrypt->{'ltisec_crslinkprot'}) {
 5190:                     $showextra = 1;
 5191:                 }
 5192:             } else {
 5193:                 if (($encrypt->{'toolsec_crs'}) || ($encrypt->{'toolsec_dom'})) {
 5194:                     $showextra = 1;
 5195:                 }
 5196:             }
 5197:             unless (grep(/^\Q$hostid\E$/,@ids)) {
 5198:                 $switch = 1;
 5199:             }
 5200:             if ($showextra) {
 5201:                 $numshown ++;
 5202:                 $divsty = 'display:inline-block';
 5203:             } else {
 5204:                 $divsty = 'display:none';
 5205:             }
 5206:             $extra .= '<fieldset id="'.$context.'_info_'.$hostid.'" style="'.$divsty.'">'.
 5207:                       '<legend>'.$hostid.'</legend>';
 5208:             if ($switch) {
 5209:                 my $switchserver = '<a href="/adm/switchserver?otherserver='.$hostid.'&amp;role='.
 5210:                                    &HTML::Entities::encode($env{'request.role'},'\'<>"&').
 5211:                                    '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5212:                 if (exists($privkeys->{$hostid})) {
 5213:                     $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" />'.
 5214:                               '<span class="LC_nobreak">'.
 5215:                               &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5216:                               '<span class="LC_nobreak">'.&mt('Change?').
 5217:                               '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5218:                               ('&nbsp;'x2).
 5219:                               '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5220:                               '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5221:                               '<span class="LC_nobreak"> - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5222:                               '</span></div>';
 5223:                 } else {
 5224:                     $extra .= '<span class="LC_nobreak">'.
 5225:                               &mt('Key required').' - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5226:                               '</span>'."\n";
 5227:                 }
 5228:             } elsif (exists($privkeys->{$hostid})) {
 5229:                 $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" /><span class="LC_nobreak">'.
 5230:                           &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5231:                           '<span class="LC_nobreak">'.&mt('Change?').
 5232:                           '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5233:                           ('&nbsp;'x2).
 5234:                           '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5235:                           '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5236:                           '<span class="LC_nobreak">'.&mt('New Key').':'.
 5237:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5238:                           '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.'.$context.'_privkey_'.$hostid.'.type='."'text'".' } else { this.form.'.$context.'_privkey_'.$hostid.'.type='."'password'".' }" />'.&mt('Visible input').'</label>'.
 5239:                           '</span></div>';
 5240:             } else {
 5241:                 $extra .= '<span class="LC_nobreak">'.&mt('Encryption Key').':'.
 5242:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5243:                           '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.'.$context.'_privkey_'.$hostid.'.type='."'text'".' } else { this.form.'.$context.'_privkey_'.$hostid.'.type='."'password'".' }" />'.&mt('Visible input').'</label>';
 5244:             }
 5245:             $extra .= '</fieldset>';
 5246:         }
 5247:     }
 5248:     my (%choices,@toggles,%defaultchecked);
 5249:     if ($context eq 'ltisec') {
 5250:         %choices = &Apache::lonlocal::texthash (
 5251:                                                   ltisec_crslinkprot => 'Encrypt stored link protection secrets defined in courses',
 5252:                                                   ltisec_domlinkprot => 'Encrypt stored link protection secrets defined in domain',
 5253:                                                   ltisec_consumers   => 'Encrypt stored consumer secrets defined in domain',
 5254:                                                );
 5255:         @toggles = qw(ltisec_crslinkprot ltisec_domlinkprot ltisec_consumers);
 5256:         %defaultchecked = (
 5257:                            'ltisec_crslinkprot' => 'off',
 5258:                            'ltisec_domlinkprot' => 'off',
 5259:                            'ltisec_consumers'   => 'off',
 5260:                           );
 5261:     } else {
 5262:         %choices = &Apache::lonlocal::texthash (
 5263:                                                   toolsec_crs => 'Encrypt stored external tool secrets defined in courses',
 5264:                                                   toolsec_dom => 'Encrypt stored external tool secrets defined in domain',
 5265:                                                );
 5266:         @toggles = qw(toolsec_crs toolsec_dom);
 5267:         %defaultchecked = (
 5268:                            'toolsec_crs' => 'off',
 5269:                            'toolsec_dom' => 'off',
 5270:                           );
 5271:     }
 5272:     my ($onclick,$itemcount);
 5273:     $onclick = 'javascript:toggleLTIEncKey(this.form,'."'$context'".');';
 5274:     ($output,$itemcount) = &radiobutton_prefs($encrypt,\@toggles,\%defaultchecked,
 5275:                                               \%choices,$itemcount,$onclick,'','left','no');
 5276: 
 5277:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5278:     my $noprivkeysty = 'display:inline-block';
 5279:     if ($numshown) {
 5280:         $noprivkeysty = 'display:none';
 5281:     }
 5282:     $output .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.&mt('Encryption Key(s)').'</td>'.
 5283:                '<td><div id="'.$context.'_noprivkey" style="'.$noprivkeysty.'" >'.
 5284:                '<span class="LC_nobreak">'.&mt('Not in use').'</span></div>'.
 5285:                $extra.
 5286:                '</td></tr>';
 5287:     $itemcount ++;
 5288:     $$rowtotal += $itemcount;
 5289:     return $output;
 5290: }
 5291: 
 5292: sub print_lti {
 5293:     my ($position,$dom,$settings,$rowtotal) = @_;
 5294:     my $itemcount = 1;
 5295:     my ($datatable,$css_class);
 5296:     my (%rules,%encrypt,%privkeys,%linkprot,%suggestions);
 5297:     if (ref($settings) eq 'HASH') {
 5298:         if ($position eq 'top') {
 5299:             if (exists($settings->{'encrypt'})) {
 5300:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 5301:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 5302:                         if ($key eq 'consumers') {
 5303:                             $encrypt{'ltisec_'.$key} = $settings->{'encrypt'}{$key};
 5304:                         } else {
 5305:                             $encrypt{'ltisec_'.$key.'linkprot'} = $settings->{'encrypt'}{$key};
 5306:                         }
 5307:                     }
 5308:                 }
 5309:             }
 5310:             if (exists($settings->{'private'})) {
 5311:                 if (ref($settings->{'private'}) eq 'HASH') {
 5312:                     if (ref($settings->{'private'}) eq 'HASH') {
 5313:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 5314:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 5315:                         }
 5316:                     }
 5317:                 }
 5318:             }
 5319:         } elsif ($position eq 'upper') {
 5320:             if (exists($settings->{'rules'})) {
 5321:                 if (ref($settings->{'rules'}) eq 'HASH') {
 5322:                     %rules = %{$settings->{'rules'}};
 5323:                 }
 5324:             }
 5325:         } elsif ($position eq 'middle') {
 5326:             if (exists($settings->{'suggested'})) {
 5327:                 if (ref($settings->{'suggested'}) eq 'HASH') {
 5328:                     %suggestions = %{$settings->{'suggested'}};
 5329:                 }
 5330:             }
 5331:         } elsif ($position eq 'lower') {
 5332:             if (exists($settings->{'linkprot'})) {
 5333:                 if (ref($settings->{'linkprot'}) eq 'HASH') {
 5334:                     %linkprot = %{$settings->{'linkprot'}};
 5335:                     if ($linkprot{'lock'}) {
 5336:                         delete($linkprot{'lock'});
 5337:                     }
 5338:                 }
 5339:             }
 5340:         } else {
 5341:             foreach my $key ('encrypt','private','rules','linkprot','suggestions') {
 5342:                 if (exists($settings->{$key})) {
 5343:                     delete($settings->{$key});
 5344:                 }
 5345:             }
 5346:         }
 5347:     }
 5348:     if ($position eq 'top') {
 5349:         $datatable = &secrets_form($dom,'ltisec',\%encrypt,\%privkeys,$rowtotal);
 5350:     } elsif ($position eq 'upper') {
 5351:         $datatable = &password_rules('ltisecrets',\$itemcount,\%rules);
 5352:         $$rowtotal += $itemcount;
 5353:     } elsif ($position eq 'middle') {
 5354:         $datatable = &linkprot_suggestions(\%suggestions,\$itemcount);
 5355:         $$rowtotal += $itemcount;
 5356:     } elsif ($position eq 'lower') {
 5357:         $datatable .= &Apache::courseprefs::print_linkprotection($dom,'',$settings,$rowtotal,'','','domain');
 5358:     } else {
 5359:         my ($switchserver,$switchmessage);
 5360:         $switchserver = &check_switchserver($dom);
 5361:         $switchmessage = &mt("submit from domain's primary library server: [_1].",$switchserver);
 5362:         my $maxnum = 0;
 5363:         my %ordered;
 5364:         if (ref($settings) eq 'HASH') {
 5365:             foreach my $item (keys(%{$settings})) {
 5366:                 if (ref($settings->{$item}) eq 'HASH') {
 5367:                     my $num = $settings->{$item}{'order'};
 5368:                     if ($num eq '') {
 5369:                         $num = scalar(keys(%{$settings}));
 5370:                     }
 5371:                     $ordered{$num} = $item;
 5372:                 }
 5373:             }
 5374:         }
 5375:         $maxnum = scalar(keys(%ordered));
 5376:         my %lt = &lti_names();
 5377:         if (keys(%ordered)) {
 5378:             my @items = sort { $a <=> $b } keys(%ordered);
 5379:             for (my $i=0; $i<@items; $i++) {
 5380:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5381:                 my $item = $ordered{$items[$i]};
 5382:                 my ($key,$secret,$usable,$lifetime,$consumer,$requser,$crsinc,$current);
 5383:                 if (ref($settings->{$item}) eq 'HASH') {
 5384:                     $key = $settings->{$item}->{'key'};
 5385:                     $usable = $settings->{$item}->{'usable'};
 5386:                     $lifetime = $settings->{$item}->{'lifetime'};
 5387:                     $consumer = $settings->{$item}->{'consumer'};
 5388:                     $requser = $settings->{$item}->{'requser'};
 5389:                     $crsinc = $settings->{$item}->{'crsinc'};
 5390:                     $current = $settings->{$item};
 5391:                 }
 5392:                 my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 5393:                 my %checkedrequser = (
 5394:                                        yes => ' checked="checked"',
 5395:                                        no  => '',
 5396:                                      );
 5397:                 if (!$requser) {
 5398:                     $checkedrequser{'no'} = $checkedrequser{'yes'};
 5399:                     $checkedrequser{'yes'} = '';
 5400:                 }
 5401:                 my $onclickcrsinc = ' onclick="toggleLTI(this.form,'."'crsinc','$i'".');"';
 5402:                 my %checkedcrsinc = (
 5403:                                       yes => ' checked="checked"',
 5404:                                       no  => '',
 5405:                                     );
 5406:                 if (!$crsinc) {
 5407:                     $checkedcrsinc{'no'} = $checkedcrsinc{'yes'};
 5408:                     $checkedcrsinc{'yes'} = '';
 5409:                 }
 5410:                 my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 5411:                 $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5412:                              .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 5413:                 for (my $k=0; $k<=$maxnum; $k++) {
 5414:                     my $vpos = $k+1;
 5415:                     my $selstr;
 5416:                     if ($k == $i) {
 5417:                         $selstr = ' selected="selected" ';
 5418:                     }
 5419:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5420:                 }
 5421:                 $datatable .= '</select>'.('&nbsp;'x2).
 5422:                     '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 5423:                     &mt('Delete?').'</label></span></td>'.
 5424:                     '<td colspan="2">'.
 5425:                     '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5426:                     '<span class="LC_nobreak">'.$lt{'consumer'}.
 5427:                     ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 5428:                     ('&nbsp;'x2).
 5429:                     '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 5430:                     '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 5431:                     ('&nbsp;'x2).
 5432:                     '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 5433:                     'value="'.$lifetime.'" size="3" /></span><br /><br />';
 5434:                 if ($key ne '') {
 5435:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'};
 5436:                     if ($switchserver) {
 5437:                         $datatable .= ': ['.&mt('[_1] to view/edit',$switchserver).']';
 5438:                     } else {
 5439:                         $datatable .= ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />';
 5440:                     }
 5441:                     $datatable .= '</span> '.('&nbsp;'x2);
 5442:                 } elsif (!$switchserver) {
 5443:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':'.
 5444:                                   '<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />'.
 5445:                                   '</span> '.('&nbsp;'x2);
 5446:                 }
 5447:                 if ($switchserver) {
 5448:                     if ($usable ne '') {
 5449:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 5450:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5451:                                       '<span class="LC_nobreak">'.&mt('Change secret?').
 5452:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 5453:                                       ('&nbsp;'x2).
 5454:                                      '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').'</label>'.('&nbsp;'x2).
 5455:                                       '</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 5456:                                       '<span class="LC_nobreak"> - '.$switchmessage.'</span>'.
 5457:                                       '</div>';
 5458:                     } elsif ($key eq '') {
 5459:                         $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 5460:                     } else {
 5461:                         $datatable .= '<span class="LC_nobreak">'.&mt('Secret required').' - '.$switchmessage.'</span>'."\n";
 5462:                     }
 5463:                 } else {
 5464:                     if ($usable ne '') {
 5465:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 5466:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5467:                                       '<span class="LC_nobreak">'.&mt('Change?').
 5468:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 5469:                                       ('&nbsp;'x2).
 5470:                                       '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').
 5471:                                       '</label>&nbsp;&nbsp;</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 5472:                                       '<span class="LC_nobreak">'.&mt('New Secret').':'.
 5473:                                       '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 5474:                                       '<label><input type="checkbox" name="lti_visible_'.$i.'" id="lti_visible_'.$i.'" onclick="if (this.checked) { this.form.lti_secret_'.$i.'.type='."'text'".' } else { this.form.lti_secret_'.$i.'.type='."'password'".' }" />'.&mt('Visible input').'</label></span></div>';
 5475:                     } else {
 5476:                         $datatable .=
 5477:                             '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 5478:                             '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 5479:                             '<label><input type="checkbox" name="lti_visible_'.$i.'" id="lti_visible_'.$i.'" onclick="if (this.checked) { this.form.lti_secret_'.$i.'.type='."'text'".' } else { this.form.lti_secret_'.$i.'.type='."'password'".' }" />'.&mt('Visible input').'</label>';
 5480:                     }
 5481:                 }
 5482:                 $datatable .= '<br /><br />'.
 5483:                     '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 5484:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5485:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 5486:                     '<br /><br />'.
 5487:                     '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 5488:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="1"'.$onclickcrsinc.$checkedcrsinc{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5489:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="0"'.$onclickcrsinc.$checkedcrsinc{no}.' />'.&mt('No').'</label></span>'."\n".
 5490:                     ('&nbsp;'x4).
 5491:                     '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 5492:                     '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 5493:                 $itemcount ++;
 5494:             }
 5495:         }
 5496:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5497:         my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 5498:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 5499:                       '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 5500:                       '<select name="lti_pos_add"'.$chgstr.'>';
 5501:         for (my $k=0; $k<$maxnum+1; $k++) {
 5502:             my $vpos = $k+1;
 5503:             my $selstr;
 5504:             if ($k == $maxnum) {
 5505:                 $selstr = ' selected="selected" ';
 5506:             }
 5507:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5508:         }
 5509:         $datatable .= '</select>&nbsp;'."\n".
 5510:                       '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 5511:                       '<td colspan="2">'.
 5512:                       '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5513:                       '<span class="LC_nobreak">'.$lt{'consumer'}.
 5514:                       ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 5515:                       ('&nbsp;'x2).
 5516:                       '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 5517:                       '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 5518:                       ('&nbsp;'x2).
 5519:                       '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span><br /><br />'."\n";
 5520:         if ($switchserver) {
 5521:             $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 5522:         } else {
 5523:             $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" autocomplete="off" /></span> '."\n".
 5524:                           ('&nbsp;'x2).
 5525:                           '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" autocomplete="new-password" />'.
 5526:                           '<label><input type="checkbox" name="lti_add_visible" id="lti_add_visible" onclick="if (this.checked) { this.form.lti_secret_add.type='."'text'".' } else { this.form.lti_secret_add.type='."'password'".' }" />'.&mt('Visible input').'</label></span> '."\n";
 5527:         }
 5528:         $datatable .= '<br /><br />'.
 5529:                       '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 5530:                       '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 5531:                       '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 5532:                       '<br /><br />'.
 5533:                       '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 5534:                       '<label><input type="radio" name="lti_crsinc_add" value="1" onclick="toggleLTI(this.form,'."'crsinc','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 5535:                       '<label><input type="radio" name="lti_crsinc_add" value="0" onclick="toggleLTI(this.form,'."'crsinc','add'".');" />'.&mt('No').'</label></span>'."\n".
 5536:                       '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 5537:                       '</td>'."\n".
 5538:                       '</tr>'."\n";
 5539:         $itemcount ++;
 5540:     }
 5541:     $$rowtotal += $itemcount;
 5542:     return $datatable;
 5543: }
 5544: 
 5545: sub lti_names {
 5546:     my %lt = &Apache::lonlocal::texthash(
 5547:                                           'version'   => 'LTI Version',
 5548:                                           'url'       => 'URL',
 5549:                                           'key'       => 'Key',
 5550:                                           'lifetime'  => 'Nonce lifetime (s)',
 5551:                                           'consumer'  => 'Consumer',
 5552:                                           'secret'    => 'Secret',
 5553:                                           'requser'   => "User's identity sent",
 5554:                                           'crsinc'    => "Course's identity sent",
 5555:                                           'email'     => 'Email address',
 5556:                                           'sourcedid' => 'User ID',
 5557:                                           'other'     => 'Other',
 5558:                                           'passback'  => 'Can return grades to Consumer:',
 5559:                                           'roster'    => 'Can retrieve roster from Consumer:',
 5560:                                           'topmenu'   => 'Display LON-CAPA page header',
 5561:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 5562:                                         );
 5563:     return %lt;
 5564: }
 5565: 
 5566: sub lti_options {
 5567:     my ($num,$current,$itemcount,%lt) = @_;
 5568:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 5569:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 5570:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 5571:     $checked{'storecrs'}{'Y'} = ' checked="checked"';
 5572:     $checked{'makecrs'}{'N'} = ' checked="checked"';
 5573:     $checked{'mapcrstype'} = {};
 5574:     $checked{'makeuser'} = {};
 5575:     $checked{'selfenroll'} = {};
 5576:     $checked{'crssec'} = {};
 5577:     $checked{'crssecsrc'} = {};
 5578:     $checked{'lcauth'} = {};
 5579:     $checked{'menuitem'} = {};
 5580:     if ($num eq 'add') {
 5581:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 5582:     }
 5583:     my $userfieldsty = 'none';
 5584:     my $crsfieldsty = 'none';
 5585:     my $crssecfieldsty = 'none';
 5586:     my $secsrcfieldsty = 'none';
 5587:     my $callbacksty = 'none';
 5588:     my $passbacksty = 'none';
 5589:     my $optionsty = 'block';
 5590:     my $crssty = 'block';
 5591:     my $lcauthparm;
 5592:     my $lcauthparmstyle = 'display:none';
 5593:     my $lcauthparmtext;
 5594:     my $menusty;
 5595:     my $numinrow = 4;
 5596:     my %menutitles = &ltimenu_titles();
 5597: 
 5598:     if (ref($current) eq 'HASH') {
 5599:         if (!$current->{'requser'}) {
 5600:             $optionsty = 'none';
 5601:             $crssty = 'none';
 5602:         } elsif (!$current->{'crsinc'}) {
 5603:             $crssty = 'none';
 5604:         }
 5605:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 5606:             $checked{'mapuser'}{'sourcedid'} = '';
 5607:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 5608:                 $checked{'mapuser'}{'email'} = ' checked="checked"';
 5609:             } else {
 5610:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 5611:                 $userfield = $current->{'mapuser'};
 5612:                 $userfieldsty = 'inline-block';
 5613:             }
 5614:         }
 5615:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 5616:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 5617:             if ($current->{'mapcrs'} eq 'context_id') {
 5618:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"';
 5619:             } else {
 5620:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 5621:                 $cidfield = $current->{'mapcrs'};
 5622:                 $crsfieldsty = 'inline-block';
 5623:             }
 5624:         }
 5625:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 5626:             foreach my $type (@{$current->{'mapcrstype'}}) {
 5627:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 5628:             }
 5629:         }
 5630:         if (!$current->{'storecrs'}) {
 5631:             $checked{'storecrs'}{'N'} = $checked{'storecrs'}{'Y'};
 5632:             $checked{'storecrs'}{'Y'} = '';
 5633:         }
 5634:         if ($current->{'makecrs'}) {
 5635:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 5636:         }
 5637:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 5638:             foreach my $role (@{$current->{'makeuser'}}) {
 5639:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 5640:             }
 5641:         }
 5642:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 5643:             $checked{'lcauth'}{$1} = ' checked="checked"';
 5644:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 5645:                 $lcauthparm = $current->{'lcauthparm'};
 5646:                 $lcauthparmstyle = 'display:table-row';
 5647:                 if ($current->{'lcauth'} eq 'localauth') {
 5648:                     $lcauthparmtext = &mt('Local auth argument');
 5649:                 } else {
 5650:                     $lcauthparmtext = &mt('Kerberos domain');
 5651:                 }
 5652:             }
 5653:         }
 5654:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 5655:             foreach my $role (@{$current->{'selfenroll'}}) {
 5656:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 5657:             }
 5658:         }
 5659:         if (ref($current->{'maproles'}) eq 'HASH') {
 5660:             %rolemaps = %{$current->{'maproles'}};
 5661:         }
 5662:         if ($current->{'section'} ne '') {
 5663:             $checked{'crssec'}{'Y'} = '  checked="checked"';
 5664:             $crssecfieldsty = 'inline-block';
 5665:             if ($current->{'section'} eq 'course_section_sourcedid') {
 5666:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 5667:             } else {
 5668:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 5669:                 $crssecsrc = $current->{'section'};
 5670:                 $secsrcfieldsty = 'inline-block';
 5671:             }
 5672:         } else {
 5673:             $checked{'crssec'}{'N'} = ' checked="checked"';
 5674:         }
 5675:         if ($current->{'callback'} ne '') {
 5676:             $callback = $current->{'callback'};
 5677:             $checked{'callback'}{'Y'} = ' checked="checked"';
 5678:             $callbacksty = 'inline-block';
 5679:         } else {
 5680:             $checked{'callback'}{'N'} = ' checked="checked"';
 5681:         }
 5682:         if ($current->{'topmenu'}) {
 5683:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 5684:         } else {
 5685:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 5686:         }
 5687:         if ($current->{'inlinemenu'}) {
 5688:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 5689:         } else {
 5690:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 5691:         }
 5692:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 5693:             $menusty = 'inline-block';
 5694:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 5695:                 foreach my $item (@{$current->{'lcmenu'}}) {
 5696:                     if (exists($menutitles{$item})) {
 5697:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 5698:                     }
 5699:                 }
 5700:             }
 5701:         } else {
 5702:             $menusty = 'none';
 5703:         }
 5704:     } else {
 5705:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 5706:         $checked{'crssec'}{'N'} = ' checked="checked"';
 5707:         $checked{'callback'}{'N'} = ' checked="checked"';
 5708:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 5709:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 5710:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 5711:         $menusty = 'inline-block';
 5712:     }
 5713:     my @coursetypes = ('official','unofficial','community','textbook','lti');
 5714:     my %coursetypetitles = &Apache::lonlocal::texthash (
 5715:                                official   => 'Official',
 5716:                                unofficial => 'Unofficial',
 5717:                                community  => 'Community',
 5718:                                textbook   => 'Textbook',
 5719:                                lti        => 'LTI Provider',
 5720:     );
 5721:     my @authtypes = ('internal','krb4','krb5','localauth');
 5722:     my %shortauth = (
 5723:                      internal => 'int',
 5724:                      krb4 => 'krb4',
 5725:                      krb5 => 'krb5',
 5726:                      localauth  => 'loc'
 5727:                     );
 5728:     my %authnames = &authtype_names();
 5729:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 5730:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 5731:     my @courseroles = ('cc','in','ta','ep','st');
 5732:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 5733:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 5734:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 5735:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 5736:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 5737:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 5738:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 5739:     my $output = '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Logout options').'</legend>'.
 5740:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback to logout LON-CAPA on log out from Consumer').':&nbsp;'.
 5741:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 5742:                  $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5743:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 5744:                  $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 5745:                  '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 5746:                  '<span class="LC_nobreak">'.&mt('Parameter').': '.
 5747:                  '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 5748:                  '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 5749:                  '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 5750:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 5751:     foreach my $option ('sourcedid','email','other') {
 5752:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 5753:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 5754:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5755:     }
 5756:     $output .= '</span></div>'.
 5757:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 5758:                '<input type="text" name="lti_customuser_'.$num.'" '.
 5759:                'value="'.$userfield.'" /></div></fieldset>'.
 5760:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 5761:     foreach my $ltirole (@ltiroles) {
 5762:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 5763:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';
 5764:     }
 5765:     $output .= '</fieldset>'.
 5766:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 5767:                '<table>'.
 5768:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 5769:                '</table>'.
 5770:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 5771:                '<td class="LC_left_item">';
 5772:     foreach my $auth ('lti',@authtypes) {
 5773:         my $authtext;
 5774:         if ($auth eq 'lti') {
 5775:             $authtext = &mt('None');
 5776:         } else {
 5777:             $authtext = $authnames{$shortauth{$auth}};
 5778:         }
 5779:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 5780:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 5781:                    $authtext.'</label></span> &nbsp;';
 5782:     }
 5783:     $output .= '</td></tr>'.
 5784:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 5785:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 5786:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 5787:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 5788:                '</table></fieldset>'.
 5789:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.
 5790:                &mt('LON-CAPA menu items (Course Coordinator can override)').'</legend>'.
 5791:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 5792:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 5793:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5794:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 5795:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 5796:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5797:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 5798:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 5799:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5800:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 5801:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 5802:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5803:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 5804:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 5805:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 5806:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 5807:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 5808:                    ('&nbsp;'x2);
 5809:     }
 5810:     $output .= '</span></div></fieldset>'.
 5811:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 5812:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 5813:                &mt('Unique course identifier').':&nbsp;';
 5814:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 5815:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 5816:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 5817:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5818:     }
 5819:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 5820:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 5821:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5822:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 5823:     foreach my $type (@coursetypes) {
 5824:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 5825:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 5826:                    ('&nbsp;'x2);
 5827:     }
 5828:     $output .= '</span><br /><br />'.
 5829:                '<span class="LC_nobreak">'.&mt('Store mapping of course identifier to LON-CAPA CourseID').':&nbsp;'.
 5830:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="0"'.
 5831:                $checked{'storecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5832:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="1"'.
 5833:                $checked{'storecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 5834:                '</fieldset>'.
 5835:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 5836:     foreach my $ltirole (@lticourseroles) {
 5837:         my ($selected,$selectnone);
 5838:         if ($rolemaps{$ltirole} eq '') {
 5839:             $selectnone = ' selected="selected"';
 5840:         }
 5841:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 5842:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 5843:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5844:         foreach my $role (@courseroles) {
 5845:             unless ($selectnone) {
 5846:                 if ($rolemaps{$ltirole} eq $role) {
 5847:                     $selected = ' selected="selected"';
 5848:                 } else {
 5849:                     $selected = '';
 5850:                 }
 5851:             }
 5852:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 5853:                        &Apache::lonnet::plaintext($role,'Course').
 5854:                        '</option>';
 5855:         }
 5856:         $output .= '</select></td>';
 5857:     }
 5858:     $output .= '</tr></table></fieldset>'.
 5859:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Creating courses').'</legend>'.
 5860:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 5861:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 5862:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5863:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 5864:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 5865:                '</fieldset>'.
 5866:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 5867:     foreach my $lticrsrole (@lticourseroles) {
 5868:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 5869:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 5870:     }
 5871:     $output .= '</fieldset>'.
 5872:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Course options').'</legend>'.
 5873:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 5874:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 5875:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5876:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 5877:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 5878:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 5879:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 5880:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 5881:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 5882:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 5883:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 5884:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 5885:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 5886:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 5887:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5888:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 5889:     foreach my $extra ('roster','passback') {
 5890:         my $checkedon = '';
 5891:         my $checkedoff = ' checked="checked"';
 5892:         if ($extra eq 'passback') {
 5893:             $pb1p1chk = ' checked="checked"';
 5894:             $pb1p0chk = '';
 5895:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"';
 5896:         } else {
 5897:             $onclickpb = '';
 5898:         }
 5899:         if (ref($current) eq 'HASH') {
 5900:             if (($current->{$extra})) {
 5901:                 $checkedon = $checkedoff;
 5902:                 $checkedoff = '';
 5903:                 if ($extra eq 'passback') {
 5904:                     $passbacksty = 'inline-block';
 5905:                 }
 5906:                 if ($current->{'passbackformat'} eq '1.0') {
 5907:                     $pb1p0chk =  ' checked="checked"';
 5908:                     $pb1p1chk = '';
 5909:                 }
 5910:             }
 5911:         }
 5912:         $output .= $lt{$extra}.'&nbsp;'.
 5913:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 5914:                    &mt('No').'</label>'.('&nbsp;'x2).
 5915:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 5916:                    &mt('Yes').'</label><br />';
 5917:     }
 5918:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 5919:                '<span class="LC_nobreak">'.&mt('Grade format').
 5920:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 5921:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 5922:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 5923:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 5924:                '<div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>';
 5925:     $output .= '</span></div></fieldset>';
 5926: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 5927: #
 5928: #    $output .= '</fieldset>'.
 5929: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 5930:     return $output;
 5931: }
 5932: 
 5933: sub ltimenu_titles {
 5934:     return &Apache::lonlocal::texthash(
 5935:                                         fullname    => 'Full name',
 5936:                                         coursetitle => 'Course title',
 5937:                                         role        => 'Role',
 5938:                                         logout      => 'Logout',
 5939:                                         grades      => 'Grades',
 5940:     );
 5941: }
 5942: 
 5943: sub linkprot_suggestions {
 5944:     my ($suggested,$itemcount) = @_;
 5945:     my $count = 0;
 5946:     my $next = 1;
 5947:     my %lt = &Apache::lonlocal::texthash(
 5948:                                           'name' => 'Suggested Launcher',
 5949:                                           'info' => 'Recommendations',
 5950:                                         );
 5951:     my ($datatable,$css_class,$dest);
 5952:     if (ref($suggested) eq 'HASH') {
 5953:         my @current = sort { $a <=> $b } keys(%{$suggested});
 5954:         $next += $current[-1];
 5955:         for (my $i=0; $i<@current; $i++) {
 5956:             my $num = $current[$i];
 5957:             my %values;
 5958:             if (ref($suggested->{$num}) eq 'HASH') {
 5959:                 %values = %{$suggested->{$num}};
 5960:             } else {
 5961:                 next;
 5962:             }
 5963:             $css_class = $$itemcount%2?' class="LC_odd_row"':'';
 5964:             $datatable .=
 5965:                 '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 5966:                 '<label><input type="checkbox" name="linkprot_suggested_del" value="'.$i.'" />'."\n".
 5967:                 &mt('Delete?').'</label></span></td><td>'."\n".
 5968:                 '<div class="LC_floatleft"><fieldset><legend>'.$lt{'name'}.'</legend>'."\n".
 5969:                 '<input type="text" size="15" name="linkprot_suggested_name_'.$i.'" value="'.$values{'name'}.'" autocomplete="off" />'."\n".
 5970:                 '</fieldset></div>'.
 5971:                 '<div class="LC_floatleft"><fieldset><legend>'.$lt{'info'}.'</legend>'."\n".
 5972:                 '<textarea cols="55" rows="5" name="linkprot_suggested_info_'.$i.'">'.$values{'info'}.'</textarea>'.
 5973:                 '</fieldset></div>'.
 5974:                 '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
 5975:                 '<input type="hidden" name="linkprot_suggested_id_'.$i.'" value="'.$num.'" /></td></tr>'."\n";
 5976:             $$itemcount ++;
 5977:         }
 5978:     }
 5979:     $css_class = $$itemcount%2?' class="LC_odd_row"':'';
 5980:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 5981:                   '<input type="hidden" name="linkprot_suggested_maxnum" value="'.$next.'" />'."\n".
 5982:                   '<input type="checkbox" name="linkprot_suggested_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 5983:                   '<td>'."\n".
 5984:                   '<div class="LC_floatleft"><fieldset><legend>'.$lt{'name'}.'</legend>'."\n".
 5985:                   '<input type="text" size="15" name="linkprot_suggested_name_add" value="" autocomplete="off" />'."\n".
 5986:                   '</fieldset></div>'.
 5987:                   '<div class="LC_floatleft"><fieldset><legend>'.$lt{'info'}.'</legend>'."\n".
 5988:                   '<textarea cols="55" rows="5" name="linkprot_suggested_info_add"></textarea>'.
 5989:                   '</fieldset></div>'.
 5990:                   '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
 5991:                   '</td></tr>'."\n";
 5992:     return $datatable;
 5993: }
 5994: 
 5995: sub print_coursedefaults {
 5996:     my ($position,$dom,$settings,$rowtotal) = @_;
 5997:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 5998:     my $itemcount = 1;
 5999:     my %choices =  &Apache::lonlocal::texthash (
 6000:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6001:         coursequota          => 'Default cumulative quota for all group portfolio spaces in course',
 6002:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6003:         coursecredits        => 'Credits can be specified for courses',
 6004:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6005:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6006:         inline_chem          => 'Use inline previewer for chemical reaction response in place of pop-up',
 6007:         texengine            => 'Default method to display mathematics',
 6008:         postsubmit           => 'Disable submit button/keypress following student submission',
 6009:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6010:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6011:         ltiauth              => 'Student username in LTI launch of deep-linked URL can be accepted without re-authentication',
 6012:         domexttool           => 'External Tools defined in the domain may be used in courses/communities (by type)',
 6013:         exttool              => 'External Tools can be defined and configured in courses/communities (by type)',
 6014:     );
 6015:     my %staticdefaults = (
 6016:                            anonsurvey_threshold => 10,
 6017:                            uploadquota          => 500,
 6018:                            coursequota          => 20,
 6019:                            postsubmit           => 60,
 6020:                            mysqltables          => 172800,
 6021:                            domexttool           => 1,
 6022:                            exttool              => 0,
 6023:                          );
 6024:     if ($position eq 'top') {
 6025:         %defaultchecked = (
 6026:                             'uselcmath'       => 'on',
 6027:                             'usejsme'         => 'on',
 6028:                             'inline_chem'     => 'on',
 6029:                             'canclone'        => 'none',
 6030:                           );
 6031:         @toggles = ('uselcmath','usejsme','inline_chem');
 6032:         my $deftex = $Apache::lonnet::deftex;
 6033:         if (ref($settings) eq 'HASH') {
 6034:             if ($settings->{'texengine'}) {
 6035:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6036:                     $deftex = $settings->{'texengine'};
 6037:                 }
 6038:             }
 6039:         }
 6040:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6041:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6042:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6043:                        '</span></td><td class="LC_right_item">'.
 6044:                        '<select name="texengine">'."\n";
 6045:         my %texoptions = (
 6046:                             MathJax  => 'MathJax',
 6047:                             mimetex  => &mt('Convert to Images'),
 6048:                             tth      => &mt('TeX to HTML'),
 6049:                          );
 6050:         foreach my $renderer ('MathJax','mimetex','tth') {
 6051:             my $selected = '';
 6052:             if ($renderer eq $deftex) {
 6053:                 $selected = ' selected="selected"';
 6054:             }
 6055:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6056:         }
 6057:         $mathdisp .= '</select></td></tr>'."\n";
 6058:         $itemcount ++;
 6059:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6060:                                                      \%choices,$itemcount);
 6061:         $datatable = $mathdisp.$datatable;
 6062:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6063:         $datatable .=
 6064:             '<tr'.$css_class.'><td valign="top">'.
 6065:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6066:             '</span></td><td class="LC_left_item">';
 6067:         my $currcanclone = 'none';
 6068:         my $onclick;
 6069:         my @cloneoptions = ('none','domain');
 6070:         my %clonetitles = &Apache::lonlocal::texthash (
 6071:                              none     => 'No additional course requesters',
 6072:                              domain   => "Any course requester in course's domain",
 6073:                              instcode => 'Course requests for official courses ...',
 6074:                           );
 6075:         my (%codedefaults,@code_order,@posscodes);
 6076:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6077:                                                     \@code_order) eq 'ok') {
 6078:             if (@code_order > 0) {
 6079:                 push(@cloneoptions,'instcode');
 6080:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6081:             }
 6082:         }
 6083:         if (ref($settings) eq 'HASH') {
 6084:             if ($settings->{'canclone'}) {
 6085:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6086:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6087:                         if (@code_order > 0) {
 6088:                             $currcanclone = 'instcode';
 6089:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 6090:                         }
 6091:                     }
 6092:                 } elsif ($settings->{'canclone'} eq 'domain') {
 6093:                     $currcanclone = $settings->{'canclone'};
 6094:                 }
 6095:             }
 6096:         }
 6097:         foreach my $option (@cloneoptions) {
 6098:             my ($checked,$additional);
 6099:             if ($currcanclone eq $option) {
 6100:                 $checked = ' checked="checked"';
 6101:             }
 6102:             if ($option eq 'instcode') {
 6103:                 if (@code_order) {
 6104:                     my $show = 'none';
 6105:                     if ($checked) {
 6106:                         $show = 'block';
 6107:                     }
 6108:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 6109:                                   &mt('Institutional codes for new and cloned course have identical:').
 6110:                                   '<br />';
 6111:                     foreach my $item (@code_order) {
 6112:                         my $codechk;
 6113:                         if ($checked) {
 6114:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 6115:                                 $codechk = ' checked="checked"';
 6116:                             }
 6117:                         }
 6118:                         $additional .= '<label>'.
 6119:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 6120:                                        $item.'</label>';
 6121:                     }
 6122:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 6123:                 }
 6124:             }
 6125:             $datatable .=
 6126:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 6127:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 6128:                 '</label>&nbsp;'.$additional.'</span><br />';
 6129:         }
 6130:         $datatable .= '</td>'.
 6131:                       '</tr>';
 6132:         $itemcount ++;
 6133:     } else {
 6134:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6135:         my ($currdefresponder,%defcredits,%curruploadquota,%currcoursequota,
 6136:             %deftimeout,%currmysql);
 6137:         my $currusecredits = 0;
 6138:         my $postsubmitclient = 1;
 6139:         my $ltiauth = 0;
 6140:         my %domexttool;
 6141:         my %exttool;
 6142:         my @types = ('official','unofficial','community','textbook');
 6143:         if (ref($settings) eq 'HASH') {
 6144:             if ($settings->{'ltiauth'}) {
 6145:                 $ltiauth = 1;
 6146:             }
 6147:             if (ref($settings->{'domexttool'}) eq 'HASH') {
 6148:                 foreach my $type (@types) {
 6149:                     if ($settings->{'domexttool'}->{$type}) {
 6150:                         $domexttool{$type} = ' checked="checked"';
 6151:                     }
 6152:                 }
 6153:             } else {
 6154:                 foreach my $type (@types) {
 6155:                     if ($staticdefaults{'domexttool'}) {
 6156:                         $domexttool{$type} = ' checked="checked"';
 6157:                     }
 6158:                 }
 6159:             }
 6160:             if (ref($settings->{'exttool'}) eq 'HASH') {
 6161:                 foreach my $type (@types) {
 6162:                     if ($settings->{'exttool'}->{$type}) {
 6163:                         $exttool{$type} = ' checked="checked"';
 6164:                     }
 6165:                 }
 6166:             }
 6167:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 6168:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 6169:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 6170:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 6171:                 }
 6172:             }
 6173:             if (ref($settings->{'coursequota'}) eq 'HASH') {
 6174:                 foreach my $type (keys(%{$settings->{'coursequota'}})) {
 6175:                     $currcoursequota{$type} = $settings->{'coursequota'}{$type};
 6176:                 }
 6177:             }
 6178:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 6179:                 foreach my $type (@types) {
 6180:                     next if ($type eq 'community');
 6181:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 6182:                     if ($defcredits{$type} ne '') {
 6183:                         $currusecredits = 1;
 6184:                     }
 6185:                 }
 6186:             }
 6187:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 6188:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 6189:                     $postsubmitclient = 0;
 6190:                     foreach my $type (@types) {
 6191:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6192:                     }
 6193:                 } else {
 6194:                     foreach my $type (@types) {
 6195:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 6196:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 6197:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 6198:                             } else {
 6199:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6200:                             }
 6201:                         } else {
 6202:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6203:                         }
 6204:                     }
 6205:                 }
 6206:             } else {
 6207:                 foreach my $type (@types) {
 6208:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6209:                 }
 6210:             }
 6211:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 6212:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 6213:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 6214:                 }
 6215:             } else {
 6216:                 foreach my $type (@types) {
 6217:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 6218:                 }
 6219:             }
 6220:         } else {
 6221:             foreach my $type (@types) {
 6222:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6223:                 if ($staticdefaults{'domexttool'}) {
 6224:                     $domexttool{$type} = ' checked="checked"';
 6225:                 }
 6226:             }
 6227:         }
 6228:         if (!$currdefresponder) {
 6229:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 6230:         } elsif ($currdefresponder < 1) {
 6231:             $currdefresponder = 1;
 6232:         }
 6233:         foreach my $type (@types) {
 6234:             if ($curruploadquota{$type} eq '') {
 6235:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 6236:             }
 6237:             if ($currcoursequota{$type} eq '') {
 6238:                 $currcoursequota{$type} = $staticdefaults{'coursequota'};
 6239:             }
 6240:         }
 6241:         $datatable .=
 6242:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6243:                 $choices{'anonsurvey_threshold'}.
 6244:                 '</span></td>'.
 6245:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 6246:                 '<input type="text" name="anonsurvey_threshold"'.
 6247:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 6248:                 '</td></tr>'."\n";
 6249:         $itemcount ++;
 6250:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6251:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6252:                       $choices{'uploadquota'}.
 6253:                       '</span></td>'.
 6254:                       '<td align="right" class="LC_right_item">'.
 6255:                       '<table><tr>';
 6256:         foreach my $type (@types) {
 6257:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 6258:                            '<input type="text" name="uploadquota_'.$type.'"'.
 6259:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 6260:         }
 6261:         $datatable .= '</tr></table></td></tr>'."\n";
 6262:         $itemcount ++;
 6263:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6264:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6265:                       $choices{'coursequota'}.
 6266:                       '</span></td>'.
 6267:                       '<td style="text-align: right" class="LC_right_item">'.
 6268:                       '<table><tr>';
 6269:         foreach my $type (@types) {
 6270:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6271:                            '<input type="text" name="coursequota_'.$type.'"'.
 6272:                            ' value="'.$currcoursequota{$type}.'" size="5" /></td>';
 6273:         }
 6274:         $datatable .= '</tr></table></td></tr>'."\n";
 6275:         $itemcount ++;
 6276:         my $onclick = "toggleDisplay(this.form,'credits');";
 6277:         my $display = 'none';
 6278:         if ($currusecredits) {
 6279:             $display = 'block';
 6280:         }
 6281:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 6282:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 6283:         foreach my $type (@types) {
 6284:             next if ($type eq 'community');
 6285:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 6286:                            '<input type="text" name="'.$type.'_credits"'.
 6287:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 6288:         }
 6289:         $additional .= '</tr></table></div>'."\n";
 6290:         %defaultchecked = ('coursecredits' => 'off');
 6291:         @toggles = ('coursecredits');
 6292:         my $current = {
 6293:                         'coursecredits' => $currusecredits,
 6294:                       };
 6295:         (my $table,$itemcount) =
 6296:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6297:                                \%choices,$itemcount,$onclick,$additional,'left');
 6298:         $datatable .= $table;
 6299:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 6300:         my $display = 'none';
 6301:         if ($postsubmitclient) {
 6302:             $display = 'block';
 6303:         }
 6304:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 6305:                       &mt('Number of seconds submit is disabled').'<br />'.
 6306:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 6307:                       '<table><tr>';
 6308:         foreach my $type (@types) {
 6309:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 6310:                            '<input type="text" name="'.$type.'_timeout" value="'.
 6311:                            $deftimeout{$type}.'" size="5" /></td>';
 6312:         }
 6313:         $additional .= '</tr></table></div>'."\n";
 6314:         %defaultchecked = ('postsubmit' => 'on');
 6315:         @toggles = ('postsubmit');
 6316:         $current = {
 6317:                        'postsubmit' => $postsubmitclient,
 6318:                    };
 6319:         ($table,$itemcount) =
 6320:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6321:                                \%choices,$itemcount,$onclick,$additional,'left');
 6322:         $datatable .= $table;
 6323:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6324:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6325:                       $choices{'mysqltables'}.
 6326:                       '</span></td>'.
 6327:                       '<td align="right" class="LC_right_item">'.
 6328:                       '<table><tr>';
 6329:         foreach my $type (@types) {
 6330:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 6331:                            '<input type="text" name="mysqltables_'.$type.'"'.
 6332:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 6333:         }
 6334:         $datatable .= '</tr></table></td></tr>'."\n";
 6335:         $itemcount ++;
 6336:         %defaultchecked = ('ltiauth' => 'off');
 6337:         @toggles = ('ltiauth');
 6338:         $current = {
 6339:                        'ltiauth' => $ltiauth,
 6340:                    };
 6341:         ($table,$itemcount) =
 6342:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6343:                                \%choices,$itemcount,undef,undef,'left');
 6344:         $datatable .= $table;
 6345:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6346:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6347:                       $choices{'domexttool'}.
 6348:                       '</span></td>'.
 6349:                       '<td style="text-align: right" class="LC_right_item">'.
 6350:                       '<table><tr>';
 6351:         foreach my $type (@types) {
 6352:             $datatable .= '<td style="text-align: left">'.
 6353:                           '<span class="LC_nobreak">'.
 6354:                           '<input type="checkbox" name="domexttool"'.
 6355:                           ' value="'.$type.'"'.$domexttool{$type}.' />'.
 6356:                           &mt($type).'</span></td>'."\n";
 6357:         }
 6358:         $datatable .= '</tr></table></td></tr>'."\n";
 6359:         $itemcount ++;
 6360:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6361:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6362:                       $choices{'exttool'}.
 6363:                       '</span></td>'.
 6364:                       '<td style="text-align: right" class="LC_right_item">'.
 6365:                       '<table><tr>';
 6366:         foreach my $type (@types) {
 6367:             $datatable .= '<td style="text-align: left">'.
 6368:                           '<span class="LC_nobreak">'.
 6369:                           '<input type="checkbox" name="exttool"'.
 6370:                           ' value="'.$type.'"'.$exttool{$type}.' />'.
 6371:                           &mt($type).'</span></td>'."\n";
 6372:         }
 6373:         $datatable .= '</tr></table></td></tr>'."\n";
 6374:     }
 6375:     $$rowtotal += $itemcount;
 6376:     return $datatable;
 6377: }
 6378: 
 6379: sub print_selfenrollment {
 6380:     my ($position,$dom,$settings,$rowtotal) = @_;
 6381:     my ($css_class,$datatable);
 6382:     my $itemcount = 1;
 6383:     my @types = ('official','unofficial','community','textbook');
 6384:     if (($position eq 'top') || ($position eq 'middle')) {
 6385:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 6386:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 6387:         my @rows;
 6388:         my $key;
 6389:         if ($position eq 'top') {
 6390:             $key = 'admin'; 
 6391:             if (ref($rowsref) eq 'ARRAY') {
 6392:                 @rows = @{$rowsref};
 6393:             }
 6394:         } elsif ($position eq 'middle') {
 6395:             $key = 'default';
 6396:             @rows = ('types','registered','approval','limit');
 6397:         }
 6398:         foreach my $row (@rows) {
 6399:             if (defined($titlesref->{$row})) {
 6400:                 $itemcount ++;
 6401:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6402:                 $datatable .= '<tr'.$css_class.'>'.
 6403:                               '<td>'.$titlesref->{$row}.'</td>'.
 6404:                               '<td class="LC_left_item">'.
 6405:                               '<table><tr>';
 6406:                 my (%current,%currentcap);
 6407:                 if (ref($settings) eq 'HASH') {
 6408:                     if (ref($settings->{$key}) eq 'HASH') {
 6409:                         foreach my $type (@types) {
 6410:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 6411:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 6412:                             }
 6413:                             if (($row eq 'limit') && ($key eq 'default')) {
 6414:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 6415:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 6416:                                 }
 6417:                             }
 6418:                         }
 6419:                     }
 6420:                 }
 6421:                 my %roles = (
 6422:                              '0' => &Apache::lonnet::plaintext('dc'),
 6423:                             ); 
 6424:             
 6425:                 foreach my $type (@types) {
 6426:                     unless (($row eq 'registered') && ($key eq 'default')) {
 6427:                         $datatable .= '<th>'.&mt($type).'</th>';
 6428:                     }
 6429:                 }
 6430:                 unless (($row eq 'registered') && ($key eq 'default')) {
 6431:                     $datatable .= '</tr><tr>';
 6432:                 }
 6433:                 foreach my $type (@types) {
 6434:                     if ($type eq 'community') {
 6435:                         $roles{'1'} = &mt('Community personnel');
 6436:                     } else {
 6437:                         $roles{'1'} = &mt('Course personnel');
 6438:                     }
 6439:                     $datatable .= '<td style="vertical-align: top">';
 6440:                     if ($position eq 'top') {
 6441:                         my %checked;
 6442:                         if ($current{$type} eq '0') {
 6443:                             $checked{'0'} = ' checked="checked"';
 6444:                         } else {
 6445:                             $checked{'1'} = ' checked="checked"';
 6446:                         }
 6447:                         foreach my $role ('1','0') {
 6448:                             $datatable .= '<span class="LC_nobreak"><label>'.
 6449:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 6450:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 6451:                                           $roles{$role}.'</label></span> ';
 6452:                         }
 6453:                     } else {
 6454:                         if ($row eq 'types') {
 6455:                             my %checked;
 6456:                             if ($current{$type} =~ /^(all|dom)$/) {
 6457:                                 $checked{$1} = ' checked="checked"';
 6458:                             } else {
 6459:                                 $checked{''} = ' checked="checked"';
 6460:                             }
 6461:                             foreach my $val ('','dom','all') {
 6462:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6463:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6464:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6465:                             }
 6466:                         } elsif ($row eq 'registered') {
 6467:                             my %checked;
 6468:                             if ($current{$type} eq '1') {
 6469:                                 $checked{'1'} = ' checked="checked"';
 6470:                             } else {
 6471:                                 $checked{'0'} = ' checked="checked"';
 6472:                             }
 6473:                             foreach my $val ('0','1') {
 6474:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6475:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6476:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6477:                             }
 6478:                         } elsif ($row eq 'approval') {
 6479:                             my %checked;
 6480:                             if ($current{$type} =~ /^([12])$/) {
 6481:                                 $checked{$1} = ' checked="checked"';
 6482:                             } else {
 6483:                                 $checked{'0'} = ' checked="checked"';
 6484:                             }
 6485:                             for my $val (0..2) {
 6486:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6487:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6488:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6489:                             }
 6490:                         } elsif ($row eq 'limit') {
 6491:                             my %checked;
 6492:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 6493:                                 $checked{$1} = ' checked="checked"';
 6494:                             } else {
 6495:                                 $checked{'none'} = ' checked="checked"';
 6496:                             }
 6497:                             my $cap;
 6498:                             if ($currentcap{$type} =~ /^\d+$/) {
 6499:                                 $cap = $currentcap{$type};
 6500:                             }
 6501:                             foreach my $val ('none','allstudents','selfenrolled') {
 6502:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6503:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6504:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6505:                             }
 6506:                             $datatable .= '<br />'.
 6507:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 6508:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 6509:                                           '</span>'; 
 6510:                         }
 6511:                     }
 6512:                     $datatable .= '</td>';
 6513:                 }
 6514:                 $datatable .= '</tr>';
 6515:             }
 6516:             $datatable .= '</table></td></tr>';
 6517:         }
 6518:     } elsif ($position eq 'bottom') {
 6519:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 6520:     }
 6521:     $$rowtotal += $itemcount;
 6522:     return $datatable;
 6523: }
 6524: 
 6525: sub print_validation_rows {
 6526:     my ($caller,$dom,$settings,$rowtotal) = @_;
 6527:     my ($itemsref,$namesref,$fieldsref);
 6528:     if ($caller eq 'selfenroll') { 
 6529:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 6530:     } elsif ($caller eq 'requestcourses') {
 6531:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6532:     }
 6533:     my %currvalidation;
 6534:     if (ref($settings) eq 'HASH') {
 6535:         if (ref($settings->{'validation'}) eq 'HASH') {
 6536:             %currvalidation = %{$settings->{'validation'}};
 6537:         }
 6538:     }
 6539:     my $datatable;
 6540:     my $itemcount = 0;
 6541:     foreach my $item (@{$itemsref}) {
 6542:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6543:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6544:                       $namesref->{$item}.
 6545:                       '</span></td>'.
 6546:                       '<td class="LC_left_item">';
 6547:         if (($item eq 'url') || ($item eq 'button')) {
 6548:             $datatable .= '<span class="LC_nobreak">'.
 6549:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 6550:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 6551:         } elsif ($item eq 'fields') {
 6552:             my @currfields;
 6553:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 6554:                 @currfields = @{$currvalidation{$item}};
 6555:             }
 6556:             foreach my $field (@{$fieldsref}) {
 6557:                 my $check = '';
 6558:                 if (grep(/^\Q$field\E$/,@currfields)) {
 6559:                     $check = ' checked="checked"';
 6560:                 }
 6561:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6562:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 6563:                               ' value="'.$field.'"'.$check.' />'.$field.
 6564:                               '</label></span> ';
 6565:             }
 6566:         } elsif ($item eq 'markup') {
 6567:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 6568:                            $currvalidation{$item}.
 6569:                               '</textarea>';
 6570:         }
 6571:         $datatable .= '</td></tr>'."\n";
 6572:         if (ref($rowtotal)) {
 6573:             $itemcount ++;
 6574:         }
 6575:     }
 6576:     if ($caller eq 'requestcourses') {
 6577:         my %currhash;
 6578:         if (ref($settings) eq 'HASH') {
 6579:             if (ref($settings->{'validation'}) eq 'HASH') {
 6580:                 if ($settings->{'validation'}{'dc'} ne '') {
 6581:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 6582:                 }
 6583:             }
 6584:         }
 6585:         my $numinrow = 2;
 6586:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 6587:                                                        'validationdc',%currhash);
 6588:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6589:         $datatable .= '<tr'.$css_class.'><td>';
 6590:         if ($numdc > 1) {
 6591:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 6592:         } else {
 6593:             $datatable .=  &mt('Course creation processed as: ');
 6594:         }
 6595:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 6596:         $itemcount ++;
 6597:     }
 6598:     if (ref($rowtotal)) {
 6599:         $$rowtotal += $itemcount;
 6600:     }
 6601:     return $datatable;
 6602: }
 6603: 
 6604: sub print_passwords {
 6605:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 6606:     my ($datatable,$css_class);
 6607:     my $itemcount = 0;
 6608:     my %titles = &Apache::lonlocal::texthash (
 6609:         captcha        => '"Forgot Password" CAPTCHA validation',
 6610:         link           => 'Reset link expiration (hours)',
 6611:         case           => 'Case-sensitive usernames/e-mail',
 6612:         prelink        => 'Information required (form 1)',
 6613:         postlink       => 'Information required (form 2)',
 6614:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 6615:         customtext     => 'Domain specific text (HTML)',
 6616:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 6617:         intauth_check  => 'Check bcrypt cost if authenticated',
 6618:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 6619:         permanent      => 'Permanent e-mail address',
 6620:         critical       => 'Critical notification address',
 6621:         notify         => 'Notification address',
 6622:         min            => 'Minimum password length',
 6623:         max            => 'Maximum password length',
 6624:         chars          => 'Required characters',
 6625:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 6626:     );
 6627:     if ($position eq 'top') {
 6628:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6629:         my $shownlinklife = 2;
 6630:         my $prelink = 'both';
 6631:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 6632:         if (ref($settings) eq 'HASH') {
 6633:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 6634:                 $shownlinklife = $settings->{resetlink};
 6635:             }
 6636:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 6637:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 6638:             }
 6639:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 6640:                 $prelink = $settings->{resetprelink};
 6641:             }
 6642:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 6643:                 %postlink = %{$settings->{resetpostlink}};
 6644:             }
 6645:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 6646:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 6647:             }
 6648:             if ($settings->{resetremove}) {
 6649:                 $nostdtext = 1;
 6650:             }
 6651:             if ($settings->{resetcustom}) {
 6652:                 $customurl = $settings->{resetcustom};
 6653:             }
 6654:         } else {
 6655:             if (ref($types) eq 'ARRAY') {
 6656:                 foreach my $item (@{$types}) {
 6657:                     $casesens{$item} = 1;
 6658:                     $postlink{$item} = ['username','email'];
 6659:                 }
 6660:             }
 6661:             $casesens{'default'} = 1;
 6662:             $postlink{'default'} = ['username','email'];
 6663:             $prelink = 'both';
 6664:             %emailsrc = (
 6665:                           permanent => 1,
 6666:                           critical  => 1,
 6667:                           notify    => 1,
 6668:             );
 6669:         }
 6670:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 6671:         $itemcount ++;
 6672:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6673:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 6674:                       '<td class="LC_left_item">'.
 6675:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 6676:                       'name="passwords_link" size="3" /></td></tr>';
 6677:         $itemcount ++;
 6678:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6679:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 6680:                       '<td class="LC_left_item">';
 6681:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6682:             foreach my $item (@{$types}) {
 6683:                 my $checkedcase;
 6684:                 if ($casesens{$item}) {
 6685:                     $checkedcase = ' checked="checked"';
 6686:                 }
 6687:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6688:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 6689:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 6690:                               '</span>&nbsp;&nbsp; ';
 6691:             }
 6692:         }
 6693:         my $checkedcase;
 6694:         if ($casesens{'default'}) {
 6695:             $checkedcase = ' checked="checked"';
 6696:         }
 6697:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 6698:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 6699:                       $othertitle.'</label></span></td>';
 6700:         $itemcount ++;
 6701:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6702:         my %checkedpre = (
 6703:                              both => ' checked="checked"',
 6704:                              either => '',
 6705:                          );
 6706:         if ($prelink eq 'either') {
 6707:             $checkedpre{either} = ' checked="checked"';
 6708:             $checkedpre{both} = '';
 6709:         }
 6710:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 6711:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6712:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 6713:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 6714:                       '<span class="LC_nobreak"><label>'.
 6715:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 6716:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 6717:         $itemcount ++;
 6718:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6719:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 6720:                       '<td class="LC_left_item">';
 6721:         my %postlinked;
 6722:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6723:             foreach my $item (@{$types}) {
 6724:                 undef(%postlinked);
 6725:                 $datatable .= '<fieldset style="display: inline-block;">'.
 6726:                               '<legend>'.$usertypes->{$item}.'</legend>';
 6727:                 if (ref($postlink{$item}) eq 'ARRAY') {
 6728:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 6729:                 }
 6730:                 foreach my $field ('email','username') {
 6731:                     my $checked;
 6732:                     if ($postlinked{$field}) {
 6733:                         $checked = ' checked="checked"';
 6734:                     }
 6735:                     $datatable .= '<span class="LC_nobreak"><label>'.
 6736:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 6737:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 6738:                                   '<span>&nbsp;&nbsp; ';
 6739:                 }
 6740:                 $datatable .= '</fieldset>';
 6741:             }
 6742:         }
 6743:         if (ref($postlink{'default'}) eq 'ARRAY') {
 6744:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 6745:         }
 6746:         $datatable .= '<fieldset style="display: inline-block;">'.
 6747:                       '<legend>'.$othertitle.'</legend>';
 6748:         foreach my $field ('email','username') {
 6749:             my $checked;
 6750:             if ($postlinked{$field}) {
 6751:                 $checked = ' checked="checked"';
 6752:             }
 6753:             $datatable .= '<span class="LC_nobreak"><label>'.
 6754:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 6755:                           $field.'"'.$checked.' />'.$field.'</label>'.
 6756:                           '<span>&nbsp;&nbsp; ';
 6757:         }
 6758:         $datatable .= '</fieldset></td></tr>';
 6759:         $itemcount ++;
 6760:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6761:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 6762:                       '<td class="LC_left_item">';
 6763:         foreach my $type ('permanent','critical','notify') {
 6764:             my $checkedemail;
 6765:             if ($emailsrc{$type}) {
 6766:                 $checkedemail = ' checked="checked"';
 6767:             }
 6768:             $datatable .= '<span class="LC_nobreak"><label>'.
 6769:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 6770:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 6771:                           '<span>&nbsp;&nbsp; ';
 6772:         }
 6773:         $datatable .= '</td></tr>';
 6774:         $itemcount ++;
 6775:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6776:         my $switchserver = &check_switchserver($dom,$confname);
 6777:         my ($showstd,$noshowstd);
 6778:         if ($nostdtext) {
 6779:             $noshowstd = ' checked="checked"';
 6780:         } else {
 6781:             $showstd = ' checked="checked"';
 6782:         }
 6783:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 6784:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6785:                       &mt('Retain standard text:').
 6786:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 6787:                       &mt('Yes').'</label>'.'&nbsp;'.
 6788:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 6789:                       &mt('No').'</label></span><br />'.
 6790:                       '<span class="LC_fontsize_small">'.
 6791:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 6792:                       &mt('Include custom text:');
 6793:         if ($customurl) {
 6794:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 6795:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6796:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 6797:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 6798:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 6799:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 6800:         }
 6801:         if ($switchserver) {
 6802:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 6803:         } else {
 6804:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6805:                          '<input type="file" name="passwords_customfile" /></span>';
 6806:         }
 6807:         $datatable .= '</td></tr>';
 6808:     } elsif ($position eq 'middle') {
 6809:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 6810:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6811:         my %defaults;
 6812:         if (ref($domconf{'defaults'}) eq 'HASH') {
 6813:             %defaults = %{$domconf{'defaults'}};
 6814:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6815:                 $defaults{'intauth_cost'} = 10;
 6816:             }
 6817:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6818:                 $defaults{'intauth_check'} = 0;
 6819:             }
 6820:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6821:                 $defaults{'intauth_switch'} = 0;
 6822:             }
 6823:         } else {
 6824:             %defaults = (
 6825:                           'intauth_cost'   => 10,
 6826:                           'intauth_check'  => 0,
 6827:                           'intauth_switch' => 0,
 6828:                         );
 6829:         }
 6830:         foreach my $item (@items) {
 6831:             if ($itemcount%2) {
 6832:                 $css_class = '';
 6833:             } else {
 6834:                 $css_class = ' class="LC_odd_row" ';
 6835:             }
 6836:             $datatable .= '<tr'.$css_class.'>'.
 6837:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 6838:                           '</span></td><td class="LC_left_item" colspan="3">';
 6839:             if ($item eq 'intauth_switch') {
 6840:                 my @options = (0,1,2);
 6841:                 my %optiondesc = &Apache::lonlocal::texthash (
 6842:                                    0 => 'No',
 6843:                                    1 => 'Yes',
 6844:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6845:                                  );
 6846:                 $datatable .= '<table width="100%">';
 6847:                 foreach my $option (@options) {
 6848:                     my $checked = ' ';
 6849:                     if ($defaults{$item} eq $option) {
 6850:                         $checked = ' checked="checked"';
 6851:                     }
 6852:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6853:                                   '<label><input type="radio" name="'.$item.
 6854:                                   '" value="'.$option.'"'.$checked.' />'.
 6855:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6856:                 }
 6857:                 $datatable .= '</table>';
 6858:             } elsif ($item eq 'intauth_check') {
 6859:                 my @options = (0,1,2);
 6860:                 my %optiondesc = &Apache::lonlocal::texthash (
 6861:                                    0 => 'No',
 6862:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6863:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6864:                                  );
 6865:                 $datatable .= '<table width="100%">';
 6866:                 foreach my $option (@options) {
 6867:                     my $checked = ' ';
 6868:                     my $onclick;
 6869:                     if ($defaults{$item} eq $option) {
 6870:                         $checked = ' checked="checked"';
 6871:                     }
 6872:                     if ($option == 2) {
 6873:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 6874:                     }
 6875:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6876:                                   '<label><input type="radio" name="'.$item.
 6877:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 6878:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6879:                 }
 6880:                 $datatable .= '</table>';
 6881:             } else {
 6882:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6883:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 6884:             }
 6885:             $datatable .= '</td></tr>';
 6886:             $itemcount ++;
 6887:         }
 6888:     } elsif ($position eq 'lower') {
 6889:         $datatable .= &password_rules('passwords',\$itemcount,$settings);
 6890:     } else {
 6891:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6892:         my %ownerchg = (
 6893:                           by  => {},
 6894:                           for => {},
 6895:                        );
 6896:         my %ownertitles = &Apache::lonlocal::texthash (
 6897:                             by  => 'Course owner status(es) allowed',
 6898:                             for => 'Student status(es) allowed',
 6899:                           );
 6900:         if (ref($settings) eq 'HASH') {
 6901:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 6902:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 6903:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 6904:                 }
 6905:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 6906:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 6907:                 }
 6908:             }
 6909:         }
 6910:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6911:         $datatable .= '<tr '.$css_class.'>'.
 6912:                       '<td>'.
 6913:                       &mt('Requirements').'<ul>'.
 6914:                       '<li>'.&mt("Course 'type' is not a Community").'</li>'.
 6915:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 6916:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 6917:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 6918:                       '</ul>'.
 6919:                       '</td>'.
 6920:                       '<td class="LC_left_item">';
 6921:         foreach my $item ('by','for') {
 6922:             $datatable .= '<fieldset style="display: inline-block;">'.
 6923:                           '<legend>'.$ownertitles{$item}.'</legend>';
 6924:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6925:                 foreach my $type (@{$types}) {
 6926:                     my $checked;
 6927:                     if ($ownerchg{$item}{$type}) {
 6928:                         $checked = ' checked="checked"';
 6929:                     }
 6930:                     $datatable .= '<span class="LC_nobreak"><label>'.
 6931:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 6932:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 6933:                                   '</span>&nbsp;&nbsp; ';
 6934:                 }
 6935:             }
 6936:             my $checked;
 6937:             if ($ownerchg{$item}{'default'}) {
 6938:                 $checked = ' checked="checked"';
 6939:             }
 6940:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 6941:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 6942:                           $othertitle.'</label></span></fieldset>';
 6943:         }
 6944:         $datatable .= '</td></tr>';
 6945:     }
 6946:     return $datatable;
 6947: }
 6948: 
 6949: sub password_rules {
 6950:     my ($prefix,$itemcountref,$settings) = @_;
 6951:     my ($min,$max,%chars,$numsaved,$numinrow);
 6952:     my %titles;
 6953:     if ($prefix eq 'passwords') {
 6954:         %titles = &Apache::lonlocal::texthash (
 6955:             min            => 'Minimum password length',
 6956:             max            => 'Maximum password length',
 6957:             chars          => 'Required characters',
 6958:         );
 6959:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
 6960:         %titles = &Apache::lonlocal::texthash (
 6961:             min            => 'Minimum secret length',
 6962:             max            => 'Maximum secret length',
 6963:             chars          => 'Required characters',
 6964:         );
 6965:     }
 6966:     $min = $Apache::lonnet::passwdmin;
 6967:     my $datatable;
 6968:     my $itemcount;
 6969:     if (ref($itemcountref)) {
 6970:         $itemcount = $$itemcountref;
 6971:     }
 6972:     if (ref($settings) eq 'HASH') {
 6973:         if ($settings->{min}) {
 6974:             $min = $settings->{min};
 6975:         }
 6976:         if ($settings->{max}) {
 6977:             $max = $settings->{max};
 6978:         }
 6979:         if (ref($settings->{chars}) eq 'ARRAY') {
 6980:             map { $chars{$_} = 1; } (@{$settings->{chars}});
 6981:         }
 6982:         if ($prefix eq 'passwords') {
 6983:             if ($settings->{numsaved}) {
 6984:                 $numsaved = $settings->{numsaved};
 6985:             }
 6986:         }
 6987:     }
 6988:     my %rulenames = &Apache::lonlocal::texthash(
 6989:                                                  uc => 'At least one upper case letter',
 6990:                                                  lc => 'At least one lower case letter',
 6991:                                                  num => 'At least one number',
 6992:                                                  spec => 'At least one non-alphanumeric',
 6993:                                                );
 6994:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6995:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 6996:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 6997:                   '<input type="text" name="'.$prefix.'_min" value="'.$min.'" size="3" '.
 6998:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 6999:                   '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 7000:                   '</span></td></tr>';
 7001:     $itemcount ++;
 7002:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7003:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 7004:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 7005:                   '<input type="text" name="'.$prefix.'_max" value="'.$max.'" size="3" '.
 7006:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 7007:                   '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 7008:                   '</span></td></tr>';
 7009:     $itemcount ++;
 7010:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7011:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 7012:                   '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 7013:                   '</span></td>';
 7014:     my $numinrow = 2;
 7015:     my @possrules = ('uc','lc','num','spec');
 7016:     $datatable .= '<td class="LC_left_item"><table>';
 7017:     for (my $i=0; $i<@possrules; $i++) {
 7018:         my ($rem,$checked);
 7019:         if ($chars{$possrules[$i]}) {
 7020:             $checked = ' checked="checked"';
 7021:         }
 7022:         $rem = $i%($numinrow);
 7023:         if ($rem == 0) {
 7024:             if ($i > 0) {
 7025:                 $datatable .= '</tr>';
 7026:             }
 7027:             $datatable .= '<tr>';
 7028:         }
 7029:         $datatable .= '<td><span class="LC_nobreak"><label>'.
 7030:                       '<input type="checkbox" name="'.$prefix.'_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 7031:                       $rulenames{$possrules[$i]}.'</label></span></td>';
 7032:     }
 7033:     my $rem = @possrules%($numinrow);
 7034:     my $colsleft = $numinrow - $rem;
 7035:     if ($colsleft > 1 ) {
 7036:         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7037:                       '&nbsp;</td>';
 7038:     } elsif ($colsleft == 1) {
 7039:         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7040:     }
 7041:     $datatable .='</table></td></tr>';
 7042:     $itemcount ++;
 7043:     if ($prefix eq 'passwords') {
 7044:         $titles{'numsaved'} = &mt('Number of previous passwords to save and disallow reuse');
 7045:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7046:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 7047:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7048:                       '<input type="text" name="'.$prefix.'_numsaved" value="'.$numsaved.'" size="3" '.
 7049:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 7050:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 7051:                       '</span></td></tr>';
 7052:         $itemcount ++;
 7053:     }
 7054:     if (ref($itemcountref)) {
 7055:         $$itemcountref += $itemcount;
 7056:     }
 7057:     return $datatable;
 7058: }
 7059: 
 7060: sub print_wafproxy {
 7061:     my ($position,$dom,$settings,$rowtotal) = @_;
 7062:     my $css_class;
 7063:     my $itemcount = 0;
 7064:     my $datatable;
 7065:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7066:     my (%othercontrol,%otherdoms,%aliases,%saml,%values,$setdom,$showdom);
 7067:     my %lt = &wafproxy_titles();
 7068:     foreach my $server (sort(keys(%servers))) {
 7069:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 7070:         next if ($serverhome eq '');
 7071:         my $serverdom;
 7072:         if ($serverhome ne $server) {
 7073:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 7074:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 7075:                 $othercontrol{$server} = $serverdom;
 7076:             }
 7077:         } else {
 7078:             $serverdom = &Apache::lonnet::host_domain($server);
 7079:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 7080:             if ($serverdom ne $dom) {
 7081:                 $othercontrol{$server} = $serverdom;
 7082:             } else {
 7083:                 $setdom = 1;
 7084:                 if (ref($settings) eq 'HASH') {
 7085:                     if (ref($settings->{'alias'}) eq 'HASH') {
 7086:                         $aliases{$dom} = $settings->{'alias'};
 7087:                         if ($aliases{$dom} ne '') {
 7088:                             $showdom = 1;
 7089:                         }
 7090:                     }
 7091:                     if (ref($settings->{'saml'}) eq 'HASH') {
 7092:                         $saml{$dom} = $settings->{'saml'};
 7093:                     }
 7094:                 }
 7095:             }
 7096:         }
 7097:     }
 7098:     if ($setdom) {
 7099:         %{$values{$dom}} = ();
 7100:         if (ref($settings) eq 'HASH') {
 7101:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 7102:                 $values{$dom}{$item} = $settings->{$item};
 7103:             }
 7104:         }
 7105:     }
 7106:     if (keys(%othercontrol)) {
 7107:         %otherdoms = reverse(%othercontrol);
 7108:         foreach my $domain (keys(%otherdoms)) {
 7109:             %{$values{$domain}} = ();
 7110:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 7111:             if (ref($config{'wafproxy'}) eq 'HASH') {
 7112:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 7113:                 if (exists($config{'wafproxy'}{'saml'})) {
 7114:                     $saml{$domain} = $config{'wafproxy'}{'saml'};
 7115:                 }
 7116:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 7117:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 7118:                 }
 7119:             }
 7120:         }
 7121:     }
 7122:     if ($position eq 'top') {
 7123:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7124:         my %aliasinfo;
 7125:         foreach my $server (sort(keys(%servers))) {
 7126:             $itemcount ++;
 7127:             my $dom_in_effect;
 7128:             my $aliasrows = '<tr>'.
 7129:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7130:                             &mt('Hostname').': '.
 7131:                             '<span class="LC_nobreak LC_cusr_emph">'.
 7132:                             &Apache::lonnet::hostname($server).'</span></td><td>&nbsp;</td>';
 7133:             if ($othercontrol{$server}) {
 7134:                 $dom_in_effect = $othercontrol{$server};
 7135:                 my ($current,$forsaml);
 7136:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 7137:                     $current = $aliases{$dom_in_effect}{$server};
 7138:                 }
 7139:                 if (ref($saml{$dom_in_effect}) eq 'HASH') {
 7140:                     if ($saml{$dom_in_effect}{$server}) {
 7141:                         $forsaml = 1;
 7142:                     }
 7143:                 }
 7144:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7145:                               &mt('Alias').':&nbsp';
 7146:                 if ($current) {
 7147:                     $aliasrows .= $current;
 7148:                     if ($forsaml) {
 7149:                          $aliasrows .= '&nbsp;('.&mt('also for SSO Auth').')';
 7150:                     }
 7151:                 } else {
 7152:                     $aliasrows .= &mt('None');
 7153:                 }
 7154:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 7155:                               &mt('controlled by domain: [_1]',
 7156:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 7157:             } else {
 7158:                 $dom_in_effect = $dom;
 7159:                 my ($current,$samlon,$samloff);
 7160:                 $samloff = ' checked="checked"';
 7161:                 if (ref($aliases{$dom}) eq 'HASH') {
 7162:                     if ($aliases{$dom}{$server}) {
 7163:                         $current = $aliases{$dom}{$server};
 7164:                     }
 7165:                 }
 7166:                 if (ref($saml{$dom}) eq 'HASH') {
 7167:                     if ($saml{$dom}{$server}) {
 7168:                         $samlon = $samloff;
 7169:                         undef($samloff);
 7170:                     }
 7171:                 }
 7172:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7173:                               &mt('Alias').':&nbsp;'.
 7174:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 7175:                               'value="'.$current.'" size="30" />'.
 7176:                               ('&nbsp;'x2).'<span class="LC_nobreak">'.
 7177:                               &mt('Alias used for SSO Auth').':&nbsp;<label>'.
 7178:                               '<input type="radio" value="0"'.$samloff.' name="wafproxy_alias_saml_'.$server.'" />'.
 7179:                               &mt('No').'</label>&nbsp;<label>'.
 7180:                               '<input type="radio" value="1"'.$samlon.' name="wafproxy_alias_saml_'.$server.'" />'.
 7181:                               &mt('Yes').'</label></span>'.
 7182:                               '</td>';
 7183:             }
 7184:             $aliasrows .= '</tr>';
 7185:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 7186:         }
 7187:         if ($aliasinfo{$dom}) {
 7188:             my ($onclick,$wafon,$wafoff,$showtable);
 7189:             $onclick = ' onclick="javascript:toggleWAF();"';
 7190:             $wafoff = ' checked="checked"';
 7191:             $showtable = ' style="display:none";';
 7192:             if ($showdom) {
 7193:                 $wafon = $wafoff;
 7194:                 $wafoff = '';
 7195:                 $showtable = ' style="display:inline;"';
 7196:             }
 7197:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7198:             $datatable = '<tr'.$css_class.'>'.
 7199:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 7200:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 7201:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 7202:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 7203:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 7204:                          &mt('No').'</label></span></td>'.
 7205:                          '<td class="LC_left_item">'.
 7206:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 7207:                          '</table></td></tr>';
 7208:             $itemcount++;
 7209:         }
 7210:         if (keys(%otherdoms)) {
 7211:             foreach my $key (sort(keys(%otherdoms))) {
 7212:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7213:                 $datatable .= '<tr'.$css_class.'>'.
 7214:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 7215:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 7216:                               '</table></td></tr>';
 7217:                 $itemcount++;
 7218:             }
 7219:         }
 7220:     } else {
 7221:         my %ip_methods = &remoteip_methods();
 7222:         if ($setdom) {
 7223:             $itemcount ++;
 7224:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7225:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 7226:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 7227:             $wafstyle = ' style="display:none;"';
 7228:             $nowafstyle = ' style="display:table-row;"';
 7229:             $currwafdisplay = ' style="display: none"';
 7230:             $wafrangestyle = ' style="display: none"';
 7231:             $curr_remotip = 'n';
 7232:             $ssltossl = ' checked="checked"';
 7233:             if ($showdom) {
 7234:                 $wafstyle = ' style="display:table-row;"';
 7235:                 $nowafstyle =  ' style="display:none;"';
 7236:                 if (keys(%{$values{$dom}})) {
 7237:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 7238:                         $curr_remotip = $values{$dom}{remoteip};
 7239:                     }
 7240:                     if ($curr_remotip eq 'h') {
 7241:                         $currwafdisplay = ' style="display:table-row"';
 7242:                         $wafrangestyle = ' style="display:inline-block;"';
 7243:                     }
 7244:                     if ($values{$dom}{'sslopt'}) {
 7245:                         $alltossl = ' checked="checked"';
 7246:                         $ssltossl = '';
 7247:                     }
 7248:                 }
 7249:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 7250:                     $vpndircheck = ' checked="checked"';
 7251:                     $currwafvpn = ' style="display:table-row;"';
 7252:                     $wafrangestyle = ' style="display:inline-block;"';
 7253:                 } else {
 7254:                     $vpnaliascheck = ' checked="checked"';
 7255:                     $currwafvpn = ' style="display:none;"';
 7256:                 }
 7257:             }
 7258:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 7259:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 7260:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 7261:                           '</tr>'.
 7262:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 7263:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 7264:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 7265:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 7266:                           &mt('Range(s) stored in CIDR notation').'</div></td>'.
 7267:                           '<td class="LC_left_item"><table>'.
 7268:                           '<tr>'.
 7269:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 7270:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 7271:             foreach my $option ('m','h','n') {
 7272:                 my $sel;
 7273:                 if ($option eq $curr_remotip) {
 7274:                    $sel = ' selected="selected"';
 7275:                 }
 7276:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 7277:                               $ip_methods{$option}.'</option>';
 7278:             }
 7279:             $datatable .= '</select></td></tr>'."\n".
 7280:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 7281:                           $lt{'ipheader'}.':&nbsp;'.
 7282:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 7283:                           'name="wafproxy_ipheader" />'.
 7284:                           '</td></tr>'."\n".
 7285:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 7286:                           $lt{'trusted'}.':<br />'.
 7287:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 7288:                           $values{$dom}{'trusted'}.'</textarea>'.
 7289:                           '</td></tr>'."\n".
 7290:                           '<tr><td><hr /></td></tr>'."\n".
 7291:                           '<tr>'.
 7292:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 7293:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 7294:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 7295:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 7296:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 7297:             foreach my $item ('vpnint','vpnext') {
 7298:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 7299:                               '<td valign="top">'.$lt{$item}.':<br />'.
 7300:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 7301:                               $values{$dom}{$item}.'</textarea>'.
 7302:                               '</td></tr>'."\n";
 7303:             }
 7304:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 7305:                           '<tr>'.
 7306:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 7307:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 7308:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 7309:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 7310:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 7311:                           '</table></td></tr>';
 7312:         }
 7313:         if (keys(%otherdoms)) {
 7314:             foreach my $domain (sort(keys(%otherdoms))) {
 7315:                 $itemcount ++;
 7316:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7317:                 $datatable .= '<tr'.$css_class.'>'.
 7318:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 7319:                               '<td class="LC_left_item"><table>';
 7320:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 7321:                     my $showval = &mt('None');
 7322:                     if ($item eq 'ssl') {
 7323:                         $showval = $lt{'ssltossl'};
 7324:                     }
 7325:                     if ($values{$domain}{$item}) {
 7326:                         $showval = $values{$domain}{$item};
 7327:                         if ($item eq 'ssl') {
 7328:                             $showval = $lt{'alltossl'};
 7329:                         } elsif ($item eq 'remoteip') {
 7330:                             $showval = $ip_methods{$values{$domain}{$item}};
 7331:                         }
 7332:                     }
 7333:                     $datatable .= '<tr>'.
 7334:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 7335:                 }
 7336:                 $datatable .= '</table></td></tr>';
 7337:             }
 7338:         }
 7339:     }
 7340:     $$rowtotal += $itemcount;
 7341:     return $datatable;
 7342: }
 7343: 
 7344: sub wafproxy_titles {
 7345:     return &Apache::lonlocal::texthash(
 7346:                remoteip   => "Method for determining user's IP",
 7347:                ipheader   => 'Request header containing remote IP',
 7348:                trusted    => 'Trusted IP range(s)',
 7349:                vpnaccess  => 'Access from institutional VPN',
 7350:                vpndirect  => 'via regular hostname (no WAF)',
 7351:                vpnaliased => 'via aliased hostname (WAF)',
 7352:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 7353:                vpnext     => 'IP Range(s) for backend WAF connections',
 7354:                sslopt     => 'Forwarding http/https',
 7355:                alltossl   => 'WAF forwards both http and https requests to https',
 7356:                ssltossl   => 'WAF forwards http requests to http and https to https',
 7357:            );
 7358: }
 7359: 
 7360: sub remoteip_methods {
 7361:     return &Apache::lonlocal::texthash(
 7362:               m => 'Use Apache mod_remoteip',
 7363:               h => 'Use headers parsed by LON-CAPA',
 7364:               n => 'Not in use',
 7365:            );
 7366: }
 7367: 
 7368: sub print_usersessions {
 7369:     my ($position,$dom,$settings,$rowtotal) = @_;
 7370:     my ($css_class,$datatable,%checked,%choices);
 7371:     my (%by_ip,%by_location,@intdoms);
 7372:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7373: 
 7374:     my @alldoms = &Apache::lonnet::all_domains();
 7375:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 7376:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7377:     my %altids = &id_for_thisdom(%servers);
 7378:     my $itemcount = 1;
 7379:     if ($position eq 'top') {
 7380:         if (keys(%serverhomes) > 1) {
 7381:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 7382:             my ($curroffloadnow,$curroffloadoth);
 7383:             if (ref($settings) eq 'HASH') {
 7384:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 7385:                     $curroffloadnow = $settings->{'offloadnow'};
 7386:                 }
 7387:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 7388:                     $curroffloadoth = $settings->{'offloadoth'};
 7389:                 }
 7390:             }
 7391:             my $other_insts = scalar(keys(%by_location));
 7392:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 7393:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 7394:         } else {
 7395:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 7396:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 7397:                           '</td></tr>';
 7398:         }
 7399:     } else {
 7400:         if (keys(%by_location) == 0) {
 7401:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 7402:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 7403:                           '</td></tr>';
 7404:         } else {
 7405:             my %lt = &usersession_titles();
 7406:             my $numinrow = 5;
 7407:             my $prefix;
 7408:             my @types;
 7409:             if ($position eq 'bottom') {
 7410:                 $prefix = 'remote';
 7411:                 @types = ('version','excludedomain','includedomain');
 7412:             } else {
 7413:                 $prefix = 'hosted';
 7414:                 @types = ('excludedomain','includedomain');
 7415:             }
 7416:             my (%current,%checkedon,%checkedoff);
 7417:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 7418:             my @locations = sort(keys(%by_location));
 7419:             foreach my $type (@types) {
 7420:                 $checkedon{$type} = '';
 7421:                 $checkedoff{$type} = ' checked="checked"';
 7422:             }
 7423:             if (ref($settings) eq 'HASH') {
 7424:                 if (ref($settings->{$prefix}) eq 'HASH') {
 7425:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 7426:                         $current{$key} = $settings->{$prefix}{$key};
 7427:                         if ($key eq 'version') {
 7428:                             if ($current{$key} ne '') {
 7429:                                 $checkedon{$key} = ' checked="checked"';
 7430:                                 $checkedoff{$key} = '';
 7431:                             }
 7432:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 7433:                             $checkedon{$key} = ' checked="checked"';
 7434:                             $checkedoff{$key} = '';
 7435:                         }
 7436:                     }
 7437:                 }
 7438:             }
 7439:             foreach my $type (@types) {
 7440:                 next if ($type ne 'version' && !@locations);
 7441:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7442:                 $datatable .= '<tr'.$css_class.'>
 7443:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 7444:                                <span class="LC_nobreak">&nbsp;
 7445:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 7446:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 7447:                 if ($type eq 'version') {
 7448:                     my $selector = '<select name="'.$prefix.'_version">';
 7449:                     foreach my $version (@lcversions) {
 7450:                         my $selected = '';
 7451:                         if ($current{'version'} eq $version) {
 7452:                             $selected = ' selected="selected"';
 7453:                         }
 7454:                         $selector .= ' <option value="'.$version.'"'.
 7455:                                      $selected.'>'.$version.'</option>';
 7456:                     }
 7457:                     $selector .= '</select> ';
 7458:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 7459:                 } else {
 7460:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 7461:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 7462:                                  ' />'.('&nbsp;'x2).
 7463:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 7464:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 7465:                                  "\n".
 7466:                                  '</div><div><table>';
 7467:                     my $rem;
 7468:                     for (my $i=0; $i<@locations; $i++) {
 7469:                         my ($showloc,$value,$checkedtype);
 7470:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 7471:                             my $ip = $by_location{$locations[$i]}->[0];
 7472:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 7473:                                  $value = join(':',@{$by_ip{$ip}});
 7474:                                 $showloc = join(', ',@{$by_ip{$ip}});
 7475:                                 if (ref($current{$type}) eq 'ARRAY') {
 7476:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 7477:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 7478:                                             $checkedtype = ' checked="checked"';
 7479:                                             last;
 7480:                                         }
 7481:                                     }
 7482:                                 }
 7483:                             }
 7484:                         }
 7485:                         $rem = $i%($numinrow);
 7486:                         if ($rem == 0) {
 7487:                             if ($i > 0) {
 7488:                                 $datatable .= '</tr>';
 7489:                             }
 7490:                             $datatable .= '<tr>';
 7491:                         }
 7492:                         $datatable .= '<td class="LC_left_item">'.
 7493:                                       '<span class="LC_nobreak"><label>'.
 7494:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 7495:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 7496:                                       '</label></span></td>';
 7497:                     }
 7498:                     $rem = @locations%($numinrow);
 7499:                     my $colsleft = $numinrow - $rem;
 7500:                     if ($colsleft > 1 ) {
 7501:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7502:                                       '&nbsp;</td>';
 7503:                     } elsif ($colsleft == 1) {
 7504:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7505:                     }
 7506:                     $datatable .= '</tr></table>';
 7507:                 }
 7508:                 $datatable .= '</td></tr>';
 7509:                 $itemcount ++;
 7510:             }
 7511:         }
 7512:     }
 7513:     $$rowtotal += $itemcount;
 7514:     return $datatable;
 7515: }
 7516: 
 7517: sub build_location_hashes {
 7518:     my ($intdoms,$by_ip,$by_location) = @_;
 7519:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 7520:                   (ref($by_location) eq 'HASH')); 
 7521:     my %iphost = &Apache::lonnet::get_iphost();
 7522:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 7523:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 7524:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 7525:         foreach my $id (@{$iphost{$primary_ip}}) {
 7526:             my $intdom = &Apache::lonnet::internet_dom($id);
 7527:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 7528:                 push(@{$intdoms},$intdom);
 7529:             }
 7530:         }
 7531:     }
 7532:     foreach my $ip (keys(%iphost)) {
 7533:         if (ref($iphost{$ip}) eq 'ARRAY') {
 7534:             foreach my $id (@{$iphost{$ip}}) {
 7535:                 my $location = &Apache::lonnet::internet_dom($id);
 7536:                 if ($location) {
 7537:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 7538:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 7539:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 7540:                             push(@{$by_ip->{$ip}},$location);
 7541:                         }
 7542:                     } else {
 7543:                         $by_ip->{$ip} = [$location];
 7544:                     }
 7545:                 }
 7546:             }
 7547:         }
 7548:     }
 7549:     foreach my $ip (sort(keys(%{$by_ip}))) {
 7550:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 7551:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 7552:             my $first = $by_ip->{$ip}->[0];
 7553:             if (ref($by_location->{$first}) eq 'ARRAY') {
 7554:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 7555:                     push(@{$by_location->{$first}},$ip);
 7556:                 }
 7557:             } else {
 7558:                 $by_location->{$first} = [$ip];
 7559:             }
 7560:         }
 7561:     }
 7562:     return;
 7563: }
 7564: 
 7565: sub current_offloads_to {
 7566:     my ($dom,$settings,$servers) = @_;
 7567:     my (%spareid,%otherdomconfigs);
 7568:     if (ref($servers) eq 'HASH') {
 7569:         foreach my $lonhost (sort(keys(%{$servers}))) {
 7570:             my $gotspares;
 7571:             if (ref($settings) eq 'HASH') {
 7572:                 if (ref($settings->{'spares'}) eq 'HASH') {
 7573:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 7574:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 7575:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 7576:                         $gotspares = 1;
 7577:                     }
 7578:                 }
 7579:             }
 7580:             unless ($gotspares) {
 7581:                 my $gotspares;
 7582:                 my $serverhomeID =
 7583:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 7584:                 my $serverhomedom =
 7585:                     &Apache::lonnet::host_domain($serverhomeID);
 7586:                 if ($serverhomedom ne $dom) {
 7587:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 7588:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 7589:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 7590:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 7591:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 7592:                                 $gotspares = 1;
 7593:                             }
 7594:                         }
 7595:                     } else {
 7596:                         $otherdomconfigs{$serverhomedom} =
 7597:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 7598:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 7599:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 7600:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 7601:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 7602:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 7603:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 7604:                                         $gotspares = 1;
 7605:                                     }
 7606:                                 }
 7607:                             }
 7608:                         }
 7609:                     }
 7610:                 }
 7611:             }
 7612:             unless ($gotspares) {
 7613:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 7614:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 7615:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 7616:                } else {
 7617:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 7618:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 7619:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 7620:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 7621:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 7622:                     } else {
 7623:                         my %what = (
 7624:                              spareid => 1,
 7625:                         );
 7626:                         my ($result,$returnhash) = 
 7627:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 7628:                         if ($result eq 'ok') { 
 7629:                             if (ref($returnhash) eq 'HASH') {
 7630:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 7631:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 7632:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 7633:                                 }
 7634:                             }
 7635:                         }
 7636:                     }
 7637:                 }
 7638:             }
 7639:         }
 7640:     }
 7641:     return %spareid;
 7642: }
 7643: 
 7644: sub spares_row {
 7645:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 7646:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 7647:     my $css_class;
 7648:     my $numinrow = 4;
 7649:     my $itemcount = 1;
 7650:     my $datatable;
 7651:     my %typetitles = &sparestype_titles();
 7652:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 7653:         foreach my $server (sort(keys(%{$servers}))) {
 7654:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 7655:             my ($othercontrol,$serverdom);
 7656:             if ($serverhome ne $server) {
 7657:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 7658:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 7659:             } else {
 7660:                 $serverdom = &Apache::lonnet::host_domain($server);
 7661:                 if ($serverdom ne $dom) {
 7662:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 7663:                 }
 7664:             }
 7665:             next unless (ref($spareid->{$server}) eq 'HASH');
 7666:             my ($checkednow,$checkedoth);
 7667:             if (ref($curroffloadnow) eq 'HASH') {
 7668:                 if ($curroffloadnow->{$server}) {
 7669:                     $checkednow = ' checked="checked"';
 7670:                 }
 7671:             }
 7672:             if (ref($curroffloadoth) eq 'HASH') {
 7673:                 if ($curroffloadoth->{$server}) {
 7674:                     $checkedoth = ' checked="checked"';
 7675:                 }
 7676:             }
 7677:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7678:             $datatable .= '<tr'.$css_class.'>
 7679:                            <td rowspan="2">
 7680:                             <span class="LC_nobreak">'.
 7681:                           &mt('[_1] when busy, offloads to:'
 7682:                               ,'<b>'.$server.'</b>').'</span><br />'.
 7683:                           '<span class="LC_nobreak">'."\n".
 7684:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 7685:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 7686:                           "\n";
 7687:             if ($other_insts) {
 7688:                 $datatable .= '<br />'.
 7689:                               '<span class="LC_nobreak">'."\n".
 7690:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 7691:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 7692:                           "\n";
 7693:             }
 7694:             my (%current,%canselect);
 7695:             my @choices = 
 7696:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 7697:             foreach my $type ('primary','default') {
 7698:                 if (ref($spareid->{$server}) eq 'HASH') {
 7699:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 7700:                         my @spares = @{$spareid->{$server}{$type}};
 7701:                         if (@spares > 0) {
 7702:                             if ($othercontrol) {
 7703:                                 $current{$type} = join(', ',@spares);
 7704:                             } else {
 7705:                                 $current{$type} .= '<table>';
 7706:                                 my $numspares = scalar(@spares);
 7707:                                 for (my $i=0;  $i<@spares; $i++) {
 7708:                                     my $rem = $i%($numinrow);
 7709:                                     if ($rem == 0) {
 7710:                                         if ($i > 0) {
 7711:                                             $current{$type} .= '</tr>';
 7712:                                         }
 7713:                                         $current{$type} .= '<tr>';
 7714:                                     }
 7715:                                     $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;'.
 7716:                                                        $spareid->{$server}{$type}[$i].
 7717:                                                        '</label></td>'."\n";
 7718:                                 }
 7719:                                 my $rem = @spares%($numinrow);
 7720:                                 my $colsleft = $numinrow - $rem;
 7721:                                 if ($colsleft > 1 ) {
 7722:                                     $current{$type} .= '<td colspan="'.$colsleft.
 7723:                                                        '" class="LC_left_item">'.
 7724:                                                        '&nbsp;</td>';
 7725:                                 } elsif ($colsleft == 1) {
 7726:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 7727:                                 }
 7728:                                 $current{$type} .= '</tr></table>';
 7729:                             }
 7730:                         }
 7731:                     }
 7732:                     if ($current{$type} eq '') {
 7733:                         $current{$type} = &mt('None specified');
 7734:                     }
 7735:                     if ($othercontrol) {
 7736:                         if ($type eq 'primary') {
 7737:                             $canselect{$type} = $othercontrol;
 7738:                         }
 7739:                     } else {
 7740:                         $canselect{$type} = 
 7741:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 7742:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 7743:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 7744:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 7745:                         if (@choices > 0) {
 7746:                             foreach my $lonhost (@choices) {
 7747:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 7748:                             }
 7749:                         }
 7750:                         $canselect{$type} .= '</select>'."\n";
 7751:                     }
 7752:                 } else {
 7753:                     $current{$type} = &mt('Could not be determined');
 7754:                     if ($type eq 'primary') {
 7755:                         $canselect{$type} =  $othercontrol;
 7756:                     }
 7757:                 }
 7758:                 if ($type eq 'default') {
 7759:                     $datatable .= '<tr'.$css_class.'>';
 7760:                 }
 7761:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 7762:                               '<td>'.$current{$type}.'</td>'."\n".
 7763:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 7764:             }
 7765:             $itemcount ++;
 7766:         }
 7767:     }
 7768:     $$rowtotal += $itemcount;
 7769:     return $datatable;
 7770: }
 7771: 
 7772: sub possible_newspares {
 7773:     my ($server,$currspares,$serverhomes,$altids) = @_;
 7774:     my $serverhostname = &Apache::lonnet::hostname($server);
 7775:     my %excluded;
 7776:     if ($serverhostname ne '') {
 7777:         %excluded = (
 7778:                        $serverhostname => 1,
 7779:                     );
 7780:     }
 7781:     if (ref($currspares) eq 'HASH') {
 7782:         foreach my $type (keys(%{$currspares})) {
 7783:             if (ref($currspares->{$type}) eq 'ARRAY') {
 7784:                 if (@{$currspares->{$type}} > 0) {
 7785:                     foreach my $curr (@{$currspares->{$type}}) {
 7786:                         my $hostname = &Apache::lonnet::hostname($curr);
 7787:                         $excluded{$hostname} = 1;
 7788:                     }
 7789:                 }
 7790:             }
 7791:         }
 7792:     }
 7793:     my @choices;
 7794:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 7795:         if (keys(%{$serverhomes}) > 1) {
 7796:             foreach my $name (sort(keys(%{$serverhomes}))) {
 7797:                 unless ($excluded{$name}) {
 7798:                     if (exists($altids->{$serverhomes->{$name}})) {
 7799:                         push(@choices,$altids->{$serverhomes->{$name}});
 7800:                     } else {
 7801:                         push(@choices,$serverhomes->{$name});
 7802:                     }
 7803:                 }
 7804:             }
 7805:         }
 7806:     }
 7807:     return sort(@choices);
 7808: }
 7809: 
 7810: sub print_loadbalancing {
 7811:     my ($dom,$settings,$rowtotal) = @_;
 7812:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7813:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7814:     my $numinrow = 1;
 7815:     my $datatable;
 7816:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7817:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 7818:     if (ref($settings) eq 'HASH') {
 7819:         %existing = %{$settings};
 7820:     }
 7821:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 7822:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 7823:                                   \%currtargets,\%currrules,\%currcookies);
 7824:     } else {
 7825:         return;
 7826:     }
 7827:     my ($othertitle,$usertypes,$types) =
 7828:         &Apache::loncommon::sorted_inst_types($dom);
 7829:     my $rownum = 8;
 7830:     if (ref($types) eq 'ARRAY') {
 7831:         $rownum += scalar(@{$types});
 7832:     }
 7833:     my @css_class = ('LC_odd_row','LC_even_row');
 7834:     my $balnum = 0;
 7835:     my $islast;
 7836:     my (@toshow,$disabledtext);
 7837:     if (keys(%currbalancer) > 0) {
 7838:         @toshow = sort(keys(%currbalancer));
 7839:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 7840:             push(@toshow,'');
 7841:         }
 7842:     } else {
 7843:         @toshow = ('');
 7844:         $disabledtext = &mt('No existing load balancer');
 7845:     }
 7846:     foreach my $lonhost (@toshow) {
 7847:         if ($balnum == scalar(@toshow)-1) {
 7848:             $islast = 1;
 7849:         } else {
 7850:             $islast = 0;
 7851:         }
 7852:         my $cssidx = $balnum%2;
 7853:         my $targets_div_style = 'display: none';
 7854:         my $disabled_div_style = 'display: block';
 7855:         my $homedom_div_style = 'display: none';
 7856:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 7857:                       '<td rowspan="'.$rownum.'" valign="top">'.
 7858:                       '<p>';
 7859:         if ($lonhost eq '') {
 7860:             $datatable .= '<span class="LC_nobreak">';
 7861:             if (keys(%currbalancer) > 0) {
 7862:                 $datatable .= &mt('Add balancer:');
 7863:             } else {
 7864:                 $datatable .= &mt('Enable balancer:');
 7865:             }
 7866:             $datatable .= '&nbsp;'.
 7867:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 7868:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 7869:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 7870:                           '<option value="" selected="selected">'.&mt('None').
 7871:                           '</option>'."\n";
 7872:             foreach my $server (sort(keys(%servers))) {
 7873:                 next if ($currbalancer{$server});
 7874:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 7875:             }
 7876:             $datatable .=
 7877:                 '</select>'."\n".
 7878:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 7879:         } else {
 7880:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 7881:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 7882:                            &mt('Stop balancing').'</label>'.
 7883:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 7884:             $targets_div_style = 'display: block';
 7885:             $disabled_div_style = 'display: none';
 7886:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 7887:                 $homedom_div_style = 'display: block';
 7888:             }
 7889:         }
 7890:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 7891:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 7892:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 7893:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 7894:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 7895:         my @sparestypes = ('primary','default');
 7896:         my %typetitles = &sparestype_titles();
 7897:         my %hostherechecked = (
 7898:                                   no => ' checked="checked"',
 7899:                               );
 7900:         my %balcookiechecked = (
 7901:                                   no => ' checked="checked"',
 7902:                                );
 7903:         foreach my $sparetype (@sparestypes) {
 7904:             my $targettable;
 7905:             for (my $i=0; $i<$numspares; $i++) {
 7906:                 my $checked;
 7907:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 7908:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 7909:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 7910:                             $checked = ' checked="checked"';
 7911:                         }
 7912:                     }
 7913:                 }
 7914:                 my ($chkboxval,$disabled);
 7915:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 7916:                     $chkboxval = $spares[$i];
 7917:                 }
 7918:                 if (exists($currbalancer{$spares[$i]})) {
 7919:                     $disabled = ' disabled="disabled"';
 7920:                 }
 7921:                 $targettable .=
 7922:                     '<td><span class="LC_nobreak"><label>'.
 7923:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 7924:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 7925:                     '</span></label></span></td>';
 7926:                 my $rem = $i%($numinrow);
 7927:                 if ($rem == 0) {
 7928:                     if (($i > 0) && ($i < $numspares-1)) {
 7929:                         $targettable .= '</tr>';
 7930:                     }
 7931:                     if ($i < $numspares-1) {
 7932:                         $targettable .= '<tr>';
 7933:                     }
 7934:                 }
 7935:             }
 7936:             if ($targettable ne '') {
 7937:                 my $rem = $numspares%($numinrow);
 7938:                 my $colsleft = $numinrow - $rem;
 7939:                 if ($colsleft > 1 ) {
 7940:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7941:                                     '&nbsp;</td>';
 7942:                 } elsif ($colsleft == 1) {
 7943:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 7944:                 }
 7945:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 7946:                                '<table><tr>'.$targettable.'</tr></table><br />';
 7947:             }
 7948:             $hostherechecked{$sparetype} = '';
 7949:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 7950:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 7951:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 7952:                         $hostherechecked{$sparetype} = ' checked="checked"';
 7953:                         $hostherechecked{'no'} = '';
 7954:                     }
 7955:                 }
 7956:             }
 7957:         }
 7958:         if ($currcookies{$lonhost}) {
 7959:             %balcookiechecked = (
 7960:                                     yes => ' checked="checked"',
 7961:                                 );
 7962:         }
 7963:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 7964:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 7965:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 7966:         foreach my $sparetype (@sparestypes) {
 7967:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 7968:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 7969:                           '</i></label><br />';
 7970:         }
 7971:         $datatable .= &mt('Use balancer cookie').'<br />'.
 7972:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 7973:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 7974:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 7975:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 7976:                       '</div></td></tr>'.
 7977:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 7978:                                            $othertitle,$usertypes,$types,\%servers,
 7979:                                            \%currbalancer,$lonhost,
 7980:                                            $targets_div_style,$homedom_div_style,
 7981:                                            $css_class[$cssidx],$balnum,$islast);
 7982:         $$rowtotal += $rownum;
 7983:         $balnum ++;
 7984:     }
 7985:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 7986:     return $datatable;
 7987: }
 7988: 
 7989: sub get_loadbalancers_config {
 7990:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 7991:     return unless ((ref($servers) eq 'HASH') &&
 7992:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 7993:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 7994:                    (ref($currcookies) eq 'HASH'));
 7995:     if (keys(%{$existing}) > 0) {
 7996:         my $oldlonhost;
 7997:         foreach my $key (sort(keys(%{$existing}))) {
 7998:             if ($key eq 'lonhost') {
 7999:                 $oldlonhost = $existing->{'lonhost'};
 8000:                 $currbalancer->{$oldlonhost} = 1;
 8001:             } elsif ($key eq 'targets') {
 8002:                 if ($oldlonhost) {
 8003:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 8004:                 }
 8005:             } elsif ($key eq 'rules') {
 8006:                 if ($oldlonhost) {
 8007:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 8008:                 }
 8009:             } elsif (ref($existing->{$key}) eq 'HASH') {
 8010:                 $currbalancer->{$key} = 1;
 8011:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 8012:                 $currrules->{$key} = $existing->{$key}{'rules'};
 8013:                 if ($existing->{$key}{'cookie'}) {
 8014:                     $currcookies->{$key} = 1;
 8015:                 }
 8016:             }
 8017:         }
 8018:     } else {
 8019:         my ($balancerref,$targetsref) =
 8020:                 &Apache::lonnet::get_lonbalancer_config($servers);
 8021:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 8022:             foreach my $server (sort(keys(%{$balancerref}))) {
 8023:                 $currbalancer->{$server} = 1;
 8024:                 $currtargets->{$server} = $targetsref->{$server};
 8025:             }
 8026:         }
 8027:     }
 8028:     return;
 8029: }
 8030: 
 8031: sub loadbalancing_rules {
 8032:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 8033:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 8034:         $css_class,$balnum,$islast) = @_;
 8035:     my $output;
 8036:     my $num = 0;
 8037:     my ($alltypes,$othertypes,$titles) =
 8038:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8039:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 8040:         foreach my $type (@{$alltypes}) {
 8041:             $num ++;
 8042:             my $current;
 8043:             if (ref($currrules) eq 'HASH') {
 8044:                 $current = $currrules->{$type};
 8045:             }
 8046:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 8047:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 8048:                     $current = '';
 8049:                 }
 8050:             }
 8051:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 8052:                                              $servers,$currbalancer,$lonhost,$dom,
 8053:                                              $targets_div_style,$homedom_div_style,
 8054:                                              $css_class,$balnum,$num,$islast);
 8055:         }
 8056:     }
 8057:     return $output;
 8058: }
 8059: 
 8060: sub loadbalancing_titles {
 8061:     my ($dom,$intdom,$usertypes,$types) = @_;
 8062:     my %othertypes = (
 8063:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 8064:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 8065:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 8066:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 8067:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 8068:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 8069:                      );
 8070:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 8071:     my @available;
 8072:     if (ref($types) eq 'ARRAY') {
 8073:         @available = @{$types};
 8074:     }
 8075:     unless (grep(/^default$/,@available)) {
 8076:         push(@available,'default');
 8077:     }
 8078:     unshift(@alltypes,@available);
 8079:     my %titles;
 8080:     foreach my $type (@alltypes) {
 8081:         if ($type =~ /^_LC_/) {
 8082:             $titles{$type} = $othertypes{$type};
 8083:         } elsif ($type eq 'default') {
 8084:             $titles{$type} = &mt('All users from [_1]',$dom);
 8085:             if (ref($types) eq 'ARRAY') {
 8086:                 if (@{$types} > 0) {
 8087:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 8088:                 }
 8089:             }
 8090:         } elsif (ref($usertypes) eq 'HASH') {
 8091:             $titles{$type} = $usertypes->{$type};
 8092:         }
 8093:     }
 8094:     return (\@alltypes,\%othertypes,\%titles);
 8095: }
 8096: 
 8097: sub loadbalance_rule_row {
 8098:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 8099:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 8100:     my @rulenames;
 8101:     my %ruletitles = &offloadtype_text();
 8102:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 8103:         @rulenames = ('balancer','offloadedto','specific');
 8104:     } else {
 8105:         @rulenames = ('default','homeserver');
 8106:         if ($type eq '_LC_external') {
 8107:             push(@rulenames,'externalbalancer');
 8108:         } else {
 8109:             push(@rulenames,'specific');
 8110:         }
 8111:         push(@rulenames,'none');
 8112:     }
 8113:     my $style = $targets_div_style;
 8114:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 8115:         $style = $homedom_div_style;
 8116:     }
 8117:     my $space;
 8118:     if ($islast && $num == 1) {
 8119:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 8120:     }
 8121:     my $output =
 8122:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 8123:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 8124:         '<td valaign="top">'.$space.
 8125:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 8126:     for (my $i=0; $i<@rulenames; $i++) {
 8127:         my $rule = $rulenames[$i];
 8128:         my ($checked,$extra);
 8129:         if ($rulenames[$i] eq 'default') {
 8130:             $rule = '';
 8131:         }
 8132:         if ($rulenames[$i] eq 'specific') {
 8133:             if (ref($servers) eq 'HASH') {
 8134:                 my $default;
 8135:                 if (($current ne '') && (exists($servers->{$current}))) {
 8136:                     $checked = ' checked="checked"';
 8137:                 }
 8138:                 unless ($checked) {
 8139:                     $default = ' selected="selected"';
 8140:                 }
 8141:                 $extra =
 8142:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 8143:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 8144:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 8145:                     '<option value=""'.$default.'></option>'."\n";
 8146:                 foreach my $server (sort(keys(%{$servers}))) {
 8147:                     if (ref($currbalancer) eq 'HASH') {
 8148:                         next if (exists($currbalancer->{$server}));
 8149:                     }
 8150:                     my $selected;
 8151:                     if ($server eq $current) {
 8152:                         $selected = ' selected="selected"';
 8153:                     }
 8154:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 8155:                 }
 8156:                 $extra .= '</select>';
 8157:             }
 8158:         } elsif ($rule eq $current) {
 8159:             $checked = ' checked="checked"';
 8160:         }
 8161:         $output .= '<span class="LC_nobreak"><label>'.
 8162:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 8163:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 8164:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 8165:                    ')"'.$checked.' />&nbsp;';
 8166:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 8167:             $output .= $ruletitles{'particular'};
 8168:         } else {
 8169:             $output .= $ruletitles{$rulenames[$i]};
 8170:         }
 8171:         $output .= '</label>'.$extra.'</span><br />'."\n";
 8172:     }
 8173:     $output .= '</div></td></tr>'."\n";
 8174:     return $output;
 8175: }
 8176: 
 8177: sub offloadtype_text {
 8178:     my %ruletitles = &Apache::lonlocal::texthash (
 8179:            'default'          => 'Offloads to default destinations',
 8180:            'homeserver'       => "Offloads to user's home server",
 8181:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 8182:            'specific'         => 'Offloads to specific server',
 8183:            'none'             => 'No offload',
 8184:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 8185:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 8186:            'particular'       => 'Session hosted (after re-auth) on server:',
 8187:     );
 8188:     return %ruletitles;
 8189: }
 8190: 
 8191: sub sparestype_titles {
 8192:     my %typestitles = &Apache::lonlocal::texthash (
 8193:                           'primary' => 'primary',
 8194:                           'default' => 'default',
 8195:                       );
 8196:     return %typestitles;
 8197: }
 8198: 
 8199: sub contact_titles {
 8200:     my %titles = &Apache::lonlocal::texthash (
 8201:                    'supportemail'    => 'Support E-mail address',
 8202:                    'adminemail'      => 'Default Server Admin E-mail address',
 8203:                    'errormail'       => 'Error reports to be e-mailed to',
 8204:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 8205:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 8206:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 8207:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 8208:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 8209:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 8210:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 8211:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 8212:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 8213:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 8214:                    'errorweights'    => 'Weights used to compute error count',
 8215:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 8216:                  );
 8217:     my %short_titles = &Apache::lonlocal::texthash (
 8218:                            adminemail   => 'Admin E-mail address',
 8219:                            supportemail => 'Support E-mail',
 8220:                        );   
 8221:     return (\%titles,\%short_titles);
 8222: }
 8223: 
 8224: sub helpform_fields {
 8225:     my %titles =  &Apache::lonlocal::texthash (
 8226:                        'username'   => 'Name',
 8227:                        'user'       => 'Username/domain',
 8228:                        'phone'      => 'Phone',
 8229:                        'cc'         => 'Cc e-mail',
 8230:                        'course'     => 'Course Details',
 8231:                        'section'    => 'Sections',
 8232:                        'screenshot' => 'File upload',
 8233:     );
 8234:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 8235:     my %possoptions = (
 8236:                         username     => ['yes','no','req'],
 8237:                         phone        => ['yes','no','req'],
 8238:                         user         => ['yes','no'],
 8239:                         cc           => ['yes','no'],
 8240:                         course       => ['yes','no'],
 8241:                         section      => ['yes','no'],
 8242:                         screenshot   => ['yes','no'],
 8243:                       );
 8244:     my %fieldoptions = &Apache::lonlocal::texthash (
 8245:                          'yes'  => 'Optional',
 8246:                          'req'  => 'Required',
 8247:                          'no'   => "Not shown",
 8248:     );
 8249:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 8250: }
 8251: 
 8252: sub tool_titles {
 8253:     my %titles = &Apache::lonlocal::texthash (
 8254:                      aboutme    => 'Personal web page',
 8255:                      blog       => 'Blog',
 8256:                      webdav     => 'WebDAV',
 8257:                      portfolio  => 'Portfolio',
 8258:                      timezone   => 'Can set time zone',
 8259:                      official   => 'Official courses (with institutional codes)',
 8260:                      unofficial => 'Unofficial courses',
 8261:                      community  => 'Communities',
 8262:                      textbook   => 'Textbook courses',
 8263:                  );
 8264:     return %titles;
 8265: }
 8266: 
 8267: sub courserequest_titles {
 8268:     my %titles = &Apache::lonlocal::texthash (
 8269:                                    official   => 'Official',
 8270:                                    unofficial => 'Unofficial',
 8271:                                    community  => 'Communities',
 8272:                                    textbook   => 'Textbook',
 8273:                                    lti        => 'LTI Provider',
 8274:                                    norequest  => 'Not allowed',
 8275:                                    approval   => 'Approval by Dom. Coord.',
 8276:                                    validate   => 'With validation',
 8277:                                    autolimit  => 'Numerical limit',
 8278:                                    unlimited  => '(blank for unlimited)',
 8279:                  );
 8280:     return %titles;
 8281: }
 8282: 
 8283: sub authorrequest_titles {
 8284:     my %titles = &Apache::lonlocal::texthash (
 8285:                                    norequest  => 'Not allowed',
 8286:                                    approval   => 'Approval by Dom. Coord.',
 8287:                                    automatic  => 'Automatic approval',
 8288:                  );
 8289:     return %titles;
 8290: }
 8291: 
 8292: sub courserequest_conditions {
 8293:     my %conditions = &Apache::lonlocal::texthash (
 8294:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 8295:        validate   => '(Processing of request subject to institutional validation).',
 8296:                  );
 8297:     return %conditions;
 8298: }
 8299: 
 8300: 
 8301: sub print_usercreation {
 8302:     my ($position,$dom,$settings,$rowtotal) = @_;
 8303:     my $numinrow = 4;
 8304:     my $datatable;
 8305:     if ($position eq 'top') {
 8306:         $$rowtotal ++;
 8307:         my $rowcount = 0;
 8308:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 8309:         if (ref($rules) eq 'HASH') {
 8310:             if (keys(%{$rules}) > 0) {
 8311:                 $datatable .= &user_formats_row('username',$settings,$rules,
 8312:                                                 $ruleorder,$numinrow,$rowcount);
 8313:                 $$rowtotal ++;
 8314:                 $rowcount ++;
 8315:             }
 8316:         }
 8317:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 8318:         if (ref($idrules) eq 'HASH') {
 8319:             if (keys(%{$idrules}) > 0) {
 8320:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 8321:                                                 $idruleorder,$numinrow,$rowcount);
 8322:                 $$rowtotal ++;
 8323:                 $rowcount ++;
 8324:             }
 8325:         }
 8326:         if ($rowcount == 0) {
 8327:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 8328:             $$rowtotal ++;
 8329:             $rowcount ++;
 8330:         }
 8331:     } elsif ($position eq 'middle') {
 8332:         my @creators = ('author','course','requestcrs');
 8333:         my ($rules,$ruleorder) =
 8334:             &Apache::lonnet::inst_userrules($dom,'username');
 8335:         my %lt = &usercreation_types();
 8336:         my %checked;
 8337:         if (ref($settings) eq 'HASH') {
 8338:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 8339:                 foreach my $item (@creators) {
 8340:                     $checked{$item} = $settings->{'cancreate'}{$item};
 8341:                 }
 8342:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 8343:                 foreach my $item (@creators) {
 8344:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 8345:                         $checked{$item} = 'none';
 8346:                     }
 8347:                 }
 8348:             }
 8349:         }
 8350:         my $rownum = 0;
 8351:         foreach my $item (@creators) {
 8352:             $rownum ++;
 8353:             if ($checked{$item} eq '') {
 8354:                 $checked{$item} = 'any';
 8355:             }
 8356:             my $css_class;
 8357:             if ($rownum%2) {
 8358:                 $css_class = '';
 8359:             } else {
 8360:                 $css_class = ' class="LC_odd_row" ';
 8361:             }
 8362:             $datatable .= '<tr'.$css_class.'>'.
 8363:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 8364:                          '</span></td><td align="right">';
 8365:             my @options = ('any');
 8366:             if (ref($rules) eq 'HASH') {
 8367:                 if (keys(%{$rules}) > 0) {
 8368:                     push(@options,('official','unofficial'));
 8369:                 }
 8370:             }
 8371:             push(@options,'none');
 8372:             foreach my $option (@options) {
 8373:                 my $type = 'radio';
 8374:                 my $check = ' ';
 8375:                 if ($checked{$item} eq $option) {
 8376:                     $check = ' checked="checked" ';
 8377:                 } 
 8378:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8379:                               '<input type="'.$type.'" name="can_createuser_'.
 8380:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 8381:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 8382:             }
 8383:             $datatable .= '</td></tr>';
 8384:         }
 8385:     } else {
 8386:         my @contexts = ('author','course','domain');
 8387:         my @authtypes = ('int','krb4','krb5','loc','lti');
 8388:         my %checked;
 8389:         if (ref($settings) eq 'HASH') {
 8390:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 8391:                 foreach my $item (@contexts) {
 8392:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 8393:                         foreach my $auth (@authtypes) {
 8394:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 8395:                                 $checked{$item}{$auth} = ' checked="checked" ';
 8396:                             }
 8397:                         }
 8398:                     }
 8399:                 }
 8400:             }
 8401:         } else {
 8402:             foreach my $item (@contexts) {
 8403:                 foreach my $auth (@authtypes) {
 8404:                     $checked{$item}{$auth} = ' checked="checked" ';
 8405:                 }
 8406:             }
 8407:         }
 8408:         my %title = &context_names();
 8409:         my %authname = &authtype_names();
 8410:         my $rownum = 0;
 8411:         my $css_class; 
 8412:         foreach my $item (@contexts) {
 8413:             if ($rownum%2) {
 8414:                 $css_class = '';
 8415:             } else {
 8416:                 $css_class = ' class="LC_odd_row" ';
 8417:             }
 8418:             $datatable .=   '<tr'.$css_class.'>'.
 8419:                             '<td>'.$title{$item}.
 8420:                             '</td><td class="LC_left_item">'.
 8421:                             '<span class="LC_nobreak">';
 8422:             foreach my $auth (@authtypes) {
 8423:                 $datatable .= '<label>'. 
 8424:                               '<input type="checkbox" name="'.$item.'_auth" '.
 8425:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 8426:                               $authname{$auth}.'</label>&nbsp;';
 8427:             }
 8428:             $datatable .= '</span></td></tr>';
 8429:             $rownum ++;
 8430:         }
 8431:         $$rowtotal += $rownum;
 8432:     }
 8433:     return $datatable;
 8434: }
 8435: 
 8436: sub print_selfcreation {
 8437:     my ($position,$dom,$settings,$rowtotal) = @_;
 8438:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 8439:         $emaildomain,$datatable);
 8440:     if (ref($settings) eq 'HASH') {
 8441:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 8442:             $createsettings = $settings->{'cancreate'};
 8443:             if (ref($createsettings) eq 'HASH') {
 8444:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 8445:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 8446:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 8447:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 8448:                         @selfcreate = ('email','login','sso');
 8449:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 8450:                         @selfcreate = ($createsettings->{'selfcreate'});
 8451:                     }
 8452:                 }
 8453:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 8454:                     $processing = $createsettings->{'selfcreateprocessing'};
 8455:                 }
 8456:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 8457:                     $emailoptions = $createsettings->{'emailoptions'};
 8458:                 }
 8459:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 8460:                     $emailverified = $createsettings->{'emailverified'};
 8461:                 }
 8462:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 8463:                     $emaildomain = $createsettings->{'emaildomain'};
 8464:                 }
 8465:             }
 8466:         }
 8467:     }
 8468:     my %radiohash;
 8469:     my $numinrow = 4;
 8470:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 8471:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8472:     if ($position eq 'top') {
 8473:         my %choices = &Apache::lonlocal::texthash (
 8474:                                                       cancreate_login      => 'Institutional Login',
 8475:                                                       cancreate_sso        => 'Institutional Single Sign On',
 8476:                                                   );
 8477:         my @toggles = sort(keys(%choices));
 8478:         my %defaultchecked = (
 8479:                                'cancreate_login' => 'off',
 8480:                                'cancreate_sso'   => 'off',
 8481:                              );
 8482:         my ($onclick,$itemcount);
 8483:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 8484:                                                      \%choices,$itemcount,$onclick);
 8485:         $$rowtotal += $itemcount;
 8486: 
 8487:         if (ref($usertypes) eq 'HASH') {
 8488:             if (keys(%{$usertypes}) > 0) {
 8489:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 8490:                                              $dom,$numinrow,$othertitle,
 8491:                                              'statustocreate',$rowtotal);
 8492:                 $$rowtotal ++;
 8493:             }
 8494:         }
 8495:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 8496:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8497:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 8498:         my $rem;
 8499:         my $numperrow = 2;
 8500:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 8501:         $datatable .= '<tr'.$css_class.'>'.
 8502:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 8503:                      '<td class="LC_left_item">'."\n".
 8504:                      '<table>'."\n";
 8505:         for (my $i=0; $i<@fields; $i++) {
 8506:             $rem = $i%($numperrow);
 8507:             if ($rem == 0) {
 8508:                 if ($i > 0) {
 8509:                     $datatable .= '</tr>';
 8510:                 }
 8511:                 $datatable .= '<tr>';
 8512:             }
 8513:             my $currval;
 8514:             if (ref($createsettings) eq 'HASH') {
 8515:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 8516:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 8517:                 }
 8518:             }
 8519:             $datatable .= '<td class="LC_left_item">'.
 8520:                           '<span class="LC_nobreak">'.
 8521:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 8522:                           'value="'.$currval.'" size="10" />&nbsp;'.
 8523:                           $fieldtitles{$fields[$i]}.'</span></td>';
 8524:         }
 8525:         my $colsleft = $numperrow - $rem;
 8526:         if ($colsleft > 1 ) {
 8527:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8528:                          '&nbsp;</td>';
 8529:         } elsif ($colsleft == 1) {
 8530:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8531:         }
 8532:         $datatable .= '</tr></table></td></tr>';
 8533:         $$rowtotal ++;
 8534:     } elsif ($position eq 'middle') {
 8535:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 8536:         my @posstypes;
 8537:         if (ref($types) eq 'ARRAY') {
 8538:             @posstypes = @{$types};
 8539:         }
 8540:         unless (grep(/^default$/,@posstypes)) {
 8541:             push(@posstypes,'default');
 8542:         }
 8543:         my %usertypeshash;
 8544:         if (ref($usertypes) eq 'HASH') {
 8545:             %usertypeshash = %{$usertypes};
 8546:         }
 8547:         $usertypeshash{'default'} = $othertitle;
 8548:         foreach my $status (@posstypes) {
 8549:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 8550:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 8551:             $$rowtotal ++;
 8552:         }
 8553:     } else {
 8554:         my %choices = &Apache::lonlocal::texthash (
 8555:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 8556:                                                   );
 8557:         my @toggles = sort(keys(%choices));
 8558:         my %defaultchecked = (
 8559:                                'cancreate_email' => 'off',
 8560:                              );
 8561:         my $customclass = 'LC_selfcreate_email';
 8562:         my $classprefix = 'LC_canmodify_emailusername_';
 8563:         my $optionsprefix = 'LC_options_emailusername_';
 8564:         my $display = 'none';
 8565:         my $rowstyle = 'display:none';
 8566:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 8567:             $display = 'block';
 8568:             $rowstyle = 'display:table-row';
 8569:         }
 8570:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 8571:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 8572:                                                      \%choices,$$rowtotal,$onclick);
 8573:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 8574:                                          $rowstyle);
 8575:         $$rowtotal ++;
 8576:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 8577:                                       $rowstyle);
 8578:         $$rowtotal ++;
 8579:         my (@ordered,@posstypes,%usertypeshash);
 8580:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 8581:         my ($emailrules,$emailruleorder) =
 8582:             &Apache::lonnet::inst_userrules($dom,'email');
 8583:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8584:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8585:         if (ref($types) eq 'ARRAY') {
 8586:             @posstypes = @{$types};
 8587:         }
 8588:         if (@posstypes) {
 8589:             unless (grep(/^default$/,@posstypes)) {
 8590:                 push(@posstypes,'default');
 8591:             }
 8592:             if (ref($usertypes) eq 'HASH') {
 8593:                 %usertypeshash = %{$usertypes};
 8594:             }
 8595:             my $currassign;
 8596:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 8597:                 $currassign = {
 8598:                                   selfassign => $domdefaults{'inststatusguest'},
 8599:                               };
 8600:                 @ordered = @{$domdefaults{'inststatusguest'}};
 8601:             } else {
 8602:                 $currassign = { selfassign => [] };
 8603:             }
 8604:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 8605:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 8606:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 8607:                                          $numinrow,$othertitle,'selfassign',
 8608:                                          $rowtotal,$onclicktypes,$customclass,
 8609:                                          $rowstyle);
 8610:             $$rowtotal ++;
 8611:             $usertypeshash{'default'} = $othertitle;
 8612:             foreach my $status (@posstypes) {
 8613:                 my $css_class;
 8614:                 if ($$rowtotal%2) {
 8615:                     $css_class = 'LC_odd_row ';
 8616:                 }
 8617:                 $css_class .= $customclass;
 8618:                 my $rowid = $optionsprefix.$status;
 8619:                 my $hidden = 1;
 8620:                 my $currstyle = 'display:none';
 8621:                 if (grep(/^\Q$status\E$/,@ordered)) {
 8622:                     $currstyle = $rowstyle;
 8623:                     $hidden = 0;
 8624:                 }
 8625:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 8626:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 8627:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 8628:                 unless ($hidden) {
 8629:                     $$rowtotal ++;
 8630:                 }
 8631:             }
 8632:         } else {
 8633:             my $css_class;
 8634:             if ($$rowtotal%2) {
 8635:                 $css_class = 'LC_odd_row ';
 8636:             }
 8637:             $css_class .= $customclass;
 8638:             $usertypeshash{'default'} = $othertitle;
 8639:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 8640:                                          $emailrules,$emailruleorder,$settings,'default','',
 8641:                                          $othertitle,$css_class,$rowstyle,$intdom);
 8642:             $$rowtotal ++;
 8643:         }
 8644:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8645:         $numinrow = 1;
 8646:         if (@posstypes) {
 8647:             foreach my $status (@posstypes) {
 8648:                 my $rowid = $classprefix.$status;
 8649:                 my $datarowstyle = 'display:none';
 8650:                 if (grep(/^\Q$status\E$/,@ordered)) {
 8651:                     $datarowstyle = $rowstyle;
 8652:                 }
 8653:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 8654:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 8655:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 8656:                 unless ($datarowstyle eq 'display:none') {
 8657:                     $$rowtotal ++;
 8658:                 }
 8659:             }
 8660:         } else {
 8661:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 8662:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 8663:                                                    $infotitles,'',$customclass,$rowstyle);
 8664:         }
 8665:     }
 8666:     return $datatable;
 8667: }
 8668: 
 8669: sub selfcreate_javascript {
 8670:     return <<"ENDSCRIPT";
 8671: 
 8672: <script type="text/javascript">
 8673: // <![CDATA[
 8674: 
 8675: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 8676:     var x = document.getElementsByClassName(target);
 8677:     var insttypes = 0;
 8678:     var insttypeRegExp = new RegExp(prefix);
 8679:     if ((x.length != undefined) && (x.length > 0)) {
 8680:         if (form.elements[radio].length != undefined) {
 8681:             for (var i=0; i<form.elements[radio].length; i++) {
 8682:                 if (form.elements[radio][i].checked) {
 8683:                     if (form.elements[radio][i].value == 1) {
 8684:                         for (var j=0; j<x.length; j++) {
 8685:                             if (x[j].id == 'undefined') {
 8686:                                 x[j].style.display = 'table-row';
 8687:                             } else if (insttypeRegExp.test(x[j].id)) {
 8688:                                 insttypes ++;
 8689:                             } else {
 8690:                                 x[j].style.display = 'table-row';
 8691:                             }
 8692:                         }
 8693:                     } else {
 8694:                         for (var j=0; j<x.length; j++) {
 8695:                             x[j].style.display = 'none';
 8696:                         }
 8697:                     }
 8698:                     break;
 8699:                 }
 8700:             }
 8701:             if (insttypes > 0) {
 8702:                 toggleDataRow(form,checkbox,target,altprefix);
 8703:                 toggleDataRow(form,checkbox,target,prefix,1);
 8704:             }
 8705:         }
 8706:     }
 8707:     return;
 8708: }
 8709: 
 8710: function toggleDataRow(form,checkbox,target,prefix,docount) {
 8711:     if (form.elements[checkbox].length != undefined) {
 8712:         var count = 0;
 8713:         if (docount) {
 8714:             for (var i=0; i<form.elements[checkbox].length; i++) {
 8715:                 if (form.elements[checkbox][i].checked) {
 8716:                     count ++;
 8717:                 }
 8718:             }
 8719:         }
 8720:         for (var i=0; i<form.elements[checkbox].length; i++) {
 8721:             var type = form.elements[checkbox][i].value;
 8722:             if (document.getElementById(prefix+type)) {
 8723:                 if (form.elements[checkbox][i].checked) {
 8724:                     document.getElementById(prefix+type).style.display = 'table-row';
 8725:                     if (count % 2 == 1) {
 8726:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 8727:                     } else {
 8728:                         document.getElementById(prefix+type).className = target;
 8729:                     }
 8730:                     count ++;
 8731:                 } else {
 8732:                     document.getElementById(prefix+type).style.display = 'none';
 8733:                 }
 8734:             }
 8735:         }
 8736:     }
 8737:     return;
 8738: }
 8739: 
 8740: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 8741:     var caller = radio+'_'+status;
 8742:     if (form.elements[caller].length != undefined) {
 8743:         for (var i=0; i<form.elements[caller].length; i++) {
 8744:             if (form.elements[caller][i].checked) {
 8745:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 8746:                     var curr = form.elements[caller][i].value;
 8747:                     if (prefix) {
 8748:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 8749:                     }
 8750:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 8751:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 8752:                     if (curr == 'custom') {
 8753:                         if (prefix) {
 8754:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 8755:                         }
 8756:                     } else if (curr == 'inst') {
 8757:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 8758:                     } else if (curr == 'noninst') {
 8759:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 8760:                     }
 8761:                     break;
 8762:                 }
 8763:             }
 8764:         }
 8765:     }
 8766: }
 8767: 
 8768: // ]]>
 8769: </script>
 8770: 
 8771: ENDSCRIPT
 8772: }
 8773: 
 8774: sub noninst_users {
 8775:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 8776:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
 8777:     my $class = 'LC_left_item';
 8778:     if ($css_class) {
 8779:         $css_class = ' class="'.$css_class.'"';
 8780:     }
 8781:     if ($rowid) {
 8782:         $rowid = ' id="'.$rowid.'"';
 8783:     }
 8784:     if ($rowstyle) {
 8785:         $rowstyle = ' style="'.$rowstyle.'"';
 8786:     }
 8787:     my ($output,$description);
 8788:     if ($type eq 'default') {
 8789:         $description = &mt('Requests for: [_1]',$typetitle);
 8790:     } else {
 8791:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 8792:     }
 8793:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 8794:               "<td>$description</td>\n".
 8795:               '<td class="'.$class.'" colspan="2">'.
 8796:               '<table><tr>';
 8797:     my %headers = &Apache::lonlocal::texthash(
 8798:               approve  => 'Processing',
 8799:               email    => 'E-mail',
 8800:               username => 'Username',
 8801:     );
 8802:     foreach my $item ('approve','email','username') {
 8803:         $output .= '<th>'.$headers{$item}.'</th>';
 8804:     }
 8805:     $output .= '</tr><tr>';
 8806:     foreach my $item ('approve','email','username') {
 8807:         $output .= '<td valign="top">';
 8808:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 8809:         if ($item eq 'approve') {
 8810:             %choices = &Apache::lonlocal::texthash (
 8811:                                                      automatic => 'Automatically approved',
 8812:                                                      approval  => 'Queued for approval',
 8813:                                                    );
 8814:             @options = ('automatic','approval');
 8815:             $hashref = $processing;
 8816:             $defoption = 'automatic';
 8817:             $name = 'cancreate_emailprocess_'.$type;
 8818:         } elsif ($item eq 'email') {
 8819:             %choices = &Apache::lonlocal::texthash (
 8820:                                                      any     => 'Any e-mail',
 8821:                                                      inst    => 'Institutional only',
 8822:                                                      noninst => 'Non-institutional only',
 8823:                                                      custom  => 'Custom restrictions',
 8824:                                                    );
 8825:             @options = ('any','inst','noninst');
 8826:             my $showcustom;
 8827:             if (ref($emailrules) eq 'HASH') {
 8828:                 if (keys(%{$emailrules}) > 0) {
 8829:                     push(@options,'custom');
 8830:                     $showcustom = 'cancreate_emailrule';
 8831:                     if (ref($settings) eq 'HASH') {
 8832:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 8833:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 8834:                                 if (exists($emailrules->{$rule})) {
 8835:                                     $hascustom ++;
 8836:                                 }
 8837:                             }
 8838:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 8839:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 8840:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 8841:                                     if (exists($emailrules->{$rule})) {
 8842:                                         $hascustom ++;
 8843:                                     }
 8844:                                 }
 8845:                             }
 8846:                         }
 8847:                     }
 8848:                 }
 8849:             }
 8850:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 8851:                                                      "'cancreate_emaildomain','$type'".');"';
 8852:             $hashref = $emailoptions;
 8853:             $defoption = 'any';
 8854:             $name = 'cancreate_emailoptions_'.$type;
 8855:         } elsif ($item eq 'username') {
 8856:             %choices = &Apache::lonlocal::texthash (
 8857:                                                      all    => 'Same as e-mail',
 8858:                                                      first  => 'Omit @domain',
 8859:                                                      free   => 'Free to choose',
 8860:                                                    );
 8861:             @options = ('all','first','free');
 8862:             $hashref = $emailverified;
 8863:             $defoption = 'all';
 8864:             $name = 'cancreate_usernameoptions_'.$type;
 8865:         }
 8866:         foreach my $option (@options) {
 8867:             my $checked;
 8868:             if (ref($hashref) eq 'HASH') {
 8869:                 if ($type eq '') {
 8870:                     if (!exists($hashref->{'default'})) {
 8871:                         if ($option eq $defoption) {
 8872:                             $checked = ' checked="checked"';
 8873:                         }
 8874:                     } else {
 8875:                         if ($hashref->{'default'} eq $option) {
 8876:                             $checked = ' checked="checked"';
 8877:                         }
 8878:                     }
 8879:                 } else {
 8880:                     if (!exists($hashref->{$type})) {
 8881:                         if ($option eq $defoption) {
 8882:                             $checked = ' checked="checked"';
 8883:                         }
 8884:                     } else {
 8885:                         if ($hashref->{$type} eq $option) {
 8886:                             $checked = ' checked="checked"';
 8887:                         }
 8888:                     }
 8889:                 }
 8890:             } elsif (($item eq 'email') && ($hascustom)) {
 8891:                 if ($option eq 'custom') {
 8892:                     $checked = ' checked="checked"';
 8893:                 }
 8894:             } elsif ($option eq $defoption) {
 8895:                 $checked = ' checked="checked"';
 8896:             }
 8897:             $output .= '<span class="LC_nobreak"><label>'.
 8898:                        '<input type="radio" name="'.$name.'"'.
 8899:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 8900:                        $choices{$option}.'</label></span><br />';
 8901:             if ($item eq 'email') {
 8902:                 if ($option eq 'custom') {
 8903:                     my $id = 'cancreate_emailrule_'.$type;
 8904:                     my $display = 'none';
 8905:                     if ($checked) {
 8906:                         $display = 'inline';
 8907:                     }
 8908:                     my $numinrow = 2;
 8909:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 8910:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 8911:                                &user_formats_row('email',$settings,$emailrules,
 8912:                                                  $emailruleorder,$numinrow,'',$type);
 8913:                               '</table></fieldset>';
 8914:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 8915:                     my %text = &Apache::lonlocal::texthash (
 8916:                                                              inst    => 'must end:',
 8917:                                                              noninst => 'cannot end:',
 8918:                                                            );
 8919:                     my $value;
 8920:                     if (ref($emaildomain) eq 'HASH') {
 8921:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 8922:                             $value = $emaildomain->{$type}->{$option};
 8923:                         }
 8924:                     }
 8925:                     if ($value eq '') {
 8926:                         $value = '@'.$intdom;
 8927:                     }
 8928:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 8929:                     my $display = 'none';
 8930:                     if ($checked) {
 8931:                         $display = 'inline';
 8932:                     }
 8933:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 8934:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 8935:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 8936:                                '</div>';
 8937:                 }
 8938:             }
 8939:         }
 8940:         $output .= '</td>'."\n";
 8941:     }
 8942:     $output .= "</tr></table></td></tr>\n";
 8943:     return $output;
 8944: }
 8945: 
 8946: sub captcha_choice {
 8947:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 8948:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 8949:         $vertext,$currver);
 8950:     my %lt = &captcha_phrases();
 8951:     $keyentry = 'hidden';
 8952:     my $colspan=2;
 8953:     if ($context eq 'cancreate') {
 8954:         $rowname = &mt('CAPTCHA validation');
 8955:     } elsif ($context eq 'login') {
 8956:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 8957:     } elsif ($context eq 'passwords') {
 8958:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 8959:         $colspan=1;
 8960:     }
 8961:     if (ref($settings) eq 'HASH') {
 8962:         if ($settings->{'captcha'}) {
 8963:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 8964:         } else {
 8965:             $checked{'original'} = ' checked="checked"';
 8966:         }
 8967:         if ($settings->{'captcha'} eq 'recaptcha') {
 8968:             $pubtext = $lt{'pub'};
 8969:             $privtext = $lt{'priv'};
 8970:             $keyentry = 'text';
 8971:             $vertext = $lt{'ver'};
 8972:             $currver = $settings->{'recaptchaversion'};
 8973:             if ($currver ne '2') {
 8974:                 $currver = 1;
 8975:             }
 8976:         }
 8977:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 8978:             $currpub = $settings->{'recaptchakeys'}{'public'};
 8979:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 8980:         }
 8981:     } else {
 8982:         $checked{'original'} = ' checked="checked"';
 8983:     }
 8984:     my $css_class;
 8985:     if ($itemcount%2) {
 8986:         $css_class = 'LC_odd_row';
 8987:     }
 8988:     if ($customcss) {
 8989:         $css_class .= " $customcss";
 8990:     }
 8991:     $css_class =~ s/^\s+//;
 8992:     if ($css_class) {
 8993:         $css_class = ' class="'.$css_class.'"';
 8994:     }
 8995:     if ($rowstyle) {
 8996:         $css_class .= ' style="'.$rowstyle.'"';
 8997:     }
 8998:     my $output = '<tr'.$css_class.'>'.
 8999:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 9000:                  '<table><tr><td>'."\n";
 9001:     foreach my $option ('original','recaptcha','notused') {
 9002:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 9003:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 9004:                    $lt{$option}.'</label></span>';
 9005:         unless ($option eq 'notused') {
 9006:             $output .= ('&nbsp;'x2)."\n";
 9007:         }
 9008:     }
 9009: #
 9010: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 9011: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 9012: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 9013: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 9014: #
 9015:     $output .= '</td></tr>'."\n".
 9016:                '<tr><td class="LC_zero_height">'."\n".
 9017:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 9018:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 9019:                $currpub.'" size="40" /></span><br />'."\n".
 9020:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 9021:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 9022:                $currpriv.'" size="40" /></span><br />'.
 9023:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 9024:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 9025:                $currver.'" size="3" /></span><br />'.
 9026:                '</td></tr></table>'."\n".
 9027:                '</td></tr>';
 9028:     return $output;
 9029: }
 9030: 
 9031: sub user_formats_row {
 9032:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 9033:     my $output;
 9034:     my %text = (
 9035:                    'username' => 'new usernames',
 9036:                    'id'       => 'IDs',
 9037:                );
 9038:     unless (($type eq 'email') || ($type eq 'unamemap')) {
 9039:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 9040:         $output = '<tr '.$css_class.'>'.
 9041:                   '<td><span class="LC_nobreak">'.
 9042:                   &mt("Format rules to check for $text{$type}: ").
 9043:                   '</td><td class="LC_left_item" colspan="2"><table>';
 9044:     }
 9045:     my $rem;
 9046:     if (ref($ruleorder) eq 'ARRAY') {
 9047:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 9048:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 9049:                 my $rem = $i%($numinrow);
 9050:                 if ($rem == 0) {
 9051:                     if ($i > 0) {
 9052:                         $output .= '</tr>';
 9053:                     }
 9054:                     $output .= '<tr>';
 9055:                 }
 9056:                 my $check = ' ';
 9057:                 if (ref($settings) eq 'HASH') {
 9058:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 9059:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 9060:                             $check = ' checked="checked" ';
 9061:                         }
 9062:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 9063:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 9064:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 9065:                                 $check = ' checked="checked" ';
 9066:                             }
 9067:                         }
 9068:                     }
 9069:                 }
 9070:                 my $name = $type.'_rule';
 9071:                 if ($type eq 'email') {
 9072:                     $name .= '_'.$status;
 9073:                 }
 9074:                 $output .= '<td class="LC_left_item">'.
 9075:                            '<span class="LC_nobreak"><label>'.
 9076:                            '<input type="checkbox" name="'.$name.'" '.
 9077:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 9078:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 9079:             }
 9080:         }
 9081:         $rem = @{$ruleorder}%($numinrow);
 9082:     }
 9083:     my $colsleft;
 9084:     if ($rem) {
 9085:         $colsleft = $numinrow - $rem;
 9086:     }
 9087:     if ($colsleft > 1 ) {
 9088:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9089:                    '&nbsp;</td>';
 9090:     } elsif ($colsleft == 1) {
 9091:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 9092:     }
 9093:     $output .= '</tr>';
 9094:     unless (($type eq 'email') || ($type eq 'unamemap')) {
 9095:         $output .= '</table></td></tr>';
 9096:     }
 9097:     return $output;
 9098: }
 9099: 
 9100: sub usercreation_types {
 9101:     my %lt = &Apache::lonlocal::texthash (
 9102:                     author     => 'When adding a co-author',
 9103:                     course     => 'When adding a user to a course',
 9104:                     requestcrs => 'When requesting a course',
 9105:                     any        => 'Any',
 9106:                     official   => 'Institutional only ',
 9107:                     unofficial => 'Non-institutional only',
 9108:                     none       => 'None',
 9109:     );
 9110:     return %lt;
 9111: }
 9112: 
 9113: sub selfcreation_types {
 9114:     my %lt = &Apache::lonlocal::texthash (
 9115:                     selfcreate => 'User creates own account',
 9116:                     any        => 'Any',
 9117:                     official   => 'Institutional only ',
 9118:                     unofficial => 'Non-institutional only',
 9119:                     email      => 'E-mail address',
 9120:                     login      => 'Institutional Login',
 9121:                     sso        => 'SSO',
 9122:              );
 9123: }
 9124: 
 9125: sub authtype_names {
 9126:     my %lt = &Apache::lonlocal::texthash(
 9127:                       int    => 'Internal',
 9128:                       krb4   => 'Kerberos 4',
 9129:                       krb5   => 'Kerberos 5',
 9130:                       loc    => 'Local',
 9131:                       lti    => 'LTI',
 9132:                   );
 9133:     return %lt;
 9134: }
 9135: 
 9136: sub context_names {
 9137:     my %context_title = &Apache::lonlocal::texthash(
 9138:        author => 'Creating users when an Author',
 9139:        course => 'Creating users when in a course',
 9140:        domain => 'Creating users when a Domain Coordinator',
 9141:     );
 9142:     return %context_title;
 9143: }
 9144: 
 9145: sub print_usermodification {
 9146:     my ($position,$dom,$settings,$rowtotal) = @_;
 9147:     my $numinrow = 4;
 9148:     my ($context,$datatable,$rowcount);
 9149:     if ($position eq 'top') {
 9150:         $rowcount = 0;
 9151:         $context = 'author'; 
 9152:         foreach my $role ('ca','aa') {
 9153:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 9154:                                                    $numinrow,$rowcount);
 9155:             $$rowtotal ++;
 9156:             $rowcount ++;
 9157:         }
 9158:     } elsif ($position eq 'bottom') {
 9159:         $context = 'course';
 9160:         $rowcount = 0;
 9161:         foreach my $role ('st','ep','ta','in','cr') {
 9162:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 9163:                                                    $numinrow,$rowcount);
 9164:             $$rowtotal ++;
 9165:             $rowcount ++;
 9166:         }
 9167:     }
 9168:     return $datatable;
 9169: }
 9170: 
 9171: sub print_defaults {
 9172:     my ($position,$dom,$settings,$rowtotal) = @_;
 9173:     my $rownum = 0;
 9174:     my ($datatable,$css_class,$titles);
 9175:     unless ($position eq 'bottom') {
 9176:         $titles = &defaults_titles($dom);
 9177:     }
 9178:     if ($position eq 'top') {
 9179:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 9180:                      'datelocale_def','portal_def');
 9181:         my %defaults;
 9182:         if (ref($settings) eq 'HASH') {
 9183:             %defaults = %{$settings};
 9184:         } else {
 9185:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9186:             foreach my $item (@items) {
 9187:                 $defaults{$item} = $domdefaults{$item};
 9188:             }
 9189:         }
 9190:         foreach my $item (@items) {
 9191:             if ($rownum%2) {
 9192:                 $css_class = '';
 9193:             } else {
 9194:                 $css_class = ' class="LC_odd_row" ';
 9195:             }
 9196:             $datatable .= '<tr'.$css_class.'>'.
 9197:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 9198:                           '</span></td><td class="LC_right_item" colspan="3">';
 9199:             if ($item eq 'auth_def') {
 9200:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
 9201:                 my %shortauth = (
 9202:                                  internal => 'int',
 9203:                                  krb4 => 'krb4',
 9204:                                  krb5 => 'krb5',
 9205:                                  localauth  => 'loc',
 9206:                                  lti => 'lti',
 9207:                                 );
 9208:                 my %authnames = &authtype_names();
 9209:                 foreach my $auth (@authtypes) {
 9210:                     my $checked = ' ';
 9211:                     if ($defaults{$item} eq $auth) {
 9212:                         $checked = ' checked="checked" ';
 9213:                     }
 9214:                     $datatable .= '<label><input type="radio" name="'.$item.
 9215:                                   '" value="'.$auth.'"'.$checked.'/>'.
 9216:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 9217:                 }
 9218:             } elsif ($item eq 'timezone_def') {
 9219:                 my $includeempty = 1;
 9220:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 9221:             } elsif ($item eq 'datelocale_def') {
 9222:                 my $includeempty = 1;
 9223:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 9224:             } elsif ($item eq 'lang_def') {
 9225:                 my $includeempty = 1;
 9226:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 9227:             } elsif ($item eq 'portal_def') {
 9228:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 9229:                               $defaults{$item}.'" size="25" onkeyup="portalExtras(this);" />';
 9230:                 my $portalsty = 'none';
 9231:                 if ($defaults{$item}) {
 9232:                     $portalsty = 'block';
 9233:                 }
 9234:                 foreach my $field ('email','web') {
 9235:                     my $checkedoff = ' checked="checked"';
 9236:                     my $checkedon;
 9237:                     if ($defaults{$item.'_'.$field}) {
 9238:                         $checkedon = $checkedoff;
 9239:                         $checkedoff = '';
 9240:                     }
 9241:                     $datatable .= '<div id="'.$item.'_'.$field.'_div" style="display:'.$portalsty.'">'.
 9242:                               '<span class="LC_nobreak">'.$titles->{$field}.'&nbsp;'.
 9243:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="1"'.$checkedon.'/>'.&mt('Yes').'</label>'.
 9244:                               ('&nbsp;'x2).
 9245:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="0"'.$checkedoff.'/>'.&mt('No').'</label>'.
 9246:                               '</div>';
 9247:                 }
 9248:             } else {
 9249:                 $datatable .= '<input type="text" name="'.$item.'" value="'.$defaults{$item}.'" />';
 9250:             }
 9251:             $datatable .= '</td></tr>';
 9252:             $rownum ++;
 9253:         }
 9254:     } elsif ($position eq 'middle') {
 9255:         my %defaults;
 9256:         if (ref($settings) eq 'HASH') {
 9257:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 9258:                 my $maxnum = @{$settings->{'inststatusorder'}};
 9259:                 for (my $i=0; $i<$maxnum; $i++) {
 9260:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 9261:                     my $item = $settings->{'inststatusorder'}->[$i];
 9262:                     my $title = $settings->{'inststatustypes'}->{$item};
 9263:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 9264:                     $datatable .= '<tr'.$css_class.'>'.
 9265:                                   '<td><span class="LC_nobreak">'.
 9266:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 9267:                     for (my $k=0; $k<=$maxnum; $k++) {
 9268:                         my $vpos = $k+1;
 9269:                         my $selstr;
 9270:                         if ($k == $i) {
 9271:                             $selstr = ' selected="selected" ';
 9272:                         }
 9273:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9274:                     }
 9275:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 9276:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 9277:                                   &mt('delete').'</span></td>'.
 9278:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed').':'.
 9279:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 9280:                                   '</span></td></tr>';
 9281:                 }
 9282:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 9283:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 9284:                 $datatable .= '<tr '.$css_class.'>'.
 9285:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 9286:                 for (my $k=0; $k<=$maxnum; $k++) {
 9287:                     my $vpos = $k+1;
 9288:                     my $selstr;
 9289:                     if ($k == $maxnum) {
 9290:                         $selstr = ' selected="selected" ';
 9291:                     }
 9292:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9293:                 }
 9294:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 9295:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 9296:                               '&nbsp;'.&mt('(new)').
 9297:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 9298:                               &mt('Name displayed').':'.
 9299:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 9300:                               '</tr>'."\n";
 9301:                 $rownum ++;
 9302:             }
 9303:         }
 9304:     } else {
 9305:         my ($unamemaprules,$ruleorder) =
 9306:             &Apache::lonnet::inst_userrules($dom,'unamemap');
 9307:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 9308:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
 9309:             my $numinrow = 2;
 9310:             $datatable .= '<tr'.$css_class.'><td>'.&mt('Available conversions').'</td><td><table>'.
 9311:                           &user_formats_row('unamemap',$settings,$unamemaprules,
 9312:                                             $ruleorder,$numinrow).
 9313:                           '</table></td></tr>';
 9314:         }
 9315:         if ($datatable eq '') {
 9316:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 9317:                           &mt('No rules set for domain in customized localenroll.pm').
 9318:                           '</td></tr>';
 9319:         }
 9320:     }
 9321:     $$rowtotal += $rownum;
 9322:     return $datatable;
 9323: }
 9324: 
 9325: sub get_languages_hash {
 9326:     my %langchoices;
 9327:     foreach my $id (&Apache::loncommon::languageids()) {
 9328:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 9329:         if ($code ne '') {
 9330:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 9331:         }
 9332:     }
 9333:     return %langchoices;
 9334: }
 9335: 
 9336: sub defaults_titles {
 9337:     my ($dom) = @_;
 9338:     my %titles = &Apache::lonlocal::texthash (
 9339:                    'auth_def'      => 'Default authentication type',
 9340:                    'auth_arg_def'  => 'Default authentication argument',
 9341:                    'lang_def'      => 'Default language',
 9342:                    'timezone_def'  => 'Default timezone',
 9343:                    'datelocale_def' => 'Default locale for dates',
 9344:                    'portal_def'     => 'Portal/Default URL',
 9345:                    'email'          => 'Email links use portal URL',
 9346:                    'web'            => 'Public web links use portal URL',
 9347:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 9348:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 9349:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 9350:                  );
 9351:     if ($dom) {
 9352:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 9353:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 9354:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 9355:         $protocol = 'http' if ($protocol ne 'https');
 9356:         if ($uint_dom) {
 9357:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 9358:                                          $uint_dom);
 9359:         }
 9360:     }
 9361:     return (\%titles);
 9362: }
 9363: 
 9364: sub print_scantron {
 9365:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 9366:     if ($position eq 'top') {
 9367:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 9368:     } else {
 9369:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 9370:     }
 9371: }
 9372: 
 9373: sub scantron_javascript {
 9374:     return <<"ENDSCRIPT";
 9375: 
 9376: <script type="text/javascript">
 9377: // <![CDATA[
 9378: 
 9379: function toggleScantron(form) {
 9380:     var csvfieldset = new Array();
 9381:     if (document.getElementById('scantroncsv_cols')) {
 9382:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 9383:     }
 9384:     if (document.getElementById('scantroncsv_options')) {
 9385:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 9386:     }
 9387:     if (csvfieldset.length) {
 9388:         if (document.getElementById('scantronconfcsv')) {
 9389:             var scantroncsv = document.getElementById('scantronconfcsv');
 9390:             if (scantroncsv.checked) {
 9391:                 for (var i=0; i<csvfieldset.length; i++) {
 9392:                     csvfieldset[i].style.display = 'block';
 9393:                 }
 9394:             } else {
 9395:                 for (var i=0; i<csvfieldset.length; i++) {
 9396:                     csvfieldset[i].style.display = 'none';
 9397:                 }
 9398:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 9399:                 if (csvselects.length) {
 9400:                     for (var j=0; j<csvselects.length; j++) {
 9401:                         csvselects[j].selectedIndex = 0;
 9402:                     }
 9403:                 }
 9404:             }
 9405:         }
 9406:     }
 9407:     return;
 9408: }
 9409: // ]]>
 9410: </script>
 9411: 
 9412: ENDSCRIPT
 9413: 
 9414: }
 9415: 
 9416: sub print_scantronformat {
 9417:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 9418:     my $itemcount = 1;
 9419:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 9420:         %confhash);
 9421:     my $switchserver = &check_switchserver($dom,$confname);
 9422:     my %lt = &Apache::lonlocal::texthash (
 9423:                 default => 'Default bubblesheet format file error',
 9424:                 custom  => 'Custom bubblesheet format file error',
 9425:              );
 9426:     my %scantronfiles = (
 9427:         default => 'default.tab',
 9428:         custom => 'custom.tab',
 9429:     );
 9430:     foreach my $key (keys(%scantronfiles)) {
 9431:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 9432:                               .$scantronfiles{$key};
 9433:     }
 9434:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 9435:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 9436:         if (!$switchserver) {
 9437:             my $servadm = $r->dir_config('lonAdmEMail');
 9438:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 9439:             if ($configuserok eq 'ok') {
 9440:                 if ($author_ok eq 'ok') {
 9441:                     my %legacyfile = (
 9442:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 9443:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 9444:                     );
 9445:                     my %md5chk;
 9446:                     foreach my $type (keys(%legacyfile)) {
 9447:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 9448:                         chomp($md5chk{$type});
 9449:                     }
 9450:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 9451:                         foreach my $type (keys(%legacyfile)) {
 9452:                             ($scantronurls{$type},my $error) =
 9453:                                 &legacy_scantronformat($r,$dom,$confname,
 9454:                                                  $type,$legacyfile{$type},
 9455:                                                  $scantronurls{$type},
 9456:                                                  $scantronfiles{$type});
 9457:                             if ($error ne '') {
 9458:                                 $error{$type} = $error;
 9459:                             }
 9460:                         }
 9461:                         if (keys(%error) == 0) {
 9462:                             $is_custom = 1;
 9463:                             $confhash{'scantron'}{'scantronformat'} =
 9464:                                 $scantronurls{'custom'};
 9465:                             my $putresult =
 9466:                                 &Apache::lonnet::put_dom('configuration',
 9467:                                                          \%confhash,$dom);
 9468:                             if ($putresult ne 'ok') {
 9469:                                 $error{'custom'} =
 9470:                                     '<span class="LC_error">'.
 9471:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 9472:                             }
 9473:                         }
 9474:                     } else {
 9475:                         ($scantronurls{'default'},my $error) =
 9476:                             &legacy_scantronformat($r,$dom,$confname,
 9477:                                           'default',$legacyfile{'default'},
 9478:                                           $scantronurls{'default'},
 9479:                                           $scantronfiles{'default'});
 9480:                         if ($error eq '') {
 9481:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 9482:                             my $putresult =
 9483:                                 &Apache::lonnet::put_dom('configuration',
 9484:                                                          \%confhash,$dom);
 9485:                             if ($putresult ne 'ok') {
 9486:                                 $error{'default'} =
 9487:                                     '<span class="LC_error">'.
 9488:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 9489:                             }
 9490:                         } else {
 9491:                             $error{'default'} = $error;
 9492:                         }
 9493:                     }
 9494:                 }
 9495:             }
 9496:         } else {
 9497:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 9498:         }
 9499:     }
 9500:     if (ref($settings) eq 'HASH') {
 9501:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 9502:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 9503:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 9504:                 $scantronurl = '';
 9505:             } else {
 9506:                 $scantronurl = $settings->{'scantronformat'};
 9507:             }
 9508:             $is_custom = 1;
 9509:         } else {
 9510:             $scantronurl = $scantronurls{'default'};
 9511:         }
 9512:     } else {
 9513:         if ($is_custom) {
 9514:             $scantronurl = $scantronurls{'custom'};
 9515:         } else {
 9516:             $scantronurl = $scantronurls{'default'};
 9517:         }
 9518:     }
 9519:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9520:     $datatable .= '<tr'.$css_class.'>';
 9521:     if (!$is_custom) {
 9522:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 9523:                       '<span class="LC_nobreak">';
 9524:         if ($scantronurl) {
 9525:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 9526:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 9527:         } else {
 9528:             $datatable = &mt('File unavailable for display');
 9529:         }
 9530:         $datatable .= '</span></td>';
 9531:         if (keys(%error) == 0) { 
 9532:             $datatable .= '<td valign="bottom">';
 9533:             if (!$switchserver) {
 9534:                 $datatable .= &mt('Upload:').'<br />';
 9535:             }
 9536:         } else {
 9537:             my $errorstr;
 9538:             foreach my $key (sort(keys(%error))) {
 9539:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 9540:             }
 9541:             $datatable .= '<td>'.$errorstr;
 9542:         }
 9543:     } else {
 9544:         if (keys(%error) > 0) {
 9545:             my $errorstr;
 9546:             foreach my $key (sort(keys(%error))) {
 9547:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 9548:             } 
 9549:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 9550:         } elsif ($scantronurl) {
 9551:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 9552:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 9553:             $datatable .= '<td><span class="LC_nobreak">'.
 9554:                           $link.
 9555:                           '<label><input type="checkbox" name="scantronformat_del"'.
 9556:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 9557:                           '<td><span class="LC_nobreak">&nbsp;'.
 9558:                           &mt('Replace:').'</span><br />';
 9559:         }
 9560:     }
 9561:     if (keys(%error) == 0) {
 9562:         if ($switchserver) {
 9563:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 9564:         } else {
 9565:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 9566:                          '<input type="file" name="scantronformat" /></span>';
 9567:         }
 9568:     }
 9569:     $datatable .= '</td></tr>';
 9570:     $$rowtotal ++;
 9571:     return $datatable;
 9572: }
 9573: 
 9574: sub legacy_scantronformat {
 9575:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 9576:     my ($url,$error);
 9577:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 9578:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 9579:         my $modified = [];
 9580:         (my $result,$url) =
 9581:             &Apache::lonconfigsettings::publishlogo($r,'copy',$legacyfile,$dom,$confname,
 9582:                                                     'scantron','','',$newfile,$modified);
 9583:         if ($result eq 'ok') {
 9584:             &update_modify_urls($r,$modified);
 9585:         } else {
 9586:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 9587:         }
 9588:     }
 9589:     return ($url,$error);
 9590: }
 9591: 
 9592: sub print_scantronconfig {
 9593:     my ($dom,$settings,$rowtotal) = @_;
 9594:     my $itemcount = 2;
 9595:     my $is_checked = ' checked="checked"';
 9596:     my %optionson = (
 9597:                      hdr => ' checked="checked"',
 9598:                      pad => ' checked="checked"',
 9599:                      rem => ' checked="checked"',
 9600:                     );
 9601:     my %optionsoff = (
 9602:                       hdr => '',
 9603:                       pad => '',
 9604:                       rem => '',
 9605:                      );
 9606:     my $currcsvsty = 'none';
 9607:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 9608:     my @fields = &scantroncsv_fields();
 9609:     my %titles = &scantronconfig_titles();
 9610:     if (ref($settings) eq 'HASH') {
 9611:         if (ref($settings->{config}) eq 'HASH') {
 9612:             if ($settings->{config}->{dat}) {
 9613:                 $checked{'dat'} = $is_checked;
 9614:             }
 9615:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 9616:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 9617:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 9618:                     if (keys(%csvfields) > 0) {
 9619:                         $checked{'csv'} = $is_checked;
 9620:                         $currcsvsty = 'block';
 9621:                     }
 9622:                 }
 9623:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 9624:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 9625:                     foreach my $option (keys(%optionson)) {
 9626:                         unless ($csvoptions{$option}) {
 9627:                             $optionsoff{$option} = $optionson{$option};
 9628:                             $optionson{$option} = '';
 9629:                         }
 9630:                     }
 9631:                 }
 9632:             }
 9633:         } else {
 9634:             $checked{'dat'} = $is_checked;
 9635:         }
 9636:     } else {
 9637:         $checked{'dat'} = $is_checked;
 9638:     }
 9639:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 9640:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 9641:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 9642:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 9643:     foreach my $item ('dat','csv') {
 9644:         my $id;
 9645:         if ($item eq 'csv') {
 9646:             $id = 'id="scantronconfcsv" ';
 9647:         }
 9648:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 9649:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 9650:         if ($item eq 'csv') {
 9651:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 9652:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 9653:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 9654:             foreach my $col (@fields) {
 9655:                 my $selnone;
 9656:                 if ($csvfields{$col} eq '') {
 9657:                     $selnone = ' selected="selected"';
 9658:                 }
 9659:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 9660:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 9661:                               '<option value=""'.$selnone.'></option>';
 9662:                 for (my $i=0; $i<20; $i++) {
 9663:                     my $shown = $i+1;
 9664:                     my $sel;
 9665:                     unless ($selnone) {
 9666:                         if (exists($csvfields{$col})) {
 9667:                             if ($csvfields{$col} == $i) {
 9668:                                 $sel = ' selected="selected"';
 9669:                             }
 9670:                         }
 9671:                     }
 9672:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 9673:                 }
 9674:                 $datatable .= '</select></td></tr>';
 9675:            }
 9676:            $datatable .= '</table></fieldset>'.
 9677:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 9678:                          '<legend>'.&mt('CSV Options').'</legend>';
 9679:            foreach my $option ('hdr','pad','rem') {
 9680:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 9681:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 9682:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 9683:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 9684:            }
 9685:            $datatable .= '</fieldset>';
 9686:            $itemcount ++;
 9687:         }
 9688:     }
 9689:     $datatable .= '</td></tr>';
 9690:     $$rowtotal ++;
 9691:     return $datatable;
 9692: }
 9693: 
 9694: sub scantronconfig_titles {
 9695:     return &Apache::lonlocal::texthash(
 9696:                                           dat => 'Standard format (.dat)',
 9697:                                           csv => 'Comma separated values (.csv)',
 9698:                                           hdr => 'Remove first line in file (contains column titles)',
 9699:                                           pad => 'Prepend 0s to PaperID',
 9700:                                           rem => 'Remove leading spaces (except Question Response columns)',
 9701:                                           CODE => 'CODE',
 9702:                                           ID   => 'Student ID',
 9703:                                           PaperID => 'Paper ID',
 9704:                                           FirstName => 'First Name',
 9705:                                           LastName => 'Last Name',
 9706:                                           FirstQuestion => 'First Question Response',
 9707:                                           Section => 'Section',
 9708:     );
 9709: }
 9710: 
 9711: sub scantroncsv_fields {
 9712:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 9713: }
 9714: 
 9715: sub print_coursecategories {
 9716:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 9717:     my $datatable;
 9718:     if ($position eq 'top') {
 9719:         my (%checked);
 9720:         my @catitems = ('unauth','auth');
 9721:         my @cattypes = ('std','domonly','codesrch','none');
 9722:         $checked{'unauth'} = 'std';
 9723:         $checked{'auth'} = 'std';
 9724:         if (ref($settings) eq 'HASH') {
 9725:             foreach my $type (@cattypes) {
 9726:                 if ($type eq $settings->{'unauth'}) {
 9727:                     $checked{'unauth'} = $type;
 9728:                 }
 9729:                 if ($type eq $settings->{'auth'}) {
 9730:                     $checked{'auth'} = $type;
 9731:                 }
 9732:             }
 9733:         }
 9734:         my %lt = &Apache::lonlocal::texthash (
 9735:                                                unauth   => 'Catalog type for unauthenticated users',
 9736:                                                auth     => 'Catalog type for authenticated users',
 9737:                                                none     => 'No catalog',
 9738:                                                std      => 'Standard catalog',
 9739:                                                domonly  => 'Domain-only catalog',
 9740:                                                codesrch => "Code search form",
 9741:                                              );
 9742:        my $itemcount = 0;
 9743:        foreach my $item (@catitems) {
 9744:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 9745:            $datatable .= '<tr '.$css_class.'>'.
 9746:                          '<td>'.$lt{$item}.'</td>'.
 9747:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 9748:            foreach my $type (@cattypes) {
 9749:                my $ischecked;
 9750:                if ($checked{$item} eq $type) {
 9751:                    $ischecked=' checked="checked"';
 9752:                }
 9753:                $datatable .= '<label>'.
 9754:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 9755:                              ' />'.$lt{$type}.'</label>&nbsp;';
 9756:            }
 9757:            $datatable .= '</span></td></tr>';
 9758:            $itemcount ++;
 9759:         }
 9760:         $$rowtotal += $itemcount;
 9761:     } elsif ($position eq 'middle') {
 9762:         my $toggle_cats_crs = ' ';
 9763:         my $toggle_cats_dom = ' checked="checked" ';
 9764:         my $can_cat_crs = ' ';
 9765:         my $can_cat_dom = ' checked="checked" ';
 9766:         my $toggle_catscomm_comm = ' ';
 9767:         my $toggle_catscomm_dom = ' checked="checked" ';
 9768:         my $can_catcomm_comm = ' ';
 9769:         my $can_catcomm_dom = ' checked="checked" ';
 9770: 
 9771:         if (ref($settings) eq 'HASH') {
 9772:             if ($settings->{'togglecats'} eq 'crs') {
 9773:                 $toggle_cats_crs = $toggle_cats_dom;
 9774:                 $toggle_cats_dom = ' ';
 9775:             }
 9776:             if ($settings->{'categorize'} eq 'crs') {
 9777:                 $can_cat_crs = $can_cat_dom;
 9778:                 $can_cat_dom = ' ';
 9779:             }
 9780:             if ($settings->{'togglecatscomm'} eq 'comm') {
 9781:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 9782:                 $toggle_catscomm_dom = ' ';
 9783:             }
 9784:             if ($settings->{'categorizecomm'} eq 'comm') {
 9785:                 $can_catcomm_comm = $can_catcomm_dom;
 9786:                 $can_catcomm_dom = ' ';
 9787:             }
 9788:         }
 9789:         my %title = &Apache::lonlocal::texthash (
 9790:                      togglecats     => 'Show/Hide a course in catalog',
 9791:                      togglecatscomm => 'Show/Hide a community in catalog',
 9792:                      categorize     => 'Assign a category to a course',
 9793:                      categorizecomm => 'Assign a category to a community',
 9794:                     );
 9795:         my %level = &Apache::lonlocal::texthash (
 9796:                      dom  => 'Set in Domain',
 9797:                      crs  => 'Set in Course',
 9798:                      comm => 'Set in Community',
 9799:                     );
 9800:         $datatable = '<tr class="LC_odd_row">'.
 9801:                   '<td>'.$title{'togglecats'}.'</td>'.
 9802:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 9803:                   '<input type="radio" name="togglecats"'.
 9804:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9805:                   '<label><input type="radio" name="togglecats"'.
 9806:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 9807:                   '</tr><tr>'.
 9808:                   '<td>'.$title{'categorize'}.'</td>'.
 9809:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 9810:                   '<label><input type="radio" name="categorize"'.
 9811:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9812:                   '<label><input type="radio" name="categorize"'.
 9813:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 9814:                   '</tr><tr class="LC_odd_row">'.
 9815:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 9816:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 9817:                   '<input type="radio" name="togglecatscomm"'.
 9818:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9819:                   '<label><input type="radio" name="togglecatscomm"'.
 9820:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 9821:                   '</tr><tr>'.
 9822:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 9823:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 9824:                   '<label><input type="radio" name="categorizecomm"'.
 9825:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9826:                   '<label><input type="radio" name="categorizecomm"'.
 9827:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 9828:                   '</tr>';
 9829:         $$rowtotal += 4;
 9830:     } else {
 9831:         my $css_class;
 9832:         my $itemcount = 1;
 9833:         my $cathash; 
 9834:         if (ref($settings) eq 'HASH') {
 9835:             $cathash = $settings->{'cats'};
 9836:         }
 9837:         if (ref($cathash) eq 'HASH') {
 9838:             my (@cats,@trails,%allitems,%idx,@jsarray);
 9839:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 9840:                                                    \%allitems,\%idx,\@jsarray);
 9841:             my $maxdepth = scalar(@cats);
 9842:             my $colattrib = '';
 9843:             if ($maxdepth > 2) {
 9844:                 $colattrib = ' colspan="2" ';
 9845:             }
 9846:             my @path;
 9847:             if (@cats > 0) {
 9848:                 if (ref($cats[0]) eq 'ARRAY') {
 9849:                     my $numtop = @{$cats[0]};
 9850:                     my $maxnum = $numtop;
 9851:                     my %default_names = (
 9852:                           instcode    => &mt('Official courses'),
 9853:                           communities => &mt('Communities'),
 9854:                     );
 9855: 
 9856:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 9857:                         ($cathash->{'instcode::0'} eq '') ||
 9858:                         (!grep(/^communities$/,@{$cats[0]})) || 
 9859:                         ($cathash->{'communities::0'} eq '')) {
 9860:                         $maxnum ++;
 9861:                     }
 9862:                     my $lastidx;
 9863:                     for (my $i=0; $i<$numtop; $i++) {
 9864:                         my $parent = $cats[0][$i];
 9865:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9866:                         my $item = &escape($parent).'::0';
 9867:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 9868:                         $lastidx = $idx{$item};
 9869:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 9870:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 9871:                         for (my $k=0; $k<=$maxnum; $k++) {
 9872:                             my $vpos = $k+1;
 9873:                             my $selstr;
 9874:                             if ($k == $i) {
 9875:                                 $selstr = ' selected="selected" ';
 9876:                             }
 9877:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9878:                         }
 9879:                         $datatable .= '</select></span></td><td>';
 9880:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 9881:                             $datatable .=  '<span class="LC_nobreak">'
 9882:                                            .$default_names{$parent}.'</span>';
 9883:                             if ($parent eq 'instcode') {
 9884:                                 $datatable .= '<br /><span class="LC_nobreak">('
 9885:                                               .&mt('with institutional codes')
 9886:                                               .')</span></td><td'.$colattrib.'>';
 9887:                             } else {
 9888:                                 $datatable .= '<table><tr><td>';
 9889:                             }
 9890:                             $datatable .= '<span class="LC_nobreak">'
 9891:                                           .'<label><input type="radio" name="'
 9892:                                           .$parent.'" value="1" checked="checked" />'
 9893:                                           .&mt('Display').'</label>';
 9894:                             if ($parent eq 'instcode') {
 9895:                                 $datatable .= '&nbsp;';
 9896:                             } else {
 9897:                                 $datatable .= '</span></td></tr><tr><td>'
 9898:                                               .'<span class="LC_nobreak">';
 9899:                             }
 9900:                             $datatable .= '<label><input type="radio" name="'
 9901:                                           .$parent.'" value="0" />'
 9902:                                           .&mt('Do not display').'</label></span>';
 9903:                             if ($parent eq 'communities') {
 9904:                                 $datatable .= '</td></tr></table>';
 9905:                             }
 9906:                             $datatable .= '</td>';
 9907:                         } else {
 9908:                             $datatable .= $parent
 9909:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 9910:                                           .'<input type="checkbox" name="deletecategory" '
 9911:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 9912:                         }
 9913:                         my $depth = 1;
 9914:                         push(@path,$parent);
 9915:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 9916:                         pop(@path);
 9917:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 9918:                         $itemcount ++;
 9919:                     }
 9920:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9921:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 9922:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 9923:                     for (my $k=0; $k<=$maxnum; $k++) {
 9924:                         my $vpos = $k+1;
 9925:                         my $selstr;
 9926:                         if ($k == $numtop) {
 9927:                             $selstr = ' selected="selected" ';
 9928:                         }
 9929:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9930:                     }
 9931:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 9932:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 9933:                                   .'</tr>'."\n";
 9934:                     $itemcount ++;
 9935:                     foreach my $default ('instcode','communities') {
 9936:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 9937:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9938:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 9939:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 9940:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 9941:                             for (my $k=0; $k<=$maxnum; $k++) {
 9942:                                 my $vpos = $k+1;
 9943:                                 my $selstr;
 9944:                                 if ($k == $maxnum) {
 9945:                                     $selstr = ' selected="selected" ';
 9946:                                 }
 9947:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9948:                             }
 9949:                             $datatable .= '</select></span></td>'.
 9950:                                           '<td><span class="LC_nobreak">'.
 9951:                                           $default_names{$default}.'</span>';
 9952:                             if ($default eq 'instcode') {
 9953:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 9954:                                               .&mt('with institutional codes').')</span>';
 9955:                             }
 9956:                             $datatable .= '</td>'
 9957:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 9958:                                           .&mt('Display').'</label>&nbsp;'
 9959:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 9960:                                           .&mt('Do not display').'</label></span></td></tr>';
 9961:                         }
 9962:                     }
 9963:                 }
 9964:             } else {
 9965:                 $datatable .= &initialize_categories($itemcount);
 9966:             }
 9967:         } else {
 9968:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 9969:                           .&initialize_categories($itemcount);
 9970:         }
 9971:         $$rowtotal += $itemcount;
 9972:     }
 9973:     return $datatable;
 9974: }
 9975: 
 9976: sub print_serverstatuses {
 9977:     my ($dom,$settings,$rowtotal) = @_;
 9978:     my $datatable;
 9979:     my @pages = &serverstatus_pages();
 9980:     my (%namedaccess,%machineaccess);
 9981:     foreach my $type (@pages) {
 9982:         $namedaccess{$type} = '';
 9983:         $machineaccess{$type}= '';
 9984:     }
 9985:     if (ref($settings) eq 'HASH') {
 9986:         foreach my $type (@pages) {
 9987:             if (exists($settings->{$type})) {
 9988:                 if (ref($settings->{$type}) eq 'HASH') {
 9989:                     foreach my $key (keys(%{$settings->{$type}})) {
 9990:                         if ($key eq 'namedusers') {
 9991:                             $namedaccess{$type} = $settings->{$type}->{$key};
 9992:                         } elsif ($key eq 'machines') {
 9993:                             $machineaccess{$type} = $settings->{$type}->{$key};
 9994:                         }
 9995:                     }
 9996:                 }
 9997:             }
 9998:         }
 9999:     }
10000:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
10001:     my $rownum = 0;
10002:     my $css_class;
10003:     foreach my $type (@pages) {
10004:         $rownum ++;
10005:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10006:         $datatable .= '<tr'.$css_class.'>'.
10007:                       '<td><span class="LC_nobreak">'.
10008:                       $titles->{$type}.'</span></td>'.
10009:                       '<td class="LC_left_item">'.
10010:                       '<input type="text" name="'.$type.'_namedusers" '.
10011:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
10012:                       '<td class="LC_right_item">'.
10013:                       '<span class="LC_nobreak">'.
10014:                       '<input type="text" name="'.$type.'_machines" '.
10015:                       'value="'.$machineaccess{$type}.'" size="10" />'.
10016:                       '</span></td></tr>'."\n";
10017:     }
10018:     $$rowtotal += $rownum;
10019:     return $datatable;
10020: }
10021: 
10022: sub serverstatus_pages {
10023:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
10024:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
10025:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
10026:             'uniquecodes','diskusage','coursecatalog');
10027: }
10028: 
10029: sub defaults_javascript {
10030:     my ($settings) = @_;
10031:     return unless (ref($settings) eq 'HASH');
10032:     my $portal_js = <<"ENDPORTAL";
10033: 
10034: function portalExtras(caller) {
10035:     var x = caller.value;
10036:     var y = new Array('email','web');
10037:     for (var i=0; i<y.length; i++) {
10038:         if (document.getElementById('portal_def_'+y[i]+'_div')) {
10039:             var z = document.getElementById('portal_def_'+y[i]+'_div');
10040:             if (x.length > 0) {
10041:                 z.style.display = 'block';
10042:             } else {
10043:                 z.style.display = 'none';
10044:             }
10045:         }
10046:     }
10047: }
10048: ENDPORTAL
10049:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10050:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
10051:         if ($maxnum eq '') {
10052:             $maxnum = 0;
10053:         }
10054:         $maxnum ++;
10055:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
10056:         return <<"ENDSCRIPT";
10057: <script type="text/javascript">
10058: // <![CDATA[
10059: function reorderTypes(form,caller) {
10060:     var changedVal;
10061: $jstext 
10062:     var newpos = 'addinststatus_pos';
10063:     var current = new Array;
10064:     var maxh = $maxnum;
10065:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
10066:     var oldVal;
10067:     if (caller == newpos) {
10068:         changedVal = newitemVal;
10069:     } else {
10070:         var curritem = 'inststatus_pos_'+caller;
10071:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
10072:         current[newitemVal] = newpos;
10073:     }
10074:     for (var i=0; i<inststatuses.length; i++) {
10075:         if (inststatuses[i] != caller) {
10076:             var elementName = 'inststatus_pos_'+inststatuses[i];
10077:             if (form.elements[elementName]) {
10078:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
10079:                 current[currVal] = elementName;
10080:             }
10081:         }
10082:     }
10083:     for (var j=0; j<maxh; j++) {
10084:         if (current[j] == undefined) {
10085:             oldVal = j;
10086:         }
10087:     }
10088:     if (oldVal < changedVal) {
10089:         for (var k=oldVal+1; k<=changedVal ; k++) {
10090:            var elementName = current[k];
10091:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
10092:         }
10093:     } else {
10094:         for (var k=changedVal; k<oldVal; k++) {
10095:             var elementName = current[k];
10096:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
10097:         }
10098:     }
10099:     return;
10100: }
10101: 
10102: $portal_js
10103: 
10104: // ]]>
10105: </script>
10106: 
10107: ENDSCRIPT
10108:     } else {
10109: return <<"ENDSCRIPT";
10110: <script type="text/javascript">
10111: // <![CDATA[
10112: $portal_js
10113: // ]]>
10114: </script>
10115: 
10116: ENDSCRIPT
10117:     }
10118:     return;
10119: }
10120: 
10121: sub passwords_javascript {
10122:     my ($prefix) = @_;
10123:     my %intalert;
10124:     if ($prefix eq 'passwords') {
10125:         %intalert = &Apache::lonlocal::texthash (
10126:             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.',
10127:             authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
10128:             passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
10129:             passmax => 'Warning: maximum password length must be a positive integer (or blank).',
10130:             passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
10131:         );
10132:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
10133:         %intalert = &Apache::lonlocal::texthash (
10134:             passmin => 'Warning: minimum secret length must be a positive integer greater than 6.',
10135:             passmax => 'Warning: maximum secret length must be a positive integer (or blank).',
10136:         );
10137:     }
10138:     &js_escape(\%intalert);
10139:     my $defmin = $Apache::lonnet::passwdmin;
10140:     my $intauthjs;
10141:     if ($prefix eq 'passwords') { $intauthjs = <<"ENDSCRIPT";
10142: 
10143: function warnIntAuth(field) {
10144:     if (field.name == 'intauth_check') {
10145:         if (field.value == '2') {
10146:             alert('$intalert{authcheck}');
10147:         }
10148:     }
10149:     if (field.name == 'intauth_cost') {
10150:         field.value.replace(/\s/g,'');
10151:         if (field.value != '') {
10152:             var regexdigit=/^\\d+\$/;
10153:             if (!regexdigit.test(field.value)) {
10154:                 alert('$intalert{authcost}');
10155:             }
10156:         }
10157:     }
10158:     return;
10159: }
10160: 
10161: ENDSCRIPT
10162: 
10163:      }
10164: 
10165:      $intauthjs .= <<"ENDSCRIPT";
10166: 
10167: function warnInt$prefix(field) {
10168:     field.value.replace(/^\s+/,'');
10169:     field.value.replace(/\s+\$/,'');
10170:     var regexdigit=/^\\d+\$/;
10171:     if (field.name == '${prefix}_min') {
10172:         if (field.value == '') {
10173:             alert('$intalert{passmin}');
10174:             field.value = '$defmin';
10175:         } else {
10176:             if (!regexdigit.test(field.value)) {
10177:                 alert('$intalert{passmin}');
10178:                 field.value = '$defmin';
10179:             }
10180:             var minval = parseInt(field.value,10);
10181:             if (minval < $defmin) {
10182:                 alert('$intalert{passmin}');
10183:                 field.value = '$defmin';
10184:             }
10185:         }
10186:     } else {
10187:         if (field.value == '0') {
10188:             field.value = '';
10189:         }
10190:         if (field.value != '') {
10191:             if (!regexdigit.test(field.value)) {
10192:                 if (field.name == '${prefix}_max') {
10193:                     alert('$intalert{passmax}');
10194:                 } else {
10195:                     if (field.name == '${prefix}_numsaved') {
10196:                         alert('$intalert{passnum}');
10197:                     }
10198:                 }
10199:                 field.value = '';
10200:             }
10201:         }
10202:     }
10203:     return;
10204: }
10205: 
10206: ENDSCRIPT
10207:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
10208: }
10209: 
10210: sub coursecategories_javascript {
10211:     my ($settings) = @_;
10212:     my ($output,$jstext,$cathash);
10213:     if (ref($settings) eq 'HASH') {
10214:         $cathash = $settings->{'cats'};
10215:     }
10216:     if (ref($cathash) eq 'HASH') {
10217:         my (@cats,@jsarray,%idx);
10218:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
10219:         if (@jsarray > 0) {
10220:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
10221:             for (my $i=0; $i<@jsarray; $i++) {
10222:                 if (ref($jsarray[$i]) eq 'ARRAY') {
10223:                     my $catstr = join('","',@{$jsarray[$i]});
10224:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
10225:                 }
10226:             }
10227:         }
10228:     } else {
10229:         $jstext  = '    var categories = Array(1);'."\n".
10230:                    '    categories[0] = Array("instcode_pos");'."\n"; 
10231:     }
10232:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
10233:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
10234:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
10235:     &js_escape(\$instcode_reserved);
10236:     &js_escape(\$communities_reserved);
10237:     &js_escape(\$choose_again);
10238:     $output = <<"ENDSCRIPT";
10239: <script type="text/javascript">
10240: // <![CDATA[
10241: function reorderCats(form,parent,item,idx) {
10242:     var changedVal;
10243: $jstext
10244:     var newpos = 'addcategory_pos';
10245:     if (parent == '') {
10246:         var has_instcode = 0;
10247:         var maxtop = categories[idx].length;
10248:         for (var j=0; j<maxtop; j++) {
10249:             if (categories[idx][j] == 'instcode::0') {
10250:                 has_instcode == 1;
10251:             }
10252:         }
10253:         if (has_instcode == 0) {
10254:             categories[idx][maxtop] = 'instcode_pos';
10255:         }
10256:     } else {
10257:         newpos += '_'+parent;
10258:     }
10259:     var maxh = 1 + categories[idx].length;
10260:     var current = new Array;
10261:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
10262:     if (item == newpos) {
10263:         changedVal = newitemVal;
10264:     } else {
10265:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
10266:         current[newitemVal] = newpos;
10267:     }
10268:     for (var i=0; i<categories[idx].length; i++) {
10269:         var elementName = categories[idx][i];
10270:         if (elementName != item) {
10271:             if (form.elements[elementName]) {
10272:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
10273:                 current[currVal] = elementName;
10274:             }
10275:         }
10276:     }
10277:     var oldVal;
10278:     for (var j=0; j<maxh; j++) {
10279:         if (current[j] == undefined) {
10280:             oldVal = j;
10281:         }
10282:     }
10283:     if (oldVal < changedVal) {
10284:         for (var k=oldVal+1; k<=changedVal ; k++) {
10285:            var elementName = current[k];
10286:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
10287:         }
10288:     } else {
10289:         for (var k=changedVal; k<oldVal; k++) {
10290:             var elementName = current[k];
10291:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
10292:         }
10293:     }
10294:     return;
10295: }
10296: 
10297: function categoryCheck(form) {
10298:     if (form.elements['addcategory_name'].value == 'instcode') {
10299:         alert('$instcode_reserved\\n$choose_again');
10300:         return false;
10301:     }
10302:     if (form.elements['addcategory_name'].value == 'communities') {
10303:         alert('$communities_reserved\\n$choose_again');
10304:         return false;
10305:     }
10306:     return true;
10307: }
10308: 
10309: // ]]>
10310: </script>
10311: 
10312: ENDSCRIPT
10313:     return $output;
10314: }
10315: 
10316: sub initialize_categories {
10317:     my ($itemcount) = @_;
10318:     my ($datatable,$css_class,$chgstr);
10319:     my %default_names = &Apache::lonlocal::texthash (
10320:                       instcode    => 'Official courses (with institutional codes)',
10321:                       communities => 'Communities',
10322:                         );
10323:     my $select0 = ' selected="selected"';
10324:     my $select1 = '';
10325:     foreach my $default ('instcode','communities') {
10326:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
10327:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
10328:         if ($default eq 'communities') {
10329:             $select1 = $select0;
10330:             $select0 = '';
10331:         }
10332:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10333:                      .'<select name="'.$default.'_pos">'
10334:                      .'<option value="0"'.$select0.'>1</option>'
10335:                      .'<option value="1"'.$select1.'>2</option>'
10336:                      .'<option value="2">3</option></select>&nbsp;'
10337:                      .$default_names{$default}
10338:                      .'</span></td><td><span class="LC_nobreak">'
10339:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
10340:                      .&mt('Display').'</label>&nbsp;<label>'
10341:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
10342:                  .'</label></span></td></tr>';
10343:         $itemcount ++;
10344:     }
10345:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10346:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
10347:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10348:                   .'<select name="addcategory_pos"'.$chgstr.'>'
10349:                   .'<option value="0">1</option>'
10350:                   .'<option value="1">2</option>'
10351:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
10352:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
10353:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
10354:                   .'</td></tr>';
10355:     return $datatable;
10356: }
10357: 
10358: sub build_category_rows {
10359:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
10360:     my ($text,$name,$item,$chgstr);
10361:     if (ref($cats) eq 'ARRAY') {
10362:         my $maxdepth = scalar(@{$cats});
10363:         if (ref($cats->[$depth]) eq 'HASH') {
10364:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
10365:                 my $numchildren = @{$cats->[$depth]{$parent}};
10366:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
10367:                 $text .= '<td><table class="LC_data_table">';
10368:                 my ($idxnum,$parent_name,$parent_item);
10369:                 my $higher = $depth - 1;
10370:                 if ($higher == 0) {
10371:                     $parent_name = &escape($parent).'::'.$higher;
10372:                 } else {
10373:                     if (ref($path) eq 'ARRAY') {
10374:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
10375:                     }
10376:                 }
10377:                 $parent_item = 'addcategory_pos_'.$parent_name;
10378:                 for (my $j=0; $j<=$numchildren; $j++) {
10379:                     if ($j < $numchildren) {
10380:                         $name = $cats->[$depth]{$parent}[$j];
10381:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
10382:                         $idxnum = $idx->{$item};
10383:                     } else {
10384:                         $name = $parent_name;
10385:                         $item = $parent_item;
10386:                     }
10387:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
10388:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
10389:                     for (my $i=0; $i<=$numchildren; $i++) {
10390:                         my $vpos = $i+1;
10391:                         my $selstr;
10392:                         if ($j == $i) {
10393:                             $selstr = ' selected="selected" ';
10394:                         }
10395:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
10396:                     }
10397:                     $text .= '</select>&nbsp;';
10398:                     if ($j < $numchildren) {
10399:                         my $deeper = $depth+1;
10400:                         $text .= $name.'&nbsp;'
10401:                                  .'<label><input type="checkbox" name="deletecategory" value="'
10402:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
10403:                         if(ref($path) eq 'ARRAY') {
10404:                             push(@{$path},$name);
10405:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
10406:                             pop(@{$path});
10407:                         }
10408:                     } else {
10409:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
10410:                         if ($j == $numchildren) {
10411:                             $text .= $name;
10412:                         } else {
10413:                             $text .= $item;
10414:                         }
10415:                         $text .= '" value="" />';
10416:                     }
10417:                     $text .= '</td></tr>';
10418:                 }
10419:                 $text .= '</table></td>';
10420:             } else {
10421:                 my $higher = $depth-1;
10422:                 if ($higher == 0) {
10423:                     $name = &escape($parent).'::'.$higher;
10424:                 } else {
10425:                     if (ref($path) eq 'ARRAY') {
10426:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
10427:                     }
10428:                 }
10429:                 my $colspan;
10430:                 if ($parent ne 'instcode') {
10431:                     $colspan = $maxdepth - $depth - 1;
10432:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
10433:                 }
10434:             }
10435:         }
10436:     }
10437:     return $text;
10438: }
10439: 
10440: sub modifiable_userdata_row {
10441:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
10442:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
10443:     my ($role,$rolename,$statustype);
10444:     $role = $item;
10445:     if ($context eq 'cancreate') {
10446:         if ($item =~ /^(emailusername)_(.+)$/) {
10447:             $role = $1;
10448:             $statustype = $2;
10449:             if (ref($usertypes) eq 'HASH') {
10450:                 if ($usertypes->{$statustype}) {
10451:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
10452:                 } else {
10453:                     $rolename = &mt('Data provided by user');
10454:                 }
10455:             }
10456:         }
10457:     } elsif ($context eq 'selfcreate') {
10458:         if (ref($usertypes) eq 'HASH') {
10459:             $rolename = $usertypes->{$role};
10460:         } else {
10461:             $rolename = $role;
10462:         }
10463:     } elsif ($context eq 'lti') {
10464:         $rolename = &mt('Institutional data used (if available)');
10465:     } else {
10466:         if ($role eq 'cr') {
10467:             $rolename = &mt('Custom role');
10468:         } else {
10469:             $rolename = &Apache::lonnet::plaintext($role);
10470:         }
10471:     }
10472:     my (@fields,%fieldtitles);
10473:     if (ref($fieldsref) eq 'ARRAY') {
10474:         @fields = @{$fieldsref};
10475:     } else {
10476:         @fields = ('lastname','firstname','middlename','generation',
10477:                    'permanentemail','id');
10478:     }
10479:     if ((ref($titlesref) eq 'HASH')) {
10480:         %fieldtitles = %{$titlesref};
10481:     } else {
10482:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10483:     }
10484:     my $output;
10485:     my $css_class;
10486:     if ($rowcount%2) {
10487:         $css_class = 'LC_odd_row';
10488:     }
10489:     if ($customcss) {
10490:         $css_class .= " $customcss";
10491:     }
10492:     $css_class =~ s/^\s+//;
10493:     if ($css_class) {
10494:         $css_class = ' class="'.$css_class.'"';
10495:     }
10496:     if ($rowstyle) {
10497:         $css_class .= ' style="'.$rowstyle.'"';
10498:     }
10499:     if ($rowid) {
10500:         $rowid = ' id="'.$rowid.'"';
10501:     }
10502:     $output = '<tr '.$css_class.$rowid.'>'.
10503:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
10504:               '<td class="LC_left_item" colspan="2"><table>';
10505:     my $rem;
10506:     my %checks;
10507:     if (ref($settings) eq 'HASH') {
10508:         my $hashref;
10509:         if ($context eq 'lti') {
10510:             if (ref($settings) eq 'HASH') {
10511:                 $hashref = $settings->{'instdata'};
10512:             }
10513:         } elsif (ref($settings->{$context}) eq 'HASH') {
10514:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
10515:                 $hashref = $settings->{'lti_instdata'};
10516:             }
10517:             if ($role eq 'emailusername') {
10518:                 if ($statustype) {
10519:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
10520:                         $hashref = $settings->{$context}->{$role}->{$statustype};
10521:                     }
10522:                 }
10523:             }
10524:         }
10525:         if (ref($hashref) eq 'HASH') {
10526:             foreach my $field (@fields) {
10527:                 if ($hashref->{$field}) {
10528:                     if ($role eq 'emailusername') {
10529:                         $checks{$field} = $hashref->{$field};
10530:                     } else {
10531:                         $checks{$field} = ' checked="checked" ';
10532:                     }
10533:                 }
10534:             }
10535:         }
10536:     }
10537:     my $total = scalar(@fields);
10538:     for (my $i=0; $i<$total; $i++) {
10539:         $rem = $i%($numinrow);
10540:         if ($rem == 0) {
10541:             if ($i > 0) {
10542:                 $output .= '</tr>';
10543:             }
10544:             $output .= '<tr>';
10545:         }
10546:         my $check = ' ';
10547:         unless ($role eq 'emailusername') {
10548:             if (exists($checks{$fields[$i]})) {
10549:                 $check = $checks{$fields[$i]};
10550:             } elsif ($context ne 'lti') {
10551:                 if ($role eq 'st') {
10552:                     if (ref($settings) ne 'HASH') {
10553:                         $check = ' checked="checked" '; 
10554:                     }
10555:                 }
10556:             }
10557:         }
10558:         $output .= '<td class="LC_left_item">'.
10559:                    '<span class="LC_nobreak">';
10560:         my $prefix = 'canmodify';
10561:         if ($role eq 'emailusername') {
10562:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
10563:                 $checks{$fields[$i]} = 'omit';
10564:             }
10565:             foreach my $option ('required','optional','omit') {
10566:                 my $checked='';
10567:                 if ($checks{$fields[$i]} eq $option) {
10568:                     $checked='checked="checked" ';
10569:                 }
10570:                 $output .= '<label>'.
10571:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
10572:                            &mt($option).'</label>'.('&nbsp;' x2);
10573:             }
10574:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
10575:         } else {
10576:             if ($context eq 'lti') {
10577:                 $prefix = 'lti';
10578:             }
10579:             $output .= '<label>'.
10580:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
10581:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
10582:                        '</label>';
10583:         }
10584:         $output .= '</span></td>';
10585:     }
10586:     $rem = $total%$numinrow;
10587:     my $colsleft;
10588:     if ($rem) {
10589:         $colsleft = $numinrow - $rem;
10590:     }
10591:     if ($colsleft > 1) {
10592:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10593:                    '&nbsp;</td>';
10594:     } elsif ($colsleft == 1) {
10595:         $output .= '<td class="LC_left_item">&nbsp;</td>';
10596:     }
10597:     $output .= '</tr></table></td></tr>';
10598:     return $output;
10599: }
10600: 
10601: sub insttypes_row {
10602:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
10603:         $customcss,$rowstyle) = @_;
10604:     my %lt = &Apache::lonlocal::texthash (
10605:                       cansearch => 'Users allowed to search',
10606:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
10607:                       lockablenames => 'User preference to lock name',
10608:                       selfassign    => 'Self-reportable affiliations',
10609:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
10610:              );
10611:     my $showdom;
10612:     if ($context eq 'cansearch') {
10613:         $showdom = ' ('.$dom.')';
10614:     }
10615:     my $class = 'LC_left_item';
10616:     if ($context eq 'statustocreate') {
10617:         $class = 'LC_right_item';
10618:     }
10619:     my $css_class;
10620:     if ($$rowtotal%2) {
10621:         $css_class = 'LC_odd_row';
10622:     }
10623:     if ($customcss) {
10624:         $css_class .= ' '.$customcss;
10625:     }
10626:     $css_class =~ s/^\s+//;
10627:     if ($css_class) {
10628:         $css_class = ' class="'.$css_class.'"';
10629:     }
10630:     if ($rowstyle) {
10631:         $css_class .= ' style="'.$rowstyle.'"';
10632:     }
10633:     if ($onclick) {
10634:         $onclick = 'onclick="'.$onclick.'" ';
10635:     }
10636:     my $output = '<tr'.$css_class.'>'.
10637:                  '<td>'.$lt{$context}.$showdom.
10638:                  '</td><td class="'.$class.'" colspan="2"><table>';
10639:     my $rem;
10640:     if (ref($types) eq 'ARRAY') {
10641:         for (my $i=0; $i<@{$types}; $i++) {
10642:             if (defined($usertypes->{$types->[$i]})) {
10643:                 my $rem = $i%($numinrow);
10644:                 if ($rem == 0) {
10645:                     if ($i > 0) {
10646:                         $output .= '</tr>';
10647:                     }
10648:                     $output .= '<tr>';
10649:                 }
10650:                 my $check = ' ';
10651:                 if (ref($settings) eq 'HASH') {
10652:                     if (ref($settings->{$context}) eq 'ARRAY') {
10653:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
10654:                             $check = ' checked="checked" ';
10655:                         }
10656:                     } elsif (ref($settings->{$context}) eq 'HASH') {
10657:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
10658:                             $check = ' checked="checked" ';
10659:                         }
10660:                     } elsif ($context eq 'statustocreate') {
10661:                         $check = ' checked="checked" ';
10662:                     }
10663:                 }
10664:                 $output .= '<td class="LC_left_item">'.
10665:                            '<span class="LC_nobreak"><label>'.
10666:                            '<input type="checkbox" name="'.$context.'" '.
10667:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
10668:                            $usertypes->{$types->[$i]}.'</label></span></td>';
10669:             }
10670:         }
10671:         $rem = @{$types}%($numinrow);
10672:     }
10673:     my $colsleft = $numinrow - $rem;
10674:     if ($context eq 'overrides') {
10675:         if ($colsleft > 1) {
10676:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
10677:         } else {
10678:             $output .= '<td class="LC_left_item">';
10679:         }
10680:         $output .= '&nbsp;';
10681:     } else {
10682:         if ($rem == 0) {
10683:             $output .= '<tr>';
10684:         }
10685:         if ($colsleft > 1) {
10686:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
10687:         } else {
10688:             $output .= '<td class="LC_left_item">';
10689:         }
10690:         my $defcheck = ' ';
10691:         if (ref($settings) eq 'HASH') {  
10692:             if (ref($settings->{$context}) eq 'ARRAY') {
10693:                 if (grep(/^default$/,@{$settings->{$context}})) {
10694:                     $defcheck = ' checked="checked" ';
10695:                 }
10696:             } elsif ($context eq 'statustocreate') {
10697:                 $defcheck = ' checked="checked" ';
10698:             }
10699:         }
10700:         $output .= '<span class="LC_nobreak"><label>'.
10701:                    '<input type="checkbox" name="'.$context.'" '.
10702:                    'value="default"'.$defcheck.$onclick.' />'.
10703:                    $othertitle.'</label></span>';
10704:     }
10705:     $output .= '</td></tr></table></td></tr>';
10706:     return $output;
10707: }
10708: 
10709: sub sorted_searchtitles {
10710:     my %searchtitles = &Apache::lonlocal::texthash(
10711:                          'uname' => 'username',
10712:                          'lastname' => 'last name',
10713:                          'lastfirst' => 'last name, first name',
10714:                      );
10715:     my @titleorder = ('uname','lastname','lastfirst');
10716:     return (\%searchtitles,\@titleorder);
10717: }
10718: 
10719: sub sorted_searchtypes {
10720:     my %srchtypes_desc = (
10721:                            exact    => 'is exact match',
10722:                            contains => 'contains ..',
10723:                            begins   => 'begins with ..',
10724:                          );
10725:     my @srchtypeorder = ('exact','begins','contains');
10726:     return (\%srchtypes_desc,\@srchtypeorder);
10727: }
10728: 
10729: sub usertype_update_row {
10730:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
10731:     my $datatable;
10732:     my $numinrow = 4;
10733:     foreach my $type (@{$types}) {
10734:         if (defined($usertypes->{$type})) {
10735:             $$rownums ++;
10736:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
10737:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
10738:                           '</td><td class="LC_left_item"><table>';
10739:             for (my $i=0; $i<@{$fields}; $i++) {
10740:                 my $rem = $i%($numinrow);
10741:                 if ($rem == 0) {
10742:                     if ($i > 0) {
10743:                         $datatable .= '</tr>';
10744:                     }
10745:                     $datatable .= '<tr>';
10746:                 }
10747:                 my $check = ' ';
10748:                 if (ref($settings) eq 'HASH') {
10749:                     if (ref($settings->{'fields'}) eq 'HASH') {
10750:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
10751:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
10752:                                 $check = ' checked="checked" ';
10753:                             }
10754:                         }
10755:                     }
10756:                 }
10757: 
10758:                 if ($i == @{$fields}-1) {
10759:                     my $colsleft = $numinrow - $rem;
10760:                     if ($colsleft > 1) {
10761:                         $datatable .= '<td colspan="'.$colsleft.'">';
10762:                     } else {
10763:                         $datatable .= '<td>';
10764:                     }
10765:                 } else {
10766:                     $datatable .= '<td>';
10767:                 }
10768:                 $datatable .= '<span class="LC_nobreak"><label>'.
10769:                               '<input type="checkbox" name="updateable_'.$type.
10770:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
10771:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
10772:             }
10773:             $datatable .= '</tr></table></td></tr>';
10774:         }
10775:     }
10776:     return $datatable;
10777: }
10778: 
10779: sub modify_login {
10780:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
10781:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
10782:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon,
10783:         %currsaml,%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso);
10784:     %title = ( coursecatalog => 'Display course catalog',
10785:                adminmail => 'Display administrator E-mail address',
10786:                helpdesk  => 'Display "Contact Helpdesk" link',
10787:                newuser => 'Link for visitors to create a user account',
10788:                loginheader => 'Log-in box header',
10789:                saml => 'Dual SSO and non-SSO login');
10790:     @offon = ('off','on');
10791:     if (ref($domconfig{login}) eq 'HASH') {
10792:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
10793:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
10794:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
10795:             }
10796:         }
10797:         if (ref($domconfig{login}{'saml'}) eq 'HASH') {
10798:             foreach my $lonhost (keys(%{$domconfig{login}{'saml'}})) {
10799:                 if (ref($domconfig{login}{'saml'}{$lonhost}) eq 'HASH') {
10800:                     $currsaml{$lonhost} = $domconfig{login}{'saml'}{$lonhost};
10801:                     $saml{$lonhost} = 1;
10802:                     $samltext{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'text'};
10803:                     $samlurl{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'url'};
10804:                     $samlalt{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'alt'};
10805:                     $samlimg{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'img'};
10806:                     $samltitle{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'title'};
10807:                     $samlwindow{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'window'};
10808:                     $samlnotsso{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'notsso'};
10809:                 }
10810:             }
10811:         }
10812:     }
10813:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
10814:                                            \%domconfig,\%loginhash);
10815:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
10816:     foreach my $item (@toggles) {
10817:         $loginhash{login}{$item} = $env{'form.'.$item};
10818:     }
10819:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
10820:     if (ref($colchanges{'login'}) eq 'HASH') {  
10821:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
10822:                                          \%loginhash);
10823:     }
10824: 
10825:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10826:     my %domservers = &Apache::lonnet::get_servers($dom);
10827:     my @loginvia_attribs = ('serverpath','custompath','exempt');
10828:     if (keys(%servers) > 1) {
10829:         foreach my $lonhost (keys(%servers)) {
10830:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
10831:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
10832:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
10833:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
10834:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
10835:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
10836:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
10837:                         $changes{'loginvia'}{$lonhost} = 1;
10838:                     } else {
10839:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
10840:                         $changes{'loginvia'}{$lonhost} = 1;
10841:                     }
10842:                 } else {
10843:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
10844:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
10845:                         $changes{'loginvia'}{$lonhost} = 1;
10846:                     }
10847:                 }
10848:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
10849:                     foreach my $item (@loginvia_attribs) {
10850:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
10851:                     }
10852:                 } else {
10853:                     foreach my $item (@loginvia_attribs) {
10854:                         my $new = $env{'form.'.$lonhost.'_'.$item};
10855:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
10856:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
10857:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
10858:                                 $new = '/';
10859:                             }
10860:                         }
10861:                         if (($item eq 'custompath') && 
10862:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
10863:                             $new = '';
10864:                         }
10865:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
10866:                             $changes{'loginvia'}{$lonhost} = 1;
10867:                         }
10868:                         if ($item eq 'exempt') {
10869:                             $new = &check_exempt_addresses($new);
10870:                         }
10871:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
10872:                     }
10873:                 }
10874:             } else {
10875:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
10876:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
10877:                     $changes{'loginvia'}{$lonhost} = 1;
10878:                     foreach my $item (@loginvia_attribs) {
10879:                         my $new = $env{'form.'.$lonhost.'_'.$item};
10880:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
10881:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
10882:                                 $new = '/';
10883:                             }
10884:                         }
10885:                         if (($item eq 'custompath') && 
10886:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
10887:                             $new = '';
10888:                         }
10889:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
10890:                     }
10891:                 }
10892:             }
10893:         }
10894:     }
10895: 
10896:     my $servadm = $r->dir_config('lonAdmEMail');
10897:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
10898:     if (ref($domconfig{'login'}) eq 'HASH') {
10899:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
10900:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
10901:                 if ($lang eq 'nolang') {
10902:                     push(@currlangs,$lang);
10903:                 } elsif (defined($langchoices{$lang})) {
10904:                     push(@currlangs,$lang);
10905:                 } else {
10906:                     next;
10907:                 }
10908:             }
10909:         }
10910:     }
10911:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
10912:     if (@currlangs > 0) {
10913:         foreach my $lang (@currlangs) {
10914:             if (grep(/^\Q$lang\E$/,@delurls)) {
10915:                 $changes{'helpurl'}{$lang} = 1;
10916:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
10917:                 $changes{'helpurl'}{$lang} = 1;
10918:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
10919:                 push(@newlangs,$lang);
10920:             } else {
10921:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
10922:             }
10923:         }
10924:     }
10925:     unless (grep(/^nolang$/,@currlangs)) {
10926:         if ($env{'form.loginhelpurl_nolang.filename'}) {
10927:             $changes{'helpurl'}{'nolang'} = 1;
10928:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
10929:             push(@newlangs,'nolang');
10930:         }
10931:     }
10932:     if ($env{'form.loginhelpurl_add_lang'}) {
10933:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
10934:             ($env{'form.loginhelpurl_add_file.filename'})) {
10935:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
10936:             $addedfile = $env{'form.loginhelpurl_add_lang'};
10937:         }
10938:     }
10939:     if ((@newlangs > 0) || ($addedfile)) {
10940:         my $error;
10941:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10942:         if ($configuserok eq 'ok') {
10943:             if ($switchserver) {
10944:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
10945:             } elsif ($author_ok eq 'ok') {
10946:                 my @allnew = @newlangs;
10947:                 if ($addedfile ne '') {
10948:                     push(@allnew,$addedfile);
10949:                 }
10950:                 my $modified = [];
10951:                 foreach my $lang (@allnew) {
10952:                     my $formelem = 'loginhelpurl_'.$lang;
10953:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
10954:                         $formelem = 'loginhelpurl_add_file';
10955:                     }
10956:                     (my $result,$newurl{$lang}) =
10957:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
10958:                                                                 "help/$lang",'','',$newfile{$lang},
10959:                                                                 $modified);
10960:                     if ($result eq 'ok') {
10961:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
10962:                         $changes{'helpurl'}{$lang} = 1;
10963:                     } else {
10964:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
10965:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
10966:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
10967:                             (!grep(/^\Q$lang\E$/,@delurls))) {
10968:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
10969:                         }
10970:                     }
10971:                 }
10972:                 &update_modify_urls($r,$modified);
10973:             } else {
10974:                 $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);
10975:             }
10976:         } else {
10977:             $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);
10978:         }
10979:         if ($error) {
10980:             &Apache::lonnet::logthis($error);
10981:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10982:         }
10983:     }
10984: 
10985:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
10986:     if (ref($domconfig{'login'}) eq 'HASH') {
10987:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
10988:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
10989:                 if ($domservers{$lonhost}) {
10990:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
10991:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
10992:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
10993:                     }
10994:                 }
10995:             }
10996:         }
10997:     }
10998:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
10999:     foreach my $lonhost (sort(keys(%domservers))) {
11000:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
11001:             $changes{'headtag'}{$lonhost} = 1;
11002:         } else {
11003:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
11004:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
11005:             }
11006:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
11007:                 push(@newhosts,$lonhost);
11008:             } elsif ($currheadtagurls{$lonhost}) {
11009:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
11010:                 if ($currexempt{$lonhost}) {
11011:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
11012:                         $changes{'headtag'}{$lonhost} = 1;
11013:                     }
11014:                 } elsif ($possexempt{$lonhost}) {
11015:                     $changes{'headtag'}{$lonhost} = 1;
11016:                 }
11017:                 if ($possexempt{$lonhost}) {
11018:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
11019:                 }
11020:             }
11021:         }
11022:     }
11023:     if (@newhosts) {
11024:         my $error;
11025:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11026:         if ($configuserok eq 'ok') {
11027:             if ($switchserver) {
11028:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
11029:             } elsif ($author_ok eq 'ok') {
11030:                 my $modified = [];
11031:                 foreach my $lonhost (@newhosts) {
11032:                     my $formelem = 'loginheadtag_'.$lonhost;
11033:                     (my $result,$newheadtagurls{$lonhost}) =
11034:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
11035:                                                                 "login/headtag/$lonhost",'','',
11036:                                                                 $env{'form.loginheadtag_'.$lonhost.'.filename'},
11037:                                                                 $modified);
11038:                     if ($result eq 'ok') {
11039:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
11040:                         $changes{'headtag'}{$lonhost} = 1;
11041:                         if ($possexempt{$lonhost}) {
11042:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
11043:                         }
11044:                     } else {
11045:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
11046:                                            $newheadtagurls{$lonhost},$result);
11047:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11048:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
11049:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
11050:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
11051:                         }
11052:                     }
11053:                 }
11054:                 &update_modify_urls($r,$modified);
11055:             } else {
11056:                 $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);
11057:             }
11058:         } else {
11059:             $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);
11060:         }
11061:         if ($error) {
11062:             &Apache::lonnet::logthis($error);
11063:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11064:         }
11065:     }
11066:     my @delsamlimg = &Apache::loncommon::get_env_multiple('form.saml_img_del');
11067:     my @newsamlimgs;
11068:     foreach my $lonhost (keys(%domservers)) {
11069:         if ($env{'form.saml_'.$lonhost}) {
11070:             if ($env{'form.saml_img_'.$lonhost.'.filename'}) {
11071:                 push(@newsamlimgs,$lonhost);
11072:             }
11073:             foreach my $item ('text','alt','url','title','window','notsso') {
11074:                 $env{'form.saml_'.$item.'_'.$lonhost} =~ s/^\s+|\s+$//g;
11075:             }
11076:             if ($saml{$lonhost}) {
11077:                 if ($env{'form.saml_window_'.$lonhost} ne '1') {
11078:                     $env{'form.saml_window_'.$lonhost} = '';
11079:                 }
11080:                 if (grep(/^\Q$lonhost\E$/,@delsamlimg)) {
11081: #FIXME Need to obsolete published image
11082:                     delete($currsaml{$lonhost}{'img'});
11083:                     $changes{'saml'}{$lonhost} = 1;
11084:                 }
11085:                 if ($env{'form.saml_alt_'.$lonhost} ne $samlalt{$lonhost}) {
11086:                     $changes{'saml'}{$lonhost} = 1;
11087:                 }
11088:                 if ($env{'form.saml_text_'.$lonhost} ne $samltext{$lonhost}) {
11089:                     $changes{'saml'}{$lonhost} = 1;
11090:                 }
11091:                 if ($env{'form.saml_url_'.$lonhost} ne $samlurl{$lonhost}) {
11092:                     $changes{'saml'}{$lonhost} = 1;
11093:                 }
11094:                 if ($env{'form.saml_title_'.$lonhost} ne $samltitle{$lonhost}) {
11095:                     $changes{'saml'}{$lonhost} = 1;
11096:                 }
11097:                 if ($env{'form.saml_window_'.$lonhost} ne $samlwindow{$lonhost}) {
11098:                     $changes{'saml'}{$lonhost} = 1;
11099:                 }
11100:                 if ($env{'form.saml_notsso_'.$lonhost} ne $samlnotsso{$lonhost}) {
11101:                     $changes{'saml'}{$lonhost} = 1;
11102:                 }
11103:             } else {
11104:                 $changes{'saml'}{$lonhost} = 1;
11105:             }
11106:             foreach my $item ('text','alt','url','title','window','notsso') {
11107:                 $currsaml{$lonhost}{$item} = $env{'form.saml_'.$item.'_'.$lonhost};
11108:             }
11109:         } else {
11110:             if ($saml{$lonhost}) {
11111:                 $changes{'saml'}{$lonhost} = 1;
11112:                 delete($currsaml{$lonhost});
11113:             }
11114:         }
11115:     }
11116:     foreach my $posshost (keys(%currsaml)) {
11117:         unless (exists($domservers{$posshost})) {
11118:             delete($currsaml{$posshost});
11119:         }
11120:     }
11121:     %{$loginhash{'login'}{'saml'}} = %currsaml;
11122:     if (@newsamlimgs) {
11123:         my $error;
11124:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11125:         if ($configuserok eq 'ok') {
11126:             if ($switchserver) {
11127:                 $error = &mt("Upload of SSO Button Image is not permitted to this server: [_1].",$switchserver);
11128:             } elsif ($author_ok eq 'ok') {
11129:                 my $modified = [];
11130:                 foreach my $lonhost (@newsamlimgs) {
11131:                     my $formelem = 'saml_img_'.$lonhost;
11132:                     my ($result,$imgurl) =
11133:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
11134:                                                                 "login/saml/$lonhost",'','',
11135:                                                                 $env{'form.saml_img_'.$lonhost.'.filename'},
11136:                                                                 $modified);
11137:                     if ($result eq 'ok') {
11138:                         $currsaml{$lonhost}{'img'} = $imgurl;
11139:                         $loginhash{'login'}{'saml'}{$lonhost}{'img'} = $imgurl;
11140:                         $changes{'saml'}{$lonhost} = 1;
11141:                     } else {
11142:                         my $puberror = &mt("Upload of SSO button image failed for [_1] because an error occurred publishing the file in RES space. Error was: [_2].",
11143:                                            $lonhost,$result);
11144:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11145:                     }
11146:                 }
11147:                 &update_modify_urls($r,$modified);
11148:             } else {
11149:                 $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);
11150:             }
11151:         } else {
11152:             $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);
11153:         }
11154:         if ($error) {
11155:             &Apache::lonnet::logthis($error);
11156:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11157:         }
11158:     }
11159:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
11160: 
11161:     my $defaulthelpfile = '/adm/loginproblems.html';
11162:     my $defaulttext = &mt('Default in use');
11163: 
11164:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
11165:                                              $dom);
11166:     if ($putresult eq 'ok') {
11167:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
11168:         my %defaultchecked = (
11169:                     'coursecatalog' => 'on',
11170:                     'helpdesk'      => 'on',
11171:                     'adminmail'     => 'off',
11172:                     'newuser'       => 'off',
11173:         );
11174:         if (ref($domconfig{'login'}) eq 'HASH') {
11175:             foreach my $item (@toggles) {
11176:                 if ($defaultchecked{$item} eq 'on') { 
11177:                     if (($domconfig{'login'}{$item} eq '0') &&
11178:                         ($env{'form.'.$item} eq '1')) {
11179:                         $changes{$item} = 1;
11180:                     } elsif (($domconfig{'login'}{$item} eq '' ||
11181:                               $domconfig{'login'}{$item} eq '1') &&
11182:                              ($env{'form.'.$item} eq '0')) {
11183:                         $changes{$item} = 1;
11184:                     }
11185:                 } elsif ($defaultchecked{$item} eq 'off') {
11186:                     if (($domconfig{'login'}{$item} eq '1') &&
11187:                         ($env{'form.'.$item} eq '0')) {
11188:                         $changes{$item} = 1;
11189:                     } elsif (($domconfig{'login'}{$item} eq '' ||
11190:                               $domconfig{'login'}{$item} eq '0') &&
11191:                              ($env{'form.'.$item} eq '1')) {
11192:                         $changes{$item} = 1;
11193:                     }
11194:                 }
11195:             }
11196:         }
11197:         if (keys(%changes) > 0 || $colchgtext) {
11198:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11199:             if (exists($changes{'saml'})) {
11200:                 my $hostid_in_use;
11201:                 my @hosts = &Apache::lonnet::current_machine_ids();
11202:                 if (@hosts > 1) {
11203:                     foreach my $hostid (@hosts) {
11204:                         if (&Apache::lonnet::host_domain($hostid) eq $dom) {
11205:                             $hostid_in_use = $hostid;
11206:                             last;
11207:                         }
11208:                     }
11209:                 } else {
11210:                     $hostid_in_use = $r->dir_config('lonHostID');
11211:                 }
11212:                 if (($hostid_in_use) &&
11213:                     (&Apache::lonnet::host_domain($hostid_in_use) eq $dom)) {
11214:                     &Apache::lonnet::devalidate_cache_new('samllanding',$hostid_in_use);
11215:                 }
11216:                 if (ref($lastactref) eq 'HASH') {
11217:                     if (ref($changes{'saml'}) eq 'HASH') {
11218:                         my %updates;
11219:                         map { $updates{$_} = 1; } keys(%{$changes{'saml'}});
11220:                         $lastactref->{'samllanding'} = \%updates;
11221:                     }
11222:                 }
11223:             }
11224:             if (ref($lastactref) eq 'HASH') {
11225:                 $lastactref->{'domainconfig'} = 1;
11226:             }
11227:             $resulttext = &mt('Changes made:').'<ul>';
11228:             foreach my $item (sort(keys(%changes))) {
11229:                 if ($item eq 'loginvia') {
11230:                     if (ref($changes{$item}) eq 'HASH') {
11231:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
11232:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11233:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
11234:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
11235:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
11236:                                     $protocol = 'http' if ($protocol ne 'https');
11237:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
11238: 
11239:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
11240:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
11241:                                     } else {
11242:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
11243:                                     }
11244:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
11245:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
11246:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
11247:                                     }
11248:                                     $resulttext .= '</li>';
11249:                                 } else {
11250:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
11251:                                 }
11252:                             } else {
11253:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
11254:                             }
11255:                         }
11256:                         $resulttext .= '</ul></li>';
11257:                     }
11258:                 } elsif ($item eq 'helpurl') {
11259:                     if (ref($changes{$item}) eq 'HASH') {
11260:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
11261:                             if (grep(/^\Q$lang\E$/,@delurls)) {
11262:                                 my ($chg,$link);
11263:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
11264:                                 if ($lang eq 'nolang') {
11265:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
11266:                                 } else {
11267:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
11268:                                 }
11269:                                 $resulttext .= '<li>'.$chg.'</li>';
11270:                             } else {
11271:                                 my $chg;
11272:                                 if ($lang eq 'nolang') {
11273:                                     $chg = &mt('custom log-in help file for no preferred language');
11274:                                 } else {
11275:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
11276:                                 }
11277:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
11278:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
11279:                                                       '?inhibitmenu=yes',$chg,600,500).
11280:                                                '</li>';
11281:                             }
11282:                         }
11283:                     }
11284:                 } elsif ($item eq 'headtag') {
11285:                     if (ref($changes{$item}) eq 'HASH') {
11286:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11287:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
11288:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
11289:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
11290:                                 $resulttext .= '<li><a href="'.
11291:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
11292:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
11293:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
11294:                                 if ($possexempt{$lonhost}) {
11295:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
11296:                                 } else {
11297:                                     $resulttext .= &mt('included for any client IP');
11298:                                 }
11299:                                 $resulttext .= '</li>';
11300:                             }
11301:                         }
11302:                     }
11303:                 } elsif ($item eq 'saml') {
11304:                     if (ref($changes{$item}) eq 'HASH') {
11305:                         my %notlt = (
11306:                                        text   => 'Text for log-in by SSO',
11307:                                        img    => 'SSO button image',
11308:                                        alt    => 'Alt text for button image',
11309:                                        url    => 'SSO URL',
11310:                                        title  => 'Tooltip for SSO link',
11311:                                        window => 'Pop-up window if iframe',
11312:                                        notsso => 'Text for non-SSO log-in',
11313:                                     );
11314:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11315:                             if (ref($currsaml{$lonhost}) eq 'HASH') {
11316:                                 $resulttext .= '<li>'.&mt("$title{$item} in use for [_1]","<b>$lonhost</b>").
11317:                                                '<ul>';
11318:                                 foreach my $key ('text','img','alt','url','title','window','notsso') {
11319:                                     if ($currsaml{$lonhost}{$key} eq '') {
11320:                                         $resulttext .= '<li>'.&mt("$notlt{$key} not in use").'</li>';
11321:                                     } else {
11322:                                         my $value = "'$currsaml{$lonhost}{$key}'";
11323:                                         if ($key eq 'img') {
11324:                                             $value = '<img src="'.$currsaml{$lonhost}{$key}.'" />';
11325:                                         } elsif ($key eq 'window') {
11326:                                             $value = 'On';
11327:                                         }
11328:                                         $resulttext .= '<li>'.&mt("$notlt{$key} set to: [_1]",
11329:                                                                   $value).'</li>';
11330:                                     }
11331:                                 }
11332:                                 $resulttext .= '</ul></li>';
11333:                             } else {
11334:                                 $resulttext .= '<li>'.&mt("$title{$item} not in use for [_1]",$lonhost).'</li>';
11335:                             }
11336:                         }
11337:                     }
11338:                 } elsif ($item eq 'captcha') {
11339:                     if (ref($loginhash{'login'}) eq 'HASH') {
11340:                         my $chgtxt;
11341:                         if ($loginhash{'login'}{$item} eq 'notused') {
11342:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
11343:                         } else {
11344:                             my %captchas = &captcha_phrases();
11345:                             if ($captchas{$loginhash{'login'}{$item}}) {
11346:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
11347:                             } else {
11348:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
11349:                             }
11350:                         }
11351:                         $resulttext .= '<li>'.$chgtxt.'</li>';
11352:                     }
11353:                 } elsif ($item eq 'recaptchakeys') {
11354:                     if (ref($loginhash{'login'}) eq 'HASH') {
11355:                         my ($privkey,$pubkey);
11356:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
11357:                             $pubkey = $loginhash{'login'}{$item}{'public'};
11358:                             $privkey = $loginhash{'login'}{$item}{'private'};
11359:                         }
11360:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
11361:                         if (!$pubkey) {
11362:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
11363:                         } else {
11364:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11365:                         }
11366:                         if (!$privkey) {
11367:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
11368:                         } else {
11369:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
11370:                         }
11371:                         $chgtxt .= '</ul>';
11372:                         $resulttext .= '<li>'.$chgtxt.'</li>';
11373:                     }
11374:                 } elsif ($item eq 'recaptchaversion') {
11375:                     if (ref($loginhash{'login'}) eq 'HASH') {
11376:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
11377:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
11378:                                            '</li>';
11379:                         }
11380:                     }
11381:                 } else {
11382:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
11383:                 }
11384:             }
11385:             $resulttext .= $colchgtext.'</ul>';
11386:         } else {
11387:             $resulttext = &mt('No changes made to log-in page settings');
11388:         }
11389:     } else {
11390:         $resulttext = '<span class="LC_error">'.
11391: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11392:     }
11393:     if ($errors) {
11394:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
11395:                        $errors.'</ul>';
11396:     }
11397:     return $resulttext;
11398: }
11399: 
11400: sub check_exempt_addresses {
11401:     my ($iplist) = @_;
11402:     $iplist =~ s/^\s+//;
11403:     $iplist =~ s/\s+$//;
11404:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
11405:     my (@okips,$new);
11406:     foreach my $ip (@poss_ips) {
11407:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
11408:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
11409:                 push(@okips,$ip);
11410:             }
11411:         }
11412:     }
11413:     if (@okips > 0) {
11414:         $new = join(',',@okips);
11415:     } else {
11416:         $new = '';
11417:     }
11418:     return $new;
11419: }
11420: 
11421: sub color_font_choices {
11422:     my %choices =
11423:         &Apache::lonlocal::texthash (
11424:             img => "Header",
11425:             bgs => "Background colors",
11426:             links => "Link colors",
11427:             images => "Images",
11428:             font => "Font color",
11429:             fontmenu => "Font menu",
11430:             pgbg => "Page",
11431:             tabbg => "Header",
11432:             sidebg => "Border",
11433:             link => "Link",
11434:             alink => "Active link",
11435:             vlink => "Visited link",
11436:         );
11437:     return %choices;
11438: }
11439: 
11440: sub modify_ipaccess {
11441:     my ($dom,$lastactref,%domconfig) = @_;
11442:     my (@allpos,%changes,%confhash,$errors,$resulttext);
11443:     my (@items,%deletions,%itemids,@warnings);
11444:     my ($typeorder,$types) = &commblocktype_text();
11445:     if ($env{'form.ipaccess_add'}) {
11446:         my $name = $env{'form.ipaccess_name_add'};
11447:         my ($newid,$error) = &get_ipaccess_id($dom,$name);
11448:         if ($newid) {
11449:             $itemids{'add'} = $newid;
11450:             push(@items,'add');
11451:             $changes{$newid} = 1;
11452:         } else {
11453:             $error = &mt('Failed to acquire unique ID for new IP access control item');
11454:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11455:         }
11456:     }
11457:     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
11458:         my @todelete = &Apache::loncommon::get_env_multiple('form.ipaccess_del');
11459:         if (@todelete) {
11460:             map { $deletions{$_} = 1; } @todelete;
11461:         }
11462:         my $maxnum = $env{'form.ipaccess_maxnum'};
11463:         for (my $i=0; $i<$maxnum; $i++) {
11464:             my $itemid = $env{'form.ipaccess_id_'.$i};
11465:             $itemid =~ s/\D+//g;
11466:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
11467:                 if ($deletions{$itemid}) {
11468:                     $changes{$itemid} = $domconfig{'ipaccess'}{$itemid}{'name'};
11469:                 } else {
11470:                     push(@items,$i);
11471:                     $itemids{$i} = $itemid;
11472:                 }
11473:             }
11474:         }
11475:     }
11476:     foreach my $idx (@items) {
11477:         my $itemid = $itemids{$idx};
11478:         next unless ($itemid);
11479:         my %current;
11480:         unless ($idx eq 'add') {
11481:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
11482:                 %current = %{$domconfig{'ipaccess'}{$itemid}};
11483:             }
11484:         }
11485:         my $position = $env{'form.ipaccess_pos_'.$itemid};
11486:         $position =~ s/\D+//g;
11487:         if ($position ne '') {
11488:             $allpos[$position] = $itemid;
11489:         }
11490:         my $name = $env{'form.ipaccess_name_'.$idx};
11491:         $name =~ s/^\s+|\s+$//g;
11492:         $confhash{$itemid}{'name'} = $name;
11493:         my $possrange = $env{'form.ipaccess_range_'.$idx};
11494:         $possrange =~ s/^\s+|\s+$//g;
11495:         unless ($possrange eq '') {
11496:             $possrange =~ s/[\r\n]+/\s/g;
11497:             $possrange =~ s/\s*-\s*/-/g;
11498:             $possrange =~ s/\s+/,/g;
11499:             $possrange =~ s/,+/,/g;
11500:             if ($possrange ne '') {
11501:                 my (@ok,$count);
11502:                 $count = 0;
11503:                 foreach my $poss (split(/\,/,$possrange)) {
11504:                     $count ++;
11505:                     $poss = &validate_ip_pattern($poss);
11506:                     if ($poss ne '') {
11507:                         push(@ok,$poss);
11508:                     }
11509:                 }
11510:                 my $diff = $count - scalar(@ok);
11511:                 if ($diff) {
11512:                     $errors .= '<li><span class="LC_error">'.
11513:                                &mt('[quant,_1,IP] invalid and excluded from saved value for IP range(s) for [_2]',
11514:                                    $diff,$name).
11515:                                '</span></li>';
11516:                 }
11517:                 if (@ok) {
11518:                     my @cidr_list;
11519:                     foreach my $item (@ok) {
11520:                         @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
11521:                     }
11522:                     $confhash{$itemid}{'ip'} = join(',',@cidr_list);
11523:                 }
11524:             }
11525:         }
11526:         foreach my $field ('name','ip') {
11527:             unless (($idx eq 'add') || ($changes{$itemid})) {
11528:                 if ($current{$field} ne $confhash{$itemid}{$field}) {
11529:                     $changes{$itemid} = 1;
11530:                     last;
11531:                 }
11532:             }
11533:         }
11534:         $confhash{$itemid}{'commblocks'} = {};
11535: 
11536:         my %commblocks;
11537:         map { $commblocks{$_} = 1; } &Apache::loncommon::get_env_multiple('form.ipaccess_block_'.$idx);
11538:         foreach my $type (@{$typeorder}) {
11539:             if ($commblocks{$type}) {
11540:                 $confhash{$itemid}{'commblocks'}{$type} = 'on';
11541:             }
11542:             unless (($idx eq 'add') || ($changes{$itemid})) {
11543:                 if (ref($current{'commblocks'}) eq 'HASH') {
11544:                     if ($confhash{$itemid}{'commblocks'}{$type} ne $current{'commblocks'}{$type}) {
11545:                         $changes{$itemid} = 1;
11546:                     }
11547:                 } elsif ($confhash{$itemid}{'commblocks'}{$type}) {
11548:                     $changes{$itemid} = 1;
11549:                 }
11550:             }
11551:         }
11552:         $confhash{$itemid}{'courses'} = {};
11553:         my %crsdeletions;
11554:         my @delcrs = &Apache::loncommon::get_env_multiple('form.ipaccess_course_delete_'.$idx);
11555:         if (@delcrs) {
11556:             map { $crsdeletions{$_} = 1; } @delcrs;
11557:         }
11558:         if (ref($current{'courses'}) eq 'HASH') {
11559:             foreach my $cid (sort(keys(%{$current{'courses'}}))) {
11560:                 if ($crsdeletions{$cid}) {
11561:                     $changes{$itemid} = 1;
11562:                 } else {
11563:                     $confhash{$itemid}{'courses'}{$cid} = 1;
11564:                 }
11565:             }
11566:         }
11567:         $env{'form.ipaccess_cnum_'.$idx} =~ s/^\s+|\s+$//g;
11568:         $env{'form.ipaccess_cdom_'.$idx} =~ s/^\s+|\s+$//g;
11569:         if (($env{'form.ipaccess_cnum_'.$idx} =~ /^$match_courseid$/) &&
11570:             ($env{'form.ipaccess_cdom_'.$idx} =~ /^$match_domain$/)) {
11571:             if (&Apache::lonnet::homeserver($env{'form.ipaccess_cnum_'.$idx},
11572:                                             $env{'form.ipaccess_cdom_'.$idx}) eq 'no_host') {
11573:                 $errors .= '<li><span class="LC_error">'.
11574:                            &mt('Invalid courseID [_1] omitted from list of allowed courses',
11575:                                $env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}).
11576:                            '</span></li>';
11577:             } else {
11578:                 $confhash{$itemid}{'courses'}{$env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}} = 1;
11579:                 $changes{$itemid} = 1;
11580:             }
11581:         }
11582:     }
11583:     if (@allpos > 0) {
11584:         my $idx = 0;
11585:         foreach my $itemid (@allpos) {
11586:             if ($itemid ne '') {
11587:                 $confhash{$itemid}{'order'} = $idx;
11588:                 unless ($changes{$itemid}) {
11589:                     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
11590:                         if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
11591:                             if ($domconfig{'ipaccess'}{$itemid}{'order'} ne $idx) {
11592:                                 $changes{$itemid} = 1;
11593:                             }
11594:                         }
11595:                     }
11596:                 }
11597:                 $idx ++;
11598:             }
11599:         }
11600:     }
11601:     if (keys(%changes)) {
11602:         my %defaultshash = (
11603:                               ipaccess => \%confhash,
11604:                            );
11605:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
11606:                                                  $dom);
11607:         if ($putresult eq 'ok') {
11608:             my $cachetime = 1800;
11609:             &Apache::lonnet::do_cache_new('ipaccess',$dom,\%confhash,$cachetime);
11610:             if (ref($lastactref) eq 'HASH') {
11611:                 $lastactref->{'ipaccess'} = 1;
11612:             }
11613:             $resulttext = &mt('Changes made:').'<ul>';
11614:             my %bynum;
11615:             foreach my $itemid (sort(keys(%changes))) {
11616:                 if (ref($confhash{$itemid}) eq 'HASH') {
11617:                     my $position = $confhash{$itemid}{'order'};
11618:                     if ($position =~ /^\d+$/) {
11619:                         $bynum{$position} = $itemid;
11620:                     }
11621:                 }
11622:             }
11623:             if (keys(%deletions)) {
11624:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
11625:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
11626:                 }
11627:             }
11628:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
11629:                 my $itemid = $bynum{$pos};
11630:                 if (ref($confhash{$itemid}) eq 'HASH') {
11631:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'name'}.'</b><ul>';
11632:                     my $position = $pos + 1;
11633:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
11634:                     if ($confhash{$itemid}{'ip'} eq '') {
11635:                         $resulttext .= '<li>'.&mt('No IP Range(s) set').'</li>';
11636:                     } else {
11637:                         $resulttext .= '<li>'.&mt('IP Range(s): [_1]',$confhash{$itemid}{'ip'}).'</li>';
11638:                     }
11639:                     if (keys(%{$confhash{$itemid}{'commblocks'}})) {
11640:                         $resulttext .= '<li>'.&mt('Functionality Blocked: [_1]',
11641:                                                   join(', ', map { $types->{$_}; } sort(keys(%{$confhash{$itemid}{'commblocks'}})))).
11642:                                        '</li>';
11643:                     } else {
11644:                         $resulttext .= '<li>'.&mt('No functionality blocked').'</li>';
11645:                     }
11646:                     if (keys(%{$confhash{$itemid}{'courses'}})) {
11647:                         my @courses;
11648:                         foreach my $cid (sort(keys(%{$confhash{$itemid}{'courses'}}))) {
11649:                             my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
11650:                             push(@courses,$courseinfo{'description'}.' ('.$cid.')');
11651:                         }
11652:                         $resulttext .= '<li>'.&mt('Courses/Communities allowed').':<ul><li>'.
11653:                                              join('</li><li>',@courses).'</li></ul>';
11654:                     } else {
11655:                         $resulttext .= '<li>'.&mt('No courses allowed').'</li>';
11656:                     }
11657:                     $resulttext .= '</ul></li>';
11658:                 }
11659:             }
11660:             $resulttext .= '</ul>';
11661:         } else {
11662:             $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
11663:         }
11664:     } else {
11665:         $resulttext = &mt('No changes made');
11666:     }
11667:     if ($errors) {
11668:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
11669:                        $errors.'</ul></p>';
11670:     }
11671:     return $resulttext;
11672: }
11673: 
11674: sub get_ipaccess_id {
11675:     my ($domain,$location) = @_;
11676:     # get lock on ipaccess db
11677:     my $lockhash = {
11678:                       lock => $env{'user.name'}.
11679:                               ':'.$env{'user.domain'},
11680:                    };
11681:     my $tries = 0;
11682:     my $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
11683:     my ($id,$error);
11684: 
11685:     while (($gotlock ne 'ok') && ($tries<10)) {
11686:         $tries ++;
11687:         sleep (0.1);
11688:         $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
11689:     }
11690:     if ($gotlock eq 'ok') {
11691:         my %currids = &Apache::lonnet::dump_dom('ipaccess',$domain);
11692:         if ($currids{'lock'}) {
11693:             delete($currids{'lock'});
11694:             if (keys(%currids)) {
11695:                 my @curr = sort { $a <=> $b } keys(%currids);
11696:                 if ($curr[-1] =~ /^\d+$/) {
11697:                     $id = 1 + $curr[-1];
11698:                 }
11699:             } else {
11700:                 $id = 1;
11701:             }
11702:             if ($id) {
11703:                 unless (&Apache::lonnet::newput_dom('ipaccess',{ $id => $location },$domain) eq 'ok') {
11704:                     $error = 'nostore';
11705:                 }
11706:             } else {
11707:                 $error = 'nonumber';
11708:             }
11709:         }
11710:         my $dellockoutcome = &Apache::lonnet::del_dom('ipaccess',['lock'],$domain);
11711:     } else {
11712:         $error = 'nolock';
11713:     }
11714:     return ($id,$error);
11715: }
11716: 
11717: sub modify_rolecolors {
11718:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
11719:     my ($resulttext,%rolehash);
11720:     $rolehash{'rolecolors'} = {};
11721:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
11722:         if ($domconfig{'rolecolors'} eq '') {
11723:             $domconfig{'rolecolors'} = {};
11724:         }
11725:     }
11726:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
11727:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
11728:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
11729:                                              $dom);
11730:     if ($putresult eq 'ok') {
11731:         if (keys(%changes) > 0) {
11732:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11733:             if (ref($lastactref) eq 'HASH') {
11734:                 $lastactref->{'domainconfig'} = 1;
11735:             }
11736:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
11737:                                              $rolehash{'rolecolors'});
11738:         } else {
11739:             $resulttext = &mt('No changes made to default color schemes');
11740:         }
11741:     } else {
11742:         $resulttext = '<span class="LC_error">'.
11743: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11744:     }
11745:     if ($errors) {
11746:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
11747:                        $errors.'</ul>';
11748:     }
11749:     return $resulttext;
11750: }
11751: 
11752: sub modify_colors {
11753:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
11754:     my (%changes,%choices);
11755:     my @bgs;
11756:     my @links = ('link','alink','vlink');
11757:     my @logintext;
11758:     my @images;
11759:     my $servadm = $r->dir_config('lonAdmEMail');
11760:     my $errors;
11761:     my %defaults;
11762:     foreach my $role (@{$roles}) {
11763:         if ($role eq 'login') {
11764:             %choices = &login_choices();
11765:             @logintext = ('textcol','bgcol');
11766:         } else {
11767:             %choices = &color_font_choices();
11768:         }
11769:         if ($role eq 'login') {
11770:             @images = ('img','logo','domlogo','login');
11771:             @bgs = ('pgbg','mainbg','sidebg');
11772:         } else {
11773:             @images = ('img');
11774:             @bgs = ('pgbg','tabbg','sidebg');
11775:         }
11776:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
11777:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
11778:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
11779:         }
11780:         if ($role eq 'login') {
11781:             foreach my $item (@logintext) {
11782:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
11783:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
11784:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
11785:                 }
11786:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
11787:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
11788:                 }
11789:             }
11790:         } else {
11791:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
11792:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
11793:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
11794:             }
11795:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
11796:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
11797:             }
11798:         }
11799:         foreach my $item (@bgs) {
11800:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
11801:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
11802:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
11803:             }
11804:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
11805:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
11806:             }
11807:         }
11808:         foreach my $item (@links) {
11809:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
11810:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
11811:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
11812:             }
11813:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
11814:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
11815:             }
11816:         }
11817:         my ($configuserok,$author_ok,$switchserver) = 
11818:             &config_check($dom,$confname,$servadm);
11819:         my ($width,$height) = &thumb_dimensions();
11820:         if (ref($domconfig->{$role}) ne 'HASH') {
11821:             $domconfig->{$role} = {};
11822:         }
11823:         foreach my $img (@images) {
11824:             if ($role eq 'login') {
11825:                 if (($img eq 'img') || ($img eq 'logo')) {  
11826:                     if (defined($env{'form.login_showlogo_'.$img})) {
11827:                         $confhash->{$role}{'showlogo'}{$img} = 1;
11828:                     } else { 
11829:                         $confhash->{$role}{'showlogo'}{$img} = 0;
11830:                     }
11831:                 }
11832:                 if ($env{'form.login_alt_'.$img} ne '') {
11833:                     $confhash->{$role}{'alttext'}{$img} = $env{'form.login_alt_'.$img};
11834:                 }
11835:             }
11836: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
11837: 		 && !defined($domconfig->{$role}{$img})
11838: 		 && !$env{'form.'.$role.'_del_'.$img}
11839: 		 && $env{'form.'.$role.'_import_'.$img}) {
11840: 		# import the old configured image from the .tab setting
11841: 		# if they haven't provided a new one 
11842: 		$domconfig->{$role}{$img} = 
11843: 		    $env{'form.'.$role.'_import_'.$img};
11844: 	    }
11845:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
11846:                 my $error;
11847:                 if ($configuserok eq 'ok') {
11848:                     if ($switchserver) {
11849:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
11850:                     } else {
11851:                         if ($author_ok eq 'ok') {
11852:                             my $modified = [];
11853:                             my ($result,$logourl) = 
11854:                                 &Apache::lonconfigsettings::publishlogo($r,'upload',$role.'_'.$img,
11855:                                                                         $dom,$confname,$img,$width,$height,
11856:                                                                         '',$modified);
11857:                             if ($result eq 'ok') {
11858:                                 $confhash->{$role}{$img} = $logourl;
11859:                                 $changes{$role}{'images'}{$img} = 1;
11860:                                 &update_modify_urls($r,$modified);
11861:                             } else {
11862:                                 $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);
11863:                             }
11864:                         } else {
11865:                             $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);
11866:                         }
11867:                     }
11868:                 } else {
11869:                     $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);
11870:                 }
11871:                 if ($error) {
11872:                     &Apache::lonnet::logthis($error);
11873:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11874:                 }
11875:             } elsif ($domconfig->{$role}{$img} ne '') {
11876:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
11877:                     my $error;
11878:                     if ($configuserok eq 'ok') {
11879: # is confname an author?
11880:                         if ($switchserver eq '') {
11881:                             if ($author_ok eq 'ok') {
11882:                                 my $modified = [];
11883:                                 my ($result,$logourl) = 
11884:                                     &Apache::lonconfigsettings::publishlogo($r,'copy',$domconfig->{$role}{$img},
11885:                                                                             $dom,$confname,$img,$width,$height,
11886:                                                                             '',$modified);
11887:                                 if ($result eq 'ok') {
11888:                                     $confhash->{$role}{$img} = $logourl;
11889: 				    $changes{$role}{'images'}{$img} = 1;
11890:                                     &update_modify_urls($r,$modified);
11891:                                 }
11892:                             }
11893:                         }
11894:                     }
11895:                 }
11896:             }
11897:         }
11898:         if (ref($domconfig) eq 'HASH') {
11899:             if (ref($domconfig->{$role}) eq 'HASH') {
11900:                 foreach my $img (@images) {
11901:                     if ($domconfig->{$role}{$img} ne '') {
11902:                         if ($env{'form.'.$role.'_del_'.$img}) {
11903:                             $confhash->{$role}{$img} = '';
11904:                             $changes{$role}{'images'}{$img} = 1;
11905:                         } else {
11906:                             if ($confhash->{$role}{$img} eq '') {
11907:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
11908:                             }
11909:                         }
11910:                     } else {
11911:                         if ($env{'form.'.$role.'_del_'.$img}) {
11912:                             $confhash->{$role}{$img} = '';
11913:                             $changes{$role}{'images'}{$img} = 1;
11914:                         } 
11915:                     }
11916:                     if ($role eq 'login') {
11917:                         if (($img eq 'logo') || ($img eq 'img')) {
11918:                             if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
11919:                                 if ($confhash->{$role}{'showlogo'}{$img} ne 
11920:                                     $domconfig->{$role}{'showlogo'}{$img}) {
11921:                                     $changes{$role}{'showlogo'}{$img} = 1; 
11922:                                 }
11923:                             } else {
11924:                                 if ($confhash->{$role}{'showlogo'}{$img} == 0) {
11925:                                     $changes{$role}{'showlogo'}{$img} = 1;
11926:                                 }
11927:                             }
11928:                         }
11929:                         if ($img ne 'login') {
11930:                             if (ref($domconfig->{$role}{'alttext'}) eq 'HASH') {
11931:                                 if ($confhash->{$role}{'alttext'}{$img} ne
11932:                                     $domconfig->{$role}{'alttext'}{$img}) {
11933:                                     $changes{$role}{'alttext'}{$img} = 1;
11934:                                 }
11935:                             } else {
11936:                                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
11937:                                     $changes{$role}{'alttext'}{$img} = 1;
11938:                                 }
11939:                             }
11940:                         }
11941:                     }
11942:                 }
11943:                 if ($domconfig->{$role}{'font'} ne '') {
11944:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
11945:                         $changes{$role}{'font'} = 1;
11946:                     }
11947:                 } else {
11948:                     if ($confhash->{$role}{'font'}) {
11949:                         $changes{$role}{'font'} = 1;
11950:                     }
11951:                 }
11952:                 if ($role ne 'login') {
11953:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
11954:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
11955:                             $changes{$role}{'fontmenu'} = 1;
11956:                         }
11957:                     } else {
11958:                         if ($confhash->{$role}{'fontmenu'}) {
11959:                             $changes{$role}{'fontmenu'} = 1;
11960:                         }
11961:                     }
11962:                 }
11963:                 foreach my $item (@bgs) {
11964:                     if ($domconfig->{$role}{$item} ne '') {
11965:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
11966:                             $changes{$role}{'bgs'}{$item} = 1;
11967:                         } 
11968:                     } else {
11969:                         if ($confhash->{$role}{$item}) {
11970:                             $changes{$role}{'bgs'}{$item} = 1;
11971:                         }
11972:                     }
11973:                 }
11974:                 foreach my $item (@links) {
11975:                     if ($domconfig->{$role}{$item} ne '') {
11976:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
11977:                             $changes{$role}{'links'}{$item} = 1;
11978:                         }
11979:                     } else {
11980:                         if ($confhash->{$role}{$item}) {
11981:                             $changes{$role}{'links'}{$item} = 1;
11982:                         }
11983:                     }
11984:                 }
11985:                 foreach my $item (@logintext) {
11986:                     if ($domconfig->{$role}{$item} ne '') {
11987:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
11988:                             $changes{$role}{'logintext'}{$item} = 1;
11989:                         }
11990:                     } else {
11991:                         if ($confhash->{$role}{$item}) {
11992:                             $changes{$role}{'logintext'}{$item} = 1;
11993:                         }
11994:                     }
11995:                 }
11996:             } else {
11997:                 &default_change_checker($role,\@images,\@links,\@bgs,
11998:                                         \@logintext,$confhash,\%changes); 
11999:             }
12000:         } else {
12001:             &default_change_checker($role,\@images,\@links,\@bgs,
12002:                                     \@logintext,$confhash,\%changes); 
12003:         }
12004:     }
12005:     return ($errors,%changes);
12006: }
12007: 
12008: sub config_check {
12009:     my ($dom,$confname,$servadm) = @_;
12010:     my ($configuserok,$author_ok,$switchserver,%currroles);
12011:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
12012:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
12013:                                                    $confname,$servadm);
12014:     if ($configuserok eq 'ok') {
12015:         $switchserver = &check_switchserver($dom,$confname);
12016:         if ($switchserver eq '') {
12017:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
12018:         }
12019:     }
12020:     return ($configuserok,$author_ok,$switchserver);
12021: }
12022: 
12023: sub default_change_checker {
12024:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
12025:     foreach my $item (@{$links}) {
12026:         if ($confhash->{$role}{$item}) {
12027:             $changes->{$role}{'links'}{$item} = 1;
12028:         }
12029:     }
12030:     foreach my $item (@{$bgs}) {
12031:         if ($confhash->{$role}{$item}) {
12032:             $changes->{$role}{'bgs'}{$item} = 1;
12033:         }
12034:     }
12035:     foreach my $item (@{$logintext}) {
12036:         if ($confhash->{$role}{$item}) {
12037:             $changes->{$role}{'logintext'}{$item} = 1;
12038:         }
12039:     }
12040:     foreach my $img (@{$images}) {
12041:         if ($env{'form.'.$role.'_del_'.$img}) {
12042:             $confhash->{$role}{$img} = '';
12043:             $changes->{$role}{'images'}{$img} = 1;
12044:         }
12045:         if ($role eq 'login') {
12046:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
12047:                 $changes->{$role}{'showlogo'}{$img} = 1;
12048:             }
12049:             if (ref($confhash->{$role}{'alttext'}) eq 'HASH') {
12050:                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
12051:                     $changes->{$role}{'alttext'}{$img} = 1;
12052:                 }
12053:             }
12054:         }
12055:     }
12056:     if ($confhash->{$role}{'font'}) {
12057:         $changes->{$role}{'font'} = 1;
12058:     }
12059: }
12060: 
12061: sub display_colorchgs {
12062:     my ($dom,$changes,$roles,$confhash) = @_;
12063:     my (%choices,$resulttext);
12064:     if (!grep(/^login$/,@{$roles})) {
12065:         $resulttext = &mt('Changes made:').'<br />';
12066:     }
12067:     foreach my $role (@{$roles}) {
12068:         if ($role eq 'login') {
12069:             %choices = &login_choices();
12070:         } else {
12071:             %choices = &color_font_choices();
12072:         }
12073:         if (ref($changes->{$role}) eq 'HASH') {
12074:             if ($role ne 'login') {
12075:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
12076:             }
12077:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
12078:                 if ($role ne 'login') {
12079:                     $resulttext .= '<ul>';
12080:                 }
12081:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
12082:                     if ($role ne 'login') {
12083:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
12084:                     }
12085:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
12086:                         if (($role eq 'login') && ($key eq 'showlogo')) {
12087:                             if ($confhash->{$role}{$key}{$item}) {
12088:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
12089:                             } else {
12090:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
12091:                             }
12092:                         } elsif (($role eq 'login') && ($key eq 'alttext')) {
12093:                             if ($confhash->{$role}{$key}{$item} ne '') {
12094:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} set to [_1].",
12095:                                                $confhash->{$role}{$key}{$item}).'</li>';
12096:                             } else {
12097:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} deleted.").'</li>';
12098:                             }
12099:                         } elsif ($confhash->{$role}{$item} eq '') {
12100:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
12101:                         } else {
12102:                             my $newitem = $confhash->{$role}{$item};
12103:                             if ($key eq 'images') {
12104:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
12105:                             }
12106:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
12107:                         }
12108:                     }
12109:                     if ($role ne 'login') {
12110:                         $resulttext .= '</ul></li>';
12111:                     }
12112:                 } else {
12113:                     if ($confhash->{$role}{$key} eq '') {
12114:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
12115:                     } else {
12116:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
12117:                     }
12118:                 }
12119:                 if ($role ne 'login') {
12120:                     $resulttext .= '</ul>';
12121:                 }
12122:             }
12123:         }
12124:     }
12125:     return $resulttext;
12126: }
12127: 
12128: sub thumb_dimensions {
12129:     return ('200','50');
12130: }
12131: 
12132: sub check_dimensions {
12133:     my ($inputfile) = @_;
12134:     my ($fullwidth,$fullheight);
12135:     if ($inputfile =~ m|^[/\w.\-]+$|) {
12136:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
12137:             my $imageinfo = <PIPE>;
12138:             if (!close(PIPE)) {
12139:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
12140:             }
12141:             chomp($imageinfo);
12142:             my ($fullsize) = 
12143:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
12144:             if ($fullsize) {
12145:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
12146:             }
12147:         }
12148:     }
12149:     return ($fullwidth,$fullheight);
12150: }
12151: 
12152: sub check_configuser {
12153:     my ($uhome,$dom,$confname,$servadm) = @_;
12154:     my ($configuserok,%currroles);
12155:     if ($uhome eq 'no_host') {
12156:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
12157:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
12158:         $configuserok = 
12159:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
12160:                              $configpass,'','','','','',undef,$servadm);
12161:     } else {
12162:         $configuserok = 'ok';
12163:         %currroles = 
12164:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
12165:     }
12166:     return ($configuserok,%currroles);
12167: }
12168: 
12169: sub check_authorstatus {
12170:     my ($dom,$confname,%currroles) = @_;
12171:     my $author_ok;
12172:     if (!$currroles{':'.$dom.':au'}) {
12173:         my $start = time;
12174:         my $end = 0;
12175:         $author_ok = 
12176:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
12177:                                         'au',$end,$start,'','','domconfig');
12178:     } else {
12179:         $author_ok = 'ok';
12180:     }
12181:     return $author_ok;
12182: }
12183: 
12184: sub update_modify_urls {
12185:     my ($r,$modified) = @_;
12186:     if ((ref($modified) eq 'ARRAY') && (@{$modified})) {
12187:         push(@{$modified_urls},$modified);
12188:         unless ($registered_cleanup) {
12189:             my $handlers = $r->get_handlers('PerlCleanupHandler');
12190:             $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
12191:             $registered_cleanup=1;
12192:         }
12193:     }
12194: }
12195: 
12196: sub notifysubscribed {
12197:     foreach my $targetsource (@{$modified_urls}){
12198:         next unless (ref($targetsource) eq 'ARRAY');
12199:         my ($target,$source)=@{$targetsource};
12200:         if ($source ne '') {
12201:             if (open(my $logfh,">>",$source.'.log')) {
12202:                 print $logfh "\nCleanup phase: Notifications\n";
12203:                 my @subscribed=&subscribed_hosts($target);
12204:                 foreach my $subhost (@subscribed) {
12205:                     print $logfh "\nNotifying host ".$subhost.':';
12206:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
12207:                     print $logfh $reply;
12208:                 }
12209:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
12210:                 foreach my $subhost (@subscribedmeta) {
12211:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
12212:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
12213:                                                         $subhost);
12214:                     print $logfh $reply;
12215:                 }
12216:                 print $logfh "\n============ Done ============\n";
12217:                 close($logfh);
12218:             }
12219:         }
12220:     }
12221:     return OK;
12222: }
12223: 
12224: sub subscribed_hosts {
12225:     my ($target) = @_;
12226:     my @subscribed;
12227:     if (open(my $fh,"<","$target.subscription")) {
12228:         while (my $subline=<$fh>) {
12229:             if ($subline =~ /^($match_lonid):/) {
12230:                 my $host = $1;
12231:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
12232:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
12233:                         push(@subscribed,$host);
12234:                     }
12235:                 }
12236:             }
12237:         }
12238:     }
12239:     return @subscribed;
12240: }
12241: 
12242: sub check_switchserver {
12243:     my ($dom,$confname) = @_;
12244:     my ($allowed,$switchserver,$home);
12245:     if ($confname eq '') {
12246:         $home = &Apache::lonnet::domain($dom,'primary');
12247:     } else {
12248:         $home = &Apache::lonnet::homeserver($confname,$dom);
12249:         if ($home eq 'no_host') {
12250:             $home = &Apache::lonnet::domain($dom,'primary');
12251:         }
12252:     }
12253:     my @ids=&Apache::lonnet::current_machine_ids();
12254:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
12255:     if (!$allowed) {
12256: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role='.
12257:                       &HTML::Entities::encode($env{'request.role'},'\'<>"&').
12258:                       '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
12259:     }
12260:     return $switchserver;
12261: }
12262: 
12263: sub modify_quotas {
12264:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
12265:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
12266:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
12267:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
12268:         $validationfieldsref);
12269:     if ($action eq 'quotas') {
12270:         $context = 'tools'; 
12271:     } else {
12272:         $context = $action;
12273:     }
12274:     if ($context eq 'requestcourses') {
12275:         @usertools = ('official','unofficial','community','textbook','lti');
12276:         @options =('norequest','approval','validate','autolimit');
12277:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
12278:         %titles = &courserequest_titles();
12279:         $toolregexp = join('|',@usertools);
12280:         %conditions = &courserequest_conditions();
12281:         $confname = $dom.'-domainconfig';
12282:         my $servadm = $r->dir_config('lonAdmEMail');
12283:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12284:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
12285:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
12286:     } elsif ($context eq 'requestauthor') {
12287:         @usertools = ('author');
12288:         %titles = &authorrequest_titles();
12289:     } else {
12290:         @usertools = ('aboutme','blog','webdav','portfolio','timezone');
12291:         %titles = &tool_titles();
12292:     }
12293:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12294:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12295:     foreach my $key (keys(%env)) {
12296:         if ($context eq 'requestcourses') {
12297:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
12298:                 my $item = $1;
12299:                 my $type = $2;
12300:                 if ($type =~ /^limit_(.+)/) {
12301:                     $limithash{$item}{$1} = $env{$key};
12302:                 } else {
12303:                     $confhash{$item}{$type} = $env{$key};
12304:                 }
12305:             }
12306:         } elsif ($context eq 'requestauthor') {
12307:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
12308:                 $confhash{$1} = $env{$key};
12309:             }
12310:         } else {
12311:             if ($key =~ /^form\.quota_(.+)$/) {
12312:                 $confhash{'defaultquota'}{$1} = $env{$key};
12313:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
12314:                 $confhash{'authorquota'}{$1} = $env{$key};
12315:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
12316:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
12317:             }
12318:         }
12319:     }
12320:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
12321:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
12322:         @approvalnotify = sort(@approvalnotify);
12323:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
12324:         my @crstypes = ('official','unofficial','community','textbook','lti');
12325:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
12326:         foreach my $type (@hasuniquecode) {
12327:             if (grep(/^\Q$type\E$/,@crstypes)) {
12328:                 $confhash{'uniquecode'}{$type} = 1;
12329:             }
12330:         }
12331:         my (%newbook,%allpos);
12332:         if ($context eq 'requestcourses') {
12333:             foreach my $type ('textbooks','templates') {
12334:                 @{$allpos{$type}} = (); 
12335:                 my $invalid;
12336:                 if ($type eq 'textbooks') {
12337:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
12338:                 } else {
12339:                     $invalid = &mt('Invalid LON-CAPA course for template');
12340:                 }
12341:                 if ($env{'form.'.$type.'_addbook'}) {
12342:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
12343:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
12344:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
12345:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
12346:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
12347:                         } else {
12348:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
12349:                             my $position = $env{'form.'.$type.'_addbook_pos'};
12350:                             $position =~ s/\D+//g;
12351:                             if ($position ne '') {
12352:                                 $allpos{$type}[$position] = $newbook{$type};
12353:                             }
12354:                         }
12355:                     } else {
12356:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
12357:                     }
12358:                 }
12359:             } 
12360:         }
12361:         if (ref($domconfig{$action}) eq 'HASH') {
12362:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
12363:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
12364:                     $changes{'notify'}{'approval'} = 1;
12365:                 }
12366:             } else {
12367:                 if ($confhash{'notify'}{'approval'}) {
12368:                     $changes{'notify'}{'approval'} = 1;
12369:                 }
12370:             }
12371:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
12372:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
12373:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
12374:                         unless ($confhash{'uniquecode'}{$crstype}) {
12375:                             $changes{'uniquecode'} = 1;
12376:                         }
12377:                     }
12378:                     unless ($changes{'uniquecode'}) {
12379:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
12380:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
12381:                                 $changes{'uniquecode'} = 1;
12382:                             }
12383:                         }
12384:                     }
12385:                } else {
12386:                    $changes{'uniquecode'} = 1;
12387:                }
12388:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
12389:                 $changes{'uniquecode'} = 1;
12390:             }
12391:             if ($context eq 'requestcourses') {
12392:                 foreach my $type ('textbooks','templates') {
12393:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
12394:                         my %deletions;
12395:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
12396:                         if (@todelete) {
12397:                             map { $deletions{$_} = 1; } @todelete;
12398:                         }
12399:                         my %imgdeletions;
12400:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
12401:                         if (@todeleteimages) {
12402:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
12403:                         }
12404:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
12405:                         for (my $i=0; $i<=$maxnum; $i++) {
12406:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
12407:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
12408:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
12409:                                 if ($deletions{$key}) {
12410:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
12411:                                         #FIXME need to obsolete item in RES space
12412:                                     }
12413:                                     next;
12414:                                 } else {
12415:                                     my $newpos = $env{'form.'.$itemid};
12416:                                     $newpos =~ s/\D+//g;
12417:                                     foreach my $item ('subject','title','publisher','author') {
12418:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
12419:                                                  ($type eq 'templates'));
12420:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
12421:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
12422:                                             $changes{$type}{$key} = 1;
12423:                                         }
12424:                                     }
12425:                                     $allpos{$type}[$newpos] = $key;
12426:                                 }
12427:                                 if ($imgdeletions{$key}) {
12428:                                     $changes{$type}{$key} = 1;
12429:                                     #FIXME need to obsolete item in RES space
12430:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
12431:                                     my ($cdom,$cnum) = split(/_/,$key);
12432:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
12433:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
12434:                                     } else {
12435:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
12436:                                                                                       $cdom,$cnum,$type,$configuserok,
12437:                                                                                       $switchserver,$author_ok);
12438:                                         if ($imgurl) {
12439:                                             $confhash{$type}{$key}{'image'} = $imgurl;
12440:                                             $changes{$type}{$key} = 1; 
12441:                                         }
12442:                                         if ($error) {
12443:                                             &Apache::lonnet::logthis($error);
12444:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12445:                                         }
12446:                                     }
12447:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
12448:                                     $confhash{$type}{$key}{'image'} = 
12449:                                         $domconfig{$action}{$type}{$key}{'image'};
12450:                                 }
12451:                             }
12452:                         }
12453:                     }
12454:                 }
12455:             }
12456:         } else {
12457:             if ($confhash{'notify'}{'approval'}) {
12458:                 $changes{'notify'}{'approval'} = 1;
12459:             }
12460:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
12461:                 $changes{'uniquecode'} = 1;
12462:             }
12463:         }
12464:         if ($context eq 'requestcourses') {
12465:             foreach my $type ('textbooks','templates') {
12466:                 if ($newbook{$type}) {
12467:                     $changes{$type}{$newbook{$type}} = 1;
12468:                     foreach my $item ('subject','title','publisher','author') {
12469:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
12470:                                  ($type eq 'template'));
12471:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
12472:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
12473:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
12474:                         }
12475:                     }
12476:                     if ($type eq 'textbooks') {
12477:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
12478:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
12479:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
12480:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
12481:                             } else {
12482:                                 my ($imageurl,$error) =
12483:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
12484:                                                             $configuserok,$switchserver,$author_ok);
12485:                                 if ($imageurl) {
12486:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
12487:                                 }
12488:                                 if ($error) {
12489:                                     &Apache::lonnet::logthis($error);
12490:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12491:                                 }
12492:                             }
12493:                         }
12494:                     }
12495:                 }
12496:                 if (@{$allpos{$type}} > 0) {
12497:                     my $idx = 0;
12498:                     foreach my $item (@{$allpos{$type}}) {
12499:                         if ($item ne '') {
12500:                             $confhash{$type}{$item}{'order'} = $idx;
12501:                             if (ref($domconfig{$action}) eq 'HASH') {
12502:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
12503:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
12504:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
12505:                                             $changes{$type}{$item} = 1;
12506:                                         }
12507:                                     }
12508:                                 }
12509:                             }
12510:                             $idx ++;
12511:                         }
12512:                     }
12513:                 }
12514:             }
12515:             if (ref($validationitemsref) eq 'ARRAY') {
12516:                 foreach my $item (@{$validationitemsref}) {
12517:                     if ($item eq 'fields') {
12518:                         my @changed;
12519:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
12520:                         if (@{$confhash{'validation'}{$item}} > 0) {
12521:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
12522:                         }
12523:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12524:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12525:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
12526:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
12527:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
12528:                                 } else {
12529:                                     @changed = @{$confhash{'validation'}{$item}};
12530:                                 }
12531:                             } else {
12532:                                 @changed = @{$confhash{'validation'}{$item}};
12533:                             }
12534:                         } else {
12535:                             @changed = @{$confhash{'validation'}{$item}};
12536:                         }
12537:                         if (@changed) {
12538:                             if ($confhash{'validation'}{$item}) {
12539:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
12540:                             } else {
12541:                                 $changes{'validation'}{$item} = &mt('None');
12542:                             }
12543:                         }
12544:                     } else {
12545:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
12546:                         if ($item eq 'markup') {
12547:                             if ($env{'form.requestcourses_validation_'.$item}) {
12548:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
12549:                             }
12550:                         }
12551:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12552:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12553:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
12554:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
12555:                                 }
12556:                             } else {
12557:                                 if ($confhash{'validation'}{$item} ne '') {
12558:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
12559:                                 }
12560:                             }
12561:                         } else {
12562:                             if ($confhash{'validation'}{$item} ne '') {
12563:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
12564:                             }
12565:                         }
12566:                     }
12567:                 }
12568:             }
12569:             if ($env{'form.validationdc'}) {
12570:                 my $newval = $env{'form.validationdc'};
12571:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
12572:                 if (exists($domcoords{$newval})) {
12573:                     $confhash{'validation'}{'dc'} = $newval;
12574:                 }
12575:             }
12576:             if (ref($confhash{'validation'}) eq 'HASH') {
12577:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12578:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12579:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
12580:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
12581:                                 if ($confhash{'validation'}{'dc'} eq '') {
12582:                                     $changes{'validation'}{'dc'} = &mt('None');
12583:                                 } else {
12584:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12585:                                 }
12586:                             }
12587:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
12588:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12589:                         }
12590:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
12591:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12592:                     }
12593:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
12594:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12595:                 }
12596:             } else {
12597:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12598:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12599:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
12600:                             $changes{'validation'}{'dc'} = &mt('None');
12601:                         }
12602:                     }
12603:                 }
12604:             }
12605:         }
12606:     } else {
12607:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
12608:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
12609:     }
12610:     foreach my $item (@usertools) {
12611:         foreach my $type (@{$types},'default','_LC_adv') {
12612:             my $unset; 
12613:             if ($context eq 'requestcourses') {
12614:                 $unset = '0';
12615:                 if ($type eq '_LC_adv') {
12616:                     $unset = '';
12617:                 }
12618:                 if ($confhash{$item}{$type} eq 'autolimit') {
12619:                     $confhash{$item}{$type} .= '=';
12620:                     unless ($limithash{$item}{$type} =~ /\D/) {
12621:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
12622:                     }
12623:                 }
12624:             } elsif ($context eq 'requestauthor') {
12625:                 $unset = '0';
12626:                 if ($type eq '_LC_adv') {
12627:                     $unset = '';
12628:                 }
12629:             } else {
12630:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
12631:                     $confhash{$item}{$type} = 1;
12632:                 } else {
12633:                     $confhash{$item}{$type} = 0;
12634:                 }
12635:             }
12636:             if (ref($domconfig{$action}) eq 'HASH') {
12637:                 if ($action eq 'requestauthor') {
12638:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
12639:                         $changes{$type} = 1;
12640:                     }
12641:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
12642:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
12643:                         $changes{$item}{$type} = 1;
12644:                     }
12645:                 } else {
12646:                     if ($context eq 'requestcourses') {
12647:                         if ($confhash{$item}{$type} ne $unset) {
12648:                             $changes{$item}{$type} = 1;
12649:                         }
12650:                     } else {
12651:                         if (!$confhash{$item}{$type}) {
12652:                             $changes{$item}{$type} = 1;
12653:                         }
12654:                     }
12655:                 }
12656:             } else {
12657:                 if ($context eq 'requestcourses') {
12658:                     if ($confhash{$item}{$type} ne $unset) {
12659:                         $changes{$item}{$type} = 1;
12660:                     }
12661:                 } elsif ($context eq 'requestauthor') {
12662:                     if ($confhash{$type} ne $unset) {
12663:                         $changes{$type} = 1;
12664:                     }
12665:                 } else {
12666:                     if (!$confhash{$item}{$type}) {
12667:                         $changes{$item}{$type} = 1;
12668:                     }
12669:                 }
12670:             }
12671:         }
12672:     }
12673:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
12674:         if (ref($domconfig{'quotas'}) eq 'HASH') {
12675:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
12676:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
12677:                     if (exists($confhash{'defaultquota'}{$key})) {
12678:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
12679:                             $changes{'defaultquota'}{$key} = 1;
12680:                         }
12681:                     } else {
12682:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
12683:                     }
12684:                 }
12685:             } else {
12686:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
12687:                     if (exists($confhash{'defaultquota'}{$key})) {
12688:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
12689:                             $changes{'defaultquota'}{$key} = 1;
12690:                         }
12691:                     } else {
12692:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
12693:                     }
12694:                 }
12695:             }
12696:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
12697:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
12698:                     if (exists($confhash{'authorquota'}{$key})) {
12699:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
12700:                             $changes{'authorquota'}{$key} = 1;
12701:                         }
12702:                     } else {
12703:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
12704:                     }
12705:                 }
12706:             }
12707:         }
12708:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
12709:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
12710:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
12711:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
12712:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
12713:                             $changes{'defaultquota'}{$key} = 1;
12714:                         }
12715:                     } else {
12716:                         if (!exists($domconfig{'quotas'}{$key})) {
12717:                             $changes{'defaultquota'}{$key} = 1;
12718:                         }
12719:                     }
12720:                 } else {
12721:                     $changes{'defaultquota'}{$key} = 1;
12722:                 }
12723:             }
12724:         }
12725:         if (ref($confhash{'authorquota'}) eq 'HASH') {
12726:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
12727:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
12728:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
12729:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
12730:                             $changes{'authorquota'}{$key} = 1;
12731:                         }
12732:                     } else {
12733:                         $changes{'authorquota'}{$key} = 1;
12734:                     }
12735:                 } else {
12736:                     $changes{'authorquota'}{$key} = 1;
12737:                 }
12738:             }
12739:         }
12740:     }
12741: 
12742:     if ($context eq 'requestauthor') {
12743:         $domdefaults{'requestauthor'} = \%confhash;
12744:     } else {
12745:         foreach my $key (keys(%confhash)) {
12746:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
12747:                 $domdefaults{$key} = $confhash{$key};
12748:             }
12749:         }
12750:     }
12751: 
12752:     my %quotahash = (
12753:                       $action => { %confhash }
12754:                     );
12755:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
12756:                                              $dom);
12757:     if ($putresult eq 'ok') {
12758:         if (keys(%changes) > 0) {
12759:             my $cachetime = 24*60*60;
12760:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12761:             if (ref($lastactref) eq 'HASH') {
12762:                 $lastactref->{'domdefaults'} = 1;
12763:             }
12764:             $resulttext = &mt('Changes made:').'<ul>';
12765:             unless (($context eq 'requestcourses') ||
12766:                     ($context eq 'requestauthor')) {
12767:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
12768:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
12769:                     foreach my $type (@{$types},'default') {
12770:                         if (defined($changes{'defaultquota'}{$type})) {
12771:                             my $typetitle = $usertypes->{$type};
12772:                             if ($type eq 'default') {
12773:                                 $typetitle = $othertitle;
12774:                             }
12775:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
12776:                         }
12777:                     }
12778:                     $resulttext .= '</ul></li>';
12779:                 }
12780:                 if (ref($changes{'authorquota'}) eq 'HASH') {
12781:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
12782:                     foreach my $type (@{$types},'default') {
12783:                         if (defined($changes{'authorquota'}{$type})) {
12784:                             my $typetitle = $usertypes->{$type};
12785:                             if ($type eq 'default') {
12786:                                 $typetitle = $othertitle;
12787:                             }
12788:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
12789:                         }
12790:                     }
12791:                     $resulttext .= '</ul></li>';
12792:                 }
12793:             }
12794:             my %newenv;
12795:             foreach my $item (@usertools) {
12796:                 my (%haschgs,%inconf);
12797:                 if ($context eq 'requestauthor') {
12798:                     %haschgs = %changes;
12799:                     %inconf = %confhash;
12800:                 } else {
12801:                     if (ref($changes{$item}) eq 'HASH') {
12802:                         %haschgs = %{$changes{$item}};
12803:                     }
12804:                     if (ref($confhash{$item}) eq 'HASH') {
12805:                         %inconf = %{$confhash{$item}};
12806:                     }
12807:                 }
12808:                 if (keys(%haschgs) > 0) {
12809:                     my $newacc = 
12810:                         &Apache::lonnet::usertools_access($env{'user.name'},
12811:                                                           $env{'user.domain'},
12812:                                                           $item,'reload',$context);
12813:                     if (($context eq 'requestcourses') ||
12814:                         ($context eq 'requestauthor')) {
12815:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
12816:                             $newenv{'environment.canrequest.'.$item} = $newacc;
12817:                         }
12818:                     } else {
12819:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
12820:                             $newenv{'environment.availabletools.'.$item} = $newacc;
12821:                         }
12822:                     }
12823:                     unless ($context eq 'requestauthor') {
12824:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
12825:                     }
12826:                     foreach my $type (@{$types},'default','_LC_adv') {
12827:                         if ($haschgs{$type}) {
12828:                             my $typetitle = $usertypes->{$type};
12829:                             if ($type eq 'default') {
12830:                                 $typetitle = $othertitle;
12831:                             } elsif ($type eq '_LC_adv') {
12832:                                 $typetitle = 'LON-CAPA Advanced Users'; 
12833:                             }
12834:                             if ($inconf{$type}) {
12835:                                 if ($context eq 'requestcourses') {
12836:                                     my $cond;
12837:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
12838:                                         if ($1 eq '') {
12839:                                             $cond = &mt('(Automatic processing of any request).');
12840:                                         } else {
12841:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
12842:                                         }
12843:                                     } else { 
12844:                                         $cond = $conditions{$inconf{$type}};
12845:                                     }
12846:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
12847:                                 } elsif ($context eq 'requestauthor') {
12848:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
12849:                                                              $titles{$inconf{$type}},$typetitle);
12850: 
12851:                                 } else {
12852:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
12853:                                 }
12854:                             } else {
12855:                                 if ($type eq '_LC_adv') {
12856:                                     if ($inconf{$type} eq '0') {
12857:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
12858:                                     } else { 
12859:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
12860:                                     }
12861:                                 } else {
12862:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
12863:                                 }
12864:                             }
12865:                         }
12866:                     }
12867:                     unless ($context eq 'requestauthor') {
12868:                         $resulttext .= '</ul></li>';
12869:                     }
12870:                 }
12871:             }
12872:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
12873:                 if (ref($changes{'notify'}) eq 'HASH') {
12874:                     if ($changes{'notify'}{'approval'}) {
12875:                         if (ref($confhash{'notify'}) eq 'HASH') {
12876:                             if ($confhash{'notify'}{'approval'}) {
12877:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
12878:                             } else {
12879:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
12880:                             }
12881:                         }
12882:                     }
12883:                 }
12884:             }
12885:             if ($action eq 'requestcourses') {
12886:                 my @offon = ('off','on');
12887:                 if ($changes{'uniquecode'}) {
12888:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
12889:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
12890:                         $resulttext .= '<li>'.
12891:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
12892:                                        '</li>';
12893:                     } else {
12894:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
12895:                                        '</li>';
12896:                     }
12897:                 }
12898:                 foreach my $type ('textbooks','templates') {
12899:                     if (ref($changes{$type}) eq 'HASH') {
12900:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
12901:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
12902:                             my %coursehash = &Apache::lonnet::coursedescription($key);
12903:                             my $coursetitle = $coursehash{'description'};
12904:                             my $position = $confhash{$type}{$key}{'order'} + 1;
12905:                             $resulttext .= '<li>';
12906:                             foreach my $item ('subject','title','publisher','author') {
12907:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
12908:                                          ($type eq 'templates'));
12909:                                 my $name = $item.':';
12910:                                 $name =~ s/^(\w)/\U$1/;
12911:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
12912:                             }
12913:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
12914:                             if ($type eq 'textbooks') {
12915:                                 if ($confhash{$type}{$key}{'image'}) {
12916:                                     $resulttext .= ' '.&mt('Image: [_1]',
12917:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
12918:                                                    ' alt="Textbook cover" />').'<br />';
12919:                                 }
12920:                             }
12921:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
12922:                         }
12923:                         $resulttext .= '</ul></li>';
12924:                     }
12925:                 }
12926:                 if (ref($changes{'validation'}) eq 'HASH') {
12927:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
12928:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
12929:                         foreach my $item (@{$validationitemsref}) {
12930:                             if (exists($changes{'validation'}{$item})) {
12931:                                 if ($item eq 'markup') {
12932:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
12933:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
12934:                                 } else {
12935:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
12936:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
12937:                                 }
12938:                             }
12939:                         }
12940:                         if (exists($changes{'validation'}{'dc'})) {
12941:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
12942:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
12943:                         }
12944:                     }
12945:                 }
12946:             }
12947:             $resulttext .= '</ul>';
12948:             if (keys(%newenv)) {
12949:                 &Apache::lonnet::appenv(\%newenv);
12950:             }
12951:         } else {
12952:             if ($context eq 'requestcourses') {
12953:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
12954:             } elsif ($context eq 'requestauthor') {
12955:                 $resulttext = &mt('No changes made to rights to request author space.');
12956:             } else {
12957:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
12958:             }
12959:         }
12960:     } else {
12961:         $resulttext = '<span class="LC_error">'.
12962: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12963:     }
12964:     if ($errors) {
12965:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
12966:                        '<ul>'.$errors.'</ul></p>';
12967:     }
12968:     return $resulttext;
12969: }
12970: 
12971: sub process_textbook_image {
12972:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
12973:     my $filename = $env{'form.'.$caller.'.filename'};
12974:     my ($error,$url);
12975:     my ($width,$height) = (50,50);
12976:     if ($configuserok eq 'ok') {
12977:         if ($switchserver) {
12978:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
12979:                          $switchserver);
12980:         } elsif ($author_ok eq 'ok') {
12981:             my $modified = [];
12982:             my ($result,$imageurl) =
12983:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
12984:                                                         "$type/$cdom/$cnum/cover",$width,$height,
12985:                                                         '',$modified);
12986:             if ($result eq 'ok') {
12987:                 $url = $imageurl;
12988:                 &update_modify_urls($r,$modified);
12989:             } else {
12990:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
12991:             }
12992:         } else {
12993:             $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);
12994:         }
12995:     } else {
12996:         $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);
12997:     }
12998:     return ($url,$error);
12999: }
13000: 
13001: sub modify_ltitools {
13002:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13003:     my (%currtoolsec,%secchanges,%newtoolsec,%newkeyset);
13004:     &fetch_secrets($dom,'toolsec',\%domconfig,\%currtoolsec,\%secchanges,\%newtoolsec,\%newkeyset);
13005: 
13006:     my $confname = $dom.'-domainconfig';
13007:     my $servadm = $r->dir_config('lonAdmEMail');
13008:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
13009: 
13010:     my ($resulttext,$ltitoolsoutput,$is_home,$errors,%ltitoolschg,%newtoolsenc,%newltitools);
13011:     my $toolserror =
13012:         &Apache::courseprefs::process_ltitools($r,$dom,$confname,$domconfig{'ltitools'},\%ltitoolschg,'domain',
13013:                                                $lastactref,$configuserok,$switchserver,$author_ok);
13014: 
13015:     my $home = &Apache::lonnet::domain($dom,'primary');
13016:     unless (($home eq 'no_host') || ($home eq '')) {
13017:         my @ids=&Apache::lonnet::current_machine_ids();
13018:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; last; } }
13019:     }
13020: 
13021:     if (keys(%ltitoolschg)) {
13022:         foreach my $id (keys(%ltitoolschg)) {
13023:             if (ref($ltitoolschg{$id}) eq 'HASH') {
13024:                 foreach my $inner (keys(%{$ltitoolschg{$id}})) {
13025:                     if (($inner eq 'secret') || ($inner eq 'key')) {
13026:                         if ($is_home) {
13027:                             $newtoolsenc{$id}{$inner} = $ltitoolschg{$id}{$inner};
13028:                         }
13029:                     }
13030:                 }
13031:             }
13032:         }
13033:         $ltitoolsoutput = &Apache::courseprefs::store_ltitools($dom,'','domain',\%ltitoolschg,$domconfig{'ltitools'});
13034:         if (keys(%ltitoolschg)) {
13035:             %newltitools = %ltitoolschg;
13036:         }
13037:     }
13038:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
13039:         foreach my $id (%{$domconfig{'ltitools'}}) {
13040:             next if ($id !~ /^\d+$/);
13041:             unless (exists($ltitoolschg{$id})) {
13042:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
13043:                     foreach my $inner (keys(%{$domconfig{'ltitools'}{$id}})) {
13044:                         if (($inner eq 'secret') || ($inner eq 'key')) {
13045:                             if ($is_home) {
13046:                                 $newtoolsenc{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
13047:                             }
13048:                         } else {
13049:                             $newltitools{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
13050:                         }
13051:                     }
13052:                 } else {
13053:                     $newltitools{$id} = $domconfig{'ltitools'}{$id};
13054:                 }
13055:             }
13056:         }
13057:     }
13058:     if ($toolserror) {
13059:         $errors = '<li>'.$toolserror.'</li>';
13060:     }
13061:     if ((keys(%ltitoolschg) == 0) && (keys(%secchanges) == 0)) {
13062:         $resulttext = &mt('No changes made.');
13063:         if ($errors) {
13064:             $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13065:                                  $errors.'</ul>';
13066:         }
13067:         return $resulttext;
13068:     }
13069:     my %ltitoolshash = (
13070:                           $action => { %newltitools }
13071:                        );
13072:     if (keys(%secchanges)) {
13073:         $ltitoolshash{'toolsec'} = \%newtoolsec;
13074:     }
13075:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,$dom);
13076:     if ($putresult eq 'ok') {
13077:         my %keystore;
13078:         if ($is_home) {
13079:             my %toolsenchash = (
13080:                                    $action => { %newtoolsenc }
13081:                                );
13082:             &Apache::lonnet::put_dom('encconfig',\%toolsenchash,$dom,undef,1);
13083:             my $cachetime = 24*60*60;
13084:             &Apache::lonnet::do_cache_new('ltitoolsenc',$dom,\%newtoolsenc,$cachetime);
13085:             &store_security($dom,'ltitools',\%secchanges,\%newkeyset,\%keystore,$lastactref);
13086:         }
13087:         $resulttext = &mt('Changes made:').'<ul>';
13088:         if (keys(%secchanges) > 0) {
13089:             $resulttext .= &lti_security_results($dom,'ltitools',\%secchanges,\%newtoolsec,\%newkeyset,\%keystore);
13090:         }
13091:         if (keys(%ltitoolschg) > 0) {
13092:             $resulttext .= $ltitoolsoutput;
13093:         }
13094:         my $cachetime = 24*60*60;
13095:         &Apache::lonnet::do_cache_new('ltitools',$dom,\%newltitools,$cachetime);
13096:         if (ref($lastactref) eq 'HASH') {
13097:             $lastactref->{'ltitools'} = 1;
13098:         }
13099:     } else {
13100:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
13101:     }
13102:     if ($errors) {
13103:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
13104:                        $errors.'</ul></p>';
13105:     }
13106:     return $resulttext;
13107: }
13108: 
13109: sub fetch_secrets {
13110:     my ($dom,$context,$domconfig,$currsec,$secchanges,$newsec,$newkeyset) = @_;
13111:     my %keyset;
13112:     %{$currsec} = ();
13113:     $newsec->{'private'}{'keys'} = [];
13114:     $newsec->{'encrypt'} = {};
13115:     $newsec->{'rules'} = {};
13116:     if ($context eq 'ltisec') {
13117:         $newsec->{'linkprot'} = {};
13118:     }
13119:     if (ref($domconfig->{$context}) eq 'HASH') {
13120:         %{$currsec} = %{$domconfig->{$context}};
13121:         if ($context eq 'ltisec') {
13122:             if (ref($currsec->{'linkprot'}) eq 'HASH') {
13123:                 foreach my $id (keys(%{$currsec->{'linkprot'}})) {
13124:                     unless ($id =~ /^\d+$/) {
13125:                         delete($currsec->{'linkprot'}{$id});
13126:                     }
13127:                 }
13128:             }
13129:         }
13130:         if (ref($currsec->{'private'}) eq 'HASH') {
13131:             if (ref($currsec->{'private'}{'keys'}) eq 'ARRAY') {
13132:                 $newsec->{'private'}{'keys'} = $currsec->{'private'}{'keys'};
13133:                 map { $keyset{$_} = 1; } @{$currsec->{'private'}{'keys'}};
13134:             }
13135:         }
13136:     }
13137:     my @items= ('crs','dom');
13138:     if ($context eq 'ltisec') {
13139:         push(@items,'consumers');
13140:     }
13141:     foreach my $item (@items) {
13142:         my $formelement;
13143:         if (($context eq 'toolsec') || ($item eq 'consumers')) {
13144:             $formelement = 'form.'.$context.'_'.$item;
13145:         } else {
13146:             $formelement = 'form.'.$context.'_'.$item.'linkprot';
13147:         }
13148:         if ($env{$formelement}) {
13149:             $newsec->{'encrypt'}{$item} = 1;
13150:             if (ref($currsec->{'encrypt'}) eq 'HASH') {
13151:                 unless ($currsec->{'encrypt'}{$item}) {
13152:                     $secchanges->{'encrypt'} = 1;
13153:                 }
13154:             } else {
13155:                 $secchanges->{'encrypt'} = 1;
13156:             }
13157:         } elsif (ref($currsec->{'encrypt'}) eq 'HASH') {
13158:             if ($currsec->{'encrypt'}{$item}) {
13159:                 $secchanges->{'encrypt'} = 1;
13160:             }
13161:         }
13162:     }
13163:     my $secrets;
13164:     if ($context eq 'ltisec') {
13165:         $secrets = 'ltisecrets';
13166:     } else {
13167:         $secrets = 'toolsecrets';
13168:     }
13169:     unless (exists($currsec->{'rules'})) {
13170:         $currsec->{'rules'} = {};
13171:     }
13172:     &password_rule_changes($secrets,$newsec->{'rules'},$currsec->{'rules'},$secchanges);
13173: 
13174:     my @ids=&Apache::lonnet::current_machine_ids();
13175:     my %servers = &Apache::lonnet::get_servers($dom,'library');
13176: 
13177:     foreach my $hostid (keys(%servers)) {
13178:         if (($hostid ne '') && (grep(/^\Q$hostid\E$/,@ids))) {
13179:             my $keyitem = 'form.'.$context.'_privkey_'.$hostid;
13180:             if (exists($env{$keyitem})) {
13181:                 $env{$keyitem} =~ s/(`)/'/g;
13182:                 if ($keyset{$hostid}) {
13183:                     if ($env{'form.'.$context.'_changeprivkey_'.$hostid}) {
13184:                         if ($env{$keyitem} ne '') {
13185:                             $secchanges->{'private'} = 1;
13186:                             $newkeyset->{$hostid} = $env{$keyitem};
13187:                         }
13188:                     }
13189:                 } elsif ($env{$keyitem} ne '') {
13190:                     unless (grep(/^\Q$hostid\E$/,@{$newsec->{'private'}{'keys'}})) {
13191:                         push(@{$newsec->{'private'}{'keys'}},$hostid);
13192:                     }
13193:                     $secchanges->{'private'} = 1;
13194:                     $newkeyset->{$hostid} = $env{$keyitem};
13195:                 }
13196:             }
13197:         }
13198:     }
13199: }
13200: 
13201: sub store_security {
13202:     my ($dom,$context,$secchanges,$newkeyset,$keystore) = @_;
13203:     return unless ((ref($secchanges) eq 'HASH') && (ref($newkeyset) eq 'HASH') &&
13204:                    (ref($keystore) eq 'HASH'));
13205:     if (keys(%{$secchanges})) {
13206:         if ($secchanges->{'private'}) {
13207:             my $who = &escape($env{'user.name'}.':'.$env{'user.domain'});
13208:             foreach my $hostid (keys(%{$newkeyset})) {
13209:                 my $storehash = {
13210:                                    key => $newkeyset->{$hostid},
13211:                                    who => $env{'user.name'}.':'.$env{'user.domain'},
13212:                                 };
13213:                 $keystore->{$hostid} = &Apache::lonnet::store_dom($storehash,$context,'private',
13214:                                                                   $dom,$hostid);
13215:             }
13216:         }
13217:     }
13218: }
13219: 
13220: sub lti_security_results {
13221:     my ($dom,$context,$secchanges,$newsec,$newkeyset,$keystore) = @_;
13222:     my $output;
13223:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
13224:     my $needs_update;
13225:     foreach my $item (keys(%{$secchanges})) {
13226:         if ($item eq 'encrypt') {
13227:             $needs_update = 1;
13228:             my %encrypted;
13229:             if ($context eq 'lti') {
13230:                 %encrypted = (
13231:                               crs  => {
13232:                                         on => &mt('Encryption of stored link protection secrets defined in courses enabled'),
13233:                                         off => &mt('Encryption of stored link protection secrets defined in courses disabled'),
13234:                                       },
13235:                               dom => {
13236:                                        on => &mt('Encryption of stored link protection secrets defined in domain enabled'),
13237:                                        off => &mt('Encryption of stored link protection secrets defined in domain disabled'),
13238:                                      },
13239:                               consumers => {
13240:                                              on => &mt('Encryption of stored consumer secrets defined in domain enabled'),
13241:                                              off => &mt('Encryption of stored consumer secrets defined in domain disabled'),
13242:                                            },
13243:                              );
13244:             } else {
13245:                 %encrypted = (
13246:                               crs  => {
13247:                                         on => &mt('Encryption of stored external tool secrets defined in courses enabled'),
13248:                                         off => &mt('Encryption of stored external tool secrets defined in courses disabled'),
13249:                                       },
13250:                               dom => {
13251:                                        on => &mt('Encryption of stored external tool secrets defined in domain enabled'),
13252:                                        off => &mt('Encryption of stored external tool secrets defined in domain disabled'),
13253:                                      },
13254:                              );
13255:             }
13256:             my @types= ('crs','dom');
13257:             if ($context eq 'lti') {
13258:                 foreach my $type (@types) {
13259:                     undef($domdefaults{'linkprotenc_'.$type});
13260:                 }
13261:                 push(@types,'consumers');
13262:                 undef($domdefaults{'ltienc_consumers'});
13263:             } elsif ($context eq 'ltitools') {
13264:                 foreach my $type (@types) {
13265:                     undef($domdefaults{'toolenc_'.$type});
13266:                 }
13267:             }
13268:             foreach my $type (@types) {
13269:                 my $shown = $encrypted{$type}{'off'};
13270:                 if (ref($newsec->{$item}) eq 'HASH') {
13271:                     if ($newsec->{$item}{$type}) {
13272:                         if ($context eq 'lti') {
13273:                             if ($type eq 'consumers') {
13274:                                 $domdefaults{'ltienc_consumers'} = 1;
13275:                             } else {
13276:                                 $domdefaults{'linkprotenc_'.$type} = 1;
13277:                             }
13278:                         } elsif ($context eq 'ltitools') {
13279:                             $domdefaults{'toolenc_'.$type} = 1;
13280:                         }
13281:                         $shown = $encrypted{$type}{'on'};
13282:                     }
13283:                 }
13284:                 $output .= '<li>'.$shown.'</li>';
13285:             }
13286:         } elsif ($item eq 'rules') {
13287:             my %titles = &Apache::lonlocal::texthash(
13288:                                       min   => 'Minimum password length',
13289:                                       max   => 'Maximum password length',
13290:                                       chars => 'Required characters',
13291:                          );
13292:             foreach my $rule ('min','max') {
13293:                 if ($newsec->{rules}{$rule} eq '') {
13294:                     if ($rule eq 'min') {
13295:                         $output .= '<li>'.&mt('[_1] not set.',$titles{$rule});
13296:                                    ' '.&mt('Default of [_1] will be used',
13297:                                            $Apache::lonnet::passwdmin).'</li>';
13298:                     } else {
13299:                         $output .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
13300:                     }
13301:                 } else {
13302:                     $output .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$newsec->{rules}{$rule}).'</li>';
13303:                 }
13304:             }
13305:             if (ref($newsec->{'rules'}{'chars'}) eq 'ARRAY') {
13306:                 if (@{$newsec->{'rules'}{'chars'}} > 0) {
13307:                     my %rulenames = &Apache::lonlocal::texthash(
13308:                                              uc => 'At least one upper case letter',
13309:                                              lc => 'At least one lower case letter',
13310:                                              num => 'At least one number',
13311:                                              spec => 'At least one non-alphanumeric',
13312:                                     );
13313:                     my $needed = '<ul><li>'.
13314:                                  join('</li><li>',map {$rulenames{$_} } @{$newsec->{'rules'}{'chars'}}).
13315:                                  '</li></ul>';
13316:                     $output .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
13317:                 } else {
13318:                     $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
13319:                 }
13320:             } else {
13321:                 $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
13322:             }
13323:         } elsif ($item eq 'private') {
13324:             $needs_update = 1;
13325:             if ($context eq 'lti') {
13326:                 undef($domdefaults{'ltiprivhosts'});
13327:             } elsif ($context eq 'ltitools') {
13328:                 undef($domdefaults{'toolprivhosts'});
13329:             }
13330:             if (keys(%{$newkeyset})) {
13331:                 my @privhosts;
13332:                 foreach my $hostid (sort(keys(%{$newkeyset}))) {
13333:                     if ($keystore->{$hostid} eq 'ok') {
13334:                         $output .= '<li>'.&mt('Encryption key for storage of shared secrets saved for [_1]',$hostid).'</li>';
13335:                         unless (grep(/^\Q$hostid\E$/,@privhosts)) {
13336:                             push(@privhosts,$hostid);
13337:                         }
13338:                     }
13339:                 }
13340:                 if (@privhosts) {
13341:                     if ($context eq 'lti') {
13342:                         $domdefaults{'ltiprivhosts'} = \@privhosts;
13343:                     } elsif ($context eq 'ltitools') {
13344:                         $domdefaults{'toolprivhosts'} = \@privhosts;
13345:                     }
13346:                 }
13347:             }
13348:         } elsif ($item eq 'linkprot') {
13349:             next;
13350:         } elsif ($item eq 'suggested') {
13351:             if ((ref($secchanges->{'suggested'}) eq 'HASH') &&
13352:                 (ref($newsec->{'suggested'}) eq 'HASH')) {
13353:                 my $suggestions;
13354:                 foreach my $id (sort { $a <=> $b } keys(%{$secchanges->{'suggested'}})) {
13355:                     if (ref($newsec->{'suggested'}->{$id}) eq 'HASH') {
13356:                         my $name = $newsec->{'suggested'}->{$id}->{'name'};
13357:                         my $info = $newsec->{'suggested'}->{$id}->{'info'};
13358:                         $suggestions .= '<li>'.&mt('Launcher: [_1]',$name).'<br />'.
13359:                                                &mt('Recommend: [_1]','<pre>'.$info.'</pre>').
13360:                                         '</li>';
13361:                     } else {
13362:                         $suggestions .= '<li>'.&mt('Recommendations deleted for Launcher: [_1]',
13363:                                                    $newsec->{'suggested'}->{$id}).'</li>';
13364:                     }
13365:                 }
13366:                 if ($suggestions) {
13367:                     $output .= '<li>'.&mt('Hints in Courses for Link Protector Configuration').
13368:                                '<ul>'.$suggestions.'</ul>'.
13369:                                '</li>';
13370:                 }
13371:             }
13372:         }
13373:     }
13374:     if ($needs_update) {
13375:         my $cachetime = 24*60*60;
13376:         &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13377:     }
13378:     return $output;
13379: }
13380: 
13381: sub modify_lti {
13382:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13383:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13384:     my ($newid,@allpos,%changes,%confhash,%ltienc,$errors,$resulttext);
13385:     my (%posslti,%posslticrs,%posscrstype);
13386:     my @courseroles = ('cc','in','ta','ep','st');
13387:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
13388:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
13389:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
13390:     my %coursetypetitles = &Apache::lonlocal::texthash (
13391:                                official   => 'Official',
13392:                                unofficial => 'Unofficial',
13393:                                community  => 'Community',
13394:                                textbook   => 'Textbook',
13395:                                placement  => 'Placement Test',
13396:                                lti        => 'LTI Provider',
13397:     );
13398:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13399:     my %lt = &lti_names();
13400:     map { $posslti{$_} = 1; } @ltiroles;
13401:     map { $posslticrs{$_} = 1; } @lticourseroles;
13402:     map { $posscrstype{$_} = 1; } @coursetypes;
13403: 
13404:     my %menutitles = &ltimenu_titles();
13405:     my (%currltisec,%secchanges,%newltisec,%newltienc,%newkeyset);
13406: 
13407:     &fetch_secrets($dom,'ltisec',\%domconfig,\%currltisec,\%secchanges,\%newltisec,\%newkeyset);
13408: 
13409:     my (%linkprotchg,$linkprotoutput,$is_home);
13410:     my $proterror = &Apache::courseprefs::process_linkprot($dom,'',$currltisec{'linkprot'},
13411:                                                            \%linkprotchg,'domain');
13412:     my $home = &Apache::lonnet::domain($dom,'primary');
13413:     unless (($home eq 'no_host') || ($home eq '')) {
13414:         my @ids=&Apache::lonnet::current_machine_ids();
13415:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; } }
13416:     }
13417: 
13418:     if (keys(%linkprotchg)) {
13419:         $secchanges{'linkprot'} = 1;
13420:         my %oldlinkprot;
13421:         if (ref($currltisec{'linkprot'}) eq 'HASH') {
13422:             %oldlinkprot = %{$currltisec{'linkprot'}};
13423:         }
13424:         foreach my $id (keys(%linkprotchg)) {
13425:             if (ref($linkprotchg{$id}) eq 'HASH') {
13426:                 foreach my $inner (keys(%{$linkprotchg{$id}})) {
13427:                     if (($inner eq 'secret') || ($inner eq 'key')) {
13428:                         if ($is_home) {
13429:                             $newltienc{$id}{$inner} = $linkprotchg{$id}{$inner};
13430:                         }
13431:                     }
13432:                 }
13433:             } else {
13434:                 $newltisec{'linkprot'}{$id} = $linkprotchg{$id};
13435:             }
13436:         }
13437:         $linkprotoutput = &Apache::courseprefs::store_linkprot($dom,'','domain',\%linkprotchg,\%oldlinkprot);
13438:         if (keys(%linkprotchg)) {
13439:             %{$newltisec{'linkprot'}} = %linkprotchg;
13440:         }
13441:     }
13442:     if (ref($currltisec{'linkprot'}) eq 'HASH') {
13443:         foreach my $id (keys(%{$currltisec{'linkprot'}})) {
13444:             next if ($id !~ /^\d+$/);
13445:             unless (exists($linkprotchg{$id})) {
13446:                 if (ref($currltisec{'linkprot'}{$id}) eq 'HASH') {
13447:                     foreach my $inner (keys(%{$currltisec{'linkprot'}{$id}})) {
13448:                         if (($inner eq 'secret') || ($inner eq 'key')) {
13449:                             if ($is_home) {
13450:                                 $newltienc{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
13451:                             }
13452:                         } else {
13453:                             $newltisec{'linkprot'}{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
13454:                         }
13455:                     }
13456:                 } else {
13457:                     $newltisec{'linkprot'}{$id} = $currltisec{'linkprot'}{$id};
13458:                 }
13459:             }
13460:         }
13461:     }
13462:     if ($proterror) {
13463:         $errors .= '<li>'.$proterror.'</li>';
13464:     }
13465: 
13466:     my (%delsuggested,%suggids,@suggested);;
13467:     if (ref($currltisec{'suggested'}) eq 'HASH') {
13468:         my $maxnum = $env{'form.linkprot_suggested_maxnum'};
13469:         my @todelete = &Apache::loncommon::get_env_multiple('form.linkprot_suggested_del');
13470:         for (my $i=0; $i<$maxnum; $i++) {
13471:             my $itemid = $env{'form.linkprot_suggested_id_'.$i};
13472:             $itemid =~ s/\D+//g;
13473:             if ($itemid) {
13474:                 if (ref($currltisec{'suggested'}->{$itemid}) eq 'HASH') {
13475:                     push(@suggested,$i);
13476:                     $suggids{$i} = $itemid;
13477:                     if ((@todelete > 0) && (grep(/^$i$/,@todelete))) {
13478:                         if (ref($currltisec{'suggested'}{$itemid}) eq 'HASH') {
13479:                             $delsuggested{$itemid} = $currltisec{'suggested'}{$itemid}{'name'};
13480:                         }
13481:                     } else {
13482:                         if ($env{'form.linkprot_suggested_name_'.$i} eq '') {
13483:                             $delsuggested{$itemid} = $currltisec{'suggested'}{$itemid}{'name'};
13484:                         } else {
13485:                             $env{'form.linkprot_suggested_name_'.$i} =~ s/(`)/'/g;
13486:                             $env{'form.linkprot_suggested_info_'.$i} =~ s/(`)/'/g;
13487:                             $newltisec{'suggested'}{$itemid}{'name'} = $env{'form.linkprot_suggested_name_'.$i};
13488:                             $newltisec{'suggested'}{$itemid}{'info'} = $env{'form.linkprot_suggested_info_'.$i};
13489:                             if (($currltisec{'suggested'}{$itemid}{'name'} ne $newltisec{'suggested'}{$itemid}{'name'}) ||
13490:                                 ($currltisec{'suggested'}{$itemid}{'info'} ne $newltisec{'suggested'}{$itemid}{'info'})) {
13491:                                 $secchanges{'suggested'}{$itemid} = 1;
13492:                             }
13493:                         }
13494:                     }
13495:                 }
13496:             }
13497:         }
13498:     }
13499:     foreach my $key (keys(%delsuggested)) {
13500:         $newltisec{'suggested'}{$key} = $delsuggested{$key};
13501:         $secchanges{'suggested'}{$key} = 1;
13502:     }
13503:     if (($env{'form.linkprot_suggested_add'}) &&
13504:         ($env{'form.linkprot_suggested_name_add'} ne '')) {
13505:         $env{'form.linkprot_suggested_name_add'} =~ s/(`)/'/g;
13506:         $env{'form.linkprot_suggested_info_add'} =~ s/(`)/'/g;
13507:         my ($newsuggid,$errormsg) = &get_lti_id($dom,$env{'form.linkprot_suggested_name_add'},'suggested');
13508:         if ($newsuggid) {
13509:             $newltisec{'suggested'}{$newsuggid}{'name'} = $env{'form.linkprot_suggested_name_add'};
13510:             $newltisec{'suggested'}{$newsuggid}{'info'} = $env{'form.linkprot_suggested_info_add'};
13511:             $secchanges{'suggested'}{$newsuggid} = 1;
13512:         } else {
13513:             my $error = &mt('Failed to acquire unique ID for new Link Protectors in Courses Suggestion');
13514:             if ($errormsg) {
13515:                 $error .= ' ('.$errormsg.')';
13516:             }
13517:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13518:         }
13519:     }
13520:     my (@items,%deletions,%itemids);
13521:     if ($env{'form.lti_add'}) {
13522:         my $consumer = $env{'form.lti_consumer_add'};
13523:         $consumer =~ s/(`)/'/g;
13524:         ($newid,my $errormsg) = &get_lti_id($dom,$consumea,'lt'r);
13525:         if ($newid) {
13526:             $itemids{'add'} = $newid;
13527:             push(@items,'add');
13528:             $changes{$newid} = 1;
13529:         } else {
13530:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
13531:             if ($errormsg) {
13532:                 $error .= ' ('.$errormsg.')';
13533:             }
13534:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13535:         }
13536:     }
13537:     if (ref($domconfig{$action}) eq 'HASH') {
13538:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
13539:         if (@todelete) {
13540:             map { $deletions{$_} = 1; } @todelete;
13541:         }
13542:         my $maxnum = $env{'form.lti_maxnum'};
13543:         for (my $i=0; $i<$maxnum; $i++) {
13544:             my $itemid = $env{'form.lti_id_'.$i};
13545:             $itemid =~ s/\D+//g;
13546:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
13547:                 if ($deletions{$itemid}) {
13548:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
13549:                 } else {
13550:                     push(@items,$i);
13551:                     $itemids{$i} = $itemid;
13552:                 }
13553:             }
13554:         }
13555:     }
13556:     my (%keystore,$secstored);
13557:     if ($is_home) {
13558:         &store_security($dom,'lti',\%secchanges,\%newkeyset,\%keystore);
13559:     }
13560: 
13561:     my ($cipher,$privnum);
13562:     if ((@items > 0) && ($is_home)) {
13563:         ($cipher,$privnum) = &get_priv_creds($dom,$home,$secchanges{'encrypt'},
13564:                                              $newltisec{'encrypt'},$keystore{$home});
13565:     }
13566:     foreach my $idx (@items) {
13567:         my $itemid = $itemids{$idx};
13568:         next unless ($itemid);
13569:         my %currlti;
13570:         unless ($idx eq 'add') {
13571:             if (ref($domconfig{$action}) eq 'HASH') {
13572:                 if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
13573:                     %currlti = %{$domconfig{$action}{$itemid}};
13574:                 }
13575:             }
13576:         }
13577:         my $position = $env{'form.lti_pos_'.$itemid};
13578:         $position =~ s/\D+//g;
13579:         if ($position ne '') {
13580:             $allpos[$position] = $itemid;
13581:         }
13582:         foreach my $item ('consumer','lifetime','requser','crsinc') {
13583:             my $formitem = 'form.lti_'.$item.'_'.$idx;
13584:             $env{$formitem} =~ s/(`)/'/g;
13585:             if ($item eq 'lifetime') {
13586:                 $env{$formitem} =~ s/[^\d.]//g;
13587:             }
13588:             if ($env{$formitem} ne '') {
13589:                 $confhash{$itemid}{$item} = $env{$formitem};
13590:                 unless (($idx eq 'add') || ($changes{$itemid})) {
13591:                     if ($currlti{$item} ne $confhash{$itemid}{$item}) {
13592:                         $changes{$itemid} = 1;
13593:                     }
13594:                 }
13595:             }
13596:         }
13597:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
13598:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
13599:         }
13600:         if ($confhash{$itemid}{'requser'}) {
13601:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
13602:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid';
13603:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
13604:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
13605:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
13606:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
13607:                 $mapuser =~ s/(`)/'/g;
13608:                 $mapuser =~ s/^\s+|\s+$//g;
13609:                 $confhash{$itemid}{'mapuser'} = $mapuser;
13610:             }
13611:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
13612:             my @makeuser;
13613:             foreach my $ltirole (sort(@possmakeuser)) {
13614:                 if ($posslti{$ltirole}) {
13615:                     push(@makeuser,$ltirole);
13616:                 }
13617:             }
13618:             $confhash{$itemid}{'makeuser'} = \@makeuser;
13619:             if (@makeuser) {
13620:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
13621:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
13622:                     $confhash{$itemid}{'lcauth'} = $lcauth;
13623:                     if ($lcauth ne 'internal') {
13624:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
13625:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
13626:                         $lcauthparm =~ s/`//g;
13627:                         if ($lcauthparm ne '') {
13628:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
13629:                         }
13630:                     }
13631:                 } else {
13632:                     $confhash{$itemid}{'lcauth'} = 'lti';
13633:                 }
13634:             }
13635:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
13636:             if (@possinstdata) {
13637:                 foreach my $field (@possinstdata) {
13638:                     if (exists($fieldtitles{$field})) {
13639:                         push(@{$confhash{$itemid}{'instdata'}});
13640:                     }
13641:                 }
13642:             }
13643:             if ($env{'form.lti_callback_'.$idx}) {
13644:                 if ($env{'form.lti_callbackparam_'.$idx}) {
13645:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
13646:                     $callback =~ s/^\s+|\s+$//g;
13647:                     $confhash{$itemid}{'callback'} = $callback;
13648:                 }
13649:             }
13650:             foreach my $field ('topmenu','inlinemenu') {
13651:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
13652:                     $confhash{$itemid}{$field} = 1;
13653:                 }
13654:             }
13655:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
13656:                 $confhash{$itemid}{lcmenu} = [];
13657:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
13658:                 foreach my $field (@possmenu) {
13659:                     if (exists($menutitles{$field})) {
13660:                         if ($field eq 'grades') {
13661:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
13662:                         }
13663:                         push(@{$confhash{$itemid}{lcmenu}},$field);
13664:                     }
13665:                 }
13666:             }
13667:             if ($confhash{$itemid}{'crsinc'}) {
13668:                 if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
13669:                     ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
13670:                     $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
13671:                 } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
13672:                     my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx};
13673:                     $mapcrs =~ s/(`)/'/g;
13674:                     $mapcrs =~ s/^\s+|\s+$//g;
13675:                     $confhash{$itemid}{'mapcrs'} = $mapcrs;
13676:                 }
13677:                 my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
13678:                 my @crstypes;
13679:                 foreach my $type (sort(@posstypes)) {
13680:                     if ($posscrstype{$type}) {
13681:                         push(@crstypes,$type);
13682:                     }
13683:                 }
13684:                 $confhash{$itemid}{'mapcrstype'} = \@crstypes;
13685:                 if ($env{'form.lti_storecrs_'.$idx}) {
13686:                     $confhash{$itemid}{'storecrs'} = 1;
13687:                 }
13688:                 if ($env{'form.lti_makecrs_'.$idx}) {
13689:                     $confhash{$itemid}{'makecrs'} = 1;
13690:                 }
13691:                 foreach my $ltirole (@lticourseroles) {
13692:                     my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
13693:                     if (grep(/^\Q$possrole\E$/,@courseroles)) {
13694:                         $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
13695:                     }
13696:                 }
13697:                 my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
13698:                 my @selfenroll;
13699:                 foreach my $type (sort(@possenroll)) {
13700:                     if ($posslticrs{$type}) {
13701:                         push(@selfenroll,$type);
13702:                     }
13703:                 }
13704:                 $confhash{$itemid}{'selfenroll'} = \@selfenroll;
13705:                 if ($env{'form.lti_crssec_'.$idx}) {
13706:                     if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
13707:                         $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
13708:                     } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
13709:                         my $section = $env{'form.lti_customsection_'.$idx};
13710:                         $section =~ s/(`)/'/g;
13711:                         $section =~ s/^\s+|\s+$//g;
13712:                         if ($section ne '') {
13713:                             $confhash{$itemid}{'section'} = $section;
13714:                         }
13715:                     }
13716:                 }
13717:                 foreach my $field ('passback','roster') {
13718:                     if ($env{'form.lti_'.$field.'_'.$idx}) {
13719:                         $confhash{$itemid}{$field} = 1;
13720:                     }
13721:                 }
13722:                 if ($env{'form.lti_passback_'.$idx}) {
13723:                     if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
13724:                         $confhash{$itemid}{'passbackformat'} = '1.0';
13725:                     } else {
13726:                         $confhash{$itemid}{'passbackformat'} = '1.1';
13727:                     }
13728:                 }
13729:             }
13730:             unless (($idx eq 'add') || ($changes{$itemid})) {
13731:                 if ($confhash{$itemid}{'crsinc'}) {
13732:                     foreach my $field ('mapcrs','storecrs','makecrs','section','passback','roster') {
13733:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
13734:                             $changes{$itemid} = 1;
13735:                         }
13736:                     }
13737:                     unless ($changes{$itemid}) {
13738:                         if ($currlti{'passback'} eq $confhash{$itemid}{'passback'}) {
13739:                             if ($currlti{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
13740:                                 $changes{$itemid} = 1;
13741:                             }
13742:                         }
13743:                     }
13744:                     foreach my $field ('mapcrstype','selfenroll') {
13745:                         unless ($changes{$itemid}) {
13746:                             if (ref($currlti{$field}) eq 'ARRAY') {
13747:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
13748:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
13749:                                                                                    $confhash{$itemid}{$field});
13750:                                     if (@diffs) {
13751:                                         $changes{$itemid} = 1;
13752:                                     }
13753:                                 } elsif (@{$currlti{$field}} > 0) {
13754:                                     $changes{$itemid} = 1;
13755:                                 }
13756:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
13757:                                 if (@{$confhash{$itemid}{$field}} > 0) {
13758:                                     $changes{$itemid} = 1;
13759:                                 }
13760:                             }
13761:                         }
13762:                     }
13763:                     unless ($changes{$itemid}) {
13764:                         if (ref($currlti{'maproles'}) eq 'HASH') {
13765:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
13766:                                 foreach my $ltirole (keys(%{$currlti{'maproles'}})) {
13767:                                     if ($currlti{'maproles'}{$ltirole} ne
13768:                                         $confhash{$itemid}{'maproles'}{$ltirole}) {
13769:                                         $changes{$itemid} = 1;
13770:                                         last;
13771:                                     }
13772:                                 }
13773:                                 unless ($changes{$itemid}) {
13774:                                     foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
13775:                                         if ($confhash{$itemid}{'maproles'}{$ltirole} ne
13776:                                             $currlti{'maproles'}{$ltirole}) {
13777:                                             $changes{$itemid} = 1;
13778:                                             last;
13779:                                         }
13780:                                     }
13781:                                 }
13782:                             } elsif (keys(%{$currlti{'maproles'}}) > 0) {
13783:                                 $changes{$itemid} = 1;
13784:                             }
13785:                         } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
13786:                             unless ($changes{$itemid}) {
13787:                                 if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
13788:                                     $changes{$itemid} = 1;
13789:                                 }
13790:                             }
13791:                         }
13792:                     }
13793:                 }
13794:                 unless ($changes{$itemid}) {
13795:                     foreach my $field ('mapuser','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
13796:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
13797:                             $changes{$itemid} = 1;
13798:                         }
13799:                     }
13800:                     unless ($changes{$itemid}) {
13801:                         foreach my $field ('makeuser','lcmenu') {
13802:                             if (ref($currlti{$field}) eq 'ARRAY') {
13803:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
13804:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
13805:                                                                                    $confhash{$itemid}{$field});
13806:                                     if (@diffs) {
13807:                                         $changes{$itemid} = 1;
13808:                                     }
13809:                                 } elsif (@{$currlti{$field}} > 0) {
13810:                                     $changes{$itemid} = 1;
13811:                                 }
13812:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
13813:                                 if (@{$confhash{$itemid}{$field}} > 0) {
13814:                                     $changes{$itemid} = 1;
13815:                                 }
13816:                             }
13817:                         }
13818:                     }
13819:                 }
13820:             }
13821:         }
13822:         if ($is_home) {
13823:             my $keyitem = 'form.lti_key_'.$idx;
13824:             $env{$keyitem} =~ s/(`)/'/g;
13825:             if ($env{$keyitem} ne '') {
13826:                 $ltienc{$itemid}{'key'} = $env{$keyitem};
13827:                 unless ($changes{$itemid}) {
13828:                     if ($currlti{'key'} ne $env{$keyitem}) {
13829:                         $changes{$itemid} = 1;
13830:                     }
13831:                 }
13832:             }
13833:             my $secretitem = 'form.lti_secret_'.$idx;
13834:             $env{$secretitem} =~ s/(`)/'/g;
13835:             if ($currlti{'usable'}) {
13836:                 if ($env{'form.lti_changesecret_'.$idx}) {
13837:                     if ($env{$secretitem} ne '') {
13838:                         if ($privnum && $cipher) {
13839:                             $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
13840:                             $confhash{$itemid}{'cipher'} = $privnum;
13841:                         } else {
13842:                             $ltienc{$itemid}{'secret'} = $env{$secretitem};
13843:                         }
13844:                         $changes{$itemid} = 1;
13845:                     }
13846:                 } else {
13847:                     $ltienc{$itemid}{'secret'} = $currlti{'secret'};
13848:                     $confhash{$itemid}{'cipher'} = $currlti{'cipher'};
13849:                 }
13850:                 if (ref($ltienc{$itemid}) eq 'HASH') {
13851:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'secret'} ne '')) {
13852:                         $confhash{$itemid}{'usable'} = 1;
13853:                     }
13854:                 }
13855:             } elsif ($env{$secretitem} ne '') {
13856:                 if ($privnum && $cipher) {
13857:                     $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
13858:                     $confhash{$itemid}{'cipher'} = $privnum;
13859:                 } else {
13860:                     $ltienc{$itemid}{'secret'} = $env{$secretitem};
13861:                 }
13862:                 if (ref($ltienc{$itemid}) eq 'HASH') {
13863:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'key'} ne '')) {
13864:                         $confhash{$itemid}{'usable'} = 1;
13865:                     }
13866:                 }
13867:                 $changes{$itemid} = 1;
13868:             }
13869:         }
13870:         unless ($changes{$itemid}) {
13871:             foreach my $key (keys(%currlti)) {
13872:                 if (ref($currlti{$key}) eq 'HASH') {
13873:                     if (ref($confhash{$itemid}{$key}) eq 'HASH') {
13874:                         foreach my $innerkey (keys(%{$currlti{$key}})) {
13875:                             unless (exists($confhash{$itemid}{$key}{$innerkey})) {
13876:                                 $changes{$itemid} = 1;
13877:                                 last;
13878:                             }
13879:                         }
13880:                     } elsif (keys(%{$currlti{$key}}) > 0) {
13881:                         $changes{$itemid} = 1;
13882:                     }
13883:                 }
13884:                 last if ($changes{$itemid});
13885:             }
13886:         }
13887:     }
13888:     if (@allpos > 0) {
13889:         my $idx = 0;
13890:         foreach my $itemid (@allpos) {
13891:             if ($itemid ne '') {
13892:                 $confhash{$itemid}{'order'} = $idx;
13893:                 if (ref($domconfig{$action}) eq 'HASH') {
13894:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
13895:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
13896:                             $changes{$itemid} = 1;
13897:                         }
13898:                     }
13899:                 }
13900:                 $idx ++;
13901:             }
13902:         }
13903:     }
13904: 
13905:     if ((keys(%changes) == 0) && (keys(%secchanges) == 0)) {
13906:         return &mt('No changes made.');
13907:     }
13908: 
13909:     my %ltihash = (
13910:                       $action => { %confhash }
13911:                   );
13912:     my %ltienchash;
13913: 
13914:     if ($is_home) {
13915:         %ltienchash = (
13916:                          $action => { %ltienc }
13917:                       );
13918:     }
13919:     if (keys(%secchanges)) {
13920:         $ltihash{'ltisec'} = \%newltisec;
13921:         if ($secchanges{'linkprot'}) {
13922:             if ($is_home) {
13923:                 $ltienchash{'linkprot'} = \%newltienc;
13924:             }
13925:         }
13926:     }
13927:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,$dom);
13928:     if ($putresult eq 'ok') {
13929:         if (keys(%ltienchash)) {
13930:             &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
13931:         }
13932:         $resulttext = &mt('Changes made:').'<ul>';
13933:         if (keys(%secchanges) > 0) {
13934:             $resulttext .= &lti_security_results($dom,'lti',\%secchanges,\%newltisec,\%newkeyset,\%keystore);
13935:             if (exists($secchanges{'linkprot'})) {
13936:                 $resulttext .= $linkprotoutput;
13937:             }
13938:         }
13939:         if (keys(%changes) > 0) {
13940:             my $cachetime = 24*60*60;
13941:             &Apache::lonnet::do_cache_new('lti',$dom,\%confhash,$cachetime);
13942:             if (ref($lastactref) eq 'HASH') {
13943:                 $lastactref->{'lti'} = 1;
13944:             }
13945:             my %bynum;
13946:             foreach my $itemid (sort(keys(%changes))) {
13947:                 if (ref($confhash{$itemid}) eq 'HASH') {
13948:                     my $position = $confhash{$itemid}{'order'};
13949:                     $bynum{$position} = $itemid;
13950:                 }
13951:             }
13952:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
13953:                 my $itemid = $bynum{$pos};
13954:                 if (ref($confhash{$itemid}) eq 'HASH') {
13955:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b><ul>';
13956:                     my $position = $pos + 1;
13957:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
13958:                     foreach my $item ('version','lifetime') {
13959:                         if ($confhash{$itemid}{$item} ne '') {
13960:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
13961:                         }
13962:                     }
13963:                     if ($ltienc{$itemid}{'key'} ne '') {
13964:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$ltienc{$itemid}{'key'}.'</li>';
13965:                     }
13966:                     if ($ltienc{$itemid}{'secret'} ne '') {
13967:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;['.&mt('not shown').']</li>';
13968:                     }
13969:                     if ($confhash{$itemid}{'requser'}) {
13970:                         if ($confhash{$itemid}{'callback'}) {
13971:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
13972:                         } else {
13973:                             $resulttext .= '<li>'.&mt('Callback to logout LON-CAPA on log out from Consumer').'</li>';
13974:                         }
13975:                         if ($confhash{$itemid}{'mapuser'}) {
13976:                             my $shownmapuser;
13977:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
13978:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
13979:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
13980:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
13981:                             } else {
13982:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
13983:                             }
13984:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
13985:                         }
13986:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
13987:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) {
13988:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
13989:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
13990:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
13991:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
13992:                                 } else {
13993:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
13994:                                                        $confhash{$itemid}{'lcauth'});
13995:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
13996:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
13997:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
13998:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
13999:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
14000:                                         }
14001:                                     } else {
14002:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
14003:                                     }
14004:                                 }
14005:                                 $resulttext .= '</li>';
14006:                             } else {
14007:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
14008:                             }
14009:                         }
14010:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
14011:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
14012:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
14013:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
14014:                             } else {
14015:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
14016:                             }
14017:                         }
14018:                         foreach my $item ('topmenu','inlinemenu') {
14019:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
14020:                             if ($confhash{$itemid}{$item}) {
14021:                                 $resulttext .= &mt('Yes');
14022:                             } else {
14023:                                 $resulttext .= &mt('No');
14024:                             }
14025:                             $resulttext .= '</li>';
14026:                         }
14027:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
14028:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
14029:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
14030:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
14031:                             } else {
14032:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
14033:                             }
14034:                         }
14035:                         if ($confhash{$itemid}{'crsinc'}) {
14036:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
14037:                                 my $rolemaps;
14038:                                 foreach my $role (@ltiroles) {
14039:                                     if ($confhash{$itemid}{'maproles'}{$role}) {
14040:                                         $rolemaps .= ('&nbsp;'x2).$role.'='.
14041:                                                      &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
14042:                                                                                 'Course').',';
14043:                                     }
14044:                                 }
14045:                                 if ($rolemaps) {
14046:                                     $rolemaps =~ s/,$//;
14047:                                     $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
14048:                                 }
14049:                             }
14050:                             if ($confhash{$itemid}{'mapcrs'}) {
14051:                                 $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
14052:                             }
14053:                             if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
14054:                                 if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
14055:                                     $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
14056:                                                    join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
14057:                                                    '</li>';
14058:                                 } else {
14059:                                     $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
14060:                                 }
14061:                             }
14062:                             if ($confhash{$itemid}{'storecrs'}) {
14063:                                 $resulttext .= '<li>'.&mt('Store mapping of course identifier to LON-CAPA CourseID').': '.$confhash{$itemid}{'storecrs'}.'</li>';
14064:                             }
14065:                             if ($confhash{$itemid}{'makecrs'}) {
14066:                                 $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
14067:                             } else {
14068:                                 $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
14069:                             }
14070:                             if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
14071:                                 if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
14072:                                     $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
14073:                                                               join(', ',@{$confhash{$itemid}{'selfenroll'}})).
14074:                                                    '</li>';
14075:                                 } else {
14076:                                     $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
14077:                                 }
14078:                             }
14079:                             if ($confhash{$itemid}{'section'}) {
14080:                                 if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
14081:                                     $resulttext .= '<li>'.&mt('User section from standard field:').
14082:                                                          ' (course_section_sourcedid)'.'</li>';
14083:                                 } else {
14084:                                     $resulttext .= '<li>'.&mt('User section from:').' '.
14085:                                                           $confhash{$itemid}{'section'}.'</li>';
14086:                                 }
14087:                             } else {
14088:                                 $resulttext .= '<li>'.&mt('No section assignment').'</li>';
14089:                             }
14090:                             foreach my $item ('passback','roster','topmenu','inlinemenu') {
14091:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
14092:                                 if ($confhash{$itemid}{$item}) {
14093:                                     $resulttext .= &mt('Yes');
14094:                                     if ($item eq 'passback') {
14095:                                         if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
14096:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
14097:                                         } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
14098:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
14099:                                         }
14100:                                     }
14101:                                 } else {
14102:                                     $resulttext .= &mt('No');
14103:                                 }
14104:                                 $resulttext .= '</li>';
14105:                             }
14106:                             if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
14107:                                 if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
14108:                                     $resulttext .= '<li>'.&mt('Menu items:').' '.
14109:                                                    join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
14110:                                 } else {
14111:                                     $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
14112:                                 }
14113:                             }
14114:                         }
14115:                     }
14116:                     $resulttext .= '</ul></li>';
14117:                 }
14118:             }
14119:             if (keys(%deletions)) {
14120:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
14121:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
14122:                 }
14123:             }
14124:         }
14125:         $resulttext .= '</ul>';
14126:         if (ref($lastactref) eq 'HASH') {
14127:             if (($secchanges{'encrypt'}) || ($secchanges{'private'}) || (exists($secchanges{'suggested'}))) {
14128:                 &Apache::lonnet::get_domain_defaults($dom,1);
14129:                 $lastactref->{'domdefaults'} = 1;
14130:             }
14131:         }
14132:     } else {
14133:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14134:     }
14135:     if ($errors) {
14136:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14137:                        $errors.'</ul>';
14138:     }
14139:     return $resulttext;
14140: }
14141: 
14142: sub get_priv_creds {
14143:     my ($dom,$home,$encchg,$encrypt,$storedsec) = @_;
14144:     my ($needenc,$cipher,$privnum);
14145:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
14146:     if (($encchg) && (ref($encrypt) eq 'HASH')) {
14147:         $needenc = $encrypt->{'consumers'}
14148:     } else {
14149:         $needenc = $domdefs{'ltienc_consumers'};
14150:     }
14151:     if ($needenc) {
14152:         if (($storedsec eq 'ok') || ((ref($domdefs{'ltiprivhosts'}) eq 'ARRAY') &&
14153:                                      (grep(/^\Q$home\E$/,@{$domdefs{'ltiprivhosts'}})))) {
14154:                         my %privhash  = &Apache::lonnet::restore_dom('lti','private',$dom,$home,1);
14155:             my $privkey = $privhash{'key'};
14156:             $privnum = $privhash{'version'};
14157:             if (($privnum) && ($privkey ne '')) {
14158:                 $cipher = Crypt::CBC->new({'key'     => $privkey,
14159:                                           'cipher'  => 'DES'});
14160:             }
14161:         }
14162:     }
14163:     return ($cipher,$privnum);
14164: }
14165: 
14166: sub get_lti_id {
14167:     my ($domain,$consumer,$dbname) = @_;
14168:     unless (($dbname eq 'lti') || ($dbname eq 'suggested')) {
14169:         return ('','invalid db');
14170:     }
14171:     # get lock on db
14172:     my $lockhash = {
14173:                       lock => $env{'user.name'}.
14174:                               ':'.$env{'user.domain'},
14175:                    };
14176:     my $tries = 0;
14177:     my $gotlock = &Apache::lonnet::newput_dom($dbname,$lockhash,$domain);
14178:     my ($id,$error);
14179: 
14180:     while (($gotlock ne 'ok') && ($tries<10)) {
14181:         $tries ++;
14182:         sleep (0.1);
14183:         $gotlock = &Apache::lonnet::newput_dom($dbname,$lockhash,$domain);
14184:     }
14185:     if ($gotlock eq 'ok') {
14186:         my %currids = &Apache::lonnet::dump_dom($dbname,$domain);
14187:         if ($currids{'lock'}) {
14188:             delete($currids{'lock'});
14189:             if (keys(%currids)) {
14190:                 my @curr = sort { $a <=> $b } keys(%currids);
14191:                 if ($curr[-1] =~ /^\d+$/) {
14192:                     $id = 1 + $curr[-1];
14193:                 }
14194:             } else {
14195:                 $id = 1;
14196:             }
14197:             if ($id) {
14198:                 unless (&Apache::lonnet::newput_dom($dbname,{ $id => $consumer },$domain) eq 'ok') {
14199:                     $error = 'nostore';
14200:                 }
14201:             } else {
14202:                 $error = 'nonumber';
14203:             }
14204:         }
14205:         my $dellockoutcome = &Apache::lonnet::del_dom($dbname,['lock'],$domain);
14206:     } else {
14207:         $error = 'nolock';
14208:     }
14209:     return ($id,$error);
14210: }
14211: 
14212: sub modify_autoenroll {
14213:     my ($dom,$lastactref,%domconfig) = @_;
14214:     my ($resulttext,%changes);
14215:     my %currautoenroll;
14216:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
14217:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
14218:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
14219:         }
14220:     }
14221:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
14222:     my %title = ( run => 'Auto-enrollment active',
14223:                   sender => 'Sender for notification messages',
14224:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
14225:                   autofailsafe => 'Failsafe for no drops if institutional data missing for a section');
14226:     my @offon = ('off','on');
14227:     my $sender_uname = $env{'form.sender_uname'};
14228:     my $sender_domain = $env{'form.sender_domain'};
14229:     if ($sender_domain eq '') {
14230:         $sender_uname = '';
14231:     } elsif ($sender_uname eq '') {
14232:         $sender_domain = '';
14233:     }
14234:     my $coowners = $env{'form.autoassign_coowners'};
14235:     my $autofailsafe = $env{'form.autoenroll_autofailsafe'};
14236:     $autofailsafe =~ s{^\s+|\s+$}{}g;
14237:     if ($autofailsafe =~ /\D/) {
14238:         undef($autofailsafe);
14239:     }
14240:     my $failsafe = $env{'form.autoenroll_failsafe'};
14241:     unless (($failsafe eq 'zero') || ($failsafe eq 'any')) {
14242:         $failsafe = 'off';
14243:         undef($autofailsafe);
14244:     }
14245:     my %autoenrollhash =  (
14246:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
14247:                                        'sender_uname' => $sender_uname,
14248:                                        'sender_domain' => $sender_domain,
14249:                                        'co-owners' => $coowners,
14250:                                        'autofailsafe' => $autofailsafe,
14251:                                        'failsafe' => $failsafe,
14252:                                 }
14253:                      );
14254:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
14255:                                              $dom);
14256:     if ($putresult eq 'ok') {
14257:         if (exists($currautoenroll{'run'})) {
14258:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
14259:                  $changes{'run'} = 1;
14260:              }
14261:         } elsif ($autorun) {
14262:             if ($env{'form.autoenroll_run'} ne '1') {
14263:                  $changes{'run'} = 1;
14264:             }
14265:         }
14266:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
14267:             $changes{'sender'} = 1;
14268:         }
14269:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
14270:             $changes{'sender'} = 1;
14271:         }
14272:         if ($currautoenroll{'co-owners'} ne '') {
14273:             if ($currautoenroll{'co-owners'} ne $coowners) {
14274:                 $changes{'coowners'} = 1;
14275:             }
14276:         } elsif ($coowners) {
14277:             $changes{'coowners'} = 1;
14278:         }
14279:         if ($currautoenroll{'autofailsafe'} ne $autofailsafe) {
14280:             $changes{'autofailsafe'} = 1;
14281:         }
14282:         if ($currautoenroll{'failsafe'} ne $failsafe) {
14283:             $changes{'failsafe'} = 1;
14284:         }
14285:         if (keys(%changes) > 0) {
14286:             $resulttext = &mt('Changes made:').'<ul>';
14287:             if ($changes{'run'}) {
14288:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
14289:             }
14290:             if ($changes{'sender'}) {
14291:                 if ($sender_uname eq '' || $sender_domain eq '') {
14292:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
14293:                 } else {
14294:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
14295:                 }
14296:             }
14297:             if ($changes{'coowners'}) {
14298:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
14299:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
14300:                 if (ref($lastactref) eq 'HASH') {
14301:                     $lastactref->{'domainconfig'} = 1;
14302:                 }
14303:             }
14304:             if ($changes{'autofailsafe'}) {
14305:                 if ($autofailsafe ne '') {
14306:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$autofailsafe).'</li>';
14307:                 } else {
14308:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
14309:                 }
14310:             }
14311:             if ($changes{'failsafe'}) {
14312:                 if ($failsafe eq 'off') {
14313:                     unless ($changes{'autofailsafe'}) {
14314:                         $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
14315:                     }
14316:                 } elsif ($failsafe eq 'zero') {
14317:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero').'</li>';
14318:                 } else {
14319:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero or greater').'</li>';
14320:                 }
14321:             }
14322:             if (($changes{'autofailsafe'}) || ($changes{'failsafe'})) {
14323:                 &Apache::lonnet::get_domain_defaults($dom,1);
14324:                 if (ref($lastactref) eq 'HASH') {
14325:                     $lastactref->{'domdefaults'} = 1;
14326:                 }
14327:             }
14328:             $resulttext .= '</ul>';
14329:         } else {
14330:             $resulttext = &mt('No changes made to auto-enrollment settings');
14331:         }
14332:     } else {
14333:         $resulttext = '<span class="LC_error">'.
14334: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14335:     }
14336:     return $resulttext;
14337: }
14338: 
14339: sub modify_autoupdate {
14340:     my ($dom,%domconfig) = @_;
14341:     my ($resulttext,%currautoupdate,%fields,%changes);
14342:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
14343:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
14344:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
14345:         }
14346:     }
14347:     my @offon = ('off','on');
14348:     my %title = &Apache::lonlocal::texthash (
14349:                     run        => 'Auto-update:',
14350:                     classlists => 'Updates to user information in classlists?',
14351:                     unexpired  => 'Skip updates for users without active or future roles?',
14352:                     lastactive => 'Skip updates for inactive users?',
14353:                 );
14354:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14355:     my %fieldtitles = &Apache::lonlocal::texthash (
14356:                         id => 'Student/Employee ID',
14357:                         permanentemail => 'E-mail address',
14358:                         lastname => 'Last Name',
14359:                         firstname => 'First Name',
14360:                         middlename => 'Middle Name',
14361:                         generation => 'Generation',
14362:                       );
14363:     $othertitle = &mt('All users');
14364:     if (keys(%{$usertypes}) >  0) {
14365:         $othertitle = &mt('Other users');
14366:     }
14367:     foreach my $key (keys(%env)) {
14368:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
14369:             my ($usertype,$item) = ($1,$2);
14370:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
14371:                 if ($usertype eq 'default') {   
14372:                     push(@{$fields{$1}},$2);
14373:                 } elsif (ref($types) eq 'ARRAY') {
14374:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
14375:                         push(@{$fields{$1}},$2);
14376:                     }
14377:                 }
14378:             }
14379:         }
14380:     }
14381:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
14382:     @lockablenames = sort(@lockablenames);
14383:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
14384:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
14385:         if (@changed) {
14386:             $changes{'lockablenames'} = 1;
14387:         }
14388:     } else {
14389:         if (@lockablenames) {
14390:             $changes{'lockablenames'} = 1;
14391:         }
14392:     }
14393:     my %updatehash = (
14394:                       autoupdate => { run => $env{'form.autoupdate_run'},
14395:                                       classlists => $env{'form.classlists'},
14396:                                       unexpired  => $env{'form.unexpired'},
14397:                                       fields => {%fields},
14398:                                       lockablenames => \@lockablenames,
14399:                                     }
14400:                      );
14401:     my $lastactivedays;
14402:     if ($env{'form.lastactive'}) {
14403:         $lastactivedays = $env{'form.lastactivedays'};
14404:         $lastactivedays =~ s/^\s+|\s+$//g;
14405:         unless ($lastactivedays =~ /^\d+$/) {
14406:             undef($lastactivedays);
14407:             $env{'form.lastactive'} = 0;
14408:         }
14409:     }
14410:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
14411:     foreach my $key (keys(%currautoupdate)) {
14412:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
14413:             if (exists($updatehash{autoupdate}{$key})) {
14414:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
14415:                     $changes{$key} = 1;
14416:                 }
14417:             }
14418:         } elsif ($key eq 'fields') {
14419:             if (ref($currautoupdate{$key}) eq 'HASH') {
14420:                 foreach my $item (@{$types},'default') {
14421:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
14422:                         my $change = 0;
14423:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
14424:                             if (!exists($fields{$item})) {
14425:                                 $change = 1;
14426:                                 last;
14427:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
14428:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
14429:                                     $change = 1;
14430:                                     last;
14431:                                 }
14432:                             }
14433:                         }
14434:                         if ($change) {
14435:                             push(@{$changes{$key}},$item);
14436:                         }
14437:                     } 
14438:                 }
14439:             }
14440:         } elsif ($key eq 'lockablenames') {
14441:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
14442:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
14443:                 if (@changed) {
14444:                     $changes{'lockablenames'} = 1;
14445:                 }
14446:             } else {
14447:                 if (@lockablenames) {
14448:                     $changes{'lockablenames'} = 1;
14449:                 }
14450:             }
14451:         }
14452:     }
14453:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
14454:         if (@lockablenames) {
14455:             $changes{'lockablenames'} = 1;
14456:         }
14457:     }
14458:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
14459:         if ($updatehash{'autoupdate'}{'unexpired'}) {
14460:             $changes{'unexpired'} = 1;
14461:         }
14462:     }
14463:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
14464:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
14465:             $changes{'lastactive'} = 1;
14466:         }
14467:     }
14468:     foreach my $item (@{$types},'default') {
14469:         if (defined($fields{$item})) {
14470:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
14471:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
14472:                     my $change = 0;
14473:                     if (ref($fields{$item}) eq 'ARRAY') {
14474:                         foreach my $type (@{$fields{$item}}) {
14475:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
14476:                                 $change = 1;
14477:                                 last;
14478:                             }
14479:                         }
14480:                     }
14481:                     if ($change) {
14482:                         push(@{$changes{'fields'}},$item);
14483:                     }
14484:                 } else {
14485:                     push(@{$changes{'fields'}},$item);
14486:                 }
14487:             } else {
14488:                 push(@{$changes{'fields'}},$item);
14489:             }
14490:         }
14491:     }
14492:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
14493:                                              $dom);
14494:     if ($putresult eq 'ok') {
14495:         if (keys(%changes) > 0) {
14496:             $resulttext = &mt('Changes made:').'<ul>';
14497:             foreach my $key (sort(keys(%changes))) {
14498:                 if ($key eq 'lockablenames') {
14499:                     $resulttext .= '<li>';
14500:                     if (@lockablenames) {
14501:                         $usertypes->{'default'} = $othertitle;
14502:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
14503:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
14504:                     } else {
14505:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
14506:                     }
14507:                     $resulttext .= '</li>';
14508:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
14509:                     foreach my $item (@{$changes{$key}}) {
14510:                         my @newvalues;
14511:                         foreach my $type (@{$fields{$item}}) {
14512:                             push(@newvalues,$fieldtitles{$type});
14513:                         }
14514:                         my $newvaluestr;
14515:                         if (@newvalues > 0) {
14516:                             $newvaluestr = join(', ',@newvalues);
14517:                         } else {
14518:                             $newvaluestr = &mt('none');
14519:                         }
14520:                         if ($item eq 'default') {
14521:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
14522:                         } else {
14523:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
14524:                         }
14525:                     }
14526:                 } else {
14527:                     my $newvalue;
14528:                     if ($key eq 'run') {
14529:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
14530:                     } elsif ($key eq 'lastactive') {
14531:                         $newvalue = $offon[$env{'form.lastactive'}];
14532:                         unless ($lastactivedays eq '') {
14533:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
14534:                         }
14535:                     } else {
14536:                         $newvalue = $offon[$env{'form.'.$key}];
14537:                     }
14538:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
14539:                 }
14540:             }
14541:             $resulttext .= '</ul>';
14542:         } else {
14543:             $resulttext = &mt('No changes made to autoupdates');
14544:         }
14545:     } else {
14546:         $resulttext = '<span class="LC_error">'.
14547: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14548:     }
14549:     return $resulttext;
14550: }
14551: 
14552: sub modify_autocreate {
14553:     my ($dom,%domconfig) = @_;
14554:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
14555:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
14556:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
14557:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
14558:         }
14559:     }
14560:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
14561:                  req => 'Auto-creation of validated requests for official courses',
14562:                  xmldc => 'Identity of course creator of courses from XML files',
14563:                );
14564:     my @types = ('xml','req');
14565:     foreach my $item (@types) {
14566:         $newvals{$item} = $env{'form.autocreate_'.$item};
14567:         $newvals{$item} =~ s/\D//g;
14568:         $newvals{$item} = 0 if ($newvals{$item} eq '');
14569:     }
14570:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
14571:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14572:     unless (exists($domcoords{$newvals{'xmldc'}})) {
14573:         $newvals{'xmldc'} = '';
14574:     } 
14575:     %autocreatehash =  (
14576:                         autocreate => { xml => $newvals{'xml'},
14577:                                         req => $newvals{'req'},
14578:                                       }
14579:                        );
14580:     if ($newvals{'xmldc'} ne '') {
14581:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
14582:     }
14583:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
14584:                                              $dom);
14585:     if ($putresult eq 'ok') {
14586:         my @items = @types;
14587:         if ($newvals{'xml'}) {
14588:             push(@items,'xmldc');
14589:         }
14590:         foreach my $item (@items) {
14591:             if (exists($currautocreate{$item})) {
14592:                 if ($currautocreate{$item} ne $newvals{$item}) {
14593:                     $changes{$item} = 1;
14594:                 }
14595:             } elsif ($newvals{$item}) {
14596:                 $changes{$item} = 1;
14597:             }
14598:         }
14599:         if (keys(%changes) > 0) {
14600:             my @offon = ('off','on'); 
14601:             $resulttext = &mt('Changes made:').'<ul>';
14602:             foreach my $item (@types) {
14603:                 if ($changes{$item}) {
14604:                     my $newtxt = $offon[$newvals{$item}];
14605:                     $resulttext .= '<li>'.
14606:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
14607:                                        '<b>','</b>').
14608:                                    '</li>';
14609:                 }
14610:             }
14611:             if ($changes{'xmldc'}) {
14612:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
14613:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
14614:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
14615:             }
14616:             $resulttext .= '</ul>';
14617:         } else {
14618:             $resulttext = &mt('No changes made to auto-creation settings');
14619:         }
14620:     } else {
14621:         $resulttext = '<span class="LC_error">'.
14622:             &mt('An error occurred: [_1]',$putresult).'</span>';
14623:     }
14624:     return $resulttext;
14625: }
14626: 
14627: sub modify_directorysrch {
14628:     my ($dom,$lastactref,%domconfig) = @_;
14629:     my ($resulttext,%changes);
14630:     my %currdirsrch;
14631:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
14632:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
14633:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
14634:         }
14635:     }
14636:     my %title = ( available => 'Institutional directory search available',
14637:                   localonly => 'Other domains can search institution',
14638:                   lcavailable => 'LON-CAPA directory search available',
14639:                   lclocalonly => 'Other domains can search LON-CAPA domain',
14640:                   searchby => 'Search types',
14641:                   searchtypes => 'Search latitude');
14642:     my @offon = ('off','on');
14643:     my @otherdoms = ('Yes','No');
14644: 
14645:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
14646:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
14647:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
14648: 
14649:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14650:     if (keys(%{$usertypes}) == 0) {
14651:         @cansearch = ('default');
14652:     } else {
14653:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
14654:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
14655:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
14656:                     push(@{$changes{'cansearch'}},$type);
14657:                 }
14658:             }
14659:             foreach my $type (@cansearch) {
14660:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
14661:                     push(@{$changes{'cansearch'}},$type);
14662:                 }
14663:             }
14664:         } else {
14665:             push(@{$changes{'cansearch'}},@cansearch);
14666:         }
14667:     }
14668: 
14669:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
14670:         foreach my $by (@{$currdirsrch{'searchby'}}) {
14671:             if (!grep(/^\Q$by\E$/,@searchby)) {
14672:                 push(@{$changes{'searchby'}},$by);
14673:             }
14674:         }
14675:         foreach my $by (@searchby) {
14676:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
14677:                 push(@{$changes{'searchby'}},$by);
14678:             }
14679:         }
14680:     } else {
14681:         push(@{$changes{'searchby'}},@searchby);
14682:     }
14683: 
14684:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
14685:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
14686:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
14687:                 push(@{$changes{'searchtypes'}},$type);
14688:             }
14689:         }
14690:         foreach my $type (@searchtypes) {
14691:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
14692:                 push(@{$changes{'searchtypes'}},$type);
14693:             }
14694:         }
14695:     } else {
14696:         if (exists($currdirsrch{'searchtypes'})) {
14697:             foreach my $type (@searchtypes) {  
14698:                 if ($type ne $currdirsrch{'searchtypes'}) { 
14699:                     push(@{$changes{'searchtypes'}},$type);
14700:                 }
14701:             }
14702:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
14703:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
14704:             }   
14705:         } else {
14706:             push(@{$changes{'searchtypes'}},@searchtypes); 
14707:         }
14708:     }
14709: 
14710:     my %dirsrch_hash =  (
14711:             directorysrch => { available => $env{'form.dirsrch_available'},
14712:                                cansearch => \@cansearch,
14713:                                localonly => $env{'form.dirsrch_instlocalonly'},
14714:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
14715:                                lcavailable => $env{'form.dirsrch_domavailable'},
14716:                                searchby => \@searchby,
14717:                                searchtypes => \@searchtypes,
14718:                              }
14719:             );
14720:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
14721:                                              $dom);
14722:     if ($putresult eq 'ok') {
14723:         if (exists($currdirsrch{'available'})) {
14724:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
14725:                  $changes{'available'} = 1;
14726:              }
14727:         } else {
14728:             if ($env{'form.dirsrch_available'} eq '1') {
14729:                 $changes{'available'} = 1;
14730:             }
14731:         }
14732:         if (exists($currdirsrch{'lcavailable'})) {
14733:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
14734:                 $changes{'lcavailable'} = 1;
14735:             }
14736:         } else {
14737:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
14738:                 $changes{'lcavailable'} = 1;
14739:             }
14740:         }
14741:         if (exists($currdirsrch{'localonly'})) {
14742:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
14743:                 $changes{'localonly'} = 1;
14744:             }
14745:         } else {
14746:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
14747:                 $changes{'localonly'} = 1;
14748:             }
14749:         }
14750:         if (exists($currdirsrch{'lclocalonly'})) {
14751:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
14752:                 $changes{'lclocalonly'} = 1;
14753:             }
14754:         } else {
14755:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
14756:                 $changes{'lclocalonly'} = 1;
14757:             }
14758:         }
14759:         if (keys(%changes) > 0) {
14760:             $resulttext = &mt('Changes made:').'<ul>';
14761:             if ($changes{'available'}) {
14762:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
14763:             }
14764:             if ($changes{'lcavailable'}) {
14765:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
14766:             }
14767:             if ($changes{'localonly'}) {
14768:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
14769:             }
14770:             if ($changes{'lclocalonly'}) {
14771:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
14772:             }
14773:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
14774:                 my $chgtext;
14775:                 if (ref($usertypes) eq 'HASH') {
14776:                     if (keys(%{$usertypes}) > 0) {
14777:                         foreach my $type (@{$types}) {
14778:                             if (grep(/^\Q$type\E$/,@cansearch)) {
14779:                                 $chgtext .= $usertypes->{$type}.'; ';
14780:                             }
14781:                         }
14782:                         if (grep(/^default$/,@cansearch)) {
14783:                             $chgtext .= $othertitle;
14784:                         } else {
14785:                             $chgtext =~ s/\; $//;
14786:                         }
14787:                         $resulttext .=
14788:                             '<li>'.
14789:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
14790:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
14791:                             '</li>';
14792:                     }
14793:                 }
14794:             }
14795:             if (ref($changes{'searchby'}) eq 'ARRAY') {
14796:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
14797:                 my $chgtext;
14798:                 foreach my $type (@{$titleorder}) {
14799:                     if (grep(/^\Q$type\E$/,@searchby)) {
14800:                         if (defined($searchtitles->{$type})) {
14801:                             $chgtext .= $searchtitles->{$type}.'; ';
14802:                         }
14803:                     }
14804:                 }
14805:                 $chgtext =~ s/\; $//;
14806:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
14807:             }
14808:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
14809:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
14810:                 my $chgtext;
14811:                 foreach my $type (@{$srchtypeorder}) {
14812:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
14813:                         if (defined($srchtypes_desc->{$type})) {
14814:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
14815:                         }
14816:                     }
14817:                 }
14818:                 $chgtext =~ s/\; $//;
14819:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
14820:             }
14821:             $resulttext .= '</ul>';
14822:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
14823:             if (ref($lastactref) eq 'HASH') {
14824:                 $lastactref->{'directorysrch'} = 1;
14825:             }
14826:         } else {
14827:             $resulttext = &mt('No changes made to directory search settings');
14828:         }
14829:     } else {
14830:         $resulttext = '<span class="LC_error">'.
14831:                       &mt('An error occurred: [_1]',$putresult).'</span>';
14832:     }
14833:     return $resulttext;
14834: }
14835: 
14836: sub modify_contacts {
14837:     my ($dom,$lastactref,%domconfig) = @_;
14838:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
14839:     if (ref($domconfig{'contacts'}) eq 'HASH') {
14840:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
14841:             $currsetting{$key} = $domconfig{'contacts'}{$key};
14842:         }
14843:     }
14844:     my (%others,%to,%bcc,%includestr,%includeloc);
14845:     my @contacts = ('supportemail','adminemail');
14846:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
14847:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
14848:     my @toggles = ('reporterrors','reportupdates','reportstatus');
14849:     my @lonstatus = ('threshold','sysmail','weights','excluded');
14850:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
14851:     foreach my $type (@mailings) {
14852:         @{$newsetting{$type}} = 
14853:             &Apache::loncommon::get_env_multiple('form.'.$type);
14854:         foreach my $item (@contacts) {
14855:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
14856:                 $contacts_hash{contacts}{$type}{$item} = 1;
14857:             } else {
14858:                 $contacts_hash{contacts}{$type}{$item} = 0;
14859:             }
14860:         }
14861:         $others{$type} = $env{'form.'.$type.'_others'};
14862:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
14863:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
14864:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
14865:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
14866:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
14867:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
14868:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
14869:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
14870:             }
14871:         }
14872:     }
14873:     foreach my $item (@contacts) {
14874:         $to{$item} = $env{'form.'.$item};
14875:         $contacts_hash{'contacts'}{$item} = $to{$item};
14876:     }
14877:     foreach my $item (@toggles) {
14878:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
14879:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
14880:         }
14881:     }
14882:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
14883:     foreach my $item (@lonstatus) {
14884:         if ($item eq 'excluded') {
14885:             my (%serverhomes,@excluded);
14886:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
14887:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
14888:             if (@possexcluded) {
14889:                 foreach my $id (sort(@possexcluded)) {
14890:                     if ($serverhomes{$id}) {
14891:                         push(@excluded,$id);
14892:                     }
14893:                 }
14894:             }
14895:             if (@excluded) {
14896:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
14897:             }
14898:         } elsif ($item eq 'weights') {
14899:             foreach my $type ('E','W','N','U') {
14900:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
14901:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
14902:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
14903:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
14904:                             $env{'form.error'.$item.'_'.$type};
14905:                     }
14906:                 }
14907:             }
14908:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
14909:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
14910:             if ($env{'form.error'.$item} =~ /^\d+$/) {
14911:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
14912:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
14913:                 }
14914:             }
14915:         }
14916:     }
14917:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
14918:         foreach my $field (@{$fields}) {
14919:             if (ref($possoptions->{$field}) eq 'ARRAY') {
14920:                 my $value = $env{'form.helpform_'.$field};
14921:                 $value =~ s/^\s+|\s+$//g;
14922:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
14923:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
14924:                     if ($field eq 'screenshot') {
14925:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
14926:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
14927:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
14928:                         }
14929:                     }
14930:                 }
14931:             }
14932:         }
14933:     }
14934:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14935:     my (@statuses,%usertypeshash,@overrides);
14936:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
14937:         @statuses = @{$types};
14938:         if (ref($usertypes) eq 'HASH') {
14939:             %usertypeshash = %{$usertypes};
14940:         }
14941:     }
14942:     if (@statuses) {
14943:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
14944:         foreach my $type (@possoverrides) {
14945:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
14946:                 push(@overrides,$type);
14947:             }
14948:         }
14949:         if (@overrides) {
14950:             foreach my $type (@overrides) {
14951:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
14952:                 foreach my $item (@contacts) {
14953:                     if (grep(/^\Q$item\E$/,@standard)) {
14954:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
14955:                         $newsetting{'override_'.$type}{$item} = 1;
14956:                     } else {
14957:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
14958:                         $newsetting{'override_'.$type}{$item} = 0;
14959:                     }
14960:                 }
14961:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
14962:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
14963:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
14964:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
14965:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
14966:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
14967:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
14968:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
14969:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
14970:                 }
14971:             }
14972:         }
14973:     }
14974:     if (keys(%currsetting) > 0) {
14975:         foreach my $item (@contacts) {
14976:             if ($to{$item} ne $currsetting{$item}) {
14977:                 $changes{$item} = 1;
14978:             }
14979:         }
14980:         foreach my $type (@mailings) {
14981:             foreach my $item (@contacts) {
14982:                 if (ref($currsetting{$type}) eq 'HASH') {
14983:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
14984:                         push(@{$changes{$type}},$item);
14985:                     }
14986:                 } else {
14987:                     push(@{$changes{$type}},@{$newsetting{$type}});
14988:                 }
14989:             }
14990:             if ($others{$type} ne $currsetting{$type}{'others'}) {
14991:                 push(@{$changes{$type}},'others');
14992:             }
14993:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
14994:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
14995:                     push(@{$changes{$type}},'bcc'); 
14996:                 }
14997:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
14998:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
14999:                     push(@{$changes{$type}},'include');
15000:                 }
15001:             }
15002:         }
15003:         if (ref($fields) eq 'ARRAY') {
15004:             if (ref($currsetting{'helpform'}) eq 'HASH') {
15005:                 foreach my $field (@{$fields}) {
15006:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
15007:                         push(@{$changes{'helpform'}},$field);
15008:                     }
15009:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15010:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
15011:                             push(@{$changes{'helpform'}},'maxsize');
15012:                         }
15013:                     }
15014:                 }
15015:             } else {
15016:                 foreach my $field (@{$fields}) {
15017:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
15018:                         push(@{$changes{'helpform'}},$field);
15019:                     }
15020:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15021:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
15022:                             push(@{$changes{'helpform'}},'maxsize');
15023:                         }
15024:                     }
15025:                 }
15026:             }
15027:         }
15028:         if (@statuses) {
15029:             if (ref($currsetting{'overrides'}) eq 'HASH') {
15030:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
15031:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
15032:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
15033:                             foreach my $item (@contacts,'bcc','others','include') {
15034:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
15035:                                     push(@{$changes{'overrides'}},$key);
15036:                                     last;
15037:                                 }
15038:                             }
15039:                         } else {
15040:                             push(@{$changes{'overrides'}},$key);
15041:                         }
15042:                     }
15043:                 }
15044:                 foreach my $key (@overrides) {
15045:                     unless (exists($currsetting{'overrides'}{$key})) {
15046:                         push(@{$changes{'overrides'}},$key);
15047:                     }
15048:                 }
15049:             } else {
15050:                 foreach my $key (@overrides) {
15051:                     push(@{$changes{'overrides'}},$key);
15052:                 }
15053:             }
15054:         }
15055:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
15056:             foreach my $key ('excluded','weights','threshold','sysmail') {
15057:                 if ($key eq 'excluded') {
15058:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
15059:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
15060:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
15061:                             (@{$currsetting{'lonstatus'}{$key}})) {
15062:                             my @diffs =
15063:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
15064:                                                                    $currsetting{'lonstatus'}{$key});
15065:                             if (@diffs) {
15066:                                 push(@{$changes{'lonstatus'}},$key);
15067:                             }
15068:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
15069:                             push(@{$changes{'lonstatus'}},$key);
15070:                         }
15071:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
15072:                              (@{$currsetting{'lonstatus'}{$key}})) {
15073:                         push(@{$changes{'lonstatus'}},$key);
15074:                     }
15075:                 } elsif ($key eq 'weights') {
15076:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
15077:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
15078:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
15079:                             foreach my $type ('E','W','N','U') {
15080:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
15081:                                         $currsetting{'lonstatus'}{$key}{$type}) {
15082:                                     push(@{$changes{'lonstatus'}},$key);
15083:                                     last;
15084:                                 }
15085:                             }
15086:                         } else {
15087:                             foreach my $type ('E','W','N','U') {
15088:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
15089:                                     push(@{$changes{'lonstatus'}},$key);
15090:                                     last;
15091:                                 }
15092:                             }
15093:                         }
15094:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
15095:                         foreach my $type ('E','W','N','U') {
15096:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
15097:                                 push(@{$changes{'lonstatus'}},$key);
15098:                                 last;
15099:                             }
15100:                         }
15101:                     }
15102:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
15103:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15104:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
15105:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
15106:                                 push(@{$changes{'lonstatus'}},$key);
15107:                             }
15108:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
15109:                             push(@{$changes{'lonstatus'}},$key);
15110:                         }
15111:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
15112:                         push(@{$changes{'lonstatus'}},$key);
15113:                     }
15114:                 }
15115:             }
15116:         } else {
15117:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15118:                 foreach my $key ('excluded','weights','threshold','sysmail') {
15119:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
15120:                         push(@{$changes{'lonstatus'}},$key);
15121:                     }
15122:                 }
15123:             }
15124:         }
15125:     } else {
15126:         my %default;
15127:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
15128:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
15129:         $default{'errormail'} = 'adminemail';
15130:         $default{'packagesmail'} = 'adminemail';
15131:         $default{'helpdeskmail'} = 'supportemail';
15132:         $default{'otherdomsmail'} = 'supportemail';
15133:         $default{'lonstatusmail'} = 'adminemail';
15134:         $default{'requestsmail'} = 'adminemail';
15135:         $default{'updatesmail'} = 'adminemail';
15136:         $default{'hostipmail'} = 'adminemail';
15137:         foreach my $item (@contacts) {
15138:            if ($to{$item} ne $default{$item}) {
15139:                $changes{$item} = 1;
15140:            }
15141:         }
15142:         foreach my $type (@mailings) {
15143:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
15144:                 push(@{$changes{$type}},@{$newsetting{$type}});
15145:             }
15146:             if ($others{$type} ne '') {
15147:                 push(@{$changes{$type}},'others');
15148:             }
15149:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15150:                 if ($bcc{$type} ne '') {
15151:                     push(@{$changes{$type}},'bcc');
15152:                 }
15153:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
15154:                     push(@{$changes{$type}},'include');
15155:                 }
15156:             }
15157:         }
15158:         if (ref($fields) eq 'ARRAY') {
15159:             foreach my $field (@{$fields}) {
15160:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
15161:                     push(@{$changes{'helpform'}},$field);
15162:                 }
15163:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15164:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
15165:                         push(@{$changes{'helpform'}},'maxsize');
15166:                     }
15167:                 }
15168:             }
15169:         }
15170:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15171:             foreach my $key ('excluded','weights','threshold','sysmail') {
15172:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
15173:                     push(@{$changes{'lonstatus'}},$key);
15174:                 }
15175:             }
15176:         }
15177:     }
15178:     foreach my $item (@toggles) {
15179:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
15180:             $changes{$item} = 1;
15181:         } elsif ((!$env{'form.'.$item}) &&
15182:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
15183:             $changes{$item} = 1;
15184:         }
15185:     }
15186:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
15187:                                              $dom);
15188:     if ($putresult eq 'ok') {
15189:         if (keys(%changes) > 0) {
15190:             &Apache::loncommon::devalidate_domconfig_cache($dom);
15191:             if (ref($lastactref) eq 'HASH') {
15192:                 $lastactref->{'domainconfig'} = 1;
15193:             }
15194:             my ($titles,$short_titles)  = &contact_titles();
15195:             $resulttext = &mt('Changes made:').'<ul>';
15196:             foreach my $item (@contacts) {
15197:                 if ($changes{$item}) {
15198:                     $resulttext .= '<li>'.$titles->{$item}.
15199:                                     &mt(' set to: ').
15200:                                     '<span class="LC_cusr_emph">'.
15201:                                     $to{$item}.'</span></li>';
15202:                 }
15203:             }
15204:             foreach my $type (@mailings) {
15205:                 if (ref($changes{$type}) eq 'ARRAY') {
15206:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15207:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
15208:                     } else {
15209:                         $resulttext .= '<li>'.$titles->{$type}.': ';
15210:                     }
15211:                     my @text;
15212:                     foreach my $item (@{$newsetting{$type}}) {
15213:                         push(@text,$short_titles->{$item});
15214:                     }
15215:                     if ($others{$type} ne '') {
15216:                         push(@text,$others{$type});
15217:                     }
15218:                     if (@text) {
15219:                         $resulttext .= '<span class="LC_cusr_emph">'.
15220:                                        join(', ',@text).'</span>';
15221:                     }
15222:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15223:                         if ($bcc{$type} ne '') {
15224:                             my $bcctext;
15225:                             if (@text) {
15226:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
15227:                             } else {
15228:                                 $bcctext = '(Bcc)';
15229:                             }
15230:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
15231:                         } elsif (!@text) {
15232:                             $resulttext .= &mt('No one');
15233:                         }
15234:                         if ($includestr{$type} ne '') {
15235:                             if ($includeloc{$type} eq 'b') {
15236:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
15237:                             } elsif ($includeloc{$type} eq 's') {
15238:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
15239:                             }
15240:                         }
15241:                     } elsif (!@text) {
15242:                         $resulttext .= &mt('No recipients');
15243:                     }
15244:                     $resulttext .= '</li>';
15245:                 }
15246:             }
15247:             if (ref($changes{'overrides'}) eq 'ARRAY') {
15248:                 my @deletions;
15249:                 foreach my $type (@{$changes{'overrides'}}) {
15250:                     if ($usertypeshash{$type}) {
15251:                         if (grep(/^\Q$type\E/,@overrides)) {
15252:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
15253:                                                       $usertypeshash{$type}).'<ul><li>';
15254:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
15255:                                 my @text;
15256:                                 foreach my $item (@contacts) {
15257:                                     if ($newsetting{'override_'.$type}{$item}) {
15258:                                         push(@text,$short_titles->{$item});
15259:                                     }
15260:                                 }
15261:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
15262:                                     push(@text,$newsetting{'override_'.$type}{'others'});
15263:                                 }
15264: 
15265:                                 if (@text) {
15266:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
15267:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
15268:                                 }
15269:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
15270:                                     my $bcctext;
15271:                                     if (@text) {
15272:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
15273:                                     } else {
15274:                                         $bcctext = '(Bcc)';
15275:                                     }
15276:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
15277:                                 } elsif (!@text) {
15278:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
15279:                                 }
15280:                                 $resulttext .= '</li>';
15281:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
15282:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
15283:                                     if ($loc eq 'b') {
15284:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
15285:                                     } elsif ($loc eq 's') {
15286:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
15287:                                     }
15288:                                 }
15289:                             }
15290:                             $resulttext .= '</li></ul></li>';
15291:                         } else {
15292:                             push(@deletions,$usertypeshash{$type});
15293:                         }
15294:                     }
15295:                 }
15296:                 if (@deletions) {
15297:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
15298:                                               join(', ',@deletions)).'</li>';
15299:                 }
15300:             }
15301:             my @offon = ('off','on');
15302:             my $corelink = &core_link_msu();
15303:             if ($changes{'reporterrors'}) {
15304:                 $resulttext .= '<li>'.
15305:                                &mt('E-mail error reports to [_1] set to "'.
15306:                                    $offon[$env{'form.reporterrors'}].'".',
15307:                                    $corelink).
15308:                                '</li>';
15309:             }
15310:             if ($changes{'reportupdates'}) {
15311:                 $resulttext .= '<li>'.
15312:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
15313:                                     $offon[$env{'form.reportupdates'}].'".',
15314:                                     $corelink).
15315:                                 '</li>';
15316:             }
15317:             if ($changes{'reportstatus'}) {
15318:                 $resulttext .= '<li>'.
15319:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
15320:                                     $offon[$env{'form.reportstatus'}].'".',
15321:                                     $corelink).
15322:                                 '</li>';
15323:             }
15324:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
15325:                 $resulttext .= '<li>'.
15326:                                &mt('Nightly status check e-mail settings').':<ul>';
15327:                 my (%defval,%use_def,%shown);
15328:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
15329:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
15330:                 $defval{'weights'} =
15331:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
15332:                 $defval{'excluded'} = &mt('None');
15333:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
15334:                     foreach my $item ('threshold','sysmail','weights','excluded') {
15335:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
15336:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
15337:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
15338:                             } elsif ($item eq 'weights') {
15339:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
15340:                                     foreach my $type ('E','W','N','U') {
15341:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
15342:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
15343:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
15344:                                         } else {
15345:                                             $shown{$item} .= $lonstatus_defs->{$type};
15346:                                         }
15347:                                         $shown{$item} .= ', ';
15348:                                     }
15349:                                     $shown{$item} =~ s/, $//;
15350:                                 } else {
15351:                                     $shown{$item} = $defval{$item};
15352:                                 }
15353:                             } elsif ($item eq 'excluded') {
15354:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
15355:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
15356:                                 } else {
15357:                                     $shown{$item} = $defval{$item};
15358:                                 }
15359:                             }
15360:                         } else {
15361:                             $shown{$item} = $defval{$item};
15362:                         }
15363:                     }
15364:                 } else {
15365:                     foreach my $item ('threshold','weights','excluded','sysmail') {
15366:                         $shown{$item} = $defval{$item};
15367:                     }
15368:                 }
15369:                 foreach my $item ('threshold','weights','excluded','sysmail') {
15370:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
15371:                                           $shown{$item}).'</li>';
15372:                 }
15373:                 $resulttext .= '</ul></li>';
15374:             }
15375:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
15376:                 my (@optional,@required,@unused,$maxsizechg);
15377:                 foreach my $field (@{$changes{'helpform'}}) {
15378:                     if ($field eq 'maxsize') {
15379:                         $maxsizechg = 1;
15380:                         next;
15381:                     }
15382:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
15383:                         push(@optional,$field);
15384:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
15385:                         push(@unused,$field);
15386:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
15387:                         push(@required,$field);
15388:                     }
15389:                 }
15390:                 if (@optional) {
15391:                     $resulttext .= '<li>'.
15392:                                    &mt('Help form fields changed to "Optional": [_1].',
15393:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
15394:                                    '</li>';
15395:                 }
15396:                 if (@required) {
15397:                     $resulttext .= '<li>'.
15398:                                    &mt('Help form fields changed to "Required": [_1].',
15399:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
15400:                                    '</li>';
15401:                 }
15402:                 if (@unused) {
15403:                     $resulttext .= '<li>'.
15404:                                    &mt('Help form fields changed to "Not shown": [_1].',
15405:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
15406:                                    '</li>';
15407:                 }
15408:                 if ($maxsizechg) {
15409:                     $resulttext .= '<li>'.
15410:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
15411:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
15412:                                    '</li>';
15413:                 }
15414:             }
15415:             $resulttext .= '</ul>';
15416:         } else {
15417:             $resulttext = &mt('No changes made to contacts and form settings');
15418:         }
15419:     } else {
15420:         $resulttext = '<span class="LC_error">'.
15421:             &mt('An error occurred: [_1]',$putresult).'</span>';
15422:     }
15423:     return $resulttext;
15424: }
15425: 
15426: sub modify_passwords {
15427:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
15428:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
15429:         $updatedefaults,$updateconf);
15430:     my $customfn = 'resetpw.html';
15431:     if (ref($domconfig{'passwords'}) eq 'HASH') {
15432:         %current = %{$domconfig{'passwords'}};
15433:     }
15434:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15435:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15436:     if (ref($types) eq 'ARRAY') {
15437:         @oktypes = @{$types};
15438:     }
15439:     push(@oktypes,'default');
15440: 
15441:     my %titles = &Apache::lonlocal::texthash (
15442:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
15443:         intauth_check  => 'Check bcrypt cost if authenticated',
15444:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
15445:         permanent      => 'Permanent e-mail address',
15446:         critical       => 'Critical notification address',
15447:         notify         => 'Notification address',
15448:         min            => 'Minimum password length',
15449:         max            => 'Maximum password length',
15450:         chars          => 'Required characters',
15451:         numsaved       => 'Number of previous passwords to save',
15452:         reset          => 'Resetting Forgotten Password',
15453:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
15454:         rules          => 'Rules for LON-CAPA Passwords',
15455:         crsownerchg    => 'Course Owner Changing Student Passwords',
15456:         username       => 'Username',
15457:         email          => 'E-mail address',
15458:     );
15459: 
15460: #
15461: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
15462: #
15463:     my (%curr_defaults,%save_defaults);
15464:     if (ref($domconfig{'defaults'}) eq 'HASH') {
15465:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
15466:             if ($key =~ /^intauth_(cost|check|switch)$/) {
15467:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
15468:             } else {
15469:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
15470:             }
15471:         }
15472:     }
15473:     my %staticdefaults = (
15474:         'resetlink'      => 2,
15475:         'resetcase'      => \@oktypes,
15476:         'resetprelink'   => 'both',
15477:         'resetemail'     => ['critical','notify','permanent'],
15478:         'intauth_cost'   => 10,
15479:         'intauth_check'  => 0,
15480:         'intauth_switch' => 0,
15481:     );
15482:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
15483:     foreach my $type (@oktypes) {
15484:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
15485:     }
15486:     my $linklife = $env{'form.passwords_link'};
15487:     $linklife =~ s/^\s+|\s+$//g;
15488:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
15489:         $newvalues{'resetlink'} = $linklife;
15490:         if ($current{'resetlink'}) {
15491:             if ($current{'resetlink'} ne $linklife) {
15492:                 $changes{'reset'} = 1;
15493:             }
15494:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
15495:             if ($staticdefaults{'resetlink'} ne $linklife) {
15496:                 $changes{'reset'} = 1;
15497:             }
15498:         }
15499:     } elsif ($current{'resetlink'}) {
15500:         $changes{'reset'} = 1;
15501:     }
15502:     my @casesens;
15503:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
15504:     foreach my $case (sort(@posscase)) {
15505:         if (grep(/^\Q$case\E$/,@oktypes)) {
15506:             push(@casesens,$case);
15507:         }
15508:     }
15509:     $newvalues{'resetcase'} = \@casesens;
15510:     if (ref($current{'resetcase'}) eq 'ARRAY') {
15511:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
15512:         if (@diffs > 0) {
15513:             $changes{'reset'} = 1;
15514:         }
15515:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
15516:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
15517:         if (@diffs > 0) {
15518:             $changes{'reset'} = 1;
15519:         }
15520:     }
15521:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
15522:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
15523:         if (exists($current{'resetprelink'})) {
15524:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
15525:                 $changes{'reset'} = 1;
15526:             }
15527:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
15528:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
15529:                 $changes{'reset'} = 1;
15530:             }
15531:         }
15532:     } elsif ($current{'resetprelink'}) {
15533:         $changes{'reset'} = 1;
15534:     }
15535:     foreach my $type (@oktypes) {
15536:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
15537:         my @postlink;
15538:         foreach my $item (sort(@possplink)) {
15539:             if ($item =~ /^(email|username)$/) {
15540:                 push(@postlink,$item);
15541:             }
15542:         }
15543:         $newvalues{'resetpostlink'}{$type} = \@postlink;
15544:         unless ($changes{'reset'}) {
15545:             if (ref($current{'resetpostlink'}) eq 'HASH') {
15546:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
15547:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
15548:                     if (@diffs > 0) {
15549:                         $changes{'reset'} = 1;
15550:                     }
15551:                 } else {
15552:                     $changes{'reset'} = 1;
15553:                 }
15554:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
15555:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
15556:                 if (@diffs > 0) {
15557:                     $changes{'reset'} = 1;
15558:                 }
15559:             }
15560:         }
15561:     }
15562:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
15563:     my @resetemail;
15564:     foreach my $item (sort(@possemailsrc)) {
15565:         if ($item =~ /^(permanent|critical|notify)$/) {
15566:             push(@resetemail,$item);
15567:         }
15568:     }
15569:     $newvalues{'resetemail'} = \@resetemail;
15570:     unless ($changes{'reset'}) {
15571:         if (ref($current{'resetemail'}) eq 'ARRAY') {
15572:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
15573:             if (@diffs > 0) {
15574:                 $changes{'reset'} = 1;
15575:             }
15576:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
15577:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
15578:             if (@diffs > 0) {
15579:                 $changes{'reset'} = 1;
15580:             }
15581:         }
15582:     }
15583:     if ($env{'form.passwords_stdtext'} == 0) {
15584:         $newvalues{'resetremove'} = 1;
15585:         unless ($current{'resetremove'}) {
15586:             $changes{'reset'} = 1;
15587:         }
15588:     } elsif ($current{'resetremove'}) {
15589:         $changes{'reset'} = 1;
15590:     }
15591:     if ($env{'form.passwords_customfile.filename'} ne '') {
15592:         my $servadm = $r->dir_config('lonAdmEMail');
15593:         my ($configuserok,$author_ok,$switchserver) =
15594:             &config_check($dom,$confname,$servadm);
15595:         my $error;
15596:         if ($configuserok eq 'ok') {
15597:             if ($switchserver) {
15598:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
15599:             } else {
15600:                 if ($author_ok eq 'ok') {
15601:                     my $modified = [];
15602:                     my ($result,$customurl) =
15603:                         &Apache::lonconfigsettings::publishlogo($r,'upload','passwords_customfile',$dom,
15604:                                                                 $confname,'customtext/resetpw','','',$customfn,
15605:                                                                 $modified);
15606:                     if ($result eq 'ok') {
15607:                         $newvalues{'resetcustom'} = $customurl;
15608:                         $changes{'reset'} = 1;
15609:                         &update_modify_urls($r,$modified);
15610:                     } else {
15611:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
15612:                     }
15613:                 } else {
15614:                     $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);
15615:                 }
15616:             }
15617:         } else {
15618:             $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);
15619:         }
15620:         if ($error) {
15621:             &Apache::lonnet::logthis($error);
15622:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15623:         }
15624:     } elsif ($current{'resetcustom'}) {
15625:         if ($env{'form.passwords_custom_del'}) {
15626:             $changes{'reset'} = 1;
15627:         } else {
15628:             $newvalues{'resetcustom'} = $current{'resetcustom'};
15629:         }
15630:     }
15631:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
15632:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
15633:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
15634:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
15635:             $changes{'intauth'} = 1;
15636:         }
15637:     } else {
15638:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
15639:     }
15640:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
15641:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
15642:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
15643:             $changes{'intauth'} = 1;
15644:         }
15645:     } else {
15646:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
15647:     }
15648:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
15649:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
15650:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
15651:             $changes{'intauth'} = 1;
15652:         }
15653:     } else {
15654:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
15655:     }
15656:     foreach my $item ('cost','check','switch') {
15657:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
15658:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
15659:             $updatedefaults = 1;
15660:         }
15661:     }
15662:     &password_rule_changes('passwords',\%newvalues,\%current,\%changes);
15663:     my %crsownerchg = (
15664:                         by => [],
15665:                         for => [],
15666:                       );
15667:     foreach my $item ('by','for') {
15668:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
15669:         foreach my $type (sort(@posstypes)) {
15670:             if (grep(/^\Q$type\E$/,@oktypes)) {
15671:                 push(@{$crsownerchg{$item}},$type);
15672:             }
15673:         }
15674:     }
15675:     $newvalues{'crsownerchg'} = \%crsownerchg;
15676:     if (ref($current{'crsownerchg'}) eq 'HASH') {
15677:         foreach my $item ('by','for') {
15678:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
15679:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
15680:                 if (@diffs > 0) {
15681:                     $changes{'crsownerchg'} = 1;
15682:                     last;
15683:                 }
15684:             }
15685:         }
15686:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
15687:         foreach my $item ('by','for') {
15688:             if (@{$crsownerchg{$item}} > 0) {
15689:                 $changes{'crsownerchg'} = 1;
15690:                 last;
15691:             }
15692:         }
15693:     }
15694: 
15695:     my %confighash = (
15696:                         defaults  => \%save_defaults,
15697:                         passwords => \%newvalues,
15698:                      );
15699:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
15700: 
15701:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
15702:     if ($putresult eq 'ok') {
15703:         if (keys(%changes) > 0) {
15704:             $resulttext = &mt('Changes made: ').'<ul>';
15705:             foreach my $key ('reset','intauth','rules','crsownerchg') {
15706:                 if ($changes{$key}) {
15707:                     unless ($key eq 'intauth') {
15708:                         $updateconf = 1;
15709:                     }
15710:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
15711:                     if ($key eq 'reset') {
15712:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
15713:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
15714:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
15715:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
15716:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
15717:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
15718:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
15719:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
15720:                             }
15721:                         } else {
15722:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
15723:                         }
15724:                         if ($confighash{'passwords'}{'resetlink'}) {
15725:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
15726:                         } else {
15727:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
15728:                                                   &mt('Will default to 2 hours').'</li>';
15729:                         }
15730:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
15731:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
15732:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
15733:                             } else {
15734:                                 my $casesens;
15735:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
15736:                                     if ($type eq 'default') {
15737:                                         $casesens .= $othertitle.', ';
15738:                                     } elsif ($usertypes->{$type} ne '') {
15739:                                         $casesens .= $usertypes->{$type}.', ';
15740:                                     }
15741:                                 }
15742:                                 $casesens =~ s/\Q, \E$//;
15743:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
15744:                             }
15745:                         } else {
15746:                             $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>';
15747:                         }
15748:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
15749:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
15750:                         } else {
15751:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
15752:                         }
15753:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
15754:                             my $output;
15755:                             if (ref($types) eq 'ARRAY') {
15756:                                 foreach my $type (@{$types}) {
15757:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
15758:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
15759:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
15760:                                         } else {
15761:                                             $output .= $usertypes->{$type}.' -- '.
15762:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
15763:                                         }
15764:                                     }
15765:                                 }
15766:                             }
15767:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
15768:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
15769:                                     $output .= $othertitle.' -- '.&mt('none');
15770:                                 } else {
15771:                                     $output .= $othertitle.' -- '.
15772:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
15773:                                 }
15774:                             }
15775:                             if ($output) {
15776:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
15777:                             } else {
15778:                                 $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>';
15779:                             }
15780:                         } else {
15781:                             $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>';
15782:                         }
15783:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
15784:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
15785:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
15786:                             } else {
15787:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
15788:                             }
15789:                         } else {
15790:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
15791:                         }
15792:                         if ($confighash{'passwords'}{'resetremove'}) {
15793:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
15794:                         } else {
15795:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
15796:                         }
15797:                         if ($confighash{'passwords'}{'resetcustom'}) {
15798:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
15799:                                                                             &mt('custom text'),600,500,undef,undef,
15800:                                                                             undef,undef,'background-color:#ffffff');
15801:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
15802:                         } else {
15803:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
15804:                         }
15805:                     } elsif ($key eq 'intauth') {
15806:                         foreach my $item ('cost','switch','check') {
15807:                             my $value = $save_defaults{$key.'_'.$item};
15808:                             if ($item eq 'switch') {
15809:                                 my %optiondesc = &Apache::lonlocal::texthash (
15810:                                                      0 => 'No',
15811:                                                      1 => 'Yes',
15812:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
15813:                                                  );
15814:                                 if ($value =~ /^(0|1|2)$/) {
15815:                                     $value = $optiondesc{$value};
15816:                                 } else {
15817:                                     $value = &mt('none -- defaults to No');
15818:                                 }
15819:                             } elsif ($item eq 'check') {
15820:                                 my %optiondesc = &Apache::lonlocal::texthash (
15821:                                                      0 => 'No',
15822:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
15823:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
15824:                                                  );
15825:                                 if ($value =~ /^(0|1|2)$/) {
15826:                                     $value = $optiondesc{$value};
15827:                                 } else {
15828:                                     $value = &mt('none -- defaults to No');
15829:                                 }
15830:                             }
15831:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
15832:                         }
15833:                     } elsif ($key eq 'rules') {
15834:                         foreach my $rule ('min','max','numsaved') {
15835:                             if ($confighash{'passwords'}{$rule} eq '') {
15836:                                 if ($rule eq 'min') {
15837:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
15838:                                                    ' '.&mt('Default of [_1] will be used',
15839:                                                            $Apache::lonnet::passwdmin).'</li>';
15840:                                 } else {
15841:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
15842:                                 }
15843:                             } else {
15844:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
15845:                             }
15846:                         }
15847:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
15848:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
15849:                                 my %rulenames = &Apache::lonlocal::texthash(
15850:                                                      uc => 'At least one upper case letter',
15851:                                                      lc => 'At least one lower case letter',
15852:                                                      num => 'At least one number',
15853:                                                      spec => 'At least one non-alphanumeric',
15854:                                                    );
15855:                                 my $needed = '<ul><li>'.
15856:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
15857:                                              '</li></ul>';
15858:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
15859:                             } else {
15860:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
15861:                             }
15862:                         } else {
15863:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
15864:                         }
15865:                     } elsif ($key eq 'crsownerchg') {
15866:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
15867:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
15868:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
15869:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
15870:                             } else {
15871:                                 my %crsownerstr;
15872:                                 foreach my $item ('by','for') {
15873:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
15874:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
15875:                                             if ($type eq 'default') {
15876:                                                 $crsownerstr{$item} .= $othertitle.', ';
15877:                                             } elsif ($usertypes->{$type} ne '') {
15878:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
15879:                                             }
15880:                                         }
15881:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
15882:                                     }
15883:                                 }
15884:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
15885:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
15886:                             }
15887:                         } else {
15888:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
15889:                         }
15890:                     }
15891:                     $resulttext .= '</ul></li>';
15892:                 }
15893:             }
15894:             $resulttext .= '</ul>';
15895:         } else {
15896:             $resulttext = &mt('No changes made to password settings');
15897:         }
15898:         my $cachetime = 24*60*60;
15899:         if ($updatedefaults) {
15900:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15901:             if (ref($lastactref) eq 'HASH') {
15902:                 $lastactref->{'domdefaults'} = 1;
15903:             }
15904:         }
15905:         if ($updateconf) {
15906:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
15907:             if (ref($lastactref) eq 'HASH') {
15908:                 $lastactref->{'passwdconf'} = 1;
15909:             }
15910:         }
15911:     } else {
15912:         $resulttext = '<span class="LC_error">'.
15913:             &mt('An error occurred: [_1]',$putresult).'</span>';
15914:     }
15915:     if ($errors) {
15916:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
15917:                        $errors.'</ul></p>';
15918:     }
15919:     return $resulttext;
15920: }
15921: 
15922: sub password_rule_changes {
15923:     my ($prefix,$newvalues,$current,$changes) = @_;
15924:     return unless ((ref($newvalues) eq 'HASH') &&
15925:                    (ref($current) eq 'HASH') &&
15926:                    (ref($changes) eq 'HASH'));
15927:     my (@rules,%staticdefaults);
15928:     if ($prefix eq 'passwords') {
15929:         @rules = ('min','max','numsaved');
15930:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
15931:         @rules = ('min','max');
15932:     }
15933:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
15934:     foreach my $rule (@rules) {
15935:         $env{'form.'.$prefix.'_'.$rule} =~ s/^\s+|\s+$//g;
15936:         my $ruleok;
15937:         if ($rule eq 'min') {
15938:             if ($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) {
15939:                 if ($env{'form.'.$prefix.'_'.$rule} >= $staticdefaults{$rule}) {
15940:                     $ruleok = 1;
15941:                 }
15942:             }
15943:         } elsif (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) &&
15944:                  ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
15945:             $ruleok = 1;
15946:         }
15947:         if ($ruleok) {
15948:             $newvalues->{$rule} = $env{'form.'.$prefix.'_'.$rule};
15949:             if (exists($current->{$rule})) {
15950:                 if ($newvalues->{$rule} ne $current->{$rule}) {
15951:                     $changes->{'rules'} = 1;
15952:                 }
15953:             } elsif ($rule eq 'min') {
15954:                 if ($staticdefaults{$rule} ne $newvalues->{$rule}) {
15955:                     $changes->{'rules'} = 1;
15956:                 }
15957:             } else {
15958:                 $changes->{'rules'} = 1;
15959:             }
15960:         } elsif (exists($current->{$rule})) {
15961:             $changes->{'rules'} = 1;
15962:         }
15963:     }
15964:     my @posschars = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_chars');
15965:     my @chars;
15966:     foreach my $item (sort(@posschars)) {
15967:         if ($item =~ /^(uc|lc|num|spec)$/) {
15968:             push(@chars,$item);
15969:         }
15970:     }
15971:     $newvalues->{'chars'} = \@chars;
15972:     unless ($changes->{'rules'}) {
15973:         if (ref($current->{'chars'}) eq 'ARRAY') {
15974:             my @diffs = &Apache::loncommon::compare_arrays($current->{'chars'},\@chars);
15975:             if (@diffs > 0) {
15976:                 $changes->{'rules'} = 1;
15977:             }
15978:         } else {
15979:             if (@chars > 0) {
15980:                 $changes->{'rules'} = 1;
15981:             }
15982:         }
15983:     }
15984:     return;
15985: }
15986: 
15987: sub modify_usercreation {
15988:     my ($dom,%domconfig) = @_;
15989:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
15990:     my $warningmsg;
15991:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
15992:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
15993:             if ($key eq 'cancreate') {
15994:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
15995:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
15996:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
15997:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
15998:                         } else {
15999:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16000:                         }
16001:                     }
16002:                 }
16003:             } elsif ($key eq 'email_rule') {
16004:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
16005:             } else {
16006:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
16007:             }
16008:         }
16009:     }
16010:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
16011:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
16012:     my @contexts = ('author','course','requestcrs');
16013:     foreach my $item(@contexts) {
16014:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
16015:     }
16016:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
16017:         foreach my $item (@contexts) {
16018:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
16019:                 push(@{$changes{'cancreate'}},$item);
16020:             }
16021:         }
16022:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
16023:         foreach my $item (@contexts) {
16024:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
16025:                 if ($cancreate{$item} ne 'any') {
16026:                     push(@{$changes{'cancreate'}},$item);
16027:                 }
16028:             } else {
16029:                 if ($cancreate{$item} ne 'none') {
16030:                     push(@{$changes{'cancreate'}},$item);
16031:                 }
16032:             }
16033:         }
16034:     } else {
16035:         foreach my $item (@contexts)  {
16036:             push(@{$changes{'cancreate'}},$item);
16037:         }
16038:     }
16039: 
16040:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
16041:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
16042:             if (!grep(/^\Q$type\E$/,@username_rule)) {
16043:                 push(@{$changes{'username_rule'}},$type);
16044:             }
16045:         }
16046:         foreach my $type (@username_rule) {
16047:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
16048:                 push(@{$changes{'username_rule'}},$type);
16049:             }
16050:         }
16051:     } else {
16052:         push(@{$changes{'username_rule'}},@username_rule);
16053:     }
16054: 
16055:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
16056:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
16057:             if (!grep(/^\Q$type\E$/,@id_rule)) {
16058:                 push(@{$changes{'id_rule'}},$type);
16059:             }
16060:         }
16061:         foreach my $type (@id_rule) {
16062:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
16063:                 push(@{$changes{'id_rule'}},$type);
16064:             }
16065:         }
16066:     } else {
16067:         push(@{$changes{'id_rule'}},@id_rule);
16068:     }
16069: 
16070:     my @authen_contexts = ('author','course','domain');
16071:     my @authtypes = ('int','krb4','krb5','loc','lti');
16072:     my %authhash;
16073:     foreach my $item (@authen_contexts) {
16074:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
16075:         foreach my $auth (@authtypes) {
16076:             if (grep(/^\Q$auth\E$/,@authallowed)) {
16077:                 $authhash{$item}{$auth} = 1;
16078:             } else {
16079:                 $authhash{$item}{$auth} = 0;
16080:             }
16081:         }
16082:     }
16083:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
16084:         foreach my $item (@authen_contexts) {
16085:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
16086:                 foreach my $auth (@authtypes) {
16087:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
16088:                         push(@{$changes{'authtypes'}},$item);
16089:                         last;
16090:                     }
16091:                 }
16092:             }
16093:         }
16094:     } else {
16095:         foreach my $item (@authen_contexts) {
16096:             push(@{$changes{'authtypes'}},$item);
16097:         }
16098:     }
16099: 
16100:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
16101:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
16102:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
16103:     $save_usercreate{'id_rule'} = \@id_rule;
16104:     $save_usercreate{'username_rule'} = \@username_rule,
16105:     $save_usercreate{'authtypes'} = \%authhash;
16106: 
16107:     my %usercreation_hash =  (
16108:         usercreation     => \%save_usercreate,
16109:     );
16110: 
16111:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
16112:                                              $dom);
16113: 
16114:     if ($putresult eq 'ok') {
16115:         if (keys(%changes) > 0) {
16116:             $resulttext = &mt('Changes made:').'<ul>';
16117:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
16118:                 my %lt = &usercreation_types();
16119:                 foreach my $type (@{$changes{'cancreate'}}) {
16120:                     my $chgtext = $lt{$type}.', ';
16121:                     if ($cancreate{$type} eq 'none') {
16122:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
16123:                     } elsif ($cancreate{$type} eq 'any') {
16124:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
16125:                     } elsif ($cancreate{$type} eq 'official') {
16126:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
16127:                     } elsif ($cancreate{$type} eq 'unofficial') {
16128:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
16129:                     }
16130:                     $resulttext .= '<li>'.$chgtext.'</li>';
16131:                 }
16132:             }
16133:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
16134:                 my ($rules,$ruleorder) = 
16135:                     &Apache::lonnet::inst_userrules($dom,'username');
16136:                 my $chgtext = '<ul>';
16137:                 foreach my $type (@username_rule) {
16138:                     if (ref($rules->{$type}) eq 'HASH') {
16139:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
16140:                     }
16141:                 }
16142:                 $chgtext .= '</ul>';
16143:                 if (@username_rule > 0) {
16144:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
16145:                 } else {
16146:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
16147:                 }
16148:             }
16149:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
16150:                 my ($idrules,$idruleorder) = 
16151:                     &Apache::lonnet::inst_userrules($dom,'id');
16152:                 my $chgtext = '<ul>';
16153:                 foreach my $type (@id_rule) {
16154:                     if (ref($idrules->{$type}) eq 'HASH') {
16155:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
16156:                     }
16157:                 }
16158:                 $chgtext .= '</ul>';
16159:                 if (@id_rule > 0) {
16160:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
16161:                 } else {
16162:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
16163:                 }
16164:             }
16165:             my %authname = &authtype_names();
16166:             my %context_title = &context_names();
16167:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
16168:                 my $chgtext = '<ul>';
16169:                 foreach my $type (@{$changes{'authtypes'}}) {
16170:                     my @allowed;
16171:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
16172:                     foreach my $auth (@authtypes) {
16173:                         if ($authhash{$type}{$auth}) {
16174:                             push(@allowed,$authname{$auth});
16175:                         }
16176:                     }
16177:                     if (@allowed > 0) {
16178:                         $chgtext .= join(', ',@allowed).'</li>';
16179:                     } else {
16180:                         $chgtext .= &mt('none').'</li>';
16181:                     }
16182:                 }
16183:                 $chgtext .= '</ul>';
16184:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
16185:                 $resulttext .= '</li>';
16186:             }
16187:             $resulttext .= '</ul>';
16188:         } else {
16189:             $resulttext = &mt('No changes made to user creation settings');
16190:         }
16191:     } else {
16192:         $resulttext = '<span class="LC_error">'.
16193:             &mt('An error occurred: [_1]',$putresult).'</span>';
16194:     }
16195:     if ($warningmsg ne '') {
16196:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
16197:     }
16198:     return $resulttext;
16199: }
16200: 
16201: sub modify_selfcreation {
16202:     my ($dom,$lastactref,%domconfig) = @_;
16203:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
16204:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
16205:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16206:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
16207:     if (ref($typesref) eq 'ARRAY') {
16208:         @types = @{$typesref};
16209:     }
16210:     if (ref($usertypesref) eq 'HASH') {
16211:         %usertypes = %{$usertypesref};
16212:     }
16213:     $usertypes{'default'} = $othertitle;
16214: #
16215: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
16216: #
16217:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
16218:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
16219:             if ($key eq 'cancreate') {
16220:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
16221:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
16222:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
16223:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
16224:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
16225:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
16226:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
16227:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
16228:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16229:                         } else {
16230:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16231:                         }
16232:                     }
16233:                 }
16234:             } elsif ($key eq 'email_rule') {
16235:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
16236:             } else {
16237:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
16238:             }
16239:         }
16240:     }
16241: #
16242: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
16243: #
16244:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
16245:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
16246:             if ($key eq 'selfcreate') {
16247:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
16248:             } else {
16249:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
16250:             }
16251:         }
16252:     }
16253: #
16254: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
16255: #
16256:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
16257:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
16258:             if ($key eq 'inststatusguest') {
16259:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
16260:             } else {
16261:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
16262:             }
16263:         }
16264:     }
16265: 
16266:     my @contexts = ('selfcreate');
16267:     @{$cancreate{'selfcreate'}} = ();
16268:     %{$cancreate{'emailusername'}} = ();
16269:     if (@types) {
16270:         @{$cancreate{'statustocreate'}} = ();
16271:     }
16272:     %{$cancreate{'selfcreateprocessing'}} = ();
16273:     %{$cancreate{'shibenv'}} = ();
16274:     %{$cancreate{'emailverified'}} = ();
16275:     %{$cancreate{'emailoptions'}} = ();
16276:     %{$cancreate{'emaildomain'}} = ();
16277:     my %selfcreatetypes = (
16278:                              sso   => 'users authenticated by institutional single sign on',
16279:                              login => 'users authenticated by institutional log-in',
16280:                              email => 'users verified by e-mail',
16281:                           );
16282: #
16283: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
16284: # is permitted.
16285: #
16286:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
16287: 
16288:     my (@statuses,%email_rule);
16289:     foreach my $item ('login','sso','email') {
16290:         if ($item eq 'email') {
16291:             if ($env{'form.cancreate_email'}) {
16292:                 if (@types) {
16293:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
16294:                     foreach my $status (@poss_statuses) {
16295:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
16296:                             push(@statuses,$status);
16297:                         }
16298:                     }
16299:                     $save_inststatus{'inststatusguest'} = \@statuses;
16300:                 } else {
16301:                     push(@statuses,'default');
16302:                 }
16303:                 if (@statuses) {
16304:                     my %curr_rule;
16305:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
16306:                         foreach my $type (@statuses) {
16307:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
16308:                         }
16309:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
16310:                         foreach my $type (@statuses) {
16311:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
16312:                         }
16313:                     }
16314:                     push(@{$cancreate{'selfcreate'}},'email');
16315:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
16316:                     my %curremaildom;
16317:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
16318:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
16319:                     }
16320:                     foreach my $type (@statuses) {
16321:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
16322:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
16323:                         }
16324:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
16325:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
16326:                         }
16327:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
16328: #
16329: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
16330: #
16331:                             my $chosen = $1;
16332:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
16333:                                 my $emaildom;
16334:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
16335:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
16336:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
16337:                                     if (ref($curremaildom{$type}) eq 'HASH') {
16338:                                         if (exists($curremaildom{$type}{$chosen})) {
16339:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
16340:                                                 push(@{$changes{'cancreate'}},'emaildomain');
16341:                                             }
16342:                                         } elsif ($emaildom ne '') {
16343:                                             push(@{$changes{'cancreate'}},'emaildomain');
16344:                                         }
16345:                                     } elsif ($emaildom ne '') {
16346:                                         push(@{$changes{'cancreate'}},'emaildomain');
16347:                                     }
16348:                                 }
16349:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
16350:                             } elsif ($chosen eq 'custom') {
16351:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
16352:                                 $email_rule{$type} = [];
16353:                                 if (ref($emailrules) eq 'HASH') {
16354:                                     foreach my $rule (@possemail_rules) {
16355:                                         if (exists($emailrules->{$rule})) {
16356:                                             push(@{$email_rule{$type}},$rule);
16357:                                         }
16358:                                     }
16359:                                 }
16360:                                 if (@{$email_rule{$type}}) {
16361:                                     $cancreate{'emailoptions'}{$type} = 'custom';
16362:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
16363:                                         if (@{$curr_rule{$type}} > 0) {
16364:                                             foreach my $rule (@{$curr_rule{$type}}) {
16365:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
16366:                                                     push(@{$changes{'email_rule'}},$type);
16367:                                                 }
16368:                                             }
16369:                                         }
16370:                                         foreach my $type (@{$email_rule{$type}}) {
16371:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
16372:                                                 push(@{$changes{'email_rule'}},$type);
16373:                                             }
16374:                                         }
16375:                                     } else {
16376:                                         push(@{$changes{'email_rule'}},$type);
16377:                                     }
16378:                                 }
16379:                             } else {
16380:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
16381:                             }
16382:                         }
16383:                     }
16384:                     if (@types) {
16385:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
16386:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
16387:                             if (@changed) {
16388:                                 push(@{$changes{'inststatus'}},'inststatusguest');
16389:                             }
16390:                         } else {
16391:                             push(@{$changes{'inststatus'}},'inststatusguest');
16392:                         }
16393:                     }
16394:                 } else {
16395:                     delete($env{'form.cancreate_email'});
16396:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
16397:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
16398:                             push(@{$changes{'inststatus'}},'inststatusguest');
16399:                         }
16400:                     }
16401:                 }
16402:             } else {
16403:                 $save_inststatus{'inststatusguest'} = [];
16404:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
16405:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
16406:                         push(@{$changes{'inststatus'}},'inststatusguest');
16407:                     }
16408:                 }
16409:             }
16410:         } else {
16411:             if ($env{'form.cancreate_'.$item}) {
16412:                 push(@{$cancreate{'selfcreate'}},$item);
16413:             }
16414:         }
16415:     }
16416:     my (%userinfo,%savecaptcha);
16417:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
16418: #
16419: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
16420: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
16421: #
16422: 
16423:     if ($env{'form.cancreate_email'}) {
16424:         push(@contexts,'emailusername');
16425:         if (@statuses) {
16426:             foreach my $type (@statuses) {
16427:                 if (ref($infofields) eq 'ARRAY') {
16428:                     foreach my $field (@{$infofields}) {
16429:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
16430:                             $cancreate{'emailusername'}{$type}{$field} = $1;
16431:                         }
16432:                     }
16433:                 }
16434:             }
16435:         }
16436: #
16437: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
16438: # queued requests for self-creation of account verified by e-mail.
16439: #
16440: 
16441:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
16442:         @approvalnotify = sort(@approvalnotify);
16443:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
16444:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
16445:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
16446:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
16447:                     push(@{$changes{'cancreate'}},'notify');
16448:                 }
16449:             } else {
16450:                 if ($cancreate{'notify'}{'approval'}) {
16451:                     push(@{$changes{'cancreate'}},'notify');
16452:                 }
16453:             }
16454:         } elsif ($cancreate{'notify'}{'approval'}) {
16455:             push(@{$changes{'cancreate'}},'notify');
16456:         }
16457: 
16458:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
16459:     }
16460: #  
16461: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
16462: # institutional log-in.
16463: #
16464:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
16465:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
16466:                ($domdefaults{'auth_def'} eq 'localauth'))) {
16467:             $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.').' '.
16468:                           &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.');
16469:         }
16470:     }
16471:     my @fields = ('lastname','firstname','middlename','generation',
16472:                   'permanentemail','id');
16473:     my @shibfields = (@fields,'inststatus');
16474:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
16475: #
16476: # Where usernames may created for institutional log-in and/or institutional single sign on:
16477: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
16478: # may self-create accounts 
16479: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
16480: # which the user may supply, if institutional data is unavailable.
16481: #
16482:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
16483:         if (@types) {
16484:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
16485:             push(@contexts,'statustocreate');
16486:             foreach my $type (@types) {
16487:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
16488:                 foreach my $field (@fields) {
16489:                     if (grep(/^\Q$field\E$/,@modifiable)) {
16490:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
16491:                     } else {
16492:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
16493:                     }
16494:                 }
16495:             }
16496:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
16497:                 foreach my $type (@types) {
16498:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
16499:                         foreach my $field (@fields) {
16500:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
16501:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
16502:                                 push(@{$changes{'selfcreate'}},$type);
16503:                                 last;
16504:                             }
16505:                         }
16506:                     }
16507:                 }
16508:             } else {
16509:                 foreach my $type (@types) {
16510:                     push(@{$changes{'selfcreate'}},$type);
16511:                 }
16512:             }
16513:         }
16514:         foreach my $field (@shibfields) {
16515:             if ($env{'form.shibenv_'.$field} ne '') {
16516:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
16517:             }
16518:         }
16519:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
16520:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
16521:                 foreach my $field (@shibfields) {
16522:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
16523:                         push(@{$changes{'cancreate'}},'shibenv');
16524:                     }
16525:                 }
16526:             } else {
16527:                 foreach my $field (@shibfields) {
16528:                     if ($env{'form.shibenv_'.$field}) {
16529:                         push(@{$changes{'cancreate'}},'shibenv');
16530:                         last;
16531:                     }
16532:                 }
16533:             }
16534:         }
16535:     }
16536:     foreach my $item (@contexts) {
16537:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
16538:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
16539:                 if (ref($cancreate{$item}) eq 'ARRAY') {
16540:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
16541:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16542:                             push(@{$changes{'cancreate'}},$item);
16543:                         }
16544:                     }
16545:                 }
16546:             }
16547:             if (ref($cancreate{$item}) eq 'ARRAY') {
16548:                 foreach my $type (@{$cancreate{$item}}) {
16549:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
16550:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16551:                             push(@{$changes{'cancreate'}},$item);
16552:                         }
16553:                     }
16554:                 }
16555:             }
16556:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
16557:             if (ref($cancreate{$item}) eq 'HASH') {
16558:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
16559:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
16560:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
16561:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
16562:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16563:                                     push(@{$changes{'cancreate'}},$item);
16564:                                 }
16565:                             }
16566:                         }
16567:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
16568:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
16569:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16570:                                 push(@{$changes{'cancreate'}},$item);
16571:                             }
16572:                         }
16573:                     }
16574:                 }
16575:                 foreach my $type (keys(%{$cancreate{$item}})) {
16576:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
16577:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
16578:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
16579:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
16580:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16581:                                         push(@{$changes{'cancreate'}},$item);
16582:                                     }
16583:                                 }
16584:                             } else {
16585:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16586:                                     push(@{$changes{'cancreate'}},$item);
16587:                                 }
16588:                             }
16589:                         }
16590:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
16591:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
16592:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16593:                                 push(@{$changes{'cancreate'}},$item);
16594:                             }
16595:                         }
16596:                     }
16597:                 }
16598:             }
16599:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
16600:             if (ref($cancreate{$item}) eq 'ARRAY') {
16601:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
16602:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16603:                         push(@{$changes{'cancreate'}},$item);
16604:                     }
16605:                 }
16606:             }
16607:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
16608:             if (ref($cancreate{$item}) eq 'HASH') {
16609:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16610:                     push(@{$changes{'cancreate'}},$item);
16611:                 }
16612:             }
16613:         } elsif ($item eq 'emailusername') {
16614:             if (ref($cancreate{$item}) eq 'HASH') {
16615:                 foreach my $type (keys(%{$cancreate{$item}})) {
16616:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
16617:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
16618:                             if ($cancreate{$item}{$type}{$field}) {
16619:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16620:                                     push(@{$changes{'cancreate'}},$item);
16621:                                 }
16622:                                 last;
16623:                             }
16624:                         }
16625:                     }
16626:                 }
16627:             }
16628:         }
16629:     }
16630: #
16631: # Populate %save_usercreate hash with updates to self-creation configuration.
16632: #
16633:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
16634:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
16635:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
16636:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
16637:     if (ref($cancreate{'notify'}) eq 'HASH') {
16638:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
16639:     }
16640:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
16641:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
16642:     }
16643:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
16644:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
16645:     }
16646:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
16647:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
16648:     }
16649:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
16650:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
16651:     }
16652:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
16653:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
16654:     }
16655:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
16656:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
16657:     }
16658:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
16659:     $save_usercreate{'email_rule'} = \%email_rule;
16660: 
16661:     my %userconfig_hash = (
16662:             usercreation     => \%save_usercreate,
16663:             usermodification => \%save_usermodify,
16664:             inststatus       => \%save_inststatus,
16665:     );
16666: 
16667:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
16668:                                              $dom);
16669: #
16670: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
16671: #
16672:     if ($putresult eq 'ok') {
16673:         if (keys(%changes) > 0) {
16674:             $resulttext = &mt('Changes made:').'<ul>';
16675:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
16676:                 my %lt = &selfcreation_types();
16677:                 foreach my $type (@{$changes{'cancreate'}}) {
16678:                     my $chgtext = '';
16679:                     if ($type eq 'selfcreate') {
16680:                         if (@{$cancreate{$type}} == 0) {
16681:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
16682:                         } else {
16683:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
16684:                                         '<ul>';
16685:                             foreach my $case (@{$cancreate{$type}}) {
16686:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
16687:                             }
16688:                             $chgtext .= '</ul>';
16689:                             if (ref($cancreate{$type}) eq 'ARRAY') {
16690:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
16691:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
16692:                                         if (@{$cancreate{'statustocreate'}} == 0) {
16693:                                             $chgtext .= '<span class="LC_warning">'.
16694:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
16695:                                                         '</span><br />';
16696:                                         }
16697:                                     }
16698:                                 }
16699:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
16700:                                     if (!@statuses) {
16701:                                         $chgtext .= '<span class="LC_warning">'.
16702:                                                     &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.").
16703:                                                     '</span><br />';
16704: 
16705:                                     }
16706:                                 }
16707:                             }
16708:                         }
16709:                     } elsif ($type eq 'shibenv') {
16710:                         if (keys(%{$cancreate{$type}}) == 0) {
16711:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
16712:                         } else {
16713:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
16714:                                         '<ul>';
16715:                             foreach my $field (@shibfields) {
16716:                                 next if ($cancreate{$type}{$field} eq '');
16717:                                 if ($field eq 'inststatus') {
16718:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
16719:                                 } else {
16720:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
16721:                                 }
16722:                             }
16723:                             $chgtext .= '</ul>';
16724:                         }
16725:                     } elsif ($type eq 'statustocreate') {
16726:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
16727:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
16728:                             if (@{$cancreate{'selfcreate'}} > 0) {
16729:                                 if (@{$cancreate{'statustocreate'}} == 0) {
16730:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
16731:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
16732:                                         $chgtext .= '<br />'.
16733:                                                     '<span class="LC_warning">'.
16734:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
16735:                                                     '</span>';
16736:                                     }
16737:                                 } elsif (keys(%usertypes) > 0) {
16738:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
16739:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
16740:                                     } else {
16741:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
16742:                                     }
16743:                                     $chgtext .= '<ul>';
16744:                                     foreach my $case (@{$cancreate{$type}}) {
16745:                                         if ($case eq 'default') {
16746:                                             $chgtext .= '<li>'.$othertitle.'</li>';
16747:                                         } else {
16748:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
16749:                                         }
16750:                                     }
16751:                                     $chgtext .= '</ul>';
16752:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
16753:                                         $chgtext .= '<span class="LC_warning">'.
16754:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
16755:                                                     '</span>';
16756:                                     }
16757:                                 }
16758:                             } else {
16759:                                 if (@{$cancreate{$type}} == 0) {
16760:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
16761:                                 } else {
16762:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
16763:                                 }
16764:                             }
16765:                             $chgtext .= '<br />';
16766:                         }
16767:                     } elsif ($type eq 'selfcreateprocessing') {
16768:                         my %choices = &Apache::lonlocal::texthash (
16769:                                                                     automatic => 'Automatic approval',
16770:                                                                     approval  => 'Queued for approval',
16771:                                                                   );
16772:                         if (@types) {
16773:                             if (@statuses) {
16774:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
16775:                                             '<ul>';
16776:                                 foreach my $status (@statuses) {
16777:                                     if ($status eq 'default') {
16778:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
16779:                                     } else {
16780:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
16781:                                     }
16782:                                 }
16783:                                 $chgtext .= '</ul>';
16784:                             }
16785:                         } else {
16786:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
16787:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
16788:                         }
16789:                     } elsif ($type eq 'emailverified') {
16790:                         my %options = &Apache::lonlocal::texthash (
16791:                                                                     all   => 'Same as e-mail',
16792:                                                                     first => 'Omit @domain',
16793:                                                                     free  => 'Free to choose',
16794:                                                                   );
16795:                         if (@types) {
16796:                             if (@statuses) {
16797:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
16798:                                             '<ul>';
16799:                                 foreach my $status (@statuses) {
16800:                                     if ($status eq 'default') {
16801:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
16802:                                     } else {
16803:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
16804:                                     }
16805:                                 }
16806:                                 $chgtext .= '</ul>';
16807:                             }
16808:                         } else {
16809:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
16810:                                             $options{$cancreate{'emailverified'}{'default'}});
16811:                         }
16812:                     } elsif ($type eq 'emailoptions') {
16813:                         my %options = &Apache::lonlocal::texthash (
16814:                                                                     any     => 'Any e-mail',
16815:                                                                     inst    => 'Institutional only',
16816:                                                                     noninst => 'Non-institutional only',
16817:                                                                     custom  => 'Custom restrictions',
16818:                                                                   );
16819:                         if (@types) {
16820:                             if (@statuses) {
16821:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
16822:                                             '<ul>';
16823:                                 foreach my $status (@statuses) {
16824:                                     if ($type eq 'default') {
16825:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
16826:                                     } else {
16827:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
16828:                                     }
16829:                                 }
16830:                                 $chgtext .= '</ul>';
16831:                             }
16832:                         } else {
16833:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
16834:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
16835:                             } else {
16836:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
16837:                                                 $options{$cancreate{'emailoptions'}{'default'}});
16838:                             }
16839:                         }
16840:                     } elsif ($type eq 'emaildomain') {
16841:                         my $output;
16842:                         if (@statuses) {
16843:                             foreach my $type (@statuses) {
16844:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
16845:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
16846:                                         if ($type eq 'default') {
16847:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
16848:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
16849:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
16850:                                             } else {
16851:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
16852:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
16853:                                             }
16854:                                         } else {
16855:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
16856:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
16857:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
16858:                                             } else {
16859:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
16860:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
16861:                                             }
16862:                                         }
16863:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
16864:                                         if ($type eq 'default') {
16865:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
16866:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
16867:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
16868:                                             } else {
16869:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
16870:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
16871:                                             }
16872:                                         } else {
16873:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
16874:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
16875:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
16876:                                             } else {
16877:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
16878:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
16879:                                             }
16880:                                         }
16881:                                     }
16882:                                 }
16883:                             }
16884:                         }
16885:                         if ($output ne '') {
16886:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
16887:                                         '<ul>'.$output.'</ul>';
16888:                         }
16889:                     } elsif ($type eq 'captcha') {
16890:                         if ($savecaptcha{$type} eq 'notused') {
16891:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
16892:                         } else {
16893:                             my %captchas = &captcha_phrases();
16894:                             if ($captchas{$savecaptcha{$type}}) {
16895:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
16896:                             } else {
16897:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
16898:                             }
16899:                         }
16900:                     } elsif ($type eq 'recaptchakeys') {
16901:                         my ($privkey,$pubkey);
16902:                         if (ref($savecaptcha{$type}) eq 'HASH') {
16903:                             $pubkey = $savecaptcha{$type}{'public'};
16904:                             $privkey = $savecaptcha{$type}{'private'};
16905:                         }
16906:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
16907:                         if (!$pubkey) {
16908:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
16909:                         } else {
16910:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
16911:                         }
16912:                         if (!$privkey) {
16913:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
16914:                         } else {
16915:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
16916:                         }
16917:                         $chgtext .= '</ul>';
16918:                     } elsif ($type eq 'recaptchaversion') {
16919:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
16920:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
16921:                         }
16922:                     } elsif ($type eq 'emailusername') {
16923:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
16924:                             if (@statuses) {
16925:                                 foreach my $type (@statuses) {
16926:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
16927:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
16928:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
16929:                                                     '<ul>';
16930:                                             foreach my $field (@{$infofields}) {
16931:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
16932:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
16933:                                                 }
16934:                                             }
16935:                                             $chgtext .= '</ul>';
16936:                                         } else {
16937:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
16938:                                         }
16939:                                     } else {
16940:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
16941:                                     }
16942:                                 }
16943:                             }
16944:                         }
16945:                     } elsif ($type eq 'notify') {
16946:                         my $numapprove = 0;
16947:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
16948:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
16949:                                 if ($cancreate{'notify'}{'approval'}) {
16950:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
16951:                                     $numapprove ++;
16952:                                 }
16953:                             }
16954:                         }
16955:                         unless ($numapprove) {
16956:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
16957:                         }
16958:                     }
16959:                     if ($chgtext) {
16960:                         $resulttext .= '<li>'.$chgtext.'</li>';
16961:                     }
16962:                 }
16963:             }
16964:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
16965:                 my ($emailrules,$emailruleorder) =
16966:                     &Apache::lonnet::inst_userrules($dom,'email');
16967:                 foreach my $type (@{$changes{'email_rule'}}) {
16968:                     if (ref($email_rule{$type}) eq 'ARRAY') {
16969:                         my $chgtext = '<ul>';
16970:                         foreach my $rule (@{$email_rule{$type}}) {
16971:                             if (ref($emailrules->{$rule}) eq 'HASH') {
16972:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
16973:                             }
16974:                         }
16975:                         $chgtext .= '</ul>';
16976:                         my $typename;
16977:                         if (@types) {
16978:                             if ($type eq 'default') {
16979:                                 $typename = $othertitle;
16980:                             } else {
16981:                                 $typename = $usertypes{$type};
16982:                             }
16983:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
16984:                         }
16985:                         if (@{$email_rule{$type}} > 0) {
16986:                             $resulttext .= '<li>'.
16987:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
16988:                                                $usertypes{$type}).
16989:                                            $chgtext.
16990:                                            '</li>';
16991:                         } else {
16992:                             $resulttext .= '<li>'.
16993:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
16994:                                            '</li>'.
16995:                                            &mt('(Affiliation: [_1])',$typename);
16996:                         }
16997:                     }
16998:                 }
16999:             }
17000:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
17001:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
17002:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
17003:                         my $chgtext = '<ul>';
17004:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
17005:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
17006:                         }
17007:                         $chgtext .= '</ul>';
17008:                         $resulttext .= '<li>'.
17009:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
17010:                                           $chgtext.
17011:                                        '</li>';
17012:                     } else {
17013:                         $resulttext .= '<li>'.
17014:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
17015:                                        '</li>';
17016:                     }
17017:                 }
17018:             }
17019:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
17020:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
17021:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
17022:                 foreach my $type (@{$changes{'selfcreate'}}) {
17023:                     my $typename = $type;
17024:                     if (keys(%usertypes) > 0) {
17025:                         if ($usertypes{$type} ne '') {
17026:                             $typename = $usertypes{$type};
17027:                         }
17028:                     }
17029:                     my @modifiable;
17030:                     $resulttext .= '<li>'.
17031:                                     &mt('Self-creation of account by users with status: [_1]',
17032:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
17033:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
17034:                     foreach my $field (@fields) {
17035:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
17036:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
17037:                         }
17038:                     }
17039:                     if (@modifiable > 0) {
17040:                         $resulttext .= join(', ',@modifiable);
17041:                     } else {
17042:                         $resulttext .= &mt('none');
17043:                     }
17044:                     $resulttext .= '</li>';
17045:                 }
17046:                 $resulttext .= '</ul></li>';
17047:             }
17048:             $resulttext .= '</ul>';
17049:             my $cachetime = 24*60*60;
17050:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
17051:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17052:             if (ref($lastactref) eq 'HASH') {
17053:                 $lastactref->{'domdefaults'} = 1;
17054:             }
17055:         } else {
17056:             $resulttext = &mt('No changes made to self-creation settings');
17057:         }
17058:     } else {
17059:         $resulttext = '<span class="LC_error">'.
17060:             &mt('An error occurred: [_1]',$putresult).'</span>';
17061:     }
17062:     if ($warningmsg ne '') {
17063:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
17064:     }
17065:     return $resulttext;
17066: }
17067: 
17068: sub process_captcha {
17069:     my ($container,$changes,$newsettings,$currsettings) = @_;
17070:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
17071:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
17072:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
17073:         $newsettings->{'captcha'} = 'original';
17074:     }
17075:     my %current;
17076:     if (ref($currsettings) eq 'HASH') {
17077:         %current = %{$currsettings};
17078:     }
17079:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
17080:         if ($container eq 'cancreate') {
17081:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17082:                 push(@{$changes->{'cancreate'}},'captcha');
17083:             } elsif (!defined($changes->{'cancreate'})) {
17084:                 $changes->{'cancreate'} = ['captcha'];
17085:             }
17086:         } elsif ($container eq 'passwords') {
17087:             $changes->{'reset'} = 1;
17088:         } else {
17089:             $changes->{'captcha'} = 1;
17090:         }
17091:     }
17092:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
17093:     if ($newsettings->{'captcha'} eq 'recaptcha') {
17094:         $newpub = $env{'form.'.$container.'_recaptchapub'};
17095:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
17096:         $newpub =~ s/[^\w\-]//g;
17097:         $newpriv =~ s/[^\w\-]//g;
17098:         $newsettings->{'recaptchakeys'} = {
17099:                                              public  => $newpub,
17100:                                              private => $newpriv,
17101:                                           };
17102:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
17103:         $newversion =~ s/\D//g;
17104:         if ($newversion ne '2') {
17105:             $newversion = 1;
17106:         }
17107:         $newsettings->{'recaptchaversion'} = $newversion;
17108:     }
17109:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
17110:         $currpub = $current{'recaptchakeys'}{'public'};
17111:         $currpriv = $current{'recaptchakeys'}{'private'};
17112:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
17113:             $newsettings->{'recaptchakeys'} = {
17114:                                                  public  => '',
17115:                                                  private => '',
17116:                                               }
17117:         }
17118:     }
17119:     if ($current{'captcha'} eq 'recaptcha') {
17120:         $currversion = $current{'recaptchaversion'};
17121:         if ($currversion ne '2') {
17122:             $currversion = 1;
17123:         }
17124:     }
17125:     if ($currversion ne $newversion) {
17126:         if ($container eq 'cancreate') {
17127:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17128:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
17129:             } elsif (!defined($changes->{'cancreate'})) {
17130:                 $changes->{'cancreate'} = ['recaptchaversion'];
17131:             }
17132:         } elsif ($container eq 'passwords') {
17133:             $changes->{'reset'} = 1;
17134:         } else {
17135:             $changes->{'recaptchaversion'} = 1;
17136:         }
17137:     }
17138:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
17139:         if ($container eq 'cancreate') {
17140:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17141:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
17142:             } elsif (!defined($changes->{'cancreate'})) {
17143:                 $changes->{'cancreate'} = ['recaptchakeys'];
17144:             }
17145:         } elsif ($container eq 'passwords') {
17146:             $changes->{'reset'} = 1;
17147:         } else {
17148:             $changes->{'recaptchakeys'} = 1;
17149:         }
17150:     }
17151:     return;
17152: }
17153: 
17154: sub modify_usermodification {
17155:     my ($dom,%domconfig) = @_;
17156:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
17157:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
17158:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
17159:             if ($key eq 'selfcreate') {
17160:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
17161:             } else {  
17162:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
17163:             }
17164:         }
17165:     }
17166:     my @contexts = ('author','course');
17167:     my %context_title = (
17168:                            author => 'In author context',
17169:                            course => 'In course context',
17170:                         );
17171:     my @fields = ('lastname','firstname','middlename','generation',
17172:                   'permanentemail','id');
17173:     my %roles = (
17174:                   author => ['ca','aa'],
17175:                   course => ['st','ep','ta','in','cr'],
17176:                 );
17177:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
17178:     foreach my $context (@contexts) {
17179:         foreach my $role (@{$roles{$context}}) {
17180:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
17181:             foreach my $item (@fields) {
17182:                 if (grep(/^\Q$item\E$/,@modifiable)) {
17183:                     $modifyhash{$context}{$role}{$item} = 1;
17184:                 } else {
17185:                     $modifyhash{$context}{$role}{$item} = 0;
17186:                 }
17187:             }
17188:         }
17189:         if (ref($curr_usermodification{$context}) eq 'HASH') {
17190:             foreach my $role (@{$roles{$context}}) {
17191:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
17192:                     foreach my $field (@fields) {
17193:                         if ($modifyhash{$context}{$role}{$field} ne 
17194:                                 $curr_usermodification{$context}{$role}{$field}) {
17195:                             push(@{$changes{$context}},$role);
17196:                             last;
17197:                         }
17198:                     }
17199:                 }
17200:             }
17201:         } else {
17202:             foreach my $context (@contexts) {
17203:                 foreach my $role (@{$roles{$context}}) {
17204:                     push(@{$changes{$context}},$role);
17205:                 }
17206:             }
17207:         }
17208:     }
17209:     my %usermodification_hash =  (
17210:                                    usermodification => \%modifyhash,
17211:                                  );
17212:     my $putresult = &Apache::lonnet::put_dom('configuration',
17213:                                              \%usermodification_hash,$dom);
17214:     if ($putresult eq 'ok') {
17215:         if (keys(%changes) > 0) {
17216:             $resulttext = &mt('Changes made: ').'<ul>';
17217:             foreach my $context (@contexts) {
17218:                 if (ref($changes{$context}) eq 'ARRAY') {
17219:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
17220:                     if (ref($changes{$context}) eq 'ARRAY') {
17221:                         foreach my $role (@{$changes{$context}}) {
17222:                             my $rolename;
17223:                             if ($role eq 'cr') {
17224:                                 $rolename = &mt('Custom');
17225:                             } else {
17226:                                 $rolename = &Apache::lonnet::plaintext($role);
17227:                             }
17228:                             my @modifiable;
17229:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
17230:                             foreach my $field (@fields) {
17231:                                 if ($modifyhash{$context}{$role}{$field}) {
17232:                                     push(@modifiable,$fieldtitles{$field});
17233:                                 }
17234:                             }
17235:                             if (@modifiable > 0) {
17236:                                 $resulttext .= join(', ',@modifiable);
17237:                             } else {
17238:                                 $resulttext .= &mt('none'); 
17239:                             }
17240:                             $resulttext .= '</li>';
17241:                         }
17242:                         $resulttext .= '</ul></li>';
17243:                     }
17244:                 }
17245:             }
17246:             $resulttext .= '</ul>';
17247:         } else {
17248:             $resulttext = &mt('No changes made to user modification settings');
17249:         }
17250:     } else {
17251:         $resulttext = '<span class="LC_error">'.
17252:             &mt('An error occurred: [_1]',$putresult).'</span>';
17253:     }
17254:     return $resulttext;
17255: }
17256: 
17257: sub modify_defaults {
17258:     my ($dom,$lastactref,%domconfig) = @_;
17259:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
17260:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17261:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
17262:                  'portal_def');
17263:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
17264:     foreach my $item (@items) {
17265:         $newvalues{$item} = $env{'form.'.$item};
17266:         if ($item eq 'auth_def') {
17267:             if ($newvalues{$item} ne '') {
17268:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
17269:                     push(@errors,$item);
17270:                 }
17271:             }
17272:         } elsif ($item eq 'lang_def') {
17273:             if ($newvalues{$item} ne '') {
17274:                 if ($newvalues{$item} =~ /^(\w+)/) {
17275:                     my $langcode = $1;
17276:                     if ($langcode ne 'x_chef') {
17277:                         if (code2language($langcode) eq '') {
17278:                             push(@errors,$item);
17279:                         }
17280:                     }
17281:                 } else {
17282:                     push(@errors,$item);
17283:                 }
17284:             }
17285:         } elsif ($item eq 'timezone_def') {
17286:             if ($newvalues{$item} ne '') {
17287:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
17288:                     push(@errors,$item);   
17289:                 }
17290:             }
17291:         } elsif ($item eq 'datelocale_def') {
17292:             if ($newvalues{$item} ne '') {
17293:                 my @datelocale_ids = DateTime::Locale->ids();
17294:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
17295:                     push(@errors,$item);
17296:                 }
17297:             }
17298:         } elsif ($item eq 'portal_def') {
17299:             if ($newvalues{$item} ne '') {
17300:                 if ($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])\/?$/) {
17301:                     foreach my $field ('email','web') {
17302:                         if ($env{'form.'.$item.'_'.$field}) {
17303:                             $newvalues{$item.'_'.$field} = $env{'form.'.$item.'_'.$field};
17304:                         }
17305:                     }
17306:                 } else {
17307:                     push(@errors,$item);
17308:                 }
17309:             }
17310:         }
17311:         if (grep(/^\Q$item\E$/,@errors)) {
17312:             $newvalues{$item} = $domdefaults{$item};
17313:             if ($item eq 'portal_def') {
17314:                 if ($domdefaults{$item}) {
17315:                     foreach my $field ('email','web') {
17316:                         if (exists($domdefaults{$item.'_'.$field})) {
17317:                             $newvalues{$item.'_'.$field} = $domdefaults{$item.'_'.$field};
17318:                         }
17319:                     }
17320:                 }
17321:             }
17322:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
17323:             $changes{$item} = 1;
17324:         }
17325:         if ($item eq 'portal_def') {
17326:             unless (grep(/^\Q$item\E$/,@errors)) {
17327:                 if ($newvalues{$item} eq '') {
17328:                     foreach my $field ('email','web') {
17329:                         if (exists($domdefaults{$item.'_'.$field})) {
17330:                             delete($domdefaults{$item.'_'.$field});
17331:                         }
17332:                     }
17333:                 } else {
17334:                     unless ($changes{$item}) {
17335:                         foreach my $field ('email','web') {
17336:                             if ($domdefaults{$item.'_'.$field} ne $newvalues{$item.'_'.$field}) {
17337:                                 $changes{$item} = 1;
17338:                                 last;
17339:                             }
17340:                         }
17341:                     }
17342:                     foreach my $field ('email','web') {
17343:                         if ($newvalues{$item.'_'.$field}) {
17344:                             $domdefaults{$item.'_'.$field} = $newvalues{$item.'_'.$field};
17345:                         } elsif (exists($domdefaults{$item.'_'.$field})) {
17346:                             delete($domdefaults{$item.'_'.$field});
17347:                         }
17348:                     }
17349:                 }
17350:             }
17351:         }
17352:         $domdefaults{$item} = $newvalues{$item};
17353:     }
17354:     my %staticdefaults = (
17355:                            'intauth_cost'   => 10,
17356:                            'intauth_check'  => 0,
17357:                            'intauth_switch' => 0,
17358:                          );
17359:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
17360:         if (exists($domdefaults{$item})) {
17361:             $newvalues{$item} = $domdefaults{$item};
17362:         } else {
17363:             $newvalues{$item} = $staticdefaults{$item};
17364:         }
17365:     }
17366:     my ($unamemaprules,$ruleorder);
17367:     my @possunamemaprules = &Apache::loncommon::get_env_multiple('form.unamemap_rule');
17368:     if (@possunamemaprules) {
17369:         ($unamemaprules,$ruleorder) =
17370:             &Apache::lonnet::inst_userrules($dom,'unamemap');
17371:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
17372:             if (@{$ruleorder} > 0) {
17373:                 my %possrules;
17374:                 map { $possrules{$_} = 1; } @possunamemaprules;
17375:                 foreach my $rule (@{$ruleorder}) {
17376:                     if ($possrules{$rule}) {
17377:                         push(@{$newvalues{'unamemap_rule'}},$rule);
17378:                     }
17379:                 }
17380:             }
17381:         }
17382:     }
17383:     if (ref($domdefaults{'unamemap_rule'}) eq 'ARRAY') {
17384:         if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
17385:             my @rulediffs = &Apache::loncommon::compare_arrays($domdefaults{'unamemap_rule'},
17386:                                                                $newvalues{'unamemap_rule'});
17387:             if (@rulediffs) {
17388:                 $changes{'unamemap_rule'} = 1;
17389:                 $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
17390:             }
17391:         } elsif (@{$domdefaults{'unamemap_rule'}} > 0) {
17392:             $changes{'unamemap_rule'} = 1;
17393:             delete($domdefaults{'unamemap_rule'});
17394:         }
17395:     } elsif (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
17396:         if (@{$newvalues{'unamemap_rule'}} > 0) {
17397:             $changes{'unamemap_rule'} = 1;
17398:             $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
17399:         }
17400:     }
17401:     my %defaults_hash = (
17402:                          defaults => \%newvalues,
17403:                         );
17404:     my $title = &defaults_titles();
17405: 
17406:     my $currinststatus;
17407:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
17408:         $currinststatus = $domconfig{'inststatus'};
17409:     } else {
17410:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17411:         $currinststatus = {
17412:                              inststatustypes => $usertypes,
17413:                              inststatusorder => $types,
17414:                              inststatusguest => [],
17415:                           };
17416:     }
17417:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
17418:     my @allpos;
17419:     my %alltypes;
17420:     my @inststatusguest;
17421:     if (ref($currinststatus) eq 'HASH') {
17422:         if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
17423:             foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
17424:                 unless (grep(/^\Q$type\E$/,@todelete)) {
17425:                     push(@inststatusguest,$type);
17426:                 }
17427:             }
17428:         }
17429:     }
17430:     my ($currtitles,$currorder);
17431:     if (ref($currinststatus) eq 'HASH') {
17432:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
17433:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
17434:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
17435:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
17436:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
17437:                     }
17438:                 }
17439:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
17440:                     my $position = $env{'form.inststatus_pos_'.$type};
17441:                     $position =~ s/\D+//g;
17442:                     $allpos[$position] = $type;
17443:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
17444:                     $alltypes{$type} =~ s/`//g;
17445:                 }
17446:             }
17447:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
17448:             $currtitles =~ s/,$//;
17449:         }
17450:     }
17451:     if ($env{'form.addinststatus'}) {
17452:         my $newtype = $env{'form.addinststatus'};
17453:         $newtype =~ s/\W//g;
17454:         unless (exists($alltypes{$newtype})) {
17455:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
17456:             $alltypes{$newtype} =~ s/`//g; 
17457:             my $position = $env{'form.addinststatus_pos'};
17458:             $position =~ s/\D+//g;
17459:             if ($position ne '') {
17460:                 $allpos[$position] = $newtype;
17461:             }
17462:         }
17463:     }
17464:     my @orderedstatus;
17465:     foreach my $type (@allpos) {
17466:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
17467:             push(@orderedstatus,$type);
17468:         }
17469:     }
17470:     foreach my $type (keys(%alltypes)) {
17471:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
17472:             delete($alltypes{$type});
17473:         }
17474:     }
17475:     $defaults_hash{'inststatus'} = {
17476:                                      inststatustypes => \%alltypes,
17477:                                      inststatusorder => \@orderedstatus,
17478:                                      inststatusguest => \@inststatusguest,
17479:                                    };
17480:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
17481:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
17482:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
17483:         }
17484:     }
17485:     if ($currorder ne join(',',@orderedstatus)) {
17486:         $changes{'inststatus'}{'inststatusorder'} = 1;
17487:     }
17488:     my $newtitles;
17489:     foreach my $item (@orderedstatus) {
17490:         $newtitles .= $alltypes{$item}.',';
17491:     }
17492:     $newtitles =~ s/,$//;
17493:     if ($currtitles ne $newtitles) {
17494:         $changes{'inststatus'}{'inststatustypes'} = 1;
17495:     }
17496:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
17497:                                              $dom);
17498:     if ($putresult eq 'ok') {
17499:         if (keys(%changes) > 0) {
17500:             $resulttext = &mt('Changes made:').'<ul>';
17501:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
17502:             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";
17503:             foreach my $item (sort(keys(%changes))) {
17504:                 if ($item eq 'inststatus') {
17505:                     if (ref($changes{'inststatus'}) eq 'HASH') {
17506:                         if (@orderedstatus) {
17507:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
17508:                             foreach my $type (@orderedstatus) { 
17509:                                 $resulttext .= $alltypes{$type}.', ';
17510:                             }
17511:                             $resulttext =~ s/, $//;
17512:                             $resulttext .= '</li>';
17513:                         } else {
17514:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
17515:                         }
17516:                     }
17517:                 } elsif ($item eq 'unamemap_rule') {
17518:                     if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
17519:                         my @rulenames;
17520:                         if (ref($unamemaprules) eq 'HASH') {
17521:                             foreach my $rule (@{$newvalues{'unamemap_rule'}}) {
17522:                                 if (ref($unamemaprules->{$rule}) eq 'HASH') {
17523:                                     push(@rulenames,$unamemaprules->{$rule}->{'name'});
17524:                                 }
17525:                             }
17526:                         }
17527:                         if (@rulenames) {
17528:                             $resulttext .= '<li>'.&mt('Mapping for missing usernames includes: [_1]',
17529:                                                      '<ul><li>'.join('</li><li>',@rulenames).'</li></ul>').
17530:                                            '</li>';
17531:                         } else {
17532:                             $resulttext .= '<li>'.&mt('No mapping for missing usernames via standard log-in').'</li>';
17533:                         }
17534:                     } else {
17535:                         $resulttext .= '<li>'.&mt('Mapping for missing usernames via standard log-in deleted').'</li>';
17536:                     }
17537:                 } else {
17538:                     my $value = $env{'form.'.$item};
17539:                     if ($value eq '') {
17540:                         $value = &mt('none');
17541:                     } elsif ($item eq 'auth_def') {
17542:                         my %authnames = &authtype_names();
17543:                         my %shortauth = (
17544:                                           internal   => 'int',
17545:                                           krb4       => 'krb4',
17546:                                           krb5       => 'krb5',
17547:                                           localauth  => 'loc',
17548:                                           lti        => 'lti',
17549:                         );
17550:                         $value = $authnames{$shortauth{$value}};
17551:                     }
17552:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
17553:                     $mailmsgtext .= "$title->{$item} set to $value\n";
17554:                     if ($item eq 'portal_def') {
17555:                         if ($env{'form.'.$item} ne '') {
17556:                             foreach my $field ('email','web') {
17557:                                 $value = $env{'form.'.$item.'_'.$field};
17558:                                 if ($value) {
17559:                                     $value = &mt('Yes');
17560:                                 } else {
17561:                                     $value = &mt('No');
17562:                                 }
17563:                                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$field},$value).'</li>';
17564:                             }
17565:                         }
17566:                     }
17567:                 }
17568:             }
17569:             $resulttext .= '</ul>';
17570:             $mailmsgtext .= "\n";
17571:             my $cachetime = 24*60*60;
17572:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17573:             if (ref($lastactref) eq 'HASH') {
17574:                 $lastactref->{'domdefaults'} = 1;
17575:             }
17576:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
17577:                 my $notify = 1;
17578:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
17579:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
17580:                         $notify = 0;
17581:                     }
17582:                 }
17583:                 if ($notify) {
17584:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
17585:                                                "LON-CAPA Domain Settings Change - $dom",
17586:                                                $mailmsgtext);
17587:                 }
17588:             }
17589:         } else {
17590:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
17591:         }
17592:     } else {
17593:         $resulttext = '<span class="LC_error">'.
17594:             &mt('An error occurred: [_1]',$putresult).'</span>';
17595:     }
17596:     if (@errors > 0) {
17597:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
17598:         foreach my $item (@errors) {
17599:             $resulttext .= ' "'.$title->{$item}.'",';
17600:         }
17601:         $resulttext =~ s/,$//;
17602:     }
17603:     return $resulttext;
17604: }
17605: 
17606: sub modify_scantron {
17607:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
17608:     my ($resulttext,%confhash,%changes,$errors);
17609:     my $custom = 'custom.tab';
17610:     my $default = 'default.tab';
17611:     my $servadm = $r->dir_config('lonAdmEMail');
17612:     my ($configuserok,$author_ok,$switchserver) =
17613:         &config_check($dom,$confname,$servadm);
17614:     if ($env{'form.scantronformat.filename'} ne '') {
17615:         my $error;
17616:         if ($configuserok eq 'ok') {
17617:             if ($switchserver) {
17618:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
17619:             } else {
17620:                 if ($author_ok eq 'ok') {
17621:                     my $modified = [];
17622:                     my ($result,$scantronurl) =
17623:                         &Apache::lonconfigsettings::publishlogo($r,'upload','scantronformat',$dom,
17624:                                                                 $confname,'scantron','','',$custom,
17625:                                                                 $modified);
17626:                     if ($result eq 'ok') {
17627:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
17628:                         $changes{'scantronformat'} = 1;
17629:                         &update_modify_urls($r,$modified);
17630:                     } else {
17631:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
17632:                     }
17633:                 } else {
17634:                     $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);
17635:                 }
17636:             }
17637:         } else {
17638:             $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);
17639:         }
17640:         if ($error) {
17641:             &Apache::lonnet::logthis($error);
17642:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
17643:         }
17644:     }
17645:     if (ref($domconfig{'scantron'}) eq 'HASH') {
17646:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
17647:             if ($env{'form.scantronformat_del'}) {
17648:                 $confhash{'scantron'}{'scantronformat'} = '';
17649:                 $changes{'scantronformat'} = 1;
17650:             } else {
17651:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
17652:             }
17653:         }
17654:     }
17655:     my @options = ('hdr','pad','rem');
17656:     my @fields = &scantroncsv_fields();
17657:     my %titles = &scantronconfig_titles();
17658:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
17659:     my ($newdat,$currdat,%newcol,%currcol);
17660:     if (grep(/^dat$/,@formats)) {
17661:         $confhash{'scantron'}{config}{dat} = 1;
17662:         $newdat = 1;
17663:     } else {
17664:         $newdat = 0;
17665:     }
17666:     if (grep(/^csv$/,@formats)) {
17667:         my %bynum;
17668:         foreach my $field (@fields) {
17669:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
17670:                 my $posscol = $1;
17671:                 if (($posscol < 20) && (!$bynum{$posscol})) {
17672:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
17673:                     $bynum{$posscol} = $field;
17674:                     $newcol{$field} = $posscol;
17675:                 }
17676:             }
17677:         }
17678:         if (keys(%newcol)) {
17679:             foreach my $option (@options) {
17680:                 if ($env{'form.scantroncsv_'.$option}) {
17681:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
17682:                 }
17683:             }
17684:         }
17685:     }
17686:     $currdat = 1;
17687:     if (ref($domconfig{'scantron'}) eq 'HASH') {
17688:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
17689:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
17690:                 $currdat = 0;
17691:             }
17692:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
17693:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
17694:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
17695:                 }
17696:             }
17697:         }
17698:     }
17699:     if ($currdat != $newdat) {
17700:         $changes{'config'} = 1;
17701:     } else {
17702:         foreach my $field (@fields) {
17703:             if ($currcol{$field} ne '') {
17704:                 if ($currcol{$field} ne $newcol{$field}) {
17705:                     $changes{'config'} = 1;
17706:                     last;
17707:                 }
17708:             } elsif ($newcol{$field} ne '') {
17709:                 $changes{'config'} = 1;
17710:                 last;
17711:             }
17712:         }
17713:     }
17714:     if (keys(%confhash) > 0) {
17715:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
17716:                                                  $dom);
17717:         if ($putresult eq 'ok') {
17718:             if (keys(%changes) > 0) {
17719:                 if (ref($confhash{'scantron'}) eq 'HASH') {
17720:                     $resulttext = &mt('Changes made:').'<ul>';
17721:                     if ($changes{'scantronformat'}) {
17722:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
17723:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
17724:                         } else {
17725:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
17726:                         }
17727:                     }
17728:                     if ($changes{'config'}) {
17729:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
17730:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
17731:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
17732:                             }
17733:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
17734:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
17735:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
17736:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
17737:                                         foreach my $field (@fields) {
17738:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
17739:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
17740:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
17741:                                             }
17742:                                         }
17743:                                         $resulttext .= '</ul></li>';
17744:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
17745:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
17746:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
17747:                                                 foreach my $option (@options) {
17748:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
17749:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
17750:                                                     }
17751:                                                 }
17752:                                                 $resulttext .= '</ul></li>';
17753:                                             }
17754:                                         }
17755:                                     }
17756:                                 }
17757:                             }
17758:                         } else {
17759:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
17760:                         }
17761:                     }
17762:                     $resulttext .= '</ul>';
17763:                 } else {
17764:                     $resulttext = &mt('Changes made to bubblesheet format file.');
17765:                 }
17766:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
17767:                 if (ref($lastactref) eq 'HASH') {
17768:                     $lastactref->{'domainconfig'} = 1;
17769:                 }
17770:             } else {
17771:                 $resulttext = &mt('No changes made to bubblesheet format settings');
17772:             }
17773:         } else {
17774:             $resulttext = '<span class="LC_error">'.
17775:                 &mt('An error occurred: [_1]',$putresult).'</span>';
17776:         }
17777:     } else {
17778:         $resulttext = &mt('No changes made to bubblesheet format settings');
17779:     }
17780:     if ($errors) {
17781:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
17782:                        $errors.'</ul></p>';
17783:     }
17784:     return $resulttext;
17785: }
17786: 
17787: sub modify_coursecategories {
17788:     my ($dom,$lastactref,%domconfig) = @_;
17789:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
17790:         $cathash);
17791:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
17792:     my @catitems = ('unauth','auth');
17793:     my @cattypes = ('std','domonly','codesrch','none');
17794:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
17795:         $cathash = $domconfig{'coursecategories'}{'cats'};
17796:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
17797:             $changes{'togglecats'} = 1;
17798:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
17799:         }
17800:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
17801:             $changes{'categorize'} = 1;
17802:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
17803:         }
17804:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
17805:             $changes{'togglecatscomm'} = 1;
17806:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
17807:         }
17808:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
17809:             $changes{'categorizecomm'} = 1;
17810:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
17811:         }
17812:         foreach my $item (@catitems) {
17813:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
17814:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
17815:                     $changes{$item} = 1;
17816:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
17817:                 }
17818:             }
17819:         }
17820:     } else {
17821:         $changes{'togglecats'} = 1;
17822:         $changes{'categorize'} = 1;
17823:         $changes{'togglecatscomm'} = 1;
17824:         $changes{'categorizecomm'} = 1;
17825:         $domconfig{'coursecategories'} = {
17826:                                              togglecats => $env{'form.togglecats'},
17827:                                              categorize => $env{'form.categorize'},
17828:                                              togglecatscomm => $env{'form.togglecatscomm'},
17829:                                              categorizecomm => $env{'form.categorizecomm'},
17830:                                          };
17831:         foreach my $item (@catitems) {
17832:             if ($env{'form.coursecat_'.$item} ne 'std') {
17833:                 $changes{$item} = 1;
17834:             }
17835:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
17836:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
17837:             }
17838:         }
17839:     }
17840:     if (ref($cathash) eq 'HASH') {
17841:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
17842:             push (@deletecategory,'instcode::0');
17843:         }
17844:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
17845:             push(@deletecategory,'communities::0');
17846:         }
17847:     }
17848:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
17849:     if (ref($cathash) eq 'HASH') {
17850:         if (@deletecategory > 0) {
17851:             #FIXME Need to remove category from all courses using a deleted category 
17852:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
17853:             foreach my $item (@deletecategory) {
17854:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
17855:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
17856:                     $deletions{$item} = 1;
17857:                     &recurse_cat_deletes($item,$cathash,\%deletions);
17858:                 }
17859:             }
17860:         }
17861:         foreach my $item (keys(%{$cathash})) {
17862:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
17863:             if ($cathash->{$item} ne $env{'form.'.$item}) {
17864:                 $reorderings{$item} = 1;
17865:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
17866:             }
17867:             if ($env{'form.addcategory_name_'.$item} ne '') {
17868:                 my $newcat = $env{'form.addcategory_name_'.$item};
17869:                 my $newdepth = $depth+1;
17870:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
17871:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
17872:                 $adds{$newitem} = 1; 
17873:             }
17874:             if ($env{'form.subcat_'.$item} ne '') {
17875:                 my $newcat = $env{'form.subcat_'.$item};
17876:                 my $newdepth = $depth+1;
17877:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
17878:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
17879:                 $adds{$newitem} = 1;
17880:             }
17881:         }
17882:     }
17883:     if ($env{'form.instcode'} eq '1') {
17884:         if (ref($cathash) eq 'HASH') {
17885:             my $newitem = 'instcode::0';
17886:             if ($cathash->{$newitem} eq '') {  
17887:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
17888:                 $adds{$newitem} = 1;
17889:             }
17890:         } else {
17891:             my $newitem = 'instcode::0';
17892:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
17893:             $adds{$newitem} = 1;
17894:         }
17895:     }
17896:     if ($env{'form.communities'} eq '1') {
17897:         if (ref($cathash) eq 'HASH') {
17898:             my $newitem = 'communities::0';
17899:             if ($cathash->{$newitem} eq '') {
17900:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
17901:                 $adds{$newitem} = 1;
17902:             }
17903:         } else {
17904:             my $newitem = 'communities::0';
17905:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
17906:             $adds{$newitem} = 1;
17907:         }
17908:     }
17909:     if ($env{'form.addcategory_name'} ne '') {
17910:         if (($env{'form.addcategory_name'} ne 'instcode') &&
17911:             ($env{'form.addcategory_name'} ne 'communities')) {
17912:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
17913:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
17914:             $adds{$newitem} = 1;
17915:         }
17916:     }
17917:     my $putresult;
17918:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
17919:         if (keys(%deletions) > 0) {
17920:             foreach my $key (keys(%deletions)) {
17921:                 if ($predelallitems{$key} ne '') {
17922:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
17923:                 }
17924:             }
17925:         }
17926:         my (@chkcats,@chktrails,%chkallitems);
17927:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
17928:         if (ref($chkcats[0]) eq 'ARRAY') {
17929:             my $depth = 0;
17930:             my $chg = 0;
17931:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
17932:                 my $name = $chkcats[0][$i];
17933:                 my $item;
17934:                 if ($name eq '') {
17935:                     $chg ++;
17936:                 } else {
17937:                     $item = &escape($name).'::0';
17938:                     if ($chg) {
17939:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
17940:                     }
17941:                     $depth ++; 
17942:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
17943:                     $depth --;
17944:                 }
17945:             }
17946:         }
17947:     }
17948:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
17949:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
17950:         if ($putresult eq 'ok') {
17951:             my %title = (
17952:                          togglecats     => 'Show/Hide a course in catalog',
17953:                          categorize     => 'Assign a category to a course',
17954:                          togglecatscomm => 'Show/Hide a community in catalog',
17955:                          categorizecomm => 'Assign a category to a community',
17956:                         );
17957:             my %level = (
17958:                          dom  => 'set in Domain ("Modify Course/Community")',
17959:                          crs  => 'set in Course ("Course Configuration")',
17960:                          comm => 'set in Community ("Community Configuration")',
17961:                          none     => 'No catalog',
17962:                          std      => 'Standard catalog',
17963:                          domonly  => 'Domain-only catalog',
17964:                          codesrch => 'Code search form',
17965:                         );
17966:             $resulttext = &mt('Changes made:').'<ul>';
17967:             if ($changes{'togglecats'}) {
17968:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
17969:             }
17970:             if ($changes{'categorize'}) {
17971:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
17972:             }
17973:             if ($changes{'togglecatscomm'}) {
17974:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
17975:             }
17976:             if ($changes{'categorizecomm'}) {
17977:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
17978:             }
17979:             if ($changes{'unauth'}) {
17980:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
17981:             }
17982:             if ($changes{'auth'}) {
17983:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
17984:             }
17985:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
17986:                 my $cathash;
17987:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
17988:                     $cathash = $domconfig{'coursecategories'}{'cats'};
17989:                 } else {
17990:                     $cathash = {};
17991:                 } 
17992:                 my (@cats,@trails,%allitems);
17993:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
17994:                 if (keys(%deletions) > 0) {
17995:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
17996:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
17997:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
17998:                     }
17999:                     $resulttext .= '</ul></li>';
18000:                 }
18001:                 if (keys(%reorderings) > 0) {
18002:                     my %sort_by_trail;
18003:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
18004:                     foreach my $key (keys(%reorderings)) {
18005:                         if ($allitems{$key} ne '') {
18006:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
18007:                         }
18008:                     }
18009:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
18010:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
18011:                     }
18012:                     $resulttext .= '</ul></li>';
18013:                 }
18014:                 if (keys(%adds) > 0) {
18015:                     my %sort_by_trail;
18016:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
18017:                     foreach my $key (keys(%adds)) {
18018:                         if ($allitems{$key} ne '') {
18019:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
18020:                         }
18021:                     }
18022:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
18023:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
18024:                     }
18025:                     $resulttext .= '</ul></li>';
18026:                 }
18027:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
18028:                 if (ref($lastactref) eq 'HASH') {
18029:                     $lastactref->{'cats'} = 1;
18030:                 }
18031:             }
18032:             $resulttext .= '</ul>';
18033:             if ($changes{'unauth'} || $changes{'auth'}) {
18034:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
18035:                 if ($changes{'auth'}) {
18036:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
18037:                 }
18038:                 if ($changes{'unauth'}) {
18039:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
18040:                 }
18041:                 my $cachetime = 24*60*60;
18042:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18043:                 if (ref($lastactref) eq 'HASH') {
18044:                     $lastactref->{'domdefaults'} = 1;
18045:                 }
18046:             }
18047:         } else {
18048:             $resulttext = '<span class="LC_error">'.
18049:                           &mt('An error occurred: [_1]',$putresult).'</span>';
18050:         }
18051:     } else {
18052:         $resulttext = &mt('No changes made to course and community categories');
18053:     }
18054:     return $resulttext;
18055: }
18056: 
18057: sub modify_serverstatuses {
18058:     my ($dom,%domconfig) = @_;
18059:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
18060:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
18061:         %currserverstatus = %{$domconfig{'serverstatuses'}};
18062:     }
18063:     my @pages = &serverstatus_pages();
18064:     foreach my $type (@pages) {
18065:         $newserverstatus{$type}{'namedusers'} = '';
18066:         $newserverstatus{$type}{'machines'} = '';
18067:         if (defined($env{'form.'.$type.'_namedusers'})) {
18068:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
18069:             my @okusers;
18070:             foreach my $user (@users) {
18071:                 my ($uname,$udom) = split(/:/,$user);
18072:                 if (($udom =~ /^$match_domain$/) &&   
18073:                     (&Apache::lonnet::domain($udom)) &&
18074:                     ($uname =~ /^$match_username$/)) {
18075:                     if (!grep(/^\Q$user\E/,@okusers)) {
18076:                         push(@okusers,$user);
18077:                     }
18078:                 }
18079:             }
18080:             if (@okusers > 0) {
18081:                  @okusers = sort(@okusers);
18082:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
18083:             }
18084:         }
18085:         if (defined($env{'form.'.$type.'_machines'})) {
18086:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
18087:             my @okmachines;
18088:             foreach my $ip (@machines) {
18089:                 my @parts = split(/\./,$ip);
18090:                 next if (@parts < 4);
18091:                 my $badip = 0;
18092:                 for (my $i=0; $i<4; $i++) {
18093:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
18094:                         $badip = 1;
18095:                         last;
18096:                     }
18097:                 }
18098:                 if (!$badip) {
18099:                     push(@okmachines,$ip);     
18100:                 }
18101:             }
18102:             @okmachines = sort(@okmachines);
18103:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
18104:         }
18105:     }
18106:     my %serverstatushash =  (
18107:                                 serverstatuses => \%newserverstatus,
18108:                             );
18109:     foreach my $type (@pages) {
18110:         foreach my $setting ('namedusers','machines') {
18111:             my (@current,@new);
18112:             if (ref($currserverstatus{$type}) eq 'HASH') {
18113:                 if ($currserverstatus{$type}{$setting} ne '') { 
18114:                     @current = split(/,/,$currserverstatus{$type}{$setting});
18115:                 }
18116:             }
18117:             if ($newserverstatus{$type}{$setting} ne '') {
18118:                 @new = split(/,/,$newserverstatus{$type}{$setting});
18119:             }
18120:             if (@current > 0) {
18121:                 if (@new > 0) {
18122:                     foreach my $item (@current) {
18123:                         if (!grep(/^\Q$item\E$/,@new)) {
18124:                             $changes{$type}{$setting} = 1;
18125:                             last;
18126:                         }
18127:                     }
18128:                     foreach my $item (@new) {
18129:                         if (!grep(/^\Q$item\E$/,@current)) {
18130:                             $changes{$type}{$setting} = 1;
18131:                             last;
18132:                         }
18133:                     }
18134:                 } else {
18135:                     $changes{$type}{$setting} = 1;
18136:                 }
18137:             } elsif (@new > 0) {
18138:                 $changes{$type}{$setting} = 1;
18139:             }
18140:         }
18141:     }
18142:     if (keys(%changes) > 0) {
18143:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
18144:         my $putresult = &Apache::lonnet::put_dom('configuration',
18145:                                                  \%serverstatushash,$dom);
18146:         if ($putresult eq 'ok') {
18147:             $resulttext .= &mt('Changes made:').'<ul>';
18148:             foreach my $type (@pages) {
18149:                 if (ref($changes{$type}) eq 'HASH') {
18150:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
18151:                     if ($changes{$type}{'namedusers'}) {
18152:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
18153:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
18154:                         } else {
18155:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
18156:                         }
18157:                     }
18158:                     if ($changes{$type}{'machines'}) {
18159:                         if ($newserverstatus{$type}{'machines'} eq '') {
18160:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
18161:                         } else {
18162:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
18163:                         }
18164: 
18165:                     }
18166:                     $resulttext .= '</ul></li>';
18167:                 }
18168:             }
18169:             $resulttext .= '</ul>';
18170:         } else {
18171:             $resulttext = '<span class="LC_error">'.
18172:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
18173: 
18174:         }
18175:     } else {
18176:         $resulttext = &mt('No changes made to access to server status pages');
18177:     }
18178:     return $resulttext;
18179: }
18180: 
18181: sub modify_helpsettings {
18182:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
18183:     my ($resulttext,$errors,%changes,%helphash);
18184:     my %defaultchecked = ('submitbugs' => 'on');
18185:     my @offon = ('off','on');
18186:     my @toggles = ('submitbugs');
18187:     my %current = ('submitbugs' => '',
18188:                    'adhoc'      => {},
18189:                   );
18190:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
18191:         %current = %{$domconfig{'helpsettings'}};
18192:     }
18193:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18194:     foreach my $item (@toggles) {
18195:         if ($defaultchecked{$item} eq 'on') { 
18196:             if ($current{$item} eq '') {
18197:                 if ($env{'form.'.$item} eq '0') {
18198:                     $changes{$item} = 1;
18199:                 }
18200:             } elsif ($current{$item} ne $env{'form.'.$item}) {
18201:                 $changes{$item} = 1;
18202:             }
18203:         } elsif ($defaultchecked{$item} eq 'off') {
18204:             if ($current{$item} eq '') {
18205:                 if ($env{'form.'.$item} eq '1') {
18206:                     $changes{$item} = 1;
18207:                 }
18208:             } elsif ($current{$item} ne $env{'form.'.$item}) {
18209:                 $changes{$item} = 1;
18210:             }
18211:         }
18212:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
18213:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
18214:         }
18215:     }
18216:     my $maxnum = $env{'form.helproles_maxnum'};
18217:     my $confname = $dom.'-domainconfig';
18218:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
18219:     my (@allpos,%newsettings,%changedprivs,$newrole);
18220:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
18221:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
18222:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
18223:     my %lt = &Apache::lonlocal::texthash(
18224:                     s      => 'system',
18225:                     d      => 'domain',
18226:                     order  => 'Display order',
18227:                     access => 'Role usage',
18228:                     all    => 'All with domain helpdesk or helpdesk assistant role',
18229:                     dh     => 'All with domain helpdesk role',
18230:                     da     => 'All with domain helpdesk assistant role',
18231:                     none   => 'None',
18232:                     status => 'Determined based on institutional status',
18233:                     inc    => 'Include all, but exclude specific personnel',
18234:                     exc    => 'Exclude all, but include specific personnel',
18235:     );
18236:     for (my $num=0; $num<=$maxnum; $num++) {
18237:         my ($prefix,$identifier,$rolename,%curr);
18238:         if ($num == $maxnum) {
18239:             next unless ($env{'form.newcusthelp'} == $maxnum);
18240:             $identifier = 'custhelp'.$num;
18241:             $prefix = 'helproles_'.$num;
18242:             $rolename = $env{'form.custhelpname'.$num};
18243:             $rolename=~s/[^A-Za-z0-9]//gs;
18244:             next if ($rolename eq '');
18245:             next if (exists($existing{'rolesdef_'.$rolename}));
18246:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
18247:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
18248:                                                      $newprivs{'c'},$confname,$dom);
18249:             if ($result ne 'ok') {
18250:                 $errors .= '<li><span class="LC_error">'.
18251:                            &mt('An error occurred storing the new custom role: [_1]',
18252:                            $result).'</span></li>';
18253:                 next;
18254:             } else {
18255:                 $changedprivs{$rolename} = \%newprivs;
18256:                 $newrole = $rolename;
18257:             }
18258:         } else {
18259:             $prefix = 'helproles_'.$num;
18260:             $rolename = $env{'form.'.$prefix};
18261:             next if ($rolename eq '');
18262:             next unless (exists($existing{'rolesdef_'.$rolename}));
18263:             $identifier = 'custhelp'.$num;
18264:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
18265:             my %currprivs;
18266:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
18267:                 split(/\_/,$existing{'rolesdef_'.$rolename});
18268:             foreach my $level ('c','d','s') {
18269:                 if ($newprivs{$level} ne $currprivs{$level}) {
18270:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
18271:                                                              $newprivs{'c'},$confname,$dom);
18272:                     if ($result ne 'ok') {
18273:                         $errors .= '<li><span class="LC_error">'.
18274:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
18275:                                        $rolename,$result).'</span></li>';
18276:                     } else {
18277:                         $changedprivs{$rolename} = \%newprivs;
18278:                     }
18279:                     last;
18280:                 }
18281:             }
18282:             if (ref($current{'adhoc'}) eq 'HASH') {
18283:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
18284:                     %curr = %{$current{'adhoc'}{$rolename}};
18285:                 }
18286:             }
18287:         }
18288:         my $newpos = $env{'form.'.$prefix.'_pos'};
18289:         $newpos =~ s/\D+//g;
18290:         $allpos[$newpos] = $rolename;
18291:         my $newdesc = $env{'form.'.$prefix.'_desc'};
18292:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
18293:         if ($curr{'desc'}) {
18294:             if ($curr{'desc'} ne $newdesc) {
18295:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
18296:                 $newsettings{$rolename}{'desc'} = $newdesc;
18297:             }
18298:         } elsif ($newdesc ne '') {
18299:             $changes{'customrole'}{$rolename}{'desc'} = 1;
18300:             $newsettings{$rolename}{'desc'} = $newdesc;
18301:         }
18302:         my $access = $env{'form.'.$prefix.'_access'};
18303:         if (grep(/^\Q$access\E$/,@accesstypes)) {
18304:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
18305:             if ($access eq 'status') {
18306:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
18307:                 if (scalar(@statuses) == 0) {
18308:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
18309:                 } else {
18310:                     my (@shownstatus,$numtypes);
18311:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
18312:                     if (ref($types) eq 'ARRAY') {
18313:                         $numtypes = scalar(@{$types});
18314:                         foreach my $type (sort(@statuses)) {
18315:                             if ($type eq 'default') {
18316:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
18317:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
18318:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
18319:                                 push(@shownstatus,$usertypes->{$type});
18320:                             }
18321:                         }
18322:                     }
18323:                     if (grep(/^default$/,@statuses)) {
18324:                         push(@shownstatus,$othertitle);
18325:                     }
18326:                     if (scalar(@shownstatus) == 1+$numtypes) {
18327:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
18328:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
18329:                     } else {
18330:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
18331:                         if (ref($curr{'status'}) eq 'ARRAY') {
18332:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
18333:                             if (@diffs) {
18334:                                 $changes{'customrole'}{$rolename}{$access} = 1;
18335:                             }
18336:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
18337:                             $changes{'customrole'}{$rolename}{$access} = 1;
18338:                         }
18339:                     }
18340:                 }
18341:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
18342:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
18343:                 my @newspecstaff;
18344:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
18345:                 foreach my $person (sort(@personnel)) {
18346:                     if ($domhelpdesk{$person}) {
18347:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
18348:                     }
18349:                 }
18350:                 if (ref($curr{$access}) eq 'ARRAY') {
18351:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
18352:                     if (@diffs) {
18353:                         $changes{'customrole'}{$rolename}{$access} = 1;
18354:                     }
18355:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
18356:                     $changes{'customrole'}{$rolename}{$access} = 1;
18357:                 }
18358:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
18359:                     my ($uname,$udom) = split(/:/,$person);
18360:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
18361:                 }
18362:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
18363:             }
18364:         } else {
18365:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
18366:         }
18367:         unless ($curr{'access'} eq $access) {
18368:             $changes{'customrole'}{$rolename}{'access'} = 1;
18369:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
18370:         }
18371:     }
18372:     if (@allpos > 0) {
18373:         my $idx = 0;
18374:         foreach my $rolename (@allpos) {
18375:             if ($rolename ne '') {
18376:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
18377:                 if (ref($current{'adhoc'}) eq 'HASH') {
18378:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
18379:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
18380:                             $changes{'customrole'}{$rolename}{'order'} = 1;
18381:                             $newsettings{$rolename}{'order'} = $idx+1;
18382:                         }
18383:                     }
18384:                 }
18385:                 $idx ++;
18386:             }
18387:         }
18388:     }
18389:     my $putresult;
18390:     if (keys(%changes) > 0) {
18391:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
18392:         if ($putresult eq 'ok') {
18393:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
18394:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
18395:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
18396:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
18397:                 }
18398:             }
18399:             my $cachetime = 24*60*60;
18400:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18401:             if (ref($lastactref) eq 'HASH') {
18402:                 $lastactref->{'domdefaults'} = 1;
18403:             }
18404:         } else {
18405:             $errors .= '<li><span class="LC_error">'.
18406:                        &mt('An error occurred storing the settings: [_1]',
18407:                            $putresult).'</span></li>';
18408:         }
18409:     }
18410:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
18411:         $resulttext = &mt('Changes made:').'<ul>';
18412:         my (%shownprivs,@levelorder);
18413:         @levelorder = ('c','d','s');
18414:         if ((keys(%changes)) && ($putresult eq 'ok')) {
18415:             foreach my $item (sort(keys(%changes))) {
18416:                 if ($item eq 'submitbugs') {
18417:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
18418:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
18419:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
18420:                 } elsif ($item eq 'customrole') {
18421:                     if (ref($changes{'customrole'}) eq 'HASH') {
18422:                         my @keyorder = ('order','desc','access','status','exc','inc');
18423:                         my %keytext = &Apache::lonlocal::texthash(
18424:                                                                    order  => 'Order',
18425:                                                                    desc   => 'Role description',
18426:                                                                    access => 'Role usage',
18427:                                                                    status => 'Allowed institutional types',
18428:                                                                    exc    => 'Allowed personnel',
18429:                                                                    inc    => 'Disallowed personnel',
18430:                         );
18431:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
18432:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
18433:                                 if ($role eq $newrole) {
18434:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
18435:                                                               $role).'<ul>';
18436:                                 } else {
18437:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
18438:                                                               $role).'<ul>';
18439:                                 }
18440:                                 foreach my $key (@keyorder) {
18441:                                     if ($changes{'customrole'}{$role}{$key}) {
18442:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
18443:                                                                   $keytext{$key},$newsettings{$role}{$key}).
18444:                                                        '</li>';
18445:                                     }
18446:                                 }
18447:                                 if (ref($changedprivs{$role}) eq 'HASH') {
18448:                                     $shownprivs{$role} = 1;
18449:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
18450:                                     foreach my $level (@levelorder) {
18451:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
18452:                                             next if ($item eq '');
18453:                                             my ($priv) = split(/\&/,$item,2);
18454:                                             if (&Apache::lonnet::plaintext($priv)) {
18455:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
18456:                                                 unless ($level eq 'c') {
18457:                                                     $resulttext .= ' ('.$lt{$level}.')';
18458:                                                 }
18459:                                                 $resulttext .= '</li>';
18460:                                             }
18461:                                         }
18462:                                     }
18463:                                     $resulttext .= '</ul>';
18464:                                 }
18465:                                 $resulttext .= '</ul></li>';
18466:                             }
18467:                         }
18468:                     }
18469:                 }
18470:             }
18471:         }
18472:         if (keys(%changedprivs)) {
18473:             foreach my $role (sort(keys(%changedprivs))) {
18474:                 unless ($shownprivs{$role}) {
18475:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
18476:                                               $role).'<ul>'.
18477:                                    '<li>'.&mt('Privileges set to :').'<ul>';
18478:                     foreach my $level (@levelorder) {
18479:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
18480:                             next if ($item eq '');
18481:                             my ($priv) = split(/\&/,$item,2);
18482:                             if (&Apache::lonnet::plaintext($priv)) {
18483:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
18484:                                 unless ($level eq 'c') {
18485:                                     $resulttext .= ' ('.$lt{$level}.')';
18486:                                 }
18487:                                 $resulttext .= '</li>';
18488:                             }
18489:                         }
18490:                     }
18491:                     $resulttext .= '</ul></li></ul></li>';
18492:                 }
18493:             }
18494:         }
18495:         $resulttext .= '</ul>';
18496:     } else {
18497:         $resulttext = &mt('No changes made to help settings');
18498:     }
18499:     if ($errors) {
18500:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
18501:                                     $errors.'</ul>';
18502:     }
18503:     return $resulttext;
18504: }
18505: 
18506: sub modify_coursedefaults {
18507:     my ($dom,$lastactref,%domconfig) = @_;
18508:     my ($resulttext,$errors,%changes,%defaultshash);
18509:     my %defaultchecked = (
18510:                            'uselcmath'       => 'on',
18511:                            'usejsme'         => 'on',
18512:                            'inline_chem'     => 'on',
18513:                            'ltiauth'         => 'off',
18514:                          );
18515:     my @toggles = ('uselcmath','usejsme','inline_chem','ltiauth');
18516:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
18517:                    'uploadquota_community','uploadquota_textbook','coursequota_official',
18518:                    'coursequota_unofficial','coursequota_community','coursequota_textbook',
18519:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
18520:                    'mysqltables_textbook');
18521:     my @types = ('official','unofficial','community','textbook');
18522:     my %staticdefaults = (
18523:                            anonsurvey_threshold => 10,
18524:                            uploadquota          => 500,
18525:                            coursequota          => 20,
18526:                            postsubmit           => 60,
18527:                            mysqltables          => 172800,
18528:                            domexttool           => 1,
18529:                          );
18530:     my %texoptions = (
18531:                         MathJax  => 'MathJax',
18532:                         mimetex  => &mt('Convert to Images'),
18533:                         tth      => &mt('TeX to HTML'),
18534:                      );
18535:     $defaultshash{'coursedefaults'} = {};
18536: 
18537:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
18538:         if ($domconfig{'coursedefaults'} eq '') {
18539:             $domconfig{'coursedefaults'} = {};
18540:         }
18541:     }
18542: 
18543:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
18544:         foreach my $item (@toggles) {
18545:             if ($defaultchecked{$item} eq 'on') {
18546:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
18547:                     ($env{'form.'.$item} eq '0')) {
18548:                     $changes{$item} = 1;
18549:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
18550:                     $changes{$item} = 1;
18551:                 }
18552:             } elsif ($defaultchecked{$item} eq 'off') {
18553:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
18554:                     ($env{'form.'.$item} eq '1')) {
18555:                     $changes{$item} = 1;
18556:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
18557:                     $changes{$item} = 1;
18558:                 }
18559:             }
18560:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
18561:         }
18562:         foreach my $item (@numbers) {
18563:             my ($currdef,$newdef);
18564:             $newdef = $env{'form.'.$item};
18565:             if ($item eq 'anonsurvey_threshold') {
18566:                 $currdef = $domconfig{'coursedefaults'}{$item};
18567:                 $newdef =~ s/\D//g;
18568:                 if ($newdef eq '' || $newdef < 1) {
18569:                     $newdef = 1;
18570:                 }
18571:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
18572:             } else {
18573:                 my ($setting,$type) = ($item =~ /^(uploadquota|coursequota|mysqltables)_(\w+)$/);
18574:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
18575:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
18576:                 }
18577:                 $newdef =~ s/[^\w.\-]//g;
18578:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
18579:             }
18580:             if ($currdef ne $newdef) {
18581:                 if ($item eq 'anonsurvey_threshold') {
18582:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
18583:                         $changes{$item} = 1;
18584:                     }
18585:                 } elsif ($item =~ /^(uploadquota|coursequota|mysqltables)_/) {
18586:                     my $setting = $1;
18587:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
18588:                         $changes{$setting} = 1;
18589:                     }
18590:                 }
18591:             }
18592:         }
18593:         my $texengine;
18594:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
18595:             $texengine = $env{'form.texengine'};
18596:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
18597:             if ($currdef eq '') {
18598:                 unless ($texengine eq $Apache::lonnet::deftex) {
18599:                     $changes{'texengine'} = 1;
18600:                 }
18601:             } elsif ($currdef ne $texengine) {
18602:                 $changes{'texengine'} = 1;
18603:             }
18604:         }
18605:         if ($texengine ne '') {
18606:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
18607:         }
18608:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
18609:         my @currclonecode;
18610:         if (ref($currclone) eq 'HASH') {
18611:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
18612:                 @currclonecode = @{$currclone->{'instcode'}};
18613:             }
18614:         }
18615:         my $newclone;
18616:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
18617:             $newclone = $env{'form.canclone'};
18618:         }
18619:         if ($newclone eq 'instcode') {
18620:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
18621:             my (%codedefaults,@code_order,@clonecode);
18622:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
18623:                                                     \@code_order);
18624:             foreach my $item (@code_order) {
18625:                 if (grep(/^\Q$item\E$/,@newcodes)) {
18626:                     push(@clonecode,$item);
18627:                 }
18628:             }
18629:             if (@clonecode) {
18630:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
18631:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
18632:                 if (@diffs) {
18633:                     $changes{'canclone'} = 1;
18634:                 }
18635:             } else {
18636:                 $newclone eq '';
18637:             }
18638:         } elsif ($newclone ne '') {
18639:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
18640:         }
18641:         if ($newclone ne $currclone) {
18642:             $changes{'canclone'} = 1;
18643:         }
18644:         my %credits;
18645:         foreach my $type (@types) {
18646:             unless ($type eq 'community') {
18647:                 $credits{$type} = $env{'form.'.$type.'_credits'};
18648:                 $credits{$type} =~ s/[^\d.]+//g;
18649:             }
18650:         }
18651:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
18652:             ($env{'form.coursecredits'} eq '1')) {
18653:             $changes{'coursecredits'} = 1;
18654:             foreach my $type (keys(%credits)) {
18655:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
18656:             }
18657:         } else {
18658:             if ($env{'form.coursecredits'} eq '1') {
18659:                 foreach my $type (@types) {
18660:                     unless ($type eq 'community') {
18661:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
18662:                             $changes{'coursecredits'} = 1;
18663:                         }
18664:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
18665:                     }
18666:                 }
18667:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
18668:                 foreach my $type (@types) {
18669:                     unless ($type eq 'community') {
18670:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
18671:                             $changes{'coursecredits'} = 1;
18672:                             last;
18673:                         }
18674:                     }
18675:                 }
18676:             }
18677:         }
18678:         if ($env{'form.postsubmit'} eq '1') {
18679:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
18680:             my %currtimeout;
18681:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
18682:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
18683:                     $changes{'postsubmit'} = 1;
18684:                 }
18685:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
18686:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
18687:                 }
18688:             } else {
18689:                 $changes{'postsubmit'} = 1;
18690:             }
18691:             foreach my $type (@types) {
18692:                 my $timeout = $env{'form.'.$type.'_timeout'};
18693:                 $timeout =~ s/\D//g;
18694:                 if ($timeout == $staticdefaults{'postsubmit'}) {
18695:                     $timeout = '';
18696:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
18697:                     $timeout = '0';
18698:                 }
18699:                 unless ($timeout eq '') {
18700:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
18701:                 }
18702:                 if (exists($currtimeout{$type})) {
18703:                     if ($timeout ne $currtimeout{$type}) {
18704:                         $changes{'postsubmit'} = 1;
18705:                     }
18706:                 } elsif ($timeout ne '') {
18707:                     $changes{'postsubmit'} = 1;
18708:                 }
18709:             }
18710:         } else {
18711:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
18712:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
18713:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
18714:                     $changes{'postsubmit'} = 1;
18715:                 }
18716:             } else {
18717:                 $changes{'postsubmit'} = 1;
18718:             }
18719:         }
18720:         my (%newdomexttool,%newexttool,%olddomexttool,%oldexttool);
18721:         map { $newdomexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.domexttool');
18722:         map { $newexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.exttool');
18723:         if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
18724:             %olddomexttool = %{$domconfig{'coursedefaults'}{'domexttool'}};
18725:         } else {
18726:            foreach my $type (@types) {
18727:                if ($staticdefaults{'domexttool'}) {
18728:                    $olddomexttool{$type} = 1;
18729:                } else {
18730:                    $olddomexttool{$type} = 0;
18731:                }
18732:             }
18733:         }
18734:         if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
18735:             %oldexttool = %{$domconfig{'coursedefaults'}{'exttool'}};
18736:         } else {
18737:             foreach my $type (@types) {
18738:                if ($staticdefaults{'exttool'}) {
18739:                    $oldexttool{$type} = 1;
18740:                } else {
18741:                    $oldexttool{$type} = 0;
18742:                }
18743:             }
18744:         }
18745:         foreach my $type (@types) {
18746:             unless ($newdomexttool{$type}) {
18747:                 $newdomexttool{$type} = 0;
18748:             }
18749:             unless ($newexttool{$type}) {
18750:                 $newexttool{$type} = 0;
18751:             }
18752:             if ($newdomexttool{$type} != $olddomexttool{$type}) {
18753:                 $changes{'domexttool'} = 1;
18754:             }
18755:             if ($newexttool{$type} != $oldexttool{$type}) {
18756:                 $changes{'exttool'} = 1;
18757:             }
18758:         }
18759:         $defaultshash{'coursedefaults'}{'domexttool'} = \%newdomexttool;
18760:         $defaultshash{'coursedefaults'}{'exttool'} = \%newexttool;
18761:     }
18762:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
18763:                                              $dom);
18764:     if ($putresult eq 'ok') {
18765:         if (keys(%changes) > 0) {
18766:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18767:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
18768:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
18769:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'}) ||
18770:                 ($changes{'inline_chem'}) || ($changes{'ltiauth'}) || ($changes{'domexttool'}) ||
18771:                 ($changes{'exttool'}) || ($changes{'coursequota'})) {
18772:                 foreach my $item ('uselcmath','usejsme','inline_chem','texengine','ltiauth') {
18773:                     if ($changes{$item}) {
18774:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
18775:                     }
18776:                 }
18777:                 if ($changes{'coursecredits'}) {
18778:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
18779:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
18780:                             $domdefaults{$type.'credits'} =
18781:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
18782:                         }
18783:                     }
18784:                 }
18785:                 if ($changes{'postsubmit'}) {
18786:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
18787:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
18788:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
18789:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
18790:                                 $domdefaults{$type.'postsubtimeout'} =
18791:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
18792:                             }
18793:                         }
18794:                     }
18795:                 }
18796:                 if ($changes{'uploadquota'}) {
18797:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
18798:                         foreach my $type (@types) {
18799:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
18800:                         }
18801:                     }
18802:                 }
18803:                 if ($changes{'coursequota'}) {
18804:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
18805:                         foreach my $type (@types) {
18806:                             $domdefaults{$type.'coursequota'}=$defaultshash{'coursedefaults'}{'coursequota'}{$type};
18807:                         }
18808:                     }
18809:                 }
18810:                 if ($changes{'canclone'}) {
18811:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
18812:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
18813:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
18814:                             if (@clonecodes) {
18815:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
18816:                             }
18817:                         }
18818:                     } else {
18819:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
18820:                     }
18821:                 }
18822:                 if ($changes{'domexttool'}) {
18823:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
18824:                         foreach my $type (@types) {
18825:                             $domdefaults{$type.'domexttool'}=$defaultshash{'coursedefaults'}{'domexttool'}{$type};
18826:                         }
18827:                     }
18828:                 }
18829:                 if ($changes{'exttool'}) {
18830:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
18831:                         foreach my $type (@types) {
18832:                             $domdefaults{$type.'exttool'}=$defaultshash{'coursedefaults'}{'exttool'}{$type};
18833:                         }
18834:                     }
18835:                 }
18836:                 my $cachetime = 24*60*60;
18837:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18838:                 if (ref($lastactref) eq 'HASH') {
18839:                     $lastactref->{'domdefaults'} = 1;
18840:                 }
18841:             }
18842:             $resulttext = &mt('Changes made:').'<ul>';
18843:             foreach my $item (sort(keys(%changes))) {
18844:                 if ($item eq 'uselcmath') {
18845:                     if ($env{'form.'.$item} eq '1') {
18846:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
18847:                     } else {
18848:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
18849:                     }
18850:                 } elsif ($item eq 'usejsme') {
18851:                     if ($env{'form.'.$item} eq '1') {
18852:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
18853:                     } else {
18854:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
18855:                     }
18856:                 } elsif ($item eq 'inline_chem') {
18857:                     if ($env{'form.'.$item} eq '1') {
18858:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses inline previewer').'</li>';
18859:                     } else {
18860:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses pop-up previewer').'</li>';
18861:                     }
18862:                 } elsif ($item eq 'texengine') {
18863:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
18864:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
18865:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
18866:                     }
18867:                 } elsif ($item eq 'anonsurvey_threshold') {
18868:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
18869:                 } elsif ($item eq 'uploadquota') {
18870:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
18871:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
18872:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
18873:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
18874:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
18875:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
18876:                                        '</ul>'.
18877:                                        '</li>';
18878:                     } else {
18879:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
18880:                     }
18881:                 } elsif ($item eq 'coursequota') {
18882:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
18883:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course set as follows:').'<ul>'.
18884:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'official'}.'</b>').'</li>'.
18885:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'unofficial'}.'</b>').'</li>'.
18886:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'textbook'}.'</b>').'</li>'.
18887:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'community'}.'</b>').'</li>'.
18888:                                        '</ul>'.
18889:                                        '</li>';
18890:                     } else {
18891:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course remains default: [_1] MB',$staticdefaults{'coursequota'}).'</li>';
18892:                     }
18893:                 } elsif ($item eq 'mysqltables') {
18894:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
18895:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
18896:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
18897:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
18898:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
18899:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
18900:                                        '</ul>'.
18901:                                        '</li>';
18902:                     } else {
18903:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
18904:                     }
18905:                 } elsif ($item eq 'postsubmit') {
18906:                     if ($domdefaults{'postsubmit'} eq 'off') {
18907:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
18908:                     } else {
18909:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
18910:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
18911:                             $resulttext .= &mt('durations:').'<ul>';
18912:                             foreach my $type (@types) {
18913:                                 $resulttext .= '<li>';
18914:                                 my $timeout;
18915:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
18916:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
18917:                                 }
18918:                                 my $display;
18919:                                 if ($timeout eq '0') {
18920:                                     $display = &mt('unlimited');
18921:                                 } elsif ($timeout eq '') {
18922:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
18923:                                 } else {
18924:                                     $display = &mt('[quant,_1,second]',$timeout);
18925:                                 }
18926:                                 if ($type eq 'community') {
18927:                                     $resulttext .= &mt('Communities');
18928:                                 } elsif ($type eq 'official') {
18929:                                     $resulttext .= &mt('Official courses');
18930:                                 } elsif ($type eq 'unofficial') {
18931:                                     $resulttext .= &mt('Unofficial courses');
18932:                                 } elsif ($type eq 'textbook') {
18933:                                     $resulttext .= &mt('Textbook courses');
18934:                                 }
18935:                                 $resulttext .= ' -- '.$display.'</li>';
18936:                             }
18937:                             $resulttext .= '</ul>';
18938:                         }
18939:                         $resulttext .= '</li>';
18940:                     }
18941:                 } elsif ($item eq 'coursecredits') {
18942:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
18943:                         if (($domdefaults{'officialcredits'} eq '') &&
18944:                             ($domdefaults{'unofficialcredits'} eq '') &&
18945:                             ($domdefaults{'textbookcredits'} eq '')) {
18946:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
18947:                         } else {
18948:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
18949:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
18950:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
18951:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
18952:                                            '</ul>'.
18953:                                            '</li>';
18954:                         }
18955:                     } else {
18956:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
18957:                     }
18958:                 } elsif ($item eq 'canclone') {
18959:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
18960:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
18961:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
18962:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
18963:                         }
18964:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
18965:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
18966:                     } else {
18967:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
18968:                     }
18969:                 } elsif ($item eq 'ltiauth') {
18970:                     if ($env{'form.'.$item} eq '1') {
18971:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL need not require re-authentication').'</li>';
18972:                     } else {
18973:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL will require re-authentication').'</li>';
18974:                     }
18975:                 } elsif ($item eq 'domexttool') {
18976:                     my @noyes = (&mt('no'),&mt('yes'));
18977:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
18978:                         $resulttext .= '<li>'.&mt('External Tools defined in the domain may be used as follows:').'<ul>'.
18979:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'official'}].'</b>').'</li>'.
18980:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'unofficial'}].'</b>').'</li>'.
18981:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'textbook'}].'</b>').'</li>'.
18982:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'placement'}].'</b>').'</li>'.
18983:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'community'}].'</b>').'</li>'.
18984:                                        '</ul>'.
18985:                                        '</li>';
18986:                     } else {
18987:                         $resulttext .= '<li>'.&mt('External Tools defined in the domain may be used in all course types, by default').'</li>';
18988:                     }
18989:                 } elsif ($item eq 'exttool') {
18990:                     my @noyes = (&mt('no'),&mt('yes'));
18991:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
18992:                         $resulttext .= '<li>'.&mt('External Tools can be defined and configured in course containers as follows:').'<ul>'.
18993:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'official'}].'</b>').'</li>'.
18994:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'unofficial'}].'</b>').'</li>'.
18995:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'textbook'}].'</b>').'</li>'.
18996:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'placement'}].'</b>').'</li>'.
18997:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'community'}].'</b>').'</li>'.
18998:                                        '</ul>'.
18999:                                        '</li>';
19000:                     } else {
19001:                         $resulttext .= '<li>'.&mt('External Tools can not be defined in any course types, by default').'</li>';
19002:                     }
19003:                 }
19004:             }
19005:             $resulttext .= '</ul>';
19006:         } else {
19007:             $resulttext = &mt('No changes made to course defaults');
19008:         }
19009:     } else {
19010:         $resulttext = '<span class="LC_error">'.
19011:             &mt('An error occurred: [_1]',$putresult).'</span>';
19012:     }
19013:     return $resulttext;
19014: }
19015: 
19016: sub modify_selfenrollment {
19017:     my ($dom,$lastactref,%domconfig) = @_;
19018:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
19019:     my @types = ('official','unofficial','community','textbook');
19020:     my %titles = &tool_titles();
19021:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
19022:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
19023:     $ordered{'default'} = ['types','registered','approval','limit'];
19024: 
19025:     my (%roles,%shown,%toplevel);
19026:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
19027: 
19028:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
19029:         if ($domconfig{'selfenrollment'} eq '') {
19030:             $domconfig{'selfenrollment'} = {};
19031:         }
19032:     }
19033:     %toplevel = (
19034:                   admin      => 'Configuration Rights',
19035:                   default    => 'Default settings',
19036:                   validation => 'Validation of self-enrollment requests',
19037:                 );
19038:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
19039: 
19040:     if (ref($ordered{'admin'}) eq 'ARRAY') {
19041:         foreach my $item (@{$ordered{'admin'}}) {
19042:             foreach my $type (@types) {
19043:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
19044:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
19045:                 } else {
19046:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
19047:                 }
19048:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
19049:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
19050:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
19051:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
19052:                             push(@{$changes{'admin'}{$type}},$item);
19053:                         }
19054:                     } else {
19055:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
19056:                             push(@{$changes{'admin'}{$type}},$item);
19057:                         }
19058:                     }
19059:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
19060:                     push(@{$changes{'admin'}{$type}},$item);
19061:                 }
19062:             }
19063:         }
19064:     }
19065: 
19066:     foreach my $item (@{$ordered{'default'}}) {
19067:         foreach my $type (@types) {
19068:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
19069:             if ($item eq 'types') {
19070:                 unless (($value eq 'all') || ($value eq 'dom')) {
19071:                     $value = '';
19072:                 }
19073:             } elsif ($item eq 'registered') {
19074:                 unless ($value eq '1') {
19075:                     $value = 0;
19076:                 }
19077:             } elsif ($item eq 'approval') {
19078:                 unless ($value =~ /^[012]$/) {
19079:                     $value = 0;
19080:                 }
19081:             } else {
19082:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
19083:                     $value = 'none';
19084:                 }
19085:             }
19086:             $selfenrollhash{'default'}{$type}{$item} = $value;
19087:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
19088:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
19089:                     if ($selfenrollhash{'default'}{$type}{$item} ne
19090:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
19091:                          push(@{$changes{'default'}{$type}},$item);
19092:                     }
19093:                 } else {
19094:                     push(@{$changes{'default'}{$type}},$item);
19095:                 }
19096:             } else {
19097:                 push(@{$changes{'default'}{$type}},$item);
19098:             }
19099:             if ($item eq 'limit') {
19100:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
19101:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
19102:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
19103:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
19104:                     }
19105:                 } else {
19106:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
19107:                 }
19108:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
19109:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
19110:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
19111:                          push(@{$changes{'default'}{$type}},'cap');
19112:                     }
19113:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
19114:                     push(@{$changes{'default'}{$type}},'cap');
19115:                 }
19116:             }
19117:         }
19118:     }
19119: 
19120:     foreach my $item (@{$itemsref}) {
19121:         if ($item eq 'fields') {
19122:             my @changed;
19123:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
19124:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
19125:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
19126:             }
19127:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
19128:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
19129:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
19130:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
19131:                 } else {
19132:                     @changed = @{$selfenrollhash{'validation'}{$item}};
19133:                 }
19134:             } else {
19135:                 @changed = @{$selfenrollhash{'validation'}{$item}};
19136:             }
19137:             if (@changed) {
19138:                 if ($selfenrollhash{'validation'}{$item}) { 
19139:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
19140:                 } else {
19141:                     $changes{'validation'}{$item} = &mt('None');
19142:                 }
19143:             }
19144:         } else {
19145:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
19146:             if ($item eq 'markup') {
19147:                if ($env{'form.selfenroll_validation_'.$item}) {
19148:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
19149:                }
19150:             }
19151:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
19152:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
19153:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
19154:                 }
19155:             }
19156:         }
19157:     }
19158: 
19159:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
19160:                                              $dom);
19161:     if ($putresult eq 'ok') {
19162:         if (keys(%changes) > 0) {
19163:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19164:             $resulttext = &mt('Changes made:').'<ul>';
19165:             foreach my $key ('admin','default','validation') {
19166:                 if (ref($changes{$key}) eq 'HASH') {
19167:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
19168:                     if ($key eq 'validation') {
19169:                         foreach my $item (@{$itemsref}) {
19170:                             if (exists($changes{$key}{$item})) {
19171:                                 if ($item eq 'markup') {
19172:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
19173:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
19174:                                 } else {  
19175:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
19176:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
19177:                                 }
19178:                             }
19179:                         }
19180:                     } else {
19181:                         foreach my $type (@types) {
19182:                             if ($type eq 'community') {
19183:                                 $roles{'1'} = &mt('Community personnel');
19184:                             } else {
19185:                                 $roles{'1'} = &mt('Course personnel');
19186:                             }
19187:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
19188:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
19189:                                     if ($key eq 'admin') {
19190:                                         my @mgrdc = ();
19191:                                         if (ref($ordered{$key}) eq 'ARRAY') {
19192:                                             foreach my $item (@{$ordered{'admin'}}) {
19193:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
19194:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
19195:                                                         push(@mgrdc,$item);
19196:                                                     }
19197:                                                 }
19198:                                             }
19199:                                             if (@mgrdc) {
19200:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
19201:                                             } else {
19202:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
19203:                                             }
19204:                                         }
19205:                                     } else {
19206:                                         if (ref($ordered{$key}) eq 'ARRAY') {
19207:                                             foreach my $item (@{$ordered{$key}}) {
19208:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
19209:                                                     $domdefaults{$type.'selfenroll'.$item} =
19210:                                                         $selfenrollhash{$key}{$type}{$item};
19211:                                                 }
19212:                                             }
19213:                                         }
19214:                                     }
19215:                                 }
19216:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
19217:                                 foreach my $item (@{$ordered{$key}}) {
19218:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
19219:                                         $resulttext .= '<li>';
19220:                                         if ($key eq 'admin') {
19221:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
19222:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
19223:                                         } else {
19224:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
19225:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
19226:                                         }
19227:                                         $resulttext .= '</li>';
19228:                                     }
19229:                                 }
19230:                                 $resulttext .= '</ul></li>';
19231:                             }
19232:                         }
19233:                         $resulttext .= '</ul></li>'; 
19234:                     }
19235:                 }
19236:             }
19237:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
19238:                 my $cachetime = 24*60*60;
19239:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19240:                 if (ref($lastactref) eq 'HASH') {
19241:                     $lastactref->{'domdefaults'} = 1;
19242:                 }
19243:             }
19244:             $resulttext .= '</ul>';
19245:         } else {
19246:             $resulttext = &mt('No changes made to self-enrollment settings');
19247:         }
19248:     } else {
19249:         $resulttext = '<span class="LC_error">'.
19250:             &mt('An error occurred: [_1]',$putresult).'</span>';
19251:     }
19252:     return $resulttext;
19253: }
19254: 
19255: sub modify_wafproxy {
19256:     my ($dom,$action,$lastactref,%domconfig) = @_;
19257:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
19258:     my (%othercontrol,%canset,%values,%curralias,%currsaml,%currvalue,@warnings,
19259:         %wafproxy,%changes,%expirecache,%expiresaml);
19260:     foreach my $server (sort(keys(%servers))) {
19261:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
19262:         if ($serverhome eq $server) {
19263:             my $serverdom = &Apache::lonnet::host_domain($server);
19264:             if ($serverdom eq $dom) {
19265:                 $canset{$server} = 1;
19266:             }
19267:         }
19268:     }
19269:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
19270:         %{$values{$dom}} = ();
19271:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
19272:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
19273:         }
19274:         if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
19275:             %currsaml = %{$domconfig{'wafproxy'}{'saml'}};
19276:         }
19277:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
19278:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
19279:         }
19280:     }
19281:     my $output;
19282:     if (keys(%canset)) {
19283:         %{$wafproxy{'alias'}} = ();
19284:         %{$wafproxy{'saml'}} = ();
19285:         foreach my $key (sort(keys(%canset))) {
19286:             if ($env{'form.wafproxy_'.$dom}) {
19287:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
19288:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
19289:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
19290:                     $changes{'alias'} = 1;
19291:                 }
19292:                 if ($env{'form.wafproxy_alias_saml_'.$key}) {
19293:                     $wafproxy{'saml'}{$key} = 1;
19294:                 }
19295:                 if ($wafproxy{'saml'}{$key} ne $currsaml{$key}) {
19296:                     $changes{'saml'} = 1;
19297:                 }
19298:             } else {
19299:                 $wafproxy{'alias'}{$key} = '';
19300:                 $wafproxy{'saml'}{$key} = '';
19301:                 if ($curralias{$key}) {
19302:                     $changes{'alias'} = 1;
19303:                 }
19304:                 if ($currsaml{$key}) {
19305:                     $changes{'saml'} = 1;
19306:                 }
19307:             }
19308:             if ($wafproxy{'alias'}{$key} eq '') {
19309:                 if ($curralias{$key}) {
19310:                     $expirecache{$key} = 1;
19311:                 }
19312:                 delete($wafproxy{'alias'}{$key});
19313:             }
19314:             if ($wafproxy{'saml'}{$key} eq '') {
19315:                 if ($currsaml{$key}) {
19316:                     $expiresaml{$key} = 1;
19317:                 }
19318:                 delete($wafproxy{'saml'}{$key});
19319:             }
19320:         }
19321:         unless (keys(%{$wafproxy{'alias'}})) {
19322:             delete($wafproxy{'alias'});
19323:         }
19324:         unless (keys(%{$wafproxy{'saml'}})) {
19325:             delete($wafproxy{'saml'});
19326:         }
19327:         # Localization for values in %warn occurs in &mt() calls separately.
19328:         my %warn = (
19329:                      trusted => 'trusted IP range(s)',
19330:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
19331:                      vpnext => 'IP range(s) for backend WAF connections',
19332:                    );
19333:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
19334:             my $possible = $env{'form.wafproxy_'.$item};
19335:             $possible =~ s/^\s+|\s+$//g;
19336:             if ($possible ne '') {
19337:                 if ($item eq 'remoteip') {
19338:                     if ($possible =~ /^[mhn]$/) {
19339:                         $wafproxy{$item} = $possible;
19340:                     }
19341:                 } elsif ($item eq 'ipheader') {
19342:                     if ($wafproxy{'remoteip'} eq 'h') {
19343:                         $wafproxy{$item} = $possible;
19344:                     }
19345:                 } elsif ($item eq 'sslopt') {
19346:                     if ($possible =~ /^0|1$/) {
19347:                         $wafproxy{$item} = $possible;
19348:                     }
19349:                 } else {
19350:                     my (@ok,$count);
19351:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
19352:                         unless ($env{'form.wafproxy_vpnaccess'}) {
19353:                             $possible = '';
19354:                         }
19355:                     } elsif ($item eq 'trusted') {
19356:                         unless ($wafproxy{'remoteip'} eq 'h') {
19357:                             $possible = '';
19358:                         }
19359:                     }
19360:                     unless ($possible eq '') {
19361:                         $possible =~ s/[\r\n]+/\s/g;
19362:                         $possible =~ s/\s*-\s*/-/g;
19363:                         $possible =~ s/\s+/,/g;
19364:                         $possible =~ s/,+/,/g;
19365:                     }
19366:                     $count = 0;
19367:                     if ($possible ne '') {
19368:                         foreach my $poss (split(/\,/,$possible)) {
19369:                             $count ++;
19370:                             $poss = &validate_ip_pattern($poss);
19371:                             if ($poss ne '') {
19372:                                 push(@ok,$poss);
19373:                             }
19374:                         }
19375:                         my $diff = $count - scalar(@ok);
19376:                         if ($diff) {
19377:                             push(@warnings,'<li>'.
19378:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
19379:                                      $diff,$warn{$item}).
19380:                                  '</li>');
19381:                         }
19382:                         if (@ok) {
19383:                             my @cidr_list;
19384:                             foreach my $item (@ok) {
19385:                                 @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
19386:                             }
19387:                             $wafproxy{$item} = join(',',@cidr_list);
19388:                         }
19389:                     }
19390:                 }
19391:                 if ($wafproxy{$item} ne $currvalue{$item}) {
19392:                     $changes{$item} = 1;
19393:                 }
19394:             } elsif ($currvalue{$item}) {
19395:                 $changes{$item} = 1;
19396:             }
19397:         }
19398:     } else {
19399:         if (keys(%curralias)) {
19400:             $changes{'alias'} = 1;
19401:         }
19402:         if (keys(%currsaml)) {
19403:             $changes{'saml'} = 1;
19404:         }
19405:         if (keys(%currvalue)) {
19406:             foreach my $key (keys(%currvalue)) {
19407:                 $changes{$key} = 1;
19408:             }
19409:         }
19410:     }
19411:     if (keys(%changes)) {
19412:         my %defaultshash = (
19413:                               wafproxy => \%wafproxy,
19414:                            );
19415:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
19416:                                                  $dom);
19417:         if ($putresult eq 'ok') {
19418:             my $cachetime = 24*60*60;
19419:             my (%domdefaults,$updatedomdefs);
19420:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
19421:                 if ($changes{$item}) {
19422:                     unless ($updatedomdefs) {
19423:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
19424:                         $updatedomdefs = 1;
19425:                     }
19426:                     if ($wafproxy{$item}) {
19427:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
19428:                     } elsif (exists($domdefaults{'waf_'.$item})) {
19429:                         delete($domdefaults{'waf_'.$item});
19430:                     }
19431:                 }
19432:             }
19433:             if ($updatedomdefs) {
19434:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19435:                 if (ref($lastactref) eq 'HASH') {
19436:                     $lastactref->{'domdefaults'} = 1;
19437:                 }
19438:             }
19439:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
19440:                 my %updates = %expirecache;
19441:                 foreach my $key (keys(%expirecache)) {
19442:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
19443:                 }
19444:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
19445:                     my $cachetime = 24*60*60;
19446:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
19447:                         $updates{$key} = 1;
19448:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
19449:                                                       $cachetime);
19450:                     }
19451:                 }
19452:                 if (ref($lastactref) eq 'HASH') {
19453:                     $lastactref->{'proxyalias'} = \%updates;
19454:                 }
19455:             }
19456:             if ((exists($wafproxy{'saml'})) || (keys(%expiresaml))) {
19457:                 my %samlupdates = %expiresaml;
19458:                 foreach my $key (keys(%expiresaml)) {
19459:                     &Apache::lonnet::devalidate_cache_new('proxysaml',$key);
19460:                 }
19461:                 if (ref($wafproxy{'saml'}) eq 'HASH') {
19462:                     my $cachetime = 24*60*60;
19463:                     foreach my $key (keys(%{$wafproxy{'saml'}})) {
19464:                         $samlupdates{$key} = 1;
19465:                         &Apache::lonnet::do_cache_new('proxysaml',$key,$wafproxy{'saml'}{$key},
19466:                                                       $cachetime);
19467:                     }
19468:                 }
19469:                 if (ref($lastactref) eq 'HASH') {
19470:                     $lastactref->{'proxysaml'} = \%samlupdates;
19471:                 }
19472:             }
19473:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
19474:             foreach my $item ('alias','saml','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
19475:                 if ($changes{$item}) {
19476:                     if ($item eq 'alias') {
19477:                         my $numaliased = 0;
19478:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
19479:                             my $shown;
19480:                             if (keys(%{$wafproxy{'alias'}})) {
19481:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
19482:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
19483:                                                          &Apache::lonnet::hostname($server),
19484:                                                          $wafproxy{'alias'}{$server}).'</li>';
19485:                                     $numaliased ++;
19486:                                 }
19487:                                 if ($numaliased) {
19488:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
19489:                                                           '<ul>'.$shown.'</ul>').'</li>';
19490:                                 }
19491:                             }
19492:                         }
19493:                         unless ($numaliased) {
19494:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
19495:                         }
19496:                     } elsif ($item eq 'saml') {
19497:                         my $shown;
19498:                         if (ref($wafproxy{'saml'}) eq 'HASH') {
19499:                             if (keys(%{$wafproxy{'saml'}})) {
19500:                                 $shown = join(', ',sort(keys(%{$wafproxy{'saml'}})));
19501:                             }
19502:                         }
19503:                         if ($shown) {
19504:                             $output .= '<li>'.&mt('Alias used by SSO Auth for: [_1]',
19505:                                                   $shown).'</li>';
19506:                         } else {
19507:                             $output .= '<li>'.&mt('No alias used for SSO Auth').'</li>';
19508:                         }
19509:                     } else {
19510:                         if ($item eq 'remoteip') {
19511:                             my %ip_methods = &remoteip_methods();
19512:                             if ($wafproxy{$item} =~ /^[mh]$/) {
19513:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
19514:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
19515:                             } else {
19516:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
19517:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
19518:                                                '</li>';
19519:                                 } else {
19520:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
19521:                                 }
19522:                             }
19523:                         } elsif ($item eq 'ipheader') {
19524:                             if ($wafproxy{$item}) {
19525:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
19526:                                                       $wafproxy{$item}).'</li>';
19527:                             } else {
19528:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
19529:                             }
19530:                         } elsif ($item eq 'trusted') {
19531:                             if ($wafproxy{$item}) {
19532:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
19533:                                                       $wafproxy{$item}).'</li>';
19534:                             } else {
19535:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
19536:                             }
19537:                         } elsif ($item eq 'vpnint') {
19538:                             if ($wafproxy{$item}) {
19539:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
19540:                                                        $wafproxy{$item}).'</li>';
19541:                             } else {
19542:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
19543:                             }
19544:                         } elsif ($item eq 'vpnext') {
19545:                             if ($wafproxy{$item}) {
19546:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
19547:                                                        $wafproxy{$item}).'</li>';
19548:                             } else {
19549:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
19550:                             }
19551:                         } elsif ($item eq 'sslopt') {
19552:                             if ($wafproxy{$item}) {
19553:                                 $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>';
19554:                             } else {
19555:                                 $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>';
19556:                             }
19557:                         }
19558:                     }
19559:                 }
19560:             }
19561:             $output .= '</ul>';
19562:         } else {
19563:             $output = '<span class="LC_error">'.
19564:                       &mt('An error occurred: [_1]',$putresult).'</span>';
19565:         }
19566:     } elsif (keys(%canset)) {
19567:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
19568:     }
19569:     if (@warnings) {
19570:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
19571:                        join("\n",@warnings).'</ul>';
19572:     }
19573:     return $output;
19574: }
19575: 
19576: sub validate_ip_pattern {
19577:     my ($pattern) = @_;
19578:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
19579:         my ($start,$end) = ($1,$2);
19580:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
19581:             if (($start !~ m{/}) && ($end !~ m{/})) {
19582:                 return $start.'-'.$end;
19583:             }
19584:         }
19585:     } elsif ($pattern ne '') {
19586:         $pattern = &Net::CIDR::cidrvalidate($pattern);
19587:         if ($pattern ne '') {
19588:             return $pattern;
19589:         }
19590:     }
19591:     return;
19592: }
19593: 
19594: sub modify_usersessions {
19595:     my ($dom,$lastactref,%domconfig) = @_;
19596:     my @hostingtypes = ('version','excludedomain','includedomain');
19597:     my @offloadtypes = ('primary','default');
19598:     my %types = (
19599:                   remote => \@hostingtypes,
19600:                   hosted => \@hostingtypes,
19601:                   spares => \@offloadtypes,
19602:                 );
19603:     my @prefixes = ('remote','hosted','spares');
19604:     my @lcversions = &Apache::lonnet::all_loncaparevs();
19605:     my (%by_ip,%by_location,@intdoms);
19606:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
19607:     my @locations = sort(keys(%by_location));
19608:     my (%defaultshash,%changes);
19609:     foreach my $prefix (@prefixes) {
19610:         $defaultshash{'usersessions'}{$prefix} = {};
19611:     }
19612:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19613:     my $resulttext;
19614:     my %iphost = &Apache::lonnet::get_iphost();
19615:     foreach my $prefix (@prefixes) {
19616:         next if ($prefix eq 'spares');
19617:         foreach my $type (@{$types{$prefix}}) {
19618:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
19619:             if ($type eq 'version') {
19620:                 my $value = $env{'form.'.$prefix.'_'.$type};
19621:                 my $okvalue;
19622:                 if ($value ne '') {
19623:                     if (grep(/^\Q$value\E$/,@lcversions)) {
19624:                         $okvalue = $value;
19625:                     }
19626:                 }
19627:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
19628:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
19629:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
19630:                             if ($inuse == 0) {
19631:                                 $changes{$prefix}{$type} = 1;
19632:                             } else {
19633:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
19634:                                     $changes{$prefix}{$type} = 1;
19635:                                 }
19636:                                 if ($okvalue ne '') {
19637:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
19638:                                 } 
19639:                             }
19640:                         } else {
19641:                             if (($inuse == 1) && ($okvalue ne '')) {
19642:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
19643:                                 $changes{$prefix}{$type} = 1;
19644:                             }
19645:                         }
19646:                     } else {
19647:                         if (($inuse == 1) && ($okvalue ne '')) {
19648:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
19649:                             $changes{$prefix}{$type} = 1;
19650:                         }
19651:                     }
19652:                 } else {
19653:                     if (($inuse == 1) && ($okvalue ne '')) {
19654:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
19655:                         $changes{$prefix}{$type} = 1;
19656:                     }
19657:                 }
19658:             } else {
19659:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
19660:                 my @okvals;
19661:                 foreach my $val (@vals) {
19662:                     if ($val =~ /:/) {
19663:                         my @items = split(/:/,$val);
19664:                         foreach my $item (@items) {
19665:                             if (ref($by_location{$item}) eq 'ARRAY') {
19666:                                 push(@okvals,$item);
19667:                             }
19668:                         }
19669:                     } else {
19670:                         if (ref($by_location{$val}) eq 'ARRAY') {
19671:                             push(@okvals,$val);
19672:                         }
19673:                     }
19674:                 }
19675:                 @okvals = sort(@okvals);
19676:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
19677:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
19678:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
19679:                             if ($inuse == 0) {
19680:                                 $changes{$prefix}{$type} = 1; 
19681:                             } else {
19682:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
19683:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
19684:                                 if (@changed > 0) {
19685:                                     $changes{$prefix}{$type} = 1;
19686:                                 }
19687:                             }
19688:                         } else {
19689:                             if ($inuse == 1) {
19690:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
19691:                                 $changes{$prefix}{$type} = 1;
19692:                             }
19693:                         } 
19694:                     } else {
19695:                         if ($inuse == 1) {
19696:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
19697:                             $changes{$prefix}{$type} = 1;
19698:                         }
19699:                     }
19700:                 } else {
19701:                     if ($inuse == 1) {
19702:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
19703:                         $changes{$prefix}{$type} = 1;
19704:                     }
19705:                 }
19706:             }
19707:         }
19708:     }
19709: 
19710:     my @alldoms = &Apache::lonnet::all_domains();
19711:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
19712:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
19713:     my $savespares;
19714: 
19715:     foreach my $lonhost (sort(keys(%servers))) {
19716:         my $serverhomeID =
19717:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
19718:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
19719:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
19720:         my %spareschg;
19721:         foreach my $type (@{$types{'spares'}}) {
19722:             my @okspares;
19723:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
19724:             foreach my $server (@checked) {
19725:                 if (&Apache::lonnet::hostname($server) ne '') {
19726:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
19727:                         unless (grep(/^\Q$server\E$/,@okspares)) {
19728:                             push(@okspares,$server);
19729:                         }
19730:                     }
19731:                 }
19732:             }
19733:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
19734:             my $newspare;
19735:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
19736:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
19737:                     $newspare = $new;
19738:                 }
19739:             }
19740:             my @spares;
19741:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
19742:                 @spares = sort(@okspares,$newspare);
19743:             } else {
19744:                 @spares = sort(@okspares);
19745:             }
19746:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
19747:             if (ref($spareid{$lonhost}) eq 'HASH') {
19748:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
19749:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
19750:                     if (@diffs > 0) {
19751:                         $spareschg{$type} = 1;
19752:                     }
19753:                 }
19754:             }
19755:         }
19756:         if (keys(%spareschg) > 0) {
19757:             $changes{'spares'}{$lonhost} = \%spareschg;
19758:         }
19759:     }
19760:     $defaultshash{'usersessions'}{'offloadnow'} = {};
19761:     $defaultshash{'usersessions'}{'offloadoth'} = {};
19762:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
19763:     my @okoffload;
19764:     if (@offloadnow) {
19765:         foreach my $server (@offloadnow) {
19766:             if (&Apache::lonnet::hostname($server) ne '') {
19767:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
19768:                     push(@okoffload,$server);
19769:                 }
19770:             }
19771:         }
19772:         if (@okoffload) {
19773:             foreach my $lonhost (@okoffload) {
19774:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
19775:             }
19776:         }
19777:     }
19778:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
19779:     my @okoffloadoth;
19780:     if (@offloadoth) {
19781:         foreach my $server (@offloadoth) {
19782:             if (&Apache::lonnet::hostname($server) ne '') {
19783:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
19784:                     push(@okoffloadoth,$server);
19785:                 }
19786:             }
19787:         }
19788:         if (@okoffloadoth) {
19789:             foreach my $lonhost (@okoffloadoth) {
19790:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
19791:             }
19792:         }
19793:     }
19794:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
19795:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
19796:             if (ref($changes{'spares'}) eq 'HASH') {
19797:                 if (keys(%{$changes{'spares'}}) > 0) {
19798:                     $savespares = 1;
19799:                 }
19800:             }
19801:         } else {
19802:             $savespares = 1;
19803:         }
19804:         foreach my $offload ('offloadnow','offloadoth') {
19805:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
19806:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
19807:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
19808:                         $changes{$offload} = 1;
19809:                         last;
19810:                     }
19811:                 }
19812:                 unless ($changes{$offload}) {
19813:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
19814:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
19815:                             $changes{$offload} = 1;
19816:                             last;
19817:                         }
19818:                     }
19819:                 }
19820:             } else {
19821:                 if (($offload eq 'offloadnow') && (@okoffload)) {
19822:                      $changes{'offloadnow'} = 1;
19823:                 }
19824:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
19825:                     $changes{'offloadoth'} = 1;
19826:                 }
19827:             }
19828:         }
19829:     } else {
19830:         if (@okoffload) {
19831:             $changes{'offloadnow'} = 1;
19832:         }
19833:         if (@okoffloadoth) {
19834:             $changes{'offloadoth'} = 1;
19835:         }
19836:     }
19837:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
19838:     if ((keys(%changes) > 0) || ($savespares)) {
19839:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
19840:                                                  $dom);
19841:         if ($putresult eq 'ok') {
19842:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
19843:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
19844:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
19845:                 }
19846:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
19847:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
19848:                 }
19849:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
19850:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
19851:                 }
19852:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
19853:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
19854:                 }
19855:             }
19856:             my $cachetime = 24*60*60;
19857:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19858:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
19859:             if (ref($lastactref) eq 'HASH') {
19860:                 $lastactref->{'domdefaults'} = 1;
19861:                 $lastactref->{'usersessions'} = 1;
19862:             }
19863:             if (keys(%changes) > 0) {
19864:                 my %lt = &usersession_titles();
19865:                 $resulttext = &mt('Changes made:').'<ul>';
19866:                 foreach my $prefix (@prefixes) {
19867:                     if (ref($changes{$prefix}) eq 'HASH') {
19868:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
19869:                         if ($prefix eq 'spares') {
19870:                             if (ref($changes{$prefix}) eq 'HASH') {
19871:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
19872:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
19873:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
19874:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
19875:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
19876:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
19877:                                         foreach my $type (@{$types{$prefix}}) {
19878:                                             if ($changes{$prefix}{$lonhost}{$type}) {
19879:                                                 my $offloadto = &mt('None');
19880:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
19881:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
19882:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
19883:                                                     }
19884:                                                 }
19885:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
19886:                                             }
19887:                                         }
19888:                                     }
19889:                                     $resulttext .= '</li>';
19890:                                 }
19891:                             }
19892:                         } else {
19893:                             foreach my $type (@{$types{$prefix}}) {
19894:                                 if (defined($changes{$prefix}{$type})) {
19895:                                     my $newvalue;
19896:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
19897:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
19898:                                             if ($type eq 'version') {
19899:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
19900:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
19901:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
19902:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
19903:                                                 }
19904:                                             }
19905:                                         }
19906:                                     }
19907:                                     if ($newvalue eq '') {
19908:                                         if ($type eq 'version') {
19909:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
19910:                                         } else {
19911:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
19912:                                         }
19913:                                     } else {
19914:                                         if ($type eq 'version') {
19915:                                             $newvalue .= ' '.&mt('(or later)');
19916:                                         }
19917:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
19918:                                     }
19919:                                 }
19920:                             }
19921:                         }
19922:                         $resulttext .= '</ul>';
19923:                     }
19924:                 }
19925:                 if ($changes{'offloadnow'}) {
19926:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
19927:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
19928:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
19929:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
19930:                                 $resulttext .= '<li>'.$lonhost.'</li>';
19931:                             }
19932:                             $resulttext .= '</ul>';
19933:                         } else {
19934:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
19935:                         }
19936:                     } else {
19937:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
19938:                     }
19939:                 }
19940:                 if ($changes{'offloadoth'}) {
19941:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
19942:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
19943:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
19944:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
19945:                                 $resulttext .= '<li>'.$lonhost.'</li>';
19946:                             }
19947:                             $resulttext .= '</ul>';
19948:                         } else {
19949:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
19950:                         }
19951:                     } else {
19952:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
19953:                     }
19954:                 }
19955:                 $resulttext .= '</ul>';
19956:             } else {
19957:                 $resulttext = $nochgmsg;
19958:             }
19959:         } else {
19960:             $resulttext = '<span class="LC_error">'.
19961:                           &mt('An error occurred: [_1]',$putresult).'</span>';
19962:         }
19963:     } else {
19964:         $resulttext = $nochgmsg;
19965:     }
19966:     return $resulttext;
19967: }
19968: 
19969: sub modify_loadbalancing {
19970:     my ($dom,%domconfig) = @_;
19971:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
19972:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
19973:     my ($othertitle,$usertypes,$types) =
19974:         &Apache::loncommon::sorted_inst_types($dom);
19975:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
19976:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
19977:     my @sparestypes = ('primary','default');
19978:     my %typetitles = &sparestype_titles();
19979:     my $resulttext;
19980:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
19981:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
19982:         %existing = %{$domconfig{'loadbalancing'}};
19983:     }
19984:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
19985:                               \%currtargets,\%currrules,\%currcookies);
19986:     my ($saveloadbalancing,%defaultshash,%changes);
19987:     my ($alltypes,$othertypes,$titles) =
19988:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
19989:     my %ruletitles = &offloadtype_text();
19990:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
19991:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
19992:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
19993:         if ($balancer eq '') {
19994:             next;
19995:         }
19996:         if (!exists($servers{$balancer})) {
19997:             if (exists($currbalancer{$balancer})) {
19998:                 push(@{$changes{'delete'}},$balancer);
19999:             }
20000:             next;
20001:         }
20002:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
20003:             push(@{$changes{'delete'}},$balancer);
20004:             next;
20005:         }
20006:         if (!exists($currbalancer{$balancer})) {
20007:             push(@{$changes{'add'}},$balancer);
20008:         }
20009:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
20010:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
20011:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
20012:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
20013:             $saveloadbalancing = 1;
20014:         }
20015:         foreach my $sparetype (@sparestypes) {
20016:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
20017:             my @offloadto;
20018:             foreach my $target (@targets) {
20019:                 if (($servers{$target}) && ($target ne $balancer)) {
20020:                     if ($sparetype eq 'default') {
20021:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
20022:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
20023:                         }
20024:                     }
20025:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
20026:                         push(@offloadto,$target);
20027:                     }
20028:                 }
20029:             }
20030:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
20031:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
20032:                     push(@offloadto,$balancer);
20033:                 }
20034:             }
20035:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
20036:         }
20037:         if ($env{'form.loadbalancing_cookie_'.$i}) {
20038:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
20039:             if (exists($currbalancer{$balancer})) {
20040:                 unless ($currcookies{$balancer}) {
20041:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
20042:                 }
20043:             }
20044:         } elsif (exists($currbalancer{$balancer})) {
20045:             if ($currcookies{$balancer}) {
20046:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
20047:             }
20048:         }
20049:         if (ref($currtargets{$balancer}) eq 'HASH') {
20050:             foreach my $sparetype (@sparestypes) {
20051:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
20052:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
20053:                     if (@targetdiffs > 0) {
20054:                         $changes{'curr'}{$balancer}{'targets'} = 1;
20055:                     }
20056:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20057:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20058:                         $changes{'curr'}{$balancer}{'targets'} = 1;
20059:                     }
20060:                 }
20061:             }
20062:         } else {
20063:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
20064:                 foreach my $sparetype (@sparestypes) {
20065:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20066:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20067:                             $changes{'curr'}{$balancer}{'targets'} = 1;
20068:                         }
20069:                     }
20070:                 }
20071:             }
20072:         }
20073:         my $ishomedom;
20074:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
20075:             $ishomedom = 1;
20076:         }
20077:         if (ref($alltypes) eq 'ARRAY') {
20078:             foreach my $type (@{$alltypes}) {
20079:                 my $rule;
20080:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
20081:                          (!$ishomedom)) {
20082:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
20083:                 }
20084:                 if ($rule eq 'specific') {
20085:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
20086:                     if (exists($servers{$specifiedhost})) {
20087:                         $rule = $specifiedhost;
20088:                     }
20089:                 }
20090:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
20091:                 if (ref($currrules{$balancer}) eq 'HASH') {
20092:                     if ($rule ne $currrules{$balancer}{$type}) {
20093:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
20094:                     }
20095:                 } elsif ($rule ne '') {
20096:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
20097:                 }
20098:             }
20099:         }
20100:     }
20101:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
20102:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
20103:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
20104:             $defaultshash{'loadbalancing'} = {};
20105:         }
20106:         my $putresult = &Apache::lonnet::put_dom('configuration',
20107:                                                  \%defaultshash,$dom);
20108:         if ($putresult eq 'ok') {
20109:             if (keys(%changes) > 0) {
20110:                 my %toupdate;
20111:                 if (ref($changes{'delete'}) eq 'ARRAY') {
20112:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
20113:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
20114:                         $toupdate{$balancer} = 1;
20115:                     }
20116:                 }
20117:                 if (ref($changes{'add'}) eq 'ARRAY') {
20118:                     foreach my $balancer (sort(@{$changes{'add'}})) {
20119:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
20120:                         $toupdate{$balancer} = 1;
20121:                     }
20122:                 }
20123:                 if (ref($changes{'curr'}) eq 'HASH') {
20124:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
20125:                         $toupdate{$balancer} = 1;
20126:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
20127:                             if ($changes{'curr'}{$balancer}{'targets'}) {
20128:                                 my %offloadstr;
20129:                                 foreach my $sparetype (@sparestypes) {
20130:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20131:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20132:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
20133:                                         }
20134:                                     }
20135:                                 }
20136:                                 if (keys(%offloadstr) == 0) {
20137:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
20138:                                 } else {
20139:                                     my $showoffload;
20140:                                     foreach my $sparetype (@sparestypes) {
20141:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
20142:                                         if (defined($offloadstr{$sparetype})) {
20143:                                             $showoffload .= $offloadstr{$sparetype};
20144:                                         } else {
20145:                                             $showoffload .= &mt('None');
20146:                                         }
20147:                                         $showoffload .= ('&nbsp;'x3);
20148:                                     }
20149:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
20150:                                 }
20151:                             }
20152:                         }
20153:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
20154:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
20155:                                 foreach my $type (@{$alltypes}) {
20156:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
20157:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
20158:                                         my $balancetext;
20159:                                         if ($rule eq '') {
20160:                                             $balancetext =  $ruletitles{'default'};
20161:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
20162:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
20163:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
20164:                                                 foreach my $sparetype (@sparestypes) {
20165:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20166:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
20167:                                                     }
20168:                                                 }
20169:                                                 foreach my $item (@{$alltypes}) {
20170:                                                     next if ($item =~  /^_LC_ipchange/);
20171:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
20172:                                                     if ($hasrule eq 'homeserver') {
20173:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
20174:                                                     } else {
20175:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
20176:                                                             if ($servers{$hasrule}) {
20177:                                                                 $toupdate{$hasrule} = 1;
20178:                                                             }
20179:                                                         }
20180:                                                     }
20181:                                                 }
20182:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
20183:                                                     $balancetext =  $ruletitles{$rule};
20184:                                                 } else {
20185:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
20186:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
20187:                                                     if ($receiver) {
20188:                                                         $toupdate{$receiver};
20189:                                                     }
20190:                                                 }
20191:                                             } else {
20192:                                                 $balancetext =  $ruletitles{$rule};
20193:                                             }
20194:                                         } else {
20195:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
20196:                                         }
20197:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
20198:                                     }
20199:                                 }
20200:                             }
20201:                         }
20202:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
20203:                             if ($currcookies{$balancer}) {
20204:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use disabled',
20205:                                                           $balancer).'</li>';
20206:                             } else {
20207:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
20208:                                                           $balancer).'</li>';
20209:                             }
20210:                         }
20211:                     }
20212:                 }
20213:                 if (keys(%toupdate)) {
20214:                     my %thismachine;
20215:                     my $updatedhere;
20216:                     my $cachetime = 60*60*24;
20217:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
20218:                     foreach my $lonhost (keys(%toupdate)) {
20219:                         if ($thismachine{$lonhost}) {
20220:                             unless ($updatedhere) {
20221:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
20222:                                                               $defaultshash{'loadbalancing'},
20223:                                                               $cachetime);
20224:                                 $updatedhere = 1;
20225:                             }
20226:                         } else {
20227:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
20228:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
20229:                         }
20230:                     }
20231:                 }
20232:                 if ($resulttext ne '') {
20233:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
20234:                 } else {
20235:                     $resulttext = $nochgmsg;
20236:                 }
20237:             } else {
20238:                 $resulttext = $nochgmsg;
20239:             }
20240:         } else {
20241:             $resulttext = '<span class="LC_error">'.
20242:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20243:         }
20244:     } else {
20245:         $resulttext = $nochgmsg;
20246:     }
20247:     return $resulttext;
20248: }
20249: 
20250: sub recurse_check {
20251:     my ($chkcats,$categories,$depth,$name) = @_;
20252:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
20253:         my $chg = 0;
20254:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
20255:             my $category = $chkcats->[$depth]{$name}[$j];
20256:             my $item;
20257:             if ($category eq '') {
20258:                 $chg ++;
20259:             } else {
20260:                 my $deeper = $depth + 1;
20261:                 $item = &escape($category).':'.&escape($name).':'.$depth;
20262:                 if ($chg) {
20263:                     $categories->{$item} -= $chg;
20264:                 }
20265:                 &recurse_check($chkcats,$categories,$deeper,$category);
20266:                 $deeper --;
20267:             }
20268:         }
20269:     }
20270:     return;
20271: }
20272: 
20273: sub recurse_cat_deletes {
20274:     my ($item,$coursecategories,$deletions) = @_;
20275:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
20276:     my $subdepth = $depth + 1;
20277:     if (ref($coursecategories) eq 'HASH') {
20278:         foreach my $subitem (keys(%{$coursecategories})) {
20279:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
20280:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
20281:                 delete($coursecategories->{$subitem});
20282:                 $deletions->{$subitem} = 1;
20283:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
20284:             }
20285:         }
20286:     }
20287:     return;
20288: }
20289: 
20290: sub active_dc_picker {
20291:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
20292:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
20293:     my @domcoord = keys(%domcoords);
20294:     if (keys(%currhash)) {
20295:         foreach my $dc (keys(%currhash)) {
20296:             unless (exists($domcoords{$dc})) {
20297:                 push(@domcoord,$dc);
20298:             }
20299:         }
20300:     }
20301:     @domcoord = sort(@domcoord);
20302:     my $numdcs = scalar(@domcoord);
20303:     my $rows = 0;
20304:     my $table;
20305:     if ($numdcs > 1) {
20306:         $table = '<table>';
20307:         for (my $i=0; $i<@domcoord; $i++) {
20308:             my $rem = $i%($numinrow);
20309:             if ($rem == 0) {
20310:                 if ($i > 0) {
20311:                     $table .= '</tr>';
20312:                 }
20313:                 $table .= '<tr>';
20314:                 $rows ++;
20315:             }
20316:             my $check = '';
20317:             if ($inputtype eq 'radio') {
20318:                 if (keys(%currhash) == 0) {
20319:                     if (!$i) {
20320:                         $check = ' checked="checked"';
20321:                     }
20322:                 } elsif (exists($currhash{$domcoord[$i]})) {
20323:                     $check = ' checked="checked"';
20324:                 }
20325:             } else {
20326:                 if (exists($currhash{$domcoord[$i]})) {
20327:                     $check = ' checked="checked"';
20328:                 }
20329:             }
20330:             if ($i == @domcoord - 1) {
20331:                 my $colsleft = $numinrow - $rem;
20332:                 if ($colsleft > 1) {
20333:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
20334:                 } else {
20335:                     $table .= '<td class="LC_left_item">';
20336:                 }
20337:             } else {
20338:                 $table .= '<td class="LC_left_item">';
20339:             }
20340:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
20341:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
20342:             $table .= '<span class="LC_nobreak"><label>'.
20343:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
20344:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
20345:             if ($user ne $dcname.':'.$dcdom) {
20346:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
20347:             }
20348:             $table .= '</label></span></td>';
20349:         }
20350:         $table .= '</tr></table>';
20351:     } elsif ($numdcs == 1) {
20352:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
20353:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
20354:         if ($inputtype eq 'radio') {
20355:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
20356:             if ($user ne $dcname.':'.$dcdom) {
20357:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
20358:             }
20359:         } else {
20360:             my $check;
20361:             if (exists($currhash{$domcoord[0]})) {
20362:                 $check = ' checked="checked"';
20363:             }
20364:             $table = '<span class="LC_nobreak"><label>'.
20365:                      '<input type="checkbox" name="'.$name.'" '.
20366:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
20367:             if ($user ne $dcname.':'.$dcdom) {
20368:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
20369:             }
20370:             $table .= '</label></span>';
20371:             $rows ++;
20372:         }
20373:     }
20374:     return ($numdcs,$table,$rows);
20375: }
20376: 
20377: sub usersession_titles {
20378:     return &Apache::lonlocal::texthash(
20379:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
20380:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
20381:                spares => 'Servers offloaded to, when busy',
20382:                version => 'LON-CAPA version requirement',
20383:                excludedomain => 'Allow all, but exclude specific domains',
20384:                includedomain => 'Deny all, but include specific domains',
20385:                primary => 'Primary (checked first)',
20386:                default => 'Default',
20387:            );
20388: }
20389: 
20390: sub id_for_thisdom {
20391:     my (%servers) = @_;
20392:     my %altids;
20393:     foreach my $server (keys(%servers)) {
20394:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
20395:         if ($serverhome ne $server) {
20396:             $altids{$serverhome} = $server;
20397:         }
20398:     }
20399:     return %altids;
20400: }
20401: 
20402: sub count_servers {
20403:     my ($currbalancer,%servers) = @_;
20404:     my (@spares,$numspares);
20405:     foreach my $lonhost (sort(keys(%servers))) {
20406:         next if ($currbalancer eq $lonhost);
20407:         push(@spares,$lonhost);
20408:     }
20409:     if ($currbalancer) {
20410:         $numspares = scalar(@spares);
20411:     } else {
20412:         $numspares = scalar(@spares) - 1;
20413:     }
20414:     return ($numspares,@spares);
20415: }
20416: 
20417: sub lonbalance_targets_js {
20418:     my ($dom,$types,$servers,$settings) = @_;
20419:     my $select = &mt('Select');
20420:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
20421:     if (ref($servers) eq 'HASH') {
20422:         $alltargets = join("','",sort(keys(%{$servers})));
20423:         my @homedoms;
20424:         foreach my $server (sort(keys(%{$servers}))) {
20425:             if (&Apache::lonnet::host_domain($server) eq $dom) {
20426:                 push(@homedoms,'1');
20427:             } else {
20428:                 push(@homedoms,'0');
20429:             }
20430:         }
20431:         $allishome = join("','",@homedoms);
20432:     }
20433:     if (ref($types) eq 'ARRAY') {
20434:         if (@{$types} > 0) {
20435:             @alltypes = @{$types};
20436:         }
20437:     }
20438:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
20439:     $allinsttypes = join("','",@alltypes);
20440:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
20441:     if (ref($settings) eq 'HASH') {
20442:         %existing = %{$settings};
20443:     }
20444:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
20445:                               \%currtargets,\%currrules,\%currcookies);
20446:     my $balancers = join("','",sort(keys(%currbalancer)));
20447:     return <<"END";
20448: 
20449: <script type="text/javascript">
20450: // <![CDATA[
20451: 
20452: currBalancers = new Array('$balancers');
20453: 
20454: function toggleTargets(balnum) {
20455:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
20456:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
20457:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
20458:     var prevbalancer = prevhostitem.value;
20459:     var baltotal = document.getElementById('loadbalancing_total').value;
20460:     prevhostitem.value = balancer;
20461:     if (prevbalancer != '') {
20462:         var prevIdx = currBalancers.indexOf(prevbalancer);
20463:         if (prevIdx != -1) {
20464:             currBalancers.splice(prevIdx,1);
20465:         }
20466:     }
20467:     if (balancer == '') {
20468:         hideSpares(balnum);
20469:     } else {
20470:         var currIdx = currBalancers.indexOf(balancer);
20471:         if (currIdx == -1) {
20472:             currBalancers.push(balancer);
20473:         }
20474:         var homedoms = new Array('$allishome');
20475:         var ishomedom = homedoms[lonhostitem.selectedIndex];
20476:         showSpares(balancer,ishomedom,balnum);
20477:     }
20478:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
20479:     return;
20480: }
20481: 
20482: function showSpares(balancer,ishomedom,balnum) {
20483:     var alltargets = new Array('$alltargets');
20484:     var insttypes = new Array('$allinsttypes');
20485:     var offloadtypes = new Array('primary','default');
20486: 
20487:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
20488:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
20489:  
20490:     for (var i=0; i<offloadtypes.length; i++) {
20491:         var count = 0;
20492:         for (var j=0; j<alltargets.length; j++) {
20493:             if (alltargets[j] != balancer) {
20494:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
20495:                 item.value = alltargets[j];
20496:                 item.style.textAlign='left';
20497:                 item.style.textFace='normal';
20498:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
20499:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
20500:                     item.disabled = '';
20501:                 } else {
20502:                     item.disabled = 'disabled';
20503:                     item.checked = false;
20504:                 }
20505:                 count ++;
20506:             }
20507:         }
20508:     }
20509:     for (var k=0; k<insttypes.length; k++) {
20510:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
20511:             if (ishomedom == 1) {
20512:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
20513:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
20514:             } else {
20515:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
20516:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
20517:             }
20518:         } else {
20519:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
20520:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
20521:         }
20522:         if ((insttypes[k] != '_LC_external') && 
20523:             ((insttypes[k] != '_LC_internetdom') ||
20524:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
20525:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
20526:             item.options.length = 0;
20527:             item.options[0] = new Option("","",true,true);
20528:             var idx = 0;
20529:             for (var m=0; m<alltargets.length; m++) {
20530:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
20531:                     idx ++;
20532:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
20533:                 }
20534:             }
20535:         }
20536:     }
20537:     return;
20538: }
20539: 
20540: function hideSpares(balnum) {
20541:     var alltargets = new Array('$alltargets');
20542:     var insttypes = new Array('$allinsttypes');
20543:     var offloadtypes = new Array('primary','default');
20544: 
20545:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
20546:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
20547: 
20548:     var total = alltargets.length - 1;
20549:     for (var i=0; i<offloadtypes; i++) {
20550:         for (var j=0; j<total; j++) {
20551:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
20552:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
20553:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
20554:         }
20555:     }
20556:     for (var k=0; k<insttypes.length; k++) {
20557:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
20558:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
20559:         if (insttypes[k] != '_LC_external') {
20560:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
20561:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
20562:         }
20563:     }
20564:     return;
20565: }
20566: 
20567: function checkOffloads(item,balnum,type) {
20568:     var alltargets = new Array('$alltargets');
20569:     var offloadtypes = new Array('primary','default');
20570:     if (item.checked) {
20571:         var total = alltargets.length - 1;
20572:         var other;
20573:         if (type == offloadtypes[0]) {
20574:             other = offloadtypes[1];
20575:         } else {
20576:             other = offloadtypes[0];
20577:         }
20578:         for (var i=0; i<total; i++) {
20579:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
20580:             if (server == item.value) {
20581:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
20582:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
20583:                 }
20584:             }
20585:         }
20586:     }
20587:     return;
20588: }
20589: 
20590: function singleServerToggle(balnum,type) {
20591:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
20592:     if (offloadtoSelIdx == 0) {
20593:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
20594:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
20595: 
20596:     } else {
20597:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
20598:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
20599:     }
20600:     return;
20601: }
20602: 
20603: function balanceruleChange(formname,balnum,type) {
20604:     if (type == '_LC_external') {
20605:         return;
20606:     }
20607:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
20608:     for (var i=0; i<typesRules.length; i++) {
20609:         if (formname.elements[typesRules[i]].checked) {
20610:             if (formname.elements[typesRules[i]].value != 'specific') {
20611:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
20612:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
20613:             } else {
20614:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
20615:             }
20616:         }
20617:     }
20618:     return;
20619: }
20620: 
20621: function balancerDeleteChange(balnum) {
20622:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
20623:     var baltotal = document.getElementById('loadbalancing_total').value;
20624:     var addtarget;
20625:     var removetarget;
20626:     var action = 'delete';
20627:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
20628:         var lonhost = hostitem.value;
20629:         var currIdx = currBalancers.indexOf(lonhost);
20630:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
20631:             if (currIdx != -1) {
20632:                 currBalancers.splice(currIdx,1);
20633:             }
20634:             addtarget = lonhost;
20635:         } else {
20636:             if (currIdx == -1) {
20637:                 currBalancers.push(lonhost);
20638:             }
20639:             removetarget = lonhost;
20640:             action = 'undelete';
20641:         }
20642:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
20643:     }
20644:     return;
20645: }
20646: 
20647: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
20648:     if (baltotal > 1) {
20649:         var offloadtypes = new Array('primary','default');
20650:         var alltargets = new Array('$alltargets');
20651:         var insttypes = new Array('$allinsttypes');
20652:         for (var i=0; i<baltotal; i++) {
20653:             if (i != balnum) {
20654:                 for (var j=0; j<offloadtypes.length; j++) {
20655:                     var total = alltargets.length - 1;
20656:                     for (var k=0; k<total; k++) {
20657:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
20658:                         var server = serveritem.value;
20659:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
20660:                             if (server == addtarget) {
20661:                                 serveritem.disabled = '';
20662:                             }
20663:                         }
20664:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
20665:                             if (server == removetarget) {
20666:                                 serveritem.disabled = 'disabled';
20667:                                 serveritem.checked = false;
20668:                             }
20669:                         }
20670:                     }
20671:                 }
20672:                 for (var j=0; j<insttypes.length; j++) {
20673:                     if (insttypes[j] != '_LC_external') {
20674:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
20675:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
20676:                             var currSel = singleserver.selectedIndex;
20677:                             var currVal = singleserver.options[currSel].value;
20678:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
20679:                                 var numoptions = singleserver.options.length;
20680:                                 var needsnew = 1;
20681:                                 for (var k=0; k<numoptions; k++) {
20682:                                     if (singleserver.options[k] == addtarget) {
20683:                                         needsnew = 0;
20684:                                         break;
20685:                                     }
20686:                                 }
20687:                                 if (needsnew == 1) {
20688:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
20689:                                 }
20690:                             }
20691:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
20692:                                 singleserver.options.length = 0;
20693:                                 if ((currVal) && (currVal != removetarget)) {
20694:                                     singleserver.options[0] = new Option("","",false,false);
20695:                                 } else {
20696:                                     singleserver.options[0] = new Option("","",true,true);
20697:                                 }
20698:                                 var idx = 0;
20699:                                 for (var m=0; m<alltargets.length; m++) {
20700:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
20701:                                         idx ++;
20702:                                         if (currVal == alltargets[m]) {
20703:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
20704:                                         } else {
20705:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
20706:                                         }
20707:                                     }
20708:                                 }
20709:                             }
20710:                         }
20711:                     }
20712:                 }
20713:             }
20714:         }
20715:     }
20716:     return;
20717: }
20718: 
20719: // ]]>
20720: </script>
20721: 
20722: END
20723: }
20724: 
20725: sub new_spares_js {
20726:     my @sparestypes = ('primary','default');
20727:     my $types = join("','",@sparestypes);
20728:     my $select = &mt('Select');
20729:     return <<"END";
20730: 
20731: <script type="text/javascript">
20732: // <![CDATA[
20733: 
20734: function updateNewSpares(formname,lonhost) {
20735:     var types = new Array('$types');
20736:     var include = new Array();
20737:     var exclude = new Array();
20738:     for (var i=0; i<types.length; i++) {
20739:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
20740:         for (var j=0; j<spareboxes.length; j++) {
20741:             if (formname.elements[spareboxes[j]].checked) {
20742:                 exclude.push(formname.elements[spareboxes[j]].value);
20743:             } else {
20744:                 include.push(formname.elements[spareboxes[j]].value);
20745:             }
20746:         }
20747:     }
20748:     for (var i=0; i<types.length; i++) {
20749:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
20750:         var selIdx = newSpare.selectedIndex;
20751:         var currnew = newSpare.options[selIdx].value;
20752:         var okSpares = new Array();
20753:         for (var j=0; j<newSpare.options.length; j++) {
20754:             var possible = newSpare.options[j].value;
20755:             if (possible != '') {
20756:                 if (exclude.indexOf(possible) == -1) {
20757:                     okSpares.push(possible);
20758:                 } else {
20759:                     if (currnew == possible) {
20760:                         selIdx = 0;
20761:                     }
20762:                 }
20763:             }
20764:         }
20765:         for (var k=0; k<include.length; k++) {
20766:             if (okSpares.indexOf(include[k]) == -1) {
20767:                 okSpares.push(include[k]);
20768:             }
20769:         }
20770:         okSpares.sort();
20771:         newSpare.options.length = 0;
20772:         if (selIdx == 0) {
20773:             newSpare.options[0] = new Option("$select","",true,true);
20774:         } else {
20775:             newSpare.options[0] = new Option("$select","",false,false);
20776:         }
20777:         for (var m=0; m<okSpares.length; m++) {
20778:             var idx = m+1;
20779:             var selThis = 0;
20780:             if (selIdx != 0) {
20781:                 if (okSpares[m] == currnew) {
20782:                     selThis = 1;
20783:                 }
20784:             }
20785:             if (selThis == 1) {
20786:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
20787:             } else {
20788:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
20789:             }
20790:         }
20791:     }
20792:     return;
20793: }
20794: 
20795: function checkNewSpares(lonhost,type) {
20796:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
20797:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
20798:     if (chosen != '') {
20799:         var othertype;
20800:         var othernewSpare;
20801:         if (type == 'primary') {
20802:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
20803:         }
20804:         if (type == 'default') {
20805:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
20806:         }
20807:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
20808:             othernewSpare.selectedIndex = 0;
20809:         }
20810:     }
20811:     return;
20812: }
20813: 
20814: // ]]>
20815: </script>
20816: 
20817: END
20818: 
20819: }
20820: 
20821: sub common_domprefs_js {
20822:     return <<"END";
20823: 
20824: <script type="text/javascript">
20825: // <![CDATA[
20826: 
20827: function getIndicesByName(formname,item) {
20828:     var group = new Array();
20829:     for (var i=0;i<formname.elements.length;i++) {
20830:         if (formname.elements[i].name == item) {
20831:             group.push(formname.elements[i].id);
20832:         }
20833:     }
20834:     return group;
20835: }
20836: 
20837: // ]]>
20838: </script>
20839: 
20840: END
20841: 
20842: }
20843: 
20844: sub recaptcha_js {
20845:     my %lt = &captcha_phrases();
20846:     return <<"END";
20847: 
20848: <script type="text/javascript">
20849: // <![CDATA[
20850: 
20851: function updateCaptcha(caller,context) {
20852:     var privitem;
20853:     var pubitem;
20854:     var privtext;
20855:     var pubtext;
20856:     var versionitem;
20857:     var versiontext;
20858:     if (document.getElementById(context+'_recaptchapub')) {
20859:         pubitem = document.getElementById(context+'_recaptchapub');
20860:     } else {
20861:         return;
20862:     }
20863:     if (document.getElementById(context+'_recaptchapriv')) {
20864:         privitem = document.getElementById(context+'_recaptchapriv');
20865:     } else {
20866:         return;
20867:     }
20868:     if (document.getElementById(context+'_recaptchapubtxt')) {
20869:         pubtext = document.getElementById(context+'_recaptchapubtxt');
20870:     } else {
20871:         return;
20872:     }
20873:     if (document.getElementById(context+'_recaptchaprivtxt')) {
20874:         privtext = document.getElementById(context+'_recaptchaprivtxt');
20875:     } else {
20876:         return;
20877:     }
20878:     if (document.getElementById(context+'_recaptchaversion')) {
20879:         versionitem = document.getElementById(context+'_recaptchaversion');
20880:     } else {
20881:         return;
20882:     }
20883:     if (document.getElementById(context+'_recaptchavertxt')) {
20884:         versiontext = document.getElementById(context+'_recaptchavertxt');
20885:     } else {
20886:         return;
20887:     }
20888:     if (caller.checked) {
20889:         if (caller.value == 'recaptcha') {
20890:             pubitem.type = 'text';
20891:             privitem.type = 'text';
20892:             pubitem.size = '40';
20893:             privitem.size = '40';
20894:             pubtext.innerHTML = "$lt{'pub'}";
20895:             privtext.innerHTML = "$lt{'priv'}";
20896:             versionitem.type = 'text';
20897:             versionitem.size = '3';
20898:             versiontext.innerHTML = "$lt{'ver'}";
20899:         } else {
20900:             pubitem.type = 'hidden';
20901:             privitem.type = 'hidden';
20902:             versionitem.type = 'hidden';
20903:             pubtext.innerHTML = '';
20904:             privtext.innerHTML = '';
20905:             versiontext.innerHTML = '';
20906:         }
20907:     }
20908:     return;
20909: }
20910: 
20911: // ]]>
20912: </script>
20913: 
20914: END
20915: 
20916: }
20917: 
20918: sub toggle_display_js {
20919:     return <<"END";
20920: 
20921: <script type="text/javascript">
20922: // <![CDATA[
20923: 
20924: function toggleDisplay(domForm,caller) {
20925:     if (document.getElementById(caller)) {
20926:         var divitem = document.getElementById(caller);
20927:         var optionsElement = domForm.coursecredits;
20928:         var checkval = 1;
20929:         var dispval = 'block';
20930:         var selfcreateRegExp = /^cancreate_emailverified/;
20931:         if (caller == 'emailoptions') {
20932:             optionsElement = domForm.cancreate_email;
20933:         }
20934:         if (caller == 'studentsubmission') {
20935:             optionsElement = domForm.postsubmit;
20936:         }
20937:         if (caller == 'cloneinstcode') {
20938:             optionsElement = domForm.canclone;
20939:             checkval = 'instcode';
20940:         }
20941:         if (selfcreateRegExp.test(caller)) {
20942:             optionsElement = domForm.elements[caller];
20943:             checkval = 'other';
20944:             dispval = 'inline'
20945:         }
20946:         if (optionsElement.length) {
20947:             var currval;
20948:             for (var i=0; i<optionsElement.length; i++) {
20949:                 if (optionsElement[i].checked) {
20950:                    currval = optionsElement[i].value;
20951:                 }
20952:             }
20953:             if (currval == checkval) {
20954:                 divitem.style.display = dispval;
20955:             } else {
20956:                 divitem.style.display = 'none';
20957:             }
20958:         }
20959:     }
20960:     return;
20961: }
20962: 
20963: // ]]>
20964: </script>
20965: 
20966: END
20967: 
20968: }
20969: 
20970: sub captcha_phrases {
20971:     return &Apache::lonlocal::texthash (
20972:                  priv => 'Private key',
20973:                  pub  => 'Public key',
20974:                  original  => 'original (CAPTCHA)',
20975:                  recaptcha => 'successor (ReCAPTCHA)',
20976:                  notused   => 'unused',
20977:                  ver => 'ReCAPTCHA version (1 or 2)',
20978:     );
20979: }
20980: 
20981: sub devalidate_remote_domconfs {
20982:     my ($dom,$cachekeys) = @_;
20983:     return unless (ref($cachekeys) eq 'HASH');
20984:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20985:     my %thismachine;
20986:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
20987:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
20988:                       'directorysrch','passwdconf','cats','proxyalias','proxysaml',
20989:                       'ipaccess');
20990:     my %cache_by_lonhost;
20991:     if (exists($cachekeys->{'samllanding'})) {
20992:         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
20993:             my %landing = %{$cachekeys->{'samllanding'}};
20994:             my %domservers = &Apache::lonnet::get_servers($dom);
20995:             if (keys(%domservers)) {
20996:                 foreach my $server (keys(%domservers)) {
20997:                     my @cached;
20998:                     next if ($thismachine{$server});
20999:                     if ($landing{$server}) {
21000:                         push(@cached,&escape('samllanding').':'.&escape($server));
21001:                     }
21002:                     if (@cached) {
21003:                         $cache_by_lonhost{$server} = \@cached;
21004:                     }
21005:                 }
21006:             }
21007:         }
21008:     }
21009:     if (keys(%servers)) {
21010:         foreach my $server (keys(%servers)) {
21011:             next if ($thismachine{$server});
21012:             my @cached;
21013:             foreach my $name (@posscached) {
21014:                 if ($cachekeys->{$name}) {
21015:                     if (($name eq 'proxyalias') || ($name eq 'proxysaml')) {
21016:                         if (ref($cachekeys->{$name}) eq 'HASH') {
21017:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
21018:                                 push(@cached,&escape($name).':'.&escape($key));
21019:                             }
21020:                         }
21021:                     } else {
21022:                         push(@cached,&escape($name).':'.&escape($dom));
21023:                     }
21024:                 }
21025:             }
21026:             if ((exists($cache_by_lonhost{$server})) &&
21027:                 (ref($cache_by_lonhost{$server}) eq 'ARRAY')) {
21028:                 push(@cached,@{$cache_by_lonhost{$server}});
21029:             }
21030:             if (@cached) {
21031:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
21032:             }
21033:         }
21034:     }
21035:     return;
21036: }
21037: 
21038: 1;

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