File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.427: download - view: text, annotated - select for diffs
Tue Jun 20 14:03:52 2023 UTC (12 months, 1 week ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Support case where assignment of roles in a different domain is queued
  pending acceptance of the role by the role recipient.
  - A user can also choose to decline the role.
  - Domain configuration can be set for domain roles, co-author roles,
    course roles and community roles.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.427 2023/06/20 14:03:52 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, placement, 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 LONCAPA::SSL;
  175: use File::Copy;
  176: use Locale::Language;
  177: use DateTime::TimeZone;
  178: use DateTime::Locale;
  179: use Time::HiRes qw( sleep );
  180: use Net::CIDR;
  181: use Crypt::CBC;
  182: 
  183: my $registered_cleanup;
  184: my $modified_urls;
  185: 
  186: sub handler {
  187:     my $r=shift;
  188:     if ($r->header_only) {
  189:         &Apache::loncommon::content_type($r,'text/html');
  190:         $r->send_http_header;
  191:         return OK;
  192:     }
  193: 
  194:     my $context = 'domain';
  195:     my $dom = $env{'request.role.domain'};
  196:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  197:     if (&Apache::lonnet::allowed('mau',$dom)) {
  198:         &Apache::loncommon::content_type($r,'text/html');
  199:         $r->send_http_header;
  200:     } else {
  201:         $env{'user.error.msg'}=
  202:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  203:         return HTTP_NOT_ACCEPTABLE;
  204:     }
  205: 
  206:     $registered_cleanup=0;
  207:     @{$modified_urls}=();
  208: 
  209:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  210:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  211:                                             ['phase','actions']);
  212:     my $phase = 'pickactions';
  213:     if ( exists($env{'form.phase'}) ) {
  214:         $phase = $env{'form.phase'};
  215:     }
  216:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  217:     my %domconfig =
  218:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  219:                 'quotas','autoenroll','autoupdate','autocreate',
  220:                 'directorysrch','usercreation','usermodification',
  221:                 'contacts','defaults','scantron','coursecategories',
  222:                 'serverstatuses','requestcourses','helpsettings',
  223:                 'coursedefaults','usersessions','loadbalancing',
  224:                 'requestauthor','selfenrollment','inststatus',
  225:                 'ltitools','toolsec','ssl','trust','lti','ltisec',
  226:                 'privacy','passwords','proctoring','wafproxy','ipaccess'],$dom);
  227:     my %encconfig =
  228:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti','proctoring','linkprot'],$dom,undef,1);
  229:     my ($checked_is_home,$is_home);
  230:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  231:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  232:             my $home = &Apache::lonnet::domain($dom,'primary');
  233:             unless (($home eq 'no_host') || ($home eq '')) {
  234:                 my @ids=&Apache::lonnet::current_machine_ids();
  235:                 if (grep(/^\Q$home\E$/,@ids)) {
  236:                     $is_home = 1;
  237:                 }
  238:             }
  239:             $checked_is_home = 1;
  240:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  241:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  242:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  243:                     $domconfig{'ltitools'}{$id}{'key'} = $encconfig{'ltitools'}{$id}{'key'};
  244:                     if (($is_home) && ($phase eq 'process')) {
  245:                         $domconfig{'ltitools'}{$id}{'secret'} = $encconfig{'ltitools'}{$id}{'secret'};
  246:                     }
  247:                 }
  248:             }
  249:         }
  250:     }
  251:     if (ref($domconfig{'lti'}) eq 'HASH') {
  252:         if (ref($encconfig{'lti'}) eq 'HASH') {
  253:             unless ($checked_is_home) {
  254:                 my $home = &Apache::lonnet::domain($dom,'primary');
  255:                 unless (($home eq 'no_host') || ($home eq '')) {
  256:                     my @ids=&Apache::lonnet::current_machine_ids();
  257:                     if (grep(/^\Q$home\E$/,@ids)) {
  258:                         $is_home = 1;
  259:                     }
  260:                 }
  261:                 $checked_is_home = 1;
  262:             }
  263:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  264:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  265:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  266:                     $domconfig{'lti'}{$id}{'key'} = $encconfig{'lti'}{$id}{'key'};
  267:                     if (($is_home) && ($phase eq 'process')) {
  268:                         $domconfig{'lti'}{$id}{'secret'} = $encconfig{'lti'}{$id}{'secret'};
  269:                     }
  270:                 }
  271:             }
  272:         }
  273:     }
  274:     if (ref($domconfig{'ltisec'}) eq 'HASH') {
  275:         if (ref($domconfig{'ltisec'}{'linkprot'}) eq 'HASH') {
  276:             if (ref($encconfig{'linkprot'}) eq 'HASH') {
  277:                 foreach my $id (keys(%{$domconfig{'ltisec'}{'linkprot'}})) {
  278:                     unless ($id =~ /^\d+$/) {
  279:                         delete($domconfig{'ltisec'}{'linkprot'}{$id});
  280:                     }
  281:                     if ((ref($domconfig{'ltisec'}{'linkprot'}{$id}) eq 'HASH') &&
  282:                         (ref($encconfig{'linkprot'}{$id}) eq 'HASH')) {
  283:                         foreach my $item ('key','secret') {
  284:                             $domconfig{'ltisec'}{'linkprot'}{$id}{$item} = $encconfig{'linkprot'}{$id}{$item};
  285:                         }
  286:                     }
  287:                 }
  288:             }
  289:         }
  290:     }
  291:     if (ref($domconfig{'proctoring'}) eq 'HASH') {
  292:         if (ref($encconfig{'proctoring'}) eq 'HASH') {
  293:             foreach my $provider (keys(%{$domconfig{'proctoring'}})) {
  294:                 if ((ref($domconfig{'proctoring'}{$provider}) eq 'HASH') &&
  295:                     (ref($encconfig{'proctoring'}{$provider}) eq 'HASH')) {
  296:                     foreach my $item ('key','secret') {
  297:                         $domconfig{'proctoring'}{$provider}{$item} = $encconfig{'proctoring'}{$provider}{$item};
  298:                     }
  299:                 }
  300:             }
  301:         }
  302:     }
  303:     my @prefs_order = ('rolecolors','login','ipaccess','defaults','wafproxy','passwords',
  304:                        'quotas','autoenroll','autoupdate','autocreate','directorysrch',
  305:                        'contacts','privacy','usercreation','selfcreation',
  306:                        'usermodification','scantron','requestcourses','requestauthor',
  307:                        'coursecategories','serverstatuses','helpsettings','coursedefaults',
  308:                        'ltitools','proctoring','selfenrollment','usersessions','ssl',
  309:                        'trust','lti');
  310:     my %existing;
  311:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  312:         %existing = %{$domconfig{'loadbalancing'}};
  313:     }
  314:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  315:         push(@prefs_order,'loadbalancing');
  316:     }
  317:     my %prefs = (
  318:         'rolecolors' =>
  319:                    { text => 'Default color schemes',
  320:                      help => 'Domain_Configuration_Color_Schemes',
  321:                      header => [{col1 => 'Student Settings',
  322:                                  col2 => '',},
  323:                                 {col1 => 'Coordinator Settings',
  324:                                  col2 => '',},
  325:                                 {col1 => 'Author Settings',
  326:                                  col2 => '',},
  327:                                 {col1 => 'Administrator Settings',
  328:                                  col2 => '',}],
  329:                       print => \&print_rolecolors,
  330:                       modify => \&modify_rolecolors,
  331:                     },
  332:         'login' =>
  333:                     { text => 'Log-in page options',
  334:                       help => 'Domain_Configuration_Login_Page',
  335:                       header => [{col1 => 'Log-in Page Items',
  336:                                   col2 => '',},
  337:                                  {col1 => 'Log-in Help',
  338:                                   col2 => 'Value'},
  339:                                  {col1 => 'Custom HTML in document head',
  340:                                   col2 => 'Value'},
  341:                                  {col1 => 'SSO',
  342:                                   col2 => 'Dual login: SSO and non-SSO options'},
  343:                                 ],
  344:                       print => \&print_login,
  345:                       modify => \&modify_login,
  346:                     },
  347:         'defaults' => 
  348:                     { text => 'Default authentication/language/timezone/portal/types',
  349:                       help => 'Domain_Configuration_LangTZAuth',
  350:                       header => [{col1 => 'Setting',
  351:                                   col2 => 'Value'},
  352:                                  {col1 => 'Institutional user types',
  353:                                   col2 => 'Name displayed'},
  354:                                  {col1 => 'Mapping for missing usernames via standard log-in',
  355:                                   col2 => 'Rules in use'}],
  356:                       print => \&print_defaults,
  357:                       modify => \&modify_defaults,
  358:                     },
  359:         'wafproxy' =>
  360:                     { text => 'Web Application Firewall/Reverse Proxy',
  361:                       help => 'Domain_Configuration_WAF_Proxy',
  362:                       header => [{col1 => 'Domain(s)',
  363:                                   col2 => 'Servers and WAF/Reverse Proxy alias(es)',
  364:                                  },
  365:                                  {col1 => 'Domain(s)',
  366:                                   col2 => 'WAF Configuration',}],
  367:                       print => \&print_wafproxy,
  368:                       modify => \&modify_wafproxy,
  369:                     },
  370:         'passwords' =>
  371:                     { text => 'Passwords (Internal authentication)',
  372:                       help => 'Domain_Configuration_Passwords',
  373:                       header => [{col1 => 'Resetting Forgotten Password',
  374:                                   col2 => 'Settings'},
  375:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  376:                                   col2 => 'Settings'},
  377:                                  {col1 => 'Rules for LON-CAPA Passwords',
  378:                                   col2 => 'Settings'},
  379:                                  {col1 => 'Course Owner Changing Student Passwords',
  380:                                   col2 => 'Settings'}],
  381:                       print => \&print_passwords,
  382:                       modify => \&modify_passwords,
  383:                     },
  384:         'quotas' => 
  385:                     { text => 'Blogs, personal pages/timezones, webDAV/quotas, portfolio',
  386:                       help => 'Domain_Configuration_Quotas',
  387:                       header => [{col1 => 'User affiliation',
  388:                                   col2 => 'Available tools',
  389:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  390:                       print => \&print_quotas,
  391:                       modify => \&modify_quotas,
  392:                     },
  393:         'autoenroll' =>
  394:                    { text => 'Auto-enrollment settings',
  395:                      help => 'Domain_Configuration_Auto_Enrollment',
  396:                      header => [{col1 => 'Configuration setting',
  397:                                  col2 => 'Value(s)'}],
  398:                      print => \&print_autoenroll,
  399:                      modify => \&modify_autoenroll,
  400:                    },
  401:         'autoupdate' => 
  402:                    { text => 'Auto-update settings',
  403:                      help => 'Domain_Configuration_Auto_Updates',
  404:                      header => [{col1 => 'Setting',
  405:                                  col2 => 'Value',},
  406:                                 {col1 => 'Setting',
  407:                                  col2 => 'Affiliation'},
  408:                                 {col1 => 'User population',
  409:                                  col2 => 'Updatable user data'}],
  410:                      print => \&print_autoupdate,
  411:                      modify => \&modify_autoupdate,
  412:                   },
  413:         'autocreate' => 
  414:                   { text => 'Auto-course creation settings',
  415:                      help => 'Domain_Configuration_Auto_Creation',
  416:                      header => [{col1 => 'Configuration Setting',
  417:                                  col2 => 'Value',}],
  418:                      print => \&print_autocreate,
  419:                      modify => \&modify_autocreate,
  420:                   },
  421:         'directorysrch' => 
  422:                   { text => 'Directory searches',
  423:                     help => 'Domain_Configuration_InstDirectory_Search',
  424:                     header => [{col1 => 'Institutional Directory Setting',
  425:                                 col2 => 'Value',},
  426:                                {col1 => 'LON-CAPA Directory Setting',
  427:                                 col2 => 'Value',}],
  428:                     print => \&print_directorysrch,
  429:                     modify => \&modify_directorysrch,
  430:                   },
  431:         'contacts' =>
  432:                   { text => 'E-mail addresses and helpform',
  433:                     help => 'Domain_Configuration_Contact_Info',
  434:                     header => [{col1 => 'Default e-mail addresses',
  435:                                 col2 => 'Value',},
  436:                                {col1 => 'Recipient(s) for notifications',
  437:                                 col2 => 'Value',},
  438:                                {col1 => 'Nightly status check e-mail',
  439:                                 col2 => 'Settings',},
  440:                                {col1 => 'Ask helpdesk form settings',
  441:                                 col2 => 'Value',},],
  442:                     print => \&print_contacts,
  443:                     modify => \&modify_contacts,
  444:                   },
  445:         'usercreation' => 
  446:                   { text => 'User creation',
  447:                     help => 'Domain_Configuration_User_Creation',
  448:                     header => [{col1 => 'Format rule type',
  449:                                 col2 => 'Format rules in force'},
  450:                                {col1 => 'User account creation',
  451:                                 col2 => 'Usernames which may be created',},
  452:                                {col1 => 'Context',
  453:                                 col2 => 'Assignable authentication types'}],
  454:                     print => \&print_usercreation,
  455:                     modify => \&modify_usercreation,
  456:                   },
  457:         'selfcreation' => 
  458:                   { text => 'Users self-creating accounts',
  459:                     help => 'Domain_Configuration_Self_Creation', 
  460:                     header => [{col1 => 'Self-creation with institutional username',
  461:                                 col2 => 'Enabled?'},
  462:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  463:                                 col2 => 'Information user can enter'},
  464:                                {col1 => 'Self-creation with e-mail verification',
  465:                                 col2 => 'Settings'}],
  466:                     print => \&print_selfcreation,
  467:                     modify => \&modify_selfcreation,
  468:                   },
  469:         'usermodification' =>
  470:                   { text => 'User modification',
  471:                     help => 'Domain_Configuration_User_Modification',
  472:                     header => [{col1 => 'Target user has role',
  473:                                 col2 => 'User information updatable in author context'},
  474:                                {col1 => 'Target user has role',
  475:                                 col2 => 'User information updatable in course context'}],
  476:                     print => \&print_usermodification,
  477:                     modify => \&modify_usermodification,
  478:                   },
  479:         'scantron' =>
  480:                   { text => 'Bubblesheet format',
  481:                     help => 'Domain_Configuration_Scantron_Format',
  482:                     header => [ {col1 => 'Bubblesheet format file',
  483:                                  col2 => ''},
  484:                                 {col1 => 'Bubblesheet data upload formats',
  485:                                  col2 => 'Settings'}],
  486:                     print => \&print_scantron,
  487:                     modify => \&modify_scantron,
  488:                   },
  489:         'requestcourses' => 
  490:                  {text => 'Request creation of courses',
  491:                   help => 'Domain_Configuration_Request_Courses',
  492:                   header => [{col1 => 'User affiliation',
  493:                               col2 => 'Availability/Processing of requests',},
  494:                              {col1 => 'Setting',
  495:                               col2 => 'Value'},
  496:                              {col1 => 'Available textbooks',
  497:                               col2 => ''},
  498:                              {col1 => 'Available templates',
  499:                               col2 => ''},
  500:                              {col1 => 'Validation (not official courses)',
  501:                               col2 => 'Value'},],
  502:                   print => \&print_quotas,
  503:                   modify => \&modify_quotas,
  504:                  },
  505:         'requestauthor' =>
  506:                  {text => 'Request Authoring Space',
  507:                   help => 'Domain_Configuration_Request_Author',
  508:                   header => [{col1 => 'User affiliation',
  509:                               col2 => 'Availability/Processing of requests',},
  510:                              {col1 => 'Setting',
  511:                               col2 => 'Value'}],
  512:                   print => \&print_quotas,
  513:                   modify => \&modify_quotas,
  514:                  },
  515:         'coursecategories' =>
  516:                   { text => 'Cataloging of courses/communities',
  517:                     help => 'Domain_Configuration_Cataloging_Courses',
  518:                     header => [{col1 => 'Catalog type/availability',
  519:                                 col2 => '',},
  520:                                {col1 => 'Category settings for standard catalog',
  521:                                 col2 => '',},
  522:                                {col1 => 'Categories',
  523:                                 col2 => '',
  524:                                }],
  525:                     print => \&print_coursecategories,
  526:                     modify => \&modify_coursecategories,
  527:                   },
  528:         'serverstatuses' =>
  529:                  {text   => 'Access to server status pages',
  530:                   help   => 'Domain_Configuration_Server_Status',
  531:                   header => [{col1 => 'Status Page',
  532:                               col2 => 'Other named users',
  533:                               col3 => 'Specific IPs',
  534:                             }],
  535:                   print => \&print_serverstatuses,
  536:                   modify => \&modify_serverstatuses,
  537:                  },
  538:         'helpsettings' =>
  539:                  {text   => 'Support settings',
  540:                   help   => 'Domain_Configuration_Help_Settings',
  541:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  542:                               col2 => 'Value'},
  543:                              {col1 => 'Helpdesk Roles',
  544:                               col2 => 'Settings'},],
  545:                   print  => \&print_helpsettings,
  546:                   modify => \&modify_helpsettings,
  547:                  },
  548:         'coursedefaults' => 
  549:                  {text => 'Course/Community defaults',
  550:                   help => 'Domain_Configuration_Course_Defaults',
  551:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  552:                               col2 => 'Value',},
  553:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  554:                               col2 => 'Value',},],
  555:                   print => \&print_coursedefaults,
  556:                   modify => \&modify_coursedefaults,
  557:                  },
  558:         'selfenrollment' => 
  559:                  {text   => 'Self-enrollment in Course/Community',
  560:                   help   => 'Domain_Configuration_Selfenrollment',
  561:                   header => [{col1 => 'Configuration Rights',
  562:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  563:                              {col1 => 'Defaults',
  564:                               col2 => 'Value'},
  565:                              {col1 => 'Self-enrollment validation (optional)',
  566:                               col2 => 'Value'},],
  567:                   print => \&print_selfenrollment,
  568:                   modify => \&modify_selfenrollment,
  569:                  },
  570:         'privacy' => 
  571:                  {text   => 'Role assignments and user privacy',
  572:                   help   => 'Domain_Configuration_User_Privacy',
  573:                   header => [{col1 => 'Role assigned in different domain',
  574:                               col2 => 'Approval options'},
  575:                              {col1 => 'Role assigned in different domain to user of type',
  576:                               col2 => 'User information available in that domain'},
  577:                              {col1 => "Role assigned in user's domain",
  578:                               col2 => 'Information viewable by privileged user'},
  579:                              {col1 => "Role assigned in user's domain",
  580:                               col2 => 'Information viewable by unprivileged user'}],
  581:                   print => \&print_privacy,
  582:                   modify => \&modify_privacy,
  583:                  },
  584:         'usersessions' =>
  585:                  {text  => 'User session hosting/offloading',
  586:                   help  => 'Domain_Configuration_User_Sessions',
  587:                   header => [{col1 => 'Domain server',
  588:                               col2 => 'Servers to offload sessions to when busy'},
  589:                              {col1 => 'Hosting of users from other domains',
  590:                               col2 => 'Rules'},
  591:                              {col1 => "Hosting domain's own users elsewhere",
  592:                               col2 => 'Rules'}],
  593:                   print => \&print_usersessions,
  594:                   modify => \&modify_usersessions,
  595:                  },
  596:         'loadbalancing' =>
  597:                  {text  => 'Dedicated Load Balancer(s)',
  598:                   help  => 'Domain_Configuration_Load_Balancing',
  599:                   header => [{col1 => 'Balancers',
  600:                               col2 => 'Default destinations',
  601:                               col3 => 'User affiliation',
  602:                               col4 => 'Overrides'},
  603:                             ],
  604:                   print => \&print_loadbalancing,
  605:                   modify => \&modify_loadbalancing,
  606:                  },
  607:         'ltitools' =>
  608:                  {text => 'External Tools (LTI)',
  609:                   help => 'Domain_Configuration_LTI_Tools',
  610:                   header => [{col1 => 'Encryption of shared secrets',
  611:                               col2 => 'Settings'},
  612:                              {col1 => 'Rules for shared secrets',
  613:                               col2 => 'Settings'},
  614:                              {col1 => 'Providers',
  615:                               col2 => 'Settings',}],
  616:                   print => \&print_ltitools,
  617:                   modify => \&modify_ltitools,
  618:                  },
  619:         'proctoring' =>
  620:                  {text => 'Remote Proctoring Integration',
  621:                   help => 'Domain_Configuration_Proctoring',
  622:                   header => [{col1 => 'Name',
  623:                               col2 => 'Configuration'}],
  624:                   print => \&print_proctoring,
  625:                   modify => \&modify_proctoring,
  626:                  },
  627:         'ssl' =>
  628:                  {text  => 'LON-CAPA Network (SSL)',
  629:                   help  => 'Domain_Configuration_Network_SSL',
  630:                   header => [{col1 => 'Server',
  631:                               col2 => 'Certificate Status'},
  632:                              {col1 => 'Connections to other servers',
  633:                               col2 => 'Rules'},
  634:                              {col1 => 'Connections from other servers',
  635:                               col2 => 'Rules'},
  636:                              {col1 => "Replicating domain's published content",
  637:                               col2 => 'Rules'}],
  638:                   print => \&print_ssl,
  639:                   modify => \&modify_ssl,
  640:                  },
  641:         'trust' =>
  642:                  {text   => 'Trust Settings',
  643:                   help   => 'Domain_Configuration_Trust',
  644:                   header => [{col1 => "Access to this domain's content by others",
  645:                               col2 => 'Rules'},
  646:                              {col1 => "Access to other domain's content by this domain",
  647:                               col2 => 'Rules'},
  648:                              {col1 => "Enrollment in this domain's courses by others",
  649:                               col2 => 'Rules',},
  650:                              {col1 => "Co-author roles in this domain for others",
  651:                               col2 => 'Rules',},
  652:                              {col1 => "Co-author roles for this domain's users elsewhere",
  653:                               col2 => 'Rules',},
  654:                              {col1 => "Domain roles in this domain assignable to others",
  655:                               col2 => 'Rules'},
  656:                              {col1 => "Course catalog for this domain displayed elsewhere",
  657:                               col2 => 'Rules'},
  658:                              {col1 => "Requests for creation of courses in this domain by others",
  659:                               col2 => 'Rules'},
  660:                              {col1 => "Users in other domains can send messages to this domain",
  661:                               col2 => 'Rules'},],
  662:                   print => \&print_trust,
  663:                   modify => \&modify_trust,
  664:                  },
  665:         'lti' =>
  666:                  {text => 'LTI Link Protection and LTI Consumers',
  667:                   help => 'Domain_Configuration_LTI_Provider',
  668:                   header => [{col1 => 'Encryption of shared secrets',
  669:                               col2 => 'Settings'},
  670:                              {col1 => 'Rules for shared secrets',
  671:                               col2 => 'Settings'},
  672:                              {col1 => 'Link Protectors',
  673:                               col2 => 'Settings'},
  674:                              {col1 => 'Consumers',
  675:                               col2 => 'Settings'},],
  676:                   print => \&print_lti,
  677:                   modify => \&modify_lti,
  678:                  },
  679:         'ipaccess' =>
  680:                        {text => 'IP-based access control',
  681:                         help => 'Domain_Configuration_IP_Access',
  682:                         header => [{col1 => 'Setting',
  683:                                     col2 => 'Value'},],
  684:                         print  => \&print_ipaccess,
  685:                         modify => \&modify_ipaccess,
  686:                        },
  687:     );
  688:     if (keys(%servers) > 1) {
  689:         $prefs{'login'}  = { text   => 'Log-in page options',
  690:                              help   => 'Domain_Configuration_Login_Page',
  691:                             header => [{col1 => 'Log-in Service',
  692:                                         col2 => 'Server Setting',},
  693:                                        {col1 => 'Log-in Page Items',
  694:                                         col2 => 'Settings'},
  695:                                        {col1 => 'Log-in Help',
  696:                                         col2 => 'Value'},
  697:                                        {col1 => 'Custom HTML in document head',
  698:                                         col2 => 'Value'},
  699:                                        {col1 => 'SSO',
  700:                                         col2 => 'Dual login: SSO and non-SSO options'},
  701:                                       ],
  702:                             print => \&print_login,
  703:                             modify => \&modify_login,
  704:                            };
  705:     }
  706: 
  707:     my @roles = ('student','coordinator','author','admin');
  708:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  709:     &Apache::lonhtmlcommon::add_breadcrumb
  710:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  711:       text=>"Settings to display/modify"});
  712:     my $confname = $dom.'-domainconfig';
  713: 
  714:     if ($phase eq 'process') {
  715:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  716:                                                               \%prefs,\%domconfig,$confname,\@roles);
  717:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  718:             $r->rflush();
  719:             &devalidate_remote_domconfs($dom,$result);
  720:         }
  721:     } elsif ($phase eq 'display') {
  722:         my $js = &recaptcha_js().
  723:                  &toggle_display_js();
  724:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  725:             my ($othertitle,$usertypes,$types) =
  726:                 &Apache::loncommon::sorted_inst_types($dom);
  727:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  728:                                           $domconfig{'loadbalancing'}).
  729:                    &new_spares_js().
  730:                    &common_domprefs_js().
  731:                    &Apache::loncommon::javascript_array_indexof();
  732:         }
  733:         if (grep(/^requestcourses$/,@actions)) {
  734:             my $javascript_validations;
  735:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  736:             $js .= <<END;
  737: <script type="text/javascript">
  738: $javascript_validations
  739: </script>
  740: $coursebrowserjs
  741: END
  742:         } elsif (grep(/^ipaccess$/,@actions)) {
  743:             $js .= &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
  744:         }
  745:         if (grep(/^selfcreation$/,@actions)) {
  746:             $js .= &selfcreate_javascript();
  747:         }
  748:         if (grep(/^contacts$/,@actions)) {
  749:             $js .= &contacts_javascript();
  750:         }
  751:         if (grep(/^scantron$/,@actions)) {
  752:             $js .= &scantron_javascript();
  753:         }
  754:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  755:     } else {
  756: # check if domconfig user exists for the domain.
  757:         my $servadm = $r->dir_config('lonAdmEMail');
  758:         my ($configuserok,$author_ok,$switchserver) =
  759:             &config_check($dom,$confname,$servadm);
  760:         unless ($configuserok eq 'ok') {
  761:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  762:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  763:                           $confname).
  764:                       '<br />'
  765:             );
  766:             if ($switchserver) {
  767:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  768:                           '<br />'.
  769:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  770:                           '<br />'.
  771:                           &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).
  772:                           '<br />'.
  773:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  774:                 );
  775:             } else {
  776:                 $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.').
  777:                           '<br />'.
  778:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  779:                 );
  780:             }
  781:             $r->print(&Apache::loncommon::end_page());
  782:             return OK;
  783:         }
  784:         if (keys(%domconfig) == 0) {
  785:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  786:             my @ids=&Apache::lonnet::current_machine_ids();
  787:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  788:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  789:                 my @loginimages = ('img','logo','domlogo','login');
  790:                 my $custom_img_count = 0;
  791:                 foreach my $img (@loginimages) {
  792:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  793:                         $custom_img_count ++;
  794:                     }
  795:                 }
  796:                 foreach my $role (@roles) {
  797:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  798:                         $custom_img_count ++;
  799:                     }
  800:                 }
  801:                 if ($custom_img_count > 0) {
  802:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  803:                     my $switch_server = &check_switchserver($dom,$confname);
  804:                     $r->print(
  805:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  806:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  807:     &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 />'.
  808:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  809:                     if ($switch_server) {
  810:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  811:                     }
  812:                     $r->print(&Apache::loncommon::end_page());
  813:                     return OK;
  814:                 }
  815:             }
  816:         }
  817:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  818:     }
  819:     return OK;
  820: }
  821: 
  822: sub process_changes {
  823:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  824:     my %domconfig;
  825:     if (ref($values) eq 'HASH') {
  826:         %domconfig = %{$values};
  827:     }
  828:     my $output;
  829:     if ($action eq 'login') {
  830:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  831:     } elsif ($action eq 'rolecolors') {
  832:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  833:                                      $lastactref,%domconfig);
  834:     } elsif ($action eq 'quotas') {
  835:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  836:     } elsif ($action eq 'autoenroll') {
  837:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  838:     } elsif ($action eq 'autoupdate') {
  839:         $output = &modify_autoupdate($dom,%domconfig);
  840:     } elsif ($action eq 'autocreate') {
  841:         $output = &modify_autocreate($dom,%domconfig);
  842:     } elsif ($action eq 'directorysrch') {
  843:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  844:     } elsif ($action eq 'usercreation') {
  845:         $output = &modify_usercreation($dom,%domconfig);
  846:     } elsif ($action eq 'selfcreation') {
  847:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  848:     } elsif ($action eq 'usermodification') {
  849:         $output = &modify_usermodification($dom,%domconfig);
  850:     } elsif ($action eq 'contacts') {
  851:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  852:     } elsif ($action eq 'defaults') {
  853:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  854:     } elsif ($action eq 'scantron') {
  855:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  856:     } elsif ($action eq 'coursecategories') {
  857:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  858:     } elsif ($action eq 'serverstatuses') {
  859:         $output = &modify_serverstatuses($dom,%domconfig);
  860:     } elsif ($action eq 'requestcourses') {
  861:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  862:     } elsif ($action eq 'requestauthor') {
  863:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  864:     } elsif ($action eq 'helpsettings') {
  865:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  866:     } elsif ($action eq 'coursedefaults') {
  867:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  868:     } elsif ($action eq 'selfenrollment') {
  869:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  870:     } elsif ($action eq 'usersessions') {
  871:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  872:     } elsif ($action eq 'loadbalancing') {
  873:         $output = &modify_loadbalancing($dom,%domconfig);
  874:     } elsif ($action eq 'ltitools') {
  875:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  876:     } elsif ($action eq 'proctoring') {
  877:         $output = &modify_proctoring($r,$dom,$action,$lastactref,%domconfig);
  878:     } elsif ($action eq 'ssl') {
  879:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  880:     } elsif ($action eq 'trust') {
  881:         $output = &modify_trust($dom,$lastactref,%domconfig);
  882:     } elsif ($action eq 'lti') {
  883:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  884:     } elsif ($action eq 'privacy') {
  885:         $output = &modify_privacy($dom,$lastactref,%domconfig);
  886:     } elsif ($action eq 'passwords') {
  887:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  888:     } elsif ($action eq 'wafproxy') {
  889:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  890:     } elsif ($action eq 'ipaccess') {
  891:         $output = &modify_ipaccess($dom,$lastactref,%domconfig);
  892:     }
  893:     return $output;
  894: }
  895: 
  896: sub print_config_box {
  897:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  898:     my $rowtotal = 0;
  899:     my $output;
  900:     if ($action eq 'coursecategories') {
  901:         $output = &coursecategories_javascript($settings);
  902:     } elsif ($action eq 'defaults') {
  903:         $output = &defaults_javascript($settings); 
  904:     } elsif ($action eq 'passwords') {
  905:         $output = &passwords_javascript($action);
  906:     } elsif ($action eq 'helpsettings') {
  907:         my (%privs,%levelscurrent);
  908:         my %full=();
  909:         my %levels=(
  910:                      course => {},
  911:                      domain => {},
  912:                      system => {},
  913:                    );
  914:         my $context = 'domain';
  915:         my $crstype = 'Course';
  916:         my $formname = 'display';
  917:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  918:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  919:         $output =
  920:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  921:                                                       \@templateroles);
  922:     } elsif ($action eq 'ltitools') {
  923:         $output .= &Apache::lonconfigsettings::ltitools_javascript($settings);
  924:     } elsif ($action eq 'lti') {
  925:         $output .= &passwords_javascript('ltisecrets')."\n".
  926:                    &lti_javascript($dom,$settings);
  927:     } elsif ($action eq 'proctoring') {
  928:         $output .= &proctoring_javascript($settings);
  929:     } elsif ($action eq 'wafproxy') {
  930:         $output .= &wafproxy_javascript($dom);
  931:     } elsif ($action eq 'autoupdate') {
  932:         $output .= &autoupdate_javascript();
  933:     } elsif ($action eq 'autoenroll') {
  934:         $output .= &autoenroll_javascript();
  935:     } elsif ($action eq 'login') {
  936:         $output .= &saml_javascript();
  937:     } elsif ($action eq 'ipaccess') {
  938:         $output .= &ipaccess_javascript($settings);
  939:     }
  940:     $output .=
  941:          '<table class="LC_nested_outer">
  942:           <tr>
  943:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  944:            &mt($item->{text}).'&nbsp;'.
  945:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  946:           '</tr>';
  947:     $rowtotal ++;
  948:     my $numheaders = 1;
  949:     if (ref($item->{'header'}) eq 'ARRAY') {
  950:         $numheaders = scalar(@{$item->{'header'}});
  951:     }
  952:     if ($numheaders > 1) {
  953:         my $colspan = '';
  954:         my $rightcolspan = '';
  955:         my $leftnobr = '';
  956:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  957:             ($action eq 'directorysrch') ||
  958:             (($action eq 'login') && ($numheaders < 5))) {
  959:             $colspan = ' colspan="2"';
  960:         }
  961:         if ($action eq 'usersessions') {
  962:             $rightcolspan = ' colspan="3"'; 
  963:         }
  964:         if ($action eq 'passwords') {
  965:             $leftnobr = ' LC_nobreak';
  966:         }
  967:         $output .= '
  968:           <tr>
  969:            <td>
  970:             <table class="LC_nested">
  971:              <tr class="LC_info_row">
  972:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  973:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  974:              </tr>';
  975:         $rowtotal ++;
  976:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  977:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  978:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  979:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  980:             ($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'wafproxy') ||
  981:             ($action eq 'lti') || ($action eq 'ltitools')) {
  982:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  983:         } elsif ($action eq 'passwords') {
  984:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  985:         } elsif ($action eq 'coursecategories') {
  986:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  987:         } elsif ($action eq 'scantron') {
  988:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  989:         } elsif ($action eq 'login') {
  990:             if ($numheaders == 5) {
  991:                 $colspan = ' colspan="2"';
  992:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  993:             } else {
  994:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  995:             }
  996:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  997:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  998:         } elsif ($action eq 'rolecolors') {
  999:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
 1000:         }
 1001:         $output .= '
 1002:            </table>
 1003:           </td>
 1004:          </tr>
 1005:          <tr>
 1006:            <td>
 1007:             <table class="LC_nested">
 1008:              <tr class="LC_info_row">
 1009:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
 1010:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
 1011:              </tr>';
 1012:             $rowtotal ++;
 1013:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
 1014:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
 1015:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
 1016:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults') ||
 1017:             ($action eq 'privacy') || ($action eq 'passwords') || ($action eq 'lti') ||
 1018:             ($action eq 'ltitools')) {
 1019:             if ($action eq 'coursecategories') {
 1020:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
 1021:                 $colspan = ' colspan="2"';
 1022:             } elsif ($action eq 'trust') {
 1023:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
 1024:             } elsif ($action eq 'passwords') {
 1025:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
 1026:             } else {
 1027:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
 1028:             }
 1029:             if ($action eq 'trust') {
 1030:                 $output .= '
 1031:             </table>
 1032:           </td>
 1033:          </tr>';
 1034:                 my @trusthdrs = qw(2 3 4 5 6 7);
 1035:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
 1036:                 for (my $i=0; $i<@trusthdrs; $i++) {
 1037:                     $output .= '
 1038:          <tr>
 1039:            <td>
 1040:             <table class="LC_nested">
 1041:              <tr class="LC_info_row">
 1042:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
 1043:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
 1044:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
 1045:             </table>
 1046:           </td>
 1047:          </tr>';
 1048:                 }
 1049:                 $output .= '
 1050:          <tr>
 1051:            <td>
 1052:             <table class="LC_nested">
 1053:              <tr class="LC_info_row">
 1054:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
 1055:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
 1056:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1057:             } else {
 1058:                 $output .= '
 1059:            </table>
 1060:           </td>
 1061:          </tr>
 1062:          <tr>
 1063:            <td>
 1064:             <table class="LC_nested">
 1065:              <tr class="LC_info_row">
 1066:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1067:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1068:              </tr>'."\n";
 1069:                 if ($action eq 'coursecategories') {
 1070:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
 1071:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || 
 1072:                          ($action eq 'passwords') || ($action eq 'lti')) {
 1073:                     if ($action eq 'passwords') {
 1074:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
 1075:                     } else {
 1076:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
 1077:                     }
 1078:                     $output .= '
 1079:              </tr>
 1080:             </table>
 1081:            </td>
 1082:           </tr>
 1083:           <tr>
 1084:            <td>
 1085:             <table class="LC_nested">
 1086:              <tr class="LC_info_row">
 1087:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1088:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n";
 1089:                     if ($action eq 'passwords') {
 1090:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
 1091:                     } else {
 1092:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1093:                     }
 1094:                     $output .= '
 1095:             </table>
 1096:           </td>
 1097:          </tr>
 1098:          <tr>';
 1099:                 } else {
 1100:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1101:                 }
 1102:             }
 1103:             $rowtotal ++;
 1104:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
 1105:                  ($action eq 'directorysrch') || ($action eq 'helpsettings') ||
 1106:                  ($action eq 'wafproxy')) {
 1107:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1108:         } elsif ($action eq 'scantron') {
 1109:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1110:         } elsif ($action eq 'ssl') {
 1111:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
 1112:             </table>
 1113:           </td>
 1114:          </tr>
 1115:          <tr>
 1116:            <td>
 1117:             <table class="LC_nested">
 1118:              <tr class="LC_info_row">
 1119:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1120:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1121:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
 1122:             </table>
 1123:           </td>
 1124:          </tr>
 1125:          <tr>
 1126:            <td>
 1127:             <table class="LC_nested">
 1128:              <tr class="LC_info_row">
 1129:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1130:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1131:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1132:         } elsif ($action eq 'login') {
 1133:             if ($numheaders == 5) {
 1134:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1135:            </table>
 1136:           </td>
 1137:          </tr>
 1138:          <tr>
 1139:            <td>
 1140:             <table class="LC_nested">
 1141:              <tr class="LC_info_row">
 1142:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1143:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1144:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1145:                 $rowtotal ++;
 1146:             } else {
 1147:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1148:             }
 1149:             $output .= '
 1150:            </table>
 1151:           </td>
 1152:          </tr>
 1153:          <tr>
 1154:            <td>
 1155:             <table class="LC_nested">
 1156:              <tr class="LC_info_row">';
 1157:             if ($numheaders == 5) {
 1158:                 $output .= '
 1159:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1160:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1161:              </tr>';
 1162:             } else {
 1163:                 $output .= '
 1164:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1165:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1166:              </tr>';
 1167:             }
 1168:             $rowtotal ++;
 1169:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal).'
 1170:            </table>
 1171:           </td>
 1172:          </tr>
 1173:          <tr>
 1174:            <td>
 1175:             <table class="LC_nested">
 1176:              <tr class="LC_info_row">';
 1177:             if ($numheaders == 5) {
 1178:                 $output .= '
 1179:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1180:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1181:              </tr>';
 1182:             } else {
 1183:                 $output .= '
 1184:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1185:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1186:              </tr>';
 1187:             }
 1188:             $rowtotal ++;
 1189:             $output .= &print_login('saml',$dom,$confname,$phase,$settings,\$rowtotal);
 1190:         } elsif ($action eq 'requestcourses') {
 1191:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1192:             $rowtotal ++;
 1193:             $output .= &print_studentcode($settings,\$rowtotal).'
 1194:            </table>
 1195:           </td>
 1196:          </tr>
 1197:          <tr>
 1198:            <td>
 1199:             <table class="LC_nested">
 1200:              <tr class="LC_info_row">
 1201:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1202:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1203:                        &textbookcourses_javascript($settings).
 1204:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1205:             </table>
 1206:            </td>
 1207:           </tr>
 1208:          <tr>
 1209:            <td>
 1210:             <table class="LC_nested">
 1211:              <tr class="LC_info_row">
 1212:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1213:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1214:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1215:             </table>
 1216:            </td>
 1217:           </tr>
 1218:           <tr>
 1219:            <td>
 1220:             <table class="LC_nested">
 1221:              <tr class="LC_info_row">
 1222:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1223:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1224:              </tr>'.
 1225:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1226:         } elsif ($action eq 'requestauthor') {
 1227:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1228:             $rowtotal ++;
 1229:         } elsif ($action eq 'rolecolors') {
 1230:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1231:            </table>
 1232:           </td>
 1233:          </tr>
 1234:          <tr>
 1235:            <td>
 1236:             <table class="LC_nested">
 1237:              <tr class="LC_info_row">
 1238:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1239:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1240:               <td class="LC_right_item" style="vertical-align: top">'.
 1241:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1242:              </tr>'.
 1243:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1244:            </table>
 1245:           </td>
 1246:          </tr>
 1247:          <tr>
 1248:            <td>
 1249:             <table class="LC_nested">
 1250:              <tr class="LC_info_row">
 1251:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1252:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1253:              </tr>'.
 1254:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1255:             $rowtotal += 2;
 1256:         }
 1257:     } else {
 1258:         $output .= '
 1259:           <tr>
 1260:            <td>
 1261:             <table class="LC_nested">
 1262:              <tr class="LC_info_row">';
 1263:         if ($action eq 'login') {
 1264:             $output .= '  
 1265:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1266:         } elsif ($action eq 'serverstatuses') {
 1267:             $output .= '
 1268:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1269:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1270: 
 1271:         } else {
 1272:             $output .= '
 1273:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1274:         }
 1275:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1276:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1277:                        &mt($item->{'header'}->[0]->{'col2'});
 1278:             if ($action eq 'serverstatuses') {
 1279:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1280:             } 
 1281:         } else {
 1282:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1283:                        &mt($item->{'header'}->[0]->{'col2'});
 1284:         }
 1285:         $output .= '</td>';
 1286:         if ($item->{'header'}->[0]->{'col3'}) {
 1287:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1288:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1289:                             &mt($item->{'header'}->[0]->{'col3'});
 1290:             } else {
 1291:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1292:                            &mt($item->{'header'}->[0]->{'col3'});
 1293:             }
 1294:             if ($action eq 'serverstatuses') {
 1295:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1296:             }
 1297:             $output .= '</td>';
 1298:         }
 1299:         if ($item->{'header'}->[0]->{'col4'}) {
 1300:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1301:                        &mt($item->{'header'}->[0]->{'col4'});
 1302:         }
 1303:         $output .= '</tr>';
 1304:         $rowtotal ++;
 1305:         if ($action eq 'quotas') {
 1306:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1307:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1308:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1309:                  ($action eq 'proctoring') || ($action eq 'ipaccess')) {
 1310:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1311:         }
 1312:     }
 1313:     $output .= '
 1314:    </table>
 1315:   </td>
 1316:  </tr>
 1317: </table><br />';
 1318:     return ($output,$rowtotal);
 1319: }
 1320: 
 1321: sub print_login {
 1322:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1323:     my ($css_class,$datatable,$switchserver,%lt);
 1324:     my %choices = &login_choices();
 1325:     if (($caller eq 'help') || ($caller eq 'headtag') || ($caller eq 'saml')) {
 1326:         %lt = &login_file_options();
 1327:         $switchserver = &check_switchserver($dom,$confname);
 1328:     }
 1329:     if ($caller eq 'service') {
 1330:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1331:         my $choice = $choices{'disallowlogin'};
 1332:         $css_class = ' class="LC_odd_row"';
 1333:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1334:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1335:                       '<th>'.$choices{'server'}.'</th>'.
 1336:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1337:                       '<th>'.$choices{'custompath'}.'</th>'.
 1338:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1339:         my %disallowed;
 1340:         if (ref($settings) eq 'HASH') {
 1341:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1342:                %disallowed = %{$settings->{'loginvia'}};
 1343:             }
 1344:         }
 1345:         foreach my $lonhost (sort(keys(%servers))) {
 1346:             my $direct = 'selected="selected"';
 1347:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1348:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1349:                     $direct = '';
 1350:                 }
 1351:             }
 1352:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1353:                           '<td><select name="'.$lonhost.'_server">'.
 1354:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1355:                           '</option>';
 1356:             foreach my $hostid (sort(keys(%servers))) {
 1357:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1358:                 my $selected = '';
 1359:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1360:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1361:                         $selected = 'selected="selected"';
 1362:                     }
 1363:                 }
 1364:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1365:                               $servers{$hostid}.'</option>';
 1366:             }
 1367:             $datatable .= '</select></td>'.
 1368:                           '<td><select name="'.$lonhost.'_serverpath">';
 1369:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1370:                 my $pathname = $path;
 1371:                 if ($path eq 'custom') {
 1372:                     $pathname = &mt('Custom Path').' ->';
 1373:                 }
 1374:                 my $selected = '';
 1375:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1376:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1377:                         $selected = 'selected="selected"';
 1378:                     }
 1379:                 } elsif ($path eq '') {
 1380:                     $selected = 'selected="selected"';
 1381:                 }
 1382:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1383:             }
 1384:             $datatable .= '</select></td>';
 1385:             my ($custom,$exempt);
 1386:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1387:                 $custom = $disallowed{$lonhost}{'custompath'};
 1388:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1389:             }
 1390:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1391:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1392:                           '</tr>';
 1393:         }
 1394:         $datatable .= '</table></td></tr>';
 1395:         return $datatable;
 1396:     } elsif ($caller eq 'page') {
 1397:         my %defaultchecked = ( 
 1398:                                'coursecatalog' => 'on',
 1399:                                'helpdesk'      => 'on',
 1400:                                'adminmail'     => 'off',
 1401:                                'newuser'       => 'off',
 1402:                              );
 1403:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1404:         my (%checkedon,%checkedoff);
 1405:         foreach my $item (@toggles) {
 1406:             if ($defaultchecked{$item} eq 'on') { 
 1407:                 $checkedon{$item} = ' checked="checked" ';
 1408:                 $checkedoff{$item} = ' ';
 1409:             } elsif ($defaultchecked{$item} eq 'off') {
 1410:                 $checkedoff{$item} = ' checked="checked" ';
 1411:                 $checkedon{$item} = ' ';
 1412:             }
 1413:         }
 1414:         my @images = ('img','logo','domlogo','login');
 1415:         my @alttext = ('img','logo','domlogo');
 1416:         my @logintext = ('textcol','bgcol');
 1417:         my @bgs = ('pgbg','mainbg','sidebg');
 1418:         my @links = ('link','alink','vlink');
 1419:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1420:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1421:         my (%is_custom,%designs);
 1422:         my %defaults = (
 1423:                        font => $defaultdesign{'login.font'},
 1424:                        );
 1425:         foreach my $item (@images) {
 1426:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1427:             $defaults{'showlogo'}{$item} = 1;
 1428:         }
 1429:         foreach my $item (@bgs) {
 1430:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1431:         }
 1432:         foreach my $item (@logintext) {
 1433:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1434:         }
 1435:         foreach my $item (@links) {
 1436:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1437:         }
 1438:         if (ref($settings) eq 'HASH') {
 1439:             foreach my $item (@toggles) {
 1440:                 if ($settings->{$item} eq '1') {
 1441:                     $checkedon{$item} =  ' checked="checked" ';
 1442:                     $checkedoff{$item} = ' ';
 1443:                 } elsif ($settings->{$item} eq '0') {
 1444:                     $checkedoff{$item} =  ' checked="checked" ';
 1445:                     $checkedon{$item} = ' ';
 1446:                 }
 1447:             }
 1448:             foreach my $item (@images) {
 1449:                 if (defined($settings->{$item})) {
 1450:                     $designs{$item} = $settings->{$item};
 1451:                     $is_custom{$item} = 1;
 1452:                 }
 1453:                 if (defined($settings->{'showlogo'}{$item})) {
 1454:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1455:                 }
 1456:             }
 1457:             foreach my $item (@alttext) {
 1458:                 if (ref($settings->{'alttext'}) eq 'HASH') {
 1459:                     if ($settings->{'alttext'}->{$item} ne '') {
 1460:                         $designs{'alttext'}{$item} = $settings->{'alttext'}{$item};
 1461:                     }
 1462:                 }
 1463:             }
 1464:             foreach my $item (@logintext) {
 1465:                 if ($settings->{$item} ne '') {
 1466:                     $designs{'logintext'}{$item} = $settings->{$item};
 1467:                     $is_custom{$item} = 1;
 1468:                 }
 1469:             }
 1470:             if ($settings->{'font'} ne '') {
 1471:                 $designs{'font'} = $settings->{'font'};
 1472:                 $is_custom{'font'} = 1;
 1473:             }
 1474:             foreach my $item (@bgs) {
 1475:                 if ($settings->{$item} ne '') {
 1476:                     $designs{'bgs'}{$item} = $settings->{$item};
 1477:                     $is_custom{$item} = 1;
 1478:                 }
 1479:             }
 1480:             foreach my $item (@links) {
 1481:                 if ($settings->{$item} ne '') {
 1482:                     $designs{'links'}{$item} = $settings->{$item};
 1483:                     $is_custom{$item} = 1;
 1484:                 }
 1485:             }
 1486:         } else {
 1487:             if ($designhash{$dom.'.login.font'} ne '') {
 1488:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1489:                 $is_custom{'font'} = 1;
 1490:             }
 1491:             foreach my $item (@images) {
 1492:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1493:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1494:                     $is_custom{$item} = 1;
 1495:                 }
 1496:             }
 1497:             foreach my $item (@bgs) {
 1498:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1499:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1500:                     $is_custom{$item} = 1;
 1501:                 }
 1502:             }
 1503:             foreach my $item (@links) {
 1504:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1505:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1506:                     $is_custom{$item} = 1;
 1507:                 }
 1508:             }
 1509:         }
 1510:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1511:                                                       logo => 'Institution Logo',
 1512:                                                       domlogo => 'Domain Logo',
 1513:                                                       login => 'Login box');
 1514:         my $itemcount = 1;
 1515:         foreach my $item (@toggles) {
 1516:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1517:             $datatable .=  
 1518:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1519:                 '</td><td>'.
 1520:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1521:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1522:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1523:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1524:                 '</tr>';
 1525:             $itemcount ++;
 1526:         }
 1527:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1528:         $datatable .= '</tr></table></td></tr>';
 1529:     } elsif ($caller eq 'help') {
 1530:         my ($defaulturl,$defaulttype,%url,%type,%langchoices);
 1531:         my $itemcount = 1;
 1532:         $defaulturl = '/adm/loginproblems.html';
 1533:         $defaulttype = 'default';
 1534:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1535:         my @currlangs;
 1536:         if (ref($settings) eq 'HASH') {
 1537:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1538:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1539:                     next if ($settings->{'helpurl'}{$key} eq '');
 1540:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1541:                     $type{$key} = 'custom';
 1542:                     unless ($key eq 'nolang') {
 1543:                         push(@currlangs,$key);
 1544:                     }
 1545:                 }
 1546:             } elsif ($settings->{'helpurl'} ne '') {
 1547:                 $type{'nolang'} = 'custom';
 1548:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1549:             }
 1550:         }
 1551:         foreach my $lang ('nolang',sort(@currlangs)) {
 1552:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1553:             $datatable .= '<tr'.$css_class.'>';
 1554:             if ($url{$lang} eq '') {
 1555:                 $url{$lang} = $defaulturl;
 1556:             }
 1557:             if ($type{$lang} eq '') {
 1558:                 $type{$lang} = $defaulttype;
 1559:             }
 1560:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1561:             if ($lang eq 'nolang') {
 1562:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1563:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1564:             } else {
 1565:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1566:                                   $langchoices{$lang},
 1567:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1568:             }
 1569:             $datatable .= '</span></td>'."\n".
 1570:                           '<td class="LC_left_item">';
 1571:             if ($type{$lang} eq 'custom') {
 1572:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1573:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1574:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1575:             } else {
 1576:                 $datatable .= $lt{'upl'};
 1577:             }
 1578:             $datatable .='<br />';
 1579:             if ($switchserver) {
 1580:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1581:             } else {
 1582:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1583:             }
 1584:             $datatable .= '</td></tr>';
 1585:             $itemcount ++;
 1586:         }
 1587:         my @addlangs;
 1588:         foreach my $lang (sort(keys(%langchoices))) {
 1589:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1590:             push(@addlangs,$lang);
 1591:         }
 1592:         if (@addlangs > 0) {
 1593:             my %toadd;
 1594:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1595:             $toadd{''} = &mt('Select');
 1596:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1597:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1598:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1599:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1600:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1601:             if ($switchserver) {
 1602:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1603:             } else {
 1604:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1605:             }
 1606:             $datatable .= '</td></tr>';
 1607:             $itemcount ++;
 1608:         }
 1609:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1610:     } elsif ($caller eq 'headtag') {
 1611:         my %domservers = &Apache::lonnet::get_servers($dom);
 1612:         my $choice = $choices{'headtag'};
 1613:         $css_class = ' class="LC_odd_row"';
 1614:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1615:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1616:                       '<th>'.$choices{'current'}.'</th>'.
 1617:                       '<th>'.$choices{'action'}.'</th>'.
 1618:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1619:         my (%currurls,%currexempt);
 1620:         if (ref($settings) eq 'HASH') {
 1621:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1622:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1623:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1624:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1625:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1626:                     }
 1627:                 }
 1628:             }
 1629:         }
 1630:         foreach my $lonhost (sort(keys(%domservers))) {
 1631:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1632:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1633:             if ($currurls{$lonhost}) {
 1634:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1635:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1636:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1637:                               '">'.$lt{'curr'}.'</a></td>'.
 1638:                               '<td><span class="LC_nobreak"><label>'.
 1639:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1640:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1641:             } else {
 1642:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1643:             }
 1644:             $datatable .='<br />';
 1645:             if ($switchserver) {
 1646:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1647:             } else {
 1648:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1649:             }
 1650:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1651:         }
 1652:         $datatable .= '</table></td></tr>';
 1653:     } elsif ($caller eq 'saml') {
 1654:         my %domservers = &Apache::lonnet::get_servers($dom);
 1655:         $datatable .= '<tr><td colspan="3" style="text-align: left">'.
 1656:                       '<table><tr><th>'.$choices{'hostid'}.'</th>'.
 1657:                       '<th>'.$choices{'samllanding'}.'</th>'.
 1658:                       '<th>'.$choices{'samloptions'}.'</th></tr>'."\n";
 1659:         my (%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso,%styleon,%styleoff);
 1660:         foreach my $lonhost (keys(%domservers)) {
 1661:             $samlurl{$lonhost} = '/adm/sso';
 1662:             $styleon{$lonhost} = 'display:none';
 1663:             $styleoff{$lonhost} = '';
 1664:         }
 1665:         if ((ref($settings) eq 'HASH') && (ref($settings->{'saml'}) eq 'HASH')) {
 1666:             foreach my $lonhost (keys(%{$settings->{'saml'}})) {
 1667:                 if (ref($settings->{'saml'}{$lonhost}) eq 'HASH') {
 1668:                     $saml{$lonhost} = 1;
 1669:                     $samltext{$lonhost} = $settings->{'saml'}{$lonhost}{'text'};
 1670:                     $samlimg{$lonhost} = $settings->{'saml'}{$lonhost}{'img'};
 1671:                     $samlalt{$lonhost} = $settings->{'saml'}{$lonhost}{'alt'};
 1672:                     $samlurl{$lonhost} = $settings->{'saml'}{$lonhost}{'url'};
 1673:                     $samltitle{$lonhost} = $settings->{'saml'}{$lonhost}{'title'};
 1674:                     $samlwindow{$lonhost} = $settings->{'saml'}{$lonhost}{'window'};
 1675:                     $samlnotsso{$lonhost} = $settings->{'saml'}{$lonhost}{'notsso'};
 1676:                     $styleon{$lonhost} = '';
 1677:                     $styleoff{$lonhost} = 'display:none';
 1678:                 } else {
 1679:                     $styleon{$lonhost} = 'display:none';
 1680:                     $styleoff{$lonhost} = '';
 1681:                 }
 1682:             }
 1683:         }
 1684:         my $itemcount = 1;
 1685:         foreach my $lonhost (sort(keys(%domservers))) {
 1686:             my $samlon = ' ';
 1687:             my $samloff = ' checked="checked" ';
 1688:             if ($saml{$lonhost}) {
 1689:                 $samlon = $samloff;
 1690:                 $samloff = ' ';
 1691:             }
 1692:             my $samlwinon = '';
 1693:             my $samlwinoff = ' checked="checked"';
 1694:             if ($samlwindow{$lonhost}) {
 1695:                 $samlwinon = $samlwinoff;
 1696:                 $samlwinoff = '';
 1697:             }
 1698:             my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1699:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.$domservers{$lonhost}.'</span></td>'.
 1700:                           '<td><span class="LC_nobreak"><label><input type="radio" name="saml_'.$lonhost.'"'.$samloff.
 1701:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="0" />'.
 1702:                           &mt('No').'</label>'.('&nbsp;'x2).
 1703:                           '<label><input type="radio" name="saml_'.$lonhost.'"'.$samlon.
 1704:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="1" />'.
 1705:                           &mt('Yes').'</label></span></td>'.
 1706:                           '<td id="samloptionson_'.$lonhost.'" style="'.$styleon{$lonhost}.'" width="100%">'.
 1707:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th></tr>'.
 1708:                           '<tr><th>'.&mt('Text').'</th><th>'.&mt('Image').'</th>'.
 1709:                           '<th>'.&mt('Alt Text').'</th></tr>'.
 1710:                           '<tr'.$css_class.'><td><input type="text" name="saml_text_'.$lonhost.'" size="20" value="'.
 1711:                           $samltext{$lonhost}.'" /></td><td>';
 1712:             if ($samlimg{$lonhost}) {
 1713:                 $datatable .= '<img src="'.$samlimg{$lonhost}.'" /><br />'.
 1714:                               '<span class="LC_nobreak"><label>'.
 1715:                               '<input type="checkbox" name="saml_img_del" value="'.$lonhost.'" />'.
 1716:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1717:             } else {
 1718:                 $datatable .= $lt{'upl'};
 1719:             }
 1720:             $datatable .='<br />';
 1721:             if ($switchserver) {
 1722:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1723:             } else {
 1724:                 $datatable .= '<input type="file" name="saml_img_'.$lonhost.'" />';
 1725:             }
 1726:             $datatable .= '</td>'.
 1727:                           '<td><input type="text" name="saml_alt_'.$lonhost.'" size="25" '.
 1728:                           'value="'.$samlalt{$lonhost}.'" /></td></tr></table><br />'.
 1729:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th><th align="center">'.
 1730:                           '<span class="LC_nobreak">'.&mt('Non-SSO').'</span></th></tr>'.
 1731:                           '<tr><th>'.&mt('URL').'</th><th>'.&mt('Tool Tip').'</th>'.
 1732:                           '<th>'.&mt('Pop-up if iframe').'</th><th>'.&mt('Text').'</th></tr>'.
 1733:                           '<tr'.$css_class.'>'.
 1734:                           '<td><input type="text" name="saml_url_'.$lonhost.'" size="30" '.
 1735:                           'value="'.$samlurl{$lonhost}.'" /></td>'.
 1736:                           '<td><textarea name="saml_title_'.$lonhost.'" rows="3" cols="20">'.
 1737:                           $samltitle{$lonhost}.'</textarea></td>'.
 1738:                           '<td><label><input type="radio" name="saml_window_'.$lonhost.'" value=""'.$samlwinoff.'>'.
 1739:                           &mt('No').'</label>'.('&nbsp;'x2).'<label><input type="radio" '.
 1740:                           'name="saml_window_'.$lonhost.'" value="1"'.$samlwinon.'>'.&mt('Yes').'</label></td>'.
 1741:                           '<td><input type="text" name="saml_notsso_'.$lonhost.'" size="12" '.
 1742:                           'value="'.$samlnotsso{$lonhost}.'" /></td></tr>'.
 1743:                           '</table></td>'.
 1744:                           '<td id="samloptionsoff_'.$lonhost.'" style="'.$styleoff{$lonhost}.'" width="100%">&nbsp;</td></tr>';
 1745:            $itemcount ++;
 1746:         }
 1747:         $datatable .= '</table></td></tr>';
 1748:     }
 1749:     return $datatable;
 1750: }
 1751: 
 1752: sub login_choices {
 1753:     my %choices =
 1754:         &Apache::lonlocal::texthash (
 1755:             coursecatalog => 'Display Course/Community Catalog link?',
 1756:             adminmail     => "Display Administrator's E-mail Address?",
 1757:             helpdesk      => 'Display "Contact Helpdesk" link',
 1758:             disallowlogin => "Login page requests redirected",
 1759:             hostid        => "Server",
 1760:             server        => "Redirect to:",
 1761:             serverpath    => "Path",
 1762:             custompath    => "Custom", 
 1763:             exempt        => "Exempt IP(s)",
 1764:             directlogin   => "No redirect",
 1765:             newuser       => "Link to create a user account",
 1766:             img           => "Header",
 1767:             logo          => "Main Logo",
 1768:             domlogo       => "Domain Logo",
 1769:             login         => "Log-in Header", 
 1770:             textcol       => "Text color",
 1771:             bgcol         => "Box color",
 1772:             bgs           => "Background colors",
 1773:             links         => "Link colors",
 1774:             font          => "Font color",
 1775:             pgbg          => "Header",
 1776:             mainbg        => "Page",
 1777:             sidebg        => "Login box",
 1778:             link          => "Link",
 1779:             alink         => "Active link",
 1780:             vlink         => "Visited link",
 1781:             headtag       => "Custom markup",
 1782:             action        => "Action",
 1783:             current       => "Current",
 1784:             samllanding   => "Dual login?",
 1785:             samloptions   => "Options",
 1786:             alttext       => "Alt text",
 1787:         );
 1788:     return %choices;
 1789: }
 1790: 
 1791: sub login_file_options {
 1792:       return &Apache::lonlocal::texthash(
 1793:                                            del     => 'Delete?',
 1794:                                            rep     => 'Replace:',
 1795:                                            upl     => 'Upload:',
 1796:                                            curr    => 'View contents',
 1797:                                            default => 'Default',
 1798:                                            custom  => 'Custom',
 1799:                                            none    => 'None',
 1800:       );
 1801: }
 1802: 
 1803: sub print_ipaccess {
 1804:     my ($dom,$settings,$rowtotal) = @_;
 1805:     my $css_class;
 1806:     my $itemcount = 0;
 1807:     my $datatable;
 1808:     my %ordered;
 1809:     if (ref($settings) eq 'HASH') {
 1810:         foreach my $item (keys(%{$settings})) {
 1811:             if (ref($settings->{$item}) eq 'HASH') {
 1812:                 my $num = $settings->{$item}{'order'};
 1813:                 if ($num eq '') {
 1814:                     $num = scalar(keys(%{$settings}));
 1815:                 }
 1816:                 $ordered{$num} = $item;
 1817:             }
 1818:         }
 1819:     }
 1820:     my $maxnum = scalar(keys(%ordered));
 1821:     if (keys(%ordered)) {
 1822:         my @items = sort { $a <=> $b } keys(%ordered);
 1823:         for (my $i=0; $i<@items; $i++) {
 1824:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1825:             my $item = $ordered{$items[$i]};
 1826:             my ($name,$ipranges,%commblocks,%courses);
 1827:             if (ref($settings->{$item}) eq 'HASH') {
 1828:                 $name = $settings->{$item}->{'name'};
 1829:                 $ipranges = $settings->{$item}->{'ip'};
 1830:                 if (ref($settings->{$item}->{'commblocks'}) eq 'HASH') {
 1831:                     %commblocks = %{$settings->{$item}->{'commblocks'}};
 1832:                 }
 1833:                 if (ref($settings->{$item}->{'courses'}) eq 'HASH') {
 1834:                     %courses = %{$settings->{$item}->{'courses'}};
 1835:                 }
 1836:             }
 1837:             my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_".$item."'".');"';
 1838:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 1839:                          .'<select name="ipaccess_pos_'.$item.'"'.$chgstr.'>';
 1840:             for (my $k=0; $k<=$maxnum; $k++) {
 1841:                 my $vpos = $k+1;
 1842:                 my $selstr;
 1843:                 if ($k == $i) {
 1844:                     $selstr = ' selected="selected" ';
 1845:                 }
 1846:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1847:             }
 1848:             $datatable .= '</select>'.('&nbsp;'x2).
 1849:                 '<label><input type="checkbox" name="ipaccess_del" value="'.$item.'" />'.
 1850:                 &mt('Delete?').'</label></span></td>'.
 1851:                 '<td colspan="2"><input type="hidden" name="ipaccess_id_'.$i.'" value="'.$item.'" />'.
 1852:                 &ipaccess_options($i,$itemcount,$dom,$name,$ipranges,\%commblocks,\%courses).
 1853:                 '</td></tr>';
 1854:             $itemcount ++;
 1855:         }
 1856:     }
 1857:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1858:     my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_add'".');"';
 1859:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 1860:                   '<input type="hidden" name="ipaccess_maxnum" value="'.$maxnum.'" />'."\n".
 1861:                   '<select name="ipaccess_pos_add"'.$chgstr.'>';
 1862:     for (my $k=0; $k<$maxnum+1; $k++) {
 1863:         my $vpos = $k+1;
 1864:         my $selstr;
 1865:         if ($k == $maxnum) {
 1866:             $selstr = ' selected="selected" ';
 1867:         }
 1868:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1869:     }
 1870:     $datatable .= '</select>&nbsp;'."\n".
 1871:                   '<input type="checkbox" name="ipaccess_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 1872:                   '<td colspan="2">'.
 1873:                   &ipaccess_options('add',$itemcount,$dom).
 1874:                   '</td>'."\n".
 1875:                   '</tr>'."\n";
 1876:     $$rowtotal ++;
 1877:     return $datatable;
 1878: }
 1879: 
 1880: sub ipaccess_options {
 1881:     my ($num,$itemcount,$dom,$name,$ipranges,$blocksref,$coursesref) = @_;
 1882:     my (%currblocks,%currcourses,$output);
 1883:     if (ref($blocksref) eq 'HASH') {
 1884:         %currblocks = %{$blocksref};
 1885:     }
 1886:     if (ref($coursesref) eq 'HASH') {
 1887:         %currcourses = %{$coursesref};
 1888:     }
 1889:     $output = '<fieldset><legend>'.&mt('Location(s)').'</legend>'.
 1890:               '<span class="LC_nobreak">'.&mt('Name').':&nbsp;'.
 1891:               '<input type="text" name="ipaccess_name_'.$num.'" value="'.$name.'" />'.
 1892:               '</span></fieldset>'.
 1893:               '<fieldset><legend>'.&mt('IP Range(s)').'</legend>'.
 1894:               &mt('Format for each IP range').': '.&mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 1895:               &mt('Range(s) will be stored as IP netblock(s) in CIDR notation (comma separated)').'<br />'.
 1896:               '<textarea name="ipaccess_range_'.$num.'" rows="3" cols="80">'.
 1897:               $ipranges.'</textarea></fieldset>'.
 1898:               '<fieldset><legend>'.&mt('Functionality Blocked?').'</legend>'.
 1899:               &blocker_checkboxes($num,$blocksref).'</fieldset>'.
 1900:               '<fieldset><legend>'.&mt('Courses/Communities allowed').'</legend>'.
 1901:               '<table>';
 1902:     foreach my $cid (sort(keys(%currcourses))) {
 1903:         my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
 1904:         $output .= '<tr><td><span class="LC_nobreak">'.
 1905:                    '<label><input type="checkbox" name="ipaccess_course_delete_'.$num.'" value="'.$cid.'" />'.
 1906:                    &mt('Delete?').'&nbsp;<span class="LC_cusr_emph">'.$courseinfo{'description'}.'</span></label></span>'.
 1907:                    ' <span class="LC_fontsize_medium">('.$cid.')</span></td></tr>';
 1908:     }
 1909:     $output .= '<tr><td><span class="LC_nobreak">'.&mt('Add').':&nbsp;'.
 1910:                '<input type="text" name="ipaccess_cdesc_'.$num.'" value="" onfocus="this.blur();opencrsbrowser('."'display','ipaccess_cnum_$num','ipaccess_cdom_$num','ipaccess_cdesc_$num'".');" />'.
 1911:                 &Apache::loncommon::selectcourse_link('display','ipaccess_cnum_'.$num,'ipaccess_cdom_'.$num,'ipaccess_cdesc_'.$num,$dom,undef,'Course/Community').
 1912:                '<input type="hidden" name="ipaccess_cnum_'.$num.'" value="" />'.
 1913:                '<input type="hidden" name="ipaccess_cdom_'.$num.'" value="" />'.
 1914:                '</span></td></tr></table>'."\n".
 1915:                '</fieldset>';
 1916:     return $output;
 1917: }
 1918: 
 1919: sub blocker_checkboxes {
 1920:     my ($num,$blocks) = @_;
 1921:     my ($typeorder,$types) = &commblocktype_text();
 1922:     my $numinrow = 6;
 1923:     my $output = '<table>';
 1924:     for (my $i=0; $i<@{$typeorder}; $i++) {
 1925:         my $block = $typeorder->[$i];
 1926:         my $blockstatus;
 1927:         if (ref($blocks) eq 'HASH') {
 1928:             if ($blocks->{$block} eq 'on') {
 1929:                 $blockstatus = 'checked="checked"';
 1930:             }
 1931:         }
 1932:         my $rem = $i%($numinrow);
 1933:         if ($rem == 0) {
 1934:             if ($i > 0) {
 1935:                 $output .= '</tr>';
 1936:             }
 1937:             $output .= '<tr>';
 1938:         }
 1939:         if ($i == scalar(@{$typeorder})-1) {
 1940:             my $colsleft = $numinrow-$rem;
 1941:             if ($colsleft > 1) {
 1942:                 $output .= '<td colspan="'.$colsleft.'">';
 1943:             } else {
 1944:                 $output .= '<td>';
 1945:             }
 1946:         } else {
 1947:             $output .= '<td>';
 1948:         }
 1949:         my $item = 'ipaccess_block_'.$num;
 1950:         if ($blockstatus) {
 1951:             $blockstatus = ' '.$blockstatus;
 1952:         }
 1953:         $output .= '<span class="LC_nobreak"><label>'."\n".
 1954:                    '<input type="checkbox" name="'.$item.'"'.
 1955:                    $blockstatus.' value="'.$block.'"'.' />'.
 1956:                    $types->{$block}.'</label></span>'."\n".
 1957:                    '<br /></td>';
 1958:     }
 1959:     $output .= '</tr></table>';
 1960:     return $output;
 1961: }
 1962: 
 1963: sub commblocktype_text {
 1964:     my %types = &Apache::lonlocal::texthash(
 1965:         'com' => 'Messaging',
 1966:         'chat' => 'Chat Room',
 1967:         'boards' => 'Discussion',
 1968:         'port' => 'Portfolio',
 1969:         'groups' => 'Groups',
 1970:         'blogs' => 'Blogs',
 1971:         'about' => 'User Information',
 1972:         'printout' => 'Printouts',
 1973:         'passwd' => 'Change Password',
 1974:         'grades' => 'Gradebook',
 1975:         'search' => 'Course search',
 1976:         'wishlist' => 'Stored links',
 1977:         'annotate' => 'Annotations',
 1978:     );
 1979:     my $typeorder = ['com','chat','boards','port','groups','blogs','about','wishlist','printout','grades','search','annotate','passwd'];
 1980:     return ($typeorder,\%types);
 1981: }
 1982: 
 1983: sub print_rolecolors {
 1984:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1985:     my %choices = &color_font_choices();
 1986:     my @bgs = ('pgbg','tabbg','sidebg');
 1987:     my @links = ('link','alink','vlink');
 1988:     my @images = ('img');
 1989:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1990:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1991:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1992:     my (%is_custom,%designs);
 1993:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1994:     if (ref($settings) eq 'HASH') {
 1995:         if (ref($settings->{$role}) eq 'HASH') {
 1996:             if ($settings->{$role}->{'img'} ne '') {
 1997:                 $designs{'img'} = $settings->{$role}->{'img'};
 1998:                 $is_custom{'img'} = 1;
 1999:             }
 2000:             if ($settings->{$role}->{'font'} ne '') {
 2001:                 $designs{'font'} = $settings->{$role}->{'font'};
 2002:                 $is_custom{'font'} = 1;
 2003:             }
 2004:             if ($settings->{$role}->{'fontmenu'} ne '') {
 2005:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 2006:                 $is_custom{'fontmenu'} = 1;
 2007:             }
 2008:             foreach my $item (@bgs) {
 2009:                 if ($settings->{$role}->{$item} ne '') {
 2010:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 2011:                     $is_custom{$item} = 1;
 2012:                 }
 2013:             }
 2014:             foreach my $item (@links) {
 2015:                 if ($settings->{$role}->{$item} ne '') {
 2016:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 2017:                     $is_custom{$item} = 1;
 2018:                 }
 2019:             }
 2020:         }
 2021:     } else {
 2022:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 2023:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 2024:             $is_custom{'img'} = 1;
 2025:         }
 2026:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 2027:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 2028:             $is_custom{'fontmenu'} = 1; 
 2029:         }
 2030:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 2031:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 2032:             $is_custom{'font'} = 1;
 2033:         }
 2034:         foreach my $item (@bgs) {
 2035:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2036:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2037:                 $is_custom{$item} = 1;
 2038:             
 2039:             }
 2040:         }
 2041:         foreach my $item (@links) {
 2042:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2043:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2044:                 $is_custom{$item} = 1;
 2045:             }
 2046:         }
 2047:     }
 2048:     my $itemcount = 1;
 2049:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 2050:     $datatable .= '</tr></table></td></tr>';
 2051:     return $datatable;
 2052: }
 2053: 
 2054: sub role_defaults {
 2055:     my ($role,$bgs,$links,$images,$logintext) = @_;
 2056:     my %defaults;
 2057:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 2058:         return %defaults;
 2059:     }
 2060:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 2061:     if ($role eq 'login') {
 2062:         %defaults = (
 2063:                        font => $defaultdesign{$role.'.font'},
 2064:                     );
 2065:         if (ref($logintext) eq 'ARRAY') {
 2066:             foreach my $item (@{$logintext}) {
 2067:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 2068:             }
 2069:         }
 2070:         foreach my $item (@{$images}) {
 2071:             $defaults{'showlogo'}{$item} = 1;
 2072:         }
 2073:     } else {
 2074:         %defaults = (
 2075:                        img => $defaultdesign{$role.'.img'},
 2076:                        font => $defaultdesign{$role.'.font'},
 2077:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 2078:                     );
 2079:     }
 2080:     foreach my $item (@{$bgs}) {
 2081:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 2082:     }
 2083:     foreach my $item (@{$links}) {
 2084:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 2085:     }
 2086:     foreach my $item (@{$images}) {
 2087:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 2088:     }
 2089:     return %defaults;
 2090: }
 2091: 
 2092: sub display_color_options {
 2093:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 2094:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 2095:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 2096:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2097:     my $datatable = '<tr'.$css_class.'>'.
 2098:         '<td>'.$choices->{'font'}.'</td>';
 2099:     if (!$is_custom->{'font'}) {
 2100:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 2101:     } else {
 2102:         $datatable .= '<td>&nbsp;</td>';
 2103:     }
 2104:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 2105: 
 2106:     $datatable .= '<td><span class="LC_nobreak">'.
 2107:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 2108:                   ' value="'.$current_color.'" />&nbsp;'.
 2109:                   '&nbsp;</span></td></tr>';
 2110:     unless ($role eq 'login') { 
 2111:         $datatable .= '<tr'.$css_class.'>'.
 2112:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 2113:         if (!$is_custom->{'fontmenu'}) {
 2114:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 2115:         } else {
 2116:             $datatable .= '<td>&nbsp;</td>';
 2117:         }
 2118: 	$current_color = $designs->{'fontmenu'} ?
 2119: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 2120:         $datatable .= '<td><span class="LC_nobreak">'.
 2121:                       '<input class="colorchooser" type="text" size="10" name="'
 2122: 		      .$role.'_fontmenu"'.
 2123:                       ' value="'.$current_color.'" />&nbsp;'.
 2124:                       '&nbsp;</span></td></tr>';
 2125:     }
 2126:     my $switchserver = &check_switchserver($dom,$confname);
 2127:     foreach my $img (@{$images}) {
 2128: 	$itemcount ++;
 2129:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2130:         $datatable .= '<tr'.$css_class.'>'.
 2131:                       '<td>'.$choices->{$img};
 2132:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors,$alttext);
 2133:         if ($role eq 'login') {
 2134:             if ($img eq 'login') {
 2135:                 $login_hdr_pick =
 2136:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 2137:                 $logincolors =
 2138:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 2139:                                        $designs,$defaults);
 2140:             } else {
 2141:                 if ($img ne 'domlogo') {
 2142:                     $datatable.= &logo_display_options($img,$defaults,$designs);
 2143:                 }
 2144:                 if (ref($designs->{'alttext'}) eq 'HASH') {
 2145:                     $alttext = $designs->{'alttext'}{$img};
 2146:                 }
 2147:             }
 2148:         }
 2149:         $datatable .= '</td>';
 2150:         if ($designs->{$img} ne '') {
 2151:             $imgfile = $designs->{$img};
 2152: 	    $img_import = ($imgfile =~ m{^/adm/});
 2153:         } else {
 2154:             $imgfile = $defaults->{$img};
 2155:         }
 2156:         if ($imgfile) {
 2157:             my ($showfile,$fullsize);
 2158:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 2159:                 my $urldir = $1;
 2160:                 my $filename = $2;
 2161:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 2162:                 if (@info) {
 2163:                     my $thumbfile = 'tn-'.$filename;
 2164:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 2165:                     if (@thumb) {
 2166:                         $showfile = $urldir.'/'.$thumbfile;
 2167:                     } else {
 2168:                         $showfile = $imgfile;
 2169:                     }
 2170:                 } else {
 2171:                     $showfile = '';
 2172:                 }
 2173:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 2174:                 $showfile = $imgfile;
 2175:                 my $imgdir = $1;
 2176:                 my $filename = $2;
 2177:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 2178:                     $showfile = "/$imgdir/tn-".$filename;
 2179:                 } else {
 2180:                     my $input = $londocroot.$imgfile;
 2181:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 2182:                     if (!-e $output) {
 2183:                         my ($width,$height) = &thumb_dimensions();
 2184:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 2185:                         if ($fullwidth ne '' && $fullheight ne '') {
 2186:                             if ($fullwidth > $width && $fullheight > $height) { 
 2187:                                 my $size = $width.'x'.$height;
 2188:                                 my @args = ('convert','-sample',$size,$input,$output);
 2189:                                 system({$args[0]} @args);
 2190:                                 $showfile = "/$imgdir/tn-".$filename;
 2191:                             }
 2192:                         }
 2193:                     }
 2194:                 }
 2195:             }
 2196:             if ($showfile) {
 2197:                 if ($showfile =~ m{^/(adm|res)/}) {
 2198:                     if ($showfile =~ m{^/res/}) {
 2199:                         my $local_showfile =
 2200:                             &Apache::lonnet::filelocation('',$showfile);
 2201:                         &Apache::lonnet::repcopy($local_showfile);
 2202:                     }
 2203:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 2204:                 }
 2205:                 if ($imgfile) {
 2206:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 2207:                         if ($imgfile =~ m{^/res/}) {
 2208:                             my $local_imgfile =
 2209:                                 &Apache::lonnet::filelocation('',$imgfile);
 2210:                             &Apache::lonnet::repcopy($local_imgfile);
 2211:                         }
 2212:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 2213:                     } else {
 2214:                         $fullsize = $imgfile;
 2215:                     }
 2216:                 }
 2217:                 $datatable .= '<td>';
 2218:                 if ($img eq 'login') {
 2219:                     $datatable .= $login_hdr_pick;
 2220:                 } 
 2221:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 2222:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 2223:             } else {
 2224:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2225:                               &mt('Upload:').'<br />';
 2226:             }
 2227:         } else {
 2228:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2229:                           &mt('Upload:').'<br />';
 2230:         }
 2231:         if ($switchserver) {
 2232:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2233:         } else {
 2234:             if ($img ne 'login') { # suppress file selection for Log-in header
 2235:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 2236:             }
 2237:         }
 2238:         if (($role eq 'login') && ($img ne 'login')) {
 2239:             $datatable .= ('&nbsp;' x2).' <span class="LC_nobreak"><label>'.$choices->{'alttext'}.':'.
 2240:                           '<input type="text" name="'.$role.'_alt_'.$img.'" size="10" value="'.$alttext.'" />'.
 2241:                           '</label></span>';
 2242:         }
 2243:         $datatable .= '</td></tr>';
 2244:     }
 2245:     $itemcount ++;
 2246:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2247:     $datatable .= '<tr'.$css_class.'>'.
 2248:                   '<td>'.$choices->{'bgs'}.'</td>';
 2249:     my $bgs_def;
 2250:     foreach my $item (@{$bgs}) {
 2251:         if (!$is_custom->{$item}) {
 2252:             $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>';
 2253:         }
 2254:     }
 2255:     if ($bgs_def) {
 2256:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 2257:     } else {
 2258:         $datatable .= '<td>&nbsp;</td>';
 2259:     }
 2260:     $datatable .= '<td class="LC_right_item">'.
 2261:                   '<table border="0"><tr>';
 2262: 
 2263:     foreach my $item (@{$bgs}) {
 2264:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 2265: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 2266:         if ($designs->{'bgs'}{$item}) {
 2267:             $datatable .= '&nbsp;';
 2268:         }
 2269:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2270:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2271:     }
 2272:     $datatable .= '</tr></table></td></tr>';
 2273:     $itemcount ++;
 2274:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2275:     $datatable .= '<tr'.$css_class.'>'.
 2276:                   '<td>'.$choices->{'links'}.'</td>';
 2277:     my $links_def;
 2278:     foreach my $item (@{$links}) {
 2279:         if (!$is_custom->{$item}) {
 2280:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 2281:         }
 2282:     }
 2283:     if ($links_def) {
 2284:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 2285:     } else {
 2286:         $datatable .= '<td>&nbsp;</td>';
 2287:     }
 2288:     $datatable .= '<td class="LC_right_item">'.
 2289:                   '<table border="0"><tr>';
 2290:     foreach my $item (@{$links}) {
 2291: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 2292:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 2293:         if ($designs->{'links'}{$item}) {
 2294:             $datatable.='&nbsp;';
 2295:         }
 2296:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 2297:                       '" /></td>';
 2298:     }
 2299:     $$rowtotal += $itemcount;
 2300:     return $datatable;
 2301: }
 2302: 
 2303: sub logo_display_options {
 2304:     my ($img,$defaults,$designs) = @_;
 2305:     my $checkedon;
 2306:     if (ref($defaults) eq 'HASH') {
 2307:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 2308:             if ($defaults->{'showlogo'}{$img}) {
 2309:                 $checkedon = 'checked="checked" ';     
 2310:             }
 2311:         } 
 2312:     }
 2313:     if (ref($designs) eq 'HASH') {
 2314:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 2315:             if (defined($designs->{'showlogo'}{$img})) {
 2316:                 if ($designs->{'showlogo'}{$img} == 0) {
 2317:                     $checkedon = '';
 2318:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 2319:                     $checkedon = 'checked="checked" ';
 2320:                 }
 2321:             }
 2322:         }
 2323:     }
 2324:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 2325:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 2326:            &mt('show').'</label>'."\n";
 2327: }
 2328: 
 2329: sub login_header_options  {
 2330:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 2331:     my $output = '';
 2332:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 2333:         $output .= &mt('Text default(s):').'<br />';
 2334:         if (!$is_custom->{'textcol'}) {
 2335:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 2336:                        '&nbsp;&nbsp;&nbsp;';
 2337:         }
 2338:         if (!$is_custom->{'bgcol'}) {
 2339:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 2340:                        '<span id="css_'.$role.'_font" style="background-color: '.
 2341:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 2342:         }
 2343:         $output .= '<br />';
 2344:     }
 2345:     $output .='<br />';
 2346:     return $output;
 2347: }
 2348: 
 2349: sub login_text_colors {
 2350:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 2351:     my $color_menu = '<table border="0"><tr>';
 2352:     foreach my $item (@{$logintext}) {
 2353:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 2354:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 2355:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2356:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2357:     }
 2358:     $color_menu .= '</tr></table><br />';
 2359:     return $color_menu;
 2360: }
 2361: 
 2362: sub image_changes {
 2363:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 2364:     my $output;
 2365:     if ($img eq 'login') {
 2366:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 2367:     } elsif (!$is_custom) {
 2368:         if ($img ne 'domlogo') {
 2369:             $output = &mt('Default image:').'<br />';
 2370:         } else {
 2371:             $output = &mt('Default in use:').'<br />';
 2372:         }
 2373:     }
 2374:     if ($img ne 'login') {
 2375:         if ($img_import) {
 2376:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 2377:         }
 2378:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 2379:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 2380:         if ($is_custom) {
 2381:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 2382:                        '<input type="checkbox" name="'.
 2383:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 2384:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 2385:         } else {
 2386:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 2387:         }
 2388:     }
 2389:     return $output;
 2390: }
 2391: 
 2392: sub print_quotas {
 2393:     my ($dom,$settings,$rowtotal,$action) = @_;
 2394:     my $context;
 2395:     if ($action eq 'quotas') {
 2396:         $context = 'tools';
 2397:     } else {
 2398:         $context = $action;
 2399:     }
 2400:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 2401:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2402:     my $typecount = 0;
 2403:     my ($css_class,%titles);
 2404:     if ($context eq 'requestcourses') {
 2405:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 2406:         @options =('norequest','approval','validate','autolimit');
 2407:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2408:         %titles = &courserequest_titles();
 2409:     } elsif ($context eq 'requestauthor') {
 2410:         @usertools = ('author');
 2411:         @options = ('norequest','approval','automatic');
 2412:         %titles = &authorrequest_titles();
 2413:     } else {
 2414:         @usertools = ('aboutme','blog','webdav','portfolio','timezone');
 2415:         %titles = &tool_titles();
 2416:     }
 2417:     if (ref($types) eq 'ARRAY') {
 2418:         foreach my $type (@{$types}) {
 2419:             my ($currdefquota,$currauthorquota);
 2420:             unless (($context eq 'requestcourses') ||
 2421:                     ($context eq 'requestauthor')) {
 2422:                 if (ref($settings) eq 'HASH') {
 2423:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2424:                         $currdefquota = $settings->{defaultquota}->{$type};
 2425:                     } else {
 2426:                         $currdefquota = $settings->{$type};
 2427:                     }
 2428:                     if (ref($settings->{authorquota}) eq 'HASH') {
 2429:                         $currauthorquota = $settings->{authorquota}->{$type};
 2430:                     }
 2431:                 }
 2432:             }
 2433:             if (defined($usertypes->{$type})) {
 2434:                 $typecount ++;
 2435:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2436:                 $datatable .= '<tr'.$css_class.'>'.
 2437:                               '<td>'.$usertypes->{$type}.'</td>'.
 2438:                               '<td class="LC_left_item">';
 2439:                 if ($context eq 'requestcourses') {
 2440:                     $datatable .= '<table><tr>';
 2441:                 }
 2442:                 my %cell;  
 2443:                 foreach my $item (@usertools) {
 2444:                     if ($context eq 'requestcourses') {
 2445:                         my ($curroption,$currlimit);
 2446:                         if (ref($settings) eq 'HASH') {
 2447:                             if (ref($settings->{$item}) eq 'HASH') {
 2448:                                 $curroption = $settings->{$item}->{$type};
 2449:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2450:                                     $currlimit = $1; 
 2451:                                 }
 2452:                             }
 2453:                         }
 2454:                         if (!$curroption) {
 2455:                             $curroption = 'norequest';
 2456:                         }
 2457:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2458:                         foreach my $option (@options) {
 2459:                             my $val = $option;
 2460:                             if ($option eq 'norequest') {
 2461:                                 $val = 0;  
 2462:                             }
 2463:                             if ($option eq 'validate') {
 2464:                                 my $canvalidate = 0;
 2465:                                 if (ref($validations{$item}) eq 'HASH') { 
 2466:                                     if ($validations{$item}{$type}) {
 2467:                                         $canvalidate = 1;
 2468:                                     }
 2469:                                 }
 2470:                                 next if (!$canvalidate);
 2471:                             }
 2472:                             my $checked = '';
 2473:                             if ($option eq $curroption) {
 2474:                                 $checked = ' checked="checked"';
 2475:                             } elsif ($option eq 'autolimit') {
 2476:                                 if ($curroption =~ /^autolimit/) {
 2477:                                     $checked = ' checked="checked"';
 2478:                                 }                       
 2479:                             } 
 2480:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2481:                                   '<input type="radio" name="crsreq_'.$item.
 2482:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2483:                                   $titles{$option}.'</label>';
 2484:                             if ($option eq 'autolimit') {
 2485:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2486:                                                 $item.'_limit_'.$type.'" size="1" '.
 2487:                                                 'value="'.$currlimit.'" />';
 2488:                             }
 2489:                             $cell{$item} .= '</span> ';
 2490:                             if ($option eq 'autolimit') {
 2491:                                 $cell{$item} .= $titles{'unlimited'};
 2492:                             }
 2493:                         }
 2494:                     } elsif ($context eq 'requestauthor') {
 2495:                         my $curroption;
 2496:                         if (ref($settings) eq 'HASH') {
 2497:                             $curroption = $settings->{$type};
 2498:                         }
 2499:                         if (!$curroption) {
 2500:                             $curroption = 'norequest';
 2501:                         }
 2502:                         foreach my $option (@options) {
 2503:                             my $val = $option;
 2504:                             if ($option eq 'norequest') {
 2505:                                 $val = 0;
 2506:                             }
 2507:                             my $checked = '';
 2508:                             if ($option eq $curroption) {
 2509:                                 $checked = ' checked="checked"';
 2510:                             }
 2511:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2512:                                   '<input type="radio" name="authorreq_'.$type.
 2513:                                   '" value="'.$val.'"'.$checked.' />'.
 2514:                                   $titles{$option}.'</label></span>&nbsp; ';
 2515:                         }
 2516:                     } else {
 2517:                         my $checked = 'checked="checked" ';
 2518:                         if ($item eq 'timezone') {
 2519:                             $checked = '';
 2520:                         }
 2521:                         if (ref($settings) eq 'HASH') {
 2522:                             if (ref($settings->{$item}) eq 'HASH') {
 2523:                                 if (!$settings->{$item}->{$type}) {
 2524:                                     $checked = '';
 2525:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2526:                                     $checked =  'checked="checked" ';
 2527:                                 }
 2528:                             }
 2529:                         }
 2530:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2531:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2532:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2533:                                       '</label></span>&nbsp; ';
 2534:                     }
 2535:                 }
 2536:                 if ($context eq 'requestcourses') {
 2537:                     $datatable .= '</tr><tr>';
 2538:                     foreach my $item (@usertools) {
 2539:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2540:                     }
 2541:                     $datatable .= '</tr></table>';
 2542:                 }
 2543:                 $datatable .= '</td>';
 2544:                 unless (($context eq 'requestcourses') ||
 2545:                         ($context eq 'requestauthor')) {
 2546:                     $datatable .= 
 2547:                               '<td class="LC_right_item">'.
 2548:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2549:                               '<input type="text" name="quota_'.$type.
 2550:                               '" value="'.$currdefquota.
 2551:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2552:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2553:                               '<input type="text" name="authorquota_'.$type.
 2554:                               '" value="'.$currauthorquota.
 2555:                               '" size="5" /></span></td>';
 2556:                 }
 2557:                 $datatable .= '</tr>';
 2558:             }
 2559:         }
 2560:     }
 2561:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2562:         $defaultquota = '20';
 2563:         $authorquota = '500';
 2564:         if (ref($settings) eq 'HASH') {
 2565:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2566:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2567:             } elsif (defined($settings->{'default'})) {
 2568:                 $defaultquota = $settings->{'default'};
 2569:             }
 2570:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2571:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2572:             }
 2573:         }
 2574:     }
 2575:     $typecount ++;
 2576:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2577:     $datatable .= '<tr'.$css_class.'>'.
 2578:                   '<td>'.$othertitle.'</td>'.
 2579:                   '<td class="LC_left_item">';
 2580:     if ($context eq 'requestcourses') {
 2581:         $datatable .= '<table><tr>';
 2582:     }
 2583:     my %defcell;
 2584:     foreach my $item (@usertools) {
 2585:         if ($context eq 'requestcourses') {
 2586:             my ($curroption,$currlimit);
 2587:             if (ref($settings) eq 'HASH') {
 2588:                 if (ref($settings->{$item}) eq 'HASH') {
 2589:                     $curroption = $settings->{$item}->{'default'};
 2590:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2591:                         $currlimit = $1;
 2592:                     }
 2593:                 }
 2594:             }
 2595:             if (!$curroption) {
 2596:                 $curroption = 'norequest';
 2597:             }
 2598:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2599:             foreach my $option (@options) {
 2600:                 my $val = $option;
 2601:                 if ($option eq 'norequest') {
 2602:                     $val = 0;
 2603:                 }
 2604:                 if ($option eq 'validate') {
 2605:                     my $canvalidate = 0;
 2606:                     if (ref($validations{$item}) eq 'HASH') {
 2607:                         if ($validations{$item}{'default'}) {
 2608:                             $canvalidate = 1;
 2609:                         }
 2610:                     }
 2611:                     next if (!$canvalidate);
 2612:                 }
 2613:                 my $checked = '';
 2614:                 if ($option eq $curroption) {
 2615:                     $checked = ' checked="checked"';
 2616:                 } elsif ($option eq 'autolimit') {
 2617:                     if ($curroption =~ /^autolimit/) {
 2618:                         $checked = ' checked="checked"';
 2619:                     }
 2620:                 }
 2621:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2622:                                   '<input type="radio" name="crsreq_'.$item.
 2623:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2624:                                   $titles{$option}.'</label>';
 2625:                 if ($option eq 'autolimit') {
 2626:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2627:                                        $item.'_limit_default" size="1" '.
 2628:                                        'value="'.$currlimit.'" />';
 2629:                 }
 2630:                 $defcell{$item} .= '</span> ';
 2631:                 if ($option eq 'autolimit') {
 2632:                     $defcell{$item} .= $titles{'unlimited'};
 2633:                 }
 2634:             }
 2635:         } elsif ($context eq 'requestauthor') {
 2636:             my $curroption;
 2637:             if (ref($settings) eq 'HASH') {
 2638:                 $curroption = $settings->{'default'};
 2639:             }
 2640:             if (!$curroption) {
 2641:                 $curroption = 'norequest';
 2642:             }
 2643:             foreach my $option (@options) {
 2644:                 my $val = $option;
 2645:                 if ($option eq 'norequest') {
 2646:                     $val = 0;
 2647:                 }
 2648:                 my $checked = '';
 2649:                 if ($option eq $curroption) {
 2650:                     $checked = ' checked="checked"';
 2651:                 }
 2652:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2653:                               '<input type="radio" name="authorreq_default"'.
 2654:                               ' value="'.$val.'"'.$checked.' />'.
 2655:                               $titles{$option}.'</label></span>&nbsp; ';
 2656:             }
 2657:         } else {
 2658:             my $checked = 'checked="checked" ';
 2659:             if (ref($settings) eq 'HASH') {
 2660:                 if (ref($settings->{$item}) eq 'HASH') {
 2661:                     if ($settings->{$item}->{'default'} == 0) {
 2662:                         $checked = '';
 2663:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2664:                         $checked = 'checked="checked" ';
 2665:                     }
 2666:                 }
 2667:             }
 2668:             $datatable .= '<span class="LC_nobreak"><label>'.
 2669:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2670:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2671:                           '</label></span>&nbsp; ';
 2672:         }
 2673:     }
 2674:     if ($context eq 'requestcourses') {
 2675:         $datatable .= '</tr><tr>';
 2676:         foreach my $item (@usertools) {
 2677:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2678:         }
 2679:         $datatable .= '</tr></table>';
 2680:     }
 2681:     $datatable .= '</td>';
 2682:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2683:         $datatable .= '<td class="LC_right_item">'.
 2684:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2685:                       '<input type="text" name="defaultquota" value="'.
 2686:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2687:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2688:                       '<input type="text" name="authorquota" value="'.
 2689:                       $authorquota.'" size="5" /></span></td>';
 2690:     }
 2691:     $datatable .= '</tr>';
 2692:     $typecount ++;
 2693:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2694:     $datatable .= '<tr'.$css_class.'>'.
 2695:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2696:     if ($context eq 'requestcourses') {
 2697:         $datatable .= &mt('(overrides affiliation, if set)').
 2698:                       '</td>'.
 2699:                       '<td class="LC_left_item">'.
 2700:                       '<table><tr>';
 2701:     } else {
 2702:         $datatable .= &mt('(overrides affiliation, if checked)').
 2703:                       '</td>'.
 2704:                       '<td class="LC_left_item" colspan="2">'.
 2705:                       '<br />';
 2706:     }
 2707:     my %advcell;
 2708:     foreach my $item (@usertools) {
 2709:         if ($context eq 'requestcourses') {
 2710:             my ($curroption,$currlimit);
 2711:             if (ref($settings) eq 'HASH') {
 2712:                 if (ref($settings->{$item}) eq 'HASH') {
 2713:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2714:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2715:                         $currlimit = $1;
 2716:                     }
 2717:                 }
 2718:             }
 2719:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2720:             my $checked = '';
 2721:             if ($curroption eq '') {
 2722:                 $checked = ' checked="checked"';
 2723:             }
 2724:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2725:                                '<input type="radio" name="crsreq_'.$item.
 2726:                                '__LC_adv" value=""'.$checked.' />'.
 2727:                                &mt('No override set').'</label></span>&nbsp; ';
 2728:             foreach my $option (@options) {
 2729:                 my $val = $option;
 2730:                 if ($option eq 'norequest') {
 2731:                     $val = 0;
 2732:                 }
 2733:                 if ($option eq 'validate') {
 2734:                     my $canvalidate = 0;
 2735:                     if (ref($validations{$item}) eq 'HASH') {
 2736:                         if ($validations{$item}{'_LC_adv'}) {
 2737:                             $canvalidate = 1;
 2738:                         }
 2739:                     }
 2740:                     next if (!$canvalidate);
 2741:                 }
 2742:                 my $checked = '';
 2743:                 if ($val eq $curroption) {
 2744:                     $checked = ' checked="checked"';
 2745:                 } elsif ($option eq 'autolimit') {
 2746:                     if ($curroption =~ /^autolimit/) {
 2747:                         $checked = ' checked="checked"';
 2748:                     }
 2749:                 }
 2750:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2751:                                   '<input type="radio" name="crsreq_'.$item.
 2752:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2753:                                   $titles{$option}.'</label>';
 2754:                 if ($option eq 'autolimit') {
 2755:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2756:                                        $item.'_limit__LC_adv" size="1" '.
 2757:                                        'value="'.$currlimit.'" />';
 2758:                 }
 2759:                 $advcell{$item} .= '</span> ';
 2760:                 if ($option eq 'autolimit') {
 2761:                     $advcell{$item} .= $titles{'unlimited'};
 2762:                 }
 2763:             }
 2764:         } elsif ($context eq 'requestauthor') {
 2765:             my $curroption;
 2766:             if (ref($settings) eq 'HASH') {
 2767:                 $curroption = $settings->{'_LC_adv'};
 2768:             }
 2769:             my $checked = '';
 2770:             if ($curroption eq '') {
 2771:                 $checked = ' checked="checked"';
 2772:             }
 2773:             $datatable .= '<span class="LC_nobreak"><label>'.
 2774:                           '<input type="radio" name="authorreq__LC_adv"'.
 2775:                           ' value=""'.$checked.' />'.
 2776:                           &mt('No override set').'</label></span>&nbsp; ';
 2777:             foreach my $option (@options) {
 2778:                 my $val = $option;
 2779:                 if ($option eq 'norequest') {
 2780:                     $val = 0;
 2781:                 }
 2782:                 my $checked = '';
 2783:                 if ($val eq $curroption) {
 2784:                     $checked = ' checked="checked"';
 2785:                 }
 2786:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2787:                               '<input type="radio" name="authorreq__LC_adv"'.
 2788:                               ' value="'.$val.'"'.$checked.' />'.
 2789:                               $titles{$option}.'</label></span>&nbsp; ';
 2790:             }
 2791:         } else {
 2792:             my $checked = 'checked="checked" ';
 2793:             if (ref($settings) eq 'HASH') {
 2794:                 if (ref($settings->{$item}) eq 'HASH') {
 2795:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2796:                         $checked = '';
 2797:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2798:                         $checked = 'checked="checked" ';
 2799:                     }
 2800:                 }
 2801:             }
 2802:             $datatable .= '<span class="LC_nobreak"><label>'.
 2803:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2804:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2805:                           '</label></span>&nbsp; ';
 2806:         }
 2807:     }
 2808:     if ($context eq 'requestcourses') {
 2809:         $datatable .= '</tr><tr>';
 2810:         foreach my $item (@usertools) {
 2811:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2812:         }
 2813:         $datatable .= '</tr></table>';
 2814:     }
 2815:     $datatable .= '</td></tr>';
 2816:     $$rowtotal += $typecount;
 2817:     return $datatable;
 2818: }
 2819: 
 2820: sub print_requestmail {
 2821:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2822:     my ($now,$datatable,%currapp);
 2823:     $now = time;
 2824:     if (ref($settings) eq 'HASH') {
 2825:         if (ref($settings->{'notify'}) eq 'HASH') {
 2826:             if ($settings->{'notify'}{'approval'} ne '') {
 2827:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2828:             }
 2829:         }
 2830:     }
 2831:     my $numinrow = 2;
 2832:     my $css_class;
 2833:     if ($$rowtotal%2) {
 2834:         $css_class = 'LC_odd_row';
 2835:     }
 2836:     if ($customcss) {
 2837:         $css_class .= " $customcss";
 2838:     }
 2839:     $css_class =~ s/^\s+//;
 2840:     if ($css_class) {
 2841:         $css_class = ' class="'.$css_class.'"';
 2842:     }
 2843:     if ($rowstyle) {
 2844:         $css_class .= ' style="'.$rowstyle.'"';
 2845:     }
 2846:     my $text;
 2847:     if ($action eq 'requestcourses') {
 2848:         $text = &mt('Receive notification of course requests requiring approval');
 2849:     } elsif ($action eq 'requestauthor') {
 2850:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2851:     } else {
 2852:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2853:     }
 2854:     $datatable = '<tr'.$css_class.'>'.
 2855:                  ' <td>'.$text.'</td>'.
 2856:                  ' <td class="LC_left_item">';
 2857:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2858:                                                  $action.'notifyapproval',%currapp);
 2859:     if ($numdc > 0) {
 2860:         $datatable .= $table;
 2861:     } else {
 2862:         $datatable .= &mt('There are no active Domain Coordinators');
 2863:     }
 2864:     $datatable .='</td></tr>';
 2865:     return $datatable;
 2866: }
 2867: 
 2868: sub print_studentcode {
 2869:     my ($settings,$rowtotal) = @_;
 2870:     my $rownum = 0; 
 2871:     my ($output,%current);
 2872:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2873:     if (ref($settings) eq 'HASH') {
 2874:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2875:             foreach my $type (@crstypes) {
 2876:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2877:             }
 2878:         }
 2879:     }
 2880:     $output .= '<tr>'.
 2881:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2882:                '<td class="LC_left_item">';
 2883:     foreach my $type (@crstypes) {
 2884:         my $check = ' ';
 2885:         if ($current{$type}) {
 2886:             $check = ' checked="checked" ';
 2887:         }
 2888:         $output .= '<span class="LC_nobreak"><label>'.
 2889:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2890:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2891:     }
 2892:     $output .= '</td></tr>';
 2893:     $$rowtotal ++;
 2894:     return $output;
 2895: }
 2896: 
 2897: sub print_textbookcourses {
 2898:     my ($dom,$type,$settings,$rowtotal) = @_;
 2899:     my $rownum = 0;
 2900:     my $css_class;
 2901:     my $itemcount = 1;
 2902:     my $maxnum = 0;
 2903:     my $bookshash;
 2904:     if (ref($settings) eq 'HASH') {
 2905:         $bookshash = $settings->{$type};
 2906:     }
 2907:     my %ordered;
 2908:     if (ref($bookshash) eq 'HASH') {
 2909:         foreach my $item (keys(%{$bookshash})) {
 2910:             if (ref($bookshash->{$item}) eq 'HASH') {
 2911:                 my $num = $bookshash->{$item}{'order'};
 2912:                 $ordered{$num} = $item;
 2913:             }
 2914:         }
 2915:     }
 2916:     my $confname = $dom.'-domainconfig';
 2917:     my $switchserver = &check_switchserver($dom,$confname);
 2918:     my $maxnum = scalar(keys(%ordered));
 2919:     my $datatable;
 2920:     if (keys(%ordered)) {
 2921:         my @items = sort { $a <=> $b } keys(%ordered);
 2922:         for (my $i=0; $i<@items; $i++) {
 2923:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2924:             my $key = $ordered{$items[$i]};
 2925:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2926:             my $coursetitle = $coursehash{'description'};
 2927:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2928:             if (ref($bookshash->{$key}) eq 'HASH') {
 2929:                 $subject = $bookshash->{$key}->{'subject'};
 2930:                 $title = $bookshash->{$key}->{'title'};
 2931:                 if ($type eq 'textbooks') {
 2932:                     $publisher = $bookshash->{$key}->{'publisher'};
 2933:                     $author = $bookshash->{$key}->{'author'};
 2934:                     $image = $bookshash->{$key}->{'image'};
 2935:                     if ($image ne '') {
 2936:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2937:                         my $imagethumb = "$path/tn-".$imagefile;
 2938:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2939:                     }
 2940:                 }
 2941:             }
 2942:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2943:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2944:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2945:             for (my $k=0; $k<=$maxnum; $k++) {
 2946:                 my $vpos = $k+1;
 2947:                 my $selstr;
 2948:                 if ($k == $i) {
 2949:                     $selstr = ' selected="selected" ';
 2950:                 }
 2951:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2952:             }
 2953:             $datatable .= '</select>'.('&nbsp;'x2).
 2954:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2955:                 &mt('Delete?').'</label></span></td>'.
 2956:                 '<td colspan="2">'.
 2957:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2958:                 ('&nbsp;'x2).
 2959:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2960:             if ($type eq 'textbooks') {
 2961:                 $datatable .= ('&nbsp;'x2).
 2962:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2963:                               ('&nbsp;'x2).
 2964:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2965:                               ('&nbsp;'x2).
 2966:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2967:                 if ($image) {
 2968:                     $datatable .= $imgsrc.
 2969:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2970:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2971:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2972:                 }
 2973:                 if ($switchserver) {
 2974:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2975:                 } else {
 2976:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2977:                 }
 2978:             }
 2979:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2980:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2981:                           $coursetitle.'</span></td></tr>'."\n";
 2982:             $itemcount ++;
 2983:         }
 2984:     }
 2985:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2986:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2987:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2988:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2989:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2990:     for (my $k=0; $k<$maxnum+1; $k++) {
 2991:         my $vpos = $k+1;
 2992:         my $selstr;
 2993:         if ($k == $maxnum) {
 2994:             $selstr = ' selected="selected" ';
 2995:         }
 2996:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2997:     }
 2998:     $datatable .= '</select>&nbsp;'."\n".
 2999:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 3000:                   '<td colspan="2">'.
 3001:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 3002:                   ('&nbsp;'x2).
 3003:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 3004:                   ('&nbsp;'x2);
 3005:     if ($type eq 'textbooks') {
 3006:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 3007:                       ('&nbsp;'x2).
 3008:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 3009:                       ('&nbsp;'x2).
 3010:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 3011:         if ($switchserver) {
 3012:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3013:         } else {
 3014:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 3015:         }
 3016:         $datatable .= '</span>'."\n";
 3017:     }
 3018:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 3019:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 3020:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 3021:                   &Apache::loncommon::selectcourse_link
 3022:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 3023:                   '</span></td>'."\n".
 3024:                   '</tr>'."\n";
 3025:     $itemcount ++;
 3026:     return $datatable;
 3027: }
 3028: 
 3029: sub textbookcourses_javascript {
 3030:     my ($settings) = @_;
 3031:     return unless(ref($settings) eq 'HASH');
 3032:     my (%ordered,%total,%jstext);
 3033:     foreach my $type ('textbooks','templates') {
 3034:         $total{$type} = 0;
 3035:         if (ref($settings->{$type}) eq 'HASH') {
 3036:             foreach my $item (keys(%{$settings->{$type}})) {
 3037:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 3038:                     my $num = $settings->{$type}->{$item}{'order'};
 3039:                     $ordered{$type}{$num} = $item;
 3040:                 }
 3041:             }
 3042:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 3043:         }
 3044:         my @jsarray = ();
 3045:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 3046:             push(@jsarray,$ordered{$type}{$item});
 3047:         }
 3048:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 3049:     }
 3050:     return <<"ENDSCRIPT";
 3051: <script type="text/javascript">
 3052: // <![CDATA[
 3053: function reorderBooks(form,item,caller) {
 3054:     var changedVal;
 3055: $jstext{'textbooks'};
 3056: $jstext{'templates'};
 3057:     var newpos;
 3058:     var maxh;
 3059:     if (caller == 'textbooks') {  
 3060:         newpos = 'textbooks_addbook_pos';
 3061:         maxh = 1 + $total{'textbooks'};
 3062:     } else {
 3063:         newpos = 'templates_addbook_pos';
 3064:         maxh = 1 + $total{'templates'};
 3065:     }
 3066:     var current = new Array;
 3067:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3068:     if (item == newpos) {
 3069:         changedVal = newitemVal;
 3070:     } else {
 3071:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3072:         current[newitemVal] = newpos;
 3073:     }
 3074:     if (caller == 'textbooks') {
 3075:         for (var i=0; i<textbooks.length; i++) {
 3076:             var elementName = 'textbooks_'+textbooks[i];
 3077:             if (elementName != item) {
 3078:                 if (form.elements[elementName]) {
 3079:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3080:                     current[currVal] = elementName;
 3081:                 }
 3082:             }
 3083:         }
 3084:     }
 3085:     if (caller == 'templates') {
 3086:         for (var i=0; i<templates.length; i++) {
 3087:             var elementName = 'templates_'+templates[i];
 3088:             if (elementName != item) {
 3089:                 if (form.elements[elementName]) {
 3090:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3091:                     current[currVal] = elementName;
 3092:                 }
 3093:             }
 3094:         }
 3095:     }
 3096:     var oldVal;
 3097:     for (var j=0; j<maxh; j++) {
 3098:         if (current[j] == undefined) {
 3099:             oldVal = j;
 3100:         }
 3101:     }
 3102:     if (oldVal < changedVal) {
 3103:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3104:            var elementName = current[k];
 3105:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3106:         }
 3107:     } else {
 3108:         for (var k=changedVal; k<oldVal; k++) {
 3109:             var elementName = current[k];
 3110:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3111:         }
 3112:     }
 3113:     return;
 3114: }
 3115: 
 3116: // ]]>
 3117: </script>
 3118: 
 3119: ENDSCRIPT
 3120: }
 3121: 
 3122: sub ltitools_javascript {
 3123:     my ($settings) = @_;
 3124:     my $togglejs = &ltitools_toggle_js();
 3125:     unless (ref($settings) eq 'HASH') {
 3126:         return $togglejs;
 3127:     }
 3128:     my (%ordered,$total,%jstext);
 3129:     $total = 0;
 3130:     foreach my $item (keys(%{$settings})) {
 3131:         if (ref($settings->{$item}) eq 'HASH') {
 3132:             my $num = $settings->{$item}{'order'};
 3133:             $ordered{$num} = $item;
 3134:         }
 3135:     }
 3136:     $total = scalar(keys(%{$settings}));
 3137:     my @jsarray = ();
 3138:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3139:         push(@jsarray,$ordered{$item});
 3140:     }
 3141:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 3142:     return <<"ENDSCRIPT";
 3143: <script type="text/javascript">
 3144: // <![CDATA[
 3145: function reorderLTITools(form,item) {
 3146:     var changedVal;
 3147: $jstext
 3148:     var newpos = 'ltitools_add_pos';
 3149:     var maxh = 1 + $total;
 3150:     var current = new Array;
 3151:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3152:     if (item == newpos) {
 3153:         changedVal = newitemVal;
 3154:     } else {
 3155:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3156:         current[newitemVal] = newpos;
 3157:     }
 3158:     for (var i=0; i<ltitools.length; i++) {
 3159:         var elementName = 'ltitools_'+ltitools[i];
 3160:         if (elementName != item) {
 3161:             if (form.elements[elementName]) {
 3162:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3163:                 current[currVal] = elementName;
 3164:             }
 3165:         }
 3166:     }
 3167:     var oldVal;
 3168:     for (var j=0; j<maxh; j++) {
 3169:         if (current[j] == undefined) {
 3170:             oldVal = j;
 3171:         }
 3172:     }
 3173:     if (oldVal < changedVal) {
 3174:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3175:            var elementName = current[k];
 3176:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3177:         }
 3178:     } else {
 3179:         for (var k=changedVal; k<oldVal; k++) {
 3180:             var elementName = current[k];
 3181:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3182:         }
 3183:     }
 3184:     return;
 3185: }
 3186: 
 3187: // ]]>
 3188: </script>
 3189: 
 3190: $togglejs
 3191: 
 3192: ENDSCRIPT
 3193: }
 3194: 
 3195: sub ltitools_toggle_js {
 3196:     return <<"ENDSCRIPT";
 3197: <script type="text/javascript">
 3198: // <![CDATA[
 3199: 
 3200: function toggleLTITools(form,setting,item) {
 3201:     var radioname = '';
 3202:     var divid = '';
 3203:     if ((setting == 'passback') || (setting == 'roster')) {
 3204:         radioname = 'ltitools_'+setting+'_'+item;
 3205:         divid = 'ltitools_'+setting+'time_'+item;
 3206:         var num = form.elements[radioname].length;
 3207:         if (num) {
 3208:             var setvis = '';
 3209:             for (var i=0; i<num; i++) {
 3210:                 if (form.elements[radioname][i].checked) {
 3211:                     if (form.elements[radioname][i].value == '1') {
 3212:                         if (document.getElementById(divid)) {
 3213:                             document.getElementById(divid).style.display = 'inline-block';
 3214:                         }
 3215:                         setvis = 1;
 3216:                     }
 3217:                     break;
 3218:                 }
 3219:             }
 3220:         }
 3221:         if (!setvis) {
 3222:             if (document.getElementById(divid)) {
 3223:                 document.getElementById(divid).style.display = 'none';
 3224:             }
 3225:         }
 3226:     }
 3227:     if (setting == 'user') {
 3228:         divid = 'ltitools_'+setting+'_div_'+item;
 3229:         var checkid = 'ltitools_'+setting+'_field_'+item;
 3230:         if (document.getElementById(divid)) {
 3231:             if (document.getElementById(checkid)) {
 3232:                 if (document.getElementById(checkid).checked) {
 3233:                     document.getElementById(divid).style.display = 'inline-block';
 3234:                 } else {
 3235:                     document.getElementById(divid).style.display = 'none';
 3236:                 }
 3237:             }
 3238:         }
 3239:     }
 3240:     return;
 3241: }
 3242: // ]]>
 3243: </script>
 3244: 
 3245: ENDSCRIPT
 3246: }
 3247: 
 3248: sub wafproxy_javascript {
 3249:     my ($dom) = @_;
 3250:     return <<"ENDSCRIPT";
 3251: <script type="text/javascript">
 3252: // <![CDATA[
 3253: function updateWAF() {
 3254:     if (document.getElementById('wafproxy_remoteip')) {
 3255:         var wafremote = 0;
 3256:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 3257:             wafremote = 1;
 3258:         }
 3259:         var fields = new Array('header','trust');
 3260:         for (var i=0; i<fields.length; i++) {
 3261:             if (document.getElementById('wafproxy_'+fields[i])) {
 3262:                 if (wafremote == 1) {
 3263:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 3264:                 }
 3265:                 else {
 3266:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 3267:                 }
 3268:             }
 3269:         }
 3270:         if (document.getElementById('wafproxyranges_$dom')) {
 3271:             if (wafremote == 1) {
 3272:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3273:             } else {
 3274:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3275:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 3276:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 3277:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3278:                         }
 3279:                     }
 3280:                 }
 3281:             }
 3282:         }
 3283:     }
 3284:     return;
 3285: }
 3286: 
 3287: function checkWAF() {
 3288:     if (document.getElementById('wafproxy_remoteip')) {
 3289:         var wafvpn = 0;
 3290:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3291:             if (document.display.wafproxy_vpnaccess[i].checked) {
 3292:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 3293:                     wafvpn = 1;
 3294:                 }
 3295:                 break;
 3296:             }
 3297:         }
 3298:         var vpn = new Array('vpnint','vpnext');
 3299:         for (var i=0; i<vpn.length; i++) {
 3300:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 3301:                 if (wafvpn == 1) {
 3302:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 3303:                 }
 3304:                 else {
 3305:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 3306:                 }
 3307:             }
 3308:         }
 3309:         if (document.getElementById('wafproxyranges_$dom')) {
 3310:             if (wafvpn == 1) {
 3311:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3312:             }
 3313:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 3314:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3315:             }
 3316:         }
 3317:     }
 3318:     return;
 3319: }
 3320: 
 3321: function toggleWAF() {
 3322:     if (document.getElementById('wafproxy_table')) {
 3323:         var wafproxy = 0;
 3324:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 3325:              if (document.display.wafproxy_${dom}[i].checked) {
 3326:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 3327:                      wafproxy = 1;
 3328:                      break;
 3329:                 }
 3330:             }
 3331:         }
 3332:         if (wafproxy == 1) {
 3333:             document.getElementById('wafproxy_table').style.display='inline';
 3334:         }
 3335:         else {
 3336:            document.getElementById('wafproxy_table').style.display='none';
 3337:         }
 3338:         if (document.getElementById('wafproxyrow_${dom}')) {
 3339:             if (wafproxy == 1) {
 3340:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 3341:             }
 3342:             else {
 3343:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 3344:             }
 3345:         }
 3346:         if (document.getElementById('nowafproxyrow_$dom')) {
 3347:             if (wafproxy == 1) {
 3348:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 3349:             }
 3350:             else {
 3351:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 3352:             }
 3353:         }
 3354:     }
 3355:     return;
 3356: }
 3357: // ]]>
 3358: </script>
 3359: 
 3360: ENDSCRIPT
 3361: }
 3362: 
 3363: sub proctoring_javascript {
 3364:     my ($settings) = @_;
 3365:     my (%ordered,$total,%jstext);
 3366:     $total = 0;
 3367:     if (ref($settings) eq 'HASH') {
 3368:         foreach my $item (keys(%{$settings})) {
 3369:             if (ref($settings->{$item}) eq 'HASH') {
 3370:                 my $num = $settings->{$item}{'order'};
 3371:                 $ordered{$num} = $item;
 3372:             }
 3373:         }
 3374:         $total = scalar(keys(%{$settings}));
 3375:     } else {
 3376:         %ordered = (
 3377:                        0 => 'proctorio',
 3378:                        1 => 'examity',
 3379:                    );
 3380:         $total = 2; 
 3381:     }
 3382:     my @jsarray = ();
 3383:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3384:         push(@jsarray,$ordered{$item});
 3385:     }
 3386:     my $jstext = '    var proctors = Array('."'".join("','",@jsarray)."'".');'."\n";
 3387:     return <<"ENDSCRIPT";
 3388: <script type="text/javascript">
 3389: // <![CDATA[
 3390: function reorderProctoring(form,item) {
 3391:     var changedVal;
 3392: $jstext
 3393:     var maxh = $total;
 3394:     var current = new Array;
 3395:     var changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3396:     for (var i=0; i<proctors.length; i++) {
 3397:         var elementName = 'proctoring_pos_'+proctors[i];
 3398:         if (elementName != item) {
 3399:             if (form.elements[elementName]) {
 3400:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3401:                 current[currVal] = elementName;
 3402:             }
 3403:         }
 3404:     }
 3405:     var oldVal;
 3406:     for (var j=0; j<maxh; j++) {
 3407:         if (current[j] == undefined) {
 3408:             oldVal = j;
 3409:         }
 3410:     }
 3411:     if (oldVal < changedVal) {
 3412:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3413:            var elementName = current[k];
 3414:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3415:         }
 3416:     } else {
 3417:         for (var k=changedVal; k<oldVal; k++) {
 3418:             var elementName = current[k];
 3419:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3420:         }
 3421:     }
 3422:     return;
 3423: }
 3424: 
 3425: function toggleProctoring(form,item) {
 3426:     var fieldsets = document.getElementsByClassName('proctoring_'+item);
 3427:     if (fieldsets.length) {
 3428:         var radioname = 'proctoring_available_'+item;
 3429:         var num = form.elements[radioname].length;
 3430:         if (num) {
 3431:             var setvis = '';
 3432:             for (var i=0; i<num; i++) {
 3433:                 if (form.elements[radioname][i].checked) {
 3434:                     if (form.elements[radioname][i].value == '1') {
 3435:                        setvis = 1;
 3436:                        break;
 3437:                     }
 3438:                 }
 3439:             }
 3440:             for (var j=0; j<fieldsets.length; j++) {
 3441:                 if (setvis) {
 3442:                     fieldsets[j].style.display = 'block';
 3443:                 } else {
 3444:                     fieldsets[j].style.display = 'none';
 3445:                 }
 3446:             }
 3447:         }
 3448:     }
 3449:     return;
 3450: }
 3451: 
 3452: // ]]>
 3453: </script>
 3454: 
 3455: ENDSCRIPT
 3456: }
 3457: 
 3458: 
 3459: sub lti_javascript {
 3460:     my ($dom,$settings) = @_;
 3461:     my $togglejs = &lti_toggle_js($dom);
 3462:     my $linkprot_js = &Apache::courseprefs::linkprot_javascript();
 3463:     unless (ref($settings) eq 'HASH') {
 3464:         return $togglejs.'
 3465: <script type="text/javascript">
 3466: // <![CDATA[
 3467: 
 3468: '.$linkprot_js.'
 3469: 
 3470: // ]]>
 3471: </script>
 3472: ';
 3473:     }
 3474:     my (%ordered,$total,%jstext);
 3475:     $total = scalar(keys(%{$settings}));
 3476:     foreach my $item (keys(%{$settings})) {
 3477:         if (ref($settings->{$item}) eq 'HASH') {
 3478:             my $num = $settings->{$item}{'order'};
 3479:             if ($num eq '') {
 3480:                 $num = $total - 1;
 3481:             }
 3482:             $ordered{$num} = $item;
 3483:         }
 3484:     }
 3485:     my @jsarray = ();
 3486:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3487:         push(@jsarray,$ordered{$item});
 3488:     }
 3489:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 3490:     return <<"ENDSCRIPT";
 3491: <script type="text/javascript">
 3492: // <![CDATA[
 3493: function reorderLTI(form,item) {
 3494:     var changedVal;
 3495: $jstext
 3496:     var newpos = 'lti_pos_add';
 3497:     var maxh = 1 + $total;
 3498:     var current = new Array;
 3499:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3500:     if (item == newpos) {
 3501:         changedVal = newitemVal;
 3502:     } else {
 3503:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3504:         current[newitemVal] = newpos;
 3505:     }
 3506:     for (var i=0; i<lti.length; i++) {
 3507:         var elementName = 'lti_pos_'+lti[i];
 3508:         if (elementName != item) {
 3509:             if (form.elements[elementName]) {
 3510:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3511:                 current[currVal] = elementName;
 3512:             }
 3513:         }
 3514:     }
 3515:     var oldVal;
 3516:     for (var j=0; j<maxh; j++) {
 3517:         if (current[j] == undefined) {
 3518:             oldVal = j;
 3519:         }
 3520:     }
 3521:     if (oldVal < changedVal) {
 3522:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3523:            var elementName = current[k];
 3524:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3525:         }
 3526:     } else {
 3527:         for (var k=changedVal; k<oldVal; k++) {
 3528:             var elementName = current[k];
 3529:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3530:         }
 3531:     }
 3532:     return;
 3533: }
 3534: 
 3535: $linkprot_js
 3536: 
 3537: // ]]>
 3538: </script>
 3539: 
 3540: $togglejs
 3541: 
 3542: ENDSCRIPT
 3543: }
 3544: 
 3545: sub lti_toggle_js {
 3546:     my ($dom) = @_;
 3547:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3548:                             localauth => 'Local auth argument',
 3549:                             krb       => 'Kerberos domain',
 3550:                          );
 3551:     my $crsincalert = &mt('"User\'s identity sent" needs to be set to "Yes" first,[_1] before setting "Course\'s identity sent" to "Yes"',"\n");
 3552:     &js_escape(\$crsincalert);
 3553:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 3554:     my $primary = &Apache::lonnet::domain($dom,'primary');
 3555:     my $course_servers = "'".join("','",keys(%servers))."'";
 3556:     return <<"ENDSCRIPT";
 3557: <script type="text/javascript">
 3558: // <![CDATA[
 3559: 
 3560: function toggleLTI(form,setting,item) {
 3561:     if ((setting == 'requser') || (setting == 'crsinc')) {
 3562:         var usrfieldsets = document.getElementsByClassName('ltioption_usr_'+item);
 3563:         var setvis = '';
 3564:         var radioname = 'lti_requser_'+item;
 3565:         var num = form.elements[radioname].length;
 3566:         if (num) {
 3567:             for (var i=0; i<num; i++) {
 3568:                 if (form.elements[radioname][i].checked) {
 3569:                     if (form.elements[radioname][i].value == '1') {
 3570:                         setvis = 1;
 3571:                         break;
 3572:                     }
 3573:                 }
 3574:             }
 3575:         }
 3576:         if (usrfieldsets.length) {
 3577:             for (var j=0; j<usrfieldsets.length; j++) {
 3578:                 if (setvis) {
 3579:                     usrfieldsets[j].style.display = 'block';
 3580:                 } else {
 3581:                     usrfieldsets[j].style.display = 'none';
 3582:                 }
 3583:             }
 3584:         }
 3585:         var crsfieldsets = document.getElementsByClassName('ltioption_crs_'+item);
 3586:         if (crsfieldsets.length) {
 3587:             radioname = 'lti_crsinc_'+item;
 3588:             var num = form.elements[radioname].length;
 3589:             if (num) {
 3590:                 var crsvis = '';
 3591:                 for (var i=0; i<num; i++) {
 3592:                     if (form.elements[radioname][i].checked) {
 3593:                         if (form.elements[radioname][i].value == '1') {
 3594:                             if (setvis == '') {
 3595:                                 if (setting == 'crsinc'){
 3596:                                     alert("$crsincalert");
 3597:                                     form.elements[radioname][0].checked = true;
 3598:                                 }
 3599:                             } else {
 3600:                                 crsvis = 1;
 3601:                             }
 3602:                             break;
 3603:                         }
 3604:                     }
 3605:                 }
 3606:                 setvis = crsvis;
 3607:             }
 3608:             for (var j=0; j<crsfieldsets.length; j++) {
 3609:                 if (setvis) {
 3610:                     crsfieldsets[j].style.display = 'block';
 3611:                 } else {
 3612:                     crsfieldsets[j].style.display = 'none';
 3613:                 }
 3614:             }
 3615:         }
 3616:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3617:         var radioname = '';
 3618:         var divid = '';
 3619:         if (setting == 'user') {
 3620:             radioname = 'lti_mapuser_'+item;
 3621:             divid = 'lti_userfield_'+item;
 3622:         } else if (setting == 'crs') {
 3623:             radioname = 'lti_mapcrs_'+item;
 3624:             divid = 'lti_crsfield_'+item;
 3625:         } else if (setting == 'callback') {
 3626:             radioname = 'lti_callback_'+item;
 3627:             divid = 'lti_callbackfield_'+item;
 3628:         } else {
 3629:             radioname = 'lti_passback_'+item;
 3630:             divid =  'lti_passback_'+item;
 3631:         }
 3632:         var num = form.elements[radioname].length;
 3633:         if (num) {
 3634:             var setvis = '';
 3635:             for (var i=0; i<num; i++) {
 3636:                if (form.elements[radioname][i].checked) {
 3637:                    if ((setting == 'passback') || (setting == 'callback')) {
 3638:                        if (form.elements[radioname][i].value == '1') {
 3639:                            if (document.getElementById(divid)) {
 3640:                                document.getElementById(divid).style.display = 'inline-block';
 3641:                            }
 3642:                            setvis = 1;
 3643:                            break;
 3644:                        }
 3645:                    } else {
 3646:                        if (form.elements[radioname][i].value == 'other') {
 3647:                            if (document.getElementById(divid)) {
 3648:                                document.getElementById(divid).style.display = 'inline-block';
 3649:                            }
 3650:                            setvis = 1;
 3651:                            break;
 3652:                        }
 3653:                    }
 3654:                }
 3655:             }
 3656:             if (!setvis) {
 3657:                 if (document.getElementById(divid)) {
 3658:                     document.getElementById(divid).style.display = 'none';
 3659:                 }
 3660:             }
 3661:         }
 3662:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3663:         var numsec = form.elements['lti_crssec_'+item].length;
 3664:         if (numsec) {
 3665:             var setvis = '';
 3666:             for (var i=0; i<numsec; i++) {
 3667:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3668:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3669:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3670:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3671:                             setvis = 1;
 3672:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3673:                             if (numsrcsec) {
 3674:                                 var setsrcvis = '';
 3675:                                 for (var j=0; j<numsrcsec; j++) {
 3676:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3677:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3678:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3679:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3680:                                                 setsrcvis = 1;
 3681:                                             }
 3682:                                         }
 3683:                                     }
 3684:                                 }
 3685:                                 if (!setsrcvis) {
 3686:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3687:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3688:                                     }
 3689:                                 }
 3690:                             }
 3691:                         }
 3692:                     }
 3693:                 }
 3694:             }
 3695:             if (!setvis) {
 3696:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3697:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3698:                 }
 3699:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3700:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3701:                 }
 3702:             }
 3703:         }
 3704:     } else if (setting == 'lcauth') {
 3705:         var numauth = form.elements['lti_lcauth_'+item].length;
 3706:         if (numauth) {
 3707:             for (var i=0; i<numauth; i++) {
 3708:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3709:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3710:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3711:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3712:                         } else {
 3713:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3714:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3715:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3716:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3717:                                 } else {
 3718:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3719:                                 }
 3720:                             }
 3721:                         }
 3722:                     }
 3723:                 }
 3724:             }
 3725:         }
 3726:     } else if (setting == 'lcmenu') {
 3727:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3728:         var divid = 'lti_menufield_'+item;
 3729:         var setvis = '';
 3730:         for (var i=0; i<menus.length; i++) {
 3731:             var radioname = menus[i];
 3732:             var num = form.elements[radioname].length;
 3733:             if (num) {
 3734:                 for (var j=0; j<num; j++) {
 3735:                     if (form.elements[radioname][j].checked) {
 3736:                         if (form.elements[radioname][j].value == '1') {
 3737:                             if (document.getElementById(divid)) {
 3738:                                 document.getElementById(divid).style.display = 'inline-block';
 3739:                             }
 3740:                             setvis = 1;
 3741:                             break;
 3742:                         }
 3743:                     }
 3744:                 }
 3745:             }
 3746:             if (setvis == 1) {
 3747:                 break;
 3748:             }
 3749:         }
 3750:         if (!setvis) {
 3751:             if (document.getElementById(divid)) {
 3752:                 document.getElementById(divid).style.display = 'none';
 3753:             }
 3754:         }
 3755:     }
 3756:     return;
 3757: }
 3758: 
 3759: // ]]>
 3760: </script>
 3761: 
 3762: ENDSCRIPT
 3763: }
 3764: 
 3765: sub autoupdate_javascript {
 3766:     return <<"ENDSCRIPT";
 3767: <script type="text/javascript">
 3768: // <![CDATA[
 3769: function toggleLastActiveDays(form) {
 3770:     var radioname = 'lastactive';
 3771:     var divid = 'lastactive_div';
 3772:     var num = form.elements[radioname].length;
 3773:     if (num) {
 3774:         var setvis = '';
 3775:         for (var i=0; i<num; i++) {
 3776:             if (form.elements[radioname][i].checked) {
 3777:                 if (form.elements[radioname][i].value == '1') {
 3778:                     if (document.getElementById(divid)) {
 3779:                         document.getElementById(divid).style.display = 'inline-block';
 3780:                     }
 3781:                     setvis = 1;
 3782:                 }
 3783:                 break;
 3784:             }
 3785:         }
 3786:         if (!setvis) {
 3787:             if (document.getElementById(divid)) {
 3788:                 document.getElementById(divid).style.display = 'none';
 3789:             }
 3790:         }
 3791:     }
 3792:     return;
 3793: }
 3794: // ]]>
 3795: </script>
 3796: 
 3797: ENDSCRIPT
 3798: }
 3799: 
 3800: sub autoenroll_javascript {
 3801:     return <<"ENDSCRIPT";
 3802: <script type="text/javascript">
 3803: // <![CDATA[
 3804: function toggleFailsafe(form) {
 3805:     var radioname = 'autoenroll_failsafe';
 3806:     var divid = 'autoenroll_failsafe_div';
 3807:     var num = form.elements[radioname].length;
 3808:     if (num) {
 3809:         var setvis = '';
 3810:         for (var i=0; i<num; i++) {
 3811:             if (form.elements[radioname][i].checked) {
 3812:                 if ((form.elements[radioname][i].value == 'zero') || (form.elements[radioname][i].value == 'any')) {
 3813:                     if (document.getElementById(divid)) {
 3814:                         document.getElementById(divid).style.display = 'inline-block';
 3815:                     }
 3816:                     setvis = 1;
 3817:                 }
 3818:                 break;
 3819:             }
 3820:         }
 3821:         if (!setvis) {
 3822:             if (document.getElementById(divid)) {
 3823:                 document.getElementById(divid).style.display = 'none';
 3824:             }
 3825:         }
 3826:     }
 3827:     return;
 3828: }
 3829: // ]]>
 3830: </script>
 3831: 
 3832: ENDSCRIPT
 3833: }
 3834: 
 3835: sub saml_javascript {
 3836:     return <<"ENDSCRIPT";
 3837: <script type="text/javascript">
 3838: // <![CDATA[
 3839: function toggleSamlOptions(form,hostid) {
 3840:     var radioname = 'saml_'+hostid;
 3841:     var tablecellon = 'samloptionson_'+hostid;
 3842:     var tablecelloff = 'samloptionsoff_'+hostid;
 3843:     var num = form.elements[radioname].length;
 3844:     if (num) {
 3845:         var setvis = '';
 3846:         for (var i=0; i<num; i++) {
 3847:             if (form.elements[radioname][i].checked) {
 3848:                 if (form.elements[radioname][i].value == '1') {
 3849:                     if (document.getElementById(tablecellon)) {
 3850:                         document.getElementById(tablecellon).style.display='';
 3851:                     }
 3852:                     if (document.getElementById(tablecelloff)) {
 3853:                         document.getElementById(tablecelloff).style.display='none';
 3854:                     }
 3855:                     setvis = 1;
 3856:                 }
 3857:                 break;
 3858:             }
 3859:         }
 3860:         if (!setvis) {
 3861:             if (document.getElementById(tablecellon)) {
 3862:                 document.getElementById(tablecellon).style.display='none';
 3863:             }
 3864:             if (document.getElementById(tablecelloff)) {
 3865:                 document.getElementById(tablecelloff).style.display='';
 3866:             }
 3867:         }
 3868:     }
 3869:     return;
 3870: }
 3871: // ]]>
 3872: </script>
 3873: 
 3874: ENDSCRIPT
 3875: }
 3876: 
 3877: sub ipaccess_javascript {
 3878:     my ($settings) = @_;
 3879:     my (%ordered,$total,%jstext);
 3880:     $total = 0;
 3881:     if (ref($settings) eq 'HASH') {
 3882:         foreach my $item (keys(%{$settings})) {
 3883:             if (ref($settings->{$item}) eq 'HASH') {
 3884:                 my $num = $settings->{$item}{'order'};
 3885:                 $ordered{$num} = $item;
 3886:             }
 3887:         }
 3888:         $total = scalar(keys(%{$settings}));
 3889:     }
 3890:     my @jsarray = ();
 3891:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3892:         push(@jsarray,$ordered{$item});
 3893:     }
 3894:     my $jstext = '    var ipaccess = Array('."'".join("','",@jsarray)."'".');'."\n";
 3895:     return <<"ENDSCRIPT";
 3896: <script type="text/javascript">
 3897: // <![CDATA[
 3898: function reorderIPaccess(form,item) {
 3899:     var changedVal;
 3900: $jstext
 3901:     var newpos = 'ipaccess_pos_add';
 3902:     var maxh = 1 + $total;
 3903:     var current = new Array;
 3904:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3905:     if (item == newpos) {
 3906:         changedVal = newitemVal;
 3907:     } else {
 3908:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3909:         current[newitemVal] = newpos;
 3910:     }
 3911:     for (var i=0; i<ipaccess.length; i++) {
 3912:         var elementName = 'ipaccess_pos_'+ipaccess[i];
 3913:         if (elementName != item) {
 3914:             if (form.elements[elementName]) {
 3915:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3916:                 current[currVal] = elementName;
 3917:             }
 3918:         }
 3919:     }
 3920:     var oldVal;
 3921:     for (var j=0; j<maxh; j++) {
 3922:         if (current[j] == undefined) {
 3923:             oldVal = j;
 3924:         }
 3925:     }
 3926:     if (oldVal < changedVal) {
 3927:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3928:            var elementName = current[k];
 3929:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3930:         }
 3931:     } else {
 3932:         for (var k=changedVal; k<oldVal; k++) {
 3933:             var elementName = current[k];
 3934:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3935:         }
 3936:     }
 3937:     return;
 3938: }
 3939: // ]]>
 3940: </script>
 3941: 
 3942: ENDSCRIPT
 3943: }
 3944: 
 3945: sub print_autoenroll {
 3946:     my ($dom,$settings,$rowtotal) = @_;
 3947:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3948:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,
 3949:         $failsafe,$autofailsafe,$failsafesty,%failsafechecked);
 3950:     $failsafesty = 'none';
 3951:     %failsafechecked = (
 3952:         off => ' checked="checked"',
 3953:     );
 3954:     if (ref($settings) eq 'HASH') {
 3955:         if (exists($settings->{'run'})) {
 3956:             if ($settings->{'run'} eq '0') {
 3957:                 $runoff = ' checked="checked" ';
 3958:                 $runon = ' ';
 3959:             } else {
 3960:                 $runon = ' checked="checked" ';
 3961:                 $runoff = ' ';
 3962:             }
 3963:         } else {
 3964:             if ($autorun) {
 3965:                 $runon = ' checked="checked" ';
 3966:                 $runoff = ' ';
 3967:             } else {
 3968:                 $runoff = ' checked="checked" ';
 3969:                 $runon = ' ';
 3970:             }
 3971:         }
 3972:         if (exists($settings->{'co-owners'})) {
 3973:             if ($settings->{'co-owners'} eq '0') {
 3974:                 $coownersoff = ' checked="checked" ';
 3975:                 $coownerson = ' ';
 3976:             } else {
 3977:                 $coownerson = ' checked="checked" ';
 3978:                 $coownersoff = ' ';
 3979:             }
 3980:         } else {
 3981:             $coownersoff = ' checked="checked" ';
 3982:             $coownerson = ' ';
 3983:         }
 3984:         if (exists($settings->{'sender_domain'})) {
 3985:             $defdom = $settings->{'sender_domain'};
 3986:         }
 3987:         if (exists($settings->{'failsafe'})) {
 3988:             $failsafe = $settings->{'failsafe'};
 3989:             if ($failsafe eq 'zero') {
 3990:                 $failsafechecked{'zero'} = ' checked="checked"';
 3991:                 $failsafechecked{'off'} = '';
 3992:                 $failsafesty = 'inline-block';
 3993:             } elsif ($failsafe eq 'any') {
 3994:                 $failsafechecked{'any'} = ' checked="checked"';
 3995:                 $failsafechecked{'off'} = '';
 3996:             }
 3997:             $autofailsafe = $settings->{'autofailsafe'};
 3998:         } elsif (exists($settings->{'autofailsafe'})) {
 3999:             $autofailsafe = $settings->{'autofailsafe'};
 4000:             if ($autofailsafe ne '') {
 4001:                 $failsafechecked{'zero'} = ' checked="checked"';
 4002:                 $failsafe = 'zero';
 4003:                 $failsafechecked{'off'} = '';
 4004:             }
 4005:         }
 4006:     } else {
 4007:         if ($autorun) {
 4008:             $runon = ' checked="checked" ';
 4009:             $runoff = ' ';
 4010:         } else {
 4011:             $runoff = ' checked="checked" ';
 4012:             $runon = ' ';
 4013:         }
 4014:     }
 4015:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 4016:     my $notif_sender;
 4017:     if (ref($settings) eq 'HASH') {
 4018:         $notif_sender = $settings->{'sender_uname'};
 4019:     }
 4020:     my $datatable='<tr class="LC_odd_row">'.
 4021:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 4022:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4023:                   '<input type="radio" name="autoenroll_run"'.
 4024:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4025:                   '<label><input type="radio" name="autoenroll_run"'.
 4026:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4027:                   '</tr><tr>'.
 4028:                   '<td>'.&mt('Notification messages - sender').
 4029:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 4030:                   &mt('username').':&nbsp;'.
 4031:                   '<input type="text" name="sender_uname" value="'.
 4032:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 4033:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 4034:                   '<tr class="LC_odd_row">'.
 4035:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 4036:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4037:                   '<input type="radio" name="autoassign_coowners"'.
 4038:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4039:                   '<label><input type="radio" name="autoassign_coowners"'.
 4040:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4041:                   '</tr><tr>'.
 4042:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 4043:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 4044:                   '<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; '.
 4045:                   '<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 />'.
 4046:                   '<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>'.
 4047:                   '<div class="LC_floatleft" style="display:'.$failsafesty.';" id="autoenroll_failsafe_div">'.
 4048:                   '<span class="LC_nobreak">'.
 4049:                   &mt('Threshold for number of students in section to drop: [_1]',
 4050:                       '<input type="text" name="autoenroll_autofailsafe" value="'.$autofailsafe.'" size="4" />').
 4051:                   '</span></div></td></tr>';
 4052:     $$rowtotal += 4;
 4053:     return $datatable;
 4054: }
 4055: 
 4056: sub print_autoupdate {
 4057:     my ($position,$dom,$settings,$rowtotal) = @_;
 4058:     my ($enable,$datatable);
 4059:     if ($position eq 'top') {
 4060:         my %choices = &Apache::lonlocal::texthash (
 4061:                           run        => 'Auto-update active?',
 4062:                           classlists => 'Update information in classlists?',
 4063:                           unexpired  => 'Skip updates for users without active or future roles?',
 4064:                           lastactive => 'Skip updates for inactive users?',
 4065:         );
 4066:         my $itemcount = 0;
 4067:         my $updateon = ' ';
 4068:         my $updateoff = ' checked="checked" ';
 4069:         if (ref($settings) eq 'HASH') {
 4070:             if ($settings->{'run'} eq '1') {
 4071:                 $updateon = $updateoff;
 4072:                 $updateoff = ' ';
 4073:             }
 4074:         }
 4075:         $enable = '<tr class="LC_odd_row">'.
 4076:                   '<td>'.$choices{'run'}.'</td>'.
 4077:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4078:                   '<input type="radio" name="autoupdate_run"'.
 4079:                   $updateoff.'value="0" />'.&mt('No').'</label>&nbsp;'.
 4080:                   '<label><input type="radio" name="autoupdate_run"'.
 4081:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 4082:                   '</tr>';
 4083:         my @toggles = ('classlists','unexpired');
 4084:         my %defaultchecked = ('classlists' => 'off',
 4085:                               'unexpired'  => 'off'
 4086:                               );
 4087:         $$rowtotal ++;
 4088:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4089:                                                      \%choices,$itemcount,'','','left','no');
 4090:         $datatable = $enable.$datatable;
 4091:         $$rowtotal += $itemcount;
 4092:         my $lastactiveon = ' ';
 4093:         my $lastactiveoff = ' checked="checked" ';
 4094:         my $lastactivestyle = 'none';
 4095:         my $lastactivedays;
 4096:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 4097:         if (ref($settings) eq 'HASH') {
 4098:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 4099:                 $lastactiveon = $lastactiveoff;
 4100:                 $lastactiveoff = ' ';
 4101:                 $lastactivestyle = 'inline-block';
 4102:                 $lastactivedays = $settings->{'lastactive'};
 4103:             }
 4104:         }
 4105:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4106:         $datatable .= '<tr'.$css_class.'>'.
 4107:                       '<td>'.$choices{'lastactive'}.'</td>'.
 4108:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4109:                       '<input type="radio" name="lastactive"'.
 4110:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4111:                       '&nbsp;<label>'.
 4112:                       '<input type="radio" name="lastactive"'.
 4113:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 4114:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 4115:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 4116:                           '<input type="text" size="5" name="lastactivedays" value="'.
 4117:                           $lastactivedays.'" />').
 4118:                       '</span></td>'.
 4119:                       '</tr>';
 4120:         $$rowtotal ++;
 4121:     } elsif ($position eq 'middle') {
 4122:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4123:         my $numinrow = 3;
 4124:         my $locknamesettings;
 4125:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 4126:                                      $dom,$numinrow,$othertitle,
 4127:                                     'lockablenames',$rowtotal);
 4128:         $$rowtotal ++;
 4129:     } else {
 4130:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4131:         my @fields = ('lastname','firstname','middlename','generation',
 4132:                       'permanentemail','id');
 4133:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4134:         my $numrows = 0;
 4135:         if (ref($types) eq 'ARRAY') {
 4136:             if (@{$types} > 0) {
 4137:                 $datatable = 
 4138:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 4139:                                          \@fields,$types,\$numrows);
 4140:                     $$rowtotal += @{$types}; 
 4141:             }
 4142:         }
 4143:         $datatable .= 
 4144:             &usertype_update_row($settings,{'default' => $othertitle},
 4145:                                  \%fieldtitles,\@fields,['default'],
 4146:                                  \$numrows);
 4147:         $$rowtotal ++;     
 4148:     }
 4149:     return $datatable;
 4150: }
 4151: 
 4152: sub print_autocreate {
 4153:     my ($dom,$settings,$rowtotal) = @_;
 4154:     my (%createon,%createoff,%currhash);
 4155:     my @types = ('xml','req');
 4156:     if (ref($settings) eq 'HASH') {
 4157:         foreach my $item (@types) {
 4158:             $createoff{$item} = ' checked="checked" ';
 4159:             $createon{$item} = ' ';
 4160:             if (exists($settings->{$item})) {
 4161:                 if ($settings->{$item}) {
 4162:                     $createon{$item} = ' checked="checked" ';
 4163:                     $createoff{$item} = ' ';
 4164:                 }
 4165:             }
 4166:         }
 4167:         if ($settings->{'xmldc'} ne '') {
 4168:             $currhash{$settings->{'xmldc'}} = 1;
 4169:         }
 4170:     } else {
 4171:         foreach my $item (@types) {
 4172:             $createoff{$item} = ' checked="checked" ';
 4173:             $createon{$item} = ' ';
 4174:         }
 4175:     }
 4176:     $$rowtotal += 2;
 4177:     my $numinrow = 2;
 4178:     my $datatable='<tr class="LC_odd_row">'.
 4179:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 4180:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4181:                   '<input type="radio" name="autocreate_xml"'.
 4182:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4183:                   '<label><input type="radio" name="autocreate_xml"'.
 4184:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 4185:                   '</td></tr><tr>'.
 4186:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 4187:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4188:                   '<input type="radio" name="autocreate_req"'.
 4189:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4190:                   '<label><input type="radio" name="autocreate_req"'.
 4191:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 4192:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4193:                                                    'autocreate_xmldc',%currhash);
 4194:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 4195:     if ($numdc > 1) {
 4196:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 4197:                       '</td><td class="LC_left_item">';
 4198:     } else {
 4199:         $datatable .= &mt('Course creation processed as:').
 4200:                       '</td><td class="LC_right_item">';
 4201:     }
 4202:     $datatable .= $dctable.'</td></tr>';
 4203:     $$rowtotal += $rows;
 4204:     return $datatable;
 4205: }
 4206: 
 4207: sub print_directorysrch {
 4208:     my ($position,$dom,$settings,$rowtotal) = @_;
 4209:     my $datatable;
 4210:     if ($position eq 'top') {
 4211:         my $instsrchon = ' ';
 4212:         my $instsrchoff = ' checked="checked" ';
 4213:         my ($exacton,$containson,$beginson);
 4214:         my $instlocalon = ' ';
 4215:         my $instlocaloff = ' checked="checked" ';
 4216:         if (ref($settings) eq 'HASH') {
 4217:             if ($settings->{'available'} eq '1') {
 4218:                 $instsrchon = $instsrchoff;
 4219:                 $instsrchoff = ' ';
 4220:             }
 4221:             if ($settings->{'localonly'} eq '1') {
 4222:                 $instlocalon = $instlocaloff;
 4223:                 $instlocaloff = ' ';
 4224:             }
 4225:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 4226:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 4227:                     if ($type eq 'exact') {
 4228:                         $exacton = ' checked="checked" ';
 4229:                     } elsif ($type eq 'contains') {
 4230:                         $containson = ' checked="checked" ';
 4231:                     } elsif ($type eq 'begins') {
 4232:                         $beginson = ' checked="checked" ';
 4233:                     }
 4234:                 }
 4235:             } else {
 4236:                 if ($settings->{'searchtypes'} eq 'exact') {
 4237:                     $exacton = ' checked="checked" ';
 4238:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 4239:                     $containson = ' checked="checked" ';
 4240:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 4241:                     $exacton = ' checked="checked" ';
 4242:                     $containson = ' checked="checked" ';
 4243:                 }
 4244:             }
 4245:         }
 4246:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 4247:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4248: 
 4249:         my $numinrow = 4;
 4250:         my $cansrchrow = 0;
 4251:         $datatable='<tr class="LC_odd_row">'.
 4252:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 4253:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4254:                    '<input type="radio" name="dirsrch_available"'.
 4255:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4256:                    '<label><input type="radio" name="dirsrch_available"'.
 4257:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4258:                    '</tr><tr>'.
 4259:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 4260:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4261:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 4262:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4263:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 4264:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4265:                    '</tr>';
 4266:         $$rowtotal += 2;
 4267:         if (ref($usertypes) eq 'HASH') {
 4268:             if (keys(%{$usertypes}) > 0) {
 4269:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4270:                                              $numinrow,$othertitle,'cansearch',
 4271:                                              $rowtotal);
 4272:                 $cansrchrow = 1;
 4273:             }
 4274:         }
 4275:         if ($cansrchrow) {
 4276:             $$rowtotal ++;
 4277:             $datatable .= '<tr>';
 4278:         } else {
 4279:             $datatable .= '<tr class="LC_odd_row">';
 4280:         }
 4281:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 4282:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 4283:         foreach my $title (@{$titleorder}) {
 4284:             if (defined($searchtitles->{$title})) {
 4285:                 my $check = ' ';
 4286:                 if (ref($settings) eq 'HASH') {
 4287:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 4288:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 4289:                             $check = ' checked="checked" ';
 4290:                         }
 4291:                     }
 4292:                 }
 4293:                 $datatable .= '<td class="LC_left_item">'.
 4294:                               '<span class="LC_nobreak"><label>'.
 4295:                               '<input type="checkbox" name="searchby" '.
 4296:                               'value="'.$title.'"'.$check.'/>'.
 4297:                               $searchtitles->{$title}.'</label></span></td>';
 4298:             }
 4299:         }
 4300:         $datatable .= '</tr></table></td></tr>';
 4301:         $$rowtotal ++;
 4302:         if ($cansrchrow) {
 4303:             $datatable .= '<tr class="LC_odd_row">';
 4304:         } else {
 4305:             $datatable .= '<tr>';
 4306:         }
 4307:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 4308:                       '<td class="LC_left_item" colspan="2">'.
 4309:                       '<span class="LC_nobreak"><label>'.
 4310:                       '<input type="checkbox" name="searchtypes" '.
 4311:                       $exacton.' value="exact" />'.&mt('Exact match').
 4312:                       '</label>&nbsp;'.
 4313:                       '<label><input type="checkbox" name="searchtypes" '.
 4314:                       $beginson.' value="begins" />'.&mt('Begins with').
 4315:                       '</label>&nbsp;'.
 4316:                       '<label><input type="checkbox" name="searchtypes" '.
 4317:                       $containson.' value="contains" />'.&mt('Contains').
 4318:                       '</label></span></td></tr>';
 4319:         $$rowtotal ++;
 4320:     } else {
 4321:         my $domsrchon = ' checked="checked" ';
 4322:         my $domsrchoff = ' ';
 4323:         my $domlocalon = ' ';
 4324:         my $domlocaloff = ' checked="checked" ';
 4325:         if (ref($settings) eq 'HASH') {
 4326:             if ($settings->{'lclocalonly'} eq '1') {
 4327:                 $domlocalon = $domlocaloff;
 4328:                 $domlocaloff = ' ';
 4329:             }
 4330:             if ($settings->{'lcavailable'} eq '0') {
 4331:                 $domsrchoff = $domsrchon;
 4332:                 $domsrchon = ' ';
 4333:             }
 4334:         }
 4335:         $datatable='<tr class="LC_odd_row">'.
 4336:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 4337:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4338:                       '<input type="radio" name="dirsrch_domavailable"'.
 4339:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4340:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 4341:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4342:                       '</tr><tr>'.
 4343:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 4344:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4345:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 4346:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4347:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 4348:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4349:                       '</tr>';
 4350:         $$rowtotal += 2;
 4351:     }
 4352:     return $datatable;
 4353: }
 4354: 
 4355: sub print_contacts {
 4356:     my ($position,$dom,$settings,$rowtotal) = @_;
 4357:     my $datatable;
 4358:     my @contacts = ('adminemail','supportemail');
 4359:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 4360:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 4361:     if ($position eq 'top') {
 4362:         if (ref($settings) eq 'HASH') {
 4363:             foreach my $item (@contacts) {
 4364:                 if (exists($settings->{$item})) {
 4365:                     $to{$item} = $settings->{$item};
 4366:                 }
 4367:             }
 4368:         }
 4369:     } elsif ($position eq 'middle') {
 4370:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 4371:                      'updatesmail','idconflictsmail','hostipmail');
 4372:         foreach my $type (@mailings) {
 4373:             $otheremails{$type} = '';
 4374:         }
 4375:     } elsif ($position eq 'lower') {
 4376:         if (ref($settings) eq 'HASH') {
 4377:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 4378:                 %lonstatus = %{$settings->{'lonstatus'}};
 4379:             }
 4380:         }
 4381:     } else {
 4382:         @mailings = ('helpdeskmail','otherdomsmail');
 4383:         foreach my $type (@mailings) {
 4384:             $otheremails{$type} = '';
 4385:         }
 4386:         $bccemails{'helpdeskmail'} = '';
 4387:         $bccemails{'otherdomsmail'} = '';
 4388:         $includestr{'helpdeskmail'} = '';
 4389:         $includestr{'otherdomsmail'} = '';
 4390:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 4391:     }
 4392:     if (ref($settings) eq 'HASH') {
 4393:         unless (($position eq 'top') || ($position eq 'lower')) {
 4394:             foreach my $type (@mailings) {
 4395:                 if (exists($settings->{$type})) {
 4396:                     if (ref($settings->{$type}) eq 'HASH') {
 4397:                         foreach my $item (@contacts) {
 4398:                             if ($settings->{$type}{$item}) {
 4399:                                 $checked{$type}{$item} = ' checked="checked" ';
 4400:                             }
 4401:                         }
 4402:                         $otheremails{$type} = $settings->{$type}{'others'};
 4403:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4404:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 4405:                             if ($settings->{$type}{'include'} ne '') {
 4406:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4407:                                 $includestr{$type} = &unescape($includestr{$type});
 4408:                             }
 4409:                         }
 4410:                     }
 4411:                 } elsif ($type eq 'lonstatusmail') {
 4412:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4413:                 }
 4414:             }
 4415:         }
 4416:         if ($position eq 'bottom') {
 4417:             foreach my $type (@mailings) {
 4418:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 4419:                 if ($settings->{$type}{'include'} ne '') {
 4420:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4421:                     $includestr{$type} = &unescape($includestr{$type});
 4422:                 }
 4423:             }
 4424:             if (ref($settings->{'helpform'}) eq 'HASH') {
 4425:                 if (ref($fields) eq 'ARRAY') {
 4426:                     foreach my $field (@{$fields}) {
 4427:                         $currfield{$field} = $settings->{'helpform'}{$field};
 4428:                     }
 4429:                 }
 4430:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 4431:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 4432:                 } else {
 4433:                     $maxsize = '1.0';
 4434:                 }
 4435:             } else {
 4436:                 if (ref($fields) eq 'ARRAY') {
 4437:                     foreach my $field (@{$fields}) {
 4438:                         $currfield{$field} = 'yes';
 4439:                     }
 4440:                 }
 4441:                 $maxsize = '1.0';
 4442:             }
 4443:         }
 4444:     } else {
 4445:         if ($position eq 'top') {
 4446:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 4447:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 4448:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 4449:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 4450:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4451:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 4452:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 4453:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 4454:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 4455:         } elsif ($position eq 'bottom') {
 4456:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 4457:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 4458:             if (ref($fields) eq 'ARRAY') {
 4459:                 foreach my $field (@{$fields}) {
 4460:                     $currfield{$field} = 'yes';
 4461:                 }
 4462:             }
 4463:             $maxsize = '1.0';
 4464:         }
 4465:     }
 4466:     my ($titles,$short_titles) = &contact_titles();
 4467:     my $rownum = 0;
 4468:     my $css_class;
 4469:     if ($position eq 'top') {
 4470:         foreach my $item (@contacts) {
 4471:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4472:             $datatable .= '<tr'.$css_class.'>'. 
 4473:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4474:                           '</span></td><td class="LC_right_item">'.
 4475:                           '<input type="text" name="'.$item.'" value="'.
 4476:                           $to{$item}.'" /></td></tr>';
 4477:             $rownum ++;
 4478:         }
 4479:     } elsif ($position eq 'bottom') {
 4480:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4481:         $datatable .= '<tr'.$css_class.'>'.
 4482:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 4483:                       &mt('(e-mail, subject, and description always shown)').
 4484:                       '</td><td class="LC_left_item">';
 4485:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 4486:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 4487:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 4488:             foreach my $field (@{$fields}) {
 4489:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 4490:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 4491:                     $datatable .= ' '.&mt('(logged-in users)');
 4492:                 }
 4493:                 $datatable .='</td><td>';
 4494:                 my $clickaction;
 4495:                 if ($field eq 'screenshot') {
 4496:                     $clickaction = ' onclick="screenshotSize(this);"';
 4497:                 }
 4498:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 4499:                     foreach my $option (@{$possoptions->{$field}}) {
 4500:                         my $checked;
 4501:                         if ($currfield{$field} eq $option) {
 4502:                             $checked = ' checked="checked"';
 4503:                         }
 4504:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4505:                                       '<input type="radio" name="helpform_'.$field.'" '.
 4506:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 4507:                                       '</label></span>'.('&nbsp;'x2);
 4508:                     }
 4509:                 }
 4510:                 if ($field eq 'screenshot') {
 4511:                     my $display;
 4512:                     if ($currfield{$field} eq 'no') {
 4513:                         $display = ' style="display:none"';
 4514:                     }
 4515:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 4516:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 4517:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 4518:                 }
 4519:                 $datatable .= '</td></tr>';
 4520:             }
 4521:             $datatable .= '</table>';
 4522:         }
 4523:         $datatable .= '</td></tr>'."\n";
 4524:         $rownum ++;
 4525:     }
 4526:     unless (($position eq 'top') || ($position eq 'lower')) {
 4527:         foreach my $type (@mailings) {
 4528:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4529:             $datatable .= '<tr'.$css_class.'>'.
 4530:                           '<td><span class="LC_nobreak">'.
 4531:                           $titles->{$type}.': </span></td>'.
 4532:                           '<td class="LC_left_item">';
 4533:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4534:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 4535:             }
 4536:             $datatable .= '<span class="LC_nobreak">';
 4537:             foreach my $item (@contacts) {
 4538:                 $datatable .= '<label>'.
 4539:                               '<input type="checkbox" name="'.$type.'"'.
 4540:                               $checked{$type}{$item}.
 4541:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 4542:                               '</label>&nbsp;';
 4543:             }
 4544:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4545:                           '<input type="text" name="'.$type.'_others" '.
 4546:                           'value="'.$otheremails{$type}.'"  />';
 4547:             my %locchecked;
 4548:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4549:                 foreach my $loc ('s','b') {
 4550:                     if ($includeloc{$type} eq $loc) {
 4551:                         $locchecked{$loc} = ' checked="checked"';
 4552:                         last;
 4553:                     }
 4554:                 }
 4555:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4556:                               '<input type="text" name="'.$type.'_bcc" '.
 4557:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 4558:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4559:                               &mt('Text automatically added to e-mail:').' '.
 4560:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 4561:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4562:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4563:                               ('&nbsp;'x2).
 4564:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4565:                               '</span></fieldset>';
 4566:             }
 4567:             $datatable .= '</td></tr>'."\n";
 4568:             $rownum ++;
 4569:         }
 4570:     }
 4571:     if ($position eq 'middle') {
 4572:         my %choices;
 4573:         my $corelink = &core_link_msu();
 4574:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 4575:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 4576:                                         $corelink);
 4577:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 4578:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 4579:         my %defaultchecked = ('reporterrors'  => 'on',
 4580:                               'reportupdates' => 'on',
 4581:                               'reportstatus'  => 'on');
 4582:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4583:                                                    \%choices,$rownum);
 4584:         $datatable .= $reports;
 4585:     } elsif ($position eq 'lower') {
 4586:         my (%current,%excluded,%weights);
 4587:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 4588:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 4589:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 4590:         } else {
 4591:             $current{'errorthreshold'} = $defaults->{'threshold'};
 4592:         }
 4593:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 4594:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 4595:         } else {
 4596:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 4597:         }
 4598:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 4599:             foreach my $type ('E','W','N','U') {
 4600:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 4601:                     $weights{$type} = $lonstatus{'weights'}{$type};
 4602:                 } else {
 4603:                     $weights{$type} = $defaults->{$type};
 4604:                 }
 4605:             }
 4606:         } else {
 4607:             foreach my $type ('E','W','N','U') {
 4608:                 $weights{$type} = $defaults->{$type};
 4609:             }
 4610:         }
 4611:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 4612:             if (@{$lonstatus{'excluded'}} > 0) {
 4613:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 4614:             }
 4615:         }
 4616:         foreach my $item ('errorthreshold','errorsysmail') {
 4617:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4618:             $datatable .= '<tr'.$css_class.'>'.
 4619:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 4620:                           $titles->{$item}.
 4621:                           '</span></td><td class="LC_left_item">'.
 4622:                           '<input type="text" name="'.$item.'" value="'.
 4623:                           $current{$item}.'" size="5" /></td></tr>';
 4624:             $rownum ++;
 4625:         }
 4626:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4627:         $datatable .= '<tr'.$css_class.'>'.
 4628:                       '<td class="LC_left_item">'.
 4629:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 4630:                       '</span></td><td class="LC_left_item"><table><tr>';
 4631:         foreach my $type ('E','W','N','U') {
 4632:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 4633:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 4634:                           $weights{$type}.'" size="5" /></td>';
 4635:         }
 4636:         $datatable .= '</tr></table></tr>';
 4637:         $rownum ++;
 4638:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4639:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 4640:                       $titles->{'errorexcluded'}.'</td>'.
 4641:                       '<td class="LC_left_item"><table>';
 4642:         my $numinrow = 4;
 4643:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 4644:         for (my $i=0; $i<@ids; $i++) {
 4645:             my $rem = $i%($numinrow);
 4646:             if ($rem == 0) {
 4647:                 if ($i > 0) {
 4648:                     $datatable .= '</tr>';
 4649:                 }
 4650:                 $datatable .= '<tr>';
 4651:             }
 4652:             my $check;
 4653:             if ($excluded{$ids[$i]}) {
 4654:                 $check = ' checked="checked" ';
 4655:             }
 4656:             $datatable .= '<td class="LC_left_item">'.
 4657:                           '<span class="LC_nobreak"><label>'.
 4658:                           '<input type="checkbox" name="errorexcluded" '.
 4659:                           'value="'.$ids[$i].'"'.$check.' />'.
 4660:                           $ids[$i].'</label></span></td>';
 4661:         }
 4662:         my $colsleft = $numinrow - @ids%($numinrow);
 4663:         if ($colsleft > 1 ) {
 4664:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4665:                           '&nbsp;</td>';
 4666:         } elsif ($colsleft == 1) {
 4667:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4668:         }
 4669:         $datatable .= '</tr></table></td></tr>';
 4670:         $rownum ++;
 4671:     } elsif ($position eq 'bottom') {
 4672:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4673:         my (@posstypes,%usertypeshash);
 4674:         if (ref($types) eq 'ARRAY') {
 4675:             @posstypes = @{$types};
 4676:         }
 4677:         if (@posstypes) {
 4678:             if (ref($usertypes) eq 'HASH') {
 4679:                 %usertypeshash = %{$usertypes};
 4680:             }
 4681:             my @overridden;
 4682:             my $numinrow = 4;
 4683:             if (ref($settings) eq 'HASH') {
 4684:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 4685:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 4686:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 4687:                             push(@overridden,$key);
 4688:                             foreach my $item (@contacts) {
 4689:                                 if ($settings->{'overrides'}{$key}{$item}) {
 4690:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 4691:                                 }
 4692:                             }
 4693:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 4694:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 4695:                             $includeloc{'override_'.$key} = '';
 4696:                             $includestr{'override_'.$key} = '';
 4697:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 4698:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 4699:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 4700:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 4701:                             }
 4702:                         }
 4703:                     }
 4704:                 }
 4705:             }
 4706:             my $customclass = 'LC_helpdesk_override';
 4707:             my $optionsprefix = 'LC_options_helpdesk_';
 4708: 
 4709:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 4710:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4711:                                          $numinrow,$othertitle,'overrides',
 4712:                                          \$rownum,$onclicktypes,$customclass);
 4713:             $rownum ++;
 4714:             $usertypeshash{'default'} = $othertitle;
 4715:             foreach my $status (@posstypes) {
 4716:                 my $css_class;
 4717:                 if ($rownum%2) {
 4718:                     $css_class = 'LC_odd_row ';
 4719:                 }
 4720:                 $css_class .= $customclass;
 4721:                 my $rowid = $optionsprefix.$status;
 4722:                 my $hidden = 1;
 4723:                 my $currstyle = 'display:none';
 4724:                 if (grep(/^\Q$status\E$/,@overridden)) {
 4725:                     $currstyle = 'display:table-row';
 4726:                     $hidden = 0;
 4727:                 }
 4728:                 my $key = 'override_'.$status;
 4729:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 4730:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 4731:                                                   $usertypeshash{$status},$css_class,$currstyle,
 4732:                                                   \@contacts,$short_titles);
 4733:                 unless ($hidden) {
 4734:                     $rownum ++;
 4735:                 }
 4736:             }
 4737:         }
 4738:     }
 4739:     $$rowtotal += $rownum;
 4740:     return $datatable;
 4741: }
 4742: 
 4743: sub core_link_msu {
 4744:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 4745:                                           &mt('LON-CAPA core group - MSU'),600,500);
 4746: }
 4747: 
 4748: sub overridden_helpdesk {
 4749:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 4750:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 4751:     my $class = 'LC_left_item';
 4752:     if ($css_class) {
 4753:         $css_class = ' class="'.$css_class.'"';
 4754:     }
 4755:     if ($rowid) {
 4756:         $rowid = ' id="'.$rowid.'"';
 4757:     }
 4758:     if ($rowstyle) {
 4759:         $rowstyle = ' style="'.$rowstyle.'"';
 4760:     }
 4761:     my ($output,$description);
 4762:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 4763:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 4764:               "<td>$description</td>\n".
 4765:               '<td class="'.$class.'" colspan="2">'.
 4766:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 4767:               '<span class="LC_nobreak">';
 4768:     if (ref($contacts) eq 'ARRAY') {
 4769:         foreach my $item (@{$contacts}) {
 4770:             my $check;
 4771:             if (ref($checked) eq 'HASH') {
 4772:                $check = $checked->{$item};
 4773:             }
 4774:             my $title;
 4775:             if (ref($short_titles) eq 'HASH') {
 4776:                 $title = $short_titles->{$item};
 4777:             }
 4778:             $output .= '<label>'.
 4779:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 4780:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 4781:         }
 4782:     }
 4783:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4784:                '<input type="text" name="override_'.$type.'_others" '.
 4785:                'value="'.$otheremails.'"  />';
 4786:     my %locchecked;
 4787:     foreach my $loc ('s','b') {
 4788:         if ($includeloc eq $loc) {
 4789:             $locchecked{$loc} = ' checked="checked"';
 4790:             last;
 4791:         }
 4792:     }
 4793:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4794:                '<input type="text" name="override_'.$type.'_bcc" '.
 4795:                'value="'.$bccemails.'"  /></fieldset>'.
 4796:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4797:                &mt('Text automatically added to e-mail:').' '.
 4798:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4799:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4800:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4801:                ('&nbsp;'x2).
 4802:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4803:                '</span></fieldset>'.
 4804:                '</td></tr>'."\n";
 4805:     return $output;
 4806: }
 4807: 
 4808: sub contacts_javascript {
 4809:     return <<"ENDSCRIPT";
 4810: 
 4811: <script type="text/javascript">
 4812: // <![CDATA[
 4813: 
 4814: function screenshotSize(field) {
 4815:     if (document.getElementById('help_screenshotsize')) {
 4816:         if (field.value == 'no') {
 4817:             document.getElementById('help_screenshotsize').style.display="none";
 4818:         } else {
 4819:             document.getElementById('help_screenshotsize').style.display="";
 4820:         }
 4821:     }
 4822:     return;
 4823: }
 4824: 
 4825: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4826:     if (form.elements[checkbox].length != undefined) {
 4827:         var count = 0;
 4828:         if (docount) {
 4829:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4830:                 if (form.elements[checkbox][i].checked) {
 4831:                     count ++;
 4832:                 }
 4833:             }
 4834:         }
 4835:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4836:             var type = form.elements[checkbox][i].value;
 4837:             if (document.getElementById(prefix+type)) {
 4838:                 if (form.elements[checkbox][i].checked) {
 4839:                     document.getElementById(prefix+type).style.display = 'table-row';
 4840:                     if (count % 2 == 1) {
 4841:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4842:                     } else {
 4843:                         document.getElementById(prefix+type).className = target;
 4844:                     }
 4845:                     count ++;
 4846:                 } else {
 4847:                     document.getElementById(prefix+type).style.display = 'none';
 4848:                 }
 4849:             }
 4850:         }
 4851:     }
 4852:     return;
 4853: }
 4854: 
 4855: // ]]>
 4856: </script>
 4857: 
 4858: ENDSCRIPT
 4859: }
 4860: 
 4861: sub print_helpsettings {
 4862:     my ($position,$dom,$settings,$rowtotal) = @_;
 4863:     my $confname = $dom.'-domainconfig';
 4864:     my $formname = 'display';
 4865:     my ($datatable,$itemcount);
 4866:     if ($position eq 'top') {
 4867:         $itemcount = 1;
 4868:         my (%choices,%defaultchecked,@toggles);
 4869:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4870:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4871:                                      &mt('LON-CAPA bug tracker'),600,500));
 4872:         %defaultchecked = ('submitbugs' => 'on');
 4873:         @toggles = ('submitbugs');
 4874:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4875:                                                      \%choices,$itemcount);
 4876:         $$rowtotal ++;
 4877:     } else {
 4878:         my $css_class;
 4879:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4880:         my (%customroles,%ordered,%current);
 4881:         if (ref($settings) eq 'HASH') {
 4882:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4883:                 %current = %{$settings->{'adhoc'}};
 4884:             }
 4885:         }
 4886:         my $count = 0;
 4887:         foreach my $key (sort(keys(%existing))) {
 4888:             if ($key=~/^rolesdef\_(\w+)$/) {
 4889:                 my $rolename = $1;
 4890:                 my (%privs,$order);
 4891:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4892:                 $customroles{$rolename} = \%privs;
 4893:                 if (ref($current{$rolename}) eq 'HASH') {
 4894:                     $order = $current{$rolename}{'order'};
 4895:                 }
 4896:                 if ($order eq '') {
 4897:                     $order = $count;
 4898:                 }
 4899:                 $ordered{$order} = $rolename;
 4900:                 $count++;
 4901:             }
 4902:         }
 4903:         my $maxnum = scalar(keys(%ordered));
 4904:         my @roles_by_num = ();
 4905:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4906:             push(@roles_by_num,$item);
 4907:         }
 4908:         my $context = 'domprefs';
 4909:         my $crstype = 'Course';
 4910:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4911:         my @accesstypes = ('all','dh','da','none');
 4912:         my ($numstatustypes,@jsarray);
 4913:         if (ref($types) eq 'ARRAY') {
 4914:             if (@{$types} > 0) {
 4915:                 $numstatustypes = scalar(@{$types});
 4916:                 push(@accesstypes,'status');
 4917:                 @jsarray = ('bystatus');
 4918:             }
 4919:         }
 4920:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4921:         if (keys(%domhelpdesk)) {
 4922:             push(@accesstypes,('inc','exc'));
 4923:             push(@jsarray,('notinc','notexc'));
 4924:         }
 4925:         my $hiddenstr = join("','",@jsarray);
 4926:         my $context = 'domprefs';
 4927:         my $crstype = 'Course';
 4928:         my $prefix = 'helproles_';
 4929:         my $add_class = 'LC_hidden';
 4930:         foreach my $num (@roles_by_num) {
 4931:             my $role = $ordered{$num};
 4932:             my ($desc,$access,@statuses);
 4933:             if (ref($current{$role}) eq 'HASH') {
 4934:                 $desc = $current{$role}{'desc'};
 4935:                 $access = $current{$role}{'access'};
 4936:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4937:                     @statuses = @{$current{$role}{'insttypes'}};
 4938:                 }
 4939:             }
 4940:             if ($desc eq '') {
 4941:                 $desc = $role;
 4942:             }
 4943:             my $identifier = 'custhelp'.$num;
 4944:             my %full=();
 4945:             my %levels= (
 4946:                          course => {},
 4947:                          domain => {},
 4948:                          system => {},
 4949:                         );
 4950:             my %levelscurrent=(
 4951:                                course => {},
 4952:                                domain => {},
 4953:                                system => {},
 4954:                               );
 4955:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4956:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4957:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4958:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4959:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 4960:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4961:             for (my $k=0; $k<=$maxnum; $k++) {
 4962:                 my $vpos = $k+1;
 4963:                 my $selstr;
 4964:                 if ($k == $num) {
 4965:                     $selstr = ' selected="selected" ';
 4966:                 }
 4967:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4968:             }
 4969:             $datatable .= '</select>'.('&nbsp;'x2).
 4970:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 4971:                           '</td>'.
 4972:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4973:                           &mt('Name shown to users:').
 4974:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 4975:                           '</fieldset>'.
 4976:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4977:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4978:                           '<fieldset>'.
 4979:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4980:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4981:                                                                    \%levelscurrent,$identifier,
 4982:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4983:                           '</fieldset></td>';
 4984:             $itemcount ++;
 4985:         }
 4986:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4987:         my $newcust = 'custhelp'.$count;
 4988:         my (%privs,%levelscurrent);
 4989:         my %full=();
 4990:         my %levels= (
 4991:                      course => {},
 4992:                      domain => {},
 4993:                      system => {},
 4994:                     );
 4995:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4996:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4997:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4998:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 4999:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 5000:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 5001:         for (my $k=0; $k<$maxnum+1; $k++) {
 5002:             my $vpos = $k+1;
 5003:             my $selstr;
 5004:             if ($k == $maxnum) {
 5005:                 $selstr = ' selected="selected" ';
 5006:             }
 5007:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5008:         }
 5009:         $datatable .= '</select>&nbsp;'."\n".
 5010:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 5011:                       '</label></span></td>'.
 5012:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 5013:                       '<span class="LC_nobreak">'.
 5014:                       &mt('Internal name:').
 5015:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 5016:                       '</span>'.('&nbsp;'x4).
 5017:                       '<span class="LC_nobreak">'.
 5018:                       &mt('Name shown to users:').
 5019:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 5020:                       '</span></fieldset>'.
 5021:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 5022:                                              $usertypes,$types,\%domhelpdesk).
 5023:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 5024:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 5025:                                                                 \@templateroles,$newcust).
 5026:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 5027:                                                                \%levelscurrent,$newcust).
 5028:                       '</fieldset>'.
 5029:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 5030:                       '</td></tr>';
 5031:         $count ++;
 5032:         $$rowtotal += $count;
 5033:     }
 5034:     return $datatable;
 5035: }
 5036: 
 5037: sub adhocbutton {
 5038:     my ($prefix,$num,$field,$visibility) = @_;
 5039:     my %lt = &Apache::lonlocal::texthash(
 5040:                                           show => 'Show details',
 5041:                                           hide => 'Hide details',
 5042:                                         );
 5043:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 5044:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 5045:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 5046:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 5047: }
 5048: 
 5049: sub helpsettings_javascript {
 5050:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 5051:     return unless(ref($roles_by_num) eq 'ARRAY');
 5052:     my %html_js_lt = &Apache::lonlocal::texthash(
 5053:                                           show => 'Show details',
 5054:                                           hide => 'Hide details',
 5055:                                         );
 5056:     &html_escape(\%html_js_lt);
 5057:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 5058:     return <<"ENDSCRIPT";
 5059: <script type="text/javascript">
 5060: // <![CDATA[
 5061: 
 5062: function reorderHelpRoles(form,item) {
 5063:     var changedVal;
 5064: $jstext
 5065:     var newpos = 'helproles_${total}_pos';
 5066:     var maxh = 1 + $total;
 5067:     var current = new Array();
 5068:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5069:     if (item == newpos) {
 5070:         changedVal = newitemVal;
 5071:     } else {
 5072:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5073:         current[newitemVal] = newpos;
 5074:     }
 5075:     for (var i=0; i<helproles.length; i++) {
 5076:         var elementName = 'helproles_'+helproles[i]+'_pos';
 5077:         if (elementName != item) {
 5078:             if (form.elements[elementName]) {
 5079:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5080:                 current[currVal] = elementName;
 5081:             }
 5082:         }
 5083:     }
 5084:     var oldVal;
 5085:     for (var j=0; j<maxh; j++) {
 5086:         if (current[j] == undefined) {
 5087:             oldVal = j;
 5088:         }
 5089:     }
 5090:     if (oldVal < changedVal) {
 5091:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5092:            var elementName = current[k];
 5093:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5094:         }
 5095:     } else {
 5096:         for (var k=changedVal; k<oldVal; k++) {
 5097:             var elementName = current[k];
 5098:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5099:         }
 5100:     }
 5101:     return;
 5102: }
 5103: 
 5104: function helpdeskAccess(num) {
 5105:     var curraccess = null;
 5106:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 5107:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 5108:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 5109:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 5110:             }
 5111:         }
 5112:     }
 5113:     var shown = Array();
 5114:     var hidden = Array();
 5115:     if (curraccess == 'none') {
 5116:         hidden = Array('$hiddenstr');
 5117:     } else {
 5118:         if (curraccess == 'status') {
 5119:             shown = Array('bystatus');
 5120:             hidden = Array('notinc','notexc');
 5121:         } else {
 5122:             if (curraccess == 'exc') {
 5123:                 shown = Array('notexc');
 5124:                 hidden = Array('notinc','bystatus');
 5125:             }
 5126:             if (curraccess == 'inc') {
 5127:                 shown = Array('notinc');
 5128:                 hidden = Array('notexc','bystatus');
 5129:             }
 5130:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 5131:                 hidden = Array('notinc','notexc','bystatus');
 5132:             }
 5133:         }
 5134:     }
 5135:     if (hidden.length > 0) {
 5136:         for (var i=0; i<hidden.length; i++) {
 5137:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 5138:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 5139:             }
 5140:         }
 5141:     }
 5142:     if (shown.length > 0) {
 5143:         for (var i=0; i<shown.length; i++) {
 5144:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 5145:                 if (shown[i] == 'privs') {
 5146:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 5147:                 } else {
 5148:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 5149:                 }
 5150:             }
 5151:         }
 5152:     }
 5153:     return;
 5154: }
 5155: 
 5156: function toggleHelpdeskItem(num,field) {
 5157:     if (document.getElementById('helproles_'+num+'_'+field)) {
 5158:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 5159:             document.getElementById('helproles_'+num+'_'+field).className =
 5160:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 5161:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5162:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 5163:             }
 5164:         } else {
 5165:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 5166:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5167:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 5168:             }
 5169:         }
 5170:     }
 5171:     return;
 5172: }
 5173: 
 5174: // ]]>
 5175: </script>
 5176: 
 5177: ENDSCRIPT
 5178: }
 5179: 
 5180: sub helpdeskroles_access {
 5181:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 5182:         $usertypes,$types,$domhelpdesk) = @_;
 5183:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 5184:     my %lt = &Apache::lonlocal::texthash(
 5185:                     'rou'    => 'Role usage',
 5186:                     'whi'    => 'Which helpdesk personnel may use this role?',
 5187:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 5188:                     'dh'     => 'All with domain helpdesk role',
 5189:                     'da'     => 'All with domain helpdesk assistant role',
 5190:                     'none'   => 'None',
 5191:                     'status' => 'Determined based on institutional status',
 5192:                     'inc'    => 'Include all, but exclude specific personnel',
 5193:                     'exc'    => 'Exclude all, but include specific personnel',
 5194:                   );
 5195:     my %usecheck = (
 5196:                      all => ' checked="checked"',
 5197:                    );
 5198:     my %displaydiv = (
 5199:                       status => 'none',
 5200:                       inc    => 'none',
 5201:                       exc    => 'none',
 5202:                       priv   => 'block',
 5203:                      );
 5204:     my $output;
 5205:     if (ref($current) eq 'HASH') {
 5206:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 5207:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 5208:                 $usecheck{$current->{access}} = $usecheck{'all'};
 5209:                 delete($usecheck{'all'});
 5210:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 5211:                     my $access = $1;
 5212:                     $displaydiv{$access} = 'inline';
 5213:                 } elsif ($current->{access} eq 'none') {
 5214:                     $displaydiv{'priv'} = 'none';
 5215:                 }
 5216:             }
 5217:         }
 5218:     }
 5219:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 5220:               '<p>'.$lt{'whi'}.'</p>';
 5221:     foreach my $access (@{$accesstypes}) {
 5222:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 5223:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 5224:                    $lt{$access}.'</label>';
 5225:         if ($access eq 'status') {
 5226:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 5227:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 5228:                                                                  $othertitle,$usertypes,$types).
 5229:                        '</div>';
 5230:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 5231:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 5232:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5233:                        '</div>';
 5234:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 5235:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 5236:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5237:                        '</div>';
 5238:         }
 5239:         $output .= '</p>';
 5240:     }
 5241:     $output .= '</fieldset>';
 5242:     return $output;
 5243: }
 5244: 
 5245: sub radiobutton_prefs {
 5246:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 5247:         $additional,$align,$firstval) = @_;
 5248:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 5249:                    (ref($choices) eq 'HASH'));
 5250: 
 5251:     my (%checkedon,%checkedoff,$datatable,$css_class);
 5252: 
 5253:     foreach my $item (@{$toggles}) {
 5254:         if ($defaultchecked->{$item} eq 'on') {
 5255:             $checkedon{$item} = ' checked="checked" ';
 5256:             $checkedoff{$item} = ' ';
 5257:         } elsif ($defaultchecked->{$item} eq 'off') {
 5258:             $checkedoff{$item} = ' checked="checked" ';
 5259:             $checkedon{$item} = ' ';
 5260:         }
 5261:     }
 5262:     if (ref($settings) eq 'HASH') {
 5263:         foreach my $item (@{$toggles}) {
 5264:             if ($settings->{$item} eq '1') {
 5265:                 $checkedon{$item} =  ' checked="checked" ';
 5266:                 $checkedoff{$item} = ' ';
 5267:             } elsif ($settings->{$item} eq '0') {
 5268:                 $checkedoff{$item} =  ' checked="checked" ';
 5269:                 $checkedon{$item} = ' ';
 5270:             }
 5271:         }
 5272:     }
 5273:     if ($onclick) {
 5274:         $onclick = ' onclick="'.$onclick.'"';
 5275:     }
 5276:     foreach my $item (@{$toggles}) {
 5277:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5278:         $datatable .=
 5279:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 5280:             '<span class="LC_nobreak">'.$choices->{$item}.
 5281:             '</span></td>';
 5282:         if ($align eq 'left') {
 5283:             $datatable .= '<td class="LC_left_item">';
 5284:         } else {
 5285:             $datatable .= '<td class="LC_right_item">';
 5286:         }
 5287:         $datatable .= '<span class="LC_nobreak">';
 5288:         if ($firstval eq 'no') {
 5289:             $datatable .=
 5290:                 '<label><input type="radio" name="'.
 5291:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 5292:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5293:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 5294:         } else {
 5295:             $datatable .=
 5296:                 '<label><input type="radio" name="'.
 5297:                 $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 5298:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5299:                 $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 5300:         }
 5301:         $datatable .= '</span>'.$additional.'</td></tr>';
 5302:         $itemcount ++;
 5303:     }
 5304:     return ($datatable,$itemcount);
 5305: }
 5306: 
 5307: sub print_ltitools {
 5308:     my ($position,$dom,$settings,$rowtotal) = @_;
 5309:     my (%rules,%encrypt,%privkeys,%linkprot);
 5310:     if (ref($settings) eq 'HASH') {
 5311:         if ($position eq 'top') {
 5312:             if (exists($settings->{'encrypt'})) {
 5313:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 5314:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 5315:                         $encrypt{'toolsec_'.$key} = $settings->{'encrypt'}{$key};
 5316:                     }
 5317:                 }
 5318:             }
 5319:             if (exists($settings->{'private'})) {
 5320:                 if (ref($settings->{'private'}) eq 'HASH') {
 5321:                     if (ref($settings->{'private'}) eq 'HASH') {
 5322:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 5323:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 5324:                         }
 5325:                     }
 5326:                 }
 5327:             }
 5328:         } elsif ($position eq 'middle') {
 5329:             if (exists($settings->{'rules'})) {
 5330:                 if (ref($settings->{'rules'}) eq 'HASH') {
 5331:                     %rules = %{$settings->{'rules'}};
 5332:                 }
 5333:             }
 5334:         } else {
 5335:             foreach my $key ('encrypt','private','rules') {
 5336:                 if (exists($settings->{$key})) {
 5337:                     delete($settings->{$key});
 5338:                 }
 5339:             }
 5340:         }
 5341:     }
 5342:     my $datatable;
 5343:     my $itemcount = 1;
 5344:     if ($position eq 'top') {
 5345:         $datatable = &secrets_form($dom,'toolsec',\%encrypt,\%privkeys,$rowtotal);
 5346:     } elsif ($position eq 'middle') {
 5347:         $datatable = &password_rules('toolsecrets',\$itemcount,\%rules);
 5348:         $$rowtotal += $itemcount;
 5349:     } else {
 5350:         $datatable = &Apache::courseprefs::print_ltitools($dom,'',$settings,\$rowtotal,'','','domain');
 5351:     }
 5352:     return $datatable;
 5353: }
 5354: 
 5355: sub ltitools_names {
 5356:     my %lt = &Apache::lonlocal::texthash(
 5357:                                           'title'          => 'Title',
 5358:                                           'version'        => 'Version',
 5359:                                           'msgtype'        => 'Message Type',
 5360:                                           'sigmethod'      => 'Signature Method',
 5361:                                           'url'            => 'URL',
 5362:                                           'key'            => 'Key',
 5363:                                           'lifetime'       => 'Nonce lifetime (s)',
 5364:                                           'secret'         => 'Secret',
 5365:                                           'icon'           => 'Icon',
 5366:                                           'user'           => 'User',
 5367:                                           'fullname'       => 'Full Name',
 5368:                                           'firstname'      => 'First Name',
 5369:                                           'lastname'       => 'Last Name',
 5370:                                           'email'          => 'E-mail',
 5371:                                           'roles'          => 'Role',
 5372:                                           'window'         => 'Window',
 5373:                                           'tab'            => 'Tab',
 5374:                                           'iframe'         => 'iFrame',
 5375:                                           'height'         => 'Height',
 5376:                                           'width'          => 'Width',
 5377:                                           'linktext'       => 'Default Link Text',
 5378:                                           'explanation'    => 'Default Explanation',
 5379:                                           'passback'       => 'Tool can return grades:',
 5380:                                           'roster'         => 'Tool can retrieve roster:',
 5381:                                           'crstarget'      => 'Display target',
 5382:                                           'crslabel'       => 'Course label',
 5383:                                           'crstitle'       => 'Course title',
 5384:                                           'crslinktext'    => 'Link Text',
 5385:                                           'crsexplanation' => 'Explanation',
 5386:                                           'crsappend'      => 'Provider URL',
 5387:                                         );
 5388:     return %lt;
 5389: }
 5390: 
 5391: sub secrets_form {
 5392:     my ($dom,$context,$encrypt,$privkeys,$rowtotal) = @_;
 5393:     my @ids=&Apache::lonnet::current_machine_ids();
 5394:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 5395:     my $primary = &Apache::lonnet::domain($dom,'primary');
 5396:     my ($css_class,$extra,$numshown,$itemcount,$output);
 5397:     $itemcount = 0;
 5398:     foreach my $hostid (sort(keys(%servers))) {
 5399:         my ($showextra,$divsty,$switch);
 5400:         if ($hostid eq $primary) {
 5401:             if ($context eq 'ltisec') {
 5402:                 if (($encrypt->{'ltisec_consumers'}) || ($encrypt->{'ltisec_domlinkprot'})) {
 5403:                     $showextra = 1;
 5404:                 }
 5405:                 if ($encrypt->{'ltisec_crslinkprot'}) {
 5406:                     $showextra = 1;
 5407:                 }
 5408:             } else {
 5409:                 if (($encrypt->{'toolsec_crs'}) || ($encrypt->{'toolsec_dom'})) {
 5410:                     $showextra = 1;
 5411:                 }
 5412:             }
 5413:             unless (grep(/^\Q$hostid\E$/,@ids)) {
 5414:                 $switch = 1;
 5415:             }
 5416:             if ($showextra) {
 5417:                 $numshown ++;
 5418:                 $divsty = 'display:inline-block';
 5419:             } else {
 5420:                 $divsty = 'display:none';
 5421:             }
 5422:             $extra .= '<fieldset id="'.$context.'_info_'.$hostid.'" style="'.$divsty.'">'.
 5423:                       '<legend>'.$hostid.'</legend>';
 5424:             if ($switch) {
 5425:                 my $switchserver = '<a href="/adm/switchserver?otherserver='.$hostid.'&amp;role='.
 5426:                                    &HTML::Entities::encode($env{'request.role'},'\'<>"&').
 5427:                                    '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5428:                 if (exists($privkeys->{$hostid})) {
 5429:                     $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" />'.
 5430:                               '<span class="LC_nobreak">'.
 5431:                               &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5432:                               '<span class="LC_nobreak">'.&mt('Change?').
 5433:                               '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5434:                               ('&nbsp;'x2).
 5435:                               '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5436:                               '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5437:                               '<span class="LC_nobreak"> - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5438:                               '</span></div>';
 5439:                 } else {
 5440:                     $extra .= '<span class="LC_nobreak">'.
 5441:                               &mt('Key required').' - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5442:                               '</span>'."\n";
 5443:                 }
 5444:             } elsif (exists($privkeys->{$hostid})) {
 5445:                 $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" /><span class="LC_nobreak">'.
 5446:                           &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5447:                           '<span class="LC_nobreak">'.&mt('Change?').
 5448:                           '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5449:                           ('&nbsp;'x2).
 5450:                           '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5451:                           '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5452:                           '<span class="LC_nobreak">'.&mt('New Key').':'.
 5453:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5454:                           '<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>'.
 5455:                           '</span></div>';
 5456:             } else {
 5457:                 $extra .= '<span class="LC_nobreak">'.&mt('Encryption Key').':'.
 5458:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5459:                           '<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>';
 5460:             }
 5461:             $extra .= '</fieldset>';
 5462:         }
 5463:     }
 5464:     my (%choices,@toggles,%defaultchecked);
 5465:     if ($context eq 'ltisec') {
 5466:         %choices = &Apache::lonlocal::texthash (
 5467:                                                   ltisec_crslinkprot => 'Encrypt stored link protection secrets defined in courses',
 5468:                                                   ltisec_domlinkprot => 'Encrypt stored link protection secrets defined in domain',
 5469:                                                   ltisec_consumers   => 'Encrypt stored consumer secrets defined in domain',
 5470:                                                );
 5471:         @toggles = qw(ltisec_crslinkprot ltisec_domlinkprot ltisec_consumers);
 5472:         %defaultchecked = (
 5473:                            'ltisec_crslinkprot' => 'off',
 5474:                            'ltisec_domlinkprot' => 'off',
 5475:                            'ltisec_consumers'   => 'off',
 5476:                           );
 5477:     } else {
 5478:         %choices = &Apache::lonlocal::texthash (
 5479:                                                   toolsec_crs => 'Encrypt stored external tool secrets defined in courses',
 5480:                                                   toolsec_dom => 'Encrypt stored external tool secrets defined in domain',
 5481:                                                );
 5482:         @toggles = qw(toolsec_crs toolsec_dom);
 5483:         %defaultchecked = (
 5484:                            'toolsec_crs' => 'off',
 5485:                            'toolsec_dom' => 'off',
 5486:                           );
 5487:     }
 5488:     my ($onclick,$itemcount);
 5489:     $onclick = 'javascript:toggleLTIEncKey(this.form,'."'$context'".');';
 5490:     ($output,$itemcount) = &radiobutton_prefs($encrypt,\@toggles,\%defaultchecked,
 5491:                                               \%choices,$itemcount,$onclick,'','left','no');
 5492: 
 5493:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5494:     my $noprivkeysty = 'display:inline-block';
 5495:     if ($numshown) {
 5496:         $noprivkeysty = 'display:none';
 5497:     }
 5498:     $output .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.&mt('Encryption Key(s)').'</td>'.
 5499:                '<td><div id="'.$context.'_noprivkey" style="'.$noprivkeysty.'" >'.
 5500:                '<span class="LC_nobreak">'.&mt('Not in use').'</span></div>'.
 5501:                $extra.
 5502:                '</td></tr>';
 5503:     $itemcount ++;
 5504:     $$rowtotal += $itemcount;
 5505:     return $output;
 5506: }
 5507: 
 5508: sub print_proctoring {
 5509:     my ($dom,$settings,$rowtotal) = @_;
 5510:     my $itemcount = 1;
 5511:     my (%ordered,%providernames,%current,%currentdef);
 5512:     my $confname = $dom.'-domainconfig';
 5513:     my $switchserver = &check_switchserver($dom,$confname);
 5514:     if (ref($settings) eq 'HASH') {
 5515:         foreach my $item (keys(%{$settings})) {
 5516:             if (ref($settings->{$item}) eq 'HASH') {
 5517:                 my $num = $settings->{$item}{'order'};
 5518:                 $ordered{$num} = $item;
 5519:             }
 5520:         }
 5521:     } else {
 5522:         %ordered = (
 5523:                      1 => 'proctorio',
 5524:                      2 => 'examity',
 5525:                    );
 5526:     }
 5527:     %providernames = &proctoring_providernames();
 5528:     my $maxnum = scalar(keys(%ordered));
 5529:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
 5530:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
 5531:     if (ref($requref) eq 'HASH') {
 5532:         %requserfields = %{$requref};
 5533:     }
 5534:     if (ref($opturef) eq 'HASH') {
 5535:         %optuserfields = %{$opturef};
 5536:     }
 5537:     if (ref($defref) eq 'HASH') {
 5538:         %defaults = %{$defref};
 5539:     }
 5540:     if (ref($extref) eq 'HASH') {
 5541:         %extended = %{$extref};
 5542:     }
 5543:     if (ref($crsref) eq 'HASH') {
 5544:         %crsconf = %{$crsref};
 5545:     }
 5546:     if (ref($rolesref) eq 'ARRAY') {
 5547:         @courseroles = @{$rolesref};
 5548:     }
 5549:     if (ref($ltiref) eq 'ARRAY') {
 5550:         @ltiroles = @{$ltiref};
 5551:     }
 5552:     my $datatable;
 5553:     my $css_class;
 5554:     if (keys(%ordered)) {
 5555:         my @items = sort { $a <=> $b } keys(%ordered);
 5556:         for (my $i=0; $i<@items; $i++) {
 5557:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5558:             my $provider = $ordered{$items[$i]};
 5559:             my $optionsty = 'none';
 5560:             my ($available,$version,$lifetime,$imgsrc,$userincdom,$showroles,
 5561:                 %checkedfields,%rolemaps,%inuse,%crsconfig,%current);
 5562:             if (ref($settings) eq 'HASH') {
 5563:                 if (ref($settings->{$provider}) eq 'HASH') {
 5564:                     %current = %{$settings->{$provider}};
 5565:                     if ($current{'available'}) {
 5566:                         $optionsty = 'block';
 5567:                         $available = 1;
 5568:                     }
 5569:                     if ($current{'lifetime'} =~ /^\d+$/) {
 5570:                         $lifetime = $current{'lifetime'};
 5571:                     }
 5572:                     if ($current{'version'} =~ /^\d+\.\d+$/) {
 5573:                         $version = $current{'version'};
 5574:                     }
 5575:                     if ($current{'image'} ne '') {
 5576:                         $imgsrc = '<img src="'.$current{'image'}.'" alt="'.&mt('Proctoring service icon').'" />';
 5577:                     }
 5578:                     if (ref($current{'fields'}) eq 'ARRAY') {
 5579:                         map { $checkedfields{$_} = 1; } @{$current{'fields'}};
 5580:                     }
 5581:                     $userincdom = $current{'incdom'};
 5582:                     if (ref($current{'roles'}) eq 'HASH') {
 5583:                         %rolemaps = %{$current{'roles'}};
 5584:                         $checkedfields{'roles'} = 1;
 5585:                     }
 5586:                     if (ref($current{'defaults'}) eq 'ARRAY') {
 5587:                         foreach my $val (@{$current{'defaults'}}) {
 5588:                             if (grep(/^\Q$val\E$/,@{$defaults{$provider}})) {
 5589:                                 $inuse{$val} = 1;
 5590:                             } else {
 5591:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5592:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5593:                                         if (grep(/^\Q$val\E$/,@{$extended{$provider}{$poss}})) {
 5594:                                             $inuse{$poss} = $val;
 5595:                                             last;
 5596:                                         }
 5597:                                     }
 5598:                                 }
 5599:                             }
 5600:                         }
 5601:                     } elsif (ref($current{'defaults'}) eq 'HASH') {
 5602:                         foreach my $key (keys(%{$current{'defaults'}})) {
 5603:                             my $currval = $current{'defaults'}{$key}; 
 5604:                             if (grep(/^\Q$key\E$/,@{$defaults{$provider}})) {
 5605:                                 $inuse{$key} = 1;
 5606:                             } else {
 5607:                                 my $match;
 5608:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5609:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5610:                                         if (grep(/^\Q$key\E$/,@{$extended{$provider}{$poss}})) {
 5611:                                             $inuse{$poss} = $key;
 5612:                                             last;
 5613:                                         }
 5614:                                     } elsif (ref($extended{$provider}{$poss}) eq 'HASH') {
 5615:                                         foreach my $inner (sort(keys(%{$extended{$provider}{$poss}}))) {
 5616:                                             if (ref($extended{$provider}{$poss}{$inner}) eq 'ARRAY') {
 5617:                                                 if (grep(/^\Q$currval\E$/,@{$extended{$provider}{$poss}{$inner}})) {
 5618:                                                     $currentdef{$inner} = $currval;
 5619:                                                     $match = 1;
 5620:                                                     last;
 5621:                                                 }
 5622:                                             } elsif ($inner eq $key) {
 5623:                                                 $currentdef{$key} = $currval;
 5624:                                                 $match = 1;
 5625:                                                 last;
 5626:                                             }
 5627:                                         }
 5628:                                     }
 5629:                                     last if ($match);
 5630:                                 }
 5631:                             }
 5632:                         }
 5633:                     }
 5634:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
 5635:                         map { $crsconfig{$_} = 1; } @{$current{'crsconf'}};
 5636:                     }
 5637:                 }
 5638:             }
 5639:             my %lt = &proctoring_titles($provider);
 5640:             my %fieldtitles = &proctoring_fieldtitles($provider);
 5641:             my $onclickavailable = ' onclick="toggleProctoring(this.form,'."'$provider'".');"';
 5642:             my %checkedavailable = (
 5643:                                    yes => '',
 5644:                                    no  => ' checked="checked"',
 5645:                                  );
 5646:             if ($available) {
 5647:                 $checkedavailable{'yes'} = $checkedavailable{'no'};
 5648:                 $checkedavailable{'no'} = '';
 5649:             }
 5650:             my $chgstr = ' onchange="javascript:reorderProctoring(this.form,'."'proctoring_pos_".$provider."'".');"';
 5651:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5652:                          .'<select name="proctoring_pos_'.$provider.'"'.$chgstr.'>';
 5653:             for (my $k=0; $k<$maxnum; $k++) {
 5654:                 my $vpos = $k+1;
 5655:                 my $selstr;
 5656:                 if ($k == $i) {
 5657:                     $selstr = ' selected="selected" ';
 5658:                 }
 5659:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5660:             }
 5661:             if ($version eq '') {
 5662:                 if ($provider eq 'proctorio') {
 5663:                     $version = '1.0';
 5664:                 } elsif ($provider eq 'examity') {
 5665:                     $version = '1.1';
 5666:                 }
 5667:             }
 5668:             if ($lifetime eq '') {
 5669:                 $lifetime = '300';
 5670:             }
 5671:             $datatable .=
 5672:                 '</select>'.('&nbsp;'x2).'<b>'.$providernames{$provider}.'</b></span><br />'.
 5673:                 '<span class="LC_nobreak">'.$lt{'avai'}.'&nbsp;'.
 5674:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="1"'.$onclickavailable.$checkedavailable{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5675:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="0"'.$onclickavailable.$checkedavailable{no}.' />'.&mt('No').'</label></span>'."\n".
 5676:                 '</td>'.
 5677:                 '<td colspan="2">'.
 5678:                 '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'base'}.'</legend>'.
 5679:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="proctoring_'.$provider.'_version">'.
 5680:                 '<option value="'.$version.'" selected="selected">'.$version.'</option></select></span> '."\n".
 5681:                 ('&nbsp;'x2).
 5682:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="proctoring_'.$provider.'_sigmethod">'.
 5683:                 '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5684:                 '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5685:                 ('&nbsp;'x2).
 5686:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="proctoring_'.$provider.'_lifetime" value="'.$lifetime.'" /></span> '."\n".
 5687:                 '<br />'.
 5688:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="proctoring_'.$provider.'_url" value="'.$current{'url'}.'" /></span> '."\n".
 5689:                 '<br />'.
 5690:                 '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="proctoring_'.$provider.'_key" value="'.$current{'key'}.'" /></span> '."\n".
 5691:                 ('&nbsp;'x2).
 5692:                 '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="proctoring_'.$provider.'_secret" value="'.$current{'secret'}.'" />'.
 5693:                 '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.proctoring_'.$provider.'_secret.type='."'text'".' } else { this.form.proctoring_'.$provider.'_secret.type='."'password'".' }" />'.$lt{'visible'}.'</label></span><br />'."\n";
 5694:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5695:             if ($imgsrc) {
 5696:                 $datatable .= $imgsrc.
 5697:                               '<label><input type="checkbox" name="proctoring_image_del"'.
 5698:                               ' value="'.$provider.'" />'.&mt('Delete?').'</label></span> '.
 5699:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:');
 5700:             }
 5701:             $datatable .= '&nbsp;';
 5702:             if ($switchserver) {
 5703:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5704:             } else {
 5705:                 $datatable .= '<input type="file" name="proctoring_image_'.$provider.'" value="" />';
 5706:             }
 5707:             unless ($imgsrc) {
 5708:                 $datatable .= '<br />('.&mt('if larger than 21x21 pixels, image will be scaled').')';
 5709:             }
 5710:             $datatable .= '</fieldset>'."\n";
 5711:             if (ref($requserfields{$provider}) eq 'ARRAY') {
 5712:                 if (@{$requserfields{$provider}} > 0) {
 5713:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'requ'}.'</legend>';
 5714:                     foreach my $field (@{$requserfields{$provider}}) {
 5715:                         $datatable .= '<span class="LC_nobreak">'.
 5716:                                       '<label><input type="checkbox" name="proctoring_reqd_'.$provider.'" value="'.$field.'" checked="checked" disabled="disabled" />'.
 5717:                                       $lt{$field}.'</label>';
 5718:                         if ($field eq 'user') {
 5719:                             my $seluserdom = '';
 5720:                             my $unseluserdom = ' selected="selected"';
 5721:                             if ($userincdom) {
 5722:                                 $seluserdom = $unseluserdom;
 5723:                                 $unseluserdom = '';
 5724:                             }
 5725:                             $datatable .= ':&nbsp;'.
 5726:                                 '<select name="proctoring_userincdom_'.$provider.'">'.
 5727:                                 '<option value="0"'.$unseluserdom.'>'.$lt{'username'}.'</option>'.
 5728:                                 '<option value="1"'.$seluserdom.'>'.$lt{'uname:dom'}.'</option>'.
 5729:                                 '</select>&nbsp;';
 5730:                         } else {
 5731:                             $datatable .= '&nbsp;';
 5732:                             if ($field eq 'roles') {
 5733:                                 $showroles = 1; 
 5734:                             } 
 5735:                         }
 5736:                         $datatable .= '</span> ';
 5737:                     }
 5738:                 }
 5739:                 $datatable .= '</fieldset>'."\n";
 5740:             }
 5741:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
 5742:                 if (@{$optuserfields{$provider}} > 0) {
 5743:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'optu'}.'</legend>'; 
 5744:                     foreach my $field (@{$optuserfields{$provider}}) {
 5745:                         my $checked;
 5746:                         if ($checkedfields{$field}) {
 5747:                             $checked = ' checked="checked"';
 5748:                         }
 5749:                         $datatable .= '<span class="LC_nobreak">'.
 5750:                                       '<label><input type="checkbox" name="proctoring_optional_'.$provider.'" value="'.$field.'"'.$checked.' />'.$lt{$field}.'</label></span>&nbsp; ';
 5751:                     }
 5752:                     $datatable .= '</fieldset>'."\n";
 5753:                 }
 5754:             }
 5755:             if (ref($defaults{$provider}) eq 'ARRAY') {
 5756:                 if (@{$defaults{$provider}}) {
 5757:                     my (%options,@selectboxes);
 5758:                     if (ref($extended{$provider}) eq 'HASH') {
 5759:                         %options = %{$extended{$provider}};
 5760:                     }
 5761:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'defa'}.'</legend>';
 5762:                     my ($rem,$numinrow,$dropdowns);
 5763:                     if ($provider eq 'proctorio') { 
 5764:                         $datatable .= '<table>';
 5765:                         $numinrow = 4;
 5766:                     }
 5767:                     my $i = 0;
 5768:                     foreach my $field (@{$defaults{$provider}}) {
 5769:                         my $checked;
 5770:                         if ($inuse{$field}) {
 5771:                             $checked = ' checked="checked"';
 5772:                         }
 5773:                         if ($provider eq 'examity') {
 5774:                             if ($field eq 'display') {
 5775:                                 $datatable .= '<span class="LC_nobreak">'.&mt('Display target:');
 5776:                                 foreach my $option ('iframe','tab','window') {
 5777:                                     my $checkdisp;
 5778:                                     if ($currentdef{'target'} eq $option) {
 5779:                                         $checkdisp = ' checked="checked"';
 5780:                                     }
 5781:                                     $datatable .= '<label><input type="radio" name="proctoring_target_'.$provider.'" value="'.$option.'"'.$checkdisp.' />'.
 5782:                                     $fieldtitles{$option}.'</label>'.('&nbsp;'x2);
 5783:                                 }
 5784:                                 $datatable .= ('&nbsp;'x4);
 5785:                                 foreach my $dimen ('width','height') {
 5786:                                     $datatable .= '<label>'.$fieldtitles{$dimen}.'&nbsp;'.
 5787:                                                   '<input type="text" name="proctoring_'.$dimen.'_'.$provider.'" size="5" '.
 5788:                                                   'value="'.$currentdef{$dimen}.'" /></label>'.
 5789:                                                   ('&nbsp;'x2);
 5790:                                 }
 5791:                                 $datatable .= '</span><br />'.
 5792:                                               '<div class="LC_left_float">'.$fieldtitles{'linktext'}.'<br />'.
 5793:                                               '<input type="text" name="proctoring_linktext_'.$provider.'" '.
 5794:                                               'size="25" value="'.$currentdef{'linktext'}.'" /></div>'.
 5795:                                               '<div class="LC_left_float">'.$fieldtitles{'explanation'}.'<br />'.
 5796:                                               '<textarea name="proctoring_explanation_'.$provider.'" rows="5" cols="40">'.
 5797:                                               $currentdef{'explanation'}.
 5798:                                               '</textarea></div><div style=""></div><br />';
 5799:                             }
 5800:                         } else {
 5801:                             if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
 5802:                                 my ($output,$selnone);
 5803:                                 unless ($checked) {
 5804:                                     $selnone = ' selected="selected"';
 5805:                                 }
 5806:                                 $output .= '<span class="LC_nobreak">'.$fieldtitles{$field}.': '.
 5807:                                            '<select name="proctoring_defaults_'.$field.'_'.$provider.'">'.
 5808:                                            '<option value=""'.$selnone.'>'.&mt('Not in use').'</option>'; 
 5809:                                 foreach my $option (@{$options{$field}}) {
 5810:                                     my $sel; 
 5811:                                     if ($inuse{$field} eq $option) {
 5812:                                         $sel = ' selected="selected"';
 5813:                                     }
 5814:                                     $output .= '<option value="'.$option.'"'.$sel.'>'.$fieldtitles{$option}.'</option>';
 5815:                                 }
 5816:                                 $output .= '</select></span>';
 5817:                                 push(@selectboxes,$output);
 5818:                             } else {
 5819:                                 $rem = $i%($numinrow);
 5820:                                 if ($rem == 0) {
 5821:                                     if ($i > 0) {
 5822:                                         $datatable .= '</tr>';
 5823:                                     }
 5824:                                     $datatable .= '<tr>';
 5825:                                 }
 5826:                                 $datatable .= '<td class="LC_left_item">'.
 5827:                                               '<span class="LC_nobreak">'.
 5828:                                               '<label><input type="checkbox" name="proctoring_defaults_'.$provider.'" value="'.$field.'"'.$checked.' />'.
 5829:                                               $fieldtitles{$field}.'</label></span></td>';
 5830:                                 $i++;
 5831:                             }
 5832:                         }
 5833:                     }
 5834:                     if ($provider eq 'proctorio') {
 5835:                         if ($numinrow) {
 5836:                             $rem = $i%$numinrow;
 5837:                         }
 5838:                         my $colsleft = $numinrow - $rem;
 5839:                         if ($colsleft > 1) {
 5840:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5841:                         } else {
 5842:                             $datatable .= '<td class="LC_left_item">';
 5843:                         }
 5844:                         $datatable .= '&nbsp;'.
 5845:                                       '</td></tr></table>';
 5846:                         if (@selectboxes) {
 5847:                             $datatable .= '<hr /><table>';
 5848:                             $numinrow = 2;
 5849:                             for (my $i=0; $i<@selectboxes; $i++) {
 5850:                                 $rem = $i%($numinrow);
 5851:                                 if ($rem == 0) {
 5852:                                     if ($i > 0) {
 5853:                                         $datatable .= '</tr>';
 5854:                                     }
 5855:                                     $datatable .= '<tr>';
 5856:                                 }
 5857:                                 $datatable .= '<td class="LC_left_item">'.
 5858:                                               $selectboxes[$i].'</td>';
 5859:                             }
 5860:                             if ($numinrow) {
 5861:                                 $rem = $i%$numinrow;
 5862:                             }
 5863:                             $colsleft = $numinrow - $rem;
 5864:                             if ($colsleft > 1) {
 5865:                                 $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5866:                             } else {
 5867:                                 $datatable .= '<td class="LC_left_item">';
 5868:                             }
 5869:                             $datatable .= '&nbsp;'.
 5870:                                           '</td></tr></table>';
 5871:                         }
 5872:                     }
 5873:                     $datatable .= '</fieldset>';
 5874:                 }
 5875:                 if (ref($crsconf{$provider}) eq 'ARRAY') {
 5876:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5877:                                   '<legend>'.&mt('Configurable in course').'</legend>';
 5878:                     my ($rem,$numinrow);
 5879:                     if ($provider eq 'proctorio') {
 5880:                         $datatable .= '<table>';
 5881:                         $numinrow = 4;
 5882:                     }
 5883:                     my $i = 0;
 5884:                     foreach my $item (@{$crsconf{$provider}}) {
 5885:                         my $name;
 5886:                         if ($provider eq 'examity') {
 5887:                             $name = $lt{'crs'.$item};
 5888:                         } elsif ($provider eq 'proctorio') {
 5889:                             $name = $fieldtitles{$item};
 5890:                             $rem = $i%($numinrow);
 5891:                             if ($rem == 0) {
 5892:                                 if ($i > 0) {
 5893:                                     $datatable .= '</tr>';
 5894:                                 }
 5895:                                 $datatable .= '<tr>';
 5896:                             }
 5897:                             $datatable .= '<td class="LC_left_item>';
 5898:                         }
 5899:                         my $checked;
 5900:                         if ($crsconfig{$item}) {
 5901:                             $checked = ' checked="checked"';
 5902:                         }
 5903:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5904:                                       '<input type="checkbox" name="proctoring_crsconf_'.$provider.'" value="'.$item.'"'.$checked.' />'.
 5905:                                       $name.'</label></span>';
 5906:                         if ($provider eq 'examity') {
 5907:                             $datatable .= '&nbsp; ';
 5908:                         }
 5909:                         $datatable .= "\n";
 5910:                         $i++;
 5911:                     }
 5912:                     if ($provider eq 'proctorio') {
 5913:                         if ($numinrow) {
 5914:                             $rem = $i%$numinrow;
 5915:                         }
 5916:                         my $colsleft = $numinrow - $rem;
 5917:                         if ($colsleft > 1) {
 5918:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5919:                         } else {
 5920:                             $datatable .= '<td class="LC_left_item">';
 5921:                         }
 5922:                         $datatable .= '&nbsp;'.
 5923:                                       '</td></tr></table>';
 5924:                     }
 5925:                     $datatable .= '</fieldset>';
 5926:                 }
 5927:                 if ($showroles) {
 5928:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5929:                                   '<legend>'.&mt('Role mapping').'</legend><table><tr>';
 5930:                     foreach my $role (@courseroles) {
 5931:                         my ($selected,$selectnone);
 5932:                         if (!$rolemaps{$role}) {
 5933:                             $selectnone = ' selected="selected"';
 5934:                         }
 5935:                         $datatable .= '<td style="text-align: center">'.
 5936:                                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5937:                                       '<select name="proctoring_roles_'.$role.'_'.$provider.'">'.
 5938:                                       '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5939:                         foreach my $ltirole (@ltiroles) {
 5940:                             unless ($selectnone) {
 5941:                                 if ($rolemaps{$role} eq $ltirole) {
 5942:                                     $selected = ' selected="selected"';
 5943:                                 } else {
 5944:                                     $selected = '';
 5945:                                 }
 5946:                             }
 5947:                             $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 5948:                         }
 5949:                         $datatable .= '</select></td>';
 5950:                     }
 5951:                     $datatable .= '</tr></table></fieldset>'.
 5952:                                   '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5953:                                   '<legend>'.&mt('Custom items sent on launch').'</legend>'.
 5954:                                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 5955:                                   '<tr><td></td><td>lms</td>'.
 5956:                                   '<td><input type="text" name="proctoring_customval_lms_'.$provider.'"'.
 5957:                                   ' value="Loncapa" disabled="disabled"/></td></tr>';
 5958:                     if ((ref($settings) eq 'HASH') && (ref($settings->{$provider}) eq 'HASH') &&
 5959:                         (ref($settings->{$provider}->{'custom'}) eq 'HASH')) {
 5960:                         my %custom = %{$settings->{$provider}->{'custom'}};
 5961:                         if (keys(%custom) > 0) {
 5962:                             foreach my $key (sort(keys(%custom))) {
 5963:                                 next if ($key eq 'lms');
 5964:                                 $datatable .= '<tr><td><span class="LC_nobreak">'.
 5965:                                               '<label><input type="checkbox" name="proctoring_customdel_'.$provider.'" value="'.
 5966:                                               $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 5967:                                               '<td><input type="text" name="proctoring_customval_'.$key.'_'.$provider.'"'.
 5968:                                               ' value="'.$custom{$key}.'" /></td></tr>';
 5969:                             }
 5970:                         }
 5971:                     }
 5972:                     $datatable .= '<tr><td><span class="LC_nobreak">'.
 5973:                                   '<label><input type="checkbox" name="proctoring_customadd" value="'.$provider.'" />'.
 5974:                                   &mt('Add more').'</label></span></td><td><input type="text" name="proctoring_custom_name_'.$provider.'" />'.
 5975:                                   '</td><td><input type="text" name="proctoring_custom_value_'.$provider.'" /></td></tr>'.
 5976:                                   '</table></fieldset></td></tr>'."\n";
 5977:                 }
 5978:                 $datatable .= '</td></tr>';
 5979:             }
 5980:             $itemcount ++;
 5981:         }
 5982:     }
 5983:     return $datatable;
 5984: }
 5985: 
 5986: sub proctoring_data {
 5987:     my $requserfields = {
 5988:                       proctorio => ['user'],
 5989:                       examity   => ['roles','user'],
 5990:                      };
 5991:     my $optuserfields = {
 5992:                         proctorio => ['fullname'],
 5993:                         examity   => ['fullname','firstname','lastname','email'],
 5994:                      };
 5995:     my $defaults = {
 5996:                   proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 5997:                                 'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 5998:                                 'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 5999:                                 'closetabs','onescreen','print','downloads','cache','rightclick',
 6000:                                 'reentry','calculator','whiteboard'],
 6001:                   examity   => ['display'],
 6002:                 };
 6003:     my $extended = { 
 6004:                   proctorio => {
 6005:                                  verifyid => ['verifyidauto','verifyidlive'],
 6006:                                  fullscreen => ['fullscreenlenient','fullscreenmoderate','fullscreensever'],
 6007:                                  tabslinks => ['notabs','linksonly'],
 6008:                                  reentry => ['noreentry','agentreentry'],
 6009:                                  calculator => ['calculatorbasic','calculatorsci'],
 6010:                                },
 6011:                   examity => {
 6012:                                display => {
 6013:                                            target      => ['iframe','tab','window'],
 6014:                                            width       => '',
 6015:                                            height      => '',
 6016:                                            linktext    => '',
 6017:                                            explanation => '',
 6018:                                           },
 6019:                              },
 6020:                 };
 6021:     my $crsconf = {
 6022:                  proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6023:                                'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6024:                                'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6025:                                'closetabs','onescreen','print','downloads','cache','rightclick',
 6026:                                'reentry','calculator','whiteboard'],
 6027:                  examity => ['label','title','target','linktext','explanation','append'],
 6028:                };
 6029:     my $courseroles = ['cc','in','ta','ep','st'];
 6030:     my $ltiroles = ['Instructor','ContentDeveloper','TeachingAssistant','Learner'];
 6031:     return ($requserfields,$optuserfields,$defaults,$extended,$crsconf,$courseroles,$ltiroles);
 6032: }
 6033: 
 6034: sub proctoring_titles {
 6035:     my ($item) = @_;
 6036:     my (%common_lt,%custom_lt);
 6037:     %common_lt = &Apache::lonlocal::texthash (
 6038:                      'avai'      => 'Available?',
 6039:                      'base'      => 'Basic Settings',
 6040:                      'requ'      => 'User data required to be sent on launch',
 6041:                      'optu'      => 'User data optionally sent on launch',
 6042:                      'udsl'      => 'User data sent on launch',
 6043:                      'defa'      => 'Defaults for items configurable in course',
 6044:                      'sigmethod' => 'Signature Method',
 6045:                      'key'       => 'Key',
 6046:                      'lifetime'  => 'Nonce lifetime (s)',
 6047:                      'secret'    => 'Secret',
 6048:                      'icon'      => 'Icon',
 6049:                      'fullname'  => 'Full Name',
 6050:                      'visible'   => 'Visible input',
 6051:                      'username'  => 'username',
 6052:                      'user'      => 'User',
 6053:                  );
 6054:     if ($item eq 'proctorio') {
 6055:         %custom_lt = &Apache::lonlocal::texthash (
 6056:                          'version'        => 'OAuth version',
 6057:                          'url'            => 'API URL',
 6058:                          'uname:dom'      => 'username-domain',
 6059:         );
 6060:     } elsif ($item eq 'examity') {
 6061:         %custom_lt = &Apache::lonlocal::texthash (
 6062:                          'version'        => 'LTI Version',
 6063:                          'url'            => 'URL',
 6064:                          'uname:dom'      => 'username:domain',
 6065:                          'msgtype'        => 'Message Type',
 6066:                          'firstname'      => 'First Name',
 6067:                          'lastname'       => 'Last Name',
 6068:                          'email'          => 'E-mail',
 6069:                          'roles'          => 'Role',
 6070:                          'crstarget'      => 'Display target',
 6071:                          'crslabel'       => 'Course label',
 6072:                          'crstitle'       => 'Course title', 
 6073:                          'crslinktext'    => 'Link Text',
 6074:                          'crsexplanation' => 'Explanation',
 6075:                          'crsappend'      => 'Provider URL',
 6076:         );
 6077:     }
 6078:     my %lt = (%common_lt,%custom_lt);
 6079:     return %lt;
 6080: }
 6081: 
 6082: sub proctoring_fieldtitles {
 6083:     my ($item) = @_;
 6084:     if ($item eq 'proctorio') {
 6085:         return &Apache::lonlocal::texthash (
 6086:                   'recordvideo' => 'Record video',
 6087:                   'recordaudio' => 'Record audio',
 6088:                   'recordscreen' => 'Record screen',
 6089:                   'recordwebtraffic' => 'Record web traffic',
 6090:                   'recordroomstart' => 'Record room scan',
 6091:                   'verifyvideo' => 'Verify webcam',
 6092:                   'verifyaudio' => 'Verify microphone',
 6093:                   'verifydesktop' => 'Verify desktop recording',
 6094:                   'verifyid' => 'Photo ID verification',
 6095:                   'verifysignature' => 'Require signature',
 6096:                   'fullscreen' => 'Fullscreen',
 6097:                   'clipboard' => 'Disable copy/paste',
 6098:                   'tabslinks' => 'New tabs/windows',
 6099:                   'closetabs' => 'Close other tabs',
 6100:                   'onescreen' => 'Limit to single screen',
 6101:                   'print' => 'Disable Printing',
 6102:                   'downloads' => 'Disable Downloads',
 6103:                   'cache' => 'Empty cache after exam',
 6104:                   'rightclick' => 'Disable right click',
 6105:                   'reentry' => 'Re-entry to exam',
 6106:                   'calculator' => 'Onscreen calculator',
 6107:                   'whiteboard' => 'Onscreen whiteboard',
 6108:                   'verifyidauto' => 'Automated verification',
 6109:                   'verifyidlive' => 'Live agent verification',
 6110:                   'fullscreenlenient' => 'Forced, but can navigate away for up to 30s',
 6111:                   'fullscreenmoderate' => 'Forced, but can navigate away for up to 15s',
 6112:                   'fullscreensever' => 'Forced, navigation away ends exam',
 6113:                   'notabs' => 'Disaallowed',
 6114:                   'linksonly' => 'Allowed from links in exam',
 6115:                   'noreentry' => 'Disallowed',
 6116:                   'agentreentry' => 'Agent required for re-entry',
 6117:                   'calculatorbasic' => 'Basic',
 6118:                   'calculatorsci' => 'Scientific',
 6119:         );
 6120:     } elsif ($item eq 'examity') {
 6121:         return &Apache::lonlocal::texthash (
 6122:                 'target'         => 'Display target',   
 6123:                 'window'         => 'Window',
 6124:                 'tab'            => 'Tab',
 6125:                 'iframe'         => 'iFrame',
 6126:                 'height'         => 'Height (pixels)',
 6127:                 'width'          => 'Width (pixels)',
 6128:                 'linktext'       => 'Default Link Text',
 6129:                 'explanation'    => 'Default Explanation',
 6130:                 'append'         => 'Provider URL',
 6131:         );
 6132:     }
 6133: }
 6134: 
 6135: sub proctoring_providernames {
 6136:     return (
 6137:              proctorio => 'Proctorio',
 6138:              examity   => 'Examity',
 6139:            );
 6140: }
 6141: 
 6142: sub print_lti {
 6143:     my ($position,$dom,$settings,$rowtotal) = @_;
 6144:     my $itemcount = 1;
 6145:     my ($datatable,$css_class);
 6146:     my (%rules,%encrypt,%privkeys,%linkprot);
 6147:     if (ref($settings) eq 'HASH') {
 6148:         if ($position eq 'top') {
 6149:             if (exists($settings->{'encrypt'})) {
 6150:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 6151:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 6152:                         if ($key eq 'consumers') {
 6153:                             $encrypt{'ltisec_'.$key} = $settings->{'encrypt'}{$key};
 6154:                         } else {
 6155:                             $encrypt{'ltisec_'.$key.'linkprot'} = $settings->{'encrypt'}{$key};
 6156:                         }
 6157:                     }
 6158:                 }
 6159:             }
 6160:             if (exists($settings->{'private'})) {
 6161:                 if (ref($settings->{'private'}) eq 'HASH') {
 6162:                     if (ref($settings->{'private'}) eq 'HASH') {
 6163:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 6164:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 6165:                         }
 6166:                     }
 6167:                 }
 6168:             }
 6169:         } elsif ($position eq 'middle') {
 6170:             if (exists($settings->{'rules'})) {
 6171:                 if (ref($settings->{'rules'}) eq 'HASH') {
 6172:                     %rules = %{$settings->{'rules'}};
 6173:                 }
 6174:             }
 6175:         } elsif ($position eq 'lower') {
 6176:             if (exists($settings->{'linkprot'})) {
 6177:                 if (ref($settings->{'linkprot'}) eq 'HASH') {
 6178:                     %linkprot = %{$settings->{'linkprot'}};
 6179:                     if ($linkprot{'lock'}) {
 6180:                         delete($linkprot{'lock'});
 6181:                     }
 6182:                 }
 6183:             }
 6184:         } else {
 6185:             foreach my $key ('encrypt','private','rules','linkprot') {
 6186:                 if (exists($settings->{$key})) {
 6187:                     delete($settings->{$key});
 6188:                 }
 6189:             }
 6190:         }
 6191:     }
 6192:     if ($position eq 'top') {
 6193:         $datatable = &secrets_form($dom,'ltisec',\%encrypt,\%privkeys,$rowtotal);
 6194:     } elsif ($position eq 'middle') {
 6195:         $datatable = &password_rules('ltisecrets',\$itemcount,\%rules);
 6196:         $$rowtotal += $itemcount;
 6197:     } elsif ($position eq 'lower') {
 6198:          $datatable .= &Apache::courseprefs::print_linkprotection($dom,'',$settings,$rowtotal,'','','domain');
 6199:     } else {
 6200:         my ($switchserver,$switchmessage);
 6201:         $switchserver = &check_switchserver($dom);
 6202:         $switchmessage = &mt("submit from domain's primary library server: [_1].",$switchserver);
 6203:         my $maxnum = 0;
 6204:         my %ordered;
 6205:         if (ref($settings) eq 'HASH') {
 6206:             foreach my $item (keys(%{$settings})) {
 6207:                 if (ref($settings->{$item}) eq 'HASH') {
 6208:                     my $num = $settings->{$item}{'order'};
 6209:                     if ($num eq '') {
 6210:                         $num = scalar(keys(%{$settings}));
 6211:                     }
 6212:                     $ordered{$num} = $item;
 6213:                 }
 6214:             }
 6215:         }
 6216:         $maxnum = scalar(keys(%ordered));
 6217:         my %lt = &lti_names();
 6218:         if (keys(%ordered)) {
 6219:             my @items = sort { $a <=> $b } keys(%ordered);
 6220:             for (my $i=0; $i<@items; $i++) {
 6221:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6222:                 my $item = $ordered{$items[$i]};
 6223:                 my ($key,$secret,$usable,$lifetime,$consumer,$requser,$crsinc,$current);
 6224:                 if (ref($settings->{$item}) eq 'HASH') {
 6225:                     $key = $settings->{$item}->{'key'};
 6226:                     $usable = $settings->{$item}->{'usable'};
 6227:                     $lifetime = $settings->{$item}->{'lifetime'};
 6228:                     $consumer = $settings->{$item}->{'consumer'};
 6229:                     $requser = $settings->{$item}->{'requser'};
 6230:                     $crsinc = $settings->{$item}->{'crsinc'};
 6231:                     $current = $settings->{$item};
 6232:                 }
 6233:                 my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 6234:                 my %checkedrequser = (
 6235:                                        yes => ' checked="checked"',
 6236:                                        no  => '',
 6237:                                      );
 6238:                 if (!$requser) {
 6239:                     $checkedrequser{'no'} = $checkedrequser{'yes'};
 6240:                     $checkedrequser{'yes'} = '';
 6241:                 }
 6242:                 my $onclickcrsinc = ' onclick="toggleLTI(this.form,'."'crsinc','$i'".');"';
 6243:                 my %checkedcrsinc = (
 6244:                                       yes => ' checked="checked"',
 6245:                                       no  => '',
 6246:                                     );
 6247:                 if (!$crsinc) {
 6248:                     $checkedcrsinc{'no'} = $checkedcrsinc{'yes'};
 6249:                     $checkedcrsinc{'yes'} = '';
 6250:                 }
 6251:                 my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 6252:                 $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6253:                              .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 6254:                 for (my $k=0; $k<=$maxnum; $k++) {
 6255:                     my $vpos = $k+1;
 6256:                     my $selstr;
 6257:                     if ($k == $i) {
 6258:                         $selstr = ' selected="selected" ';
 6259:                     }
 6260:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6261:                 }
 6262:                 $datatable .= '</select>'.('&nbsp;'x2).
 6263:                     '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 6264:                     &mt('Delete?').'</label></span></td>'.
 6265:                     '<td colspan="2">'.
 6266:                     '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6267:                     '<span class="LC_nobreak">'.$lt{'consumer'}.
 6268:                     ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 6269:                     ('&nbsp;'x2).
 6270:                     '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 6271:                     '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 6272:                     ('&nbsp;'x2).
 6273:                     '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 6274:                     'value="'.$lifetime.'" size="3" /></span><br /><br />';
 6275:                 if ($key ne '') {
 6276:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'};
 6277:                     if ($switchserver) {
 6278:                         $datatable .= ': ['.&mt('[_1] to view/edit',$switchserver).']';
 6279:                     } else {
 6280:                         $datatable .= ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />';
 6281:                     }
 6282:                     $datatable .= '</span> '.('&nbsp;'x2);
 6283:                 } elsif (!$switchserver) {
 6284:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':'.
 6285:                                   '<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />'.
 6286:                                   '</span> '.('&nbsp;'x2);
 6287:                 }
 6288:                 if ($switchserver) {
 6289:                     if ($usable ne '') {
 6290:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 6291:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 6292:                                       '<span class="LC_nobreak">'.&mt('Change secret?').
 6293:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 6294:                                       ('&nbsp;'x2).
 6295:                                      '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').'</label>'.('&nbsp;'x2).
 6296:                                       '</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 6297:                                       '<span class="LC_nobreak"> - '.$switchmessage.'</span>'.
 6298:                                       '</div>';
 6299:                     } elsif ($key eq '') {
 6300:                         $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 6301:                     } else {
 6302:                         $datatable .= '<span class="LC_nobreak">'.&mt('Secret required').' - '.$switchmessage.'</span>'."\n";
 6303:                     }
 6304:                 } else {
 6305:                     if ($usable ne '') {
 6306:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 6307:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 6308:                                       '<span class="LC_nobreak">'.&mt('Change?').
 6309:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 6310:                                       ('&nbsp;'x2).
 6311:                                       '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').
 6312:                                       '</label>&nbsp;&nbsp;</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 6313:                                       '<span class="LC_nobreak">'.&mt('New Secret').':'.
 6314:                                       '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 6315:                                       '<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>';
 6316:                     } else {
 6317:                         $datatable .=
 6318:                             '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 6319:                             '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 6320:                             '<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>';
 6321:                     }
 6322:                 }
 6323:                 $datatable .= '<br /><br />'.
 6324:                     '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6325:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6326:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 6327:                     '<br /><br />'.
 6328:                     '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6329:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="1"'.$onclickcrsinc.$checkedcrsinc{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6330:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="0"'.$onclickcrsinc.$checkedcrsinc{no}.' />'.&mt('No').'</label></span>'."\n".
 6331:                     ('&nbsp;'x4).
 6332:                     '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 6333:                     '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 6334:                 $itemcount ++;
 6335:             }
 6336:         }
 6337:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6338:         my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 6339:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6340:                       '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 6341:                       '<select name="lti_pos_add"'.$chgstr.'>';
 6342:         for (my $k=0; $k<$maxnum+1; $k++) {
 6343:             my $vpos = $k+1;
 6344:             my $selstr;
 6345:             if ($k == $maxnum) {
 6346:                 $selstr = ' selected="selected" ';
 6347:             }
 6348:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6349:         }
 6350:         $datatable .= '</select>&nbsp;'."\n".
 6351:                       '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6352:                       '<td colspan="2">'.
 6353:                       '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6354:                       '<span class="LC_nobreak">'.$lt{'consumer'}.
 6355:                       ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 6356:                       ('&nbsp;'x2).
 6357:                       '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 6358:                       '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 6359:                       ('&nbsp;'x2).
 6360:                       '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span><br /><br />'."\n";
 6361:         if ($switchserver) {
 6362:             $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 6363:         } else {
 6364:             $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" autocomplete="off" /></span> '."\n".
 6365:                           ('&nbsp;'x2).
 6366:                           '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" autocomplete="new-password" />'.
 6367:                           '<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";
 6368:         }
 6369:         $datatable .= '<br /><br />'.
 6370:                       '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6371:                       '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6372:                       '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 6373:                       '<br /><br />'.
 6374:                       '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6375:                       '<label><input type="radio" name="lti_crsinc_add" value="1" onclick="toggleLTI(this.form,'."'crsinc','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6376:                       '<label><input type="radio" name="lti_crsinc_add" value="0" onclick="toggleLTI(this.form,'."'crsinc','add'".');" />'.&mt('No').'</label></span>'."\n".
 6377:                       '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 6378:                       '</td>'."\n".
 6379:                       '</tr>'."\n";
 6380:         $itemcount ++;
 6381:     }
 6382:     $$rowtotal += $itemcount;
 6383:     return $datatable;
 6384: }
 6385: 
 6386: sub lti_names {
 6387:     my %lt = &Apache::lonlocal::texthash(
 6388:                                           'version'   => 'LTI Version',
 6389:                                           'url'       => 'URL',
 6390:                                           'key'       => 'Key',
 6391:                                           'lifetime'  => 'Nonce lifetime (s)',
 6392:                                           'consumer'  => 'Consumer',
 6393:                                           'secret'    => 'Secret',
 6394:                                           'requser'   => "User's identity sent",
 6395:                                           'crsinc'    => "Course's identity sent",
 6396:                                           'email'     => 'Email address',
 6397:                                           'sourcedid' => 'User ID',
 6398:                                           'other'     => 'Other',
 6399:                                           'passback'  => 'Can return grades to Consumer:',
 6400:                                           'roster'    => 'Can retrieve roster from Consumer:',
 6401:                                           'topmenu'   => 'Display LON-CAPA page header',
 6402:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 6403:                                         );
 6404:     return %lt;
 6405: }
 6406: 
 6407: sub lti_options {
 6408:     my ($num,$current,$itemcount,%lt) = @_;
 6409:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 6410:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 6411:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 6412:     $checked{'storecrs'}{'Y'} = ' checked="checked"';
 6413:     $checked{'makecrs'}{'N'} = ' checked="checked"';
 6414:     $checked{'mapcrstype'} = {};
 6415:     $checked{'makeuser'} = {};
 6416:     $checked{'selfenroll'} = {};
 6417:     $checked{'crssec'} = {};
 6418:     $checked{'crssecsrc'} = {};
 6419:     $checked{'lcauth'} = {};
 6420:     $checked{'menuitem'} = {};
 6421:     if ($num eq 'add') {
 6422:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 6423:     }
 6424:     my $userfieldsty = 'none';
 6425:     my $crsfieldsty = 'none';
 6426:     my $crssecfieldsty = 'none';
 6427:     my $secsrcfieldsty = 'none';
 6428:     my $callbacksty = 'none';
 6429:     my $passbacksty = 'none';
 6430:     my $optionsty = 'block';
 6431:     my $crssty = 'block';
 6432:     my $lcauthparm;
 6433:     my $lcauthparmstyle = 'display:none';
 6434:     my $lcauthparmtext;
 6435:     my $menusty;
 6436:     my $numinrow = 4;
 6437:     my %menutitles = &ltimenu_titles();
 6438: 
 6439:     if (ref($current) eq 'HASH') {
 6440:         if (!$current->{'requser'}) {
 6441:             $optionsty = 'none';
 6442:             $crssty = 'none';
 6443:         } elsif (!$current->{'crsinc'}) {
 6444:             $crssty = 'none';
 6445:         }
 6446:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 6447:             $checked{'mapuser'}{'sourcedid'} = '';
 6448:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 6449:                 $checked{'mapuser'}{'email'} = ' checked="checked"';
 6450:             } else {
 6451:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 6452:                 $userfield = $current->{'mapuser'};
 6453:                 $userfieldsty = 'inline-block';
 6454:             }
 6455:         }
 6456:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 6457:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 6458:             if ($current->{'mapcrs'} eq 'context_id') {
 6459:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"';
 6460:             } else {
 6461:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 6462:                 $cidfield = $current->{'mapcrs'};
 6463:                 $crsfieldsty = 'inline-block';
 6464:             }
 6465:         }
 6466:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 6467:             foreach my $type (@{$current->{'mapcrstype'}}) {
 6468:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 6469:             }
 6470:         }
 6471:         if (!$current->{'storecrs'}) {
 6472:             $checked{'storecrs'}{'N'} = $checked{'storecrs'}{'Y'};
 6473:             $checked{'storecrs'}{'Y'} = '';
 6474:         }
 6475:         if ($current->{'makecrs'}) {
 6476:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 6477:         }
 6478:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 6479:             foreach my $role (@{$current->{'makeuser'}}) {
 6480:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 6481:             }
 6482:         }
 6483:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 6484:             $checked{'lcauth'}{$1} = ' checked="checked"';
 6485:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 6486:                 $lcauthparm = $current->{'lcauthparm'};
 6487:                 $lcauthparmstyle = 'display:table-row';
 6488:                 if ($current->{'lcauth'} eq 'localauth') {
 6489:                     $lcauthparmtext = &mt('Local auth argument');
 6490:                 } else {
 6491:                     $lcauthparmtext = &mt('Kerberos domain');
 6492:                 }
 6493:             }
 6494:         }
 6495:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 6496:             foreach my $role (@{$current->{'selfenroll'}}) {
 6497:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 6498:             }
 6499:         }
 6500:         if (ref($current->{'maproles'}) eq 'HASH') {
 6501:             %rolemaps = %{$current->{'maproles'}};
 6502:         }
 6503:         if ($current->{'section'} ne '') {
 6504:             $checked{'crssec'}{'Y'} = '  checked="checked"';
 6505:             $crssecfieldsty = 'inline-block';
 6506:             if ($current->{'section'} eq 'course_section_sourcedid') {
 6507:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 6508:             } else {
 6509:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 6510:                 $crssecsrc = $current->{'section'};
 6511:                 $secsrcfieldsty = 'inline-block';
 6512:             }
 6513:         } else {
 6514:             $checked{'crssec'}{'N'} = ' checked="checked"';
 6515:         }
 6516:         if ($current->{'callback'} ne '') {
 6517:             $callback = $current->{'callback'};
 6518:             $checked{'callback'}{'Y'} = ' checked="checked"';
 6519:             $callbacksty = 'inline-block';
 6520:         } else {
 6521:             $checked{'callback'}{'N'} = ' checked="checked"';
 6522:         }
 6523:         if ($current->{'topmenu'}) {
 6524:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 6525:         } else {
 6526:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 6527:         }
 6528:         if ($current->{'inlinemenu'}) {
 6529:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6530:         } else {
 6531:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 6532:         }
 6533:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 6534:             $menusty = 'inline-block';
 6535:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 6536:                 foreach my $item (@{$current->{'lcmenu'}}) {
 6537:                     if (exists($menutitles{$item})) {
 6538:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 6539:                     }
 6540:                 }
 6541:             }
 6542:         } else {
 6543:             $menusty = 'none';
 6544:         }
 6545:     } else {
 6546:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 6547:         $checked{'crssec'}{'N'} = ' checked="checked"';
 6548:         $checked{'callback'}{'N'} = ' checked="checked"';
 6549:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 6550:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6551:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 6552:         $menusty = 'inline-block';
 6553:     }
 6554:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 6555:     my %coursetypetitles = &Apache::lonlocal::texthash (
 6556:                                official   => 'Official',
 6557:                                unofficial => 'Unofficial',
 6558:                                community  => 'Community',
 6559:                                textbook   => 'Textbook',
 6560:                                placement  => 'Placement Test',
 6561:                                lti        => 'LTI Provider',
 6562:     );
 6563:     my @authtypes = ('internal','krb4','krb5','localauth');
 6564:     my %shortauth = (
 6565:                      internal => 'int',
 6566:                      krb4 => 'krb4',
 6567:                      krb5 => 'krb5',
 6568:                      localauth  => 'loc'
 6569:                     );
 6570:     my %authnames = &authtype_names();
 6571:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 6572:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 6573:     my @courseroles = ('cc','in','ta','ep','st');
 6574:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 6575:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 6576:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 6577:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 6578:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 6579:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 6580:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 6581:     my $output = '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Logout options').'</legend>'.
 6582:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback to logout LON-CAPA on log out from Consumer').':&nbsp;'.
 6583:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 6584:                  $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6585:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 6586:                  $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 6587:                  '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 6588:                  '<span class="LC_nobreak">'.&mt('Parameter').': '.
 6589:                  '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 6590:                  '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 6591:                  '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 6592:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 6593:     foreach my $option ('sourcedid','email','other') {
 6594:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 6595:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 6596:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6597:     }
 6598:     $output .= '</span></div>'.
 6599:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 6600:                '<input type="text" name="lti_customuser_'.$num.'" '.
 6601:                'value="'.$userfield.'" /></div></fieldset>'.
 6602:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 6603:     foreach my $ltirole (@ltiroles) {
 6604:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 6605:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';
 6606:     }
 6607:     $output .= '</fieldset>'.
 6608:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 6609:                '<table>'.
 6610:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 6611:                '</table>'.
 6612:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 6613:                '<td class="LC_left_item">';
 6614:     foreach my $auth ('lti',@authtypes) {
 6615:         my $authtext;
 6616:         if ($auth eq 'lti') {
 6617:             $authtext = &mt('None');
 6618:         } else {
 6619:             $authtext = $authnames{$shortauth{$auth}};
 6620:         }
 6621:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 6622:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 6623:                    $authtext.'</label></span> &nbsp;';
 6624:     }
 6625:     $output .= '</td></tr>'.
 6626:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 6627:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 6628:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 6629:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 6630:                '</table></fieldset>'.
 6631:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.
 6632:                &mt('LON-CAPA menu items (Course Coordinator can override)').'</legend>'.
 6633:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 6634:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 6635:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6636:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 6637:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 6638:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6639:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 6640:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 6641:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6642:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 6643:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 6644:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6645:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 6646:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 6647:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 6648:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 6649:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 6650:                    ('&nbsp;'x2);
 6651:     }
 6652:     $output .= '</span></div></fieldset>'.
 6653:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 6654:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 6655:                &mt('Unique course identifier').':&nbsp;';
 6656:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 6657:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 6658:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 6659:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6660:     }
 6661:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 6662:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 6663:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6664:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 6665:     foreach my $type (@coursetypes) {
 6666:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 6667:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 6668:                    ('&nbsp;'x2);
 6669:     }
 6670:     $output .= '</span><br /><br />'.
 6671:                '<span class="LC_nobreak">'.&mt('Store mapping of course identifier to LON-CAPA CourseID').':&nbsp;'.
 6672:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="0"'.
 6673:                $checked{'storecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6674:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="1"'.
 6675:                $checked{'storecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6676:                '</fieldset>'.
 6677:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 6678:     foreach my $ltirole (@lticourseroles) {
 6679:         my ($selected,$selectnone);
 6680:         if ($rolemaps{$ltirole} eq '') {
 6681:             $selectnone = ' selected="selected"';
 6682:         }
 6683:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 6684:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 6685:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 6686:         foreach my $role (@courseroles) {
 6687:             unless ($selectnone) {
 6688:                 if ($rolemaps{$ltirole} eq $role) {
 6689:                     $selected = ' selected="selected"';
 6690:                 } else {
 6691:                     $selected = '';
 6692:                 }
 6693:             }
 6694:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 6695:                        &Apache::lonnet::plaintext($role,'Course').
 6696:                        '</option>';
 6697:         }
 6698:         $output .= '</select></td>';
 6699:     }
 6700:     $output .= '</tr></table></fieldset>'.
 6701:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Creating courses').'</legend>'.
 6702:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 6703:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 6704:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6705:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 6706:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6707:                '</fieldset>'.
 6708:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 6709:     foreach my $lticrsrole (@lticourseroles) {
 6710:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 6711:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 6712:     }
 6713:     $output .= '</fieldset>'.
 6714:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Course options').'</legend>'.
 6715:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 6716:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 6717:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6718:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 6719:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 6720:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 6721:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 6722:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 6723:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 6724:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 6725:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 6726:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 6727:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 6728:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 6729:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 6730:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 6731:     foreach my $extra ('roster','passback') {
 6732:         my $checkedon = '';
 6733:         my $checkedoff = ' checked="checked"';
 6734:         if ($extra eq 'passback') {
 6735:             $pb1p1chk = ' checked="checked"';
 6736:             $pb1p0chk = '';
 6737:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"';
 6738:         } else {
 6739:             $onclickpb = '';
 6740:         }
 6741:         if (ref($current) eq 'HASH') {
 6742:             if (($current->{$extra})) {
 6743:                 $checkedon = $checkedoff;
 6744:                 $checkedoff = '';
 6745:                 if ($extra eq 'passback') {
 6746:                     $passbacksty = 'inline-block';
 6747:                 }
 6748:                 if ($current->{'passbackformat'} eq '1.0') {
 6749:                     $pb1p0chk =  ' checked="checked"';
 6750:                     $pb1p1chk = '';
 6751:                 }
 6752:             }
 6753:         }
 6754:         $output .= $lt{$extra}.'&nbsp;'.
 6755:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 6756:                    &mt('No').'</label>'.('&nbsp;'x2).
 6757:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 6758:                    &mt('Yes').'</label><br />';
 6759:     }
 6760:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 6761:                '<span class="LC_nobreak">'.&mt('Grade format').
 6762:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 6763:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 6764:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 6765:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 6766:                '<div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>';
 6767:     $output .= '</span></div></fieldset>';
 6768: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 6769: #
 6770: #    $output .= '</fieldset>'.
 6771: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 6772:     return $output;
 6773: }
 6774: 
 6775: sub ltimenu_titles {
 6776:     return &Apache::lonlocal::texthash(
 6777:                                         fullname    => 'Full name',
 6778:                                         coursetitle => 'Course title',
 6779:                                         role        => 'Role',
 6780:                                         logout      => 'Logout',
 6781:                                         grades      => 'Grades',
 6782:     );
 6783: }
 6784: 
 6785: sub print_coursedefaults {
 6786:     my ($position,$dom,$settings,$rowtotal) = @_;
 6787:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6788:     my $itemcount = 1;
 6789:     my %choices =  &Apache::lonlocal::texthash (
 6790:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 6791:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6792:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6793:         coursecredits        => 'Credits can be specified for courses',
 6794:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6795:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6796:         inline_chem          => 'Use inline previewer for chemical reaction response in place of pop-up',
 6797:         texengine            => 'Default method to display mathematics',
 6798:         postsubmit           => 'Disable submit button/keypress following student submission',
 6799:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6800:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6801:         ltiauth              => 'Student username in LTI launch of deep-linked URL can be accepted without re-authentication',
 6802:         domexttool           => 'External Tools defined in the domain may be used in courses/communities (by type)',
 6803:         exttool              => 'External Tools can be defined and configured in courses/communities (by type)',
 6804:     );
 6805:     my %staticdefaults = (
 6806:                            anonsurvey_threshold => 10,
 6807:                            uploadquota          => 500,
 6808:                            postsubmit           => 60,
 6809:                            mysqltables          => 172800,
 6810:                            domexttool           => 1,
 6811:                            exttool              => 0,
 6812:                          );
 6813:     if ($position eq 'top') {
 6814:         %defaultchecked = (
 6815:                             'canuse_pdfforms' => 'off',
 6816:                             'uselcmath'       => 'on',
 6817:                             'usejsme'         => 'on',
 6818:                             'inline_chem'     => 'on',
 6819:                             'canclone'        => 'none',
 6820:                           );
 6821:         @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem');
 6822:         my $deftex = $Apache::lonnet::deftex;
 6823:         if (ref($settings) eq 'HASH') {
 6824:             if ($settings->{'texengine'}) {
 6825:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6826:                     $deftex = $settings->{'texengine'};
 6827:                 }
 6828:             }
 6829:         }
 6830:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6831:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6832:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6833:                        '</span></td><td class="LC_right_item">'.
 6834:                        '<select name="texengine">'."\n";
 6835:         my %texoptions = (
 6836:                             MathJax  => 'MathJax',
 6837:                             mimetex  => &mt('Convert to Images'),
 6838:                             tth      => &mt('TeX to HTML'),
 6839:                          );
 6840:         foreach my $renderer ('MathJax','mimetex','tth') {
 6841:             my $selected = '';
 6842:             if ($renderer eq $deftex) {
 6843:                 $selected = ' selected="selected"';
 6844:             }
 6845:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6846:         }
 6847:         $mathdisp .= '</select></td></tr>'."\n";
 6848:         $itemcount ++;
 6849:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6850:                                                      \%choices,$itemcount);
 6851:         $datatable = $mathdisp.$datatable;
 6852:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6853:         $datatable .=
 6854:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 6855:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6856:             '</span></td><td class="LC_left_item">';
 6857:         my $currcanclone = 'none';
 6858:         my $onclick;
 6859:         my @cloneoptions = ('none','domain');
 6860:         my %clonetitles = &Apache::lonlocal::texthash (
 6861:                              none     => 'No additional course requesters',
 6862:                              domain   => "Any course requester in course's domain",
 6863:                              instcode => 'Course requests for official courses ...',
 6864:                           );
 6865:         my (%codedefaults,@code_order,@posscodes);
 6866:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6867:                                                     \@code_order) eq 'ok') {
 6868:             if (@code_order > 0) {
 6869:                 push(@cloneoptions,'instcode');
 6870:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6871:             }
 6872:         }
 6873:         if (ref($settings) eq 'HASH') {
 6874:             if ($settings->{'canclone'}) {
 6875:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6876:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6877:                         if (@code_order > 0) {
 6878:                             $currcanclone = 'instcode';
 6879:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 6880:                         }
 6881:                     }
 6882:                 } elsif ($settings->{'canclone'} eq 'domain') {
 6883:                     $currcanclone = $settings->{'canclone'};
 6884:                 }
 6885:             }
 6886:         }
 6887:         foreach my $option (@cloneoptions) {
 6888:             my ($checked,$additional);
 6889:             if ($currcanclone eq $option) {
 6890:                 $checked = ' checked="checked"';
 6891:             }
 6892:             if ($option eq 'instcode') {
 6893:                 if (@code_order) {
 6894:                     my $show = 'none';
 6895:                     if ($checked) {
 6896:                         $show = 'block';
 6897:                     }
 6898:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 6899:                                   &mt('Institutional codes for new and cloned course have identical:').
 6900:                                   '<br />';
 6901:                     foreach my $item (@code_order) {
 6902:                         my $codechk;
 6903:                         if ($checked) {
 6904:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 6905:                                 $codechk = ' checked="checked"';
 6906:                             }
 6907:                         }
 6908:                         $additional .= '<label>'.
 6909:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 6910:                                        $item.'</label>';
 6911:                     }
 6912:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 6913:                 }
 6914:             }
 6915:             $datatable .=
 6916:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 6917:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 6918:                 '</label>&nbsp;'.$additional.'</span><br />';
 6919:         }
 6920:         $datatable .= '</td>'.
 6921:                       '</tr>';
 6922:         $itemcount ++;
 6923:     } else {
 6924:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6925:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 6926:         my $currusecredits = 0;
 6927:         my $postsubmitclient = 1;
 6928:         my $ltiauth = 0;
 6929:         my %domexttool;
 6930:         my %exttool;
 6931:         my @types = ('official','unofficial','community','textbook','placement');
 6932:         if (ref($settings) eq 'HASH') {
 6933:             if ($settings->{'ltiauth'}) {
 6934:                 $ltiauth = 1;
 6935:             }
 6936:             if (ref($settings->{'domexttool'}) eq 'HASH') {
 6937:                 foreach my $type (@types) {
 6938:                     if ($settings->{'domexttool'}->{$type}) {
 6939:                         $domexttool{$type} = ' checked="checked"';
 6940:                     }
 6941:                 }
 6942:             } else {
 6943:                 foreach my $type (@types) {
 6944:                     if ($staticdefaults{'domexttool'}) {
 6945:                         $domexttool{$type} = ' checked="checked"';
 6946:                     }
 6947:                 }
 6948:             }
 6949:             if (ref($settings->{'exttool'}) eq 'HASH') {
 6950:                 foreach my $type (@types) {
 6951:                     if ($settings->{'exttool'}->{$type}) {
 6952:                         $exttool{$type} = ' checked="checked"';
 6953:                     }
 6954:                 }
 6955:             }
 6956:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 6957:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 6958:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 6959:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 6960:                 }
 6961:             }
 6962:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 6963:                 foreach my $type (@types) {
 6964:                     next if ($type eq 'community');
 6965:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 6966:                     if ($defcredits{$type} ne '') {
 6967:                         $currusecredits = 1;
 6968:                     }
 6969:                 }
 6970:             }
 6971:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 6972:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 6973:                     $postsubmitclient = 0;
 6974:                     foreach my $type (@types) {
 6975:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6976:                     }
 6977:                 } else {
 6978:                     foreach my $type (@types) {
 6979:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 6980:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 6981:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 6982:                             } else {
 6983:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6984:                             }
 6985:                         } else {
 6986:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6987:                         }
 6988:                     }
 6989:                 }
 6990:             } else {
 6991:                 foreach my $type (@types) {
 6992:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6993:                 }
 6994:             }
 6995:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 6996:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 6997:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 6998:                 }
 6999:             } else {
 7000:                 foreach my $type (@types) {
 7001:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 7002:                 }
 7003:             }
 7004:         } else {
 7005:             foreach my $type (@types) {
 7006:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7007:                 if ($staticdefaults{'domexttool'}) {
 7008:                     $domexttool{$type} = ' checked="checked"';
 7009:                 }
 7010:             }
 7011:         }
 7012:         if (!$currdefresponder) {
 7013:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 7014:         } elsif ($currdefresponder < 1) {
 7015:             $currdefresponder = 1;
 7016:         }
 7017:         foreach my $type (@types) {
 7018:             if ($curruploadquota{$type} eq '') {
 7019:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 7020:             }
 7021:         }
 7022:         $datatable .=
 7023:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7024:                 $choices{'anonsurvey_threshold'}.
 7025:                 '</span></td>'.
 7026:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 7027:                 '<input type="text" name="anonsurvey_threshold"'.
 7028:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 7029:                 '</td></tr>'."\n";
 7030:         $itemcount ++;
 7031:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7032:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7033:                       $choices{'uploadquota'}.
 7034:                       '</span></td>'.
 7035:                       '<td style="text-align: right" class="LC_right_item">'.
 7036:                       '<table><tr>';
 7037:         foreach my $type (@types) {
 7038:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7039:                            '<input type="text" name="uploadquota_'.$type.'"'.
 7040:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 7041:         }
 7042:         $datatable .= '</tr></table></td></tr>'."\n";
 7043:         $itemcount ++;
 7044:         my $onclick = "toggleDisplay(this.form,'credits');";
 7045:         my $display = 'none';
 7046:         if ($currusecredits) {
 7047:             $display = 'block';
 7048:         }
 7049:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 7050:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 7051:         foreach my $type (@types) {
 7052:             next if ($type eq 'community');
 7053:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7054:                            '<input type="text" name="'.$type.'_credits"'.
 7055:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 7056:         }
 7057:         $additional .= '</tr></table></div>'."\n";
 7058:         %defaultchecked = ('coursecredits' => 'off');
 7059:         @toggles = ('coursecredits');
 7060:         my $current = {
 7061:                         'coursecredits' => $currusecredits,
 7062:                       };
 7063:         (my $table,$itemcount) =
 7064:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7065:                                \%choices,$itemcount,$onclick,$additional,'left');
 7066:         $datatable .= $table;
 7067:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 7068:         my $display = 'none';
 7069:         if ($postsubmitclient) {
 7070:             $display = 'block';
 7071:         }
 7072:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 7073:                       &mt('Number of seconds submit is disabled').'<br />'.
 7074:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 7075:                       '<table><tr>';
 7076:         foreach my $type (@types) {
 7077:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7078:                            '<input type="text" name="'.$type.'_timeout" value="'.
 7079:                            $deftimeout{$type}.'" size="5" /></td>';
 7080:         }
 7081:         $additional .= '</tr></table></div>'."\n";
 7082:         %defaultchecked = ('postsubmit' => 'on');
 7083:         @toggles = ('postsubmit');
 7084:         $current = {
 7085:                        'postsubmit' => $postsubmitclient,
 7086:                    };
 7087:         ($table,$itemcount) =
 7088:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7089:                                \%choices,$itemcount,$onclick,$additional,'left');
 7090:         $datatable .= $table;
 7091:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7092:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7093:                       $choices{'mysqltables'}.
 7094:                       '</span></td>'.
 7095:                       '<td style="text-align: right" class="LC_right_item">'.
 7096:                       '<table><tr>';
 7097:         foreach my $type (@types) {
 7098:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7099:                            '<input type="text" name="mysqltables_'.$type.'"'.
 7100:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 7101:         }
 7102:         $datatable .= '</tr></table></td></tr>'."\n";
 7103:         $itemcount ++;
 7104:         %defaultchecked = ('ltiauth' => 'off');
 7105:         @toggles = ('ltiauth');
 7106:         $current = {
 7107:                        'ltiauth' => $ltiauth,
 7108:                    };
 7109:         ($table,$itemcount) =
 7110:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7111:                                \%choices,$itemcount,undef,undef,'left');
 7112:         $datatable .= $table;
 7113:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7114:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7115:                       $choices{'domexttool'}.
 7116:                       '</span></td>'.
 7117:                       '<td style="text-align: right" class="LC_right_item">'.
 7118:                       '<table><tr>';
 7119:         foreach my $type (@types) {
 7120:             $datatable .= '<td style="text-align: left">'.
 7121:                           '<span class="LC_nobreak">'.
 7122:                           '<input type="checkbox" name="domexttool"'.
 7123:                           ' value="'.$type.'"'.$domexttool{$type}.' />'.
 7124:                           &mt($type).'</span></td>'."\n";
 7125:         }
 7126:         $datatable .= '</tr></table></td></tr>'."\n";
 7127:         $itemcount ++;
 7128:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7129:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7130:                       $choices{'exttool'}.
 7131:                       '</span></td>'.
 7132:                       '<td style="text-align: right" class="LC_right_item">'.
 7133:                       '<table><tr>';
 7134:         foreach my $type (@types) {
 7135:             $datatable .= '<td style="text-align: left">'.
 7136:                           '<span class="LC_nobreak">'.
 7137:                           '<input type="checkbox" name="exttool"'.
 7138:                           ' value="'.$type.'"'.$exttool{$type}.' />'.
 7139:                           &mt($type).'</span></td>'."\n";
 7140:         }
 7141:         $datatable .= '</tr></table></td></tr>'."\n";
 7142:     }
 7143:     $$rowtotal += $itemcount;
 7144:     return $datatable;
 7145: }
 7146: 
 7147: sub print_selfenrollment {
 7148:     my ($position,$dom,$settings,$rowtotal) = @_;
 7149:     my ($css_class,$datatable);
 7150:     my $itemcount = 1;
 7151:     my @types = ('official','unofficial','community','textbook','placement');
 7152:     if (($position eq 'top') || ($position eq 'middle')) {
 7153:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 7154:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 7155:         my @rows;
 7156:         my $key;
 7157:         if ($position eq 'top') {
 7158:             $key = 'admin'; 
 7159:             if (ref($rowsref) eq 'ARRAY') {
 7160:                 @rows = @{$rowsref};
 7161:             }
 7162:         } elsif ($position eq 'middle') {
 7163:             $key = 'default';
 7164:             @rows = ('types','registered','approval','limit');
 7165:         }
 7166:         foreach my $row (@rows) {
 7167:             if (defined($titlesref->{$row})) {
 7168:                 $itemcount ++;
 7169:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7170:                 $datatable .= '<tr'.$css_class.'>'.
 7171:                               '<td>'.$titlesref->{$row}.'</td>'.
 7172:                               '<td class="LC_left_item">'.
 7173:                               '<table><tr>';
 7174:                 my (%current,%currentcap);
 7175:                 if (ref($settings) eq 'HASH') {
 7176:                     if (ref($settings->{$key}) eq 'HASH') {
 7177:                         foreach my $type (@types) {
 7178:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7179:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 7180:                             }
 7181:                             if (($row eq 'limit') && ($key eq 'default')) {
 7182:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7183:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 7184:                                 }
 7185:                             }
 7186:                         }
 7187:                     }
 7188:                 }
 7189:                 my %roles = (
 7190:                              '0' => &Apache::lonnet::plaintext('dc'),
 7191:                             ); 
 7192:             
 7193:                 foreach my $type (@types) {
 7194:                     unless (($row eq 'registered') && ($key eq 'default')) {
 7195:                         $datatable .= '<th>'.&mt($type).'</th>';
 7196:                     }
 7197:                 }
 7198:                 unless (($row eq 'registered') && ($key eq 'default')) {
 7199:                     $datatable .= '</tr><tr>';
 7200:                 }
 7201:                 foreach my $type (@types) {
 7202:                     if ($type eq 'community') {
 7203:                         $roles{'1'} = &mt('Community personnel');
 7204:                     } else {
 7205:                         $roles{'1'} = &mt('Course personnel');
 7206:                     }
 7207:                     $datatable .= '<td style="vertical-align: top">';
 7208:                     if ($position eq 'top') {
 7209:                         my %checked;
 7210:                         if ($current{$type} eq '0') {
 7211:                             $checked{'0'} = ' checked="checked"';
 7212:                         } else {
 7213:                             $checked{'1'} = ' checked="checked"';
 7214:                         }
 7215:                         foreach my $role ('1','0') {
 7216:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7217:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 7218:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 7219:                                           $roles{$role}.'</label></span> ';
 7220:                         }
 7221:                     } else {
 7222:                         if ($row eq 'types') {
 7223:                             my %checked;
 7224:                             if ($current{$type} =~ /^(all|dom)$/) {
 7225:                                 $checked{$1} = ' checked="checked"';
 7226:                             } else {
 7227:                                 $checked{''} = ' checked="checked"';
 7228:                             }
 7229:                             foreach my $val ('','dom','all') {
 7230:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7231:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7232:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7233:                             }
 7234:                         } elsif ($row eq 'registered') {
 7235:                             my %checked;
 7236:                             if ($current{$type} eq '1') {
 7237:                                 $checked{'1'} = ' checked="checked"';
 7238:                             } else {
 7239:                                 $checked{'0'} = ' checked="checked"';
 7240:                             }
 7241:                             foreach my $val ('0','1') {
 7242:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7243:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7244:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7245:                             }
 7246:                         } elsif ($row eq 'approval') {
 7247:                             my %checked;
 7248:                             if ($current{$type} =~ /^([12])$/) {
 7249:                                 $checked{$1} = ' checked="checked"';
 7250:                             } else {
 7251:                                 $checked{'0'} = ' checked="checked"';
 7252:                             }
 7253:                             for my $val (0..2) {
 7254:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7255:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7256:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7257:                             }
 7258:                         } elsif ($row eq 'limit') {
 7259:                             my %checked;
 7260:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 7261:                                 $checked{$1} = ' checked="checked"';
 7262:                             } else {
 7263:                                 $checked{'none'} = ' checked="checked"';
 7264:                             }
 7265:                             my $cap;
 7266:                             if ($currentcap{$type} =~ /^\d+$/) {
 7267:                                 $cap = $currentcap{$type};
 7268:                             }
 7269:                             foreach my $val ('none','allstudents','selfenrolled') {
 7270:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7271:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7272:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7273:                             }
 7274:                             $datatable .= '<br />'.
 7275:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 7276:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 7277:                                           '</span>'; 
 7278:                         }
 7279:                     }
 7280:                     $datatable .= '</td>';
 7281:                 }
 7282:                 $datatable .= '</tr>';
 7283:             }
 7284:             $datatable .= '</table></td></tr>';
 7285:         }
 7286:     } elsif ($position eq 'bottom') {
 7287:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 7288:     }
 7289:     $$rowtotal += $itemcount;
 7290:     return $datatable;
 7291: }
 7292: 
 7293: sub print_validation_rows {
 7294:     my ($caller,$dom,$settings,$rowtotal) = @_;
 7295:     my ($itemsref,$namesref,$fieldsref);
 7296:     if ($caller eq 'selfenroll') { 
 7297:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 7298:     } elsif ($caller eq 'requestcourses') {
 7299:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7300:     }
 7301:     my %currvalidation;
 7302:     if (ref($settings) eq 'HASH') {
 7303:         if (ref($settings->{'validation'}) eq 'HASH') {
 7304:             %currvalidation = %{$settings->{'validation'}};
 7305:         }
 7306:     }
 7307:     my $datatable;
 7308:     my $itemcount = 0;
 7309:     foreach my $item (@{$itemsref}) {
 7310:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7311:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7312:                       $namesref->{$item}.
 7313:                       '</span></td>'.
 7314:                       '<td class="LC_left_item">';
 7315:         if (($item eq 'url') || ($item eq 'button')) {
 7316:             $datatable .= '<span class="LC_nobreak">'.
 7317:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 7318:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 7319:         } elsif ($item eq 'fields') {
 7320:             my @currfields;
 7321:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 7322:                 @currfields = @{$currvalidation{$item}};
 7323:             }
 7324:             foreach my $field (@{$fieldsref}) {
 7325:                 my $check = '';
 7326:                 if (grep(/^\Q$field\E$/,@currfields)) {
 7327:                     $check = ' checked="checked"';
 7328:                 }
 7329:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7330:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 7331:                               ' value="'.$field.'"'.$check.' />'.$field.
 7332:                               '</label></span> ';
 7333:             }
 7334:         } elsif ($item eq 'markup') {
 7335:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 7336:                            $currvalidation{$item}.
 7337:                               '</textarea>';
 7338:         }
 7339:         $datatable .= '</td></tr>'."\n";
 7340:         if (ref($rowtotal)) {
 7341:             $itemcount ++;
 7342:         }
 7343:     }
 7344:     if ($caller eq 'requestcourses') {
 7345:         my %currhash;
 7346:         if (ref($settings) eq 'HASH') {
 7347:             if (ref($settings->{'validation'}) eq 'HASH') {
 7348:                 if ($settings->{'validation'}{'dc'} ne '') {
 7349:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 7350:                 }
 7351:             }
 7352:         }
 7353:         my $numinrow = 2;
 7354:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 7355:                                                        'validationdc',%currhash);
 7356:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7357:         $datatable .= '<tr'.$css_class.'><td>';
 7358:         if ($numdc > 1) {
 7359:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 7360:         } else {
 7361:             $datatable .=  &mt('Course creation processed as: ');
 7362:         }
 7363:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 7364:         $itemcount ++;
 7365:     }
 7366:     if (ref($rowtotal)) {
 7367:         $$rowtotal += $itemcount;
 7368:     }
 7369:     return $datatable;
 7370: }
 7371: 
 7372: sub print_privacy {
 7373:     my ($position,$dom,$settings,$rowtotal) = @_;
 7374:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 7375:     my $itemcount = 0;
 7376:     if ($position eq 'top') {
 7377:         $numinrow = 2;
 7378:     } else {
 7379:         @items = ('domain','author','course','community');
 7380:         %names = &Apache::lonlocal::texthash (
 7381:                      domain => 'Assigned domain role(s)',
 7382:                      author => 'Assigned co-author role(s)',
 7383:                      course => 'Assigned course role(s)',
 7384:                      community => 'Assigned community role(s)',
 7385:                  );
 7386:         $numinrow = 4;
 7387:         ($othertitle,$usertypes,$types) =
 7388:             &Apache::loncommon::sorted_inst_types($dom);
 7389:     }
 7390:     if (($position eq 'top') || ($position eq 'middle')) {
 7391:         my (%by_ip,%by_location,@intdoms,@instdoms);
 7392:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7393:         if ($position eq 'top') {
 7394:             my %curr;
 7395:             my @options = ('none','user','domain','auto');
 7396:             my %titles = &Apache::lonlocal::texthash (
 7397:                 none   => 'Not allowed',
 7398:                 user   => 'User authorizes',
 7399:                 domain => 'DC authorizes',
 7400:                 auto   => 'Unrestricted',
 7401:                 instdom => 'Other domain shares institution/provider',
 7402:                 extdom => 'Other domain has different institution/provider',
 7403:                 notify => 'Notify when role needs authorization',
 7404:             );
 7405:             my %names = &Apache::lonlocal::texthash (
 7406:                 domain => 'Domain role',
 7407:                 author => 'Co-author role',
 7408:                 course => 'Course role',
 7409:                 community => 'Community role',
 7410:             );
 7411:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7412:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7413:             foreach my $domtype ('instdom','extdom') {
 7414:                 my (%checked,$skip);
 7415:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7416:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 7417:                               '<td class="LC_left_item">';
 7418:                 if ($domtype eq 'instdom') {
 7419:                     unless (@instdoms > 1) {
 7420:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 7421:                         $skip = 1;
 7422:                     }
 7423:                 } elsif ($domtype eq 'extdom') {
 7424:                     if (keys(%by_location) == 0) {
 7425:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 7426:                         $skip = 1;
 7427:                     }
 7428:                 }
 7429:                 unless ($skip) {
 7430:                     foreach my $roletype ('domain','author','course','community') {
 7431:                         $checked{'auto'} = ' checked="checked"';
 7432:                         if (ref($settings) eq 'HASH') {
 7433:                             if (ref($settings->{approval}) eq 'HASH') {
 7434:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 7435:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 7436:                                         $checked{$1} = ' checked="checked"';
 7437:                                         $checked{'auto'} = '';
 7438:                                     }
 7439:                                 }
 7440:                             }
 7441:                         }
 7442:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 7443:                         foreach my $option (@options) {
 7444:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7445:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 7446:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 7447:                                           '</label></span>&nbsp; ';
 7448:                         }
 7449:                         $datatable .= '</fieldset>';
 7450:                     }
 7451:                 }
 7452:                 $datatable .= '</td></tr>';
 7453:                 $itemcount ++;
 7454:             }
 7455:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7456:             $datatable .= '<tr'.$css_class.'><td>'.$titles{'notify'}.'</td>'.
 7457:                           '<td class="LC_left_item">';
 7458:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7459:                 my %curr;
 7460:                 if (ref($settings) eq 'HASH') {
 7461:                     if ($settings->{'notify'} ne '') {
 7462:                         map {$curr{$_}=1;} split(/,/,$settings->{'notify'});
 7463:                     }
 7464:                 }
 7465:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7466:                 my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 7467:                                                              'privacy_notify',%curr);
 7468:                 if ($numdc > 0) {
 7469:                     $datatable .= $table;
 7470:                 } else {
 7471:                     $datatable .= &mt('There are no active Domain Coordinators');
 7472:                 }
 7473:             } else {
 7474:                 $datatable .= &mt('Nothing to set here, as there are no other domains');
 7475:             }
 7476:             $datatable .='</td></tr>';
 7477:         } elsif ($position eq 'middle') {
 7478:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7479:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7480:                     foreach my $item (@{$types}) {
 7481:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 7482:                                                                $numinrow,$itemcount,'','','','','',
 7483:                                                                '',$usertypes->{$item});
 7484:                         $itemcount ++;
 7485:                     }
 7486:                 }
 7487:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 7488:                                                        $numinrow,$itemcount,'','','','','',
 7489:                                                        '',$othertitle);
 7490:                 $itemcount ++;
 7491:             } else {
 7492:                 my (@insttypes,%insttitles);
 7493:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7494:                     @insttypes = @{$types};
 7495:                     %insttitles = %{$usertypes};
 7496:                 }
 7497:                 foreach my $item (@insttypes,'default') {
 7498:                     my $title;
 7499:                     if ($item eq 'default') {
 7500:                         $title = $othertitle;
 7501:                     } else {
 7502:                         $title = $insttitles{$item};
 7503:                     }
 7504:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7505:                     $datatable .= '<tr'.$css_class.'>'.
 7506:                                   '<td class="LC_left_item">'.$title.'</td>'.
 7507:                                   '<td class="LC_left_item">'.
 7508:                                   &mt('Nothing to set here, as there are no other domains').
 7509:                                   '</td></tr>';
 7510:                     $itemcount ++;
 7511:                 }
 7512:             }
 7513:         }
 7514:     } else {
 7515:         my $prefix;
 7516:         if ($position eq 'lower') {
 7517:             $prefix = 'priv';
 7518:         } else {
 7519:             $prefix = 'unpriv';
 7520:         }
 7521:         foreach my $item (@items) {
 7522:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 7523:                                                    $numinrow,$itemcount,'','','','','',
 7524:                                                    '',$names{$item});
 7525:             $itemcount ++;
 7526:         }
 7527:     }
 7528:     if (ref($rowtotal)) {
 7529:         $$rowtotal += $itemcount;
 7530:     }
 7531:     return $datatable;
 7532: }
 7533: 
 7534: sub print_passwords {
 7535:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 7536:     my ($datatable,$css_class);
 7537:     my $itemcount = 0;
 7538:     my %titles = &Apache::lonlocal::texthash (
 7539:         captcha        => '"Forgot Password" CAPTCHA validation',
 7540:         link           => 'Reset link expiration (hours)',
 7541:         case           => 'Case-sensitive usernames/e-mail',
 7542:         prelink        => 'Information required (form 1)',
 7543:         postlink       => 'Information required (form 2)',
 7544:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 7545:         customtext     => 'Domain specific text (HTML)',
 7546:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 7547:         intauth_check  => 'Check bcrypt cost if authenticated',
 7548:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 7549:         permanent      => 'Permanent e-mail address',
 7550:         critical       => 'Critical notification address',
 7551:         notify         => 'Notification address',
 7552:         min            => 'Minimum password length',
 7553:         max            => 'Maximum password length',
 7554:         chars          => 'Required characters',
 7555:         expire         => 'Password expiration (days)',
 7556:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 7557:     );
 7558:     if ($position eq 'top') {
 7559:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7560:         my $shownlinklife = 2;
 7561:         my $prelink = 'both';
 7562:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 7563:         if (ref($settings) eq 'HASH') {
 7564:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 7565:                 $shownlinklife = $settings->{resetlink};
 7566:             }
 7567:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 7568:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 7569:             }
 7570:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 7571:                 $prelink = $settings->{resetprelink};
 7572:             }
 7573:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 7574:                 %postlink = %{$settings->{resetpostlink}};
 7575:             }
 7576:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 7577:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 7578:             }
 7579:             if ($settings->{resetremove}) {
 7580:                 $nostdtext = 1;
 7581:             }
 7582:             if ($settings->{resetcustom}) {
 7583:                 $customurl = $settings->{resetcustom};
 7584:             }
 7585:         } else {
 7586:             if (ref($types) eq 'ARRAY') {
 7587:                 foreach my $item (@{$types}) {
 7588:                     $casesens{$item} = 1;
 7589:                     $postlink{$item} = ['username','email'];
 7590:                 }
 7591:             }
 7592:             $casesens{'default'} = 1;
 7593:             $postlink{'default'} = ['username','email'];
 7594:             $prelink = 'both';
 7595:             %emailsrc = (
 7596:                           permanent => 1,
 7597:                           critical  => 1,
 7598:                           notify    => 1,
 7599:             );
 7600:         }
 7601:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 7602:         $itemcount ++;
 7603:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7604:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 7605:                       '<td class="LC_left_item">'.
 7606:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 7607:                       'name="passwords_link" size="3" /></td></tr>';
 7608:         $itemcount ++;
 7609:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7610:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 7611:                       '<td class="LC_left_item">';
 7612:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7613:             foreach my $item (@{$types}) {
 7614:                 my $checkedcase;
 7615:                 if ($casesens{$item}) {
 7616:                     $checkedcase = ' checked="checked"';
 7617:                 }
 7618:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7619:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 7620:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 7621:                               '</span>&nbsp;&nbsp; ';
 7622:             }
 7623:         }
 7624:         my $checkedcase;
 7625:         if ($casesens{'default'}) {
 7626:             $checkedcase = ' checked="checked"';
 7627:         }
 7628:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7629:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 7630:                       $othertitle.'</label></span></td>';
 7631:         $itemcount ++;
 7632:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7633:         my %checkedpre = (
 7634:                              both => ' checked="checked"',
 7635:                              either => '',
 7636:                          );
 7637:         if ($prelink eq 'either') {
 7638:             $checkedpre{either} = ' checked="checked"';
 7639:             $checkedpre{both} = '';
 7640:         }
 7641:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 7642:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7643:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 7644:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 7645:                       '<span class="LC_nobreak"><label>'.
 7646:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 7647:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 7648:         $itemcount ++;
 7649:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7650:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 7651:                       '<td class="LC_left_item">';
 7652:         my %postlinked;
 7653:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7654:             foreach my $item (@{$types}) {
 7655:                 undef(%postlinked);
 7656:                 $datatable .= '<fieldset style="display: inline-block;">'.
 7657:                               '<legend>'.$usertypes->{$item}.'</legend>';
 7658:                 if (ref($postlink{$item}) eq 'ARRAY') {
 7659:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 7660:                 }
 7661:                 foreach my $field ('email','username') {
 7662:                     my $checked;
 7663:                     if ($postlinked{$field}) {
 7664:                         $checked = ' checked="checked"';
 7665:                     }
 7666:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7667:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 7668:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 7669:                                   '<span>&nbsp;&nbsp; ';
 7670:                 }
 7671:                 $datatable .= '</fieldset>';
 7672:             }
 7673:         }
 7674:         if (ref($postlink{'default'}) eq 'ARRAY') {
 7675:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 7676:         }
 7677:         $datatable .= '<fieldset style="display: inline-block;">'.
 7678:                       '<legend>'.$othertitle.'</legend>';
 7679:         foreach my $field ('email','username') {
 7680:             my $checked;
 7681:             if ($postlinked{$field}) {
 7682:                 $checked = ' checked="checked"';
 7683:             }
 7684:             $datatable .= '<span class="LC_nobreak"><label>'.
 7685:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 7686:                           $field.'"'.$checked.' />'.$field.'</label>'.
 7687:                           '<span>&nbsp;&nbsp; ';
 7688:         }
 7689:         $datatable .= '</fieldset></td></tr>';
 7690:         $itemcount ++;
 7691:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7692:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 7693:                       '<td class="LC_left_item">';
 7694:         foreach my $type ('permanent','critical','notify') {
 7695:             my $checkedemail;
 7696:             if ($emailsrc{$type}) {
 7697:                 $checkedemail = ' checked="checked"';
 7698:             }
 7699:             $datatable .= '<span class="LC_nobreak"><label>'.
 7700:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 7701:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 7702:                           '<span>&nbsp;&nbsp; ';
 7703:         }
 7704:         $datatable .= '</td></tr>';
 7705:         $itemcount ++;
 7706:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7707:         my $switchserver = &check_switchserver($dom,$confname);
 7708:         my ($showstd,$noshowstd);
 7709:         if ($nostdtext) {
 7710:             $noshowstd = ' checked="checked"';
 7711:         } else {
 7712:             $showstd = ' checked="checked"';
 7713:         }
 7714:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 7715:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7716:                       &mt('Retain standard text:').
 7717:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 7718:                       &mt('Yes').'</label>'.'&nbsp;'.
 7719:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 7720:                       &mt('No').'</label></span><br />'.
 7721:                       '<span class="LC_fontsize_small">'.
 7722:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 7723:                       &mt('Include custom text:');
 7724:         if ($customurl) {
 7725:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 7726:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7727:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 7728:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 7729:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 7730:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 7731:         }
 7732:         if ($switchserver) {
 7733:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 7734:         } else {
 7735:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 7736:                          '<input type="file" name="passwords_customfile" /></span>';
 7737:         }
 7738:         $datatable .= '</td></tr>';
 7739:     } elsif ($position eq 'middle') {
 7740:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 7741:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 7742:         my %defaults;
 7743:         if (ref($domconf{'defaults'}) eq 'HASH') {
 7744:             %defaults = %{$domconf{'defaults'}};
 7745:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 7746:                 $defaults{'intauth_cost'} = 10;
 7747:             }
 7748:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 7749:                 $defaults{'intauth_check'} = 0;
 7750:             }
 7751:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 7752:                 $defaults{'intauth_switch'} = 0;
 7753:             }
 7754:         } else {
 7755:             %defaults = (
 7756:                           'intauth_cost'   => 10,
 7757:                           'intauth_check'  => 0,
 7758:                           'intauth_switch' => 0,
 7759:                         );
 7760:         }
 7761:         foreach my $item (@items) {
 7762:             if ($itemcount%2) {
 7763:                 $css_class = '';
 7764:             } else {
 7765:                 $css_class = ' class="LC_odd_row" ';
 7766:             }
 7767:             $datatable .= '<tr'.$css_class.'>'.
 7768:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 7769:                           '</span></td><td class="LC_left_item" colspan="3">';
 7770:             if ($item eq 'intauth_switch') {
 7771:                 my @options = (0,1,2);
 7772:                 my %optiondesc = &Apache::lonlocal::texthash (
 7773:                                    0 => 'No',
 7774:                                    1 => 'Yes',
 7775:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 7776:                                  );
 7777:                 $datatable .= '<table width="100%">';
 7778:                 foreach my $option (@options) {
 7779:                     my $checked = ' ';
 7780:                     if ($defaults{$item} eq $option) {
 7781:                         $checked = ' checked="checked"';
 7782:                     }
 7783:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7784:                                   '<label><input type="radio" name="'.$item.
 7785:                                   '" value="'.$option.'"'.$checked.' />'.
 7786:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7787:                 }
 7788:                 $datatable .= '</table>';
 7789:             } elsif ($item eq 'intauth_check') {
 7790:                 my @options = (0,1,2);
 7791:                 my %optiondesc = &Apache::lonlocal::texthash (
 7792:                                    0 => 'No',
 7793:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 7794:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 7795:                                  );
 7796:                 $datatable .= '<table width="100%">';
 7797:                 foreach my $option (@options) {
 7798:                     my $checked = ' ';
 7799:                     my $onclick;
 7800:                     if ($defaults{$item} eq $option) {
 7801:                         $checked = ' checked="checked"';
 7802:                     }
 7803:                     if ($option == 2) {
 7804:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 7805:                     }
 7806:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7807:                                   '<label><input type="radio" name="'.$item.
 7808:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 7809:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7810:                 }
 7811:                 $datatable .= '</table>';
 7812:             } else {
 7813:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7814:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 7815:             }
 7816:             $datatable .= '</td></tr>';
 7817:             $itemcount ++;
 7818:         }
 7819:     } elsif ($position eq 'lower') {
 7820:         $datatable .= &password_rules('passwords',\$itemcount,$settings);
 7821:     } else {
 7822:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7823:         my %ownerchg = (
 7824:                           by  => {},
 7825:                           for => {},
 7826:                        );
 7827:         my %ownertitles = &Apache::lonlocal::texthash (
 7828:                             by  => 'Course owner status(es) allowed',
 7829:                             for => 'Student status(es) allowed',
 7830:                           );
 7831:         if (ref($settings) eq 'HASH') {
 7832:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 7833:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 7834:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 7835:                 }
 7836:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 7837:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 7838:                 }
 7839:             }
 7840:         }
 7841:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7842:         $datatable .= '<tr '.$css_class.'>'.
 7843:                       '<td>'.
 7844:                       &mt('Requirements').'<ul>'.
 7845:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 7846:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 7847:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 7848:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 7849:                       '</ul>'.
 7850:                       '</td>'.
 7851:                       '<td class="LC_left_item">';
 7852:         foreach my $item ('by','for') {
 7853:             $datatable .= '<fieldset style="display: inline-block;">'.
 7854:                           '<legend>'.$ownertitles{$item}.'</legend>';
 7855:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7856:                 foreach my $type (@{$types}) {
 7857:                     my $checked;
 7858:                     if ($ownerchg{$item}{$type}) {
 7859:                         $checked = ' checked="checked"';
 7860:                     }
 7861:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7862:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 7863:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 7864:                                   '</span>&nbsp;&nbsp; ';
 7865:                 }
 7866:             }
 7867:             my $checked;
 7868:             if ($ownerchg{$item}{'default'}) {
 7869:                 $checked = ' checked="checked"';
 7870:             }
 7871:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7872:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 7873:                           $othertitle.'</label></span></fieldset>';
 7874:         }
 7875:         $datatable .= '</td></tr>';
 7876:     }
 7877:     return $datatable;
 7878: }
 7879: 
 7880: sub password_rules {
 7881:     my ($prefix,$itemcountref,$settings) = @_;
 7882:     my ($min,$max,%chars,$expire,$numsaved,$numinrow);
 7883:     my %titles;
 7884:     if ($prefix eq 'passwords') {
 7885:         %titles = &Apache::lonlocal::texthash (
 7886:             min            => 'Minimum password length',
 7887:             max            => 'Maximum password length',
 7888:             chars          => 'Required characters',
 7889:         );
 7890:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
 7891:         %titles = &Apache::lonlocal::texthash (
 7892:             min            => 'Minimum secret length',
 7893:             max            => 'Maximum secret length',
 7894:             chars          => 'Required characters',
 7895:         );
 7896:     }
 7897:     $min = $Apache::lonnet::passwdmin;
 7898:     my $datatable;
 7899:     my $itemcount;
 7900:     if (ref($itemcountref)) {
 7901:         $itemcount = $$itemcountref;
 7902:     }
 7903:     if (ref($settings) eq 'HASH') {
 7904:         if ($settings->{min}) {
 7905:             $min = $settings->{min};
 7906:         }
 7907:         if ($settings->{max}) {
 7908:             $max = $settings->{max};
 7909:         }
 7910:         if (ref($settings->{chars}) eq 'ARRAY') {
 7911:             map { $chars{$_} = 1; } (@{$settings->{chars}});
 7912:         }
 7913:         if ($prefix eq 'passwords') {
 7914:             if ($settings->{expire}) {
 7915:                 $expire = $settings->{expire};
 7916:             }
 7917:             if ($settings->{numsaved}) {
 7918:                 $numsaved = $settings->{numsaved};
 7919:             }
 7920:         }
 7921:     }
 7922:     my %rulenames = &Apache::lonlocal::texthash(
 7923:                                                  uc => 'At least one upper case letter',
 7924:                                                  lc => 'At least one lower case letter',
 7925:                                                  num => 'At least one number',
 7926:                                                  spec => 'At least one non-alphanumeric',
 7927:                                                );
 7928:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7929:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 7930:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 7931:                   '<input type="text" name="'.$prefix.'_min" value="'.$min.'" size="3" '.
 7932:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 7933:                   '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 7934:                   '</span></td></tr>';
 7935:     $itemcount ++;
 7936:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7937:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 7938:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 7939:                   '<input type="text" name="'.$prefix.'_max" value="'.$max.'" size="3" '.
 7940:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 7941:                   '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 7942:                   '</span></td></tr>';
 7943:     $itemcount ++;
 7944:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7945:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 7946:                   '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 7947:                   '</span></td>';
 7948:     my $numinrow = 2;
 7949:     my @possrules = ('uc','lc','num','spec');
 7950:     $datatable .= '<td class="LC_left_item"><table>';
 7951:     for (my $i=0; $i<@possrules; $i++) {
 7952:         my ($rem,$checked);
 7953:         if ($chars{$possrules[$i]}) {
 7954:             $checked = ' checked="checked"';
 7955:         }
 7956:         $rem = $i%($numinrow);
 7957:         if ($rem == 0) {
 7958:             if ($i > 0) {
 7959:                 $datatable .= '</tr>';
 7960:             }
 7961:             $datatable .= '<tr>';
 7962:         }
 7963:         $datatable .= '<td><span class="LC_nobreak"><label>'.
 7964:                       '<input type="checkbox" name="'.$prefix.'_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 7965:                       $rulenames{$possrules[$i]}.'</label></span></td>';
 7966:     }
 7967:     my $rem = @possrules%($numinrow);
 7968:     my $colsleft = $numinrow - $rem;
 7969:     if ($colsleft > 1 ) {
 7970:         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7971:                       '&nbsp;</td>';
 7972:     } elsif ($colsleft == 1) {
 7973:         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7974:     }
 7975:     $datatable .='</table></td></tr>';
 7976:     $itemcount ++;
 7977:     if ($prefix eq 'passwords') {
 7978:         $titles{'expire'} = &mt('Password expiration (days)');
 7979:         $titles{'numsaved'} = &mt('Number of previous passwords to save and disallow reuse');
 7980:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7981:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 7982:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7983:                       '<input type="text" name="'.$prefix.'_expire" value="'.$expire.'" size="4" '.
 7984:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 7985:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 7986:                       '</span></td></tr>';
 7987:         $itemcount ++;
 7988:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7989:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 7990:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7991:                       '<input type="text" name="'.$prefix.'_numsaved" value="'.$numsaved.'" size="3" '.
 7992:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 7993:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 7994:                       '</span></td></tr>';
 7995:         $itemcount ++;
 7996:     }
 7997:     if (ref($itemcountref)) {
 7998:         $$itemcountref += $itemcount;
 7999:     }
 8000:     return $datatable;
 8001: }
 8002: 
 8003: sub print_wafproxy {
 8004:     my ($position,$dom,$settings,$rowtotal) = @_;
 8005:     my $css_class;
 8006:     my $itemcount = 0;
 8007:     my $datatable;
 8008:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8009:     my (%othercontrol,%otherdoms,%aliases,%saml,%values,$setdom,$showdom);
 8010:     my %lt = &wafproxy_titles();
 8011:     foreach my $server (sort(keys(%servers))) {
 8012:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8013:         next if ($serverhome eq '');
 8014:         my $serverdom;
 8015:         if ($serverhome ne $server) {
 8016:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 8017:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 8018:                 $othercontrol{$server} = $serverdom;
 8019:             }
 8020:         } else {
 8021:             $serverdom = &Apache::lonnet::host_domain($server);
 8022:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 8023:             if ($serverdom ne $dom) {
 8024:                 $othercontrol{$server} = $serverdom;
 8025:             } else {
 8026:                 $setdom = 1;
 8027:                 if (ref($settings) eq 'HASH') {
 8028:                     if (ref($settings->{'alias'}) eq 'HASH') {
 8029:                         $aliases{$dom} = $settings->{'alias'};
 8030:                         if ($aliases{$dom} ne '') {
 8031:                             $showdom = 1;
 8032:                         }
 8033:                     }
 8034:                     if (ref($settings->{'saml'}) eq 'HASH') {
 8035:                         $saml{$dom} = $settings->{'saml'};
 8036:                     }
 8037:                 }
 8038:             }
 8039:         }
 8040:     }
 8041:     if ($setdom) {
 8042:         %{$values{$dom}} = ();
 8043:         if (ref($settings) eq 'HASH') {
 8044:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 8045:                 $values{$dom}{$item} = $settings->{$item};
 8046:             }
 8047:         }
 8048:     }
 8049:     if (keys(%othercontrol)) {
 8050:         %otherdoms = reverse(%othercontrol);
 8051:         foreach my $domain (keys(%otherdoms)) {
 8052:             %{$values{$domain}} = ();
 8053:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 8054:             if (ref($config{'wafproxy'}) eq 'HASH') {
 8055:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 8056:                 if (exists($config{'wafproxy'}{'saml'})) {
 8057:                     $saml{$domain} = $config{'wafproxy'}{'saml'};
 8058:                 }
 8059:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 8060:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 8061:                 }
 8062:             }
 8063:         }
 8064:     }
 8065:     if ($position eq 'top') {
 8066:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8067:         my %aliasinfo;
 8068:         foreach my $server (sort(keys(%servers))) {
 8069:             $itemcount ++;
 8070:             my $dom_in_effect;
 8071:             my $aliasrows = '<tr>'.
 8072:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8073:                             &mt('Hostname').':&nbsp;'.
 8074:                             '<i>'.&Apache::lonnet::hostname($server).'</i></td><td>&nbsp;</td>';
 8075:             if ($othercontrol{$server}) {
 8076:                 $dom_in_effect = $othercontrol{$server};
 8077:                 my ($current,$forsaml);
 8078:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 8079:                     $current = $aliases{$dom_in_effect}{$server};
 8080:                 }
 8081:                 if (ref($saml{$dom_in_effect}) eq 'HASH') {
 8082:                     if ($saml{$dom_in_effect}{$server}) {
 8083:                         $forsaml = 1;
 8084:                     }
 8085:                 }
 8086:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8087:                               &mt('Alias').':&nbsp';
 8088:                 if ($current) {
 8089:                     $aliasrows .= $current;
 8090:                     if ($forsaml) {
 8091:                          $aliasrows .= '&nbsp;('.&mt('also for SSO Auth').')';
 8092:                     }
 8093:                 } else {
 8094:                     $aliasrows .= &mt('None');
 8095:                 }
 8096:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 8097:                               &mt('controlled by domain: [_1]',
 8098:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 8099:             } else {
 8100:                 $dom_in_effect = $dom;
 8101:                 my ($current,$samlon,$samloff);
 8102:                 $samloff = ' checked="checked"';
 8103:                 if (ref($aliases{$dom}) eq 'HASH') {
 8104:                     if ($aliases{$dom}{$server}) {
 8105:                         $current = $aliases{$dom}{$server};
 8106:                     }
 8107:                 }
 8108:                 if (ref($saml{$dom}) eq 'HASH') {
 8109:                     if ($saml{$dom}{$server}) {
 8110:                         $samlon = $samloff;
 8111:                         undef($samloff);
 8112:                     }
 8113:                 }
 8114:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8115:                               &mt('Alias').':&nbsp;'.
 8116:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 8117:                               'value="'.$current.'" size="30" />'.
 8118:                               ('&nbsp;'x2).'<span class="LC_nobreak">'.
 8119:                               &mt('Alias used for SSO Auth').':&nbsp;<label>'.
 8120:                               '<input type="radio" value="0"'.$samloff.' name="wafproxy_alias_saml_'.$server.'" />'.
 8121:                               &mt('No').'</label>&nbsp;<label>'.
 8122:                               '<input type="radio" value="1"'.$samlon.' name="wafproxy_alias_saml_'.$server.'" />'.
 8123:                               &mt('Yes').'</label></span>'.
 8124:                               '</td>';
 8125:             }
 8126:             $aliasrows .= '</tr>';
 8127:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 8128:         }
 8129:         if ($aliasinfo{$dom}) {
 8130:             my ($onclick,$wafon,$wafoff,$showtable);
 8131:             $onclick = ' onclick="javascript:toggleWAF();"';
 8132:             $wafoff = ' checked="checked"';
 8133:             $showtable = ' style="display:none";';
 8134:             if ($showdom) {
 8135:                 $wafon = $wafoff;
 8136:                 $wafoff = '';
 8137:                 $showtable = ' style="display:inline;"';
 8138:             }
 8139:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8140:             $datatable = '<tr'.$css_class.'>'.
 8141:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 8142:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 8143:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 8144:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 8145:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 8146:                          &mt('No').'</label></span></td>'.
 8147:                          '<td class="LC_left_item">'.
 8148:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 8149:                          '</table></td></tr>';
 8150:             $itemcount++;
 8151:         }
 8152:         if (keys(%otherdoms)) {
 8153:             foreach my $key (sort(keys(%otherdoms))) {
 8154:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8155:                 $datatable .= '<tr'.$css_class.'>'.
 8156:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 8157:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 8158:                               '</table></td></tr>';
 8159:                 $itemcount++;
 8160:             }
 8161:         }
 8162:     } else {
 8163:         my %ip_methods = &remoteip_methods();
 8164:         if ($setdom) {
 8165:             $itemcount ++;
 8166:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8167:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 8168:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 8169:             $wafstyle = ' style="display:none;"';
 8170:             $nowafstyle = ' style="display:table-row;"';
 8171:             $currwafdisplay = ' style="display: none"';
 8172:             $wafrangestyle = ' style="display: none"';
 8173:             $curr_remotip = 'n';
 8174:             $ssltossl = ' checked="checked"';
 8175:             if ($showdom) {
 8176:                 $wafstyle = ' style="display:table-row;"';
 8177:                 $nowafstyle =  ' style="display:none;"';
 8178:                 if (keys(%{$values{$dom}})) {
 8179:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 8180:                         $curr_remotip = $values{$dom}{remoteip};
 8181:                     }
 8182:                     if ($curr_remotip eq 'h') {
 8183:                         $currwafdisplay = ' style="display:table-row"';
 8184:                         $wafrangestyle = ' style="display:inline-block;"';
 8185:                     }
 8186:                     if ($values{$dom}{'sslopt'}) {
 8187:                         $alltossl = ' checked="checked"';
 8188:                         $ssltossl = '';
 8189:                     }
 8190:                 }
 8191:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 8192:                     $vpndircheck = ' checked="checked"';
 8193:                     $currwafvpn = ' style="display:table-row;"';
 8194:                     $wafrangestyle = ' style="display:inline-block;"';
 8195:                 } else {
 8196:                     $vpnaliascheck = ' checked="checked"';
 8197:                     $currwafvpn = ' style="display:none;"';
 8198:                 }
 8199:             }
 8200:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8201:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 8202:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 8203:                           '</tr>'.
 8204:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8205:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 8206:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 8207:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 8208:                           &mt('Range(s) stored in CIDR notation').'</div></td>'.
 8209:                           '<td class="LC_left_item"><table>'.
 8210:                           '<tr>'.
 8211:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 8212:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 8213:             foreach my $option ('m','h','n') {
 8214:                 my $sel;
 8215:                 if ($option eq $curr_remotip) {
 8216:                    $sel = ' selected="selected"';
 8217:                 }
 8218:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 8219:                               $ip_methods{$option}.'</option>';
 8220:             }
 8221:             $datatable .= '</select></td></tr>'."\n".
 8222:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 8223:                           $lt{'ipheader'}.':&nbsp;'.
 8224:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 8225:                           'name="wafproxy_ipheader" />'.
 8226:                           '</td></tr>'."\n".
 8227:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 8228:                           $lt{'trusted'}.':<br />'.
 8229:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 8230:                           $values{$dom}{'trusted'}.'</textarea>'.
 8231:                           '</td></tr>'."\n".
 8232:                           '<tr><td><hr /></td></tr>'."\n".
 8233:                           '<tr>'.
 8234:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 8235:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 8236:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 8237:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 8238:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 8239:             foreach my $item ('vpnint','vpnext') {
 8240:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 8241:                               '<td valign="top">'.$lt{$item}.':<br />'.
 8242:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 8243:                               $values{$dom}{$item}.'</textarea>'.
 8244:                               '</td></tr>'."\n";
 8245:             }
 8246:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 8247:                           '<tr>'.
 8248:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 8249:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 8250:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 8251:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 8252:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 8253:                           '</table></td></tr>';
 8254:         }
 8255:         if (keys(%otherdoms)) {
 8256:             foreach my $domain (sort(keys(%otherdoms))) {
 8257:                 $itemcount ++;
 8258:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8259:                 $datatable .= '<tr'.$css_class.'>'.
 8260:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 8261:                               '<td class="LC_left_item"><table>';
 8262:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 8263:                     my $showval = &mt('None');
 8264:                     if ($item eq 'ssl') {
 8265:                         $showval = $lt{'ssltossl'};
 8266:                     }
 8267:                     if ($values{$domain}{$item}) {
 8268:                         $showval = $values{$domain}{$item};
 8269:                         if ($item eq 'ssl') {
 8270:                             $showval = $lt{'alltossl'};
 8271:                         } elsif ($item eq 'remoteip') {
 8272:                             $showval = $ip_methods{$values{$domain}{$item}};
 8273:                         }
 8274:                     }
 8275:                     $datatable .= '<tr>'.
 8276:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 8277:                 }
 8278:                 $datatable .= '</table></td></tr>';
 8279:             }
 8280:         }
 8281:     }
 8282:     $$rowtotal += $itemcount;
 8283:     return $datatable;
 8284: }
 8285: 
 8286: sub wafproxy_titles {
 8287:     return &Apache::lonlocal::texthash(
 8288:                remoteip   => "Method for determining user's IP",
 8289:                ipheader   => 'Request header containing remote IP',
 8290:                trusted    => 'Trusted IP range(s)',
 8291:                vpnaccess  => 'Access from institutional VPN',
 8292:                vpndirect  => 'via regular hostname (no WAF)',
 8293:                vpnaliased => 'via aliased hostname (WAF)',
 8294:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 8295:                vpnext     => 'IP Range(s) for backend WAF connections',
 8296:                sslopt     => 'Forwarding http/https',
 8297:                alltossl   => 'WAF forwards both http and https requests to https',
 8298:                ssltossl   => 'WAF forwards http requests to http and https to https',
 8299:            );
 8300: }
 8301: 
 8302: sub remoteip_methods {
 8303:     return &Apache::lonlocal::texthash(
 8304:               m => 'Use Apache mod_remoteip',
 8305:               h => 'Use headers parsed by LON-CAPA',
 8306:               n => 'Not in use',
 8307:            );
 8308: }
 8309: 
 8310: sub print_usersessions {
 8311:     my ($position,$dom,$settings,$rowtotal) = @_;
 8312:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 8313:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8314:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8315: 
 8316:     my @alldoms = &Apache::lonnet::all_domains();
 8317:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8318:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8319:     my %altids = &id_for_thisdom(%servers);
 8320:     if ($position eq 'top') {
 8321:         if (keys(%serverhomes) > 1) {
 8322:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 8323:             my ($curroffloadnow,$curroffloadoth);
 8324:             if (ref($settings) eq 'HASH') {
 8325:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 8326:                     $curroffloadnow = $settings->{'offloadnow'};
 8327:                 }
 8328:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 8329:                     $curroffloadoth = $settings->{'offloadoth'};
 8330:                 }
 8331:             }
 8332:             my $other_insts = scalar(keys(%by_location));
 8333:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 8334:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 8335:         } else {
 8336:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 8337:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 8338:                           '</td></tr>';
 8339:         }
 8340:     } else {
 8341:         my %titles = &usersession_titles();
 8342:         my ($prefix,@types);
 8343:         if ($position eq 'bottom') {
 8344:             $prefix = 'remote';
 8345:             @types = ('version','excludedomain','includedomain');
 8346:         } else {
 8347:             $prefix = 'hosted';
 8348:             @types = ('excludedomain','includedomain');
 8349:         }
 8350:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8351:     }
 8352:     $$rowtotal += $itemcount;
 8353:     return $datatable;
 8354: }
 8355: 
 8356: sub rules_by_location {
 8357:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 8358:     my ($datatable,$itemcount,$css_class);
 8359:     if (keys(%{$by_location}) == 0) {
 8360:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8361:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 8362:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 8363:                      '</td></tr>';
 8364:         $itemcount = 1;
 8365:     } else {
 8366:         $itemcount = 0;
 8367:         my $numinrow = 5;
 8368:         my (%current,%checkedon,%checkedoff);
 8369:         my @locations = sort(keys(%{$by_location}));
 8370:         foreach my $type (@{$types}) {
 8371:             $checkedon{$type} = '';
 8372:             $checkedoff{$type} = ' checked="checked"';
 8373:         }
 8374:         if (ref($settings) eq 'HASH') {
 8375:             if (ref($settings->{$prefix}) eq 'HASH') {
 8376:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 8377:                     $current{$key} = $settings->{$prefix}{$key};
 8378:                     if ($key eq 'version') {
 8379:                         if ($current{$key} ne '') {
 8380:                             $checkedon{$key} = ' checked="checked"';
 8381:                             $checkedoff{$key} = '';
 8382:                         }
 8383:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 8384:                         $checkedon{$key} = ' checked="checked"';
 8385:                         $checkedoff{$key} = '';
 8386:                     }
 8387:                 }
 8388:             }
 8389:         }
 8390:         foreach my $type (@{$types}) {
 8391:             next if ($type ne 'version' && !@locations);
 8392:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8393:             $datatable .= '<tr'.$css_class.'>
 8394:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 8395:                            <span class="LC_nobreak">&nbsp;
 8396:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 8397:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 8398:             if ($type eq 'version') {
 8399:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 8400:                 my $selector = '<select name="'.$prefix.'_version">';
 8401:                 foreach my $version (@lcversions) {
 8402:                     my $selected = '';
 8403:                     if ($current{'version'} eq $version) {
 8404:                         $selected = ' selected="selected"';
 8405:                     }
 8406:                     $selector .= ' <option value="'.$version.'"'.
 8407:                                  $selected.'>'.$version.'</option>';
 8408:                 }
 8409:                 $selector .= '</select> ';
 8410:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 8411:             } else {
 8412:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 8413:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 8414:                              ' />'.('&nbsp;'x2).
 8415:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 8416:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 8417:                              "\n".
 8418:                              '</div><div><table>';
 8419:                 my $rem;
 8420:                 for (my $i=0; $i<@locations; $i++) {
 8421:                     my ($showloc,$value,$checkedtype);
 8422:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 8423:                         my $ip = $by_location->{$locations[$i]}->[0];
 8424:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8425:                             $value = join(':',@{$by_ip->{$ip}});
 8426:                             $showloc = join(', ',@{$by_ip->{$ip}});
 8427:                             if (ref($current{$type}) eq 'ARRAY') {
 8428:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 8429:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 8430:                                         $checkedtype = ' checked="checked"';
 8431:                                         last;
 8432:                                     }
 8433:                                 }
 8434:                             }
 8435:                         }
 8436:                     }
 8437:                     $rem = $i%($numinrow);
 8438:                     if ($rem == 0) {
 8439:                         if ($i > 0) {
 8440:                             $datatable .= '</tr>';
 8441:                         }
 8442:                         $datatable .= '<tr>';
 8443:                     }
 8444:                     $datatable .= '<td class="LC_left_item">'.
 8445:                                   '<span class="LC_nobreak"><label>'.
 8446:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 8447:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 8448:                                   '</label></span></td>';
 8449:                 }
 8450:                 $rem = @locations%($numinrow);
 8451:                 my $colsleft = $numinrow - $rem;
 8452:                 if ($colsleft > 1 ) {
 8453:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8454:                                   '&nbsp;</td>';
 8455:                 } elsif ($colsleft == 1) {
 8456:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8457:                 }
 8458:                 $datatable .= '</tr></table>';
 8459:             }
 8460:             $datatable .= '</td></tr>';
 8461:             $itemcount ++;
 8462:         }
 8463:     }
 8464:     return ($datatable,$itemcount);
 8465: }
 8466: 
 8467: sub print_ssl {
 8468:     my ($position,$dom,$settings,$rowtotal) = @_;
 8469:     my ($css_class,$datatable);
 8470:     my $itemcount = 1;
 8471:     if ($position eq 'top') {
 8472:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8473:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8474:         my $same_institution;
 8475:         if ($intdom ne '') {
 8476:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 8477:             if (ref($internet_names) eq 'ARRAY') {
 8478:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 8479:                     $same_institution = 1;
 8480:                 }
 8481:             }
 8482:         }
 8483:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8484:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 8485:         if ($same_institution) {
 8486:             my %domservers = &Apache::lonnet::get_servers($dom);
 8487:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 8488:         } else {
 8489:             $datatable .= &mt("You need to be logged into one of your own domain's servers to display information about the status of LON-CAPA SSL certificates.");
 8490:         }
 8491:         $datatable .= '</td></tr>';
 8492:         $itemcount ++;
 8493:     } else {
 8494:         my %titles = &ssl_titles();
 8495:         my (%by_ip,%by_location,@intdoms,@instdoms);
 8496:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8497:         my @alldoms = &Apache::lonnet::all_domains();
 8498:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8499:         my @domservers = &Apache::lonnet::get_servers($dom);
 8500:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8501:         my %altids = &id_for_thisdom(%servers);
 8502:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 8503:             my $legacy;
 8504:             unless (ref($settings) eq 'HASH') {
 8505:                 my $name;
 8506:                 if ($position eq 'connto') {
 8507:                     $name = 'loncAllowInsecure';
 8508:                 } else {
 8509:                     $name = 'londAllowInsecure';
 8510:                 }
 8511:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 8512:                 my @ids=&Apache::lonnet::current_machine_ids();
 8513:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 8514:                     my %what = (
 8515:                                    $name => 1,
 8516:                                );
 8517:                     my ($result,$returnhash) =
 8518:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 8519:                     if ($result eq 'ok') {
 8520:                         if (ref($returnhash) eq 'HASH') {
 8521:                             $legacy = $returnhash->{$name};
 8522:                         }
 8523:                     }
 8524:                 } else {
 8525:                     $legacy = $Apache::lonnet::perlvar{$name};
 8526:                 }
 8527:             }
 8528:             foreach my $type ('dom','intdom','other') {
 8529:                 my %checked;
 8530:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8531:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 8532:                               '<td class="LC_right_item">';
 8533:                 my $skip; 
 8534:                 if ($type eq 'dom') {
 8535:                     unless (keys(%servers) > 1) {
 8536:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 8537:                         $skip = 1;
 8538:                     }
 8539:                 }
 8540:                 if ($type eq 'intdom') {
 8541:                     unless (@instdoms > 1) {
 8542:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 8543:                         $skip = 1;
 8544:                     } 
 8545:                 } elsif ($type eq 'other') {
 8546:                     if (keys(%by_location) == 0) {
 8547:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 8548:                         $skip = 1;
 8549:                     }
 8550:                 }
 8551:                 unless ($skip) {
 8552:                     $checked{'yes'} = ' checked="checked"'; 
 8553:                     if (ref($settings) eq 'HASH') {
 8554:                         if (ref($settings->{$position}) eq 'HASH') {
 8555:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 8556:                                 $checked{$1} = $checked{'yes'};
 8557:                                 delete($checked{'yes'}); 
 8558:                             }
 8559:                         }
 8560:                     } else {
 8561:                         if ($legacy == 0) {
 8562:                             $checked{'req'} = $checked{'yes'};
 8563:                             delete($checked{'yes'});    
 8564:                         }
 8565:                     }
 8566:                     foreach my $option ('no','yes','req') {
 8567:                         $datatable .= '<span class="LC_nobreak"><label>'.
 8568:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 8569:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 8570:                                       '</label></span>'.('&nbsp;'x2);
 8571:                     }
 8572:                 }
 8573:                 $datatable .= '</td></tr>';
 8574:                 $itemcount ++; 
 8575:             }
 8576:         } else {
 8577:             my $prefix = 'replication';
 8578:             my @types = ('certreq','nocertreq');
 8579:             if (keys(%by_location) == 0) {
 8580:                 $datatable .= '<tr'.$css_class.'><td>'.
 8581:                               &mt('Nothing to set here, as there are no other institutions').
 8582:                               '</td></tr>';
 8583:                 $itemcount ++;
 8584:             } else {
 8585:                 ($datatable,$itemcount) = 
 8586:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8587:             }
 8588:         }
 8589:     }
 8590:     $$rowtotal += $itemcount;
 8591:     return $datatable;
 8592: }
 8593: 
 8594: sub ssl_titles {
 8595:     return &Apache::lonlocal::texthash (
 8596:                dom           => 'LON-CAPA servers/VMs from same domain',
 8597:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 8598:                other         => 'External LON-CAPA servers/VMs',
 8599:                connto        => 'Connections to other servers',
 8600:                connfrom      => 'Connections from other servers',
 8601:                replication   => 'Replicating content to other institutions',
 8602:                certreq       => 'Client certificate required, but specific domains exempt',
 8603:                nocertreq     => 'No client certificate required, except for specific domains',
 8604:                no            => 'SSL not used',
 8605:                yes           => 'SSL Optional (used if available)',
 8606:                req           => 'SSL Required',
 8607:     );
 8608: }
 8609: 
 8610: sub print_trust {
 8611:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 8612:     my ($css_class,$datatable,%checked,%choices);
 8613:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8614:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8615:     my $itemcount = 1;
 8616:     my %titles = &trust_titles();
 8617:     my @types = ('exc','inc');
 8618:     if ($prefix eq 'top') {
 8619:         $prefix = 'content';
 8620:     } elsif ($prefix eq 'bottom') {
 8621:         $prefix = 'msg';
 8622:     }
 8623:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8624:     $$rowtotal += $itemcount;
 8625:     return $datatable;
 8626: }
 8627: 
 8628: sub trust_titles {
 8629:     return &Apache::lonlocal::texthash(
 8630:                content  => "Access to this domain's content by others",
 8631:                shared   => "Access to other domain's content by this domain",
 8632:                enroll   => "Enrollment in this domain's courses by others", 
 8633:                othcoau  => "Co-author roles in this domain for others",
 8634:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 8635:                domroles => "Domain roles in this domain assignable to others",
 8636:                catalog  => "Course Catalog for this domain displayed elsewhere",
 8637:                reqcrs   => "Requests for creation of courses in this domain by others",
 8638:                msg      => "Users in other domains can send messages to this domain",
 8639:                exc      => "Allow all, but exclude specific domains",
 8640:                inc      => "Deny all, but include specific domains",
 8641:            );
 8642: } 
 8643: 
 8644: sub build_location_hashes {
 8645:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 8646:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 8647:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 8648:     my %iphost = &Apache::lonnet::get_iphost();
 8649:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 8650:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 8651:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 8652:         foreach my $id (@{$iphost{$primary_ip}}) {
 8653:             my $intdom = &Apache::lonnet::internet_dom($id);
 8654:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 8655:                 push(@{$intdoms},$intdom);
 8656:             }
 8657:         }
 8658:     }
 8659:     foreach my $ip (keys(%iphost)) {
 8660:         if (ref($iphost{$ip}) eq 'ARRAY') {
 8661:             foreach my $id (@{$iphost{$ip}}) {
 8662:                 my $location = &Apache::lonnet::internet_dom($id);
 8663:                 if ($location) {
 8664:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 8665:                         my $dom = &Apache::lonnet::host_domain($id);
 8666:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 8667:                             push(@{$instdoms},$dom);
 8668:                         }
 8669:                         next;
 8670:                     }
 8671:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8672:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 8673:                             push(@{$by_ip->{$ip}},$location);
 8674:                         }
 8675:                     } else {
 8676:                         $by_ip->{$ip} = [$location];
 8677:                     }
 8678:                 }
 8679:             }
 8680:         }
 8681:     }
 8682:     foreach my $ip (sort(keys(%{$by_ip}))) {
 8683:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8684:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 8685:             my $first = $by_ip->{$ip}->[0];
 8686:             if (ref($by_location->{$first}) eq 'ARRAY') {
 8687:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 8688:                     push(@{$by_location->{$first}},$ip);
 8689:                 }
 8690:             } else {
 8691:                 $by_location->{$first} = [$ip];
 8692:             }
 8693:         }
 8694:     }
 8695:     return;
 8696: }
 8697: 
 8698: sub current_offloads_to {
 8699:     my ($dom,$settings,$servers) = @_;
 8700:     my (%spareid,%otherdomconfigs);
 8701:     if (ref($servers) eq 'HASH') {
 8702:         foreach my $lonhost (sort(keys(%{$servers}))) {
 8703:             my $gotspares;
 8704:             if (ref($settings) eq 'HASH') {
 8705:                 if (ref($settings->{'spares'}) eq 'HASH') {
 8706:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 8707:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 8708:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 8709:                         $gotspares = 1;
 8710:                     }
 8711:                 }
 8712:             }
 8713:             unless ($gotspares) {
 8714:                 my $gotspares;
 8715:                 my $serverhomeID =
 8716:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 8717:                 my $serverhomedom =
 8718:                     &Apache::lonnet::host_domain($serverhomeID);
 8719:                 if ($serverhomedom ne $dom) {
 8720:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 8721:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8722:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8723:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 8724:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 8725:                                 $gotspares = 1;
 8726:                             }
 8727:                         }
 8728:                     } else {
 8729:                         $otherdomconfigs{$serverhomedom} =
 8730:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 8731:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 8732:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8733:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8734:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 8735:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 8736:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 8737:                                         $gotspares = 1;
 8738:                                     }
 8739:                                 }
 8740:                             }
 8741:                         }
 8742:                     }
 8743:                 }
 8744:             }
 8745:             unless ($gotspares) {
 8746:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 8747:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 8748:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 8749:                } else {
 8750:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 8751:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 8752:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 8753:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 8754:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 8755:                     } else {
 8756:                         my %what = (
 8757:                              spareid => 1,
 8758:                         );
 8759:                         my ($result,$returnhash) = 
 8760:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 8761:                         if ($result eq 'ok') { 
 8762:                             if (ref($returnhash) eq 'HASH') {
 8763:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 8764:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 8765:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 8766:                                 }
 8767:                             }
 8768:                         }
 8769:                     }
 8770:                 }
 8771:             }
 8772:         }
 8773:     }
 8774:     return %spareid;
 8775: }
 8776: 
 8777: sub spares_row {
 8778:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 8779:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 8780:     my $css_class;
 8781:     my $numinrow = 4;
 8782:     my $itemcount = 1;
 8783:     my $datatable;
 8784:     my %typetitles = &sparestype_titles();
 8785:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 8786:         foreach my $server (sort(keys(%{$servers}))) {
 8787:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 8788:             my ($othercontrol,$serverdom);
 8789:             if ($serverhome ne $server) {
 8790:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 8791:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 8792:             } else {
 8793:                 $serverdom = &Apache::lonnet::host_domain($server);
 8794:                 if ($serverdom ne $dom) {
 8795:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 8796:                 }
 8797:             }
 8798:             next unless (ref($spareid->{$server}) eq 'HASH');
 8799:             my ($checkednow,$checkedoth);
 8800:             if (ref($curroffloadnow) eq 'HASH') {
 8801:                 if ($curroffloadnow->{$server}) {
 8802:                     $checkednow = ' checked="checked"';
 8803:                 }
 8804:             }
 8805:             if (ref($curroffloadoth) eq 'HASH') {
 8806:                 if ($curroffloadoth->{$server}) {
 8807:                     $checkedoth = ' checked="checked"';
 8808:                 }
 8809:             }
 8810:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8811:             $datatable .= '<tr'.$css_class.'>
 8812:                            <td rowspan="2">
 8813:                             <span class="LC_nobreak">'.
 8814:                           &mt('[_1] when busy, offloads to:'
 8815:                               ,'<b>'.$server.'</b>').'</span><br />'.
 8816:                           '<span class="LC_nobreak">'."\n".
 8817:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 8818:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 8819:                           "\n";
 8820:             if ($other_insts) {
 8821:                 $datatable .= '<br />'.
 8822:                               '<span class="LC_nobreak">'."\n".
 8823:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 8824:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 8825:                           "\n";
 8826:             }
 8827:             my (%current,%canselect);
 8828:             my @choices = 
 8829:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 8830:             foreach my $type ('primary','default') {
 8831:                 if (ref($spareid->{$server}) eq 'HASH') {
 8832:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 8833:                         my @spares = @{$spareid->{$server}{$type}};
 8834:                         if (@spares > 0) {
 8835:                             if ($othercontrol) {
 8836:                                 $current{$type} = join(', ',@spares);
 8837:                             } else {
 8838:                                 $current{$type} .= '<table>';
 8839:                                 my $numspares = scalar(@spares);
 8840:                                 for (my $i=0;  $i<@spares; $i++) {
 8841:                                     my $rem = $i%($numinrow);
 8842:                                     if ($rem == 0) {
 8843:                                         if ($i > 0) {
 8844:                                             $current{$type} .= '</tr>';
 8845:                                         }
 8846:                                         $current{$type} .= '<tr>';
 8847:                                     }
 8848:                                     $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;'.
 8849:                                                        $spareid->{$server}{$type}[$i].
 8850:                                                        '</label></td>'."\n";
 8851:                                 }
 8852:                                 my $rem = @spares%($numinrow);
 8853:                                 my $colsleft = $numinrow - $rem;
 8854:                                 if ($colsleft > 1 ) {
 8855:                                     $current{$type} .= '<td colspan="'.$colsleft.
 8856:                                                        '" class="LC_left_item">'.
 8857:                                                        '&nbsp;</td>';
 8858:                                 } elsif ($colsleft == 1) {
 8859:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 8860:                                 }
 8861:                                 $current{$type} .= '</tr></table>';
 8862:                             }
 8863:                         }
 8864:                     }
 8865:                     if ($current{$type} eq '') {
 8866:                         $current{$type} = &mt('None specified');
 8867:                     }
 8868:                     if ($othercontrol) {
 8869:                         if ($type eq 'primary') {
 8870:                             $canselect{$type} = $othercontrol;
 8871:                         }
 8872:                     } else {
 8873:                         $canselect{$type} = 
 8874:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 8875:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 8876:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 8877:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 8878:                         if (@choices > 0) {
 8879:                             foreach my $lonhost (@choices) {
 8880:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 8881:                             }
 8882:                         }
 8883:                         $canselect{$type} .= '</select>'."\n";
 8884:                     }
 8885:                 } else {
 8886:                     $current{$type} = &mt('Could not be determined');
 8887:                     if ($type eq 'primary') {
 8888:                         $canselect{$type} =  $othercontrol;
 8889:                     }
 8890:                 }
 8891:                 if ($type eq 'default') {
 8892:                     $datatable .= '<tr'.$css_class.'>';
 8893:                 }
 8894:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 8895:                               '<td>'.$current{$type}.'</td>'."\n".
 8896:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 8897:             }
 8898:             $itemcount ++;
 8899:         }
 8900:     }
 8901:     $$rowtotal += $itemcount;
 8902:     return $datatable;
 8903: }
 8904: 
 8905: sub possible_newspares {
 8906:     my ($server,$currspares,$serverhomes,$altids) = @_;
 8907:     my $serverhostname = &Apache::lonnet::hostname($server);
 8908:     my %excluded;
 8909:     if ($serverhostname ne '') {
 8910:         %excluded = (
 8911:                        $serverhostname => 1,
 8912:                     );
 8913:     }
 8914:     if (ref($currspares) eq 'HASH') {
 8915:         foreach my $type (keys(%{$currspares})) {
 8916:             if (ref($currspares->{$type}) eq 'ARRAY') {
 8917:                 if (@{$currspares->{$type}} > 0) {
 8918:                     foreach my $curr (@{$currspares->{$type}}) {
 8919:                         my $hostname = &Apache::lonnet::hostname($curr);
 8920:                         $excluded{$hostname} = 1;
 8921:                     }
 8922:                 }
 8923:             }
 8924:         }
 8925:     }
 8926:     my @choices;
 8927:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 8928:         if (keys(%{$serverhomes}) > 1) {
 8929:             foreach my $name (sort(keys(%{$serverhomes}))) {
 8930:                 unless ($excluded{$name}) {
 8931:                     if (exists($altids->{$serverhomes->{$name}})) {
 8932:                         push(@choices,$altids->{$serverhomes->{$name}});
 8933:                     } else {
 8934:                         push(@choices,$serverhomes->{$name});
 8935:                     }
 8936:                 }
 8937:             }
 8938:         }
 8939:     }
 8940:     return sort(@choices);
 8941: }
 8942: 
 8943: sub print_loadbalancing {
 8944:     my ($dom,$settings,$rowtotal) = @_;
 8945:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8946:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8947:     my $numinrow = 1;
 8948:     my $datatable;
 8949:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8950:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 8951:     if (ref($settings) eq 'HASH') {
 8952:         %existing = %{$settings};
 8953:     }
 8954:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 8955:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8956:                                   \%currtargets,\%currrules,\%currcookies);
 8957:     } else {
 8958:         return;
 8959:     }
 8960:     my ($othertitle,$usertypes,$types) =
 8961:         &Apache::loncommon::sorted_inst_types($dom);
 8962:     my $rownum = 8;
 8963:     if (ref($types) eq 'ARRAY') {
 8964:         $rownum += scalar(@{$types});
 8965:     }
 8966:     my @css_class = ('LC_odd_row','LC_even_row');
 8967:     my $balnum = 0;
 8968:     my $islast;
 8969:     my (@toshow,$disabledtext);
 8970:     if (keys(%currbalancer) > 0) {
 8971:         @toshow = sort(keys(%currbalancer));
 8972:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 8973:             push(@toshow,'');
 8974:         }
 8975:     } else {
 8976:         @toshow = ('');
 8977:         $disabledtext = &mt('No existing load balancer');
 8978:     }
 8979:     foreach my $lonhost (@toshow) {
 8980:         if ($balnum == scalar(@toshow)-1) {
 8981:             $islast = 1;
 8982:         } else {
 8983:             $islast = 0;
 8984:         }
 8985:         my $cssidx = $balnum%2;
 8986:         my $targets_div_style = 'display: none';
 8987:         my $disabled_div_style = 'display: block';
 8988:         my $homedom_div_style = 'display: none';
 8989:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 8990:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 8991:                       '<p>';
 8992:         if ($lonhost eq '') {
 8993:             $datatable .= '<span class="LC_nobreak">';
 8994:             if (keys(%currbalancer) > 0) {
 8995:                 $datatable .= &mt('Add balancer:');
 8996:             } else {
 8997:                 $datatable .= &mt('Enable balancer:');
 8998:             }
 8999:             $datatable .= '&nbsp;'.
 9000:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 9001:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 9002:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 9003:                           '<option value="" selected="selected">'.&mt('None').
 9004:                           '</option>'."\n";
 9005:             foreach my $server (sort(keys(%servers))) {
 9006:                 next if ($currbalancer{$server});
 9007:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 9008:             }
 9009:             $datatable .=
 9010:                 '</select>'."\n".
 9011:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 9012:         } else {
 9013:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 9014:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 9015:                            &mt('Stop balancing').'</label>'.
 9016:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 9017:             $targets_div_style = 'display: block';
 9018:             $disabled_div_style = 'display: none';
 9019:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 9020:                 $homedom_div_style = 'display: block';
 9021:             }
 9022:         }
 9023:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 9024:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 9025:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 9026:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 9027:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 9028:         my @sparestypes = ('primary','default');
 9029:         my %typetitles = &sparestype_titles();
 9030:         my %hostherechecked = (
 9031:                                   no => ' checked="checked"',
 9032:                               );
 9033:         my %balcookiechecked = (
 9034:                                   no => ' checked="checked"',
 9035:                                );
 9036:         foreach my $sparetype (@sparestypes) {
 9037:             my $targettable;
 9038:             for (my $i=0; $i<$numspares; $i++) {
 9039:                 my $checked;
 9040:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 9041:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 9042:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 9043:                             $checked = ' checked="checked"';
 9044:                         }
 9045:                     }
 9046:                 }
 9047:                 my ($chkboxval,$disabled);
 9048:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 9049:                     $chkboxval = $spares[$i];
 9050:                 }
 9051:                 if (exists($currbalancer{$spares[$i]})) {
 9052:                     $disabled = ' disabled="disabled"';
 9053:                 }
 9054:                 $targettable .=
 9055:                     '<td><span class="LC_nobreak"><label>'.
 9056:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 9057:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 9058:                     '</span></label></span></td>';
 9059:                 my $rem = $i%($numinrow);
 9060:                 if ($rem == 0) {
 9061:                     if (($i > 0) && ($i < $numspares-1)) {
 9062:                         $targettable .= '</tr>';
 9063:                     }
 9064:                     if ($i < $numspares-1) {
 9065:                         $targettable .= '<tr>';
 9066:                     }
 9067:                 }
 9068:             }
 9069:             if ($targettable ne '') {
 9070:                 my $rem = $numspares%($numinrow);
 9071:                 my $colsleft = $numinrow - $rem;
 9072:                 if ($colsleft > 1 ) {
 9073:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9074:                                     '&nbsp;</td>';
 9075:                 } elsif ($colsleft == 1) {
 9076:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 9077:                 }
 9078:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 9079:                                '<table><tr>'.$targettable.'</tr></table><br />';
 9080:             }
 9081:             $hostherechecked{$sparetype} = '';
 9082:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 9083:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 9084:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 9085:                         $hostherechecked{$sparetype} = ' checked="checked"';
 9086:                         $hostherechecked{'no'} = '';
 9087:                     }
 9088:                 }
 9089:             }
 9090:         }
 9091:         if ($currcookies{$lonhost}) {
 9092:             %balcookiechecked = (
 9093:                                     yes => ' checked="checked"',
 9094:                                 );
 9095:         }
 9096:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 9097:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 9098:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 9099:         foreach my $sparetype (@sparestypes) {
 9100:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 9101:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 9102:                           '</i></label><br />';
 9103:         }
 9104:         $datatable .= &mt('Use balancer cookie').'<br />'.
 9105:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 9106:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 9107:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 9108:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 9109:                       '</div></td></tr>'.
 9110:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 9111:                                            $othertitle,$usertypes,$types,\%servers,
 9112:                                            \%currbalancer,$lonhost,
 9113:                                            $targets_div_style,$homedom_div_style,
 9114:                                            $css_class[$cssidx],$balnum,$islast);
 9115:         $$rowtotal += $rownum;
 9116:         $balnum ++;
 9117:     }
 9118:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 9119:     return $datatable;
 9120: }
 9121: 
 9122: sub get_loadbalancers_config {
 9123:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 9124:     return unless ((ref($servers) eq 'HASH') &&
 9125:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 9126:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 9127:                    (ref($currcookies) eq 'HASH'));
 9128:     if (keys(%{$existing}) > 0) {
 9129:         my $oldlonhost;
 9130:         foreach my $key (sort(keys(%{$existing}))) {
 9131:             if ($key eq 'lonhost') {
 9132:                 $oldlonhost = $existing->{'lonhost'};
 9133:                 $currbalancer->{$oldlonhost} = 1;
 9134:             } elsif ($key eq 'targets') {
 9135:                 if ($oldlonhost) {
 9136:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 9137:                 }
 9138:             } elsif ($key eq 'rules') {
 9139:                 if ($oldlonhost) {
 9140:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 9141:                 }
 9142:             } elsif (ref($existing->{$key}) eq 'HASH') {
 9143:                 $currbalancer->{$key} = 1;
 9144:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 9145:                 $currrules->{$key} = $existing->{$key}{'rules'};
 9146:                 if ($existing->{$key}{'cookie'}) {
 9147:                     $currcookies->{$key} = 1;
 9148:                 }
 9149:             }
 9150:         }
 9151:     } else {
 9152:         my ($balancerref,$targetsref) =
 9153:                 &Apache::lonnet::get_lonbalancer_config($servers);
 9154:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 9155:             foreach my $server (sort(keys(%{$balancerref}))) {
 9156:                 $currbalancer->{$server} = 1;
 9157:                 $currtargets->{$server} = $targetsref->{$server};
 9158:             }
 9159:         }
 9160:     }
 9161:     return;
 9162: }
 9163: 
 9164: sub loadbalancing_rules {
 9165:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 9166:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 9167:         $css_class,$balnum,$islast) = @_;
 9168:     my $output;
 9169:     my $num = 0;
 9170:     my ($alltypes,$othertypes,$titles) =
 9171:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 9172:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 9173:         foreach my $type (@{$alltypes}) {
 9174:             $num ++;
 9175:             my $current;
 9176:             if (ref($currrules) eq 'HASH') {
 9177:                 $current = $currrules->{$type};
 9178:             }
 9179:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9180:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 9181:                     $current = '';
 9182:                 }
 9183:             }
 9184:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 9185:                                              $servers,$currbalancer,$lonhost,$dom,
 9186:                                              $targets_div_style,$homedom_div_style,
 9187:                                              $css_class,$balnum,$num,$islast);
 9188:         }
 9189:     }
 9190:     return $output;
 9191: }
 9192: 
 9193: sub loadbalancing_titles {
 9194:     my ($dom,$intdom,$usertypes,$types) = @_;
 9195:     my %othertypes = (
 9196:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 9197:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 9198:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 9199:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 9200:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 9201:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 9202:                      );
 9203:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 9204:     my @available;
 9205:     if (ref($types) eq 'ARRAY') {
 9206:         @available = @{$types};
 9207:     }
 9208:     unless (grep(/^default$/,@available)) {
 9209:         push(@available,'default');
 9210:     }
 9211:     unshift(@alltypes,@available);
 9212:     my %titles;
 9213:     foreach my $type (@alltypes) {
 9214:         if ($type =~ /^_LC_/) {
 9215:             $titles{$type} = $othertypes{$type};
 9216:         } elsif ($type eq 'default') {
 9217:             $titles{$type} = &mt('All users from [_1]',$dom);
 9218:             if (ref($types) eq 'ARRAY') {
 9219:                 if (@{$types} > 0) {
 9220:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 9221:                 }
 9222:             }
 9223:         } elsif (ref($usertypes) eq 'HASH') {
 9224:             $titles{$type} = $usertypes->{$type};
 9225:         }
 9226:     }
 9227:     return (\@alltypes,\%othertypes,\%titles);
 9228: }
 9229: 
 9230: sub loadbalance_rule_row {
 9231:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 9232:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 9233:     my @rulenames;
 9234:     my %ruletitles = &offloadtype_text();
 9235:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 9236:         @rulenames = ('balancer','offloadedto','specific');
 9237:     } else {
 9238:         @rulenames = ('default','homeserver');
 9239:         if ($type eq '_LC_external') {
 9240:             push(@rulenames,'externalbalancer');
 9241:         } else {
 9242:             push(@rulenames,'specific');
 9243:         }
 9244:         push(@rulenames,'none');
 9245:     }
 9246:     my $style = $targets_div_style;
 9247:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9248:         $style = $homedom_div_style;
 9249:     }
 9250:     my $space;
 9251:     if ($islast && $num == 1) {
 9252:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 9253:     }
 9254:     my $output =
 9255:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 9256:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 9257:         '<td valaign="top">'.$space.
 9258:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 9259:     for (my $i=0; $i<@rulenames; $i++) {
 9260:         my $rule = $rulenames[$i];
 9261:         my ($checked,$extra);
 9262:         if ($rulenames[$i] eq 'default') {
 9263:             $rule = '';
 9264:         }
 9265:         if ($rulenames[$i] eq 'specific') {
 9266:             if (ref($servers) eq 'HASH') {
 9267:                 my $default;
 9268:                 if (($current ne '') && (exists($servers->{$current}))) {
 9269:                     $checked = ' checked="checked"';
 9270:                 }
 9271:                 unless ($checked) {
 9272:                     $default = ' selected="selected"';
 9273:                 }
 9274:                 $extra =
 9275:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9276:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9277:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 9278:                     '<option value=""'.$default.'></option>'."\n";
 9279:                 foreach my $server (sort(keys(%{$servers}))) {
 9280:                     if (ref($currbalancer) eq 'HASH') {
 9281:                         next if (exists($currbalancer->{$server}));
 9282:                     }
 9283:                     my $selected;
 9284:                     if ($server eq $current) {
 9285:                         $selected = ' selected="selected"';
 9286:                     }
 9287:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 9288:                 }
 9289:                 $extra .= '</select>';
 9290:             }
 9291:         } elsif ($rule eq $current) {
 9292:             $checked = ' checked="checked"';
 9293:         }
 9294:         $output .= '<span class="LC_nobreak"><label>'.
 9295:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 9296:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 9297:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 9298:                    ')"'.$checked.' />&nbsp;';
 9299:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 9300:             $output .= $ruletitles{'particular'};
 9301:         } else {
 9302:             $output .= $ruletitles{$rulenames[$i]};
 9303:         }
 9304:         $output .= '</label>'.$extra.'</span><br />'."\n";
 9305:     }
 9306:     $output .= '</div></td></tr>'."\n";
 9307:     return $output;
 9308: }
 9309: 
 9310: sub offloadtype_text {
 9311:     my %ruletitles = &Apache::lonlocal::texthash (
 9312:            'default'          => 'Offloads to default destinations',
 9313:            'homeserver'       => "Offloads to user's home server",
 9314:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 9315:            'specific'         => 'Offloads to specific server',
 9316:            'none'             => 'No offload',
 9317:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 9318:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 9319:            'particular'       => 'Session hosted (after re-auth) on server:',
 9320:     );
 9321:     return %ruletitles;
 9322: }
 9323: 
 9324: sub sparestype_titles {
 9325:     my %typestitles = &Apache::lonlocal::texthash (
 9326:                           'primary' => 'primary',
 9327:                           'default' => 'default',
 9328:                       );
 9329:     return %typestitles;
 9330: }
 9331: 
 9332: sub contact_titles {
 9333:     my %titles = &Apache::lonlocal::texthash (
 9334:                    'supportemail'    => 'Support E-mail address',
 9335:                    'adminemail'      => 'Default Server Admin E-mail address',
 9336:                    'errormail'       => 'Error reports to be e-mailed to',
 9337:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 9338:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 9339:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 9340:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 9341:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 9342:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 9343:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 9344:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 9345:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 9346:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 9347:                    'errorweights'    => 'Weights used to compute error count',
 9348:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 9349:                  );
 9350:     my %short_titles = &Apache::lonlocal::texthash (
 9351:                            adminemail   => 'Admin E-mail address',
 9352:                            supportemail => 'Support E-mail',
 9353:                        );   
 9354:     return (\%titles,\%short_titles);
 9355: }
 9356: 
 9357: sub helpform_fields {
 9358:     my %titles =  &Apache::lonlocal::texthash (
 9359:                        'username'   => 'Name',
 9360:                        'user'       => 'Username/domain',
 9361:                        'phone'      => 'Phone',
 9362:                        'cc'         => 'Cc e-mail',
 9363:                        'course'     => 'Course Details',
 9364:                        'section'    => 'Sections',
 9365:                        'screenshot' => 'File upload',
 9366:     );
 9367:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 9368:     my %possoptions = (
 9369:                         username     => ['yes','no','req'],
 9370:                         phone        => ['yes','no','req'],
 9371:                         user         => ['yes','no'],
 9372:                         cc           => ['yes','no'],
 9373:                         course       => ['yes','no'],
 9374:                         section      => ['yes','no'],
 9375:                         screenshot   => ['yes','no'],
 9376:                       );
 9377:     my %fieldoptions = &Apache::lonlocal::texthash (
 9378:                          'yes'  => 'Optional',
 9379:                          'req'  => 'Required',
 9380:                          'no'   => "Not shown",
 9381:     );
 9382:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 9383: }
 9384: 
 9385: sub tool_titles {
 9386:     my %titles = &Apache::lonlocal::texthash (
 9387:                      aboutme    => 'Personal web page',
 9388:                      blog       => 'Blog',
 9389:                      webdav     => 'WebDAV',
 9390:                      portfolio  => 'Portfolio',
 9391:                      timezone   => 'Can set time zone',
 9392:                      official   => 'Official courses (with institutional codes)',
 9393:                      unofficial => 'Unofficial courses',
 9394:                      community  => 'Communities',
 9395:                      textbook   => 'Textbook courses',
 9396:                      placement  => 'Placement tests',
 9397:                  );
 9398:     return %titles;
 9399: }
 9400: 
 9401: sub courserequest_titles {
 9402:     my %titles = &Apache::lonlocal::texthash (
 9403:                                    official   => 'Official',
 9404:                                    unofficial => 'Unofficial',
 9405:                                    community  => 'Communities',
 9406:                                    textbook   => 'Textbook',
 9407:                                    placement  => 'Placement tests',
 9408:                                    lti        => 'LTI Provider',
 9409:                                    norequest  => 'Not allowed',
 9410:                                    approval   => 'Approval by DC',
 9411:                                    validate   => 'With validation',
 9412:                                    autolimit  => 'Numerical limit',
 9413:                                    unlimited  => '(blank for unlimited)',
 9414:                  );
 9415:     return %titles;
 9416: }
 9417: 
 9418: sub authorrequest_titles {
 9419:     my %titles = &Apache::lonlocal::texthash (
 9420:                                    norequest  => 'Not allowed',
 9421:                                    approval   => 'Approval by Dom. Coord.',
 9422:                                    automatic  => 'Automatic approval',
 9423:                  );
 9424:     return %titles;
 9425: }
 9426: 
 9427: sub courserequest_conditions {
 9428:     my %conditions = &Apache::lonlocal::texthash (
 9429:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 9430:        validate   => '(Processing of request subject to institutional validation).',
 9431:                  );
 9432:     return %conditions;
 9433: }
 9434: 
 9435: 
 9436: sub print_usercreation {
 9437:     my ($position,$dom,$settings,$rowtotal) = @_;
 9438:     my $numinrow = 4;
 9439:     my $datatable;
 9440:     if ($position eq 'top') {
 9441:         $$rowtotal ++;
 9442:         my $rowcount = 0;
 9443:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 9444:         if (ref($rules) eq 'HASH') {
 9445:             if (keys(%{$rules}) > 0) {
 9446:                 $datatable .= &user_formats_row('username',$settings,$rules,
 9447:                                                 $ruleorder,$numinrow,$rowcount);
 9448:                 $$rowtotal ++;
 9449:                 $rowcount ++;
 9450:             }
 9451:         }
 9452:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 9453:         if (ref($idrules) eq 'HASH') {
 9454:             if (keys(%{$idrules}) > 0) {
 9455:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 9456:                                                 $idruleorder,$numinrow,$rowcount);
 9457:                 $$rowtotal ++;
 9458:                 $rowcount ++;
 9459:             }
 9460:         }
 9461:         if ($rowcount == 0) {
 9462:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 9463:             $$rowtotal ++;
 9464:             $rowcount ++;
 9465:         }
 9466:     } elsif ($position eq 'middle') {
 9467:         my @creators = ('author','course','requestcrs');
 9468:         my ($rules,$ruleorder) =
 9469:             &Apache::lonnet::inst_userrules($dom,'username');
 9470:         my %lt = &usercreation_types();
 9471:         my %checked;
 9472:         if (ref($settings) eq 'HASH') {
 9473:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 9474:                 foreach my $item (@creators) {
 9475:                     $checked{$item} = $settings->{'cancreate'}{$item};
 9476:                 }
 9477:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 9478:                 foreach my $item (@creators) {
 9479:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 9480:                         $checked{$item} = 'none';
 9481:                     }
 9482:                 }
 9483:             }
 9484:         }
 9485:         my $rownum = 0;
 9486:         foreach my $item (@creators) {
 9487:             $rownum ++;
 9488:             if ($checked{$item} eq '') {
 9489:                 $checked{$item} = 'any';
 9490:             }
 9491:             my $css_class;
 9492:             if ($rownum%2) {
 9493:                 $css_class = '';
 9494:             } else {
 9495:                 $css_class = ' class="LC_odd_row" ';
 9496:             }
 9497:             $datatable .= '<tr'.$css_class.'>'.
 9498:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 9499:                          '</span></td><td style="text-align: right">';
 9500:             my @options = ('any');
 9501:             if (ref($rules) eq 'HASH') {
 9502:                 if (keys(%{$rules}) > 0) {
 9503:                     push(@options,('official','unofficial'));
 9504:                 }
 9505:             }
 9506:             push(@options,'none');
 9507:             foreach my $option (@options) {
 9508:                 my $type = 'radio';
 9509:                 my $check = ' ';
 9510:                 if ($checked{$item} eq $option) {
 9511:                     $check = ' checked="checked" ';
 9512:                 } 
 9513:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9514:                               '<input type="'.$type.'" name="can_createuser_'.
 9515:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 9516:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 9517:             }
 9518:             $datatable .= '</td></tr>';
 9519:         }
 9520:     } else {
 9521:         my @contexts = ('author','course','domain');
 9522:         my @authtypes = ('int','krb4','krb5','loc','lti');
 9523:         my %checked;
 9524:         if (ref($settings) eq 'HASH') {
 9525:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 9526:                 foreach my $item (@contexts) {
 9527:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 9528:                         foreach my $auth (@authtypes) {
 9529:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 9530:                                 $checked{$item}{$auth} = ' checked="checked" ';
 9531:                             }
 9532:                         }
 9533:                     }
 9534:                 }
 9535:             }
 9536:         } else {
 9537:             foreach my $item (@contexts) {
 9538:                 foreach my $auth (@authtypes) {
 9539:                     $checked{$item}{$auth} = ' checked="checked" ';
 9540:                 }
 9541:             }
 9542:         }
 9543:         my %title = &context_names();
 9544:         my %authname = &authtype_names();
 9545:         my $rownum = 0;
 9546:         my $css_class; 
 9547:         foreach my $item (@contexts) {
 9548:             if ($rownum%2) {
 9549:                 $css_class = '';
 9550:             } else {
 9551:                 $css_class = ' class="LC_odd_row" ';
 9552:             }
 9553:             $datatable .=   '<tr'.$css_class.'>'.
 9554:                             '<td>'.$title{$item}.
 9555:                             '</td><td class="LC_left_item">'.
 9556:                             '<span class="LC_nobreak">';
 9557:             foreach my $auth (@authtypes) {
 9558:                 $datatable .= '<label>'. 
 9559:                               '<input type="checkbox" name="'.$item.'_auth" '.
 9560:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 9561:                               $authname{$auth}.'</label>&nbsp;';
 9562:             }
 9563:             $datatable .= '</span></td></tr>';
 9564:             $rownum ++;
 9565:         }
 9566:         $$rowtotal += $rownum;
 9567:     }
 9568:     return $datatable;
 9569: }
 9570: 
 9571: sub print_selfcreation {
 9572:     my ($position,$dom,$settings,$rowtotal) = @_;
 9573:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 9574:         $emaildomain,$datatable);
 9575:     if (ref($settings) eq 'HASH') {
 9576:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 9577:             $createsettings = $settings->{'cancreate'};
 9578:             if (ref($createsettings) eq 'HASH') {
 9579:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 9580:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 9581:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 9582:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 9583:                         @selfcreate = ('email','login','sso');
 9584:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 9585:                         @selfcreate = ($createsettings->{'selfcreate'});
 9586:                     }
 9587:                 }
 9588:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 9589:                     $processing = $createsettings->{'selfcreateprocessing'};
 9590:                 }
 9591:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 9592:                     $emailoptions = $createsettings->{'emailoptions'};
 9593:                 }
 9594:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 9595:                     $emailverified = $createsettings->{'emailverified'};
 9596:                 }
 9597:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 9598:                     $emaildomain = $createsettings->{'emaildomain'};
 9599:                 }
 9600:             }
 9601:         }
 9602:     }
 9603:     my %radiohash;
 9604:     my $numinrow = 4;
 9605:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 9606:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9607:     if ($position eq 'top') {
 9608:         my %choices = &Apache::lonlocal::texthash (
 9609:                                                       cancreate_login      => 'Institutional Login',
 9610:                                                       cancreate_sso        => 'Institutional Single Sign On',
 9611:                                                   );
 9612:         my @toggles = sort(keys(%choices));
 9613:         my %defaultchecked = (
 9614:                                'cancreate_login' => 'off',
 9615:                                'cancreate_sso'   => 'off',
 9616:                              );
 9617:         my ($onclick,$itemcount);
 9618:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 9619:                                                      \%choices,$itemcount,$onclick);
 9620:         $$rowtotal += $itemcount;
 9621: 
 9622:         if (ref($usertypes) eq 'HASH') {
 9623:             if (keys(%{$usertypes}) > 0) {
 9624:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 9625:                                              $dom,$numinrow,$othertitle,
 9626:                                              'statustocreate',$rowtotal);
 9627:                 $$rowtotal ++;
 9628:             }
 9629:         }
 9630:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 9631:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9632:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 9633:         my $rem;
 9634:         my $numperrow = 2;
 9635:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 9636:         $datatable .= '<tr'.$css_class.'>'.
 9637:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 9638:                      '<td class="LC_left_item">'."\n".
 9639:                      '<table>'."\n";
 9640:         for (my $i=0; $i<@fields; $i++) {
 9641:             $rem = $i%($numperrow);
 9642:             if ($rem == 0) {
 9643:                 if ($i > 0) {
 9644:                     $datatable .= '</tr>';
 9645:                 }
 9646:                 $datatable .= '<tr>';
 9647:             }
 9648:             my $currval;
 9649:             if (ref($createsettings) eq 'HASH') {
 9650:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 9651:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 9652:                 }
 9653:             }
 9654:             $datatable .= '<td class="LC_left_item">'.
 9655:                           '<span class="LC_nobreak">'.
 9656:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 9657:                           'value="'.$currval.'" size="10" />&nbsp;'.
 9658:                           $fieldtitles{$fields[$i]}.'</span></td>';
 9659:         }
 9660:         my $colsleft = $numperrow - $rem;
 9661:         if ($colsleft > 1 ) {
 9662:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9663:                          '&nbsp;</td>';
 9664:         } elsif ($colsleft == 1) {
 9665:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 9666:         }
 9667:         $datatable .= '</tr></table></td></tr>';
 9668:         $$rowtotal ++;
 9669:     } elsif ($position eq 'middle') {
 9670:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 9671:         my @posstypes;
 9672:         if (ref($types) eq 'ARRAY') {
 9673:             @posstypes = @{$types};
 9674:         }
 9675:         unless (grep(/^default$/,@posstypes)) {
 9676:             push(@posstypes,'default');
 9677:         }
 9678:         my %usertypeshash;
 9679:         if (ref($usertypes) eq 'HASH') {
 9680:             %usertypeshash = %{$usertypes};
 9681:         }
 9682:         $usertypeshash{'default'} = $othertitle;
 9683:         foreach my $status (@posstypes) {
 9684:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 9685:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 9686:             $$rowtotal ++;
 9687:         }
 9688:     } else {
 9689:         my %choices = &Apache::lonlocal::texthash (
 9690:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 9691:                                                   );
 9692:         my @toggles = sort(keys(%choices));
 9693:         my %defaultchecked = (
 9694:                                'cancreate_email' => 'off',
 9695:                              );
 9696:         my $customclass = 'LC_selfcreate_email';
 9697:         my $classprefix = 'LC_canmodify_emailusername_';
 9698:         my $optionsprefix = 'LC_options_emailusername_';
 9699:         my $display = 'none';
 9700:         my $rowstyle = 'display:none';
 9701:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 9702:             $display = 'block';
 9703:             $rowstyle = 'display:table-row';
 9704:         }
 9705:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 9706:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 9707:                                                      \%choices,$$rowtotal,$onclick);
 9708:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 9709:                                          $rowstyle);
 9710:         $$rowtotal ++;
 9711:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 9712:                                       $rowstyle);
 9713:         $$rowtotal ++;
 9714:         my (@ordered,@posstypes,%usertypeshash);
 9715:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 9716:         my ($emailrules,$emailruleorder) =
 9717:             &Apache::lonnet::inst_userrules($dom,'email');
 9718:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9719:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9720:         if (ref($types) eq 'ARRAY') {
 9721:             @posstypes = @{$types};
 9722:         }
 9723:         if (@posstypes) {
 9724:             unless (grep(/^default$/,@posstypes)) {
 9725:                 push(@posstypes,'default');
 9726:             }
 9727:             if (ref($usertypes) eq 'HASH') {
 9728:                 %usertypeshash = %{$usertypes};
 9729:             }
 9730:             my $currassign;
 9731:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 9732:                 $currassign = {
 9733:                                   selfassign => $domdefaults{'inststatusguest'},
 9734:                               };
 9735:                 @ordered = @{$domdefaults{'inststatusguest'}};
 9736:             } else {
 9737:                 $currassign = { selfassign => [] };
 9738:             }
 9739:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 9740:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 9741:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 9742:                                          $numinrow,$othertitle,'selfassign',
 9743:                                          $rowtotal,$onclicktypes,$customclass,
 9744:                                          $rowstyle);
 9745:             $$rowtotal ++;
 9746:             $usertypeshash{'default'} = $othertitle;
 9747:             foreach my $status (@posstypes) {
 9748:                 my $css_class;
 9749:                 if ($$rowtotal%2) {
 9750:                     $css_class = 'LC_odd_row ';
 9751:                 }
 9752:                 $css_class .= $customclass;
 9753:                 my $rowid = $optionsprefix.$status;
 9754:                 my $hidden = 1;
 9755:                 my $currstyle = 'display:none';
 9756:                 if (grep(/^\Q$status\E$/,@ordered)) {
 9757:                     $currstyle = $rowstyle;
 9758:                     $hidden = 0;
 9759:                 }
 9760:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 9761:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 9762:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 9763:                 unless ($hidden) {
 9764:                     $$rowtotal ++;
 9765:                 }
 9766:             }
 9767:         } else {
 9768:             my $css_class;
 9769:             if ($$rowtotal%2) {
 9770:                 $css_class = 'LC_odd_row ';
 9771:             }
 9772:             $css_class .= $customclass;
 9773:             $usertypeshash{'default'} = $othertitle;
 9774:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 9775:                                          $emailrules,$emailruleorder,$settings,'default','',
 9776:                                          $othertitle,$css_class,$rowstyle,$intdom);
 9777:             $$rowtotal ++;
 9778:         }
 9779:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 9780:         $numinrow = 1;
 9781:         if (@posstypes) {
 9782:             foreach my $status (@posstypes) {
 9783:                 my $rowid = $classprefix.$status;
 9784:                 my $datarowstyle = 'display:none';
 9785:                 if (grep(/^\Q$status\E$/,@ordered)) {
 9786:                     $datarowstyle = $rowstyle;
 9787:                 }
 9788:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 9789:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 9790:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 9791:                 unless ($datarowstyle eq 'display:none') {
 9792:                     $$rowtotal ++;
 9793:                 }
 9794:             }
 9795:         } else {
 9796:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 9797:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 9798:                                                    $infotitles,'',$customclass,$rowstyle);
 9799:         }
 9800:     }
 9801:     return $datatable;
 9802: }
 9803: 
 9804: sub selfcreate_javascript {
 9805:     return <<"ENDSCRIPT";
 9806: 
 9807: <script type="text/javascript">
 9808: // <![CDATA[
 9809: 
 9810: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 9811:     var x = document.getElementsByClassName(target);
 9812:     var insttypes = 0;
 9813:     var insttypeRegExp = new RegExp(prefix);
 9814:     if ((x.length != undefined) && (x.length > 0)) {
 9815:         if (form.elements[radio].length != undefined) {
 9816:             for (var i=0; i<form.elements[radio].length; i++) {
 9817:                 if (form.elements[radio][i].checked) {
 9818:                     if (form.elements[radio][i].value == 1) {
 9819:                         for (var j=0; j<x.length; j++) {
 9820:                             if (x[j].id == 'undefined') {
 9821:                                 x[j].style.display = 'table-row';
 9822:                             } else if (insttypeRegExp.test(x[j].id)) {
 9823:                                 insttypes ++;
 9824:                             } else {
 9825:                                 x[j].style.display = 'table-row';
 9826:                             }
 9827:                         }
 9828:                     } else {
 9829:                         for (var j=0; j<x.length; j++) {
 9830:                             x[j].style.display = 'none';
 9831:                         }
 9832:                     }
 9833:                     break;
 9834:                 }
 9835:             }
 9836:             if (insttypes > 0) {
 9837:                 toggleDataRow(form,checkbox,target,altprefix);
 9838:                 toggleDataRow(form,checkbox,target,prefix,1);
 9839:             }
 9840:         }
 9841:     }
 9842:     return;
 9843: }
 9844: 
 9845: function toggleDataRow(form,checkbox,target,prefix,docount) {
 9846:     if (form.elements[checkbox].length != undefined) {
 9847:         var count = 0;
 9848:         if (docount) {
 9849:             for (var i=0; i<form.elements[checkbox].length; i++) {
 9850:                 if (form.elements[checkbox][i].checked) {
 9851:                     count ++;
 9852:                 }
 9853:             }
 9854:         }
 9855:         for (var i=0; i<form.elements[checkbox].length; i++) {
 9856:             var type = form.elements[checkbox][i].value;
 9857:             if (document.getElementById(prefix+type)) {
 9858:                 if (form.elements[checkbox][i].checked) {
 9859:                     document.getElementById(prefix+type).style.display = 'table-row';
 9860:                     if (count % 2 == 1) {
 9861:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 9862:                     } else {
 9863:                         document.getElementById(prefix+type).className = target;
 9864:                     }
 9865:                     count ++;
 9866:                 } else {
 9867:                     document.getElementById(prefix+type).style.display = 'none';
 9868:                 }
 9869:             }
 9870:         }
 9871:     }
 9872:     return;
 9873: }
 9874: 
 9875: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 9876:     var caller = radio+'_'+status;
 9877:     if (form.elements[caller].length != undefined) {
 9878:         for (var i=0; i<form.elements[caller].length; i++) {
 9879:             if (form.elements[caller][i].checked) {
 9880:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 9881:                     var curr = form.elements[caller][i].value;
 9882:                     if (prefix) {
 9883:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 9884:                     }
 9885:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 9886:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 9887:                     if (curr == 'custom') {
 9888:                         if (prefix) {
 9889:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 9890:                         }
 9891:                     } else if (curr == 'inst') {
 9892:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 9893:                     } else if (curr == 'noninst') {
 9894:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 9895:                     }
 9896:                     break;
 9897:                 }
 9898:             }
 9899:         }
 9900:     }
 9901: }
 9902: 
 9903: // ]]>
 9904: </script>
 9905: 
 9906: ENDSCRIPT
 9907: }
 9908: 
 9909: sub noninst_users {
 9910:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 9911:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
 9912:     my $class = 'LC_left_item';
 9913:     if ($css_class) {
 9914:         $css_class = ' class="'.$css_class.'"';
 9915:     }
 9916:     if ($rowid) {
 9917:         $rowid = ' id="'.$rowid.'"';
 9918:     }
 9919:     if ($rowstyle) {
 9920:         $rowstyle = ' style="'.$rowstyle.'"';
 9921:     }
 9922:     my ($output,$description);
 9923:     if ($type eq 'default') {
 9924:         $description = &mt('Requests for: [_1]',$typetitle);
 9925:     } else {
 9926:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 9927:     }
 9928:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 9929:               "<td>$description</td>\n".
 9930:               '<td class="'.$class.'" colspan="2">'.
 9931:               '<table><tr>';
 9932:     my %headers = &Apache::lonlocal::texthash(
 9933:               approve  => 'Processing',
 9934:               email    => 'E-mail',
 9935:               username => 'Username',
 9936:     );
 9937:     foreach my $item ('approve','email','username') {
 9938:         $output .= '<th>'.$headers{$item}.'</th>';
 9939:     }
 9940:     $output .= '</tr><tr>';
 9941:     foreach my $item ('approve','email','username') {
 9942:         $output .= '<td style="vertical-align: top">';
 9943:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 9944:         if ($item eq 'approve') {
 9945:             %choices = &Apache::lonlocal::texthash (
 9946:                                                      automatic => 'Automatically approved',
 9947:                                                      approval  => 'Queued for approval',
 9948:                                                    );
 9949:             @options = ('automatic','approval');
 9950:             $hashref = $processing;
 9951:             $defoption = 'automatic';
 9952:             $name = 'cancreate_emailprocess_'.$type;
 9953:         } elsif ($item eq 'email') {
 9954:             %choices = &Apache::lonlocal::texthash (
 9955:                                                      any     => 'Any e-mail',
 9956:                                                      inst    => 'Institutional only',
 9957:                                                      noninst => 'Non-institutional only',
 9958:                                                      custom  => 'Custom restrictions',
 9959:                                                    );
 9960:             @options = ('any','inst','noninst');
 9961:             my $showcustom;
 9962:             if (ref($emailrules) eq 'HASH') {
 9963:                 if (keys(%{$emailrules}) > 0) {
 9964:                     push(@options,'custom');
 9965:                     $showcustom = 'cancreate_emailrule';
 9966:                     if (ref($settings) eq 'HASH') {
 9967:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 9968:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 9969:                                 if (exists($emailrules->{$rule})) {
 9970:                                     $hascustom ++;
 9971:                                 }
 9972:                             }
 9973:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 9974:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 9975:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 9976:                                     if (exists($emailrules->{$rule})) {
 9977:                                         $hascustom ++;
 9978:                                     }
 9979:                                 }
 9980:                             }
 9981:                         }
 9982:                     }
 9983:                 }
 9984:             }
 9985:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 9986:                                                      "'cancreate_emaildomain','$type'".');"';
 9987:             $hashref = $emailoptions;
 9988:             $defoption = 'any';
 9989:             $name = 'cancreate_emailoptions_'.$type;
 9990:         } elsif ($item eq 'username') {
 9991:             %choices = &Apache::lonlocal::texthash (
 9992:                                                      all    => 'Same as e-mail',
 9993:                                                      first  => 'Omit @domain',
 9994:                                                      free   => 'Free to choose',
 9995:                                                    );
 9996:             @options = ('all','first','free');
 9997:             $hashref = $emailverified;
 9998:             $defoption = 'all';
 9999:             $name = 'cancreate_usernameoptions_'.$type;
10000:         }
10001:         foreach my $option (@options) {
10002:             my $checked;
10003:             if (ref($hashref) eq 'HASH') {
10004:                 if ($type eq '') {
10005:                     if (!exists($hashref->{'default'})) {
10006:                         if ($option eq $defoption) {
10007:                             $checked = ' checked="checked"';
10008:                         }
10009:                     } else {
10010:                         if ($hashref->{'default'} eq $option) {
10011:                             $checked = ' checked="checked"';
10012:                         }
10013:                     }
10014:                 } else {
10015:                     if (!exists($hashref->{$type})) {
10016:                         if ($option eq $defoption) {
10017:                             $checked = ' checked="checked"';
10018:                         }
10019:                     } else {
10020:                         if ($hashref->{$type} eq $option) {
10021:                             $checked = ' checked="checked"';
10022:                         }
10023:                     }
10024:                 }
10025:             } elsif (($item eq 'email') && ($hascustom)) {
10026:                 if ($option eq 'custom') {
10027:                     $checked = ' checked="checked"';
10028:                 }
10029:             } elsif ($option eq $defoption) {
10030:                 $checked = ' checked="checked"';
10031:             }
10032:             $output .= '<span class="LC_nobreak"><label>'.
10033:                        '<input type="radio" name="'.$name.'"'.
10034:                        $checked.' value="'.$option.'"'.$onclick.' />'.
10035:                        $choices{$option}.'</label></span><br />';
10036:             if ($item eq 'email') {
10037:                 if ($option eq 'custom') {
10038:                     my $id = 'cancreate_emailrule_'.$type;
10039:                     my $display = 'none';
10040:                     if ($checked) {
10041:                         $display = 'inline';
10042:                     }
10043:                     my $numinrow = 2;
10044:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
10045:                                '<legend>'.&mt('Disallow').'</legend><table>'.
10046:                                &user_formats_row('email',$settings,$emailrules,
10047:                                                  $emailruleorder,$numinrow,'',$type);
10048:                               '</table></fieldset>';
10049:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
10050:                     my %text = &Apache::lonlocal::texthash (
10051:                                                              inst    => 'must end:',
10052:                                                              noninst => 'cannot end:',
10053:                                                            );
10054:                     my $value;
10055:                     if (ref($emaildomain) eq 'HASH') {
10056:                         if (ref($emaildomain->{$type}) eq 'HASH') {
10057:                             $value = $emaildomain->{$type}->{$option};
10058:                         }
10059:                     }
10060:                     if ($value eq '') {
10061:                         $value = '@'.$intdom;
10062:                     }
10063:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
10064:                     my $display = 'none';
10065:                     if ($checked) {
10066:                         $display = 'inline';
10067:                     }
10068:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
10069:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
10070:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
10071:                                '</div>';
10072:                 }
10073:             }
10074:         }
10075:         $output .= '</td>'."\n";
10076:     }
10077:     $output .= "</tr></table></td></tr>\n";
10078:     return $output;
10079: }
10080: 
10081: sub captcha_choice {
10082:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
10083:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
10084:         $vertext,$currver);
10085:     my %lt = &captcha_phrases();
10086:     $keyentry = 'hidden';
10087:     my $colspan=2;
10088:     if ($context eq 'cancreate') {
10089:         $rowname = &mt('CAPTCHA validation');
10090:     } elsif ($context eq 'login') {
10091:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
10092:     } elsif ($context eq 'passwords') {
10093:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
10094:         $colspan=1;
10095:     }
10096:     if (ref($settings) eq 'HASH') {
10097:         if ($settings->{'captcha'}) {
10098:             $checked{$settings->{'captcha'}} = ' checked="checked"';
10099:         } else {
10100:             $checked{'original'} = ' checked="checked"';
10101:         }
10102:         if ($settings->{'captcha'} eq 'recaptcha') {
10103:             $pubtext = $lt{'pub'};
10104:             $privtext = $lt{'priv'};
10105:             $keyentry = 'text';
10106:             $vertext = $lt{'ver'};
10107:             $currver = $settings->{'recaptchaversion'};
10108:             if ($currver ne '2') {
10109:                 $currver = 1;
10110:             }
10111:         }
10112:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
10113:             $currpub = $settings->{'recaptchakeys'}{'public'};
10114:             $currpriv = $settings->{'recaptchakeys'}{'private'};
10115:         }
10116:     } else {
10117:         $checked{'original'} = ' checked="checked"';
10118:     }
10119:     my $css_class;
10120:     if ($itemcount%2) {
10121:         $css_class = 'LC_odd_row';
10122:     }
10123:     if ($customcss) {
10124:         $css_class .= " $customcss";
10125:     }
10126:     $css_class =~ s/^\s+//;
10127:     if ($css_class) {
10128:         $css_class = ' class="'.$css_class.'"';
10129:     }
10130:     if ($rowstyle) {
10131:         $css_class .= ' style="'.$rowstyle.'"';
10132:     }
10133:     my $output = '<tr'.$css_class.'>'.
10134:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
10135:                  '<table><tr><td>'."\n";
10136:     foreach my $option ('original','recaptcha','notused') {
10137:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
10138:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
10139:                    $lt{$option}.'</label></span>';
10140:         unless ($option eq 'notused') {
10141:             $output .= ('&nbsp;'x2)."\n";
10142:         }
10143:     }
10144: #
10145: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
10146: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
10147: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
10148: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
10149: #
10150:     $output .= '</td></tr>'."\n".
10151:                '<tr><td class="LC_zero_height">'."\n".
10152:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
10153:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
10154:                $currpub.'" size="40" /></span><br />'."\n".
10155:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
10156:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
10157:                $currpriv.'" size="40" /></span><br />'.
10158:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
10159:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
10160:                $currver.'" size="3" /></span><br />'.
10161:                '</td></tr></table>'."\n".
10162:                '</td></tr>';
10163:     return $output;
10164: }
10165: 
10166: sub user_formats_row {
10167:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
10168:     my $output;
10169:     my %text = (
10170:                    'username' => 'new usernames',
10171:                    'id'       => 'IDs',
10172:                );
10173:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10174:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
10175:         $output = '<tr '.$css_class.'>'.
10176:                   '<td><span class="LC_nobreak">'.
10177:                   &mt("Format rules to check for $text{$type}: ").
10178:                   '</td><td class="LC_left_item" colspan="2"><table>';
10179:     }
10180:     my $rem;
10181:     if (ref($ruleorder) eq 'ARRAY') {
10182:         for (my $i=0; $i<@{$ruleorder}; $i++) {
10183:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
10184:                 my $rem = $i%($numinrow);
10185:                 if ($rem == 0) {
10186:                     if ($i > 0) {
10187:                         $output .= '</tr>';
10188:                     }
10189:                     $output .= '<tr>';
10190:                 }
10191:                 my $check = ' ';
10192:                 if (ref($settings) eq 'HASH') {
10193:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
10194:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
10195:                             $check = ' checked="checked" ';
10196:                         }
10197:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
10198:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
10199:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
10200:                                 $check = ' checked="checked" ';
10201:                             }
10202:                         }
10203:                     }
10204:                 }
10205:                 my $name = $type.'_rule';
10206:                 if ($type eq 'email') {
10207:                     $name .= '_'.$status;
10208:                 }
10209:                 $output .= '<td class="LC_left_item">'.
10210:                            '<span class="LC_nobreak"><label>'.
10211:                            '<input type="checkbox" name="'.$name.'" '.
10212:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
10213:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
10214:             }
10215:         }
10216:         $rem = @{$ruleorder}%($numinrow);
10217:     }
10218:     my $colsleft;
10219:     if ($rem) {
10220:         $colsleft = $numinrow - $rem;
10221:     }
10222:     if ($colsleft > 1 ) {
10223:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10224:                    '&nbsp;</td>';
10225:     } elsif ($colsleft == 1) {
10226:         $output .= '<td class="LC_left_item">&nbsp;</td>';
10227:     }
10228:     $output .= '</tr>';
10229:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10230:         $output .= '</table></td></tr>';
10231:     }
10232:     return $output;
10233: }
10234: 
10235: sub usercreation_types {
10236:     my %lt = &Apache::lonlocal::texthash (
10237:                     author     => 'When adding a co-author',
10238:                     course     => 'When adding a user to a course',
10239:                     requestcrs => 'When requesting a course',
10240:                     any        => 'Any',
10241:                     official   => 'Institutional only ',
10242:                     unofficial => 'Non-institutional only',
10243:                     none       => 'None',
10244:     );
10245:     return %lt;
10246: }
10247: 
10248: sub selfcreation_types {
10249:     my %lt = &Apache::lonlocal::texthash (
10250:                     selfcreate => 'User creates own account',
10251:                     any        => 'Any',
10252:                     official   => 'Institutional only ',
10253:                     unofficial => 'Non-institutional only',
10254:                     email      => 'E-mail address',
10255:                     login      => 'Institutional Login',
10256:                     sso        => 'SSO',
10257:              );
10258: }
10259: 
10260: sub authtype_names {
10261:     my %lt = &Apache::lonlocal::texthash(
10262:                       int    => 'Internal',
10263:                       krb4   => 'Kerberos 4',
10264:                       krb5   => 'Kerberos 5',
10265:                       loc    => 'Local',
10266:                       lti    => 'LTI',
10267:                   );
10268:     return %lt;
10269: }
10270: 
10271: sub context_names {
10272:     my %context_title = &Apache::lonlocal::texthash(
10273:        author => 'Creating users when an Author',
10274:        course => 'Creating users when in a course',
10275:        domain => 'Creating users when a Domain Coordinator',
10276:     );
10277:     return %context_title;
10278: }
10279: 
10280: sub print_usermodification {
10281:     my ($position,$dom,$settings,$rowtotal) = @_;
10282:     my $numinrow = 4;
10283:     my ($context,$datatable,$rowcount);
10284:     if ($position eq 'top') {
10285:         $rowcount = 0;
10286:         $context = 'author'; 
10287:         foreach my $role ('ca','aa') {
10288:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10289:                                                    $numinrow,$rowcount);
10290:             $$rowtotal ++;
10291:             $rowcount ++;
10292:         }
10293:     } elsif ($position eq 'bottom') {
10294:         $context = 'course';
10295:         $rowcount = 0;
10296:         foreach my $role ('st','ep','ta','in','cr') {
10297:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10298:                                                    $numinrow,$rowcount);
10299:             $$rowtotal ++;
10300:             $rowcount ++;
10301:         }
10302:     }
10303:     return $datatable;
10304: }
10305: 
10306: sub print_defaults {
10307:     my ($position,$dom,$settings,$rowtotal) = @_;
10308:     my $rownum = 0;
10309:     my ($datatable,$css_class,$titles);
10310:     unless ($position eq 'bottom') {
10311:         $titles = &defaults_titles($dom);
10312:     }
10313:     if ($position eq 'top') {
10314:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
10315:                      'datelocale_def','portal_def');
10316:         my %defaults;
10317:         if (ref($settings) eq 'HASH') {
10318:             %defaults = %{$settings};
10319:         } else {
10320:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10321:             foreach my $item (@items) {
10322:                 $defaults{$item} = $domdefaults{$item};
10323:             }
10324:         }
10325:         foreach my $item (@items) {
10326:             if ($rownum%2) {
10327:                 $css_class = '';
10328:             } else {
10329:                 $css_class = ' class="LC_odd_row" ';
10330:             }
10331:             $datatable .= '<tr'.$css_class.'>'.
10332:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
10333:                           '</span></td><td class="LC_right_item" colspan="3">';
10334:             if ($item eq 'auth_def') {
10335:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
10336:                 my %shortauth = (
10337:                                  internal => 'int',
10338:                                  krb4 => 'krb4',
10339:                                  krb5 => 'krb5',
10340:                                  localauth  => 'loc',
10341:                                  lti => 'lti',
10342:                                 );
10343:                 my %authnames = &authtype_names();
10344:                 foreach my $auth (@authtypes) {
10345:                     my $checked = ' ';
10346:                     if ($defaults{$item} eq $auth) {
10347:                         $checked = ' checked="checked" ';
10348:                     }
10349:                     $datatable .= '<label><input type="radio" name="'.$item.
10350:                                   '" value="'.$auth.'"'.$checked.'/>'.
10351:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
10352:                 }
10353:             } elsif ($item eq 'timezone_def') {
10354:                 my $includeempty = 1;
10355:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
10356:             } elsif ($item eq 'datelocale_def') {
10357:                 my $includeempty = 1;
10358:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
10359:             } elsif ($item eq 'lang_def') {
10360:                 my $includeempty = 1;
10361:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
10362:             } elsif ($item eq 'portal_def') {
10363:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
10364:                               $defaults{$item}.'" size="25" onkeyup="portalExtras(this);" />';
10365:                 my $portalsty = 'none';
10366:                 if ($defaults{$item}) {
10367:                     $portalsty = 'block';
10368:                 }
10369:                 foreach my $field ('email','web') {
10370:                     my $checkedoff = ' checked="checked"';
10371:                     my $checkedon;
10372:                     if ($defaults{$item.'_'.$field}) {
10373:                         $checkedon = $checkedoff;
10374:                         $checkedoff = '';
10375:                     }
10376:                     $datatable .= '<div id="'.$item.'_'.$field.'_div" style="display:'.$portalsty.'">'.
10377:                               '<span class="LC_nobreak">'.$titles->{$field}.'&nbsp;'.
10378:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="1"'.$checkedon.'/>'.&mt('Yes').'</label>'.
10379:                               ('&nbsp;'x2).
10380:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="0"'.$checkedoff.'/>'.&mt('No').'</label>'.
10381:                               '</div>';
10382:                 }
10383:             } else {
10384:                 $datatable .= '<input type="text" name="'.$item.'" value="'.$defaults{$item}.'" />';
10385:             }
10386:             $datatable .= '</td></tr>';
10387:             $rownum ++;
10388:         }
10389:     } elsif ($position eq 'middle') {
10390:         my %defaults;
10391:         if (ref($settings) eq 'HASH') {
10392:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10393:                 my $maxnum = @{$settings->{'inststatusorder'}};
10394:                 for (my $i=0; $i<$maxnum; $i++) {
10395:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
10396:                     my $item = $settings->{'inststatusorder'}->[$i];
10397:                     my $title = $settings->{'inststatustypes'}->{$item};
10398:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
10399:                     $datatable .= '<tr'.$css_class.'>'.
10400:                                   '<td><span class="LC_nobreak">'.
10401:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
10402:                     for (my $k=0; $k<=$maxnum; $k++) {
10403:                         my $vpos = $k+1;
10404:                         my $selstr;
10405:                         if ($k == $i) {
10406:                             $selstr = ' selected="selected" ';
10407:                         }
10408:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10409:                     }
10410:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
10411:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
10412:                                   &mt('delete').'</span></td>'.
10413:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed').':'.
10414:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
10415:                                   '</span></td></tr>';
10416:                 }
10417:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
10418:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
10419:                 $datatable .= '<tr '.$css_class.'>'.
10420:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
10421:                 for (my $k=0; $k<=$maxnum; $k++) {
10422:                     my $vpos = $k+1;
10423:                     my $selstr;
10424:                     if ($k == $maxnum) {
10425:                         $selstr = ' selected="selected" ';
10426:                     }
10427:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10428:                 }
10429:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
10430:                               '<input type="text" size="10" name="addinststatus" value="" />'.
10431:                               '&nbsp;'.&mt('(new)').
10432:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
10433:                               &mt('Name displayed').':'.
10434:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
10435:                               '</tr>'."\n";
10436:                 $rownum ++;
10437:             }
10438:         }
10439:     } else {
10440:         my ($unamemaprules,$ruleorder) =
10441:             &Apache::lonnet::inst_userrules($dom,'unamemap');
10442:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10443:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
10444:             my $numinrow = 2;
10445:             $datatable .= '<tr'.$css_class.'><td>'.&mt('Available conversions').'</td><td><table>'.
10446:                           &user_formats_row('unamemap',$settings,$unamemaprules,
10447:                                             $ruleorder,$numinrow).
10448:                           '</table></td></tr>';
10449:         }
10450:         if ($datatable eq '') {
10451:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
10452:                           &mt('No rules set for domain in customized localenroll.pm').
10453:                           '</td></tr>';
10454:         }
10455:     }
10456:     $$rowtotal += $rownum;
10457:     return $datatable;
10458: }
10459: 
10460: sub get_languages_hash {
10461:     my %langchoices;
10462:     foreach my $id (&Apache::loncommon::languageids()) {
10463:         my $code = &Apache::loncommon::supportedlanguagecode($id);
10464:         if ($code ne '') {
10465:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
10466:         }
10467:     }
10468:     return %langchoices;
10469: }
10470: 
10471: sub defaults_titles {
10472:     my ($dom) = @_;
10473:     my %titles = &Apache::lonlocal::texthash (
10474:                    'auth_def'      => 'Default authentication type',
10475:                    'auth_arg_def'  => 'Default authentication argument',
10476:                    'lang_def'      => 'Default language',
10477:                    'timezone_def'  => 'Default timezone',
10478:                    'datelocale_def' => 'Default locale for dates',
10479:                    'portal_def'     => 'Portal/Default URL',
10480:                    'email'          => 'Email links use portal URL',
10481:                    'web'            => 'Public web links use portal URL',
10482:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
10483:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
10484:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
10485:                  );
10486:     if ($dom) {
10487:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
10488:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
10489:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
10490:         $protocol = 'http' if ($protocol ne 'https');
10491:         if ($uint_dom) {
10492:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
10493:                                          $uint_dom);
10494:         }
10495:     }
10496:     return (\%titles);
10497: }
10498: 
10499: sub print_scantron {
10500:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
10501:     if ($position eq 'top') {
10502:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
10503:     } else {
10504:         return &print_scantronconfig($dom,$settings,\$rowtotal);
10505:     }
10506: }
10507: 
10508: sub scantron_javascript {
10509:     return <<"ENDSCRIPT";
10510: 
10511: <script type="text/javascript">
10512: // <![CDATA[
10513: 
10514: function toggleScantron(form) {
10515:     var csvfieldset = new Array();
10516:     if (document.getElementById('scantroncsv_cols')) {
10517:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
10518:     }
10519:     if (document.getElementById('scantroncsv_options')) {
10520:         csvfieldset.push(document.getElementById('scantroncsv_options'));
10521:     }
10522:     if (csvfieldset.length) {
10523:         if (document.getElementById('scantronconfcsv')) {
10524:             var scantroncsv = document.getElementById('scantronconfcsv');
10525:             if (scantroncsv.checked) {
10526:                 for (var i=0; i<csvfieldset.length; i++) {
10527:                     csvfieldset[i].style.display = 'block';
10528:                 }
10529:             } else {
10530:                 for (var i=0; i<csvfieldset.length; i++) {
10531:                     csvfieldset[i].style.display = 'none';
10532:                 }
10533:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
10534:                 if (csvselects.length) {
10535:                     for (var j=0; j<csvselects.length; j++) {
10536:                         csvselects[j].selectedIndex = 0;
10537:                     }
10538:                 }
10539:             }
10540:         }
10541:     }
10542:     return;
10543: }
10544: // ]]>
10545: </script>
10546: 
10547: ENDSCRIPT
10548: 
10549: }
10550: 
10551: sub print_scantronformat {
10552:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
10553:     my $itemcount = 1;
10554:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
10555:         %confhash);
10556:     my $switchserver = &check_switchserver($dom,$confname);
10557:     my %lt = &Apache::lonlocal::texthash (
10558:                 default => 'Default bubblesheet format file error',
10559:                 custom  => 'Custom bubblesheet format file error',
10560:              );
10561:     my %scantronfiles = (
10562:         default => 'default.tab',
10563:         custom => 'custom.tab',
10564:     );
10565:     foreach my $key (keys(%scantronfiles)) {
10566:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
10567:                               .$scantronfiles{$key};
10568:     }
10569:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
10570:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
10571:         if (!$switchserver) {
10572:             my $servadm = $r->dir_config('lonAdmEMail');
10573:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
10574:             if ($configuserok eq 'ok') {
10575:                 if ($author_ok eq 'ok') {
10576:                     my %legacyfile = (
10577:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
10578:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
10579:                     );
10580:                     my %md5chk;
10581:                     foreach my $type (keys(%legacyfile)) {
10582:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
10583:                         chomp($md5chk{$type});
10584:                     }
10585:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
10586:                         foreach my $type (keys(%legacyfile)) {
10587:                             ($scantronurls{$type},my $error) =
10588:                                 &legacy_scantronformat($r,$dom,$confname,
10589:                                                  $type,$legacyfile{$type},
10590:                                                  $scantronurls{$type},
10591:                                                  $scantronfiles{$type});
10592:                             if ($error ne '') {
10593:                                 $error{$type} = $error;
10594:                             }
10595:                         }
10596:                         if (keys(%error) == 0) {
10597:                             $is_custom = 1;
10598:                             $confhash{'scantron'}{'scantronformat'} =
10599:                                 $scantronurls{'custom'};
10600:                             my $putresult =
10601:                                 &Apache::lonnet::put_dom('configuration',
10602:                                                          \%confhash,$dom);
10603:                             if ($putresult ne 'ok') {
10604:                                 $error{'custom'} =
10605:                                     '<span class="LC_error">'.
10606:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
10607:                             }
10608:                         }
10609:                     } else {
10610:                         ($scantronurls{'default'},my $error) =
10611:                             &legacy_scantronformat($r,$dom,$confname,
10612:                                           'default',$legacyfile{'default'},
10613:                                           $scantronurls{'default'},
10614:                                           $scantronfiles{'default'});
10615:                         if ($error eq '') {
10616:                             $confhash{'scantron'}{'scantronformat'} = ''; 
10617:                             my $putresult =
10618:                                 &Apache::lonnet::put_dom('configuration',
10619:                                                          \%confhash,$dom);
10620:                             if ($putresult ne 'ok') {
10621:                                 $error{'default'} =
10622:                                     '<span class="LC_error">'.
10623:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
10624:                             }
10625:                         } else {
10626:                             $error{'default'} = $error;
10627:                         }
10628:                     }
10629:                 }
10630:             }
10631:         } else {
10632:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
10633:         }
10634:     }
10635:     if (ref($settings) eq 'HASH') {
10636:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
10637:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
10638:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
10639:                 $scantronurl = '';
10640:             } else {
10641:                 $scantronurl = $settings->{'scantronformat'};
10642:             }
10643:             $is_custom = 1;
10644:         } else {
10645:             $scantronurl = $scantronurls{'default'};
10646:         }
10647:     } else {
10648:         if ($is_custom) {
10649:             $scantronurl = $scantronurls{'custom'};
10650:         } else {
10651:             $scantronurl = $scantronurls{'default'};
10652:         }
10653:     }
10654:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10655:     $datatable .= '<tr'.$css_class.'>';
10656:     if (!$is_custom) {
10657:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
10658:                       '<span class="LC_nobreak">';
10659:         if ($scantronurl) {
10660:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
10661:                                                          undef,undef,undef,undef,'background-color:#ffffff');
10662:         } else {
10663:             $datatable = &mt('File unavailable for display');
10664:         }
10665:         $datatable .= '</span></td>';
10666:         if (keys(%error) == 0) { 
10667:             $datatable .= '<td style="vertical-align: bottom">';
10668:             if (!$switchserver) {
10669:                 $datatable .= &mt('Upload:').'<br />';
10670:             }
10671:         } else {
10672:             my $errorstr;
10673:             foreach my $key (sort(keys(%error))) {
10674:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
10675:             }
10676:             $datatable .= '<td>'.$errorstr;
10677:         }
10678:     } else {
10679:         if (keys(%error) > 0) {
10680:             my $errorstr;
10681:             foreach my $key (sort(keys(%error))) {
10682:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
10683:             } 
10684:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
10685:         } elsif ($scantronurl) {
10686:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
10687:                                                        undef,undef,undef,undef,'background-color:#ffffff');
10688:             $datatable .= '<td><span class="LC_nobreak">'.
10689:                           $link.
10690:                           '<label><input type="checkbox" name="scantronformat_del"'.
10691:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
10692:                           '<td><span class="LC_nobreak">&nbsp;'.
10693:                           &mt('Replace:').'</span><br />';
10694:         }
10695:     }
10696:     if (keys(%error) == 0) {
10697:         if ($switchserver) {
10698:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
10699:         } else {
10700:             $datatable .='<span class="LC_nobreak">&nbsp;'.
10701:                          '<input type="file" name="scantronformat" /></span>';
10702:         }
10703:     }
10704:     $datatable .= '</td></tr>';
10705:     $$rowtotal ++;
10706:     return $datatable;
10707: }
10708: 
10709: sub legacy_scantronformat {
10710:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
10711:     my ($url,$error);
10712:     my @statinfo = &Apache::lonnet::stat_file($newurl);
10713:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
10714:         my $modified = [];
10715:         (my $result,$url) =
10716:             &Apache::lonconfigsettings::publishlogo($r,'copy',$legacyfile,$dom,$confname,
10717:                                                     'scantron','','',$newfile,$modified);
10718:         if ($result eq 'ok') {
10719:             &update_modify_urls($r,$modified);
10720:         } else {
10721:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
10722:         }
10723:     }
10724:     return ($url,$error);
10725: }
10726: 
10727: sub print_scantronconfig {
10728:     my ($dom,$settings,$rowtotal) = @_;
10729:     my $itemcount = 2;
10730:     my $is_checked = ' checked="checked"';
10731:     my %optionson = (
10732:                      hdr => ' checked="checked"',
10733:                      pad => ' checked="checked"',
10734:                      rem => ' checked="checked"',
10735:                     );
10736:     my %optionsoff = (
10737:                       hdr => '',
10738:                       pad => '',
10739:                       rem => '',
10740:                      );
10741:     my $currcsvsty = 'none';
10742:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
10743:     my @fields = &scantroncsv_fields();
10744:     my %titles = &scantronconfig_titles();
10745:     if (ref($settings) eq 'HASH') {
10746:         if (ref($settings->{config}) eq 'HASH') {
10747:             if ($settings->{config}->{dat}) {
10748:                 $checked{'dat'} = $is_checked;
10749:             }
10750:             if (ref($settings->{config}->{csv}) eq 'HASH') {
10751:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
10752:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
10753:                     if (keys(%csvfields) > 0) {
10754:                         $checked{'csv'} = $is_checked;
10755:                         $currcsvsty = 'block';
10756:                     }
10757:                 }
10758:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
10759:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
10760:                     foreach my $option (keys(%optionson)) {
10761:                         unless ($csvoptions{$option}) {
10762:                             $optionsoff{$option} = $optionson{$option};
10763:                             $optionson{$option} = '';
10764:                         }
10765:                     }
10766:                 }
10767:             }
10768:         } else {
10769:             $checked{'dat'} = $is_checked;
10770:         }
10771:     } else {
10772:         $checked{'dat'} = $is_checked;
10773:     }
10774:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
10775:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
10776:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
10777:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
10778:     foreach my $item ('dat','csv') {
10779:         my $id;
10780:         if ($item eq 'csv') {
10781:             $id = 'id="scantronconfcsv" ';
10782:         }
10783:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
10784:                       $titles{$item}.'</label>'.('&nbsp;'x3);
10785:         if ($item eq 'csv') {
10786:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
10787:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
10788:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
10789:             foreach my $col (@fields) {
10790:                 my $selnone;
10791:                 if ($csvfields{$col} eq '') {
10792:                     $selnone = ' selected="selected"';
10793:                 }
10794:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
10795:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
10796:                               '<option value=""'.$selnone.'></option>';
10797:                 for (my $i=0; $i<20; $i++) {
10798:                     my $shown = $i+1;
10799:                     my $sel;
10800:                     unless ($selnone) {
10801:                         if (exists($csvfields{$col})) {
10802:                             if ($csvfields{$col} == $i) {
10803:                                 $sel = ' selected="selected"';
10804:                             }
10805:                         }
10806:                     }
10807:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
10808:                 }
10809:                 $datatable .= '</select></td></tr>';
10810:            }
10811:            $datatable .= '</table></fieldset>'.
10812:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
10813:                          '<legend>'.&mt('CSV Options').'</legend>';
10814:            foreach my $option ('hdr','pad','rem') {
10815:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
10816:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
10817:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
10818:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
10819:            }
10820:            $datatable .= '</fieldset>';
10821:            $itemcount ++;
10822:         }
10823:     }
10824:     $datatable .= '</td></tr>';
10825:     $$rowtotal ++;
10826:     return $datatable;
10827: }
10828: 
10829: sub scantronconfig_titles {
10830:     return &Apache::lonlocal::texthash(
10831:                                           dat => 'Standard format (.dat)',
10832:                                           csv => 'Comma separated values (.csv)',
10833:                                           hdr => 'Remove first line in file (contains column titles)',
10834:                                           pad => 'Prepend 0s to PaperID',
10835:                                           rem => 'Remove leading spaces (except Question Response columns)',
10836:                                           CODE => 'CODE',
10837:                                           ID   => 'Student ID',
10838:                                           PaperID => 'Paper ID',
10839:                                           FirstName => 'First Name',
10840:                                           LastName => 'Last Name',
10841:                                           FirstQuestion => 'First Question Response',
10842:                                           Section => 'Section',
10843:     );
10844: }
10845: 
10846: sub scantroncsv_fields {
10847:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
10848: }
10849: 
10850: sub print_coursecategories {
10851:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
10852:     my $datatable;
10853:     if ($position eq 'top') {
10854:         my (%checked);
10855:         my @catitems = ('unauth','auth');
10856:         my @cattypes = ('std','domonly','codesrch','none');
10857:         $checked{'unauth'} = 'std';
10858:         $checked{'auth'} = 'std';
10859:         if (ref($settings) eq 'HASH') {
10860:             foreach my $type (@cattypes) {
10861:                 if ($type eq $settings->{'unauth'}) {
10862:                     $checked{'unauth'} = $type;
10863:                 }
10864:                 if ($type eq $settings->{'auth'}) {
10865:                     $checked{'auth'} = $type;
10866:                 }
10867:             }
10868:         }
10869:         my %lt = &Apache::lonlocal::texthash (
10870:                                                unauth   => 'Catalog type for unauthenticated users',
10871:                                                auth     => 'Catalog type for authenticated users',
10872:                                                none     => 'No catalog',
10873:                                                std      => 'Standard catalog',
10874:                                                domonly  => 'Domain-only catalog',
10875:                                                codesrch => "Code search form",
10876:                                              );
10877:        my $itemcount = 0;
10878:        foreach my $item (@catitems) {
10879:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
10880:            $datatable .= '<tr '.$css_class.'>'.
10881:                          '<td>'.$lt{$item}.'</td>'.
10882:                          '<td class="LC_right_item"><span class="LC_nobreak">';
10883:            foreach my $type (@cattypes) {
10884:                my $ischecked;
10885:                if ($checked{$item} eq $type) {
10886:                    $ischecked=' checked="checked"';
10887:                }
10888:                $datatable .= '<label>'.
10889:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
10890:                              ' />'.$lt{$type}.'</label>&nbsp;';
10891:            }
10892:            $datatable .= '</span></td></tr>';
10893:            $itemcount ++;
10894:         }
10895:         $$rowtotal += $itemcount;
10896:     } elsif ($position eq 'middle') {
10897:         my $toggle_cats_crs = ' ';
10898:         my $toggle_cats_dom = ' checked="checked" ';
10899:         my $can_cat_crs = ' ';
10900:         my $can_cat_dom = ' checked="checked" ';
10901:         my $toggle_catscomm_comm = ' ';
10902:         my $toggle_catscomm_dom = ' checked="checked" ';
10903:         my $can_catcomm_comm = ' ';
10904:         my $can_catcomm_dom = ' checked="checked" ';
10905:         my $toggle_catsplace_place = ' ';
10906:         my $toggle_catsplace_dom = ' checked="checked" ';
10907:         my $can_catplace_place = ' ';
10908:         my $can_catplace_dom = ' checked="checked" ';
10909: 
10910:         if (ref($settings) eq 'HASH') {
10911:             if ($settings->{'togglecats'} eq 'crs') {
10912:                 $toggle_cats_crs = $toggle_cats_dom;
10913:                 $toggle_cats_dom = ' ';
10914:             }
10915:             if ($settings->{'categorize'} eq 'crs') {
10916:                 $can_cat_crs = $can_cat_dom;
10917:                 $can_cat_dom = ' ';
10918:             }
10919:             if ($settings->{'togglecatscomm'} eq 'comm') {
10920:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
10921:                 $toggle_catscomm_dom = ' ';
10922:             }
10923:             if ($settings->{'categorizecomm'} eq 'comm') {
10924:                 $can_catcomm_comm = $can_catcomm_dom;
10925:                 $can_catcomm_dom = ' ';
10926:             }
10927:             if ($settings->{'togglecatsplace'} eq 'place') {
10928:                 $toggle_catsplace_place = $toggle_catsplace_dom;
10929:                 $toggle_catsplace_dom = ' ';
10930:             }
10931:             if ($settings->{'categorizeplace'} eq 'place') {
10932:                 $can_catplace_place = $can_catplace_dom;
10933:                 $can_catplace_dom = ' ';
10934:             }
10935:         }
10936:         my %title = &Apache::lonlocal::texthash (
10937:                      togglecats      => 'Show/Hide a course in catalog',
10938:                      togglecatscomm  => 'Show/Hide a community in catalog',
10939:                      togglecatsplace => 'Show/Hide a placement test in catalog',
10940:                      categorize      => 'Assign a category to a course',
10941:                      categorizecomm  => 'Assign a category to a community',
10942:                      categorizeplace => 'Assign a category to a placement test',
10943:                     );
10944:         my %level = &Apache::lonlocal::texthash (
10945:                      dom   => 'Set in Domain',
10946:                      crs   => 'Set in Course',
10947:                      comm  => 'Set in Community',
10948:                      place => 'Set in Placement Test',
10949:                     );
10950:         $datatable = '<tr class="LC_odd_row">'.
10951:                   '<td>'.$title{'togglecats'}.'</td>'.
10952:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10953:                   '<input type="radio" name="togglecats"'.
10954:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10955:                   '<label><input type="radio" name="togglecats"'.
10956:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
10957:                   '</tr><tr>'.
10958:                   '<td>'.$title{'categorize'}.'</td>'.
10959:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10960:                   '<label><input type="radio" name="categorize"'.
10961:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10962:                   '<label><input type="radio" name="categorize"'.
10963:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
10964:                   '</tr><tr class="LC_odd_row">'.
10965:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
10966:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10967:                   '<input type="radio" name="togglecatscomm"'.
10968:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10969:                   '<label><input type="radio" name="togglecatscomm"'.
10970:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
10971:                   '</tr><tr>'.
10972:                   '<td>'.$title{'categorizecomm'}.'</td>'.
10973:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10974:                   '<label><input type="radio" name="categorizecomm"'.
10975:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10976:                   '<label><input type="radio" name="categorizecomm"'.
10977:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
10978:                   '</tr><tr class="LC_odd_row">'.
10979:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
10980:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10981:                   '<input type="radio" name="togglecatsplace"'.
10982:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10983:                   '<label><input type="radio" name="togglecatscomm"'.
10984:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
10985:                   '</tr><tr>'.
10986:                   '<td>'.$title{'categorizeplace'}.'</td>'.
10987:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10988:                   '<label><input type="radio" name="categorizeplace"'.
10989:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10990:                   '<label><input type="radio" name="categorizeplace"'.
10991:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
10992:                   '</tr>';
10993:         $$rowtotal += 6;
10994:     } else {
10995:         my $css_class;
10996:         my $itemcount = 1;
10997:         my $cathash; 
10998:         if (ref($settings) eq 'HASH') {
10999:             $cathash = $settings->{'cats'};
11000:         }
11001:         if (ref($cathash) eq 'HASH') {
11002:             my (@cats,@trails,%allitems,%idx,@jsarray);
11003:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
11004:                                                    \%allitems,\%idx,\@jsarray);
11005:             my $maxdepth = scalar(@cats);
11006:             my $colattrib = '';
11007:             if ($maxdepth > 2) {
11008:                 $colattrib = ' colspan="2" ';
11009:             }
11010:             my @path;
11011:             if (@cats > 0) {
11012:                 if (ref($cats[0]) eq 'ARRAY') {
11013:                     my $numtop = @{$cats[0]};
11014:                     my $maxnum = $numtop;
11015:                     my %default_names = (
11016:                           instcode    => &mt('Official courses'),
11017:                           communities => &mt('Communities'),
11018:                           placement   => &mt('Placement Tests'),
11019:                     );
11020: 
11021:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
11022:                         ($cathash->{'instcode::0'} eq '') ||
11023:                         (!grep(/^communities$/,@{$cats[0]})) || 
11024:                         ($cathash->{'communities::0'} eq '') ||
11025:                         (!grep(/^placement$/,@{$cats[0]})) ||
11026:                         ($cathash->{'placement::0'} eq '')) {
11027:                         $maxnum ++;
11028:                     }
11029:                     my $lastidx;
11030:                     for (my $i=0; $i<$numtop; $i++) {
11031:                         my $parent = $cats[0][$i];
11032:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11033:                         my $item = &escape($parent).'::0';
11034:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
11035:                         $lastidx = $idx{$item};
11036:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11037:                                       .'<select name="'.$item.'"'.$chgstr.'>';
11038:                         for (my $k=0; $k<=$maxnum; $k++) {
11039:                             my $vpos = $k+1;
11040:                             my $selstr;
11041:                             if ($k == $i) {
11042:                                 $selstr = ' selected="selected" ';
11043:                             }
11044:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11045:                         }
11046:                         $datatable .= '</select></span></td><td>';
11047:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
11048:                             $datatable .=  '<span class="LC_nobreak">'
11049:                                            .$default_names{$parent}.'</span>';
11050:                             if ($parent eq 'instcode') {
11051:                                 $datatable .= '<br /><span class="LC_nobreak">('
11052:                                               .&mt('with institutional codes')
11053:                                               .')</span></td><td'.$colattrib.'>';
11054:                             } else {
11055:                                 $datatable .= '<table><tr><td>';
11056:                             }
11057:                             $datatable .= '<span class="LC_nobreak">'
11058:                                           .'<label><input type="radio" name="'
11059:                                           .$parent.'" value="1" checked="checked" />'
11060:                                           .&mt('Display').'</label>';
11061:                             if ($parent eq 'instcode') {
11062:                                 $datatable .= '&nbsp;';
11063:                             } else {
11064:                                 $datatable .= '</span></td></tr><tr><td>'
11065:                                               .'<span class="LC_nobreak">';
11066:                             }
11067:                             $datatable .= '<label><input type="radio" name="'
11068:                                           .$parent.'" value="0" />'
11069:                                           .&mt('Do not display').'</label></span>';
11070:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
11071:                                 $datatable .= '</td></tr></table>';
11072:                             }
11073:                             $datatable .= '</td>';
11074:                         } else {
11075:                             $datatable .= $parent
11076:                                           .'&nbsp;<span class="LC_nobreak"><label>'
11077:                                           .'<input type="checkbox" name="deletecategory" '
11078:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
11079:                         }
11080:                         my $depth = 1;
11081:                         push(@path,$parent);
11082:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
11083:                         pop(@path);
11084:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
11085:                         $itemcount ++;
11086:                     }
11087:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11088:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
11089:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
11090:                     for (my $k=0; $k<=$maxnum; $k++) {
11091:                         my $vpos = $k+1;
11092:                         my $selstr;
11093:                         if ($k == $numtop) {
11094:                             $selstr = ' selected="selected" ';
11095:                         }
11096:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11097:                     }
11098:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
11099:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
11100:                                   .'</tr>'."\n";
11101:                     $itemcount ++;
11102:                     foreach my $default ('instcode','communities','placement') {
11103:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
11104:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
11105:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
11106:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
11107:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
11108:                             for (my $k=0; $k<=$maxnum; $k++) {
11109:                                 my $vpos = $k+1;
11110:                                 my $selstr;
11111:                                 if ($k == $maxnum) {
11112:                                     $selstr = ' selected="selected" ';
11113:                                 }
11114:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11115:                             }
11116:                             $datatable .= '</select></span></td>'.
11117:                                           '<td><span class="LC_nobreak">'.
11118:                                           $default_names{$default}.'</span>';
11119:                             if ($default eq 'instcode') {
11120:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
11121:                                               .&mt('with institutional codes').')</span>';
11122:                             }
11123:                             $datatable .= '</td>'
11124:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
11125:                                           .&mt('Display').'</label>&nbsp;'
11126:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
11127:                                           .&mt('Do not display').'</label></span></td></tr>';
11128:                         }
11129:                     }
11130:                 }
11131:             } else {
11132:                 $datatable .= &initialize_categories($itemcount);
11133:             }
11134:         } else {
11135:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
11136:                           .&initialize_categories($itemcount);
11137:         }
11138:         $$rowtotal += $itemcount;
11139:     }
11140:     return $datatable;
11141: }
11142: 
11143: sub print_serverstatuses {
11144:     my ($dom,$settings,$rowtotal) = @_;
11145:     my $datatable;
11146:     my @pages = &serverstatus_pages();
11147:     my (%namedaccess,%machineaccess);
11148:     foreach my $type (@pages) {
11149:         $namedaccess{$type} = '';
11150:         $machineaccess{$type}= '';
11151:     }
11152:     if (ref($settings) eq 'HASH') {
11153:         foreach my $type (@pages) {
11154:             if (exists($settings->{$type})) {
11155:                 if (ref($settings->{$type}) eq 'HASH') {
11156:                     foreach my $key (keys(%{$settings->{$type}})) {
11157:                         if ($key eq 'namedusers') {
11158:                             $namedaccess{$type} = $settings->{$type}->{$key};
11159:                         } elsif ($key eq 'machines') {
11160:                             $machineaccess{$type} = $settings->{$type}->{$key};
11161:                         }
11162:                     }
11163:                 }
11164:             }
11165:         }
11166:     }
11167:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
11168:     my $rownum = 0;
11169:     my $css_class;
11170:     foreach my $type (@pages) {
11171:         $rownum ++;
11172:         $css_class = $rownum%2?' class="LC_odd_row"':'';
11173:         $datatable .= '<tr'.$css_class.'>'.
11174:                       '<td><span class="LC_nobreak">'.
11175:                       $titles->{$type}.'</span></td>'.
11176:                       '<td class="LC_left_item">'.
11177:                       '<input type="text" name="'.$type.'_namedusers" '.
11178:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
11179:                       '<td class="LC_right_item">'.
11180:                       '<span class="LC_nobreak">'.
11181:                       '<input type="text" name="'.$type.'_machines" '.
11182:                       'value="'.$machineaccess{$type}.'" size="10" />'.
11183:                       '</span></td></tr>'."\n";
11184:     }
11185:     $$rowtotal += $rownum;
11186:     return $datatable;
11187: }
11188: 
11189: sub serverstatus_pages {
11190:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
11191:             'checksums','clusterstatus','certstatus','metadata_keywords',
11192:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
11193:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
11194: }
11195: 
11196: sub defaults_javascript {
11197:     my ($settings) = @_;
11198:     return unless (ref($settings) eq 'HASH');
11199:     my $portal_js = <<"ENDPORTAL";
11200: 
11201: function portalExtras(caller) {
11202:     var x = caller.value;
11203:     var y = new Array('email','web');
11204:     for (var i=0; i<y.length; i++) {
11205:         if (document.getElementById('portal_def_'+y[i]+'_div')) {
11206:             var z = document.getElementById('portal_def_'+y[i]+'_div');
11207:             if (x.length > 0) {
11208:                 z.style.display = 'block';
11209:             } else {
11210:                 z.style.display = 'none';
11211:             }
11212:         }
11213:     }
11214: }
11215: ENDPORTAL
11216:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
11217:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
11218:         if ($maxnum eq '') {
11219:             $maxnum = 0;
11220:         }
11221:         $maxnum ++;
11222:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
11223:         return <<"ENDSCRIPT";
11224: <script type="text/javascript">
11225: // <![CDATA[
11226: function reorderTypes(form,caller) {
11227:     var changedVal;
11228: $jstext 
11229:     var newpos = 'addinststatus_pos';
11230:     var current = new Array;
11231:     var maxh = $maxnum;
11232:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11233:     var oldVal;
11234:     if (caller == newpos) {
11235:         changedVal = newitemVal;
11236:     } else {
11237:         var curritem = 'inststatus_pos_'+caller;
11238:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
11239:         current[newitemVal] = newpos;
11240:     }
11241:     for (var i=0; i<inststatuses.length; i++) {
11242:         if (inststatuses[i] != caller) {
11243:             var elementName = 'inststatus_pos_'+inststatuses[i];
11244:             if (form.elements[elementName]) {
11245:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11246:                 current[currVal] = elementName;
11247:             }
11248:         }
11249:     }
11250:     for (var j=0; j<maxh; j++) {
11251:         if (current[j] == undefined) {
11252:             oldVal = j;
11253:         }
11254:     }
11255:     if (oldVal < changedVal) {
11256:         for (var k=oldVal+1; k<=changedVal ; k++) {
11257:            var elementName = current[k];
11258:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11259:         }
11260:     } else {
11261:         for (var k=changedVal; k<oldVal; k++) {
11262:             var elementName = current[k];
11263:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11264:         }
11265:     }
11266:     return;
11267: }
11268: 
11269: $portal_js
11270: 
11271: // ]]>
11272: </script>
11273: 
11274: ENDSCRIPT
11275:     } else {
11276: return <<"ENDSCRIPT";
11277: <script type="text/javascript">
11278: // <![CDATA[
11279: $portal_js
11280: // ]]>
11281: </script>
11282: 
11283: ENDSCRIPT
11284:     }
11285:     return;
11286: }
11287: 
11288: sub passwords_javascript {
11289:     my ($prefix) = @_;
11290:     my %intalert;
11291:     if ($prefix eq 'passwords') {
11292:         %intalert = &Apache::lonlocal::texthash (
11293:             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.',
11294:             authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
11295:             passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
11296:             passmax => 'Warning: maximum password length must be a positive integer (or blank).',
11297:             passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
11298:             passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
11299:         );
11300:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
11301:         %intalert = &Apache::lonlocal::texthash (
11302:             passmin => 'Warning: minimum secret length must be a positive integer greater than 6.',
11303:             passmax => 'Warning: maximum secret length must be a positive integer (or blank).',
11304:         );
11305:     }
11306:     &js_escape(\%intalert);
11307:     my $defmin = $Apache::lonnet::passwdmin;
11308:     my $intauthjs;
11309:     if ($prefix eq 'passwords') { $intauthjs = <<"ENDSCRIPT";
11310: 
11311: function warnIntAuth(field) {
11312:     if (field.name == 'intauth_check') {
11313:         if (field.value == '2') {
11314:             alert('$intalert{authcheck}');
11315:         }
11316:     }
11317:     if (field.name == 'intauth_cost') {
11318:         field.value.replace(/\s/g,'');
11319:         if (field.value != '') {
11320:             var regexdigit=/^\\d+\$/;
11321:             if (!regexdigit.test(field.value)) {
11322:                 alert('$intalert{authcost}');
11323:             }
11324:         }
11325:     }
11326:     return;
11327: }
11328: 
11329: ENDSCRIPT
11330: 
11331:      }
11332: 
11333:      $intauthjs .= <<"ENDSCRIPT";
11334: 
11335: function warnInt$prefix(field) {
11336:     field.value.replace(/^\s+/,'');
11337:     field.value.replace(/\s+\$/,'');
11338:     var regexdigit=/^\\d+\$/;
11339:     if (field.name == '${prefix}_min') {
11340:         if (field.value == '') {
11341:             alert('$intalert{passmin}');
11342:             field.value = '$defmin';
11343:         } else {
11344:             if (!regexdigit.test(field.value)) {
11345:                 alert('$intalert{passmin}');
11346:                 field.value = '$defmin';
11347:             }
11348:             var minval = parseInt(field.value,10);
11349:             if (minval < $defmin) {
11350:                 alert('$intalert{passmin}');
11351:                 field.value = '$defmin';
11352:             }
11353:         }
11354:     } else {
11355:         if (field.value == '0') {
11356:             field.value = '';
11357:         }
11358:         if (field.value != '') {
11359:             if (field.name == '${prefix}_expire') {
11360:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
11361:                 if (!regexpposnum.test(field.value)) {
11362:                     alert('$intalert{passexp}');
11363:                     field.value = '';
11364:                 } else {
11365:                     var expval = parseFloat(field.value);
11366:                     if (expval == 0) {
11367:                         alert('$intalert{passexp}');
11368:                         field.value = '';
11369:                     }
11370:                 }
11371:             } else {
11372:                 if (!regexdigit.test(field.value)) {
11373:                     if (field.name == '${prefix}_max') {
11374:                         alert('$intalert{passmax}');
11375:                     } else {
11376:                         if (field.name == '${prefix}_numsaved') {
11377:                             alert('$intalert{passnum}');
11378:                         }
11379:                     }
11380:                     field.value = '';
11381:                 }
11382:             }
11383:         }
11384:     }
11385:     return;
11386: }
11387: 
11388: ENDSCRIPT
11389:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
11390: }
11391: 
11392: sub coursecategories_javascript {
11393:     my ($settings) = @_;
11394:     my ($output,$jstext,$cathash);
11395:     if (ref($settings) eq 'HASH') {
11396:         $cathash = $settings->{'cats'};
11397:     }
11398:     if (ref($cathash) eq 'HASH') {
11399:         my (@cats,@jsarray,%idx);
11400:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
11401:         if (@jsarray > 0) {
11402:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
11403:             for (my $i=0; $i<@jsarray; $i++) {
11404:                 if (ref($jsarray[$i]) eq 'ARRAY') {
11405:                     my $catstr = join('","',@{$jsarray[$i]});
11406:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
11407:                 }
11408:             }
11409:         }
11410:     } else {
11411:         $jstext  = '    var categories = Array(1);'."\n".
11412:                    '    categories[0] = Array("instcode_pos");'."\n"; 
11413:     }
11414:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
11415:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
11416:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
11417:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
11418:     &js_escape(\$instcode_reserved);
11419:     &js_escape(\$communities_reserved);
11420:     &js_escape(\$placement_reserved);
11421:     &js_escape(\$choose_again);
11422:     $output = <<"ENDSCRIPT";
11423: <script type="text/javascript">
11424: // <![CDATA[
11425: function reorderCats(form,parent,item,idx) {
11426:     var changedVal;
11427: $jstext
11428:     var newpos = 'addcategory_pos';
11429:     if (parent == '') {
11430:         var has_instcode = 0;
11431:         var maxtop = categories[idx].length;
11432:         for (var j=0; j<maxtop; j++) {
11433:             if (categories[idx][j] == 'instcode::0') {
11434:                 has_instcode == 1;
11435:             }
11436:         }
11437:         if (has_instcode == 0) {
11438:             categories[idx][maxtop] = 'instcode_pos';
11439:         }
11440:     } else {
11441:         newpos += '_'+parent;
11442:     }
11443:     var maxh = 1 + categories[idx].length;
11444:     var current = new Array;
11445:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11446:     if (item == newpos) {
11447:         changedVal = newitemVal;
11448:     } else {
11449:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
11450:         current[newitemVal] = newpos;
11451:     }
11452:     for (var i=0; i<categories[idx].length; i++) {
11453:         var elementName = categories[idx][i];
11454:         if (elementName != item) {
11455:             if (form.elements[elementName]) {
11456:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11457:                 current[currVal] = elementName;
11458:             }
11459:         }
11460:     }
11461:     var oldVal;
11462:     for (var j=0; j<maxh; j++) {
11463:         if (current[j] == undefined) {
11464:             oldVal = j;
11465:         }
11466:     }
11467:     if (oldVal < changedVal) {
11468:         for (var k=oldVal+1; k<=changedVal ; k++) {
11469:            var elementName = current[k];
11470:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11471:         }
11472:     } else {
11473:         for (var k=changedVal; k<oldVal; k++) {
11474:             var elementName = current[k];
11475:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11476:         }
11477:     }
11478:     return;
11479: }
11480: 
11481: function categoryCheck(form) {
11482:     if (form.elements['addcategory_name'].value == 'instcode') {
11483:         alert('$instcode_reserved\\n$choose_again');
11484:         return false;
11485:     }
11486:     if (form.elements['addcategory_name'].value == 'communities') {
11487:         alert('$communities_reserved\\n$choose_again');
11488:         return false;
11489:     }
11490:     if (form.elements['addcategory_name'].value == 'placement') {
11491:         alert('$placement_reserved\\n$choose_again');
11492:         return false;
11493:     }
11494:     return true;
11495: }
11496: 
11497: // ]]>
11498: </script>
11499: 
11500: ENDSCRIPT
11501:     return $output;
11502: }
11503: 
11504: sub initialize_categories {
11505:     my ($itemcount) = @_;
11506:     my ($datatable,$css_class,$chgstr);
11507:     my %default_names = &Apache::lonlocal::texthash (
11508:                       instcode    => 'Official courses (with institutional codes)',
11509:                       communities => 'Communities',
11510:                       placement   => 'Placement Tests',
11511:                         );
11512:     my %selnum = (
11513:                    instcode    => '0',
11514:                    communities => '1',
11515:                    placement   => '2',
11516:                  );
11517:     my %selected;
11518:     foreach my $default ('instcode','communities','placement') {
11519:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11520:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
11521:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
11522:         $selected{$selnum{$default}} = ' selected="selected"';
11523:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11524:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
11525:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
11526:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
11527:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
11528:                      .'<option value="3">4</option></select>&nbsp;'
11529:                      .$default_names{$default}
11530:                      .'</span></td><td><span class="LC_nobreak">'
11531:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
11532:                      .&mt('Display').'</label>&nbsp;<label>'
11533:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
11534:                  .'</label></span></td></tr>';
11535:         $itemcount ++;
11536:     }
11537:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11538:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
11539:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11540:                   .'<select name="addcategory_pos"'.$chgstr.'>'
11541:                   .'<option value="0">1</option>'
11542:                   .'<option value="1">2</option>'
11543:                   .'<option value="2">3</option>'
11544:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
11545:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
11546:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
11547:                   .'</td></tr>';
11548:     return $datatable;
11549: }
11550: 
11551: sub build_category_rows {
11552:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
11553:     my ($text,$name,$item,$chgstr);
11554:     if (ref($cats) eq 'ARRAY') {
11555:         my $maxdepth = scalar(@{$cats});
11556:         if (ref($cats->[$depth]) eq 'HASH') {
11557:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
11558:                 my $numchildren = @{$cats->[$depth]{$parent}};
11559:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
11560:                 $text .= '<td><table class="LC_data_table">';
11561:                 my ($idxnum,$parent_name,$parent_item);
11562:                 my $higher = $depth - 1;
11563:                 if ($higher == 0) {
11564:                     $parent_name = &escape($parent).'::'.$higher;
11565:                 } else {
11566:                     if (ref($path) eq 'ARRAY') {
11567:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11568:                     }
11569:                 }
11570:                 $parent_item = 'addcategory_pos_'.$parent_name;
11571:                 for (my $j=0; $j<=$numchildren; $j++) {
11572:                     if ($j < $numchildren) {
11573:                         $name = $cats->[$depth]{$parent}[$j];
11574:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
11575:                         $idxnum = $idx->{$item};
11576:                     } else {
11577:                         $name = $parent_name;
11578:                         $item = $parent_item;
11579:                     }
11580:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
11581:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
11582:                     for (my $i=0; $i<=$numchildren; $i++) {
11583:                         my $vpos = $i+1;
11584:                         my $selstr;
11585:                         if ($j == $i) {
11586:                             $selstr = ' selected="selected" ';
11587:                         }
11588:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
11589:                     }
11590:                     $text .= '</select>&nbsp;';
11591:                     if ($j < $numchildren) {
11592:                         my $deeper = $depth+1;
11593:                         $text .= $name.'&nbsp;'
11594:                                  .'<label><input type="checkbox" name="deletecategory" value="'
11595:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
11596:                         if(ref($path) eq 'ARRAY') {
11597:                             push(@{$path},$name);
11598:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
11599:                             pop(@{$path});
11600:                         }
11601:                     } else {
11602:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
11603:                         if ($j == $numchildren) {
11604:                             $text .= $name;
11605:                         } else {
11606:                             $text .= $item;
11607:                         }
11608:                         $text .= '" value="" />';
11609:                     }
11610:                     $text .= '</td></tr>';
11611:                 }
11612:                 $text .= '</table></td>';
11613:             } else {
11614:                 my $higher = $depth-1;
11615:                 if ($higher == 0) {
11616:                     $name = &escape($parent).'::'.$higher;
11617:                 } else {
11618:                     if (ref($path) eq 'ARRAY') {
11619:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11620:                     }
11621:                 }
11622:                 my $colspan;
11623:                 if ($parent ne 'instcode') {
11624:                     $colspan = $maxdepth - $depth - 1;
11625:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
11626:                 }
11627:             }
11628:         }
11629:     }
11630:     return $text;
11631: }
11632: 
11633: sub modifiable_userdata_row {
11634:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
11635:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
11636:     my ($role,$rolename,$statustype);
11637:     $role = $item;
11638:     if ($context eq 'cancreate') {
11639:         if ($item =~ /^(emailusername)_(.+)$/) {
11640:             $role = $1;
11641:             $statustype = $2;
11642:             if (ref($usertypes) eq 'HASH') {
11643:                 if ($usertypes->{$statustype}) {
11644:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
11645:                 } else {
11646:                     $rolename = &mt('Data provided by user');
11647:                 }
11648:             }
11649:         }
11650:     } elsif ($context eq 'selfcreate') {
11651:         if (ref($usertypes) eq 'HASH') {
11652:             $rolename = $usertypes->{$role};
11653:         } else {
11654:             $rolename = $role;
11655:         }
11656:     } elsif ($context eq 'lti') {
11657:         $rolename = &mt('Institutional data used (if available)');
11658:     } elsif ($context eq 'privacy') {
11659:         $rolename = $itemdesc;
11660:     } else {
11661:         if ($role eq 'cr') {
11662:             $rolename = &mt('Custom role');
11663:         } else {
11664:             $rolename = &Apache::lonnet::plaintext($role);
11665:         }
11666:     }
11667:     my (@fields,%fieldtitles);
11668:     if (ref($fieldsref) eq 'ARRAY') {
11669:         @fields = @{$fieldsref};
11670:     } else {
11671:         @fields = ('lastname','firstname','middlename','generation',
11672:                    'permanentemail','id');
11673:     }
11674:     if ((ref($titlesref) eq 'HASH')) {
11675:         %fieldtitles = %{$titlesref};
11676:     } else {
11677:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11678:     }
11679:     my $output;
11680:     my $css_class;
11681:     if ($rowcount%2) {
11682:         $css_class = 'LC_odd_row';
11683:     }
11684:     if ($customcss) {
11685:         $css_class .= " $customcss";
11686:     }
11687:     $css_class =~ s/^\s+//;
11688:     if ($css_class) {
11689:         $css_class = ' class="'.$css_class.'"';
11690:     }
11691:     if ($rowstyle) {
11692:         $css_class .= ' style="'.$rowstyle.'"';
11693:     }
11694:     if ($rowid) {
11695:         $rowid = ' id="'.$rowid.'"';
11696:     }
11697:     $output = '<tr '.$css_class.$rowid.'>'.
11698:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
11699:               '<td class="LC_left_item" colspan="2"><table>';
11700:     my $rem;
11701:     my %checks;
11702:     if (ref($settings) eq 'HASH') {
11703:         my $hashref;
11704:         if ($context eq 'lti') {
11705:             if (ref($settings) eq 'HASH') {
11706:                 $hashref = $settings->{'instdata'};
11707:             }
11708:         } elsif ($context eq 'privacy') {
11709:             my ($key,$inner) = split(/_/,$role);
11710:             if (ref($settings) eq 'HASH') {
11711:                 if (ref($settings->{$key}) eq 'HASH') {
11712:                     $hashref = $settings->{$key}->{$inner};
11713:                 }
11714:             }
11715:         } elsif (ref($settings->{$context}) eq 'HASH') {
11716:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
11717:                 $hashref = $settings->{'lti_instdata'};
11718:             }
11719:             if ($role eq 'emailusername') {
11720:                 if ($statustype) {
11721:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
11722:                         $hashref = $settings->{$context}->{$role}->{$statustype};
11723:                     }
11724:                 }
11725:             }
11726:         }
11727:         if (ref($hashref) eq 'HASH') {
11728:             foreach my $field (@fields) {
11729:                 if ($hashref->{$field}) {
11730:                     if ($role eq 'emailusername') {
11731:                         $checks{$field} = $hashref->{$field};
11732:                     } else {
11733:                         $checks{$field} = ' checked="checked" ';
11734:                     }
11735:                 }
11736:             }
11737:         }
11738:     }
11739:     my $total = scalar(@fields);
11740:     for (my $i=0; $i<$total; $i++) {
11741:         $rem = $i%($numinrow);
11742:         if ($rem == 0) {
11743:             if ($i > 0) {
11744:                 $output .= '</tr>';
11745:             }
11746:             $output .= '<tr>';
11747:         }
11748:         my $check = ' ';
11749:         unless ($role eq 'emailusername') {
11750:             if (exists($checks{$fields[$i]})) {
11751:                 $check = $checks{$fields[$i]};
11752:             } elsif ($context eq 'privacy') {
11753:                 if ($role =~ /^priv_(domain|course)$/) {
11754:                     if (ref($settings) ne 'HASH') {
11755:                         $check = ' checked="checked" ';
11756:                     }
11757:                 } elsif ($role =~ /^priv_(author|community)$/) {
11758:                     if (ref($settings) ne 'HASH') {
11759:                         unless ($fields[$i] eq 'id') {
11760:                             $check = ' checked="checked" ';
11761:                         }
11762:                     }
11763:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
11764:                     if (ref($settings) ne 'HASH') {
11765:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
11766:                             $check = ' checked="checked" ';
11767:                         }
11768:                     }
11769:                 }
11770:             } elsif ($context ne 'lti') {
11771:                 if ($role eq 'st') {
11772:                     if (ref($settings) ne 'HASH') {
11773:                         $check = ' checked="checked" '; 
11774:                     }
11775:                 }
11776:             }
11777:         }
11778:         $output .= '<td class="LC_left_item">'.
11779:                    '<span class="LC_nobreak">';
11780:         my $prefix = 'canmodify';
11781:         if ($role eq 'emailusername') {
11782:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
11783:                 $checks{$fields[$i]} = 'omit';
11784:             }
11785:             foreach my $option ('required','optional','omit') {
11786:                 my $checked='';
11787:                 if ($checks{$fields[$i]} eq $option) {
11788:                     $checked='checked="checked" ';
11789:                 }
11790:                 $output .= '<label>'.
11791:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
11792:                            &mt($option).'</label>'.('&nbsp;' x2);
11793:             }
11794:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
11795:         } else {
11796:             if ($context eq 'lti') {
11797:                 $prefix = 'lti';
11798:             } elsif ($context eq 'privacy') {
11799:                 $prefix = 'privacy';
11800:             }
11801:             $output .= '<label>'.
11802:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
11803:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
11804:                        '</label>';
11805:         }
11806:         $output .= '</span></td>';
11807:     }
11808:     $rem = $total%$numinrow;
11809:     my $colsleft;
11810:     if ($rem) {
11811:         $colsleft = $numinrow - $rem;
11812:     }
11813:     if ($colsleft > 1) {
11814:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
11815:                    '&nbsp;</td>';
11816:     } elsif ($colsleft == 1) {
11817:         $output .= '<td class="LC_left_item">&nbsp;</td>';
11818:     }
11819:     $output .= '</tr></table></td></tr>';
11820:     return $output;
11821: }
11822: 
11823: sub insttypes_row {
11824:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
11825:         $customcss,$rowstyle) = @_;
11826:     my %lt = &Apache::lonlocal::texthash (
11827:                       cansearch => 'Users allowed to search',
11828:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
11829:                       lockablenames => 'User preference to lock name',
11830:                       selfassign    => 'Self-reportable affiliations',
11831:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
11832:              );
11833:     my $showdom;
11834:     if ($context eq 'cansearch') {
11835:         $showdom = ' ('.$dom.')';
11836:     }
11837:     my $class = 'LC_left_item';
11838:     if ($context eq 'statustocreate') {
11839:         $class = 'LC_right_item';
11840:     }
11841:     my $css_class;
11842:     if ($$rowtotal%2) {
11843:         $css_class = 'LC_odd_row';
11844:     }
11845:     if ($customcss) {
11846:         $css_class .= ' '.$customcss;
11847:     }
11848:     $css_class =~ s/^\s+//;
11849:     if ($css_class) {
11850:         $css_class = ' class="'.$css_class.'"';
11851:     }
11852:     if ($rowstyle) {
11853:         $css_class .= ' style="'.$rowstyle.'"';
11854:     }
11855:     if ($onclick) {
11856:         $onclick = 'onclick="'.$onclick.'" ';
11857:     }
11858:     my $output = '<tr'.$css_class.'>'.
11859:                  '<td>'.$lt{$context}.$showdom.
11860:                  '</td><td class="'.$class.'" colspan="2"><table>';
11861:     my $rem;
11862:     if (ref($types) eq 'ARRAY') {
11863:         for (my $i=0; $i<@{$types}; $i++) {
11864:             if (defined($usertypes->{$types->[$i]})) {
11865:                 my $rem = $i%($numinrow);
11866:                 if ($rem == 0) {
11867:                     if ($i > 0) {
11868:                         $output .= '</tr>';
11869:                     }
11870:                     $output .= '<tr>';
11871:                 }
11872:                 my $check = ' ';
11873:                 if (ref($settings) eq 'HASH') {
11874:                     if (ref($settings->{$context}) eq 'ARRAY') {
11875:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
11876:                             $check = ' checked="checked" ';
11877:                         }
11878:                     } elsif (ref($settings->{$context}) eq 'HASH') {
11879:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
11880:                             $check = ' checked="checked" ';
11881:                         }
11882:                     } elsif ($context eq 'statustocreate') {
11883:                         $check = ' checked="checked" ';
11884:                     }
11885:                 }
11886:                 $output .= '<td class="LC_left_item">'.
11887:                            '<span class="LC_nobreak"><label>'.
11888:                            '<input type="checkbox" name="'.$context.'" '.
11889:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
11890:                            $usertypes->{$types->[$i]}.'</label></span></td>';
11891:             }
11892:         }
11893:         $rem = @{$types}%($numinrow);
11894:     }
11895:     my $colsleft = $numinrow - $rem;
11896:     if ($context eq 'overrides') {
11897:         if ($colsleft > 1) {
11898:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
11899:         } else {
11900:             $output .= '<td class="LC_left_item">';
11901:         }
11902:         $output .= '&nbsp;';
11903:     } else {
11904:         if ($rem == 0) {
11905:             $output .= '<tr>';
11906:         }
11907:         if ($colsleft > 1) {
11908:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
11909:         } else {
11910:             $output .= '<td class="LC_left_item">';
11911:         }
11912:         my $defcheck = ' ';
11913:         if (ref($settings) eq 'HASH') {  
11914:             if (ref($settings->{$context}) eq 'ARRAY') {
11915:                 if (grep(/^default$/,@{$settings->{$context}})) {
11916:                     $defcheck = ' checked="checked" ';
11917:                 }
11918:             } elsif ($context eq 'statustocreate') {
11919:                 $defcheck = ' checked="checked" ';
11920:             }
11921:         }
11922:         $output .= '<span class="LC_nobreak"><label>'.
11923:                    '<input type="checkbox" name="'.$context.'" '.
11924:                    'value="default"'.$defcheck.$onclick.' />'.
11925:                    $othertitle.'</label></span>';
11926:     }
11927:     $output .= '</td></tr></table></td></tr>';
11928:     return $output;
11929: }
11930: 
11931: sub sorted_searchtitles {
11932:     my %searchtitles = &Apache::lonlocal::texthash(
11933:                          'uname' => 'username',
11934:                          'lastname' => 'last name',
11935:                          'lastfirst' => 'last name, first name',
11936:                      );
11937:     my @titleorder = ('uname','lastname','lastfirst');
11938:     return (\%searchtitles,\@titleorder);
11939: }
11940: 
11941: sub sorted_searchtypes {
11942:     my %srchtypes_desc = (
11943:                            exact    => 'is exact match',
11944:                            contains => 'contains ..',
11945:                            begins   => 'begins with ..',
11946:                          );
11947:     my @srchtypeorder = ('exact','begins','contains');
11948:     return (\%srchtypes_desc,\@srchtypeorder);
11949: }
11950: 
11951: sub usertype_update_row {
11952:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
11953:     my $datatable;
11954:     my $numinrow = 4;
11955:     foreach my $type (@{$types}) {
11956:         if (defined($usertypes->{$type})) {
11957:             $$rownums ++;
11958:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
11959:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
11960:                           '</td><td class="LC_left_item"><table>';
11961:             for (my $i=0; $i<@{$fields}; $i++) {
11962:                 my $rem = $i%($numinrow);
11963:                 if ($rem == 0) {
11964:                     if ($i > 0) {
11965:                         $datatable .= '</tr>';
11966:                     }
11967:                     $datatable .= '<tr>';
11968:                 }
11969:                 my $check = ' ';
11970:                 if (ref($settings) eq 'HASH') {
11971:                     if (ref($settings->{'fields'}) eq 'HASH') {
11972:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
11973:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
11974:                                 $check = ' checked="checked" ';
11975:                             }
11976:                         }
11977:                     }
11978:                 }
11979: 
11980:                 if ($i == @{$fields}-1) {
11981:                     my $colsleft = $numinrow - $rem;
11982:                     if ($colsleft > 1) {
11983:                         $datatable .= '<td colspan="'.$colsleft.'">';
11984:                     } else {
11985:                         $datatable .= '<td>';
11986:                     }
11987:                 } else {
11988:                     $datatable .= '<td>';
11989:                 }
11990:                 $datatable .= '<span class="LC_nobreak"><label>'.
11991:                               '<input type="checkbox" name="updateable_'.$type.
11992:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
11993:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
11994:             }
11995:             $datatable .= '</tr></table></td></tr>';
11996:         }
11997:     }
11998:     return $datatable;
11999: }
12000: 
12001: sub modify_login {
12002:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12003:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
12004:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon,
12005:         %currsaml,%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso);
12006:     %title = ( coursecatalog => 'Display course catalog',
12007:                adminmail => 'Display administrator E-mail address',
12008:                helpdesk  => 'Display "Contact Helpdesk" link',
12009:                newuser => 'Link for visitors to create a user account',
12010:                loginheader => 'Log-in box header',
12011:                saml => 'Dual SSO and non-SSO login');
12012:     @offon = ('off','on');
12013:     if (ref($domconfig{login}) eq 'HASH') {
12014:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
12015:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
12016:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
12017:             }
12018:         }
12019:         if (ref($domconfig{login}{'saml'}) eq 'HASH') {
12020:             foreach my $lonhost (keys(%{$domconfig{login}{'saml'}})) {
12021:                 if (ref($domconfig{login}{'saml'}{$lonhost}) eq 'HASH') {
12022:                     $currsaml{$lonhost} = $domconfig{login}{'saml'}{$lonhost};
12023:                     $saml{$lonhost} = 1;
12024:                     $samltext{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'text'};
12025:                     $samlurl{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'url'};
12026:                     $samlalt{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'alt'};
12027:                     $samlimg{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'img'};
12028:                     $samltitle{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'title'};
12029:                     $samlwindow{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'window'};
12030:                     $samlnotsso{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'notsso'};
12031:                 }
12032:             }
12033:         }
12034:     }
12035:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
12036:                                            \%domconfig,\%loginhash);
12037:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12038:     foreach my $item (@toggles) {
12039:         $loginhash{login}{$item} = $env{'form.'.$item};
12040:     }
12041:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
12042:     if (ref($colchanges{'login'}) eq 'HASH') {  
12043:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
12044:                                          \%loginhash);
12045:     }
12046: 
12047:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12048:     my %domservers = &Apache::lonnet::get_servers($dom);
12049:     my @loginvia_attribs = ('serverpath','custompath','exempt');
12050:     if (keys(%servers) > 1) {
12051:         foreach my $lonhost (keys(%servers)) {
12052:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
12053:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
12054:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
12055:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
12056:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
12057:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12058:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12059:                         $changes{'loginvia'}{$lonhost} = 1;
12060:                     } else {
12061:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
12062:                         $changes{'loginvia'}{$lonhost} = 1;
12063:                     }
12064:                 } else {
12065:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12066:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12067:                         $changes{'loginvia'}{$lonhost} = 1;
12068:                     }
12069:                 }
12070:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
12071:                     foreach my $item (@loginvia_attribs) {
12072:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
12073:                     }
12074:                 } else {
12075:                     foreach my $item (@loginvia_attribs) {
12076:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12077:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12078:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
12079:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12080:                                 $new = '/';
12081:                             }
12082:                         }
12083:                         if (($item eq 'custompath') && 
12084:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12085:                             $new = '';
12086:                         }
12087:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
12088:                             $changes{'loginvia'}{$lonhost} = 1;
12089:                         }
12090:                         if ($item eq 'exempt') {
12091:                             $new = &check_exempt_addresses($new);
12092:                         }
12093:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12094:                     }
12095:                 }
12096:             } else {
12097:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12098:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12099:                     $changes{'loginvia'}{$lonhost} = 1;
12100:                     foreach my $item (@loginvia_attribs) {
12101:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12102:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12103:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12104:                                 $new = '/';
12105:                             }
12106:                         }
12107:                         if (($item eq 'custompath') && 
12108:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12109:                             $new = '';
12110:                         }
12111:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12112:                     }
12113:                 }
12114:             }
12115:         }
12116:     }
12117: 
12118:     my $servadm = $r->dir_config('lonAdmEMail');
12119:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
12120:     if (ref($domconfig{'login'}) eq 'HASH') {
12121:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
12122:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
12123:                 if ($lang eq 'nolang') {
12124:                     push(@currlangs,$lang);
12125:                 } elsif (defined($langchoices{$lang})) {
12126:                     push(@currlangs,$lang);
12127:                 } else {
12128:                     next;
12129:                 }
12130:             }
12131:         }
12132:     }
12133:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
12134:     if (@currlangs > 0) {
12135:         foreach my $lang (@currlangs) {
12136:             if (grep(/^\Q$lang\E$/,@delurls)) {
12137:                 $changes{'helpurl'}{$lang} = 1;
12138:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
12139:                 $changes{'helpurl'}{$lang} = 1;
12140:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
12141:                 push(@newlangs,$lang);
12142:             } else {
12143:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12144:             }
12145:         }
12146:     }
12147:     unless (grep(/^nolang$/,@currlangs)) {
12148:         if ($env{'form.loginhelpurl_nolang.filename'}) {
12149:             $changes{'helpurl'}{'nolang'} = 1;
12150:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
12151:             push(@newlangs,'nolang');
12152:         }
12153:     }
12154:     if ($env{'form.loginhelpurl_add_lang'}) {
12155:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
12156:             ($env{'form.loginhelpurl_add_file.filename'})) {
12157:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
12158:             $addedfile = $env{'form.loginhelpurl_add_lang'};
12159:         }
12160:     }
12161:     if ((@newlangs > 0) || ($addedfile)) {
12162:         my $error;
12163:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12164:         if ($configuserok eq 'ok') {
12165:             if ($switchserver) {
12166:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
12167:             } elsif ($author_ok eq 'ok') {
12168:                 my @allnew = @newlangs;
12169:                 if ($addedfile ne '') {
12170:                     push(@allnew,$addedfile);
12171:                 }
12172:                 my $modified = [];
12173:                 foreach my $lang (@allnew) {
12174:                     my $formelem = 'loginhelpurl_'.$lang;
12175:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
12176:                         $formelem = 'loginhelpurl_add_file';
12177:                     }
12178:                     (my $result,$newurl{$lang}) =
12179:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12180:                                                                 "help/$lang",'','',$newfile{$lang},
12181:                                                                 $modified);
12182:                     if ($result eq 'ok') {
12183:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
12184:                         $changes{'helpurl'}{$lang} = 1;
12185:                     } else {
12186:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
12187:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12188:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
12189:                             (!grep(/^\Q$lang\E$/,@delurls))) {
12190:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12191:                         }
12192:                     }
12193:                 }
12194:                 &update_modify_urls($r,$modified);
12195:             } else {
12196:                 $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);
12197:             }
12198:         } else {
12199:             $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);
12200:         }
12201:         if ($error) {
12202:             &Apache::lonnet::logthis($error);
12203:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12204:         }
12205:     }
12206: 
12207:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
12208:     if (ref($domconfig{'login'}) eq 'HASH') {
12209:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
12210:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
12211:                 if ($domservers{$lonhost}) {
12212:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12213:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
12214:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
12215:                     }
12216:                 }
12217:             }
12218:         }
12219:     }
12220:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
12221:     foreach my $lonhost (sort(keys(%domservers))) {
12222:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12223:             $changes{'headtag'}{$lonhost} = 1;
12224:         } else {
12225:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
12226:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
12227:             }
12228:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
12229:                 push(@newhosts,$lonhost);
12230:             } elsif ($currheadtagurls{$lonhost}) {
12231:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
12232:                 if ($currexempt{$lonhost}) {
12233:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
12234:                         $changes{'headtag'}{$lonhost} = 1;
12235:                     }
12236:                 } elsif ($possexempt{$lonhost}) {
12237:                     $changes{'headtag'}{$lonhost} = 1;
12238:                 }
12239:                 if ($possexempt{$lonhost}) {
12240:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12241:                 }
12242:             }
12243:         }
12244:     }
12245:     if (@newhosts) {
12246:         my $error;
12247:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12248:         if ($configuserok eq 'ok') {
12249:             if ($switchserver) {
12250:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
12251:             } elsif ($author_ok eq 'ok') {
12252:                 my $modified = [];
12253:                 foreach my $lonhost (@newhosts) {
12254:                     my $formelem = 'loginheadtag_'.$lonhost;
12255:                     (my $result,$newheadtagurls{$lonhost}) =
12256:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12257:                                                                 "login/headtag/$lonhost",'','',
12258:                                                                 $env{'form.loginheadtag_'.$lonhost.'.filename'},
12259:                                                                 $modified);
12260:                     if ($result eq 'ok') {
12261:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
12262:                         $changes{'headtag'}{$lonhost} = 1;
12263:                         if ($possexempt{$lonhost}) {
12264:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12265:                         }
12266:                     } else {
12267:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
12268:                                            $newheadtagurls{$lonhost},$result);
12269:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12270:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
12271:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
12272:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
12273:                         }
12274:                     }
12275:                 }
12276:                 &update_modify_urls($r,$modified);
12277:             } else {
12278:                 $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);
12279:             }
12280:         } else {
12281:             $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);
12282:         }
12283:         if ($error) {
12284:             &Apache::lonnet::logthis($error);
12285:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12286:         }
12287:     }
12288:     my @delsamlimg = &Apache::loncommon::get_env_multiple('form.saml_img_del');
12289:     my @newsamlimgs;
12290:     foreach my $lonhost (keys(%domservers)) {
12291:         if ($env{'form.saml_'.$lonhost}) {
12292:             if ($env{'form.saml_img_'.$lonhost.'.filename'}) {
12293:                 push(@newsamlimgs,$lonhost);
12294:             }
12295:             foreach my $item ('text','alt','url','title','window','notsso') {
12296:                 $env{'form.saml_'.$item.'_'.$lonhost} =~ s/^\s+|\s+$//g;
12297:             }
12298:             if ($saml{$lonhost}) {
12299:                 if ($env{'form.saml_window_'.$lonhost} ne '1') {
12300:                     $env{'form.saml_window_'.$lonhost} = '';
12301:                 }
12302:                 if (grep(/^\Q$lonhost\E$/,@delsamlimg)) {
12303: #FIXME Need to obsolete published image
12304:                     delete($currsaml{$lonhost}{'img'});
12305:                     $changes{'saml'}{$lonhost} = 1;
12306:                 }
12307:                 if ($env{'form.saml_alt_'.$lonhost} ne $samlalt{$lonhost}) {
12308:                     $changes{'saml'}{$lonhost} = 1;
12309:                 }
12310:                 if ($env{'form.saml_text_'.$lonhost} ne $samltext{$lonhost}) {
12311:                     $changes{'saml'}{$lonhost} = 1;
12312:                 }
12313:                 if ($env{'form.saml_url_'.$lonhost} ne $samlurl{$lonhost}) {
12314:                     $changes{'saml'}{$lonhost} = 1;
12315:                 }
12316:                 if ($env{'form.saml_title_'.$lonhost} ne $samltitle{$lonhost}) {
12317:                     $changes{'saml'}{$lonhost} = 1;
12318:                 }
12319:                 if ($env{'form.saml_window_'.$lonhost} ne $samlwindow{$lonhost}) {
12320:                     $changes{'saml'}{$lonhost} = 1;
12321:                 }
12322:                 if ($env{'form.saml_notsso_'.$lonhost} ne $samlnotsso{$lonhost}) {
12323:                     $changes{'saml'}{$lonhost} = 1;
12324:                 }
12325:             } else {
12326:                 $changes{'saml'}{$lonhost} = 1;
12327:             }
12328:             foreach my $item ('text','alt','url','title','window','notsso') {
12329:                 $currsaml{$lonhost}{$item} = $env{'form.saml_'.$item.'_'.$lonhost};
12330:             }
12331:         } else {
12332:             if ($saml{$lonhost}) {
12333:                 $changes{'saml'}{$lonhost} = 1;
12334:                 delete($currsaml{$lonhost});
12335:             }
12336:         }
12337:     }
12338:     foreach my $posshost (keys(%currsaml)) {
12339:         unless (exists($domservers{$posshost})) {
12340:             delete($currsaml{$posshost});
12341:         }
12342:     }
12343:     %{$loginhash{'login'}{'saml'}} = %currsaml;
12344:     if (@newsamlimgs) {
12345:         my $error;
12346:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12347:         if ($configuserok eq 'ok') {
12348:             if ($switchserver) {
12349:                 $error = &mt("Upload of SSO Button Image is not permitted to this server: [_1].",$switchserver);
12350:             } elsif ($author_ok eq 'ok') {
12351:                 my $modified = [];
12352:                 foreach my $lonhost (@newsamlimgs) {
12353:                     my $formelem = 'saml_img_'.$lonhost;
12354:                     my ($result,$imgurl) =
12355:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12356:                                                                 "login/saml/$lonhost",'','',
12357:                                                                 $env{'form.saml_img_'.$lonhost.'.filename'},
12358:                                                                 $modified);
12359:                     if ($result eq 'ok') {
12360:                         $currsaml{$lonhost}{'img'} = $imgurl;
12361:                         $loginhash{'login'}{'saml'}{$lonhost}{'img'} = $imgurl;
12362:                         $changes{'saml'}{$lonhost} = 1;
12363:                     } else {
12364:                         my $puberror = &mt("Upload of SSO button image failed for [_1] because an error occurred publishing the file in RES space. Error was: [_2].",
12365:                                            $lonhost,$result);
12366:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12367:                     }
12368:                 }
12369:                 &update_modify_urls($r,$modified);
12370:             } else {
12371:                 $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);
12372:             }
12373:         } else {
12374:             $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);
12375:         }
12376:         if ($error) {
12377:             &Apache::lonnet::logthis($error);
12378:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12379:         }
12380:     }
12381:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
12382: 
12383:     my $defaulthelpfile = '/adm/loginproblems.html';
12384:     my $defaulttext = &mt('Default in use');
12385: 
12386:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
12387:                                              $dom);
12388:     if ($putresult eq 'ok') {
12389:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12390:         my %defaultchecked = (
12391:                     'coursecatalog' => 'on',
12392:                     'helpdesk'      => 'on',
12393:                     'adminmail'     => 'off',
12394:                     'newuser'       => 'off',
12395:         );
12396:         if (ref($domconfig{'login'}) eq 'HASH') {
12397:             foreach my $item (@toggles) {
12398:                 if ($defaultchecked{$item} eq 'on') { 
12399:                     if (($domconfig{'login'}{$item} eq '0') &&
12400:                         ($env{'form.'.$item} eq '1')) {
12401:                         $changes{$item} = 1;
12402:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12403:                               $domconfig{'login'}{$item} eq '1') &&
12404:                              ($env{'form.'.$item} eq '0')) {
12405:                         $changes{$item} = 1;
12406:                     }
12407:                 } elsif ($defaultchecked{$item} eq 'off') {
12408:                     if (($domconfig{'login'}{$item} eq '1') &&
12409:                         ($env{'form.'.$item} eq '0')) {
12410:                         $changes{$item} = 1;
12411:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12412:                               $domconfig{'login'}{$item} eq '0') &&
12413:                              ($env{'form.'.$item} eq '1')) {
12414:                         $changes{$item} = 1;
12415:                     }
12416:                 }
12417:             }
12418:         }
12419:         if (keys(%changes) > 0 || $colchgtext) {
12420:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12421:             if (exists($changes{'saml'})) {
12422:                 my $hostid_in_use;
12423:                 my @hosts = &Apache::lonnet::current_machine_ids();
12424:                 if (@hosts > 1) {
12425:                     foreach my $hostid (@hosts) {
12426:                         if (&Apache::lonnet::host_domain($hostid) eq $dom) {
12427:                             $hostid_in_use = $hostid;
12428:                             last;
12429:                         }
12430:                     }
12431:                 } else {
12432:                     $hostid_in_use = $r->dir_config('lonHostID');
12433:                 }
12434:                 if (($hostid_in_use) &&
12435:                     (&Apache::lonnet::host_domain($hostid_in_use) eq $dom)) {
12436:                     &Apache::lonnet::devalidate_cache_new('samllanding',$hostid_in_use);
12437:                 }
12438:                 if (ref($lastactref) eq 'HASH') {
12439:                     if (ref($changes{'saml'}) eq 'HASH') {
12440:                         my %updates;
12441:                         map { $updates{$_} = 1; } keys(%{$changes{'saml'}});
12442:                         $lastactref->{'samllanding'} = \%updates;
12443:                     }
12444:                 }
12445:             }
12446:             if (ref($lastactref) eq 'HASH') {
12447:                 $lastactref->{'domainconfig'} = 1;
12448:             }
12449:             $resulttext = &mt('Changes made:').'<ul>';
12450:             foreach my $item (sort(keys(%changes))) {
12451:                 if ($item eq 'loginvia') {
12452:                     if (ref($changes{$item}) eq 'HASH') {
12453:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
12454:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12455:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
12456:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
12457:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
12458:                                     $protocol = 'http' if ($protocol ne 'https');
12459:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
12460: 
12461:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
12462:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
12463:                                     } else {
12464:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
12465:                                     }
12466:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
12467:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
12468:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
12469:                                     }
12470:                                     $resulttext .= '</li>';
12471:                                 } else {
12472:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
12473:                                 }
12474:                             } else {
12475:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
12476:                             }
12477:                         }
12478:                         $resulttext .= '</ul></li>';
12479:                     }
12480:                 } elsif ($item eq 'helpurl') {
12481:                     if (ref($changes{$item}) eq 'HASH') {
12482:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
12483:                             if (grep(/^\Q$lang\E$/,@delurls)) {
12484:                                 my ($chg,$link);
12485:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
12486:                                 if ($lang eq 'nolang') {
12487:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
12488:                                 } else {
12489:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
12490:                                 }
12491:                                 $resulttext .= '<li>'.$chg.'</li>';
12492:                             } else {
12493:                                 my $chg;
12494:                                 if ($lang eq 'nolang') {
12495:                                     $chg = &mt('custom log-in help file for no preferred language');
12496:                                 } else {
12497:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
12498:                                 }
12499:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
12500:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
12501:                                                       '?inhibitmenu=yes',$chg,600,500).
12502:                                                '</li>';
12503:                             }
12504:                         }
12505:                     }
12506:                 } elsif ($item eq 'headtag') {
12507:                     if (ref($changes{$item}) eq 'HASH') {
12508:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12509:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12510:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
12511:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12512:                                 $resulttext .= '<li><a href="'.
12513:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
12514:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
12515:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
12516:                                 if ($possexempt{$lonhost}) {
12517:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
12518:                                 } else {
12519:                                     $resulttext .= &mt('included for any client IP');
12520:                                 }
12521:                                 $resulttext .= '</li>';
12522:                             }
12523:                         }
12524:                     }
12525:                 } elsif ($item eq 'saml') {
12526:                     if (ref($changes{$item}) eq 'HASH') {
12527:                         my %notlt = (
12528:                                        text   => 'Text for log-in by SSO',
12529:                                        img    => 'SSO button image',
12530:                                        alt    => 'Alt text for button image',
12531:                                        url    => 'SSO URL',
12532:                                        title  => 'Tooltip for SSO link',
12533:                                        window => 'Pop-up window if iframe',
12534:                                        notsso => 'Text for non-SSO log-in',
12535:                                     );
12536:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12537:                             if (ref($currsaml{$lonhost}) eq 'HASH') {
12538:                                 $resulttext .= '<li>'.&mt("$title{$item} in use for [_1]","<b>$lonhost</b>").
12539:                                                '<ul>';
12540:                                 foreach my $key ('text','img','alt','url','title','window','notsso') {
12541:                                     if ($currsaml{$lonhost}{$key} eq '') {
12542:                                         $resulttext .= '<li>'.&mt("$notlt{$key} not in use").'</li>';
12543:                                     } else {
12544:                                         my $value = "'$currsaml{$lonhost}{$key}'";
12545:                                         if ($key eq 'img') {
12546:                                             $value = '<img src="'.$currsaml{$lonhost}{$key}.'" />';
12547:                                         } elsif ($key eq 'window') {
12548:                                             $value = 'On';
12549:                                         }
12550:                                         $resulttext .= '<li>'.&mt("$notlt{$key} set to: [_1]",
12551:                                                                   $value).'</li>';
12552:                                     }
12553:                                 }
12554:                                 $resulttext .= '</ul></li>';
12555:                             } else {
12556:                                 $resulttext .= '<li>'.&mt("$title{$item} not in use for [_1]",$lonhost).'</li>';
12557:                             }
12558:                         }
12559:                     }
12560:                 } elsif ($item eq 'captcha') {
12561:                     if (ref($loginhash{'login'}) eq 'HASH') {
12562:                         my $chgtxt;
12563:                         if ($loginhash{'login'}{$item} eq 'notused') {
12564:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
12565:                         } else {
12566:                             my %captchas = &captcha_phrases();
12567:                             if ($captchas{$loginhash{'login'}{$item}}) {
12568:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
12569:                             } else {
12570:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
12571:                             }
12572:                         }
12573:                         $resulttext .= '<li>'.$chgtxt.'</li>';
12574:                     }
12575:                 } elsif ($item eq 'recaptchakeys') {
12576:                     if (ref($loginhash{'login'}) eq 'HASH') {
12577:                         my ($privkey,$pubkey);
12578:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
12579:                             $pubkey = $loginhash{'login'}{$item}{'public'};
12580:                             $privkey = $loginhash{'login'}{$item}{'private'};
12581:                         }
12582:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
12583:                         if (!$pubkey) {
12584:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
12585:                         } else {
12586:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
12587:                         }
12588:                         if (!$privkey) {
12589:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
12590:                         } else {
12591:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
12592:                         }
12593:                         $chgtxt .= '</ul>';
12594:                         $resulttext .= '<li>'.$chgtxt.'</li>';
12595:                     }
12596:                 } elsif ($item eq 'recaptchaversion') {
12597:                     if (ref($loginhash{'login'}) eq 'HASH') {
12598:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
12599:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
12600:                                            '</li>';
12601:                         }
12602:                     }
12603:                 } else {
12604:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
12605:                 }
12606:             }
12607:             $resulttext .= $colchgtext.'</ul>';
12608:         } else {
12609:             $resulttext = &mt('No changes made to log-in page settings');
12610:         }
12611:     } else {
12612:         $resulttext = '<span class="LC_error">'.
12613: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12614:     }
12615:     if ($errors) {
12616:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
12617:                        $errors.'</ul>';
12618:     }
12619:     return $resulttext;
12620: }
12621: 
12622: sub check_exempt_addresses {
12623:     my ($iplist) = @_;
12624:     $iplist =~ s/^\s+//;
12625:     $iplist =~ s/\s+$//;
12626:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
12627:     my (@okips,$new);
12628:     foreach my $ip (@poss_ips) {
12629:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
12630:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
12631:                 push(@okips,$ip);
12632:             }
12633:         }
12634:     }
12635:     if (@okips > 0) {
12636:         $new = join(',',@okips);
12637:     } else {
12638:         $new = '';
12639:     }
12640:     return $new;
12641: }
12642: 
12643: sub color_font_choices {
12644:     my %choices =
12645:         &Apache::lonlocal::texthash (
12646:             img => "Header",
12647:             bgs => "Background colors",
12648:             links => "Link colors",
12649:             images => "Images",
12650:             font => "Font color",
12651:             fontmenu => "Font menu",
12652:             pgbg => "Page",
12653:             tabbg => "Header",
12654:             sidebg => "Border",
12655:             link => "Link",
12656:             alink => "Active link",
12657:             vlink => "Visited link",
12658:         );
12659:     return %choices;
12660: }
12661: 
12662: sub modify_ipaccess {
12663:     my ($dom,$lastactref,%domconfig) = @_;
12664:     my (@allpos,%changes,%confhash,$errors,$resulttext);
12665:     my (@items,%deletions,%itemids,@warnings);
12666:     my ($typeorder,$types) = &commblocktype_text();
12667:     if ($env{'form.ipaccess_add'}) {
12668:         my $name = $env{'form.ipaccess_name_add'};
12669:         my ($newid,$error) = &get_ipaccess_id($dom,$name);
12670:         if ($newid) {
12671:             $itemids{'add'} = $newid;
12672:             push(@items,'add');
12673:             $changes{$newid} = 1;
12674:         } else {
12675:             $error = &mt('Failed to acquire unique ID for new IP access control item');
12676:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12677:         }
12678:     }
12679:     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
12680:         my @todelete = &Apache::loncommon::get_env_multiple('form.ipaccess_del');
12681:         if (@todelete) {
12682:             map { $deletions{$_} = 1; } @todelete;
12683:         }
12684:         my $maxnum = $env{'form.ipaccess_maxnum'};
12685:         for (my $i=0; $i<$maxnum; $i++) {
12686:             my $itemid = $env{'form.ipaccess_id_'.$i};
12687:             $itemid =~ s/\D+//g;
12688:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
12689:                 if ($deletions{$itemid}) {
12690:                     $changes{$itemid} = $domconfig{'ipaccess'}{$itemid}{'name'};
12691:                 } else {
12692:                     push(@items,$i);
12693:                     $itemids{$i} = $itemid;
12694:                 }
12695:             }
12696:         }
12697:     }
12698:     foreach my $idx (@items) {
12699:         my $itemid = $itemids{$idx};
12700:         next unless ($itemid);
12701:         my %current;
12702:         unless ($idx eq 'add') {
12703:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
12704:                 %current = %{$domconfig{'ipaccess'}{$itemid}};
12705:             }
12706:         }
12707:         my $position = $env{'form.ipaccess_pos_'.$itemid};
12708:         $position =~ s/\D+//g;
12709:         if ($position ne '') {
12710:             $allpos[$position] = $itemid;
12711:         }
12712:         my $name = $env{'form.ipaccess_name_'.$idx};
12713:         $name =~ s/^\s+|\s+$//g;
12714:         $confhash{$itemid}{'name'} = $name;
12715:         my $possrange = $env{'form.ipaccess_range_'.$idx};
12716:         $possrange =~ s/^\s+|\s+$//g;
12717:         unless ($possrange eq '') {
12718:             $possrange =~ s/[\r\n]+/\s/g;
12719:             $possrange =~ s/\s*-\s*/-/g;
12720:             $possrange =~ s/\s+/,/g;
12721:             $possrange =~ s/,+/,/g;
12722:             if ($possrange ne '') {
12723:                 my (@ok,$count);
12724:                 $count = 0;
12725:                 foreach my $poss (split(/\,/,$possrange)) {
12726:                     $count ++;
12727:                     $poss = &validate_ip_pattern($poss);
12728:                     if ($poss ne '') {
12729:                         push(@ok,$poss);
12730:                     }
12731:                 }
12732:                 my $diff = $count - scalar(@ok);
12733:                 if ($diff) {
12734:                     $errors .= '<li><span class="LC_error">'.
12735:                                &mt('[quant,_1,IP] invalid and excluded from saved value for IP range(s) for [_2]',
12736:                                    $diff,$name).
12737:                                '</span></li>';
12738:                 }
12739:                 if (@ok) {
12740:                     my @cidr_list;
12741:                     foreach my $item (@ok) {
12742:                         @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
12743:                     }
12744:                     $confhash{$itemid}{'ip'} = join(',',@cidr_list);
12745:                 }
12746:             }
12747:         }
12748:         foreach my $field ('name','ip') {
12749:             unless (($idx eq 'add') || ($changes{$itemid})) {
12750:                 if ($current{$field} ne $confhash{$itemid}{$field}) {
12751:                     $changes{$itemid} = 1;
12752:                     last;
12753:                 }
12754:             }
12755:         }
12756:         $confhash{$itemid}{'commblocks'} = {};
12757: 
12758:         my %commblocks;
12759:         map { $commblocks{$_} = 1; } &Apache::loncommon::get_env_multiple('form.ipaccess_block_'.$idx);
12760:         foreach my $type (@{$typeorder}) {
12761:             if ($commblocks{$type}) {
12762:                 $confhash{$itemid}{'commblocks'}{$type} = 'on';
12763:             }
12764:             unless (($idx eq 'add') || ($changes{$itemid})) {
12765:                 if (ref($current{'commblocks'}) eq 'HASH') {
12766:                     if ($confhash{$itemid}{'commblocks'}{$type} ne $current{'commblocks'}{$type}) {
12767:                         $changes{$itemid} = 1;
12768:                     }
12769:                 } elsif ($confhash{$itemid}{'commblocks'}{$type}) {
12770:                     $changes{$itemid} = 1;
12771:                 }
12772:             }
12773:         }
12774:         $confhash{$itemid}{'courses'} = {};
12775:         my %crsdeletions;
12776:         my @delcrs = &Apache::loncommon::get_env_multiple('form.ipaccess_course_delete_'.$idx);
12777:         if (@delcrs) {
12778:             map { $crsdeletions{$_} = 1; } @delcrs;
12779:         }
12780:         if (ref($current{'courses'}) eq 'HASH') {
12781:             foreach my $cid (sort(keys(%{$current{'courses'}}))) {
12782:                 if ($crsdeletions{$cid}) {
12783:                     $changes{$itemid} = 1;
12784:                 } else {
12785:                     $confhash{$itemid}{'courses'}{$cid} = 1;
12786:                 }
12787:             }
12788:         }
12789:         $env{'form.ipaccess_cnum_'.$idx} =~ s/^\s+|\s+$//g;
12790:         $env{'form.ipaccess_cdom_'.$idx} =~ s/^\s+|\s+$//g;
12791:         if (($env{'form.ipaccess_cnum_'.$idx} =~ /^$match_courseid$/) &&
12792:             ($env{'form.ipaccess_cdom_'.$idx} =~ /^$match_domain$/)) {
12793:             if (&Apache::lonnet::homeserver($env{'form.ipaccess_cnum_'.$idx},
12794:                                             $env{'form.ipaccess_cdom_'.$idx}) eq 'no_host') {
12795:                 $errors .= '<li><span class="LC_error">'.
12796:                            &mt('Invalid courseID [_1] omitted from list of allowed courses',
12797:                                $env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}).
12798:                            '</span></li>';
12799:             } else {
12800:                 $confhash{$itemid}{'courses'}{$env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}} = 1;
12801:                 $changes{$itemid} = 1;
12802:             }
12803:         }
12804:     }
12805:     if (@allpos > 0) {
12806:         my $idx = 0;
12807:         foreach my $itemid (@allpos) {
12808:             if ($itemid ne '') {
12809:                 $confhash{$itemid}{'order'} = $idx;
12810:                 unless ($changes{$itemid}) {
12811:                     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
12812:                         if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
12813:                             if ($domconfig{'ipaccess'}{$itemid}{'order'} ne $idx) {
12814:                                 $changes{$itemid} = 1;
12815:                             }
12816:                         }
12817:                     }
12818:                 }
12819:                 $idx ++;
12820:             }
12821:         }
12822:     }
12823:     if (keys(%changes)) {
12824:         my %defaultshash = (
12825:                               ipaccess => \%confhash,
12826:                            );
12827:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
12828:                                                  $dom);
12829:         if ($putresult eq 'ok') {
12830:             my $cachetime = 1800;
12831:             &Apache::lonnet::do_cache_new('ipaccess',$dom,\%confhash,$cachetime);
12832:             if (ref($lastactref) eq 'HASH') {
12833:                 $lastactref->{'ipaccess'} = 1;
12834:             }
12835:             $resulttext = &mt('Changes made:').'<ul>';
12836:             my %bynum;
12837:             foreach my $itemid (sort(keys(%changes))) {
12838:                 if (ref($confhash{$itemid}) eq 'HASH') {
12839:                     my $position = $confhash{$itemid}{'order'};
12840:                     if ($position =~ /^\d+$/) {
12841:                         $bynum{$position} = $itemid;
12842:                     }
12843:                 }
12844:             }
12845:             if (keys(%deletions)) {
12846:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
12847:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
12848:                 }
12849:             }
12850:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
12851:                 my $itemid = $bynum{$pos};
12852:                 if (ref($confhash{$itemid}) eq 'HASH') {
12853:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'name'}.'</b><ul>';
12854:                     my $position = $pos + 1;
12855:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
12856:                     if ($confhash{$itemid}{'ip'} eq '') {
12857:                         $resulttext .= '<li>'.&mt('No IP Range(s) set').'</li>';
12858:                     } else {
12859:                         $resulttext .= '<li>'.&mt('IP Range(s): [_1]',$confhash{$itemid}{'ip'}).'</li>';
12860:                     }
12861:                     if (keys(%{$confhash{$itemid}{'commblocks'}})) {
12862:                         $resulttext .= '<li>'.&mt('Functionality Blocked: [_1]',
12863:                                                   join(', ', map { $types->{$_}; } sort(keys(%{$confhash{$itemid}{'commblocks'}})))).
12864:                                        '</li>';
12865:                     } else {
12866:                         $resulttext .= '<li>'.&mt('No functionality blocked').'</li>';
12867:                     }
12868:                     if (keys(%{$confhash{$itemid}{'courses'}})) {
12869:                         my @courses;
12870:                         foreach my $cid (sort(keys(%{$confhash{$itemid}{'courses'}}))) {
12871:                             my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
12872:                             push(@courses,$courseinfo{'description'}.' ('.$cid.')');
12873:                         }
12874:                         $resulttext .= '<li>'.&mt('Courses/Communities allowed').':<ul><li>'.
12875:                                              join('</li><li>',@courses).'</li></ul>';
12876:                     } else {
12877:                         $resulttext .= '<li>'.&mt('No courses allowed').'</li>';
12878:                     }
12879:                     $resulttext .= '</ul></li>';
12880:                 }
12881:             }
12882:             $resulttext .= '</ul>';
12883:         } else {
12884:             $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
12885:         }
12886:     } else {
12887:         $resulttext = &mt('No changes made');
12888:     }
12889:     if ($errors) {
12890:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
12891:                        $errors.'</ul></p>';
12892:     }
12893:     return $resulttext;
12894: }
12895: 
12896: sub get_ipaccess_id {
12897:     my ($domain,$location) = @_;
12898:     # get lock on ipaccess db
12899:     my $lockhash = {
12900:                       lock => $env{'user.name'}.
12901:                               ':'.$env{'user.domain'},
12902:                    };
12903:     my $tries = 0;
12904:     my $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
12905:     my ($id,$error);
12906: 
12907:     while (($gotlock ne 'ok') && ($tries<10)) {
12908:         $tries ++;
12909:         sleep (0.1);
12910:         $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
12911:     }
12912:     if ($gotlock eq 'ok') {
12913:         my %currids = &Apache::lonnet::dump_dom('ipaccess',$domain);
12914:         if ($currids{'lock'}) {
12915:             delete($currids{'lock'});
12916:             if (keys(%currids)) {
12917:                 my @curr = sort { $a <=> $b } keys(%currids);
12918:                 if ($curr[-1] =~ /^\d+$/) {
12919:                     $id = 1 + $curr[-1];
12920:                 }
12921:             } else {
12922:                 $id = 1;
12923:             }
12924:             if ($id) {
12925:                 unless (&Apache::lonnet::newput_dom('ipaccess',{ $id => $location },$domain) eq 'ok') {
12926:                     $error = 'nostore';
12927:                 }
12928:             } else {
12929:                 $error = 'nonumber';
12930:             }
12931:         }
12932:         my $dellockoutcome = &Apache::lonnet::del_dom('ipaccess',['lock'],$domain);
12933:     } else {
12934:         $error = 'nolock';
12935:     }
12936:     return ($id,$error);
12937: }
12938: 
12939: sub modify_rolecolors {
12940:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
12941:     my ($resulttext,%rolehash);
12942:     $rolehash{'rolecolors'} = {};
12943:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
12944:         if ($domconfig{'rolecolors'} eq '') {
12945:             $domconfig{'rolecolors'} = {};
12946:         }
12947:     }
12948:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
12949:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
12950:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
12951:                                              $dom);
12952:     if ($putresult eq 'ok') {
12953:         if (keys(%changes) > 0) {
12954:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12955:             if (ref($lastactref) eq 'HASH') {
12956:                 $lastactref->{'domainconfig'} = 1;
12957:             }
12958:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
12959:                                              $rolehash{'rolecolors'});
12960:         } else {
12961:             $resulttext = &mt('No changes made to default color schemes');
12962:         }
12963:     } else {
12964:         $resulttext = '<span class="LC_error">'.
12965: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12966:     }
12967:     if ($errors) {
12968:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12969:                        $errors.'</ul>';
12970:     }
12971:     return $resulttext;
12972: }
12973: 
12974: sub modify_colors {
12975:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
12976:     my (%changes,%choices);
12977:     my @bgs;
12978:     my @links = ('link','alink','vlink');
12979:     my @logintext;
12980:     my @images;
12981:     my $servadm = $r->dir_config('lonAdmEMail');
12982:     my $errors;
12983:     my %defaults;
12984:     foreach my $role (@{$roles}) {
12985:         if ($role eq 'login') {
12986:             %choices = &login_choices();
12987:             @logintext = ('textcol','bgcol');
12988:         } else {
12989:             %choices = &color_font_choices();
12990:         }
12991:         if ($role eq 'login') {
12992:             @images = ('img','logo','domlogo','login');
12993:             @bgs = ('pgbg','mainbg','sidebg');
12994:         } else {
12995:             @images = ('img');
12996:             @bgs = ('pgbg','tabbg','sidebg');
12997:         }
12998:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
12999:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
13000:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
13001:         }
13002:         if ($role eq 'login') {
13003:             foreach my $item (@logintext) {
13004:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13005:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13006:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13007:                 }
13008:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
13009:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13010:                 }
13011:             }
13012:         } else {
13013:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
13014:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
13015:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
13016:             }
13017:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
13018:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
13019:             }
13020:         }
13021:         foreach my $item (@bgs) {
13022:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13023:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13024:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13025:             }
13026:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
13027:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13028:             }
13029:         }
13030:         foreach my $item (@links) {
13031:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13032:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13033:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13034:             }
13035:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
13036:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13037:             }
13038:         }
13039:         my ($configuserok,$author_ok,$switchserver) = 
13040:             &config_check($dom,$confname,$servadm);
13041:         my ($width,$height) = &thumb_dimensions();
13042:         if (ref($domconfig->{$role}) ne 'HASH') {
13043:             $domconfig->{$role} = {};
13044:         }
13045:         foreach my $img (@images) {
13046:             if ($role eq 'login') {
13047:                 if (($img eq 'img') || ($img eq 'logo')) {  
13048:                     if (defined($env{'form.login_showlogo_'.$img})) {
13049:                         $confhash->{$role}{'showlogo'}{$img} = 1;
13050:                     } else { 
13051:                         $confhash->{$role}{'showlogo'}{$img} = 0;
13052:                     }
13053:                 }
13054:                 if ($env{'form.login_alt_'.$img} ne '') {
13055:                     $confhash->{$role}{'alttext'}{$img} = $env{'form.login_alt_'.$img};
13056:                 }
13057:             }
13058: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
13059: 		 && !defined($domconfig->{$role}{$img})
13060: 		 && !$env{'form.'.$role.'_del_'.$img}
13061: 		 && $env{'form.'.$role.'_import_'.$img}) {
13062: 		# import the old configured image from the .tab setting
13063: 		# if they haven't provided a new one 
13064: 		$domconfig->{$role}{$img} = 
13065: 		    $env{'form.'.$role.'_import_'.$img};
13066: 	    }
13067:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
13068:                 my $error;
13069:                 if ($configuserok eq 'ok') {
13070:                     if ($switchserver) {
13071:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
13072:                     } else {
13073:                         if ($author_ok eq 'ok') {
13074:                             my $modified = [];
13075:                             my ($result,$logourl) = 
13076:                                 &Apache::lonconfigsettings::publishlogo($r,'upload',$role.'_'.$img,
13077:                                                                         $dom,$confname,$img,$width,$height,
13078:                                                                         '',$modified);
13079:                             if ($result eq 'ok') {
13080:                                 $confhash->{$role}{$img} = $logourl;
13081:                                 $changes{$role}{'images'}{$img} = 1;
13082:                                 &update_modify_urls($r,$modified);
13083:                             } else {
13084:                                 $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);
13085:                             }
13086:                         } else {
13087:                             $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);
13088:                         }
13089:                     }
13090:                 } else {
13091:                     $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);
13092:                 }
13093:                 if ($error) {
13094:                     &Apache::lonnet::logthis($error);
13095:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13096:                 }
13097:             } elsif ($domconfig->{$role}{$img} ne '') {
13098:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
13099:                     my $error;
13100:                     if ($configuserok eq 'ok') {
13101: # is confname an author?
13102:                         if ($switchserver eq '') {
13103:                             if ($author_ok eq 'ok') {
13104:                                 my $modified = [];
13105:                                 my ($result,$logourl) = 
13106:                                     &Apache::lonconfigsettings::publishlogo($r,'copy',$domconfig->{$role}{$img},
13107:                                                                             $dom,$confname,$img,$width,$height,
13108:                                                                             '',$modified);
13109:                                 if ($result eq 'ok') {
13110:                                     $confhash->{$role}{$img} = $logourl;
13111: 				    $changes{$role}{'images'}{$img} = 1;
13112:                                     &update_modify_urls($r,$modified);
13113:                                 }
13114:                             }
13115:                         }
13116:                     }
13117:                 }
13118:             }
13119:         }
13120:         if (ref($domconfig) eq 'HASH') {
13121:             if (ref($domconfig->{$role}) eq 'HASH') {
13122:                 foreach my $img (@images) {
13123:                     if ($domconfig->{$role}{$img} ne '') {
13124:                         if ($env{'form.'.$role.'_del_'.$img}) {
13125:                             $confhash->{$role}{$img} = '';
13126:                             $changes{$role}{'images'}{$img} = 1;
13127:                         } else {
13128:                             if ($confhash->{$role}{$img} eq '') {
13129:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
13130:                             }
13131:                         }
13132:                     } else {
13133:                         if ($env{'form.'.$role.'_del_'.$img}) {
13134:                             $confhash->{$role}{$img} = '';
13135:                             $changes{$role}{'images'}{$img} = 1;
13136:                         } 
13137:                     }
13138:                     if ($role eq 'login') {
13139:                         if (($img eq 'logo') || ($img eq 'img')) {
13140:                             if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
13141:                                 if ($confhash->{$role}{'showlogo'}{$img} ne 
13142:                                     $domconfig->{$role}{'showlogo'}{$img}) {
13143:                                     $changes{$role}{'showlogo'}{$img} = 1; 
13144:                                 }
13145:                             } else {
13146:                                 if ($confhash->{$role}{'showlogo'}{$img} == 0) {
13147:                                     $changes{$role}{'showlogo'}{$img} = 1;
13148:                                 }
13149:                             }
13150:                         }
13151:                         if ($img ne 'login') {
13152:                             if (ref($domconfig->{$role}{'alttext'}) eq 'HASH') {
13153:                                 if ($confhash->{$role}{'alttext'}{$img} ne
13154:                                     $domconfig->{$role}{'alttext'}{$img}) {
13155:                                     $changes{$role}{'alttext'}{$img} = 1;
13156:                                 }
13157:                             } else {
13158:                                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
13159:                                     $changes{$role}{'alttext'}{$img} = 1;
13160:                                 }
13161:                             }
13162:                         }
13163:                     }
13164:                 }
13165:                 if ($domconfig->{$role}{'font'} ne '') {
13166:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
13167:                         $changes{$role}{'font'} = 1;
13168:                     }
13169:                 } else {
13170:                     if ($confhash->{$role}{'font'}) {
13171:                         $changes{$role}{'font'} = 1;
13172:                     }
13173:                 }
13174:                 if ($role ne 'login') {
13175:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
13176:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
13177:                             $changes{$role}{'fontmenu'} = 1;
13178:                         }
13179:                     } else {
13180:                         if ($confhash->{$role}{'fontmenu'}) {
13181:                             $changes{$role}{'fontmenu'} = 1;
13182:                         }
13183:                     }
13184:                 }
13185:                 foreach my $item (@bgs) {
13186:                     if ($domconfig->{$role}{$item} ne '') {
13187:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13188:                             $changes{$role}{'bgs'}{$item} = 1;
13189:                         } 
13190:                     } else {
13191:                         if ($confhash->{$role}{$item}) {
13192:                             $changes{$role}{'bgs'}{$item} = 1;
13193:                         }
13194:                     }
13195:                 }
13196:                 foreach my $item (@links) {
13197:                     if ($domconfig->{$role}{$item} ne '') {
13198:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13199:                             $changes{$role}{'links'}{$item} = 1;
13200:                         }
13201:                     } else {
13202:                         if ($confhash->{$role}{$item}) {
13203:                             $changes{$role}{'links'}{$item} = 1;
13204:                         }
13205:                     }
13206:                 }
13207:                 foreach my $item (@logintext) {
13208:                     if ($domconfig->{$role}{$item} ne '') {
13209:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13210:                             $changes{$role}{'logintext'}{$item} = 1;
13211:                         }
13212:                     } else {
13213:                         if ($confhash->{$role}{$item}) {
13214:                             $changes{$role}{'logintext'}{$item} = 1;
13215:                         }
13216:                     }
13217:                 }
13218:             } else {
13219:                 &default_change_checker($role,\@images,\@links,\@bgs,
13220:                                         \@logintext,$confhash,\%changes); 
13221:             }
13222:         } else {
13223:             &default_change_checker($role,\@images,\@links,\@bgs,
13224:                                     \@logintext,$confhash,\%changes); 
13225:         }
13226:     }
13227:     return ($errors,%changes);
13228: }
13229: 
13230: sub config_check {
13231:     my ($dom,$confname,$servadm) = @_;
13232:     my ($configuserok,$author_ok,$switchserver,%currroles);
13233:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
13234:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
13235:                                                    $confname,$servadm);
13236:     if ($configuserok eq 'ok') {
13237:         $switchserver = &check_switchserver($dom,$confname);
13238:         if ($switchserver eq '') {
13239:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
13240:         }
13241:     }
13242:     return ($configuserok,$author_ok,$switchserver);
13243: }
13244: 
13245: sub default_change_checker {
13246:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
13247:     foreach my $item (@{$links}) {
13248:         if ($confhash->{$role}{$item}) {
13249:             $changes->{$role}{'links'}{$item} = 1;
13250:         }
13251:     }
13252:     foreach my $item (@{$bgs}) {
13253:         if ($confhash->{$role}{$item}) {
13254:             $changes->{$role}{'bgs'}{$item} = 1;
13255:         }
13256:     }
13257:     foreach my $item (@{$logintext}) {
13258:         if ($confhash->{$role}{$item}) {
13259:             $changes->{$role}{'logintext'}{$item} = 1;
13260:         }
13261:     }
13262:     foreach my $img (@{$images}) {
13263:         if ($env{'form.'.$role.'_del_'.$img}) {
13264:             $confhash->{$role}{$img} = '';
13265:             $changes->{$role}{'images'}{$img} = 1;
13266:         }
13267:         if ($role eq 'login') {
13268:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
13269:                 $changes->{$role}{'showlogo'}{$img} = 1;
13270:             }
13271:             if (ref($confhash->{$role}{'alttext'}) eq 'HASH') {
13272:                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
13273:                     $changes->{$role}{'alttext'}{$img} = 1;
13274:                 }
13275:             }
13276:         }
13277:     }
13278:     if ($confhash->{$role}{'font'}) {
13279:         $changes->{$role}{'font'} = 1;
13280:     }
13281: }
13282: 
13283: sub display_colorchgs {
13284:     my ($dom,$changes,$roles,$confhash) = @_;
13285:     my (%choices,$resulttext);
13286:     if (!grep(/^login$/,@{$roles})) {
13287:         $resulttext = &mt('Changes made:').'<br />';
13288:     }
13289:     foreach my $role (@{$roles}) {
13290:         if ($role eq 'login') {
13291:             %choices = &login_choices();
13292:         } else {
13293:             %choices = &color_font_choices();
13294:         }
13295:         if (ref($changes->{$role}) eq 'HASH') {
13296:             if ($role ne 'login') {
13297:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
13298:             }
13299:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
13300:                 if ($role ne 'login') {
13301:                     $resulttext .= '<ul>';
13302:                 }
13303:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
13304:                     if ($role ne 'login') {
13305:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
13306:                     }
13307:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
13308:                         if (($role eq 'login') && ($key eq 'showlogo')) {
13309:                             if ($confhash->{$role}{$key}{$item}) {
13310:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
13311:                             } else {
13312:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
13313:                             }
13314:                         } elsif (($role eq 'login') && ($key eq 'alttext')) {
13315:                             if ($confhash->{$role}{$key}{$item} ne '') {
13316:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} set to [_1].",
13317:                                                $confhash->{$role}{$key}{$item}).'</li>';
13318:                             } else {
13319:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} deleted.").'</li>';
13320:                             }
13321:                         } elsif ($confhash->{$role}{$item} eq '') {
13322:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
13323:                         } else {
13324:                             my $newitem = $confhash->{$role}{$item};
13325:                             if ($key eq 'images') {
13326:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
13327:                             }
13328:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
13329:                         }
13330:                     }
13331:                     if ($role ne 'login') {
13332:                         $resulttext .= '</ul></li>';
13333:                     }
13334:                 } else {
13335:                     if ($confhash->{$role}{$key} eq '') {
13336:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
13337:                     } else {
13338:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
13339:                     }
13340:                 }
13341:                 if ($role ne 'login') {
13342:                     $resulttext .= '</ul>';
13343:                 }
13344:             }
13345:         }
13346:     }
13347:     return $resulttext;
13348: }
13349: 
13350: sub thumb_dimensions {
13351:     return ('200','50');
13352: }
13353: 
13354: sub check_dimensions {
13355:     my ($inputfile) = @_;
13356:     my ($fullwidth,$fullheight);
13357:     if ($inputfile =~ m|^[/\w.\-]+$|) {
13358:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
13359:             my $imageinfo = <PIPE>;
13360:             if (!close(PIPE)) {
13361:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
13362:             }
13363:             chomp($imageinfo);
13364:             my ($fullsize) = 
13365:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
13366:             if ($fullsize) {
13367:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
13368:             }
13369:         }
13370:     }
13371:     return ($fullwidth,$fullheight);
13372: }
13373: 
13374: sub check_configuser {
13375:     my ($uhome,$dom,$confname,$servadm) = @_;
13376:     my ($configuserok,%currroles);
13377:     if ($uhome eq 'no_host') {
13378:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
13379:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
13380:         $configuserok = 
13381:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
13382:                              $configpass,'','','','','',undef,$servadm);
13383:     } else {
13384:         $configuserok = 'ok';
13385:         %currroles = 
13386:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
13387:     }
13388:     return ($configuserok,%currroles);
13389: }
13390: 
13391: sub check_authorstatus {
13392:     my ($dom,$confname,%currroles) = @_;
13393:     my $author_ok;
13394:     if (!$currroles{':'.$dom.':au'}) {
13395:         my $start = time;
13396:         my $end = 0;
13397:         $author_ok = 
13398:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
13399:                                         'au',$end,$start,'','','domconfig');
13400:     } else {
13401:         $author_ok = 'ok';
13402:     }
13403:     return $author_ok;
13404: }
13405: 
13406: sub update_modify_urls {
13407:     my ($r,$modified) = @_;
13408:     if ((ref($modified) eq 'ARRAY') && (@{$modified})) {
13409:         push(@{$modified_urls},$modified);
13410:         unless ($registered_cleanup) {
13411:             my $handlers = $r->get_handlers('PerlCleanupHandler');
13412:             $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
13413:             $registered_cleanup=1;
13414:         }
13415:     }
13416: }
13417: 
13418: sub notifysubscribed {
13419:     foreach my $targetsource (@{$modified_urls}){
13420:         next unless (ref($targetsource) eq 'ARRAY');
13421:         my ($target,$source)=@{$targetsource};
13422:         if ($source ne '') {
13423:             if (open(my $logfh,">>",$source.'.log')) {
13424:                 print $logfh "\nCleanup phase: Notifications\n";
13425:                 my @subscribed=&subscribed_hosts($target);
13426:                 foreach my $subhost (@subscribed) {
13427:                     print $logfh "\nNotifying host ".$subhost.':';
13428:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
13429:                     print $logfh $reply;
13430:                 }
13431:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
13432:                 foreach my $subhost (@subscribedmeta) {
13433:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
13434:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
13435:                                                         $subhost);
13436:                     print $logfh $reply;
13437:                 }
13438:                 print $logfh "\n============ Done ============\n";
13439:                 close($logfh);
13440:             }
13441:         }
13442:     }
13443:     return OK;
13444: }
13445: 
13446: sub subscribed_hosts {
13447:     my ($target) = @_;
13448:     my @subscribed;
13449:     if (open(my $fh,"<","$target.subscription")) {
13450:         while (my $subline=<$fh>) {
13451:             if ($subline =~ /^($match_lonid):/) {
13452:                 my $host = $1;
13453:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
13454:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
13455:                         push(@subscribed,$host);
13456:                     }
13457:                 }
13458:             }
13459:         }
13460:     }
13461:     return @subscribed;
13462: }
13463: 
13464: sub check_switchserver {
13465:     my ($dom,$confname) = @_;
13466:     my ($allowed,$switchserver,$home);
13467:     if ($confname eq '') {
13468:         $home = &Apache::lonnet::domain($dom,'primary');
13469:     } else {
13470:         $home = &Apache::lonnet::homeserver($confname,$dom);
13471:         if ($home eq 'no_host') {
13472:             $home = &Apache::lonnet::domain($dom,'primary');
13473:         }
13474:     }
13475:     my @ids=&Apache::lonnet::current_machine_ids();
13476:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
13477:     if (!$allowed) {
13478: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role='.
13479:                       &HTML::Entities::encode($env{'request.role'},'\'<>"&').
13480:                       '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
13481:     }
13482:     return $switchserver;
13483: }
13484: 
13485: sub modify_quotas {
13486:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13487:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
13488:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
13489:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
13490:         $validationfieldsref);
13491:     if ($action eq 'quotas') {
13492:         $context = 'tools'; 
13493:     } else {
13494:         $context = $action;
13495:     }
13496:     if ($context eq 'requestcourses') {
13497:         @usertools = ('official','unofficial','community','textbook','placement','lti');
13498:         @options =('norequest','approval','validate','autolimit');
13499:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
13500:         %titles = &courserequest_titles();
13501:         $toolregexp = join('|',@usertools);
13502:         %conditions = &courserequest_conditions();
13503:         $confname = $dom.'-domainconfig';
13504:         my $servadm = $r->dir_config('lonAdmEMail');
13505:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
13506:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
13507:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
13508:     } elsif ($context eq 'requestauthor') {
13509:         @usertools = ('author');
13510:         %titles = &authorrequest_titles();
13511:     } else {
13512:         @usertools = ('aboutme','blog','webdav','portfolio','timezone');
13513:         %titles = &tool_titles();
13514:     }
13515:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13516:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13517:     foreach my $key (keys(%env)) {
13518:         if ($context eq 'requestcourses') {
13519:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
13520:                 my $item = $1;
13521:                 my $type = $2;
13522:                 if ($type =~ /^limit_(.+)/) {
13523:                     $limithash{$item}{$1} = $env{$key};
13524:                 } else {
13525:                     $confhash{$item}{$type} = $env{$key};
13526:                 }
13527:             }
13528:         } elsif ($context eq 'requestauthor') {
13529:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
13530:                 $confhash{$1} = $env{$key};
13531:             }
13532:         } else {
13533:             if ($key =~ /^form\.quota_(.+)$/) {
13534:                 $confhash{'defaultquota'}{$1} = $env{$key};
13535:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
13536:                 $confhash{'authorquota'}{$1} = $env{$key};
13537:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
13538:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
13539:             }
13540:         }
13541:     }
13542:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
13543:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
13544:         @approvalnotify = sort(@approvalnotify);
13545:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
13546:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
13547:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
13548:         foreach my $type (@hasuniquecode) {
13549:             if (grep(/^\Q$type\E$/,@crstypes)) {
13550:                 $confhash{'uniquecode'}{$type} = 1;
13551:             }
13552:         }
13553:         my (%newbook,%allpos);
13554:         if ($context eq 'requestcourses') {
13555:             foreach my $type ('textbooks','templates') {
13556:                 @{$allpos{$type}} = (); 
13557:                 my $invalid;
13558:                 if ($type eq 'textbooks') {
13559:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
13560:                 } else {
13561:                     $invalid = &mt('Invalid LON-CAPA course for template');
13562:                 }
13563:                 if ($env{'form.'.$type.'_addbook'}) {
13564:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
13565:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
13566:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
13567:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
13568:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
13569:                         } else {
13570:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
13571:                             my $position = $env{'form.'.$type.'_addbook_pos'};
13572:                             $position =~ s/\D+//g;
13573:                             if ($position ne '') {
13574:                                 $allpos{$type}[$position] = $newbook{$type};
13575:                             }
13576:                         }
13577:                     } else {
13578:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
13579:                     }
13580:                 }
13581:             } 
13582:         }
13583:         if (ref($domconfig{$action}) eq 'HASH') {
13584:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
13585:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
13586:                     $changes{'notify'}{'approval'} = 1;
13587:                 }
13588:             } else {
13589:                 if ($confhash{'notify'}{'approval'}) {
13590:                     $changes{'notify'}{'approval'} = 1;
13591:                 }
13592:             }
13593:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
13594:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
13595:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
13596:                         unless ($confhash{'uniquecode'}{$crstype}) {
13597:                             $changes{'uniquecode'} = 1;
13598:                         }
13599:                     }
13600:                     unless ($changes{'uniquecode'}) {
13601:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
13602:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
13603:                                 $changes{'uniquecode'} = 1;
13604:                             }
13605:                         }
13606:                     }
13607:                } else {
13608:                    $changes{'uniquecode'} = 1;
13609:                }
13610:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
13611:                 $changes{'uniquecode'} = 1;
13612:             }
13613:             if ($context eq 'requestcourses') {
13614:                 foreach my $type ('textbooks','templates') {
13615:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
13616:                         my %deletions;
13617:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
13618:                         if (@todelete) {
13619:                             map { $deletions{$_} = 1; } @todelete;
13620:                         }
13621:                         my %imgdeletions;
13622:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
13623:                         if (@todeleteimages) {
13624:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
13625:                         }
13626:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
13627:                         for (my $i=0; $i<=$maxnum; $i++) {
13628:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
13629:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
13630:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
13631:                                 if ($deletions{$key}) {
13632:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
13633:                                         #FIXME need to obsolete item in RES space
13634:                                     }
13635:                                     next;
13636:                                 } else {
13637:                                     my $newpos = $env{'form.'.$itemid};
13638:                                     $newpos =~ s/\D+//g;
13639:                                     foreach my $item ('subject','title','publisher','author') {
13640:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
13641:                                                  ($type eq 'templates'));
13642:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
13643:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
13644:                                             $changes{$type}{$key} = 1;
13645:                                         }
13646:                                     }
13647:                                     $allpos{$type}[$newpos] = $key;
13648:                                 }
13649:                                 if ($imgdeletions{$key}) {
13650:                                     $changes{$type}{$key} = 1;
13651:                                     #FIXME need to obsolete item in RES space
13652:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
13653:                                     my ($cdom,$cnum) = split(/_/,$key);
13654:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
13655:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
13656:                                     } else {
13657:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
13658:                                                                                       $cdom,$cnum,$type,$configuserok,
13659:                                                                                       $switchserver,$author_ok);
13660:                                         if ($imgurl) {
13661:                                             $confhash{$type}{$key}{'image'} = $imgurl;
13662:                                             $changes{$type}{$key} = 1; 
13663:                                         }
13664:                                         if ($error) {
13665:                                             &Apache::lonnet::logthis($error);
13666:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13667:                                         }
13668:                                     }
13669:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
13670:                                     $confhash{$type}{$key}{'image'} = 
13671:                                         $domconfig{$action}{$type}{$key}{'image'};
13672:                                 }
13673:                             }
13674:                         }
13675:                     }
13676:                 }
13677:             }
13678:         } else {
13679:             if ($confhash{'notify'}{'approval'}) {
13680:                 $changes{'notify'}{'approval'} = 1;
13681:             }
13682:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
13683:                 $changes{'uniquecode'} = 1;
13684:             }
13685:         }
13686:         if ($context eq 'requestcourses') {
13687:             foreach my $type ('textbooks','templates') {
13688:                 if ($newbook{$type}) {
13689:                     $changes{$type}{$newbook{$type}} = 1;
13690:                     foreach my $item ('subject','title','publisher','author') {
13691:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
13692:                                  ($type eq 'template'));
13693:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
13694:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
13695:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
13696:                         }
13697:                     }
13698:                     if ($type eq 'textbooks') {
13699:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
13700:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
13701:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
13702:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
13703:                             } else {
13704:                                 my ($imageurl,$error) =
13705:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
13706:                                                             $configuserok,$switchserver,$author_ok);
13707:                                 if ($imageurl) {
13708:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
13709:                                 }
13710:                                 if ($error) {
13711:                                     &Apache::lonnet::logthis($error);
13712:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13713:                                 }
13714:                             }
13715:                         }
13716:                     }
13717:                 }
13718:                 if (@{$allpos{$type}} > 0) {
13719:                     my $idx = 0;
13720:                     foreach my $item (@{$allpos{$type}}) {
13721:                         if ($item ne '') {
13722:                             $confhash{$type}{$item}{'order'} = $idx;
13723:                             if (ref($domconfig{$action}) eq 'HASH') {
13724:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
13725:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
13726:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
13727:                                             $changes{$type}{$item} = 1;
13728:                                         }
13729:                                     }
13730:                                 }
13731:                             }
13732:                             $idx ++;
13733:                         }
13734:                     }
13735:                 }
13736:             }
13737:             if (ref($validationitemsref) eq 'ARRAY') {
13738:                 foreach my $item (@{$validationitemsref}) {
13739:                     if ($item eq 'fields') {
13740:                         my @changed;
13741:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
13742:                         if (@{$confhash{'validation'}{$item}} > 0) {
13743:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
13744:                         }
13745:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13746:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13747:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
13748:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
13749:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
13750:                                 } else {
13751:                                     @changed = @{$confhash{'validation'}{$item}};
13752:                                 }
13753:                             } else {
13754:                                 @changed = @{$confhash{'validation'}{$item}};
13755:                             }
13756:                         } else {
13757:                             @changed = @{$confhash{'validation'}{$item}};
13758:                         }
13759:                         if (@changed) {
13760:                             if ($confhash{'validation'}{$item}) {
13761:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
13762:                             } else {
13763:                                 $changes{'validation'}{$item} = &mt('None');
13764:                             }
13765:                         }
13766:                     } else {
13767:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
13768:                         if ($item eq 'markup') {
13769:                             if ($env{'form.requestcourses_validation_'.$item}) {
13770:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13771:                             }
13772:                         }
13773:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13774:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13775:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
13776:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13777:                                 }
13778:                             } else {
13779:                                 if ($confhash{'validation'}{$item} ne '') {
13780:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13781:                                 }
13782:                             }
13783:                         } else {
13784:                             if ($confhash{'validation'}{$item} ne '') {
13785:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13786:                             }
13787:                         }
13788:                     }
13789:                 }
13790:             }
13791:             if ($env{'form.validationdc'}) {
13792:                 my $newval = $env{'form.validationdc'};
13793:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
13794:                 if (exists($domcoords{$newval})) {
13795:                     $confhash{'validation'}{'dc'} = $newval;
13796:                 }
13797:             }
13798:             if (ref($confhash{'validation'}) eq 'HASH') {
13799:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13800:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13801:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
13802:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
13803:                                 if ($confhash{'validation'}{'dc'} eq '') {
13804:                                     $changes{'validation'}{'dc'} = &mt('None');
13805:                                 } else {
13806:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13807:                                 }
13808:                             }
13809:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
13810:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13811:                         }
13812:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
13813:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13814:                     }
13815:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
13816:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13817:                 }
13818:             } else {
13819:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13820:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13821:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
13822:                             $changes{'validation'}{'dc'} = &mt('None');
13823:                         }
13824:                     }
13825:                 }
13826:             }
13827:         }
13828:     } else {
13829:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
13830:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
13831:     }
13832:     foreach my $item (@usertools) {
13833:         foreach my $type (@{$types},'default','_LC_adv') {
13834:             my $unset; 
13835:             if ($context eq 'requestcourses') {
13836:                 $unset = '0';
13837:                 if ($type eq '_LC_adv') {
13838:                     $unset = '';
13839:                 }
13840:                 if ($confhash{$item}{$type} eq 'autolimit') {
13841:                     $confhash{$item}{$type} .= '=';
13842:                     unless ($limithash{$item}{$type} =~ /\D/) {
13843:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
13844:                     }
13845:                 }
13846:             } elsif ($context eq 'requestauthor') {
13847:                 $unset = '0';
13848:                 if ($type eq '_LC_adv') {
13849:                     $unset = '';
13850:                 }
13851:             } else {
13852:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
13853:                     $confhash{$item}{$type} = 1;
13854:                 } else {
13855:                     $confhash{$item}{$type} = 0;
13856:                 }
13857:             }
13858:             if (ref($domconfig{$action}) eq 'HASH') {
13859:                 if ($action eq 'requestauthor') {
13860:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
13861:                         $changes{$type} = 1;
13862:                     }
13863:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
13864:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
13865:                         $changes{$item}{$type} = 1;
13866:                     }
13867:                 } else {
13868:                     if ($context eq 'requestcourses') {
13869:                         if ($confhash{$item}{$type} ne $unset) {
13870:                             $changes{$item}{$type} = 1;
13871:                         }
13872:                     } else {
13873:                         if (!$confhash{$item}{$type}) {
13874:                             $changes{$item}{$type} = 1;
13875:                         }
13876:                     }
13877:                 }
13878:             } else {
13879:                 if ($context eq 'requestcourses') {
13880:                     if ($confhash{$item}{$type} ne $unset) {
13881:                         $changes{$item}{$type} = 1;
13882:                     }
13883:                 } elsif ($context eq 'requestauthor') {
13884:                     if ($confhash{$type} ne $unset) {
13885:                         $changes{$type} = 1;
13886:                     }
13887:                 } else {
13888:                     if (!$confhash{$item}{$type}) {
13889:                         $changes{$item}{$type} = 1;
13890:                     }
13891:                 }
13892:             }
13893:         }
13894:     }
13895:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
13896:         if (ref($domconfig{'quotas'}) eq 'HASH') {
13897:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
13898:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
13899:                     if (exists($confhash{'defaultquota'}{$key})) {
13900:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
13901:                             $changes{'defaultquota'}{$key} = 1;
13902:                         }
13903:                     } else {
13904:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
13905:                     }
13906:                 }
13907:             } else {
13908:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
13909:                     if (exists($confhash{'defaultquota'}{$key})) {
13910:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
13911:                             $changes{'defaultquota'}{$key} = 1;
13912:                         }
13913:                     } else {
13914:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
13915:                     }
13916:                 }
13917:             }
13918:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
13919:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
13920:                     if (exists($confhash{'authorquota'}{$key})) {
13921:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
13922:                             $changes{'authorquota'}{$key} = 1;
13923:                         }
13924:                     } else {
13925:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
13926:                     }
13927:                 }
13928:             }
13929:         }
13930:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
13931:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
13932:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
13933:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
13934:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
13935:                             $changes{'defaultquota'}{$key} = 1;
13936:                         }
13937:                     } else {
13938:                         if (!exists($domconfig{'quotas'}{$key})) {
13939:                             $changes{'defaultquota'}{$key} = 1;
13940:                         }
13941:                     }
13942:                 } else {
13943:                     $changes{'defaultquota'}{$key} = 1;
13944:                 }
13945:             }
13946:         }
13947:         if (ref($confhash{'authorquota'}) eq 'HASH') {
13948:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
13949:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
13950:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
13951:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
13952:                             $changes{'authorquota'}{$key} = 1;
13953:                         }
13954:                     } else {
13955:                         $changes{'authorquota'}{$key} = 1;
13956:                     }
13957:                 } else {
13958:                     $changes{'authorquota'}{$key} = 1;
13959:                 }
13960:             }
13961:         }
13962:     }
13963: 
13964:     if ($context eq 'requestauthor') {
13965:         $domdefaults{'requestauthor'} = \%confhash;
13966:     } else {
13967:         foreach my $key (keys(%confhash)) {
13968:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
13969:                 $domdefaults{$key} = $confhash{$key};
13970:             }
13971:         }
13972:     }
13973: 
13974:     my %quotahash = (
13975:                       $action => { %confhash }
13976:                     );
13977:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
13978:                                              $dom);
13979:     if ($putresult eq 'ok') {
13980:         if (keys(%changes) > 0) {
13981:             my $cachetime = 24*60*60;
13982:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13983:             if (ref($lastactref) eq 'HASH') {
13984:                 $lastactref->{'domdefaults'} = 1;
13985:             }
13986:             $resulttext = &mt('Changes made:').'<ul>';
13987:             unless (($context eq 'requestcourses') ||
13988:                     ($context eq 'requestauthor')) {
13989:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
13990:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
13991:                     foreach my $type (@{$types},'default') {
13992:                         if (defined($changes{'defaultquota'}{$type})) {
13993:                             my $typetitle = $usertypes->{$type};
13994:                             if ($type eq 'default') {
13995:                                 $typetitle = $othertitle;
13996:                             }
13997:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
13998:                         }
13999:                     }
14000:                     $resulttext .= '</ul></li>';
14001:                 }
14002:                 if (ref($changes{'authorquota'}) eq 'HASH') {
14003:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
14004:                     foreach my $type (@{$types},'default') {
14005:                         if (defined($changes{'authorquota'}{$type})) {
14006:                             my $typetitle = $usertypes->{$type};
14007:                             if ($type eq 'default') {
14008:                                 $typetitle = $othertitle;
14009:                             }
14010:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
14011:                         }
14012:                     }
14013:                     $resulttext .= '</ul></li>';
14014:                 }
14015:             }
14016:             my %newenv;
14017:             foreach my $item (@usertools) {
14018:                 my (%haschgs,%inconf);
14019:                 if ($context eq 'requestauthor') {
14020:                     %haschgs = %changes;
14021:                     %inconf = %confhash;
14022:                 } else {
14023:                     if (ref($changes{$item}) eq 'HASH') {
14024:                         %haschgs = %{$changes{$item}};
14025:                     }
14026:                     if (ref($confhash{$item}) eq 'HASH') {
14027:                         %inconf = %{$confhash{$item}};
14028:                     }
14029:                 }
14030:                 if (keys(%haschgs) > 0) {
14031:                     my $newacc = 
14032:                         &Apache::lonnet::usertools_access($env{'user.name'},
14033:                                                           $env{'user.domain'},
14034:                                                           $item,'reload',$context);
14035:                     if (($context eq 'requestcourses') ||
14036:                         ($context eq 'requestauthor')) {
14037:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
14038:                             $newenv{'environment.canrequest.'.$item} = $newacc;
14039:                         }
14040:                     } else {
14041:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
14042:                             $newenv{'environment.availabletools.'.$item} = $newacc;
14043:                         }
14044:                     }
14045:                     unless ($context eq 'requestauthor') {
14046:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
14047:                     }
14048:                     foreach my $type (@{$types},'default','_LC_adv') {
14049:                         if ($haschgs{$type}) {
14050:                             my $typetitle = $usertypes->{$type};
14051:                             if ($type eq 'default') {
14052:                                 $typetitle = $othertitle;
14053:                             } elsif ($type eq '_LC_adv') {
14054:                                 $typetitle = 'LON-CAPA Advanced Users'; 
14055:                             }
14056:                             if ($inconf{$type}) {
14057:                                 if ($context eq 'requestcourses') {
14058:                                     my $cond;
14059:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
14060:                                         if ($1 eq '') {
14061:                                             $cond = &mt('(Automatic processing of any request).');
14062:                                         } else {
14063:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
14064:                                         }
14065:                                     } else { 
14066:                                         $cond = $conditions{$inconf{$type}};
14067:                                     }
14068:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
14069:                                 } elsif ($context eq 'requestauthor') {
14070:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
14071:                                                              $titles{$inconf{$type}},$typetitle);
14072: 
14073:                                 } else {
14074:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
14075:                                 }
14076:                             } else {
14077:                                 if ($type eq '_LC_adv') {
14078:                                     if ($inconf{$type} eq '0') {
14079:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14080:                                     } else { 
14081:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
14082:                                     }
14083:                                 } else {
14084:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14085:                                 }
14086:                             }
14087:                         }
14088:                     }
14089:                     unless ($context eq 'requestauthor') {
14090:                         $resulttext .= '</ul></li>';
14091:                     }
14092:                 }
14093:             }
14094:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
14095:                 if (ref($changes{'notify'}) eq 'HASH') {
14096:                     if ($changes{'notify'}{'approval'}) {
14097:                         if (ref($confhash{'notify'}) eq 'HASH') {
14098:                             if ($confhash{'notify'}{'approval'}) {
14099:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
14100:                             } else {
14101:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
14102:                             }
14103:                         }
14104:                     }
14105:                 }
14106:             }
14107:             if ($action eq 'requestcourses') {
14108:                 my @offon = ('off','on');
14109:                 if ($changes{'uniquecode'}) {
14110:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
14111:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
14112:                         $resulttext .= '<li>'.
14113:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
14114:                                        '</li>';
14115:                     } else {
14116:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
14117:                                        '</li>';
14118:                     }
14119:                 }
14120:                 foreach my $type ('textbooks','templates') {
14121:                     if (ref($changes{$type}) eq 'HASH') {
14122:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
14123:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
14124:                             my %coursehash = &Apache::lonnet::coursedescription($key);
14125:                             my $coursetitle = $coursehash{'description'};
14126:                             my $position = $confhash{$type}{$key}{'order'} + 1;
14127:                             $resulttext .= '<li>';
14128:                             foreach my $item ('subject','title','publisher','author') {
14129:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
14130:                                          ($type eq 'templates'));
14131:                                 my $name = $item.':';
14132:                                 $name =~ s/^(\w)/\U$1/;
14133:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
14134:                             }
14135:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
14136:                             if ($type eq 'textbooks') {
14137:                                 if ($confhash{$type}{$key}{'image'}) {
14138:                                     $resulttext .= ' '.&mt('Image: [_1]',
14139:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
14140:                                                    ' alt="Textbook cover" />').'<br />';
14141:                                 }
14142:                             }
14143:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
14144:                         }
14145:                         $resulttext .= '</ul></li>';
14146:                     }
14147:                 }
14148:                 if (ref($changes{'validation'}) eq 'HASH') {
14149:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
14150:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
14151:                         foreach my $item (@{$validationitemsref}) {
14152:                             if (exists($changes{'validation'}{$item})) {
14153:                                 if ($item eq 'markup') {
14154:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14155:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
14156:                                 } else {
14157:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14158:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
14159:                                 }
14160:                             }
14161:                         }
14162:                         if (exists($changes{'validation'}{'dc'})) {
14163:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
14164:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
14165:                         }
14166:                     }
14167:                 }
14168:             }
14169:             $resulttext .= '</ul>';
14170:             if (keys(%newenv)) {
14171:                 &Apache::lonnet::appenv(\%newenv);
14172:             }
14173:         } else {
14174:             if ($context eq 'requestcourses') {
14175:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
14176:             } elsif ($context eq 'requestauthor') {
14177:                 $resulttext = &mt('No changes made to rights to request author space.');
14178:             } else {
14179:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
14180:             }
14181:         }
14182:     } else {
14183:         $resulttext = '<span class="LC_error">'.
14184: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14185:     }
14186:     if ($errors) {
14187:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
14188:                        '<ul>'.$errors.'</ul></p>';
14189:     }
14190:     return $resulttext;
14191: }
14192: 
14193: sub process_textbook_image {
14194:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
14195:     my $filename = $env{'form.'.$caller.'.filename'};
14196:     my ($error,$url);
14197:     my ($width,$height) = (50,50);
14198:     if ($configuserok eq 'ok') {
14199:         if ($switchserver) {
14200:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
14201:                          $switchserver);
14202:         } elsif ($author_ok eq 'ok') {
14203:             my $modified = [];
14204:             my ($result,$imageurl) =
14205:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
14206:                                                         "$type/$cdom/$cnum/cover",$width,$height,
14207:                                                         '',$modified);
14208:             if ($result eq 'ok') {
14209:                 $url = $imageurl;
14210:                 &update_modify_urls($r,$modified);
14211:             } else {
14212:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14213:             }
14214:         } else {
14215:             $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);
14216:         }
14217:     } else {
14218:         $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);
14219:     }
14220:     return ($url,$error);
14221: }
14222: 
14223: sub modify_ltitools {
14224:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14225:     my (%currtoolsec,%secchanges,%newtoolsec,%newkeyset);
14226:     &fetch_secrets($dom,'toolsec',\%domconfig,\%currtoolsec,\%secchanges,\%newtoolsec,\%newkeyset);
14227: 
14228:     my $confname = $dom.'-domainconfig';
14229:     my $servadm = $r->dir_config('lonAdmEMail');
14230:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14231: 
14232:     my ($resulttext,$ltitoolsoutput,$is_home,$errors,%ltitoolschg,%newtoolsenc,%newltitools);
14233:     my $toolserror =
14234:         &Apache::courseprefs::process_ltitools($r,$dom,$confname,$domconfig{'ltitools'},\%ltitoolschg,'domain',
14235:                                                $lastactref,$configuserok,$switchserver,$author_ok);
14236: 
14237:     my $home = &Apache::lonnet::domain($dom,'primary');
14238:     unless (($home eq 'no_host') || ($home eq '')) {
14239:         my @ids=&Apache::lonnet::current_machine_ids();
14240:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; last; } }
14241:     }
14242: 
14243:     if (keys(%ltitoolschg)) {
14244:         foreach my $id (keys(%ltitoolschg)) {
14245:             if (ref($ltitoolschg{$id}) eq 'HASH') {
14246:                 foreach my $inner (keys(%{$ltitoolschg{$id}})) {
14247:                     if (($inner eq 'secret') || ($inner eq 'key')) {
14248:                         if ($is_home) {
14249:                             $newtoolsenc{$id}{$inner} = $ltitoolschg{$id}{$inner};
14250:                         }
14251:                     }
14252:                 }
14253:             }
14254:         }
14255:         $ltitoolsoutput = &Apache::courseprefs::store_ltitools($dom,'','domain',\%ltitoolschg,$domconfig{'ltitools'});
14256:         if (keys(%ltitoolschg)) {
14257:             %newltitools = %ltitoolschg;
14258:         }
14259:     }
14260:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
14261:         foreach my $id (%{$domconfig{'ltitools'}}) {
14262:             next if ($id !~ /^\d+$/);
14263:             unless (exists($ltitoolschg{$id})) {
14264:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
14265:                     foreach my $inner (keys(%{$domconfig{'ltitools'}{$id}})) {
14266:                         if (($inner eq 'secret') || ($inner eq 'key')) {
14267:                             if ($is_home) {
14268:                                 $newtoolsenc{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14269:                             }
14270:                         } else {
14271:                             $newltitools{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14272:                         }
14273:                     }
14274:                 } else {
14275:                     $newltitools{$id} = $domconfig{'ltitools'}{$id};
14276:                 }
14277:             }
14278:         }
14279:     }
14280:     if ($toolserror) {
14281:         $errors = '<li>'.$toolserror.'</li>';
14282:     }
14283:     if ((keys(%ltitoolschg) == 0) && (keys(%secchanges) == 0)) {
14284:         $resulttext = &mt('No changes made.');
14285:         if ($errors) {
14286:             $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
14287:                                  $errors.'</ul>';
14288:         }
14289:         return $resulttext;
14290:     }
14291:     my %ltitoolshash = (
14292:                           $action => { %newltitools }
14293:                        );
14294:     if (keys(%secchanges)) {
14295:         $ltitoolshash{'toolsec'} = \%newtoolsec;
14296:     }
14297:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,$dom);
14298:     if ($putresult eq 'ok') {
14299:         my %keystore;
14300:         if ($is_home) {
14301:             my %toolsenchash = (
14302:                                    $action => { %newtoolsenc }
14303:                                );
14304:             &Apache::lonnet::put_dom('encconfig',\%toolsenchash,$dom,undef,1);
14305:             my $cachetime = 24*60*60;
14306:             &Apache::lonnet::do_cache_new('ltitoolsenc',$dom,\%newtoolsenc,$cachetime);
14307:             &store_security($dom,'ltitools',\%secchanges,\%newkeyset,\%keystore,$lastactref);
14308:         }
14309:         $resulttext = &mt('Changes made:').'<ul>';
14310:         if (keys(%secchanges) > 0) {
14311:             $resulttext .= &lti_security_results($dom,'ltitools',\%secchanges,\%newtoolsec,\%newkeyset,\%keystore);
14312:         }
14313:         if (keys(%ltitoolschg) > 0) {
14314:             $resulttext .= $ltitoolsoutput;
14315:         }
14316:         my $cachetime = 24*60*60;
14317:         &Apache::lonnet::do_cache_new('ltitools',$dom,\%newltitools,$cachetime);
14318:         if (ref($lastactref) eq 'HASH') {
14319:             $lastactref->{'ltitools'} = 1;
14320:         }
14321:     } else {
14322:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14323:     }
14324:     if ($errors) {
14325:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
14326:                        $errors.'</ul></p>';
14327:     }
14328:     return $resulttext;
14329: }
14330: 
14331: sub fetch_secrets {
14332:     my ($dom,$context,$domconfig,$currsec,$secchanges,$newsec,$newkeyset) = @_;
14333:     my %keyset;
14334:     %{$currsec} = ();
14335:     $newsec->{'private'}{'keys'} = [];
14336:     $newsec->{'encrypt'} = {};
14337:     $newsec->{'rules'} = {};
14338:     if ($context eq 'ltisec') {
14339:         $newsec->{'linkprot'} = {};
14340:     }
14341:     if (ref($domconfig->{$context}) eq 'HASH') {
14342:         %{$currsec} = %{$domconfig->{$context}};
14343:         if ($context eq 'ltisec') {
14344:             if (ref($currsec->{'linkprot'}) eq 'HASH') {
14345:                 foreach my $id (keys(%{$currsec->{'linkprot'}})) {
14346:                     unless ($id =~ /^\d+$/) {
14347:                         delete($currsec->{'linkprot'}{$id});
14348:                     }
14349:                 }
14350:             }
14351:         }
14352:         if (ref($currsec->{'private'}) eq 'HASH') {
14353:             if (ref($currsec->{'private'}{'keys'}) eq 'ARRAY') {
14354:                 $newsec->{'private'}{'keys'} = $currsec->{'private'}{'keys'};
14355:                 map { $keyset{$_} = 1; } @{$currsec->{'private'}{'keys'}};
14356:             }
14357:         }
14358:     }
14359:     my @items= ('crs','dom');
14360:     if ($context eq 'ltisec') {
14361:         push(@items,'consumers');
14362:     }
14363:     foreach my $item (@items) {
14364:         my $formelement;
14365:         if (($context eq 'toolsec') || ($item eq 'consumers')) {
14366:             $formelement = 'form.'.$context.'_'.$item;
14367:         } else {
14368:             $formelement = 'form.'.$context.'_'.$item.'linkprot';
14369:         }
14370:         if ($env{$formelement}) {
14371:             $newsec->{'encrypt'}{$item} = 1;
14372:             if (ref($currsec->{'encrypt'}) eq 'HASH') {
14373:                 unless ($currsec->{'encrypt'}{$item}) {
14374:                     $secchanges->{'encrypt'} = 1;
14375:                 }
14376:             } else {
14377:                 $secchanges->{'encrypt'} = 1;
14378:             }
14379:         } elsif (ref($currsec->{'encrypt'}) eq 'HASH') {
14380:             if ($currsec->{'encrypt'}{$item}) {
14381:                 $secchanges->{'encrypt'} = 1;
14382:             }
14383:         }
14384:     }
14385:     my $secrets;
14386:     if ($context eq 'ltisec') {
14387:         $secrets = 'ltisecrets';
14388:     } else {
14389:         $secrets = 'toolsecrets';
14390:     }
14391:     unless (exists($currsec->{'rules'})) {
14392:         $currsec->{'rules'} = {};
14393:     }
14394:     &password_rule_changes($secrets,$newsec->{'rules'},$currsec->{'rules'},$secchanges);
14395: 
14396:     my @ids=&Apache::lonnet::current_machine_ids();
14397:     my %servers = &Apache::lonnet::get_servers($dom,'library');
14398: 
14399:     foreach my $hostid (keys(%servers)) {
14400:         if (($hostid ne '') && (grep(/^\Q$hostid\E$/,@ids))) {
14401:             my $keyitem = 'form.'.$context.'_privkey_'.$hostid;
14402:             if (exists($env{$keyitem})) {
14403:                 $env{$keyitem} =~ s/(`)/'/g;
14404:                 if ($keyset{$hostid}) {
14405:                     if ($env{'form.'.$context.'_changeprivkey_'.$hostid}) {
14406:                         if ($env{$keyitem} ne '') {
14407:                             $secchanges->{'private'} = 1;
14408:                             $newkeyset->{$hostid} = $env{$keyitem};
14409:                         }
14410:                     }
14411:                 } elsif ($env{$keyitem} ne '') {
14412:                     unless (grep(/^\Q$hostid\E$/,@{$newsec->{'private'}{'keys'}})) {
14413:                         push(@{$newsec->{'private'}{'keys'}},$hostid);
14414:                     }
14415:                     $secchanges->{'private'} = 1;
14416:                     $newkeyset->{$hostid} = $env{$keyitem};
14417:                 }
14418:             }
14419:         }
14420:     }
14421: }
14422: 
14423: sub store_security {
14424:     my ($dom,$context,$secchanges,$newkeyset,$keystore) = @_;
14425:     return unless ((ref($secchanges) eq 'HASH') && (ref($newkeyset) eq 'HASH') &&
14426:                    (ref($keystore) eq 'HASH'));
14427:     if (keys(%{$secchanges})) {
14428:         if ($secchanges->{'private'}) {
14429:             my $who = &escape($env{'user.name'}.':'.$env{'user.domain'});
14430:             foreach my $hostid (keys(%{$newkeyset})) {
14431:                 my $storehash = {
14432:                                    key => $newkeyset->{$hostid},
14433:                                    who => $env{'user.name'}.':'.$env{'user.domain'},
14434:                                 };
14435:                 $keystore->{$hostid} = &Apache::lonnet::store_dom($storehash,$context,'private',
14436:                                                                   $dom,$hostid);
14437:             }
14438:         }
14439:     }
14440: }
14441: 
14442: sub lti_security_results {
14443:     my ($dom,$context,$secchanges,$newsec,$newkeyset,$keystore) = @_;
14444:     my $output;
14445:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
14446:     my $needs_update;
14447:     foreach my $item (keys(%{$secchanges})) {
14448:         if ($item eq 'encrypt') {
14449:             $needs_update = 1;
14450:             my %encrypted;
14451:             if ($context eq 'lti') {
14452:                 %encrypted = (
14453:                               crs  => {
14454:                                         on => &mt('Encryption of stored link protection secrets defined in courses enabled'),
14455:                                         off => &mt('Encryption of stored link protection secrets defined in courses disabled'),
14456:                                       },
14457:                               dom => {
14458:                                        on => &mt('Encryption of stored link protection secrets defined in domain enabled'),
14459:                                        off => &mt('Encryption of stored link protection secrets defined in domain disabled'),
14460:                                      },
14461:                               consumers => {
14462:                                              on => &mt('Encryption of stored consumer secrets defined in domain enabled'),
14463:                                              off => &mt('Encryption of stored consumer secrets defined in domain disabled'),
14464:                                            },
14465:                              );
14466:             } else {
14467:                 %encrypted = (
14468:                               crs  => {
14469:                                         on => &mt('Encryption of stored external tool secrets defined in courses enabled'),
14470:                                         off => &mt('Encryption of stored external tool secrets defined in courses disabled'),
14471:                                       },
14472:                               dom => {
14473:                                        on => &mt('Encryption of stored external tool secrets defined in domain enabled'),
14474:                                        off => &mt('Encryption of stored external tool secrets defined in domain disabled'),
14475:                                      },
14476:                              );
14477: 
14478:             }
14479:             my @types= ('crs','dom');
14480:             if ($context eq 'lti') {
14481:                 foreach my $type (@types) {
14482:                     undef($domdefaults{'linkprotenc_'.$type});
14483:                 }
14484:                 push(@types,'consumers');
14485:                 undef($domdefaults{'ltienc_consumers'});
14486:             } elsif ($context eq 'ltitools') {
14487:                 foreach my $type (@types) {
14488:                     undef($domdefaults{'toolenc_'.$type});
14489:                 }
14490:             }
14491:             foreach my $type (@types) {
14492:                 my $shown = $encrypted{$type}{'off'};
14493:                 if (ref($newsec->{$item}) eq 'HASH') {
14494:                     if ($newsec->{$item}{$type}) {
14495:                         if ($context eq 'lti') {
14496:                             if ($type eq 'consumers') {
14497:                                 $domdefaults{'ltienc_consumers'} = 1;
14498:                             } else {
14499:                                 $domdefaults{'linkprotenc_'.$type} = 1;
14500:                             }
14501:                         } elsif ($context eq 'ltitools') {
14502:                             $domdefaults{'toolenc_'.$type} = 1;
14503:                         }
14504:                         $shown = $encrypted{$type}{'on'};
14505:                     }
14506:                 }
14507:                 $output .= '<li>'.$shown.'</li>';
14508:             }
14509:         } elsif ($item eq 'rules') {
14510:             my %titles = &Apache::lonlocal::texthash(
14511:                                       min   => 'Minimum password length',
14512:                                       max   => 'Maximum password length',
14513:                                       chars => 'Required characters',
14514:                          );
14515:             foreach my $rule ('min','max') {
14516:                 if ($newsec->{rules}{$rule} eq '') {
14517:                     if ($rule eq 'min') {
14518:                         $output .= '<li>'.&mt('[_1] not set.',$titles{$rule});
14519:                                    ' '.&mt('Default of [_1] will be used',
14520:                                            $Apache::lonnet::passwdmin).'</li>';
14521:                     } else {
14522:                         $output .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
14523:                     }
14524:                 } else {
14525:                     $output .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$newsec->{rules}{$rule}).'</li>';
14526:                 }
14527:             }
14528:             if (ref($newsec->{'rules'}{'chars'}) eq 'ARRAY') {
14529:                 if (@{$newsec->{'rules'}{'chars'}} > 0) {
14530:                     my %rulenames = &Apache::lonlocal::texthash(
14531:                                              uc => 'At least one upper case letter',
14532:                                              lc => 'At least one lower case letter',
14533:                                              num => 'At least one number',
14534:                                              spec => 'At least one non-alphanumeric',
14535:                                     );
14536:                     my $needed = '<ul><li>'.
14537:                                  join('</li><li>',map {$rulenames{$_} } @{$newsec->{'rules'}{'chars'}}).
14538:                                  '</li></ul>';
14539:                     $output .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
14540:                 } else {
14541:                     $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
14542:                 }
14543:             } else {
14544:                 $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
14545:             }
14546:         } elsif ($item eq 'private') {
14547:             $needs_update = 1;
14548:             if ($context eq 'lti') {
14549:                 undef($domdefaults{'ltiprivhosts'});
14550:             } elsif ($context eq 'ltitools') {
14551:                 undef($domdefaults{'toolprivhosts'});
14552:             }
14553:             if (keys(%{$newkeyset})) {
14554:                 my @privhosts;
14555:                 foreach my $hostid (sort(keys(%{$newkeyset}))) {
14556:                     if ($keystore->{$hostid} eq 'ok') {
14557:                         $output .= '<li>'.&mt('Encryption key for storage of shared secrets saved for [_1]',$hostid).'</li>';
14558:                         unless (grep(/^\Q$hostid\E$/,@privhosts)) {
14559:                             push(@privhosts,$hostid);
14560:                         }
14561:                     }
14562:                 }
14563:                 if (@privhosts) {
14564:                     if ($context eq 'lti') {
14565:                         $domdefaults{'ltiprivhosts'} = \@privhosts;
14566:                     } elsif ($context eq 'ltitools') {
14567:                         $domdefaults{'toolprivhosts'} = \@privhosts;
14568:                     }
14569:                 }
14570:             }
14571:         } elsif ($item eq 'linkprot') {
14572:             next;
14573:         }
14574:     }
14575:     if ($needs_update) {
14576:         my $cachetime = 24*60*60;
14577:         &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14578:     }
14579:     return $output;
14580: }
14581: 
14582: sub modify_proctoring {
14583:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14584:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14585:     my (@allpos,%changes,%confhash,%encconfhash,$errors,$resulttext,%imgdeletions);
14586:     my $confname = $dom.'-domainconfig';
14587:     my $servadm = $r->dir_config('lonAdmEMail');
14588:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14589:     my %providernames = &proctoring_providernames();
14590:     my $maxnum = scalar(keys(%providernames));
14591: 
14592:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
14593:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
14594:     if (ref($requref) eq 'HASH') {
14595:         %requserfields = %{$requref};
14596:     }
14597:     if (ref($opturef) eq 'HASH') {
14598:         %optuserfields = %{$opturef};
14599:     }
14600:     if (ref($defref) eq 'HASH') {
14601:         %defaults = %{$defref};
14602:     }
14603:     if (ref($extref) eq 'HASH') {
14604:         %extended = %{$extref};
14605:     }
14606:     if (ref($crsref) eq 'HASH') {
14607:         %crsconf = %{$crsref};
14608:     }
14609:     if (ref($rolesref) eq 'ARRAY') {
14610:         @courseroles = @{$rolesref};
14611:     }
14612:     if (ref($ltiref) eq 'ARRAY') {
14613:         @ltiroles = @{$ltiref};
14614:     }
14615: 
14616:     if (ref($domconfig{$action}) eq 'HASH') {
14617:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.proctoring_image_del');
14618:         if (@todeleteimages) {
14619:             map { $imgdeletions{$_} = 1; } @todeleteimages;
14620:         }
14621:     }
14622:     my %customadds;
14623:     my @newcustom = &Apache::loncommon::get_env_multiple('form.proctoring_customadd');
14624:     if (@newcustom) {
14625:         map { $customadds{$_} = 1; } @newcustom;
14626:     }
14627:     foreach my $provider (sort(keys(%providernames))) {
14628:         $confhash{$provider} = {};
14629:         my $pos = $env{'form.proctoring_pos_'.$provider};
14630:         $pos =~ s/\D+//g;
14631:         $allpos[$pos] = $provider;
14632:         my (%current,%currentenc);
14633:         my $showroles = 0;
14634:         if (ref($domconfig{$action}) eq 'HASH') {
14635:             if (ref($domconfig{$action}{$provider}) eq 'HASH') {
14636:                 %current = %{$domconfig{$action}{$provider}};
14637:                 foreach my $item ('key','secret') { 
14638:                     $currentenc{$item} = $current{$item};
14639:                     delete($current{$item});
14640:                 }
14641:             }
14642:         }
14643:         if ($env{'form.proctoring_available_'.$provider}) {
14644:             $confhash{$provider}{'available'} = 1;
14645:             unless ($current{'available'}) {
14646:                 $changes{$provider} = 1;
14647:             }
14648:         } else {
14649:             %{$confhash{$provider}} = %current;
14650:             %{$encconfhash{$provider}} = %currentenc;
14651:             $confhash{$provider}{'available'} = 0;
14652:             if ($current{'available'}) {
14653:                 $changes{$provider} = 1;
14654:             }
14655:         }
14656:         if ($confhash{$provider}{'available'}) {
14657:             foreach my $field ('lifetime','version','sigmethod','url','key','secret') {
14658:                 my $possval = $env{'form.proctoring_'.$provider.'_'.$field};
14659:                 if ($field eq 'lifetime') {
14660:                     if ($possval =~ /^\d+$/) {
14661:                         $confhash{$provider}{$field} = $possval;
14662:                     }
14663:                 } elsif ($field eq 'version') {
14664:                     if ($possval =~ /^\d+\.\d+$/) {
14665:                         $confhash{$provider}{$field} = $possval;
14666:                     }
14667:                 } elsif ($field eq 'sigmethod') {
14668:                     if ($possval =~ /^\QHMAC-SHA\E(1|256)$/) {
14669:                         $confhash{$provider}{$field} = $possval;
14670:                     }
14671:                 } elsif ($field eq 'url') {
14672:                     $confhash{$provider}{$field} = $possval;
14673:                 } elsif (($field eq 'key') || ($field eq 'secret')) {
14674:                     $encconfhash{$provider}{$field} = $possval;
14675:                     unless ($currentenc{$field} eq $possval) {
14676:                         $changes{$provider} = 1;
14677:                     }
14678:                 }
14679:                 unless (($field eq 'key') || ($field eq 'secret')) {
14680:                     unless ($current{$field} eq $confhash{$provider}{$field}) {
14681:                         $changes{$provider} = 1;
14682:                     }
14683:                 }
14684:             }
14685:             if ($imgdeletions{$provider}) {
14686:                 $changes{$provider} = 1;
14687:             } elsif ($env{'form.proctoring_image_'.$provider.'.filename'} ne '') {
14688:                 my ($imageurl,$error) =
14689:                     &process_proctoring_image($r,$dom,$confname,'proctoring_image_'.$provider,$provider,
14690:                                               $configuserok,$switchserver,$author_ok);
14691:                 if ($imageurl) {
14692:                     $confhash{$provider}{'image'} = $imageurl;
14693:                     $changes{$provider} = 1; 
14694:                 }
14695:                 if ($error) {
14696:                     &Apache::lonnet::logthis($error);
14697:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14698:                 }
14699:             } elsif (exists($current{'image'})) {
14700:                 $confhash{$provider}{'image'} = $current{'image'};
14701:             }
14702:             if (ref($requserfields{$provider}) eq 'ARRAY') {
14703:                 if (@{$requserfields{$provider}} > 0) {
14704:                     if (grep(/^user$/,@{$requserfields{$provider}})) {
14705:                         if ($env{'form.proctoring_userincdom_'.$provider}) {
14706:                             $confhash{$provider}{'incdom'} = 1;
14707:                         }
14708:                         unless ($current{'incdom'} eq $confhash{$provider}{'incdom'}) {
14709:                             $changes{$provider} = 1;
14710:                         }
14711:                     }
14712:                     if (grep(/^roles$/,@{$requserfields{$provider}})) {
14713:                         $showroles = 1;
14714:                     }
14715:                 }
14716:             }
14717:             $confhash{$provider}{'fields'} = [];
14718:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
14719:                 if (@{$optuserfields{$provider}} > 0) {
14720:                     my @optfields = &Apache::loncommon::get_env_multiple('form.proctoring_optional_'.$provider);
14721:                     foreach my $field (@{$optuserfields{$provider}}) {
14722:                         if (grep(/^\Q$field\E$/,@optfields)) {
14723:                             push(@{$confhash{$provider}{'fields'}},$field);
14724:                         }
14725:                     }
14726:                 }
14727:                 if (ref($current{'fields'}) eq 'ARRAY') {
14728:                     unless ($changes{$provider}) {
14729:                         my @new = sort(@{$confhash{$provider}{'fields'}});
14730:                         my @old = sort(@{$current{'fields'}}); 
14731:                         my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14732:                         if (@diffs) {
14733:                             $changes{$provider} = 1;
14734:                         }
14735:                     }
14736:                 } elsif (@{$confhash{$provider}{'fields'}}) {
14737:                     $changes{$provider} = 1;
14738:                 }
14739:             }
14740:             if (ref($defaults{$provider}) eq 'ARRAY') {  
14741:                 if (@{$defaults{$provider}} > 0) {
14742:                     my %options;
14743:                     if (ref($extended{$provider}) eq 'HASH') {
14744:                         %options = %{$extended{$provider}};
14745:                     }
14746:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_defaults_'.$provider);
14747:                     foreach my $field (@{$defaults{$provider}}) {
14748:                         if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
14749:                             my $poss = $env{'form.proctoring_defaults_'.$field.'_'.$provider};
14750:                             if (grep(/^\Q$poss\E$/,@{$options{$field}})) {
14751:                                 push(@{$confhash{$provider}{'defaults'}},$poss); 
14752:                             }
14753:                         } elsif ((exists($options{$field})) && (ref($options{$field}) eq 'HASH')) {
14754:                             foreach my $inner (keys(%{$options{$field}})) {
14755:                                 if (ref($options{$field}{$inner}) eq 'ARRAY') {
14756:                                     my $poss = $env{'form.proctoring_'.$inner.'_'.$provider};
14757:                                     if (grep(/^\Q$poss\E$/,@{$options{$field}{$inner}})) {
14758:                                         $confhash{$provider}{'defaults'}{$inner} = $poss;
14759:                                     }
14760:                                 } else {
14761:                                     $confhash{$provider}{'defaults'}{$inner} = $env{'form.proctoring_'.$inner.'_'.$provider};
14762:                                 }
14763:                             }
14764:                         } else {
14765:                             if (grep(/^\Q$field\E$/,@checked)) {
14766:                                 push(@{$confhash{$provider}{'defaults'}},$field);
14767:                             }
14768:                         }
14769:                     }
14770:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
14771:                         if (ref($current{'defaults'}) eq 'ARRAY') {
14772:                             unless ($changes{$provider}) {
14773:                                 my @new = sort(@{$confhash{$provider}{'defaults'}});
14774:                                 my @old = sort(@{$current{'defaults'}});
14775:                                 my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14776:                                 if (@diffs) {
14777:                                     $changes{$provider} = 1; 
14778:                                 }
14779:                             }
14780:                         } elsif (ref($current{'defaults'}) eq 'ARRAY') {
14781:                             if (@{$current{'defaults'}}) {
14782:                                 $changes{$provider} = 1;
14783:                             }
14784:                         }
14785:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
14786:                         if (ref($current{'defaults'}) eq 'HASH') {
14787:                             unless ($changes{$provider}) {
14788:                                 foreach my $key (keys(%{$confhash{$provider}{'defaults'}})) {
14789:                                     unless ($confhash{$provider}{'defaults'}{$key} eq $current{'defaults'}{$key}) {
14790:                                         $changes{$provider} = 1;
14791:                                         last;
14792:                                     }
14793:                                 }
14794:                             }
14795:                             unless ($changes{$provider}) {
14796:                                 foreach my $key (keys(%{$current{'defaults'}})) {
14797:                                     unless ($current{'defaults'}{$key} eq $confhash{$provider}{'defaults'}{$key}) {
14798:                                         $changes{$provider} = 1;
14799:                                         last;
14800:                                     }
14801:                                 }
14802:                             }
14803:                         } elsif (keys(%{$confhash{$provider}{'defaults'}})) {
14804:                             $changes{$provider} = 1;
14805:                         }
14806:                     }
14807:                 }
14808:             }
14809:             if (ref($crsconf{$provider}) eq 'ARRAY') {
14810:                 if (@{$crsconf{$provider}} > 0) {
14811:                     $confhash{$provider}{'crsconf'} = [];
14812:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_crsconf_'.$provider);
14813:                     foreach my $crsfield (@{$crsconf{$provider}}) {
14814:                         if (grep(/^\Q$crsfield\E$/,@checked)) {
14815:                             push(@{$confhash{$provider}{'crsconf'}},$crsfield);
14816:                         }
14817:                     }
14818:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
14819:                         unless ($changes{$provider}) {  
14820:                             my @new = sort(@{$confhash{$provider}{'crsconf'}});
14821:                             my @old = sort(@{$current{'crsconf'}});
14822:                             my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14823:                             if (@diffs) {
14824:                                 $changes{$provider} = 1;
14825:                             }
14826:                         }
14827:                     } elsif (@{$confhash{$provider}{'crsconf'}}) {
14828:                         $changes{$provider} = 1;
14829:                     }
14830:                 }
14831:             }
14832:             if ($showroles) {
14833:                 $confhash{$provider}{'roles'} = {};
14834:                 foreach my $role (@courseroles) {
14835:                     my $poss = $env{'form.proctoring_roles_'.$role.'_'.$provider};
14836:                     if (grep(/^\Q$poss\E$/,@ltiroles)) {
14837:                         $confhash{$provider}{'roles'}{$role} = $poss;
14838:                     }
14839:                 }
14840:                 unless ($changes{$provider}) {
14841:                     if (ref($current{'roles'}) eq 'HASH') {
14842:                         foreach my $role (keys(%{$current{'roles'}})) {
14843:                             unless ($current{'roles'}{$role} eq $confhash{$provider}{'roles'}{$role}) {
14844:                                 $changes{$provider} = 1;
14845:                                 last
14846:                             }
14847:                         }
14848:                         unless ($changes{$provider}) {
14849:                             foreach my $role (keys(%{$confhash{$provider}{'roles'}})) {
14850:                                 unless ($confhash{$provider}{'roles'}{$role} eq $current{'roles'}{$role}) {
14851:                                     $changes{$provider} = 1;
14852:                                     last;
14853:                                 }
14854:                             }
14855:                         }
14856:                     } elsif (keys(%{$confhash{$provider}{'roles'}})) {
14857:                         $changes{$provider} = 1;
14858:                     }
14859:                 }
14860:             }
14861:             if (ref($current{'custom'}) eq 'HASH') {
14862:                 my @customdels = &Apache::loncommon::get_env_multiple('form.proctoring_customdel_'.$provider);
14863:                 foreach my $key (keys(%{$current{'custom'}})) {
14864:                     if (grep(/^\Q$key\E$/,@customdels)) {
14865:                         $changes{$provider} = 1;
14866:                     } else {
14867:                         $confhash{$provider}{'custom'}{$key} = $env{'form.proctoring_customval_'.$key.'_'.$provider};
14868:                         if ($confhash{$provider}{'custom'}{$key} ne $current{'custom'}{$key}) {
14869:                             $changes{$provider} = 1;
14870:                         }
14871:                     }
14872:                 }
14873:             }
14874:             if ($customadds{$provider}) {
14875:                 my $name = $env{'form.proctoring_custom_name_'.$provider};
14876:                 $name =~ s/(`)/'/g;
14877:                 $name =~ s/^\s+//;
14878:                 $name =~ s/\s+$//;
14879:                 my $value = $env{'form.proctoring_custom_value_'.$provider};
14880:                 $value =~ s/(`)/'/g;
14881:                 $value =~ s/^\s+//;
14882:                 $value =~ s/\s+$//;
14883:                 if ($name ne '') {
14884:                     $confhash{$provider}{'custom'}{$name} = $value;
14885:                     $changes{$provider} = 1;
14886:                 }
14887:             }
14888:         }
14889:     }
14890:     if (@allpos > 0) {
14891:         my $idx = 0;
14892:         foreach my $provider (@allpos) {
14893:             if ($provider ne '') {
14894:                 $confhash{$provider}{'order'} = $idx;
14895:                 unless ($changes{$provider}) {
14896:                     if (ref($domconfig{$action}) eq 'HASH') {
14897:                         if (ref($domconfig{$action}{$provider}) eq 'HASH') {
14898:                             if ($domconfig{$action}{$provider}{'order'} ne $idx) {
14899:                                 $changes{$provider} = 1;
14900:                             }
14901:                         }
14902:                     }
14903:                 }
14904:                 $idx ++;
14905:             }
14906:         }
14907:     }
14908:     my %proc_hash = (
14909:                           $action => { %confhash }
14910:                        );
14911:     my $putresult = &Apache::lonnet::put_dom('configuration',\%proc_hash,
14912:                                              $dom);
14913:     if ($putresult eq 'ok') {
14914:         my %proc_enchash = (
14915:                              $action => { %encconfhash }
14916:                          );
14917:         &Apache::lonnet::put_dom('encconfig',\%proc_enchash,$dom,undef,1);
14918:         if (keys(%changes) > 0) {
14919:             my $cachetime = 24*60*60;
14920:             my %procall = %confhash;
14921:             foreach my $provider (keys(%procall)) {
14922:                 if (ref($encconfhash{$provider}) eq 'HASH') {
14923:                     foreach my $key ('key','secret') {
14924:                         $procall{$provider}{$key} = $encconfhash{$provider}{$key};
14925:                     }
14926:                 }
14927:             }
14928:             &Apache::lonnet::do_cache_new('proctoring',$dom,\%procall,$cachetime);
14929:             if (ref($lastactref) eq 'HASH') {
14930:                 $lastactref->{'proctoring'} = 1;
14931:             }
14932:             $resulttext = &mt('Configuration for Provider(s) with changes:').'<ul>';
14933:             my %bynum;
14934:             foreach my $provider (sort(keys(%changes))) {
14935:                 my $position = $confhash{$provider}{'order'};
14936:                 $bynum{$position} = $provider;
14937:             }
14938:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
14939:                 my $provider = $bynum{$pos};
14940:                 my %lt = &proctoring_titles($provider);
14941:                 my %fieldtitles = &proctoring_fieldtitles($provider);
14942:                 if (!$confhash{$provider}{'available'}) {
14943:                     $resulttext .= '<li>'.&mt('Proctoring integration unavailable for: [_1]','<b>'.$providernames{$provider}.'</b>').'</li>';
14944:                 } else {
14945:                     $resulttext .= '<li>'.&mt('Proctoring integration available for: [_1]','<b>'.$providernames{$provider}.'</b>');
14946:                     if ($confhash{$provider}{'image'}) {
14947:                         $resulttext .= '&nbsp;'.
14948:                                        '<img src="'.$confhash{$provider}{'image'}.'"'.
14949:                                        ' alt="'.&mt('Proctoring icon').'" />';
14950:                     }
14951:                     $resulttext .= '<ul>';
14952:                     my $position = $pos + 1;
14953:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
14954:                     foreach my $key ('version','sigmethod','url','lifetime') {
14955:                         if ($confhash{$provider}{$key} ne '') {
14956:                             $resulttext .= '<li>'.$lt{$key}.':&nbsp;'.$confhash{$provider}{$key}.'</li>';
14957:                         }
14958:                     }
14959:                     if ($encconfhash{$provider}{'key'} ne '') {
14960:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfhash{$provider}{'key'}.'</li>';
14961:                     }
14962:                     if ($encconfhash{$provider}{'secret'} ne '') {
14963:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
14964:                         my $num = length($encconfhash{$provider}{'secret'});
14965:                         $resulttext .= ('*'x$num).'</li>';
14966:                     }
14967:                     my (@fields,$showroles);
14968:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
14969:                         push(@fields,@{$requserfields{$provider}});
14970:                     }
14971:                     if (ref($confhash{$provider}{'fields'}) eq 'ARRAY') {
14972:                         push(@fields,@{$confhash{$provider}{'fields'}});
14973:                     } elsif (ref($confhash{$provider}{'fields'}) eq 'HASH') {
14974:                         push(@fields,(keys(%{$confhash{$provider}{'fields'}})));
14975:                     }
14976:                     if (@fields) {
14977:                         if (grep(/^roles$/,@fields)) {
14978:                             $showroles = 1;
14979:                         }
14980:                         $resulttext .= '<li>'.$lt{'udsl'}.':&nbsp;"'.
14981:                                        join('", "', map { $lt{$_}; } @fields).'"</li>';
14982:                     }
14983:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
14984:                         if (grep(/^user$/,@{$requserfields{$provider}})) {
14985:                             if ($confhash{$provider}{'incdom'}) {
14986:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'uname:dom'}).'</li>';
14987:                             } else { 
14988:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'username'}).'</li>';
14989:                             }
14990:                         }
14991:                     }
14992:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
14993:                         if (@{$confhash{$provider}{'defaults'}} > 0) {
14994:                             $resulttext .= '<li>'.$lt{'defa'};
14995:                             foreach my $field (@{$confhash{$provider}{'defaults'}}) {
14996:                                 $resulttext .= ' "'.$fieldtitles{$field}.'",';
14997:                             }
14998:                             $resulttext =~ s/,$//;
14999:                             $resulttext .= '</li>';
15000:                         }
15001:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
15002:                         if (keys(%{$confhash{$provider}{'defaults'}})) {
15003:                             $resulttext .= '<li>'.$lt{'defa'}.':&nbsp;<ul>';
15004:                             foreach my $key (sort(keys(%{$confhash{$provider}{'defaults'}}))) {
15005:                                 if ($confhash{$provider}{'defaults'}{$key} ne '') {
15006:                                     $resulttext .= '<li>'.$fieldtitles{$key}.' = '.$confhash{$provider}{'defaults'}{$key}.'</li>';
15007:                                 }
15008:                             }
15009:                             $resulttext .= '</ul></li>';
15010:                         }
15011:                     }
15012:                     if (ref($crsconf{$provider}) eq 'ARRAY') {
15013:                         if (@{$crsconf{$provider}} > 0) {
15014:                             $resulttext .= '<li>'.&mt('Configurable in course:');
15015:                             my $numconfig = 0;
15016:                             if (ref($confhash{$provider}{'crsconf'}) eq 'ARRAY') {
15017:                                 if (@{$confhash{$provider}{'crsconf'}} > 0) {
15018:                                     foreach my $field (@{$confhash{$provider}{'crsconf'}}) {
15019:                                         $numconfig ++;
15020:                                         if ($provider eq 'examity') {
15021:                                             $resulttext .= ' "'.$lt{'crs'.$field}.'",';
15022:                                         } else {
15023:                                             $resulttext .= ' "'.$fieldtitles{$field}.'",';
15024:                                         }
15025:                                     }
15026:                                     $resulttext =~ s/,$//;
15027:                                 }
15028:                             }
15029:                             if (!$numconfig) {
15030:                                 $resulttext .= '&nbsp;'.&mt('None');
15031:                             }
15032:                             $resulttext .= '</li>';
15033:                         }
15034:                     }
15035:                     if ($showroles) {
15036:                         if (ref($confhash{$provider}{'roles'}) eq 'HASH') {
15037:                             my $rolemaps;
15038:                             foreach my $role (@courseroles) {
15039:                                 if ($confhash{$provider}{'roles'}{$role}) {
15040:                                     $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
15041:                                                  $confhash{$provider}{'roles'}{$role}.',';
15042:                                 }
15043:                             }
15044:                             if ($rolemaps) {
15045:                                 $rolemaps =~ s/,$//;
15046:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
15047:                             }
15048:                         }
15049:                     }
15050:                     if (ref($confhash{$provider}{'custom'}) eq 'HASH') {
15051:                         my $customlist;
15052:                         if (keys(%{$confhash{$provider}{'custom'}})) {
15053:                             foreach my $key (sort(keys(%{$confhash{$provider}{'custom'}}))) {
15054:                                 $customlist .= $key.'='.$confhash{$provider}{'custom'}{$key}.', ';
15055:                             }
15056:                             $customlist =~ s/,$//;
15057:                         }
15058:                         if ($customlist) {
15059:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
15060:                         }
15061:                     } 
15062:                     $resulttext .= '</ul></li>';
15063:                 }
15064:             }
15065:             $resulttext .= '</ul>';
15066:         } else {
15067:             $resulttext = &mt('No changes made.');
15068:         }
15069:     } else {
15070:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15071:     }
15072:     if ($errors) {
15073:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15074:                        $errors.'</ul>';
15075:     }
15076:     return $resulttext;
15077: }
15078: 
15079: sub process_proctoring_image {
15080:     my ($r,$dom,$confname,$caller,$provider,$configuserok,$switchserver,$author_ok) = @_;
15081:     my $filename = $env{'form.'.$caller.'.filename'};
15082:     my ($error,$url);
15083:     my ($width,$height) = (21,21);
15084:     if ($configuserok eq 'ok') {
15085:         if ($switchserver) {
15086:             $error = &mt('Upload of Remote Proctoring Provider icon is not permitted to this server: [_1]',
15087:                          $switchserver);
15088:         } elsif ($author_ok eq 'ok') {
15089:             my $modified = [];
15090:             my ($result,$imageurl,$madethumb) =
15091:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
15092:                                                         "proctoring/$provider/icon",$width,$height,
15093:                                                         '',$modified);
15094:             if ($result eq 'ok') {
15095:                 if ($madethumb) {
15096:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
15097:                     my $imagethumb = "$path/tn-".$imagefile;
15098:                     $url = $imagethumb;
15099:                 } else {
15100:                     $url = $imageurl;
15101:                 }
15102:                 &update_modify_urls($r,$modified);
15103:             } else {
15104:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
15105:             }
15106:         } else {
15107:             $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);
15108:         }
15109:     } else {
15110:         $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);
15111:     }
15112:     return ($url,$error);
15113: }
15114: 
15115: sub modify_lti {
15116:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15117:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15118:     my ($newid,@allpos,%changes,%confhash,%ltienc,$errors,$resulttext);
15119:     my (%posslti,%posslticrs,%posscrstype);
15120:     my @courseroles = ('cc','in','ta','ep','st');
15121:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
15122:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
15123:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
15124:     my %coursetypetitles = &Apache::lonlocal::texthash (
15125:                                official   => 'Official',
15126:                                unofficial => 'Unofficial',
15127:                                community  => 'Community',
15128:                                textbook   => 'Textbook',
15129:                                placement  => 'Placement Test',
15130:                                lti        => 'LTI Provider',
15131:     );
15132:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
15133:     my %lt = &lti_names();
15134:     map { $posslti{$_} = 1; } @ltiroles;
15135:     map { $posslticrs{$_} = 1; } @lticourseroles;
15136:     map { $posscrstype{$_} = 1; } @coursetypes;
15137: 
15138:     my %menutitles = &ltimenu_titles();
15139:     my (%currltisec,%secchanges,%newltisec,%newltienc,%newkeyset);
15140: 
15141:     &fetch_secrets($dom,'ltisec',\%domconfig,\%currltisec,\%secchanges,\%newltisec,\%newkeyset);
15142: 
15143:     my (%linkprotchg,$linkprotoutput,$is_home);
15144:     my $proterror = &Apache::courseprefs::process_linkprot($dom,'',$currltisec{'linkprot'},
15145:                                                            \%linkprotchg,'domain');
15146:     my $home = &Apache::lonnet::domain($dom,'primary');
15147:     unless (($home eq 'no_host') || ($home eq '')) {
15148:         my @ids=&Apache::lonnet::current_machine_ids();
15149:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; } }
15150:     }
15151: 
15152:     if (keys(%linkprotchg)) {
15153:         $secchanges{'linkprot'} = 1;
15154:         my %oldlinkprot;
15155:         if (ref($currltisec{'linkprot'}) eq 'HASH') {
15156:             %oldlinkprot = %{$currltisec{'linkprot'}};
15157:         }
15158:         foreach my $id (keys(%linkprotchg)) {
15159:             if (ref($linkprotchg{$id}) eq 'HASH') {
15160:                 foreach my $inner (keys(%{$linkprotchg{$id}})) {
15161:                     if (($inner eq 'secret') || ($inner eq 'key')) {
15162:                         if ($is_home) {
15163:                             $newltienc{$id}{$inner} = $linkprotchg{$id}{$inner};
15164:                         }
15165:                     }
15166:                 }
15167:             } else {
15168:                 $newltisec{'linkprot'}{$id} = $linkprotchg{$id};
15169:             }
15170:         }
15171:         $linkprotoutput = &Apache::courseprefs::store_linkprot($dom,'','domain',\%linkprotchg,\%oldlinkprot);
15172:         if (keys(%linkprotchg)) {
15173:             %{$newltisec{'linkprot'}} = %linkprotchg;
15174:         }
15175:     }
15176:     if (ref($currltisec{'linkprot'}) eq 'HASH') {
15177:         foreach my $id (%{$currltisec{'linkprot'}}) {
15178:             next if ($id !~ /^\d+$/);
15179:             unless (exists($linkprotchg{$id})) {
15180:                 if (ref($currltisec{'linkprot'}{$id}) eq 'HASH') {
15181:                     foreach my $inner (keys(%{$currltisec{'linkprot'}{$id}})) {
15182:                         if (($inner eq 'secret') || ($inner eq 'key')) {
15183:                             if ($is_home) {
15184:                                 $newltienc{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15185:                             }
15186:                         } else {
15187:                             $newltisec{'linkprot'}{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15188:                         }
15189:                     }
15190:                 } else {
15191:                     $newltisec{'linkprot'}{$id} = $currltisec{'linkprot'}{$id};
15192:                 }
15193:             }
15194:         }
15195:     }
15196:     if ($proterror) {
15197:         $errors .= '<li>'.$proterror.'</li>';
15198:     }
15199:     my (@items,%deletions,%itemids);
15200:     if ($env{'form.lti_add'}) {
15201:         my $consumer = $env{'form.lti_consumer_add'};
15202:         $consumer =~ s/(`)/'/g;
15203:         ($newid,my $error) = &get_lti_id($dom,$consumer);
15204:         if ($newid) {
15205:             $itemids{'add'} = $newid;
15206:             push(@items,'add');
15207:             $changes{$newid} = 1;
15208:         } else {
15209:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
15210:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15211:         }
15212:     }
15213:     if (ref($domconfig{$action}) eq 'HASH') {
15214:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
15215:         if (@todelete) {
15216:             map { $deletions{$_} = 1; } @todelete;
15217:         }
15218:         my $maxnum = $env{'form.lti_maxnum'};
15219:         for (my $i=0; $i<$maxnum; $i++) {
15220:             my $itemid = $env{'form.lti_id_'.$i};
15221:             $itemid =~ s/\D+//g;
15222:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15223:                 if ($deletions{$itemid}) {
15224:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
15225:                 } else {
15226:                     push(@items,$i);
15227:                     $itemids{$i} = $itemid;
15228:                 }
15229:             }
15230:         }
15231:     }
15232:     my (%keystore,$secstored);
15233:     if ($is_home) {
15234:         &store_security($dom,'lti',\%secchanges,\%newkeyset,\%keystore);
15235:     }
15236: 
15237:     my ($cipher,$privnum);
15238:     if ((@items > 0) && ($is_home)) {
15239:         ($cipher,$privnum) = &get_priv_creds($dom,$home,$secchanges{'encrypt'},
15240:                                              $newltisec{'encrypt'},$keystore{$home});
15241:     }
15242:     foreach my $idx (@items) {
15243:         my $itemid = $itemids{$idx};
15244:         next unless ($itemid);
15245:         my %currlti;
15246:         unless ($idx eq 'add') {
15247:             if (ref($domconfig{$action}) eq 'HASH') {
15248:                 if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15249:                     %currlti = %{$domconfig{$action}{$itemid}};
15250:                 }
15251:             }
15252:         }
15253:         my $position = $env{'form.lti_pos_'.$itemid};
15254:         $position =~ s/\D+//g;
15255:         if ($position ne '') {
15256:             $allpos[$position] = $itemid;
15257:         }
15258:         foreach my $item ('consumer','lifetime','requser','crsinc') {
15259:             my $formitem = 'form.lti_'.$item.'_'.$idx;
15260:             $env{$formitem} =~ s/(`)/'/g;
15261:             if ($item eq 'lifetime') {
15262:                 $env{$formitem} =~ s/[^\d.]//g;
15263:             }
15264:             if ($env{$formitem} ne '') {
15265:                 $confhash{$itemid}{$item} = $env{$formitem};
15266:                 unless (($idx eq 'add') || ($changes{$itemid})) {
15267:                     if ($currlti{$item} ne $confhash{$itemid}{$item}) {
15268:                         $changes{$itemid} = 1;
15269:                     }
15270:                 }
15271:             }
15272:         }
15273:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
15274:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
15275:         }
15276:         if ($confhash{$itemid}{'requser'}) {
15277:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
15278:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid';
15279:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
15280:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
15281:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
15282:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
15283:                 $mapuser =~ s/(`)/'/g;
15284:                 $mapuser =~ s/^\s+|\s+$//g;
15285:                 $confhash{$itemid}{'mapuser'} = $mapuser;
15286:             }
15287:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
15288:             my @makeuser;
15289:             foreach my $ltirole (sort(@possmakeuser)) {
15290:                 if ($posslti{$ltirole}) {
15291:                     push(@makeuser,$ltirole);
15292:                 }
15293:             }
15294:             $confhash{$itemid}{'makeuser'} = \@makeuser;
15295:             if (@makeuser) {
15296:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
15297:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
15298:                     $confhash{$itemid}{'lcauth'} = $lcauth;
15299:                     if ($lcauth ne 'internal') {
15300:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
15301:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
15302:                         $lcauthparm =~ s/`//g;
15303:                         if ($lcauthparm ne '') {
15304:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
15305:                         }
15306:                     }
15307:                 } else {
15308:                     $confhash{$itemid}{'lcauth'} = 'lti';
15309:                 }
15310:             }
15311:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
15312:             if (@possinstdata) {
15313:                 foreach my $field (@possinstdata) {
15314:                     if (exists($fieldtitles{$field})) {
15315:                         push(@{$confhash{$itemid}{'instdata'}});
15316:                     }
15317:                 }
15318:             }
15319:             if ($env{'form.lti_callback_'.$idx}) {
15320:                 if ($env{'form.lti_callbackparam_'.$idx}) {
15321:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
15322:                     $callback =~ s/^\s+|\s+$//g;
15323:                     $confhash{$itemid}{'callback'} = $callback;
15324:                 }
15325:             }
15326:             foreach my $field ('topmenu','inlinemenu') {
15327:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
15328:                     $confhash{$itemid}{$field} = 1;
15329:                 }
15330:             }
15331:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
15332:                 $confhash{$itemid}{lcmenu} = [];
15333:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
15334:                 foreach my $field (@possmenu) {
15335:                     if (exists($menutitles{$field})) {
15336:                         if ($field eq 'grades') {
15337:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
15338:                         }
15339:                         push(@{$confhash{$itemid}{lcmenu}},$field);
15340:                     }
15341:                 }
15342:             }
15343:             if ($confhash{$itemid}{'crsinc'}) {
15344:                 if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
15345:                     ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
15346:                     $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
15347:                 } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
15348:                     my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
15349:                     $mapcrs =~ s/(`)/'/g;
15350:                     $mapcrs =~ s/^\s+|\s+$//g;
15351:                     $confhash{$itemid}{'mapcrs'} = $mapcrs;
15352:                 }
15353:                 my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
15354:                 my @crstypes;
15355:                 foreach my $type (sort(@posstypes)) {
15356:                     if ($posscrstype{$type}) {
15357:                         push(@crstypes,$type);
15358:                     }
15359:                 }
15360:                 $confhash{$itemid}{'mapcrstype'} = \@crstypes;
15361:                 if ($env{'form.lti_storecrs_'.$idx}) {
15362:                     $confhash{$itemid}{'storecrs'} = 1;
15363:                 }       
15364:                 if ($env{'form.lti_makecrs_'.$idx}) {
15365:                     $confhash{$itemid}{'makecrs'} = 1;
15366:                 }
15367:                 foreach my $ltirole (@lticourseroles) {
15368:                     my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
15369:                     if (grep(/^\Q$possrole\E$/,@courseroles)) {
15370:                         $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
15371:                     }
15372:                 }
15373:                 my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
15374:                 my @selfenroll;
15375:                 foreach my $type (sort(@possenroll)) {
15376:                     if ($posslticrs{$type}) {
15377:                         push(@selfenroll,$type);
15378:                     }
15379:                 }
15380:                 $confhash{$itemid}{'selfenroll'} = \@selfenroll;
15381:                 if ($env{'form.lti_crssec_'.$idx}) {
15382:                     if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
15383:                         $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
15384:                     } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
15385:                         my $section = $env{'form.lti_customsection_'.$idx};
15386:                         $section =~ s/(`)/'/g;
15387:                         $section =~ s/^\s+|\s+$//g;
15388:                         if ($section ne '') {
15389:                             $confhash{$itemid}{'section'} = $section;
15390:                         }
15391:                     }
15392:                 }
15393:                 foreach my $field ('passback','roster') {
15394:                     if ($env{'form.lti_'.$field.'_'.$idx}) {
15395:                         $confhash{$itemid}{$field} = 1;
15396:                     }
15397:                 }
15398:                 if ($env{'form.lti_passback_'.$idx}) {
15399:                     if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
15400:                         $confhash{$itemid}{'passbackformat'} = '1.0';
15401:                     } else {
15402:                         $confhash{$itemid}{'passbackformat'} = '1.1';
15403:                     }
15404:                 }
15405:             }
15406:             unless (($idx eq 'add') || ($changes{$itemid})) {
15407:                 if ($confhash{$itemid}{'crsinc'}) {
15408:                     foreach my $field ('mapcrs','storecrs','makecrs','section','passback','roster') {
15409:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
15410:                             $changes{$itemid} = 1;
15411:                         }
15412:                     }
15413:                     unless ($changes{$itemid}) {
15414:                         if ($currlti{'passback'} eq $confhash{$itemid}{'passback'}) {
15415:                             if ($currlti{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
15416:                                 $changes{$itemid} = 1;
15417:                             }
15418:                         }
15419:                     }
15420:                     foreach my $field ('mapcrstype','selfenroll') {
15421:                         unless ($changes{$itemid}) {
15422:                             if (ref($currlti{$field}) eq 'ARRAY') {
15423:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15424:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
15425:                                                                                    $confhash{$itemid}{$field});
15426:                                     if (@diffs) {
15427:                                         $changes{$itemid} = 1;
15428:                                     }
15429:                                 } elsif (@{$currlti{$field}} > 0) {
15430:                                     $changes{$itemid} = 1;
15431:                                 }
15432:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15433:                                 if (@{$confhash{$itemid}{$field}} > 0) {
15434:                                     $changes{$itemid} = 1;
15435:                                 }
15436:                             }
15437:                         }
15438:                     }
15439:                     unless ($changes{$itemid}) {
15440:                         if (ref($currlti{'maproles'}) eq 'HASH') {
15441:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15442:                                 foreach my $ltirole (keys(%{$currlti{'maproles'}})) {
15443:                                     if ($currlti{'maproles'}{$ltirole} ne 
15444:                                         $confhash{$itemid}{'maproles'}{$ltirole}) {
15445:                                         $changes{$itemid} = 1;
15446:                                         last;
15447:                                     }
15448:                                 }
15449:                                 unless ($changes{$itemid}) {
15450:                                     foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
15451:                                         if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
15452:                                             $currlti{'maproles'}{$ltirole}) {
15453:                                             $changes{$itemid} = 1;
15454:                                             last;
15455:                                         }
15456:                                     }
15457:                                 }
15458:                             } elsif (keys(%{$currlti{'maproles'}}) > 0) {
15459:                                 $changes{$itemid} = 1;
15460:                             }
15461:                         } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15462:                             unless ($changes{$itemid}) {
15463:                                 if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
15464:                                     $changes{$itemid} = 1;
15465:                                 }
15466:                             }
15467:                         }
15468:                     }
15469:                 }
15470:                 unless ($changes{$itemid}) {
15471:                     foreach my $field ('mapuser','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
15472:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
15473:                             $changes{$itemid} = 1;
15474:                         }
15475:                     }
15476:                     unless ($changes{$itemid}) {
15477:                         foreach my $field ('makeuser','lcmenu') {
15478:                             if (ref($currlti{$field}) eq 'ARRAY') {
15479:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15480:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
15481:                                                                                    $confhash{$itemid}{$field});
15482:                                     if (@diffs) {
15483:                                         $changes{$itemid} = 1;
15484:                                     }
15485:                                 } elsif (@{$currlti{$field}} > 0) {
15486:                                     $changes{$itemid} = 1;
15487:                                 }
15488:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15489:                                 if (@{$confhash{$itemid}{$field}} > 0) {
15490:                                     $changes{$itemid} = 1;
15491:                                 }
15492:                             }
15493:                         }
15494:                     }
15495:                 }
15496:             }
15497:         }
15498:         if ($is_home) {
15499:             my $keyitem = 'form.lti_key_'.$idx;
15500:             $env{$keyitem} =~ s/(`)/'/g;
15501:             if ($env{$keyitem} ne '') {
15502:                 $ltienc{$itemid}{'key'} = $env{$keyitem};
15503:                 unless ($changes{$itemid}) {
15504:                     if ($currlti{'key'} ne $env{$keyitem}) {
15505:                         $changes{$itemid} = 1;
15506:                     }
15507:                 }
15508:             }
15509:             my $secretitem = 'form.lti_secret_'.$idx;
15510:             $env{$secretitem} =~ s/(`)/'/g;
15511:             if ($currlti{'usable'}) {
15512:                 if ($env{'form.lti_changesecret_'.$idx}) {
15513:                     if ($env{$secretitem} ne '') {
15514:                         if ($privnum && $cipher) {
15515:                             $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
15516:                             $confhash{$itemid}{'cipher'} = $privnum;
15517:                         } else {
15518:                             $ltienc{$itemid}{'secret'} = $env{$secretitem};
15519:                         }
15520:                         $changes{$itemid} = 1;
15521:                     }
15522:                 } else {
15523:                     $ltienc{$itemid}{'secret'} = $currlti{'secret'};
15524:                     $confhash{$itemid}{'cipher'} = $currlti{'cipher'};
15525:                 }
15526:                 if (ref($ltienc{$itemid}) eq 'HASH') {
15527:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'secret'} ne '')) {
15528:                         $confhash{$itemid}{'usable'} = 1;
15529:                     }
15530:                 }
15531:             } elsif ($env{$secretitem} ne '') {
15532:                 if ($privnum && $cipher) {
15533:                     $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
15534:                     $confhash{$itemid}{'cipher'} = $privnum;
15535:                 } else {
15536:                     $ltienc{$itemid}{'secret'} = $env{$secretitem};
15537:                 }
15538:                 if (ref($ltienc{$itemid}) eq 'HASH') {
15539:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'key'} ne '')) {
15540:                         $confhash{$itemid}{'usable'} = 1;
15541:                     }
15542:                 }
15543:                 $changes{$itemid} = 1;
15544:             }
15545:         }
15546:         unless ($changes{$itemid}) {
15547:             foreach my $key (keys(%currlti)) {
15548:                 if (ref($currlti{$key}) eq 'HASH') {
15549:                     if (ref($confhash{$itemid}{$key}) eq 'HASH') {
15550:                         foreach my $innerkey (keys(%{$currlti{$key}})) {
15551:                             unless (exists($confhash{$itemid}{$key}{$innerkey})) {
15552:                                 $changes{$itemid} = 1;
15553:                                 last;
15554:                             }
15555:                         }
15556:                     } elsif (keys(%{$currlti{$key}}) > 0) {
15557:                         $changes{$itemid} = 1;
15558:                     }
15559:                 }
15560:                 last if ($changes{$itemid});
15561:             }
15562:         }
15563:     }
15564:     if (@allpos > 0) {
15565:         my $idx = 0;
15566:         foreach my $itemid (@allpos) {
15567:             if ($itemid ne '') {
15568:                 $confhash{$itemid}{'order'} = $idx;
15569:                 if (ref($domconfig{$action}) eq 'HASH') {
15570:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15571:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
15572:                             $changes{$itemid} = 1;
15573:                         }
15574:                     }
15575:                 }
15576:                 $idx ++;
15577:             }
15578:         }
15579:     }
15580: 
15581:     if ((keys(%changes) == 0) && (keys(%secchanges) == 0)) {
15582:         return &mt('No changes made.');
15583:     }
15584: 
15585:     my %ltihash = (
15586:                       $action => { %confhash }
15587:                   );
15588:     my %ltienchash;
15589: 
15590:     if ($is_home) {
15591:         %ltienchash = (
15592:                          $action => { %ltienc }
15593:                       );
15594:     }
15595:     if (keys(%secchanges)) {
15596:         $ltihash{'ltisec'} = \%newltisec;
15597:         if ($secchanges{'linkprot'}) {
15598:             if ($is_home) {
15599:                 $ltienchash{'linkprot'} = \%newltienc;
15600:             }
15601:         }
15602:     }
15603:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,$dom);
15604:     if ($putresult eq 'ok') {
15605:         if (keys(%ltienchash)) {
15606:             &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
15607:         }
15608:         $resulttext = &mt('Changes made:').'<ul>';
15609:         if (keys(%secchanges) > 0) {
15610:             $resulttext .= &lti_security_results($dom,'lti',\%secchanges,\%newltisec,\%newkeyset,\%keystore);
15611:             if (exists($secchanges{'linkprot'})) {
15612:                 $resulttext .= $linkprotoutput;
15613:             }
15614:         }
15615:         if (keys(%changes) > 0) {
15616:             my $cachetime = 24*60*60;
15617:             &Apache::lonnet::do_cache_new('lti',$dom,\%confhash,$cachetime);
15618:             if (ref($lastactref) eq 'HASH') {
15619:                 $lastactref->{'lti'} = 1;
15620:             }
15621:             my %bynum;
15622:             foreach my $itemid (sort(keys(%changes))) {
15623:                 if (ref($confhash{$itemid}) eq 'HASH') {
15624:                     my $position = $confhash{$itemid}{'order'};
15625:                     $bynum{$position} = $itemid;
15626:                 }
15627:             }
15628:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
15629:                 my $itemid = $bynum{$pos};
15630:                 if (ref($confhash{$itemid}) eq 'HASH') {
15631:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b><ul>';
15632:                     my $position = $pos + 1;
15633:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
15634:                     foreach my $item ('version','lifetime') {
15635:                         if ($confhash{$itemid}{$item} ne '') {
15636:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
15637:                         }
15638:                     }
15639:                     if ($ltienc{$itemid}{'key'} ne '') {
15640:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$ltienc{$itemid}{'key'}.'</li>';
15641:                     }
15642:                     if ($ltienc{$itemid}{'secret'} ne '') {
15643:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;['.&mt('not shown').']</li>';
15644:                     }
15645:                     if ($confhash{$itemid}{'requser'}) {
15646:                         if ($confhash{$itemid}{'callback'}) {
15647:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
15648:                         } else {
15649:                             $resulttext .= '<li>'.&mt('Callback to logout LON-CAPA on log out from Consumer').'</li>';
15650:                         }
15651:                         if ($confhash{$itemid}{'mapuser'}) {
15652:                             my $shownmapuser;
15653:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
15654:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
15655:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
15656:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
15657:                             } else {
15658:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
15659:                             }
15660:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
15661:                         }
15662:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
15663:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
15664:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
15665:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
15666:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
15667:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
15668:                                 } else {
15669:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
15670:                                                        $confhash{$itemid}{'lcauth'});
15671:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
15672:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
15673:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
15674:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
15675:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
15676:                                         }
15677:                                     } else {
15678:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
15679:                                     }
15680:                                 }
15681:                                 $resulttext .= '</li>';
15682:                             } else {
15683:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
15684:                             }
15685:                         }
15686:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
15687:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
15688:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
15689:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
15690:                             } else {
15691:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
15692:                             }
15693:                         }
15694:                         foreach my $item ('topmenu','inlinemenu') {
15695:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
15696:                             if ($confhash{$itemid}{$item}) {
15697:                                 $resulttext .= &mt('Yes');
15698:                             } else {
15699:                                 $resulttext .= &mt('No');
15700:                             }
15701:                             $resulttext .= '</li>';
15702:                         }
15703:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
15704:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
15705:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
15706:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
15707:                             } else {
15708:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
15709:                             }
15710:                         }
15711:                         if ($confhash{$itemid}{'crsinc'}) {
15712:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15713:                                 my $rolemaps;
15714:                                 foreach my $role (@ltiroles) {
15715:                                     if ($confhash{$itemid}{'maproles'}{$role}) {
15716:                                         $rolemaps .= ('&nbsp;'x2).$role.'='.
15717:                                                      &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
15718:                                                                                 'Course').',';
15719:                                     }
15720:                                 }
15721:                                 if ($rolemaps) {
15722:                                     $rolemaps =~ s/,$//;
15723:                                     $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
15724:                                 }
15725:                             }
15726:                             if ($confhash{$itemid}{'mapcrs'}) {
15727:                                 $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
15728:                             }
15729:                             if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
15730:                                 if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
15731:                                     $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
15732:                                                    join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
15733:                                                    '</li>';
15734:                                 } else {
15735:                                     $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
15736:                                 }
15737:                             }
15738:                             if ($confhash{$itemid}{'storecrs'}) {
15739:                                 $resulttext .= '<li>'.&mt('Store mapping of course identifier to LON-CAPA CourseID').': '.$confhash{$itemid}{'storecrs'}.'</li>';
15740:                             }
15741:                             if ($confhash{$itemid}{'makecrs'}) {
15742:                                 $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
15743:                             } else {
15744:                                 $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
15745:                             }
15746:                             if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
15747:                                 if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
15748:                                     $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
15749:                                                               join(', ',@{$confhash{$itemid}{'selfenroll'}})).
15750:                                                    '</li>';
15751:                                 } else {
15752:                                     $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
15753:                                 }
15754:                             }
15755:                             if ($confhash{$itemid}{'section'}) {
15756:                                 if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
15757:                                     $resulttext .= '<li>'.&mt('User section from standard field:').
15758:                                                          ' (course_section_sourcedid)'.'</li>';  
15759:                                 } else {
15760:                                     $resulttext .= '<li>'.&mt('User section from:').' '.
15761:                                                           $confhash{$itemid}{'section'}.'</li>';
15762:                                 }
15763:                             } else {
15764:                                 $resulttext .= '<li>'.&mt('No section assignment').'</li>';
15765:                             }
15766:                             foreach my $item ('passback','roster','topmenu','inlinemenu') {
15767:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
15768:                                 if ($confhash{$itemid}{$item}) {
15769:                                     $resulttext .= &mt('Yes');
15770:                                     if ($item eq 'passback') {
15771:                                         if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
15772:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
15773:                                         } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
15774:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
15775:                                         }
15776:                                     }
15777:                                 } else {
15778:                                     $resulttext .= &mt('No');
15779:                                 }
15780:                                 $resulttext .= '</li>';
15781:                             }
15782:                             if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
15783:                                 if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
15784:                                     $resulttext .= '<li>'.&mt('Menu items:').' '.
15785:                                                    join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
15786:                                 } else {
15787:                                     $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
15788:                                 }
15789:                             }
15790:                         }
15791:                     }
15792:                     $resulttext .= '</ul></li>';
15793:                 }
15794:             }
15795:             if (keys(%deletions)) {
15796:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
15797:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
15798:                 }
15799:             }
15800:         }
15801:         $resulttext .= '</ul>';
15802:         if (ref($lastactref) eq 'HASH') {
15803:             if (($secchanges{'encrypt'}) || ($secchanges{'private'})) {
15804:                 $lastactref->{'domdefaults'} = 1;
15805:             }
15806:         }
15807:     } else {
15808:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15809:     }
15810:     if ($errors) {
15811:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15812:                        $errors.'</ul>';
15813:     }
15814:     return $resulttext;
15815: }
15816: 
15817: sub get_priv_creds {
15818:     my ($dom,$home,$encchg,$encrypt,$storedsec) = @_;
15819:     my ($needenc,$cipher,$privnum);
15820:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
15821:     if (($encchg) && (ref($encrypt) eq 'HASH')) {
15822:         $needenc = $encrypt->{'consumers'} 
15823:     } else {
15824:         $needenc = $domdefs{'ltienc_consumers'};
15825:     }
15826:     if ($needenc) {
15827:         if (($storedsec eq 'ok') || ((ref($domdefs{'ltiprivhosts'}) eq 'ARRAY') &&
15828:                                      (grep(/^\Q$home\E$/,@{$domdefs{'ltiprivhosts'}})))) {
15829:                         my %privhash  = &Apache::lonnet::restore_dom('lti','private',$dom,$home,1);
15830:             my $privkey = $privhash{'key'};
15831:             $privnum = $privhash{'version'};
15832:             if (($privnum) && ($privkey ne '')) {
15833:                 $cipher = Crypt::CBC->new({'key'     => $privkey,
15834:                                           'cipher'  => 'DES'});
15835:             }
15836:         }
15837:     }
15838:     return ($cipher,$privnum);
15839: }
15840: 
15841: sub get_lti_id {
15842:     my ($domain,$consumer) = @_;
15843:     # get lock on lti db
15844:     my $lockhash = {
15845:                       lock => $env{'user.name'}.
15846:                               ':'.$env{'user.domain'},
15847:                    };
15848:     my $tries = 0;
15849:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
15850:     my ($id,$error);
15851: 
15852:     while (($gotlock ne 'ok') && ($tries<10)) {
15853:         $tries ++;
15854:         sleep (0.1);
15855:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
15856:     }
15857:     if ($gotlock eq 'ok') {
15858:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
15859:         if ($currids{'lock'}) {
15860:             delete($currids{'lock'});
15861:             if (keys(%currids)) {
15862:                 my @curr = sort { $a <=> $b } keys(%currids);
15863:                 if ($curr[-1] =~ /^\d+$/) {
15864:                     $id = 1 + $curr[-1];
15865:                 }
15866:             } else {
15867:                 $id = 1;
15868:             }
15869:             if ($id) {
15870:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
15871:                     $error = 'nostore';
15872:                 }
15873:             } else {
15874:                 $error = 'nonumber';
15875:             }
15876:         }
15877:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
15878:     } else {
15879:         $error = 'nolock';
15880:     }
15881:     return ($id,$error);
15882: }
15883: 
15884: sub modify_autoenroll {
15885:     my ($dom,$lastactref,%domconfig) = @_;
15886:     my ($resulttext,%changes);
15887:     my %currautoenroll;
15888:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
15889:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
15890:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
15891:         }
15892:     }
15893:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
15894:     my %title = ( run => 'Auto-enrollment active',
15895:                   sender => 'Sender for notification messages',
15896:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
15897:                   autofailsafe => 'Failsafe for no drops if institutional data missing for a section');
15898:     my @offon = ('off','on');
15899:     my $sender_uname = $env{'form.sender_uname'};
15900:     my $sender_domain = $env{'form.sender_domain'};
15901:     if ($sender_domain eq '') {
15902:         $sender_uname = '';
15903:     } elsif ($sender_uname eq '') {
15904:         $sender_domain = '';
15905:     }
15906:     my $coowners = $env{'form.autoassign_coowners'};
15907:     my $autofailsafe = $env{'form.autoenroll_autofailsafe'};
15908:     $autofailsafe =~ s{^\s+|\s+$}{}g;
15909:     if ($autofailsafe =~ /\D/) {
15910:         undef($autofailsafe);
15911:     }
15912:     my $failsafe = $env{'form.autoenroll_failsafe'};
15913:     unless (($failsafe eq 'zero') || ($failsafe eq 'any')) {
15914:         $failsafe = 'off';
15915:         undef($autofailsafe);
15916:     }
15917:     my %autoenrollhash =  (
15918:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
15919:                                        'sender_uname' => $sender_uname,
15920:                                        'sender_domain' => $sender_domain,
15921:                                        'co-owners' => $coowners,
15922:                                        'autofailsafe' => $autofailsafe,
15923:                                        'failsafe' => $failsafe,
15924:                                 }
15925:                      );
15926:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
15927:                                              $dom);
15928:     if ($putresult eq 'ok') {
15929:         if (exists($currautoenroll{'run'})) {
15930:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
15931:                  $changes{'run'} = 1;
15932:              }
15933:         } elsif ($autorun) {
15934:             if ($env{'form.autoenroll_run'} ne '1') {
15935:                  $changes{'run'} = 1;
15936:             }
15937:         }
15938:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
15939:             $changes{'sender'} = 1;
15940:         }
15941:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
15942:             $changes{'sender'} = 1;
15943:         }
15944:         if ($currautoenroll{'co-owners'} ne '') {
15945:             if ($currautoenroll{'co-owners'} ne $coowners) {
15946:                 $changes{'coowners'} = 1;
15947:             }
15948:         } elsif ($coowners) {
15949:             $changes{'coowners'} = 1;
15950:         }
15951:         if ($currautoenroll{'autofailsafe'} ne $autofailsafe) {
15952:             $changes{'autofailsafe'} = 1;
15953:         }
15954:         if ($currautoenroll{'failsafe'} ne $failsafe) {
15955:             $changes{'failsafe'} = 1;
15956:         }
15957:         if (keys(%changes) > 0) {
15958:             $resulttext = &mt('Changes made:').'<ul>';
15959:             if ($changes{'run'}) {
15960:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
15961:             }
15962:             if ($changes{'sender'}) {
15963:                 if ($sender_uname eq '' || $sender_domain eq '') {
15964:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
15965:                 } else {
15966:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
15967:                 }
15968:             }
15969:             if ($changes{'coowners'}) {
15970:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
15971:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
15972:                 if (ref($lastactref) eq 'HASH') {
15973:                     $lastactref->{'domainconfig'} = 1;
15974:                 }
15975:             }
15976:             if ($changes{'autofailsafe'}) {
15977:                 if ($autofailsafe ne '') {
15978:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$autofailsafe).'</li>';
15979:                 } else {
15980:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
15981:                 }
15982:             }
15983:             if ($changes{'failsafe'}) {
15984:                 if ($failsafe eq 'off') {
15985:                     unless ($changes{'autofailsafe'}) {
15986:                         $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
15987:                     }
15988:                 } elsif ($failsafe eq 'zero') {
15989:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero').'</li>';
15990:                 } else {
15991:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero or greater').'</li>';
15992:                 }
15993:             }
15994:             if (($changes{'autofailsafe'}) || ($changes{'failsafe'})) {
15995:                 &Apache::lonnet::get_domain_defaults($dom,1);
15996:                 if (ref($lastactref) eq 'HASH') {
15997:                     $lastactref->{'domdefaults'} = 1;
15998:                 }
15999:             }
16000:             $resulttext .= '</ul>';
16001:         } else {
16002:             $resulttext = &mt('No changes made to auto-enrollment settings');
16003:         }
16004:     } else {
16005:         $resulttext = '<span class="LC_error">'.
16006: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16007:     }
16008:     return $resulttext;
16009: }
16010: 
16011: sub modify_autoupdate {
16012:     my ($dom,%domconfig) = @_;
16013:     my ($resulttext,%currautoupdate,%fields,%changes);
16014:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
16015:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
16016:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
16017:         }
16018:     }
16019:     my @offon = ('off','on');
16020:     my %title = &Apache::lonlocal::texthash (
16021:                     run        => 'Auto-update:',
16022:                     classlists => 'Updates to user information in classlists?',
16023:                     unexpired  => 'Skip updates for users without active or future roles?',
16024:                     lastactive => 'Skip updates for inactive users?',
16025:                 );
16026:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16027:     my %fieldtitles = &Apache::lonlocal::texthash (
16028:                         id => 'Student/Employee ID',
16029:                         permanentemail => 'E-mail address',
16030:                         lastname => 'Last Name',
16031:                         firstname => 'First Name',
16032:                         middlename => 'Middle Name',
16033:                         generation => 'Generation',
16034:                       );
16035:     $othertitle = &mt('All users');
16036:     if (keys(%{$usertypes}) >  0) {
16037:         $othertitle = &mt('Other users');
16038:     }
16039:     foreach my $key (keys(%env)) {
16040:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
16041:             my ($usertype,$item) = ($1,$2);
16042:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
16043:                 if ($usertype eq 'default') {   
16044:                     push(@{$fields{$1}},$2);
16045:                 } elsif (ref($types) eq 'ARRAY') {
16046:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
16047:                         push(@{$fields{$1}},$2);
16048:                     }
16049:                 }
16050:             }
16051:         }
16052:     }
16053:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
16054:     @lockablenames = sort(@lockablenames);
16055:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
16056:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16057:         if (@changed) {
16058:             $changes{'lockablenames'} = 1;
16059:         }
16060:     } else {
16061:         if (@lockablenames) {
16062:             $changes{'lockablenames'} = 1;
16063:         }
16064:     }
16065:     my %updatehash = (
16066:                       autoupdate => { run => $env{'form.autoupdate_run'},
16067:                                       classlists => $env{'form.classlists'},
16068:                                       unexpired  => $env{'form.unexpired'},
16069:                                       fields => {%fields},
16070:                                       lockablenames => \@lockablenames,
16071:                                     }
16072:                      );
16073:     my $lastactivedays;
16074:     if ($env{'form.lastactive'}) {
16075:         $lastactivedays = $env{'form.lastactivedays'};
16076:         $lastactivedays =~ s/^\s+|\s+$//g;
16077:         unless ($lastactivedays =~ /^\d+$/) {
16078:             undef($lastactivedays);
16079:             $env{'form.lastactive'} = 0;
16080:         }
16081:     }
16082:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
16083:     foreach my $key (keys(%currautoupdate)) {
16084:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
16085:             if (exists($updatehash{autoupdate}{$key})) {
16086:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
16087:                     $changes{$key} = 1;
16088:                 }
16089:             }
16090:         } elsif ($key eq 'fields') {
16091:             if (ref($currautoupdate{$key}) eq 'HASH') {
16092:                 foreach my $item (@{$types},'default') {
16093:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
16094:                         my $change = 0;
16095:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
16096:                             if (!exists($fields{$item})) {
16097:                                 $change = 1;
16098:                                 last;
16099:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
16100:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
16101:                                     $change = 1;
16102:                                     last;
16103:                                 }
16104:                             }
16105:                         }
16106:                         if ($change) {
16107:                             push(@{$changes{$key}},$item);
16108:                         }
16109:                     } 
16110:                 }
16111:             }
16112:         } elsif ($key eq 'lockablenames') {
16113:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
16114:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16115:                 if (@changed) {
16116:                     $changes{'lockablenames'} = 1;
16117:                 }
16118:             } else {
16119:                 if (@lockablenames) {
16120:                     $changes{'lockablenames'} = 1;
16121:                 }
16122:             }
16123:         }
16124:     }
16125:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
16126:         if (@lockablenames) {
16127:             $changes{'lockablenames'} = 1;
16128:         }
16129:     }
16130:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
16131:         if ($updatehash{'autoupdate'}{'unexpired'}) {
16132:             $changes{'unexpired'} = 1;
16133:         }
16134:     }
16135:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
16136:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
16137:             $changes{'lastactive'} = 1;
16138:         }
16139:     }
16140:     foreach my $item (@{$types},'default') {
16141:         if (defined($fields{$item})) {
16142:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
16143:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
16144:                     my $change = 0;
16145:                     if (ref($fields{$item}) eq 'ARRAY') {
16146:                         foreach my $type (@{$fields{$item}}) {
16147:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
16148:                                 $change = 1;
16149:                                 last;
16150:                             }
16151:                         }
16152:                     }
16153:                     if ($change) {
16154:                         push(@{$changes{'fields'}},$item);
16155:                     }
16156:                 } else {
16157:                     push(@{$changes{'fields'}},$item);
16158:                 }
16159:             } else {
16160:                 push(@{$changes{'fields'}},$item);
16161:             }
16162:         }
16163:     }
16164:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
16165:                                              $dom);
16166:     if ($putresult eq 'ok') {
16167:         if (keys(%changes) > 0) {
16168:             $resulttext = &mt('Changes made:').'<ul>';
16169:             foreach my $key (sort(keys(%changes))) {
16170:                 if ($key eq 'lockablenames') {
16171:                     $resulttext .= '<li>';
16172:                     if (@lockablenames) {
16173:                         $usertypes->{'default'} = $othertitle;
16174:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
16175:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
16176:                     } else {
16177:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
16178:                     }
16179:                     $resulttext .= '</li>';
16180:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
16181:                     foreach my $item (@{$changes{$key}}) {
16182:                         my @newvalues;
16183:                         foreach my $type (@{$fields{$item}}) {
16184:                             push(@newvalues,$fieldtitles{$type});
16185:                         }
16186:                         my $newvaluestr;
16187:                         if (@newvalues > 0) {
16188:                             $newvaluestr = join(', ',@newvalues);
16189:                         } else {
16190:                             $newvaluestr = &mt('none');
16191:                         }
16192:                         if ($item eq 'default') {
16193:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
16194:                         } else {
16195:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
16196:                         }
16197:                     }
16198:                 } else {
16199:                     my $newvalue;
16200:                     if ($key eq 'run') {
16201:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
16202:                     } elsif ($key eq 'lastactive') {
16203:                         $newvalue = $offon[$env{'form.lastactive'}];
16204:                         unless ($lastactivedays eq '') {
16205:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
16206:                         }
16207:                     } else {
16208:                         $newvalue = $offon[$env{'form.'.$key}];
16209:                     }
16210:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
16211:                 }
16212:             }
16213:             $resulttext .= '</ul>';
16214:         } else {
16215:             $resulttext = &mt('No changes made to autoupdates');
16216:         }
16217:     } else {
16218:         $resulttext = '<span class="LC_error">'.
16219: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16220:     }
16221:     return $resulttext;
16222: }
16223: 
16224: sub modify_autocreate {
16225:     my ($dom,%domconfig) = @_;
16226:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
16227:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
16228:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
16229:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
16230:         }
16231:     }
16232:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
16233:                  req => 'Auto-creation of validated requests for official courses',
16234:                  xmldc => 'Identity of course creator of courses from XML files',
16235:                );
16236:     my @types = ('xml','req');
16237:     foreach my $item (@types) {
16238:         $newvals{$item} = $env{'form.autocreate_'.$item};
16239:         $newvals{$item} =~ s/\D//g;
16240:         $newvals{$item} = 0 if ($newvals{$item} eq '');
16241:     }
16242:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
16243:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
16244:     unless (exists($domcoords{$newvals{'xmldc'}})) {
16245:         $newvals{'xmldc'} = '';
16246:     } 
16247:     %autocreatehash =  (
16248:                         autocreate => { xml => $newvals{'xml'},
16249:                                         req => $newvals{'req'},
16250:                                       }
16251:                        );
16252:     if ($newvals{'xmldc'} ne '') {
16253:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
16254:     }
16255:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
16256:                                              $dom);
16257:     if ($putresult eq 'ok') {
16258:         my @items = @types;
16259:         if ($newvals{'xml'}) {
16260:             push(@items,'xmldc');
16261:         }
16262:         foreach my $item (@items) {
16263:             if (exists($currautocreate{$item})) {
16264:                 if ($currautocreate{$item} ne $newvals{$item}) {
16265:                     $changes{$item} = 1;
16266:                 }
16267:             } elsif ($newvals{$item}) {
16268:                 $changes{$item} = 1;
16269:             }
16270:         }
16271:         if (keys(%changes) > 0) {
16272:             my @offon = ('off','on'); 
16273:             $resulttext = &mt('Changes made:').'<ul>';
16274:             foreach my $item (@types) {
16275:                 if ($changes{$item}) {
16276:                     my $newtxt = $offon[$newvals{$item}];
16277:                     $resulttext .= '<li>'.
16278:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
16279:                                        '<b>','</b>').
16280:                                    '</li>';
16281:                 }
16282:             }
16283:             if ($changes{'xmldc'}) {
16284:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
16285:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
16286:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
16287:             }
16288:             $resulttext .= '</ul>';
16289:         } else {
16290:             $resulttext = &mt('No changes made to auto-creation settings');
16291:         }
16292:     } else {
16293:         $resulttext = '<span class="LC_error">'.
16294:             &mt('An error occurred: [_1]',$putresult).'</span>';
16295:     }
16296:     return $resulttext;
16297: }
16298: 
16299: sub modify_directorysrch {
16300:     my ($dom,$lastactref,%domconfig) = @_;
16301:     my ($resulttext,%changes);
16302:     my %currdirsrch;
16303:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
16304:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
16305:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
16306:         }
16307:     }
16308:     my %title = ( available => 'Institutional directory search available',
16309:                   localonly => 'Other domains can search institution',
16310:                   lcavailable => 'LON-CAPA directory search available',
16311:                   lclocalonly => 'Other domains can search LON-CAPA domain',
16312:                   searchby => 'Search types',
16313:                   searchtypes => 'Search latitude');
16314:     my @offon = ('off','on');
16315:     my @otherdoms = ('Yes','No');
16316: 
16317:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
16318:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
16319:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
16320: 
16321:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16322:     if (keys(%{$usertypes}) == 0) {
16323:         @cansearch = ('default');
16324:     } else {
16325:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
16326:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
16327:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
16328:                     push(@{$changes{'cansearch'}},$type);
16329:                 }
16330:             }
16331:             foreach my $type (@cansearch) {
16332:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
16333:                     push(@{$changes{'cansearch'}},$type);
16334:                 }
16335:             }
16336:         } else {
16337:             push(@{$changes{'cansearch'}},@cansearch);
16338:         }
16339:     }
16340: 
16341:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
16342:         foreach my $by (@{$currdirsrch{'searchby'}}) {
16343:             if (!grep(/^\Q$by\E$/,@searchby)) {
16344:                 push(@{$changes{'searchby'}},$by);
16345:             }
16346:         }
16347:         foreach my $by (@searchby) {
16348:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
16349:                 push(@{$changes{'searchby'}},$by);
16350:             }
16351:         }
16352:     } else {
16353:         push(@{$changes{'searchby'}},@searchby);
16354:     }
16355: 
16356:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
16357:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
16358:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
16359:                 push(@{$changes{'searchtypes'}},$type);
16360:             }
16361:         }
16362:         foreach my $type (@searchtypes) {
16363:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
16364:                 push(@{$changes{'searchtypes'}},$type);
16365:             }
16366:         }
16367:     } else {
16368:         if (exists($currdirsrch{'searchtypes'})) {
16369:             foreach my $type (@searchtypes) {  
16370:                 if ($type ne $currdirsrch{'searchtypes'}) { 
16371:                     push(@{$changes{'searchtypes'}},$type);
16372:                 }
16373:             }
16374:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
16375:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
16376:             }   
16377:         } else {
16378:             push(@{$changes{'searchtypes'}},@searchtypes); 
16379:         }
16380:     }
16381: 
16382:     my %dirsrch_hash =  (
16383:             directorysrch => { available => $env{'form.dirsrch_available'},
16384:                                cansearch => \@cansearch,
16385:                                localonly => $env{'form.dirsrch_instlocalonly'},
16386:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
16387:                                lcavailable => $env{'form.dirsrch_domavailable'},
16388:                                searchby => \@searchby,
16389:                                searchtypes => \@searchtypes,
16390:                              }
16391:             );
16392:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
16393:                                              $dom);
16394:     if ($putresult eq 'ok') {
16395:         if (exists($currdirsrch{'available'})) {
16396:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
16397:                  $changes{'available'} = 1;
16398:              }
16399:         } else {
16400:             if ($env{'form.dirsrch_available'} eq '1') {
16401:                 $changes{'available'} = 1;
16402:             }
16403:         }
16404:         if (exists($currdirsrch{'lcavailable'})) {
16405:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
16406:                 $changes{'lcavailable'} = 1;
16407:             }
16408:         } else {
16409:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
16410:                 $changes{'lcavailable'} = 1;
16411:             }
16412:         }
16413:         if (exists($currdirsrch{'localonly'})) {
16414:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
16415:                 $changes{'localonly'} = 1;
16416:             }
16417:         } else {
16418:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
16419:                 $changes{'localonly'} = 1;
16420:             }
16421:         }
16422:         if (exists($currdirsrch{'lclocalonly'})) {
16423:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
16424:                 $changes{'lclocalonly'} = 1;
16425:             }
16426:         } else {
16427:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
16428:                 $changes{'lclocalonly'} = 1;
16429:             }
16430:         }
16431:         if (keys(%changes) > 0) {
16432:             $resulttext = &mt('Changes made:').'<ul>';
16433:             if ($changes{'available'}) {
16434:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
16435:             }
16436:             if ($changes{'lcavailable'}) {
16437:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
16438:             }
16439:             if ($changes{'localonly'}) {
16440:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
16441:             }
16442:             if ($changes{'lclocalonly'}) {
16443:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
16444:             }
16445:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
16446:                 my $chgtext;
16447:                 if (ref($usertypes) eq 'HASH') {
16448:                     if (keys(%{$usertypes}) > 0) {
16449:                         foreach my $type (@{$types}) {
16450:                             if (grep(/^\Q$type\E$/,@cansearch)) {
16451:                                 $chgtext .= $usertypes->{$type}.'; ';
16452:                             }
16453:                         }
16454:                         if (grep(/^default$/,@cansearch)) {
16455:                             $chgtext .= $othertitle;
16456:                         } else {
16457:                             $chgtext =~ s/\; $//;
16458:                         }
16459:                         $resulttext .=
16460:                             '<li>'.
16461:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
16462:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
16463:                             '</li>';
16464:                     }
16465:                 }
16466:             }
16467:             if (ref($changes{'searchby'}) eq 'ARRAY') {
16468:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
16469:                 my $chgtext;
16470:                 foreach my $type (@{$titleorder}) {
16471:                     if (grep(/^\Q$type\E$/,@searchby)) {
16472:                         if (defined($searchtitles->{$type})) {
16473:                             $chgtext .= $searchtitles->{$type}.'; ';
16474:                         }
16475:                     }
16476:                 }
16477:                 $chgtext =~ s/\; $//;
16478:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
16479:             }
16480:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
16481:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
16482:                 my $chgtext;
16483:                 foreach my $type (@{$srchtypeorder}) {
16484:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
16485:                         if (defined($srchtypes_desc->{$type})) {
16486:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
16487:                         }
16488:                     }
16489:                 }
16490:                 $chgtext =~ s/\; $//;
16491:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
16492:             }
16493:             $resulttext .= '</ul>';
16494:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
16495:             if (ref($lastactref) eq 'HASH') {
16496:                 $lastactref->{'directorysrch'} = 1;
16497:             }
16498:         } else {
16499:             $resulttext = &mt('No changes made to directory search settings');
16500:         }
16501:     } else {
16502:         $resulttext = '<span class="LC_error">'.
16503:                       &mt('An error occurred: [_1]',$putresult).'</span>';
16504:     }
16505:     return $resulttext;
16506: }
16507: 
16508: sub modify_contacts {
16509:     my ($dom,$lastactref,%domconfig) = @_;
16510:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
16511:     if (ref($domconfig{'contacts'}) eq 'HASH') {
16512:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
16513:             $currsetting{$key} = $domconfig{'contacts'}{$key};
16514:         }
16515:     }
16516:     my (%others,%to,%bcc,%includestr,%includeloc);
16517:     my @contacts = ('supportemail','adminemail');
16518:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
16519:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
16520:     my @toggles = ('reporterrors','reportupdates','reportstatus');
16521:     my @lonstatus = ('threshold','sysmail','weights','excluded');
16522:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
16523:     foreach my $type (@mailings) {
16524:         @{$newsetting{$type}} = 
16525:             &Apache::loncommon::get_env_multiple('form.'.$type);
16526:         foreach my $item (@contacts) {
16527:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
16528:                 $contacts_hash{contacts}{$type}{$item} = 1;
16529:             } else {
16530:                 $contacts_hash{contacts}{$type}{$item} = 0;
16531:             }
16532:         }
16533:         $others{$type} = $env{'form.'.$type.'_others'};
16534:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
16535:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16536:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
16537:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
16538:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
16539:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
16540:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
16541:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
16542:             }
16543:         }
16544:     }
16545:     foreach my $item (@contacts) {
16546:         $to{$item} = $env{'form.'.$item};
16547:         $contacts_hash{'contacts'}{$item} = $to{$item};
16548:     }
16549:     foreach my $item (@toggles) {
16550:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
16551:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
16552:         }
16553:     }
16554:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
16555:     foreach my $item (@lonstatus) {
16556:         if ($item eq 'excluded') {
16557:             my (%serverhomes,@excluded);
16558:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
16559:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
16560:             if (@possexcluded) {
16561:                 foreach my $id (sort(@possexcluded)) {
16562:                     if ($serverhomes{$id}) {
16563:                         push(@excluded,$id);
16564:                     }
16565:                 }
16566:             }
16567:             if (@excluded) {
16568:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
16569:             }
16570:         } elsif ($item eq 'weights') {
16571:             foreach my $type ('E','W','N','U') {
16572:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
16573:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
16574:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
16575:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
16576:                             $env{'form.error'.$item.'_'.$type};
16577:                     }
16578:                 }
16579:             }
16580:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
16581:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
16582:             if ($env{'form.error'.$item} =~ /^\d+$/) {
16583:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
16584:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
16585:                 }
16586:             }
16587:         }
16588:     }
16589:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
16590:         foreach my $field (@{$fields}) {
16591:             if (ref($possoptions->{$field}) eq 'ARRAY') {
16592:                 my $value = $env{'form.helpform_'.$field};
16593:                 $value =~ s/^\s+|\s+$//g;
16594:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
16595:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
16596:                     if ($field eq 'screenshot') {
16597:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
16598:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
16599:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
16600:                         }
16601:                     }
16602:                 }
16603:             }
16604:         }
16605:     }
16606:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16607:     my (@statuses,%usertypeshash,@overrides);
16608:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
16609:         @statuses = @{$types};
16610:         if (ref($usertypes) eq 'HASH') {
16611:             %usertypeshash = %{$usertypes};
16612:         }
16613:     }
16614:     if (@statuses) {
16615:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
16616:         foreach my $type (@possoverrides) {
16617:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
16618:                 push(@overrides,$type);
16619:             }
16620:         }
16621:         if (@overrides) {
16622:             foreach my $type (@overrides) {
16623:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
16624:                 foreach my $item (@contacts) {
16625:                     if (grep(/^\Q$item\E$/,@standard)) {
16626:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
16627:                         $newsetting{'override_'.$type}{$item} = 1;
16628:                     } else {
16629:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
16630:                         $newsetting{'override_'.$type}{$item} = 0;
16631:                     }
16632:                 }
16633:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
16634:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
16635:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
16636:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
16637:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
16638:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
16639:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
16640:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
16641:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
16642:                 }
16643:             }
16644:         }
16645:     }
16646:     if (keys(%currsetting) > 0) {
16647:         foreach my $item (@contacts) {
16648:             if ($to{$item} ne $currsetting{$item}) {
16649:                 $changes{$item} = 1;
16650:             }
16651:         }
16652:         foreach my $type (@mailings) {
16653:             foreach my $item (@contacts) {
16654:                 if (ref($currsetting{$type}) eq 'HASH') {
16655:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
16656:                         push(@{$changes{$type}},$item);
16657:                     }
16658:                 } else {
16659:                     push(@{$changes{$type}},@{$newsetting{$type}});
16660:                 }
16661:             }
16662:             if ($others{$type} ne $currsetting{$type}{'others'}) {
16663:                 push(@{$changes{$type}},'others');
16664:             }
16665:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16666:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
16667:                     push(@{$changes{$type}},'bcc'); 
16668:                 }
16669:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
16670:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
16671:                     push(@{$changes{$type}},'include');
16672:                 }
16673:             }
16674:         }
16675:         if (ref($fields) eq 'ARRAY') {
16676:             if (ref($currsetting{'helpform'}) eq 'HASH') {
16677:                 foreach my $field (@{$fields}) {
16678:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
16679:                         push(@{$changes{'helpform'}},$field);
16680:                     }
16681:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16682:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
16683:                             push(@{$changes{'helpform'}},'maxsize');
16684:                         }
16685:                     }
16686:                 }
16687:             } else {
16688:                 foreach my $field (@{$fields}) {
16689:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
16690:                         push(@{$changes{'helpform'}},$field);
16691:                     }
16692:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16693:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
16694:                             push(@{$changes{'helpform'}},'maxsize');
16695:                         }
16696:                     }
16697:                 }
16698:             }
16699:         }
16700:         if (@statuses) {
16701:             if (ref($currsetting{'overrides'}) eq 'HASH') {
16702:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
16703:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
16704:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
16705:                             foreach my $item (@contacts,'bcc','others','include') {
16706:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
16707:                                     push(@{$changes{'overrides'}},$key);
16708:                                     last;
16709:                                 }
16710:                             }
16711:                         } else {
16712:                             push(@{$changes{'overrides'}},$key);
16713:                         }
16714:                     }
16715:                 }
16716:                 foreach my $key (@overrides) {
16717:                     unless (exists($currsetting{'overrides'}{$key})) {
16718:                         push(@{$changes{'overrides'}},$key);
16719:                     }
16720:                 }
16721:             } else {
16722:                 foreach my $key (@overrides) {
16723:                     push(@{$changes{'overrides'}},$key);
16724:                 }
16725:             }
16726:         }
16727:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
16728:             foreach my $key ('excluded','weights','threshold','sysmail') {
16729:                 if ($key eq 'excluded') {
16730:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
16731:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
16732:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
16733:                             (@{$currsetting{'lonstatus'}{$key}})) {
16734:                             my @diffs =
16735:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
16736:                                                                    $currsetting{'lonstatus'}{$key});
16737:                             if (@diffs) {
16738:                                 push(@{$changes{'lonstatus'}},$key);
16739:                             }
16740:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
16741:                             push(@{$changes{'lonstatus'}},$key);
16742:                         }
16743:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
16744:                              (@{$currsetting{'lonstatus'}{$key}})) {
16745:                         push(@{$changes{'lonstatus'}},$key);
16746:                     }
16747:                 } elsif ($key eq 'weights') {
16748:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
16749:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
16750:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
16751:                             foreach my $type ('E','W','N','U') {
16752:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
16753:                                         $currsetting{'lonstatus'}{$key}{$type}) {
16754:                                     push(@{$changes{'lonstatus'}},$key);
16755:                                     last;
16756:                                 }
16757:                             }
16758:                         } else {
16759:                             foreach my $type ('E','W','N','U') {
16760:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
16761:                                     push(@{$changes{'lonstatus'}},$key);
16762:                                     last;
16763:                                 }
16764:                             }
16765:                         }
16766:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
16767:                         foreach my $type ('E','W','N','U') {
16768:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
16769:                                 push(@{$changes{'lonstatus'}},$key);
16770:                                 last;
16771:                             }
16772:                         }
16773:                     }
16774:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
16775:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16776:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
16777:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
16778:                                 push(@{$changes{'lonstatus'}},$key);
16779:                             }
16780:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
16781:                             push(@{$changes{'lonstatus'}},$key);
16782:                         }
16783:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
16784:                         push(@{$changes{'lonstatus'}},$key);
16785:                     }
16786:                 }
16787:             }
16788:         } else {
16789:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16790:                 foreach my $key ('excluded','weights','threshold','sysmail') {
16791:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
16792:                         push(@{$changes{'lonstatus'}},$key);
16793:                     }
16794:                 }
16795:             }
16796:         }
16797:     } else {
16798:         my %default;
16799:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
16800:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
16801:         $default{'errormail'} = 'adminemail';
16802:         $default{'packagesmail'} = 'adminemail';
16803:         $default{'helpdeskmail'} = 'supportemail';
16804:         $default{'otherdomsmail'} = 'supportemail';
16805:         $default{'lonstatusmail'} = 'adminemail';
16806:         $default{'requestsmail'} = 'adminemail';
16807:         $default{'updatesmail'} = 'adminemail';
16808:         $default{'hostipmail'} = 'adminemail';
16809:         foreach my $item (@contacts) {
16810:            if ($to{$item} ne $default{$item}) {
16811:                $changes{$item} = 1;
16812:            }
16813:         }
16814:         foreach my $type (@mailings) {
16815:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
16816:                 push(@{$changes{$type}},@{$newsetting{$type}});
16817:             }
16818:             if ($others{$type} ne '') {
16819:                 push(@{$changes{$type}},'others');
16820:             }
16821:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16822:                 if ($bcc{$type} ne '') {
16823:                     push(@{$changes{$type}},'bcc');
16824:                 }
16825:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
16826:                     push(@{$changes{$type}},'include');
16827:                 }
16828:             }
16829:         }
16830:         if (ref($fields) eq 'ARRAY') {
16831:             foreach my $field (@{$fields}) {
16832:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
16833:                     push(@{$changes{'helpform'}},$field);
16834:                 }
16835:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16836:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
16837:                         push(@{$changes{'helpform'}},'maxsize');
16838:                     }
16839:                 }
16840:             }
16841:         }
16842:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16843:             foreach my $key ('excluded','weights','threshold','sysmail') {
16844:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
16845:                     push(@{$changes{'lonstatus'}},$key);
16846:                 }
16847:             }
16848:         }
16849:     }
16850:     foreach my $item (@toggles) {
16851:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
16852:             $changes{$item} = 1;
16853:         } elsif ((!$env{'form.'.$item}) &&
16854:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
16855:             $changes{$item} = 1;
16856:         }
16857:     }
16858:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
16859:                                              $dom);
16860:     if ($putresult eq 'ok') {
16861:         if (keys(%changes) > 0) {
16862:             &Apache::loncommon::devalidate_domconfig_cache($dom);
16863:             if (ref($lastactref) eq 'HASH') {
16864:                 $lastactref->{'domainconfig'} = 1;
16865:             }
16866:             my ($titles,$short_titles)  = &contact_titles();
16867:             $resulttext = &mt('Changes made:').'<ul>';
16868:             foreach my $item (@contacts) {
16869:                 if ($changes{$item}) {
16870:                     $resulttext .= '<li>'.$titles->{$item}.
16871:                                     &mt(' set to: ').
16872:                                     '<span class="LC_cusr_emph">'.
16873:                                     $to{$item}.'</span></li>';
16874:                 }
16875:             }
16876:             foreach my $type (@mailings) {
16877:                 if (ref($changes{$type}) eq 'ARRAY') {
16878:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16879:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
16880:                     } else {
16881:                         $resulttext .= '<li>'.$titles->{$type}.': ';
16882:                     }
16883:                     my @text;
16884:                     foreach my $item (@{$newsetting{$type}}) {
16885:                         push(@text,$short_titles->{$item});
16886:                     }
16887:                     if ($others{$type} ne '') {
16888:                         push(@text,$others{$type});
16889:                     }
16890:                     if (@text) {
16891:                         $resulttext .= '<span class="LC_cusr_emph">'.
16892:                                        join(', ',@text).'</span>';
16893:                     }
16894:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16895:                         if ($bcc{$type} ne '') {
16896:                             my $bcctext;
16897:                             if (@text) {
16898:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
16899:                             } else {
16900:                                 $bcctext = '(Bcc)';
16901:                             }
16902:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
16903:                         } elsif (!@text) {
16904:                             $resulttext .= &mt('No one');
16905:                         }
16906:                         if ($includestr{$type} ne '') {
16907:                             if ($includeloc{$type} eq 'b') {
16908:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
16909:                             } elsif ($includeloc{$type} eq 's') {
16910:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
16911:                             }
16912:                         }
16913:                     } elsif (!@text) {
16914:                         $resulttext .= &mt('No recipients');
16915:                     }
16916:                     $resulttext .= '</li>';
16917:                 }
16918:             }
16919:             if (ref($changes{'overrides'}) eq 'ARRAY') {
16920:                 my @deletions;
16921:                 foreach my $type (@{$changes{'overrides'}}) {
16922:                     if ($usertypeshash{$type}) {
16923:                         if (grep(/^\Q$type\E/,@overrides)) {
16924:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
16925:                                                       $usertypeshash{$type}).'<ul><li>';
16926:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
16927:                                 my @text;
16928:                                 foreach my $item (@contacts) {
16929:                                     if ($newsetting{'override_'.$type}{$item}) {
16930:                                         push(@text,$short_titles->{$item});
16931:                                     }
16932:                                 }
16933:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
16934:                                     push(@text,$newsetting{'override_'.$type}{'others'});
16935:                                 }
16936: 
16937:                                 if (@text) {
16938:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
16939:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
16940:                                 }
16941:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
16942:                                     my $bcctext;
16943:                                     if (@text) {
16944:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
16945:                                     } else {
16946:                                         $bcctext = '(Bcc)';
16947:                                     }
16948:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
16949:                                 } elsif (!@text) {
16950:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
16951:                                 }
16952:                                 $resulttext .= '</li>';
16953:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
16954:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
16955:                                     if ($loc eq 'b') {
16956:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
16957:                                     } elsif ($loc eq 's') {
16958:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
16959:                                     }
16960:                                 }
16961:                             }
16962:                             $resulttext .= '</li></ul></li>';
16963:                         } else {
16964:                             push(@deletions,$usertypeshash{$type});
16965:                         }
16966:                     }
16967:                 }
16968:                 if (@deletions) {
16969:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
16970:                                               join(', ',@deletions)).'</li>';
16971:                 }
16972:             }
16973:             my @offon = ('off','on');
16974:             my $corelink = &core_link_msu();
16975:             if ($changes{'reporterrors'}) {
16976:                 $resulttext .= '<li>'.
16977:                                &mt('E-mail error reports to [_1] set to "'.
16978:                                    $offon[$env{'form.reporterrors'}].'".',
16979:                                    $corelink).
16980:                                '</li>';
16981:             }
16982:             if ($changes{'reportupdates'}) {
16983:                 $resulttext .= '<li>'.
16984:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
16985:                                     $offon[$env{'form.reportupdates'}].'".',
16986:                                     $corelink).
16987:                                 '</li>';
16988:             }
16989:             if ($changes{'reportstatus'}) {
16990:                 $resulttext .= '<li>'.
16991:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
16992:                                     $offon[$env{'form.reportstatus'}].'".',
16993:                                     $corelink).
16994:                                 '</li>';
16995:             }
16996:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
16997:                 $resulttext .= '<li>'.
16998:                                &mt('Nightly status check e-mail settings').':<ul>';
16999:                 my (%defval,%use_def,%shown);
17000:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
17001:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
17002:                 $defval{'weights'} =
17003:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
17004:                 $defval{'excluded'} = &mt('None');
17005:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
17006:                     foreach my $item ('threshold','sysmail','weights','excluded') {
17007:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
17008:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
17009:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
17010:                             } elsif ($item eq 'weights') {
17011:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
17012:                                     foreach my $type ('E','W','N','U') {
17013:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
17014:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
17015:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
17016:                                         } else {
17017:                                             $shown{$item} .= $lonstatus_defs->{$type};
17018:                                         }
17019:                                         $shown{$item} .= ', ';
17020:                                     }
17021:                                     $shown{$item} =~ s/, $//;
17022:                                 } else {
17023:                                     $shown{$item} = $defval{$item};
17024:                                 }
17025:                             } elsif ($item eq 'excluded') {
17026:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
17027:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
17028:                                 } else {
17029:                                     $shown{$item} = $defval{$item};
17030:                                 }
17031:                             }
17032:                         } else {
17033:                             $shown{$item} = $defval{$item};
17034:                         }
17035:                     }
17036:                 } else {
17037:                     foreach my $item ('threshold','weights','excluded','sysmail') {
17038:                         $shown{$item} = $defval{$item};
17039:                     }
17040:                 }
17041:                 foreach my $item ('threshold','weights','excluded','sysmail') {
17042:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
17043:                                           $shown{$item}).'</li>';
17044:                 }
17045:                 $resulttext .= '</ul></li>';
17046:             }
17047:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
17048:                 my (@optional,@required,@unused,$maxsizechg);
17049:                 foreach my $field (@{$changes{'helpform'}}) {
17050:                     if ($field eq 'maxsize') {
17051:                         $maxsizechg = 1;
17052:                         next;
17053:                     }
17054:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
17055:                         push(@optional,$field);
17056:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
17057:                         push(@unused,$field);
17058:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
17059:                         push(@required,$field);
17060:                     }
17061:                 }
17062:                 if (@optional) {
17063:                     $resulttext .= '<li>'.
17064:                                    &mt('Help form fields changed to "Optional": [_1].',
17065:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
17066:                                    '</li>';
17067:                 }
17068:                 if (@required) {
17069:                     $resulttext .= '<li>'.
17070:                                    &mt('Help form fields changed to "Required": [_1].',
17071:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
17072:                                    '</li>';
17073:                 }
17074:                 if (@unused) {
17075:                     $resulttext .= '<li>'.
17076:                                    &mt('Help form fields changed to "Not shown": [_1].',
17077:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
17078:                                    '</li>';
17079:                 }
17080:                 if ($maxsizechg) {
17081:                     $resulttext .= '<li>'.
17082:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
17083:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
17084:                                    '</li>';
17085:                 }
17086:             }
17087:             $resulttext .= '</ul>';
17088:         } else {
17089:             $resulttext = &mt('No changes made to contacts and form settings');
17090:         }
17091:     } else {
17092:         $resulttext = '<span class="LC_error">'.
17093:             &mt('An error occurred: [_1].',$putresult).'</span>';
17094:     }
17095:     return $resulttext;
17096: }
17097: 
17098: sub modify_privacy {
17099:     my ($dom,$lastactref,%domconfig) = @_;
17100:     my ($resulttext,%current,%changes);
17101:     if (ref($domconfig{'privacy'}) eq 'HASH') {
17102:         %current = %{$domconfig{'privacy'}};
17103:     }
17104:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
17105:     my @items = ('domain','author','course','community');
17106:     my %names = &Apache::lonlocal::texthash (
17107:                    domain => 'Assigned domain role(s)',
17108:                    author => 'Assigned co-author role(s)',
17109:                    course => 'Assigned course role(s)',
17110:                    community => 'Assigned community role(s)',
17111:                 );
17112:     my %roles = &Apache::lonlocal::texthash (
17113:                    domain => 'Domain role',
17114:                    author => 'Co-author role',
17115:                    course => 'Course role',
17116:                    community => 'Community role',
17117:                 );
17118:     my %titles = &Apache::lonlocal::texthash (
17119:                   approval => 'Approval for role in different domain',
17120:                   othdom   => 'User information available in other domain',
17121:                   priv     => 'Information viewable by privileged user in same domain',
17122:                   unpriv   => 'Information viewable by unprivileged user in same domain',
17123:                   instdom  => 'Other domain shares institution/provider',
17124:                   extdom   => 'Other domain has different institution/provider',
17125:                   none     => 'Not allowed',
17126:                   user     => 'User authorizes',
17127:                   domain   => 'Domain Coordinator authorizes',
17128:                   auto     => 'Unrestricted',
17129:                   notify   => 'Notify when role needs authorization',
17130:     );
17131:     my %fieldnames = &Apache::lonlocal::texthash (
17132:                         id => 'Student/Employee ID',
17133:                         permanentemail => 'E-mail address',
17134:                         lastname => 'Last Name',
17135:                         firstname => 'First Name',
17136:                         middlename => 'Middle Name',
17137:                         generation => 'Generation',
17138:     );
17139:     my ($othertitle,$usertypes,$types) =
17140:         &Apache::loncommon::sorted_inst_types($dom);
17141:     my (%by_ip,%by_location,@intdoms,@instdoms);
17142:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
17143: 
17144:     my %privacyhash = (
17145:                        'approval' => {
17146:                                        instdom => {},
17147:                                        extdom  => {},
17148:                                      },
17149:                        'othdom'   => {},
17150:                        'priv'     => {},
17151:                        'unpriv'   => {},
17152:                       );
17153:     foreach my $item (@items) {
17154:         if (@instdoms > 1) {
17155:             if ($env{'form.privacy_approval_instdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17156:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
17157:             }
17158:             if (ref($current{'approval'}) eq 'HASH') {
17159:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
17160:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
17161:                         $changes{'approval'} = 1;
17162:                     }
17163:                 }
17164:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
17165:                 $changes{'approval'} = 1;
17166:             }
17167:         }
17168:         if (keys(%by_location) > 0) {
17169:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17170:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
17171:             }
17172:             if (ref($current{'approval'}) eq 'HASH') {
17173:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
17174:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
17175:                         $changes{'approval'} = 1;
17176:                     }
17177:                 }
17178:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
17179:                 $changes{'approval'} = 1;
17180:             }
17181:         }
17182:         foreach my $status ('priv','unpriv') {
17183:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
17184:             my @newvalues;
17185:             foreach my $field (@possibles) {
17186:                 if (grep(/^\Q$field\E$/,@fields)) {
17187:                     $privacyhash{$status}{$item}{$field} = 1;
17188:                     push(@newvalues,$field);
17189:                 }
17190:             }
17191:             @newvalues = sort(@newvalues);
17192:             if (ref($current{$status}) eq 'HASH') {
17193:                 if (ref($current{$status}{$item}) eq 'HASH') {
17194:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
17195:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17196:                     if (@diffs > 0) {
17197:                         $changes{$status} = 1;
17198:                     }
17199:                 }
17200:             } else {
17201:                 my @stdfields;
17202:                 foreach my $field (@fields) {
17203:                     if ($field eq 'id') {
17204:                         next if ($status eq 'unpriv');
17205:                         next if (($status eq 'priv') && ($item eq 'community'));
17206:                     }
17207:                     push(@stdfields,$field);
17208:                 }
17209:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17210:                 if (@diffs > 0) {
17211:                     $changes{$status} = 1;
17212:                 }
17213:             }
17214:         }
17215:     }
17216:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
17217:         my @statuses;
17218:         if (ref($types) eq 'ARRAY') {
17219:             @statuses = @{$types};
17220:         }
17221:         foreach my $type (@statuses,'default') {
17222:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
17223:             my @newvalues;
17224:             foreach my $field (sort(@possfields)) {
17225:                 if (grep(/^\Q$field\E$/,@fields)) {
17226:                     $privacyhash{'othdom'}{$type}{$field} = 1;
17227:                     push(@newvalues,$field);
17228:                 }
17229:             }
17230:             @newvalues = sort(@newvalues);
17231:             if (ref($current{'othdom'}) eq 'HASH') {
17232:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
17233:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
17234:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17235:                     if (@diffs > 0) {
17236:                         $changes{'othdom'} = 1;
17237:                     }
17238:                 }
17239:             } else {
17240:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
17241:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17242:                 if (@diffs > 0) {
17243:                     $changes{'othdom'} = 1;
17244:                 }
17245:             }
17246:         }
17247:         my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
17248:         my %notify;
17249:         foreach my $possdc (&Apache::loncommon::get_env_multiple('form.privacy_notify')) {
17250:             if (exists($domcoords{$possdc})) {
17251:                 $notify{$possdc} = 1;
17252:             }
17253:         }
17254:         my $notify = join(',',sort(keys(%notify)));
17255:         if ($current{'notify'} ne $notify) {
17256:             $changes{'notify'} = 1;
17257:         }
17258:         $privacyhash{'notify'} = $notify;
17259:     }
17260:     my %confighash = (
17261:                         privacy => \%privacyhash,
17262:                      );
17263:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
17264:     if ($putresult eq 'ok') {
17265:         if (keys(%changes) > 0) {
17266:             $resulttext = &mt('Changes made: ').'<ul>';
17267:             foreach my $key ('approval','notify','othdom','priv','unpriv') {
17268:                 if ($changes{$key}) {
17269:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
17270:                     if ($key eq 'approval') {
17271:                         if (keys(%{$privacyhash{$key}{instdom}})) {
17272:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
17273:                             foreach my $item (@items) {
17274:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
17275:                             }
17276:                             $resulttext .= '</ul></li>';
17277:                         }
17278:                         if (keys(%{$privacyhash{$key}{extdom}})) {
17279:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
17280:                             foreach my $item (@items) {
17281:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
17282:                             }
17283:                             $resulttext .= '</ul></li>';
17284:                         }
17285:                     } elsif ($key eq 'notify') {
17286:                         if ($privacyhash{$key}) {
17287:                             foreach my $dc (split(/,/,$privacyhash{$key})) {
17288:                                 my ($dcname,$dcdom) = split(/:/,$dc);
17289:                                 $resulttext .= '<li>'.&Apache::loncommon::plainname($dcname,$dcdom).'</li>';
17290:                             }
17291:                         } else {
17292:                             $resulttext .= '<li>'.&mt('No DCs to notify').'</li>';
17293:                         }
17294:                     } elsif ($key eq 'othdom') {
17295:                         my @statuses;
17296:                         if (ref($types) eq 'ARRAY') {
17297:                             @statuses = @{$types};
17298:                         }
17299:                         if (ref($privacyhash{$key}) eq 'HASH') {
17300:                             foreach my $status (@statuses,'default') {
17301:                                 if ($status eq 'default') {
17302:                                     $resulttext .= '<li>'.$othertitle.': ';
17303:                                 } elsif (ref($usertypes) eq 'HASH') {
17304:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
17305:                                 } else {
17306:                                     next;
17307:                                 }
17308:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
17309:                                     if (keys(%{$privacyhash{$key}{$status}})) {
17310:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
17311:                                     } else {
17312:                                         $resulttext .= &mt('none');
17313:                                     }
17314:                                 }
17315:                                 $resulttext .= '</li>';
17316:                             }
17317:                         }
17318:                     } else {
17319:                         foreach my $item (@items) {
17320:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
17321:                                 $resulttext .= '<li>'.$names{$item}.': ';
17322:                                 if (keys(%{$privacyhash{$key}{$item}})) {
17323:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
17324:                                 } else {
17325:                                     $resulttext .= &mt('none');
17326:                                 }
17327:                                 $resulttext .= '</li>';
17328:                             }
17329:                         }
17330:                     }
17331:                     $resulttext .= '</ul></li>';
17332:                 }
17333:             }
17334:             $resulttext .= '</ul>';
17335:             if ($changes{'approval'}) {
17336:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
17337:                 delete($domdefaults{'userapprovals'});
17338:                 if (ref($privacyhash{'approval'}) eq 'HASH') {
17339:                     foreach my $domtype ('instdom','extdom') {
17340:                         if (ref($privacyhash{'approval'}{$domtype}) eq 'HASH') {
17341:                             foreach my $roletype ('domain','author','course','community') {
17342:                                 if ($privacyhash{'approval'}{$domtype}{$roletype} eq 'user') {
17343:                                     $domdefaults{'userapprovals'} = 1;
17344:                                     last;
17345:                                 }
17346:                             }
17347:                         }
17348:                         last if ($domdefaults{'userapprovals'});               
17349:                     }
17350:                 }
17351:                 my $cachetime = 24*60*60;
17352:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17353:                 if (ref($lastactref) eq 'HASH') {
17354:                     $lastactref->{'domdefaults'} = 1;
17355:                 }
17356:             }
17357:         } else {
17358:             $resulttext = &mt('No changes made to user information settings');
17359:         }
17360:     } else {
17361:         $resulttext = '<span class="LC_error">'.
17362:             &mt('An error occurred: [_1]',$putresult).'</span>';
17363:     }
17364:     return $resulttext;
17365: }
17366: 
17367: sub modify_passwords {
17368:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
17369:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
17370:         $updatedefaults,$updateconf);
17371:     my $customfn = 'resetpw.html';
17372:     if (ref($domconfig{'passwords'}) eq 'HASH') {
17373:         %current = %{$domconfig{'passwords'}};
17374:     }
17375:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17376:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17377:     if (ref($types) eq 'ARRAY') {
17378:         @oktypes = @{$types};
17379:     }
17380:     push(@oktypes,'default');
17381: 
17382:     my %titles = &Apache::lonlocal::texthash (
17383:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
17384:         intauth_check  => 'Check bcrypt cost if authenticated',
17385:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
17386:         permanent      => 'Permanent e-mail address',
17387:         critical       => 'Critical notification address',
17388:         notify         => 'Notification address',
17389:         min            => 'Minimum password length',
17390:         max            => 'Maximum password length',
17391:         chars          => 'Required characters',
17392:         expire         => 'Password expiration (days)',
17393:         numsaved       => 'Number of previous passwords to save',
17394:         reset          => 'Resetting Forgotten Password',
17395:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
17396:         rules          => 'Rules for LON-CAPA Passwords',
17397:         crsownerchg    => 'Course Owner Changing Student Passwords',
17398:         username       => 'Username',
17399:         email          => 'E-mail address',
17400:     );
17401: 
17402: #
17403: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
17404: #
17405:     my (%curr_defaults,%save_defaults);
17406:     if (ref($domconfig{'defaults'}) eq 'HASH') {
17407:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
17408:             if ($key =~ /^intauth_(cost|check|switch)$/) {
17409:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
17410:             } else {
17411:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
17412:             }
17413:         }
17414:     }
17415:     my %staticdefaults = (
17416:         'resetlink'      => 2,
17417:         'resetcase'      => \@oktypes,
17418:         'resetprelink'   => 'both',
17419:         'resetemail'     => ['critical','notify','permanent'],
17420:         'intauth_cost'   => 10,
17421:         'intauth_check'  => 0,
17422:         'intauth_switch' => 0,
17423:     );
17424:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
17425:     foreach my $type (@oktypes) {
17426:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
17427:     }
17428:     my $linklife = $env{'form.passwords_link'};
17429:     $linklife =~ s/^\s+|\s+$//g;
17430:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
17431:         $newvalues{'resetlink'} = $linklife;
17432:         if ($current{'resetlink'}) {
17433:             if ($current{'resetlink'} ne $linklife) {
17434:                 $changes{'reset'} = 1;
17435:             }
17436:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17437:             if ($staticdefaults{'resetlink'} ne $linklife) {
17438:                 $changes{'reset'} = 1;
17439:             }
17440:         }
17441:     } elsif ($current{'resetlink'}) {
17442:         $changes{'reset'} = 1;
17443:     }
17444:     my @casesens;
17445:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
17446:     foreach my $case (sort(@posscase)) {
17447:         if (grep(/^\Q$case\E$/,@oktypes)) {
17448:             push(@casesens,$case);
17449:         }
17450:     }
17451:     $newvalues{'resetcase'} = \@casesens;
17452:     if (ref($current{'resetcase'}) eq 'ARRAY') {
17453:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
17454:         if (@diffs > 0) {
17455:             $changes{'reset'} = 1;
17456:         }
17457:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17458:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
17459:         if (@diffs > 0) {
17460:             $changes{'reset'} = 1;
17461:         }
17462:     }
17463:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
17464:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
17465:         if (exists($current{'resetprelink'})) {
17466:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
17467:                 $changes{'reset'} = 1;
17468:             }
17469:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17470:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
17471:                 $changes{'reset'} = 1;
17472:             }
17473:         }
17474:     } elsif ($current{'resetprelink'}) {
17475:         $changes{'reset'} = 1;
17476:     }
17477:     foreach my $type (@oktypes) {
17478:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
17479:         my @postlink;
17480:         foreach my $item (sort(@possplink)) {
17481:             if ($item =~ /^(email|username)$/) {
17482:                 push(@postlink,$item);
17483:             }
17484:         }
17485:         $newvalues{'resetpostlink'}{$type} = \@postlink;
17486:         unless ($changes{'reset'}) {
17487:             if (ref($current{'resetpostlink'}) eq 'HASH') {
17488:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
17489:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
17490:                     if (@diffs > 0) {
17491:                         $changes{'reset'} = 1;
17492:                     }
17493:                 } else {
17494:                     $changes{'reset'} = 1;
17495:                 }
17496:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17497:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
17498:                 if (@diffs > 0) {
17499:                     $changes{'reset'} = 1;
17500:                 }
17501:             }
17502:         }
17503:     }
17504:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
17505:     my @resetemail;
17506:     foreach my $item (sort(@possemailsrc)) {
17507:         if ($item =~ /^(permanent|critical|notify)$/) {
17508:             push(@resetemail,$item);
17509:         }
17510:     }
17511:     $newvalues{'resetemail'} = \@resetemail;
17512:     unless ($changes{'reset'}) {
17513:         if (ref($current{'resetemail'}) eq 'ARRAY') {
17514:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
17515:             if (@diffs > 0) {
17516:                 $changes{'reset'} = 1;
17517:             }
17518:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17519:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
17520:             if (@diffs > 0) {
17521:                 $changes{'reset'} = 1;
17522:             }
17523:         }
17524:     }
17525:     if ($env{'form.passwords_stdtext'} == 0) {
17526:         $newvalues{'resetremove'} = 1;
17527:         unless ($current{'resetremove'}) {
17528:             $changes{'reset'} = 1;
17529:         }
17530:     } elsif ($current{'resetremove'}) {
17531:         $changes{'reset'} = 1;
17532:     }
17533:     if ($env{'form.passwords_customfile.filename'} ne '') {
17534:         my $servadm = $r->dir_config('lonAdmEMail');
17535:         my ($configuserok,$author_ok,$switchserver) =
17536:             &config_check($dom,$confname,$servadm);
17537:         my $error;
17538:         if ($configuserok eq 'ok') {
17539:             if ($switchserver) {
17540:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
17541:             } else {
17542:                 if ($author_ok eq 'ok') {
17543:                     my $modified = [];
17544:                     my ($result,$customurl) =
17545:                         &Apache::lonconfigsettings::publishlogo($r,'upload','passwords_customfile',$dom,
17546:                                                                 $confname,'customtext/resetpw','','',$customfn,
17547:                                                                 $modified);
17548:                     if ($result eq 'ok') {
17549:                         $newvalues{'resetcustom'} = $customurl;
17550:                         $changes{'reset'} = 1;
17551:                         &update_modify_urls($r,$modified);
17552:                     } else {
17553:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
17554:                     }
17555:                 } else {
17556:                     $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);
17557:                 }
17558:             }
17559:         } else {
17560:             $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);
17561:         }
17562:         if ($error) {
17563:             &Apache::lonnet::logthis($error);
17564:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
17565:         }
17566:     } elsif ($current{'resetcustom'}) {
17567:         if ($env{'form.passwords_custom_del'}) {
17568:             $changes{'reset'} = 1;
17569:         } else {
17570:             $newvalues{'resetcustom'} = $current{'resetcustom'};
17571:         }
17572:     }
17573:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
17574:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
17575:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
17576:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
17577:             $changes{'intauth'} = 1;
17578:         }
17579:     } else {
17580:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
17581:     }
17582:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
17583:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
17584:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
17585:             $changes{'intauth'} = 1;
17586:         }
17587:     } else {
17588:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
17589:     }
17590:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
17591:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
17592:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
17593:             $changes{'intauth'} = 1;
17594:         }
17595:     } else {
17596:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
17597:     }
17598:     foreach my $item ('cost','check','switch') {
17599:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
17600:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
17601:             $updatedefaults = 1;
17602:         }
17603:     }
17604:     &password_rule_changes('passwords',\%newvalues,\%current,\%changes);
17605:     my %crsownerchg = (
17606:                         by => [],
17607:                         for => [],
17608:                       );
17609:     foreach my $item ('by','for') {
17610:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
17611:         foreach my $type (sort(@posstypes)) {
17612:             if (grep(/^\Q$type\E$/,@oktypes)) {
17613:                 push(@{$crsownerchg{$item}},$type);
17614:             }
17615:         }
17616:     }
17617:     $newvalues{'crsownerchg'} = \%crsownerchg;
17618:     if (ref($current{'crsownerchg'}) eq 'HASH') {
17619:         foreach my $item ('by','for') {
17620:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
17621:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
17622:                 if (@diffs > 0) {
17623:                     $changes{'crsownerchg'} = 1;
17624:                     last;
17625:                 }
17626:             }
17627:         }
17628:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
17629:         foreach my $item ('by','for') {
17630:             if (@{$crsownerchg{$item}} > 0) {
17631:                 $changes{'crsownerchg'} = 1;
17632:                 last;
17633:             }
17634:         }
17635:     }
17636: 
17637:     my %confighash = (
17638:                         defaults  => \%save_defaults,
17639:                         passwords => \%newvalues,
17640:                      );
17641:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
17642: 
17643:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
17644:     if ($putresult eq 'ok') {
17645:         if (keys(%changes) > 0) {
17646:             $resulttext = &mt('Changes made: ').'<ul>';
17647:             foreach my $key ('reset','intauth','rules','crsownerchg') {
17648:                 if ($changes{$key}) {
17649:                     unless ($key eq 'intauth') {
17650:                         $updateconf = 1;
17651:                     }
17652:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
17653:                     if ($key eq 'reset') {
17654:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
17655:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
17656:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
17657:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
17658:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
17659:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
17660:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
17661:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
17662:                             }
17663:                         } else {
17664:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
17665:                         }
17666:                         if ($confighash{'passwords'}{'resetlink'}) {
17667:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
17668:                         } else {
17669:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
17670:                                                   &mt('Will default to 2 hours').'</li>';
17671:                         }
17672:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
17673:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
17674:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
17675:                             } else {
17676:                                 my $casesens;
17677:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
17678:                                     if ($type eq 'default') {
17679:                                         $casesens .= $othertitle.', ';
17680:                                     } elsif ($usertypes->{$type} ne '') {
17681:                                         $casesens .= $usertypes->{$type}.', ';
17682:                                     }
17683:                                 }
17684:                                 $casesens =~ s/\Q, \E$//;
17685:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
17686:                             }
17687:                         } else {
17688:                             $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>';
17689:                         }
17690:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
17691:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
17692:                         } else {
17693:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
17694:                         }
17695:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
17696:                             my $output;
17697:                             if (ref($types) eq 'ARRAY') {
17698:                                 foreach my $type (@{$types}) {
17699:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
17700:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
17701:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
17702:                                         } else {
17703:                                             $output .= $usertypes->{$type}.' -- '.
17704:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
17705:                                         }
17706:                                     }
17707:                                 }
17708:                             }
17709:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
17710:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
17711:                                     $output .= $othertitle.' -- '.&mt('none');
17712:                                 } else {
17713:                                     $output .= $othertitle.' -- '.
17714:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
17715:                                 }
17716:                             }
17717:                             if ($output) {
17718:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
17719:                             } else {
17720:                                 $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>';
17721:                             }
17722:                         } else {
17723:                             $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>';
17724:                         }
17725:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
17726:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
17727:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
17728:                             } else {
17729:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
17730:                             }
17731:                         } else {
17732:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
17733:                         }
17734:                         if ($confighash{'passwords'}{'resetremove'}) {
17735:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
17736:                         } else {
17737:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
17738:                         }
17739:                         if ($confighash{'passwords'}{'resetcustom'}) {
17740:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
17741:                                                                             &mt('custom text'),600,500,undef,undef,
17742:                                                                             undef,undef,'background-color:#ffffff');
17743:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
17744:                         } else {
17745:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
17746:                         }
17747:                     } elsif ($key eq 'intauth') {
17748:                         foreach my $item ('cost','switch','check') {
17749:                             my $value = $save_defaults{$key.'_'.$item};
17750:                             if ($item eq 'switch') {
17751:                                 my %optiondesc = &Apache::lonlocal::texthash (
17752:                                                      0 => 'No',
17753:                                                      1 => 'Yes',
17754:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
17755:                                                  );
17756:                                 if ($value =~ /^(0|1|2)$/) {
17757:                                     $value = $optiondesc{$value};
17758:                                 } else {
17759:                                     $value = &mt('none -- defaults to No');
17760:                                 }
17761:                             } elsif ($item eq 'check') {
17762:                                 my %optiondesc = &Apache::lonlocal::texthash (
17763:                                                      0 => 'No',
17764:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
17765:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
17766:                                                  );
17767:                                 if ($value =~ /^(0|1|2)$/) {
17768:                                     $value = $optiondesc{$value};
17769:                                 } else {
17770:                                     $value = &mt('none -- defaults to No');
17771:                                 }
17772:                             }
17773:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
17774:                         }
17775:                     } elsif ($key eq 'rules') {
17776:                         foreach my $rule ('min','max','expire','numsaved') {
17777:                             if ($confighash{'passwords'}{$rule} eq '') {
17778:                                 if ($rule eq 'min') {
17779:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
17780:                                                    ' '.&mt('Default of [_1] will be used',
17781:                                                            $Apache::lonnet::passwdmin).'</li>';
17782:                                 } else {
17783:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
17784:                                 }
17785:                             } else {
17786:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
17787:                             }
17788:                         }
17789:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
17790:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
17791:                                 my %rulenames = &Apache::lonlocal::texthash(
17792:                                                      uc => 'At least one upper case letter',
17793:                                                      lc => 'At least one lower case letter',
17794:                                                      num => 'At least one number',
17795:                                                      spec => 'At least one non-alphanumeric',
17796:                                                    );
17797:                                 my $needed = '<ul><li>'.
17798:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
17799:                                              '</li></ul>';
17800:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
17801:                             } else {
17802:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
17803:                             }
17804:                         } else {
17805:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
17806:                         }
17807:                     } elsif ($key eq 'crsownerchg') {
17808:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
17809:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
17810:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
17811:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
17812:                             } else {
17813:                                 my %crsownerstr;
17814:                                 foreach my $item ('by','for') {
17815:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
17816:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
17817:                                             if ($type eq 'default') {
17818:                                                 $crsownerstr{$item} .= $othertitle.', ';
17819:                                             } elsif ($usertypes->{$type} ne '') {
17820:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
17821:                                             }
17822:                                         }
17823:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
17824:                                     }
17825:                                 }
17826:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
17827:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
17828:                             }
17829:                         } else {
17830:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
17831:                         }
17832:                     }
17833:                     $resulttext .= '</ul></li>';
17834:                 }
17835:             }
17836:             $resulttext .= '</ul>';
17837:         } else {
17838:             $resulttext = &mt('No changes made to password settings');
17839:         }
17840:         my $cachetime = 24*60*60;
17841:         if ($updatedefaults) {
17842:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17843:             if (ref($lastactref) eq 'HASH') {
17844:                 $lastactref->{'domdefaults'} = 1;
17845:             }
17846:         }
17847:         if ($updateconf) {
17848:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
17849:             if (ref($lastactref) eq 'HASH') {
17850:                 $lastactref->{'passwdconf'} = 1;
17851:             }
17852:         }
17853:     } else {
17854:         $resulttext = '<span class="LC_error">'.
17855:             &mt('An error occurred: [_1]',$putresult).'</span>';
17856:     }
17857:     if ($errors) {
17858:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
17859:                        $errors.'</ul></p>';
17860:     }
17861:     return $resulttext;
17862: }
17863: 
17864: sub password_rule_changes {
17865:     my ($prefix,$newvalues,$current,$changes) = @_;
17866:     return unless ((ref($newvalues) eq 'HASH') &&
17867:                    (ref($current) eq 'HASH') &&
17868:                    (ref($changes) eq 'HASH'));
17869:     my (@rules,%staticdefaults);
17870:     if ($prefix eq 'passwords') {
17871:         @rules = ('min','max','expire','numsaved');
17872:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
17873:         @rules = ('min','max');
17874:     }
17875:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
17876:     foreach my $rule (@rules) {
17877:         $env{'form.'.$prefix.'_'.$rule} =~ s/^\s+|\s+$//g;
17878:         my $ruleok;
17879:         if ($rule eq 'expire') {
17880:             if (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+(|\.\d*)$/) &&
17881:                 ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
17882:                 $ruleok = 1;
17883:             }
17884:         } elsif ($rule eq 'min') {
17885:             if ($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) {
17886:                 if ($env{'form.'.$prefix.'_'.$rule} >= $staticdefaults{$rule}) {
17887:                     $ruleok = 1;
17888:                 }
17889:             }
17890:         } elsif (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) &&
17891:                  ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
17892:             $ruleok = 1;
17893:         }
17894:         if ($ruleok) {
17895:             $newvalues->{$rule} = $env{'form.'.$prefix.'_'.$rule};
17896:             if (exists($current->{$rule})) {
17897:                 if ($newvalues->{$rule} ne $current->{$rule}) {
17898:                     $changes->{'rules'} = 1;
17899:                 }
17900:             } elsif ($rule eq 'min') {
17901:                 if ($staticdefaults{$rule} ne $newvalues->{$rule}) {
17902:                     $changes->{'rules'} = 1;
17903:                 }
17904:             } else {
17905:                 $changes->{'rules'} = 1;
17906:             }
17907:         } elsif (exists($current->{$rule})) {
17908:             $changes->{'rules'} = 1;
17909:         }
17910:     }
17911:     my @posschars = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_chars');
17912:     my @chars;
17913:     foreach my $item (sort(@posschars)) {
17914:         if ($item =~ /^(uc|lc|num|spec)$/) {
17915:             push(@chars,$item);
17916:         }
17917:     }
17918:     $newvalues->{'chars'} = \@chars;
17919:     unless ($changes->{'rules'}) {
17920:         if (ref($current->{'chars'}) eq 'ARRAY') {
17921:             my @diffs = &Apache::loncommon::compare_arrays($current->{'chars'},\@chars);
17922:             if (@diffs > 0) {
17923:                 $changes->{'rules'} = 1;
17924:             }
17925:         } else {
17926:             if (@chars > 0) {
17927:                 $changes->{'rules'} = 1;
17928:             }
17929:         }
17930:     }
17931:     return;
17932: }
17933: 
17934: sub modify_usercreation {
17935:     my ($dom,%domconfig) = @_;
17936:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
17937:     my $warningmsg;
17938:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
17939:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
17940:             if ($key eq 'cancreate') {
17941:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
17942:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
17943:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
17944:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17945:                         } else {
17946:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17947:                         }
17948:                     }
17949:                 }
17950:             } elsif ($key eq 'email_rule') {
17951:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
17952:             } else {
17953:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
17954:             }
17955:         }
17956:     }
17957:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
17958:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
17959:     my @contexts = ('author','course','requestcrs');
17960:     foreach my $item(@contexts) {
17961:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
17962:     }
17963:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
17964:         foreach my $item (@contexts) {
17965:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
17966:                 push(@{$changes{'cancreate'}},$item);
17967:             }
17968:         }
17969:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
17970:         foreach my $item (@contexts) {
17971:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
17972:                 if ($cancreate{$item} ne 'any') {
17973:                     push(@{$changes{'cancreate'}},$item);
17974:                 }
17975:             } else {
17976:                 if ($cancreate{$item} ne 'none') {
17977:                     push(@{$changes{'cancreate'}},$item);
17978:                 }
17979:             }
17980:         }
17981:     } else {
17982:         foreach my $item (@contexts)  {
17983:             push(@{$changes{'cancreate'}},$item);
17984:         }
17985:     }
17986: 
17987:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
17988:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
17989:             if (!grep(/^\Q$type\E$/,@username_rule)) {
17990:                 push(@{$changes{'username_rule'}},$type);
17991:             }
17992:         }
17993:         foreach my $type (@username_rule) {
17994:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
17995:                 push(@{$changes{'username_rule'}},$type);
17996:             }
17997:         }
17998:     } else {
17999:         push(@{$changes{'username_rule'}},@username_rule);
18000:     }
18001: 
18002:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
18003:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
18004:             if (!grep(/^\Q$type\E$/,@id_rule)) {
18005:                 push(@{$changes{'id_rule'}},$type);
18006:             }
18007:         }
18008:         foreach my $type (@id_rule) {
18009:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
18010:                 push(@{$changes{'id_rule'}},$type);
18011:             }
18012:         }
18013:     } else {
18014:         push(@{$changes{'id_rule'}},@id_rule);
18015:     }
18016: 
18017:     my @authen_contexts = ('author','course','domain');
18018:     my @authtypes = ('int','krb4','krb5','loc','lti');
18019:     my %authhash;
18020:     foreach my $item (@authen_contexts) {
18021:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
18022:         foreach my $auth (@authtypes) {
18023:             if (grep(/^\Q$auth\E$/,@authallowed)) {
18024:                 $authhash{$item}{$auth} = 1;
18025:             } else {
18026:                 $authhash{$item}{$auth} = 0;
18027:             }
18028:         }
18029:     }
18030:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
18031:         foreach my $item (@authen_contexts) {
18032:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
18033:                 foreach my $auth (@authtypes) {
18034:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
18035:                         push(@{$changes{'authtypes'}},$item);
18036:                         last;
18037:                     }
18038:                 }
18039:             }
18040:         }
18041:     } else {
18042:         foreach my $item (@authen_contexts) {
18043:             push(@{$changes{'authtypes'}},$item);
18044:         }
18045:     }
18046: 
18047:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
18048:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
18049:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
18050:     $save_usercreate{'id_rule'} = \@id_rule;
18051:     $save_usercreate{'username_rule'} = \@username_rule,
18052:     $save_usercreate{'authtypes'} = \%authhash;
18053: 
18054:     my %usercreation_hash =  (
18055:         usercreation     => \%save_usercreate,
18056:     );
18057: 
18058:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
18059:                                              $dom);
18060: 
18061:     if ($putresult eq 'ok') {
18062:         if (keys(%changes) > 0) {
18063:             $resulttext = &mt('Changes made:').'<ul>';
18064:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
18065:                 my %lt = &usercreation_types();
18066:                 foreach my $type (@{$changes{'cancreate'}}) {
18067:                     my $chgtext = $lt{$type}.', ';
18068:                     if ($cancreate{$type} eq 'none') {
18069:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
18070:                     } elsif ($cancreate{$type} eq 'any') {
18071:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
18072:                     } elsif ($cancreate{$type} eq 'official') {
18073:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
18074:                     } elsif ($cancreate{$type} eq 'unofficial') {
18075:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
18076:                     }
18077:                     $resulttext .= '<li>'.$chgtext.'</li>';
18078:                 }
18079:             }
18080:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
18081:                 my ($rules,$ruleorder) = 
18082:                     &Apache::lonnet::inst_userrules($dom,'username');
18083:                 my $chgtext = '<ul>';
18084:                 foreach my $type (@username_rule) {
18085:                     if (ref($rules->{$type}) eq 'HASH') {
18086:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
18087:                     }
18088:                 }
18089:                 $chgtext .= '</ul>';
18090:                 if (@username_rule > 0) {
18091:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
18092:                 } else {
18093:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
18094:                 }
18095:             }
18096:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
18097:                 my ($idrules,$idruleorder) = 
18098:                     &Apache::lonnet::inst_userrules($dom,'id');
18099:                 my $chgtext = '<ul>';
18100:                 foreach my $type (@id_rule) {
18101:                     if (ref($idrules->{$type}) eq 'HASH') {
18102:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
18103:                     }
18104:                 }
18105:                 $chgtext .= '</ul>';
18106:                 if (@id_rule > 0) {
18107:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
18108:                 } else {
18109:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
18110:                 }
18111:             }
18112:             my %authname = &authtype_names();
18113:             my %context_title = &context_names();
18114:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
18115:                 my $chgtext = '<ul>';
18116:                 foreach my $type (@{$changes{'authtypes'}}) {
18117:                     my @allowed;
18118:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
18119:                     foreach my $auth (@authtypes) {
18120:                         if ($authhash{$type}{$auth}) {
18121:                             push(@allowed,$authname{$auth});
18122:                         }
18123:                     }
18124:                     if (@allowed > 0) {
18125:                         $chgtext .= join(', ',@allowed).'</li>';
18126:                     } else {
18127:                         $chgtext .= &mt('none').'</li>';
18128:                     }
18129:                 }
18130:                 $chgtext .= '</ul>';
18131:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
18132:                 $resulttext .= '</li>';
18133:             }
18134:             $resulttext .= '</ul>';
18135:         } else {
18136:             $resulttext = &mt('No changes made to user creation settings');
18137:         }
18138:     } else {
18139:         $resulttext = '<span class="LC_error">'.
18140:             &mt('An error occurred: [_1]',$putresult).'</span>';
18141:     }
18142:     if ($warningmsg ne '') {
18143:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
18144:     }
18145:     return $resulttext;
18146: }
18147: 
18148: sub modify_selfcreation {
18149:     my ($dom,$lastactref,%domconfig) = @_;
18150:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
18151:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
18152:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18153:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
18154:     if (ref($typesref) eq 'ARRAY') {
18155:         @types = @{$typesref};
18156:     }
18157:     if (ref($usertypesref) eq 'HASH') {
18158:         %usertypes = %{$usertypesref};
18159:     }
18160:     $usertypes{'default'} = $othertitle;
18161: #
18162: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
18163: #
18164:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
18165:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
18166:             if ($key eq 'cancreate') {
18167:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
18168:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
18169:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
18170:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
18171:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
18172:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
18173:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
18174:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
18175:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18176:                         } else {
18177:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18178:                         }
18179:                     }
18180:                 }
18181:             } elsif ($key eq 'email_rule') {
18182:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
18183:             } else {
18184:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
18185:             }
18186:         }
18187:     }
18188: #
18189: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
18190: #
18191:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
18192:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
18193:             if ($key eq 'selfcreate') {
18194:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
18195:             } else {
18196:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
18197:             }
18198:         }
18199:     }
18200: #
18201: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
18202: #
18203:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
18204:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
18205:             if ($key eq 'inststatusguest') {
18206:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
18207:             } else {
18208:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
18209:             }
18210:         }
18211:     }
18212: 
18213:     my @contexts = ('selfcreate');
18214:     @{$cancreate{'selfcreate'}} = ();
18215:     %{$cancreate{'emailusername'}} = ();
18216:     if (@types) {
18217:         @{$cancreate{'statustocreate'}} = ();
18218:     }
18219:     %{$cancreate{'selfcreateprocessing'}} = ();
18220:     %{$cancreate{'shibenv'}} = ();
18221:     %{$cancreate{'emailverified'}} = ();
18222:     %{$cancreate{'emailoptions'}} = ();
18223:     %{$cancreate{'emaildomain'}} = ();
18224:     my %selfcreatetypes = (
18225:                              sso   => 'users authenticated by institutional single sign on',
18226:                              login => 'users authenticated by institutional log-in',
18227:                              email => 'users verified by e-mail',
18228:                           );
18229: #
18230: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
18231: # is permitted.
18232: #
18233:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
18234: 
18235:     my (@statuses,%email_rule);
18236:     foreach my $item ('login','sso','email') {
18237:         if ($item eq 'email') {
18238:             if ($env{'form.cancreate_email'}) {
18239:                 if (@types) {
18240:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
18241:                     foreach my $status (@poss_statuses) {
18242:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
18243:                             push(@statuses,$status);
18244:                         }
18245:                     }
18246:                     $save_inststatus{'inststatusguest'} = \@statuses;
18247:                 } else {
18248:                     push(@statuses,'default');
18249:                 }
18250:                 if (@statuses) {
18251:                     my %curr_rule;
18252:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
18253:                         foreach my $type (@statuses) {
18254:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
18255:                         }
18256:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
18257:                         foreach my $type (@statuses) {
18258:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
18259:                         }
18260:                     }
18261:                     push(@{$cancreate{'selfcreate'}},'email');
18262:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
18263:                     my %curremaildom;
18264:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
18265:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
18266:                     }
18267:                     foreach my $type (@statuses) {
18268:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
18269:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
18270:                         }
18271:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
18272:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
18273:                         }
18274:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
18275: #
18276: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
18277: #
18278:                             my $chosen = $1;
18279:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
18280:                                 my $emaildom;
18281:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
18282:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
18283:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
18284:                                     if (ref($curremaildom{$type}) eq 'HASH') {
18285:                                         if (exists($curremaildom{$type}{$chosen})) {
18286:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
18287:                                                 push(@{$changes{'cancreate'}},'emaildomain');
18288:                                             }
18289:                                         } elsif ($emaildom ne '') {
18290:                                             push(@{$changes{'cancreate'}},'emaildomain');
18291:                                         }
18292:                                     } elsif ($emaildom ne '') {
18293:                                         push(@{$changes{'cancreate'}},'emaildomain');
18294:                                     }
18295:                                 }
18296:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
18297:                             } elsif ($chosen eq 'custom') {
18298:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
18299:                                 $email_rule{$type} = [];
18300:                                 if (ref($emailrules) eq 'HASH') {
18301:                                     foreach my $rule (@possemail_rules) {
18302:                                         if (exists($emailrules->{$rule})) {
18303:                                             push(@{$email_rule{$type}},$rule);
18304:                                         }
18305:                                     }
18306:                                 }
18307:                                 if (@{$email_rule{$type}}) {
18308:                                     $cancreate{'emailoptions'}{$type} = 'custom';
18309:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
18310:                                         if (@{$curr_rule{$type}} > 0) {
18311:                                             foreach my $rule (@{$curr_rule{$type}}) {
18312:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
18313:                                                     push(@{$changes{'email_rule'}},$type);
18314:                                                 }
18315:                                             }
18316:                                         }
18317:                                         foreach my $type (@{$email_rule{$type}}) {
18318:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
18319:                                                 push(@{$changes{'email_rule'}},$type);
18320:                                             }
18321:                                         }
18322:                                     } else {
18323:                                         push(@{$changes{'email_rule'}},$type);
18324:                                     }
18325:                                 }
18326:                             } else {
18327:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
18328:                             }
18329:                         }
18330:                     }
18331:                     if (@types) {
18332:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18333:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
18334:                             if (@changed) {
18335:                                 push(@{$changes{'inststatus'}},'inststatusguest');
18336:                             }
18337:                         } else {
18338:                             push(@{$changes{'inststatus'}},'inststatusguest');
18339:                         }
18340:                     }
18341:                 } else {
18342:                     delete($env{'form.cancreate_email'});
18343:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18344:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
18345:                             push(@{$changes{'inststatus'}},'inststatusguest');
18346:                         }
18347:                     }
18348:                 }
18349:             } else {
18350:                 $save_inststatus{'inststatusguest'} = [];
18351:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18352:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
18353:                         push(@{$changes{'inststatus'}},'inststatusguest');
18354:                     }
18355:                 }
18356:             }
18357:         } else {
18358:             if ($env{'form.cancreate_'.$item}) {
18359:                 push(@{$cancreate{'selfcreate'}},$item);
18360:             }
18361:         }
18362:     }
18363:     my (%userinfo,%savecaptcha);
18364:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
18365: #
18366: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
18367: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
18368: #
18369: 
18370:     if ($env{'form.cancreate_email'}) {
18371:         push(@contexts,'emailusername');
18372:         if (@statuses) {
18373:             foreach my $type (@statuses) {
18374:                 if (ref($infofields) eq 'ARRAY') {
18375:                     foreach my $field (@{$infofields}) {
18376:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
18377:                             $cancreate{'emailusername'}{$type}{$field} = $1;
18378:                         }
18379:                     }
18380:                 }
18381:             }
18382:         }
18383: #
18384: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
18385: # queued requests for self-creation of account verified by e-mail.
18386: #
18387: 
18388:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
18389:         @approvalnotify = sort(@approvalnotify);
18390:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
18391:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18392:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
18393:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
18394:                     push(@{$changes{'cancreate'}},'notify');
18395:                 }
18396:             } else {
18397:                 if ($cancreate{'notify'}{'approval'}) {
18398:                     push(@{$changes{'cancreate'}},'notify');
18399:                 }
18400:             }
18401:         } elsif ($cancreate{'notify'}{'approval'}) {
18402:             push(@{$changes{'cancreate'}},'notify');
18403:         }
18404: 
18405:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
18406:     }
18407: #  
18408: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
18409: # institutional log-in.
18410: #
18411:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
18412:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
18413:                ($domdefaults{'auth_def'} eq 'localauth'))) {
18414:             $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.').' '.
18415:                           &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.');
18416:         }
18417:     }
18418:     my @fields = ('lastname','firstname','middlename','generation',
18419:                   'permanentemail','id');
18420:     my @shibfields = (@fields,'inststatus');
18421:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
18422: #
18423: # Where usernames may created for institutional log-in and/or institutional single sign on:
18424: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
18425: # may self-create accounts 
18426: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
18427: # which the user may supply, if institutional data is unavailable.
18428: #
18429:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
18430:         if (@types) {
18431:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
18432:             push(@contexts,'statustocreate');
18433:             foreach my $type (@types) {
18434:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
18435:                 foreach my $field (@fields) {
18436:                     if (grep(/^\Q$field\E$/,@modifiable)) {
18437:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
18438:                     } else {
18439:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
18440:                     }
18441:                 }
18442:             }
18443:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
18444:                 foreach my $type (@types) {
18445:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
18446:                         foreach my $field (@fields) {
18447:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
18448:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
18449:                                 push(@{$changes{'selfcreate'}},$type);
18450:                                 last;
18451:                             }
18452:                         }
18453:                     }
18454:                 }
18455:             } else {
18456:                 foreach my $type (@types) {
18457:                     push(@{$changes{'selfcreate'}},$type);
18458:                 }
18459:             }
18460:         }
18461:         foreach my $field (@shibfields) {
18462:             if ($env{'form.shibenv_'.$field} ne '') {
18463:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
18464:             }
18465:         }
18466:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18467:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
18468:                 foreach my $field (@shibfields) {
18469:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
18470:                         push(@{$changes{'cancreate'}},'shibenv');
18471:                     }
18472:                 }
18473:             } else {
18474:                 foreach my $field (@shibfields) {
18475:                     if ($env{'form.shibenv_'.$field}) {
18476:                         push(@{$changes{'cancreate'}},'shibenv');
18477:                         last;
18478:                     }
18479:                 }
18480:             }
18481:         }
18482:     }
18483:     foreach my $item (@contexts) {
18484:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
18485:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
18486:                 if (ref($cancreate{$item}) eq 'ARRAY') {
18487:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
18488:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18489:                             push(@{$changes{'cancreate'}},$item);
18490:                         }
18491:                     }
18492:                 }
18493:             }
18494:             if (ref($cancreate{$item}) eq 'ARRAY') {
18495:                 foreach my $type (@{$cancreate{$item}}) {
18496:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
18497:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18498:                             push(@{$changes{'cancreate'}},$item);
18499:                         }
18500:                     }
18501:                 }
18502:             }
18503:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
18504:             if (ref($cancreate{$item}) eq 'HASH') {
18505:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
18506:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
18507:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
18508:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
18509:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18510:                                     push(@{$changes{'cancreate'}},$item);
18511:                                 }
18512:                             }
18513:                         }
18514:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
18515:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
18516:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18517:                                 push(@{$changes{'cancreate'}},$item);
18518:                             }
18519:                         }
18520:                     }
18521:                 }
18522:                 foreach my $type (keys(%{$cancreate{$item}})) {
18523:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
18524:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
18525:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
18526:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
18527:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18528:                                         push(@{$changes{'cancreate'}},$item);
18529:                                     }
18530:                                 }
18531:                             } else {
18532:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18533:                                     push(@{$changes{'cancreate'}},$item);
18534:                                 }
18535:                             }
18536:                         }
18537:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
18538:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
18539:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18540:                                 push(@{$changes{'cancreate'}},$item);
18541:                             }
18542:                         }
18543:                     }
18544:                 }
18545:             }
18546:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
18547:             if (ref($cancreate{$item}) eq 'ARRAY') {
18548:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
18549:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18550:                         push(@{$changes{'cancreate'}},$item);
18551:                     }
18552:                 }
18553:             }
18554:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
18555:             if (ref($cancreate{$item}) eq 'HASH') {
18556:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18557:                     push(@{$changes{'cancreate'}},$item);
18558:                 }
18559:             }
18560:         } elsif ($item eq 'emailusername') {
18561:             if (ref($cancreate{$item}) eq 'HASH') {
18562:                 foreach my $type (keys(%{$cancreate{$item}})) {
18563:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
18564:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
18565:                             if ($cancreate{$item}{$type}{$field}) {
18566:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18567:                                     push(@{$changes{'cancreate'}},$item);
18568:                                 }
18569:                                 last;
18570:                             }
18571:                         }
18572:                     }
18573:                 }
18574:             }
18575:         }
18576:     }
18577: #
18578: # Populate %save_usercreate hash with updates to self-creation configuration.
18579: #
18580:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
18581:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
18582:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
18583:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
18584:     if (ref($cancreate{'notify'}) eq 'HASH') {
18585:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
18586:     }
18587:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
18588:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
18589:     }
18590:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
18591:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
18592:     }
18593:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
18594:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
18595:     }
18596:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
18597:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
18598:     }
18599:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
18600:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
18601:     }
18602:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
18603:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
18604:     }
18605:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
18606:     $save_usercreate{'email_rule'} = \%email_rule;
18607: 
18608:     my %userconfig_hash = (
18609:             usercreation     => \%save_usercreate,
18610:             usermodification => \%save_usermodify,
18611:             inststatus       => \%save_inststatus,
18612:     );
18613: 
18614:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
18615:                                              $dom);
18616: #
18617: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
18618: #
18619:     if ($putresult eq 'ok') {
18620:         if (keys(%changes) > 0) {
18621:             $resulttext = &mt('Changes made:').'<ul>';
18622:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
18623:                 my %lt = &selfcreation_types();
18624:                 foreach my $type (@{$changes{'cancreate'}}) {
18625:                     my $chgtext = '';
18626:                     if ($type eq 'selfcreate') {
18627:                         if (@{$cancreate{$type}} == 0) {
18628:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
18629:                         } else {
18630:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
18631:                                         '<ul>';
18632:                             foreach my $case (@{$cancreate{$type}}) {
18633:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
18634:                             }
18635:                             $chgtext .= '</ul>';
18636:                             if (ref($cancreate{$type}) eq 'ARRAY') {
18637:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
18638:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
18639:                                         if (@{$cancreate{'statustocreate'}} == 0) {
18640:                                             $chgtext .= '<span class="LC_warning">'.
18641:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
18642:                                                         '</span><br />';
18643:                                         }
18644:                                     }
18645:                                 }
18646:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
18647:                                     if (!@statuses) {
18648:                                         $chgtext .= '<span class="LC_warning">'.
18649:                                                     &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.").
18650:                                                     '</span><br />';
18651: 
18652:                                     }
18653:                                 }
18654:                             }
18655:                         }
18656:                     } elsif ($type eq 'shibenv') {
18657:                         if (keys(%{$cancreate{$type}}) == 0) {
18658:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
18659:                         } else {
18660:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
18661:                                         '<ul>';
18662:                             foreach my $field (@shibfields) {
18663:                                 next if ($cancreate{$type}{$field} eq '');
18664:                                 if ($field eq 'inststatus') {
18665:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
18666:                                 } else {
18667:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
18668:                                 }
18669:                             }
18670:                             $chgtext .= '</ul>';
18671:                         }
18672:                     } elsif ($type eq 'statustocreate') {
18673:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
18674:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
18675:                             if (@{$cancreate{'selfcreate'}} > 0) {
18676:                                 if (@{$cancreate{'statustocreate'}} == 0) {
18677:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
18678:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
18679:                                         $chgtext .= '<br />'.
18680:                                                     '<span class="LC_warning">'.
18681:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
18682:                                                     '</span>';
18683:                                     }
18684:                                 } elsif (keys(%usertypes) > 0) {
18685:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
18686:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
18687:                                     } else {
18688:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
18689:                                     }
18690:                                     $chgtext .= '<ul>';
18691:                                     foreach my $case (@{$cancreate{$type}}) {
18692:                                         if ($case eq 'default') {
18693:                                             $chgtext .= '<li>'.$othertitle.'</li>';
18694:                                         } else {
18695:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
18696:                                         }
18697:                                     }
18698:                                     $chgtext .= '</ul>';
18699:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
18700:                                         $chgtext .= '<span class="LC_warning">'.
18701:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
18702:                                                     '</span>';
18703:                                     }
18704:                                 }
18705:                             } else {
18706:                                 if (@{$cancreate{$type}} == 0) {
18707:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
18708:                                 } else {
18709:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
18710:                                 }
18711:                             }
18712:                             $chgtext .= '<br />';
18713:                         }
18714:                     } elsif ($type eq 'selfcreateprocessing') {
18715:                         my %choices = &Apache::lonlocal::texthash (
18716:                                                                     automatic => 'Automatic approval',
18717:                                                                     approval  => 'Queued for approval',
18718:                                                                   );
18719:                         if (@types) {
18720:                             if (@statuses) {
18721:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
18722:                                             '<ul>';
18723:                                 foreach my $status (@statuses) {
18724:                                     if ($status eq 'default') {
18725:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
18726:                                     } else {
18727:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
18728:                                     }
18729:                                 }
18730:                                 $chgtext .= '</ul>';
18731:                             }
18732:                         } else {
18733:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
18734:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
18735:                         }
18736:                     } elsif ($type eq 'emailverified') {
18737:                         my %options = &Apache::lonlocal::texthash (
18738:                                                                     all   => 'Same as e-mail',
18739:                                                                     first => 'Omit @domain',
18740:                                                                     free  => 'Free to choose',
18741:                                                                   );
18742:                         if (@types) {
18743:                             if (@statuses) {
18744:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
18745:                                             '<ul>';
18746:                                 foreach my $status (@statuses) {
18747:                                     if ($status eq 'default') {
18748:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
18749:                                     } else {
18750:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
18751:                                     }
18752:                                 }
18753:                                 $chgtext .= '</ul>';
18754:                             }
18755:                         } else {
18756:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
18757:                                             $options{$cancreate{'emailverified'}{'default'}});
18758:                         }
18759:                     } elsif ($type eq 'emailoptions') {
18760:                         my %options = &Apache::lonlocal::texthash (
18761:                                                                     any     => 'Any e-mail',
18762:                                                                     inst    => 'Institutional only',
18763:                                                                     noninst => 'Non-institutional only',
18764:                                                                     custom  => 'Custom restrictions',
18765:                                                                   );
18766:                         if (@types) {
18767:                             if (@statuses) {
18768:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
18769:                                             '<ul>';
18770:                                 foreach my $status (@statuses) {
18771:                                     if ($type eq 'default') {
18772:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
18773:                                     } else {
18774:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
18775:                                     }
18776:                                 }
18777:                                 $chgtext .= '</ul>';
18778:                             }
18779:                         } else {
18780:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
18781:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
18782:                             } else {
18783:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
18784:                                                 $options{$cancreate{'emailoptions'}{'default'}});
18785:                             }
18786:                         }
18787:                     } elsif ($type eq 'emaildomain') {
18788:                         my $output;
18789:                         if (@statuses) {
18790:                             foreach my $type (@statuses) {
18791:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
18792:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
18793:                                         if ($type eq 'default') {
18794:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18795:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
18796:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
18797:                                             } else {
18798:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
18799:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
18800:                                             }
18801:                                         } else {
18802:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18803:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
18804:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
18805:                                             } else {
18806:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
18807:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
18808:                                             }
18809:                                         }
18810:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
18811:                                         if ($type eq 'default') {
18812:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18813:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
18814:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
18815:                                             } else {
18816:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
18817:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
18818:                                             }
18819:                                         } else {
18820:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18821:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
18822:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
18823:                                             } else {
18824:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
18825:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
18826:                                             }
18827:                                         }
18828:                                     }
18829:                                 }
18830:                             }
18831:                         }
18832:                         if ($output ne '') {
18833:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
18834:                                         '<ul>'.$output.'</ul>';
18835:                         }
18836:                     } elsif ($type eq 'captcha') {
18837:                         if ($savecaptcha{$type} eq 'notused') {
18838:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
18839:                         } else {
18840:                             my %captchas = &captcha_phrases();
18841:                             if ($captchas{$savecaptcha{$type}}) {
18842:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
18843:                             } else {
18844:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
18845:                             }
18846:                         }
18847:                     } elsif ($type eq 'recaptchakeys') {
18848:                         my ($privkey,$pubkey);
18849:                         if (ref($savecaptcha{$type}) eq 'HASH') {
18850:                             $pubkey = $savecaptcha{$type}{'public'};
18851:                             $privkey = $savecaptcha{$type}{'private'};
18852:                         }
18853:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
18854:                         if (!$pubkey) {
18855:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
18856:                         } else {
18857:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
18858:                         }
18859:                         if (!$privkey) {
18860:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
18861:                         } else {
18862:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
18863:                         }
18864:                         $chgtext .= '</ul>';
18865:                     } elsif ($type eq 'recaptchaversion') {
18866:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
18867:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
18868:                         }
18869:                     } elsif ($type eq 'emailusername') {
18870:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
18871:                             if (@statuses) {
18872:                                 foreach my $type (@statuses) {
18873:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
18874:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
18875:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
18876:                                                     '<ul>';
18877:                                             foreach my $field (@{$infofields}) {
18878:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
18879:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
18880:                                                 }
18881:                                             }
18882:                                             $chgtext .= '</ul>';
18883:                                         } else {
18884:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
18885:                                         }
18886:                                     } else {
18887:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
18888:                                     }
18889:                                 }
18890:                             }
18891:                         }
18892:                     } elsif ($type eq 'notify') {
18893:                         my $numapprove = 0;
18894:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
18895:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
18896:                                 if ($cancreate{'notify'}{'approval'}) {
18897:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
18898:                                     $numapprove ++;
18899:                                 }
18900:                             }
18901:                         }
18902:                         unless ($numapprove) {
18903:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
18904:                         }
18905:                     }
18906:                     if ($chgtext) {
18907:                         $resulttext .= '<li>'.$chgtext.'</li>';
18908:                     }
18909:                 }
18910:             }
18911:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
18912:                 my ($emailrules,$emailruleorder) =
18913:                     &Apache::lonnet::inst_userrules($dom,'email');
18914:                 foreach my $type (@{$changes{'email_rule'}}) {
18915:                     if (ref($email_rule{$type}) eq 'ARRAY') {
18916:                         my $chgtext = '<ul>';
18917:                         foreach my $rule (@{$email_rule{$type}}) {
18918:                             if (ref($emailrules->{$rule}) eq 'HASH') {
18919:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
18920:                             }
18921:                         }
18922:                         $chgtext .= '</ul>';
18923:                         my $typename;
18924:                         if (@types) {
18925:                             if ($type eq 'default') {
18926:                                 $typename = $othertitle;
18927:                             } else {
18928:                                 $typename = $usertypes{$type};
18929:                             }
18930:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
18931:                         }
18932:                         if (@{$email_rule{$type}} > 0) {
18933:                             $resulttext .= '<li>'.
18934:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
18935:                                                $usertypes{$type}).
18936:                                            $chgtext.
18937:                                            '</li>';
18938:                         } else {
18939:                             $resulttext .= '<li>'.
18940:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
18941:                                            '</li>'.
18942:                                            &mt('(Affiliation: [_1])',$typename);
18943:                         }
18944:                     }
18945:                 }
18946:             }
18947:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
18948:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
18949:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
18950:                         my $chgtext = '<ul>';
18951:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
18952:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
18953:                         }
18954:                         $chgtext .= '</ul>';
18955:                         $resulttext .= '<li>'.
18956:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
18957:                                           $chgtext.
18958:                                        '</li>';
18959:                     } else {
18960:                         $resulttext .= '<li>'.
18961:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
18962:                                        '</li>';
18963:                     }
18964:                 }
18965:             }
18966:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
18967:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
18968:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
18969:                 foreach my $type (@{$changes{'selfcreate'}}) {
18970:                     my $typename = $type;
18971:                     if (keys(%usertypes) > 0) {
18972:                         if ($usertypes{$type} ne '') {
18973:                             $typename = $usertypes{$type};
18974:                         }
18975:                     }
18976:                     my @modifiable;
18977:                     $resulttext .= '<li>'.
18978:                                     &mt('Self-creation of account by users with status: [_1]',
18979:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
18980:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
18981:                     foreach my $field (@fields) {
18982:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
18983:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
18984:                         }
18985:                     }
18986:                     if (@modifiable > 0) {
18987:                         $resulttext .= join(', ',@modifiable);
18988:                     } else {
18989:                         $resulttext .= &mt('none');
18990:                     }
18991:                     $resulttext .= '</li>';
18992:                 }
18993:                 $resulttext .= '</ul></li>';
18994:             }
18995:             $resulttext .= '</ul>';
18996:             my $cachetime = 24*60*60;
18997:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
18998:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18999:             if (ref($lastactref) eq 'HASH') {
19000:                 $lastactref->{'domdefaults'} = 1;
19001:             }
19002:         } else {
19003:             $resulttext = &mt('No changes made to self-creation settings');
19004:         }
19005:     } else {
19006:         $resulttext = '<span class="LC_error">'.
19007:             &mt('An error occurred: [_1]',$putresult).'</span>';
19008:     }
19009:     if ($warningmsg ne '') {
19010:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
19011:     }
19012:     return $resulttext;
19013: }
19014: 
19015: sub process_captcha {
19016:     my ($container,$changes,$newsettings,$currsettings) = @_;
19017:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
19018:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
19019:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
19020:         $newsettings->{'captcha'} = 'original';
19021:     }
19022:     my %current;
19023:     if (ref($currsettings) eq 'HASH') {
19024:         %current = %{$currsettings};
19025:     }
19026:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
19027:         if ($container eq 'cancreate') {
19028:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19029:                 push(@{$changes->{'cancreate'}},'captcha');
19030:             } elsif (!defined($changes->{'cancreate'})) {
19031:                 $changes->{'cancreate'} = ['captcha'];
19032:             }
19033:         } elsif ($container eq 'passwords') {
19034:             $changes->{'reset'} = 1;
19035:         } else {
19036:             $changes->{'captcha'} = 1;
19037:         }
19038:     }
19039:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
19040:     if ($newsettings->{'captcha'} eq 'recaptcha') {
19041:         $newpub = $env{'form.'.$container.'_recaptchapub'};
19042:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
19043:         $newpub =~ s/[^\w\-]//g;
19044:         $newpriv =~ s/[^\w\-]//g;
19045:         $newsettings->{'recaptchakeys'} = {
19046:                                              public  => $newpub,
19047:                                              private => $newpriv,
19048:                                           };
19049:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
19050:         $newversion =~ s/\D//g;
19051:         if ($newversion ne '2') {
19052:             $newversion = 1;
19053:         }
19054:         $newsettings->{'recaptchaversion'} = $newversion;
19055:     }
19056:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
19057:         $currpub = $current{'recaptchakeys'}{'public'};
19058:         $currpriv = $current{'recaptchakeys'}{'private'};
19059:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
19060:             $newsettings->{'recaptchakeys'} = {
19061:                                                  public  => '',
19062:                                                  private => '',
19063:                                               }
19064:         }
19065:     }
19066:     if ($current{'captcha'} eq 'recaptcha') {
19067:         $currversion = $current{'recaptchaversion'};
19068:         if ($currversion ne '2') {
19069:             $currversion = 1;
19070:         }
19071:     }
19072:     if ($currversion ne $newversion) {
19073:         if ($container eq 'cancreate') {
19074:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19075:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
19076:             } elsif (!defined($changes->{'cancreate'})) {
19077:                 $changes->{'cancreate'} = ['recaptchaversion'];
19078:             }
19079:         } elsif ($container eq 'passwords') {
19080:             $changes->{'reset'} = 1;
19081:         } else {
19082:             $changes->{'recaptchaversion'} = 1;
19083:         }
19084:     }
19085:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
19086:         if ($container eq 'cancreate') {
19087:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19088:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
19089:             } elsif (!defined($changes->{'cancreate'})) {
19090:                 $changes->{'cancreate'} = ['recaptchakeys'];
19091:             }
19092:         } elsif ($container eq 'passwords') {
19093:             $changes->{'reset'} = 1;
19094:         } else {
19095:             $changes->{'recaptchakeys'} = 1;
19096:         }
19097:     }
19098:     return;
19099: }
19100: 
19101: sub modify_usermodification {
19102:     my ($dom,%domconfig) = @_;
19103:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
19104:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
19105:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
19106:             if ($key eq 'selfcreate') {
19107:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
19108:             } else {  
19109:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
19110:             }
19111:         }
19112:     }
19113:     my @contexts = ('author','course');
19114:     my %context_title = (
19115:                            author => 'In author context',
19116:                            course => 'In course context',
19117:                         );
19118:     my @fields = ('lastname','firstname','middlename','generation',
19119:                   'permanentemail','id');
19120:     my %roles = (
19121:                   author => ['ca','aa'],
19122:                   course => ['st','ep','ta','in','cr'],
19123:                 );
19124:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19125:     foreach my $context (@contexts) {
19126:         foreach my $role (@{$roles{$context}}) {
19127:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
19128:             foreach my $item (@fields) {
19129:                 if (grep(/^\Q$item\E$/,@modifiable)) {
19130:                     $modifyhash{$context}{$role}{$item} = 1;
19131:                 } else {
19132:                     $modifyhash{$context}{$role}{$item} = 0;
19133:                 }
19134:             }
19135:         }
19136:         if (ref($curr_usermodification{$context}) eq 'HASH') {
19137:             foreach my $role (@{$roles{$context}}) {
19138:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
19139:                     foreach my $field (@fields) {
19140:                         if ($modifyhash{$context}{$role}{$field} ne 
19141:                                 $curr_usermodification{$context}{$role}{$field}) {
19142:                             push(@{$changes{$context}},$role);
19143:                             last;
19144:                         }
19145:                     }
19146:                 }
19147:             }
19148:         } else {
19149:             foreach my $context (@contexts) {
19150:                 foreach my $role (@{$roles{$context}}) {
19151:                     push(@{$changes{$context}},$role);
19152:                 }
19153:             }
19154:         }
19155:     }
19156:     my %usermodification_hash =  (
19157:                                    usermodification => \%modifyhash,
19158:                                  );
19159:     my $putresult = &Apache::lonnet::put_dom('configuration',
19160:                                              \%usermodification_hash,$dom);
19161:     if ($putresult eq 'ok') {
19162:         if (keys(%changes) > 0) {
19163:             $resulttext = &mt('Changes made: ').'<ul>';
19164:             foreach my $context (@contexts) {
19165:                 if (ref($changes{$context}) eq 'ARRAY') {
19166:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
19167:                     if (ref($changes{$context}) eq 'ARRAY') {
19168:                         foreach my $role (@{$changes{$context}}) {
19169:                             my $rolename;
19170:                             if ($role eq 'cr') {
19171:                                 $rolename = &mt('Custom');
19172:                             } else {
19173:                                 $rolename = &Apache::lonnet::plaintext($role);
19174:                             }
19175:                             my @modifiable;
19176:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
19177:                             foreach my $field (@fields) {
19178:                                 if ($modifyhash{$context}{$role}{$field}) {
19179:                                     push(@modifiable,$fieldtitles{$field});
19180:                                 }
19181:                             }
19182:                             if (@modifiable > 0) {
19183:                                 $resulttext .= join(', ',@modifiable);
19184:                             } else {
19185:                                 $resulttext .= &mt('none'); 
19186:                             }
19187:                             $resulttext .= '</li>';
19188:                         }
19189:                         $resulttext .= '</ul></li>';
19190:                     }
19191:                 }
19192:             }
19193:             $resulttext .= '</ul>';
19194:         } else {
19195:             $resulttext = &mt('No changes made to user modification settings');
19196:         }
19197:     } else {
19198:         $resulttext = '<span class="LC_error">'.
19199:             &mt('An error occurred: [_1]',$putresult).'</span>';
19200:     }
19201:     return $resulttext;
19202: }
19203: 
19204: sub modify_defaults {
19205:     my ($dom,$lastactref,%domconfig) = @_;
19206:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
19207:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19208:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
19209:                  'portal_def');
19210:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
19211:     foreach my $item (@items) {
19212:         $newvalues{$item} = $env{'form.'.$item};
19213:         if ($item eq 'auth_def') {
19214:             if ($newvalues{$item} ne '') {
19215:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
19216:                     push(@errors,$item);
19217:                 }
19218:             }
19219:         } elsif ($item eq 'lang_def') {
19220:             if ($newvalues{$item} ne '') {
19221:                 if ($newvalues{$item} =~ /^(\w+)/) {
19222:                     my $langcode = $1;
19223:                     if ($langcode ne 'x_chef') {
19224:                         if (code2language($langcode) eq '') {
19225:                             push(@errors,$item);
19226:                         }
19227:                     }
19228:                 } else {
19229:                     push(@errors,$item);
19230:                 }
19231:             }
19232:         } elsif ($item eq 'timezone_def') {
19233:             if ($newvalues{$item} ne '') {
19234:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
19235:                     push(@errors,$item);   
19236:                 }
19237:             }
19238:         } elsif ($item eq 'datelocale_def') {
19239:             if ($newvalues{$item} ne '') {
19240:                 my @datelocale_ids = DateTime::Locale->ids();
19241:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
19242:                     push(@errors,$item);
19243:                 }
19244:             }
19245:         } elsif ($item eq 'portal_def') {
19246:             if ($newvalues{$item} ne '') {
19247:                 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])\/?$/) {
19248:                     foreach my $field ('email','web') {
19249:                         if ($env{'form.'.$item.'_'.$field}) {
19250:                             $newvalues{$item.'_'.$field} = $env{'form.'.$item.'_'.$field};
19251:                         }
19252:                     }
19253:                 } else {
19254:                     push(@errors,$item);
19255:                 }
19256:             }
19257:         }
19258:         if (grep(/^\Q$item\E$/,@errors)) {
19259:             $newvalues{$item} = $domdefaults{$item};
19260:             if ($item eq 'portal_def') {
19261:                 if ($domdefaults{$item}) {
19262:                     foreach my $field ('email','web') {
19263:                         if (exists($domdefaults{$item.'_'.$field})) {
19264:                             $newvalues{$item.'_'.$field} = $domdefaults{$item.'_'.$field};
19265:                         }
19266:                     }
19267:                 }
19268:             }
19269:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
19270:             $changes{$item} = 1;
19271:         }
19272:         if ($item eq 'portal_def') {
19273:             unless (grep(/^\Q$item\E$/,@errors)) {
19274:                 if ($newvalues{$item} eq '') {
19275:                     foreach my $field ('email','web') {
19276:                         if (exists($domdefaults{$item.'_'.$field})) {
19277:                             delete($domdefaults{$item.'_'.$field});
19278:                         }
19279:                     }
19280:                 } else {
19281:                     unless ($changes{$item}) {
19282:                         foreach my $field ('email','web') {
19283:                             if ($domdefaults{$item.'_'.$field} ne $newvalues{$item.'_'.$field}) {
19284:                                 $changes{$item} = 1;
19285:                                 last;
19286:                             }
19287:                         }
19288:                     }
19289:                     foreach my $field ('email','web') {
19290:                         if ($newvalues{$item.'_'.$field}) {
19291:                             $domdefaults{$item.'_'.$field} = $newvalues{$item.'_'.$field};
19292:                         } elsif (exists($domdefaults{$item.'_'.$field})) {
19293:                             delete($domdefaults{$item.'_'.$field});
19294:                         }
19295:                     }
19296:                 }
19297:             }
19298:         }
19299:         $domdefaults{$item} = $newvalues{$item};
19300:     }
19301:     my %staticdefaults = (
19302:                            'intauth_cost'   => 10,
19303:                            'intauth_check'  => 0,
19304:                            'intauth_switch' => 0,
19305:                          );
19306:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
19307:         if (exists($domdefaults{$item})) {
19308:             $newvalues{$item} = $domdefaults{$item};
19309:         } else {
19310:             $newvalues{$item} = $staticdefaults{$item};
19311:         }
19312:     }
19313:     my ($unamemaprules,$ruleorder);
19314:     my @possunamemaprules = &Apache::loncommon::get_env_multiple('form.unamemap_rule');
19315:     if (@possunamemaprules) {
19316:         ($unamemaprules,$ruleorder) =
19317:             &Apache::lonnet::inst_userrules($dom,'unamemap');
19318:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
19319:             if (@{$ruleorder} > 0) {
19320:                 my %possrules;
19321:                 map { $possrules{$_} = 1; } @possunamemaprules;
19322:                 foreach my $rule (@{$ruleorder}) {
19323:                     if ($possrules{$rule}) {
19324:                         push(@{$newvalues{'unamemap_rule'}},$rule);
19325:                     }
19326:                 }
19327:             }
19328:         }
19329:     }
19330:     if (ref($domdefaults{'unamemap_rule'}) eq 'ARRAY') {
19331:         if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
19332:             my @rulediffs = &Apache::loncommon::compare_arrays($domdefaults{'unamemap_rule'},
19333:                                                                $newvalues{'unamemap_rule'});
19334:             if (@rulediffs) {
19335:                 $changes{'unamemap_rule'} = 1;
19336:                 $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
19337:             }
19338:         } elsif (@{$domdefaults{'unamemap_rule'}} > 0) {
19339:             $changes{'unamemap_rule'} = 1;
19340:             delete($domdefaults{'unamemap_rule'});
19341:         }
19342:     } elsif (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
19343:         if (@{$newvalues{'unamemap_rule'}} > 0) {
19344:             $changes{'unamemap_rule'} = 1;
19345:             $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
19346:         }
19347:     }
19348:     my %defaults_hash = (
19349:                          defaults => \%newvalues,
19350:                         );
19351:     my $title = &defaults_titles();
19352: 
19353:     my $currinststatus;
19354:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
19355:         $currinststatus = $domconfig{'inststatus'};
19356:     } else {
19357:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
19358:         $currinststatus = {
19359:                              inststatustypes => $usertypes,
19360:                              inststatusorder => $types,
19361:                              inststatusguest => [],
19362:                           };
19363:     }
19364:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
19365:     my @allpos;
19366:     my %alltypes;
19367:     my @inststatusguest;
19368:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
19369:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
19370:             unless (grep(/^\Q$type\E$/,@todelete)) {
19371:                 push(@inststatusguest,$type);
19372:             }
19373:         }
19374:     }
19375:     my ($currtitles,$currorder);
19376:     if (ref($currinststatus) eq 'HASH') {
19377:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
19378:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
19379:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
19380:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
19381:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
19382:                     }
19383:                 }
19384:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
19385:                     my $position = $env{'form.inststatus_pos_'.$type};
19386:                     $position =~ s/\D+//g;
19387:                     $allpos[$position] = $type;
19388:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
19389:                     $alltypes{$type} =~ s/`//g;
19390:                 }
19391:             }
19392:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
19393:             $currtitles =~ s/,$//;
19394:         }
19395:     }
19396:     if ($env{'form.addinststatus'}) {
19397:         my $newtype = $env{'form.addinststatus'};
19398:         $newtype =~ s/\W//g;
19399:         unless (exists($alltypes{$newtype})) {
19400:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
19401:             $alltypes{$newtype} =~ s/`//g; 
19402:             my $position = $env{'form.addinststatus_pos'};
19403:             $position =~ s/\D+//g;
19404:             if ($position ne '') {
19405:                 $allpos[$position] = $newtype;
19406:             }
19407:         }
19408:     }
19409:     my @orderedstatus;
19410:     foreach my $type (@allpos) {
19411:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
19412:             push(@orderedstatus,$type);
19413:         }
19414:     }
19415:     foreach my $type (keys(%alltypes)) {
19416:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
19417:             delete($alltypes{$type});
19418:         }
19419:     }
19420:     $defaults_hash{'inststatus'} = {
19421:                                      inststatustypes => \%alltypes,
19422:                                      inststatusorder => \@orderedstatus,
19423:                                      inststatusguest => \@inststatusguest,
19424:                                    };
19425:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
19426:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
19427:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
19428:         }
19429:     }
19430:     if ($currorder ne join(',',@orderedstatus)) {
19431:         $changes{'inststatus'}{'inststatusorder'} = 1;
19432:     }
19433:     my $newtitles;
19434:     foreach my $item (@orderedstatus) {
19435:         $newtitles .= $alltypes{$item}.',';
19436:     }
19437:     $newtitles =~ s/,$//;
19438:     if ($currtitles ne $newtitles) {
19439:         $changes{'inststatus'}{'inststatustypes'} = 1;
19440:     }
19441:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
19442:                                              $dom);
19443:     if ($putresult eq 'ok') {
19444:         if (keys(%changes) > 0) {
19445:             $resulttext = &mt('Changes made:').'<ul>';
19446:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
19447:             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";
19448:             foreach my $item (sort(keys(%changes))) {
19449:                 if ($item eq 'inststatus') {
19450:                     if (ref($changes{'inststatus'}) eq 'HASH') {
19451:                         if (@orderedstatus) {
19452:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
19453:                             foreach my $type (@orderedstatus) { 
19454:                                 $resulttext .= $alltypes{$type}.', ';
19455:                             }
19456:                             $resulttext =~ s/, $//;
19457:                             $resulttext .= '</li>';
19458:                         } else {
19459:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
19460:                         }
19461:                     }
19462:                 } elsif ($item eq 'unamemap_rule') {
19463:                     if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
19464:                         my @rulenames;
19465:                         if (ref($unamemaprules) eq 'HASH') {
19466:                             foreach my $rule (@{$newvalues{'unamemap_rule'}}) {
19467:                                 if (ref($unamemaprules->{$rule}) eq 'HASH') {
19468:                                     push(@rulenames,$unamemaprules->{$rule}->{'name'});
19469:                                 }
19470:                             }
19471:                         }
19472:                         if (@rulenames) {
19473:                             $resulttext .= '<li>'.&mt('Mapping for missing usernames includes: [_1]',
19474:                                                      '<ul><li>'.join('</li><li>',@rulenames).'</li></ul>').
19475:                                            '</li>';
19476:                         } else {
19477:                             $resulttext .= '<li>'.&mt('No mapping for missing usernames via standard log-in').'</li>';
19478:                         }
19479:                     } else {
19480:                         $resulttext .= '<li>'.&mt('Mapping for missing usernames via standard log-in deleted').'</li>';
19481:                     }
19482:                 } else {
19483:                     my $value = $env{'form.'.$item};
19484:                     if ($value eq '') {
19485:                         $value = &mt('none');
19486:                     } elsif ($item eq 'auth_def') {
19487:                         my %authnames = &authtype_names();
19488:                         my %shortauth = (
19489:                                           internal   => 'int',
19490:                                           krb4       => 'krb4',
19491:                                           krb5       => 'krb5',
19492:                                           localauth  => 'loc',
19493:                                           lti        => 'lti',
19494:                         );
19495:                         $value = $authnames{$shortauth{$value}};
19496:                     }
19497:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
19498:                     $mailmsgtext .= "$title->{$item} set to $value\n";
19499:                     if ($item eq 'portal_def') {
19500:                         if ($env{'form.'.$item} ne '') {
19501:                             foreach my $field ('email','web') {
19502:                                 $value = $env{'form.'.$item.'_'.$field};
19503:                                 if ($value) {
19504:                                     $value = &mt('Yes');
19505:                                 } else {
19506:                                     $value = &mt('No');
19507:                                 }
19508:                                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$field},$value).'</li>';
19509:                             }
19510:                         }
19511:                     }
19512:                 }
19513:             }
19514:             $resulttext .= '</ul>';
19515:             $mailmsgtext .= "\n";
19516:             my $cachetime = 24*60*60;
19517:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19518:             if (ref($lastactref) eq 'HASH') {
19519:                 $lastactref->{'domdefaults'} = 1;
19520:             }
19521:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
19522:                 my $notify = 1;
19523:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
19524:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
19525:                         $notify = 0;
19526:                     }
19527:                 }
19528:                 if ($notify) {
19529:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
19530:                                                "LON-CAPA Domain Settings Change - $dom",
19531:                                                $mailmsgtext);
19532:                 }
19533:             }
19534:         } else {
19535:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
19536:         }
19537:     } else {
19538:         $resulttext = '<span class="LC_error">'.
19539:             &mt('An error occurred: [_1]',$putresult).'</span>';
19540:     }
19541:     if (@errors > 0) {
19542:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
19543:         foreach my $item (@errors) {
19544:             $resulttext .= ' "'.$title->{$item}.'",';
19545:         }
19546:         $resulttext =~ s/,$//;
19547:     }
19548:     return $resulttext;
19549: }
19550: 
19551: sub modify_scantron {
19552:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
19553:     my ($resulttext,%confhash,%changes,$errors);
19554:     my $custom = 'custom.tab';
19555:     my $default = 'default.tab';
19556:     my $servadm = $r->dir_config('lonAdmEMail');
19557:     my ($configuserok,$author_ok,$switchserver) =
19558:         &config_check($dom,$confname,$servadm);
19559:     if ($env{'form.scantronformat.filename'} ne '') {
19560:         my $error;
19561:         if ($configuserok eq 'ok') {
19562:             if ($switchserver) {
19563:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
19564:             } else {
19565:                 if ($author_ok eq 'ok') {
19566:                     my $modified = [];
19567:                     my ($result,$scantronurl) =
19568:                         &Apache::lonconfigsettings::publishlogo($r,'upload','scantronformat',$dom,
19569:                                                                 $confname,'scantron','','',$custom,
19570:                                                                 $modified);
19571:                     if ($result eq 'ok') {
19572:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
19573:                         $changes{'scantronformat'} = 1;
19574:                         &update_modify_urls($r,$modified);
19575:                     } else {
19576:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
19577:                     }
19578:                 } else {
19579:                     $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);
19580:                 }
19581:             }
19582:         } else {
19583:             $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);
19584:         }
19585:         if ($error) {
19586:             &Apache::lonnet::logthis($error);
19587:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
19588:         }
19589:     }
19590:     if (ref($domconfig{'scantron'}) eq 'HASH') {
19591:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
19592:             if ($env{'form.scantronformat_del'}) {
19593:                 $confhash{'scantron'}{'scantronformat'} = '';
19594:                 $changes{'scantronformat'} = 1;
19595:             } else {
19596:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
19597:             }
19598:         }
19599:     }
19600:     my @options = ('hdr','pad','rem');
19601:     my @fields = &scantroncsv_fields();
19602:     my %titles = &scantronconfig_titles();
19603:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
19604:     my ($newdat,$currdat,%newcol,%currcol);
19605:     if (grep(/^dat$/,@formats)) {
19606:         $confhash{'scantron'}{config}{dat} = 1;
19607:         $newdat = 1;
19608:     } else {
19609:         $newdat = 0;
19610:     }
19611:     if (grep(/^csv$/,@formats)) {
19612:         my %bynum;
19613:         foreach my $field (@fields) {
19614:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
19615:                 my $posscol = $1;
19616:                 if (($posscol < 20) && (!$bynum{$posscol})) {
19617:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
19618:                     $bynum{$posscol} = $field;
19619:                     $newcol{$field} = $posscol;
19620:                 }
19621:             }
19622:         }
19623:         if (keys(%newcol)) {
19624:             foreach my $option (@options) {
19625:                 if ($env{'form.scantroncsv_'.$option}) {
19626:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
19627:                 }
19628:             }
19629:         }
19630:     }
19631:     $currdat = 1;
19632:     if (ref($domconfig{'scantron'}) eq 'HASH') {
19633:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
19634:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
19635:                 $currdat = 0;
19636:             }
19637:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
19638:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
19639:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
19640:                 }
19641:             }
19642:         }
19643:     }
19644:     if ($currdat != $newdat) {
19645:         $changes{'config'} = 1;
19646:     } else {
19647:         foreach my $field (@fields) {
19648:             if ($currcol{$field} ne '') {
19649:                 if ($currcol{$field} ne $newcol{$field}) {
19650:                     $changes{'config'} = 1;
19651:                     last;
19652:                 }
19653:             } elsif ($newcol{$field} ne '') {
19654:                 $changes{'config'} = 1;
19655:                 last;
19656:             }
19657:         }
19658:     }
19659:     if (keys(%confhash) > 0) {
19660:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
19661:                                                  $dom);
19662:         if ($putresult eq 'ok') {
19663:             if (keys(%changes) > 0) {
19664:                 if (ref($confhash{'scantron'}) eq 'HASH') {
19665:                     $resulttext = &mt('Changes made:').'<ul>';
19666:                     if ($changes{'scantronformat'}) {
19667:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
19668:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
19669:                         } else {
19670:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
19671:                         }
19672:                     }
19673:                     if ($changes{'config'}) {
19674:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
19675:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
19676:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
19677:                             }
19678:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
19679:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
19680:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
19681:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
19682:                                         foreach my $field (@fields) {
19683:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
19684:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
19685:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
19686:                                             }
19687:                                         }
19688:                                         $resulttext .= '</ul></li>';
19689:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
19690:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
19691:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
19692:                                                 foreach my $option (@options) {
19693:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
19694:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
19695:                                                     }
19696:                                                 }
19697:                                                 $resulttext .= '</ul></li>';
19698:                                             }
19699:                                         }
19700:                                     }
19701:                                 }
19702:                             }
19703:                         } else {
19704:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
19705:                         }
19706:                     }
19707:                     $resulttext .= '</ul>';
19708:                 } else {
19709:                     $resulttext = &mt('Changes made to bubblesheet format file.');
19710:                 }
19711:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
19712:                 if (ref($lastactref) eq 'HASH') {
19713:                     $lastactref->{'domainconfig'} = 1;
19714:                 }
19715:             } else {
19716:                 $resulttext = &mt('No changes made to bubblesheet format settings');
19717:             }
19718:         } else {
19719:             $resulttext = '<span class="LC_error">'.
19720:                 &mt('An error occurred: [_1]',$putresult).'</span>';
19721:         }
19722:     } else {
19723:         $resulttext = &mt('No changes made to bubblesheet format file'); 
19724:     }
19725:     if ($errors) {
19726:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
19727:                        $errors.'</ul></p>';
19728:     }
19729:     return $resulttext;
19730: }
19731: 
19732: sub modify_coursecategories {
19733:     my ($dom,$lastactref,%domconfig) = @_;
19734:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
19735:         $cathash);
19736:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
19737:     my @catitems = ('unauth','auth');
19738:     my @cattypes = ('std','domonly','codesrch','none');
19739:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
19740:         $cathash = $domconfig{'coursecategories'}{'cats'};
19741:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
19742:             $changes{'togglecats'} = 1;
19743:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
19744:         }
19745:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
19746:             $changes{'categorize'} = 1;
19747:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
19748:         }
19749:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
19750:             $changes{'togglecatscomm'} = 1;
19751:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
19752:         }
19753:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
19754:             $changes{'categorizecomm'} = 1;
19755:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
19756: 
19757:         }
19758:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
19759:             $changes{'togglecatsplace'} = 1;
19760:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
19761:         }
19762:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
19763:             $changes{'categorizeplace'} = 1;
19764:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
19765:         }
19766:         foreach my $item (@catitems) {
19767:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
19768:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
19769:                     $changes{$item} = 1;
19770:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
19771:                 }
19772:             }
19773:         }
19774:     } else {
19775:         $changes{'togglecats'} = 1;
19776:         $changes{'categorize'} = 1;
19777:         $changes{'togglecatscomm'} = 1;
19778:         $changes{'categorizecomm'} = 1;
19779:         $changes{'togglecatsplace'} = 1;
19780:         $changes{'categorizeplace'} = 1;
19781:         $domconfig{'coursecategories'} = {
19782:                                              togglecats => $env{'form.togglecats'},
19783:                                              categorize => $env{'form.categorize'},
19784:                                              togglecatscomm => $env{'form.togglecatscomm'},
19785:                                              categorizecomm => $env{'form.categorizecomm'},
19786:                                              togglecatsplace => $env{'form.togglecatsplace'},
19787:                                              categorizeplace => $env{'form.categorizeplace'},
19788:                                          };
19789:         foreach my $item (@catitems) {
19790:             if ($env{'form.coursecat_'.$item} ne 'std') {
19791:                 $changes{$item} = 1;
19792:             }
19793:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
19794:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
19795:             }
19796:         }
19797:     }
19798:     if (ref($cathash) eq 'HASH') {
19799:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
19800:             push (@deletecategory,'instcode::0');
19801:         }
19802:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
19803:             push(@deletecategory,'communities::0');
19804:         }
19805:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
19806:             push(@deletecategory,'placement::0');
19807:         }
19808:     }
19809:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
19810:     if (ref($cathash) eq 'HASH') {
19811:         if (@deletecategory > 0) {
19812:             #FIXME Need to remove category from all courses using a deleted category 
19813:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
19814:             foreach my $item (@deletecategory) {
19815:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
19816:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
19817:                     $deletions{$item} = 1;
19818:                     &recurse_cat_deletes($item,$cathash,\%deletions);
19819:                 }
19820:             }
19821:         }
19822:         foreach my $item (keys(%{$cathash})) {
19823:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
19824:             if ($cathash->{$item} ne $env{'form.'.$item}) {
19825:                 $reorderings{$item} = 1;
19826:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
19827:             }
19828:             if ($env{'form.addcategory_name_'.$item} ne '') {
19829:                 my $newcat = $env{'form.addcategory_name_'.$item};
19830:                 my $newdepth = $depth+1;
19831:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
19832:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
19833:                 $adds{$newitem} = 1; 
19834:             }
19835:             if ($env{'form.subcat_'.$item} ne '') {
19836:                 my $newcat = $env{'form.subcat_'.$item};
19837:                 my $newdepth = $depth+1;
19838:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
19839:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
19840:                 $adds{$newitem} = 1;
19841:             }
19842:         }
19843:     }
19844:     if ($env{'form.instcode'} eq '1') {
19845:         if (ref($cathash) eq 'HASH') {
19846:             my $newitem = 'instcode::0';
19847:             if ($cathash->{$newitem} eq '') {  
19848:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
19849:                 $adds{$newitem} = 1;
19850:             }
19851:         } else {
19852:             my $newitem = 'instcode::0';
19853:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
19854:             $adds{$newitem} = 1;
19855:         }
19856:     }
19857:     if ($env{'form.communities'} eq '1') {
19858:         if (ref($cathash) eq 'HASH') {
19859:             my $newitem = 'communities::0';
19860:             if ($cathash->{$newitem} eq '') {
19861:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
19862:                 $adds{$newitem} = 1;
19863:             }
19864:         } else {
19865:             my $newitem = 'communities::0';
19866:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
19867:             $adds{$newitem} = 1;
19868:         }
19869:     }
19870:     if ($env{'form.placement'} eq '1') {
19871:         if (ref($cathash) eq 'HASH') {
19872:             my $newitem = 'placement::0';
19873:             if ($cathash->{$newitem} eq '') {
19874:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
19875:                 $adds{$newitem} = 1;
19876:             }
19877:         } else {
19878:             my $newitem = 'placement::0';
19879:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
19880:             $adds{$newitem} = 1;
19881:         }
19882:     }
19883:     if ($env{'form.addcategory_name'} ne '') {
19884:         if (($env{'form.addcategory_name'} ne 'instcode') &&
19885:             ($env{'form.addcategory_name'} ne 'communities') &&
19886:             ($env{'form.addcategory_name'} ne 'placement')) {
19887:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
19888:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
19889:             $adds{$newitem} = 1;
19890:         }
19891:     }
19892:     my $putresult;
19893:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19894:         if (keys(%deletions) > 0) {
19895:             foreach my $key (keys(%deletions)) {
19896:                 if ($predelallitems{$key} ne '') {
19897:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
19898:                 }
19899:             }
19900:         }
19901:         my (@chkcats,@chktrails,%chkallitems);
19902:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
19903:         if (ref($chkcats[0]) eq 'ARRAY') {
19904:             my $depth = 0;
19905:             my $chg = 0;
19906:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
19907:                 my $name = $chkcats[0][$i];
19908:                 my $item;
19909:                 if ($name eq '') {
19910:                     $chg ++;
19911:                 } else {
19912:                     $item = &escape($name).'::0';
19913:                     if ($chg) {
19914:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
19915:                     }
19916:                     $depth ++; 
19917:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
19918:                     $depth --;
19919:                 }
19920:             }
19921:         }
19922:     }
19923:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19924:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
19925:         if ($putresult eq 'ok') {
19926:             my %title = (
19927:                          togglecats     => 'Show/Hide a course in catalog',
19928:                          categorize     => 'Assign a category to a course',
19929:                          togglecatscomm => 'Show/Hide a community in catalog',
19930:                          categorizecomm => 'Assign a category to a community',
19931:                         );
19932:             my %level = (
19933:                          dom  => 'set in Domain ("Modify Course/Community")',
19934:                          crs  => 'set in Course ("Course Configuration")',
19935:                          comm => 'set in Community ("Community Configuration")',
19936:                          none     => 'No catalog',
19937:                          std      => 'Standard catalog',
19938:                          domonly  => 'Domain-only catalog',
19939:                          codesrch => 'Code search form',
19940:                         );
19941:             $resulttext = &mt('Changes made:').'<ul>';
19942:             if ($changes{'togglecats'}) {
19943:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
19944:             }
19945:             if ($changes{'categorize'}) {
19946:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
19947:             }
19948:             if ($changes{'togglecatscomm'}) {
19949:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
19950:             }
19951:             if ($changes{'categorizecomm'}) {
19952:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
19953:             }
19954:             if ($changes{'unauth'}) {
19955:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
19956:             }
19957:             if ($changes{'auth'}) {
19958:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
19959:             }
19960:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19961:                 my $cathash;
19962:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
19963:                     $cathash = $domconfig{'coursecategories'}{'cats'};
19964:                 } else {
19965:                     $cathash = {};
19966:                 } 
19967:                 my (@cats,@trails,%allitems);
19968:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
19969:                 if (keys(%deletions) > 0) {
19970:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
19971:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
19972:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
19973:                     }
19974:                     $resulttext .= '</ul></li>';
19975:                 }
19976:                 if (keys(%reorderings) > 0) {
19977:                     my %sort_by_trail;
19978:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
19979:                     foreach my $key (keys(%reorderings)) {
19980:                         if ($allitems{$key} ne '') {
19981:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
19982:                         }
19983:                     }
19984:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
19985:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
19986:                     }
19987:                     $resulttext .= '</ul></li>';
19988:                 }
19989:                 if (keys(%adds) > 0) {
19990:                     my %sort_by_trail;
19991:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
19992:                     foreach my $key (keys(%adds)) {
19993:                         if ($allitems{$key} ne '') {
19994:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
19995:                         }
19996:                     }
19997:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
19998:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
19999:                     }
20000:                     $resulttext .= '</ul></li>';
20001:                 }
20002:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
20003:                 if (ref($lastactref) eq 'HASH') {
20004:                     $lastactref->{'cats'} = 1;
20005:                 }
20006:             }
20007:             $resulttext .= '</ul>';
20008:             if ($changes{'unauth'} || $changes{'auth'}) {
20009:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
20010:                 if ($changes{'auth'}) {
20011:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
20012:                 }
20013:                 if ($changes{'unauth'}) {
20014:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
20015:                 }
20016:                 my $cachetime = 24*60*60;
20017:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20018:                 if (ref($lastactref) eq 'HASH') {
20019:                     $lastactref->{'domdefaults'} = 1;
20020:                 }
20021:             }
20022:         } else {
20023:             $resulttext = '<span class="LC_error">'.
20024:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20025:         }
20026:     } else {
20027:         $resulttext = &mt('No changes made to course and community categories');
20028:     }
20029:     return $resulttext;
20030: }
20031: 
20032: sub modify_serverstatuses {
20033:     my ($dom,%domconfig) = @_;
20034:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
20035:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
20036:         %currserverstatus = %{$domconfig{'serverstatuses'}};
20037:     }
20038:     my @pages = &serverstatus_pages();
20039:     foreach my $type (@pages) {
20040:         $newserverstatus{$type}{'namedusers'} = '';
20041:         $newserverstatus{$type}{'machines'} = '';
20042:         if (defined($env{'form.'.$type.'_namedusers'})) {
20043:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
20044:             my @okusers;
20045:             foreach my $user (@users) {
20046:                 my ($uname,$udom) = split(/:/,$user);
20047:                 if (($udom =~ /^$match_domain$/) &&   
20048:                     (&Apache::lonnet::domain($udom)) &&
20049:                     ($uname =~ /^$match_username$/)) {
20050:                     if (!grep(/^\Q$user\E/,@okusers)) {
20051:                         push(@okusers,$user);
20052:                     }
20053:                 }
20054:             }
20055:             if (@okusers > 0) {
20056:                  @okusers = sort(@okusers);
20057:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
20058:             }
20059:         }
20060:         if (defined($env{'form.'.$type.'_machines'})) {
20061:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
20062:             my @okmachines;
20063:             foreach my $ip (@machines) {
20064:                 my @parts = split(/\./,$ip);
20065:                 next if (@parts < 4);
20066:                 my $badip = 0;
20067:                 for (my $i=0; $i<4; $i++) {
20068:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
20069:                         $badip = 1;
20070:                         last;
20071:                     }
20072:                 }
20073:                 if (!$badip) {
20074:                     push(@okmachines,$ip);     
20075:                 }
20076:             }
20077:             @okmachines = sort(@okmachines);
20078:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
20079:         }
20080:     }
20081:     my %serverstatushash =  (
20082:                                 serverstatuses => \%newserverstatus,
20083:                             );
20084:     foreach my $type (@pages) {
20085:         foreach my $setting ('namedusers','machines') {
20086:             my (@current,@new);
20087:             if (ref($currserverstatus{$type}) eq 'HASH') {
20088:                 if ($currserverstatus{$type}{$setting} ne '') { 
20089:                     @current = split(/,/,$currserverstatus{$type}{$setting});
20090:                 }
20091:             }
20092:             if ($newserverstatus{$type}{$setting} ne '') {
20093:                 @new = split(/,/,$newserverstatus{$type}{$setting});
20094:             }
20095:             if (@current > 0) {
20096:                 if (@new > 0) {
20097:                     foreach my $item (@current) {
20098:                         if (!grep(/^\Q$item\E$/,@new)) {
20099:                             $changes{$type}{$setting} = 1;
20100:                             last;
20101:                         }
20102:                     }
20103:                     foreach my $item (@new) {
20104:                         if (!grep(/^\Q$item\E$/,@current)) {
20105:                             $changes{$type}{$setting} = 1;
20106:                             last;
20107:                         }
20108:                     }
20109:                 } else {
20110:                     $changes{$type}{$setting} = 1;
20111:                 }
20112:             } elsif (@new > 0) {
20113:                 $changes{$type}{$setting} = 1;
20114:             }
20115:         }
20116:     }
20117:     if (keys(%changes) > 0) {
20118:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
20119:         my $putresult = &Apache::lonnet::put_dom('configuration',
20120:                                                  \%serverstatushash,$dom);
20121:         if ($putresult eq 'ok') {
20122:             $resulttext .= &mt('Changes made:').'<ul>';
20123:             foreach my $type (@pages) {
20124:                 if (ref($changes{$type}) eq 'HASH') {
20125:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
20126:                     if ($changes{$type}{'namedusers'}) {
20127:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
20128:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
20129:                         } else {
20130:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
20131:                         }
20132:                     }
20133:                     if ($changes{$type}{'machines'}) {
20134:                         if ($newserverstatus{$type}{'machines'} eq '') {
20135:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
20136:                         } else {
20137:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
20138:                         }
20139: 
20140:                     }
20141:                     $resulttext .= '</ul></li>';
20142:                 }
20143:             }
20144:             $resulttext .= '</ul>';
20145:         } else {
20146:             $resulttext = '<span class="LC_error">'.
20147:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
20148: 
20149:         }
20150:     } else {
20151:         $resulttext = &mt('No changes made to access to server status pages');
20152:     }
20153:     return $resulttext;
20154: }
20155: 
20156: sub modify_helpsettings {
20157:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
20158:     my ($resulttext,$errors,%changes,%helphash);
20159:     my %defaultchecked = ('submitbugs' => 'on');
20160:     my @offon = ('off','on');
20161:     my @toggles = ('submitbugs');
20162:     my %current = ('submitbugs' => '',
20163:                    'adhoc'      => {},
20164:                   );
20165:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
20166:         %current = %{$domconfig{'helpsettings'}};
20167:     }
20168:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20169:     foreach my $item (@toggles) {
20170:         if ($defaultchecked{$item} eq 'on') { 
20171:             if ($current{$item} eq '') {
20172:                 if ($env{'form.'.$item} eq '0') {
20173:                     $changes{$item} = 1;
20174:                 }
20175:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20176:                 $changes{$item} = 1;
20177:             }
20178:         } elsif ($defaultchecked{$item} eq 'off') {
20179:             if ($current{$item} eq '') {
20180:                 if ($env{'form.'.$item} eq '1') {
20181:                     $changes{$item} = 1;
20182:                 }
20183:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20184:                 $changes{$item} = 1;
20185:             }
20186:         }
20187:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
20188:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
20189:         }
20190:     }
20191:     my $maxnum = $env{'form.helproles_maxnum'};
20192:     my $confname = $dom.'-domainconfig';
20193:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
20194:     my (@allpos,%newsettings,%changedprivs,$newrole);
20195:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
20196:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
20197:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
20198:     my %lt = &Apache::lonlocal::texthash(
20199:                     s      => 'system',
20200:                     d      => 'domain',
20201:                     order  => 'Display order',
20202:                     access => 'Role usage',
20203:                     all    => 'All with domain helpdesk or helpdesk assistant role',
20204:                     dh     => 'All with domain helpdesk role',
20205:                     da     => 'All with domain helpdesk assistant role',
20206:                     none   => 'None',
20207:                     status => 'Determined based on institutional status',
20208:                     inc    => 'Include all, but exclude specific personnel',
20209:                     exc    => 'Exclude all, but include specific personnel',
20210:     );
20211:     for (my $num=0; $num<=$maxnum; $num++) {
20212:         my ($prefix,$identifier,$rolename,%curr);
20213:         if ($num == $maxnum) {
20214:             next unless ($env{'form.newcusthelp'} == $maxnum);
20215:             $identifier = 'custhelp'.$num;
20216:             $prefix = 'helproles_'.$num;
20217:             $rolename = $env{'form.custhelpname'.$num};
20218:             $rolename=~s/[^A-Za-z0-9]//gs;
20219:             next if ($rolename eq '');
20220:             next if (exists($existing{'rolesdef_'.$rolename}));
20221:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
20222:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
20223:                                                      $newprivs{'c'},$confname,$dom);
20224:             if ($result ne 'ok') {
20225:                 $errors .= '<li><span class="LC_error">'.
20226:                            &mt('An error occurred storing the new custom role: [_1]',
20227:                            $result).'</span></li>';
20228:                 next;
20229:             } else {
20230:                 $changedprivs{$rolename} = \%newprivs;
20231:                 $newrole = $rolename;
20232:             }
20233:         } else {
20234:             $prefix = 'helproles_'.$num;
20235:             $rolename = $env{'form.'.$prefix};
20236:             next if ($rolename eq '');
20237:             next unless (exists($existing{'rolesdef_'.$rolename}));
20238:             $identifier = 'custhelp'.$num;
20239:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
20240:             my %currprivs;
20241:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
20242:                 split(/\_/,$existing{'rolesdef_'.$rolename});
20243:             foreach my $level ('c','d','s') {
20244:                 if ($newprivs{$level} ne $currprivs{$level}) {
20245:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
20246:                                                              $newprivs{'c'},$confname,$dom);
20247:                     if ($result ne 'ok') {
20248:                         $errors .= '<li><span class="LC_error">'.
20249:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
20250:                                        $rolename,$result).'</span></li>';
20251:                     } else {
20252:                         $changedprivs{$rolename} = \%newprivs;
20253:                     }
20254:                     last;
20255:                 }
20256:             }
20257:             if (ref($current{'adhoc'}) eq 'HASH') {
20258:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
20259:                     %curr = %{$current{'adhoc'}{$rolename}};
20260:                 }
20261:             }
20262:         }
20263:         my $newpos = $env{'form.'.$prefix.'_pos'};
20264:         $newpos =~ s/\D+//g;
20265:         $allpos[$newpos] = $rolename;
20266:         my $newdesc = $env{'form.'.$prefix.'_desc'};
20267:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
20268:         if ($curr{'desc'}) {
20269:             if ($curr{'desc'} ne $newdesc) {
20270:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
20271:                 $newsettings{$rolename}{'desc'} = $newdesc;
20272:             }
20273:         } elsif ($newdesc ne '') {
20274:             $changes{'customrole'}{$rolename}{'desc'} = 1;
20275:             $newsettings{$rolename}{'desc'} = $newdesc;
20276:         }
20277:         my $access = $env{'form.'.$prefix.'_access'};
20278:         if (grep(/^\Q$access\E$/,@accesstypes)) {
20279:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
20280:             if ($access eq 'status') {
20281:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
20282:                 if (scalar(@statuses) == 0) {
20283:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
20284:                 } else {
20285:                     my (@shownstatus,$numtypes);
20286:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
20287:                     if (ref($types) eq 'ARRAY') {
20288:                         $numtypes = scalar(@{$types});
20289:                         foreach my $type (sort(@statuses)) {
20290:                             if ($type eq 'default') {
20291:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
20292:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
20293:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
20294:                                 push(@shownstatus,$usertypes->{$type});
20295:                             }
20296:                         }
20297:                     }
20298:                     if (grep(/^default$/,@statuses)) {
20299:                         push(@shownstatus,$othertitle);
20300:                     }
20301:                     if (scalar(@shownstatus) == 1+$numtypes) {
20302:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
20303:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
20304:                     } else {
20305:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
20306:                         if (ref($curr{'status'}) eq 'ARRAY') {
20307:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
20308:                             if (@diffs) {
20309:                                 $changes{'customrole'}{$rolename}{$access} = 1;
20310:                             }
20311:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20312:                             $changes{'customrole'}{$rolename}{$access} = 1;
20313:                         }
20314:                     }
20315:                 }
20316:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
20317:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
20318:                 my @newspecstaff;
20319:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
20320:                 foreach my $person (sort(@personnel)) {
20321:                     if ($domhelpdesk{$person}) {
20322:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
20323:                     }
20324:                 }
20325:                 if (ref($curr{$access}) eq 'ARRAY') {
20326:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
20327:                     if (@diffs) {
20328:                         $changes{'customrole'}{$rolename}{$access} = 1;
20329:                     }
20330:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20331:                     $changes{'customrole'}{$rolename}{$access} = 1;
20332:                 }
20333:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20334:                     my ($uname,$udom) = split(/:/,$person);
20335:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
20336:                 }
20337:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
20338:             }
20339:         } else {
20340:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
20341:         }
20342:         unless ($curr{'access'} eq $access) {
20343:             $changes{'customrole'}{$rolename}{'access'} = 1;
20344:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
20345:         }
20346:     }
20347:     if (@allpos > 0) {
20348:         my $idx = 0;
20349:         foreach my $rolename (@allpos) {
20350:             if ($rolename ne '') {
20351:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
20352:                 if (ref($current{'adhoc'}) eq 'HASH') {
20353:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
20354:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
20355:                             $changes{'customrole'}{$rolename}{'order'} = 1;
20356:                             $newsettings{$rolename}{'order'} = $idx+1;
20357:                         }
20358:                     }
20359:                 }
20360:                 $idx ++;
20361:             }
20362:         }
20363:     }
20364:     my $putresult;
20365:     if (keys(%changes) > 0) {
20366:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
20367:         if ($putresult eq 'ok') {
20368:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
20369:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
20370:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
20371:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
20372:                 }
20373:             }
20374:             my $cachetime = 24*60*60;
20375:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20376:             if (ref($lastactref) eq 'HASH') {
20377:                 $lastactref->{'domdefaults'} = 1;
20378:             }
20379:         } else {
20380:             $errors .= '<li><span class="LC_error">'.
20381:                        &mt('An error occurred storing the settings: [_1]',
20382:                            $putresult).'</span></li>';
20383:         }
20384:     }
20385:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
20386:         $resulttext = &mt('Changes made:').'<ul>';
20387:         my (%shownprivs,@levelorder);
20388:         @levelorder = ('c','d','s');
20389:         if ((keys(%changes)) && ($putresult eq 'ok')) {
20390:             foreach my $item (sort(keys(%changes))) {
20391:                 if ($item eq 'submitbugs') {
20392:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
20393:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
20394:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
20395:                 } elsif ($item eq 'customrole') {
20396:                     if (ref($changes{'customrole'}) eq 'HASH') {
20397:                         my @keyorder = ('order','desc','access','status','exc','inc');
20398:                         my %keytext = &Apache::lonlocal::texthash(
20399:                                                                    order  => 'Order',
20400:                                                                    desc   => 'Role description',
20401:                                                                    access => 'Role usage',
20402:                                                                    status => 'Allowed institutional types',
20403:                                                                    exc    => 'Allowed personnel',
20404:                                                                    inc    => 'Disallowed personnel',
20405:                         );
20406:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
20407:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
20408:                                 if ($role eq $newrole) {
20409:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
20410:                                                               $role).'<ul>';
20411:                                 } else {
20412:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
20413:                                                               $role).'<ul>';
20414:                                 }
20415:                                 foreach my $key (@keyorder) {
20416:                                     if ($changes{'customrole'}{$role}{$key}) {
20417:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
20418:                                                                   $keytext{$key},$newsettings{$role}{$key}).
20419:                                                        '</li>';
20420:                                     }
20421:                                 }
20422:                                 if (ref($changedprivs{$role}) eq 'HASH') {
20423:                                     $shownprivs{$role} = 1;
20424:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
20425:                                     foreach my $level (@levelorder) {
20426:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
20427:                                             next if ($item eq '');
20428:                                             my ($priv) = split(/\&/,$item,2);
20429:                                             if (&Apache::lonnet::plaintext($priv)) {
20430:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
20431:                                                 unless ($level eq 'c') {
20432:                                                     $resulttext .= ' ('.$lt{$level}.')';
20433:                                                 }
20434:                                                 $resulttext .= '</li>';
20435:                                             }
20436:                                         }
20437:                                     }
20438:                                     $resulttext .= '</ul>';
20439:                                 }
20440:                                 $resulttext .= '</ul></li>';
20441:                             }
20442:                         }
20443:                     }
20444:                 }
20445:             }
20446:         }
20447:         if (keys(%changedprivs)) {
20448:             foreach my $role (sort(keys(%changedprivs))) {
20449:                 unless ($shownprivs{$role}) {
20450:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
20451:                                               $role).'<ul>'.
20452:                                    '<li>'.&mt('Privileges set to :').'<ul>';
20453:                     foreach my $level (@levelorder) {
20454:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
20455:                             next if ($item eq '');
20456:                             my ($priv) = split(/\&/,$item,2);
20457:                             if (&Apache::lonnet::plaintext($priv)) {
20458:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
20459:                                 unless ($level eq 'c') {
20460:                                     $resulttext .= ' ('.$lt{$level}.')';
20461:                                 }
20462:                                 $resulttext .= '</li>';
20463:                             }
20464:                         }
20465:                     }
20466:                     $resulttext .= '</ul></li></ul></li>';
20467:                 }
20468:             }
20469:         }
20470:         $resulttext .= '</ul>';
20471:     } else {
20472:         $resulttext = &mt('No changes made to help settings');
20473:     }
20474:     if ($errors) {
20475:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
20476:                                     $errors.'</ul>';
20477:     }
20478:     return $resulttext;
20479: }
20480: 
20481: sub modify_coursedefaults {
20482:     my ($dom,$lastactref,%domconfig) = @_;
20483:     my ($resulttext,$errors,%changes,%defaultshash);
20484:     my %defaultchecked = (
20485:                            'canuse_pdfforms' => 'off',
20486:                            'uselcmath'       => 'on',
20487:                            'usejsme'         => 'on',
20488:                            'inline_chem'     => 'on',
20489:                            'ltiauth'         => 'off',
20490:                          );
20491:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem','ltiauth');
20492:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
20493:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
20494:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
20495:                    'mysqltables_textbook','mysqltables_placement');
20496:     my @types = ('official','unofficial','community','textbook','placement');
20497:     my %staticdefaults = (
20498:                            anonsurvey_threshold => 10,
20499:                            uploadquota          => 500,
20500:                            postsubmit           => 60,
20501:                            mysqltables          => 172800,
20502:                            domexttool           => 1,
20503:                          );
20504:     my %texoptions = (
20505:                         MathJax  => 'MathJax',
20506:                         mimetex  => &mt('Convert to Images'),
20507:                         tth      => &mt('TeX to HTML'),
20508:                      );
20509:     $defaultshash{'coursedefaults'} = {};
20510: 
20511:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
20512:         if ($domconfig{'coursedefaults'} eq '') {
20513:             $domconfig{'coursedefaults'} = {};
20514:         }
20515:     }
20516: 
20517:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
20518:         foreach my $item (@toggles) {
20519:             if ($defaultchecked{$item} eq 'on') {
20520:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
20521:                     ($env{'form.'.$item} eq '0')) {
20522:                     $changes{$item} = 1;
20523:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
20524:                     $changes{$item} = 1;
20525:                 }
20526:             } elsif ($defaultchecked{$item} eq 'off') {
20527:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
20528:                     ($env{'form.'.$item} eq '1')) {
20529:                     $changes{$item} = 1;
20530:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
20531:                     $changes{$item} = 1;
20532:                 }
20533:             }
20534:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
20535:         }
20536:         foreach my $item (@numbers) {
20537:             my ($currdef,$newdef);
20538:             $newdef = $env{'form.'.$item};
20539:             if ($item eq 'anonsurvey_threshold') {
20540:                 $currdef = $domconfig{'coursedefaults'}{$item};
20541:                 $newdef =~ s/\D//g;
20542:                 if ($newdef eq '' || $newdef < 1) {
20543:                     $newdef = 1;
20544:                 }
20545:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
20546:             } else {
20547:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
20548:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
20549:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
20550:                 }
20551:                 $newdef =~ s/[^\w.\-]//g;
20552:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
20553:             }
20554:             if ($currdef ne $newdef) {
20555:                 if ($item eq 'anonsurvey_threshold') {
20556:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
20557:                         $changes{$item} = 1;
20558:                     }
20559:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
20560:                     my $setting = $1;
20561:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
20562:                         $changes{$setting} = 1;
20563:                     }
20564:                 }
20565:             }
20566:         }
20567:         my $texengine;
20568:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
20569:             $texengine = $env{'form.texengine'};
20570:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
20571:             if ($currdef eq '') {
20572:                 unless ($texengine eq $Apache::lonnet::deftex) {
20573:                     $changes{'texengine'} = 1;
20574:                 }
20575:             } elsif ($currdef ne $texengine) {
20576:                 $changes{'texengine'} = 1;
20577:             }
20578:         }
20579:         if ($texengine ne '') {
20580:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
20581:         }
20582:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
20583:         my @currclonecode;
20584:         if (ref($currclone) eq 'HASH') {
20585:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
20586:                 @currclonecode = @{$currclone->{'instcode'}};
20587:             }
20588:         }
20589:         my $newclone;
20590:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
20591:             $newclone = $env{'form.canclone'};
20592:         }
20593:         if ($newclone eq 'instcode') {
20594:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
20595:             my (%codedefaults,@code_order,@clonecode);
20596:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
20597:                                                     \@code_order);
20598:             foreach my $item (@code_order) {
20599:                 if (grep(/^\Q$item\E$/,@newcodes)) {
20600:                     push(@clonecode,$item);
20601:                 }
20602:             }
20603:             if (@clonecode) {
20604:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
20605:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
20606:                 if (@diffs) {
20607:                     $changes{'canclone'} = 1;
20608:                 }
20609:             } else {
20610:                 $newclone eq '';
20611:             }
20612:         } elsif ($newclone ne '') {
20613:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
20614:         }
20615:         if ($newclone ne $currclone) {
20616:             $changes{'canclone'} = 1;
20617:         }
20618:         my %credits;
20619:         foreach my $type (@types) {
20620:             unless ($type eq 'community') {
20621:                 $credits{$type} = $env{'form.'.$type.'_credits'};
20622:                 $credits{$type} =~ s/[^\d.]+//g;
20623:             }
20624:         }
20625:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
20626:             ($env{'form.coursecredits'} eq '1')) {
20627:             $changes{'coursecredits'} = 1;
20628:             foreach my $type (keys(%credits)) {
20629:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
20630:             }
20631:         } else {
20632:             if ($env{'form.coursecredits'} eq '1') {
20633:                 foreach my $type (@types) {
20634:                     unless ($type eq 'community') {
20635:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
20636:                             $changes{'coursecredits'} = 1;
20637:                         }
20638:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
20639:                     }
20640:                 }
20641:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
20642:                 foreach my $type (@types) {
20643:                     unless ($type eq 'community') {
20644:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
20645:                             $changes{'coursecredits'} = 1;
20646:                             last;
20647:                         }
20648:                     }
20649:                 }
20650:             }
20651:         }
20652:         if ($env{'form.postsubmit'} eq '1') {
20653:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
20654:             my %currtimeout;
20655:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20656:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
20657:                     $changes{'postsubmit'} = 1;
20658:                 }
20659:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20660:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
20661:                 }
20662:             } else {
20663:                 $changes{'postsubmit'} = 1;
20664:             }
20665:             foreach my $type (@types) {
20666:                 my $timeout = $env{'form.'.$type.'_timeout'};
20667:                 $timeout =~ s/\D//g;
20668:                 if ($timeout == $staticdefaults{'postsubmit'}) {
20669:                     $timeout = '';
20670:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
20671:                     $timeout = '0';
20672:                 }
20673:                 unless ($timeout eq '') {
20674:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
20675:                 }
20676:                 if (exists($currtimeout{$type})) {
20677:                     if ($timeout ne $currtimeout{$type}) {
20678:                         $changes{'postsubmit'} = 1;
20679:                     }
20680:                 } elsif ($timeout ne '') {
20681:                     $changes{'postsubmit'} = 1;
20682:                 }
20683:             }
20684:         } else {
20685:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
20686:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20687:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
20688:                     $changes{'postsubmit'} = 1;
20689:                 }
20690:             } else {
20691:                 $changes{'postsubmit'} = 1;
20692:             }
20693:         }
20694:         my (%newdomexttool,%newexttool,%olddomexttool,%oldexttool);
20695:         map { $newdomexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.domexttool');
20696:         map { $newexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.exttool');
20697:         if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
20698:             %olddomexttool = %{$domconfig{'coursedefaults'}{'domexttool'}};
20699:         } else {
20700:            foreach my $type (@types) {
20701:                if ($staticdefaults{'domexttool'}) {
20702:                    $olddomexttool{$type} = 1;
20703:                } else {
20704:                    $olddomexttool{$type} = 0;
20705:                }
20706:             }
20707:         }
20708:         if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
20709:             %oldexttool = %{$domconfig{'coursedefaults'}{'exttool'}};
20710:         } else {
20711:             foreach my $type (@types) {
20712:                if ($staticdefaults{'exttool'}) {
20713:                    $oldexttool{$type} = 1;
20714:                } else {
20715:                    $oldexttool{$type} = 0;
20716:                }
20717:             }
20718:         }
20719:         foreach my $type (@types) {
20720:             unless ($newdomexttool{$type}) {
20721:                 $newdomexttool{$type} = 0;
20722:             }
20723:             unless ($newexttool{$type}) {
20724:                 $newexttool{$type} = 0;
20725:             }
20726:             if ($newdomexttool{$type} != $olddomexttool{$type}) {
20727:                 $changes{'domexttool'} = 1;
20728:             }
20729:             if ($newexttool{$type} != $oldexttool{$type}) {
20730:                 $changes{'exttool'} = 1;
20731:             }
20732:         }
20733:         $defaultshash{'coursedefaults'}{'domexttool'} = \%newdomexttool;
20734:         $defaultshash{'coursedefaults'}{'exttool'} = \%newexttool;
20735:     }
20736:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20737:                                              $dom);
20738:     if ($putresult eq 'ok') {
20739:         if (keys(%changes) > 0) {
20740:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20741:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
20742:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
20743:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'}) ||
20744:                 ($changes{'inline_chem'}) || ($changes{'ltiauth'}) || ($changes{'domexttool'}) ||
20745:                 ($changes{'exttool'}) ) {
20746:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','inline_chem','texengine',
20747:                                   'ltiauth') {
20748:                     if ($changes{$item}) {
20749:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
20750:                     }
20751:                 }
20752:                 if ($changes{'coursecredits'}) {
20753:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
20754:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
20755:                             $domdefaults{$type.'credits'} =
20756:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
20757:                         }
20758:                     }
20759:                 }
20760:                 if ($changes{'postsubmit'}) {
20761:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20762:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
20763:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20764:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
20765:                                 $domdefaults{$type.'postsubtimeout'} =
20766:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
20767:                             }
20768:                         }
20769:                     }
20770:                 }
20771:                 if ($changes{'uploadquota'}) {
20772:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
20773:                         foreach my $type (@types) {
20774:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
20775:                         }
20776:                     }
20777:                 }
20778:                 if ($changes{'canclone'}) {
20779:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
20780:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
20781:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
20782:                             if (@clonecodes) {
20783:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
20784:                             }
20785:                         }
20786:                     } else {
20787:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
20788:                     }
20789:                 }
20790:                 if ($changes{'domexttool'}) {
20791:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
20792:                         foreach my $type (@types) {
20793:                             $domdefaults{$type.'domexttool'}=$defaultshash{'coursedefaults'}{'domexttool'}{$type};
20794:                         }
20795:                     }
20796:                 }
20797:                 if ($changes{'exttool'}) {
20798:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
20799:                         foreach my $type (@types) {
20800:                             $domdefaults{$type.'exttool'}=$defaultshash{'coursedefaults'}{'exttool'}{$type};
20801:                         }
20802:                     }
20803:                 }
20804:                 my $cachetime = 24*60*60;
20805:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20806:                 if (ref($lastactref) eq 'HASH') {
20807:                     $lastactref->{'domdefaults'} = 1;
20808:                 }
20809:             }
20810:             $resulttext = &mt('Changes made:').'<ul>';
20811:             foreach my $item (sort(keys(%changes))) {
20812:                 if ($item eq 'canuse_pdfforms') {
20813:                     if ($env{'form.'.$item} eq '1') {
20814:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
20815:                     } else {
20816:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
20817:                     }
20818:                 } elsif ($item eq 'uselcmath') {
20819:                     if ($env{'form.'.$item} eq '1') {
20820:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
20821:                     } else {
20822:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
20823:                     }
20824:                 } elsif ($item eq 'usejsme') {
20825:                     if ($env{'form.'.$item} eq '1') {
20826:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
20827:                     } else {
20828:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
20829:                     }
20830:                 } elsif ($item eq 'inline_chem') {
20831:                     if ($env{'form.'.$item} eq '1') {
20832:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses inline previewer').'</li>';
20833:                     } else {
20834:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses pop-up previewer').'</li>';
20835:                     }
20836:                 } elsif ($item eq 'texengine') {
20837:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
20838:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
20839:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
20840:                     }
20841:                 } elsif ($item eq 'anonsurvey_threshold') {
20842:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
20843:                 } elsif ($item eq 'uploadquota') {
20844:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
20845:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
20846:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
20847:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
20848:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
20849:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
20850:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
20851:                                        '</ul>'.
20852:                                        '</li>';
20853:                     } else {
20854:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
20855:                     }
20856:                 } elsif ($item eq 'mysqltables') {
20857:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
20858:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
20859:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
20860:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
20861:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
20862:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
20863:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
20864:                                        '</ul>'.
20865:                                        '</li>';
20866:                     } else {
20867:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
20868:                     }
20869:                 } elsif ($item eq 'postsubmit') {
20870:                     if ($domdefaults{'postsubmit'} eq 'off') {
20871:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
20872:                     } else {
20873:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
20874:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20875:                             $resulttext .= &mt('durations:').'<ul>';
20876:                             foreach my $type (@types) {
20877:                                 $resulttext .= '<li>';
20878:                                 my $timeout;
20879:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20880:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
20881:                                 }
20882:                                 my $display;
20883:                                 if ($timeout eq '0') {
20884:                                     $display = &mt('unlimited');
20885:                                 } elsif ($timeout eq '') {
20886:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
20887:                                 } else {
20888:                                     $display = &mt('[quant,_1,second]',$timeout);
20889:                                 }
20890:                                 if ($type eq 'community') {
20891:                                     $resulttext .= &mt('Communities');
20892:                                 } elsif ($type eq 'official') {
20893:                                     $resulttext .= &mt('Official courses');
20894:                                 } elsif ($type eq 'unofficial') {
20895:                                     $resulttext .= &mt('Unofficial courses');
20896:                                 } elsif ($type eq 'textbook') {
20897:                                     $resulttext .= &mt('Textbook courses');
20898:                                 } elsif ($type eq 'placement') {
20899:                                     $resulttext .= &mt('Placement tests');
20900:                                 }
20901:                                 $resulttext .= ' -- '.$display.'</li>';
20902:                             }
20903:                             $resulttext .= '</ul>';
20904:                         }
20905:                         $resulttext .= '</li>';
20906:                     }
20907:                 } elsif ($item eq 'coursecredits') {
20908:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
20909:                         if (($domdefaults{'officialcredits'} eq '') &&
20910:                             ($domdefaults{'unofficialcredits'} eq '') &&
20911:                             ($domdefaults{'textbookcredits'} eq '')) {
20912:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
20913:                         } else {
20914:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
20915:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
20916:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
20917:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
20918:                                            '</ul>'.
20919:                                            '</li>';
20920:                         }
20921:                     } else {
20922:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
20923:                     }
20924:                 } elsif ($item eq 'canclone') {
20925:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
20926:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
20927:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
20928:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
20929:                         }
20930:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
20931:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
20932:                     } else {
20933:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
20934:                     }
20935:                 } elsif ($item eq 'ltiauth') {
20936:                     if ($env{'form.'.$item} eq '1') {
20937:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL need not require re-authentication').'</li>';
20938:                     } else {
20939:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL will require re-authentication').'</li>';
20940:                     }
20941:                 } elsif ($item eq 'domexttool') {
20942:                     my @noyes = (&mt('no'),&mt('yes'));
20943:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
20944:                         $resulttext .= '<li>'.&mt('External Tools defined in the domain may be used as follows:').'<ul>'.
20945:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'official'}].'</b>').'</li>'.
20946:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'unofficial'}].'</b>').'</li>'.
20947:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'textbook'}].'</b>').'</li>'.
20948:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'placement'}].'</b>').'</li>'.
20949:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'community'}].'</b>').'</li>'.
20950:                                        '</ul>'.
20951:                                        '</li>';
20952:                     } else {
20953:                         $resulttext .= '<li>'.&mt('External Tools defined in the domain may be used in all course types, by default').'</li>';
20954:                     }
20955:                 } elsif ($item eq 'exttool') {
20956:                     my @noyes = (&mt('no'),&mt('yes'));
20957:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
20958:                         $resulttext .= '<li>'.&mt('External Tools can be defined and configured in course containers as follows:').'<ul>'.
20959:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'official'}].'</b>').'</li>'.
20960:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'unofficial'}].'</b>').'</li>'.
20961:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'textbook'}].'</b>').'</li>'.
20962:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'placement'}].'</b>').'</li>'.
20963:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'community'}].'</b>').'</li>'.
20964:                                        '</ul>'.
20965:                                        '</li>';
20966:                     } else {
20967:                         $resulttext .= '<li>'.&mt('External Tools can not be defined in any course types, by default').'</li>';
20968:                     }
20969:                 }
20970:             }
20971:             $resulttext .= '</ul>';
20972:         } else {
20973:             $resulttext = &mt('No changes made to course defaults');
20974:         }
20975:     } else {
20976:         $resulttext = '<span class="LC_error">'.
20977:             &mt('An error occurred: [_1]',$putresult).'</span>';
20978:     }
20979:     return $resulttext;
20980: }
20981: 
20982: sub modify_selfenrollment {
20983:     my ($dom,$lastactref,%domconfig) = @_;
20984:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
20985:     my @types = ('official','unofficial','community','textbook','placement');
20986:     my %titles = &tool_titles();
20987:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
20988:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
20989:     $ordered{'default'} = ['types','registered','approval','limit'];
20990: 
20991:     my (%roles,%shown,%toplevel);
20992:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
20993: 
20994:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
20995:         if ($domconfig{'selfenrollment'} eq '') {
20996:             $domconfig{'selfenrollment'} = {};
20997:         }
20998:     }
20999:     %toplevel = (
21000:                   admin      => 'Configuration Rights',
21001:                   default    => 'Default settings',
21002:                   validation => 'Validation of self-enrollment requests',
21003:                 );
21004:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
21005: 
21006:     if (ref($ordered{'admin'}) eq 'ARRAY') {
21007:         foreach my $item (@{$ordered{'admin'}}) {
21008:             foreach my $type (@types) {
21009:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
21010:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
21011:                 } else {
21012:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
21013:                 }
21014:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
21015:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
21016:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
21017:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
21018:                             push(@{$changes{'admin'}{$type}},$item);
21019:                         }
21020:                     } else {
21021:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
21022:                             push(@{$changes{'admin'}{$type}},$item);
21023:                         }
21024:                     }
21025:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
21026:                     push(@{$changes{'admin'}{$type}},$item);
21027:                 }
21028:             }
21029:         }
21030:     }
21031: 
21032:     foreach my $item (@{$ordered{'default'}}) {
21033:         foreach my $type (@types) {
21034:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
21035:             if ($item eq 'types') {
21036:                 unless (($value eq 'all') || ($value eq 'dom')) {
21037:                     $value = '';
21038:                 }
21039:             } elsif ($item eq 'registered') {
21040:                 unless ($value eq '1') {
21041:                     $value = 0;
21042:                 }
21043:             } elsif ($item eq 'approval') {
21044:                 unless ($value =~ /^[012]$/) {
21045:                     $value = 0;
21046:                 }
21047:             } else {
21048:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
21049:                     $value = 'none';
21050:                 }
21051:             }
21052:             $selfenrollhash{'default'}{$type}{$item} = $value;
21053:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
21054:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
21055:                     if ($selfenrollhash{'default'}{$type}{$item} ne
21056:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
21057:                          push(@{$changes{'default'}{$type}},$item);
21058:                     }
21059:                 } else {
21060:                     push(@{$changes{'default'}{$type}},$item);
21061:                 }
21062:             } else {
21063:                 push(@{$changes{'default'}{$type}},$item);
21064:             }
21065:             if ($item eq 'limit') {
21066:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
21067:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
21068:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
21069:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
21070:                     }
21071:                 } else {
21072:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
21073:                 }
21074:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
21075:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
21076:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
21077:                          push(@{$changes{'default'}{$type}},'cap');
21078:                     }
21079:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
21080:                     push(@{$changes{'default'}{$type}},'cap');
21081:                 }
21082:             }
21083:         }
21084:     }
21085: 
21086:     foreach my $item (@{$itemsref}) {
21087:         if ($item eq 'fields') {
21088:             my @changed;
21089:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
21090:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
21091:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
21092:             }
21093:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
21094:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
21095:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
21096:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
21097:                 } else {
21098:                     @changed = @{$selfenrollhash{'validation'}{$item}};
21099:                 }
21100:             } else {
21101:                 @changed = @{$selfenrollhash{'validation'}{$item}};
21102:             }
21103:             if (@changed) {
21104:                 if ($selfenrollhash{'validation'}{$item}) { 
21105:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
21106:                 } else {
21107:                     $changes{'validation'}{$item} = &mt('None');
21108:                 }
21109:             }
21110:         } else {
21111:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
21112:             if ($item eq 'markup') {
21113:                if ($env{'form.selfenroll_validation_'.$item}) {
21114:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
21115:                }
21116:             }
21117:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
21118:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
21119:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
21120:                 }
21121:             }
21122:         }
21123:     }
21124: 
21125:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
21126:                                              $dom);
21127:     if ($putresult eq 'ok') {
21128:         if (keys(%changes) > 0) {
21129:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21130:             $resulttext = &mt('Changes made:').'<ul>';
21131:             foreach my $key ('admin','default','validation') {
21132:                 if (ref($changes{$key}) eq 'HASH') {
21133:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
21134:                     if ($key eq 'validation') {
21135:                         foreach my $item (@{$itemsref}) {
21136:                             if (exists($changes{$key}{$item})) {
21137:                                 if ($item eq 'markup') {
21138:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
21139:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
21140:                                 } else {  
21141:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
21142:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
21143:                                 }
21144:                             }
21145:                         }
21146:                     } else {
21147:                         foreach my $type (@types) {
21148:                             if ($type eq 'community') {
21149:                                 $roles{'1'} = &mt('Community personnel');
21150:                             } else {
21151:                                 $roles{'1'} = &mt('Course personnel');
21152:                             }
21153:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
21154:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
21155:                                     if ($key eq 'admin') {
21156:                                         my @mgrdc = ();
21157:                                         if (ref($ordered{$key}) eq 'ARRAY') {
21158:                                             foreach my $item (@{$ordered{'admin'}}) {
21159:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
21160:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
21161:                                                         push(@mgrdc,$item);
21162:                                                     }
21163:                                                 }
21164:                                             }
21165:                                             if (@mgrdc) {
21166:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
21167:                                             } else {
21168:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
21169:                                             }
21170:                                         }
21171:                                     } else {
21172:                                         if (ref($ordered{$key}) eq 'ARRAY') {
21173:                                             foreach my $item (@{$ordered{$key}}) {
21174:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
21175:                                                     $domdefaults{$type.'selfenroll'.$item} =
21176:                                                         $selfenrollhash{$key}{$type}{$item};
21177:                                                 }
21178:                                             }
21179:                                         }
21180:                                     }
21181:                                 }
21182:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
21183:                                 foreach my $item (@{$ordered{$key}}) {
21184:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
21185:                                         $resulttext .= '<li>';
21186:                                         if ($key eq 'admin') {
21187:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
21188:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
21189:                                         } else {
21190:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
21191:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
21192:                                         }
21193:                                         $resulttext .= '</li>';
21194:                                     }
21195:                                 }
21196:                                 $resulttext .= '</ul></li>';
21197:                             }
21198:                         }
21199:                         $resulttext .= '</ul></li>'; 
21200:                     }
21201:                 }
21202:             }
21203:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
21204:                 my $cachetime = 24*60*60;
21205:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21206:                 if (ref($lastactref) eq 'HASH') {
21207:                     $lastactref->{'domdefaults'} = 1;
21208:                 }
21209:             }
21210:             $resulttext .= '</ul>';
21211:         } else {
21212:             $resulttext = &mt('No changes made to self-enrollment settings');
21213:         }
21214:     } else {
21215:         $resulttext = '<span class="LC_error">'.
21216:             &mt('An error occurred: [_1]',$putresult).'</span>';
21217:     }
21218:     return $resulttext;
21219: }
21220: 
21221: sub modify_wafproxy {
21222:     my ($dom,$action,$lastactref,%domconfig) = @_;
21223:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21224:     my (%othercontrol,%canset,%values,%curralias,%currsaml,%currvalue,@warnings,
21225:         %wafproxy,%changes,%expirecache,%expiresaml);
21226:     foreach my $server (sort(keys(%servers))) {
21227:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
21228:         if ($serverhome eq $server) {
21229:             my $serverdom = &Apache::lonnet::host_domain($server);
21230:             if ($serverdom eq $dom) {
21231:                 $canset{$server} = 1;
21232:             }
21233:         }
21234:     }
21235:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
21236:         %{$values{$dom}} = ();
21237:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
21238:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
21239:         }
21240:         if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
21241:             %currsaml = %{$domconfig{'wafproxy'}{'saml'}};
21242:         }
21243:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
21244:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
21245:         }
21246:     }
21247:     my $output;
21248:     if (keys(%canset)) {
21249:         %{$wafproxy{'alias'}} = ();
21250:         %{$wafproxy{'saml'}} = ();
21251:         foreach my $key (sort(keys(%canset))) {
21252:             if ($env{'form.wafproxy_'.$dom}) {
21253:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
21254:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
21255:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
21256:                     $changes{'alias'} = 1;
21257:                 }
21258:                 if ($env{'form.wafproxy_alias_saml_'.$key}) {
21259:                     $wafproxy{'saml'}{$key} = 1;
21260:                 }
21261:                 if ($wafproxy{'saml'}{$key} ne $currsaml{$key}) {
21262:                     $changes{'saml'} = 1;
21263:                 }
21264:             } else {
21265:                 $wafproxy{'alias'}{$key} = '';
21266:                 $wafproxy{'saml'}{$key} = '';
21267:                 if ($curralias{$key}) {
21268:                     $changes{'alias'} = 1;
21269:                 }
21270:                 if ($currsaml{$key}) {
21271:                     $changes{'saml'} = 1;
21272:                 }
21273:             }
21274:             if ($wafproxy{'alias'}{$key} eq '') {
21275:                 if ($curralias{$key}) {
21276:                     $expirecache{$key} = 1;
21277:                 }
21278:                 delete($wafproxy{'alias'}{$key});
21279:             }
21280:             if ($wafproxy{'saml'}{$key} eq '') {
21281:                 if ($currsaml{$key}) {
21282:                     $expiresaml{$key} = 1;
21283:                 }
21284:                 delete($wafproxy{'saml'}{$key});
21285:             }
21286:         }
21287:         unless (keys(%{$wafproxy{'alias'}})) {
21288:             delete($wafproxy{'alias'});
21289:         }
21290:         unless (keys(%{$wafproxy{'saml'}})) {
21291:             delete($wafproxy{'saml'});
21292:         }
21293:         # Localization for values in %warn occurs in &mt() calls separately.
21294:         my %warn = (
21295:                      trusted => 'trusted IP range(s)',
21296:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
21297:                      vpnext => 'IP range(s) for backend WAF connections',
21298:                    );
21299:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
21300:             my $possible = $env{'form.wafproxy_'.$item};
21301:             $possible =~ s/^\s+|\s+$//g;
21302:             if ($possible ne '') {
21303:                 if ($item eq 'remoteip') {
21304:                     if ($possible =~ /^[mhn]$/) {
21305:                         $wafproxy{$item} = $possible;
21306:                     }
21307:                 } elsif ($item eq 'ipheader') {
21308:                     if ($wafproxy{'remoteip'} eq 'h') {
21309:                         $wafproxy{$item} = $possible;
21310:                     }
21311:                 } elsif ($item eq 'sslopt') {
21312:                     if ($possible =~ /^0|1$/) {
21313:                         $wafproxy{$item} = $possible;
21314:                     }
21315:                 } else {
21316:                     my (@ok,$count);
21317:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
21318:                         unless ($env{'form.wafproxy_vpnaccess'}) {
21319:                             $possible = '';
21320:                         }
21321:                     } elsif ($item eq 'trusted') {
21322:                         unless ($wafproxy{'remoteip'} eq 'h') {
21323:                             $possible = '';
21324:                         }
21325:                     }
21326:                     unless ($possible eq '') {
21327:                         $possible =~ s/[\r\n]+/\s/g;
21328:                         $possible =~ s/\s*-\s*/-/g;
21329:                         $possible =~ s/\s+/,/g;
21330:                         $possible =~ s/,+/,/g;
21331:                     }
21332:                     $count = 0;
21333:                     if ($possible ne '') {
21334:                         foreach my $poss (split(/\,/,$possible)) {
21335:                             $count ++;
21336:                             $poss = &validate_ip_pattern($poss);
21337:                             if ($poss ne '') {
21338:                                 push(@ok,$poss);
21339:                             }
21340:                         }
21341:                         my $diff = $count - scalar(@ok);
21342:                         if ($diff) {
21343:                             push(@warnings,'<li>'.
21344:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
21345:                                      $diff,$warn{$item}).
21346:                                  '</li>');
21347:                         }
21348:                         if (@ok) {
21349:                             my @cidr_list;
21350:                             foreach my $item (@ok) {
21351:                                 @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
21352:                             }
21353:                             $wafproxy{$item} = join(',',@cidr_list);
21354:                         }
21355:                     }
21356:                 }
21357:                 if ($wafproxy{$item} ne $currvalue{$item}) {
21358:                     $changes{$item} = 1;
21359:                 }
21360:             } elsif ($currvalue{$item}) {
21361:                 $changes{$item} = 1;
21362:             }
21363:         }
21364:     } else {
21365:         if (keys(%curralias)) {
21366:             $changes{'alias'} = 1;
21367:         }
21368:         if (keys(%currsaml)) {
21369:             $changes{'saml'} = 1;
21370:         }
21371:         if (keys(%currvalue)) {
21372:             foreach my $key (keys(%currvalue)) {
21373:                 $changes{$key} = 1;
21374:             }
21375:         }
21376:     }
21377:     if (keys(%changes)) {
21378:         my %defaultshash = (
21379:                               wafproxy => \%wafproxy,
21380:                            );
21381:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21382:                                                  $dom);
21383:         if ($putresult eq 'ok') {
21384:             my $cachetime = 24*60*60;
21385:             my (%domdefaults,$updatedomdefs);
21386:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
21387:                 if ($changes{$item}) {
21388:                     unless ($updatedomdefs) {
21389:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
21390:                         $updatedomdefs = 1;
21391:                     }
21392:                     if ($wafproxy{$item}) {
21393:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
21394:                     } elsif (exists($domdefaults{'waf_'.$item})) {
21395:                         delete($domdefaults{'waf_'.$item});
21396:                     }
21397:                 }
21398:             }
21399:             if ($updatedomdefs) {
21400:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21401:                 if (ref($lastactref) eq 'HASH') {
21402:                     $lastactref->{'domdefaults'} = 1;
21403:                 }
21404:             }
21405:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
21406:                 my %updates = %expirecache;
21407:                 foreach my $key (keys(%expirecache)) {
21408:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
21409:                 }
21410:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
21411:                     my $cachetime = 24*60*60;
21412:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
21413:                         $updates{$key} = 1;
21414:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
21415:                                                       $cachetime);
21416:                     }
21417:                 }
21418:                 if (ref($lastactref) eq 'HASH') {
21419:                     $lastactref->{'proxyalias'} = \%updates;
21420:                 }
21421:             }
21422:             if ((exists($wafproxy{'saml'})) || (keys(%expiresaml))) {
21423:                 my %samlupdates = %expiresaml;
21424:                 foreach my $key (keys(%expiresaml)) {
21425:                     &Apache::lonnet::devalidate_cache_new('proxysaml',$key);
21426:                 }
21427:                 if (ref($wafproxy{'saml'}) eq 'HASH') {
21428:                     my $cachetime = 24*60*60;
21429:                     foreach my $key (keys(%{$wafproxy{'saml'}})) {
21430:                         $samlupdates{$key} = 1;
21431:                         &Apache::lonnet::do_cache_new('proxysaml',$key,$wafproxy{'saml'}{$key},
21432:                                                       $cachetime);
21433:                     }
21434:                 }
21435:                 if (ref($lastactref) eq 'HASH') {
21436:                     $lastactref->{'proxysaml'} = \%samlupdates;
21437:                 }
21438:             }
21439:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
21440:             foreach my $item ('alias','saml','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
21441:                 if ($changes{$item}) {
21442:                     if ($item eq 'alias') {
21443:                         my $numaliased = 0;
21444:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
21445:                             my $shown;
21446:                             if (keys(%{$wafproxy{'alias'}})) {
21447:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
21448:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
21449:                                                          &Apache::lonnet::hostname($server),
21450:                                                          $wafproxy{'alias'}{$server}).'</li>';
21451:                                     $numaliased ++;
21452:                                 }
21453:                                 if ($numaliased) {
21454:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
21455:                                                           '<ul>'.$shown.'</ul>').'</li>';
21456:                                 }
21457:                             }
21458:                         }
21459:                         unless ($numaliased) {
21460:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
21461:                         }
21462:                     } elsif ($item eq 'saml') {
21463:                         my $shown;
21464:                         if (ref($wafproxy{'saml'}) eq 'HASH') {
21465:                             if (keys(%{$wafproxy{'saml'}})) {
21466:                                 $shown = join(', ',sort(keys(%{$wafproxy{'saml'}})));
21467:                             }
21468:                         }
21469:                         if ($shown) {
21470:                             $output .= '<li>'.&mt('Alias used by SSO Auth for: [_1]',
21471:                                                   $shown).'</li>';
21472:                         } else {
21473:                             $output .= '<li>'.&mt('No alias used for SSO Auth').'</li>';
21474:                         }
21475:                     } else {
21476:                         if ($item eq 'remoteip') {
21477:                             my %ip_methods = &remoteip_methods();
21478:                             if ($wafproxy{$item} =~ /^[mh]$/) {
21479:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
21480:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
21481:                             } else {
21482:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
21483:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
21484:                                                '</li>';
21485:                                 } else {
21486:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
21487:                                 }
21488:                             }
21489:                         } elsif ($item eq 'ipheader') {
21490:                             if ($wafproxy{$item}) {
21491:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
21492:                                                       $wafproxy{$item}).'</li>';
21493:                             } else {
21494:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
21495:                             }
21496:                         } elsif ($item eq 'trusted') {
21497:                             if ($wafproxy{$item}) {
21498:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
21499:                                                       $wafproxy{$item}).'</li>';
21500:                             } else {
21501:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
21502:                             }
21503:                         } elsif ($item eq 'vpnint') {
21504:                             if ($wafproxy{$item}) {
21505:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
21506:                                                        $wafproxy{$item}).'</li>';
21507:                             } else {
21508:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
21509:                             }
21510:                         } elsif ($item eq 'vpnext') {
21511:                             if ($wafproxy{$item}) {
21512:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
21513:                                                        $wafproxy{$item}).'</li>';
21514:                             } else {
21515:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
21516:                             }
21517:                         } elsif ($item eq 'sslopt') {
21518:                             if ($wafproxy{$item}) {
21519:                                 $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>';
21520:                             } else {
21521:                                 $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>';
21522:                             }
21523:                         }
21524:                     }
21525:                 }
21526:             }
21527:             $output .= '</ul>';
21528:         } else {
21529:             $output = '<span class="LC_error">'.
21530:                       &mt('An error occurred: [_1]',$putresult).'</span>';
21531:         }
21532:     } elsif (keys(%canset)) {
21533:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
21534:     }
21535:     if (@warnings) {
21536:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
21537:                        join("\n",@warnings).'</ul>';
21538:     }
21539:     return $output;
21540: }
21541: 
21542: sub validate_ip_pattern {
21543:     my ($pattern) = @_;
21544:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
21545:         my ($start,$end) = ($1,$2);
21546:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
21547:             if (($start !~ m{/}) && ($end !~ m{/})) {
21548:                 return $start.'-'.$end;
21549:             }
21550:         }
21551:     } elsif ($pattern ne '') {
21552:         $pattern = &Net::CIDR::cidrvalidate($pattern);
21553:         if ($pattern ne '') {
21554:             return $pattern;
21555:         }
21556:     }
21557:     return;
21558: }
21559: 
21560: sub modify_usersessions {
21561:     my ($dom,$lastactref,%domconfig) = @_;
21562:     my @hostingtypes = ('version','excludedomain','includedomain');
21563:     my @offloadtypes = ('primary','default');
21564:     my %types = (
21565:                   remote => \@hostingtypes,
21566:                   hosted => \@hostingtypes,
21567:                   spares => \@offloadtypes,
21568:                 );
21569:     my @prefixes = ('remote','hosted','spares');
21570:     my @lcversions = &Apache::lonnet::all_loncaparevs();
21571:     my (%by_ip,%by_location,@intdoms,@instdoms);
21572:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
21573:     my @locations = sort(keys(%by_location));
21574:     my (%defaultshash,%changes);
21575:     foreach my $prefix (@prefixes) {
21576:         $defaultshash{'usersessions'}{$prefix} = {};
21577:     }
21578:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21579:     my $resulttext;
21580:     my %iphost = &Apache::lonnet::get_iphost();
21581:     foreach my $prefix (@prefixes) {
21582:         next if ($prefix eq 'spares');
21583:         foreach my $type (@{$types{$prefix}}) {
21584:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
21585:             if ($type eq 'version') {
21586:                 my $value = $env{'form.'.$prefix.'_'.$type};
21587:                 my $okvalue;
21588:                 if ($value ne '') {
21589:                     if (grep(/^\Q$value\E$/,@lcversions)) {
21590:                         $okvalue = $value;
21591:                     }
21592:                 }
21593:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
21594:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
21595:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
21596:                             if ($inuse == 0) {
21597:                                 $changes{$prefix}{$type} = 1;
21598:                             } else {
21599:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
21600:                                     $changes{$prefix}{$type} = 1;
21601:                                 }
21602:                                 if ($okvalue ne '') {
21603:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21604:                                 } 
21605:                             }
21606:                         } else {
21607:                             if (($inuse == 1) && ($okvalue ne '')) {
21608:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21609:                                 $changes{$prefix}{$type} = 1;
21610:                             }
21611:                         }
21612:                     } else {
21613:                         if (($inuse == 1) && ($okvalue ne '')) {
21614:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21615:                             $changes{$prefix}{$type} = 1;
21616:                         }
21617:                     }
21618:                 } else {
21619:                     if (($inuse == 1) && ($okvalue ne '')) {
21620:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21621:                         $changes{$prefix}{$type} = 1;
21622:                     }
21623:                 }
21624:             } else {
21625:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
21626:                 my @okvals;
21627:                 foreach my $val (@vals) {
21628:                     if ($val =~ /:/) {
21629:                         my @items = split(/:/,$val);
21630:                         foreach my $item (@items) {
21631:                             if (ref($by_location{$item}) eq 'ARRAY') {
21632:                                 push(@okvals,$item);
21633:                             }
21634:                         }
21635:                     } else {
21636:                         if (ref($by_location{$val}) eq 'ARRAY') {
21637:                             push(@okvals,$val);
21638:                         }
21639:                     }
21640:                 }
21641:                 @okvals = sort(@okvals);
21642:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
21643:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
21644:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
21645:                             if ($inuse == 0) {
21646:                                 $changes{$prefix}{$type} = 1; 
21647:                             } else {
21648:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21649:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
21650:                                 if (@changed > 0) {
21651:                                     $changes{$prefix}{$type} = 1;
21652:                                 }
21653:                             }
21654:                         } else {
21655:                             if ($inuse == 1) {
21656:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21657:                                 $changes{$prefix}{$type} = 1;
21658:                             }
21659:                         } 
21660:                     } else {
21661:                         if ($inuse == 1) {
21662:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21663:                             $changes{$prefix}{$type} = 1;
21664:                         }
21665:                     }
21666:                 } else {
21667:                     if ($inuse == 1) {
21668:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21669:                         $changes{$prefix}{$type} = 1;
21670:                     }
21671:                 }
21672:             }
21673:         }
21674:     }
21675: 
21676:     my @alldoms = &Apache::lonnet::all_domains();
21677:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21678:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
21679:     my $savespares;
21680: 
21681:     foreach my $lonhost (sort(keys(%servers))) {
21682:         my $serverhomeID =
21683:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
21684:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
21685:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
21686:         my %spareschg;
21687:         foreach my $type (@{$types{'spares'}}) {
21688:             my @okspares;
21689:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
21690:             foreach my $server (@checked) {
21691:                 if (&Apache::lonnet::hostname($server) ne '') {
21692:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
21693:                         unless (grep(/^\Q$server\E$/,@okspares)) {
21694:                             push(@okspares,$server);
21695:                         }
21696:                     }
21697:                 }
21698:             }
21699:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
21700:             my $newspare;
21701:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
21702:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
21703:                     $newspare = $new;
21704:                 }
21705:             }
21706:             my @spares;
21707:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
21708:                 @spares = sort(@okspares,$newspare);
21709:             } else {
21710:                 @spares = sort(@okspares);
21711:             }
21712:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
21713:             if (ref($spareid{$lonhost}) eq 'HASH') {
21714:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
21715:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
21716:                     if (@diffs > 0) {
21717:                         $spareschg{$type} = 1;
21718:                     }
21719:                 }
21720:             }
21721:         }
21722:         if (keys(%spareschg) > 0) {
21723:             $changes{'spares'}{$lonhost} = \%spareschg;
21724:         }
21725:     }
21726:     $defaultshash{'usersessions'}{'offloadnow'} = {};
21727:     $defaultshash{'usersessions'}{'offloadoth'} = {};
21728:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
21729:     my @okoffload;
21730:     if (@offloadnow) {
21731:         foreach my $server (@offloadnow) {
21732:             if (&Apache::lonnet::hostname($server) ne '') {
21733:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
21734:                     push(@okoffload,$server);
21735:                 }
21736:             }
21737:         }
21738:         if (@okoffload) {
21739:             foreach my $lonhost (@okoffload) {
21740:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
21741:             }
21742:         }
21743:     }
21744:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
21745:     my @okoffloadoth;
21746:     if (@offloadoth) {
21747:         foreach my $server (@offloadoth) {
21748:             if (&Apache::lonnet::hostname($server) ne '') {
21749:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
21750:                     push(@okoffloadoth,$server);
21751:                 }
21752:             }
21753:         }
21754:         if (@okoffloadoth) {
21755:             foreach my $lonhost (@okoffloadoth) {
21756:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
21757:             }
21758:         }
21759:     }
21760:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
21761:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
21762:             if (ref($changes{'spares'}) eq 'HASH') {
21763:                 if (keys(%{$changes{'spares'}}) > 0) {
21764:                     $savespares = 1;
21765:                 }
21766:             }
21767:         } else {
21768:             $savespares = 1;
21769:         }
21770:         foreach my $offload ('offloadnow','offloadoth') {
21771:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
21772:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
21773:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
21774:                         $changes{$offload} = 1;
21775:                         last;
21776:                     }
21777:                 }
21778:                 unless ($changes{$offload}) {
21779:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
21780:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
21781:                             $changes{$offload} = 1;
21782:                             last;
21783:                         }
21784:                     }
21785:                 }
21786:             } else {
21787:                 if (($offload eq 'offloadnow') && (@okoffload)) {
21788:                      $changes{'offloadnow'} = 1;
21789:                 }
21790:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
21791:                     $changes{'offloadoth'} = 1;
21792:                 }
21793:             }
21794:         }
21795:     } else {
21796:         if (@okoffload) {
21797:             $changes{'offloadnow'} = 1;
21798:         }
21799:         if (@okoffloadoth) {
21800:             $changes{'offloadoth'} = 1;
21801:         }
21802:     }
21803:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
21804:     if ((keys(%changes) > 0) || ($savespares)) {
21805:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21806:                                                  $dom);
21807:         if ($putresult eq 'ok') {
21808:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
21809:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
21810:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
21811:                 }
21812:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
21813:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
21814:                 }
21815:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
21816:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
21817:                 }
21818:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
21819:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
21820:                 }
21821:             }
21822:             my $cachetime = 24*60*60;
21823:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21824:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
21825:             if (ref($lastactref) eq 'HASH') {
21826:                 $lastactref->{'domdefaults'} = 1;
21827:                 $lastactref->{'usersessions'} = 1;
21828:             }
21829:             if (keys(%changes) > 0) {
21830:                 my %lt = &usersession_titles();
21831:                 $resulttext = &mt('Changes made:').'<ul>';
21832:                 foreach my $prefix (@prefixes) {
21833:                     if (ref($changes{$prefix}) eq 'HASH') {
21834:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
21835:                         if ($prefix eq 'spares') {
21836:                             if (ref($changes{$prefix}) eq 'HASH') {
21837:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
21838:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
21839:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
21840:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
21841:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
21842:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
21843:                                         foreach my $type (@{$types{$prefix}}) {
21844:                                             if ($changes{$prefix}{$lonhost}{$type}) {
21845:                                                 my $offloadto = &mt('None');
21846:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
21847:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
21848:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
21849:                                                     }
21850:                                                 }
21851:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
21852:                                             }
21853:                                         }
21854:                                     }
21855:                                     $resulttext .= '</li>';
21856:                                 }
21857:                             }
21858:                         } else {
21859:                             foreach my $type (@{$types{$prefix}}) {
21860:                                 if (defined($changes{$prefix}{$type})) {
21861:                                     my ($newvalue,$notinuse);
21862:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
21863:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
21864:                                             if ($type eq 'version') {
21865:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
21866:                                             } else {
21867:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
21868:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
21869:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
21870:                                                     }
21871:                                                 } else {
21872:                                                     $notinuse = 1;
21873:                                                 }
21874:                                             }
21875:                                         }
21876:                                     }
21877:                                     if ($newvalue eq '') {
21878:                                         if ($type eq 'version') {
21879:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
21880:                                         } elsif ($notinuse) {
21881:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
21882:                                         } else {
21883:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
21884:                                         }
21885:                                     } else {
21886:                                         if ($type eq 'version') {
21887:                                             $newvalue .= ' '.&mt('(or later)');
21888:                                         }
21889:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
21890:                                     }
21891:                                 }
21892:                             }
21893:                         }
21894:                         $resulttext .= '</ul>';
21895:                     }
21896:                 }
21897:                 if ($changes{'offloadnow'}) {
21898:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
21899:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
21900:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
21901:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
21902:                                 $resulttext .= '<li>'.$lonhost.'</li>';
21903:                             }
21904:                             $resulttext .= '</ul>';
21905:                         } else {
21906:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
21907:                         }
21908:                     } else {
21909:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
21910:                     }
21911:                 }
21912:                 if ($changes{'offloadoth'}) {
21913:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
21914:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
21915:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
21916:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
21917:                                 $resulttext .= '<li>'.$lonhost.'</li>';
21918:                             }
21919:                             $resulttext .= '</ul>';
21920:                         } else {
21921:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
21922:                         }
21923:                     } else {
21924:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
21925:                     }
21926:                 }
21927:                 $resulttext .= '</ul>';
21928:             } else {
21929:                 $resulttext = $nochgmsg;
21930:             }
21931:         } else {
21932:             $resulttext = '<span class="LC_error">'.
21933:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21934:         }
21935:     } else {
21936:         $resulttext = $nochgmsg;
21937:     }
21938:     return $resulttext;
21939: }
21940: 
21941: sub modify_ssl {
21942:     my ($dom,$lastactref,%domconfig) = @_;
21943:     my (%by_ip,%by_location,@intdoms,@instdoms);
21944:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
21945:     my @locations = sort(keys(%by_location));
21946:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21947:     my (%defaultshash,%changes);
21948:     my $action = 'ssl';
21949:     my @prefixes = ('connto','connfrom','replication');
21950:     foreach my $prefix (@prefixes) {
21951:         $defaultshash{$action}{$prefix} = {};
21952:     }
21953:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21954:     my $resulttext;
21955:     my %iphost = &Apache::lonnet::get_iphost();
21956:     my @reptypes = ('certreq','nocertreq');
21957:     my @connecttypes = ('dom','intdom','other');
21958:     my %types = (
21959:                   connto      => \@connecttypes,
21960:                   connfrom    => \@connecttypes,
21961:                   replication => \@reptypes,
21962:                 );
21963:     foreach my $prefix (sort(keys(%types))) {
21964:         foreach my $type (@{$types{$prefix}}) {
21965:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
21966:                 my $value = 'yes';
21967:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
21968:                     $value = $env{'form.'.$prefix.'_'.$type};
21969:                 }
21970:                 if (ref($domconfig{$action}) eq 'HASH') {
21971:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
21972:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
21973:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
21974:                                 $changes{$prefix}{$type} = 1;
21975:                             }
21976:                             $defaultshash{$action}{$prefix}{$type} = $value;
21977:                         } else {
21978:                             $defaultshash{$action}{$prefix}{$type} = $value;
21979:                             $changes{$prefix}{$type} = 1;
21980:                         }
21981:                     } else {
21982:                         $defaultshash{$action}{$prefix}{$type} = $value;
21983:                         $changes{$prefix}{$type} = 1;
21984:                     }
21985:                 } else {
21986:                     $defaultshash{$action}{$prefix}{$type} = $value;
21987:                     $changes{$prefix}{$type} = 1;
21988:                 }
21989:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
21990:                     delete($changes{$prefix}{$type});
21991:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
21992:                     delete($changes{$prefix}{$type});
21993:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
21994:                     delete($changes{$prefix}{$type});
21995:                 }
21996:             } elsif ($prefix eq 'replication') {
21997:                 if (@locations > 0) {
21998:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
21999:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22000:                     my @okvals;
22001:                     foreach my $val (@vals) {
22002:                         if ($val =~ /:/) {
22003:                             my @items = split(/:/,$val);
22004:                             foreach my $item (@items) {
22005:                                 if (ref($by_location{$item}) eq 'ARRAY') {
22006:                                     push(@okvals,$item);
22007:                                 }
22008:                             }
22009:                         } else {
22010:                             if (ref($by_location{$val}) eq 'ARRAY') {
22011:                                 push(@okvals,$val);
22012:                             }
22013:                         }
22014:                     }
22015:                     @okvals = sort(@okvals);
22016:                     if (ref($domconfig{$action}) eq 'HASH') {
22017:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
22018:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
22019:                                 if ($inuse == 0) {
22020:                                     $changes{$prefix}{$type} = 1;
22021:                                 } else {
22022:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
22023:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
22024:                                     if (@changed > 0) {
22025:                                         $changes{$prefix}{$type} = 1;
22026:                                     }
22027:                                 }
22028:                             } else {
22029:                                 if ($inuse == 1) {
22030:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
22031:                                     $changes{$prefix}{$type} = 1;
22032:                                 }
22033:                             }
22034:                         } else {
22035:                             if ($inuse == 1) {
22036:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
22037:                                 $changes{$prefix}{$type} = 1;
22038:                             }
22039:                         }
22040:                     } else {
22041:                         if ($inuse == 1) {
22042:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
22043:                             $changes{$prefix}{$type} = 1;
22044:                         }
22045:                     }
22046:                 }
22047:             }
22048:         }
22049:     }
22050:     if (keys(%changes)) {
22051:         foreach my $prefix (keys(%changes)) {
22052:             if (ref($changes{$prefix}) eq 'HASH') {
22053:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
22054:                     delete($changes{$prefix});
22055:                 }
22056:             } else {
22057:                 delete($changes{$prefix});
22058:             }
22059:         }
22060:     }
22061:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
22062:     if (keys(%changes) > 0) {
22063:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22064:                                                  $dom);
22065:         if ($putresult eq 'ok') {
22066:             if (ref($defaultshash{$action}) eq 'HASH') {
22067:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
22068:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
22069:                 }
22070:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
22071:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
22072:                 }
22073:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
22074:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
22075:                 }
22076:             }
22077:             my $cachetime = 24*60*60;
22078:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22079:             if (ref($lastactref) eq 'HASH') {
22080:                 $lastactref->{'domdefaults'} = 1;
22081:             }
22082:             if (keys(%changes) > 0) {
22083:                 my %titles = &ssl_titles();
22084:                 $resulttext = &mt('Changes made:').'<ul>';
22085:                 foreach my $prefix (@prefixes) {
22086:                     if (ref($changes{$prefix}) eq 'HASH') {
22087:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
22088:                         foreach my $type (@{$types{$prefix}}) {
22089:                             if (defined($changes{$prefix}{$type})) {
22090:                                 my ($newvalue,$notinuse);
22091:                                 if (ref($defaultshash{$action}) eq 'HASH') {
22092:                                     if (ref($defaultshash{$action}{$prefix})) {
22093:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
22094:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
22095:                                         } else {
22096:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
22097:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
22098:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
22099:                                                 }
22100:                                             } else {
22101:                                                 $notinuse = 1;
22102:                                             }
22103:                                         }
22104:                                     }
22105:                                     if ($notinuse) {
22106:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
22107:                                     } elsif ($newvalue eq '') {
22108:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
22109:                                     } else {
22110:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
22111:                                     }
22112:                                 }
22113:                             }
22114:                         }
22115:                         $resulttext .= '</ul>';
22116:                     }
22117:                 }
22118:             } else {
22119:                 $resulttext = $nochgmsg;
22120:             }
22121:         } else {
22122:             $resulttext = '<span class="LC_error">'.
22123:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22124:         }
22125:     } else {
22126:         $resulttext = $nochgmsg;
22127:     }
22128:     return $resulttext;
22129: }
22130: 
22131: sub modify_trust {
22132:     my ($dom,$lastactref,%domconfig) = @_;
22133:     my (%by_ip,%by_location,@intdoms,@instdoms);
22134:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22135:     my @locations = sort(keys(%by_location));
22136:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
22137:     my @types = ('exc','inc');
22138:     my (%defaultshash,%changes);
22139:     foreach my $prefix (@prefixes) {
22140:         $defaultshash{'trust'}{$prefix} = {};
22141:     }
22142:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22143:     my $resulttext;
22144:     foreach my $prefix (@prefixes) {
22145:         foreach my $type (@types) {
22146:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
22147:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22148:             my @okvals;
22149:             foreach my $val (@vals) {
22150:                 if ($val =~ /:/) {
22151:                     my @items = split(/:/,$val);
22152:                     foreach my $item (@items) {
22153:                         if (ref($by_location{$item}) eq 'ARRAY') {
22154:                             push(@okvals,$item);
22155:                         }
22156:                     }
22157:                 } else {
22158:                     if (ref($by_location{$val}) eq 'ARRAY') {
22159:                         push(@okvals,$val);
22160:                     }
22161:                 }
22162:             }
22163:             @okvals = sort(@okvals);
22164:             if (ref($domconfig{'trust'}) eq 'HASH') {
22165:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
22166:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
22167:                         if ($inuse == 0) {
22168:                             $changes{$prefix}{$type} = 1;
22169:                         } else {
22170:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
22171:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
22172:                             if (@changed > 0) {
22173:                                 $changes{$prefix}{$type} = 1;
22174:                             }
22175:                         }
22176:                     } else {
22177:                         if ($inuse == 1) {
22178:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
22179:                             $changes{$prefix}{$type} = 1;
22180:                         }
22181:                     }
22182:                 } else {
22183:                     if ($inuse == 1) {
22184:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
22185:                         $changes{$prefix}{$type} = 1;
22186:                     }
22187:                 }
22188:             } else {
22189:                 if ($inuse == 1) {
22190:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
22191:                     $changes{$prefix}{$type} = 1;
22192:                 }
22193:             }
22194:         }
22195:     }
22196:     my $nochgmsg = &mt('No changes made to trust settings.');
22197:     if (keys(%changes) > 0) {
22198:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22199:                                                  $dom);
22200:         if ($putresult eq 'ok') {
22201:             if (ref($defaultshash{'trust'}) eq 'HASH') {
22202:                 foreach my $prefix (@prefixes) {
22203:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
22204:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
22205:                     }
22206:                 }
22207:             }
22208:             my $cachetime = 24*60*60;
22209:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22210:             &Apache::lonnet::do_cache_new('trust',$dom,$defaultshash{'trust'},3600);
22211:             if (ref($lastactref) eq 'HASH') {
22212:                 $lastactref->{'domdefaults'} = 1;
22213:                 $lastactref->{'trust'} = 1;
22214:             }
22215:             if (keys(%changes) > 0) {
22216:                 my %lt = &trust_titles();
22217:                 $resulttext = &mt('Changes made:').'<ul>';
22218:                 foreach my $prefix (@prefixes) {
22219:                     if (ref($changes{$prefix}) eq 'HASH') {
22220:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
22221:                         foreach my $type (@types) {
22222:                             if (defined($changes{$prefix}{$type})) {
22223:                                 my ($newvalue,$notinuse);
22224:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
22225:                                     if (ref($defaultshash{'trust'}{$prefix})) {
22226:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
22227:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
22228:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
22229:                                             }
22230:                                         } else {
22231:                                             $notinuse = 1;
22232:                                         }
22233:                                     }
22234:                                 }
22235:                                 if ($notinuse) {
22236:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
22237:                                 } elsif ($newvalue eq '') {
22238:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
22239:                                 } else {
22240:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
22241:                                 }
22242:                             }
22243:                         }
22244:                         $resulttext .= '</ul>';
22245:                     }
22246:                 }
22247:                 $resulttext .= '</ul>';
22248:             } else {
22249:                 $resulttext = $nochgmsg;
22250:             }
22251:         } else {
22252:             $resulttext = '<span class="LC_error">'.
22253:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22254:         }
22255:     } else {
22256:         $resulttext = $nochgmsg;
22257:     }
22258:     return $resulttext;
22259: }
22260: 
22261: sub modify_loadbalancing {
22262:     my ($dom,%domconfig) = @_;
22263:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
22264:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
22265:     my ($othertitle,$usertypes,$types) =
22266:         &Apache::loncommon::sorted_inst_types($dom);
22267:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22268:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
22269:     my @sparestypes = ('primary','default');
22270:     my %typetitles = &sparestype_titles();
22271:     my $resulttext;
22272:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
22273:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
22274:         %existing = %{$domconfig{'loadbalancing'}};
22275:     }
22276:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
22277:                               \%currtargets,\%currrules,\%currcookies);
22278:     my ($saveloadbalancing,%defaultshash,%changes);
22279:     my ($alltypes,$othertypes,$titles) =
22280:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
22281:     my %ruletitles = &offloadtype_text();
22282:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
22283:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
22284:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
22285:         if ($balancer eq '') {
22286:             next;
22287:         }
22288:         if (!exists($servers{$balancer})) {
22289:             if (exists($currbalancer{$balancer})) {
22290:                 push(@{$changes{'delete'}},$balancer);
22291:             }
22292:             next;
22293:         }
22294:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
22295:             push(@{$changes{'delete'}},$balancer);
22296:             next;
22297:         }
22298:         if (!exists($currbalancer{$balancer})) {
22299:             push(@{$changes{'add'}},$balancer);
22300:         }
22301:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
22302:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
22303:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
22304:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
22305:             $saveloadbalancing = 1;
22306:         }
22307:         foreach my $sparetype (@sparestypes) {
22308:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
22309:             my @offloadto;
22310:             foreach my $target (@targets) {
22311:                 if (($servers{$target}) && ($target ne $balancer)) {
22312:                     if ($sparetype eq 'default') {
22313:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
22314:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
22315:                         }
22316:                     }
22317:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
22318:                         push(@offloadto,$target);
22319:                     }
22320:                 }
22321:             }
22322:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
22323:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
22324:                     push(@offloadto,$balancer);
22325:                 }
22326:             }
22327:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
22328:         }
22329:         if ($env{'form.loadbalancing_cookie_'.$i}) {
22330:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
22331:             if (exists($currbalancer{$balancer})) {
22332:                 unless ($currcookies{$balancer}) {
22333:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
22334:                 }
22335:             }
22336:         } elsif (exists($currbalancer{$balancer})) {
22337:             if ($currcookies{$balancer}) {
22338:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
22339:             }
22340:         }
22341:         if (ref($currtargets{$balancer}) eq 'HASH') {
22342:             foreach my $sparetype (@sparestypes) {
22343:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
22344:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
22345:                     if (@targetdiffs > 0) {
22346:                         $changes{'curr'}{$balancer}{'targets'} = 1;
22347:                     }
22348:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22349:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22350:                         $changes{'curr'}{$balancer}{'targets'} = 1;
22351:                     }
22352:                 }
22353:             }
22354:         } else {
22355:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
22356:                 foreach my $sparetype (@sparestypes) {
22357:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22358:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22359:                             $changes{'curr'}{$balancer}{'targets'} = 1;
22360:                         }
22361:                     }
22362:                 }
22363:             }
22364:         }
22365:         my $ishomedom;
22366:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
22367:             $ishomedom = 1;
22368:         }
22369:         if (ref($alltypes) eq 'ARRAY') {
22370:             foreach my $type (@{$alltypes}) {
22371:                 my $rule;
22372:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
22373:                          (!$ishomedom)) {
22374:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
22375:                 }
22376:                 if ($rule eq 'specific') {
22377:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
22378:                     if (exists($servers{$specifiedhost})) {
22379:                         $rule = $specifiedhost;
22380:                     }
22381:                 }
22382:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
22383:                 if (ref($currrules{$balancer}) eq 'HASH') {
22384:                     if ($rule ne $currrules{$balancer}{$type}) {
22385:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
22386:                     }
22387:                 } elsif ($rule ne '') {
22388:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
22389:                 }
22390:             }
22391:         }
22392:     }
22393:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
22394:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
22395:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
22396:             $defaultshash{'loadbalancing'} = {};
22397:         }
22398:         my $putresult = &Apache::lonnet::put_dom('configuration',
22399:                                                  \%defaultshash,$dom);
22400:         if ($putresult eq 'ok') {
22401:             if (keys(%changes) > 0) {
22402:                 my %toupdate;
22403:                 if (ref($changes{'delete'}) eq 'ARRAY') {
22404:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
22405:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
22406:                         $toupdate{$balancer} = 1;
22407:                     }
22408:                 }
22409:                 if (ref($changes{'add'}) eq 'ARRAY') {
22410:                     foreach my $balancer (sort(@{$changes{'add'}})) {
22411:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
22412:                         $toupdate{$balancer} = 1;
22413:                     }
22414:                 }
22415:                 if (ref($changes{'curr'}) eq 'HASH') {
22416:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
22417:                         $toupdate{$balancer} = 1;
22418:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
22419:                             if ($changes{'curr'}{$balancer}{'targets'}) {
22420:                                 my %offloadstr;
22421:                                 foreach my $sparetype (@sparestypes) {
22422:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22423:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22424:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
22425:                                         }
22426:                                     }
22427:                                 }
22428:                                 if (keys(%offloadstr) == 0) {
22429:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
22430:                                 } else {
22431:                                     my $showoffload;
22432:                                     foreach my $sparetype (@sparestypes) {
22433:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
22434:                                         if (defined($offloadstr{$sparetype})) {
22435:                                             $showoffload .= $offloadstr{$sparetype};
22436:                                         } else {
22437:                                             $showoffload .= &mt('None');
22438:                                         }
22439:                                         $showoffload .= ('&nbsp;'x3);
22440:                                     }
22441:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
22442:                                 }
22443:                             }
22444:                         }
22445:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
22446:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
22447:                                 foreach my $type (@{$alltypes}) {
22448:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
22449:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
22450:                                         my $balancetext;
22451:                                         if ($rule eq '') {
22452:                                             $balancetext =  $ruletitles{'default'};
22453:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
22454:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
22455:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
22456:                                                 foreach my $sparetype (@sparestypes) {
22457:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22458:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
22459:                                                     }
22460:                                                 }
22461:                                                 foreach my $item (@{$alltypes}) {
22462:                                                     next if ($item =~  /^_LC_ipchange/);
22463:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
22464:                                                     if ($hasrule eq 'homeserver') {
22465:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
22466:                                                     } else {
22467:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
22468:                                                             if ($servers{$hasrule}) {
22469:                                                                 $toupdate{$hasrule} = 1;
22470:                                                             }
22471:                                                         }
22472:                                                     }
22473:                                                 }
22474:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
22475:                                                     $balancetext =  $ruletitles{$rule};
22476:                                                 } else {
22477:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
22478:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
22479:                                                     if ($receiver) {
22480:                                                         $toupdate{$receiver};
22481:                                                     }
22482:                                                 }
22483:                                             } else {
22484:                                                 $balancetext =  $ruletitles{$rule};
22485:                                             }
22486:                                         } else {
22487:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
22488:                                         }
22489:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
22490:                                     }
22491:                                 }
22492:                             }
22493:                         }
22494:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
22495:                             if ($currcookies{$balancer}) {
22496:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use disabled',
22497:                                                           $balancer).'</li>';
22498:                             } else {
22499:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
22500:                                                           $balancer).'</li>';
22501:                             }
22502:                         }
22503:                     }
22504:                 }
22505:                 if (keys(%toupdate)) {
22506:                     my %thismachine;
22507:                     my $updatedhere;
22508:                     my $cachetime = 60*60*24;
22509:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
22510:                     foreach my $lonhost (keys(%toupdate)) {
22511:                         if ($thismachine{$lonhost}) {
22512:                             unless ($updatedhere) {
22513:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
22514:                                                               $defaultshash{'loadbalancing'},
22515:                                                               $cachetime);
22516:                                 $updatedhere = 1;
22517:                             }
22518:                         } else {
22519:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
22520:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
22521:                         }
22522:                     }
22523:                 }
22524:                 if ($resulttext ne '') {
22525:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
22526:                 } else {
22527:                     $resulttext = $nochgmsg;
22528:                 }
22529:             } else {
22530:                 $resulttext = $nochgmsg;
22531:             }
22532:         } else {
22533:             $resulttext = '<span class="LC_error">'.
22534:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22535:         }
22536:     } else {
22537:         $resulttext = $nochgmsg;
22538:     }
22539:     return $resulttext;
22540: }
22541: 
22542: sub recurse_check {
22543:     my ($chkcats,$categories,$depth,$name) = @_;
22544:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
22545:         my $chg = 0;
22546:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
22547:             my $category = $chkcats->[$depth]{$name}[$j];
22548:             my $item;
22549:             if ($category eq '') {
22550:                 $chg ++;
22551:             } else {
22552:                 my $deeper = $depth + 1;
22553:                 $item = &escape($category).':'.&escape($name).':'.$depth;
22554:                 if ($chg) {
22555:                     $categories->{$item} -= $chg;
22556:                 }
22557:                 &recurse_check($chkcats,$categories,$deeper,$category);
22558:                 $deeper --;
22559:             }
22560:         }
22561:     }
22562:     return;
22563: }
22564: 
22565: sub recurse_cat_deletes {
22566:     my ($item,$coursecategories,$deletions) = @_;
22567:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
22568:     my $subdepth = $depth + 1;
22569:     if (ref($coursecategories) eq 'HASH') {
22570:         foreach my $subitem (keys(%{$coursecategories})) {
22571:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
22572:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
22573:                 delete($coursecategories->{$subitem});
22574:                 $deletions->{$subitem} = 1;
22575:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
22576:             }
22577:         }
22578:     }
22579:     return;
22580: }
22581: 
22582: sub active_dc_picker {
22583:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
22584:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
22585:     my @domcoord = keys(%domcoords);
22586:     if (keys(%currhash)) {
22587:         foreach my $dc (keys(%currhash)) {
22588:             unless (exists($domcoords{$dc})) {
22589:                 push(@domcoord,$dc);
22590:             }
22591:         }
22592:     }
22593:     @domcoord = sort(@domcoord);
22594:     my $numdcs = scalar(@domcoord);
22595:     my $rows = 0;
22596:     my $table;
22597:     if ($numdcs > 1) {
22598:         $table = '<table>';
22599:         for (my $i=0; $i<@domcoord; $i++) {
22600:             my $rem = $i%($numinrow);
22601:             if ($rem == 0) {
22602:                 if ($i > 0) {
22603:                     $table .= '</tr>';
22604:                 }
22605:                 $table .= '<tr>';
22606:                 $rows ++;
22607:             }
22608:             my $check = '';
22609:             if ($inputtype eq 'radio') {
22610:                 if (keys(%currhash) == 0) {
22611:                     if (!$i) {
22612:                         $check = ' checked="checked"';
22613:                     }
22614:                 } elsif (exists($currhash{$domcoord[$i]})) {
22615:                     $check = ' checked="checked"';
22616:                 }
22617:             } else {
22618:                 if (exists($currhash{$domcoord[$i]})) {
22619:                     $check = ' checked="checked"';
22620:                 }
22621:             }
22622:             if ($i == @domcoord - 1) {
22623:                 my $colsleft = $numinrow - $rem;
22624:                 if ($colsleft > 1) {
22625:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
22626:                 } else {
22627:                     $table .= '<td class="LC_left_item">';
22628:                 }
22629:             } else {
22630:                 $table .= '<td class="LC_left_item">';
22631:             }
22632:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
22633:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
22634:             $table .= '<span class="LC_nobreak"><label>'.
22635:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
22636:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
22637:             if ($user ne $dcname.':'.$dcdom) {
22638:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
22639:             }
22640:             $table .= '</label></span></td>';
22641:         }
22642:         $table .= '</tr></table>';
22643:     } elsif ($numdcs == 1) {
22644:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
22645:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
22646:         if ($inputtype eq 'radio') {
22647:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
22648:             if ($user ne $dcname.':'.$dcdom) {
22649:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
22650:             }
22651:         } else {
22652:             my $check;
22653:             if (exists($currhash{$domcoord[0]})) {
22654:                 $check = ' checked="checked"';
22655:             }
22656:             $table = '<span class="LC_nobreak"><label>'.
22657:                      '<input type="checkbox" name="'.$name.'" '.
22658:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
22659:             if ($user ne $dcname.':'.$dcdom) {
22660:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
22661:             }
22662:             $table .= '</label></span>';
22663:             $rows ++;
22664:         }
22665:     }
22666:     return ($numdcs,$table,$rows);
22667: }
22668: 
22669: sub usersession_titles {
22670:     return &Apache::lonlocal::texthash(
22671:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
22672:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
22673:                spares => 'Servers offloaded to, when busy',
22674:                version => 'LON-CAPA version requirement',
22675:                excludedomain => 'Allow all, but exclude specific domains',
22676:                includedomain => 'Deny all, but include specific domains',
22677:                primary => 'Primary (checked first)',
22678:                default => 'Default',
22679:            );
22680: }
22681: 
22682: sub id_for_thisdom {
22683:     my (%servers) = @_;
22684:     my %altids;
22685:     foreach my $server (keys(%servers)) {
22686:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
22687:         if ($serverhome ne $server) {
22688:             $altids{$serverhome} = $server;
22689:         }
22690:     }
22691:     return %altids;
22692: }
22693: 
22694: sub count_servers {
22695:     my ($currbalancer,%servers) = @_;
22696:     my (@spares,$numspares);
22697:     foreach my $lonhost (sort(keys(%servers))) {
22698:         next if ($currbalancer eq $lonhost);
22699:         push(@spares,$lonhost);
22700:     }
22701:     if ($currbalancer) {
22702:         $numspares = scalar(@spares);
22703:     } else {
22704:         $numspares = scalar(@spares) - 1;
22705:     }
22706:     return ($numspares,@spares);
22707: }
22708: 
22709: sub lonbalance_targets_js {
22710:     my ($dom,$types,$servers,$settings) = @_;
22711:     my $select = &mt('Select');
22712:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
22713:     if (ref($servers) eq 'HASH') {
22714:         $alltargets = join("','",sort(keys(%{$servers})));
22715:         my @homedoms;
22716:         foreach my $server (sort(keys(%{$servers}))) {
22717:             if (&Apache::lonnet::host_domain($server) eq $dom) {
22718:                 push(@homedoms,'1');
22719:             } else {
22720:                 push(@homedoms,'0');
22721:             }
22722:         }
22723:         $allishome = join("','",@homedoms);
22724:     }
22725:     if (ref($types) eq 'ARRAY') {
22726:         if (@{$types} > 0) {
22727:             @alltypes = @{$types};
22728:         }
22729:     }
22730:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
22731:     $allinsttypes = join("','",@alltypes);
22732:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
22733:     if (ref($settings) eq 'HASH') {
22734:         %existing = %{$settings};
22735:     }
22736:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
22737:                               \%currtargets,\%currrules,\%currcookies);
22738:     my $balancers = join("','",sort(keys(%currbalancer)));
22739:     return <<"END";
22740: 
22741: <script type="text/javascript">
22742: // <![CDATA[
22743: 
22744: currBalancers = new Array('$balancers');
22745: 
22746: function toggleTargets(balnum) {
22747:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
22748:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
22749:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
22750:     var prevbalancer = prevhostitem.value;
22751:     var baltotal = document.getElementById('loadbalancing_total').value;
22752:     prevhostitem.value = balancer;
22753:     if (prevbalancer != '') {
22754:         var prevIdx = currBalancers.indexOf(prevbalancer);
22755:         if (prevIdx != -1) {
22756:             currBalancers.splice(prevIdx,1);
22757:         }
22758:     }
22759:     if (balancer == '') {
22760:         hideSpares(balnum);
22761:     } else {
22762:         var currIdx = currBalancers.indexOf(balancer);
22763:         if (currIdx == -1) {
22764:             currBalancers.push(balancer);
22765:         }
22766:         var homedoms = new Array('$allishome');
22767:         var ishomedom = homedoms[lonhostitem.selectedIndex];
22768:         showSpares(balancer,ishomedom,balnum);
22769:     }
22770:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
22771:     return;
22772: }
22773: 
22774: function showSpares(balancer,ishomedom,balnum) {
22775:     var alltargets = new Array('$alltargets');
22776:     var insttypes = new Array('$allinsttypes');
22777:     var offloadtypes = new Array('primary','default');
22778: 
22779:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
22780:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
22781:  
22782:     for (var i=0; i<offloadtypes.length; i++) {
22783:         var count = 0;
22784:         for (var j=0; j<alltargets.length; j++) {
22785:             if (alltargets[j] != balancer) {
22786:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
22787:                 item.value = alltargets[j];
22788:                 item.style.textAlign='left';
22789:                 item.style.textFace='normal';
22790:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
22791:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
22792:                     item.disabled = '';
22793:                 } else {
22794:                     item.disabled = 'disabled';
22795:                     item.checked = false;
22796:                 }
22797:                 count ++;
22798:             }
22799:         }
22800:     }
22801:     for (var k=0; k<insttypes.length; k++) {
22802:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
22803:             if (ishomedom == 1) {
22804:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
22805:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
22806:             } else {
22807:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
22808:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
22809:             }
22810:         } else {
22811:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
22812:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
22813:         }
22814:         if ((insttypes[k] != '_LC_external') && 
22815:             ((insttypes[k] != '_LC_internetdom') ||
22816:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
22817:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
22818:             item.options.length = 0;
22819:             item.options[0] = new Option("","",true,true);
22820:             var idx = 0;
22821:             for (var m=0; m<alltargets.length; m++) {
22822:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
22823:                     idx ++;
22824:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
22825:                 }
22826:             }
22827:         }
22828:     }
22829:     return;
22830: }
22831: 
22832: function hideSpares(balnum) {
22833:     var alltargets = new Array('$alltargets');
22834:     var insttypes = new Array('$allinsttypes');
22835:     var offloadtypes = new Array('primary','default');
22836: 
22837:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
22838:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
22839: 
22840:     var total = alltargets.length - 1;
22841:     for (var i=0; i<offloadtypes; i++) {
22842:         for (var j=0; j<total; j++) {
22843:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
22844:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
22845:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
22846:         }
22847:     }
22848:     for (var k=0; k<insttypes.length; k++) {
22849:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
22850:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
22851:         if (insttypes[k] != '_LC_external') {
22852:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
22853:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
22854:         }
22855:     }
22856:     return;
22857: }
22858: 
22859: function checkOffloads(item,balnum,type) {
22860:     var alltargets = new Array('$alltargets');
22861:     var offloadtypes = new Array('primary','default');
22862:     if (item.checked) {
22863:         var total = alltargets.length - 1;
22864:         var other;
22865:         if (type == offloadtypes[0]) {
22866:             other = offloadtypes[1];
22867:         } else {
22868:             other = offloadtypes[0];
22869:         }
22870:         for (var i=0; i<total; i++) {
22871:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
22872:             if (server == item.value) {
22873:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
22874:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
22875:                 }
22876:             }
22877:         }
22878:     }
22879:     return;
22880: }
22881: 
22882: function singleServerToggle(balnum,type) {
22883:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
22884:     if (offloadtoSelIdx == 0) {
22885:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
22886:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
22887: 
22888:     } else {
22889:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
22890:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
22891:     }
22892:     return;
22893: }
22894: 
22895: function balanceruleChange(formname,balnum,type) {
22896:     if (type == '_LC_external') {
22897:         return;
22898:     }
22899:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
22900:     for (var i=0; i<typesRules.length; i++) {
22901:         if (formname.elements[typesRules[i]].checked) {
22902:             if (formname.elements[typesRules[i]].value != 'specific') {
22903:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
22904:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
22905:             } else {
22906:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
22907:             }
22908:         }
22909:     }
22910:     return;
22911: }
22912: 
22913: function balancerDeleteChange(balnum) {
22914:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
22915:     var baltotal = document.getElementById('loadbalancing_total').value;
22916:     var addtarget;
22917:     var removetarget;
22918:     var action = 'delete';
22919:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
22920:         var lonhost = hostitem.value;
22921:         var currIdx = currBalancers.indexOf(lonhost);
22922:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
22923:             if (currIdx != -1) {
22924:                 currBalancers.splice(currIdx,1);
22925:             }
22926:             addtarget = lonhost;
22927:         } else {
22928:             if (currIdx == -1) {
22929:                 currBalancers.push(lonhost);
22930:             }
22931:             removetarget = lonhost;
22932:             action = 'undelete';
22933:         }
22934:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
22935:     }
22936:     return;
22937: }
22938: 
22939: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
22940:     if (baltotal > 1) {
22941:         var offloadtypes = new Array('primary','default');
22942:         var alltargets = new Array('$alltargets');
22943:         var insttypes = new Array('$allinsttypes');
22944:         for (var i=0; i<baltotal; i++) {
22945:             if (i != balnum) {
22946:                 for (var j=0; j<offloadtypes.length; j++) {
22947:                     var total = alltargets.length - 1;
22948:                     for (var k=0; k<total; k++) {
22949:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
22950:                         var server = serveritem.value;
22951:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
22952:                             if (server == addtarget) {
22953:                                 serveritem.disabled = '';
22954:                             }
22955:                         }
22956:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
22957:                             if (server == removetarget) {
22958:                                 serveritem.disabled = 'disabled';
22959:                                 serveritem.checked = false;
22960:                             }
22961:                         }
22962:                     }
22963:                 }
22964:                 for (var j=0; j<insttypes.length; j++) {
22965:                     if (insttypes[j] != '_LC_external') {
22966:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
22967:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
22968:                             var currSel = singleserver.selectedIndex;
22969:                             var currVal = singleserver.options[currSel].value;
22970:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
22971:                                 var numoptions = singleserver.options.length;
22972:                                 var needsnew = 1;
22973:                                 for (var k=0; k<numoptions; k++) {
22974:                                     if (singleserver.options[k] == addtarget) {
22975:                                         needsnew = 0;
22976:                                         break;
22977:                                     }
22978:                                 }
22979:                                 if (needsnew == 1) {
22980:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
22981:                                 }
22982:                             }
22983:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
22984:                                 singleserver.options.length = 0;
22985:                                 if ((currVal) && (currVal != removetarget)) {
22986:                                     singleserver.options[0] = new Option("","",false,false);
22987:                                 } else {
22988:                                     singleserver.options[0] = new Option("","",true,true);
22989:                                 }
22990:                                 var idx = 0;
22991:                                 for (var m=0; m<alltargets.length; m++) {
22992:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
22993:                                         idx ++;
22994:                                         if (currVal == alltargets[m]) {
22995:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
22996:                                         } else {
22997:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
22998:                                         }
22999:                                     }
23000:                                 }
23001:                             }
23002:                         }
23003:                     }
23004:                 }
23005:             }
23006:         }
23007:     }
23008:     return;
23009: }
23010: 
23011: // ]]>
23012: </script>
23013: 
23014: END
23015: }
23016: 
23017: sub new_spares_js {
23018:     my @sparestypes = ('primary','default');
23019:     my $types = join("','",@sparestypes);
23020:     my $select = &mt('Select');
23021:     return <<"END";
23022: 
23023: <script type="text/javascript">
23024: // <![CDATA[
23025: 
23026: function updateNewSpares(formname,lonhost) {
23027:     var types = new Array('$types');
23028:     var include = new Array();
23029:     var exclude = new Array();
23030:     for (var i=0; i<types.length; i++) {
23031:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
23032:         for (var j=0; j<spareboxes.length; j++) {
23033:             if (formname.elements[spareboxes[j]].checked) {
23034:                 exclude.push(formname.elements[spareboxes[j]].value);
23035:             } else {
23036:                 include.push(formname.elements[spareboxes[j]].value);
23037:             }
23038:         }
23039:     }
23040:     for (var i=0; i<types.length; i++) {
23041:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
23042:         var selIdx = newSpare.selectedIndex;
23043:         var currnew = newSpare.options[selIdx].value;
23044:         var okSpares = new Array();
23045:         for (var j=0; j<newSpare.options.length; j++) {
23046:             var possible = newSpare.options[j].value;
23047:             if (possible != '') {
23048:                 if (exclude.indexOf(possible) == -1) {
23049:                     okSpares.push(possible);
23050:                 } else {
23051:                     if (currnew == possible) {
23052:                         selIdx = 0;
23053:                     }
23054:                 }
23055:             }
23056:         }
23057:         for (var k=0; k<include.length; k++) {
23058:             if (okSpares.indexOf(include[k]) == -1) {
23059:                 okSpares.push(include[k]);
23060:             }
23061:         }
23062:         okSpares.sort();
23063:         newSpare.options.length = 0;
23064:         if (selIdx == 0) {
23065:             newSpare.options[0] = new Option("$select","",true,true);
23066:         } else {
23067:             newSpare.options[0] = new Option("$select","",false,false);
23068:         }
23069:         for (var m=0; m<okSpares.length; m++) {
23070:             var idx = m+1;
23071:             var selThis = 0;
23072:             if (selIdx != 0) {
23073:                 if (okSpares[m] == currnew) {
23074:                     selThis = 1;
23075:                 }
23076:             }
23077:             if (selThis == 1) {
23078:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
23079:             } else {
23080:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
23081:             }
23082:         }
23083:     }
23084:     return;
23085: }
23086: 
23087: function checkNewSpares(lonhost,type) {
23088:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
23089:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
23090:     if (chosen != '') {
23091:         var othertype;
23092:         var othernewSpare;
23093:         if (type == 'primary') {
23094:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
23095:         }
23096:         if (type == 'default') {
23097:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
23098:         }
23099:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
23100:             othernewSpare.selectedIndex = 0;
23101:         }
23102:     }
23103:     return;
23104: }
23105: 
23106: // ]]>
23107: </script>
23108: 
23109: END
23110: 
23111: }
23112: 
23113: sub common_domprefs_js {
23114:     return <<"END";
23115: 
23116: <script type="text/javascript">
23117: // <![CDATA[
23118: 
23119: function getIndicesByName(formname,item) {
23120:     var group = new Array();
23121:     for (var i=0;i<formname.elements.length;i++) {
23122:         if (formname.elements[i].name == item) {
23123:             group.push(formname.elements[i].id);
23124:         }
23125:     }
23126:     return group;
23127: }
23128: 
23129: // ]]>
23130: </script>
23131: 
23132: END
23133: 
23134: }
23135: 
23136: sub recaptcha_js {
23137:     my %lt = &captcha_phrases();
23138:     return <<"END";
23139: 
23140: <script type="text/javascript">
23141: // <![CDATA[
23142: 
23143: function updateCaptcha(caller,context) {
23144:     var privitem;
23145:     var pubitem;
23146:     var privtext;
23147:     var pubtext;
23148:     var versionitem;
23149:     var versiontext;
23150:     if (document.getElementById(context+'_recaptchapub')) {
23151:         pubitem = document.getElementById(context+'_recaptchapub');
23152:     } else {
23153:         return;
23154:     }
23155:     if (document.getElementById(context+'_recaptchapriv')) {
23156:         privitem = document.getElementById(context+'_recaptchapriv');
23157:     } else {
23158:         return;
23159:     }
23160:     if (document.getElementById(context+'_recaptchapubtxt')) {
23161:         pubtext = document.getElementById(context+'_recaptchapubtxt');
23162:     } else {
23163:         return;
23164:     }
23165:     if (document.getElementById(context+'_recaptchaprivtxt')) {
23166:         privtext = document.getElementById(context+'_recaptchaprivtxt');
23167:     } else {
23168:         return;
23169:     }
23170:     if (document.getElementById(context+'_recaptchaversion')) {
23171:         versionitem = document.getElementById(context+'_recaptchaversion');
23172:     } else {
23173:         return;
23174:     }
23175:     if (document.getElementById(context+'_recaptchavertxt')) {
23176:         versiontext = document.getElementById(context+'_recaptchavertxt');
23177:     } else {
23178:         return;
23179:     }
23180:     if (caller.checked) {
23181:         if (caller.value == 'recaptcha') {
23182:             pubitem.type = 'text';
23183:             privitem.type = 'text';
23184:             pubitem.size = '40';
23185:             privitem.size = '40';
23186:             pubtext.innerHTML = "$lt{'pub'}";
23187:             privtext.innerHTML = "$lt{'priv'}";
23188:             versionitem.type = 'text';
23189:             versionitem.size = '3';
23190:             versiontext.innerHTML = "$lt{'ver'}";
23191:         } else {
23192:             pubitem.type = 'hidden';
23193:             privitem.type = 'hidden';
23194:             versionitem.type = 'hidden';
23195:             pubtext.innerHTML = '';
23196:             privtext.innerHTML = '';
23197:             versiontext.innerHTML = '';
23198:         }
23199:     }
23200:     return;
23201: }
23202: 
23203: // ]]>
23204: </script>
23205: 
23206: END
23207: 
23208: }
23209: 
23210: sub toggle_display_js {
23211:     return <<"END";
23212: 
23213: <script type="text/javascript">
23214: // <![CDATA[
23215: 
23216: function toggleDisplay(domForm,caller) {
23217:     if (document.getElementById(caller)) {
23218:         var divitem = document.getElementById(caller);
23219:         var optionsElement = domForm.coursecredits;
23220:         var checkval = 1;
23221:         var dispval = 'block';
23222:         var selfcreateRegExp = /^cancreate_emailverified/;
23223:         if (caller == 'emailoptions') {
23224:             optionsElement = domForm.cancreate_email;
23225:         }
23226:         if (caller == 'studentsubmission') {
23227:             optionsElement = domForm.postsubmit;
23228:         }
23229:         if (caller == 'cloneinstcode') {
23230:             optionsElement = domForm.canclone;
23231:             checkval = 'instcode';
23232:         }
23233:         if (selfcreateRegExp.test(caller)) {
23234:             optionsElement = domForm.elements[caller];
23235:             checkval = 'other';
23236:             dispval = 'inline'
23237:         }
23238:         if (optionsElement.length) {
23239:             var currval;
23240:             for (var i=0; i<optionsElement.length; i++) {
23241:                 if (optionsElement[i].checked) {
23242:                    currval = optionsElement[i].value;
23243:                 }
23244:             }
23245:             if (currval == checkval) {
23246:                 divitem.style.display = dispval;
23247:             } else {
23248:                 divitem.style.display = 'none';
23249:             }
23250:         }
23251:     }
23252:     return;
23253: }
23254: 
23255: // ]]>
23256: </script>
23257: 
23258: END
23259: 
23260: }
23261: 
23262: sub captcha_phrases {
23263:     return &Apache::lonlocal::texthash (
23264:                  priv => 'Private key',
23265:                  pub  => 'Public key',
23266:                  original  => 'original (CAPTCHA)',
23267:                  recaptcha => 'successor (ReCAPTCHA)',
23268:                  notused   => 'unused',
23269:                  ver => 'ReCAPTCHA version (1 or 2)',
23270:     );
23271: }
23272: 
23273: sub devalidate_remote_domconfs {
23274:     my ($dom,$cachekeys) = @_;
23275:     return unless (ref($cachekeys) eq 'HASH');
23276:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
23277:     my %thismachine;
23278:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
23279:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
23280:                       'directorysrch','passwdconf','cats','proxyalias','proxysaml',
23281:                       'ipaccess','trust');
23282:     my %cache_by_lonhost;
23283:     if (exists($cachekeys->{'samllanding'})) {
23284:         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
23285:             my %landing = %{$cachekeys->{'samllanding'}};
23286:             my %domservers = &Apache::lonnet::get_servers($dom);
23287:             if (keys(%domservers)) {
23288:                 foreach my $server (keys(%domservers)) {
23289:                     my @cached;
23290:                     next if ($thismachine{$server});
23291:                     if ($landing{$server}) {
23292:                         push(@cached,&escape('samllanding').':'.&escape($server));
23293:                     }
23294:                     if (@cached) {
23295:                         $cache_by_lonhost{$server} = \@cached;
23296:                     }
23297:                 }
23298:             }
23299:         }
23300:     }
23301:     if (keys(%servers)) {
23302:         foreach my $server (keys(%servers)) {
23303:             next if ($thismachine{$server});
23304:             my @cached;
23305:             foreach my $name (@posscached) {
23306:                 if ($cachekeys->{$name}) {
23307:                     if (($name eq 'proxyalias') || ($name eq 'proxysaml')) {
23308:                         if (ref($cachekeys->{$name}) eq 'HASH') {
23309:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
23310:                                 push(@cached,&escape($name).':'.&escape($key));
23311:                             }
23312:                         }
23313:                     } else {
23314:                         push(@cached,&escape($name).':'.&escape($dom));
23315:                     }
23316:                 }
23317:             }
23318:             if ((exists($cache_by_lonhost{$server})) &&
23319:                 (ref($cache_by_lonhost{$server}) eq 'ARRAY')) {
23320:                 push(@cached,@{$cache_by_lonhost{$server}});
23321:             }
23322:             if (@cached) {
23323:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
23324:             }
23325:         }
23326:     }
23327:     return;
23328: }
23329: 
23330: 1;

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