File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.118.2.20: download - view: text, annotated - select for diffs
Tue Feb 27 16:47:14 2024 UTC (3 months, 2 weeks ago) by raeburn
Branches: version_2_11_4_msu
- For 2.11.4 (modified)
  Include changes in 1.429, 1.431, 1.435, 1.436

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.118.2.20 2024/02/27 16:47:14 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.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial, community, textbook, and lti).
  107: In each case the radio buttons allow the selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use Apache::lonuserutils();
  168: use Apache::loncoursequeueadmin();
  169: use Apache::courseprefs();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use File::Copy;
  174: use Locale::Language;
  175: use DateTime::TimeZone;
  176: use DateTime::Locale;
  177: use Time::HiRes qw( sleep );
  178: use Net::CIDR;
  179: use Crypt::CBC;
  180: 
  181: my $registered_cleanup;
  182: my $modified_urls;
  183: 
  184: sub handler {
  185:     my $r=shift;
  186:     if ($r->header_only) {
  187:         &Apache::loncommon::content_type($r,'text/html');
  188:         $r->send_http_header;
  189:         return OK;
  190:     }
  191: 
  192:     my $context = 'domain';
  193:     my $dom = $env{'request.role.domain'};
  194:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  195:     if (&Apache::lonnet::allowed('mau',$dom)) {
  196:         &Apache::loncommon::content_type($r,'text/html');
  197:         $r->send_http_header;
  198:     } else {
  199:         $env{'user.error.msg'}=
  200:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  201:         return HTTP_NOT_ACCEPTABLE;
  202:     }
  203: 
  204:     $registered_cleanup=0;
  205:     @{$modified_urls}=();
  206: 
  207:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  208:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  209:                                             ['phase','actions']);
  210:     my $phase = 'pickactions';
  211:     if ( exists($env{'form.phase'}) ) {
  212:         $phase = $env{'form.phase'};
  213:     }
  214:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  215:     my %domconfig =
  216:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  217:                 'quotas','autoenroll','autoupdate','autocreate',
  218:                 'directorysrch','usercreation','usermodification',
  219:                 'contacts','defaults','scantron','coursecategories',
  220:                 'serverstatuses','requestcourses','helpsettings',
  221:                 'coursedefaults','usersessions','loadbalancing',
  222:                 'requestauthor','selfenrollment','inststatus',
  223:                 'passwords','ltitools','toolsec','lti','ltisec',
  224:                 'wafproxy','ipaccess','authordefaults'],$dom);
  225:     my %encconfig =
  226:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti','linkprot'],$dom,undef,1);
  227:     my ($checked_is_home,$is_home);
  228:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  229:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  230:             my $home = &Apache::lonnet::domain($dom,'primary');
  231:             unless (($home eq 'no_host') || ($home eq '')) {
  232:                 my @ids=&Apache::lonnet::current_machine_ids();
  233:                 if (grep(/^\Q$home\E$/,@ids)) {
  234:                     $is_home = 1;
  235:                 }
  236:             }
  237:             $checked_is_home = 1;
  238:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  239:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  240:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  241:                     $domconfig{'ltitools'}{$id}{'key'} = $encconfig{'ltitools'}{$id}{'key'};
  242:                     if (($is_home) && ($phase eq 'process')) {
  243:                         $domconfig{'ltitools'}{$id}{'secret'} = $encconfig{'ltitools'}{$id}{'secret'};
  244:                     }
  245:                 }
  246:             }
  247:         }
  248:     }
  249:     if (ref($domconfig{'lti'}) eq 'HASH') {
  250:         if (ref($encconfig{'lti'}) eq 'HASH') {
  251:             unless ($checked_is_home) {
  252:                 my $home = &Apache::lonnet::domain($dom,'primary');
  253:                 unless (($home eq 'no_host') || ($home eq '')) {
  254:                     my @ids=&Apache::lonnet::current_machine_ids();
  255:                     if (grep(/^\Q$home\E$/,@ids)) {
  256:                         $is_home = 1;
  257:                     }
  258:                 }
  259:                 $checked_is_home = 1;
  260:             }
  261:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  262:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  263:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  264:                     $domconfig{'lti'}{$id}{'key'} = $encconfig{'lti'}{$id}{'key'};
  265:                     if (($is_home) && ($phase eq 'process')) {
  266:                         $domconfig{'lti'}{$id}{'secret'} = $encconfig{'lti'}{$id}{'secret'};
  267:                     }
  268:                 }
  269:             }
  270:         }
  271:     }
  272:     if (ref($domconfig{'ltisec'}) eq 'HASH') {
  273:         if (ref($domconfig{'ltisec'}{'linkprot'}) eq 'HASH') {
  274:             if (ref($encconfig{'linkprot'}) eq 'HASH') {
  275:                 foreach my $id (keys(%{$domconfig{'ltisec'}{'linkprot'}})) {
  276:                     unless ($id =~ /^\d+$/) {
  277:                         delete($domconfig{'ltisec'}{'linkprot'}{$id});
  278:                     }
  279:                     if ((ref($domconfig{'ltisec'}{'linkprot'}{$id}) eq 'HASH') &&
  280:                         (ref($encconfig{'linkprot'}{$id}) eq 'HASH')) {
  281:                         foreach my $item ('key','secret') {
  282:                             $domconfig{'ltisec'}{'linkprot'}{$id}{$item} = $encconfig{'linkprot'}{$id}{$item};
  283:                         }
  284:                     }
  285:                 }
  286:             }
  287:         }
  288:     }
  289:     my @prefs_order = ('rolecolors','login','ipaccess','defaults','wafproxy','passwords',
  290:                        'quotas','autoenroll','autoupdate','autocreate','directorysrch',
  291:                        'contacts','usercreation','selfcreation','usermodification',
  292:                        'scantron','requestcourses','requestauthor','coursecategories',
  293:                        'serverstatuses','helpsettings','coursedefaults',
  294:                        'authordefaults','ltitools','selfenrollment','usersessions','lti');
  295:     my %existing;
  296:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  297:         %existing = %{$domconfig{'loadbalancing'}};
  298:     }
  299:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  300:         push(@prefs_order,'loadbalancing');
  301:     }
  302:     my %prefs = (
  303:         'rolecolors' =>
  304:                    { text => 'Default color schemes',
  305:                      help => 'Domain_Configuration_Color_Schemes',
  306:                      header => [{col1 => 'Student Settings',
  307:                                  col2 => '',},
  308:                                 {col1 => 'Coordinator Settings',
  309:                                  col2 => '',},
  310:                                 {col1 => 'Author Settings',
  311:                                  col2 => '',},
  312:                                 {col1 => 'Administrator Settings',
  313:                                  col2 => '',}],
  314:                       print => \&print_rolecolors,
  315:                       modify => \&modify_rolecolors,
  316:                     },
  317:         'login' =>
  318:                     { text => 'Log-in page options',
  319:                       help => 'Domain_Configuration_Login_Page',
  320:                       header => [{col1 => 'Log-in Page Items',
  321:                                   col2 => '',},
  322:                                  {col1 => 'Log-in Help',
  323:                                   col2 => 'Value'},
  324:                                  {col1 => 'Custom HTML in document head',
  325:                                   col2 => 'Value'},
  326:                                  {col1 => 'SSO',
  327:                                   col2 => 'Dual login: SSO and non-SSO options'},
  328:                                 ],
  329:                       print => \&print_login,
  330:                       modify => \&modify_login,
  331:                     },
  332:         'defaults' => 
  333:                     { text => 'Default authentication/language/timezone/portal/types',
  334:                       help => 'Domain_Configuration_LangTZAuth',
  335:                       header => [{col1 => 'Setting',
  336:                                   col2 => 'Value'},
  337:                                  {col1 => 'Institutional user types',
  338:                                   col2 => 'Name displayed'},
  339:                                  {col1 => 'Mapping for missing usernames via standard log-in',
  340:                                   col2 => 'Rules in use'}],
  341:                       print => \&print_defaults,
  342:                       modify => \&modify_defaults,
  343:                     },
  344:         'wafproxy' =>
  345:                     { text => 'Web Application Firewall/Reverse Proxy',
  346:                       help => 'Domain_Configuration_WAF_Proxy',
  347:                       header => [{col1 => 'Domain(s)',
  348:                                   col2 => 'Servers and WAF/Reverse Proxy alias(es)',
  349:                                  },
  350:                                  {col1 => 'Domain(s)',
  351:                                   col2 => 'WAF Configuration',}],
  352:                       print => \&print_wafproxy,
  353:                       modify => \&modify_wafproxy,
  354:                     },
  355:         'passwords' =>
  356:                     { text => 'Passwords (Internal authentication)',
  357:                       help => 'Domain_Configuration_Passwords',
  358:                       header => [{col1 => 'Resetting Forgotten Password',
  359:                                   col2 => 'Settings'},
  360:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  361:                                   col2 => 'Settings'},
  362:                                  {col1 => 'Rules for LON-CAPA Passwords',
  363:                                   col2 => 'Settings'},
  364:                                  {col1 => 'Course Owner Changing Student Passwords',
  365:                                   col2 => 'Settings'}],
  366:                       print => \&print_passwords,
  367:                       modify => \&modify_passwords,
  368:                     },
  369:         'quotas' => 
  370:                     { text => 'Blogs, personal pages/timezones, portfolio/quotas',
  371:                       help => 'Domain_Configuration_Quotas',
  372:                       header => [{col1 => 'User affiliation',
  373:                                   col2 => 'Available tools',
  374:                                   col3 => 'Portfolio quota (MB)',}],
  375:                       print => \&print_quotas,
  376:                       modify => \&modify_quotas,
  377:                     },
  378:         'autoenroll' =>
  379:                    { text => 'Auto-enrollment settings',
  380:                      help => 'Domain_Configuration_Auto_Enrollment',
  381:                      header => [{col1 => 'Configuration setting',
  382:                                  col2 => 'Value(s)'}],
  383:                      print => \&print_autoenroll,
  384:                      modify => \&modify_autoenroll,
  385:                    },
  386:         'autoupdate' => 
  387:                    { text => 'Auto-update settings',
  388:                      help => 'Domain_Configuration_Auto_Updates',
  389:                      header => [{col1 => 'Setting',
  390:                                  col2 => 'Value',},
  391:                                 {col1 => 'Setting',
  392:                                  col2 => 'Affiliation'},
  393:                                 {col1 => 'User population',
  394:                                  col2 => 'Updatable user data'}],
  395:                      print => \&print_autoupdate,
  396:                      modify => \&modify_autoupdate,
  397:                   },
  398:         'autocreate' => 
  399:                   { text => 'Auto-course creation settings',
  400:                      help => 'Domain_Configuration_Auto_Creation',
  401:                      header => [{col1 => 'Configuration Setting',
  402:                                  col2 => 'Value',}],
  403:                      print => \&print_autocreate,
  404:                      modify => \&modify_autocreate,
  405:                   },
  406:         'directorysrch' => 
  407:                   { text => 'Directory searches',
  408:                     help => 'Domain_Configuration_InstDirectory_Search',
  409:                     header => [{col1 => 'Institutional Directory Setting',
  410:                                 col2 => 'Value',},
  411:                                {col1 => 'LON-CAPA Directory Setting',
  412:                                 col2 => 'Value',}],
  413:                     print => \&print_directorysrch,
  414:                     modify => \&modify_directorysrch,
  415:                   },
  416:         'contacts' =>
  417:                   { text => 'E-mail addresses and helpform',
  418:                     help => 'Domain_Configuration_Contact_Info',
  419:                     header => [{col1 => 'Default e-mail addresses',
  420:                                 col2 => 'Value',},
  421:                                {col1 => 'Recipient(s) for notifications',
  422:                                 col2 => 'Value',},
  423:                                {col1 => 'Nightly status check e-mail',
  424:                                 col2 => 'Settings',},
  425:                                {col1 => 'Ask helpdesk form settings',
  426:                                 col2 => 'Value',},],
  427:                     print => \&print_contacts,
  428:                     modify => \&modify_contacts,
  429:                   },
  430:         'usercreation' => 
  431:                   { text => 'User creation',
  432:                     help => 'Domain_Configuration_User_Creation',
  433:                     header => [{col1 => 'Format rule type',
  434:                                 col2 => 'Format rules in force'},
  435:                                {col1 => 'User account creation',
  436:                                 col2 => 'Usernames which may be created',},
  437:                                {col1 => 'Context',
  438:                                 col2 => 'Assignable authentication types'}],
  439:                     print => \&print_usercreation,
  440:                     modify => \&modify_usercreation,
  441:                   },
  442:         'selfcreation' => 
  443:                   { text => 'Users self-creating accounts',
  444:                     help => 'Domain_Configuration_Self_Creation', 
  445:                     header => [{col1 => 'Self-creation with institutional username',
  446:                                 col2 => 'Enabled?'},
  447:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  448:                                 col2 => 'Information user can enter'},
  449:                                {col1 => 'Self-creation with e-mail verification',
  450:                                 col2 => 'Settings'}],
  451:                     print => \&print_selfcreation,
  452:                     modify => \&modify_selfcreation,
  453:                   },
  454:         'usermodification' =>
  455:                   { text => 'User modification',
  456:                     help => 'Domain_Configuration_User_Modification',
  457:                     header => [{col1 => 'Target user has role',
  458:                                 col2 => 'User information updatable in author context'},
  459:                                {col1 => 'Target user has role',
  460:                                 col2 => 'User information updatable in course context'}],
  461:                     print => \&print_usermodification,
  462:                     modify => \&modify_usermodification,
  463:                   },
  464:         'scantron' =>
  465:                   { text => 'Bubblesheet format',
  466:                     help => 'Domain_Configuration_Scantron_Format',
  467:                     header => [ {col1 => 'Bubblesheet format file',
  468:                                  col2 => ''},
  469:                                 {col1 => 'Bubblesheet data upload formats',
  470:                                  col2 => 'Settings'}],
  471:                     print => \&print_scantron,
  472:                     modify => \&modify_scantron,
  473:                   },
  474:         'requestcourses' => 
  475:                  {text => 'Request creation of courses',
  476:                   help => 'Domain_Configuration_Request_Courses',
  477:                   header => [{col1 => 'User affiliation',
  478:                               col2 => 'Availability/Processing of requests',},
  479:                              {col1 => 'Setting',
  480:                               col2 => 'Value'},
  481:                              {col1 => 'Available textbooks',
  482:                               col2 => ''},
  483:                              {col1 => 'Available templates',
  484:                               col2 => ''},
  485:                              {col1 => 'Validation (not official courses)',
  486:                               col2 => 'Value'},],
  487:                   print => \&print_quotas,
  488:                   modify => \&modify_quotas,
  489:                  },
  490:         'requestauthor' =>
  491:                  {text => 'Request Authoring Space',
  492:                   help => 'Domain_Configuration_Request_Author',
  493:                   header => [{col1 => 'User affiliation',
  494:                               col2 => 'Availability/Processing of requests',},
  495:                              {col1 => 'Setting',
  496:                               col2 => 'Value'}],
  497:                   print => \&print_quotas,
  498:                   modify => \&modify_quotas,
  499:                  },
  500:         'coursecategories' =>
  501:                   { text => 'Cataloging of courses/communities',
  502:                     help => 'Domain_Configuration_Cataloging_Courses',
  503:                     header => [{col1 => 'Catalog type/availability',
  504:                                 col2 => '',},
  505:                                {col1 => 'Category settings for standard catalog',
  506:                                 col2 => '',},
  507:                                {col1 => 'Categories',
  508:                                 col2 => '',
  509:                                }],
  510:                     print => \&print_coursecategories,
  511:                     modify => \&modify_coursecategories,
  512:                   },
  513:         'serverstatuses' =>
  514:                  {text   => 'Access to server status pages',
  515:                   help   => 'Domain_Configuration_Server_Status',
  516:                   header => [{col1 => 'Status Page',
  517:                               col2 => 'Other named users',
  518:                               col3 => 'Specific IPs',
  519:                             }],
  520:                   print => \&print_serverstatuses,
  521:                   modify => \&modify_serverstatuses,
  522:                  },
  523:         'helpsettings' =>
  524:                  {text   => 'Support settings',
  525:                   help   => 'Domain_Configuration_Help_Settings',
  526:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  527:                               col2 => 'Value'},
  528:                              {col1 => 'Helpdesk Roles',
  529:                               col2 => 'Settings'},],
  530:                   print  => \&print_helpsettings,
  531:                   modify => \&modify_helpsettings,
  532:                  },
  533:         'coursedefaults' => 
  534:                  {text => 'Course/Community defaults',
  535:                   help => 'Domain_Configuration_Course_Defaults',
  536:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  537:                               col2 => 'Value',},
  538:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  539:                               col2 => 'Value',},],
  540:                   print => \&print_coursedefaults,
  541:                   modify => \&modify_coursedefaults,
  542:                  },
  543:         'selfenrollment' => 
  544:                  {text   => 'Self-enrollment in Course/Community',
  545:                   help   => 'Domain_Configuration_Selfenrollment',
  546:                   header => [{col1 => 'Configuration Rights',
  547:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  548:                              {col1 => 'Defaults',
  549:                               col2 => 'Value'},
  550:                              {col1 => 'Self-enrollment validation (optional)',
  551:                               col2 => 'Value'},],
  552:                   print => \&print_selfenrollment,
  553:                   modify => \&modify_selfenrollment,
  554:                  },
  555:         'usersessions' =>
  556:                  {text  => 'User session hosting/offloading',
  557:                   help  => 'Domain_Configuration_User_Sessions',
  558:                   header => [{col1 => 'Domain server',
  559:                               col2 => 'Servers to offload sessions to when busy'},
  560:                              {col1 => 'Hosting of users from other domains',
  561:                               col2 => 'Rules'},
  562:                              {col1 => "Hosting domain's own users elsewhere",
  563:                               col2 => 'Rules'}],
  564:                   print => \&print_usersessions,
  565:                   modify => \&modify_usersessions,
  566:                  },
  567:         'loadbalancing' =>
  568:                  {text  => 'Dedicated Load Balancer(s)',
  569:                   help  => 'Domain_Configuration_Load_Balancing',
  570:                   header => [{col1 => 'Balancers',
  571:                               col2 => 'Default destinations',
  572:                               col3 => 'User affiliation',
  573:                               col4 => 'Overrides'},
  574:                             ],
  575:                   print => \&print_loadbalancing,
  576:                   modify => \&modify_loadbalancing,
  577:                  },
  578:         'ltitools' =>
  579:                  {text => 'External Tools (LTI)',
  580:                   help => 'Domain_Configuration_LTI_Tools',
  581:                   header => [{col1 => 'Encryption of shared secrets',
  582:                               col2 => 'Settings'},
  583:                              {col1 => 'Rules for shared secrets',
  584:                               col2 => 'Settings'},
  585:                              {col1 => 'Providers',
  586:                               col2 => 'Settings',}],
  587:                   print => \&print_ltitools,
  588:                   modify => \&modify_ltitools,
  589:                  },
  590:         'lti' =>
  591:                  {text => 'LTI Link Protection and LTI Consumers',
  592:                   help => 'Domain_Configuration_LTI_Provider',
  593:                   header => [{col1 => 'Encryption of shared secrets',
  594:                               col2 => 'Settings'},
  595:                              {col1 => 'Rules for shared secrets',
  596:                               col2 => 'Settings'},
  597:                              {col1 => 'Link Protectors in Courses',
  598:                               col2 => 'Values'},
  599:                              {col1 => 'Link Protectors',
  600:                               col2 => 'Settings'},
  601:                              {col1 => 'Consumers',
  602:                               col2 => 'Settings'},],
  603:                   print => \&print_lti,
  604:                   modify => \&modify_lti,
  605:                  },
  606:         'ipaccess' =>
  607:                        {text => 'IP-based access control',
  608:                         help => 'Domain_Configuration_IP_Access',
  609:                         header => [{col1 => 'Setting',
  610:                                     col2 => 'Value'},],
  611:                         print  => \&print_ipaccess,
  612:                         modify => \&modify_ipaccess,
  613:                        },
  614:         'authordefaults' =>
  615:                             {text => 'Authoring Space defaults',
  616:                              help => 'Domain_Configuration_Author_Defaults',
  617:                              header => [{col1 => 'Defaults which can be overridden by Author',
  618:                                          col2 => 'Settings',},
  619:                                         {col1 => 'Defaults which can be overridden by a Dom. Coord.',
  620:                                          col2 => 'Settings',},],
  621:                              print => \&print_authordefaults,
  622:                              modify => \&modify_authordefaults,
  623:                             },
  624:     );
  625:     if (keys(%servers) > 1) {
  626:         $prefs{'login'}  = { text   => 'Log-in page options',
  627:                              help   => 'Domain_Configuration_Login_Page',
  628:                             header => [{col1 => 'Log-in Service',
  629:                                         col2 => 'Server Setting',},
  630:                                        {col1 => 'Log-in Page Items',
  631:                                         col2 => 'Settings'},
  632:                                        {col1 => 'Log-in Help',
  633:                                         col2 => 'Value'},
  634:                                        {col1 => 'Custom HTML in document head',
  635:                                         col2 => 'Value'},
  636:                                        {col1 => 'SSO',
  637:                                         col2 => 'Dual login: SSO and non-SSO options'},
  638:                                       ],
  639:                             print => \&print_login,
  640:                             modify => \&modify_login,
  641:                            };
  642:     }
  643: 
  644:     my @roles = ('student','coordinator','author','admin');
  645:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  646:     &Apache::lonhtmlcommon::add_breadcrumb
  647:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  648:       text=>"Settings to display/modify"});
  649:     my $confname = $dom.'-domainconfig';
  650: 
  651:     if ($phase eq 'process') {
  652:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  653:                                                               \%prefs,\%domconfig,$confname,\@roles);
  654:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  655:             $r->rflush();
  656:             &devalidate_remote_domconfs($dom,$result);
  657:         }
  658:     } elsif ($phase eq 'display') {
  659:         my $js = &recaptcha_js().
  660:                  &toggle_display_js();
  661:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  662:             my ($othertitle,$usertypes,$types) =
  663:                 &Apache::loncommon::sorted_inst_types($dom);
  664:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  665:                                           $domconfig{'loadbalancing'}).
  666:                    &new_spares_js().
  667:                    &common_domprefs_js().
  668:                    &Apache::loncommon::javascript_array_indexof();
  669:         }
  670:         if (grep(/^requestcourses$/,@actions)) {
  671:             my $javascript_validations;
  672:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  673:             $js .= <<END;
  674: <script type="text/javascript">
  675: $javascript_validations
  676: </script>
  677: $coursebrowserjs
  678: END
  679:         } elsif (grep(/^ipaccess$/,@actions)) {
  680:             $js .= &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
  681:         }
  682:         if (grep(/^selfcreation$/,@actions)) {
  683:             $js .= &selfcreate_javascript();
  684:         }
  685:         if (grep(/^contacts$/,@actions)) {
  686:             $js .= &contacts_javascript();
  687:         }
  688:         if (grep(/^scantron$/,@actions)) {
  689:             $js .= &scantron_javascript();
  690:         }
  691:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  692:     } else {
  693: # check if domconfig user exists for the domain.
  694:         my $servadm = $r->dir_config('lonAdmEMail');
  695:         my ($configuserok,$author_ok,$switchserver) =
  696:             &config_check($dom,$confname,$servadm);
  697:         unless ($configuserok eq 'ok') {
  698:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  699:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  700:                           $confname).
  701:                       '<br />'
  702:             );
  703:             if ($switchserver) {
  704:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  705:                           '<br />'.
  706:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  707:                           '<br />'.
  708:                           &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).
  709:                           '<br />'.
  710:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  711:                 );
  712:             } else {
  713:                 $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.').
  714:                           '<br />'.
  715:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  716:                 );
  717:             }
  718:             $r->print(&Apache::loncommon::end_page());
  719:             return OK;
  720:         }
  721:         if (keys(%domconfig) == 0) {
  722:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  723:             my @ids=&Apache::lonnet::current_machine_ids();
  724:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  725:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  726:                 my @loginimages = ('img','logo','domlogo','login');
  727:                 my $custom_img_count = 0;
  728:                 foreach my $img (@loginimages) {
  729:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  730:                         $custom_img_count ++;
  731:                     }
  732:                 }
  733:                 foreach my $role (@roles) {
  734:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  735:                         $custom_img_count ++;
  736:                     }
  737:                 }
  738:                 if ($custom_img_count > 0) {
  739:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  740:                     my $switch_server = &check_switchserver($dom,$confname);
  741:                     $r->print(
  742:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  743:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  744:     &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 />'.
  745:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  746:                     if ($switch_server) {
  747:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  748:                     }
  749:                     $r->print(&Apache::loncommon::end_page());
  750:                     return OK;
  751:                 }
  752:             }
  753:         }
  754:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  755:     }
  756:     return OK;
  757: }
  758: 
  759: sub process_changes {
  760:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  761:     my %domconfig;
  762:     if (ref($values) eq 'HASH') {
  763:         %domconfig = %{$values};
  764:     }
  765:     my $output;
  766:     if ($action eq 'login') {
  767:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  768:     } elsif ($action eq 'rolecolors') {
  769:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  770:                                      $lastactref,%domconfig);
  771:     } elsif ($action eq 'quotas') {
  772:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  773:     } elsif ($action eq 'autoenroll') {
  774:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  775:     } elsif ($action eq 'autoupdate') {
  776:         $output = &modify_autoupdate($dom,%domconfig);
  777:     } elsif ($action eq 'autocreate') {
  778:         $output = &modify_autocreate($dom,%domconfig);
  779:     } elsif ($action eq 'directorysrch') {
  780:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  781:     } elsif ($action eq 'usercreation') {
  782:         $output = &modify_usercreation($dom,%domconfig);
  783:     } elsif ($action eq 'selfcreation') {
  784:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  785:     } elsif ($action eq 'usermodification') {
  786:         $output = &modify_usermodification($dom,%domconfig);
  787:     } elsif ($action eq 'contacts') {
  788:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  789:     } elsif ($action eq 'defaults') {
  790:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  791:     } elsif ($action eq 'scantron') {
  792:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  793:     } elsif ($action eq 'coursecategories') {
  794:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  795:     } elsif ($action eq 'serverstatuses') {
  796:         $output = &modify_serverstatuses($dom,%domconfig);
  797:     } elsif ($action eq 'requestcourses') {
  798:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  799:     } elsif ($action eq 'requestauthor') {
  800:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  801:     } elsif ($action eq 'helpsettings') {
  802:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  803:     } elsif ($action eq 'coursedefaults') {
  804:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  805:     } elsif ($action eq 'selfenrollment') {
  806:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  807:     } elsif ($action eq 'usersessions') {
  808:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  809:     } elsif ($action eq 'loadbalancing') {
  810:         $output = &modify_loadbalancing($dom,%domconfig);
  811:     } elsif ($action eq 'ltitools') {
  812:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  813:     } elsif ($action eq 'lti') {
  814:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  815:     } elsif ($action eq 'passwords') {
  816:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  817:     } elsif ($action eq 'wafproxy') {
  818:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  819:     } elsif ($action eq 'ipaccess') {
  820:         $output = &modify_ipaccess($dom,$lastactref,%domconfig);
  821:     } elsif ($action eq 'authordefaults') {
  822:         $output = &modify_authordefaults($dom,$lastactref,%domconfig);
  823:     }
  824:     return $output;
  825: }
  826: 
  827: sub print_config_box {
  828:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  829:     my $rowtotal = 0;
  830:     my $output;
  831:     if ($action eq 'coursecategories') {
  832:         $output = &coursecategories_javascript($settings);
  833:     } elsif ($action eq 'defaults') {
  834:         $output = &defaults_javascript($settings); 
  835:     } elsif ($action eq 'passwords') {
  836:         $output = &passwords_javascript($action);
  837:     } elsif ($action eq 'helpsettings') {
  838:         my (%privs,%levelscurrent);
  839:         my %full=();
  840:         my %levels=(
  841:                      course => {},
  842:                      domain => {},
  843:                      system => {},
  844:                    );
  845:         my $context = 'domain';
  846:         my $crstype = 'Course';
  847:         my $formname = 'display';
  848:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  849:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  850:         $output =
  851:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  852:                                                       \@templateroles);
  853:     } elsif ($action eq 'ltitools') {
  854:         $output .= &Apache::lonconfigsettings::ltitools_javascript($settings);
  855:     } elsif ($action eq 'lti') {
  856:         $output .= &passwords_javascript('ltisecrets')."\n".
  857:                    &lti_javascript($dom,$settings);
  858:     } elsif ($action eq 'wafproxy') {
  859:         $output .= &wafproxy_javascript($dom);
  860:     } elsif ($action eq 'autoupdate') {
  861:         $output .= &autoupdate_javascript();
  862:     } elsif ($action eq 'autoenroll') {
  863:         $output .= &autoenroll_javascript();
  864:     } elsif ($action eq 'login') {
  865:         $output .= &saml_javascript();
  866:     } elsif ($action eq 'ipaccess') {
  867:         $output .= &ipaccess_javascript($settings);
  868:     } elsif ($action eq 'authordefaults') {
  869:         $output .= &authordefaults_javascript();
  870:     }
  871:     $output .=
  872:          '<table class="LC_nested_outer">
  873:           <tr>
  874:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  875:            &mt($item->{text}).'&nbsp;'.
  876:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  877:           '</tr>';
  878:     $rowtotal ++;
  879:     my $numheaders = 1;
  880:     if (ref($item->{'header'}) eq 'ARRAY') {
  881:         $numheaders = scalar(@{$item->{'header'}});
  882:     }
  883:     if ($numheaders > 1) {
  884:         my $colspan = '';
  885:         my $rightcolspan = '';
  886:         my $leftnobr = '';
  887:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  888:             ($action eq 'directorysrch') ||
  889:             (($action eq 'login') && ($numheaders < 5))) {
  890:             $colspan = ' colspan="2"';
  891:         }
  892:         if ($action eq 'usersessions') {
  893:             $rightcolspan = ' colspan="3"'; 
  894:         }
  895:         if ($action eq 'passwords') {
  896:             $leftnobr = ' LC_nobreak';
  897:         }
  898:         $output .= '
  899:           <tr>
  900:            <td>
  901:             <table class="LC_nested">
  902:              <tr class="LC_info_row">
  903:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  904:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  905:              </tr>';
  906:         $rowtotal ++;
  907:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  908:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  909:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  910:             ($action eq 'helpsettings') || ($action eq 'contacts') || ($action eq 'wafproxy') ||
  911:             ($action eq 'lti') || ($action eq 'ltitools') || ($action eq 'authordefaults')) {
  912:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  913:         } elsif ($action eq 'passwords') {
  914:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  915:         } elsif ($action eq 'coursecategories') {
  916:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  917:         } elsif ($action eq 'scantron') {
  918:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  919:         } elsif ($action eq 'login') {
  920:             if ($numheaders == 5) {
  921:                 $colspan = ' colspan="2"';
  922:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  923:             } else {
  924:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  925:             }
  926:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  927:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  928:         } elsif ($action eq 'rolecolors') {
  929:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  930:         }
  931:         $output .= '
  932:            </table>
  933:           </td>
  934:          </tr>
  935:          <tr>
  936:            <td>
  937:             <table class="LC_nested">
  938:              <tr class="LC_info_row">
  939:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  940:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  941:              </tr>';
  942:             $rowtotal ++;
  943:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  944:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  945:             ($action eq 'usersessions') || ($action eq 'coursecategories') ||
  946:             ($action eq 'contacts') || ($action eq 'passwords') || 
  947:             ($action eq 'defaults') || ($action eq 'lti') ||
  948:             ($action eq 'ltitools')) {
  949:             if ($action eq 'coursecategories') {
  950:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  951:                 $colspan = ' colspan="2"';
  952:             } elsif ($action eq 'passwords') {
  953:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  954:             } elsif ($action eq 'lti') {
  955:                 $output .= $item->{'print'}->('upper',$dom,$settings,\$rowtotal).'
  956:                            </table>
  957:                           </td>
  958:                          </tr>
  959:                          <tr>
  960:                           <td>
  961:                           <table class="LC_nested">
  962:                            <tr class="LC_info_row">
  963:                             <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  964:                             <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  965:                            </tr>'."\n".
  966:                            $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  967:             } else {
  968:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  969:             }
  970:             my $hdridx = 2;
  971:             if ($action eq 'lti') {
  972:                 $hdridx = 3;
  973:             }
  974:             $output .= '
  975:            </table>
  976:           </td>
  977:          </tr>
  978:          <tr>
  979:            <td>
  980:             <table class="LC_nested">
  981:              <tr class="LC_info_row">
  982:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col1'}).'</td>
  983:               <td class="LC_right_item">'.&mt($item->{'header'}->[$hdridx]->{'col2'}).'</td>
  984:              </tr>'."\n";
  985:             if ($action eq 'coursecategories') {
  986:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  987:             } elsif (($action eq 'contacts') || ($action eq 'passwords')) {
  988:                 if ($action eq 'passwords') {
  989:                     $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
  990:                 } else {
  991:                     $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
  992:                 }
  993:                 $hdridx ++;
  994:                 $output .= '
  995:              </tr>
  996:             </table>
  997:            </td>
  998:           </tr>
  999:           <tr>
 1000:            <td>
 1001:             <table class="LC_nested">
 1002:              <tr class="LC_info_row">
 1003:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col1'}).'</td>
 1004:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col2'}).'</td></tr>'."\n";
 1005:                 if ($action eq 'passwords') {
 1006:                     $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
 1007:                 } else {
 1008:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1009:                 }
 1010:                 $output .= '
 1011:             </table>
 1012:           </td>
 1013:          </tr>
 1014:          <tr>';
 1015:             } else {
 1016:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1017:             }
 1018:             $rowtotal ++;
 1019:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
 1020:                  ($action eq 'directorysrch') || ($action eq 'helpsettings') ||
 1021:                  ($action eq 'wafproxy') || ($action eq 'authordefaults')) {
 1022:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1023:         } elsif ($action eq 'scantron') {
 1024:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1025:         } elsif ($action eq 'login') {
 1026:             if ($numheaders == 5) {
 1027:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1028:            </table>
 1029:           </td>
 1030:          </tr>
 1031:          <tr>
 1032:            <td>
 1033:             <table class="LC_nested">
 1034:              <tr class="LC_info_row">
 1035:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1036:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1037:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1038:                 $rowtotal ++;
 1039:             } else {
 1040:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1041:             }
 1042:             $output .= '
 1043:            </table>
 1044:           </td>
 1045:          </tr>
 1046:          <tr>
 1047:            <td>
 1048:             <table class="LC_nested">
 1049:              <tr class="LC_info_row">';
 1050:             if ($numheaders == 5) {
 1051:                 $output .= '
 1052:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1053:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1054:              </tr>';
 1055:             } else {
 1056:                 $output .= '
 1057:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1058:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1059:              </tr>';
 1060:             }
 1061:             $rowtotal ++;
 1062:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal).'
 1063:            </table>
 1064:           </td>
 1065:          </tr>
 1066:          <tr>
 1067:            <td>
 1068:             <table class="LC_nested">
 1069:              <tr class="LC_info_row">';
 1070:             if ($numheaders == 5) {
 1071:                 $output .= '
 1072:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1073:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1074:              </tr>';
 1075:             } else {
 1076:                 $output .= '
 1077:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1078:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1079:              </tr>';
 1080:             }
 1081:             $rowtotal ++;
 1082:             $output .= &print_login('saml',$dom,$confname,$phase,$settings,\$rowtotal);
 1083:         } elsif ($action eq 'requestcourses') {
 1084:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1085:             $rowtotal ++;
 1086:             $output .= &print_studentcode($settings,\$rowtotal).'
 1087:            </table>
 1088:           </td>
 1089:          </tr>
 1090:          <tr>
 1091:            <td>
 1092:             <table class="LC_nested">
 1093:              <tr class="LC_info_row">
 1094:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1095:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1096:                        &textbookcourses_javascript($settings).
 1097:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1098:             </table>
 1099:            </td>
 1100:           </tr>
 1101:          <tr>
 1102:            <td>
 1103:             <table class="LC_nested">
 1104:              <tr class="LC_info_row">
 1105:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1106:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1107:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1108:             </table>
 1109:            </td>
 1110:           </tr>
 1111:           <tr>
 1112:            <td>
 1113:             <table class="LC_nested">
 1114:              <tr class="LC_info_row">
 1115:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1116:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1117:              </tr>'.
 1118:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1119:         } elsif ($action eq 'requestauthor') {
 1120:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1121:             $rowtotal ++;
 1122:         } elsif ($action eq 'rolecolors') {
 1123:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1124:            </table>
 1125:           </td>
 1126:          </tr>
 1127:          <tr>
 1128:            <td>
 1129:             <table class="LC_nested">
 1130:              <tr class="LC_info_row">
 1131:               <td class="LC_left_item"'.$colspan.' valign="top">'.
 1132:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1133:               <td class="LC_right_item" valign="top">'.
 1134:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1135:              </tr>'.
 1136:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1137:            </table>
 1138:           </td>
 1139:          </tr>
 1140:          <tr>
 1141:            <td>
 1142:             <table class="LC_nested">
 1143:              <tr class="LC_info_row">
 1144:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1145:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1146:              </tr>'.
 1147:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1148:             $rowtotal += 2;
 1149:         }
 1150:     } else {
 1151:         $output .= '
 1152:           <tr>
 1153:            <td>
 1154:             <table class="LC_nested">
 1155:              <tr class="LC_info_row">';
 1156:         if ($action eq 'login') {
 1157:             $output .= '  
 1158:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1159:         } elsif ($action eq 'serverstatuses') {
 1160:             $output .= '
 1161:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1162:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1163: 
 1164:         } else {
 1165:             $output .= '
 1166:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1167:         }
 1168:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1169:             $output .= '<td class="LC_left_item" valign="top">'.
 1170:                        &mt($item->{'header'}->[0]->{'col2'});
 1171:             if ($action eq 'serverstatuses') {
 1172:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1173:             } 
 1174:         } else {
 1175:             $output .= '<td class="LC_right_item" valign="top">'.
 1176:                        &mt($item->{'header'}->[0]->{'col2'});
 1177:         }
 1178:         $output .= '</td>';
 1179:         if ($item->{'header'}->[0]->{'col3'}) {
 1180:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1181:                 $output .= '<td class="LC_left_item" valign="top">'.
 1182:                             &mt($item->{'header'}->[0]->{'col3'});
 1183:             } else {
 1184:                 $output .= '<td class="LC_right_item" valign="top">'.
 1185:                            &mt($item->{'header'}->[0]->{'col3'});
 1186:             }
 1187:             if ($action eq 'serverstatuses') {
 1188:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1189:             }
 1190:             $output .= '</td>';
 1191:         }
 1192:         if ($item->{'header'}->[0]->{'col4'}) {
 1193:             $output .= '<td class="LC_right_item" valign="top">'.
 1194:                        &mt($item->{'header'}->[0]->{'col4'});
 1195:         }
 1196:         $output .= '</tr>';
 1197:         $rowtotal ++;
 1198:         if ($action eq 'quotas') {
 1199:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1200:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1201:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') ||
 1202:                  ($action eq 'ipaccess')) {
 1203:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1204:         }
 1205:     }
 1206:     $output .= '
 1207:    </table>
 1208:   </td>
 1209:  </tr>
 1210: </table><br />';
 1211:     return ($output,$rowtotal);
 1212: }
 1213: 
 1214: sub print_login {
 1215:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1216:     my ($css_class,$datatable,$switchserver,%lt);
 1217:     my %choices = &login_choices();
 1218:     if (($caller eq 'help') || ($caller eq 'headtag') || ($caller eq 'saml')) {
 1219:         %lt = &login_file_options();
 1220:         $switchserver = &check_switchserver($dom,$confname);
 1221:     }
 1222:     if ($caller eq 'service') {
 1223:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1224:         my $choice = $choices{'disallowlogin'};
 1225:         $css_class = ' class="LC_odd_row"';
 1226:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1227:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1228:                       '<th>'.$choices{'server'}.'</th>'.
 1229:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1230:                       '<th>'.$choices{'custompath'}.'</th>'.
 1231:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1232:         my %disallowed;
 1233:         if (ref($settings) eq 'HASH') {
 1234:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1235:                %disallowed = %{$settings->{'loginvia'}};
 1236:             }
 1237:         }
 1238:         foreach my $lonhost (sort(keys(%servers))) {
 1239:             my $direct = 'selected="selected"';
 1240:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1241:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1242:                     $direct = '';
 1243:                 }
 1244:             }
 1245:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1246:                           '<td><select name="'.$lonhost.'_server">'.
 1247:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1248:                           '</option>';
 1249:             foreach my $hostid (sort(keys(%servers))) {
 1250:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1251:                 my $selected = '';
 1252:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1253:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1254:                         $selected = 'selected="selected"';
 1255:                     }
 1256:                 }
 1257:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1258:                               $servers{$hostid}.'</option>';
 1259:             }
 1260:             $datatable .= '</select></td>'.
 1261:                           '<td><select name="'.$lonhost.'_serverpath">';
 1262:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1263:                 my $pathname = $path;
 1264:                 if ($path eq 'custom') {
 1265:                     $pathname = &mt('Custom Path').' ->';
 1266:                 }
 1267:                 my $selected = '';
 1268:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1269:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1270:                         $selected = 'selected="selected"';
 1271:                     }
 1272:                 } elsif ($path eq '') {
 1273:                     $selected = 'selected="selected"';
 1274:                 }
 1275:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1276:             }
 1277:             $datatable .= '</select></td>';
 1278:             my ($custom,$exempt);
 1279:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1280:                 $custom = $disallowed{$lonhost}{'custompath'};
 1281:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1282:             }
 1283:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1284:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1285:                           '</tr>';
 1286:         }
 1287:         $datatable .= '</table></td></tr>';
 1288:         return $datatable;
 1289:     } elsif ($caller eq 'page') {
 1290:         my %defaultchecked = ( 
 1291:                                'coursecatalog' => 'on',
 1292:                                'helpdesk'      => 'on',
 1293:                                'adminmail'     => 'off',
 1294:                                'newuser'       => 'off',
 1295:                              );
 1296:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1297:         my (%checkedon,%checkedoff);
 1298:         foreach my $item (@toggles) {
 1299:             if ($defaultchecked{$item} eq 'on') { 
 1300:                 $checkedon{$item} = ' checked="checked" ';
 1301:                 $checkedoff{$item} = ' ';
 1302:             } elsif ($defaultchecked{$item} eq 'off') {
 1303:                 $checkedoff{$item} = ' checked="checked" ';
 1304:                 $checkedon{$item} = ' ';
 1305:             }
 1306:         }
 1307:         my @images = ('img','logo','domlogo','login');
 1308:         my @alttext = ('img','logo','domlogo');
 1309:         my @logintext = ('textcol','bgcol');
 1310:         my @bgs = ('pgbg','mainbg','sidebg');
 1311:         my @links = ('link','alink','vlink');
 1312:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1313:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1314:         my (%is_custom,%designs);
 1315:         my %defaults = (
 1316:                        font => $defaultdesign{'login.font'},
 1317:                        );
 1318:         foreach my $item (@images) {
 1319:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1320:             $defaults{'showlogo'}{$item} = 1;
 1321:         }
 1322:         foreach my $item (@bgs) {
 1323:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1324:         }
 1325:         foreach my $item (@logintext) {
 1326:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1327:         }
 1328:         foreach my $item (@links) {
 1329:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1330:         }
 1331:         if (ref($settings) eq 'HASH') {
 1332:             foreach my $item (@toggles) {
 1333:                 if ($settings->{$item} eq '1') {
 1334:                     $checkedon{$item} =  ' checked="checked" ';
 1335:                     $checkedoff{$item} = ' ';
 1336:                 } elsif ($settings->{$item} eq '0') {
 1337:                     $checkedoff{$item} =  ' checked="checked" ';
 1338:                     $checkedon{$item} = ' ';
 1339:                 }
 1340:             }
 1341:             foreach my $item (@images) {
 1342:                 if (defined($settings->{$item})) {
 1343:                     $designs{$item} = $settings->{$item};
 1344:                     $is_custom{$item} = 1;
 1345:                 }
 1346:                 if (defined($settings->{'showlogo'}{$item})) {
 1347:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1348:                 }
 1349:             }
 1350:             foreach my $item (@alttext) {
 1351:                 if (ref($settings->{'alttext'}) eq 'HASH') {
 1352:                     if ($settings->{'alttext'}->{$item} ne '') {
 1353:                         $designs{'alttext'}{$item} = $settings->{'alttext'}{$item};
 1354:                     }
 1355:                 }
 1356:             }
 1357:             foreach my $item (@logintext) {
 1358:                 if ($settings->{$item} ne '') {
 1359:                     $designs{'logintext'}{$item} = $settings->{$item};
 1360:                     $is_custom{$item} = 1;
 1361:                 }
 1362:             }
 1363:             if ($settings->{'font'} ne '') {
 1364:                 $designs{'font'} = $settings->{'font'};
 1365:                 $is_custom{'font'} = 1;
 1366:             }
 1367:             foreach my $item (@bgs) {
 1368:                 if ($settings->{$item} ne '') {
 1369:                     $designs{'bgs'}{$item} = $settings->{$item};
 1370:                     $is_custom{$item} = 1;
 1371:                 }
 1372:             }
 1373:             foreach my $item (@links) {
 1374:                 if ($settings->{$item} ne '') {
 1375:                     $designs{'links'}{$item} = $settings->{$item};
 1376:                     $is_custom{$item} = 1;
 1377:                 }
 1378:             }
 1379:         } else {
 1380:             if ($designhash{$dom.'.login.font'} ne '') {
 1381:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1382:                 $is_custom{'font'} = 1;
 1383:             }
 1384:             foreach my $item (@images) {
 1385:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1386:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1387:                     $is_custom{$item} = 1;
 1388:                 }
 1389:             }
 1390:             foreach my $item (@bgs) {
 1391:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1392:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1393:                     $is_custom{$item} = 1;
 1394:                 }
 1395:             }
 1396:             foreach my $item (@links) {
 1397:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1398:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1399:                     $is_custom{$item} = 1;
 1400:                 }
 1401:             }
 1402:         }
 1403:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1404:                                                       logo => 'Institution Logo',
 1405:                                                       domlogo => 'Domain Logo',
 1406:                                                       login => 'Login box');
 1407:         my $itemcount = 1;
 1408:         foreach my $item (@toggles) {
 1409:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1410:             $datatable .=  
 1411:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1412:                 '</td><td>'.
 1413:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1414:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1415:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1416:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1417:                 '</tr>';
 1418:             $itemcount ++;
 1419:         }
 1420:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1421:         $datatable .= '</tr></table></td></tr>';
 1422:     } elsif ($caller eq 'help') {
 1423:         my ($defaulturl,$defaulttype,%url,%type,%langchoices);
 1424:         my $itemcount = 1;
 1425:         $defaulturl = '/adm/loginproblems.html';
 1426:         $defaulttype = 'default';
 1427:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1428:         my @currlangs;
 1429:         if (ref($settings) eq 'HASH') {
 1430:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1431:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1432:                     next if ($settings->{'helpurl'}{$key} eq '');
 1433:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1434:                     $type{$key} = 'custom';
 1435:                     unless ($key eq 'nolang') {
 1436:                         push(@currlangs,$key);
 1437:                     }
 1438:                 }
 1439:             } elsif ($settings->{'helpurl'} ne '') {
 1440:                 $type{'nolang'} = 'custom';
 1441:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1442:             }
 1443:         }
 1444:         foreach my $lang ('nolang',sort(@currlangs)) {
 1445:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1446:             $datatable .= '<tr'.$css_class.'>';
 1447:             if ($url{$lang} eq '') {
 1448:                 $url{$lang} = $defaulturl;
 1449:             }
 1450:             if ($type{$lang} eq '') {
 1451:                 $type{$lang} = $defaulttype;
 1452:             }
 1453:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1454:             if ($lang eq 'nolang') {
 1455:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1456:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1457:             } else {
 1458:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1459:                                   $langchoices{$lang},
 1460:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1461:             }
 1462:             $datatable .= '</span></td>'."\n".
 1463:                           '<td class="LC_left_item">';
 1464:             if ($type{$lang} eq 'custom') {
 1465:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1466:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1467:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1468:             } else {
 1469:                 $datatable .= $lt{'upl'};
 1470:             }
 1471:             $datatable .='<br />';
 1472:             if ($switchserver) {
 1473:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1474:             } else {
 1475:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1476:             }
 1477:             $datatable .= '</td></tr>';
 1478:             $itemcount ++;
 1479:         }
 1480:         my @addlangs;
 1481:         foreach my $lang (sort(keys(%langchoices))) {
 1482:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1483:             push(@addlangs,$lang);
 1484:         }
 1485:         if (@addlangs > 0) {
 1486:             my %toadd;
 1487:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1488:             $toadd{''} = &mt('Select');
 1489:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1490:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1491:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1492:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1493:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1494:             if ($switchserver) {
 1495:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1496:             } else {
 1497:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1498:             }
 1499:             $datatable .= '</td></tr>';
 1500:             $itemcount ++;
 1501:         }
 1502:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1503:     } elsif ($caller eq 'headtag') {
 1504:         my %domservers = &Apache::lonnet::get_servers($dom);
 1505:         my $choice = $choices{'headtag'};
 1506:         $css_class = ' class="LC_odd_row"';
 1507:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1508:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1509:                       '<th>'.$choices{'current'}.'</th>'.
 1510:                       '<th>'.$choices{'action'}.'</th>'.
 1511:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1512:         my (%currurls,%currexempt);
 1513:         if (ref($settings) eq 'HASH') {
 1514:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1515:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1516:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1517:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1518:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1519:                     }
 1520:                 }
 1521:             }
 1522:         }
 1523:         foreach my $lonhost (sort(keys(%domservers))) {
 1524:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1525:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1526:             if ($currurls{$lonhost}) {
 1527:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1528:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1529:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1530:                               '">'.$lt{'curr'}.'</a></td>'.
 1531:                               '<td><span class="LC_nobreak"><label>'.
 1532:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1533:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1534:             } else {
 1535:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1536:             }
 1537:             $datatable .='<br />';
 1538:             if ($switchserver) {
 1539:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1540:             } else {
 1541:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1542:             }
 1543:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1544:         }
 1545:         $datatable .= '</table></td></tr>';
 1546:     } elsif ($caller eq 'saml') {
 1547:         my %domservers = &Apache::lonnet::get_servers($dom);
 1548:         $datatable .= '<tr><td colspan="3" style="text-align: left">'.
 1549:                       '<table><tr><th>'.$choices{'hostid'}.'</th>'.
 1550:                       '<th>'.$choices{'samllanding'}.'</th>'.
 1551:                       '<th>'.$choices{'samloptions'}.'</th></tr>'."\n";
 1552:         my (%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso,%styleon,%styleoff);
 1553:         foreach my $lonhost (keys(%domservers)) {
 1554:             $samlurl{$lonhost} = '/adm/sso';
 1555:             $styleon{$lonhost} = 'display:none';
 1556:             $styleoff{$lonhost} = '';
 1557:         }
 1558:         if ((ref($settings) eq 'HASH') && (ref($settings->{'saml'}) eq 'HASH')) {
 1559:             foreach my $lonhost (keys(%{$settings->{'saml'}})) {
 1560:                 if (ref($settings->{'saml'}{$lonhost}) eq 'HASH') {
 1561:                     $saml{$lonhost} = 1;
 1562:                     $samltext{$lonhost} = $settings->{'saml'}{$lonhost}{'text'};
 1563:                     $samlimg{$lonhost} = $settings->{'saml'}{$lonhost}{'img'};
 1564:                     $samlalt{$lonhost} = $settings->{'saml'}{$lonhost}{'alt'};
 1565:                     $samlurl{$lonhost} = $settings->{'saml'}{$lonhost}{'url'};
 1566:                     $samltitle{$lonhost} = $settings->{'saml'}{$lonhost}{'title'};
 1567:                     $samlwindow{$lonhost} = $settings->{'saml'}{$lonhost}{'window'};
 1568:                     $samlnotsso{$lonhost} = $settings->{'saml'}{$lonhost}{'notsso'};
 1569:                     $styleon{$lonhost} = '';
 1570:                     $styleoff{$lonhost} = 'display:none';
 1571:                 } else {
 1572:                     $styleon{$lonhost} = 'display:none';
 1573:                     $styleoff{$lonhost} = '';
 1574:                 }
 1575:             }
 1576:         }
 1577:         my $itemcount = 1;
 1578:         foreach my $lonhost (sort(keys(%domservers))) {
 1579:             my $samlon = ' ';
 1580:             my $samloff = ' checked="checked" ';
 1581:             if ($saml{$lonhost}) {
 1582:                 $samlon = $samloff;
 1583:                 $samloff = ' ';
 1584:             }
 1585:             my $samlwinon = '';
 1586:             my $samlwinoff = ' checked="checked"';
 1587:             if ($samlwindow{$lonhost}) {
 1588:                 $samlwinon = $samlwinoff;
 1589:                 $samlwinoff = '';
 1590:             }
 1591:             my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1592:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.$domservers{$lonhost}.'</span></td>'.
 1593:                           '<td><span class="LC_nobreak"><label><input type="radio" name="saml_'.$lonhost.'"'.$samloff.
 1594:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="0" />'.
 1595:                           &mt('No').'</label>'.('&nbsp;'x2).
 1596:                           '<label><input type="radio" name="saml_'.$lonhost.'"'.$samlon.
 1597:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="1" />'.
 1598:                           &mt('Yes').'</label></span></td>'.
 1599:                           '<td id="samloptionson_'.$lonhost.'" style="'.$styleon{$lonhost}.'" width="100%">'.
 1600:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th></tr>'.
 1601:                           '<tr><th>'.&mt('Text').'</th><th>'.&mt('Image').'</th>'.
 1602:                           '<th>'.&mt('Alt Text').'</th></tr>'.
 1603:                           '<tr'.$css_class.'><td><input type="text" name="saml_text_'.$lonhost.'" size="20" value="'.
 1604:                           $samltext{$lonhost}.'" /></td><td>';
 1605:             if ($samlimg{$lonhost}) {
 1606:                 $datatable .= '<img src="'.$samlimg{$lonhost}.'" /><br />'.
 1607:                               '<span class="LC_nobreak"><label>'.
 1608:                               '<input type="checkbox" name="saml_img_del" value="'.$lonhost.'" />'.
 1609:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1610:             } else {
 1611:                 $datatable .= $lt{'upl'};
 1612:             }
 1613:             $datatable .='<br />';
 1614:             if ($switchserver) {
 1615:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1616:             } else {
 1617:                 $datatable .= '<input type="file" name="saml_img_'.$lonhost.'" />';
 1618:             }
 1619:             $datatable .= '</td>'.
 1620:                           '<td><input type="text" name="saml_alt_'.$lonhost.'" size="25" '.
 1621:                           'value="'.$samlalt{$lonhost}.'" /></td></tr></table><br />'.
 1622:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th><th align="center">'.
 1623:                           '<span class="LC_nobreak">'.&mt('Non-SSO').'</span></th></tr>'.
 1624:                           '<tr><th>'.&mt('URL').'</th><th>'.&mt('Tool Tip').'</th>'.
 1625:                           '<th>'.&mt('Pop-up if iframe').'</th><th>'.&mt('Text').'</th></tr>'.
 1626:                           '<tr'.$css_class.'>'.
 1627:                           '<td><input type="text" name="saml_url_'.$lonhost.'" size="30" '.
 1628:                           'value="'.$samlurl{$lonhost}.'" /></td>'.
 1629:                           '<td><textarea name="saml_title_'.$lonhost.'" rows="3" cols="20">'.
 1630:                           $samltitle{$lonhost}.'</textarea></td>'.
 1631:                           '<td><label><input type="radio" name="saml_window_'.$lonhost.'" value=""'.$samlwinoff.'>'.
 1632:                           &mt('No').'</label>'.('&nbsp;'x2).'<label><input type="radio" '.
 1633:                           'name="saml_window_'.$lonhost.'" value="1"'.$samlwinon.'>'.&mt('Yes').'</label></td>'.
 1634:                           '<td><input type="text" name="saml_notsso_'.$lonhost.'" size="12" '.
 1635:                           'value="'.$samlnotsso{$lonhost}.'" /></td></tr>'.
 1636:                           '</table></td>'.
 1637:                           '<td id="samloptionsoff_'.$lonhost.'" style="'.$styleoff{$lonhost}.'" width="100%">&nbsp;</td></tr>';
 1638:            $itemcount ++;
 1639:         }
 1640:         $datatable .= '</table></td></tr>';
 1641:     }
 1642:     return $datatable;
 1643: }
 1644: 
 1645: sub login_choices {
 1646:     my %choices =
 1647:         &Apache::lonlocal::texthash (
 1648:             coursecatalog => 'Display Course/Community Catalog link?',
 1649:             adminmail     => "Display Administrator's E-mail Address?",
 1650:             helpdesk      => 'Display "Contact Helpdesk" link',
 1651:             disallowlogin => "Login page requests redirected",
 1652:             hostid        => "Server",
 1653:             server        => "Redirect to:",
 1654:             serverpath    => "Path",
 1655:             custompath    => "Custom", 
 1656:             exempt        => "Exempt IP(s)",
 1657:             directlogin   => "No redirect",
 1658:             newuser       => "Link to create a user account",
 1659:             img           => "Header",
 1660:             logo          => "Main Logo",
 1661:             domlogo       => "Domain Logo",
 1662:             login         => "Log-in Header", 
 1663:             textcol       => "Text color",
 1664:             bgcol         => "Box color",
 1665:             bgs           => "Background colors",
 1666:             links         => "Link colors",
 1667:             font          => "Font color",
 1668:             pgbg          => "Header",
 1669:             mainbg        => "Page",
 1670:             sidebg        => "Login box",
 1671:             link          => "Link",
 1672:             alink         => "Active link",
 1673:             vlink         => "Visited link",
 1674:             headtag       => "Custom markup",
 1675:             action        => "Action",
 1676:             current       => "Current",
 1677:             samllanding   => "Dual login?",
 1678:             samloptions   => "Options",
 1679:             alttext       => "Alt text",
 1680:         );
 1681:     return %choices;
 1682: }
 1683: 
 1684: sub login_file_options {
 1685:       return &Apache::lonlocal::texthash(
 1686:                                            del     => 'Delete?',
 1687:                                            rep     => 'Replace:',
 1688:                                            upl     => 'Upload:',
 1689:                                            curr    => 'View contents',
 1690:                                            default => 'Default',
 1691:                                            custom  => 'Custom',
 1692:                                            none    => 'None',
 1693:       );
 1694: }
 1695: 
 1696: sub print_ipaccess {
 1697:     my ($dom,$settings,$rowtotal) = @_;
 1698:     my $css_class;
 1699:     my $itemcount = 0;
 1700:     my $datatable;
 1701:     my %ordered;
 1702:     if (ref($settings) eq 'HASH') {
 1703:         foreach my $item (keys(%{$settings})) {
 1704:             if (ref($settings->{$item}) eq 'HASH') {
 1705:                 my $num = $settings->{$item}{'order'};
 1706:                 if ($num eq '') {
 1707:                     $num = scalar(keys(%{$settings}));
 1708:                 }
 1709:                 $ordered{$num} = $item;
 1710:             }
 1711:         }
 1712:     }
 1713:     my $maxnum = scalar(keys(%ordered));
 1714:     if (keys(%ordered)) {
 1715:         my @items = sort { $a <=> $b } keys(%ordered);
 1716:         for (my $i=0; $i<@items; $i++) {
 1717:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1718:             my $item = $ordered{$items[$i]};
 1719:             my ($name,$ipranges,%commblocks,%courses);
 1720:             if (ref($settings->{$item}) eq 'HASH') {
 1721:                 $name = $settings->{$item}->{'name'};
 1722:                 $ipranges = $settings->{$item}->{'ip'};
 1723:                 if (ref($settings->{$item}->{'commblocks'}) eq 'HASH') {
 1724:                     %commblocks = %{$settings->{$item}->{'commblocks'}};
 1725:                 }
 1726:                 if (ref($settings->{$item}->{'courses'}) eq 'HASH') {
 1727:                     %courses = %{$settings->{$item}->{'courses'}};
 1728:                 }
 1729:             }
 1730:             my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_".$item."'".');"';
 1731:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 1732:                          .'<select name="ipaccess_pos_'.$item.'"'.$chgstr.'>';
 1733:             for (my $k=0; $k<=$maxnum; $k++) {
 1734:                 my $vpos = $k+1;
 1735:                 my $selstr;
 1736:                 if ($k == $i) {
 1737:                     $selstr = ' selected="selected" ';
 1738:                 }
 1739:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1740:             }
 1741:             $datatable .= '</select>'.('&nbsp;'x2).
 1742:                 '<label><input type="checkbox" name="ipaccess_del" value="'.$item.'" />'.
 1743:                 &mt('Delete?').'</label></span></td>'.
 1744:                 '<td colspan="2"><input type="hidden" name="ipaccess_id_'.$i.'" value="'.$item.'" />'.
 1745:                 &ipaccess_options($i,$itemcount,$dom,$name,$ipranges,\%commblocks,\%courses).
 1746:                 '</td></tr>';
 1747:             $itemcount ++;
 1748:         }
 1749:     }
 1750:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1751:     my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_add'".');"';
 1752:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 1753:                   '<input type="hidden" name="ipaccess_maxnum" value="'.$maxnum.'" />'."\n".
 1754:                   '<select name="ipaccess_pos_add"'.$chgstr.'>';
 1755:     for (my $k=0; $k<$maxnum+1; $k++) {
 1756:         my $vpos = $k+1;
 1757:         my $selstr;
 1758:         if ($k == $maxnum) {
 1759:             $selstr = ' selected="selected" ';
 1760:         }
 1761:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1762:     }
 1763:     $datatable .= '</select>&nbsp;'."\n".
 1764:                   '<input type="checkbox" name="ipaccess_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 1765:                   '<td colspan="2">'.
 1766:                   &ipaccess_options('add',$itemcount,$dom).
 1767:                   '</td>'."\n".
 1768:                   '</tr>'."\n";
 1769:     $$rowtotal ++;
 1770:     return $datatable;
 1771: }
 1772: 
 1773: sub ipaccess_options {
 1774:     my ($num,$itemcount,$dom,$name,$ipranges,$blocksref,$coursesref) = @_;
 1775:     my (%currblocks,%currcourses,$output);
 1776:     if (ref($blocksref) eq 'HASH') {
 1777:         %currblocks = %{$blocksref};
 1778:     }
 1779:     if (ref($coursesref) eq 'HASH') {
 1780:         %currcourses = %{$coursesref};
 1781:     }
 1782:     $output = '<fieldset><legend>'.&mt('Location(s)').'</legend>'.
 1783:               '<span class="LC_nobreak">'.&mt('Name').':&nbsp;'.
 1784:               '<input type="text" name="ipaccess_name_'.$num.'" value="'.$name.'" />'.
 1785:               '</span></fieldset>'.
 1786:               '<fieldset><legend>'.&mt('IP Range(s)').'</legend>'.
 1787:               &mt('Format for each IP range').': '.&mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 1788:               &mt('Range(s) will be stored as IP netblock(s) in CIDR notation (comma separated)').'<br />'.
 1789:               '<textarea name="ipaccess_range_'.$num.'" rows="3" cols="80">'.
 1790:               $ipranges.'</textarea></fieldset>'.
 1791:               '<fieldset><legend>'.&mt('Functionality Blocked?').'</legend>'.
 1792:               &blocker_checkboxes($num,$blocksref).'</fieldset>'.
 1793:               '<fieldset><legend>'.&mt('Courses/Communities allowed').'</legend>'.
 1794:               '<table>';
 1795:     foreach my $cid (sort(keys(%currcourses))) {
 1796:         my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
 1797:         $output .= '<tr><td><span class="LC_nobreak">'.
 1798:                    '<label><input type="checkbox" name="ipaccess_course_delete_'.$num.'" value="'.$cid.'" />'.
 1799:                    &mt('Delete?').'&nbsp;<span class="LC_cusr_emph">'.$courseinfo{'description'}.'</span></label></span>'.
 1800:                    ' <span class="LC_fontsize_medium">('.$cid.')</span></td></tr>';
 1801:     }
 1802:     $output .= '<tr><td><span class="LC_nobreak">'.&mt('Add').':&nbsp;'.
 1803:                '<input type="text" name="ipaccess_cdesc_'.$num.'" value="" onfocus="this.blur();opencrsbrowser('."'display','ipaccess_cnum_$num','ipaccess_cdom_$num','ipaccess_cdesc_$num'".');" />'.
 1804:                 &Apache::loncommon::selectcourse_link('display','ipaccess_cnum_'.$num,'ipaccess_cdom_'.$num,'ipaccess_cdesc_'.$num,$dom,undef,'Course/Community').
 1805:                '<input type="hidden" name="ipaccess_cnum_'.$num.'" value="" />'.
 1806:                '<input type="hidden" name="ipaccess_cdom_'.$num.'" value="" />'.
 1807:                '</span></td></tr></table>'."\n".
 1808:                '</fieldset>';
 1809:     return $output;
 1810: }
 1811: 
 1812: sub blocker_checkboxes {
 1813:     my ($num,$blocks) = @_;
 1814:     my ($typeorder,$types) = &commblocktype_text();
 1815:     my $numinrow = 6;
 1816:     my $output = '<table>';
 1817:     for (my $i=0; $i<@{$typeorder}; $i++) {
 1818:         my $block = $typeorder->[$i];
 1819:         my $blockstatus;
 1820:         if (ref($blocks) eq 'HASH') {
 1821:             if ($blocks->{$block} eq 'on') {
 1822:                 $blockstatus = 'checked="checked"';
 1823:             }
 1824:         }
 1825:         my $rem = $i%($numinrow);
 1826:         if ($rem == 0) {
 1827:             if ($i > 0) {
 1828:                 $output .= '</tr>';
 1829:             }
 1830:             $output .= '<tr>';
 1831:         }
 1832:         if ($i == scalar(@{$typeorder})-1) {
 1833:             my $colsleft = $numinrow-$rem;
 1834:             if ($colsleft > 1) {
 1835:                 $output .= '<td colspan="'.$colsleft.'">';
 1836:             } else {
 1837:                 $output .= '<td>';
 1838:             }
 1839:         } else {
 1840:             $output .= '<td>';
 1841:         }
 1842:         my $item = 'ipaccess_block_'.$num;
 1843:         if ($blockstatus) {
 1844:             $blockstatus = ' '.$blockstatus;
 1845:         }
 1846:         $output .= '<span class="LC_nobreak"><label>'."\n".
 1847:                    '<input type="checkbox" name="'.$item.'"'.
 1848:                    $blockstatus.' value="'.$block.'"'.' />'.
 1849:                    $types->{$block}.'</label></span>'."\n".
 1850:                    '<br /></td>';
 1851:     }
 1852:     $output .= '</tr></table>';
 1853:     return $output;
 1854: }
 1855: 
 1856: sub commblocktype_text {
 1857:     my %types = &Apache::lonlocal::texthash(
 1858:         'com' => 'Messaging',
 1859:         'chat' => 'Chat Room',
 1860:         'boards' => 'Discussion',
 1861:         'port' => 'Portfolio',
 1862:         'groups' => 'Groups',
 1863:         'blogs' => 'Blogs',
 1864:         'about' => 'User Information',
 1865:         'printout' => 'Printouts',
 1866:         'passwd' => 'Change Password',
 1867:         'grades' => 'Gradebook',
 1868:         'search' => 'Course search',
 1869:         'wishlist' => 'Stored links',
 1870:         'annotate' => 'Annotations',
 1871:     );
 1872:     my $typeorder = ['com','chat','boards','port','groups','blogs','about','wishlist','printout','grades','search','annotate','passwd'];
 1873:     return ($typeorder,\%types);
 1874: }
 1875: 
 1876: sub print_rolecolors {
 1877:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1878:     my %choices = &color_font_choices();
 1879:     my @bgs = ('pgbg','tabbg','sidebg');
 1880:     my @links = ('link','alink','vlink');
 1881:     my @images = ('img');
 1882:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1883:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1884:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1885:     my (%is_custom,%designs);
 1886:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1887:     if (ref($settings) eq 'HASH') {
 1888:         if (ref($settings->{$role}) eq 'HASH') {
 1889:             if ($settings->{$role}->{'img'} ne '') {
 1890:                 $designs{'img'} = $settings->{$role}->{'img'};
 1891:                 $is_custom{'img'} = 1;
 1892:             }
 1893:             if ($settings->{$role}->{'font'} ne '') {
 1894:                 $designs{'font'} = $settings->{$role}->{'font'};
 1895:                 $is_custom{'font'} = 1;
 1896:             }
 1897:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1898:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1899:                 $is_custom{'fontmenu'} = 1;
 1900:             }
 1901:             foreach my $item (@bgs) {
 1902:                 if ($settings->{$role}->{$item} ne '') {
 1903:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1904:                     $is_custom{$item} = 1;
 1905:                 }
 1906:             }
 1907:             foreach my $item (@links) {
 1908:                 if ($settings->{$role}->{$item} ne '') {
 1909:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1910:                     $is_custom{$item} = 1;
 1911:                 }
 1912:             }
 1913:         }
 1914:     } else {
 1915:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1916:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1917:             $is_custom{'img'} = 1;
 1918:         }
 1919:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1920:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1921:             $is_custom{'fontmenu'} = 1; 
 1922:         }
 1923:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1924:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1925:             $is_custom{'font'} = 1;
 1926:         }
 1927:         foreach my $item (@bgs) {
 1928:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1929:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1930:                 $is_custom{$item} = 1;
 1931:             
 1932:             }
 1933:         }
 1934:         foreach my $item (@links) {
 1935:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1936:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1937:                 $is_custom{$item} = 1;
 1938:             }
 1939:         }
 1940:     }
 1941:     my $itemcount = 1;
 1942:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1943:     $datatable .= '</tr></table></td></tr>';
 1944:     return $datatable;
 1945: }
 1946: 
 1947: sub role_defaults {
 1948:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1949:     my %defaults;
 1950:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1951:         return %defaults;
 1952:     }
 1953:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1954:     if ($role eq 'login') {
 1955:         %defaults = (
 1956:                        font => $defaultdesign{$role.'.font'},
 1957:                     );
 1958:         if (ref($logintext) eq 'ARRAY') {
 1959:             foreach my $item (@{$logintext}) {
 1960:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1961:             }
 1962:         }
 1963:         foreach my $item (@{$images}) {
 1964:             $defaults{'showlogo'}{$item} = 1;
 1965:         }
 1966:     } else {
 1967:         %defaults = (
 1968:                        img => $defaultdesign{$role.'.img'},
 1969:                        font => $defaultdesign{$role.'.font'},
 1970:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1971:                     );
 1972:     }
 1973:     foreach my $item (@{$bgs}) {
 1974:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1975:     }
 1976:     foreach my $item (@{$links}) {
 1977:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1978:     }
 1979:     foreach my $item (@{$images}) {
 1980:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1981:     }
 1982:     return %defaults;
 1983: }
 1984: 
 1985: sub display_color_options {
 1986:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1987:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1988:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1989:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1990:     my $datatable = '<tr'.$css_class.'>'.
 1991:         '<td>'.$choices->{'font'}.'</td>';
 1992:     if (!$is_custom->{'font'}) {
 1993:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1994:     } else {
 1995:         $datatable .= '<td>&nbsp;</td>';
 1996:     }
 1997:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1998: 
 1999:     $datatable .= '<td><span class="LC_nobreak">'.
 2000:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 2001:                   ' value="'.$current_color.'" />&nbsp;'.
 2002:                   '&nbsp;</span></td></tr>';
 2003:     unless ($role eq 'login') { 
 2004:         $datatable .= '<tr'.$css_class.'>'.
 2005:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 2006:         if (!$is_custom->{'fontmenu'}) {
 2007:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 2008:         } else {
 2009:             $datatable .= '<td>&nbsp;</td>';
 2010:         }
 2011: 	$current_color = $designs->{'fontmenu'} ?
 2012: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 2013:         $datatable .= '<td><span class="LC_nobreak">'.
 2014:                       '<input class="colorchooser" type="text" size="10" name="'
 2015: 		      .$role.'_fontmenu"'.
 2016:                       ' value="'.$current_color.'" />&nbsp;'.
 2017:                       '&nbsp;</span></td></tr>';
 2018:     }
 2019:     my $switchserver = &check_switchserver($dom,$confname);
 2020:     foreach my $img (@{$images}) {
 2021: 	$itemcount ++;
 2022:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2023:         $datatable .= '<tr'.$css_class.'>'.
 2024:                       '<td>'.$choices->{$img};
 2025:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors,$alttext);
 2026:         if ($role eq 'login') {
 2027:             if ($img eq 'login') {
 2028:                 $login_hdr_pick =
 2029:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 2030:                 $logincolors =
 2031:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 2032:                                        $designs,$defaults);
 2033:             } else {
 2034:                 if ($img ne 'domlogo') {
 2035:                     $datatable.= &logo_display_options($img,$defaults,$designs);
 2036:                 }
 2037:                 if (ref($designs->{'alttext'}) eq 'HASH') {
 2038:                     $alttext = $designs->{'alttext'}{$img};
 2039:                 }
 2040:             }
 2041:         }
 2042:         $datatable .= '</td>';
 2043:         if ($designs->{$img} ne '') {
 2044:             $imgfile = $designs->{$img};
 2045: 	    $img_import = ($imgfile =~ m{^/adm/});
 2046:         } else {
 2047:             $imgfile = $defaults->{$img};
 2048:         }
 2049:         if ($imgfile) {
 2050:             my ($showfile,$fullsize);
 2051:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 2052:                 my $urldir = $1;
 2053:                 my $filename = $2;
 2054:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 2055:                 if (@info) {
 2056:                     my $thumbfile = 'tn-'.$filename;
 2057:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 2058:                     if (@thumb) {
 2059:                         $showfile = $urldir.'/'.$thumbfile;
 2060:                     } else {
 2061:                         $showfile = $imgfile;
 2062:                     }
 2063:                 } else {
 2064:                     $showfile = '';
 2065:                 }
 2066:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 2067:                 $showfile = $imgfile;
 2068:                 my $imgdir = $1;
 2069:                 my $filename = $2;
 2070:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 2071:                     $showfile = "/$imgdir/tn-".$filename;
 2072:                 } else {
 2073:                     my $input = $londocroot.$imgfile;
 2074:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 2075:                     if (!-e $output) {
 2076:                         my ($width,$height) = &thumb_dimensions();
 2077:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 2078:                         if ($fullwidth ne '' && $fullheight ne '') {
 2079:                             if ($fullwidth > $width && $fullheight > $height) { 
 2080:                                 my $size = $width.'x'.$height;
 2081:                                 my @args = ('convert','-sample',$size,$input,$output);
 2082:                                 system({$args[0]} @args);
 2083:                                 $showfile = "/$imgdir/tn-".$filename;
 2084:                             }
 2085:                         }
 2086:                     }
 2087:                 }
 2088:             }
 2089:             if ($showfile) {
 2090:                 if ($showfile =~ m{^/(adm|res)/}) {
 2091:                     if ($showfile =~ m{^/res/}) {
 2092:                         my $local_showfile =
 2093:                             &Apache::lonnet::filelocation('',$showfile);
 2094:                         &Apache::lonnet::repcopy($local_showfile);
 2095:                     }
 2096:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 2097:                 }
 2098:                 if ($imgfile) {
 2099:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 2100:                         if ($imgfile =~ m{^/res/}) {
 2101:                             my $local_imgfile =
 2102:                                 &Apache::lonnet::filelocation('',$imgfile);
 2103:                             &Apache::lonnet::repcopy($local_imgfile);
 2104:                         }
 2105:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 2106:                     } else {
 2107:                         $fullsize = $imgfile;
 2108:                     }
 2109:                 }
 2110:                 $datatable .= '<td>';
 2111:                 if ($img eq 'login') {
 2112:                     $datatable .= $login_hdr_pick;
 2113:                 } 
 2114:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 2115:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 2116:             } else {
 2117:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2118:                               &mt('Upload:').'<br />';
 2119:             }
 2120:         } else {
 2121:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2122:                           &mt('Upload:').'<br />';
 2123:         }
 2124:         if ($switchserver) {
 2125:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2126:         } else {
 2127:             if ($img ne 'login') { # suppress file selection for Log-in header
 2128:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 2129:             }
 2130:         }
 2131:         if (($role eq 'login') && ($img ne 'login')) {
 2132:             $datatable .= ('&nbsp;' x2).' <span class="LC_nobreak"><label>'.$choices->{'alttext'}.':'.
 2133:                           '<input type="text" name="'.$role.'_alt_'.$img.'" size="10" value="'.$alttext.'" />'.
 2134:                           '</label></span>';
 2135:         }
 2136:         $datatable .= '</td></tr>';
 2137:     }
 2138:     $itemcount ++;
 2139:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2140:     $datatable .= '<tr'.$css_class.'>'.
 2141:                   '<td>'.$choices->{'bgs'}.'</td>';
 2142:     my $bgs_def;
 2143:     foreach my $item (@{$bgs}) {
 2144:         if (!$is_custom->{$item}) {
 2145:             $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>';
 2146:         }
 2147:     }
 2148:     if ($bgs_def) {
 2149:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 2150:     } else {
 2151:         $datatable .= '<td>&nbsp;</td>';
 2152:     }
 2153:     $datatable .= '<td class="LC_right_item">'.
 2154:                   '<table border="0"><tr>';
 2155: 
 2156:     foreach my $item (@{$bgs}) {
 2157:         $datatable .= '<td align="center">'.$choices->{$item};
 2158: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 2159:         if ($designs->{'bgs'}{$item}) {
 2160:             $datatable .= '&nbsp;';
 2161:         }
 2162:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2163:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2164:     }
 2165:     $datatable .= '</tr></table></td></tr>';
 2166:     $itemcount ++;
 2167:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2168:     $datatable .= '<tr'.$css_class.'>'.
 2169:                   '<td>'.$choices->{'links'}.'</td>';
 2170:     my $links_def;
 2171:     foreach my $item (@{$links}) {
 2172:         if (!$is_custom->{$item}) {
 2173:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 2174:         }
 2175:     }
 2176:     if ($links_def) {
 2177:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 2178:     } else {
 2179:         $datatable .= '<td>&nbsp;</td>';
 2180:     }
 2181:     $datatable .= '<td class="LC_right_item">'.
 2182:                   '<table border="0"><tr>';
 2183:     foreach my $item (@{$links}) {
 2184: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 2185:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 2186:         if ($designs->{'links'}{$item}) {
 2187:             $datatable.='&nbsp;';
 2188:         }
 2189:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 2190:                       '" /></td>';
 2191:     }
 2192:     $$rowtotal += $itemcount;
 2193:     return $datatable;
 2194: }
 2195: 
 2196: sub logo_display_options {
 2197:     my ($img,$defaults,$designs) = @_;
 2198:     my $checkedon;
 2199:     if (ref($defaults) eq 'HASH') {
 2200:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 2201:             if ($defaults->{'showlogo'}{$img}) {
 2202:                 $checkedon = 'checked="checked" ';     
 2203:             }
 2204:         } 
 2205:     }
 2206:     if (ref($designs) eq 'HASH') {
 2207:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 2208:             if (defined($designs->{'showlogo'}{$img})) {
 2209:                 if ($designs->{'showlogo'}{$img} == 0) {
 2210:                     $checkedon = '';
 2211:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 2212:                     $checkedon = 'checked="checked" ';
 2213:                 }
 2214:             }
 2215:         }
 2216:     }
 2217:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 2218:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 2219:            &mt('show').'</label>'."\n";
 2220: }
 2221: 
 2222: sub login_header_options  {
 2223:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 2224:     my $output = '';
 2225:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 2226:         $output .= &mt('Text default(s):').'<br />';
 2227:         if (!$is_custom->{'textcol'}) {
 2228:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 2229:                        '&nbsp;&nbsp;&nbsp;';
 2230:         }
 2231:         if (!$is_custom->{'bgcol'}) {
 2232:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 2233:                        '<span id="css_'.$role.'_font" style="background-color: '.
 2234:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 2235:         }
 2236:         $output .= '<br />';
 2237:     }
 2238:     $output .='<br />';
 2239:     return $output;
 2240: }
 2241: 
 2242: sub login_text_colors {
 2243:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 2244:     my $color_menu = '<table border="0"><tr>';
 2245:     foreach my $item (@{$logintext}) {
 2246:         $color_menu .= '<td align="center">'.$choices->{$item};
 2247:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 2248:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2249:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2250:     }
 2251:     $color_menu .= '</tr></table><br />';
 2252:     return $color_menu;
 2253: }
 2254: 
 2255: sub image_changes {
 2256:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 2257:     my $output;
 2258:     if ($img eq 'login') {
 2259:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 2260:     } elsif (!$is_custom) {
 2261:         if ($img ne 'domlogo') {
 2262:             $output = &mt('Default image:').'<br />';
 2263:         } else {
 2264:             $output = &mt('Default in use:').'<br />';
 2265:         }
 2266:     }
 2267:     if ($img ne 'login') {
 2268:         if ($img_import) {
 2269:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 2270:         }
 2271:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 2272:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 2273:         if ($is_custom) {
 2274:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 2275:                        '<input type="checkbox" name="'.
 2276:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 2277:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 2278:         } else {
 2279:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 2280:         }
 2281:     }
 2282:     return $output;
 2283: }
 2284: 
 2285: sub print_quotas {
 2286:     my ($dom,$settings,$rowtotal,$action) = @_;
 2287:     my $context;
 2288:     if ($action eq 'quotas') {
 2289:         $context = 'tools';
 2290:     } else {
 2291:         $context = $action;
 2292:     }
 2293:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 2294:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2295:     my $typecount = 0;
 2296:     my ($css_class,%titles);
 2297:     if ($context eq 'requestcourses') {
 2298:         @usertools = ('official','unofficial','community','textbook','lti');
 2299:         @options =('norequest','approval','validate','autolimit');
 2300:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2301:         %titles = &courserequest_titles();
 2302:     } elsif ($context eq 'requestauthor') {
 2303:         @usertools = ('author');
 2304:         @options = ('norequest','approval','automatic');
 2305:         %titles = &authorrequest_titles();
 2306:     } else {
 2307:         @usertools = ('aboutme','blog','portfolio','timezone');
 2308:         %titles = &tool_titles();
 2309:     }
 2310:     if (ref($types) eq 'ARRAY') {
 2311:         foreach my $type (@{$types}) {
 2312:             my $currdefquota;
 2313:             unless (($context eq 'requestcourses') ||
 2314:                     ($context eq 'requestauthor')) {
 2315:                 if (ref($settings) eq 'HASH') {
 2316:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2317:                         $currdefquota = $settings->{defaultquota}->{$type};
 2318:                     } else {
 2319:                         $currdefquota = $settings->{$type};
 2320:                     }
 2321:                 }
 2322:             }
 2323:             if (defined($usertypes->{$type})) {
 2324:                 $typecount ++;
 2325:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2326:                 $datatable .= '<tr'.$css_class.'>'.
 2327:                               '<td>'.$usertypes->{$type}.'</td>'.
 2328:                               '<td class="LC_left_item">';
 2329:                 if ($context eq 'requestcourses') {
 2330:                     $datatable .= '<table><tr>';
 2331:                 }
 2332:                 my %cell;  
 2333:                 foreach my $item (@usertools) {
 2334:                     if ($context eq 'requestcourses') {
 2335:                         my ($curroption,$currlimit);
 2336:                         if (ref($settings) eq 'HASH') {
 2337:                             if (ref($settings->{$item}) eq 'HASH') {
 2338:                                 $curroption = $settings->{$item}->{$type};
 2339:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2340:                                     $currlimit = $1; 
 2341:                                 }
 2342:                             }
 2343:                         }
 2344:                         if (!$curroption) {
 2345:                             $curroption = 'norequest';
 2346:                         }
 2347:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2348:                         foreach my $option (@options) {
 2349:                             my $val = $option;
 2350:                             if ($option eq 'norequest') {
 2351:                                 $val = 0;  
 2352:                             }
 2353:                             if ($option eq 'validate') {
 2354:                                 my $canvalidate = 0;
 2355:                                 if (ref($validations{$item}) eq 'HASH') { 
 2356:                                     if ($validations{$item}{$type}) {
 2357:                                         $canvalidate = 1;
 2358:                                     }
 2359:                                 }
 2360:                                 next if (!$canvalidate);
 2361:                             }
 2362:                             my $checked = '';
 2363:                             if ($option eq $curroption) {
 2364:                                 $checked = ' checked="checked"';
 2365:                             } elsif ($option eq 'autolimit') {
 2366:                                 if ($curroption =~ /^autolimit/) {
 2367:                                     $checked = ' checked="checked"';
 2368:                                 }                       
 2369:                             } 
 2370:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2371:                                   '<input type="radio" name="crsreq_'.$item.
 2372:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2373:                                   $titles{$option}.'</label>';
 2374:                             if ($option eq 'autolimit') {
 2375:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2376:                                                 $item.'_limit_'.$type.'" size="1" '.
 2377:                                                 'value="'.$currlimit.'" />';
 2378:                             }
 2379:                             $cell{$item} .= '</span> ';
 2380:                             if ($option eq 'autolimit') {
 2381:                                 $cell{$item} .= $titles{'unlimited'};
 2382:                             }
 2383:                         }
 2384:                     } elsif ($context eq 'requestauthor') {
 2385:                         my $curroption;
 2386:                         if (ref($settings) eq 'HASH') {
 2387:                             $curroption = $settings->{$type};
 2388:                         }
 2389:                         if (!$curroption) {
 2390:                             $curroption = 'norequest';
 2391:                         }
 2392:                         foreach my $option (@options) {
 2393:                             my $val = $option;
 2394:                             if ($option eq 'norequest') {
 2395:                                 $val = 0;
 2396:                             }
 2397:                             my $checked = '';
 2398:                             if ($option eq $curroption) {
 2399:                                 $checked = ' checked="checked"';
 2400:                             }
 2401:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2402:                                   '<input type="radio" name="authorreq_'.$type.
 2403:                                   '" value="'.$val.'"'.$checked.' />'.
 2404:                                   $titles{$option}.'</label></span>&nbsp; ';
 2405:                         }
 2406:                     } else {
 2407:                         my $checked = 'checked="checked" ';
 2408:                         if ($item eq 'timezone') {
 2409:                             $checked = '';
 2410:                         }
 2411:                         if (ref($settings) eq 'HASH') {
 2412:                             if (ref($settings->{$item}) eq 'HASH') {
 2413:                                 if (!$settings->{$item}->{$type}) {
 2414:                                     $checked = '';
 2415:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2416:                                     $checked =  'checked="checked" ';
 2417:                                 }
 2418:                             }
 2419:                         }
 2420:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2421:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2422:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2423:                                       '</label></span>&nbsp; ';
 2424:                     }
 2425:                 }
 2426:                 if ($context eq 'requestcourses') {
 2427:                     $datatable .= '</tr><tr>';
 2428:                     foreach my $item (@usertools) {
 2429:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2430:                     }
 2431:                     $datatable .= '</tr></table>';
 2432:                 }
 2433:                 $datatable .= '</td>';
 2434:                 unless (($context eq 'requestcourses') ||
 2435:                         ($context eq 'requestauthor')) {
 2436:                     $datatable .= 
 2437:                               '<td class="LC_right_item">'.
 2438:                               '<span class="LC_nobreak">'.
 2439:                               '<input type="text" name="quota_'.$type.
 2440:                               '" value="'.$currdefquota.
 2441:                               '" size="5" /></span></td>';
 2442:                 }
 2443:                 $datatable .= '</tr>';
 2444:             }
 2445:         }
 2446:     }
 2447:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2448:         $defaultquota = '20';
 2449:         if (ref($settings) eq 'HASH') {
 2450:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2451:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2452:             } elsif (defined($settings->{'default'})) {
 2453:                 $defaultquota = $settings->{'default'};
 2454:             }
 2455:         }
 2456:     }
 2457:     $typecount ++;
 2458:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2459:     $datatable .= '<tr'.$css_class.'>'.
 2460:                   '<td>'.$othertitle.'</td>'.
 2461:                   '<td class="LC_left_item">';
 2462:     if ($context eq 'requestcourses') {
 2463:         $datatable .= '<table><tr>';
 2464:     }
 2465:     my %defcell;
 2466:     foreach my $item (@usertools) {
 2467:         if ($context eq 'requestcourses') {
 2468:             my ($curroption,$currlimit);
 2469:             if (ref($settings) eq 'HASH') {
 2470:                 if (ref($settings->{$item}) eq 'HASH') {
 2471:                     $curroption = $settings->{$item}->{'default'};
 2472:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2473:                         $currlimit = $1;
 2474:                     }
 2475:                 }
 2476:             }
 2477:             if (!$curroption) {
 2478:                 $curroption = 'norequest';
 2479:             }
 2480:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2481:             foreach my $option (@options) {
 2482:                 my $val = $option;
 2483:                 if ($option eq 'norequest') {
 2484:                     $val = 0;
 2485:                 }
 2486:                 if ($option eq 'validate') {
 2487:                     my $canvalidate = 0;
 2488:                     if (ref($validations{$item}) eq 'HASH') {
 2489:                         if ($validations{$item}{'default'}) {
 2490:                             $canvalidate = 1;
 2491:                         }
 2492:                     }
 2493:                     next if (!$canvalidate);
 2494:                 }
 2495:                 my $checked = '';
 2496:                 if ($option eq $curroption) {
 2497:                     $checked = ' checked="checked"';
 2498:                 } elsif ($option eq 'autolimit') {
 2499:                     if ($curroption =~ /^autolimit/) {
 2500:                         $checked = ' checked="checked"';
 2501:                     }
 2502:                 }
 2503:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2504:                                   '<input type="radio" name="crsreq_'.$item.
 2505:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2506:                                   $titles{$option}.'</label>';
 2507:                 if ($option eq 'autolimit') {
 2508:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2509:                                        $item.'_limit_default" size="1" '.
 2510:                                        'value="'.$currlimit.'" />';
 2511:                 }
 2512:                 $defcell{$item} .= '</span> ';
 2513:                 if ($option eq 'autolimit') {
 2514:                     $defcell{$item} .= $titles{'unlimited'};
 2515:                 }
 2516:             }
 2517:         } elsif ($context eq 'requestauthor') {
 2518:             my $curroption;
 2519:             if (ref($settings) eq 'HASH') {
 2520:                 $curroption = $settings->{'default'};
 2521:             }
 2522:             if (!$curroption) {
 2523:                 $curroption = 'norequest';
 2524:             }
 2525:             foreach my $option (@options) {
 2526:                 my $val = $option;
 2527:                 if ($option eq 'norequest') {
 2528:                     $val = 0;
 2529:                 }
 2530:                 my $checked = '';
 2531:                 if ($option eq $curroption) {
 2532:                     $checked = ' checked="checked"';
 2533:                 }
 2534:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2535:                               '<input type="radio" name="authorreq_default"'.
 2536:                               ' value="'.$val.'"'.$checked.' />'.
 2537:                               $titles{$option}.'</label></span>&nbsp; ';
 2538:             }
 2539:         } else {
 2540:             my $checked = 'checked="checked" ';
 2541:             if (ref($settings) eq 'HASH') {
 2542:                 if (ref($settings->{$item}) eq 'HASH') {
 2543:                     if ($settings->{$item}->{'default'} == 0) {
 2544:                         $checked = '';
 2545:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2546:                         $checked = 'checked="checked" ';
 2547:                     }
 2548:                 }
 2549:             }
 2550:             $datatable .= '<span class="LC_nobreak"><label>'.
 2551:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2552:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2553:                           '</label></span>&nbsp; ';
 2554:         }
 2555:     }
 2556:     if ($context eq 'requestcourses') {
 2557:         $datatable .= '</tr><tr>';
 2558:         foreach my $item (@usertools) {
 2559:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2560:         }
 2561:         $datatable .= '</tr></table>';
 2562:     }
 2563:     $datatable .= '</td>';
 2564:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2565:         $datatable .= '<td class="LC_right_item">'.
 2566:                       '<span class="LC_nobreak">'.
 2567:                       '<input type="text" name="defaultquota" value="'.
 2568:                       $defaultquota.'" size="5" /></span></td>';
 2569:     }
 2570:     $datatable .= '</tr>';
 2571:     $typecount ++;
 2572:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2573:     $datatable .= '<tr'.$css_class.'>'.
 2574:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2575:     if ($context eq 'requestcourses') {
 2576:         $datatable .= &mt('(overrides affiliation, if set)').
 2577:                       '</td>'.
 2578:                       '<td class="LC_left_item">'.
 2579:                       '<table><tr>';
 2580:     } else {
 2581:         $datatable .= &mt('(overrides affiliation, if checked)').
 2582:                       '</td>'.
 2583:                       '<td class="LC_left_item" colspan="2">'.
 2584:                       '<br />';
 2585:     }
 2586:     my %advcell;
 2587:     foreach my $item (@usertools) {
 2588:         if ($context eq 'requestcourses') {
 2589:             my ($curroption,$currlimit);
 2590:             if (ref($settings) eq 'HASH') {
 2591:                 if (ref($settings->{$item}) eq 'HASH') {
 2592:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2593:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2594:                         $currlimit = $1;
 2595:                     }
 2596:                 }
 2597:             }
 2598:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2599:             my $checked = '';
 2600:             if ($curroption eq '') {
 2601:                 $checked = ' checked="checked"';
 2602:             }
 2603:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2604:                                '<input type="radio" name="crsreq_'.$item.
 2605:                                '__LC_adv" value=""'.$checked.' />'.
 2606:                                &mt('No override set').'</label></span>&nbsp; ';
 2607:             foreach my $option (@options) {
 2608:                 my $val = $option;
 2609:                 if ($option eq 'norequest') {
 2610:                     $val = 0;
 2611:                 }
 2612:                 if ($option eq 'validate') {
 2613:                     my $canvalidate = 0;
 2614:                     if (ref($validations{$item}) eq 'HASH') {
 2615:                         if ($validations{$item}{'_LC_adv'}) {
 2616:                             $canvalidate = 1;
 2617:                         }
 2618:                     }
 2619:                     next if (!$canvalidate);
 2620:                 }
 2621:                 my $checked = '';
 2622:                 if ($val eq $curroption) {
 2623:                     $checked = ' checked="checked"';
 2624:                 } elsif ($option eq 'autolimit') {
 2625:                     if ($curroption =~ /^autolimit/) {
 2626:                         $checked = ' checked="checked"';
 2627:                     }
 2628:                 }
 2629:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2630:                                   '<input type="radio" name="crsreq_'.$item.
 2631:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2632:                                   $titles{$option}.'</label>';
 2633:                 if ($option eq 'autolimit') {
 2634:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2635:                                        $item.'_limit__LC_adv" size="1" '.
 2636:                                        'value="'.$currlimit.'" />';
 2637:                 }
 2638:                 $advcell{$item} .= '</span> ';
 2639:                 if ($option eq 'autolimit') {
 2640:                     $advcell{$item} .= $titles{'unlimited'};
 2641:                 }
 2642:             }
 2643:         } elsif ($context eq 'requestauthor') {
 2644:             my $curroption;
 2645:             if (ref($settings) eq 'HASH') {
 2646:                 $curroption = $settings->{'_LC_adv'};
 2647:             }
 2648:             my $checked = '';
 2649:             if ($curroption eq '') {
 2650:                 $checked = ' checked="checked"';
 2651:             }
 2652:             $datatable .= '<span class="LC_nobreak"><label>'.
 2653:                           '<input type="radio" name="authorreq__LC_adv"'.
 2654:                           ' value=""'.$checked.' />'.
 2655:                           &mt('No override set').'</label></span>&nbsp; ';
 2656:             foreach my $option (@options) {
 2657:                 my $val = $option;
 2658:                 if ($option eq 'norequest') {
 2659:                     $val = 0;
 2660:                 }
 2661:                 my $checked = '';
 2662:                 if ($val eq $curroption) {
 2663:                     $checked = ' checked="checked"';
 2664:                 }
 2665:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2666:                               '<input type="radio" name="authorreq__LC_adv"'.
 2667:                               ' value="'.$val.'"'.$checked.' />'.
 2668:                               $titles{$option}.'</label></span>&nbsp; ';
 2669:             }
 2670:         } else {
 2671:             my $checked = 'checked="checked" ';
 2672:             if (ref($settings) eq 'HASH') {
 2673:                 if (ref($settings->{$item}) eq 'HASH') {
 2674:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2675:                         $checked = '';
 2676:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2677:                         $checked = 'checked="checked" ';
 2678:                     }
 2679:                 }
 2680:             }
 2681:             $datatable .= '<span class="LC_nobreak"><label>'.
 2682:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2683:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2684:                           '</label></span>&nbsp; ';
 2685:         }
 2686:     }
 2687:     if ($context eq 'requestcourses') {
 2688:         $datatable .= '</tr><tr>';
 2689:         foreach my $item (@usertools) {
 2690:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2691:         }
 2692:         $datatable .= '</tr></table>';
 2693:     }
 2694:     $datatable .= '</td></tr>';
 2695:     $$rowtotal += $typecount;
 2696:     return $datatable;
 2697: }
 2698: 
 2699: sub print_requestmail {
 2700:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2701:     my ($now,$datatable,%currapp);
 2702:     $now = time;
 2703:     if (ref($settings) eq 'HASH') {
 2704:         if (ref($settings->{'notify'}) eq 'HASH') {
 2705:             if ($settings->{'notify'}{'approval'} ne '') {
 2706:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2707:             }
 2708:         }
 2709:     }
 2710:     my $numinrow = 2;
 2711:     my $css_class;
 2712:     if ($$rowtotal%2) {
 2713:         $css_class = 'LC_odd_row';
 2714:     }
 2715:     if ($customcss) {
 2716:         $css_class .= " $customcss";
 2717:     }
 2718:     $css_class =~ s/^\s+//;
 2719:     if ($css_class) {
 2720:         $css_class = ' class="'.$css_class.'"';
 2721:     }
 2722:     if ($rowstyle) {
 2723:         $css_class .= ' style="'.$rowstyle.'"';
 2724:     }
 2725:     my $text;
 2726:     if ($action eq 'requestcourses') {
 2727:         $text = &mt('Receive notification of course requests requiring approval');
 2728:     } elsif ($action eq 'requestauthor') {
 2729:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2730:     } else {
 2731:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2732:     }
 2733:     $datatable = '<tr'.$css_class.'>'.
 2734:                  ' <td>'.$text.'</td>'.
 2735:                  ' <td class="LC_left_item">';
 2736:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2737:                                                  $action.'notifyapproval',%currapp);
 2738:     if ($numdc > 0) {
 2739:         $datatable .= $table;
 2740:     } else {
 2741:         $datatable .= &mt('There are no active Domain Coordinators');
 2742:     }
 2743:     $datatable .='</td></tr>';
 2744:     return $datatable;
 2745: }
 2746: 
 2747: sub print_studentcode {
 2748:     my ($settings,$rowtotal) = @_;
 2749:     my $rownum = 0; 
 2750:     my ($output,%current);
 2751:     my @crstypes = ('official','unofficial','community','textbook','lti');
 2752:     if (ref($settings) eq 'HASH') {
 2753:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2754:             foreach my $type (@crstypes) {
 2755:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2756:             }
 2757:         }
 2758:     }
 2759:     $output .= '<tr>'.
 2760:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2761:                '<td class="LC_left_item">';
 2762:     foreach my $type (@crstypes) {
 2763:         my $check = ' ';
 2764:         if ($current{$type}) {
 2765:             $check = ' checked="checked" ';
 2766:         }
 2767:         $output .= '<span class="LC_nobreak"><label>'.
 2768:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2769:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2770:     }
 2771:     $output .= '</td></tr>';
 2772:     $$rowtotal ++;
 2773:     return $output;
 2774: }
 2775: 
 2776: sub print_textbookcourses {
 2777:     my ($dom,$type,$settings,$rowtotal) = @_;
 2778:     my $rownum = 0;
 2779:     my $css_class;
 2780:     my $itemcount = 1;
 2781:     my $maxnum = 0;
 2782:     my $bookshash;
 2783:     if (ref($settings) eq 'HASH') {
 2784:         $bookshash = $settings->{$type};
 2785:     }
 2786:     my %ordered;
 2787:     if (ref($bookshash) eq 'HASH') {
 2788:         foreach my $item (keys(%{$bookshash})) {
 2789:             if (ref($bookshash->{$item}) eq 'HASH') {
 2790:                 my $num = $bookshash->{$item}{'order'};
 2791:                 $ordered{$num} = $item;
 2792:             }
 2793:         }
 2794:     }
 2795:     my $confname = $dom.'-domainconfig';
 2796:     my $switchserver = &check_switchserver($dom,$confname);
 2797:     my $maxnum = scalar(keys(%ordered));
 2798:     my $datatable;
 2799:     if (keys(%ordered)) {
 2800:         my @items = sort { $a <=> $b } keys(%ordered);
 2801:         for (my $i=0; $i<@items; $i++) {
 2802:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2803:             my $key = $ordered{$items[$i]};
 2804:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2805:             my $coursetitle = $coursehash{'description'};
 2806:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2807:             if (ref($bookshash->{$key}) eq 'HASH') {
 2808:                 $subject = $bookshash->{$key}->{'subject'};
 2809:                 $title = $bookshash->{$key}->{'title'};
 2810:                 if ($type eq 'textbooks') {
 2811:                     $publisher = $bookshash->{$key}->{'publisher'};
 2812:                     $author = $bookshash->{$key}->{'author'};
 2813:                     $image = $bookshash->{$key}->{'image'};
 2814:                     if ($image ne '') {
 2815:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2816:                         my $imagethumb = "$path/tn-".$imagefile;
 2817:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2818:                     }
 2819:                 }
 2820:             }
 2821:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2822:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2823:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2824:             for (my $k=0; $k<=$maxnum; $k++) {
 2825:                 my $vpos = $k+1;
 2826:                 my $selstr;
 2827:                 if ($k == $i) {
 2828:                     $selstr = ' selected="selected" ';
 2829:                 }
 2830:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2831:             }
 2832:             $datatable .= '</select>'.('&nbsp;'x2).
 2833:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2834:                 &mt('Delete?').'</label></span></td>'.
 2835:                 '<td colspan="2">'.
 2836:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2837:                 ('&nbsp;'x2).
 2838:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2839:             if ($type eq 'textbooks') {
 2840:                 $datatable .= ('&nbsp;'x2).
 2841:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2842:                               ('&nbsp;'x2).
 2843:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2844:                               ('&nbsp;'x2).
 2845:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2846:                 if ($image) {
 2847:                     $datatable .= $imgsrc.
 2848:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2849:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2850:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2851:                 }
 2852:                 if ($switchserver) {
 2853:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2854:                 } else {
 2855:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2856:                 }
 2857:             }
 2858:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2859:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2860:                           $coursetitle.'</span></td></tr>'."\n";
 2861:             $itemcount ++;
 2862:         }
 2863:     }
 2864:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2865:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2866:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2867:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2868:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2869:     for (my $k=0; $k<$maxnum+1; $k++) {
 2870:         my $vpos = $k+1;
 2871:         my $selstr;
 2872:         if ($k == $maxnum) {
 2873:             $selstr = ' selected="selected" ';
 2874:         }
 2875:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2876:     }
 2877:     $datatable .= '</select>&nbsp;'."\n".
 2878:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2879:                   '<td colspan="2">'.
 2880:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2881:                   ('&nbsp;'x2).
 2882:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2883:                   ('&nbsp;'x2);
 2884:     if ($type eq 'textbooks') {
 2885:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2886:                       ('&nbsp;'x2).
 2887:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2888:                       ('&nbsp;'x2).
 2889:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2890:         if ($switchserver) {
 2891:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2892:         } else {
 2893:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2894:         }
 2895:         $datatable .= '</span>'."\n";
 2896:     }
 2897:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2898:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2899:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2900:                   &Apache::loncommon::selectcourse_link
 2901:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2902:                   '</span></td>'."\n".
 2903:                   '</tr>'."\n";
 2904:     $itemcount ++;
 2905:     return $datatable;
 2906: }
 2907: 
 2908: sub textbookcourses_javascript {
 2909:     my ($settings) = @_;
 2910:     return unless(ref($settings) eq 'HASH');
 2911:     my (%ordered,%total,%jstext);
 2912:     foreach my $type ('textbooks','templates') {
 2913:         $total{$type} = 0;
 2914:         if (ref($settings->{$type}) eq 'HASH') {
 2915:             foreach my $item (keys(%{$settings->{$type}})) {
 2916:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2917:                     my $num = $settings->{$type}->{$item}{'order'};
 2918:                     $ordered{$type}{$num} = $item;
 2919:                 }
 2920:             }
 2921:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2922:         }
 2923:         my @jsarray = ();
 2924:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2925:             push(@jsarray,$ordered{$type}{$item});
 2926:         }
 2927:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2928:     }
 2929:     return <<"ENDSCRIPT";
 2930: <script type="text/javascript">
 2931: // <![CDATA[
 2932: function reorderBooks(form,item,caller) {
 2933:     var changedVal;
 2934: $jstext{'textbooks'};
 2935: $jstext{'templates'};
 2936:     var newpos;
 2937:     var maxh;
 2938:     if (caller == 'textbooks') {  
 2939:         newpos = 'textbooks_addbook_pos';
 2940:         maxh = 1 + $total{'textbooks'};
 2941:     } else {
 2942:         newpos = 'templates_addbook_pos';
 2943:         maxh = 1 + $total{'templates'};
 2944:     }
 2945:     var current = new Array;
 2946:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2947:     if (item == newpos) {
 2948:         changedVal = newitemVal;
 2949:     } else {
 2950:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2951:         current[newitemVal] = newpos;
 2952:     }
 2953:     if (caller == 'textbooks') {
 2954:         for (var i=0; i<textbooks.length; i++) {
 2955:             var elementName = 'textbooks_'+textbooks[i];
 2956:             if (elementName != item) {
 2957:                 if (form.elements[elementName]) {
 2958:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2959:                     current[currVal] = elementName;
 2960:                 }
 2961:             }
 2962:         }
 2963:     }
 2964:     if (caller == 'templates') {
 2965:         for (var i=0; i<templates.length; i++) {
 2966:             var elementName = 'templates_'+templates[i];
 2967:             if (elementName != item) {
 2968:                 if (form.elements[elementName]) {
 2969:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2970:                     current[currVal] = elementName;
 2971:                 }
 2972:             }
 2973:         }
 2974:     }
 2975:     var oldVal;
 2976:     for (var j=0; j<maxh; j++) {
 2977:         if (current[j] == undefined) {
 2978:             oldVal = j;
 2979:         }
 2980:     }
 2981:     if (oldVal < changedVal) {
 2982:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2983:            var elementName = current[k];
 2984:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2985:         }
 2986:     } else {
 2987:         for (var k=changedVal; k<oldVal; k++) {
 2988:             var elementName = current[k];
 2989:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2990:         }
 2991:     }
 2992:     return;
 2993: }
 2994: 
 2995: // ]]>
 2996: </script>
 2997: 
 2998: ENDSCRIPT
 2999: }
 3000: 
 3001: sub ltitools_javascript {
 3002:     my ($settings) = @_;
 3003:     my $togglejs = &ltitools_toggle_js();
 3004:     unless (ref($settings) eq 'HASH') {
 3005:         return $togglejs;
 3006:     }
 3007:     my (%ordered,$total,%jstext);
 3008:     $total = 0;
 3009:     foreach my $item (keys(%{$settings})) {
 3010:         if (ref($settings->{$item}) eq 'HASH') {
 3011:             my $num = $settings->{$item}{'order'};
 3012:             $ordered{$num} = $item;
 3013:         }
 3014:     }
 3015:     $total = scalar(keys(%{$settings}));
 3016:     my @jsarray = ();
 3017:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3018:         push(@jsarray,$ordered{$item});
 3019:     }
 3020:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 3021:     return <<"ENDSCRIPT";
 3022: <script type="text/javascript">
 3023: // <![CDATA[
 3024: function reorderLTITools(form,item) {
 3025:     var changedVal;
 3026: $jstext
 3027:     var newpos = 'ltitools_add_pos';
 3028:     var maxh = 1 + $total;
 3029:     var current = new Array;
 3030:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3031:     if (item == newpos) {
 3032:         changedVal = newitemVal;
 3033:     } else {
 3034:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3035:         current[newitemVal] = newpos;
 3036:     }
 3037:     for (var i=0; i<ltitools.length; i++) {
 3038:         var elementName = 'ltitools_'+ltitools[i];
 3039:         if (elementName != item) {
 3040:             if (form.elements[elementName]) {
 3041:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3042:                 current[currVal] = elementName;
 3043:             }
 3044:         }
 3045:     }
 3046:     var oldVal;
 3047:     for (var j=0; j<maxh; j++) {
 3048:         if (current[j] == undefined) {
 3049:             oldVal = j;
 3050:         }
 3051:     }
 3052:     if (oldVal < changedVal) {
 3053:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3054:            var elementName = current[k];
 3055:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3056:         }
 3057:     } else {
 3058:         for (var k=changedVal; k<oldVal; k++) {
 3059:             var elementName = current[k];
 3060:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3061:         }
 3062:     }
 3063:     return;
 3064: }
 3065: 
 3066: // ]]>
 3067: </script>
 3068: 
 3069: $togglejs
 3070: 
 3071: ENDSCRIPT
 3072: }
 3073: 
 3074: sub ltitools_toggle_js {
 3075:     return <<"ENDSCRIPT";
 3076: <script type="text/javascript">
 3077: // <![CDATA[
 3078: 
 3079: function toggleLTITools(form,setting,item) {
 3080:     var radioname = '';
 3081:     var divid = '';
 3082:     if ((setting == 'passback') || (setting == 'roster')) {
 3083:         radioname = 'ltitools_'+setting+'_'+item;
 3084:         divid = 'ltitools_'+setting+'time_'+item;
 3085:         var num = form.elements[radioname].length;
 3086:         if (num) {
 3087:             var setvis = '';
 3088:             for (var i=0; i<num; i++) {
 3089:                 if (form.elements[radioname][i].checked) {
 3090:                     if (form.elements[radioname][i].value == '1') {
 3091:                         if (document.getElementById(divid)) {
 3092:                             document.getElementById(divid).style.display = 'inline-block';
 3093:                         }
 3094:                         setvis = 1;
 3095:                     }
 3096:                     break;
 3097:                 }
 3098:             }
 3099:         }
 3100:         if (!setvis) {
 3101:             if (document.getElementById(divid)) {
 3102:                 document.getElementById(divid).style.display = 'none';
 3103:             }
 3104:         }
 3105:     }
 3106:     if (setting == 'user') {
 3107:         divid = 'ltitools_'+setting+'_div_'+item;
 3108:         var checkid = 'ltitools_'+setting+'_field_'+item;
 3109:         if (document.getElementById(divid)) {
 3110:             if (document.getElementById(checkid)) {
 3111:                 if (document.getElementById(checkid).checked) {
 3112:                     document.getElementById(divid).style.display = 'inline-block';
 3113:                 } else {
 3114:                     document.getElementById(divid).style.display = 'none';
 3115:                 }
 3116:             }
 3117:         }
 3118:     }
 3119:     return;
 3120: }
 3121: // ]]>
 3122: </script>
 3123: 
 3124: ENDSCRIPT
 3125: }
 3126: 
 3127: sub wafproxy_javascript {
 3128:     my ($dom) = @_;
 3129:     return <<"ENDSCRIPT";
 3130: <script type="text/javascript">
 3131: // <![CDATA[
 3132: function updateWAF() {
 3133:     if (document.getElementById('wafproxy_remoteip')) {
 3134:         var wafremote = 0;
 3135:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 3136:             wafremote = 1;
 3137:         }
 3138:         var fields = new Array('header','trust');
 3139:         for (var i=0; i<fields.length; i++) {
 3140:             if (document.getElementById('wafproxy_'+fields[i])) {
 3141:                 if (wafremote == 1) {
 3142:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 3143:                 }
 3144:                 else {
 3145:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 3146:                 }
 3147:             }
 3148:         }
 3149:         if (document.getElementById('wafproxyranges_$dom')) {
 3150:             if (wafremote == 1) {
 3151:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3152:             } else {
 3153:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3154:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 3155:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 3156:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3157:                         }
 3158:                     }
 3159:                 }
 3160:             }
 3161:         }
 3162:     }
 3163:     return;
 3164: }
 3165: 
 3166: function checkWAF() {
 3167:     if (document.getElementById('wafproxy_remoteip')) {
 3168:         var wafvpn = 0;
 3169:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3170:             if (document.display.wafproxy_vpnaccess[i].checked) {
 3171:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 3172:                     wafvpn = 1;
 3173:                 }
 3174:                 break;
 3175:             }
 3176:         }
 3177:         var vpn = new Array('vpnint','vpnext');
 3178:         for (var i=0; i<vpn.length; i++) {
 3179:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 3180:                 if (wafvpn == 1) {
 3181:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 3182:                 }
 3183:                 else {
 3184:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 3185:                 }
 3186:             }
 3187:         }
 3188:         if (document.getElementById('wafproxyranges_$dom')) {
 3189:             if (wafvpn == 1) {
 3190:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3191:             }
 3192:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 3193:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3194:             }
 3195:         }
 3196:     }
 3197:     return;
 3198: }
 3199: 
 3200: function toggleWAF() {
 3201:     if (document.getElementById('wafproxy_table')) {
 3202:         var wafproxy = 0;
 3203:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 3204:              if (document.display.wafproxy_${dom}[i].checked) {
 3205:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 3206:                      wafproxy = 1;
 3207:                      break;
 3208:                 }
 3209:             }
 3210:         }
 3211:         if (wafproxy == 1) {
 3212:             document.getElementById('wafproxy_table').style.display='inline';
 3213:         }
 3214:         else {
 3215:            document.getElementById('wafproxy_table').style.display='none';
 3216:         }
 3217:         if (document.getElementById('wafproxyrow_${dom}')) {
 3218:             if (wafproxy == 1) {
 3219:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 3220:             }
 3221:             else {
 3222:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 3223:             }
 3224:         }
 3225:         if (document.getElementById('nowafproxyrow_$dom')) {
 3226:             if (wafproxy == 1) {
 3227:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 3228:             }
 3229:             else {
 3230:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 3231:             }
 3232:         }
 3233:     }
 3234:     return;
 3235: }
 3236: // ]]>
 3237: </script>
 3238: 
 3239: ENDSCRIPT
 3240: }
 3241: 
 3242: sub lti_javascript {
 3243:     my ($dom,$settings) = @_;
 3244:     my $togglejs = &lti_toggle_js($dom);
 3245:     my $linkprot_js = &Apache::courseprefs::linkprot_javascript();
 3246:     unless (ref($settings) eq 'HASH') {
 3247:         return $togglejs.'
 3248: <script type="text/javascript">
 3249: // <![CDATA[
 3250: 
 3251: '.$linkprot_js.'
 3252: 
 3253: // ]]>
 3254: </script>
 3255: ';
 3256:     }
 3257:     my (%ordered,$total,%jstext);
 3258:     $total = scalar(keys(%{$settings}));
 3259:     foreach my $item (keys(%{$settings})) {
 3260:         if (ref($settings->{$item}) eq 'HASH') {
 3261:             my $num = $settings->{$item}{'order'};
 3262:             if ($num eq '') {
 3263:                 $num = $total - 1;
 3264:             }
 3265:             $ordered{$num} = $item;
 3266:         }
 3267:     }
 3268:     my @jsarray = ();
 3269:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3270:         push(@jsarray,$ordered{$item});
 3271:     }
 3272:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 3273:     return <<"ENDSCRIPT";
 3274: <script type="text/javascript">
 3275: // <![CDATA[
 3276: function reorderLTI(form,item) {
 3277:     var changedVal;
 3278: $jstext
 3279:     var newpos = 'lti_pos_add';
 3280:     var maxh = 1 + $total;
 3281:     var current = new Array;
 3282:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3283:     if (item == newpos) {
 3284:         changedVal = newitemVal;
 3285:     } else {
 3286:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3287:         current[newitemVal] = newpos;
 3288:     }
 3289:     for (var i=0; i<lti.length; i++) {
 3290:         var elementName = 'lti_pos_'+lti[i];
 3291:         if (elementName != item) {
 3292:             if (form.elements[elementName]) {
 3293:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3294:                 current[currVal] = elementName;
 3295:             }
 3296:         }
 3297:     }
 3298:     var oldVal;
 3299:     for (var j=0; j<maxh; j++) {
 3300:         if (current[j] == undefined) {
 3301:             oldVal = j;
 3302:         }
 3303:     }
 3304:     if (oldVal < changedVal) {
 3305:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3306:            var elementName = current[k];
 3307:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3308:         }
 3309:     } else {
 3310:         for (var k=changedVal; k<oldVal; k++) {
 3311:             var elementName = current[k];
 3312:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3313:         }
 3314:     }
 3315:     return;
 3316: }
 3317: 
 3318: $linkprot_js
 3319: 
 3320: // ]]>
 3321: </script>
 3322: 
 3323: $togglejs
 3324: 
 3325: ENDSCRIPT
 3326: }
 3327: 
 3328: sub lti_toggle_js {
 3329:     my ($dom) = @_;
 3330:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3331:                             localauth => 'Local auth argument',
 3332:                             krb       => 'Kerberos domain',
 3333:                          );
 3334:     my $crsincalert = &mt('"User\'s identity sent" needs to be set to "Yes" first,[_1] before setting "Course\'s identity sent" to "Yes"',"\n");
 3335:     &js_escape(\$crsincalert);
 3336:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 3337:     my $primary = &Apache::lonnet::domain($dom,'primary');
 3338:     my $course_servers = "'".join("','",keys(%servers))."'";
 3339:     return <<"ENDSCRIPT";
 3340: <script type="text/javascript">
 3341: // <![CDATA[
 3342: 
 3343: function toggleLTI(form,setting,item) {
 3344:     if ((setting == 'requser') || (setting == 'crsinc')) {
 3345:         var usrfieldsets = document.getElementsByClassName('ltioption_usr_'+item);
 3346:         var setvis = '';
 3347:         var radioname = 'lti_requser_'+item;
 3348:         var num = form.elements[radioname].length;
 3349:         if (num) {
 3350:             for (var i=0; i<num; i++) {
 3351:                 if (form.elements[radioname][i].checked) {
 3352:                     if (form.elements[radioname][i].value == '1') {
 3353:                         setvis = 1;
 3354:                         break;
 3355:                     }
 3356:                 }
 3357:             }
 3358:         }
 3359:         if (usrfieldsets.length) {
 3360:             for (var j=0; j<usrfieldsets.length; j++) {
 3361:                 if (setvis) {
 3362:                     usrfieldsets[j].style.display = 'block';
 3363:                 } else {
 3364:                     usrfieldsets[j].style.display = 'none';
 3365:                 }
 3366:             }
 3367:         }
 3368:         var crsfieldsets = document.getElementsByClassName('ltioption_crs_'+item);
 3369:         if (crsfieldsets.length) {
 3370:             radioname = 'lti_crsinc_'+item;
 3371:             var num = form.elements[radioname].length;
 3372:             if (num) {
 3373:                 var crsvis = '';
 3374:                 for (var i=0; i<num; i++) {
 3375:                     if (form.elements[radioname][i].checked) {
 3376:                         if (form.elements[radioname][i].value == '1') {
 3377:                             if (setvis == '') {
 3378:                                 if (setting == 'crsinc'){
 3379:                                     alert("$crsincalert");
 3380:                                     form.elements[radioname][0].checked = true;
 3381:                                 }
 3382:                             } else {
 3383:                                 crsvis = 1;
 3384:                             }
 3385:                             break;
 3386:                         }
 3387:                     }
 3388:                 }
 3389:                 setvis = crsvis;
 3390:             }
 3391:             for (var j=0; j<crsfieldsets.length; j++) {
 3392:                 if (setvis) {
 3393:                     crsfieldsets[j].style.display = 'block';
 3394:                 } else {
 3395:                     crsfieldsets[j].style.display = 'none';
 3396:                 }
 3397:             }
 3398:         }
 3399:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3400:         var radioname = '';
 3401:         var divid = '';
 3402:         if (setting == 'user') {
 3403:             radioname = 'lti_mapuser_'+item;
 3404:             divid = 'lti_userfield_'+item;
 3405:         } else if (setting == 'crs') {
 3406:             radioname = 'lti_mapcrs_'+item;
 3407:             divid = 'lti_crsfield_'+item;
 3408:         } else if (setting == 'callback') {
 3409:             radioname = 'lti_callback_'+item;
 3410:             divid = 'lti_callbackfield_'+item;
 3411:         } else {
 3412:             radioname = 'lti_passback_'+item;
 3413:             divid =  'lti_passback_'+item;
 3414:         }
 3415:         var num = form.elements[radioname].length;
 3416:         if (num) {
 3417:             var setvis = '';
 3418:             for (var i=0; i<num; i++) {
 3419:                if (form.elements[radioname][i].checked) {
 3420:                    if ((setting == 'passback') || (setting == 'callback')) {
 3421:                        if (form.elements[radioname][i].value == '1') {
 3422:                            if (document.getElementById(divid)) {
 3423:                                document.getElementById(divid).style.display = 'inline-block';
 3424:                            }
 3425:                            setvis = 1;
 3426:                            break;
 3427:                        }
 3428:                    } else {
 3429:                        if (form.elements[radioname][i].value == 'other') {
 3430:                            if (document.getElementById(divid)) {
 3431:                                document.getElementById(divid).style.display = 'inline-block';
 3432:                            }
 3433:                            setvis = 1;
 3434:                            break;
 3435:                        }
 3436:                    }
 3437:                }
 3438:             }
 3439:             if (!setvis) {
 3440:                 if (document.getElementById(divid)) {
 3441:                     document.getElementById(divid).style.display = 'none';
 3442:                 }
 3443:             }
 3444:         }
 3445:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3446:         var numsec = form.elements['lti_crssec_'+item].length;
 3447:         if (numsec) {
 3448:             var setvis = '';
 3449:             for (var i=0; i<numsec; i++) {
 3450:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3451:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3452:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3453:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3454:                             setvis = 1;
 3455:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3456:                             if (numsrcsec) {
 3457:                                 var setsrcvis = '';
 3458:                                 for (var j=0; j<numsrcsec; j++) {
 3459:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3460:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3461:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3462:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3463:                                                 setsrcvis = 1;
 3464:                                             }
 3465:                                         }
 3466:                                     }
 3467:                                 }
 3468:                                 if (!setsrcvis) {
 3469:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3470:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3471:                                     }
 3472:                                 }
 3473:                             }
 3474:                         }
 3475:                     }
 3476:                 }
 3477:             }
 3478:             if (!setvis) {
 3479:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3480:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3481:                 }
 3482:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3483:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3484:                 }
 3485:             }
 3486:         }
 3487:     } else if (setting == 'lcauth') {
 3488:         var numauth = form.elements['lti_lcauth_'+item].length;
 3489:         if (numauth) {
 3490:             for (var i=0; i<numauth; i++) {
 3491:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3492:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3493:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3494:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3495:                         } else {
 3496:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3497:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3498:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3499:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3500:                                 } else {
 3501:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3502:                                 }
 3503:                             }
 3504:                         }
 3505:                     }
 3506:                 }
 3507:             }
 3508:         }
 3509:     } else if (setting == 'lcmenu') {
 3510:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3511:         var divid = 'lti_menufield_'+item;
 3512:         var setvis = '';
 3513:         for (var i=0; i<menus.length; i++) {
 3514:             var radioname = menus[i];
 3515:             var num = form.elements[radioname].length;
 3516:             if (num) {
 3517:                 for (var j=0; j<num; j++) {
 3518:                     if (form.elements[radioname][j].checked) {
 3519:                         if (form.elements[radioname][j].value == '1') {
 3520:                             if (document.getElementById(divid)) {
 3521:                                 document.getElementById(divid).style.display = 'inline-block';
 3522:                             }
 3523:                             setvis = 1;
 3524:                             break;
 3525:                         }
 3526:                     }
 3527:                 }
 3528:             }
 3529:             if (setvis == 1) {
 3530:                 break;
 3531:             }
 3532:         }
 3533:         if (!setvis) {
 3534:             if (document.getElementById(divid)) {
 3535:                 document.getElementById(divid).style.display = 'none';
 3536:             }
 3537:         }
 3538:     }
 3539:     return;
 3540: }
 3541: 
 3542: // ]]>
 3543: </script>
 3544: 
 3545: ENDSCRIPT
 3546: }
 3547: 
 3548: sub autoupdate_javascript {
 3549:     return <<"ENDSCRIPT";
 3550: <script type="text/javascript">
 3551: // <![CDATA[
 3552: function toggleLastActiveDays(form) {
 3553:     var radioname = 'lastactive';
 3554:     var divid = 'lastactive_div';
 3555:     var num = form.elements[radioname].length;
 3556:     if (num) {
 3557:         var setvis = '';
 3558:         for (var i=0; i<num; i++) {
 3559:             if (form.elements[radioname][i].checked) {
 3560:                 if (form.elements[radioname][i].value == '1') {
 3561:                     if (document.getElementById(divid)) {
 3562:                         document.getElementById(divid).style.display = 'inline-block';
 3563:                     }
 3564:                     setvis = 1;
 3565:                 }
 3566:                 break;
 3567:             }
 3568:         }
 3569:         if (!setvis) {
 3570:             if (document.getElementById(divid)) {
 3571:                 document.getElementById(divid).style.display = 'none';
 3572:             }
 3573:         }
 3574:     }
 3575:     return;
 3576: }
 3577: // ]]>
 3578: </script>
 3579: 
 3580: ENDSCRIPT
 3581: }
 3582: 
 3583: sub autoenroll_javascript {
 3584:     return <<"ENDSCRIPT";
 3585: <script type="text/javascript">
 3586: // <![CDATA[
 3587: function toggleFailsafe(form) {
 3588:     var radioname = 'autoenroll_failsafe';
 3589:     var divid = 'autoenroll_failsafe_div';
 3590:     var num = form.elements[radioname].length;
 3591:     if (num) {
 3592:         var setvis = '';
 3593:         for (var i=0; i<num; i++) {
 3594:             if (form.elements[radioname][i].checked) {
 3595:                 if ((form.elements[radioname][i].value == 'zero') || (form.elements[radioname][i].value == 'any')) {
 3596:                     if (document.getElementById(divid)) {
 3597:                         document.getElementById(divid).style.display = 'inline-block';
 3598:                     }
 3599:                     setvis = 1;
 3600:                 }
 3601:                 break;
 3602:             }
 3603:         }
 3604:         if (!setvis) {
 3605:             if (document.getElementById(divid)) {
 3606:                 document.getElementById(divid).style.display = 'none';
 3607:             }
 3608:         }
 3609:     }
 3610:     return;
 3611: }
 3612: // ]]>
 3613: </script>
 3614: 
 3615: ENDSCRIPT
 3616: }
 3617: 
 3618: sub saml_javascript {
 3619:     return <<"ENDSCRIPT";
 3620: <script type="text/javascript">
 3621: // <![CDATA[
 3622: function toggleSamlOptions(form,hostid) {
 3623:     var radioname = 'saml_'+hostid;
 3624:     var tablecellon = 'samloptionson_'+hostid;
 3625:     var tablecelloff = 'samloptionsoff_'+hostid;
 3626:     var num = form.elements[radioname].length;
 3627:     if (num) {
 3628:         var setvis = '';
 3629:         for (var i=0; i<num; i++) {
 3630:             if (form.elements[radioname][i].checked) {
 3631:                 if (form.elements[radioname][i].value == '1') {
 3632:                     if (document.getElementById(tablecellon)) {
 3633:                         document.getElementById(tablecellon).style.display='';
 3634:                     }
 3635:                     if (document.getElementById(tablecelloff)) {
 3636:                         document.getElementById(tablecelloff).style.display='none';
 3637:                     }
 3638:                     setvis = 1;
 3639:                 }
 3640:                 break;
 3641:             }
 3642:         }
 3643:         if (!setvis) {
 3644:             if (document.getElementById(tablecellon)) {
 3645:                 document.getElementById(tablecellon).style.display='none';
 3646:             }
 3647:             if (document.getElementById(tablecelloff)) {
 3648:                 document.getElementById(tablecelloff).style.display='';
 3649:             }
 3650:         }
 3651:     }
 3652:     return;
 3653: }
 3654: // ]]>
 3655: </script>
 3656: 
 3657: ENDSCRIPT
 3658: }
 3659: 
 3660: sub ipaccess_javascript {
 3661:     my ($settings) = @_;
 3662:     my (%ordered,$total,%jstext);
 3663:     $total = 0;
 3664:     if (ref($settings) eq 'HASH') {
 3665:         foreach my $item (keys(%{$settings})) {
 3666:             if (ref($settings->{$item}) eq 'HASH') {
 3667:                 my $num = $settings->{$item}{'order'};
 3668:                 $ordered{$num} = $item;
 3669:             }
 3670:         }
 3671:         $total = scalar(keys(%{$settings}));
 3672:     }
 3673:     my @jsarray = ();
 3674:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3675:         push(@jsarray,$ordered{$item});
 3676:     }
 3677:     my $jstext = '    var ipaccess = Array('."'".join("','",@jsarray)."'".');'."\n";
 3678:     return <<"ENDSCRIPT";
 3679: <script type="text/javascript">
 3680: // <![CDATA[
 3681: function reorderIPaccess(form,item) {
 3682:     var changedVal;
 3683: $jstext
 3684:     var newpos = 'ipaccess_pos_add';
 3685:     var maxh = 1 + $total;
 3686:     var current = new Array;
 3687:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3688:     if (item == newpos) {
 3689:         changedVal = newitemVal;
 3690:     } else {
 3691:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3692:         current[newitemVal] = newpos;
 3693:     }
 3694:     for (var i=0; i<ipaccess.length; i++) {
 3695:         var elementName = 'ipaccess_pos_'+ipaccess[i];
 3696:         if (elementName != item) {
 3697:             if (form.elements[elementName]) {
 3698:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3699:                 current[currVal] = elementName;
 3700:             }
 3701:         }
 3702:     }
 3703:     var oldVal;
 3704:     for (var j=0; j<maxh; j++) {
 3705:         if (current[j] == undefined) {
 3706:             oldVal = j;
 3707:         }
 3708:     }
 3709:     if (oldVal < changedVal) {
 3710:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3711:            var elementName = current[k];
 3712:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3713:         }
 3714:     } else {
 3715:         for (var k=changedVal; k<oldVal; k++) {
 3716:             var elementName = current[k];
 3717:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3718:         }
 3719:     }
 3720:     return;
 3721: }
 3722: // ]]>
 3723: </script>
 3724: 
 3725: ENDSCRIPT
 3726: }
 3727: 
 3728: sub authordefaults_javascript {
 3729:     my %alert = &Apache::lonlocal::texthash (
 3730:                     reqd => 'Warning: at least one editor needs to be available.',
 3731:                     rest => 'Unchecking this editor disallowed while others unchecked.',
 3732:     );
 3733:     &js_escape(\%alert);
 3734:     return <<"ENDSCRIPT";
 3735: <script type="text/javascript">
 3736: // <![CDATA[
 3737: 
 3738: function checkEditors(form,checkbox,current) {
 3739:     if (form.elements[checkbox].length != undefined) {
 3740:         var count = 0;
 3741:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3742:             if (form.elements[checkbox][i].checked) {
 3743:                 count ++;
 3744:             }
 3745:         }
 3746:         if (count == 0) {
 3747:             if (current.type =='radio') {
 3748:                 current.checked = true;
 3749:                 alert('$alert{reqd}\\n$alert{rest}');
 3750:             }
 3751:         }
 3752:     }
 3753:     return;
 3754: }
 3755: // ]]>
 3756: </script>
 3757: 
 3758: ENDSCRIPT
 3759: }
 3760: 
 3761: sub print_autoenroll {
 3762:     my ($dom,$settings,$rowtotal) = @_;
 3763:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3764:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,
 3765:         $failsafe,$autofailsafe,$failsafesty,%failsafechecked);
 3766:     $failsafesty = 'none';
 3767:     %failsafechecked = (
 3768:         off => ' checked="checked"',
 3769:     );
 3770:     if (ref($settings) eq 'HASH') {
 3771:         if (exists($settings->{'run'})) {
 3772:             if ($settings->{'run'} eq '0') {
 3773:                 $runoff = ' checked="checked" ';
 3774:                 $runon = ' ';
 3775:             } else {
 3776:                 $runon = ' checked="checked" ';
 3777:                 $runoff = ' ';
 3778:             }
 3779:         } else {
 3780:             if ($autorun) {
 3781:                 $runon = ' checked="checked" ';
 3782:                 $runoff = ' ';
 3783:             } else {
 3784:                 $runoff = ' checked="checked" ';
 3785:                 $runon = ' ';
 3786:             }
 3787:         }
 3788:         if (exists($settings->{'co-owners'})) {
 3789:             if ($settings->{'co-owners'} eq '0') {
 3790:                 $coownersoff = ' checked="checked" ';
 3791:                 $coownerson = ' ';
 3792:             } else {
 3793:                 $coownerson = ' checked="checked" ';
 3794:                 $coownersoff = ' ';
 3795:             }
 3796:         } else {
 3797:             $coownersoff = ' checked="checked" ';
 3798:             $coownerson = ' ';
 3799:         }
 3800:         if (exists($settings->{'sender_domain'})) {
 3801:             $defdom = $settings->{'sender_domain'};
 3802:         }
 3803:         if (exists($settings->{'failsafe'})) {
 3804:             $failsafe = $settings->{'failsafe'};
 3805:             if ($failsafe eq 'zero') {
 3806:                 $failsafechecked{'zero'} = ' checked="checked"';
 3807:                 $failsafechecked{'off'} = '';
 3808:                 $failsafesty = 'inline-block';
 3809:             } elsif ($failsafe eq 'any') {
 3810:                 $failsafechecked{'any'} = ' checked="checked"';
 3811:                 $failsafechecked{'off'} = '';
 3812:             }
 3813:             $autofailsafe = $settings->{'autofailsafe'};
 3814:         } elsif (exists($settings->{'autofailsafe'})) {
 3815:             $autofailsafe = $settings->{'autofailsafe'};
 3816:             if ($autofailsafe ne '') {
 3817:                 $failsafechecked{'zero'} = ' checked="checked"';
 3818:                 $failsafe = 'zero';
 3819:                 $failsafechecked{'off'} = '';
 3820:             }
 3821:         }
 3822:     } else {
 3823:         if ($autorun) {
 3824:             $runon = ' checked="checked" ';
 3825:             $runoff = ' ';
 3826:         } else {
 3827:             $runoff = ' checked="checked" ';
 3828:             $runon = ' ';
 3829:         }
 3830:     }
 3831:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3832:     my $notif_sender;
 3833:     if (ref($settings) eq 'HASH') {
 3834:         $notif_sender = $settings->{'sender_uname'};
 3835:     }
 3836:     my $datatable='<tr class="LC_odd_row">'.
 3837:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3838:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3839:                   '<input type="radio" name="autoenroll_run"'.
 3840:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3841:                   '<label><input type="radio" name="autoenroll_run"'.
 3842:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3843:                   '</tr><tr>'.
 3844:                   '<td>'.&mt('Notification messages - sender').
 3845:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 3846:                   &mt('username').':&nbsp;'.
 3847:                   '<input type="text" name="sender_uname" value="'.
 3848:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 3849:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 3850:                   '<tr class="LC_odd_row">'.
 3851:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 3852:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3853:                   '<input type="radio" name="autoassign_coowners"'.
 3854:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3855:                   '<label><input type="radio" name="autoassign_coowners"'.
 3856:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3857:                   '</tr><tr>'.
 3858:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 3859:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 3860:                   '<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; '.
 3861:                   '<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 />'.
 3862:                   '<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>'.
 3863:                   '<div class="LC_floatleft" style="display:'.$failsafesty.';" id="autoenroll_failsafe_div">'.
 3864:                   '<span class="LC_nobreak">'.
 3865:                   &mt('Threshold for number of students in section to drop: [_1]',
 3866:                       '<input type="text" name="autoenroll_autofailsafe" value="'.$autofailsafe.'" size="4" />').
 3867:                   '</span></div></td></tr>';
 3868:     $$rowtotal += 4;
 3869:     return $datatable;
 3870: }
 3871: 
 3872: sub print_autoupdate {
 3873:     my ($position,$dom,$settings,$rowtotal) = @_;
 3874:     my ($enable,$datatable);
 3875:     if ($position eq 'top') {
 3876:         my %choices = &Apache::lonlocal::texthash (
 3877:                           run        => 'Auto-update active?',
 3878:                           classlists => 'Update information in classlists?',
 3879:                           unexpired  => 'Skip updates for users without active or future roles?',
 3880:                           lastactive => 'Skip updates for inactive users?',
 3881:         );
 3882:         my $itemcount = 0;
 3883:         my $updateon = ' ';
 3884:         my $updateoff = ' checked="checked" ';
 3885:         if (ref($settings) eq 'HASH') {
 3886:             if ($settings->{'run'} eq '1') {
 3887:                 $updateon = $updateoff;
 3888:                 $updateoff = ' ';
 3889:             }
 3890:         }
 3891:         $enable = '<tr class="LC_odd_row">'.
 3892:                   '<td>'.$choices{'run'}.'</td>'.
 3893:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3894:                   '<input type="radio" name="autoupdate_run"'.
 3895:                   $updateoff.'value="0" />'.&mt('No').'</label>&nbsp;'.
 3896:                   '<label><input type="radio" name="autoupdate_run"'.
 3897:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 3898:                   '</tr>';
 3899:         my @toggles = ('classlists','unexpired');
 3900:         my %defaultchecked = ('classlists' => 'off',
 3901:                               'unexpired'  => 'off'
 3902:                               );
 3903:         $$rowtotal ++;
 3904:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3905:                                                      \%choices,$itemcount,'','','left','no');
 3906:         $datatable = $enable.$datatable;
 3907:         $$rowtotal += $itemcount;
 3908:         my $lastactiveon = ' ';
 3909:         my $lastactiveoff = ' checked="checked" ';
 3910:         my $lastactivestyle = 'none';
 3911:         my $lastactivedays;
 3912:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 3913:         if (ref($settings) eq 'HASH') {
 3914:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 3915:                 $lastactiveon = $lastactiveoff;
 3916:                 $lastactiveoff = ' ';
 3917:                 $lastactivestyle = 'inline-block';
 3918:                 $lastactivedays = $settings->{'lastactive'};
 3919:             }
 3920:         }
 3921:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3922:         $datatable .= '<tr'.$css_class.'>'.
 3923:                       '<td>'.$choices{'lastactive'}.'</td>'.
 3924:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3925:                       '<input type="radio" name="lastactive"'.
 3926:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3927:                       '&nbsp;<label>'.
 3928:                       '<input type="radio" name="lastactive"'.
 3929:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 3930:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 3931:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 3932:                           '<input type="text" size="5" name="lastactivedays" value="'.
 3933:                           $lastactivedays.'" />').
 3934:                       '</span></td>'.
 3935:                       '</tr>';
 3936:         $$rowtotal ++;
 3937:     } elsif ($position eq 'middle') {
 3938:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3939:         my $numinrow = 3;
 3940:         my $locknamesettings;
 3941:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3942:                                      $dom,$numinrow,$othertitle,
 3943:                                     'lockablenames',$rowtotal);
 3944:         $$rowtotal ++;
 3945:     } else {
 3946:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3947:         my @fields = ('lastname','firstname','middlename','generation',
 3948:                       'permanentemail','id');
 3949:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3950:         my $numrows = 0;
 3951:         if (ref($types) eq 'ARRAY') {
 3952:             if (@{$types} > 0) {
 3953:                 $datatable = 
 3954:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3955:                                          \@fields,$types,\$numrows);
 3956:                     $$rowtotal += @{$types}; 
 3957:             }
 3958:         }
 3959:         $datatable .= 
 3960:             &usertype_update_row($settings,{'default' => $othertitle},
 3961:                                  \%fieldtitles,\@fields,['default'],
 3962:                                  \$numrows);
 3963:         $$rowtotal ++;     
 3964:     }
 3965:     return $datatable;
 3966: }
 3967: 
 3968: sub print_autocreate {
 3969:     my ($dom,$settings,$rowtotal) = @_;
 3970:     my (%createon,%createoff,%currhash);
 3971:     my @types = ('xml','req');
 3972:     if (ref($settings) eq 'HASH') {
 3973:         foreach my $item (@types) {
 3974:             $createoff{$item} = ' checked="checked" ';
 3975:             $createon{$item} = ' ';
 3976:             if (exists($settings->{$item})) {
 3977:                 if ($settings->{$item}) {
 3978:                     $createon{$item} = ' checked="checked" ';
 3979:                     $createoff{$item} = ' ';
 3980:                 }
 3981:             }
 3982:         }
 3983:         if ($settings->{'xmldc'} ne '') {
 3984:             $currhash{$settings->{'xmldc'}} = 1;
 3985:         }
 3986:     } else {
 3987:         foreach my $item (@types) {
 3988:             $createoff{$item} = ' checked="checked" ';
 3989:             $createon{$item} = ' ';
 3990:         }
 3991:     }
 3992:     $$rowtotal += 2;
 3993:     my $numinrow = 2;
 3994:     my $datatable='<tr class="LC_odd_row">'.
 3995:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 3996:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3997:                   '<input type="radio" name="autocreate_xml"'.
 3998:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3999:                   '<label><input type="radio" name="autocreate_xml"'.
 4000:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 4001:                   '</td></tr><tr>'.
 4002:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 4003:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4004:                   '<input type="radio" name="autocreate_req"'.
 4005:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4006:                   '<label><input type="radio" name="autocreate_req"'.
 4007:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 4008:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4009:                                                    'autocreate_xmldc',%currhash);
 4010:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 4011:     if ($numdc > 1) {
 4012:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 4013:                       '</td><td class="LC_left_item">';
 4014:     } else {
 4015:         $datatable .= &mt('Course creation processed as:').
 4016:                       '</td><td class="LC_right_item">';
 4017:     }
 4018:     $datatable .= $dctable.'</td></tr>';
 4019:     $$rowtotal += $rows;
 4020:     return $datatable;
 4021: }
 4022: 
 4023: sub print_directorysrch {
 4024:     my ($position,$dom,$settings,$rowtotal) = @_;
 4025:     my $datatable;
 4026:     if ($position eq 'top') {
 4027:         my $instsrchon = ' ';
 4028:         my $instsrchoff = ' checked="checked" ';
 4029:         my ($exacton,$containson,$beginson);
 4030:         my $instlocalon = ' ';
 4031:         my $instlocaloff = ' checked="checked" ';
 4032:         if (ref($settings) eq 'HASH') {
 4033:             if ($settings->{'available'} eq '1') {
 4034:                 $instsrchon = $instsrchoff;
 4035:                 $instsrchoff = ' ';
 4036:             }
 4037:             if ($settings->{'localonly'} eq '1') {
 4038:                 $instlocalon = $instlocaloff;
 4039:                 $instlocaloff = ' ';
 4040:             }
 4041:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 4042:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 4043:                     if ($type eq 'exact') {
 4044:                         $exacton = ' checked="checked" ';
 4045:                     } elsif ($type eq 'contains') {
 4046:                         $containson = ' checked="checked" ';
 4047:                     } elsif ($type eq 'begins') {
 4048:                         $beginson = ' checked="checked" ';
 4049:                     }
 4050:                 }
 4051:             } else {
 4052:                 if ($settings->{'searchtypes'} eq 'exact') {
 4053:                     $exacton = ' checked="checked" ';
 4054:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 4055:                     $containson = ' checked="checked" ';
 4056:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 4057:                     $exacton = ' checked="checked" ';
 4058:                     $containson = ' checked="checked" ';
 4059:                 }
 4060:             }
 4061:         }
 4062:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 4063:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4064: 
 4065:         my $numinrow = 4;
 4066:         my $cansrchrow = 0;
 4067:         $datatable='<tr class="LC_odd_row">'.
 4068:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 4069:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4070:                    '<input type="radio" name="dirsrch_available"'.
 4071:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4072:                    '<label><input type="radio" name="dirsrch_available"'.
 4073:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4074:                    '</tr><tr>'.
 4075:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 4076:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4077:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 4078:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4079:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 4080:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4081:                    '</tr>';
 4082:         $$rowtotal += 2;
 4083:         if (ref($usertypes) eq 'HASH') {
 4084:             if (keys(%{$usertypes}) > 0) {
 4085:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4086:                                              $numinrow,$othertitle,'cansearch',
 4087:                                              $rowtotal);
 4088:                 $cansrchrow = 1;
 4089:             }
 4090:         }
 4091:         if ($cansrchrow) {
 4092:             $$rowtotal ++;
 4093:             $datatable .= '<tr>';
 4094:         } else {
 4095:             $datatable .= '<tr class="LC_odd_row">';
 4096:         }
 4097:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 4098:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 4099:         foreach my $title (@{$titleorder}) {
 4100:             if (defined($searchtitles->{$title})) {
 4101:                 my $check = ' ';
 4102:                 if (ref($settings) eq 'HASH') {
 4103:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 4104:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 4105:                             $check = ' checked="checked" ';
 4106:                         }
 4107:                     }
 4108:                 }
 4109:                 $datatable .= '<td class="LC_left_item">'.
 4110:                               '<span class="LC_nobreak"><label>'.
 4111:                               '<input type="checkbox" name="searchby" '.
 4112:                               'value="'.$title.'"'.$check.'/>'.
 4113:                               $searchtitles->{$title}.'</label></span></td>';
 4114:             }
 4115:         }
 4116:         $datatable .= '</tr></table></td></tr>';
 4117:         $$rowtotal ++;
 4118:         if ($cansrchrow) {
 4119:             $datatable .= '<tr class="LC_odd_row">';
 4120:         } else {
 4121:             $datatable .= '<tr>';
 4122:         }
 4123:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 4124:                       '<td class="LC_left_item" colspan="2">'.
 4125:                       '<span class="LC_nobreak"><label>'.
 4126:                       '<input type="checkbox" name="searchtypes" '.
 4127:                       $exacton.' value="exact" />'.&mt('Exact match').
 4128:                       '</label>&nbsp;'.
 4129:                       '<label><input type="checkbox" name="searchtypes" '.
 4130:                       $beginson.' value="begins" />'.&mt('Begins with').
 4131:                       '</label>&nbsp;'.
 4132:                       '<label><input type="checkbox" name="searchtypes" '.
 4133:                       $containson.' value="contains" />'.&mt('Contains').
 4134:                       '</label></span></td></tr>';
 4135:         $$rowtotal ++;
 4136:     } else {
 4137:         my $domsrchon = ' checked="checked" ';
 4138:         my $domsrchoff = ' ';
 4139:         my $domlocalon = ' ';
 4140:         my $domlocaloff = ' checked="checked" ';
 4141:         if (ref($settings) eq 'HASH') {
 4142:             if ($settings->{'lclocalonly'} eq '1') {
 4143:                 $domlocalon = $domlocaloff;
 4144:                 $domlocaloff = ' ';
 4145:             }
 4146:             if ($settings->{'lcavailable'} eq '0') {
 4147:                 $domsrchoff = $domsrchon;
 4148:                 $domsrchon = ' ';
 4149:             }
 4150:         }
 4151:         $datatable='<tr class="LC_odd_row">'.
 4152:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 4153:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4154:                       '<input type="radio" name="dirsrch_domavailable"'.
 4155:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4156:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 4157:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4158:                       '</tr><tr>'.
 4159:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 4160:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4161:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 4162:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4163:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 4164:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4165:                       '</tr>';
 4166:         $$rowtotal += 2;
 4167:     }
 4168:     return $datatable;
 4169: }
 4170: 
 4171: sub print_contacts {
 4172:     my ($position,$dom,$settings,$rowtotal) = @_;
 4173:     my $datatable;
 4174:     my @contacts = ('adminemail','supportemail');
 4175:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 4176:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 4177:     if ($position eq 'top') {
 4178:         if (ref($settings) eq 'HASH') {
 4179:             foreach my $item (@contacts) {
 4180:                 if (exists($settings->{$item})) {
 4181:                     $to{$item} = $settings->{$item};
 4182:                 }
 4183:             }
 4184:         }
 4185:     } elsif ($position eq 'middle') {
 4186:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 4187:                      'updatesmail','idconflictsmail','hostipmail');
 4188:         foreach my $type (@mailings) {
 4189:             $otheremails{$type} = '';
 4190:         }
 4191:     } elsif ($position eq 'lower') {
 4192:         if (ref($settings) eq 'HASH') {
 4193:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 4194:                 %lonstatus = %{$settings->{'lonstatus'}};
 4195:             }
 4196:         }
 4197:     } else {
 4198:         @mailings = ('helpdeskmail','otherdomsmail');
 4199:         foreach my $type (@mailings) {
 4200:             $otheremails{$type} = '';
 4201:         }
 4202:         $bccemails{'helpdeskmail'} = '';
 4203:         $bccemails{'otherdomsmail'} = '';
 4204:         $includestr{'helpdeskmail'} = '';
 4205:         $includestr{'otherdomsmail'} = '';
 4206:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 4207:     }
 4208:     if (ref($settings) eq 'HASH') {
 4209:         unless (($position eq 'top') || ($position eq 'lower')) {
 4210:             foreach my $type (@mailings) {
 4211:                 if (exists($settings->{$type})) {
 4212:                     if (ref($settings->{$type}) eq 'HASH') {
 4213:                         foreach my $item (@contacts) {
 4214:                             if ($settings->{$type}{$item}) {
 4215:                                 $checked{$type}{$item} = ' checked="checked" ';
 4216:                             }
 4217:                         }
 4218:                         $otheremails{$type} = $settings->{$type}{'others'};
 4219:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4220:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 4221:                             if ($settings->{$type}{'include'} ne '') {
 4222:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4223:                                 $includestr{$type} = &unescape($includestr{$type});
 4224:                             }
 4225:                         }
 4226:                     }
 4227:                 } elsif ($type eq 'lonstatusmail') {
 4228:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4229:                 }
 4230:             }
 4231:         }
 4232:         if ($position eq 'bottom') {
 4233:             foreach my $type (@mailings) {
 4234:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 4235:                 if ($settings->{$type}{'include'} ne '') {
 4236:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4237:                     $includestr{$type} = &unescape($includestr{$type});
 4238:                 }
 4239:             }
 4240:             if (ref($settings->{'helpform'}) eq 'HASH') {
 4241:                 if (ref($fields) eq 'ARRAY') {
 4242:                     foreach my $field (@{$fields}) {
 4243:                         $currfield{$field} = $settings->{'helpform'}{$field};
 4244:                     }
 4245:                 }
 4246:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 4247:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 4248:                 } else {
 4249:                     $maxsize = '1.0';
 4250:                 }
 4251:             } else {
 4252:                 if (ref($fields) eq 'ARRAY') {
 4253:                     foreach my $field (@{$fields}) {
 4254:                         $currfield{$field} = 'yes';
 4255:                     }
 4256:                 }
 4257:                 $maxsize = '1.0';
 4258:             }
 4259:         }
 4260:     } else {
 4261:         if ($position eq 'top') {
 4262:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 4263:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 4264:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 4265:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 4266:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4267:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 4268:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 4269:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 4270:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 4271:         } elsif ($position eq 'bottom') {
 4272:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 4273:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 4274:             if (ref($fields) eq 'ARRAY') {
 4275:                 foreach my $field (@{$fields}) {
 4276:                     $currfield{$field} = 'yes';
 4277:                 }
 4278:             }
 4279:             $maxsize = '1.0';
 4280:         }
 4281:     }
 4282:     my ($titles,$short_titles) = &contact_titles();
 4283:     my $rownum = 0;
 4284:     my $css_class;
 4285:     if ($position eq 'top') {
 4286:         foreach my $item (@contacts) {
 4287:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4288:             $datatable .= '<tr'.$css_class.'>'. 
 4289:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4290:                           '</span></td><td class="LC_right_item">'.
 4291:                           '<input type="text" name="'.$item.'" value="'.
 4292:                           $to{$item}.'" /></td></tr>';
 4293:             $rownum ++;
 4294:         }
 4295:     } elsif ($position eq 'bottom') {
 4296:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4297:         $datatable .= '<tr'.$css_class.'>'.
 4298:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 4299:                       &mt('(e-mail, subject, and description always shown)').
 4300:                       '</td><td class="LC_left_item">';
 4301:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 4302:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 4303:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 4304:             foreach my $field (@{$fields}) {
 4305:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 4306:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 4307:                     $datatable .= ' '.&mt('(logged-in users)');
 4308:                 }
 4309:                 $datatable .='</td><td>';
 4310:                 my $clickaction;
 4311:                 if ($field eq 'screenshot') {
 4312:                     $clickaction = ' onclick="screenshotSize(this);"';
 4313:                 }
 4314:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 4315:                     foreach my $option (@{$possoptions->{$field}}) {
 4316:                         my $checked;
 4317:                         if ($currfield{$field} eq $option) {
 4318:                             $checked = ' checked="checked"';
 4319:                         }
 4320:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4321:                                       '<input type="radio" name="helpform_'.$field.'" '.
 4322:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 4323:                                       '</label></span>'.('&nbsp;'x2);
 4324:                     }
 4325:                 }
 4326:                 if ($field eq 'screenshot') {
 4327:                     my $display;
 4328:                     if ($currfield{$field} eq 'no') {
 4329:                         $display = ' style="display:none"';
 4330:                     }
 4331:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 4332:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 4333:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 4334:                 }
 4335:                 $datatable .= '</td></tr>';
 4336:             }
 4337:             $datatable .= '</table>';
 4338:         }
 4339:         $datatable .= '</td></tr>'."\n";
 4340:         $rownum ++;
 4341:     }
 4342:     unless (($position eq 'top') || ($position eq 'lower')) {
 4343:         foreach my $type (@mailings) {
 4344:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4345:             $datatable .= '<tr'.$css_class.'>'.
 4346:                           '<td><span class="LC_nobreak">'.
 4347:                           $titles->{$type}.': </span></td>'.
 4348:                           '<td class="LC_left_item">';
 4349:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4350:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 4351:             }
 4352:             $datatable .= '<span class="LC_nobreak">';
 4353:             foreach my $item (@contacts) {
 4354:                 $datatable .= '<label>'.
 4355:                               '<input type="checkbox" name="'.$type.'"'.
 4356:                               $checked{$type}{$item}.
 4357:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 4358:                               '</label>&nbsp;';
 4359:             }
 4360:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4361:                           '<input type="text" name="'.$type.'_others" '.
 4362:                           'value="'.$otheremails{$type}.'"  />';
 4363:             my %locchecked;
 4364:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4365:                 foreach my $loc ('s','b') {
 4366:                     if ($includeloc{$type} eq $loc) {
 4367:                         $locchecked{$loc} = ' checked="checked"';
 4368:                         last;
 4369:                     }
 4370:                 }
 4371:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4372:                               '<input type="text" name="'.$type.'_bcc" '.
 4373:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 4374:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4375:                               &mt('Text automatically added to e-mail:').' '.
 4376:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 4377:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4378:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4379:                               ('&nbsp;'x2).
 4380:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4381:                               '</span></fieldset>';
 4382:             }
 4383:             $datatable .= '</td></tr>'."\n";
 4384:             $rownum ++;
 4385:         }
 4386:     }
 4387:     if ($position eq 'middle') {
 4388:         my %choices;
 4389:         my $corelink = &core_link_msu();
 4390:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 4391:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 4392:                                         $corelink);
 4393:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 4394:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 4395:         my %defaultchecked = ('reporterrors'  => 'on',
 4396:                               'reportupdates' => 'on',
 4397:                               'reportstatus'  => 'on');
 4398:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4399:                                                    \%choices,$rownum);
 4400:         $datatable .= $reports;
 4401:     } elsif ($position eq 'lower') {
 4402:         my (%current,%excluded,%weights);
 4403:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 4404:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 4405:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 4406:         } else {
 4407:             $current{'errorthreshold'} = $defaults->{'threshold'};
 4408:         }
 4409:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 4410:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 4411:         } else {
 4412:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 4413:         }
 4414:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 4415:             foreach my $type ('E','W','N','U') {
 4416:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 4417:                     $weights{$type} = $lonstatus{'weights'}{$type};
 4418:                 } else {
 4419:                     $weights{$type} = $defaults->{$type};
 4420:                 }
 4421:             }
 4422:         } else {
 4423:             foreach my $type ('E','W','N','U') {
 4424:                 $weights{$type} = $defaults->{$type};
 4425:             }
 4426:         }
 4427:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 4428:             if (@{$lonstatus{'excluded'}} > 0) {
 4429:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 4430:             }
 4431:         }
 4432:         foreach my $item ('errorthreshold','errorsysmail') {
 4433:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4434:             $datatable .= '<tr'.$css_class.'>'.
 4435:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 4436:                           $titles->{$item}.
 4437:                           '</span></td><td class="LC_left_item">'.
 4438:                           '<input type="text" name="'.$item.'" value="'.
 4439:                           $current{$item}.'" size="5" /></td></tr>';
 4440:             $rownum ++;
 4441:         }
 4442:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4443:         $datatable .= '<tr'.$css_class.'>'.
 4444:                       '<td class="LC_left_item">'.
 4445:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 4446:                       '</span></td><td class="LC_left_item"><table><tr>';
 4447:         foreach my $type ('E','W','N','U') {
 4448:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 4449:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 4450:                           $weights{$type}.'" size="5" /></td>';
 4451:         }
 4452:         $datatable .= '</tr></table></tr>';
 4453:         $rownum ++;
 4454:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4455:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 4456:                       $titles->{'errorexcluded'}.'</td>'.
 4457:                       '<td class="LC_left_item"><table>';
 4458:         my $numinrow = 4;
 4459:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 4460:         for (my $i=0; $i<@ids; $i++) {
 4461:             my $rem = $i%($numinrow);
 4462:             if ($rem == 0) {
 4463:                 if ($i > 0) {
 4464:                     $datatable .= '</tr>';
 4465:                 }
 4466:                 $datatable .= '<tr>';
 4467:             }
 4468:             my $check;
 4469:             if ($excluded{$ids[$i]}) {
 4470:                 $check = ' checked="checked" ';
 4471:             }
 4472:             $datatable .= '<td class="LC_left_item">'.
 4473:                           '<span class="LC_nobreak"><label>'.
 4474:                           '<input type="checkbox" name="errorexcluded" '.
 4475:                           'value="'.$ids[$i].'"'.$check.' />'.
 4476:                           $ids[$i].'</label></span></td>';
 4477:         }
 4478:         my $colsleft = $numinrow - @ids%($numinrow);
 4479:         if ($colsleft > 1 ) {
 4480:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4481:                           '&nbsp;</td>';
 4482:         } elsif ($colsleft == 1) {
 4483:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4484:         }
 4485:         $datatable .= '</tr></table></td></tr>';
 4486:         $rownum ++;
 4487:     } elsif ($position eq 'bottom') {
 4488:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4489:         my (@posstypes,%usertypeshash);
 4490:         if (ref($types) eq 'ARRAY') {
 4491:             @posstypes = @{$types};
 4492:         }
 4493:         if (@posstypes) {
 4494:             if (ref($usertypes) eq 'HASH') {
 4495:                 %usertypeshash = %{$usertypes};
 4496:             }
 4497:             my @overridden;
 4498:             my $numinrow = 4;
 4499:             if (ref($settings) eq 'HASH') {
 4500:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 4501:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 4502:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 4503:                             push(@overridden,$key);
 4504:                             foreach my $item (@contacts) {
 4505:                                 if ($settings->{'overrides'}{$key}{$item}) {
 4506:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 4507:                                 }
 4508:                             }
 4509:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 4510:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 4511:                             $includeloc{'override_'.$key} = '';
 4512:                             $includestr{'override_'.$key} = '';
 4513:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 4514:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 4515:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 4516:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 4517:                             }
 4518:                         }
 4519:                     }
 4520:                 }
 4521:             }
 4522:             my $customclass = 'LC_helpdesk_override';
 4523:             my $optionsprefix = 'LC_options_helpdesk_';
 4524: 
 4525:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 4526:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4527:                                          $numinrow,$othertitle,'overrides',
 4528:                                          \$rownum,$onclicktypes,$customclass);
 4529:             $rownum ++;
 4530:             $usertypeshash{'default'} = $othertitle;
 4531:             foreach my $status (@posstypes) {
 4532:                 my $css_class;
 4533:                 if ($rownum%2) {
 4534:                     $css_class = 'LC_odd_row ';
 4535:                 }
 4536:                 $css_class .= $customclass;
 4537:                 my $rowid = $optionsprefix.$status;
 4538:                 my $hidden = 1;
 4539:                 my $currstyle = 'display:none';
 4540:                 if (grep(/^\Q$status\E$/,@overridden)) {
 4541:                     $currstyle = 'display:table-row';
 4542:                     $hidden = 0;
 4543:                 }
 4544:                 my $key = 'override_'.$status;
 4545:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 4546:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 4547:                                                   $usertypeshash{$status},$css_class,$currstyle,
 4548:                                                   \@contacts,$short_titles);
 4549:                 unless ($hidden) {
 4550:                     $rownum ++;
 4551:                 }
 4552:             }
 4553:         }
 4554:     }
 4555:     $$rowtotal += $rownum;
 4556:     return $datatable;
 4557: }
 4558: 
 4559: sub core_link_msu {
 4560:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 4561:                                           &mt('LON-CAPA core group - MSU'),600,500);
 4562: }
 4563: 
 4564: sub overridden_helpdesk {
 4565:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 4566:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 4567:     my $class = 'LC_left_item';
 4568:     if ($css_class) {
 4569:         $css_class = ' class="'.$css_class.'"';
 4570:     }
 4571:     if ($rowid) {
 4572:         $rowid = ' id="'.$rowid.'"';
 4573:     }
 4574:     if ($rowstyle) {
 4575:         $rowstyle = ' style="'.$rowstyle.'"';
 4576:     }
 4577:     my ($output,$description);
 4578:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 4579:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 4580:               "<td>$description</td>\n".
 4581:               '<td class="'.$class.'" colspan="2">'.
 4582:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 4583:               '<span class="LC_nobreak">';
 4584:     if (ref($contacts) eq 'ARRAY') {
 4585:         foreach my $item (@{$contacts}) {
 4586:             my $check;
 4587:             if (ref($checked) eq 'HASH') {
 4588:                $check = $checked->{$item};
 4589:             }
 4590:             my $title;
 4591:             if (ref($short_titles) eq 'HASH') {
 4592:                 $title = $short_titles->{$item};
 4593:             }
 4594:             $output .= '<label>'.
 4595:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 4596:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 4597:         }
 4598:     }
 4599:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4600:                '<input type="text" name="override_'.$type.'_others" '.
 4601:                'value="'.$otheremails.'"  />';
 4602:     my %locchecked;
 4603:     foreach my $loc ('s','b') {
 4604:         if ($includeloc eq $loc) {
 4605:             $locchecked{$loc} = ' checked="checked"';
 4606:             last;
 4607:         }
 4608:     }
 4609:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4610:                '<input type="text" name="override_'.$type.'_bcc" '.
 4611:                'value="'.$bccemails.'"  /></fieldset>'.
 4612:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4613:                &mt('Text automatically added to e-mail:').' '.
 4614:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4615:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4616:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4617:                ('&nbsp;'x2).
 4618:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4619:                '</span></fieldset>'.
 4620:                '</td></tr>'."\n";
 4621:     return $output;
 4622: }
 4623: 
 4624: sub contacts_javascript {
 4625:     return <<"ENDSCRIPT";
 4626: 
 4627: <script type="text/javascript">
 4628: // <![CDATA[
 4629: 
 4630: function screenshotSize(field) {
 4631:     if (document.getElementById('help_screenshotsize')) {
 4632:         if (field.value == 'no') {
 4633:             document.getElementById('help_screenshotsize').style.display="none";
 4634:         } else {
 4635:             document.getElementById('help_screenshotsize').style.display="";
 4636:         }
 4637:     }
 4638:     return;
 4639: }
 4640: 
 4641: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4642:     if (form.elements[checkbox].length != undefined) {
 4643:         var count = 0;
 4644:         if (docount) {
 4645:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4646:                 if (form.elements[checkbox][i].checked) {
 4647:                     count ++;
 4648:                 }
 4649:             }
 4650:         }
 4651:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4652:             var type = form.elements[checkbox][i].value;
 4653:             if (document.getElementById(prefix+type)) {
 4654:                 if (form.elements[checkbox][i].checked) {
 4655:                     document.getElementById(prefix+type).style.display = 'table-row';
 4656:                     if (count % 2 == 1) {
 4657:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4658:                     } else {
 4659:                         document.getElementById(prefix+type).className = target;
 4660:                     }
 4661:                     count ++;
 4662:                 } else {
 4663:                     document.getElementById(prefix+type).style.display = 'none';
 4664:                 }
 4665:             }
 4666:         }
 4667:     }
 4668:     return;
 4669: }
 4670: 
 4671: // ]]>
 4672: </script>
 4673: 
 4674: ENDSCRIPT
 4675: }
 4676: 
 4677: sub print_helpsettings {
 4678:     my ($position,$dom,$settings,$rowtotal) = @_;
 4679:     my $confname = $dom.'-domainconfig';
 4680:     my $formname = 'display';
 4681:     my ($datatable,$itemcount);
 4682:     if ($position eq 'top') {
 4683:         $itemcount = 1;
 4684:         my (%choices,%defaultchecked,@toggles);
 4685:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4686:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4687:                                      &mt('LON-CAPA bug tracker'),600,500));
 4688:         %defaultchecked = ('submitbugs' => 'on');
 4689:         @toggles = ('submitbugs');
 4690:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4691:                                                      \%choices,$itemcount);
 4692:         $$rowtotal ++;
 4693:     } else {
 4694:         my $css_class;
 4695:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4696:         my (%customroles,%ordered,%current);
 4697:         if (ref($settings) eq 'HASH') {
 4698:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4699:                 %current = %{$settings->{'adhoc'}};
 4700:             }
 4701:         }
 4702:         my $count = 0;
 4703:         foreach my $key (sort(keys(%existing))) {
 4704:             if ($key=~/^rolesdef\_(\w+)$/) {
 4705:                 my $rolename = $1;
 4706:                 my (%privs,$order);
 4707:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4708:                 $customroles{$rolename} = \%privs;
 4709:                 if (ref($current{$rolename}) eq 'HASH') {
 4710:                     $order = $current{$rolename}{'order'};
 4711:                 }
 4712:                 if ($order eq '') {
 4713:                     $order = $count;
 4714:                 }
 4715:                 $ordered{$order} = $rolename;
 4716:                 $count++;
 4717:             }
 4718:         }
 4719:         my $maxnum = scalar(keys(%ordered));
 4720:         my @roles_by_num = ();
 4721:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4722:             push(@roles_by_num,$item);
 4723:         }
 4724:         my $context = 'domprefs';
 4725:         my $crstype = 'Course';
 4726:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4727:         my @accesstypes = ('all','dh','da','none');
 4728:         my ($numstatustypes,@jsarray);
 4729:         if (ref($types) eq 'ARRAY') {
 4730:             if (@{$types} > 0) {
 4731:                 $numstatustypes = scalar(@{$types});
 4732:                 push(@accesstypes,'status');
 4733:                 @jsarray = ('bystatus');
 4734:             }
 4735:         }
 4736:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4737:         if (keys(%domhelpdesk)) {
 4738:             push(@accesstypes,('inc','exc'));
 4739:             push(@jsarray,('notinc','notexc'));
 4740:         }
 4741:         my $hiddenstr = join("','",@jsarray);
 4742:         my $context = 'domprefs';
 4743:         my $crstype = 'Course';
 4744:         my $prefix = 'helproles_';
 4745:         my $add_class = 'LC_hidden';
 4746:         foreach my $num (@roles_by_num) {
 4747:             my $role = $ordered{$num};
 4748:             my ($desc,$access,@statuses);
 4749:             if (ref($current{$role}) eq 'HASH') {
 4750:                 $desc = $current{$role}{'desc'};
 4751:                 $access = $current{$role}{'access'};
 4752:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4753:                     @statuses = @{$current{$role}{'insttypes'}};
 4754:                 }
 4755:             }
 4756:             if ($desc eq '') {
 4757:                 $desc = $role;
 4758:             }
 4759:             my $identifier = 'custhelp'.$num;
 4760:             my %full=();
 4761:             my %levels= (
 4762:                          course => {},
 4763:                          domain => {},
 4764:                          system => {},
 4765:                         );
 4766:             my %levelscurrent=(
 4767:                                course => {},
 4768:                                domain => {},
 4769:                                system => {},
 4770:                               );
 4771:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4772:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4773:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4774:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4775:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 4776:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4777:             for (my $k=0; $k<=$maxnum; $k++) {
 4778:                 my $vpos = $k+1;
 4779:                 my $selstr;
 4780:                 if ($k == $num) {
 4781:                     $selstr = ' selected="selected" ';
 4782:                 }
 4783:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4784:             }
 4785:             $datatable .= '</select>'.('&nbsp;'x2).
 4786:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 4787:                           '</td>'.
 4788:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4789:                           &mt('Name shown to users:').
 4790:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 4791:                           '</fieldset>'.
 4792:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4793:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4794:                           '<fieldset>'.
 4795:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4796:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4797:                                                                    \%levelscurrent,$identifier,
 4798:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4799:                           '</fieldset></td>';
 4800:             $itemcount ++;
 4801:         }
 4802:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4803:         my $newcust = 'custhelp'.$count;
 4804:         my (%privs,%levelscurrent);
 4805:         my %full=();
 4806:         my %levels= (
 4807:                      course => {},
 4808:                      domain => {},
 4809:                      system => {},
 4810:                     );
 4811:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4812:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4813:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4814:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 4815:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4816:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4817:         for (my $k=0; $k<$maxnum+1; $k++) {
 4818:             my $vpos = $k+1;
 4819:             my $selstr;
 4820:             if ($k == $maxnum) {
 4821:                 $selstr = ' selected="selected" ';
 4822:             }
 4823:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4824:         }
 4825:         $datatable .= '</select>&nbsp;'."\n".
 4826:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4827:                       '</label></span></td>'.
 4828:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4829:                       '<span class="LC_nobreak">'.
 4830:                       &mt('Internal name:').
 4831:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4832:                       '</span>'.('&nbsp;'x4).
 4833:                       '<span class="LC_nobreak">'.
 4834:                       &mt('Name shown to users:').
 4835:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4836:                       '</span></fieldset>'.
 4837:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4838:                                              $usertypes,$types,\%domhelpdesk).
 4839:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4840:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4841:                                                                 \@templateroles,$newcust).
 4842:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4843:                                                                \%levelscurrent,$newcust).
 4844:                       '</fieldset>'.
 4845:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4846:                       '</td></tr>';
 4847:         $count ++;
 4848:         $$rowtotal += $count;
 4849:     }
 4850:     return $datatable;
 4851: }
 4852: 
 4853: sub adhocbutton {
 4854:     my ($prefix,$num,$field,$visibility) = @_;
 4855:     my %lt = &Apache::lonlocal::texthash(
 4856:                                           show => 'Show details',
 4857:                                           hide => 'Hide details',
 4858:                                         );
 4859:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4860:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4861:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4862:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4863: }
 4864: 
 4865: sub helpsettings_javascript {
 4866:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4867:     return unless(ref($roles_by_num) eq 'ARRAY');
 4868:     my %html_js_lt = &Apache::lonlocal::texthash(
 4869:                                           show => 'Show details',
 4870:                                           hide => 'Hide details',
 4871:                                         );
 4872:     &html_escape(\%html_js_lt);
 4873:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4874:     return <<"ENDSCRIPT";
 4875: <script type="text/javascript">
 4876: // <![CDATA[
 4877: 
 4878: function reorderHelpRoles(form,item) {
 4879:     var changedVal;
 4880: $jstext
 4881:     var newpos = 'helproles_${total}_pos';
 4882:     var maxh = 1 + $total;
 4883:     var current = new Array();
 4884:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4885:     if (item == newpos) {
 4886:         changedVal = newitemVal;
 4887:     } else {
 4888:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4889:         current[newitemVal] = newpos;
 4890:     }
 4891:     for (var i=0; i<helproles.length; i++) {
 4892:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4893:         if (elementName != item) {
 4894:             if (form.elements[elementName]) {
 4895:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4896:                 current[currVal] = elementName;
 4897:             }
 4898:         }
 4899:     }
 4900:     var oldVal;
 4901:     for (var j=0; j<maxh; j++) {
 4902:         if (current[j] == undefined) {
 4903:             oldVal = j;
 4904:         }
 4905:     }
 4906:     if (oldVal < changedVal) {
 4907:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4908:            var elementName = current[k];
 4909:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4910:         }
 4911:     } else {
 4912:         for (var k=changedVal; k<oldVal; k++) {
 4913:             var elementName = current[k];
 4914:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4915:         }
 4916:     }
 4917:     return;
 4918: }
 4919: 
 4920: function helpdeskAccess(num) {
 4921:     var curraccess = null;
 4922:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4923:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4924:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4925:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4926:             }
 4927:         }
 4928:     }
 4929:     var shown = Array();
 4930:     var hidden = Array();
 4931:     if (curraccess == 'none') {
 4932:         hidden = Array('$hiddenstr');
 4933:     } else {
 4934:         if (curraccess == 'status') {
 4935:             shown = Array('bystatus');
 4936:             hidden = Array('notinc','notexc');
 4937:         } else {
 4938:             if (curraccess == 'exc') {
 4939:                 shown = Array('notexc');
 4940:                 hidden = Array('notinc','bystatus');
 4941:             }
 4942:             if (curraccess == 'inc') {
 4943:                 shown = Array('notinc');
 4944:                 hidden = Array('notexc','bystatus');
 4945:             }
 4946:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4947:                 hidden = Array('notinc','notexc','bystatus');
 4948:             }
 4949:         }
 4950:     }
 4951:     if (hidden.length > 0) {
 4952:         for (var i=0; i<hidden.length; i++) {
 4953:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4954:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4955:             }
 4956:         }
 4957:     }
 4958:     if (shown.length > 0) {
 4959:         for (var i=0; i<shown.length; i++) {
 4960:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4961:                 if (shown[i] == 'privs') {
 4962:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4963:                 } else {
 4964:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4965:                 }
 4966:             }
 4967:         }
 4968:     }
 4969:     return;
 4970: }
 4971: 
 4972: function toggleHelpdeskItem(num,field) {
 4973:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4974:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4975:             document.getElementById('helproles_'+num+'_'+field).className =
 4976:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4977:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4978:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4979:             }
 4980:         } else {
 4981:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4982:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4983:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4984:             }
 4985:         }
 4986:     }
 4987:     return;
 4988: }
 4989: 
 4990: // ]]>
 4991: </script>
 4992: 
 4993: ENDSCRIPT
 4994: }
 4995: 
 4996: sub helpdeskroles_access {
 4997:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 4998:         $usertypes,$types,$domhelpdesk) = @_;
 4999:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 5000:     my %lt = &Apache::lonlocal::texthash(
 5001:                     'rou'    => 'Role usage',
 5002:                     'whi'    => 'Which helpdesk personnel may use this role?',
 5003:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 5004:                     'dh'     => 'All with domain helpdesk role',
 5005:                     'da'     => 'All with domain helpdesk assistant role',
 5006:                     'none'   => 'None',
 5007:                     'status' => 'Determined based on institutional status',
 5008:                     'inc'    => 'Include all, but exclude specific personnel',
 5009:                     'exc'    => 'Exclude all, but include specific personnel',
 5010:                   );
 5011:     my %usecheck = (
 5012:                      all => ' checked="checked"',
 5013:                    );
 5014:     my %displaydiv = (
 5015:                       status => 'none',
 5016:                       inc    => 'none',
 5017:                       exc    => 'none',
 5018:                       priv   => 'block',
 5019:                      );
 5020:     my $output;
 5021:     if (ref($current) eq 'HASH') {
 5022:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 5023:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 5024:                 $usecheck{$current->{access}} = $usecheck{'all'};
 5025:                 delete($usecheck{'all'});
 5026:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 5027:                     my $access = $1;
 5028:                     $displaydiv{$access} = 'inline';
 5029:                 } elsif ($current->{access} eq 'none') {
 5030:                     $displaydiv{'priv'} = 'none';
 5031:                 }
 5032:             }
 5033:         }
 5034:     }
 5035:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 5036:               '<p>'.$lt{'whi'}.'</p>';
 5037:     foreach my $access (@{$accesstypes}) {
 5038:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 5039:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 5040:                    $lt{$access}.'</label>';
 5041:         if ($access eq 'status') {
 5042:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 5043:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 5044:                                                                  $othertitle,$usertypes,$types).
 5045:                        '</div>';
 5046:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 5047:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 5048:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5049:                        '</div>';
 5050:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 5051:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 5052:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5053:                        '</div>';
 5054:         }
 5055:         $output .= '</p>';
 5056:     }
 5057:     $output .= '</fieldset>';
 5058:     return $output;
 5059: }
 5060: 
 5061: sub radiobutton_prefs {
 5062:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 5063:         $additional,$align,$firstval) = @_;
 5064:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 5065:                    (ref($choices) eq 'HASH'));
 5066: 
 5067:     my (%checkedon,%checkedoff,$datatable,$css_class);
 5068: 
 5069:     foreach my $item (@{$toggles}) {
 5070:         if ($defaultchecked->{$item} eq 'on') {
 5071:             $checkedon{$item} = ' checked="checked" ';
 5072:             $checkedoff{$item} = ' ';
 5073:         } elsif ($defaultchecked->{$item} eq 'off') {
 5074:             $checkedoff{$item} = ' checked="checked" ';
 5075:             $checkedon{$item} = ' ';
 5076:         }
 5077:     }
 5078:     if (ref($settings) eq 'HASH') {
 5079:         foreach my $item (@{$toggles}) {
 5080:             if ($settings->{$item} eq '1') {
 5081:                 $checkedon{$item} =  ' checked="checked" ';
 5082:                 $checkedoff{$item} = ' ';
 5083:             } elsif ($settings->{$item} eq '0') {
 5084:                 $checkedoff{$item} =  ' checked="checked" ';
 5085:                 $checkedon{$item} = ' ';
 5086:             }
 5087:         }
 5088:     }
 5089:     if ($onclick) {
 5090:         $onclick = ' onclick="'.$onclick.'"';
 5091:     }
 5092:     foreach my $item (@{$toggles}) {
 5093:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5094:         $datatable .=
 5095:             '<tr'.$css_class.'><td valign="top">'.
 5096:             '<span class="LC_nobreak">'.$choices->{$item}.
 5097:             '</span></td>';
 5098:         if ($align eq 'left') {
 5099:             $datatable .= '<td class="LC_left_item">';
 5100:         } else {
 5101:             $datatable .= '<td class="LC_right_item">';
 5102:         }
 5103:         $datatable .= '<span class="LC_nobreak">';
 5104:         if ($firstval eq 'no') {
 5105:             $datatable .=
 5106:                 '<label><input type="radio" name="'.
 5107:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 5108:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5109:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 5110:         } else {
 5111:             $datatable .=
 5112:                 '<label><input type="radio" name="'.
 5113:                 $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 5114:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5115:                 $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 5116:         }
 5117:         $datatable .= '</span>'.$additional.'</td></tr>';
 5118:         $itemcount ++;
 5119:     }
 5120:     return ($datatable,$itemcount);
 5121: }
 5122: 
 5123: sub print_ltitools {
 5124:     my ($position,$dom,$settings,$rowtotal) = @_;
 5125:     my (%rules,%encrypt,%privkeys,%linkprot);
 5126:     if (ref($settings) eq 'HASH') {
 5127:         if ($position eq 'top') {
 5128:             if (exists($settings->{'encrypt'})) {
 5129:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 5130:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 5131:                         $encrypt{'toolsec_'.$key} = $settings->{'encrypt'}{$key};
 5132:                     }
 5133:                 }
 5134:             }
 5135:             if (exists($settings->{'private'})) {
 5136:                 if (ref($settings->{'private'}) eq 'HASH') {
 5137:                     if (ref($settings->{'private'}) eq 'HASH') {
 5138:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 5139:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 5140:                         }
 5141:                     }
 5142:                 }
 5143:             }
 5144:         } elsif ($position eq 'middle') {
 5145:             if (exists($settings->{'rules'})) {
 5146:                 if (ref($settings->{'rules'}) eq 'HASH') {
 5147:                     %rules = %{$settings->{'rules'}};
 5148:                 }
 5149:             }
 5150:         } else {
 5151:             foreach my $key ('encrypt','private','rules') {
 5152:                 if (exists($settings->{$key})) {
 5153:                     delete($settings->{$key});
 5154:                 }
 5155:             }
 5156:         }
 5157:     }
 5158:     my $datatable;
 5159:     my $itemcount = 1;
 5160:     if ($position eq 'top') {
 5161:         $datatable = &secrets_form($dom,'toolsec',\%encrypt,\%privkeys,$rowtotal);
 5162:     } elsif ($position eq 'middle') {
 5163:         $datatable = &password_rules('toolsecrets',\$itemcount,\%rules);
 5164:         $$rowtotal += $itemcount;
 5165:     } else {
 5166:         $datatable = &Apache::courseprefs::print_ltitools($dom,'',$settings,\$rowtotal,'','','domain');
 5167:     }
 5168:     return $datatable;
 5169: }
 5170: 
 5171: sub ltitools_names {
 5172:     my %lt = &Apache::lonlocal::texthash(
 5173:                                           'title'          => 'Title',
 5174:                                           'version'        => 'Version',
 5175:                                           'msgtype'        => 'Message Type',
 5176:                                           'sigmethod'      => 'Signature Method',
 5177:                                           'url'            => 'URL',
 5178:                                           'key'            => 'Key',
 5179:                                           'lifetime'       => 'Nonce lifetime (s)',
 5180:                                           'secret'         => 'Secret',
 5181:                                           'icon'           => 'Icon',
 5182:                                           'user'           => 'User',
 5183:                                           'fullname'       => 'Full Name',
 5184:                                           'firstname'      => 'First Name',
 5185:                                           'lastname'       => 'Last Name',
 5186:                                           'email'          => 'E-mail',
 5187:                                           'roles'          => 'Role',
 5188:                                           'window'         => 'Window',
 5189:                                           'tab'            => 'Tab',
 5190:                                           'iframe'         => 'iFrame',
 5191:                                           'height'         => 'Height',
 5192:                                           'width'          => 'Width',
 5193:                                           'linktext'       => 'Default Link Text',
 5194:                                           'explanation'    => 'Default Explanation',
 5195:                                           'passback'       => 'Tool can return grades:',
 5196:                                           'roster'         => 'Tool can retrieve roster:',
 5197:                                           'crstarget'      => 'Display target',
 5198:                                           'crslabel'       => 'Course label',
 5199:                                           'crstitle'       => 'Course title',
 5200:                                           'crslinktext'    => 'Link Text',
 5201:                                           'crsexplanation' => 'Explanation',
 5202:                                           'crsappend'      => 'Provider URL',
 5203:                                         );
 5204:     return %lt;
 5205: }
 5206: 
 5207: sub secrets_form {
 5208:     my ($dom,$context,$encrypt,$privkeys,$rowtotal) = @_;
 5209:     my @ids=&Apache::lonnet::current_machine_ids();
 5210:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 5211:     my $primary = &Apache::lonnet::domain($dom,'primary');
 5212:     my ($css_class,$extra,$numshown,$itemcount,$output);
 5213:     $itemcount = 0;
 5214:     foreach my $hostid (sort(keys(%servers))) {
 5215:         my ($showextra,$divsty,$switch);
 5216:         if ($hostid eq $primary) {
 5217:             if ($context eq 'ltisec') {
 5218:                 if (($encrypt->{'ltisec_consumers'}) || ($encrypt->{'ltisec_domlinkprot'})) {
 5219:                     $showextra = 1;
 5220:                 }
 5221:                 if ($encrypt->{'ltisec_crslinkprot'}) {
 5222:                     $showextra = 1;
 5223:                 }
 5224:             } else {
 5225:                 if (($encrypt->{'toolsec_crs'}) || ($encrypt->{'toolsec_dom'})) {
 5226:                     $showextra = 1;
 5227:                 }
 5228:             }
 5229:             unless (grep(/^\Q$hostid\E$/,@ids)) {
 5230:                 $switch = 1;
 5231:             }
 5232:             if ($showextra) {
 5233:                 $numshown ++;
 5234:                 $divsty = 'display:inline-block';
 5235:             } else {
 5236:                 $divsty = 'display:none';
 5237:             }
 5238:             $extra .= '<fieldset id="'.$context.'_info_'.$hostid.'" style="'.$divsty.'">'.
 5239:                       '<legend>'.$hostid.'</legend>';
 5240:             if ($switch) {
 5241:                 my $switchserver = '<a href="/adm/switchserver?otherserver='.$hostid.'&amp;role='.
 5242:                                    &HTML::Entities::encode($env{'request.role'},'\'<>"&').
 5243:                                    '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5244:                 if (exists($privkeys->{$hostid})) {
 5245:                     $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" />'.
 5246:                               '<span class="LC_nobreak">'.
 5247:                               &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5248:                               '<span class="LC_nobreak">'.&mt('Change?').
 5249:                               '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5250:                               ('&nbsp;'x2).
 5251:                               '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5252:                               '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5253:                               '<span class="LC_nobreak"> - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5254:                               '</span></div>';
 5255:                 } else {
 5256:                     $extra .= '<span class="LC_nobreak">'.
 5257:                               &mt('Key required').' - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5258:                               '</span>'."\n";
 5259:                 }
 5260:             } elsif (exists($privkeys->{$hostid})) {
 5261:                 $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" /><span class="LC_nobreak">'.
 5262:                           &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5263:                           '<span class="LC_nobreak">'.&mt('Change?').
 5264:                           '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5265:                           ('&nbsp;'x2).
 5266:                           '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5267:                           '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5268:                           '<span class="LC_nobreak">'.&mt('New Key').':'.
 5269:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5270:                           '<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>'.
 5271:                           '</span></div>';
 5272:             } else {
 5273:                 $extra .= '<span class="LC_nobreak">'.&mt('Encryption Key').':'.
 5274:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5275:                           '<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>';
 5276:             }
 5277:             $extra .= '</fieldset>';
 5278:         }
 5279:     }
 5280:     my (%choices,@toggles,%defaultchecked);
 5281:     if ($context eq 'ltisec') {
 5282:         %choices = &Apache::lonlocal::texthash (
 5283:                                                   ltisec_crslinkprot => 'Encrypt stored link protection secrets defined in courses',
 5284:                                                   ltisec_domlinkprot => 'Encrypt stored link protection secrets defined in domain',
 5285:                                                   ltisec_consumers   => 'Encrypt stored consumer secrets defined in domain',
 5286:                                                );
 5287:         @toggles = qw(ltisec_crslinkprot ltisec_domlinkprot ltisec_consumers);
 5288:         %defaultchecked = (
 5289:                            'ltisec_crslinkprot' => 'off',
 5290:                            'ltisec_domlinkprot' => 'off',
 5291:                            'ltisec_consumers'   => 'off',
 5292:                           );
 5293:     } else {
 5294:         %choices = &Apache::lonlocal::texthash (
 5295:                                                   toolsec_crs => 'Encrypt stored external tool secrets defined in courses',
 5296:                                                   toolsec_dom => 'Encrypt stored external tool secrets defined in domain',
 5297:                                                );
 5298:         @toggles = qw(toolsec_crs toolsec_dom);
 5299:         %defaultchecked = (
 5300:                            'toolsec_crs' => 'off',
 5301:                            'toolsec_dom' => 'off',
 5302:                           );
 5303:     }
 5304:     my ($onclick,$itemcount);
 5305:     $onclick = 'javascript:toggleLTIEncKey(this.form,'."'$context'".');';
 5306:     ($output,$itemcount) = &radiobutton_prefs($encrypt,\@toggles,\%defaultchecked,
 5307:                                               \%choices,$itemcount,$onclick,'','left','no');
 5308: 
 5309:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5310:     my $noprivkeysty = 'display:inline-block';
 5311:     if ($numshown) {
 5312:         $noprivkeysty = 'display:none';
 5313:     }
 5314:     $output .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.&mt('Encryption Key(s)').'</td>'.
 5315:                '<td><div id="'.$context.'_noprivkey" style="'.$noprivkeysty.'" >'.
 5316:                '<span class="LC_nobreak">'.&mt('Not in use').'</span></div>'.
 5317:                $extra.
 5318:                '</td></tr>';
 5319:     $itemcount ++;
 5320:     $$rowtotal += $itemcount;
 5321:     return $output;
 5322: }
 5323: 
 5324: sub print_lti {
 5325:     my ($position,$dom,$settings,$rowtotal) = @_;
 5326:     my $itemcount = 1;
 5327:     my ($datatable,$css_class);
 5328:     my (%rules,%encrypt,%privkeys,%linkprot,%suggestions);
 5329:     if (ref($settings) eq 'HASH') {
 5330:         if ($position eq 'top') {
 5331:             if (exists($settings->{'encrypt'})) {
 5332:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 5333:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 5334:                         if ($key eq 'consumers') {
 5335:                             $encrypt{'ltisec_'.$key} = $settings->{'encrypt'}{$key};
 5336:                         } else {
 5337:                             $encrypt{'ltisec_'.$key.'linkprot'} = $settings->{'encrypt'}{$key};
 5338:                         }
 5339:                     }
 5340:                 }
 5341:             }
 5342:             if (exists($settings->{'private'})) {
 5343:                 if (ref($settings->{'private'}) eq 'HASH') {
 5344:                     if (ref($settings->{'private'}) eq 'HASH') {
 5345:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 5346:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 5347:                         }
 5348:                     }
 5349:                 }
 5350:             }
 5351:         } elsif ($position eq 'upper') {
 5352:             if (exists($settings->{'rules'})) {
 5353:                 if (ref($settings->{'rules'}) eq 'HASH') {
 5354:                     %rules = %{$settings->{'rules'}};
 5355:                 }
 5356:             }
 5357:         } elsif ($position eq 'middle') {
 5358:             if (exists($settings->{'suggested'})) {
 5359:                 if (ref($settings->{'suggested'}) eq 'HASH') {
 5360:                     %suggestions = %{$settings->{'suggested'}};
 5361:                 }
 5362:             }
 5363:         } elsif ($position eq 'lower') {
 5364:             if (exists($settings->{'linkprot'})) {
 5365:                 if (ref($settings->{'linkprot'}) eq 'HASH') {
 5366:                     %linkprot = %{$settings->{'linkprot'}};
 5367:                     if ($linkprot{'lock'}) {
 5368:                         delete($linkprot{'lock'});
 5369:                     }
 5370:                 }
 5371:             }
 5372:         } else {
 5373:             foreach my $key ('encrypt','private','rules','linkprot','suggestions') {
 5374:                 if (exists($settings->{$key})) {
 5375:                     delete($settings->{$key});
 5376:                 }
 5377:             }
 5378:         }
 5379:     }
 5380:     if ($position eq 'top') {
 5381:         $datatable = &secrets_form($dom,'ltisec',\%encrypt,\%privkeys,$rowtotal);
 5382:     } elsif ($position eq 'upper') {
 5383:         $datatable = &password_rules('ltisecrets',\$itemcount,\%rules);
 5384:         $$rowtotal += $itemcount;
 5385:     } elsif ($position eq 'middle') {
 5386:         $datatable = &linkprot_suggestions(\%suggestions,\$itemcount);
 5387:         $$rowtotal += $itemcount;
 5388:     } elsif ($position eq 'lower') {
 5389:         $datatable .= &Apache::courseprefs::print_linkprotection($dom,'',$settings,$rowtotal,'','','domain');
 5390:     } else {
 5391:         my ($switchserver,$switchmessage);
 5392:         $switchserver = &check_switchserver($dom);
 5393:         $switchmessage = &mt("submit from domain's primary library server: [_1].",$switchserver);
 5394:         my $maxnum = 0;
 5395:         my %ordered;
 5396:         if (ref($settings) eq 'HASH') {
 5397:             foreach my $item (keys(%{$settings})) {
 5398:                 if (ref($settings->{$item}) eq 'HASH') {
 5399:                     my $num = $settings->{$item}{'order'};
 5400:                     if ($num eq '') {
 5401:                         $num = scalar(keys(%{$settings}));
 5402:                     }
 5403:                     $ordered{$num} = $item;
 5404:                 }
 5405:             }
 5406:         }
 5407:         $maxnum = scalar(keys(%ordered));
 5408:         my %lt = &lti_names();
 5409:         if (keys(%ordered)) {
 5410:             my @items = sort { $a <=> $b } keys(%ordered);
 5411:             for (my $i=0; $i<@items; $i++) {
 5412:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5413:                 my $item = $ordered{$items[$i]};
 5414:                 my ($key,$secret,$usable,$lifetime,$consumer,$requser,$crsinc,$current);
 5415:                 if (ref($settings->{$item}) eq 'HASH') {
 5416:                     $key = $settings->{$item}->{'key'};
 5417:                     $usable = $settings->{$item}->{'usable'};
 5418:                     $lifetime = $settings->{$item}->{'lifetime'};
 5419:                     $consumer = $settings->{$item}->{'consumer'};
 5420:                     $requser = $settings->{$item}->{'requser'};
 5421:                     $crsinc = $settings->{$item}->{'crsinc'};
 5422:                     $current = $settings->{$item};
 5423:                 }
 5424:                 my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 5425:                 my %checkedrequser = (
 5426:                                        yes => ' checked="checked"',
 5427:                                        no  => '',
 5428:                                      );
 5429:                 if (!$requser) {
 5430:                     $checkedrequser{'no'} = $checkedrequser{'yes'};
 5431:                     $checkedrequser{'yes'} = '';
 5432:                 }
 5433:                 my $onclickcrsinc = ' onclick="toggleLTI(this.form,'."'crsinc','$i'".');"';
 5434:                 my %checkedcrsinc = (
 5435:                                       yes => ' checked="checked"',
 5436:                                       no  => '',
 5437:                                     );
 5438:                 if (!$crsinc) {
 5439:                     $checkedcrsinc{'no'} = $checkedcrsinc{'yes'};
 5440:                     $checkedcrsinc{'yes'} = '';
 5441:                 }
 5442:                 my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 5443:                 $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5444:                              .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 5445:                 for (my $k=0; $k<=$maxnum; $k++) {
 5446:                     my $vpos = $k+1;
 5447:                     my $selstr;
 5448:                     if ($k == $i) {
 5449:                         $selstr = ' selected="selected" ';
 5450:                     }
 5451:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5452:                 }
 5453:                 $datatable .= '</select>'.('&nbsp;'x2).
 5454:                     '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 5455:                     &mt('Delete?').'</label></span></td>'.
 5456:                     '<td colspan="2">'.
 5457:                     '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5458:                     '<span class="LC_nobreak">'.$lt{'consumer'}.
 5459:                     ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 5460:                     ('&nbsp;'x2).
 5461:                     '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 5462:                     '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 5463:                     ('&nbsp;'x2).
 5464:                     '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 5465:                     'value="'.$lifetime.'" size="3" /></span><br /><br />';
 5466:                 if ($key ne '') {
 5467:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'};
 5468:                     if ($switchserver) {
 5469:                         $datatable .= ': ['.&mt('[_1] to view/edit',$switchserver).']';
 5470:                     } else {
 5471:                         $datatable .= ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />';
 5472:                     }
 5473:                     $datatable .= '</span> '.('&nbsp;'x2);
 5474:                 } elsif (!$switchserver) {
 5475:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':'.
 5476:                                   '<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />'.
 5477:                                   '</span> '.('&nbsp;'x2);
 5478:                 }
 5479:                 if ($switchserver) {
 5480:                     if ($usable ne '') {
 5481:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 5482:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5483:                                       '<span class="LC_nobreak">'.&mt('Change secret?').
 5484:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 5485:                                       ('&nbsp;'x2).
 5486:                                      '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').'</label>'.('&nbsp;'x2).
 5487:                                       '</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 5488:                                       '<span class="LC_nobreak"> - '.$switchmessage.'</span>'.
 5489:                                       '</div>';
 5490:                     } elsif ($key eq '') {
 5491:                         $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 5492:                     } else {
 5493:                         $datatable .= '<span class="LC_nobreak">'.&mt('Secret required').' - '.$switchmessage.'</span>'."\n";
 5494:                     }
 5495:                 } else {
 5496:                     if ($usable ne '') {
 5497:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 5498:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5499:                                       '<span class="LC_nobreak">'.&mt('Change?').
 5500:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 5501:                                       ('&nbsp;'x2).
 5502:                                       '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').
 5503:                                       '</label>&nbsp;&nbsp;</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 5504:                                       '<span class="LC_nobreak">'.&mt('New Secret').':'.
 5505:                                       '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 5506:                                       '<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>';
 5507:                     } else {
 5508:                         $datatable .=
 5509:                             '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 5510:                             '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 5511:                             '<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>';
 5512:                     }
 5513:                 }
 5514:                 $datatable .= '<br /><br />'.
 5515:                     '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 5516:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5517:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 5518:                     '<br /><br />'.
 5519:                     '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 5520:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="1"'.$onclickcrsinc.$checkedcrsinc{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5521:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="0"'.$onclickcrsinc.$checkedcrsinc{no}.' />'.&mt('No').'</label></span>'."\n".
 5522:                     ('&nbsp;'x4).
 5523:                     '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 5524:                     '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 5525:                 $itemcount ++;
 5526:             }
 5527:         }
 5528:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5529:         my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 5530:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 5531:                       '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 5532:                       '<select name="lti_pos_add"'.$chgstr.'>';
 5533:         for (my $k=0; $k<$maxnum+1; $k++) {
 5534:             my $vpos = $k+1;
 5535:             my $selstr;
 5536:             if ($k == $maxnum) {
 5537:                 $selstr = ' selected="selected" ';
 5538:             }
 5539:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5540:         }
 5541:         $datatable .= '</select>&nbsp;'."\n".
 5542:                       '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 5543:                       '<td colspan="2">'.
 5544:                       '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5545:                       '<span class="LC_nobreak">'.$lt{'consumer'}.
 5546:                       ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 5547:                       ('&nbsp;'x2).
 5548:                       '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 5549:                       '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 5550:                       ('&nbsp;'x2).
 5551:                       '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span><br /><br />'."\n";
 5552:         if ($switchserver) {
 5553:             $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 5554:         } else {
 5555:             $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" autocomplete="off" /></span> '."\n".
 5556:                           ('&nbsp;'x2).
 5557:                           '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" autocomplete="new-password" />'.
 5558:                           '<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";
 5559:         }
 5560:         $datatable .= '<br /><br />'.
 5561:                       '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 5562:                       '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 5563:                       '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 5564:                       '<br /><br />'.
 5565:                       '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 5566:                       '<label><input type="radio" name="lti_crsinc_add" value="1" onclick="toggleLTI(this.form,'."'crsinc','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 5567:                       '<label><input type="radio" name="lti_crsinc_add" value="0" onclick="toggleLTI(this.form,'."'crsinc','add'".');" />'.&mt('No').'</label></span>'."\n".
 5568:                       '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 5569:                       '</td>'."\n".
 5570:                       '</tr>'."\n";
 5571:         $itemcount ++;
 5572:     }
 5573:     $$rowtotal += $itemcount;
 5574:     return $datatable;
 5575: }
 5576: 
 5577: sub lti_names {
 5578:     my %lt = &Apache::lonlocal::texthash(
 5579:                                           'version'   => 'LTI Version',
 5580:                                           'url'       => 'URL',
 5581:                                           'key'       => 'Key',
 5582:                                           'lifetime'  => 'Nonce lifetime (s)',
 5583:                                           'consumer'  => 'Consumer',
 5584:                                           'secret'    => 'Secret',
 5585:                                           'requser'   => "User's identity sent",
 5586:                                           'crsinc'    => "Course's identity sent",
 5587:                                           'email'     => 'Email address',
 5588:                                           'sourcedid' => 'User ID',
 5589:                                           'other'     => 'Other',
 5590:                                           'passback'  => 'Can return grades to Consumer:',
 5591:                                           'roster'    => 'Can retrieve roster from Consumer:',
 5592:                                           'topmenu'   => 'Display LON-CAPA page header',
 5593:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 5594:                                         );
 5595:     return %lt;
 5596: }
 5597: 
 5598: sub lti_options {
 5599:     my ($num,$current,$itemcount,%lt) = @_;
 5600:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 5601:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 5602:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 5603:     $checked{'storecrs'}{'Y'} = ' checked="checked"';
 5604:     $checked{'makecrs'}{'N'} = ' checked="checked"';
 5605:     $checked{'mapcrstype'} = {};
 5606:     $checked{'makeuser'} = {};
 5607:     $checked{'selfenroll'} = {};
 5608:     $checked{'crssec'} = {};
 5609:     $checked{'crssecsrc'} = {};
 5610:     $checked{'lcauth'} = {};
 5611:     $checked{'menuitem'} = {};
 5612:     if ($num eq 'add') {
 5613:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 5614:     }
 5615:     my $userfieldsty = 'none';
 5616:     my $crsfieldsty = 'none';
 5617:     my $crssecfieldsty = 'none';
 5618:     my $secsrcfieldsty = 'none';
 5619:     my $callbacksty = 'none';
 5620:     my $passbacksty = 'none';
 5621:     my $optionsty = 'block';
 5622:     my $crssty = 'block';
 5623:     my $lcauthparm;
 5624:     my $lcauthparmstyle = 'display:none';
 5625:     my $lcauthparmtext;
 5626:     my $menusty;
 5627:     my $numinrow = 4;
 5628:     my %menutitles = &ltimenu_titles();
 5629: 
 5630:     if (ref($current) eq 'HASH') {
 5631:         if (!$current->{'requser'}) {
 5632:             $optionsty = 'none';
 5633:             $crssty = 'none';
 5634:         } elsif (!$current->{'crsinc'}) {
 5635:             $crssty = 'none';
 5636:         }
 5637:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 5638:             $checked{'mapuser'}{'sourcedid'} = '';
 5639:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 5640:                 $checked{'mapuser'}{'email'} = ' checked="checked"';
 5641:             } else {
 5642:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 5643:                 $userfield = $current->{'mapuser'};
 5644:                 $userfieldsty = 'inline-block';
 5645:             }
 5646:         }
 5647:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 5648:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 5649:             if ($current->{'mapcrs'} eq 'context_id') {
 5650:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"';
 5651:             } else {
 5652:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 5653:                 $cidfield = $current->{'mapcrs'};
 5654:                 $crsfieldsty = 'inline-block';
 5655:             }
 5656:         }
 5657:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 5658:             foreach my $type (@{$current->{'mapcrstype'}}) {
 5659:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 5660:             }
 5661:         }
 5662:         if (!$current->{'storecrs'}) {
 5663:             $checked{'storecrs'}{'N'} = $checked{'storecrs'}{'Y'};
 5664:             $checked{'storecrs'}{'Y'} = '';
 5665:         }
 5666:         if ($current->{'makecrs'}) {
 5667:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 5668:         }
 5669:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 5670:             foreach my $role (@{$current->{'makeuser'}}) {
 5671:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 5672:             }
 5673:         }
 5674:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 5675:             $checked{'lcauth'}{$1} = ' checked="checked"';
 5676:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 5677:                 $lcauthparm = $current->{'lcauthparm'};
 5678:                 $lcauthparmstyle = 'display:table-row';
 5679:                 if ($current->{'lcauth'} eq 'localauth') {
 5680:                     $lcauthparmtext = &mt('Local auth argument');
 5681:                 } else {
 5682:                     $lcauthparmtext = &mt('Kerberos domain');
 5683:                 }
 5684:             }
 5685:         }
 5686:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 5687:             foreach my $role (@{$current->{'selfenroll'}}) {
 5688:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 5689:             }
 5690:         }
 5691:         if (ref($current->{'maproles'}) eq 'HASH') {
 5692:             %rolemaps = %{$current->{'maproles'}};
 5693:         }
 5694:         if ($current->{'section'} ne '') {
 5695:             $checked{'crssec'}{'Y'} = '  checked="checked"';
 5696:             $crssecfieldsty = 'inline-block';
 5697:             if ($current->{'section'} eq 'course_section_sourcedid') {
 5698:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 5699:             } else {
 5700:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 5701:                 $crssecsrc = $current->{'section'};
 5702:                 $secsrcfieldsty = 'inline-block';
 5703:             }
 5704:         } else {
 5705:             $checked{'crssec'}{'N'} = ' checked="checked"';
 5706:         }
 5707:         if ($current->{'callback'} ne '') {
 5708:             $callback = $current->{'callback'};
 5709:             $checked{'callback'}{'Y'} = ' checked="checked"';
 5710:             $callbacksty = 'inline-block';
 5711:         } else {
 5712:             $checked{'callback'}{'N'} = ' checked="checked"';
 5713:         }
 5714:         if ($current->{'topmenu'}) {
 5715:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 5716:         } else {
 5717:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 5718:         }
 5719:         if ($current->{'inlinemenu'}) {
 5720:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 5721:         } else {
 5722:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 5723:         }
 5724:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 5725:             $menusty = 'inline-block';
 5726:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 5727:                 foreach my $item (@{$current->{'lcmenu'}}) {
 5728:                     if (exists($menutitles{$item})) {
 5729:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 5730:                     }
 5731:                 }
 5732:             }
 5733:         } else {
 5734:             $menusty = 'none';
 5735:         }
 5736:     } else {
 5737:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 5738:         $checked{'crssec'}{'N'} = ' checked="checked"';
 5739:         $checked{'callback'}{'N'} = ' checked="checked"';
 5740:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 5741:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 5742:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 5743:         $menusty = 'inline-block';
 5744:     }
 5745:     my @coursetypes = ('official','unofficial','community','textbook','lti');
 5746:     my %coursetypetitles = &Apache::lonlocal::texthash (
 5747:                                official   => 'Official',
 5748:                                unofficial => 'Unofficial',
 5749:                                community  => 'Community',
 5750:                                textbook   => 'Textbook',
 5751:                                lti        => 'LTI Provider',
 5752:     );
 5753:     my @authtypes = ('internal','krb4','krb5','localauth');
 5754:     my %shortauth = (
 5755:                      internal => 'int',
 5756:                      krb4 => 'krb4',
 5757:                      krb5 => 'krb5',
 5758:                      localauth  => 'loc'
 5759:                     );
 5760:     my %authnames = &authtype_names();
 5761:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 5762:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 5763:     my @courseroles = ('cc','in','ta','ep','st');
 5764:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 5765:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 5766:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 5767:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 5768:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 5769:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 5770:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 5771:     my $output = '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Logout options').'</legend>'.
 5772:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback to logout LON-CAPA on log out from Consumer').':&nbsp;'.
 5773:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 5774:                  $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5775:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 5776:                  $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 5777:                  '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 5778:                  '<span class="LC_nobreak">'.&mt('Parameter').': '.
 5779:                  '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 5780:                  '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 5781:                  '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 5782:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 5783:     foreach my $option ('sourcedid','email','other') {
 5784:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 5785:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 5786:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5787:     }
 5788:     $output .= '</span></div>'.
 5789:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 5790:                '<input type="text" name="lti_customuser_'.$num.'" '.
 5791:                'value="'.$userfield.'" /></div></fieldset>'.
 5792:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 5793:     foreach my $ltirole (@ltiroles) {
 5794:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 5795:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';
 5796:     }
 5797:     $output .= '</fieldset>'.
 5798:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 5799:                '<table>'.
 5800:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 5801:                '</table>'.
 5802:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 5803:                '<td class="LC_left_item">';
 5804:     foreach my $auth ('lti',@authtypes) {
 5805:         my $authtext;
 5806:         if ($auth eq 'lti') {
 5807:             $authtext = &mt('None');
 5808:         } else {
 5809:             $authtext = $authnames{$shortauth{$auth}};
 5810:         }
 5811:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 5812:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 5813:                    $authtext.'</label></span> &nbsp;';
 5814:     }
 5815:     $output .= '</td></tr>'.
 5816:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 5817:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 5818:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 5819:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 5820:                '</table></fieldset>'.
 5821:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.
 5822:                &mt('LON-CAPA menu items (Course Coordinator can override)').'</legend>'.
 5823:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 5824:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 5825:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5826:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 5827:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 5828:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5829:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 5830:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 5831:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5832:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 5833:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 5834:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5835:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 5836:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 5837:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 5838:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 5839:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 5840:                    ('&nbsp;'x2);
 5841:     }
 5842:     $output .= '</span></div></fieldset>'.
 5843:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 5844:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 5845:                &mt('Unique course identifier').':&nbsp;';
 5846:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 5847:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 5848:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 5849:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5850:     }
 5851:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 5852:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 5853:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5854:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 5855:     foreach my $type (@coursetypes) {
 5856:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 5857:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 5858:                    ('&nbsp;'x2);
 5859:     }
 5860:     $output .= '</span><br /><br />'.
 5861:                '<span class="LC_nobreak">'.&mt('Store mapping of course identifier to LON-CAPA CourseID').':&nbsp;'.
 5862:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="0"'.
 5863:                $checked{'storecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5864:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="1"'.
 5865:                $checked{'storecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 5866:                '</fieldset>'.
 5867:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 5868:     foreach my $ltirole (@lticourseroles) {
 5869:         my ($selected,$selectnone);
 5870:         if ($rolemaps{$ltirole} eq '') {
 5871:             $selectnone = ' selected="selected"';
 5872:         }
 5873:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 5874:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 5875:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5876:         foreach my $role (@courseroles) {
 5877:             unless ($selectnone) {
 5878:                 if ($rolemaps{$ltirole} eq $role) {
 5879:                     $selected = ' selected="selected"';
 5880:                 } else {
 5881:                     $selected = '';
 5882:                 }
 5883:             }
 5884:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 5885:                        &Apache::lonnet::plaintext($role,'Course').
 5886:                        '</option>';
 5887:         }
 5888:         $output .= '</select></td>';
 5889:     }
 5890:     $output .= '</tr></table></fieldset>'.
 5891:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Creating courses').'</legend>'.
 5892:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 5893:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 5894:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5895:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 5896:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 5897:                '</fieldset>'.
 5898:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 5899:     foreach my $lticrsrole (@lticourseroles) {
 5900:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 5901:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 5902:     }
 5903:     $output .= '</fieldset>'.
 5904:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Course options').'</legend>'.
 5905:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 5906:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 5907:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5908:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 5909:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 5910:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 5911:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 5912:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 5913:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 5914:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 5915:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 5916:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 5917:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 5918:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 5919:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5920:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 5921:     foreach my $extra ('roster','passback') {
 5922:         my $checkedon = '';
 5923:         my $checkedoff = ' checked="checked"';
 5924:         if ($extra eq 'passback') {
 5925:             $pb1p1chk = ' checked="checked"';
 5926:             $pb1p0chk = '';
 5927:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"';
 5928:         } else {
 5929:             $onclickpb = '';
 5930:         }
 5931:         if (ref($current) eq 'HASH') {
 5932:             if (($current->{$extra})) {
 5933:                 $checkedon = $checkedoff;
 5934:                 $checkedoff = '';
 5935:                 if ($extra eq 'passback') {
 5936:                     $passbacksty = 'inline-block';
 5937:                 }
 5938:                 if ($current->{'passbackformat'} eq '1.0') {
 5939:                     $pb1p0chk =  ' checked="checked"';
 5940:                     $pb1p1chk = '';
 5941:                 }
 5942:             }
 5943:         }
 5944:         $output .= $lt{$extra}.'&nbsp;'.
 5945:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 5946:                    &mt('No').'</label>'.('&nbsp;'x2).
 5947:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 5948:                    &mt('Yes').'</label><br />';
 5949:     }
 5950:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 5951:                '<span class="LC_nobreak">'.&mt('Grade format').
 5952:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 5953:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 5954:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 5955:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 5956:                '<div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>';
 5957:     $output .= '</span></div></fieldset>';
 5958: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 5959: #
 5960: #    $output .= '</fieldset>'.
 5961: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 5962:     return $output;
 5963: }
 5964: 
 5965: sub ltimenu_titles {
 5966:     return &Apache::lonlocal::texthash(
 5967:                                         fullname    => 'Full name',
 5968:                                         coursetitle => 'Course title',
 5969:                                         role        => 'Role',
 5970:                                         logout      => 'Logout',
 5971:                                         grades      => 'Grades',
 5972:     );
 5973: }
 5974: 
 5975: sub linkprot_suggestions {
 5976:     my ($suggested,$itemcount) = @_;
 5977:     my $count = 0;
 5978:     my $next = 1;
 5979:     my %lt = &Apache::lonlocal::texthash(
 5980:                                           'name' => 'Suggested Launcher',
 5981:                                           'info' => 'Recommendations',
 5982:                                         );
 5983:     my ($datatable,$css_class,$dest);
 5984:     if (ref($suggested) eq 'HASH') {
 5985:         my @current = sort { $a <=> $b } keys(%{$suggested});
 5986:         $next += $current[-1];
 5987:         for (my $i=0; $i<@current; $i++) {
 5988:             my $num = $current[$i];
 5989:             my %values;
 5990:             if (ref($suggested->{$num}) eq 'HASH') {
 5991:                 %values = %{$suggested->{$num}};
 5992:             } else {
 5993:                 next;
 5994:             }
 5995:             $css_class = $$itemcount%2?' class="LC_odd_row"':'';
 5996:             $datatable .=
 5997:                 '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 5998:                 '<label><input type="checkbox" name="linkprot_suggested_del" value="'.$i.'" />'."\n".
 5999:                 &mt('Delete?').'</label></span></td><td>'."\n".
 6000:                 '<div class="LC_floatleft"><fieldset><legend>'.$lt{'name'}.'</legend>'."\n".
 6001:                 '<input type="text" size="15" name="linkprot_suggested_name_'.$i.'" value="'.$values{'name'}.'" autocomplete="off" />'."\n".
 6002:                 '</fieldset></div>'.
 6003:                 '<div class="LC_floatleft"><fieldset><legend>'.$lt{'info'}.'</legend>'."\n".
 6004:                 '<textarea cols="55" rows="5" name="linkprot_suggested_info_'.$i.'">'.$values{'info'}.'</textarea>'.
 6005:                 '</fieldset></div>'.
 6006:                 '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
 6007:                 '<input type="hidden" name="linkprot_suggested_id_'.$i.'" value="'.$num.'" /></td></tr>'."\n";
 6008:             $$itemcount ++;
 6009:         }
 6010:     }
 6011:     $css_class = $$itemcount%2?' class="LC_odd_row"':'';
 6012:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6013:                   '<input type="hidden" name="linkprot_suggested_maxnum" value="'.$next.'" />'."\n".
 6014:                   '<input type="checkbox" name="linkprot_suggested_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6015:                   '<td>'."\n".
 6016:                   '<div class="LC_floatleft"><fieldset><legend>'.$lt{'name'}.'</legend>'."\n".
 6017:                   '<input type="text" size="15" name="linkprot_suggested_name_add" value="" autocomplete="off" />'."\n".
 6018:                   '</fieldset></div>'.
 6019:                   '<div class="LC_floatleft"><fieldset><legend>'.$lt{'info'}.'</legend>'."\n".
 6020:                   '<textarea cols="55" rows="5" name="linkprot_suggested_info_add"></textarea>'.
 6021:                   '</fieldset></div>'.
 6022:                   '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
 6023:                   '</td></tr>'."\n";
 6024:     return $datatable;
 6025: }
 6026: 
 6027: sub print_coursedefaults {
 6028:     my ($position,$dom,$settings,$rowtotal) = @_;
 6029:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6030:     my $itemcount = 1;
 6031:     my %choices =  &Apache::lonlocal::texthash (
 6032:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6033:         coursequota          => 'Default cumulative quota for all group portfolio spaces in course (MB)',
 6034:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6035:         coursecredits        => 'Credits can be specified for courses',
 6036:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6037:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6038:         inline_chem          => 'Use inline previewer for chemical reaction response in place of pop-up',
 6039:         texengine            => 'Default method to display mathematics',
 6040:         postsubmit           => 'Disable submit button/keypress following student submission',
 6041:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6042:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6043:         ltiauth              => 'Student username in LTI launch of deep-linked URL can be accepted without re-authentication',
 6044:         domexttool           => 'External Tools defined in the domain may be used in courses/communities (by type)',
 6045:         exttool              => 'External Tools can be defined and configured in courses/communities (by type)',
 6046:     );
 6047:     my %staticdefaults = (
 6048:                            anonsurvey_threshold => 10,
 6049:                            uploadquota          => 500,
 6050:                            coursequota          => 20,
 6051:                            postsubmit           => 60,
 6052:                            mysqltables          => 172800,
 6053:                            domexttool           => 1,
 6054:                            exttool              => 0,
 6055:                          );
 6056:     if ($position eq 'top') {
 6057:         %defaultchecked = (
 6058:                             'uselcmath'       => 'on',
 6059:                             'usejsme'         => 'on',
 6060:                             'inline_chem'     => 'on',
 6061:                             'canclone'        => 'none',
 6062:                           );
 6063:         @toggles = ('uselcmath','usejsme','inline_chem');
 6064:         my $deftex = $Apache::lonnet::deftex;
 6065:         if (ref($settings) eq 'HASH') {
 6066:             if ($settings->{'texengine'}) {
 6067:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6068:                     $deftex = $settings->{'texengine'};
 6069:                 }
 6070:             }
 6071:         }
 6072:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6073:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6074:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6075:                        '</span></td><td class="LC_right_item">'.
 6076:                        '<select name="texengine">'."\n";
 6077:         my %texoptions = (
 6078:                             MathJax  => 'MathJax',
 6079:                             mimetex  => &mt('Convert to Images'),
 6080:                             tth      => &mt('TeX to HTML'),
 6081:                          );
 6082:         foreach my $renderer ('MathJax','mimetex','tth') {
 6083:             my $selected = '';
 6084:             if ($renderer eq $deftex) {
 6085:                 $selected = ' selected="selected"';
 6086:             }
 6087:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6088:         }
 6089:         $mathdisp .= '</select></td></tr>'."\n";
 6090:         $itemcount ++;
 6091:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6092:                                                      \%choices,$itemcount);
 6093:         $datatable = $mathdisp.$datatable;
 6094:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6095:         $datatable .=
 6096:             '<tr'.$css_class.'><td valign="top">'.
 6097:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6098:             '</span></td><td class="LC_left_item">';
 6099:         my $currcanclone = 'none';
 6100:         my $onclick;
 6101:         my @cloneoptions = ('none','domain');
 6102:         my %clonetitles = &Apache::lonlocal::texthash (
 6103:                              none     => 'No additional course requesters',
 6104:                              domain   => "Any course requester in course's domain",
 6105:                              instcode => 'Course requests for official courses ...',
 6106:                           );
 6107:         my (%codedefaults,@code_order,@posscodes);
 6108:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6109:                                                     \@code_order) eq 'ok') {
 6110:             if (@code_order > 0) {
 6111:                 push(@cloneoptions,'instcode');
 6112:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6113:             }
 6114:         }
 6115:         if (ref($settings) eq 'HASH') {
 6116:             if ($settings->{'canclone'}) {
 6117:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6118:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6119:                         if (@code_order > 0) {
 6120:                             $currcanclone = 'instcode';
 6121:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 6122:                         }
 6123:                     }
 6124:                 } elsif ($settings->{'canclone'} eq 'domain') {
 6125:                     $currcanclone = $settings->{'canclone'};
 6126:                 }
 6127:             }
 6128:         }
 6129:         foreach my $option (@cloneoptions) {
 6130:             my ($checked,$additional);
 6131:             if ($currcanclone eq $option) {
 6132:                 $checked = ' checked="checked"';
 6133:             }
 6134:             if ($option eq 'instcode') {
 6135:                 if (@code_order) {
 6136:                     my $show = 'none';
 6137:                     if ($checked) {
 6138:                         $show = 'block';
 6139:                     }
 6140:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 6141:                                   &mt('Institutional codes for new and cloned course have identical:').
 6142:                                   '<br />';
 6143:                     foreach my $item (@code_order) {
 6144:                         my $codechk;
 6145:                         if ($checked) {
 6146:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 6147:                                 $codechk = ' checked="checked"';
 6148:                             }
 6149:                         }
 6150:                         $additional .= '<label>'.
 6151:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 6152:                                        $item.'</label>';
 6153:                     }
 6154:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 6155:                 }
 6156:             }
 6157:             $datatable .=
 6158:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 6159:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 6160:                 '</label>&nbsp;'.$additional.'</span><br />';
 6161:         }
 6162:         $datatable .= '</td>'.
 6163:                       '</tr>';
 6164:         $itemcount ++;
 6165:     } else {
 6166:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6167:         my ($currdefresponder,%defcredits,%curruploadquota,%currcoursequota,
 6168:             %deftimeout,%currmysql);
 6169:         my $currusecredits = 0;
 6170:         my $postsubmitclient = 1;
 6171:         my $ltiauth = 0;
 6172:         my %domexttool;
 6173:         my %exttool;
 6174:         my @types = ('official','unofficial','community','textbook');
 6175:         if (ref($settings) eq 'HASH') {
 6176:             if ($settings->{'ltiauth'}) {
 6177:                 $ltiauth = 1;
 6178:             }
 6179:             if (ref($settings->{'domexttool'}) eq 'HASH') {
 6180:                 foreach my $type (@types) {
 6181:                     if ($settings->{'domexttool'}->{$type}) {
 6182:                         $domexttool{$type} = ' checked="checked"';
 6183:                     }
 6184:                 }
 6185:             } else {
 6186:                 foreach my $type (@types) {
 6187:                     if ($staticdefaults{'domexttool'}) {
 6188:                         $domexttool{$type} = ' checked="checked"';
 6189:                     }
 6190:                 }
 6191:             }
 6192:             if (ref($settings->{'exttool'}) eq 'HASH') {
 6193:                 foreach my $type (@types) {
 6194:                     if ($settings->{'exttool'}->{$type}) {
 6195:                         $exttool{$type} = ' checked="checked"';
 6196:                     }
 6197:                 }
 6198:             }
 6199:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 6200:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 6201:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 6202:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 6203:                 }
 6204:             }
 6205:             if (ref($settings->{'coursequota'}) eq 'HASH') {
 6206:                 foreach my $type (keys(%{$settings->{'coursequota'}})) {
 6207:                     $currcoursequota{$type} = $settings->{'coursequota'}{$type};
 6208:                 }
 6209:             }
 6210:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 6211:                 foreach my $type (@types) {
 6212:                     next if ($type eq 'community');
 6213:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 6214:                     if ($defcredits{$type} ne '') {
 6215:                         $currusecredits = 1;
 6216:                     }
 6217:                 }
 6218:             }
 6219:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 6220:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 6221:                     $postsubmitclient = 0;
 6222:                     foreach my $type (@types) {
 6223:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6224:                     }
 6225:                 } else {
 6226:                     foreach my $type (@types) {
 6227:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 6228:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 6229:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 6230:                             } else {
 6231:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6232:                             }
 6233:                         } else {
 6234:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6235:                         }
 6236:                     }
 6237:                 }
 6238:             } else {
 6239:                 foreach my $type (@types) {
 6240:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6241:                 }
 6242:             }
 6243:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 6244:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 6245:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 6246:                 }
 6247:             } else {
 6248:                 foreach my $type (@types) {
 6249:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 6250:                 }
 6251:             }
 6252:         } else {
 6253:             foreach my $type (@types) {
 6254:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6255:                 if ($staticdefaults{'domexttool'}) {
 6256:                     $domexttool{$type} = ' checked="checked"';
 6257:                 }
 6258:             }
 6259:         }
 6260:         if (!$currdefresponder) {
 6261:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 6262:         } elsif ($currdefresponder < 1) {
 6263:             $currdefresponder = 1;
 6264:         }
 6265:         foreach my $type (@types) {
 6266:             if ($curruploadquota{$type} eq '') {
 6267:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 6268:             }
 6269:             if ($currcoursequota{$type} eq '') {
 6270:                 $currcoursequota{$type} = $staticdefaults{'coursequota'};
 6271:             }
 6272:         }
 6273:         $datatable .=
 6274:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6275:                 $choices{'anonsurvey_threshold'}.
 6276:                 '</span></td>'.
 6277:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 6278:                 '<input type="text" name="anonsurvey_threshold"'.
 6279:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 6280:                 '</td></tr>'."\n";
 6281:         $itemcount ++;
 6282:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6283:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6284:                       $choices{'uploadquota'}.
 6285:                       '</span></td>'.
 6286:                       '<td align="right" class="LC_right_item">'.
 6287:                       '<table><tr>';
 6288:         foreach my $type (@types) {
 6289:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 6290:                            '<input type="text" name="uploadquota_'.$type.'"'.
 6291:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 6292:         }
 6293:         $datatable .= '</tr></table></td></tr>'."\n";
 6294:         $itemcount ++;
 6295:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6296:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6297:                       $choices{'coursequota'}.
 6298:                       '</span></td>'.
 6299:                       '<td style="text-align: right" class="LC_right_item">'.
 6300:                       '<table><tr>';
 6301:         foreach my $type (@types) {
 6302:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6303:                            '<input type="text" name="coursequota_'.$type.'"'.
 6304:                            ' value="'.$currcoursequota{$type}.'" size="5" /></td>';
 6305:         }
 6306:         $datatable .= '</tr></table></td></tr>'."\n";
 6307:         $itemcount ++;
 6308:         my $onclick = "toggleDisplay(this.form,'credits');";
 6309:         my $display = 'none';
 6310:         if ($currusecredits) {
 6311:             $display = 'block';
 6312:         }
 6313:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 6314:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 6315:         foreach my $type (@types) {
 6316:             next if ($type eq 'community');
 6317:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 6318:                            '<input type="text" name="'.$type.'_credits"'.
 6319:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 6320:         }
 6321:         $additional .= '</tr></table></div>'."\n";
 6322:         %defaultchecked = ('coursecredits' => 'off');
 6323:         @toggles = ('coursecredits');
 6324:         my $current = {
 6325:                         'coursecredits' => $currusecredits,
 6326:                       };
 6327:         (my $table,$itemcount) =
 6328:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6329:                                \%choices,$itemcount,$onclick,$additional,'left');
 6330:         $datatable .= $table;
 6331:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 6332:         my $display = 'none';
 6333:         if ($postsubmitclient) {
 6334:             $display = 'block';
 6335:         }
 6336:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 6337:                       &mt('Number of seconds submit is disabled').'<br />'.
 6338:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 6339:                       '<table><tr>';
 6340:         foreach my $type (@types) {
 6341:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 6342:                            '<input type="text" name="'.$type.'_timeout" value="'.
 6343:                            $deftimeout{$type}.'" size="5" /></td>';
 6344:         }
 6345:         $additional .= '</tr></table></div>'."\n";
 6346:         %defaultchecked = ('postsubmit' => 'on');
 6347:         @toggles = ('postsubmit');
 6348:         $current = {
 6349:                        'postsubmit' => $postsubmitclient,
 6350:                    };
 6351:         ($table,$itemcount) =
 6352:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6353:                                \%choices,$itemcount,$onclick,$additional,'left');
 6354:         $datatable .= $table;
 6355:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6356:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6357:                       $choices{'mysqltables'}.
 6358:                       '</span></td>'.
 6359:                       '<td align="right" class="LC_right_item">'.
 6360:                       '<table><tr>';
 6361:         foreach my $type (@types) {
 6362:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 6363:                            '<input type="text" name="mysqltables_'.$type.'"'.
 6364:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 6365:         }
 6366:         $datatable .= '</tr></table></td></tr>'."\n";
 6367:         $itemcount ++;
 6368:         %defaultchecked = ('ltiauth' => 'off');
 6369:         @toggles = ('ltiauth');
 6370:         $current = {
 6371:                        'ltiauth' => $ltiauth,
 6372:                    };
 6373:         ($table,$itemcount) =
 6374:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6375:                                \%choices,$itemcount,undef,undef,'left');
 6376:         $datatable .= $table;
 6377:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6378:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6379:                       $choices{'domexttool'}.
 6380:                       '</span></td>'.
 6381:                       '<td style="text-align: right" class="LC_right_item">'.
 6382:                       '<table><tr>';
 6383:         foreach my $type (@types) {
 6384:             $datatable .= '<td style="text-align: left">'.
 6385:                           '<span class="LC_nobreak">'.
 6386:                           '<input type="checkbox" name="domexttool"'.
 6387:                           ' value="'.$type.'"'.$domexttool{$type}.' />'.
 6388:                           &mt($type).'</span></td>'."\n";
 6389:         }
 6390:         $datatable .= '</tr></table></td></tr>'."\n";
 6391:         $itemcount ++;
 6392:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6393:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6394:                       $choices{'exttool'}.
 6395:                       '</span></td>'.
 6396:                       '<td style="text-align: right" class="LC_right_item">'.
 6397:                       '<table><tr>';
 6398:         foreach my $type (@types) {
 6399:             $datatable .= '<td style="text-align: left">'.
 6400:                           '<span class="LC_nobreak">'.
 6401:                           '<input type="checkbox" name="exttool"'.
 6402:                           ' value="'.$type.'"'.$exttool{$type}.' />'.
 6403:                           &mt($type).'</span></td>'."\n";
 6404:         }
 6405:         $datatable .= '</tr></table></td></tr>'."\n";
 6406:     }
 6407:     $$rowtotal += $itemcount;
 6408:     return $datatable;
 6409: }
 6410: 
 6411: sub print_authordefaults {
 6412:     my ($position,$dom,$settings,$rowtotal) = @_;
 6413:     my ($css_class,$datatable,%checkedon,%checkedoff);
 6414:     my $itemcount = 1;
 6415:     my %titles = &authordefaults_titles();
 6416:     if ($position eq 'top') {
 6417:         my %defaultchecked = (
 6418:                             'nocodemirror' => 'off',
 6419:                             'domcoordacc'  => 'on',
 6420:                           );
 6421:         my @toggles = ('nocodemirror','domcoordacc');
 6422:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6423:                                                      \%titles,$itemcount);
 6424:         my %staticdefaults = (
 6425:                                 'copyright'    => 'default',
 6426:                                 'sourceavail'  => 'closed',
 6427:                              );
 6428:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6429:         my %currrights;
 6430:         foreach my $item ('copyright','sourceavail') {
 6431:             $currrights{$item} = $staticdefaults{$item};
 6432:             if (ref($settings) eq 'HASH') {
 6433:                 if (exists($settings->{$item})) {
 6434:                     $currrights{$item} = $settings->{$item};
 6435:                 }
 6436:             }
 6437:         }
 6438:         $datatable .= '<tr'.$css_class.'><td style="vertical-align: top">'.
 6439:                       '<span class="LC_nobreak">'.$titles{'copyright'}.
 6440:                       '</span></td><td class="LC_right_item">'.
 6441:                       &selectbox('copyright',$currrights{'copyright'},'',
 6442:                                  \&Apache::loncommon::copyrightdescription,
 6443:                                  (grep !/^priv|custom$/,(&Apache::loncommon::copyrightids))).
 6444:                       '</td></tr>'."\n";
 6445:         $itemcount ++;
 6446:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6447:         $datatable .= '<tr'.$css_class.'><td style="vertical-align: top">'.
 6448:                       '<span class="LC_nobreak">'.$titles{'sourceavail'}.
 6449:                       '</span></td><td class="LC_right_item">'.
 6450:                       &selectbox('sourceavail',$currrights{'sourceavail'},'',
 6451:                                  \&Apache::loncommon::source_copyrightdescription,
 6452:                                  (&Apache::loncommon::source_copyrightids)).
 6453:                       '</td></tr>'."\n";
 6454:     } else {
 6455:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6456:         my $curreditors;
 6457:         my %staticdefaults = (
 6458:                                 editors => ['edit','xml'],
 6459:                                 authorquota => 500,
 6460:                                 webdav => 0,
 6461:                              );
 6462:         my $curreditors = $staticdefaults{'editors'};
 6463:         if ((ref($settings) eq 'HASH') &&
 6464:             (ref($settings->{'editors'}) eq 'ARRAY')) {
 6465:             $curreditors = $settings->{'editors'};
 6466:         } else {
 6467:             $curreditors = $staticdefaults{'editors'};
 6468:         }
 6469:         my @editors = ('edit','xml','daxe');
 6470:         $datatable = '<tr'.$css_class.'>'."\n".
 6471:                      '<td>'.$titles{'editors'}.'</td>'."\n".
 6472:                      '<td class="LC_left_item">'."\n".
 6473:                      '<span class="LC_nobreak">';
 6474:         foreach my $editor (@editors) {
 6475:             my $checked;
 6476:             if (grep(/^\Q$editor\E$/,@{$curreditors})) {
 6477:                 $checked = ' checked="checked"';
 6478:             }
 6479:             $datatable .= '<label>'.
 6480:                           '<input type="checkbox" name="author_editors" '.
 6481:                           $checked.' value="'.$editor.'" '.
 6482:                           'onclick="javascript:checkEditors(this.form,'."'author_editors'".',this);" />'.
 6483:                           $titles{$editor}.'</label>&nbsp;';
 6484:         }
 6485:         $datatable .= '</span>'."\n".'</td>'."\n".'</tr>'."\n";
 6486:         $itemcount ++;
 6487:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6488:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6489:         my @insttypes;
 6490:         if (ref($types) eq 'ARRAY') {
 6491:             @insttypes = @{$types};
 6492:         }
 6493:         my $typecount = 0;
 6494:         my %domconf = &Apache::lonnet::get_dom('configuration',['quotas'],$dom);
 6495:         my @items = ('webdav','authorquota');
 6496:         my %quotas;
 6497:         if (ref($domconf{'quotas'}) eq 'HASH') {
 6498:             %quotas = %{$domconf{'quotas'}};
 6499:             foreach my $item (@items) {
 6500:                 if (ref($quotas{$item}) eq 'HASH') {
 6501:                     foreach my $type (@insttypes,'default') {
 6502:                         if ($item eq 'authorquota') {
 6503:                             if ($quotas{$item}{$type} !~ /^\d+$/) {
 6504:                                 $quotas{$item}{$type} = $staticdefaults{$item};
 6505:                             }
 6506:                         } elsif ($item eq 'webdav') {
 6507:                             if ($quotas{$item}{$type} !~ /^(0|1)$/) {
 6508:                                 $quotas{$item}{$type} = $staticdefaults{$item};
 6509:                             }
 6510:                         }
 6511:                     }
 6512:                 } else {
 6513:                     foreach my $type (@insttypes,'default') {
 6514:                         $quotas{$item}{$type} = $staticdefaults{$item};
 6515:                     }
 6516:                 }
 6517:             }
 6518:         } else {
 6519:             foreach my $item (@items) {
 6520:                 foreach my $type (@insttypes,'default') {
 6521:                     $quotas{$item}{$type} = $staticdefaults{$item};
 6522:                 }
 6523:             }
 6524:         }
 6525:         if (ref($usertypes) eq 'HASH') {
 6526:             my $numinrow = 4;
 6527:             my $onclick = '';
 6528:             $datatable .= &insttypes_row(\%quotas,$types,$usertypes,$dom,
 6529:                                          $numinrow,$othertitle,'authorquota',
 6530:                                          \$itemcount,$onclick);
 6531:             $itemcount ++;
 6532:             $datatable .= &insttypes_row(\%quotas,$types,$usertypes,$dom,
 6533:                                          $numinrow,$othertitle,'webdav',
 6534:                                          \$itemcount);
 6535:             $itemcount ++;
 6536:         }
 6537:         my $checkedno = ' checked="checked"';
 6538:         my ($checkedon,$checkedoff);
 6539:         if (ref($quotas{'webdav'}) eq 'HASH') {
 6540:             if ($quotas{'webdav'}{'_LC_adv'} =~ /^0|1$/) {
 6541:                 if ($quotas{'webdav'}{'_LC_adv'}) {
 6542:                     $checkedon = $checkedno;
 6543:                 } else {
 6544:                     $checkedoff = $checkedno;
 6545:                 }
 6546:                 undef($checkedno);
 6547:             }
 6548:         }
 6549:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6550:         $datatable .= '<tr'.$css_class.'>'.
 6551:                       '<td>'.$titles{'webdav_LC_adv'}.'<br />'.
 6552:                              $titles{'webdav_LC_adv_over'}.
 6553:                       '</td>'.
 6554:                       '<td class="LC_left_item">';
 6555:         foreach my $option ('none','off','on') {
 6556:             my ($text,$val,$checked);
 6557:             if ($option eq 'none') {
 6558:                 $text = $titles{'none'};
 6559:                 $val = '';
 6560:                 $checked = $checkedno;
 6561:             } elsif ($option eq 'off') {
 6562:                 $text = $titles{'overoff'};
 6563:                 $val = 0;
 6564:                 $checked = $checkedoff;
 6565:             } elsif ($option eq 'on') {
 6566:                 $text = $titles{'overon'};
 6567:                 $val = 1;
 6568:                 $checked = $checkedon;
 6569:             }
 6570:             $datatable .= '<span class="LC_nobreak"><label>'.
 6571:                           '<input type="radio" name="webdav_LC_adv"'.
 6572:                           ' value="'.$val.'"'.$checked.' />'.
 6573:                           $text.'</label></span>&nbsp; ';
 6574:         }
 6575:         $datatable .= '</td></tr>';
 6576:         $itemcount ++;
 6577:     }
 6578:     $$rowtotal += $itemcount;
 6579:     return $datatable;
 6580: }
 6581: 
 6582: sub authordefaults_titles {
 6583:     return &Apache::lonlocal::texthash(
 6584:                copyright => 'Copyright/Distribution',
 6585:                sourceavail => ' Source Available',
 6586:                editors => 'Available Editors',
 6587:                webdav => 'WebDAV',
 6588:                authorquota => 'Authoring Space quotas (MB)',
 6589:                nocodemirror => 'Deactivate CodeMirror for EditXML editor',
 6590:                domcoordacc => 'Dom. Coords. can enter Authoring Spaces in domain',
 6591:                edit  => 'Standard editor (Edit)',
 6592:                xml   => 'Text editor (EditXML)',
 6593:                daxe  => 'Daxe editor (Daxe)',
 6594:                webdav_LC_adv => 'WebDAV access for LON-CAPA "advanced" users',
 6595:                webdav_LC_adv_over => '(overrides access based on affiliation, if set)',
 6596:                none => 'No override set',
 6597:                overon => 'Override -- webDAV on',
 6598:                overoff => 'Override -- webDAV off',
 6599:     );
 6600: }
 6601: 
 6602: sub selectbox {
 6603:     my ($name,$value,$readonly,$functionref,@idlist)=@_;
 6604:     my $selout = '<select name="'.$name.'">';
 6605:     foreach my $id (@idlist) {
 6606:         $selout.='<option value="'.$id.'"';
 6607:         if ($id eq $value) {
 6608:             $selout.=' selected="selected"';
 6609:         }
 6610:         if ($readonly) {
 6611:             $selout .= ' disabled="disabled"';
 6612:         }
 6613:         $selout.='>'.&{$functionref}($id).'</option>';
 6614:     }
 6615:     $selout.='</select>';
 6616:     return $selout;
 6617: }
 6618: 
 6619: sub print_selfenrollment {
 6620:     my ($position,$dom,$settings,$rowtotal) = @_;
 6621:     my ($css_class,$datatable);
 6622:     my $itemcount = 1;
 6623:     my @types = ('official','unofficial','community','textbook');
 6624:     if (($position eq 'top') || ($position eq 'middle')) {
 6625:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 6626:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 6627:         my @rows;
 6628:         my $key;
 6629:         if ($position eq 'top') {
 6630:             $key = 'admin'; 
 6631:             if (ref($rowsref) eq 'ARRAY') {
 6632:                 @rows = @{$rowsref};
 6633:             }
 6634:         } elsif ($position eq 'middle') {
 6635:             $key = 'default';
 6636:             @rows = ('types','registered','approval','limit');
 6637:         }
 6638:         foreach my $row (@rows) {
 6639:             if (defined($titlesref->{$row})) {
 6640:                 $itemcount ++;
 6641:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6642:                 $datatable .= '<tr'.$css_class.'>'.
 6643:                               '<td>'.$titlesref->{$row}.'</td>'.
 6644:                               '<td class="LC_left_item">'.
 6645:                               '<table><tr>';
 6646:                 my (%current,%currentcap);
 6647:                 if (ref($settings) eq 'HASH') {
 6648:                     if (ref($settings->{$key}) eq 'HASH') {
 6649:                         foreach my $type (@types) {
 6650:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 6651:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 6652:                             }
 6653:                             if (($row eq 'limit') && ($key eq 'default')) {
 6654:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 6655:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 6656:                                 }
 6657:                             }
 6658:                         }
 6659:                     }
 6660:                 }
 6661:                 my %roles = (
 6662:                              '0' => &Apache::lonnet::plaintext('dc'),
 6663:                             ); 
 6664:             
 6665:                 foreach my $type (@types) {
 6666:                     unless (($row eq 'registered') && ($key eq 'default')) {
 6667:                         $datatable .= '<th>'.&mt($type).'</th>';
 6668:                     }
 6669:                 }
 6670:                 unless (($row eq 'registered') && ($key eq 'default')) {
 6671:                     $datatable .= '</tr><tr>';
 6672:                 }
 6673:                 foreach my $type (@types) {
 6674:                     if ($type eq 'community') {
 6675:                         $roles{'1'} = &mt('Community personnel');
 6676:                     } else {
 6677:                         $roles{'1'} = &mt('Course personnel');
 6678:                     }
 6679:                     $datatable .= '<td style="vertical-align: top">';
 6680:                     if ($position eq 'top') {
 6681:                         my %checked;
 6682:                         if ($current{$type} eq '0') {
 6683:                             $checked{'0'} = ' checked="checked"';
 6684:                         } else {
 6685:                             $checked{'1'} = ' checked="checked"';
 6686:                         }
 6687:                         foreach my $role ('1','0') {
 6688:                             $datatable .= '<span class="LC_nobreak"><label>'.
 6689:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 6690:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 6691:                                           $roles{$role}.'</label></span> ';
 6692:                         }
 6693:                     } else {
 6694:                         if ($row eq 'types') {
 6695:                             my %checked;
 6696:                             if ($current{$type} =~ /^(all|dom)$/) {
 6697:                                 $checked{$1} = ' checked="checked"';
 6698:                             } else {
 6699:                                 $checked{''} = ' checked="checked"';
 6700:                             }
 6701:                             foreach my $val ('','dom','all') {
 6702:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6703:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6704:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6705:                             }
 6706:                         } elsif ($row eq 'registered') {
 6707:                             my %checked;
 6708:                             if ($current{$type} eq '1') {
 6709:                                 $checked{'1'} = ' checked="checked"';
 6710:                             } else {
 6711:                                 $checked{'0'} = ' checked="checked"';
 6712:                             }
 6713:                             foreach my $val ('0','1') {
 6714:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6715:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6716:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6717:                             }
 6718:                         } elsif ($row eq 'approval') {
 6719:                             my %checked;
 6720:                             if ($current{$type} =~ /^([12])$/) {
 6721:                                 $checked{$1} = ' checked="checked"';
 6722:                             } else {
 6723:                                 $checked{'0'} = ' checked="checked"';
 6724:                             }
 6725:                             for my $val (0..2) {
 6726:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6727:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6728:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6729:                             }
 6730:                         } elsif ($row eq 'limit') {
 6731:                             my %checked;
 6732:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 6733:                                 $checked{$1} = ' checked="checked"';
 6734:                             } else {
 6735:                                 $checked{'none'} = ' checked="checked"';
 6736:                             }
 6737:                             my $cap;
 6738:                             if ($currentcap{$type} =~ /^\d+$/) {
 6739:                                 $cap = $currentcap{$type};
 6740:                             }
 6741:                             foreach my $val ('none','allstudents','selfenrolled') {
 6742:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6743:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6744:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6745:                             }
 6746:                             $datatable .= '<br />'.
 6747:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 6748:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 6749:                                           '</span>'; 
 6750:                         }
 6751:                     }
 6752:                     $datatable .= '</td>';
 6753:                 }
 6754:                 $datatable .= '</tr>';
 6755:             }
 6756:             $datatable .= '</table></td></tr>';
 6757:         }
 6758:     } elsif ($position eq 'bottom') {
 6759:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 6760:     }
 6761:     $$rowtotal += $itemcount;
 6762:     return $datatable;
 6763: }
 6764: 
 6765: sub print_validation_rows {
 6766:     my ($caller,$dom,$settings,$rowtotal) = @_;
 6767:     my ($itemsref,$namesref,$fieldsref);
 6768:     if ($caller eq 'selfenroll') { 
 6769:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 6770:     } elsif ($caller eq 'requestcourses') {
 6771:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6772:     }
 6773:     my %currvalidation;
 6774:     if (ref($settings) eq 'HASH') {
 6775:         if (ref($settings->{'validation'}) eq 'HASH') {
 6776:             %currvalidation = %{$settings->{'validation'}};
 6777:         }
 6778:     }
 6779:     my $datatable;
 6780:     my $itemcount = 0;
 6781:     foreach my $item (@{$itemsref}) {
 6782:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6783:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6784:                       $namesref->{$item}.
 6785:                       '</span></td>'.
 6786:                       '<td class="LC_left_item">';
 6787:         if (($item eq 'url') || ($item eq 'button')) {
 6788:             $datatable .= '<span class="LC_nobreak">'.
 6789:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 6790:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 6791:         } elsif ($item eq 'fields') {
 6792:             my @currfields;
 6793:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 6794:                 @currfields = @{$currvalidation{$item}};
 6795:             }
 6796:             foreach my $field (@{$fieldsref}) {
 6797:                 my $check = '';
 6798:                 if (grep(/^\Q$field\E$/,@currfields)) {
 6799:                     $check = ' checked="checked"';
 6800:                 }
 6801:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6802:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 6803:                               ' value="'.$field.'"'.$check.' />'.$field.
 6804:                               '</label></span> ';
 6805:             }
 6806:         } elsif ($item eq 'markup') {
 6807:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 6808:                            $currvalidation{$item}.
 6809:                               '</textarea>';
 6810:         }
 6811:         $datatable .= '</td></tr>'."\n";
 6812:         if (ref($rowtotal)) {
 6813:             $itemcount ++;
 6814:         }
 6815:     }
 6816:     if ($caller eq 'requestcourses') {
 6817:         my %currhash;
 6818:         if (ref($settings) eq 'HASH') {
 6819:             if (ref($settings->{'validation'}) eq 'HASH') {
 6820:                 if ($settings->{'validation'}{'dc'} ne '') {
 6821:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 6822:                 }
 6823:             }
 6824:         }
 6825:         my $numinrow = 2;
 6826:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 6827:                                                        'validationdc',%currhash);
 6828:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6829:         $datatable .= '<tr'.$css_class.'><td>';
 6830:         if ($numdc > 1) {
 6831:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 6832:         } else {
 6833:             $datatable .=  &mt('Course creation processed as: ');
 6834:         }
 6835:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 6836:         $itemcount ++;
 6837:     }
 6838:     if (ref($rowtotal)) {
 6839:         $$rowtotal += $itemcount;
 6840:     }
 6841:     return $datatable;
 6842: }
 6843: 
 6844: sub print_passwords {
 6845:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 6846:     my ($datatable,$css_class);
 6847:     my $itemcount = 0;
 6848:     my %titles = &Apache::lonlocal::texthash (
 6849:         captcha        => '"Forgot Password" CAPTCHA validation',
 6850:         link           => 'Reset link expiration (hours)',
 6851:         case           => 'Case-sensitive usernames/e-mail',
 6852:         prelink        => 'Information required (form 1)',
 6853:         postlink       => 'Information required (form 2)',
 6854:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 6855:         customtext     => 'Domain specific text (HTML)',
 6856:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 6857:         intauth_check  => 'Check bcrypt cost if authenticated',
 6858:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 6859:         permanent      => 'Permanent e-mail address',
 6860:         critical       => 'Critical notification address',
 6861:         notify         => 'Notification address',
 6862:         min            => 'Minimum password length',
 6863:         max            => 'Maximum password length',
 6864:         chars          => 'Required characters',
 6865:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 6866:     );
 6867:     if ($position eq 'top') {
 6868:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6869:         my $shownlinklife = 2;
 6870:         my $prelink = 'both';
 6871:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 6872:         if (ref($settings) eq 'HASH') {
 6873:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 6874:                 $shownlinklife = $settings->{resetlink};
 6875:             }
 6876:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 6877:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 6878:             }
 6879:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 6880:                 $prelink = $settings->{resetprelink};
 6881:             }
 6882:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 6883:                 %postlink = %{$settings->{resetpostlink}};
 6884:             }
 6885:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 6886:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 6887:             }
 6888:             if ($settings->{resetremove}) {
 6889:                 $nostdtext = 1;
 6890:             }
 6891:             if ($settings->{resetcustom}) {
 6892:                 $customurl = $settings->{resetcustom};
 6893:             }
 6894:         } else {
 6895:             if (ref($types) eq 'ARRAY') {
 6896:                 foreach my $item (@{$types}) {
 6897:                     $casesens{$item} = 1;
 6898:                     $postlink{$item} = ['username','email'];
 6899:                 }
 6900:             }
 6901:             $casesens{'default'} = 1;
 6902:             $postlink{'default'} = ['username','email'];
 6903:             $prelink = 'both';
 6904:             %emailsrc = (
 6905:                           permanent => 1,
 6906:                           critical  => 1,
 6907:                           notify    => 1,
 6908:             );
 6909:         }
 6910:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 6911:         $itemcount ++;
 6912:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6913:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 6914:                       '<td class="LC_left_item">'.
 6915:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 6916:                       'name="passwords_link" size="3" /></td></tr>';
 6917:         $itemcount ++;
 6918:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6919:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 6920:                       '<td class="LC_left_item">';
 6921:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6922:             foreach my $item (@{$types}) {
 6923:                 my $checkedcase;
 6924:                 if ($casesens{$item}) {
 6925:                     $checkedcase = ' checked="checked"';
 6926:                 }
 6927:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6928:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 6929:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 6930:                               '</span>&nbsp;&nbsp; ';
 6931:             }
 6932:         }
 6933:         my $checkedcase;
 6934:         if ($casesens{'default'}) {
 6935:             $checkedcase = ' checked="checked"';
 6936:         }
 6937:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 6938:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 6939:                       $othertitle.'</label></span></td>';
 6940:         $itemcount ++;
 6941:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6942:         my %checkedpre = (
 6943:                              both => ' checked="checked"',
 6944:                              either => '',
 6945:                          );
 6946:         if ($prelink eq 'either') {
 6947:             $checkedpre{either} = ' checked="checked"';
 6948:             $checkedpre{both} = '';
 6949:         }
 6950:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 6951:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6952:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 6953:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 6954:                       '<span class="LC_nobreak"><label>'.
 6955:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 6956:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 6957:         $itemcount ++;
 6958:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6959:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 6960:                       '<td class="LC_left_item">';
 6961:         my %postlinked;
 6962:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6963:             foreach my $item (@{$types}) {
 6964:                 undef(%postlinked);
 6965:                 $datatable .= '<fieldset style="display: inline-block;">'.
 6966:                               '<legend>'.$usertypes->{$item}.'</legend>';
 6967:                 if (ref($postlink{$item}) eq 'ARRAY') {
 6968:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 6969:                 }
 6970:                 foreach my $field ('email','username') {
 6971:                     my $checked;
 6972:                     if ($postlinked{$field}) {
 6973:                         $checked = ' checked="checked"';
 6974:                     }
 6975:                     $datatable .= '<span class="LC_nobreak"><label>'.
 6976:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 6977:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 6978:                                   '<span>&nbsp;&nbsp; ';
 6979:                 }
 6980:                 $datatable .= '</fieldset>';
 6981:             }
 6982:         }
 6983:         if (ref($postlink{'default'}) eq 'ARRAY') {
 6984:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 6985:         }
 6986:         $datatable .= '<fieldset style="display: inline-block;">'.
 6987:                       '<legend>'.$othertitle.'</legend>';
 6988:         foreach my $field ('email','username') {
 6989:             my $checked;
 6990:             if ($postlinked{$field}) {
 6991:                 $checked = ' checked="checked"';
 6992:             }
 6993:             $datatable .= '<span class="LC_nobreak"><label>'.
 6994:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 6995:                           $field.'"'.$checked.' />'.$field.'</label>'.
 6996:                           '<span>&nbsp;&nbsp; ';
 6997:         }
 6998:         $datatable .= '</fieldset></td></tr>';
 6999:         $itemcount ++;
 7000:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7001:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 7002:                       '<td class="LC_left_item">';
 7003:         foreach my $type ('permanent','critical','notify') {
 7004:             my $checkedemail;
 7005:             if ($emailsrc{$type}) {
 7006:                 $checkedemail = ' checked="checked"';
 7007:             }
 7008:             $datatable .= '<span class="LC_nobreak"><label>'.
 7009:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 7010:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 7011:                           '<span>&nbsp;&nbsp; ';
 7012:         }
 7013:         $datatable .= '</td></tr>';
 7014:         $itemcount ++;
 7015:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7016:         my $switchserver = &check_switchserver($dom,$confname);
 7017:         my ($showstd,$noshowstd);
 7018:         if ($nostdtext) {
 7019:             $noshowstd = ' checked="checked"';
 7020:         } else {
 7021:             $showstd = ' checked="checked"';
 7022:         }
 7023:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 7024:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7025:                       &mt('Retain standard text:').
 7026:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 7027:                       &mt('Yes').'</label>'.'&nbsp;'.
 7028:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 7029:                       &mt('No').'</label></span><br />'.
 7030:                       '<span class="LC_fontsize_small">'.
 7031:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 7032:                       &mt('Include custom text:');
 7033:         if ($customurl) {
 7034:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 7035:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7036:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 7037:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 7038:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 7039:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 7040:         }
 7041:         if ($switchserver) {
 7042:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 7043:         } else {
 7044:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 7045:                          '<input type="file" name="passwords_customfile" /></span>';
 7046:         }
 7047:         $datatable .= '</td></tr>';
 7048:     } elsif ($position eq 'middle') {
 7049:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 7050:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 7051:         my %defaults;
 7052:         if (ref($domconf{'defaults'}) eq 'HASH') {
 7053:             %defaults = %{$domconf{'defaults'}};
 7054:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 7055:                 $defaults{'intauth_cost'} = 10;
 7056:             }
 7057:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 7058:                 $defaults{'intauth_check'} = 0;
 7059:             }
 7060:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 7061:                 $defaults{'intauth_switch'} = 0;
 7062:             }
 7063:         } else {
 7064:             %defaults = (
 7065:                           'intauth_cost'   => 10,
 7066:                           'intauth_check'  => 0,
 7067:                           'intauth_switch' => 0,
 7068:                         );
 7069:         }
 7070:         foreach my $item (@items) {
 7071:             if ($itemcount%2) {
 7072:                 $css_class = '';
 7073:             } else {
 7074:                 $css_class = ' class="LC_odd_row" ';
 7075:             }
 7076:             $datatable .= '<tr'.$css_class.'>'.
 7077:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 7078:                           '</span></td><td class="LC_left_item" colspan="3">';
 7079:             if ($item eq 'intauth_switch') {
 7080:                 my @options = (0,1,2);
 7081:                 my %optiondesc = &Apache::lonlocal::texthash (
 7082:                                    0 => 'No',
 7083:                                    1 => 'Yes',
 7084:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 7085:                                  );
 7086:                 $datatable .= '<table width="100%">';
 7087:                 foreach my $option (@options) {
 7088:                     my $checked = ' ';
 7089:                     if ($defaults{$item} eq $option) {
 7090:                         $checked = ' checked="checked"';
 7091:                     }
 7092:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7093:                                   '<label><input type="radio" name="'.$item.
 7094:                                   '" value="'.$option.'"'.$checked.' />'.
 7095:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7096:                 }
 7097:                 $datatable .= '</table>';
 7098:             } elsif ($item eq 'intauth_check') {
 7099:                 my @options = (0,1,2);
 7100:                 my %optiondesc = &Apache::lonlocal::texthash (
 7101:                                    0 => 'No',
 7102:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 7103:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 7104:                                  );
 7105:                 $datatable .= '<table width="100%">';
 7106:                 foreach my $option (@options) {
 7107:                     my $checked = ' ';
 7108:                     my $onclick;
 7109:                     if ($defaults{$item} eq $option) {
 7110:                         $checked = ' checked="checked"';
 7111:                     }
 7112:                     if ($option == 2) {
 7113:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 7114:                     }
 7115:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7116:                                   '<label><input type="radio" name="'.$item.
 7117:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 7118:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7119:                 }
 7120:                 $datatable .= '</table>';
 7121:             } else {
 7122:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7123:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 7124:             }
 7125:             $datatable .= '</td></tr>';
 7126:             $itemcount ++;
 7127:         }
 7128:     } elsif ($position eq 'lower') {
 7129:         $datatable .= &password_rules('passwords',\$itemcount,$settings);
 7130:     } else {
 7131:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7132:         my %ownerchg = (
 7133:                           by  => {},
 7134:                           for => {},
 7135:                        );
 7136:         my %ownertitles = &Apache::lonlocal::texthash (
 7137:                             by  => 'Course owner status(es) allowed',
 7138:                             for => 'Student status(es) allowed',
 7139:                           );
 7140:         if (ref($settings) eq 'HASH') {
 7141:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 7142:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 7143:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 7144:                 }
 7145:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 7146:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 7147:                 }
 7148:             }
 7149:         }
 7150:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7151:         $datatable .= '<tr '.$css_class.'>'.
 7152:                       '<td>'.
 7153:                       &mt('Requirements').'<ul>'.
 7154:                       '<li>'.&mt("Course 'type' is not a Community").'</li>'.
 7155:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 7156:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 7157:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 7158:                       '</ul>'.
 7159:                       '</td>'.
 7160:                       '<td class="LC_left_item">';
 7161:         foreach my $item ('by','for') {
 7162:             $datatable .= '<fieldset style="display: inline-block;">'.
 7163:                           '<legend>'.$ownertitles{$item}.'</legend>';
 7164:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7165:                 foreach my $type (@{$types}) {
 7166:                     my $checked;
 7167:                     if ($ownerchg{$item}{$type}) {
 7168:                         $checked = ' checked="checked"';
 7169:                     }
 7170:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7171:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 7172:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 7173:                                   '</span>&nbsp;&nbsp; ';
 7174:                 }
 7175:             }
 7176:             my $checked;
 7177:             if ($ownerchg{$item}{'default'}) {
 7178:                 $checked = ' checked="checked"';
 7179:             }
 7180:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7181:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 7182:                           $othertitle.'</label></span></fieldset>';
 7183:         }
 7184:         $datatable .= '</td></tr>';
 7185:     }
 7186:     return $datatable;
 7187: }
 7188: 
 7189: sub password_rules {
 7190:     my ($prefix,$itemcountref,$settings) = @_;
 7191:     my ($min,$max,%chars,$numsaved,$numinrow);
 7192:     my %titles;
 7193:     if ($prefix eq 'passwords') {
 7194:         %titles = &Apache::lonlocal::texthash (
 7195:             min            => 'Minimum password length',
 7196:             max            => 'Maximum password length',
 7197:             chars          => 'Required characters',
 7198:         );
 7199:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
 7200:         %titles = &Apache::lonlocal::texthash (
 7201:             min            => 'Minimum secret length',
 7202:             max            => 'Maximum secret length',
 7203:             chars          => 'Required characters',
 7204:         );
 7205:     }
 7206:     $min = $Apache::lonnet::passwdmin;
 7207:     my $datatable;
 7208:     my $itemcount;
 7209:     if (ref($itemcountref)) {
 7210:         $itemcount = $$itemcountref;
 7211:     }
 7212:     if (ref($settings) eq 'HASH') {
 7213:         if ($settings->{min}) {
 7214:             $min = $settings->{min};
 7215:         }
 7216:         if ($settings->{max}) {
 7217:             $max = $settings->{max};
 7218:         }
 7219:         if (ref($settings->{chars}) eq 'ARRAY') {
 7220:             map { $chars{$_} = 1; } (@{$settings->{chars}});
 7221:         }
 7222:         if ($prefix eq 'passwords') {
 7223:             if ($settings->{numsaved}) {
 7224:                 $numsaved = $settings->{numsaved};
 7225:             }
 7226:         }
 7227:     }
 7228:     my %rulenames = &Apache::lonlocal::texthash(
 7229:                                                  uc => 'At least one upper case letter',
 7230:                                                  lc => 'At least one lower case letter',
 7231:                                                  num => 'At least one number',
 7232:                                                  spec => 'At least one non-alphanumeric',
 7233:                                                );
 7234:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7235:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 7236:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 7237:                   '<input type="text" name="'.$prefix.'_min" value="'.$min.'" size="3" '.
 7238:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 7239:                   '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 7240:                   '</span></td></tr>';
 7241:     $itemcount ++;
 7242:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7243:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 7244:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 7245:                   '<input type="text" name="'.$prefix.'_max" value="'.$max.'" size="3" '.
 7246:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 7247:                   '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 7248:                   '</span></td></tr>';
 7249:     $itemcount ++;
 7250:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7251:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 7252:                   '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 7253:                   '</span></td>';
 7254:     my $numinrow = 2;
 7255:     my @possrules = ('uc','lc','num','spec');
 7256:     $datatable .= '<td class="LC_left_item"><table>';
 7257:     for (my $i=0; $i<@possrules; $i++) {
 7258:         my ($rem,$checked);
 7259:         if ($chars{$possrules[$i]}) {
 7260:             $checked = ' checked="checked"';
 7261:         }
 7262:         $rem = $i%($numinrow);
 7263:         if ($rem == 0) {
 7264:             if ($i > 0) {
 7265:                 $datatable .= '</tr>';
 7266:             }
 7267:             $datatable .= '<tr>';
 7268:         }
 7269:         $datatable .= '<td><span class="LC_nobreak"><label>'.
 7270:                       '<input type="checkbox" name="'.$prefix.'_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 7271:                       $rulenames{$possrules[$i]}.'</label></span></td>';
 7272:     }
 7273:     my $rem = @possrules%($numinrow);
 7274:     my $colsleft = $numinrow - $rem;
 7275:     if ($colsleft > 1 ) {
 7276:         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7277:                       '&nbsp;</td>';
 7278:     } elsif ($colsleft == 1) {
 7279:         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7280:     }
 7281:     $datatable .='</table></td></tr>';
 7282:     $itemcount ++;
 7283:     if ($prefix eq 'passwords') {
 7284:         $titles{'numsaved'} = &mt('Number of previous passwords to save and disallow reuse');
 7285:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7286:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 7287:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7288:                       '<input type="text" name="'.$prefix.'_numsaved" value="'.$numsaved.'" size="3" '.
 7289:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 7290:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 7291:                       '</span></td></tr>';
 7292:         $itemcount ++;
 7293:     }
 7294:     if (ref($itemcountref)) {
 7295:         $$itemcountref += $itemcount;
 7296:     }
 7297:     return $datatable;
 7298: }
 7299: 
 7300: sub print_wafproxy {
 7301:     my ($position,$dom,$settings,$rowtotal) = @_;
 7302:     my $css_class;
 7303:     my $itemcount = 0;
 7304:     my $datatable;
 7305:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7306:     my (%othercontrol,%otherdoms,%aliases,%saml,%values,$setdom,$showdom);
 7307:     my %lt = &wafproxy_titles();
 7308:     foreach my $server (sort(keys(%servers))) {
 7309:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 7310:         next if ($serverhome eq '');
 7311:         my $serverdom;
 7312:         if ($serverhome ne $server) {
 7313:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 7314:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 7315:                 $othercontrol{$server} = $serverdom;
 7316:             }
 7317:         } else {
 7318:             $serverdom = &Apache::lonnet::host_domain($server);
 7319:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 7320:             if ($serverdom ne $dom) {
 7321:                 $othercontrol{$server} = $serverdom;
 7322:             } else {
 7323:                 $setdom = 1;
 7324:                 if (ref($settings) eq 'HASH') {
 7325:                     if (ref($settings->{'alias'}) eq 'HASH') {
 7326:                         $aliases{$dom} = $settings->{'alias'};
 7327:                         if ($aliases{$dom} ne '') {
 7328:                             $showdom = 1;
 7329:                         }
 7330:                     }
 7331:                     if (ref($settings->{'saml'}) eq 'HASH') {
 7332:                         $saml{$dom} = $settings->{'saml'};
 7333:                     }
 7334:                 }
 7335:             }
 7336:         }
 7337:     }
 7338:     if ($setdom) {
 7339:         %{$values{$dom}} = ();
 7340:         if (ref($settings) eq 'HASH') {
 7341:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 7342:                 $values{$dom}{$item} = $settings->{$item};
 7343:             }
 7344:         }
 7345:     }
 7346:     if (keys(%othercontrol)) {
 7347:         %otherdoms = reverse(%othercontrol);
 7348:         foreach my $domain (keys(%otherdoms)) {
 7349:             %{$values{$domain}} = ();
 7350:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 7351:             if (ref($config{'wafproxy'}) eq 'HASH') {
 7352:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 7353:                 if (exists($config{'wafproxy'}{'saml'})) {
 7354:                     $saml{$domain} = $config{'wafproxy'}{'saml'};
 7355:                 }
 7356:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 7357:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 7358:                 }
 7359:             }
 7360:         }
 7361:     }
 7362:     if ($position eq 'top') {
 7363:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7364:         my %aliasinfo;
 7365:         foreach my $server (sort(keys(%servers))) {
 7366:             $itemcount ++;
 7367:             my $dom_in_effect;
 7368:             my $aliasrows = '<tr>'.
 7369:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7370:                             &mt('Hostname').': '.
 7371:                             '<span class="LC_nobreak LC_cusr_emph">'.
 7372:                             &Apache::lonnet::hostname($server).'</span></td><td>&nbsp;</td>';
 7373:             if ($othercontrol{$server}) {
 7374:                 $dom_in_effect = $othercontrol{$server};
 7375:                 my ($current,$forsaml);
 7376:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 7377:                     $current = $aliases{$dom_in_effect}{$server};
 7378:                 }
 7379:                 if (ref($saml{$dom_in_effect}) eq 'HASH') {
 7380:                     if ($saml{$dom_in_effect}{$server}) {
 7381:                         $forsaml = 1;
 7382:                     }
 7383:                 }
 7384:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7385:                               &mt('Alias').':&nbsp';
 7386:                 if ($current) {
 7387:                     $aliasrows .= $current;
 7388:                     if ($forsaml) {
 7389:                          $aliasrows .= '&nbsp;('.&mt('also for SSO Auth').')';
 7390:                     }
 7391:                 } else {
 7392:                     $aliasrows .= &mt('None');
 7393:                 }
 7394:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 7395:                               &mt('controlled by domain: [_1]',
 7396:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 7397:             } else {
 7398:                 $dom_in_effect = $dom;
 7399:                 my ($current,$samlon,$samloff);
 7400:                 $samloff = ' checked="checked"';
 7401:                 if (ref($aliases{$dom}) eq 'HASH') {
 7402:                     if ($aliases{$dom}{$server}) {
 7403:                         $current = $aliases{$dom}{$server};
 7404:                     }
 7405:                 }
 7406:                 if (ref($saml{$dom}) eq 'HASH') {
 7407:                     if ($saml{$dom}{$server}) {
 7408:                         $samlon = $samloff;
 7409:                         undef($samloff);
 7410:                     }
 7411:                 }
 7412:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7413:                               &mt('Alias').':&nbsp;'.
 7414:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 7415:                               'value="'.$current.'" size="30" />'.
 7416:                               ('&nbsp;'x2).'<span class="LC_nobreak">'.
 7417:                               &mt('Alias used for SSO Auth').':&nbsp;<label>'.
 7418:                               '<input type="radio" value="0"'.$samloff.' name="wafproxy_alias_saml_'.$server.'" />'.
 7419:                               &mt('No').'</label>&nbsp;<label>'.
 7420:                               '<input type="radio" value="1"'.$samlon.' name="wafproxy_alias_saml_'.$server.'" />'.
 7421:                               &mt('Yes').'</label></span>'.
 7422:                               '</td>';
 7423:             }
 7424:             $aliasrows .= '</tr>';
 7425:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 7426:         }
 7427:         if ($aliasinfo{$dom}) {
 7428:             my ($onclick,$wafon,$wafoff,$showtable);
 7429:             $onclick = ' onclick="javascript:toggleWAF();"';
 7430:             $wafoff = ' checked="checked"';
 7431:             $showtable = ' style="display:none";';
 7432:             if ($showdom) {
 7433:                 $wafon = $wafoff;
 7434:                 $wafoff = '';
 7435:                 $showtable = ' style="display:inline;"';
 7436:             }
 7437:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7438:             $datatable = '<tr'.$css_class.'>'.
 7439:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 7440:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 7441:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 7442:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 7443:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 7444:                          &mt('No').'</label></span></td>'.
 7445:                          '<td class="LC_left_item">'.
 7446:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 7447:                          '</table></td></tr>';
 7448:             $itemcount++;
 7449:         }
 7450:         if (keys(%otherdoms)) {
 7451:             foreach my $key (sort(keys(%otherdoms))) {
 7452:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7453:                 $datatable .= '<tr'.$css_class.'>'.
 7454:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 7455:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 7456:                               '</table></td></tr>';
 7457:                 $itemcount++;
 7458:             }
 7459:         }
 7460:     } else {
 7461:         my %ip_methods = &remoteip_methods();
 7462:         if ($setdom) {
 7463:             $itemcount ++;
 7464:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7465:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 7466:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 7467:             $wafstyle = ' style="display:none;"';
 7468:             $nowafstyle = ' style="display:table-row;"';
 7469:             $currwafdisplay = ' style="display: none"';
 7470:             $wafrangestyle = ' style="display: none"';
 7471:             $curr_remotip = 'n';
 7472:             $ssltossl = ' checked="checked"';
 7473:             if ($showdom) {
 7474:                 $wafstyle = ' style="display:table-row;"';
 7475:                 $nowafstyle =  ' style="display:none;"';
 7476:                 if (keys(%{$values{$dom}})) {
 7477:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 7478:                         $curr_remotip = $values{$dom}{remoteip};
 7479:                     }
 7480:                     if ($curr_remotip eq 'h') {
 7481:                         $currwafdisplay = ' style="display:table-row"';
 7482:                         $wafrangestyle = ' style="display:inline-block;"';
 7483:                     }
 7484:                     if ($values{$dom}{'sslopt'}) {
 7485:                         $alltossl = ' checked="checked"';
 7486:                         $ssltossl = '';
 7487:                     }
 7488:                 }
 7489:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 7490:                     $vpndircheck = ' checked="checked"';
 7491:                     $currwafvpn = ' style="display:table-row;"';
 7492:                     $wafrangestyle = ' style="display:inline-block;"';
 7493:                 } else {
 7494:                     $vpnaliascheck = ' checked="checked"';
 7495:                     $currwafvpn = ' style="display:none;"';
 7496:                 }
 7497:             }
 7498:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 7499:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 7500:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 7501:                           '</tr>'.
 7502:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 7503:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 7504:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 7505:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 7506:                           &mt('Range(s) stored in CIDR notation').'</div></td>'.
 7507:                           '<td class="LC_left_item"><table>'.
 7508:                           '<tr>'.
 7509:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 7510:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 7511:             foreach my $option ('m','h','n') {
 7512:                 my $sel;
 7513:                 if ($option eq $curr_remotip) {
 7514:                    $sel = ' selected="selected"';
 7515:                 }
 7516:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 7517:                               $ip_methods{$option}.'</option>';
 7518:             }
 7519:             $datatable .= '</select></td></tr>'."\n".
 7520:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 7521:                           $lt{'ipheader'}.':&nbsp;'.
 7522:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 7523:                           'name="wafproxy_ipheader" />'.
 7524:                           '</td></tr>'."\n".
 7525:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 7526:                           $lt{'trusted'}.':<br />'.
 7527:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 7528:                           $values{$dom}{'trusted'}.'</textarea>'.
 7529:                           '</td></tr>'."\n".
 7530:                           '<tr><td><hr /></td></tr>'."\n".
 7531:                           '<tr>'.
 7532:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 7533:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 7534:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 7535:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 7536:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 7537:             foreach my $item ('vpnint','vpnext') {
 7538:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 7539:                               '<td valign="top">'.$lt{$item}.':<br />'.
 7540:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 7541:                               $values{$dom}{$item}.'</textarea>'.
 7542:                               '</td></tr>'."\n";
 7543:             }
 7544:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 7545:                           '<tr>'.
 7546:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 7547:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 7548:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 7549:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 7550:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 7551:                           '</table></td></tr>';
 7552:         }
 7553:         if (keys(%otherdoms)) {
 7554:             foreach my $domain (sort(keys(%otherdoms))) {
 7555:                 $itemcount ++;
 7556:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7557:                 $datatable .= '<tr'.$css_class.'>'.
 7558:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 7559:                               '<td class="LC_left_item"><table>';
 7560:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 7561:                     my $showval = &mt('None');
 7562:                     if ($item eq 'ssl') {
 7563:                         $showval = $lt{'ssltossl'};
 7564:                     }
 7565:                     if ($values{$domain}{$item}) {
 7566:                         $showval = $values{$domain}{$item};
 7567:                         if ($item eq 'ssl') {
 7568:                             $showval = $lt{'alltossl'};
 7569:                         } elsif ($item eq 'remoteip') {
 7570:                             $showval = $ip_methods{$values{$domain}{$item}};
 7571:                         }
 7572:                     }
 7573:                     $datatable .= '<tr>'.
 7574:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 7575:                 }
 7576:                 $datatable .= '</table></td></tr>';
 7577:             }
 7578:         }
 7579:     }
 7580:     $$rowtotal += $itemcount;
 7581:     return $datatable;
 7582: }
 7583: 
 7584: sub wafproxy_titles {
 7585:     return &Apache::lonlocal::texthash(
 7586:                remoteip   => "Method for determining user's IP",
 7587:                ipheader   => 'Request header containing remote IP',
 7588:                trusted    => 'Trusted IP range(s)',
 7589:                vpnaccess  => 'Access from institutional VPN',
 7590:                vpndirect  => 'via regular hostname (no WAF)',
 7591:                vpnaliased => 'via aliased hostname (WAF)',
 7592:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 7593:                vpnext     => 'IP Range(s) for backend WAF connections',
 7594:                sslopt     => 'Forwarding http/https',
 7595:                alltossl   => 'WAF forwards both http and https requests to https',
 7596:                ssltossl   => 'WAF forwards http requests to http and https to https',
 7597:            );
 7598: }
 7599: 
 7600: sub remoteip_methods {
 7601:     return &Apache::lonlocal::texthash(
 7602:               m => 'Use Apache mod_remoteip',
 7603:               h => 'Use headers parsed by LON-CAPA',
 7604:               n => 'Not in use',
 7605:            );
 7606: }
 7607: 
 7608: sub print_usersessions {
 7609:     my ($position,$dom,$settings,$rowtotal) = @_;
 7610:     my ($css_class,$datatable,%checked,%choices);
 7611:     my (%by_ip,%by_location,@intdoms);
 7612:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7613: 
 7614:     my @alldoms = &Apache::lonnet::all_domains();
 7615:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 7616:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7617:     my %altids = &id_for_thisdom(%servers);
 7618:     my $itemcount = 1;
 7619:     if ($position eq 'top') {
 7620:         if (keys(%serverhomes) > 1) {
 7621:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 7622:             my ($curroffloadnow,$curroffloadoth);
 7623:             if (ref($settings) eq 'HASH') {
 7624:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 7625:                     $curroffloadnow = $settings->{'offloadnow'};
 7626:                 }
 7627:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 7628:                     $curroffloadoth = $settings->{'offloadoth'};
 7629:                 }
 7630:             }
 7631:             my $other_insts = scalar(keys(%by_location));
 7632:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 7633:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 7634:         } else {
 7635:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 7636:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 7637:                           '</td></tr>';
 7638:         }
 7639:     } else {
 7640:         if (keys(%by_location) == 0) {
 7641:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 7642:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 7643:                           '</td></tr>';
 7644:         } else {
 7645:             my %lt = &usersession_titles();
 7646:             my $numinrow = 5;
 7647:             my $prefix;
 7648:             my @types;
 7649:             if ($position eq 'bottom') {
 7650:                 $prefix = 'remote';
 7651:                 @types = ('version','excludedomain','includedomain');
 7652:             } else {
 7653:                 $prefix = 'hosted';
 7654:                 @types = ('excludedomain','includedomain');
 7655:             }
 7656:             my (%current,%checkedon,%checkedoff);
 7657:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 7658:             my @locations = sort(keys(%by_location));
 7659:             foreach my $type (@types) {
 7660:                 $checkedon{$type} = '';
 7661:                 $checkedoff{$type} = ' checked="checked"';
 7662:             }
 7663:             if (ref($settings) eq 'HASH') {
 7664:                 if (ref($settings->{$prefix}) eq 'HASH') {
 7665:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 7666:                         $current{$key} = $settings->{$prefix}{$key};
 7667:                         if ($key eq 'version') {
 7668:                             if ($current{$key} ne '') {
 7669:                                 $checkedon{$key} = ' checked="checked"';
 7670:                                 $checkedoff{$key} = '';
 7671:                             }
 7672:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 7673:                             $checkedon{$key} = ' checked="checked"';
 7674:                             $checkedoff{$key} = '';
 7675:                         }
 7676:                     }
 7677:                 }
 7678:             }
 7679:             foreach my $type (@types) {
 7680:                 next if ($type ne 'version' && !@locations);
 7681:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7682:                 $datatable .= '<tr'.$css_class.'>
 7683:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 7684:                                <span class="LC_nobreak">&nbsp;
 7685:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 7686:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 7687:                 if ($type eq 'version') {
 7688:                     my $selector = '<select name="'.$prefix.'_version">';
 7689:                     foreach my $version (@lcversions) {
 7690:                         my $selected = '';
 7691:                         if ($current{'version'} eq $version) {
 7692:                             $selected = ' selected="selected"';
 7693:                         }
 7694:                         $selector .= ' <option value="'.$version.'"'.
 7695:                                      $selected.'>'.$version.'</option>';
 7696:                     }
 7697:                     $selector .= '</select> ';
 7698:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 7699:                 } else {
 7700:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 7701:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 7702:                                  ' />'.('&nbsp;'x2).
 7703:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 7704:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 7705:                                  "\n".
 7706:                                  '</div><div><table>';
 7707:                     my $rem;
 7708:                     for (my $i=0; $i<@locations; $i++) {
 7709:                         my ($showloc,$value,$checkedtype);
 7710:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 7711:                             my $ip = $by_location{$locations[$i]}->[0];
 7712:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 7713:                                  $value = join(':',@{$by_ip{$ip}});
 7714:                                 $showloc = join(', ',@{$by_ip{$ip}});
 7715:                                 if (ref($current{$type}) eq 'ARRAY') {
 7716:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 7717:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 7718:                                             $checkedtype = ' checked="checked"';
 7719:                                             last;
 7720:                                         }
 7721:                                     }
 7722:                                 }
 7723:                             }
 7724:                         }
 7725:                         $rem = $i%($numinrow);
 7726:                         if ($rem == 0) {
 7727:                             if ($i > 0) {
 7728:                                 $datatable .= '</tr>';
 7729:                             }
 7730:                             $datatable .= '<tr>';
 7731:                         }
 7732:                         $datatable .= '<td class="LC_left_item">'.
 7733:                                       '<span class="LC_nobreak"><label>'.
 7734:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 7735:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 7736:                                       '</label></span></td>';
 7737:                     }
 7738:                     $rem = @locations%($numinrow);
 7739:                     my $colsleft = $numinrow - $rem;
 7740:                     if ($colsleft > 1 ) {
 7741:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7742:                                       '&nbsp;</td>';
 7743:                     } elsif ($colsleft == 1) {
 7744:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7745:                     }
 7746:                     $datatable .= '</tr></table>';
 7747:                 }
 7748:                 $datatable .= '</td></tr>';
 7749:                 $itemcount ++;
 7750:             }
 7751:         }
 7752:     }
 7753:     $$rowtotal += $itemcount;
 7754:     return $datatable;
 7755: }
 7756: 
 7757: sub build_location_hashes {
 7758:     my ($intdoms,$by_ip,$by_location) = @_;
 7759:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 7760:                   (ref($by_location) eq 'HASH')); 
 7761:     my %iphost = &Apache::lonnet::get_iphost();
 7762:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 7763:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 7764:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 7765:         foreach my $id (@{$iphost{$primary_ip}}) {
 7766:             my $intdom = &Apache::lonnet::internet_dom($id);
 7767:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 7768:                 push(@{$intdoms},$intdom);
 7769:             }
 7770:         }
 7771:     }
 7772:     foreach my $ip (keys(%iphost)) {
 7773:         if (ref($iphost{$ip}) eq 'ARRAY') {
 7774:             foreach my $id (@{$iphost{$ip}}) {
 7775:                 my $location = &Apache::lonnet::internet_dom($id);
 7776:                 if ($location) {
 7777:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 7778:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 7779:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 7780:                             push(@{$by_ip->{$ip}},$location);
 7781:                         }
 7782:                     } else {
 7783:                         $by_ip->{$ip} = [$location];
 7784:                     }
 7785:                 }
 7786:             }
 7787:         }
 7788:     }
 7789:     foreach my $ip (sort(keys(%{$by_ip}))) {
 7790:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 7791:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 7792:             my $first = $by_ip->{$ip}->[0];
 7793:             if (ref($by_location->{$first}) eq 'ARRAY') {
 7794:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 7795:                     push(@{$by_location->{$first}},$ip);
 7796:                 }
 7797:             } else {
 7798:                 $by_location->{$first} = [$ip];
 7799:             }
 7800:         }
 7801:     }
 7802:     return;
 7803: }
 7804: 
 7805: sub current_offloads_to {
 7806:     my ($dom,$settings,$servers) = @_;
 7807:     my (%spareid,%otherdomconfigs);
 7808:     if (ref($servers) eq 'HASH') {
 7809:         foreach my $lonhost (sort(keys(%{$servers}))) {
 7810:             my $gotspares;
 7811:             if (ref($settings) eq 'HASH') {
 7812:                 if (ref($settings->{'spares'}) eq 'HASH') {
 7813:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 7814:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 7815:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 7816:                         $gotspares = 1;
 7817:                     }
 7818:                 }
 7819:             }
 7820:             unless ($gotspares) {
 7821:                 my $gotspares;
 7822:                 my $serverhomeID =
 7823:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 7824:                 my $serverhomedom =
 7825:                     &Apache::lonnet::host_domain($serverhomeID);
 7826:                 if ($serverhomedom ne $dom) {
 7827:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 7828:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 7829:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 7830:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 7831:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 7832:                                 $gotspares = 1;
 7833:                             }
 7834:                         }
 7835:                     } else {
 7836:                         $otherdomconfigs{$serverhomedom} =
 7837:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 7838:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 7839:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 7840:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 7841:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 7842:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 7843:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 7844:                                         $gotspares = 1;
 7845:                                     }
 7846:                                 }
 7847:                             }
 7848:                         }
 7849:                     }
 7850:                 }
 7851:             }
 7852:             unless ($gotspares) {
 7853:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 7854:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 7855:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 7856:                } else {
 7857:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 7858:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 7859:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 7860:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 7861:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 7862:                     } else {
 7863:                         my %what = (
 7864:                              spareid => 1,
 7865:                         );
 7866:                         my ($result,$returnhash) = 
 7867:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 7868:                         if ($result eq 'ok') { 
 7869:                             if (ref($returnhash) eq 'HASH') {
 7870:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 7871:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 7872:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 7873:                                 }
 7874:                             }
 7875:                         }
 7876:                     }
 7877:                 }
 7878:             }
 7879:         }
 7880:     }
 7881:     return %spareid;
 7882: }
 7883: 
 7884: sub spares_row {
 7885:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 7886:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 7887:     my $css_class;
 7888:     my $numinrow = 4;
 7889:     my $itemcount = 1;
 7890:     my $datatable;
 7891:     my %typetitles = &sparestype_titles();
 7892:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 7893:         foreach my $server (sort(keys(%{$servers}))) {
 7894:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 7895:             my ($othercontrol,$serverdom);
 7896:             if ($serverhome ne $server) {
 7897:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 7898:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 7899:             } else {
 7900:                 $serverdom = &Apache::lonnet::host_domain($server);
 7901:                 if ($serverdom ne $dom) {
 7902:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 7903:                 }
 7904:             }
 7905:             next unless (ref($spareid->{$server}) eq 'HASH');
 7906:             my ($checkednow,$checkedoth);
 7907:             if (ref($curroffloadnow) eq 'HASH') {
 7908:                 if ($curroffloadnow->{$server}) {
 7909:                     $checkednow = ' checked="checked"';
 7910:                 }
 7911:             }
 7912:             if (ref($curroffloadoth) eq 'HASH') {
 7913:                 if ($curroffloadoth->{$server}) {
 7914:                     $checkedoth = ' checked="checked"';
 7915:                 }
 7916:             }
 7917:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7918:             $datatable .= '<tr'.$css_class.'>
 7919:                            <td rowspan="2">
 7920:                             <span class="LC_nobreak">'.
 7921:                           &mt('[_1] when busy, offloads to:'
 7922:                               ,'<b>'.$server.'</b>').'</span><br />'.
 7923:                           '<span class="LC_nobreak">'."\n".
 7924:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 7925:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 7926:                           "\n";
 7927:             if ($other_insts) {
 7928:                 $datatable .= '<br />'.
 7929:                               '<span class="LC_nobreak">'."\n".
 7930:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 7931:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 7932:                           "\n";
 7933:             }
 7934:             my (%current,%canselect);
 7935:             my @choices = 
 7936:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 7937:             foreach my $type ('primary','default') {
 7938:                 if (ref($spareid->{$server}) eq 'HASH') {
 7939:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 7940:                         my @spares = @{$spareid->{$server}{$type}};
 7941:                         if (@spares > 0) {
 7942:                             if ($othercontrol) {
 7943:                                 $current{$type} = join(', ',@spares);
 7944:                             } else {
 7945:                                 $current{$type} .= '<table>';
 7946:                                 my $numspares = scalar(@spares);
 7947:                                 for (my $i=0;  $i<@spares; $i++) {
 7948:                                     my $rem = $i%($numinrow);
 7949:                                     if ($rem == 0) {
 7950:                                         if ($i > 0) {
 7951:                                             $current{$type} .= '</tr>';
 7952:                                         }
 7953:                                         $current{$type} .= '<tr>';
 7954:                                     }
 7955:                                     $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;'.
 7956:                                                        $spareid->{$server}{$type}[$i].
 7957:                                                        '</label></td>'."\n";
 7958:                                 }
 7959:                                 my $rem = @spares%($numinrow);
 7960:                                 my $colsleft = $numinrow - $rem;
 7961:                                 if ($colsleft > 1 ) {
 7962:                                     $current{$type} .= '<td colspan="'.$colsleft.
 7963:                                                        '" class="LC_left_item">'.
 7964:                                                        '&nbsp;</td>';
 7965:                                 } elsif ($colsleft == 1) {
 7966:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 7967:                                 }
 7968:                                 $current{$type} .= '</tr></table>';
 7969:                             }
 7970:                         }
 7971:                     }
 7972:                     if ($current{$type} eq '') {
 7973:                         $current{$type} = &mt('None specified');
 7974:                     }
 7975:                     if ($othercontrol) {
 7976:                         if ($type eq 'primary') {
 7977:                             $canselect{$type} = $othercontrol;
 7978:                         }
 7979:                     } else {
 7980:                         $canselect{$type} = 
 7981:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 7982:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 7983:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 7984:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 7985:                         if (@choices > 0) {
 7986:                             foreach my $lonhost (@choices) {
 7987:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 7988:                             }
 7989:                         }
 7990:                         $canselect{$type} .= '</select>'."\n";
 7991:                     }
 7992:                 } else {
 7993:                     $current{$type} = &mt('Could not be determined');
 7994:                     if ($type eq 'primary') {
 7995:                         $canselect{$type} =  $othercontrol;
 7996:                     }
 7997:                 }
 7998:                 if ($type eq 'default') {
 7999:                     $datatable .= '<tr'.$css_class.'>';
 8000:                 }
 8001:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 8002:                               '<td>'.$current{$type}.'</td>'."\n".
 8003:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 8004:             }
 8005:             $itemcount ++;
 8006:         }
 8007:     }
 8008:     $$rowtotal += $itemcount;
 8009:     return $datatable;
 8010: }
 8011: 
 8012: sub possible_newspares {
 8013:     my ($server,$currspares,$serverhomes,$altids) = @_;
 8014:     my $serverhostname = &Apache::lonnet::hostname($server);
 8015:     my %excluded;
 8016:     if ($serverhostname ne '') {
 8017:         %excluded = (
 8018:                        $serverhostname => 1,
 8019:                     );
 8020:     }
 8021:     if (ref($currspares) eq 'HASH') {
 8022:         foreach my $type (keys(%{$currspares})) {
 8023:             if (ref($currspares->{$type}) eq 'ARRAY') {
 8024:                 if (@{$currspares->{$type}} > 0) {
 8025:                     foreach my $curr (@{$currspares->{$type}}) {
 8026:                         my $hostname = &Apache::lonnet::hostname($curr);
 8027:                         $excluded{$hostname} = 1;
 8028:                     }
 8029:                 }
 8030:             }
 8031:         }
 8032:     }
 8033:     my @choices;
 8034:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 8035:         if (keys(%{$serverhomes}) > 1) {
 8036:             foreach my $name (sort(keys(%{$serverhomes}))) {
 8037:                 unless ($excluded{$name}) {
 8038:                     if (exists($altids->{$serverhomes->{$name}})) {
 8039:                         push(@choices,$altids->{$serverhomes->{$name}});
 8040:                     } else {
 8041:                         push(@choices,$serverhomes->{$name});
 8042:                     }
 8043:                 }
 8044:             }
 8045:         }
 8046:     }
 8047:     return sort(@choices);
 8048: }
 8049: 
 8050: sub print_loadbalancing {
 8051:     my ($dom,$settings,$rowtotal) = @_;
 8052:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8053:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8054:     my $numinrow = 1;
 8055:     my $datatable;
 8056:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8057:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 8058:     if (ref($settings) eq 'HASH') {
 8059:         %existing = %{$settings};
 8060:     }
 8061:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 8062:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8063:                                   \%currtargets,\%currrules,\%currcookies);
 8064:     } else {
 8065:         return;
 8066:     }
 8067:     my ($othertitle,$usertypes,$types) =
 8068:         &Apache::loncommon::sorted_inst_types($dom);
 8069:     my $rownum = 8;
 8070:     if (ref($types) eq 'ARRAY') {
 8071:         $rownum += scalar(@{$types});
 8072:     }
 8073:     my @css_class = ('LC_odd_row','LC_even_row');
 8074:     my $balnum = 0;
 8075:     my $islast;
 8076:     my (@toshow,$disabledtext);
 8077:     if (keys(%currbalancer) > 0) {
 8078:         @toshow = sort(keys(%currbalancer));
 8079:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 8080:             push(@toshow,'');
 8081:         }
 8082:     } else {
 8083:         @toshow = ('');
 8084:         $disabledtext = &mt('No existing load balancer');
 8085:     }
 8086:     foreach my $lonhost (@toshow) {
 8087:         if ($balnum == scalar(@toshow)-1) {
 8088:             $islast = 1;
 8089:         } else {
 8090:             $islast = 0;
 8091:         }
 8092:         my $cssidx = $balnum%2;
 8093:         my $targets_div_style = 'display: none';
 8094:         my $disabled_div_style = 'display: block';
 8095:         my $homedom_div_style = 'display: none';
 8096:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 8097:                       '<td rowspan="'.$rownum.'" valign="top">'.
 8098:                       '<p>';
 8099:         if ($lonhost eq '') {
 8100:             $datatable .= '<span class="LC_nobreak">';
 8101:             if (keys(%currbalancer) > 0) {
 8102:                 $datatable .= &mt('Add balancer:');
 8103:             } else {
 8104:                 $datatable .= &mt('Enable balancer:');
 8105:             }
 8106:             $datatable .= '&nbsp;'.
 8107:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 8108:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 8109:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 8110:                           '<option value="" selected="selected">'.&mt('None').
 8111:                           '</option>'."\n";
 8112:             foreach my $server (sort(keys(%servers))) {
 8113:                 next if ($currbalancer{$server});
 8114:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 8115:             }
 8116:             $datatable .=
 8117:                 '</select>'."\n".
 8118:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 8119:         } else {
 8120:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 8121:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 8122:                            &mt('Stop balancing').'</label>'.
 8123:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 8124:             $targets_div_style = 'display: block';
 8125:             $disabled_div_style = 'display: none';
 8126:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 8127:                 $homedom_div_style = 'display: block';
 8128:             }
 8129:         }
 8130:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 8131:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 8132:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 8133:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 8134:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 8135:         my @sparestypes = ('primary','default');
 8136:         my %typetitles = &sparestype_titles();
 8137:         my %hostherechecked = (
 8138:                                   no => ' checked="checked"',
 8139:                               );
 8140:         my %balcookiechecked = (
 8141:                                   no => ' checked="checked"',
 8142:                                );
 8143:         foreach my $sparetype (@sparestypes) {
 8144:             my $targettable;
 8145:             for (my $i=0; $i<$numspares; $i++) {
 8146:                 my $checked;
 8147:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 8148:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 8149:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 8150:                             $checked = ' checked="checked"';
 8151:                         }
 8152:                     }
 8153:                 }
 8154:                 my ($chkboxval,$disabled);
 8155:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 8156:                     $chkboxval = $spares[$i];
 8157:                 }
 8158:                 if (exists($currbalancer{$spares[$i]})) {
 8159:                     $disabled = ' disabled="disabled"';
 8160:                 }
 8161:                 $targettable .=
 8162:                     '<td><span class="LC_nobreak"><label>'.
 8163:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 8164:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 8165:                     '</span></label></span></td>';
 8166:                 my $rem = $i%($numinrow);
 8167:                 if ($rem == 0) {
 8168:                     if (($i > 0) && ($i < $numspares-1)) {
 8169:                         $targettable .= '</tr>';
 8170:                     }
 8171:                     if ($i < $numspares-1) {
 8172:                         $targettable .= '<tr>';
 8173:                     }
 8174:                 }
 8175:             }
 8176:             if ($targettable ne '') {
 8177:                 my $rem = $numspares%($numinrow);
 8178:                 my $colsleft = $numinrow - $rem;
 8179:                 if ($colsleft > 1 ) {
 8180:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8181:                                     '&nbsp;</td>';
 8182:                 } elsif ($colsleft == 1) {
 8183:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 8184:                 }
 8185:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 8186:                                '<table><tr>'.$targettable.'</tr></table><br />';
 8187:             }
 8188:             $hostherechecked{$sparetype} = '';
 8189:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 8190:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 8191:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 8192:                         $hostherechecked{$sparetype} = ' checked="checked"';
 8193:                         $hostherechecked{'no'} = '';
 8194:                     }
 8195:                 }
 8196:             }
 8197:         }
 8198:         if ($currcookies{$lonhost}) {
 8199:             %balcookiechecked = (
 8200:                                     yes => ' checked="checked"',
 8201:                                 );
 8202:         }
 8203:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 8204:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 8205:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 8206:         foreach my $sparetype (@sparestypes) {
 8207:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 8208:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 8209:                           '</i></label><br />';
 8210:         }
 8211:         $datatable .= &mt('Use balancer cookie').'<br />'.
 8212:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 8213:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 8214:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 8215:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 8216:                       '</div></td></tr>'.
 8217:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 8218:                                            $othertitle,$usertypes,$types,\%servers,
 8219:                                            \%currbalancer,$lonhost,
 8220:                                            $targets_div_style,$homedom_div_style,
 8221:                                            $css_class[$cssidx],$balnum,$islast);
 8222:         $$rowtotal += $rownum;
 8223:         $balnum ++;
 8224:     }
 8225:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 8226:     return $datatable;
 8227: }
 8228: 
 8229: sub get_loadbalancers_config {
 8230:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 8231:     return unless ((ref($servers) eq 'HASH') &&
 8232:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 8233:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 8234:                    (ref($currcookies) eq 'HASH'));
 8235:     if (keys(%{$existing}) > 0) {
 8236:         my $oldlonhost;
 8237:         foreach my $key (sort(keys(%{$existing}))) {
 8238:             if ($key eq 'lonhost') {
 8239:                 $oldlonhost = $existing->{'lonhost'};
 8240:                 $currbalancer->{$oldlonhost} = 1;
 8241:             } elsif ($key eq 'targets') {
 8242:                 if ($oldlonhost) {
 8243:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 8244:                 }
 8245:             } elsif ($key eq 'rules') {
 8246:                 if ($oldlonhost) {
 8247:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 8248:                 }
 8249:             } elsif (ref($existing->{$key}) eq 'HASH') {
 8250:                 $currbalancer->{$key} = 1;
 8251:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 8252:                 $currrules->{$key} = $existing->{$key}{'rules'};
 8253:                 if ($existing->{$key}{'cookie'}) {
 8254:                     $currcookies->{$key} = 1;
 8255:                 }
 8256:             }
 8257:         }
 8258:     } else {
 8259:         my ($balancerref,$targetsref) =
 8260:                 &Apache::lonnet::get_lonbalancer_config($servers);
 8261:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 8262:             foreach my $server (sort(keys(%{$balancerref}))) {
 8263:                 $currbalancer->{$server} = 1;
 8264:                 $currtargets->{$server} = $targetsref->{$server};
 8265:             }
 8266:         }
 8267:     }
 8268:     return;
 8269: }
 8270: 
 8271: sub loadbalancing_rules {
 8272:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 8273:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 8274:         $css_class,$balnum,$islast) = @_;
 8275:     my $output;
 8276:     my $num = 0;
 8277:     my ($alltypes,$othertypes,$titles) =
 8278:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8279:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 8280:         foreach my $type (@{$alltypes}) {
 8281:             $num ++;
 8282:             my $current;
 8283:             if (ref($currrules) eq 'HASH') {
 8284:                 $current = $currrules->{$type};
 8285:             }
 8286:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 8287:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 8288:                     $current = '';
 8289:                 }
 8290:             }
 8291:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 8292:                                              $servers,$currbalancer,$lonhost,$dom,
 8293:                                              $targets_div_style,$homedom_div_style,
 8294:                                              $css_class,$balnum,$num,$islast);
 8295:         }
 8296:     }
 8297:     return $output;
 8298: }
 8299: 
 8300: sub loadbalancing_titles {
 8301:     my ($dom,$intdom,$usertypes,$types) = @_;
 8302:     my %othertypes = (
 8303:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 8304:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 8305:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 8306:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 8307:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 8308:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 8309:                      );
 8310:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 8311:     my @available;
 8312:     if (ref($types) eq 'ARRAY') {
 8313:         @available = @{$types};
 8314:     }
 8315:     unless (grep(/^default$/,@available)) {
 8316:         push(@available,'default');
 8317:     }
 8318:     unshift(@alltypes,@available);
 8319:     my %titles;
 8320:     foreach my $type (@alltypes) {
 8321:         if ($type =~ /^_LC_/) {
 8322:             $titles{$type} = $othertypes{$type};
 8323:         } elsif ($type eq 'default') {
 8324:             $titles{$type} = &mt('All users from [_1]',$dom);
 8325:             if (ref($types) eq 'ARRAY') {
 8326:                 if (@{$types} > 0) {
 8327:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 8328:                 }
 8329:             }
 8330:         } elsif (ref($usertypes) eq 'HASH') {
 8331:             $titles{$type} = $usertypes->{$type};
 8332:         }
 8333:     }
 8334:     return (\@alltypes,\%othertypes,\%titles);
 8335: }
 8336: 
 8337: sub loadbalance_rule_row {
 8338:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 8339:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 8340:     my @rulenames;
 8341:     my %ruletitles = &offloadtype_text();
 8342:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 8343:         @rulenames = ('balancer','offloadedto','specific');
 8344:     } else {
 8345:         @rulenames = ('default','homeserver');
 8346:         if ($type eq '_LC_external') {
 8347:             push(@rulenames,'externalbalancer');
 8348:         } else {
 8349:             push(@rulenames,'specific');
 8350:         }
 8351:         push(@rulenames,'none');
 8352:     }
 8353:     my $style = $targets_div_style;
 8354:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 8355:         $style = $homedom_div_style;
 8356:     }
 8357:     my $space;
 8358:     if ($islast && $num == 1) {
 8359:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 8360:     }
 8361:     my $output =
 8362:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 8363:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 8364:         '<td valaign="top">'.$space.
 8365:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 8366:     for (my $i=0; $i<@rulenames; $i++) {
 8367:         my $rule = $rulenames[$i];
 8368:         my ($checked,$extra);
 8369:         if ($rulenames[$i] eq 'default') {
 8370:             $rule = '';
 8371:         }
 8372:         if ($rulenames[$i] eq 'specific') {
 8373:             if (ref($servers) eq 'HASH') {
 8374:                 my $default;
 8375:                 if (($current ne '') && (exists($servers->{$current}))) {
 8376:                     $checked = ' checked="checked"';
 8377:                 }
 8378:                 unless ($checked) {
 8379:                     $default = ' selected="selected"';
 8380:                 }
 8381:                 $extra =
 8382:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 8383:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 8384:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 8385:                     '<option value=""'.$default.'></option>'."\n";
 8386:                 foreach my $server (sort(keys(%{$servers}))) {
 8387:                     if (ref($currbalancer) eq 'HASH') {
 8388:                         next if (exists($currbalancer->{$server}));
 8389:                     }
 8390:                     my $selected;
 8391:                     if ($server eq $current) {
 8392:                         $selected = ' selected="selected"';
 8393:                     }
 8394:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 8395:                 }
 8396:                 $extra .= '</select>';
 8397:             }
 8398:         } elsif ($rule eq $current) {
 8399:             $checked = ' checked="checked"';
 8400:         }
 8401:         $output .= '<span class="LC_nobreak"><label>'.
 8402:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 8403:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 8404:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 8405:                    ')"'.$checked.' />&nbsp;';
 8406:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 8407:             $output .= $ruletitles{'particular'};
 8408:         } else {
 8409:             $output .= $ruletitles{$rulenames[$i]};
 8410:         }
 8411:         $output .= '</label>'.$extra.'</span><br />'."\n";
 8412:     }
 8413:     $output .= '</div></td></tr>'."\n";
 8414:     return $output;
 8415: }
 8416: 
 8417: sub offloadtype_text {
 8418:     my %ruletitles = &Apache::lonlocal::texthash (
 8419:            'default'          => 'Offloads to default destinations',
 8420:            'homeserver'       => "Offloads to user's home server",
 8421:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 8422:            'specific'         => 'Offloads to specific server',
 8423:            'none'             => 'No offload',
 8424:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 8425:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 8426:            'particular'       => 'Session hosted (after re-auth) on server:',
 8427:     );
 8428:     return %ruletitles;
 8429: }
 8430: 
 8431: sub sparestype_titles {
 8432:     my %typestitles = &Apache::lonlocal::texthash (
 8433:                           'primary' => 'primary',
 8434:                           'default' => 'default',
 8435:                       );
 8436:     return %typestitles;
 8437: }
 8438: 
 8439: sub contact_titles {
 8440:     my %titles = &Apache::lonlocal::texthash (
 8441:                    'supportemail'    => 'Support E-mail address',
 8442:                    'adminemail'      => 'Default Server Admin E-mail address',
 8443:                    'errormail'       => 'Error reports to be e-mailed to',
 8444:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 8445:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 8446:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 8447:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 8448:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 8449:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 8450:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 8451:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 8452:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 8453:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 8454:                    'errorweights'    => 'Weights used to compute error count',
 8455:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 8456:                  );
 8457:     my %short_titles = &Apache::lonlocal::texthash (
 8458:                            adminemail   => 'Admin E-mail address',
 8459:                            supportemail => 'Support E-mail',
 8460:                        );   
 8461:     return (\%titles,\%short_titles);
 8462: }
 8463: 
 8464: sub helpform_fields {
 8465:     my %titles =  &Apache::lonlocal::texthash (
 8466:                        'username'   => 'Name',
 8467:                        'user'       => 'Username/domain',
 8468:                        'phone'      => 'Phone',
 8469:                        'cc'         => 'Cc e-mail',
 8470:                        'course'     => 'Course Details',
 8471:                        'section'    => 'Sections',
 8472:                        'screenshot' => 'File upload',
 8473:     );
 8474:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 8475:     my %possoptions = (
 8476:                         username     => ['yes','no','req'],
 8477:                         phone        => ['yes','no','req'],
 8478:                         user         => ['yes','no'],
 8479:                         cc           => ['yes','no'],
 8480:                         course       => ['yes','no'],
 8481:                         section      => ['yes','no'],
 8482:                         screenshot   => ['yes','no'],
 8483:                       );
 8484:     my %fieldoptions = &Apache::lonlocal::texthash (
 8485:                          'yes'  => 'Optional',
 8486:                          'req'  => 'Required',
 8487:                          'no'   => "Not shown",
 8488:     );
 8489:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 8490: }
 8491: 
 8492: sub tool_titles {
 8493:     my %titles = &Apache::lonlocal::texthash (
 8494:                      aboutme    => 'Personal web page',
 8495:                      blog       => 'Blog',
 8496:                      portfolio  => 'Portfolio',
 8497:                      timezone   => 'Can set time zone',
 8498:                      official   => 'Official courses (with institutional codes)',
 8499:                      unofficial => 'Unofficial courses',
 8500:                      community  => 'Communities',
 8501:                      textbook   => 'Textbook courses',
 8502:                  );
 8503:     return %titles;
 8504: }
 8505: 
 8506: sub courserequest_titles {
 8507:     my %titles = &Apache::lonlocal::texthash (
 8508:                                    official   => 'Official',
 8509:                                    unofficial => 'Unofficial',
 8510:                                    community  => 'Communities',
 8511:                                    textbook   => 'Textbook',
 8512:                                    lti        => 'LTI Provider',
 8513:                                    norequest  => 'Not allowed',
 8514:                                    approval   => 'Approval by Dom. Coord.',
 8515:                                    validate   => 'With validation',
 8516:                                    autolimit  => 'Numerical limit',
 8517:                                    unlimited  => '(blank for unlimited)',
 8518:                  );
 8519:     return %titles;
 8520: }
 8521: 
 8522: sub authorrequest_titles {
 8523:     my %titles = &Apache::lonlocal::texthash (
 8524:                                    norequest  => 'Not allowed',
 8525:                                    approval   => 'Approval by Dom. Coord.',
 8526:                                    automatic  => 'Automatic approval',
 8527:                  );
 8528:     return %titles;
 8529: }
 8530: 
 8531: sub courserequest_conditions {
 8532:     my %conditions = &Apache::lonlocal::texthash (
 8533:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 8534:        validate   => '(Processing of request subject to institutional validation).',
 8535:                  );
 8536:     return %conditions;
 8537: }
 8538: 
 8539: 
 8540: sub print_usercreation {
 8541:     my ($position,$dom,$settings,$rowtotal) = @_;
 8542:     my $numinrow = 4;
 8543:     my $datatable;
 8544:     if ($position eq 'top') {
 8545:         $$rowtotal ++;
 8546:         my $rowcount = 0;
 8547:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 8548:         if (ref($rules) eq 'HASH') {
 8549:             if (keys(%{$rules}) > 0) {
 8550:                 $datatable .= &user_formats_row('username',$settings,$rules,
 8551:                                                 $ruleorder,$numinrow,$rowcount);
 8552:                 $$rowtotal ++;
 8553:                 $rowcount ++;
 8554:             }
 8555:         }
 8556:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 8557:         if (ref($idrules) eq 'HASH') {
 8558:             if (keys(%{$idrules}) > 0) {
 8559:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 8560:                                                 $idruleorder,$numinrow,$rowcount);
 8561:                 $$rowtotal ++;
 8562:                 $rowcount ++;
 8563:             }
 8564:         }
 8565:         if ($rowcount == 0) {
 8566:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 8567:             $$rowtotal ++;
 8568:             $rowcount ++;
 8569:         }
 8570:     } elsif ($position eq 'middle') {
 8571:         my @creators = ('author','course','requestcrs');
 8572:         my ($rules,$ruleorder) =
 8573:             &Apache::lonnet::inst_userrules($dom,'username');
 8574:         my %lt = &usercreation_types();
 8575:         my %checked;
 8576:         if (ref($settings) eq 'HASH') {
 8577:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 8578:                 foreach my $item (@creators) {
 8579:                     $checked{$item} = $settings->{'cancreate'}{$item};
 8580:                 }
 8581:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 8582:                 foreach my $item (@creators) {
 8583:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 8584:                         $checked{$item} = 'none';
 8585:                     }
 8586:                 }
 8587:             }
 8588:         }
 8589:         my $rownum = 0;
 8590:         foreach my $item (@creators) {
 8591:             $rownum ++;
 8592:             if ($checked{$item} eq '') {
 8593:                 $checked{$item} = 'any';
 8594:             }
 8595:             my $css_class;
 8596:             if ($rownum%2) {
 8597:                 $css_class = '';
 8598:             } else {
 8599:                 $css_class = ' class="LC_odd_row" ';
 8600:             }
 8601:             $datatable .= '<tr'.$css_class.'>'.
 8602:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 8603:                          '</span></td><td align="right">';
 8604:             my @options = ('any');
 8605:             if (ref($rules) eq 'HASH') {
 8606:                 if (keys(%{$rules}) > 0) {
 8607:                     push(@options,('official','unofficial'));
 8608:                 }
 8609:             }
 8610:             push(@options,'none');
 8611:             foreach my $option (@options) {
 8612:                 my $type = 'radio';
 8613:                 my $check = ' ';
 8614:                 if ($checked{$item} eq $option) {
 8615:                     $check = ' checked="checked" ';
 8616:                 } 
 8617:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8618:                               '<input type="'.$type.'" name="can_createuser_'.
 8619:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 8620:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 8621:             }
 8622:             $datatable .= '</td></tr>';
 8623:         }
 8624:     } else {
 8625:         my @contexts = ('author','course','domain');
 8626:         my @authtypes = ('int','krb4','krb5','loc','lti');
 8627:         my %checked;
 8628:         if (ref($settings) eq 'HASH') {
 8629:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 8630:                 foreach my $item (@contexts) {
 8631:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 8632:                         foreach my $auth (@authtypes) {
 8633:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 8634:                                 $checked{$item}{$auth} = ' checked="checked" ';
 8635:                             }
 8636:                         }
 8637:                     }
 8638:                 }
 8639:             }
 8640:         } else {
 8641:             foreach my $item (@contexts) {
 8642:                 foreach my $auth (@authtypes) {
 8643:                     $checked{$item}{$auth} = ' checked="checked" ';
 8644:                 }
 8645:             }
 8646:         }
 8647:         my %title = &context_names();
 8648:         my %authname = &authtype_names();
 8649:         my $rownum = 0;
 8650:         my $css_class; 
 8651:         foreach my $item (@contexts) {
 8652:             if ($rownum%2) {
 8653:                 $css_class = '';
 8654:             } else {
 8655:                 $css_class = ' class="LC_odd_row" ';
 8656:             }
 8657:             $datatable .=   '<tr'.$css_class.'>'.
 8658:                             '<td>'.$title{$item}.
 8659:                             '</td><td class="LC_left_item">'.
 8660:                             '<span class="LC_nobreak">';
 8661:             foreach my $auth (@authtypes) {
 8662:                 $datatable .= '<label>'. 
 8663:                               '<input type="checkbox" name="'.$item.'_auth" '.
 8664:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 8665:                               $authname{$auth}.'</label>&nbsp;';
 8666:             }
 8667:             $datatable .= '</span></td></tr>';
 8668:             $rownum ++;
 8669:         }
 8670:         $$rowtotal += $rownum;
 8671:     }
 8672:     return $datatable;
 8673: }
 8674: 
 8675: sub print_selfcreation {
 8676:     my ($position,$dom,$settings,$rowtotal) = @_;
 8677:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 8678:         $emaildomain,$datatable);
 8679:     if (ref($settings) eq 'HASH') {
 8680:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 8681:             $createsettings = $settings->{'cancreate'};
 8682:             if (ref($createsettings) eq 'HASH') {
 8683:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 8684:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 8685:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 8686:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 8687:                         @selfcreate = ('email','login','sso');
 8688:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 8689:                         @selfcreate = ($createsettings->{'selfcreate'});
 8690:                     }
 8691:                 }
 8692:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 8693:                     $processing = $createsettings->{'selfcreateprocessing'};
 8694:                 }
 8695:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 8696:                     $emailoptions = $createsettings->{'emailoptions'};
 8697:                 }
 8698:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 8699:                     $emailverified = $createsettings->{'emailverified'};
 8700:                 }
 8701:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 8702:                     $emaildomain = $createsettings->{'emaildomain'};
 8703:                 }
 8704:             }
 8705:         }
 8706:     }
 8707:     my %radiohash;
 8708:     my $numinrow = 4;
 8709:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 8710:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8711:     if ($position eq 'top') {
 8712:         my %choices = &Apache::lonlocal::texthash (
 8713:                                                       cancreate_login      => 'Institutional Login',
 8714:                                                       cancreate_sso        => 'Institutional Single Sign On',
 8715:                                                   );
 8716:         my @toggles = sort(keys(%choices));
 8717:         my %defaultchecked = (
 8718:                                'cancreate_login' => 'off',
 8719:                                'cancreate_sso'   => 'off',
 8720:                              );
 8721:         my ($onclick,$itemcount);
 8722:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 8723:                                                      \%choices,$itemcount,$onclick);
 8724:         $$rowtotal += $itemcount;
 8725: 
 8726:         if (ref($usertypes) eq 'HASH') {
 8727:             if (keys(%{$usertypes}) > 0) {
 8728:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 8729:                                              $dom,$numinrow,$othertitle,
 8730:                                              'statustocreate',$rowtotal);
 8731:                 $$rowtotal ++;
 8732:             }
 8733:         }
 8734:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 8735:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8736:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 8737:         my $rem;
 8738:         my $numperrow = 2;
 8739:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 8740:         $datatable .= '<tr'.$css_class.'>'.
 8741:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 8742:                      '<td class="LC_left_item">'."\n".
 8743:                      '<table>'."\n";
 8744:         for (my $i=0; $i<@fields; $i++) {
 8745:             $rem = $i%($numperrow);
 8746:             if ($rem == 0) {
 8747:                 if ($i > 0) {
 8748:                     $datatable .= '</tr>';
 8749:                 }
 8750:                 $datatable .= '<tr>';
 8751:             }
 8752:             my $currval;
 8753:             if (ref($createsettings) eq 'HASH') {
 8754:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 8755:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 8756:                 }
 8757:             }
 8758:             $datatable .= '<td class="LC_left_item">'.
 8759:                           '<span class="LC_nobreak">'.
 8760:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 8761:                           'value="'.$currval.'" size="10" />&nbsp;'.
 8762:                           $fieldtitles{$fields[$i]}.'</span></td>';
 8763:         }
 8764:         my $colsleft = $numperrow - $rem;
 8765:         if ($colsleft > 1 ) {
 8766:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8767:                          '&nbsp;</td>';
 8768:         } elsif ($colsleft == 1) {
 8769:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8770:         }
 8771:         $datatable .= '</tr></table></td></tr>';
 8772:         $$rowtotal ++;
 8773:     } elsif ($position eq 'middle') {
 8774:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 8775:         my @posstypes;
 8776:         if (ref($types) eq 'ARRAY') {
 8777:             @posstypes = @{$types};
 8778:         }
 8779:         unless (grep(/^default$/,@posstypes)) {
 8780:             push(@posstypes,'default');
 8781:         }
 8782:         my %usertypeshash;
 8783:         if (ref($usertypes) eq 'HASH') {
 8784:             %usertypeshash = %{$usertypes};
 8785:         }
 8786:         $usertypeshash{'default'} = $othertitle;
 8787:         foreach my $status (@posstypes) {
 8788:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 8789:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 8790:             $$rowtotal ++;
 8791:         }
 8792:     } else {
 8793:         my %choices = &Apache::lonlocal::texthash (
 8794:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 8795:                                                   );
 8796:         my @toggles = sort(keys(%choices));
 8797:         my %defaultchecked = (
 8798:                                'cancreate_email' => 'off',
 8799:                              );
 8800:         my $customclass = 'LC_selfcreate_email';
 8801:         my $classprefix = 'LC_canmodify_emailusername_';
 8802:         my $optionsprefix = 'LC_options_emailusername_';
 8803:         my $display = 'none';
 8804:         my $rowstyle = 'display:none';
 8805:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 8806:             $display = 'block';
 8807:             $rowstyle = 'display:table-row';
 8808:         }
 8809:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 8810:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 8811:                                                      \%choices,$$rowtotal,$onclick);
 8812:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 8813:                                          $rowstyle);
 8814:         $$rowtotal ++;
 8815:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 8816:                                       $rowstyle);
 8817:         $$rowtotal ++;
 8818:         my (@ordered,@posstypes,%usertypeshash);
 8819:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 8820:         my ($emailrules,$emailruleorder) =
 8821:             &Apache::lonnet::inst_userrules($dom,'email');
 8822:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8823:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8824:         if (ref($types) eq 'ARRAY') {
 8825:             @posstypes = @{$types};
 8826:         }
 8827:         if (@posstypes) {
 8828:             unless (grep(/^default$/,@posstypes)) {
 8829:                 push(@posstypes,'default');
 8830:             }
 8831:             if (ref($usertypes) eq 'HASH') {
 8832:                 %usertypeshash = %{$usertypes};
 8833:             }
 8834:             my $currassign;
 8835:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 8836:                 $currassign = {
 8837:                                   selfassign => $domdefaults{'inststatusguest'},
 8838:                               };
 8839:                 @ordered = @{$domdefaults{'inststatusguest'}};
 8840:             } else {
 8841:                 $currassign = { selfassign => [] };
 8842:             }
 8843:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 8844:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 8845:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 8846:                                          $numinrow,$othertitle,'selfassign',
 8847:                                          $rowtotal,$onclicktypes,$customclass,
 8848:                                          $rowstyle);
 8849:             $$rowtotal ++;
 8850:             $usertypeshash{'default'} = $othertitle;
 8851:             foreach my $status (@posstypes) {
 8852:                 my $css_class;
 8853:                 if ($$rowtotal%2) {
 8854:                     $css_class = 'LC_odd_row ';
 8855:                 }
 8856:                 $css_class .= $customclass;
 8857:                 my $rowid = $optionsprefix.$status;
 8858:                 my $hidden = 1;
 8859:                 my $currstyle = 'display:none';
 8860:                 if (grep(/^\Q$status\E$/,@ordered)) {
 8861:                     $currstyle = $rowstyle;
 8862:                     $hidden = 0;
 8863:                 }
 8864:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 8865:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 8866:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 8867:                 unless ($hidden) {
 8868:                     $$rowtotal ++;
 8869:                 }
 8870:             }
 8871:         } else {
 8872:             my $css_class;
 8873:             if ($$rowtotal%2) {
 8874:                 $css_class = 'LC_odd_row ';
 8875:             }
 8876:             $css_class .= $customclass;
 8877:             $usertypeshash{'default'} = $othertitle;
 8878:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 8879:                                          $emailrules,$emailruleorder,$settings,'default','',
 8880:                                          $othertitle,$css_class,$rowstyle,$intdom);
 8881:             $$rowtotal ++;
 8882:         }
 8883:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8884:         $numinrow = 1;
 8885:         if (@posstypes) {
 8886:             foreach my $status (@posstypes) {
 8887:                 my $rowid = $classprefix.$status;
 8888:                 my $datarowstyle = 'display:none';
 8889:                 if (grep(/^\Q$status\E$/,@ordered)) {
 8890:                     $datarowstyle = $rowstyle;
 8891:                 }
 8892:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 8893:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 8894:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 8895:                 unless ($datarowstyle eq 'display:none') {
 8896:                     $$rowtotal ++;
 8897:                 }
 8898:             }
 8899:         } else {
 8900:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 8901:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 8902:                                                    $infotitles,'',$customclass,$rowstyle);
 8903:         }
 8904:     }
 8905:     return $datatable;
 8906: }
 8907: 
 8908: sub selfcreate_javascript {
 8909:     return <<"ENDSCRIPT";
 8910: 
 8911: <script type="text/javascript">
 8912: // <![CDATA[
 8913: 
 8914: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 8915:     var x = document.getElementsByClassName(target);
 8916:     var insttypes = 0;
 8917:     var insttypeRegExp = new RegExp(prefix);
 8918:     if ((x.length != undefined) && (x.length > 0)) {
 8919:         if (form.elements[radio].length != undefined) {
 8920:             for (var i=0; i<form.elements[radio].length; i++) {
 8921:                 if (form.elements[radio][i].checked) {
 8922:                     if (form.elements[radio][i].value == 1) {
 8923:                         for (var j=0; j<x.length; j++) {
 8924:                             if (x[j].id == 'undefined') {
 8925:                                 x[j].style.display = 'table-row';
 8926:                             } else if (insttypeRegExp.test(x[j].id)) {
 8927:                                 insttypes ++;
 8928:                             } else {
 8929:                                 x[j].style.display = 'table-row';
 8930:                             }
 8931:                         }
 8932:                     } else {
 8933:                         for (var j=0; j<x.length; j++) {
 8934:                             x[j].style.display = 'none';
 8935:                         }
 8936:                     }
 8937:                     break;
 8938:                 }
 8939:             }
 8940:             if (insttypes > 0) {
 8941:                 toggleDataRow(form,checkbox,target,altprefix);
 8942:                 toggleDataRow(form,checkbox,target,prefix,1);
 8943:             }
 8944:         }
 8945:     }
 8946:     return;
 8947: }
 8948: 
 8949: function toggleDataRow(form,checkbox,target,prefix,docount) {
 8950:     if (form.elements[checkbox].length != undefined) {
 8951:         var count = 0;
 8952:         if (docount) {
 8953:             for (var i=0; i<form.elements[checkbox].length; i++) {
 8954:                 if (form.elements[checkbox][i].checked) {
 8955:                     count ++;
 8956:                 }
 8957:             }
 8958:         }
 8959:         for (var i=0; i<form.elements[checkbox].length; i++) {
 8960:             var type = form.elements[checkbox][i].value;
 8961:             if (document.getElementById(prefix+type)) {
 8962:                 if (form.elements[checkbox][i].checked) {
 8963:                     document.getElementById(prefix+type).style.display = 'table-row';
 8964:                     if (count % 2 == 1) {
 8965:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 8966:                     } else {
 8967:                         document.getElementById(prefix+type).className = target;
 8968:                     }
 8969:                     count ++;
 8970:                 } else {
 8971:                     document.getElementById(prefix+type).style.display = 'none';
 8972:                 }
 8973:             }
 8974:         }
 8975:     }
 8976:     return;
 8977: }
 8978: 
 8979: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 8980:     var caller = radio+'_'+status;
 8981:     if (form.elements[caller].length != undefined) {
 8982:         for (var i=0; i<form.elements[caller].length; i++) {
 8983:             if (form.elements[caller][i].checked) {
 8984:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 8985:                     var curr = form.elements[caller][i].value;
 8986:                     if (prefix) {
 8987:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 8988:                     }
 8989:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 8990:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 8991:                     if (curr == 'custom') {
 8992:                         if (prefix) {
 8993:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 8994:                         }
 8995:                     } else if (curr == 'inst') {
 8996:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 8997:                     } else if (curr == 'noninst') {
 8998:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 8999:                     }
 9000:                     break;
 9001:                 }
 9002:             }
 9003:         }
 9004:     }
 9005: }
 9006: 
 9007: // ]]>
 9008: </script>
 9009: 
 9010: ENDSCRIPT
 9011: }
 9012: 
 9013: sub noninst_users {
 9014:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 9015:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
 9016:     my $class = 'LC_left_item';
 9017:     if ($css_class) {
 9018:         $css_class = ' class="'.$css_class.'"';
 9019:     }
 9020:     if ($rowid) {
 9021:         $rowid = ' id="'.$rowid.'"';
 9022:     }
 9023:     if ($rowstyle) {
 9024:         $rowstyle = ' style="'.$rowstyle.'"';
 9025:     }
 9026:     my ($output,$description);
 9027:     if ($type eq 'default') {
 9028:         $description = &mt('Requests for: [_1]',$typetitle);
 9029:     } else {
 9030:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 9031:     }
 9032:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 9033:               "<td>$description</td>\n".
 9034:               '<td class="'.$class.'" colspan="2">'.
 9035:               '<table><tr>';
 9036:     my %headers = &Apache::lonlocal::texthash(
 9037:               approve  => 'Processing',
 9038:               email    => 'E-mail',
 9039:               username => 'Username',
 9040:     );
 9041:     foreach my $item ('approve','email','username') {
 9042:         $output .= '<th>'.$headers{$item}.'</th>';
 9043:     }
 9044:     $output .= '</tr><tr>';
 9045:     foreach my $item ('approve','email','username') {
 9046:         $output .= '<td valign="top">';
 9047:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 9048:         if ($item eq 'approve') {
 9049:             %choices = &Apache::lonlocal::texthash (
 9050:                                                      automatic => 'Automatically approved',
 9051:                                                      approval  => 'Queued for approval',
 9052:                                                    );
 9053:             @options = ('automatic','approval');
 9054:             $hashref = $processing;
 9055:             $defoption = 'automatic';
 9056:             $name = 'cancreate_emailprocess_'.$type;
 9057:         } elsif ($item eq 'email') {
 9058:             %choices = &Apache::lonlocal::texthash (
 9059:                                                      any     => 'Any e-mail',
 9060:                                                      inst    => 'Institutional only',
 9061:                                                      noninst => 'Non-institutional only',
 9062:                                                      custom  => 'Custom restrictions',
 9063:                                                    );
 9064:             @options = ('any','inst','noninst');
 9065:             my $showcustom;
 9066:             if (ref($emailrules) eq 'HASH') {
 9067:                 if (keys(%{$emailrules}) > 0) {
 9068:                     push(@options,'custom');
 9069:                     $showcustom = 'cancreate_emailrule';
 9070:                     if (ref($settings) eq 'HASH') {
 9071:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 9072:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 9073:                                 if (exists($emailrules->{$rule})) {
 9074:                                     $hascustom ++;
 9075:                                 }
 9076:                             }
 9077:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 9078:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 9079:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 9080:                                     if (exists($emailrules->{$rule})) {
 9081:                                         $hascustom ++;
 9082:                                     }
 9083:                                 }
 9084:                             }
 9085:                         }
 9086:                     }
 9087:                 }
 9088:             }
 9089:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 9090:                                                      "'cancreate_emaildomain','$type'".');"';
 9091:             $hashref = $emailoptions;
 9092:             $defoption = 'any';
 9093:             $name = 'cancreate_emailoptions_'.$type;
 9094:         } elsif ($item eq 'username') {
 9095:             %choices = &Apache::lonlocal::texthash (
 9096:                                                      all    => 'Same as e-mail',
 9097:                                                      first  => 'Omit @domain',
 9098:                                                      free   => 'Free to choose',
 9099:                                                    );
 9100:             @options = ('all','first','free');
 9101:             $hashref = $emailverified;
 9102:             $defoption = 'all';
 9103:             $name = 'cancreate_usernameoptions_'.$type;
 9104:         }
 9105:         foreach my $option (@options) {
 9106:             my $checked;
 9107:             if (ref($hashref) eq 'HASH') {
 9108:                 if ($type eq '') {
 9109:                     if (!exists($hashref->{'default'})) {
 9110:                         if ($option eq $defoption) {
 9111:                             $checked = ' checked="checked"';
 9112:                         }
 9113:                     } else {
 9114:                         if ($hashref->{'default'} eq $option) {
 9115:                             $checked = ' checked="checked"';
 9116:                         }
 9117:                     }
 9118:                 } else {
 9119:                     if (!exists($hashref->{$type})) {
 9120:                         if ($option eq $defoption) {
 9121:                             $checked = ' checked="checked"';
 9122:                         }
 9123:                     } else {
 9124:                         if ($hashref->{$type} eq $option) {
 9125:                             $checked = ' checked="checked"';
 9126:                         }
 9127:                     }
 9128:                 }
 9129:             } elsif (($item eq 'email') && ($hascustom)) {
 9130:                 if ($option eq 'custom') {
 9131:                     $checked = ' checked="checked"';
 9132:                 }
 9133:             } elsif ($option eq $defoption) {
 9134:                 $checked = ' checked="checked"';
 9135:             }
 9136:             $output .= '<span class="LC_nobreak"><label>'.
 9137:                        '<input type="radio" name="'.$name.'"'.
 9138:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 9139:                        $choices{$option}.'</label></span><br />';
 9140:             if ($item eq 'email') {
 9141:                 if ($option eq 'custom') {
 9142:                     my $id = 'cancreate_emailrule_'.$type;
 9143:                     my $display = 'none';
 9144:                     if ($checked) {
 9145:                         $display = 'inline';
 9146:                     }
 9147:                     my $numinrow = 2;
 9148:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 9149:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 9150:                                &user_formats_row('email',$settings,$emailrules,
 9151:                                                  $emailruleorder,$numinrow,'',$type);
 9152:                               '</table></fieldset>';
 9153:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 9154:                     my %text = &Apache::lonlocal::texthash (
 9155:                                                              inst    => 'must end:',
 9156:                                                              noninst => 'cannot end:',
 9157:                                                            );
 9158:                     my $value;
 9159:                     if (ref($emaildomain) eq 'HASH') {
 9160:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 9161:                             $value = $emaildomain->{$type}->{$option};
 9162:                         }
 9163:                     }
 9164:                     if ($value eq '') {
 9165:                         $value = '@'.$intdom;
 9166:                     }
 9167:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 9168:                     my $display = 'none';
 9169:                     if ($checked) {
 9170:                         $display = 'inline';
 9171:                     }
 9172:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 9173:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 9174:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 9175:                                '</div>';
 9176:                 }
 9177:             }
 9178:         }
 9179:         $output .= '</td>'."\n";
 9180:     }
 9181:     $output .= "</tr></table></td></tr>\n";
 9182:     return $output;
 9183: }
 9184: 
 9185: sub captcha_choice {
 9186:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 9187:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 9188:         $vertext,$currver);
 9189:     my %lt = &captcha_phrases();
 9190:     $keyentry = 'hidden';
 9191:     my $colspan=2;
 9192:     if ($context eq 'cancreate') {
 9193:         $rowname = &mt('CAPTCHA validation');
 9194:     } elsif ($context eq 'login') {
 9195:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 9196:     } elsif ($context eq 'passwords') {
 9197:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 9198:         $colspan=1;
 9199:     }
 9200:     if (ref($settings) eq 'HASH') {
 9201:         if ($settings->{'captcha'}) {
 9202:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 9203:         } else {
 9204:             $checked{'original'} = ' checked="checked"';
 9205:         }
 9206:         if ($settings->{'captcha'} eq 'recaptcha') {
 9207:             $pubtext = $lt{'pub'};
 9208:             $privtext = $lt{'priv'};
 9209:             $keyentry = 'text';
 9210:             $vertext = $lt{'ver'};
 9211:             $currver = $settings->{'recaptchaversion'};
 9212:             if ($currver ne '2') {
 9213:                 $currver = 1;
 9214:             }
 9215:         }
 9216:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 9217:             $currpub = $settings->{'recaptchakeys'}{'public'};
 9218:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 9219:         }
 9220:     } else {
 9221:         $checked{'original'} = ' checked="checked"';
 9222:     }
 9223:     my $css_class;
 9224:     if ($itemcount%2) {
 9225:         $css_class = 'LC_odd_row';
 9226:     }
 9227:     if ($customcss) {
 9228:         $css_class .= " $customcss";
 9229:     }
 9230:     $css_class =~ s/^\s+//;
 9231:     if ($css_class) {
 9232:         $css_class = ' class="'.$css_class.'"';
 9233:     }
 9234:     if ($rowstyle) {
 9235:         $css_class .= ' style="'.$rowstyle.'"';
 9236:     }
 9237:     my $output = '<tr'.$css_class.'>'.
 9238:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 9239:                  '<table><tr><td>'."\n";
 9240:     foreach my $option ('original','recaptcha','notused') {
 9241:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 9242:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 9243:                    $lt{$option}.'</label></span>';
 9244:         unless ($option eq 'notused') {
 9245:             $output .= ('&nbsp;'x2)."\n";
 9246:         }
 9247:     }
 9248: #
 9249: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 9250: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 9251: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 9252: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 9253: #
 9254:     $output .= '</td></tr>'."\n".
 9255:                '<tr><td class="LC_zero_height">'."\n".
 9256:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 9257:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 9258:                $currpub.'" size="40" /></span><br />'."\n".
 9259:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 9260:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 9261:                $currpriv.'" size="40" /></span><br />'.
 9262:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 9263:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 9264:                $currver.'" size="3" /></span><br />'.
 9265:                '</td></tr></table>'."\n".
 9266:                '</td></tr>';
 9267:     return $output;
 9268: }
 9269: 
 9270: sub user_formats_row {
 9271:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 9272:     my $output;
 9273:     my %text = (
 9274:                    'username' => 'new usernames',
 9275:                    'id'       => 'IDs',
 9276:                );
 9277:     unless (($type eq 'email') || ($type eq 'unamemap')) {
 9278:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 9279:         $output = '<tr '.$css_class.'>'.
 9280:                   '<td><span class="LC_nobreak">'.
 9281:                   &mt("Format rules to check for $text{$type}: ").
 9282:                   '</td><td class="LC_left_item" colspan="2"><table>';
 9283:     }
 9284:     my $rem;
 9285:     if (ref($ruleorder) eq 'ARRAY') {
 9286:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 9287:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 9288:                 my $rem = $i%($numinrow);
 9289:                 if ($rem == 0) {
 9290:                     if ($i > 0) {
 9291:                         $output .= '</tr>';
 9292:                     }
 9293:                     $output .= '<tr>';
 9294:                 }
 9295:                 my $check = ' ';
 9296:                 if (ref($settings) eq 'HASH') {
 9297:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 9298:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 9299:                             $check = ' checked="checked" ';
 9300:                         }
 9301:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 9302:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 9303:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 9304:                                 $check = ' checked="checked" ';
 9305:                             }
 9306:                         }
 9307:                     }
 9308:                 }
 9309:                 my $name = $type.'_rule';
 9310:                 if ($type eq 'email') {
 9311:                     $name .= '_'.$status;
 9312:                 }
 9313:                 $output .= '<td class="LC_left_item">'.
 9314:                            '<span class="LC_nobreak"><label>'.
 9315:                            '<input type="checkbox" name="'.$name.'" '.
 9316:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 9317:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 9318:             }
 9319:         }
 9320:         $rem = @{$ruleorder}%($numinrow);
 9321:     }
 9322:     my $colsleft;
 9323:     if ($rem) {
 9324:         $colsleft = $numinrow - $rem;
 9325:     }
 9326:     if ($colsleft > 1 ) {
 9327:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9328:                    '&nbsp;</td>';
 9329:     } elsif ($colsleft == 1) {
 9330:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 9331:     }
 9332:     $output .= '</tr>';
 9333:     unless (($type eq 'email') || ($type eq 'unamemap')) {
 9334:         $output .= '</table></td></tr>';
 9335:     }
 9336:     return $output;
 9337: }
 9338: 
 9339: sub usercreation_types {
 9340:     my %lt = &Apache::lonlocal::texthash (
 9341:                     author     => 'When adding a co-author',
 9342:                     course     => 'When adding a user to a course',
 9343:                     requestcrs => 'When requesting a course',
 9344:                     any        => 'Any',
 9345:                     official   => 'Institutional only ',
 9346:                     unofficial => 'Non-institutional only',
 9347:                     none       => 'None',
 9348:     );
 9349:     return %lt;
 9350: }
 9351: 
 9352: sub selfcreation_types {
 9353:     my %lt = &Apache::lonlocal::texthash (
 9354:                     selfcreate => 'User creates own account',
 9355:                     any        => 'Any',
 9356:                     official   => 'Institutional only ',
 9357:                     unofficial => 'Non-institutional only',
 9358:                     email      => 'E-mail address',
 9359:                     login      => 'Institutional Login',
 9360:                     sso        => 'SSO',
 9361:              );
 9362: }
 9363: 
 9364: sub authtype_names {
 9365:     my %lt = &Apache::lonlocal::texthash(
 9366:                       int    => 'Internal',
 9367:                       krb4   => 'Kerberos 4',
 9368:                       krb5   => 'Kerberos 5',
 9369:                       loc    => 'Local',
 9370:                       lti    => 'LTI',
 9371:                   );
 9372:     return %lt;
 9373: }
 9374: 
 9375: sub context_names {
 9376:     my %context_title = &Apache::lonlocal::texthash(
 9377:        author => 'Creating users when an Author',
 9378:        course => 'Creating users when in a course',
 9379:        domain => 'Creating users when a Domain Coordinator',
 9380:     );
 9381:     return %context_title;
 9382: }
 9383: 
 9384: sub print_usermodification {
 9385:     my ($position,$dom,$settings,$rowtotal) = @_;
 9386:     my $numinrow = 4;
 9387:     my ($context,$datatable,$rowcount);
 9388:     if ($position eq 'top') {
 9389:         $rowcount = 0;
 9390:         $context = 'author'; 
 9391:         foreach my $role ('ca','aa') {
 9392:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 9393:                                                    $numinrow,$rowcount);
 9394:             $$rowtotal ++;
 9395:             $rowcount ++;
 9396:         }
 9397:     } elsif ($position eq 'bottom') {
 9398:         $context = 'course';
 9399:         $rowcount = 0;
 9400:         foreach my $role ('st','ep','ta','in','cr') {
 9401:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 9402:                                                    $numinrow,$rowcount);
 9403:             $$rowtotal ++;
 9404:             $rowcount ++;
 9405:         }
 9406:     }
 9407:     return $datatable;
 9408: }
 9409: 
 9410: sub print_defaults {
 9411:     my ($position,$dom,$settings,$rowtotal) = @_;
 9412:     my $rownum = 0;
 9413:     my ($datatable,$css_class,$titles);
 9414:     unless ($position eq 'bottom') {
 9415:         $titles = &defaults_titles($dom);
 9416:     }
 9417:     if ($position eq 'top') {
 9418:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 9419:                      'datelocale_def','portal_def');
 9420:         my %defaults;
 9421:         if (ref($settings) eq 'HASH') {
 9422:             %defaults = %{$settings};
 9423:         } else {
 9424:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9425:             foreach my $item (@items) {
 9426:                 $defaults{$item} = $domdefaults{$item};
 9427:             }
 9428:         }
 9429:         foreach my $item (@items) {
 9430:             if ($rownum%2) {
 9431:                 $css_class = '';
 9432:             } else {
 9433:                 $css_class = ' class="LC_odd_row" ';
 9434:             }
 9435:             $datatable .= '<tr'.$css_class.'>'.
 9436:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 9437:                           '</span></td><td class="LC_right_item" colspan="3">';
 9438:             if ($item eq 'auth_def') {
 9439:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
 9440:                 my %shortauth = (
 9441:                                  internal => 'int',
 9442:                                  krb4 => 'krb4',
 9443:                                  krb5 => 'krb5',
 9444:                                  localauth  => 'loc',
 9445:                                  lti => 'lti',
 9446:                                 );
 9447:                 my %authnames = &authtype_names();
 9448:                 foreach my $auth (@authtypes) {
 9449:                     my $checked = ' ';
 9450:                     if ($defaults{$item} eq $auth) {
 9451:                         $checked = ' checked="checked" ';
 9452:                     }
 9453:                     $datatable .= '<label><input type="radio" name="'.$item.
 9454:                                   '" value="'.$auth.'"'.$checked.'/>'.
 9455:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 9456:                 }
 9457:             } elsif ($item eq 'timezone_def') {
 9458:                 my $includeempty = 1;
 9459:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 9460:             } elsif ($item eq 'datelocale_def') {
 9461:                 my $includeempty = 1;
 9462:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 9463:             } elsif ($item eq 'lang_def') {
 9464:                 my $includeempty = 1;
 9465:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 9466:             } elsif ($item eq 'portal_def') {
 9467:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 9468:                               $defaults{$item}.'" size="25" onkeyup="portalExtras(this);" />';
 9469:                 my $portalsty = 'none';
 9470:                 if ($defaults{$item}) {
 9471:                     $portalsty = 'block';
 9472:                 }
 9473:                 foreach my $field ('email','web') {
 9474:                     my $checkedoff = ' checked="checked"';
 9475:                     my $checkedon;
 9476:                     if ($defaults{$item.'_'.$field}) {
 9477:                         $checkedon = $checkedoff;
 9478:                         $checkedoff = '';
 9479:                     }
 9480:                     $datatable .= '<div id="'.$item.'_'.$field.'_div" style="display:'.$portalsty.'">'.
 9481:                               '<span class="LC_nobreak">'.$titles->{$field}.'&nbsp;'.
 9482:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="1"'.$checkedon.'/>'.&mt('Yes').'</label>'.
 9483:                               ('&nbsp;'x2).
 9484:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="0"'.$checkedoff.'/>'.&mt('No').'</label>'.
 9485:                               '</div>';
 9486:                 }
 9487:             } else {
 9488:                 $datatable .= '<input type="text" name="'.$item.'" value="'.$defaults{$item}.'" />';
 9489:             }
 9490:             $datatable .= '</td></tr>';
 9491:             $rownum ++;
 9492:         }
 9493:     } elsif ($position eq 'middle') {
 9494:         my %defaults;
 9495:         if (ref($settings) eq 'HASH') {
 9496:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 9497:                 my $maxnum = @{$settings->{'inststatusorder'}};
 9498:                 for (my $i=0; $i<$maxnum; $i++) {
 9499:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 9500:                     my $item = $settings->{'inststatusorder'}->[$i];
 9501:                     my $title = $settings->{'inststatustypes'}->{$item};
 9502:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 9503:                     $datatable .= '<tr'.$css_class.'>'.
 9504:                                   '<td><span class="LC_nobreak">'.
 9505:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 9506:                     for (my $k=0; $k<=$maxnum; $k++) {
 9507:                         my $vpos = $k+1;
 9508:                         my $selstr;
 9509:                         if ($k == $i) {
 9510:                             $selstr = ' selected="selected" ';
 9511:                         }
 9512:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9513:                     }
 9514:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 9515:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 9516:                                   &mt('delete').'</span></td>'.
 9517:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed').':'.
 9518:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 9519:                                   '</span></td></tr>';
 9520:                 }
 9521:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 9522:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 9523:                 $datatable .= '<tr '.$css_class.'>'.
 9524:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 9525:                 for (my $k=0; $k<=$maxnum; $k++) {
 9526:                     my $vpos = $k+1;
 9527:                     my $selstr;
 9528:                     if ($k == $maxnum) {
 9529:                         $selstr = ' selected="selected" ';
 9530:                     }
 9531:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9532:                 }
 9533:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 9534:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 9535:                               '&nbsp;'.&mt('(new)').
 9536:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 9537:                               &mt('Name displayed').':'.
 9538:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 9539:                               '</tr>'."\n";
 9540:                 $rownum ++;
 9541:             }
 9542:         }
 9543:     } else {
 9544:         my ($unamemaprules,$ruleorder) =
 9545:             &Apache::lonnet::inst_userrules($dom,'unamemap');
 9546:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 9547:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
 9548:             my $numinrow = 2;
 9549:             $datatable .= '<tr'.$css_class.'><td>'.&mt('Available conversions').'</td><td><table>'.
 9550:                           &user_formats_row('unamemap',$settings,$unamemaprules,
 9551:                                             $ruleorder,$numinrow).
 9552:                           '</table></td></tr>';
 9553:         }
 9554:         if ($datatable eq '') {
 9555:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 9556:                           &mt('No rules set for domain in customized localenroll.pm').
 9557:                           '</td></tr>';
 9558:         }
 9559:     }
 9560:     $$rowtotal += $rownum;
 9561:     return $datatable;
 9562: }
 9563: 
 9564: sub get_languages_hash {
 9565:     my %langchoices;
 9566:     foreach my $id (&Apache::loncommon::languageids()) {
 9567:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 9568:         if ($code ne '') {
 9569:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 9570:         }
 9571:     }
 9572:     return %langchoices;
 9573: }
 9574: 
 9575: sub defaults_titles {
 9576:     my ($dom) = @_;
 9577:     my %titles = &Apache::lonlocal::texthash (
 9578:                    'auth_def'      => 'Default authentication type',
 9579:                    'auth_arg_def'  => 'Default authentication argument',
 9580:                    'lang_def'      => 'Default language',
 9581:                    'timezone_def'  => 'Default timezone',
 9582:                    'datelocale_def' => 'Default locale for dates',
 9583:                    'portal_def'     => 'Portal/Default URL',
 9584:                    'email'          => 'Email links use portal URL',
 9585:                    'web'            => 'Public web links use portal URL',
 9586:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 9587:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 9588:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 9589:                  );
 9590:     if ($dom) {
 9591:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 9592:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 9593:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 9594:         $protocol = 'http' if ($protocol ne 'https');
 9595:         if ($uint_dom) {
 9596:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 9597:                                          $uint_dom);
 9598:         }
 9599:     }
 9600:     return (\%titles);
 9601: }
 9602: 
 9603: sub print_scantron {
 9604:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 9605:     if ($position eq 'top') {
 9606:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 9607:     } else {
 9608:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 9609:     }
 9610: }
 9611: 
 9612: sub scantron_javascript {
 9613:     return <<"ENDSCRIPT";
 9614: 
 9615: <script type="text/javascript">
 9616: // <![CDATA[
 9617: 
 9618: function toggleScantron(form) {
 9619:     var csvfieldset = new Array();
 9620:     if (document.getElementById('scantroncsv_cols')) {
 9621:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 9622:     }
 9623:     if (document.getElementById('scantroncsv_options')) {
 9624:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 9625:     }
 9626:     if (csvfieldset.length) {
 9627:         if (document.getElementById('scantronconfcsv')) {
 9628:             var scantroncsv = document.getElementById('scantronconfcsv');
 9629:             if (scantroncsv.checked) {
 9630:                 for (var i=0; i<csvfieldset.length; i++) {
 9631:                     csvfieldset[i].style.display = 'block';
 9632:                 }
 9633:             } else {
 9634:                 for (var i=0; i<csvfieldset.length; i++) {
 9635:                     csvfieldset[i].style.display = 'none';
 9636:                 }
 9637:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 9638:                 if (csvselects.length) {
 9639:                     for (var j=0; j<csvselects.length; j++) {
 9640:                         csvselects[j].selectedIndex = 0;
 9641:                     }
 9642:                 }
 9643:             }
 9644:         }
 9645:     }
 9646:     return;
 9647: }
 9648: // ]]>
 9649: </script>
 9650: 
 9651: ENDSCRIPT
 9652: 
 9653: }
 9654: 
 9655: sub print_scantronformat {
 9656:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 9657:     my $itemcount = 1;
 9658:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 9659:         %confhash);
 9660:     my $switchserver = &check_switchserver($dom,$confname);
 9661:     my %lt = &Apache::lonlocal::texthash (
 9662:                 default => 'Default bubblesheet format file error',
 9663:                 custom  => 'Custom bubblesheet format file error',
 9664:              );
 9665:     my %scantronfiles = (
 9666:         default => 'default.tab',
 9667:         custom => 'custom.tab',
 9668:     );
 9669:     foreach my $key (keys(%scantronfiles)) {
 9670:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 9671:                               .$scantronfiles{$key};
 9672:     }
 9673:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 9674:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 9675:         if (!$switchserver) {
 9676:             my $servadm = $r->dir_config('lonAdmEMail');
 9677:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 9678:             if ($configuserok eq 'ok') {
 9679:                 if ($author_ok eq 'ok') {
 9680:                     my %legacyfile = (
 9681:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 9682:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 9683:                     );
 9684:                     my %md5chk;
 9685:                     foreach my $type (keys(%legacyfile)) {
 9686:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 9687:                         chomp($md5chk{$type});
 9688:                     }
 9689:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 9690:                         foreach my $type (keys(%legacyfile)) {
 9691:                             ($scantronurls{$type},my $error) =
 9692:                                 &legacy_scantronformat($r,$dom,$confname,
 9693:                                                  $type,$legacyfile{$type},
 9694:                                                  $scantronurls{$type},
 9695:                                                  $scantronfiles{$type});
 9696:                             if ($error ne '') {
 9697:                                 $error{$type} = $error;
 9698:                             }
 9699:                         }
 9700:                         if (keys(%error) == 0) {
 9701:                             $is_custom = 1;
 9702:                             $confhash{'scantron'}{'scantronformat'} =
 9703:                                 $scantronurls{'custom'};
 9704:                             my $putresult =
 9705:                                 &Apache::lonnet::put_dom('configuration',
 9706:                                                          \%confhash,$dom);
 9707:                             if ($putresult ne 'ok') {
 9708:                                 $error{'custom'} =
 9709:                                     '<span class="LC_error">'.
 9710:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 9711:                             }
 9712:                         }
 9713:                     } else {
 9714:                         ($scantronurls{'default'},my $error) =
 9715:                             &legacy_scantronformat($r,$dom,$confname,
 9716:                                           'default',$legacyfile{'default'},
 9717:                                           $scantronurls{'default'},
 9718:                                           $scantronfiles{'default'});
 9719:                         if ($error eq '') {
 9720:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 9721:                             my $putresult =
 9722:                                 &Apache::lonnet::put_dom('configuration',
 9723:                                                          \%confhash,$dom);
 9724:                             if ($putresult ne 'ok') {
 9725:                                 $error{'default'} =
 9726:                                     '<span class="LC_error">'.
 9727:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 9728:                             }
 9729:                         } else {
 9730:                             $error{'default'} = $error;
 9731:                         }
 9732:                     }
 9733:                 }
 9734:             }
 9735:         } else {
 9736:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 9737:         }
 9738:     }
 9739:     if (ref($settings) eq 'HASH') {
 9740:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 9741:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 9742:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 9743:                 $scantronurl = '';
 9744:             } else {
 9745:                 $scantronurl = $settings->{'scantronformat'};
 9746:             }
 9747:             $is_custom = 1;
 9748:         } else {
 9749:             $scantronurl = $scantronurls{'default'};
 9750:         }
 9751:     } else {
 9752:         if ($is_custom) {
 9753:             $scantronurl = $scantronurls{'custom'};
 9754:         } else {
 9755:             $scantronurl = $scantronurls{'default'};
 9756:         }
 9757:     }
 9758:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9759:     $datatable .= '<tr'.$css_class.'>';
 9760:     if (!$is_custom) {
 9761:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 9762:                       '<span class="LC_nobreak">';
 9763:         if ($scantronurl) {
 9764:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 9765:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 9766:         } else {
 9767:             $datatable = &mt('File unavailable for display');
 9768:         }
 9769:         $datatable .= '</span></td>';
 9770:         if (keys(%error) == 0) { 
 9771:             $datatable .= '<td valign="bottom">';
 9772:             if (!$switchserver) {
 9773:                 $datatable .= &mt('Upload:').'<br />';
 9774:             }
 9775:         } else {
 9776:             my $errorstr;
 9777:             foreach my $key (sort(keys(%error))) {
 9778:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 9779:             }
 9780:             $datatable .= '<td>'.$errorstr;
 9781:         }
 9782:     } else {
 9783:         if (keys(%error) > 0) {
 9784:             my $errorstr;
 9785:             foreach my $key (sort(keys(%error))) {
 9786:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 9787:             } 
 9788:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 9789:         } elsif ($scantronurl) {
 9790:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 9791:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 9792:             $datatable .= '<td><span class="LC_nobreak">'.
 9793:                           $link.
 9794:                           '<label><input type="checkbox" name="scantronformat_del"'.
 9795:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 9796:                           '<td><span class="LC_nobreak">&nbsp;'.
 9797:                           &mt('Replace:').'</span><br />';
 9798:         }
 9799:     }
 9800:     if (keys(%error) == 0) {
 9801:         if ($switchserver) {
 9802:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 9803:         } else {
 9804:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 9805:                          '<input type="file" name="scantronformat" /></span>';
 9806:         }
 9807:     }
 9808:     $datatable .= '</td></tr>';
 9809:     $$rowtotal ++;
 9810:     return $datatable;
 9811: }
 9812: 
 9813: sub legacy_scantronformat {
 9814:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 9815:     my ($url,$error);
 9816:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 9817:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 9818:         my $modified = [];
 9819:         (my $result,$url) =
 9820:             &Apache::lonconfigsettings::publishlogo($r,'copy',$legacyfile,$dom,$confname,
 9821:                                                     'scantron','','',$newfile,$modified);
 9822:         if ($result eq 'ok') {
 9823:             &update_modify_urls($r,$modified);
 9824:         } else {
 9825:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 9826:         }
 9827:     }
 9828:     return ($url,$error);
 9829: }
 9830: 
 9831: sub print_scantronconfig {
 9832:     my ($dom,$settings,$rowtotal) = @_;
 9833:     my $itemcount = 2;
 9834:     my $is_checked = ' checked="checked"';
 9835:     my %optionson = (
 9836:                      hdr => ' checked="checked"',
 9837:                      pad => ' checked="checked"',
 9838:                      rem => ' checked="checked"',
 9839:                     );
 9840:     my %optionsoff = (
 9841:                       hdr => '',
 9842:                       pad => '',
 9843:                       rem => '',
 9844:                      );
 9845:     my $currcsvsty = 'none';
 9846:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 9847:     my @fields = &scantroncsv_fields();
 9848:     my %titles = &scantronconfig_titles();
 9849:     if (ref($settings) eq 'HASH') {
 9850:         if (ref($settings->{config}) eq 'HASH') {
 9851:             if ($settings->{config}->{dat}) {
 9852:                 $checked{'dat'} = $is_checked;
 9853:             }
 9854:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 9855:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 9856:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 9857:                     if (keys(%csvfields) > 0) {
 9858:                         $checked{'csv'} = $is_checked;
 9859:                         $currcsvsty = 'block';
 9860:                     }
 9861:                 }
 9862:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 9863:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 9864:                     foreach my $option (keys(%optionson)) {
 9865:                         unless ($csvoptions{$option}) {
 9866:                             $optionsoff{$option} = $optionson{$option};
 9867:                             $optionson{$option} = '';
 9868:                         }
 9869:                     }
 9870:                 }
 9871:             }
 9872:         } else {
 9873:             $checked{'dat'} = $is_checked;
 9874:         }
 9875:     } else {
 9876:         $checked{'dat'} = $is_checked;
 9877:     }
 9878:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 9879:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 9880:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 9881:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 9882:     foreach my $item ('dat','csv') {
 9883:         my $id;
 9884:         if ($item eq 'csv') {
 9885:             $id = 'id="scantronconfcsv" ';
 9886:         }
 9887:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 9888:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 9889:         if ($item eq 'csv') {
 9890:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 9891:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 9892:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 9893:             foreach my $col (@fields) {
 9894:                 my $selnone;
 9895:                 if ($csvfields{$col} eq '') {
 9896:                     $selnone = ' selected="selected"';
 9897:                 }
 9898:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 9899:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 9900:                               '<option value=""'.$selnone.'></option>';
 9901:                 for (my $i=0; $i<20; $i++) {
 9902:                     my $shown = $i+1;
 9903:                     my $sel;
 9904:                     unless ($selnone) {
 9905:                         if (exists($csvfields{$col})) {
 9906:                             if ($csvfields{$col} == $i) {
 9907:                                 $sel = ' selected="selected"';
 9908:                             }
 9909:                         }
 9910:                     }
 9911:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 9912:                 }
 9913:                 $datatable .= '</select></td></tr>';
 9914:            }
 9915:            $datatable .= '</table></fieldset>'.
 9916:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 9917:                          '<legend>'.&mt('CSV Options').'</legend>';
 9918:            foreach my $option ('hdr','pad','rem') {
 9919:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 9920:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 9921:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 9922:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 9923:            }
 9924:            $datatable .= '</fieldset>';
 9925:            $itemcount ++;
 9926:         }
 9927:     }
 9928:     $datatable .= '</td></tr>';
 9929:     $$rowtotal ++;
 9930:     return $datatable;
 9931: }
 9932: 
 9933: sub scantronconfig_titles {
 9934:     return &Apache::lonlocal::texthash(
 9935:                                           dat => 'Standard format (.dat)',
 9936:                                           csv => 'Comma separated values (.csv)',
 9937:                                           hdr => 'Remove first line in file (contains column titles)',
 9938:                                           pad => 'Prepend 0s to PaperID',
 9939:                                           rem => 'Remove leading spaces (except Question Response columns)',
 9940:                                           CODE => 'CODE',
 9941:                                           ID   => 'Student ID',
 9942:                                           PaperID => 'Paper ID',
 9943:                                           FirstName => 'First Name',
 9944:                                           LastName => 'Last Name',
 9945:                                           FirstQuestion => 'First Question Response',
 9946:                                           Section => 'Section',
 9947:     );
 9948: }
 9949: 
 9950: sub scantroncsv_fields {
 9951:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 9952: }
 9953: 
 9954: sub print_coursecategories {
 9955:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 9956:     my $datatable;
 9957:     if ($position eq 'top') {
 9958:         my (%checked);
 9959:         my @catitems = ('unauth','auth');
 9960:         my @cattypes = ('std','domonly','codesrch','none');
 9961:         $checked{'unauth'} = 'std';
 9962:         $checked{'auth'} = 'std';
 9963:         if (ref($settings) eq 'HASH') {
 9964:             foreach my $type (@cattypes) {
 9965:                 if ($type eq $settings->{'unauth'}) {
 9966:                     $checked{'unauth'} = $type;
 9967:                 }
 9968:                 if ($type eq $settings->{'auth'}) {
 9969:                     $checked{'auth'} = $type;
 9970:                 }
 9971:             }
 9972:         }
 9973:         my %lt = &Apache::lonlocal::texthash (
 9974:                                                unauth   => 'Catalog type for unauthenticated users',
 9975:                                                auth     => 'Catalog type for authenticated users',
 9976:                                                none     => 'No catalog',
 9977:                                                std      => 'Standard catalog',
 9978:                                                domonly  => 'Domain-only catalog',
 9979:                                                codesrch => "Code search form",
 9980:                                              );
 9981:        my $itemcount = 0;
 9982:        foreach my $item (@catitems) {
 9983:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 9984:            $datatable .= '<tr '.$css_class.'>'.
 9985:                          '<td>'.$lt{$item}.'</td>'.
 9986:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 9987:            foreach my $type (@cattypes) {
 9988:                my $ischecked;
 9989:                if ($checked{$item} eq $type) {
 9990:                    $ischecked=' checked="checked"';
 9991:                }
 9992:                $datatable .= '<label>'.
 9993:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 9994:                              ' />'.$lt{$type}.'</label>&nbsp;';
 9995:            }
 9996:            $datatable .= '</span></td></tr>';
 9997:            $itemcount ++;
 9998:         }
 9999:         $$rowtotal += $itemcount;
10000:     } elsif ($position eq 'middle') {
10001:         my $toggle_cats_crs = ' ';
10002:         my $toggle_cats_dom = ' checked="checked" ';
10003:         my $can_cat_crs = ' ';
10004:         my $can_cat_dom = ' checked="checked" ';
10005:         my $toggle_catscomm_comm = ' ';
10006:         my $toggle_catscomm_dom = ' checked="checked" ';
10007:         my $can_catcomm_comm = ' ';
10008:         my $can_catcomm_dom = ' checked="checked" ';
10009: 
10010:         if (ref($settings) eq 'HASH') {
10011:             if ($settings->{'togglecats'} eq 'crs') {
10012:                 $toggle_cats_crs = $toggle_cats_dom;
10013:                 $toggle_cats_dom = ' ';
10014:             }
10015:             if ($settings->{'categorize'} eq 'crs') {
10016:                 $can_cat_crs = $can_cat_dom;
10017:                 $can_cat_dom = ' ';
10018:             }
10019:             if ($settings->{'togglecatscomm'} eq 'comm') {
10020:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
10021:                 $toggle_catscomm_dom = ' ';
10022:             }
10023:             if ($settings->{'categorizecomm'} eq 'comm') {
10024:                 $can_catcomm_comm = $can_catcomm_dom;
10025:                 $can_catcomm_dom = ' ';
10026:             }
10027:         }
10028:         my %title = &Apache::lonlocal::texthash (
10029:                      togglecats     => 'Show/Hide a course in catalog',
10030:                      togglecatscomm => 'Show/Hide a community in catalog',
10031:                      categorize     => 'Assign a category to a course',
10032:                      categorizecomm => 'Assign a category to a community',
10033:                     );
10034:         my %level = &Apache::lonlocal::texthash (
10035:                      dom  => 'Set in Domain',
10036:                      crs  => 'Set in Course',
10037:                      comm => 'Set in Community',
10038:                     );
10039:         $datatable = '<tr class="LC_odd_row">'.
10040:                   '<td>'.$title{'togglecats'}.'</td>'.
10041:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10042:                   '<input type="radio" name="togglecats"'.
10043:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10044:                   '<label><input type="radio" name="togglecats"'.
10045:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
10046:                   '</tr><tr>'.
10047:                   '<td>'.$title{'categorize'}.'</td>'.
10048:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10049:                   '<label><input type="radio" name="categorize"'.
10050:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10051:                   '<label><input type="radio" name="categorize"'.
10052:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
10053:                   '</tr><tr class="LC_odd_row">'.
10054:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
10055:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10056:                   '<input type="radio" name="togglecatscomm"'.
10057:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10058:                   '<label><input type="radio" name="togglecatscomm"'.
10059:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
10060:                   '</tr><tr>'.
10061:                   '<td>'.$title{'categorizecomm'}.'</td>'.
10062:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10063:                   '<label><input type="radio" name="categorizecomm"'.
10064:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10065:                   '<label><input type="radio" name="categorizecomm"'.
10066:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
10067:                   '</tr>';
10068:         $$rowtotal += 4;
10069:     } else {
10070:         my $css_class;
10071:         my $itemcount = 1;
10072:         my $cathash; 
10073:         if (ref($settings) eq 'HASH') {
10074:             $cathash = $settings->{'cats'};
10075:         }
10076:         if (ref($cathash) eq 'HASH') {
10077:             my (@cats,@trails,%allitems,%idx,@jsarray);
10078:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
10079:                                                    \%allitems,\%idx,\@jsarray);
10080:             my $maxdepth = scalar(@cats);
10081:             my $colattrib = '';
10082:             if ($maxdepth > 2) {
10083:                 $colattrib = ' colspan="2" ';
10084:             }
10085:             my @path;
10086:             if (@cats > 0) {
10087:                 if (ref($cats[0]) eq 'ARRAY') {
10088:                     my $numtop = @{$cats[0]};
10089:                     my $maxnum = $numtop;
10090:                     my %default_names = (
10091:                           instcode    => &mt('Official courses'),
10092:                           communities => &mt('Communities'),
10093:                     );
10094: 
10095:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
10096:                         ($cathash->{'instcode::0'} eq '') ||
10097:                         (!grep(/^communities$/,@{$cats[0]})) || 
10098:                         ($cathash->{'communities::0'} eq '')) {
10099:                         $maxnum ++;
10100:                     }
10101:                     my $lastidx;
10102:                     for (my $i=0; $i<$numtop; $i++) {
10103:                         my $parent = $cats[0][$i];
10104:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
10105:                         my $item = &escape($parent).'::0';
10106:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
10107:                         $lastidx = $idx{$item};
10108:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10109:                                       .'<select name="'.$item.'"'.$chgstr.'>';
10110:                         for (my $k=0; $k<=$maxnum; $k++) {
10111:                             my $vpos = $k+1;
10112:                             my $selstr;
10113:                             if ($k == $i) {
10114:                                 $selstr = ' selected="selected" ';
10115:                             }
10116:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10117:                         }
10118:                         $datatable .= '</select></span></td><td>';
10119:                         if ($parent eq 'instcode' || $parent eq 'communities') {
10120:                             $datatable .=  '<span class="LC_nobreak">'
10121:                                            .$default_names{$parent}.'</span>';
10122:                             if ($parent eq 'instcode') {
10123:                                 $datatable .= '<br /><span class="LC_nobreak">('
10124:                                               .&mt('with institutional codes')
10125:                                               .')</span></td><td'.$colattrib.'>';
10126:                             } else {
10127:                                 $datatable .= '<table><tr><td>';
10128:                             }
10129:                             $datatable .= '<span class="LC_nobreak">'
10130:                                           .'<label><input type="radio" name="'
10131:                                           .$parent.'" value="1" checked="checked" />'
10132:                                           .&mt('Display').'</label>';
10133:                             if ($parent eq 'instcode') {
10134:                                 $datatable .= '&nbsp;';
10135:                             } else {
10136:                                 $datatable .= '</span></td></tr><tr><td>'
10137:                                               .'<span class="LC_nobreak">';
10138:                             }
10139:                             $datatable .= '<label><input type="radio" name="'
10140:                                           .$parent.'" value="0" />'
10141:                                           .&mt('Do not display').'</label></span>';
10142:                             if ($parent eq 'communities') {
10143:                                 $datatable .= '</td></tr></table>';
10144:                             }
10145:                             $datatable .= '</td>';
10146:                         } else {
10147:                             $datatable .= $parent
10148:                                           .'&nbsp;<span class="LC_nobreak"><label>'
10149:                                           .'<input type="checkbox" name="deletecategory" '
10150:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
10151:                         }
10152:                         my $depth = 1;
10153:                         push(@path,$parent);
10154:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
10155:                         pop(@path);
10156:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
10157:                         $itemcount ++;
10158:                     }
10159:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10160:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
10161:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
10162:                     for (my $k=0; $k<=$maxnum; $k++) {
10163:                         my $vpos = $k+1;
10164:                         my $selstr;
10165:                         if ($k == $numtop) {
10166:                             $selstr = ' selected="selected" ';
10167:                         }
10168:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10169:                     }
10170:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
10171:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
10172:                                   .'</tr>'."\n";
10173:                     $itemcount ++;
10174:                     foreach my $default ('instcode','communities') {
10175:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
10176:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
10177:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
10178:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
10179:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
10180:                             for (my $k=0; $k<=$maxnum; $k++) {
10181:                                 my $vpos = $k+1;
10182:                                 my $selstr;
10183:                                 if ($k == $maxnum) {
10184:                                     $selstr = ' selected="selected" ';
10185:                                 }
10186:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10187:                             }
10188:                             $datatable .= '</select></span></td>'.
10189:                                           '<td><span class="LC_nobreak">'.
10190:                                           $default_names{$default}.'</span>';
10191:                             if ($default eq 'instcode') {
10192:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
10193:                                               .&mt('with institutional codes').')</span>';
10194:                             }
10195:                             $datatable .= '</td>'
10196:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
10197:                                           .&mt('Display').'</label>&nbsp;'
10198:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
10199:                                           .&mt('Do not display').'</label></span></td></tr>';
10200:                         }
10201:                     }
10202:                 }
10203:             } else {
10204:                 $datatable .= &initialize_categories($itemcount);
10205:             }
10206:         } else {
10207:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
10208:                           .&initialize_categories($itemcount);
10209:         }
10210:         $$rowtotal += $itemcount;
10211:     }
10212:     return $datatable;
10213: }
10214: 
10215: sub print_serverstatuses {
10216:     my ($dom,$settings,$rowtotal) = @_;
10217:     my $datatable;
10218:     my @pages = &serverstatus_pages();
10219:     my (%namedaccess,%machineaccess);
10220:     foreach my $type (@pages) {
10221:         $namedaccess{$type} = '';
10222:         $machineaccess{$type}= '';
10223:     }
10224:     if (ref($settings) eq 'HASH') {
10225:         foreach my $type (@pages) {
10226:             if (exists($settings->{$type})) {
10227:                 if (ref($settings->{$type}) eq 'HASH') {
10228:                     foreach my $key (keys(%{$settings->{$type}})) {
10229:                         if ($key eq 'namedusers') {
10230:                             $namedaccess{$type} = $settings->{$type}->{$key};
10231:                         } elsif ($key eq 'machines') {
10232:                             $machineaccess{$type} = $settings->{$type}->{$key};
10233:                         }
10234:                     }
10235:                 }
10236:             }
10237:         }
10238:     }
10239:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
10240:     my $rownum = 0;
10241:     my $css_class;
10242:     foreach my $type (@pages) {
10243:         $rownum ++;
10244:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10245:         $datatable .= '<tr'.$css_class.'>'.
10246:                       '<td><span class="LC_nobreak">'.
10247:                       $titles->{$type}.'</span></td>'.
10248:                       '<td class="LC_left_item">'.
10249:                       '<input type="text" name="'.$type.'_namedusers" '.
10250:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
10251:                       '<td class="LC_right_item">'.
10252:                       '<span class="LC_nobreak">'.
10253:                       '<input type="text" name="'.$type.'_machines" '.
10254:                       'value="'.$machineaccess{$type}.'" size="10" />'.
10255:                       '</span></td></tr>'."\n";
10256:     }
10257:     $$rowtotal += $rownum;
10258:     return $datatable;
10259: }
10260: 
10261: sub serverstatus_pages {
10262:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
10263:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
10264:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
10265:             'uniquecodes','diskusage','coursecatalog');
10266: }
10267: 
10268: sub defaults_javascript {
10269:     my ($settings) = @_;
10270:     return unless (ref($settings) eq 'HASH');
10271:     my $portal_js = <<"ENDPORTAL";
10272: 
10273: function portalExtras(caller) {
10274:     var x = caller.value;
10275:     var y = new Array('email','web');
10276:     for (var i=0; i<y.length; i++) {
10277:         if (document.getElementById('portal_def_'+y[i]+'_div')) {
10278:             var z = document.getElementById('portal_def_'+y[i]+'_div');
10279:             if (x.length > 0) {
10280:                 z.style.display = 'block';
10281:             } else {
10282:                 z.style.display = 'none';
10283:             }
10284:         }
10285:     }
10286: }
10287: ENDPORTAL
10288:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10289:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
10290:         if ($maxnum eq '') {
10291:             $maxnum = 0;
10292:         }
10293:         $maxnum ++;
10294:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
10295:         return <<"ENDSCRIPT";
10296: <script type="text/javascript">
10297: // <![CDATA[
10298: function reorderTypes(form,caller) {
10299:     var changedVal;
10300: $jstext 
10301:     var newpos = 'addinststatus_pos';
10302:     var current = new Array;
10303:     var maxh = $maxnum;
10304:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
10305:     var oldVal;
10306:     if (caller == newpos) {
10307:         changedVal = newitemVal;
10308:     } else {
10309:         var curritem = 'inststatus_pos_'+caller;
10310:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
10311:         current[newitemVal] = newpos;
10312:     }
10313:     for (var i=0; i<inststatuses.length; i++) {
10314:         if (inststatuses[i] != caller) {
10315:             var elementName = 'inststatus_pos_'+inststatuses[i];
10316:             if (form.elements[elementName]) {
10317:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
10318:                 current[currVal] = elementName;
10319:             }
10320:         }
10321:     }
10322:     for (var j=0; j<maxh; j++) {
10323:         if (current[j] == undefined) {
10324:             oldVal = j;
10325:         }
10326:     }
10327:     if (oldVal < changedVal) {
10328:         for (var k=oldVal+1; k<=changedVal ; k++) {
10329:            var elementName = current[k];
10330:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
10331:         }
10332:     } else {
10333:         for (var k=changedVal; k<oldVal; k++) {
10334:             var elementName = current[k];
10335:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
10336:         }
10337:     }
10338:     return;
10339: }
10340: 
10341: $portal_js
10342: 
10343: // ]]>
10344: </script>
10345: 
10346: ENDSCRIPT
10347:     } else {
10348: return <<"ENDSCRIPT";
10349: <script type="text/javascript">
10350: // <![CDATA[
10351: $portal_js
10352: // ]]>
10353: </script>
10354: 
10355: ENDSCRIPT
10356:     }
10357:     return;
10358: }
10359: 
10360: sub passwords_javascript {
10361:     my ($prefix) = @_;
10362:     my %intalert;
10363:     if ($prefix eq 'passwords') {
10364:         %intalert = &Apache::lonlocal::texthash (
10365:             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.',
10366:             authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
10367:             passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
10368:             passmax => 'Warning: maximum password length must be a positive integer (or blank).',
10369:             passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
10370:         );
10371:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
10372:         %intalert = &Apache::lonlocal::texthash (
10373:             passmin => 'Warning: minimum secret length must be a positive integer greater than 6.',
10374:             passmax => 'Warning: maximum secret length must be a positive integer (or blank).',
10375:         );
10376:     }
10377:     &js_escape(\%intalert);
10378:     my $defmin = $Apache::lonnet::passwdmin;
10379:     my $intauthjs;
10380:     if ($prefix eq 'passwords') { $intauthjs = <<"ENDSCRIPT";
10381: 
10382: function warnIntAuth(field) {
10383:     if (field.name == 'intauth_check') {
10384:         if (field.value == '2') {
10385:             alert('$intalert{authcheck}');
10386:         }
10387:     }
10388:     if (field.name == 'intauth_cost') {
10389:         field.value.replace(/\s/g,'');
10390:         if (field.value != '') {
10391:             var regexdigit=/^\\d+\$/;
10392:             if (!regexdigit.test(field.value)) {
10393:                 alert('$intalert{authcost}');
10394:             }
10395:         }
10396:     }
10397:     return;
10398: }
10399: 
10400: ENDSCRIPT
10401: 
10402:      }
10403: 
10404:      $intauthjs .= <<"ENDSCRIPT";
10405: 
10406: function warnInt$prefix(field) {
10407:     field.value.replace(/^\s+/,'');
10408:     field.value.replace(/\s+\$/,'');
10409:     var regexdigit=/^\\d+\$/;
10410:     if (field.name == '${prefix}_min') {
10411:         if (field.value == '') {
10412:             alert('$intalert{passmin}');
10413:             field.value = '$defmin';
10414:         } else {
10415:             if (!regexdigit.test(field.value)) {
10416:                 alert('$intalert{passmin}');
10417:                 field.value = '$defmin';
10418:             }
10419:             var minval = parseInt(field.value,10);
10420:             if (minval < $defmin) {
10421:                 alert('$intalert{passmin}');
10422:                 field.value = '$defmin';
10423:             }
10424:         }
10425:     } else {
10426:         if (field.value == '0') {
10427:             field.value = '';
10428:         }
10429:         if (field.value != '') {
10430:             if (!regexdigit.test(field.value)) {
10431:                 if (field.name == '${prefix}_max') {
10432:                     alert('$intalert{passmax}');
10433:                 } else {
10434:                     if (field.name == '${prefix}_numsaved') {
10435:                         alert('$intalert{passnum}');
10436:                     }
10437:                 }
10438:                 field.value = '';
10439:             }
10440:         }
10441:     }
10442:     return;
10443: }
10444: 
10445: ENDSCRIPT
10446:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
10447: }
10448: 
10449: sub coursecategories_javascript {
10450:     my ($settings) = @_;
10451:     my ($output,$jstext,$cathash);
10452:     if (ref($settings) eq 'HASH') {
10453:         $cathash = $settings->{'cats'};
10454:     }
10455:     if (ref($cathash) eq 'HASH') {
10456:         my (@cats,@jsarray,%idx);
10457:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
10458:         if (@jsarray > 0) {
10459:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
10460:             for (my $i=0; $i<@jsarray; $i++) {
10461:                 if (ref($jsarray[$i]) eq 'ARRAY') {
10462:                     my $catstr = join('","',@{$jsarray[$i]});
10463:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
10464:                 }
10465:             }
10466:         }
10467:     } else {
10468:         $jstext  = '    var categories = Array(1);'."\n".
10469:                    '    categories[0] = Array("instcode_pos");'."\n"; 
10470:     }
10471:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
10472:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
10473:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
10474:     &js_escape(\$instcode_reserved);
10475:     &js_escape(\$communities_reserved);
10476:     &js_escape(\$choose_again);
10477:     $output = <<"ENDSCRIPT";
10478: <script type="text/javascript">
10479: // <![CDATA[
10480: function reorderCats(form,parent,item,idx) {
10481:     var changedVal;
10482: $jstext
10483:     var newpos = 'addcategory_pos';
10484:     if (parent == '') {
10485:         var has_instcode = 0;
10486:         var maxtop = categories[idx].length;
10487:         for (var j=0; j<maxtop; j++) {
10488:             if (categories[idx][j] == 'instcode::0') {
10489:                 has_instcode == 1;
10490:             }
10491:         }
10492:         if (has_instcode == 0) {
10493:             categories[idx][maxtop] = 'instcode_pos';
10494:         }
10495:     } else {
10496:         newpos += '_'+parent;
10497:     }
10498:     var maxh = 1 + categories[idx].length;
10499:     var current = new Array;
10500:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
10501:     if (item == newpos) {
10502:         changedVal = newitemVal;
10503:     } else {
10504:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
10505:         current[newitemVal] = newpos;
10506:     }
10507:     for (var i=0; i<categories[idx].length; i++) {
10508:         var elementName = categories[idx][i];
10509:         if (elementName != item) {
10510:             if (form.elements[elementName]) {
10511:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
10512:                 current[currVal] = elementName;
10513:             }
10514:         }
10515:     }
10516:     var oldVal;
10517:     for (var j=0; j<maxh; j++) {
10518:         if (current[j] == undefined) {
10519:             oldVal = j;
10520:         }
10521:     }
10522:     if (oldVal < changedVal) {
10523:         for (var k=oldVal+1; k<=changedVal ; k++) {
10524:            var elementName = current[k];
10525:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
10526:         }
10527:     } else {
10528:         for (var k=changedVal; k<oldVal; k++) {
10529:             var elementName = current[k];
10530:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
10531:         }
10532:     }
10533:     return;
10534: }
10535: 
10536: function categoryCheck(form) {
10537:     if (form.elements['addcategory_name'].value == 'instcode') {
10538:         alert('$instcode_reserved\\n$choose_again');
10539:         return false;
10540:     }
10541:     if (form.elements['addcategory_name'].value == 'communities') {
10542:         alert('$communities_reserved\\n$choose_again');
10543:         return false;
10544:     }
10545:     return true;
10546: }
10547: 
10548: // ]]>
10549: </script>
10550: 
10551: ENDSCRIPT
10552:     return $output;
10553: }
10554: 
10555: sub initialize_categories {
10556:     my ($itemcount) = @_;
10557:     my ($datatable,$css_class,$chgstr);
10558:     my %default_names = &Apache::lonlocal::texthash (
10559:                       instcode    => 'Official courses (with institutional codes)',
10560:                       communities => 'Communities',
10561:                         );
10562:     my $select0 = ' selected="selected"';
10563:     my $select1 = '';
10564:     foreach my $default ('instcode','communities') {
10565:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
10566:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
10567:         if ($default eq 'communities') {
10568:             $select1 = $select0;
10569:             $select0 = '';
10570:         }
10571:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10572:                      .'<select name="'.$default.'_pos">'
10573:                      .'<option value="0"'.$select0.'>1</option>'
10574:                      .'<option value="1"'.$select1.'>2</option>'
10575:                      .'<option value="2">3</option></select>&nbsp;'
10576:                      .$default_names{$default}
10577:                      .'</span></td><td><span class="LC_nobreak">'
10578:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
10579:                      .&mt('Display').'</label>&nbsp;<label>'
10580:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
10581:                  .'</label></span></td></tr>';
10582:         $itemcount ++;
10583:     }
10584:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10585:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
10586:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10587:                   .'<select name="addcategory_pos"'.$chgstr.'>'
10588:                   .'<option value="0">1</option>'
10589:                   .'<option value="1">2</option>'
10590:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
10591:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
10592:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
10593:                   .'</td></tr>';
10594:     return $datatable;
10595: }
10596: 
10597: sub build_category_rows {
10598:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
10599:     my ($text,$name,$item,$chgstr);
10600:     if (ref($cats) eq 'ARRAY') {
10601:         my $maxdepth = scalar(@{$cats});
10602:         if (ref($cats->[$depth]) eq 'HASH') {
10603:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
10604:                 my $numchildren = @{$cats->[$depth]{$parent}};
10605:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
10606:                 $text .= '<td><table class="LC_data_table">';
10607:                 my ($idxnum,$parent_name,$parent_item);
10608:                 my $higher = $depth - 1;
10609:                 if ($higher == 0) {
10610:                     $parent_name = &escape($parent).'::'.$higher;
10611:                 } else {
10612:                     if (ref($path) eq 'ARRAY') {
10613:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
10614:                     }
10615:                 }
10616:                 $parent_item = 'addcategory_pos_'.$parent_name;
10617:                 for (my $j=0; $j<=$numchildren; $j++) {
10618:                     if ($j < $numchildren) {
10619:                         $name = $cats->[$depth]{$parent}[$j];
10620:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
10621:                         $idxnum = $idx->{$item};
10622:                     } else {
10623:                         $name = $parent_name;
10624:                         $item = $parent_item;
10625:                     }
10626:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
10627:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
10628:                     for (my $i=0; $i<=$numchildren; $i++) {
10629:                         my $vpos = $i+1;
10630:                         my $selstr;
10631:                         if ($j == $i) {
10632:                             $selstr = ' selected="selected" ';
10633:                         }
10634:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
10635:                     }
10636:                     $text .= '</select>&nbsp;';
10637:                     if ($j < $numchildren) {
10638:                         my $deeper = $depth+1;
10639:                         $text .= $name.'&nbsp;'
10640:                                  .'<label><input type="checkbox" name="deletecategory" value="'
10641:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
10642:                         if(ref($path) eq 'ARRAY') {
10643:                             push(@{$path},$name);
10644:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
10645:                             pop(@{$path});
10646:                         }
10647:                     } else {
10648:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
10649:                         if ($j == $numchildren) {
10650:                             $text .= $name;
10651:                         } else {
10652:                             $text .= $item;
10653:                         }
10654:                         $text .= '" value="" />';
10655:                     }
10656:                     $text .= '</td></tr>';
10657:                 }
10658:                 $text .= '</table></td>';
10659:             } else {
10660:                 my $higher = $depth-1;
10661:                 if ($higher == 0) {
10662:                     $name = &escape($parent).'::'.$higher;
10663:                 } else {
10664:                     if (ref($path) eq 'ARRAY') {
10665:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
10666:                     }
10667:                 }
10668:                 my $colspan;
10669:                 if ($parent ne 'instcode') {
10670:                     $colspan = $maxdepth - $depth - 1;
10671:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
10672:                 }
10673:             }
10674:         }
10675:     }
10676:     return $text;
10677: }
10678: 
10679: sub modifiable_userdata_row {
10680:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
10681:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
10682:     my ($role,$rolename,$statustype);
10683:     $role = $item;
10684:     if ($context eq 'cancreate') {
10685:         if ($item =~ /^(emailusername)_(.+)$/) {
10686:             $role = $1;
10687:             $statustype = $2;
10688:             if (ref($usertypes) eq 'HASH') {
10689:                 if ($usertypes->{$statustype}) {
10690:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
10691:                 } else {
10692:                     $rolename = &mt('Data provided by user');
10693:                 }
10694:             }
10695:         }
10696:     } elsif ($context eq 'selfcreate') {
10697:         if (ref($usertypes) eq 'HASH') {
10698:             $rolename = $usertypes->{$role};
10699:         } else {
10700:             $rolename = $role;
10701:         }
10702:     } elsif ($context eq 'lti') {
10703:         $rolename = &mt('Institutional data used (if available)');
10704:     } else {
10705:         if ($role eq 'cr') {
10706:             $rolename = &mt('Custom role');
10707:         } else {
10708:             $rolename = &Apache::lonnet::plaintext($role);
10709:         }
10710:     }
10711:     my (@fields,%fieldtitles);
10712:     if (ref($fieldsref) eq 'ARRAY') {
10713:         @fields = @{$fieldsref};
10714:     } else {
10715:         @fields = ('lastname','firstname','middlename','generation',
10716:                    'permanentemail','id');
10717:     }
10718:     if ((ref($titlesref) eq 'HASH')) {
10719:         %fieldtitles = %{$titlesref};
10720:     } else {
10721:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10722:     }
10723:     my $output;
10724:     my $css_class;
10725:     if ($rowcount%2) {
10726:         $css_class = 'LC_odd_row';
10727:     }
10728:     if ($customcss) {
10729:         $css_class .= " $customcss";
10730:     }
10731:     $css_class =~ s/^\s+//;
10732:     if ($css_class) {
10733:         $css_class = ' class="'.$css_class.'"';
10734:     }
10735:     if ($rowstyle) {
10736:         $css_class .= ' style="'.$rowstyle.'"';
10737:     }
10738:     if ($rowid) {
10739:         $rowid = ' id="'.$rowid.'"';
10740:     }
10741:     $output = '<tr '.$css_class.$rowid.'>'.
10742:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
10743:               '<td class="LC_left_item" colspan="2"><table>';
10744:     my $rem;
10745:     my %checks;
10746:     if (ref($settings) eq 'HASH') {
10747:         my $hashref;
10748:         if ($context eq 'lti') {
10749:             if (ref($settings) eq 'HASH') {
10750:                 $hashref = $settings->{'instdata'};
10751:             }
10752:         } elsif (ref($settings->{$context}) eq 'HASH') {
10753:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
10754:                 $hashref = $settings->{'lti_instdata'};
10755:             }
10756:             if ($role eq 'emailusername') {
10757:                 if ($statustype) {
10758:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
10759:                         $hashref = $settings->{$context}->{$role}->{$statustype};
10760:                     }
10761:                 }
10762:             }
10763:         }
10764:         if (ref($hashref) eq 'HASH') {
10765:             foreach my $field (@fields) {
10766:                 if ($hashref->{$field}) {
10767:                     if ($role eq 'emailusername') {
10768:                         $checks{$field} = $hashref->{$field};
10769:                     } else {
10770:                         $checks{$field} = ' checked="checked" ';
10771:                     }
10772:                 }
10773:             }
10774:         }
10775:     }
10776:     my $total = scalar(@fields);
10777:     for (my $i=0; $i<$total; $i++) {
10778:         $rem = $i%($numinrow);
10779:         if ($rem == 0) {
10780:             if ($i > 0) {
10781:                 $output .= '</tr>';
10782:             }
10783:             $output .= '<tr>';
10784:         }
10785:         my $check = ' ';
10786:         unless ($role eq 'emailusername') {
10787:             if (exists($checks{$fields[$i]})) {
10788:                 $check = $checks{$fields[$i]};
10789:             } elsif ($context ne 'lti') {
10790:                 if ($role eq 'st') {
10791:                     if (ref($settings) ne 'HASH') {
10792:                         $check = ' checked="checked" '; 
10793:                     }
10794:                 }
10795:             }
10796:         }
10797:         $output .= '<td class="LC_left_item">'.
10798:                    '<span class="LC_nobreak">';
10799:         my $prefix = 'canmodify';
10800:         if ($role eq 'emailusername') {
10801:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
10802:                 $checks{$fields[$i]} = 'omit';
10803:             }
10804:             foreach my $option ('required','optional','omit') {
10805:                 my $checked='';
10806:                 if ($checks{$fields[$i]} eq $option) {
10807:                     $checked='checked="checked" ';
10808:                 }
10809:                 $output .= '<label>'.
10810:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
10811:                            &mt($option).'</label>'.('&nbsp;' x2);
10812:             }
10813:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
10814:         } else {
10815:             if ($context eq 'lti') {
10816:                 $prefix = 'lti';
10817:             }
10818:             $output .= '<label>'.
10819:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
10820:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
10821:                        '</label>';
10822:         }
10823:         $output .= '</span></td>';
10824:     }
10825:     $rem = $total%$numinrow;
10826:     my $colsleft;
10827:     if ($rem) {
10828:         $colsleft = $numinrow - $rem;
10829:     }
10830:     if ($colsleft > 1) {
10831:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10832:                    '&nbsp;</td>';
10833:     } elsif ($colsleft == 1) {
10834:         $output .= '<td class="LC_left_item">&nbsp;</td>';
10835:     }
10836:     $output .= '</tr></table></td></tr>';
10837:     return $output;
10838: }
10839: 
10840: sub insttypes_row {
10841:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
10842:         $customcss,$rowstyle) = @_;
10843:     my %lt = &Apache::lonlocal::texthash (
10844:                       cansearch      => 'Users allowed to search',
10845:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
10846:                       lockablenames  => 'User preference to lock name',
10847:                       selfassign     => 'Self-reportable affiliations',
10848:                       overrides      => "Override domain's helpdesk settings based on requester's affiliation",
10849:                       webdav         => 'WebDAV access available',
10850:                       authorquota    => 'Authoring Space quota (MB)',
10851:              );
10852:     my ($showdom,$defaultquota);
10853:     if ($context eq 'cansearch') {
10854:         $showdom = ' ('.$dom.')';
10855:     } elsif ($context eq 'authorquota') {
10856:         $defaultquota = 500;
10857:     }
10858:     my $class = 'LC_left_item';
10859:     if ($context eq 'statustocreate') {
10860:         $class = 'LC_right_item';
10861:     }
10862:     my $css_class;
10863:     if ($$rowtotal%2) {
10864:         $css_class = 'LC_odd_row';
10865:     }
10866:     if ($customcss) {
10867:         $css_class .= ' '.$customcss;
10868:     }
10869:     $css_class =~ s/^\s+//;
10870:     if ($css_class) {
10871:         $css_class = ' class="'.$css_class.'"';
10872:     }
10873:     if ($rowstyle) {
10874:         $css_class .= ' style="'.$rowstyle.'"';
10875:     }
10876:     if ($onclick) {
10877:         $onclick = 'onclick="'.$onclick.'" ';
10878:     }
10879:     my $output = '<tr'.$css_class.'>'.
10880:                  '<td>'.$lt{$context}.$showdom.
10881:                  '</td><td class="'.$class.'" colspan="2"><table>';
10882:     my $rem;
10883:     if (ref($types) eq 'ARRAY') {
10884:         for (my $i=0; $i<@{$types}; $i++) {
10885:             if (defined($usertypes->{$types->[$i]})) {
10886:                 my $rem = $i%($numinrow);
10887:                 if ($rem == 0) {
10888:                     if ($i > 0) {
10889:                         $output .= '</tr>';
10890:                     }
10891:                     $output .= '<tr>';
10892:                 }
10893:                 if ($context eq 'authorquota') {
10894:                     my $currquota;
10895:                     if ($settings->{$context}->{$types->[$i]} =~ /^\d+$/) {
10896:                         $currquota = $settings->{$context}->{$types->[$i]};
10897:                     } else {
10898:                         $currquota = $defaultquota;
10899:                     }
10900:                     $output .= '<td class="LC_left_item">'."\n".
10901:                                '<label><span class="LC_nobreak">'."\n".
10902:                                $usertypes->{$types->[$i]}.'</span><br />'."\n".
10903:                                '<input type="text" name="'.$context.'_'.$types->[$i].'" '.
10904:                                'value="'.$currquota.'" size="5"'.$onclick.'/>'."\n".
10905:                                '</label></td>';
10906:                 } else {
10907:                     my $check = ' ';
10908:                     if (ref($settings) eq 'HASH') {
10909:                         if (ref($settings->{$context}) eq 'ARRAY') {
10910:                             if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
10911:                                 $check = ' checked="checked" ';
10912:                             }
10913:                         } elsif (ref($settings->{$context}) eq 'HASH') {
10914:                             if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
10915:                                 $check = ' checked="checked" ';
10916:                             } elsif ($context eq 'webdav') {
10917:                                 if ($settings->{$context}->{$types->[$i]}) {
10918:                                     $check = ' checked="checked" ';
10919:                                 }
10920:                             }
10921:                         } elsif ($context eq 'statustocreate') {
10922:                             $check = ' checked="checked" ';
10923:                         }
10924:                     }
10925:                     $output .= '<td class="LC_left_item">'.
10926:                                '<span class="LC_nobreak"><label>'.
10927:                                '<input type="checkbox" name="'.$context.'" '.
10928:                                'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
10929:                                $usertypes->{$types->[$i]}.'</label></span></td>';
10930:                 }
10931:             }
10932:         }
10933:         $rem = @{$types}%($numinrow);
10934:     }
10935:     my $colsleft = $numinrow - $rem;
10936:     if ($context eq 'overrides') {
10937:         if ($colsleft > 1) {
10938:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
10939:         } else {
10940:             $output .= '<td class="LC_left_item">';
10941:         }
10942:         $output .= '&nbsp;';
10943:     } else {
10944:         if ($rem == 0) {
10945:             $output .= '<tr>';
10946:         }
10947:         if ($colsleft > 1) {
10948:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
10949:         } else {
10950:             $output .= '<td class="LC_left_item">';
10951:         }
10952:         if ($context eq 'authorquota') {
10953:             my $currquota = 500;
10954:             if ((ref($settings) eq 'HASH') && (ref($settings->{$context}) eq 'HASH')) {
10955:                 if ($settings->{$context}{'default'} =~ /^\d+$/) {
10956:                     $currquota = $settings->{$context}{'default'};
10957:                 }
10958:             }
10959:             $output .= '<label><span class="LC_nobreak">'.$othertitle.'</span><br />'."\n".
10960:                        '<input type="text" name="'.$context.'_default" '.
10961:                        'value="'.$currquota.'" size="5"'.$onclick.'/>'."\n".
10962:                        '</label>';
10963:         } else {
10964:             my $defcheck = ' ';
10965:             if (ref($settings) eq 'HASH') {
10966:                 if (ref($settings->{$context}) eq 'ARRAY') {
10967:                     if (grep(/^default$/,@{$settings->{$context}})) {
10968:                         $defcheck = ' checked="checked" ';
10969:                     }
10970:                 } elsif (ref($settings->{$context}) eq 'HASH') {
10971:                     if (ref($settings->{$context}->{'default'}) eq 'HASH') {
10972:                         $defcheck = ' checked="checked" ';
10973:                     } elsif ($context eq 'webdav') {
10974:                         if ($settings->{$context}->{'default'}) {
10975:                             $defcheck = ' checked="checked" ';
10976:                         }
10977:                     }
10978:                 } elsif ($context eq 'statustocreate') {
10979:                     $defcheck = ' checked="checked" ';
10980:                 }
10981:             }
10982:             $output .= '<span class="LC_nobreak"><label>'.
10983:                        '<input type="checkbox" name="'.$context.'" '.
10984:                        'value="default"'.$defcheck.$onclick.'/>'.
10985:                        $othertitle.'</label></span>';
10986:         }
10987:     }
10988:     $output .= '</td></tr></table></td></tr>';
10989:     return $output;
10990: }
10991: 
10992: sub sorted_searchtitles {
10993:     my %searchtitles = &Apache::lonlocal::texthash(
10994:                          'uname' => 'username',
10995:                          'lastname' => 'last name',
10996:                          'lastfirst' => 'last name, first name',
10997:                      );
10998:     my @titleorder = ('uname','lastname','lastfirst');
10999:     return (\%searchtitles,\@titleorder);
11000: }
11001: 
11002: sub sorted_searchtypes {
11003:     my %srchtypes_desc = (
11004:                            exact    => 'is exact match',
11005:                            contains => 'contains ..',
11006:                            begins   => 'begins with ..',
11007:                          );
11008:     my @srchtypeorder = ('exact','begins','contains');
11009:     return (\%srchtypes_desc,\@srchtypeorder);
11010: }
11011: 
11012: sub usertype_update_row {
11013:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
11014:     my $datatable;
11015:     my $numinrow = 4;
11016:     foreach my $type (@{$types}) {
11017:         if (defined($usertypes->{$type})) {
11018:             $$rownums ++;
11019:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
11020:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
11021:                           '</td><td class="LC_left_item"><table>';
11022:             for (my $i=0; $i<@{$fields}; $i++) {
11023:                 my $rem = $i%($numinrow);
11024:                 if ($rem == 0) {
11025:                     if ($i > 0) {
11026:                         $datatable .= '</tr>';
11027:                     }
11028:                     $datatable .= '<tr>';
11029:                 }
11030:                 my $check = ' ';
11031:                 if (ref($settings) eq 'HASH') {
11032:                     if (ref($settings->{'fields'}) eq 'HASH') {
11033:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
11034:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
11035:                                 $check = ' checked="checked" ';
11036:                             }
11037:                         }
11038:                     }
11039:                 }
11040: 
11041:                 if ($i == @{$fields}-1) {
11042:                     my $colsleft = $numinrow - $rem;
11043:                     if ($colsleft > 1) {
11044:                         $datatable .= '<td colspan="'.$colsleft.'">';
11045:                     } else {
11046:                         $datatable .= '<td>';
11047:                     }
11048:                 } else {
11049:                     $datatable .= '<td>';
11050:                 }
11051:                 $datatable .= '<span class="LC_nobreak"><label>'.
11052:                               '<input type="checkbox" name="updateable_'.$type.
11053:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
11054:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
11055:             }
11056:             $datatable .= '</tr></table></td></tr>';
11057:         }
11058:     }
11059:     return $datatable;
11060: }
11061: 
11062: sub modify_login {
11063:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11064:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
11065:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon,
11066:         %currsaml,%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso);
11067:     %title = ( coursecatalog => 'Display course catalog',
11068:                adminmail => 'Display administrator E-mail address',
11069:                helpdesk  => 'Display "Contact Helpdesk" link',
11070:                newuser => 'Link for visitors to create a user account',
11071:                loginheader => 'Log-in box header',
11072:                saml => 'Dual SSO and non-SSO login');
11073:     @offon = ('off','on');
11074:     if (ref($domconfig{login}) eq 'HASH') {
11075:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
11076:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
11077:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
11078:             }
11079:         }
11080:         if (ref($domconfig{login}{'saml'}) eq 'HASH') {
11081:             foreach my $lonhost (keys(%{$domconfig{login}{'saml'}})) {
11082:                 if (ref($domconfig{login}{'saml'}{$lonhost}) eq 'HASH') {
11083:                     $currsaml{$lonhost} = $domconfig{login}{'saml'}{$lonhost};
11084:                     $saml{$lonhost} = 1;
11085:                     $samltext{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'text'};
11086:                     $samlurl{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'url'};
11087:                     $samlalt{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'alt'};
11088:                     $samlimg{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'img'};
11089:                     $samltitle{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'title'};
11090:                     $samlwindow{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'window'};
11091:                     $samlnotsso{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'notsso'};
11092:                 }
11093:             }
11094:         }
11095:     }
11096:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
11097:                                            \%domconfig,\%loginhash);
11098:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
11099:     foreach my $item (@toggles) {
11100:         $loginhash{login}{$item} = $env{'form.'.$item};
11101:     }
11102:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
11103:     if (ref($colchanges{'login'}) eq 'HASH') {  
11104:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
11105:                                          \%loginhash);
11106:     }
11107: 
11108:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11109:     my %domservers = &Apache::lonnet::get_servers($dom);
11110:     my @loginvia_attribs = ('serverpath','custompath','exempt');
11111:     if (keys(%servers) > 1) {
11112:         foreach my $lonhost (keys(%servers)) {
11113:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
11114:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
11115:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
11116:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
11117:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
11118:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11119:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11120:                         $changes{'loginvia'}{$lonhost} = 1;
11121:                     } else {
11122:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
11123:                         $changes{'loginvia'}{$lonhost} = 1;
11124:                     }
11125:                 } else {
11126:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11127:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11128:                         $changes{'loginvia'}{$lonhost} = 1;
11129:                     }
11130:                 }
11131:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
11132:                     foreach my $item (@loginvia_attribs) {
11133:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
11134:                     }
11135:                 } else {
11136:                     foreach my $item (@loginvia_attribs) {
11137:                         my $new = $env{'form.'.$lonhost.'_'.$item};
11138:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
11139:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
11140:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
11141:                                 $new = '/';
11142:                             }
11143:                         }
11144:                         if (($item eq 'custompath') && 
11145:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
11146:                             $new = '';
11147:                         }
11148:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
11149:                             $changes{'loginvia'}{$lonhost} = 1;
11150:                         }
11151:                         if ($item eq 'exempt') {
11152:                             $new = &check_exempt_addresses($new);
11153:                         }
11154:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
11155:                     }
11156:                 }
11157:             } else {
11158:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11159:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11160:                     $changes{'loginvia'}{$lonhost} = 1;
11161:                     foreach my $item (@loginvia_attribs) {
11162:                         my $new = $env{'form.'.$lonhost.'_'.$item};
11163:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
11164:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
11165:                                 $new = '/';
11166:                             }
11167:                         }
11168:                         if (($item eq 'custompath') && 
11169:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
11170:                             $new = '';
11171:                         }
11172:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
11173:                     }
11174:                 }
11175:             }
11176:         }
11177:     }
11178: 
11179:     my $servadm = $r->dir_config('lonAdmEMail');
11180:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
11181:     if (ref($domconfig{'login'}) eq 'HASH') {
11182:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
11183:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
11184:                 if ($lang eq 'nolang') {
11185:                     push(@currlangs,$lang);
11186:                 } elsif (defined($langchoices{$lang})) {
11187:                     push(@currlangs,$lang);
11188:                 } else {
11189:                     next;
11190:                 }
11191:             }
11192:         }
11193:     }
11194:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
11195:     if (@currlangs > 0) {
11196:         foreach my $lang (@currlangs) {
11197:             if (grep(/^\Q$lang\E$/,@delurls)) {
11198:                 $changes{'helpurl'}{$lang} = 1;
11199:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
11200:                 $changes{'helpurl'}{$lang} = 1;
11201:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
11202:                 push(@newlangs,$lang);
11203:             } else {
11204:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
11205:             }
11206:         }
11207:     }
11208:     unless (grep(/^nolang$/,@currlangs)) {
11209:         if ($env{'form.loginhelpurl_nolang.filename'}) {
11210:             $changes{'helpurl'}{'nolang'} = 1;
11211:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
11212:             push(@newlangs,'nolang');
11213:         }
11214:     }
11215:     if ($env{'form.loginhelpurl_add_lang'}) {
11216:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
11217:             ($env{'form.loginhelpurl_add_file.filename'})) {
11218:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
11219:             $addedfile = $env{'form.loginhelpurl_add_lang'};
11220:         }
11221:     }
11222:     if ((@newlangs > 0) || ($addedfile)) {
11223:         my $error;
11224:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11225:         if ($configuserok eq 'ok') {
11226:             if ($switchserver) {
11227:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
11228:             } elsif ($author_ok eq 'ok') {
11229:                 my @allnew = @newlangs;
11230:                 if ($addedfile ne '') {
11231:                     push(@allnew,$addedfile);
11232:                 }
11233:                 my $modified = [];
11234:                 foreach my $lang (@allnew) {
11235:                     my $formelem = 'loginhelpurl_'.$lang;
11236:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
11237:                         $formelem = 'loginhelpurl_add_file';
11238:                     }
11239:                     (my $result,$newurl{$lang}) =
11240:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
11241:                                                                 "help/$lang",'','',$newfile{$lang},
11242:                                                                 $modified);
11243:                     if ($result eq 'ok') {
11244:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
11245:                         $changes{'helpurl'}{$lang} = 1;
11246:                     } else {
11247:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
11248:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11249:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
11250:                             (!grep(/^\Q$lang\E$/,@delurls))) {
11251:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
11252:                         }
11253:                     }
11254:                 }
11255:                 &update_modify_urls($r,$modified);
11256:             } else {
11257:                 $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);
11258:             }
11259:         } else {
11260:             $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);
11261:         }
11262:         if ($error) {
11263:             &Apache::lonnet::logthis($error);
11264:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11265:         }
11266:     }
11267: 
11268:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
11269:     if (ref($domconfig{'login'}) eq 'HASH') {
11270:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
11271:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
11272:                 if ($domservers{$lonhost}) {
11273:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
11274:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
11275:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
11276:                     }
11277:                 }
11278:             }
11279:         }
11280:     }
11281:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
11282:     foreach my $lonhost (sort(keys(%domservers))) {
11283:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
11284:             $changes{'headtag'}{$lonhost} = 1;
11285:         } else {
11286:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
11287:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
11288:             }
11289:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
11290:                 push(@newhosts,$lonhost);
11291:             } elsif ($currheadtagurls{$lonhost}) {
11292:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
11293:                 if ($currexempt{$lonhost}) {
11294:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
11295:                         $changes{'headtag'}{$lonhost} = 1;
11296:                     }
11297:                 } elsif ($possexempt{$lonhost}) {
11298:                     $changes{'headtag'}{$lonhost} = 1;
11299:                 }
11300:                 if ($possexempt{$lonhost}) {
11301:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
11302:                 }
11303:             }
11304:         }
11305:     }
11306:     if (@newhosts) {
11307:         my $error;
11308:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11309:         if ($configuserok eq 'ok') {
11310:             if ($switchserver) {
11311:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
11312:             } elsif ($author_ok eq 'ok') {
11313:                 my $modified = [];
11314:                 foreach my $lonhost (@newhosts) {
11315:                     my $formelem = 'loginheadtag_'.$lonhost;
11316:                     (my $result,$newheadtagurls{$lonhost}) =
11317:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
11318:                                                                 "login/headtag/$lonhost",'','',
11319:                                                                 $env{'form.loginheadtag_'.$lonhost.'.filename'},
11320:                                                                 $modified);
11321:                     if ($result eq 'ok') {
11322:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
11323:                         $changes{'headtag'}{$lonhost} = 1;
11324:                         if ($possexempt{$lonhost}) {
11325:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
11326:                         }
11327:                     } else {
11328:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
11329:                                            $newheadtagurls{$lonhost},$result);
11330:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11331:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
11332:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
11333:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
11334:                         }
11335:                     }
11336:                 }
11337:                 &update_modify_urls($r,$modified);
11338:             } else {
11339:                 $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);
11340:             }
11341:         } else {
11342:             $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);
11343:         }
11344:         if ($error) {
11345:             &Apache::lonnet::logthis($error);
11346:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11347:         }
11348:     }
11349:     my @delsamlimg = &Apache::loncommon::get_env_multiple('form.saml_img_del');
11350:     my @newsamlimgs;
11351:     foreach my $lonhost (keys(%domservers)) {
11352:         if ($env{'form.saml_'.$lonhost}) {
11353:             if ($env{'form.saml_img_'.$lonhost.'.filename'}) {
11354:                 push(@newsamlimgs,$lonhost);
11355:             }
11356:             foreach my $item ('text','alt','url','title','window','notsso') {
11357:                 $env{'form.saml_'.$item.'_'.$lonhost} =~ s/^\s+|\s+$//g;
11358:             }
11359:             if ($saml{$lonhost}) {
11360:                 if ($env{'form.saml_window_'.$lonhost} ne '1') {
11361:                     $env{'form.saml_window_'.$lonhost} = '';
11362:                 }
11363:                 if (grep(/^\Q$lonhost\E$/,@delsamlimg)) {
11364: #FIXME Need to obsolete published image
11365:                     delete($currsaml{$lonhost}{'img'});
11366:                     $changes{'saml'}{$lonhost} = 1;
11367:                 }
11368:                 if ($env{'form.saml_alt_'.$lonhost} ne $samlalt{$lonhost}) {
11369:                     $changes{'saml'}{$lonhost} = 1;
11370:                 }
11371:                 if ($env{'form.saml_text_'.$lonhost} ne $samltext{$lonhost}) {
11372:                     $changes{'saml'}{$lonhost} = 1;
11373:                 }
11374:                 if ($env{'form.saml_url_'.$lonhost} ne $samlurl{$lonhost}) {
11375:                     $changes{'saml'}{$lonhost} = 1;
11376:                 }
11377:                 if ($env{'form.saml_title_'.$lonhost} ne $samltitle{$lonhost}) {
11378:                     $changes{'saml'}{$lonhost} = 1;
11379:                 }
11380:                 if ($env{'form.saml_window_'.$lonhost} ne $samlwindow{$lonhost}) {
11381:                     $changes{'saml'}{$lonhost} = 1;
11382:                 }
11383:                 if ($env{'form.saml_notsso_'.$lonhost} ne $samlnotsso{$lonhost}) {
11384:                     $changes{'saml'}{$lonhost} = 1;
11385:                 }
11386:             } else {
11387:                 $changes{'saml'}{$lonhost} = 1;
11388:             }
11389:             foreach my $item ('text','alt','url','title','window','notsso') {
11390:                 $currsaml{$lonhost}{$item} = $env{'form.saml_'.$item.'_'.$lonhost};
11391:             }
11392:         } else {
11393:             if ($saml{$lonhost}) {
11394:                 $changes{'saml'}{$lonhost} = 1;
11395:                 delete($currsaml{$lonhost});
11396:             }
11397:         }
11398:     }
11399:     foreach my $posshost (keys(%currsaml)) {
11400:         unless (exists($domservers{$posshost})) {
11401:             delete($currsaml{$posshost});
11402:         }
11403:     }
11404:     %{$loginhash{'login'}{'saml'}} = %currsaml;
11405:     if (@newsamlimgs) {
11406:         my $error;
11407:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11408:         if ($configuserok eq 'ok') {
11409:             if ($switchserver) {
11410:                 $error = &mt("Upload of SSO Button Image is not permitted to this server: [_1].",$switchserver);
11411:             } elsif ($author_ok eq 'ok') {
11412:                 my $modified = [];
11413:                 foreach my $lonhost (@newsamlimgs) {
11414:                     my $formelem = 'saml_img_'.$lonhost;
11415:                     my ($result,$imgurl) =
11416:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
11417:                                                                 "login/saml/$lonhost",'','',
11418:                                                                 $env{'form.saml_img_'.$lonhost.'.filename'},
11419:                                                                 $modified);
11420:                     if ($result eq 'ok') {
11421:                         $currsaml{$lonhost}{'img'} = $imgurl;
11422:                         $loginhash{'login'}{'saml'}{$lonhost}{'img'} = $imgurl;
11423:                         $changes{'saml'}{$lonhost} = 1;
11424:                     } else {
11425:                         my $puberror = &mt("Upload of SSO button image failed for [_1] because an error occurred publishing the file in RES space. Error was: [_2].",
11426:                                            $lonhost,$result);
11427:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11428:                     }
11429:                 }
11430:                 &update_modify_urls($r,$modified);
11431:             } else {
11432:                 $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);
11433:             }
11434:         } else {
11435:             $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);
11436:         }
11437:         if ($error) {
11438:             &Apache::lonnet::logthis($error);
11439:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11440:         }
11441:     }
11442:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
11443: 
11444:     my $defaulthelpfile = '/adm/loginproblems.html';
11445:     my $defaulttext = &mt('Default in use');
11446: 
11447:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
11448:                                              $dom);
11449:     if ($putresult eq 'ok') {
11450:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
11451:         my %defaultchecked = (
11452:                     'coursecatalog' => 'on',
11453:                     'helpdesk'      => 'on',
11454:                     'adminmail'     => 'off',
11455:                     'newuser'       => 'off',
11456:         );
11457:         if (ref($domconfig{'login'}) eq 'HASH') {
11458:             foreach my $item (@toggles) {
11459:                 if ($defaultchecked{$item} eq 'on') { 
11460:                     if (($domconfig{'login'}{$item} eq '0') &&
11461:                         ($env{'form.'.$item} eq '1')) {
11462:                         $changes{$item} = 1;
11463:                     } elsif (($domconfig{'login'}{$item} eq '' ||
11464:                               $domconfig{'login'}{$item} eq '1') &&
11465:                              ($env{'form.'.$item} eq '0')) {
11466:                         $changes{$item} = 1;
11467:                     }
11468:                 } elsif ($defaultchecked{$item} eq 'off') {
11469:                     if (($domconfig{'login'}{$item} eq '1') &&
11470:                         ($env{'form.'.$item} eq '0')) {
11471:                         $changes{$item} = 1;
11472:                     } elsif (($domconfig{'login'}{$item} eq '' ||
11473:                               $domconfig{'login'}{$item} eq '0') &&
11474:                              ($env{'form.'.$item} eq '1')) {
11475:                         $changes{$item} = 1;
11476:                     }
11477:                 }
11478:             }
11479:         }
11480:         if (keys(%changes) > 0 || $colchgtext) {
11481:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11482:             if (exists($changes{'saml'})) {
11483:                 my $hostid_in_use;
11484:                 my @hosts = &Apache::lonnet::current_machine_ids();
11485:                 if (@hosts > 1) {
11486:                     foreach my $hostid (@hosts) {
11487:                         if (&Apache::lonnet::host_domain($hostid) eq $dom) {
11488:                             $hostid_in_use = $hostid;
11489:                             last;
11490:                         }
11491:                     }
11492:                 } else {
11493:                     $hostid_in_use = $r->dir_config('lonHostID');
11494:                 }
11495:                 if (($hostid_in_use) &&
11496:                     (&Apache::lonnet::host_domain($hostid_in_use) eq $dom)) {
11497:                     &Apache::lonnet::devalidate_cache_new('samllanding',$hostid_in_use);
11498:                 }
11499:                 if (ref($lastactref) eq 'HASH') {
11500:                     if (ref($changes{'saml'}) eq 'HASH') {
11501:                         my %updates;
11502:                         map { $updates{$_} = 1; } keys(%{$changes{'saml'}});
11503:                         $lastactref->{'samllanding'} = \%updates;
11504:                     }
11505:                 }
11506:             }
11507:             if (ref($lastactref) eq 'HASH') {
11508:                 $lastactref->{'domainconfig'} = 1;
11509:             }
11510:             $resulttext = &mt('Changes made:').'<ul>';
11511:             foreach my $item (sort(keys(%changes))) {
11512:                 if ($item eq 'loginvia') {
11513:                     if (ref($changes{$item}) eq 'HASH') {
11514:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
11515:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11516:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
11517:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
11518:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
11519:                                     $protocol = 'http' if ($protocol ne 'https');
11520:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
11521: 
11522:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
11523:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
11524:                                     } else {
11525:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
11526:                                     }
11527:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
11528:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
11529:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
11530:                                     }
11531:                                     $resulttext .= '</li>';
11532:                                 } else {
11533:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
11534:                                 }
11535:                             } else {
11536:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
11537:                             }
11538:                         }
11539:                         $resulttext .= '</ul></li>';
11540:                     }
11541:                 } elsif ($item eq 'helpurl') {
11542:                     if (ref($changes{$item}) eq 'HASH') {
11543:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
11544:                             if (grep(/^\Q$lang\E$/,@delurls)) {
11545:                                 my ($chg,$link);
11546:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
11547:                                 if ($lang eq 'nolang') {
11548:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
11549:                                 } else {
11550:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
11551:                                 }
11552:                                 $resulttext .= '<li>'.$chg.'</li>';
11553:                             } else {
11554:                                 my $chg;
11555:                                 if ($lang eq 'nolang') {
11556:                                     $chg = &mt('custom log-in help file for no preferred language');
11557:                                 } else {
11558:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
11559:                                 }
11560:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
11561:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
11562:                                                       '?inhibitmenu=yes',$chg,600,500).
11563:                                                '</li>';
11564:                             }
11565:                         }
11566:                     }
11567:                 } elsif ($item eq 'headtag') {
11568:                     if (ref($changes{$item}) eq 'HASH') {
11569:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11570:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
11571:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
11572:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
11573:                                 $resulttext .= '<li><a href="'.
11574:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
11575:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
11576:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
11577:                                 if ($possexempt{$lonhost}) {
11578:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
11579:                                 } else {
11580:                                     $resulttext .= &mt('included for any client IP');
11581:                                 }
11582:                                 $resulttext .= '</li>';
11583:                             }
11584:                         }
11585:                     }
11586:                 } elsif ($item eq 'saml') {
11587:                     if (ref($changes{$item}) eq 'HASH') {
11588:                         my %notlt = (
11589:                                        text   => 'Text for log-in by SSO',
11590:                                        img    => 'SSO button image',
11591:                                        alt    => 'Alt text for button image',
11592:                                        url    => 'SSO URL',
11593:                                        title  => 'Tooltip for SSO link',
11594:                                        window => 'Pop-up window if iframe',
11595:                                        notsso => 'Text for non-SSO log-in',
11596:                                     );
11597:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11598:                             if (ref($currsaml{$lonhost}) eq 'HASH') {
11599:                                 $resulttext .= '<li>'.&mt("$title{$item} in use for [_1]","<b>$lonhost</b>").
11600:                                                '<ul>';
11601:                                 foreach my $key ('text','img','alt','url','title','window','notsso') {
11602:                                     if ($currsaml{$lonhost}{$key} eq '') {
11603:                                         $resulttext .= '<li>'.&mt("$notlt{$key} not in use").'</li>';
11604:                                     } else {
11605:                                         my $value = "'$currsaml{$lonhost}{$key}'";
11606:                                         if ($key eq 'img') {
11607:                                             $value = '<img src="'.$currsaml{$lonhost}{$key}.'" />';
11608:                                         } elsif ($key eq 'window') {
11609:                                             $value = 'On';
11610:                                         }
11611:                                         $resulttext .= '<li>'.&mt("$notlt{$key} set to: [_1]",
11612:                                                                   $value).'</li>';
11613:                                     }
11614:                                 }
11615:                                 $resulttext .= '</ul></li>';
11616:                             } else {
11617:                                 $resulttext .= '<li>'.&mt("$title{$item} not in use for [_1]",$lonhost).'</li>';
11618:                             }
11619:                         }
11620:                     }
11621:                 } elsif ($item eq 'captcha') {
11622:                     if (ref($loginhash{'login'}) eq 'HASH') {
11623:                         my $chgtxt;
11624:                         if ($loginhash{'login'}{$item} eq 'notused') {
11625:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
11626:                         } else {
11627:                             my %captchas = &captcha_phrases();
11628:                             if ($captchas{$loginhash{'login'}{$item}}) {
11629:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
11630:                             } else {
11631:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
11632:                             }
11633:                         }
11634:                         $resulttext .= '<li>'.$chgtxt.'</li>';
11635:                     }
11636:                 } elsif ($item eq 'recaptchakeys') {
11637:                     if (ref($loginhash{'login'}) eq 'HASH') {
11638:                         my ($privkey,$pubkey);
11639:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
11640:                             $pubkey = $loginhash{'login'}{$item}{'public'};
11641:                             $privkey = $loginhash{'login'}{$item}{'private'};
11642:                         }
11643:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
11644:                         if (!$pubkey) {
11645:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
11646:                         } else {
11647:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11648:                         }
11649:                         if (!$privkey) {
11650:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
11651:                         } else {
11652:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
11653:                         }
11654:                         $chgtxt .= '</ul>';
11655:                         $resulttext .= '<li>'.$chgtxt.'</li>';
11656:                     }
11657:                 } elsif ($item eq 'recaptchaversion') {
11658:                     if (ref($loginhash{'login'}) eq 'HASH') {
11659:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
11660:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
11661:                                            '</li>';
11662:                         }
11663:                     }
11664:                 } else {
11665:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
11666:                 }
11667:             }
11668:             $resulttext .= $colchgtext.'</ul>';
11669:         } else {
11670:             $resulttext = &mt('No changes made to log-in page settings');
11671:         }
11672:     } else {
11673:         $resulttext = '<span class="LC_error">'.
11674: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11675:     }
11676:     if ($errors) {
11677:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
11678:                        $errors.'</ul>';
11679:     }
11680:     return $resulttext;
11681: }
11682: 
11683: sub check_exempt_addresses {
11684:     my ($iplist) = @_;
11685:     $iplist =~ s/^\s+//;
11686:     $iplist =~ s/\s+$//;
11687:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
11688:     my (@okips,$new);
11689:     foreach my $ip (@poss_ips) {
11690:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
11691:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
11692:                 push(@okips,$ip);
11693:             }
11694:         }
11695:     }
11696:     if (@okips > 0) {
11697:         $new = join(',',@okips);
11698:     } else {
11699:         $new = '';
11700:     }
11701:     return $new;
11702: }
11703: 
11704: sub color_font_choices {
11705:     my %choices =
11706:         &Apache::lonlocal::texthash (
11707:             img => "Header",
11708:             bgs => "Background colors",
11709:             links => "Link colors",
11710:             images => "Images",
11711:             font => "Font color",
11712:             fontmenu => "Font menu",
11713:             pgbg => "Page",
11714:             tabbg => "Header",
11715:             sidebg => "Border",
11716:             link => "Link",
11717:             alink => "Active link",
11718:             vlink => "Visited link",
11719:         );
11720:     return %choices;
11721: }
11722: 
11723: sub modify_ipaccess {
11724:     my ($dom,$lastactref,%domconfig) = @_;
11725:     my (@allpos,%changes,%confhash,$errors,$resulttext);
11726:     my (@items,%deletions,%itemids,@warnings);
11727:     my ($typeorder,$types) = &commblocktype_text();
11728:     if ($env{'form.ipaccess_add'}) {
11729:         my $name = $env{'form.ipaccess_name_add'};
11730:         my ($newid,$error) = &get_ipaccess_id($dom,$name);
11731:         if ($newid) {
11732:             $itemids{'add'} = $newid;
11733:             push(@items,'add');
11734:             $changes{$newid} = 1;
11735:         } else {
11736:             $error = &mt('Failed to acquire unique ID for new IP access control item');
11737:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11738:         }
11739:     }
11740:     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
11741:         my @todelete = &Apache::loncommon::get_env_multiple('form.ipaccess_del');
11742:         if (@todelete) {
11743:             map { $deletions{$_} = 1; } @todelete;
11744:         }
11745:         my $maxnum = $env{'form.ipaccess_maxnum'};
11746:         for (my $i=0; $i<$maxnum; $i++) {
11747:             my $itemid = $env{'form.ipaccess_id_'.$i};
11748:             $itemid =~ s/\D+//g;
11749:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
11750:                 if ($deletions{$itemid}) {
11751:                     $changes{$itemid} = $domconfig{'ipaccess'}{$itemid}{'name'};
11752:                 } else {
11753:                     push(@items,$i);
11754:                     $itemids{$i} = $itemid;
11755:                 }
11756:             }
11757:         }
11758:     }
11759:     foreach my $idx (@items) {
11760:         my $itemid = $itemids{$idx};
11761:         next unless ($itemid);
11762:         my %current;
11763:         unless ($idx eq 'add') {
11764:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
11765:                 %current = %{$domconfig{'ipaccess'}{$itemid}};
11766:             }
11767:         }
11768:         my $position = $env{'form.ipaccess_pos_'.$itemid};
11769:         $position =~ s/\D+//g;
11770:         if ($position ne '') {
11771:             $allpos[$position] = $itemid;
11772:         }
11773:         my $name = $env{'form.ipaccess_name_'.$idx};
11774:         $name =~ s/^\s+|\s+$//g;
11775:         $confhash{$itemid}{'name'} = $name;
11776:         my $possrange = $env{'form.ipaccess_range_'.$idx};
11777:         $possrange =~ s/^\s+|\s+$//g;
11778:         unless ($possrange eq '') {
11779:             $possrange =~ s/[\r\n]+/\s/g;
11780:             $possrange =~ s/\s*-\s*/-/g;
11781:             $possrange =~ s/\s+/,/g;
11782:             $possrange =~ s/,+/,/g;
11783:             if ($possrange ne '') {
11784:                 my (@ok,$count);
11785:                 $count = 0;
11786:                 foreach my $poss (split(/\,/,$possrange)) {
11787:                     $count ++;
11788:                     $poss = &validate_ip_pattern($poss);
11789:                     if ($poss ne '') {
11790:                         push(@ok,$poss);
11791:                     }
11792:                 }
11793:                 my $diff = $count - scalar(@ok);
11794:                 if ($diff) {
11795:                     $errors .= '<li><span class="LC_error">'.
11796:                                &mt('[quant,_1,IP] invalid and excluded from saved value for IP range(s) for [_2]',
11797:                                    $diff,$name).
11798:                                '</span></li>';
11799:                 }
11800:                 if (@ok) {
11801:                     my @cidr_list;
11802:                     foreach my $item (@ok) {
11803:                         @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
11804:                     }
11805:                     $confhash{$itemid}{'ip'} = join(',',@cidr_list);
11806:                 }
11807:             }
11808:         }
11809:         foreach my $field ('name','ip') {
11810:             unless (($idx eq 'add') || ($changes{$itemid})) {
11811:                 if ($current{$field} ne $confhash{$itemid}{$field}) {
11812:                     $changes{$itemid} = 1;
11813:                     last;
11814:                 }
11815:             }
11816:         }
11817:         $confhash{$itemid}{'commblocks'} = {};
11818: 
11819:         my %commblocks;
11820:         map { $commblocks{$_} = 1; } &Apache::loncommon::get_env_multiple('form.ipaccess_block_'.$idx);
11821:         foreach my $type (@{$typeorder}) {
11822:             if ($commblocks{$type}) {
11823:                 $confhash{$itemid}{'commblocks'}{$type} = 'on';
11824:             }
11825:             unless (($idx eq 'add') || ($changes{$itemid})) {
11826:                 if (ref($current{'commblocks'}) eq 'HASH') {
11827:                     if ($confhash{$itemid}{'commblocks'}{$type} ne $current{'commblocks'}{$type}) {
11828:                         $changes{$itemid} = 1;
11829:                     }
11830:                 } elsif ($confhash{$itemid}{'commblocks'}{$type}) {
11831:                     $changes{$itemid} = 1;
11832:                 }
11833:             }
11834:         }
11835:         $confhash{$itemid}{'courses'} = {};
11836:         my %crsdeletions;
11837:         my @delcrs = &Apache::loncommon::get_env_multiple('form.ipaccess_course_delete_'.$idx);
11838:         if (@delcrs) {
11839:             map { $crsdeletions{$_} = 1; } @delcrs;
11840:         }
11841:         if (ref($current{'courses'}) eq 'HASH') {
11842:             foreach my $cid (sort(keys(%{$current{'courses'}}))) {
11843:                 if ($crsdeletions{$cid}) {
11844:                     $changes{$itemid} = 1;
11845:                 } else {
11846:                     $confhash{$itemid}{'courses'}{$cid} = 1;
11847:                 }
11848:             }
11849:         }
11850:         $env{'form.ipaccess_cnum_'.$idx} =~ s/^\s+|\s+$//g;
11851:         $env{'form.ipaccess_cdom_'.$idx} =~ s/^\s+|\s+$//g;
11852:         if (($env{'form.ipaccess_cnum_'.$idx} =~ /^$match_courseid$/) &&
11853:             ($env{'form.ipaccess_cdom_'.$idx} =~ /^$match_domain$/)) {
11854:             if (&Apache::lonnet::homeserver($env{'form.ipaccess_cnum_'.$idx},
11855:                                             $env{'form.ipaccess_cdom_'.$idx}) eq 'no_host') {
11856:                 $errors .= '<li><span class="LC_error">'.
11857:                            &mt('Invalid courseID [_1] omitted from list of allowed courses',
11858:                                $env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}).
11859:                            '</span></li>';
11860:             } else {
11861:                 $confhash{$itemid}{'courses'}{$env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}} = 1;
11862:                 $changes{$itemid} = 1;
11863:             }
11864:         }
11865:     }
11866:     if (@allpos > 0) {
11867:         my $idx = 0;
11868:         foreach my $itemid (@allpos) {
11869:             if ($itemid ne '') {
11870:                 $confhash{$itemid}{'order'} = $idx;
11871:                 unless ($changes{$itemid}) {
11872:                     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
11873:                         if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
11874:                             if ($domconfig{'ipaccess'}{$itemid}{'order'} ne $idx) {
11875:                                 $changes{$itemid} = 1;
11876:                             }
11877:                         }
11878:                     }
11879:                 }
11880:                 $idx ++;
11881:             }
11882:         }
11883:     }
11884:     if (keys(%changes)) {
11885:         my %defaultshash = (
11886:                               ipaccess => \%confhash,
11887:                            );
11888:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
11889:                                                  $dom);
11890:         if ($putresult eq 'ok') {
11891:             my $cachetime = 1800;
11892:             &Apache::lonnet::do_cache_new('ipaccess',$dom,\%confhash,$cachetime);
11893:             if (ref($lastactref) eq 'HASH') {
11894:                 $lastactref->{'ipaccess'} = 1;
11895:             }
11896:             $resulttext = &mt('Changes made:').'<ul>';
11897:             my %bynum;
11898:             foreach my $itemid (sort(keys(%changes))) {
11899:                 if (ref($confhash{$itemid}) eq 'HASH') {
11900:                     my $position = $confhash{$itemid}{'order'};
11901:                     if ($position =~ /^\d+$/) {
11902:                         $bynum{$position} = $itemid;
11903:                     }
11904:                 }
11905:             }
11906:             if (keys(%deletions)) {
11907:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
11908:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
11909:                 }
11910:             }
11911:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
11912:                 my $itemid = $bynum{$pos};
11913:                 if (ref($confhash{$itemid}) eq 'HASH') {
11914:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'name'}.'</b><ul>';
11915:                     my $position = $pos + 1;
11916:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
11917:                     if ($confhash{$itemid}{'ip'} eq '') {
11918:                         $resulttext .= '<li>'.&mt('No IP Range(s) set').'</li>';
11919:                     } else {
11920:                         $resulttext .= '<li>'.&mt('IP Range(s): [_1]',$confhash{$itemid}{'ip'}).'</li>';
11921:                     }
11922:                     if (keys(%{$confhash{$itemid}{'commblocks'}})) {
11923:                         $resulttext .= '<li>'.&mt('Functionality Blocked: [_1]',
11924:                                                   join(', ', map { $types->{$_}; } sort(keys(%{$confhash{$itemid}{'commblocks'}})))).
11925:                                        '</li>';
11926:                     } else {
11927:                         $resulttext .= '<li>'.&mt('No functionality blocked').'</li>';
11928:                     }
11929:                     if (keys(%{$confhash{$itemid}{'courses'}})) {
11930:                         my @courses;
11931:                         foreach my $cid (sort(keys(%{$confhash{$itemid}{'courses'}}))) {
11932:                             my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
11933:                             push(@courses,$courseinfo{'description'}.' ('.$cid.')');
11934:                         }
11935:                         $resulttext .= '<li>'.&mt('Courses/Communities allowed').':<ul><li>'.
11936:                                              join('</li><li>',@courses).'</li></ul>';
11937:                     } else {
11938:                         $resulttext .= '<li>'.&mt('No courses allowed').'</li>';
11939:                     }
11940:                     $resulttext .= '</ul></li>';
11941:                 }
11942:             }
11943:             $resulttext .= '</ul>';
11944:         } else {
11945:             $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
11946:         }
11947:     } else {
11948:         $resulttext = &mt('No changes made');
11949:     }
11950:     if ($errors) {
11951:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
11952:                        $errors.'</ul></p>';
11953:     }
11954:     return $resulttext;
11955: }
11956: 
11957: sub get_ipaccess_id {
11958:     my ($domain,$location) = @_;
11959:     # get lock on ipaccess db
11960:     my $lockhash = {
11961:                       lock => $env{'user.name'}.
11962:                               ':'.$env{'user.domain'},
11963:                    };
11964:     my $tries = 0;
11965:     my $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
11966:     my ($id,$error);
11967: 
11968:     while (($gotlock ne 'ok') && ($tries<10)) {
11969:         $tries ++;
11970:         sleep (0.1);
11971:         $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
11972:     }
11973:     if ($gotlock eq 'ok') {
11974:         my %currids = &Apache::lonnet::dump_dom('ipaccess',$domain);
11975:         if ($currids{'lock'}) {
11976:             delete($currids{'lock'});
11977:             if (keys(%currids)) {
11978:                 my @curr = sort { $a <=> $b } keys(%currids);
11979:                 if ($curr[-1] =~ /^\d+$/) {
11980:                     $id = 1 + $curr[-1];
11981:                 }
11982:             } else {
11983:                 $id = 1;
11984:             }
11985:             if ($id) {
11986:                 unless (&Apache::lonnet::newput_dom('ipaccess',{ $id => $location },$domain) eq 'ok') {
11987:                     $error = 'nostore';
11988:                 }
11989:             } else {
11990:                 $error = 'nonumber';
11991:             }
11992:         }
11993:         my $dellockoutcome = &Apache::lonnet::del_dom('ipaccess',['lock'],$domain);
11994:     } else {
11995:         $error = 'nolock';
11996:     }
11997:     return ($id,$error);
11998: }
11999: 
12000: sub modify_authordefaults {
12001:     my ($dom,$lastactref,%domconfig) = @_;
12002: #
12003: # Retrieve current domain configuration for webDAV and Authoring Space quotas from $domconfig{'quotas'}.
12004: #
12005:     my (%curr_quotas,%save_quotas,%confhash,%changes,%newvalues);
12006:     if (ref($domconfig{'quotas'}) eq 'HASH') {
12007:         foreach my $key (keys(%{$domconfig{'quotas'}})) {
12008:             if ($key =~ /^webdav|authorquota$/) {
12009:                 $curr_quotas{$key} = $domconfig{'quotas'}{$key};
12010:             } else {
12011:                 $save_quotas{$key} = $domconfig{'quotas'}{$key};
12012:             }
12013:         }
12014:     }
12015:     my %staticdefaults = (
12016:                            'copyright'    => 'default',
12017:                            'sourceavail'  => 'closed',
12018:                            'nocodemirror' => 'off',
12019:                            'domcoordacc'  => 'on',
12020:                            'editors'      => ['edit','xml'].
12021:                            'authorquota'  => 500,
12022:                            'webdav'       => 0,
12023:                          );
12024:     my %titles = &authordefaults_titles();
12025:     foreach my $item ('nocodemirror','domcoordacc') {
12026:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
12027:             $confhash{$item} = $env{'form.'.$item};
12028:         }
12029:     }
12030:     if ($env{'form.copyright'} =~ /^(default|domain|public)$/) {
12031:         $confhash{'copyright'} = $1;
12032:     }
12033:     if ($env{'form.sourceavail'} =~ /^(closed|open)$/) {
12034:         $confhash{'sourceavail'} = $1;
12035:     }
12036:     my @posseditors =  &Apache::loncommon::get_env_multiple('form.author_editors');
12037:     my @okeditors = ('edit','xml','daxe');
12038:     my @editors;
12039:     foreach my $item (@posseditors) {
12040:         if (grep(/^\Q$item\E$/,@okeditors)) {
12041:             push(@editors,$item);
12042:         }
12043:     }
12044:     $confhash{'editors'} = \@editors;
12045: 
12046:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12047:     my @insttypes;
12048:     if (ref($types) eq 'ARRAY') {
12049:         @insttypes = @{$types};
12050:     }
12051:     my @webdavon = &Apache::loncommon::get_env_multiple('form.webdav');
12052:     my %webdav;
12053:     map { $webdav{$_} = 1; } @webdavon;
12054:     foreach my $type (@insttypes,'default') {
12055:         my $possquota = $env{'form.authorquota_'.$type};
12056:         if ($possquota =~ /^\d+$/) {
12057:             $save_quotas{'authorquota'}{$type} = $possquota;
12058:         }
12059:         if ($webdav{$type}) {
12060:             $save_quotas{'webdav'}{$type} = 1;
12061:         } else {
12062:             $save_quotas{'webdav'}{$type} = 0;
12063:         }
12064:     }
12065:     if ($env{'form.webdav_LC_adv'} =~ /^(0|1)$/) {
12066:         $save_quotas{'webdav'}{'_LC_adv'} = $env{'form.webdav_LC_adv'};
12067:     }
12068:     if (ref($domconfig{'authordefaults'}) eq 'HASH') {
12069:         foreach my $item ('nocodemirror','domcoordacc','copyright','sourceavail') {
12070:             if ($domconfig{'authordefaults'}{$item} ne $confhash{$item}) {
12071:                 $changes{$item} = 1;
12072:              }
12073:         }
12074:         if (ref($domconfig{'authordefaults'}{'editors'}) eq 'ARRAY') {
12075:             my @diffs =
12076:                 &Apache::loncommon::compare_arrays($confhash{'editors'},
12077:                                                    $domconfig{'authordefaults'}{'editors'});
12078:             unless (@diffs == 0) {
12079:                 $changes{'editors'} = 1;
12080:             }
12081:         } else {
12082:             my @diffs =
12083:                 &Apache::loncommon::compare_arrays($confhash{'editors'},
12084:                                                    $staticdefaults{'editors'});
12085:             unless (@diffs == 0) {
12086:                 $changes{'editors'} = 1;
12087:             }
12088:         }
12089:     } else {
12090:         my @offon = ('off','on');
12091:         foreach my $item ('nocodemirror','domcoordacc') {
12092:             if ($offon[$confhash{$item}] ne $staticdefaults{$item}) {
12093:                 $changes{$item} = 1;
12094:             }
12095:         }
12096:         foreach my $item ('copyright','sourceavail') {
12097:             if ($confhash{$item} ne $staticdefaults{$item}) {
12098:                 $changes{$item} = 1;
12099:             }
12100:         }
12101:     }
12102:     foreach my $key ('authorquota','webdav') {
12103:         if (ref($curr_quotas{$key}) eq 'HASH') {
12104:             foreach my $type (@insttypes,'default') {
12105:                 if (exists($save_quotas{$key}{$type})) {
12106:                     if ($save_quotas{$key}{$type} ne $curr_quotas{$key}{$type}) {
12107:                         $changes{$key}{$type} = 1;
12108:                     }
12109:                 } elsif (exists($curr_quotas{$key}{$type})) {
12110:                     $save_quotas{$key}{$type} = $curr_quotas{$key}{$type};
12111:                 } else {
12112:                     $save_quotas{$key}{$type} = $staticdefaults{$key};
12113:                 }
12114:             }
12115:         } else {
12116:             foreach my $type (@insttypes,'default') {
12117:                 if (exists($save_quotas{$key}{$type})) {
12118:                     unless ($save_quotas{$key}{$type} eq $staticdefaults{$key}) {
12119:                         $changes{$key}{$type} = 1;
12120:                     }
12121:                 } else {
12122:                     $save_quotas{$key}{$type} = $staticdefaults{$key};
12123:                 }
12124:             }
12125:         }
12126:     }
12127:     if (ref($curr_quotas{'webdav'}) eq 'HASH') {
12128:         if (exists($save_quotas{'webdav'}{'_LC_adv'})) {
12129:             if ($save_quotas{'webdav'}{'_LC_adv'} ne $curr_quotas{'webdav'}{'_LC_adv'}) {
12130:                 $changes{'webdav_LC_adv'} = 1;
12131:             }
12132:         } elsif (exists($curr_quotas{'webdav'}{'_LC_adv'})) {
12133:             $changes{'webdav_LC_adv'} = 1;
12134:         }
12135:     } elsif (exists($save_quotas{'webdav'}{'_LC_adv'})) {
12136:         $changes{'webdav_LC_adv'} = 1;
12137:     }
12138:     my %confighash = (
12139:                         quotas  => \%save_quotas,
12140:                         authordefaults => \%confhash,
12141:                      );
12142:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,
12143:                                              $dom);
12144:     my $resulttext;
12145:     if ($putresult eq 'ok') {
12146:         if (keys(%changes)) {
12147:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
12148:             if ((exists($changes{'authorquota'})) || (exists($changes{'webdav'})) ||
12149:                 ($changes{'webdav_LC_adv'})) {
12150:                 if ((exists($changes{'authorquota'})) && (ref($save_quotas{'authorquota'}) eq 'HASH')) {
12151:                     $domdefaults{'authorquota'} = $save_quotas{'authorquota'};
12152:                 }
12153:                 if (((exists($changes{'webdav'})) || ($changes{'webdav_LC_adv'})) &&
12154:                     (ref($save_quotas{'webdav'}) eq 'HASH')) {
12155:                     $domdefaults{'webdav'} = $save_quotas{'webdav'};
12156:                 }
12157:             }
12158:             $resulttext = &mt('Changes made:').'<ul>';
12159:             my $authoroverride;
12160:             foreach my $key ('nocodemirror','domcoordacc','copyright','sourceavail') {
12161:                 if (exists($changes{$key})) {
12162:                     $domdefaults{$key} = $confhash{$key};
12163:                     my $shown;
12164:                     unless ($authoroverride) {
12165:                         $resulttext .= '<li>'.&mt('Defaults which can be overridden by Author').'<ul>';
12166:                         $authoroverride = 1;
12167:                     }
12168:                     if (($key eq 'nocodemirror') || ($key eq 'domcoordacc')) {
12169:                         $shown = ($confhash{$key} ? &mt('Yes') : &mt('No'));
12170:                     } elsif ($key eq 'copyright') {
12171:                         $shown = &Apache::loncommon::copyrightdescription($confhash{$key});
12172:                     } elsif ($key eq 'sourceavail') {
12173:                         $shown = &Apache::loncommon::source_copyrightdescription($confhash{$key});
12174:                     }
12175:                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{$key},$shown).'</li>';
12176:                 }
12177:             }
12178:             if ($authoroverride) {
12179:                 $resulttext .= '</ul></li>';
12180:             }
12181:             my $domcoordoverride;
12182:             foreach my $key ('editors','authorquota','webdav','webdav_LC_adv') {
12183:                 if (exists($changes{$key})) {
12184:                     my $shown;
12185:                     unless ($domcoordoverride) {
12186:                         $resulttext .= '<li>'.&mt('Defaults which can be overridden by a Domain Coodinator').'<ul>';
12187:                         $domcoordoverride = 1;
12188:                     }
12189:                     if ($key eq 'editors') {
12190:                         if (ref($confhash{'editors'}) eq 'ARRAY') {
12191:                             $domdefaults{'editors'} = join(',',@{$confhash{'editors'}});
12192:                             if (@{$confhash{'editors'}}) {
12193:                                 $shown = join(', ', map { $titles{$_} } @{$confhash{'editors'}});
12194:                             } else {
12195:                                 $shown = &mt('None');
12196:                             }
12197:                         }
12198:                     } elsif ($key eq 'authorquota') {
12199:                         foreach my $type (@insttypes) {
12200:                             $shown .= $usertypes->{$type}.' -- '.$save_quotas{$key}{$type}.', ';
12201:                         }
12202:                         $shown .= $othertitle.' -- '.$save_quotas{$key}{'default'};
12203:                     } elsif ($key eq 'webdav') {
12204:                         foreach my $type (@insttypes) {
12205:                             $shown .= $usertypes->{$type}.' -- '. ($save_quotas{$key}{$type} ? &mt('Yes') : &mt('No')).', ';
12206:                         }
12207:                         $shown .= $othertitle.' -- '. ($save_quotas{$key}{'default'} ? &mt('Yes') : &mt('No'));
12208:                     } elsif ($key eq 'webdav_LC_adv') {
12209:                         if (exists($save_quotas{'webdav'}{'_LC_adv'})) {
12210:                             $shown = ($save_quotas{'webdav'}{'_LC_adv'} ? $titles{'overon'} : $titles{'overoff'});
12211:                         } else {
12212:                             $shown = $titles{'none'};
12213:                         }
12214:                     }
12215:                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{$key},$shown).'</li>';
12216:                 }   
12217:             }
12218:             if ($domcoordoverride) {
12219:                 $resulttext .= '</ul></li>';
12220:             }
12221:             my $cachetime = 24*60*60;
12222:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12223:             if (ref($lastactref) eq 'HASH') {
12224:                 $lastactref->{'domdefaults'} = 1;
12225:             }
12226:         } else {
12227:             $resulttext = &mt('No changes made to Authoring Space defaults');
12228:         }
12229:     }
12230:     return $resulttext;
12231: }
12232: 
12233: sub modify_rolecolors {
12234:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
12235:     my ($resulttext,%rolehash);
12236:     $rolehash{'rolecolors'} = {};
12237:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
12238:         if ($domconfig{'rolecolors'} eq '') {
12239:             $domconfig{'rolecolors'} = {};
12240:         }
12241:     }
12242:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
12243:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
12244:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
12245:                                              $dom);
12246:     if ($putresult eq 'ok') {
12247:         if (keys(%changes) > 0) {
12248:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12249:             if (ref($lastactref) eq 'HASH') {
12250:                 $lastactref->{'domainconfig'} = 1;
12251:             }
12252:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
12253:                                              $rolehash{'rolecolors'});
12254:         } else {
12255:             $resulttext = &mt('No changes made to default color schemes');
12256:         }
12257:     } else {
12258:         $resulttext = '<span class="LC_error">'.
12259: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12260:     }
12261:     if ($errors) {
12262:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12263:                        $errors.'</ul>';
12264:     }
12265:     return $resulttext;
12266: }
12267: 
12268: sub modify_colors {
12269:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
12270:     my (%changes,%choices);
12271:     my @bgs;
12272:     my @links = ('link','alink','vlink');
12273:     my @logintext;
12274:     my @images;
12275:     my $servadm = $r->dir_config('lonAdmEMail');
12276:     my $errors;
12277:     my %defaults;
12278:     foreach my $role (@{$roles}) {
12279:         if ($role eq 'login') {
12280:             %choices = &login_choices();
12281:             @logintext = ('textcol','bgcol');
12282:         } else {
12283:             %choices = &color_font_choices();
12284:         }
12285:         if ($role eq 'login') {
12286:             @images = ('img','logo','domlogo','login');
12287:             @bgs = ('pgbg','mainbg','sidebg');
12288:         } else {
12289:             @images = ('img');
12290:             @bgs = ('pgbg','tabbg','sidebg');
12291:         }
12292:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
12293:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
12294:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
12295:         }
12296:         if ($role eq 'login') {
12297:             foreach my $item (@logintext) {
12298:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12299:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12300:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12301:                 }
12302:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
12303:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12304:                 }
12305:             }
12306:         } else {
12307:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
12308:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
12309:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
12310:             }
12311:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
12312:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
12313:             }
12314:         }
12315:         foreach my $item (@bgs) {
12316:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12317:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12318:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12319:             }
12320:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
12321:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12322:             }
12323:         }
12324:         foreach my $item (@links) {
12325:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12326:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12327:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12328:             }
12329:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
12330:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12331:             }
12332:         }
12333:         my ($configuserok,$author_ok,$switchserver) = 
12334:             &config_check($dom,$confname,$servadm);
12335:         my ($width,$height) = &thumb_dimensions();
12336:         if (ref($domconfig->{$role}) ne 'HASH') {
12337:             $domconfig->{$role} = {};
12338:         }
12339:         foreach my $img (@images) {
12340:             if ($role eq 'login') {
12341:                 if (($img eq 'img') || ($img eq 'logo')) {  
12342:                     if (defined($env{'form.login_showlogo_'.$img})) {
12343:                         $confhash->{$role}{'showlogo'}{$img} = 1;
12344:                     } else { 
12345:                         $confhash->{$role}{'showlogo'}{$img} = 0;
12346:                     }
12347:                 }
12348:                 if ($env{'form.login_alt_'.$img} ne '') {
12349:                     $confhash->{$role}{'alttext'}{$img} = $env{'form.login_alt_'.$img};
12350:                 }
12351:             }
12352: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
12353: 		 && !defined($domconfig->{$role}{$img})
12354: 		 && !$env{'form.'.$role.'_del_'.$img}
12355: 		 && $env{'form.'.$role.'_import_'.$img}) {
12356: 		# import the old configured image from the .tab setting
12357: 		# if they haven't provided a new one 
12358: 		$domconfig->{$role}{$img} = 
12359: 		    $env{'form.'.$role.'_import_'.$img};
12360: 	    }
12361:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
12362:                 my $error;
12363:                 if ($configuserok eq 'ok') {
12364:                     if ($switchserver) {
12365:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
12366:                     } else {
12367:                         if ($author_ok eq 'ok') {
12368:                             my $modified = [];
12369:                             my ($result,$logourl) = 
12370:                                 &Apache::lonconfigsettings::publishlogo($r,'upload',$role.'_'.$img,
12371:                                                                         $dom,$confname,$img,$width,$height,
12372:                                                                         '',$modified);
12373:                             if ($result eq 'ok') {
12374:                                 $confhash->{$role}{$img} = $logourl;
12375:                                 $changes{$role}{'images'}{$img} = 1;
12376:                                 &update_modify_urls($r,$modified);
12377:                             } else {
12378:                                 $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);
12379:                             }
12380:                         } else {
12381:                             $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);
12382:                         }
12383:                     }
12384:                 } else {
12385:                     $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);
12386:                 }
12387:                 if ($error) {
12388:                     &Apache::lonnet::logthis($error);
12389:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12390:                 }
12391:             } elsif ($domconfig->{$role}{$img} ne '') {
12392:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
12393:                     my $error;
12394:                     if ($configuserok eq 'ok') {
12395: # is confname an author?
12396:                         if ($switchserver eq '') {
12397:                             if ($author_ok eq 'ok') {
12398:                                 my $modified = [];
12399:                                 my ($result,$logourl) = 
12400:                                     &Apache::lonconfigsettings::publishlogo($r,'copy',$domconfig->{$role}{$img},
12401:                                                                             $dom,$confname,$img,$width,$height,
12402:                                                                             '',$modified);
12403:                                 if ($result eq 'ok') {
12404:                                     $confhash->{$role}{$img} = $logourl;
12405: 				    $changes{$role}{'images'}{$img} = 1;
12406:                                     &update_modify_urls($r,$modified);
12407:                                 }
12408:                             }
12409:                         }
12410:                     }
12411:                 }
12412:             }
12413:         }
12414:         if (ref($domconfig) eq 'HASH') {
12415:             if (ref($domconfig->{$role}) eq 'HASH') {
12416:                 foreach my $img (@images) {
12417:                     if ($domconfig->{$role}{$img} ne '') {
12418:                         if ($env{'form.'.$role.'_del_'.$img}) {
12419:                             $confhash->{$role}{$img} = '';
12420:                             $changes{$role}{'images'}{$img} = 1;
12421:                         } else {
12422:                             if ($confhash->{$role}{$img} eq '') {
12423:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
12424:                             }
12425:                         }
12426:                     } else {
12427:                         if ($env{'form.'.$role.'_del_'.$img}) {
12428:                             $confhash->{$role}{$img} = '';
12429:                             $changes{$role}{'images'}{$img} = 1;
12430:                         } 
12431:                     }
12432:                     if ($role eq 'login') {
12433:                         if (($img eq 'logo') || ($img eq 'img')) {
12434:                             if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
12435:                                 if ($confhash->{$role}{'showlogo'}{$img} ne 
12436:                                     $domconfig->{$role}{'showlogo'}{$img}) {
12437:                                     $changes{$role}{'showlogo'}{$img} = 1; 
12438:                                 }
12439:                             } else {
12440:                                 if ($confhash->{$role}{'showlogo'}{$img} == 0) {
12441:                                     $changes{$role}{'showlogo'}{$img} = 1;
12442:                                 }
12443:                             }
12444:                         }
12445:                         if ($img ne 'login') {
12446:                             if (ref($domconfig->{$role}{'alttext'}) eq 'HASH') {
12447:                                 if ($confhash->{$role}{'alttext'}{$img} ne
12448:                                     $domconfig->{$role}{'alttext'}{$img}) {
12449:                                     $changes{$role}{'alttext'}{$img} = 1;
12450:                                 }
12451:                             } else {
12452:                                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
12453:                                     $changes{$role}{'alttext'}{$img} = 1;
12454:                                 }
12455:                             }
12456:                         }
12457:                     }
12458:                 }
12459:                 if ($domconfig->{$role}{'font'} ne '') {
12460:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
12461:                         $changes{$role}{'font'} = 1;
12462:                     }
12463:                 } else {
12464:                     if ($confhash->{$role}{'font'}) {
12465:                         $changes{$role}{'font'} = 1;
12466:                     }
12467:                 }
12468:                 if ($role ne 'login') {
12469:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
12470:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
12471:                             $changes{$role}{'fontmenu'} = 1;
12472:                         }
12473:                     } else {
12474:                         if ($confhash->{$role}{'fontmenu'}) {
12475:                             $changes{$role}{'fontmenu'} = 1;
12476:                         }
12477:                     }
12478:                 }
12479:                 foreach my $item (@bgs) {
12480:                     if ($domconfig->{$role}{$item} ne '') {
12481:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
12482:                             $changes{$role}{'bgs'}{$item} = 1;
12483:                         } 
12484:                     } else {
12485:                         if ($confhash->{$role}{$item}) {
12486:                             $changes{$role}{'bgs'}{$item} = 1;
12487:                         }
12488:                     }
12489:                 }
12490:                 foreach my $item (@links) {
12491:                     if ($domconfig->{$role}{$item} ne '') {
12492:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
12493:                             $changes{$role}{'links'}{$item} = 1;
12494:                         }
12495:                     } else {
12496:                         if ($confhash->{$role}{$item}) {
12497:                             $changes{$role}{'links'}{$item} = 1;
12498:                         }
12499:                     }
12500:                 }
12501:                 foreach my $item (@logintext) {
12502:                     if ($domconfig->{$role}{$item} ne '') {
12503:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
12504:                             $changes{$role}{'logintext'}{$item} = 1;
12505:                         }
12506:                     } else {
12507:                         if ($confhash->{$role}{$item}) {
12508:                             $changes{$role}{'logintext'}{$item} = 1;
12509:                         }
12510:                     }
12511:                 }
12512:             } else {
12513:                 &default_change_checker($role,\@images,\@links,\@bgs,
12514:                                         \@logintext,$confhash,\%changes); 
12515:             }
12516:         } else {
12517:             &default_change_checker($role,\@images,\@links,\@bgs,
12518:                                     \@logintext,$confhash,\%changes); 
12519:         }
12520:     }
12521:     return ($errors,%changes);
12522: }
12523: 
12524: sub config_check {
12525:     my ($dom,$confname,$servadm) = @_;
12526:     my ($configuserok,$author_ok,$switchserver,%currroles);
12527:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
12528:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
12529:                                                    $confname,$servadm);
12530:     if ($configuserok eq 'ok') {
12531:         $switchserver = &check_switchserver($dom,$confname);
12532:         if ($switchserver eq '') {
12533:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
12534:         }
12535:     }
12536:     return ($configuserok,$author_ok,$switchserver);
12537: }
12538: 
12539: sub default_change_checker {
12540:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
12541:     foreach my $item (@{$links}) {
12542:         if ($confhash->{$role}{$item}) {
12543:             $changes->{$role}{'links'}{$item} = 1;
12544:         }
12545:     }
12546:     foreach my $item (@{$bgs}) {
12547:         if ($confhash->{$role}{$item}) {
12548:             $changes->{$role}{'bgs'}{$item} = 1;
12549:         }
12550:     }
12551:     foreach my $item (@{$logintext}) {
12552:         if ($confhash->{$role}{$item}) {
12553:             $changes->{$role}{'logintext'}{$item} = 1;
12554:         }
12555:     }
12556:     foreach my $img (@{$images}) {
12557:         if ($env{'form.'.$role.'_del_'.$img}) {
12558:             $confhash->{$role}{$img} = '';
12559:             $changes->{$role}{'images'}{$img} = 1;
12560:         }
12561:         if ($role eq 'login') {
12562:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
12563:                 $changes->{$role}{'showlogo'}{$img} = 1;
12564:             }
12565:             if (ref($confhash->{$role}{'alttext'}) eq 'HASH') {
12566:                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
12567:                     $changes->{$role}{'alttext'}{$img} = 1;
12568:                 }
12569:             }
12570:         }
12571:     }
12572:     if ($confhash->{$role}{'font'}) {
12573:         $changes->{$role}{'font'} = 1;
12574:     }
12575: }
12576: 
12577: sub display_colorchgs {
12578:     my ($dom,$changes,$roles,$confhash) = @_;
12579:     my (%choices,$resulttext);
12580:     if (!grep(/^login$/,@{$roles})) {
12581:         $resulttext = &mt('Changes made:').'<br />';
12582:     }
12583:     foreach my $role (@{$roles}) {
12584:         if ($role eq 'login') {
12585:             %choices = &login_choices();
12586:         } else {
12587:             %choices = &color_font_choices();
12588:         }
12589:         if (ref($changes->{$role}) eq 'HASH') {
12590:             if ($role ne 'login') {
12591:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
12592:             }
12593:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
12594:                 if ($role ne 'login') {
12595:                     $resulttext .= '<ul>';
12596:                 }
12597:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
12598:                     if ($role ne 'login') {
12599:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
12600:                     }
12601:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
12602:                         if (($role eq 'login') && ($key eq 'showlogo')) {
12603:                             if ($confhash->{$role}{$key}{$item}) {
12604:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
12605:                             } else {
12606:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
12607:                             }
12608:                         } elsif (($role eq 'login') && ($key eq 'alttext')) {
12609:                             if ($confhash->{$role}{$key}{$item} ne '') {
12610:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} set to [_1].",
12611:                                                $confhash->{$role}{$key}{$item}).'</li>';
12612:                             } else {
12613:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} deleted.").'</li>';
12614:                             }
12615:                         } elsif ($confhash->{$role}{$item} eq '') {
12616:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
12617:                         } else {
12618:                             my $newitem = $confhash->{$role}{$item};
12619:                             if ($key eq 'images') {
12620:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
12621:                             }
12622:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
12623:                         }
12624:                     }
12625:                     if ($role ne 'login') {
12626:                         $resulttext .= '</ul></li>';
12627:                     }
12628:                 } else {
12629:                     if ($confhash->{$role}{$key} eq '') {
12630:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
12631:                     } else {
12632:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
12633:                     }
12634:                 }
12635:                 if ($role ne 'login') {
12636:                     $resulttext .= '</ul>';
12637:                 }
12638:             }
12639:         }
12640:     }
12641:     return $resulttext;
12642: }
12643: 
12644: sub thumb_dimensions {
12645:     return ('200','50');
12646: }
12647: 
12648: sub check_dimensions {
12649:     my ($inputfile) = @_;
12650:     my ($fullwidth,$fullheight);
12651:     if ($inputfile =~ m|^[/\w.\-]+$|) {
12652:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
12653:             my $imageinfo = <PIPE>;
12654:             if (!close(PIPE)) {
12655:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
12656:             }
12657:             chomp($imageinfo);
12658:             my ($fullsize) = 
12659:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
12660:             if ($fullsize) {
12661:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
12662:             }
12663:         }
12664:     }
12665:     return ($fullwidth,$fullheight);
12666: }
12667: 
12668: sub check_configuser {
12669:     my ($uhome,$dom,$confname,$servadm) = @_;
12670:     my ($configuserok,%currroles);
12671:     if ($uhome eq 'no_host') {
12672:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
12673:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
12674:         $configuserok = 
12675:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
12676:                              $configpass,'','','','','',undef,$servadm);
12677:     } else {
12678:         $configuserok = 'ok';
12679:         %currroles = 
12680:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
12681:     }
12682:     return ($configuserok,%currroles);
12683: }
12684: 
12685: sub check_authorstatus {
12686:     my ($dom,$confname,%currroles) = @_;
12687:     my $author_ok;
12688:     if (!$currroles{':'.$dom.':au'}) {
12689:         my $start = time;
12690:         my $end = 0;
12691:         $author_ok = 
12692:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
12693:                                         'au',$end,$start,'','','domconfig');
12694:     } else {
12695:         $author_ok = 'ok';
12696:     }
12697:     return $author_ok;
12698: }
12699: 
12700: sub update_modify_urls {
12701:     my ($r,$modified) = @_;
12702:     if ((ref($modified) eq 'ARRAY') && (@{$modified})) {
12703:         push(@{$modified_urls},$modified);
12704:         unless ($registered_cleanup) {
12705:             my $handlers = $r->get_handlers('PerlCleanupHandler');
12706:             $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
12707:             $registered_cleanup=1;
12708:         }
12709:     }
12710: }
12711: 
12712: sub notifysubscribed {
12713:     foreach my $targetsource (@{$modified_urls}){
12714:         next unless (ref($targetsource) eq 'ARRAY');
12715:         my ($target,$source)=@{$targetsource};
12716:         if ($source ne '') {
12717:             if (open(my $logfh,">>",$source.'.log')) {
12718:                 print $logfh "\nCleanup phase: Notifications\n";
12719:                 my @subscribed=&subscribed_hosts($target);
12720:                 foreach my $subhost (@subscribed) {
12721:                     print $logfh "\nNotifying host ".$subhost.':';
12722:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
12723:                     print $logfh $reply;
12724:                 }
12725:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
12726:                 foreach my $subhost (@subscribedmeta) {
12727:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
12728:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
12729:                                                         $subhost);
12730:                     print $logfh $reply;
12731:                 }
12732:                 print $logfh "\n============ Done ============\n";
12733:                 close($logfh);
12734:             }
12735:         }
12736:     }
12737:     return OK;
12738: }
12739: 
12740: sub subscribed_hosts {
12741:     my ($target) = @_;
12742:     my @subscribed;
12743:     if (open(my $fh,"<","$target.subscription")) {
12744:         while (my $subline=<$fh>) {
12745:             if ($subline =~ /^($match_lonid):/) {
12746:                 my $host = $1;
12747:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
12748:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
12749:                         push(@subscribed,$host);
12750:                     }
12751:                 }
12752:             }
12753:         }
12754:     }
12755:     return @subscribed;
12756: }
12757: 
12758: sub check_switchserver {
12759:     my ($dom,$confname) = @_;
12760:     my ($allowed,$switchserver,$home);
12761:     if ($confname eq '') {
12762:         $home = &Apache::lonnet::domain($dom,'primary');
12763:     } else {
12764:         $home = &Apache::lonnet::homeserver($confname,$dom);
12765:         if ($home eq 'no_host') {
12766:             $home = &Apache::lonnet::domain($dom,'primary');
12767:         }
12768:     }
12769:     my @ids=&Apache::lonnet::current_machine_ids();
12770:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
12771:     if (!$allowed) {
12772: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role='.
12773:                       &HTML::Entities::encode($env{'request.role'},'\'<>"&').
12774:                       '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
12775:     }
12776:     return $switchserver;
12777: }
12778: 
12779: sub modify_quotas {
12780:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
12781:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
12782:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
12783:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
12784:         $validationfieldsref);
12785:     if ($action eq 'quotas') {
12786:         $context = 'tools';
12787:     } else {
12788:         $context = $action;
12789:     }
12790:     if ($context eq 'requestcourses') {
12791:         @usertools = ('official','unofficial','community','textbook','lti');
12792:         @options =('norequest','approval','validate','autolimit');
12793:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
12794:         %titles = &courserequest_titles();
12795:         $toolregexp = join('|',@usertools);
12796:         %conditions = &courserequest_conditions();
12797:         $confname = $dom.'-domainconfig';
12798:         my $servadm = $r->dir_config('lonAdmEMail');
12799:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12800:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
12801:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
12802:     } elsif ($context eq 'requestauthor') {
12803:         @usertools = ('author');
12804:         %titles = &authorrequest_titles();
12805:     } else {
12806:         @usertools = ('aboutme','blog','portfolio','timezone');
12807:         %titles = &tool_titles();
12808:     }
12809:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12810:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12811:     foreach my $key (keys(%env)) {
12812:         if ($context eq 'requestcourses') {
12813:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
12814:                 my $item = $1;
12815:                 my $type = $2;
12816:                 if ($type =~ /^limit_(.+)/) {
12817:                     $limithash{$item}{$1} = $env{$key};
12818:                 } else {
12819:                     $confhash{$item}{$type} = $env{$key};
12820:                 }
12821:             }
12822:         } elsif ($context eq 'requestauthor') {
12823:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
12824:                 $confhash{$1} = $env{$key};
12825:             }
12826:         } else {
12827:             if ($key =~ /^form\.quota_(.+)$/) {
12828:                 $confhash{'defaultquota'}{$1} = $env{$key};
12829:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
12830:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
12831:             }
12832:         }
12833:     }
12834:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
12835:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
12836:         @approvalnotify = sort(@approvalnotify);
12837:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
12838:         my @crstypes = ('official','unofficial','community','textbook','lti');
12839:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
12840:         foreach my $type (@hasuniquecode) {
12841:             if (grep(/^\Q$type\E$/,@crstypes)) {
12842:                 $confhash{'uniquecode'}{$type} = 1;
12843:             }
12844:         }
12845:         my (%newbook,%allpos);
12846:         if ($context eq 'requestcourses') {
12847:             foreach my $type ('textbooks','templates') {
12848:                 @{$allpos{$type}} = (); 
12849:                 my $invalid;
12850:                 if ($type eq 'textbooks') {
12851:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
12852:                 } else {
12853:                     $invalid = &mt('Invalid LON-CAPA course for template');
12854:                 }
12855:                 if ($env{'form.'.$type.'_addbook'}) {
12856:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
12857:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
12858:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
12859:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
12860:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
12861:                         } else {
12862:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
12863:                             my $position = $env{'form.'.$type.'_addbook_pos'};
12864:                             $position =~ s/\D+//g;
12865:                             if ($position ne '') {
12866:                                 $allpos{$type}[$position] = $newbook{$type};
12867:                             }
12868:                         }
12869:                     } else {
12870:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
12871:                     }
12872:                 }
12873:             } 
12874:         }
12875:         if (ref($domconfig{$action}) eq 'HASH') {
12876:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
12877:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
12878:                     $changes{'notify'}{'approval'} = 1;
12879:                 }
12880:             } else {
12881:                 if ($confhash{'notify'}{'approval'}) {
12882:                     $changes{'notify'}{'approval'} = 1;
12883:                 }
12884:             }
12885:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
12886:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
12887:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
12888:                         unless ($confhash{'uniquecode'}{$crstype}) {
12889:                             $changes{'uniquecode'} = 1;
12890:                         }
12891:                     }
12892:                     unless ($changes{'uniquecode'}) {
12893:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
12894:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
12895:                                 $changes{'uniquecode'} = 1;
12896:                             }
12897:                         }
12898:                     }
12899:                } else {
12900:                    $changes{'uniquecode'} = 1;
12901:                }
12902:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
12903:                 $changes{'uniquecode'} = 1;
12904:             }
12905:             if ($context eq 'requestcourses') {
12906:                 foreach my $type ('textbooks','templates') {
12907:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
12908:                         my %deletions;
12909:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
12910:                         if (@todelete) {
12911:                             map { $deletions{$_} = 1; } @todelete;
12912:                         }
12913:                         my %imgdeletions;
12914:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
12915:                         if (@todeleteimages) {
12916:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
12917:                         }
12918:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
12919:                         for (my $i=0; $i<=$maxnum; $i++) {
12920:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
12921:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
12922:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
12923:                                 if ($deletions{$key}) {
12924:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
12925:                                         #FIXME need to obsolete item in RES space
12926:                                     }
12927:                                     next;
12928:                                 } else {
12929:                                     my $newpos = $env{'form.'.$itemid};
12930:                                     $newpos =~ s/\D+//g;
12931:                                     foreach my $item ('subject','title','publisher','author') {
12932:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
12933:                                                  ($type eq 'templates'));
12934:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
12935:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
12936:                                             $changes{$type}{$key} = 1;
12937:                                         }
12938:                                     }
12939:                                     $allpos{$type}[$newpos] = $key;
12940:                                 }
12941:                                 if ($imgdeletions{$key}) {
12942:                                     $changes{$type}{$key} = 1;
12943:                                     #FIXME need to obsolete item in RES space
12944:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
12945:                                     my ($cdom,$cnum) = split(/_/,$key);
12946:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
12947:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
12948:                                     } else {
12949:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
12950:                                                                                       $cdom,$cnum,$type,$configuserok,
12951:                                                                                       $switchserver,$author_ok);
12952:                                         if ($imgurl) {
12953:                                             $confhash{$type}{$key}{'image'} = $imgurl;
12954:                                             $changes{$type}{$key} = 1; 
12955:                                         }
12956:                                         if ($error) {
12957:                                             &Apache::lonnet::logthis($error);
12958:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12959:                                         }
12960:                                     }
12961:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
12962:                                     $confhash{$type}{$key}{'image'} = 
12963:                                         $domconfig{$action}{$type}{$key}{'image'};
12964:                                 }
12965:                             }
12966:                         }
12967:                     }
12968:                 }
12969:             }
12970:         } else {
12971:             if ($confhash{'notify'}{'approval'}) {
12972:                 $changes{'notify'}{'approval'} = 1;
12973:             }
12974:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
12975:                 $changes{'uniquecode'} = 1;
12976:             }
12977:         }
12978:         if ($context eq 'requestcourses') {
12979:             foreach my $type ('textbooks','templates') {
12980:                 if ($newbook{$type}) {
12981:                     $changes{$type}{$newbook{$type}} = 1;
12982:                     foreach my $item ('subject','title','publisher','author') {
12983:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
12984:                                  ($type eq 'template'));
12985:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
12986:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
12987:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
12988:                         }
12989:                     }
12990:                     if ($type eq 'textbooks') {
12991:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
12992:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
12993:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
12994:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
12995:                             } else {
12996:                                 my ($imageurl,$error) =
12997:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
12998:                                                             $configuserok,$switchserver,$author_ok);
12999:                                 if ($imageurl) {
13000:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
13001:                                 }
13002:                                 if ($error) {
13003:                                     &Apache::lonnet::logthis($error);
13004:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13005:                                 }
13006:                             }
13007:                         }
13008:                     }
13009:                 }
13010:                 if (@{$allpos{$type}} > 0) {
13011:                     my $idx = 0;
13012:                     foreach my $item (@{$allpos{$type}}) {
13013:                         if ($item ne '') {
13014:                             $confhash{$type}{$item}{'order'} = $idx;
13015:                             if (ref($domconfig{$action}) eq 'HASH') {
13016:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
13017:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
13018:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
13019:                                             $changes{$type}{$item} = 1;
13020:                                         }
13021:                                     }
13022:                                 }
13023:                             }
13024:                             $idx ++;
13025:                         }
13026:                     }
13027:                 }
13028:             }
13029:             if (ref($validationitemsref) eq 'ARRAY') {
13030:                 foreach my $item (@{$validationitemsref}) {
13031:                     if ($item eq 'fields') {
13032:                         my @changed;
13033:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
13034:                         if (@{$confhash{'validation'}{$item}} > 0) {
13035:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
13036:                         }
13037:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13038:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13039:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
13040:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
13041:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
13042:                                 } else {
13043:                                     @changed = @{$confhash{'validation'}{$item}};
13044:                                 }
13045:                             } else {
13046:                                 @changed = @{$confhash{'validation'}{$item}};
13047:                             }
13048:                         } else {
13049:                             @changed = @{$confhash{'validation'}{$item}};
13050:                         }
13051:                         if (@changed) {
13052:                             if ($confhash{'validation'}{$item}) {
13053:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
13054:                             } else {
13055:                                 $changes{'validation'}{$item} = &mt('None');
13056:                             }
13057:                         }
13058:                     } else {
13059:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
13060:                         if ($item eq 'markup') {
13061:                             if ($env{'form.requestcourses_validation_'.$item}) {
13062:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13063:                             }
13064:                         }
13065:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13066:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13067:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
13068:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13069:                                 }
13070:                             } else {
13071:                                 if ($confhash{'validation'}{$item} ne '') {
13072:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13073:                                 }
13074:                             }
13075:                         } else {
13076:                             if ($confhash{'validation'}{$item} ne '') {
13077:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13078:                             }
13079:                         }
13080:                     }
13081:                 }
13082:             }
13083:             if ($env{'form.validationdc'}) {
13084:                 my $newval = $env{'form.validationdc'};
13085:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
13086:                 if (exists($domcoords{$newval})) {
13087:                     $confhash{'validation'}{'dc'} = $newval;
13088:                 }
13089:             }
13090:             if (ref($confhash{'validation'}) eq 'HASH') {
13091:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13092:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13093:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
13094:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
13095:                                 if ($confhash{'validation'}{'dc'} eq '') {
13096:                                     $changes{'validation'}{'dc'} = &mt('None');
13097:                                 } else {
13098:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13099:                                 }
13100:                             }
13101:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
13102:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13103:                         }
13104:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
13105:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13106:                     }
13107:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
13108:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13109:                 }
13110:             } else {
13111:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13112:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13113:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
13114:                             $changes{'validation'}{'dc'} = &mt('None');
13115:                         }
13116:                     }
13117:                 }
13118:             }
13119:         }
13120:     } else {
13121:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
13122:     }
13123:     foreach my $item (@usertools) {
13124:         foreach my $type (@{$types},'default','_LC_adv') {
13125:             my $unset; 
13126:             if ($context eq 'requestcourses') {
13127:                 $unset = '0';
13128:                 if ($type eq '_LC_adv') {
13129:                     $unset = '';
13130:                 }
13131:                 if ($confhash{$item}{$type} eq 'autolimit') {
13132:                     $confhash{$item}{$type} .= '=';
13133:                     unless ($limithash{$item}{$type} =~ /\D/) {
13134:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
13135:                     }
13136:                 }
13137:             } elsif ($context eq 'requestauthor') {
13138:                 $unset = '0';
13139:                 if ($type eq '_LC_adv') {
13140:                     $unset = '';
13141:                 }
13142:             } else {
13143:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
13144:                     $confhash{$item}{$type} = 1;
13145:                 } else {
13146:                     $confhash{$item}{$type} = 0;
13147:                 }
13148:             }
13149:             if (ref($domconfig{$action}) eq 'HASH') {
13150:                 if ($action eq 'requestauthor') {
13151:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
13152:                         $changes{$type} = 1;
13153:                     }
13154:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
13155:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
13156:                         $changes{$item}{$type} = 1;
13157:                     }
13158:                 } else {
13159:                     if ($context eq 'requestcourses') {
13160:                         if ($confhash{$item}{$type} ne $unset) {
13161:                             $changes{$item}{$type} = 1;
13162:                         }
13163:                     } else {
13164:                         if (!$confhash{$item}{$type}) {
13165:                             $changes{$item}{$type} = 1;
13166:                         }
13167:                     }
13168:                 }
13169:             } else {
13170:                 if ($context eq 'requestcourses') {
13171:                     if ($confhash{$item}{$type} ne $unset) {
13172:                         $changes{$item}{$type} = 1;
13173:                     }
13174:                 } elsif ($context eq 'requestauthor') {
13175:                     if ($confhash{$type} ne $unset) {
13176:                         $changes{$type} = 1;
13177:                     }
13178:                 } else {
13179:                     if (!$confhash{$item}{$type}) {
13180:                         $changes{$item}{$type} = 1;
13181:                     }
13182:                 }
13183:             }
13184:         }
13185:     }
13186:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
13187:         if (ref($domconfig{'quotas'}) eq 'HASH') {
13188:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
13189:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
13190:                     if (exists($confhash{'defaultquota'}{$key})) {
13191:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
13192:                             $changes{'defaultquota'}{$key} = 1;
13193:                         }
13194:                     } else {
13195:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
13196:                     }
13197:                 }
13198:             } else {
13199:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
13200:                     if (exists($confhash{'defaultquota'}{$key})) {
13201:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
13202:                             $changes{'defaultquota'}{$key} = 1;
13203:                         }
13204:                     } else {
13205:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
13206:                     }
13207:                 }
13208:             }
13209:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
13210:                 $confhash{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
13211:             }
13212:             if (ref($domconfig{'quotas'}{'webdav'}) eq 'HASH') {
13213:                 $confhash{'webdav'} = $domconfig{'quotas'}{'webdav'};
13214:             }
13215:         }
13216:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
13217:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
13218:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
13219:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
13220:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
13221:                             $changes{'defaultquota'}{$key} = 1;
13222:                         }
13223:                     } else {
13224:                         if (!exists($domconfig{'quotas'}{$key})) {
13225:                             $changes{'defaultquota'}{$key} = 1;
13226:                         }
13227:                     }
13228:                 } else {
13229:                     $changes{'defaultquota'}{$key} = 1;
13230:                 }
13231:             }
13232:         }
13233:     }
13234: 
13235:     if ($context eq 'requestauthor') {
13236:         $domdefaults{'requestauthor'} = \%confhash;
13237:     } else {
13238:         foreach my $key (keys(%confhash)) {
13239:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
13240:                 $domdefaults{$key} = $confhash{$key};
13241:             }
13242:         }
13243:     }
13244: 
13245:     my %quotahash = (
13246:                       $action => { %confhash }
13247:                     );
13248:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
13249:                                              $dom);
13250:     if ($putresult eq 'ok') {
13251:         if (keys(%changes) > 0) {
13252:             my $cachetime = 24*60*60;
13253:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13254:             if (ref($lastactref) eq 'HASH') {
13255:                 $lastactref->{'domdefaults'} = 1;
13256:             }
13257:             $resulttext = &mt('Changes made:').'<ul>';
13258:             unless (($context eq 'requestcourses') ||
13259:                     ($context eq 'requestauthor')) {
13260:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
13261:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
13262:                     foreach my $type (@{$types},'default') {
13263:                         if (defined($changes{'defaultquota'}{$type})) {
13264:                             my $typetitle = $usertypes->{$type};
13265:                             if ($type eq 'default') {
13266:                                 $typetitle = $othertitle;
13267:                             }
13268:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
13269:                         }
13270:                     }
13271:                     $resulttext .= '</ul></li>';
13272:                 }
13273:             }
13274:             my %newenv;
13275:             foreach my $item (@usertools) {
13276:                 my (%haschgs,%inconf);
13277:                 if ($context eq 'requestauthor') {
13278:                     %haschgs = %changes;
13279:                     %inconf = %confhash;
13280:                 } else {
13281:                     if (ref($changes{$item}) eq 'HASH') {
13282:                         %haschgs = %{$changes{$item}};
13283:                     }
13284:                     if (ref($confhash{$item}) eq 'HASH') {
13285:                         %inconf = %{$confhash{$item}};
13286:                     }
13287:                 }
13288:                 if (keys(%haschgs) > 0) {
13289:                     my $newacc = 
13290:                         &Apache::lonnet::usertools_access($env{'user.name'},
13291:                                                           $env{'user.domain'},
13292:                                                           $item,'reload',$context);
13293:                     if (($context eq 'requestcourses') ||
13294:                         ($context eq 'requestauthor')) {
13295:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
13296:                             $newenv{'environment.canrequest.'.$item} = $newacc;
13297:                         }
13298:                     } else {
13299:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
13300:                             $newenv{'environment.availabletools.'.$item} = $newacc;
13301:                         }
13302:                     }
13303:                     unless ($context eq 'requestauthor') {
13304:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
13305:                     }
13306:                     foreach my $type (@{$types},'default','_LC_adv') {
13307:                         if ($haschgs{$type}) {
13308:                             my $typetitle = $usertypes->{$type};
13309:                             if ($type eq 'default') {
13310:                                 $typetitle = $othertitle;
13311:                             } elsif ($type eq '_LC_adv') {
13312:                                 $typetitle = 'LON-CAPA Advanced Users'; 
13313:                             }
13314:                             if ($inconf{$type}) {
13315:                                 if ($context eq 'requestcourses') {
13316:                                     my $cond;
13317:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
13318:                                         if ($1 eq '') {
13319:                                             $cond = &mt('(Automatic processing of any request).');
13320:                                         } else {
13321:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
13322:                                         }
13323:                                     } else { 
13324:                                         $cond = $conditions{$inconf{$type}};
13325:                                     }
13326:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
13327:                                 } elsif ($context eq 'requestauthor') {
13328:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
13329:                                                              $titles{$inconf{$type}},$typetitle);
13330: 
13331:                                 } else {
13332:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
13333:                                 }
13334:                             } else {
13335:                                 if ($type eq '_LC_adv') {
13336:                                     if ($inconf{$type} eq '0') {
13337:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
13338:                                     } else { 
13339:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
13340:                                     }
13341:                                 } else {
13342:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
13343:                                 }
13344:                             }
13345:                         }
13346:                     }
13347:                     unless ($context eq 'requestauthor') {
13348:                         $resulttext .= '</ul></li>';
13349:                     }
13350:                 }
13351:             }
13352:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
13353:                 if (ref($changes{'notify'}) eq 'HASH') {
13354:                     if ($changes{'notify'}{'approval'}) {
13355:                         if (ref($confhash{'notify'}) eq 'HASH') {
13356:                             if ($confhash{'notify'}{'approval'}) {
13357:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
13358:                             } else {
13359:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
13360:                             }
13361:                         }
13362:                     }
13363:                 }
13364:             }
13365:             if ($action eq 'requestcourses') {
13366:                 my @offon = ('off','on');
13367:                 if ($changes{'uniquecode'}) {
13368:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
13369:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
13370:                         $resulttext .= '<li>'.
13371:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
13372:                                        '</li>';
13373:                     } else {
13374:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
13375:                                        '</li>';
13376:                     }
13377:                 }
13378:                 foreach my $type ('textbooks','templates') {
13379:                     if (ref($changes{$type}) eq 'HASH') {
13380:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
13381:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
13382:                             my %coursehash = &Apache::lonnet::coursedescription($key);
13383:                             my $coursetitle = $coursehash{'description'};
13384:                             my $position = $confhash{$type}{$key}{'order'} + 1;
13385:                             $resulttext .= '<li>';
13386:                             foreach my $item ('subject','title','publisher','author') {
13387:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
13388:                                          ($type eq 'templates'));
13389:                                 my $name = $item.':';
13390:                                 $name =~ s/^(\w)/\U$1/;
13391:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
13392:                             }
13393:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
13394:                             if ($type eq 'textbooks') {
13395:                                 if ($confhash{$type}{$key}{'image'}) {
13396:                                     $resulttext .= ' '.&mt('Image: [_1]',
13397:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
13398:                                                    ' alt="Textbook cover" />').'<br />';
13399:                                 }
13400:                             }
13401:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
13402:                         }
13403:                         $resulttext .= '</ul></li>';
13404:                     }
13405:                 }
13406:                 if (ref($changes{'validation'}) eq 'HASH') {
13407:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
13408:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
13409:                         foreach my $item (@{$validationitemsref}) {
13410:                             if (exists($changes{'validation'}{$item})) {
13411:                                 if ($item eq 'markup') {
13412:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
13413:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
13414:                                 } else {
13415:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
13416:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
13417:                                 }
13418:                             }
13419:                         }
13420:                         if (exists($changes{'validation'}{'dc'})) {
13421:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
13422:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
13423:                         }
13424:                     }
13425:                 }
13426:             }
13427:             $resulttext .= '</ul>';
13428:             if (keys(%newenv)) {
13429:                 &Apache::lonnet::appenv(\%newenv);
13430:             }
13431:         } else {
13432:             if ($context eq 'requestcourses') {
13433:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
13434:             } elsif ($context eq 'requestauthor') {
13435:                 $resulttext = &mt('No changes made to rights to request author space.');
13436:             } else {
13437:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
13438:             }
13439:         }
13440:     } else {
13441:         $resulttext = '<span class="LC_error">'.
13442: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13443:     }
13444:     if ($errors) {
13445:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
13446:                        '<ul>'.$errors.'</ul></p>';
13447:     }
13448:     return $resulttext;
13449: }
13450: 
13451: sub process_textbook_image {
13452:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
13453:     my $filename = $env{'form.'.$caller.'.filename'};
13454:     my ($error,$url);
13455:     my ($width,$height) = (50,50);
13456:     if ($configuserok eq 'ok') {
13457:         if ($switchserver) {
13458:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
13459:                          $switchserver);
13460:         } elsif ($author_ok eq 'ok') {
13461:             my $modified = [];
13462:             my ($result,$imageurl) =
13463:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
13464:                                                         "$type/$cdom/$cnum/cover",$width,$height,
13465:                                                         '',$modified);
13466:             if ($result eq 'ok') {
13467:                 $url = $imageurl;
13468:                 &update_modify_urls($r,$modified);
13469:             } else {
13470:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
13471:             }
13472:         } else {
13473:             $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);
13474:         }
13475:     } else {
13476:         $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);
13477:     }
13478:     return ($url,$error);
13479: }
13480: 
13481: sub modify_ltitools {
13482:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13483:     my (%currtoolsec,%secchanges,%newtoolsec,%newkeyset);
13484:     &fetch_secrets($dom,'toolsec',\%domconfig,\%currtoolsec,\%secchanges,\%newtoolsec,\%newkeyset);
13485: 
13486:     my $confname = $dom.'-domainconfig';
13487:     my $servadm = $r->dir_config('lonAdmEMail');
13488:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
13489: 
13490:     my ($resulttext,$ltitoolsoutput,$is_home,$errors,%ltitoolschg,%newtoolsenc,%newltitools);
13491:     my $toolserror =
13492:         &Apache::courseprefs::process_ltitools($r,$dom,$confname,$domconfig{'ltitools'},\%ltitoolschg,'domain',
13493:                                                $lastactref,$configuserok,$switchserver,$author_ok);
13494: 
13495:     my $home = &Apache::lonnet::domain($dom,'primary');
13496:     unless (($home eq 'no_host') || ($home eq '')) {
13497:         my @ids=&Apache::lonnet::current_machine_ids();
13498:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; last; } }
13499:     }
13500: 
13501:     if (keys(%ltitoolschg)) {
13502:         foreach my $id (keys(%ltitoolschg)) {
13503:             if (ref($ltitoolschg{$id}) eq 'HASH') {
13504:                 foreach my $inner (keys(%{$ltitoolschg{$id}})) {
13505:                     if (($inner eq 'secret') || ($inner eq 'key')) {
13506:                         if ($is_home) {
13507:                             $newtoolsenc{$id}{$inner} = $ltitoolschg{$id}{$inner};
13508:                         }
13509:                     }
13510:                 }
13511:             }
13512:         }
13513:         $ltitoolsoutput = &Apache::courseprefs::store_ltitools($dom,'','domain',\%ltitoolschg,$domconfig{'ltitools'});
13514:         if (keys(%ltitoolschg)) {
13515:             %newltitools = %ltitoolschg;
13516:         }
13517:     }
13518:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
13519:         foreach my $id (%{$domconfig{'ltitools'}}) {
13520:             next if ($id !~ /^\d+$/);
13521:             unless (exists($ltitoolschg{$id})) {
13522:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
13523:                     foreach my $inner (keys(%{$domconfig{'ltitools'}{$id}})) {
13524:                         if (($inner eq 'secret') || ($inner eq 'key')) {
13525:                             if ($is_home) {
13526:                                 $newtoolsenc{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
13527:                             }
13528:                         } else {
13529:                             $newltitools{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
13530:                         }
13531:                     }
13532:                 } else {
13533:                     $newltitools{$id} = $domconfig{'ltitools'}{$id};
13534:                 }
13535:             }
13536:         }
13537:     }
13538:     if ($toolserror) {
13539:         $errors = '<li>'.$toolserror.'</li>';
13540:     }
13541:     if ((keys(%ltitoolschg) == 0) && (keys(%secchanges) == 0)) {
13542:         $resulttext = &mt('No changes made.');
13543:         if ($errors) {
13544:             $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13545:                                  $errors.'</ul>';
13546:         }
13547:         return $resulttext;
13548:     }
13549:     my %ltitoolshash = (
13550:                           $action => { %newltitools }
13551:                        );
13552:     if (keys(%secchanges)) {
13553:         $ltitoolshash{'toolsec'} = \%newtoolsec;
13554:     }
13555:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,$dom);
13556:     if ($putresult eq 'ok') {
13557:         my %keystore;
13558:         if ($is_home) {
13559:             my %toolsenchash = (
13560:                                    $action => { %newtoolsenc }
13561:                                );
13562:             &Apache::lonnet::put_dom('encconfig',\%toolsenchash,$dom,undef,1);
13563:             my $cachetime = 24*60*60;
13564:             &Apache::lonnet::do_cache_new('ltitoolsenc',$dom,\%newtoolsenc,$cachetime);
13565:             &store_security($dom,'ltitools',\%secchanges,\%newkeyset,\%keystore,$lastactref);
13566:         }
13567:         $resulttext = &mt('Changes made:').'<ul>';
13568:         if (keys(%secchanges) > 0) {
13569:             $resulttext .= &lti_security_results($dom,'ltitools',\%secchanges,\%newtoolsec,\%newkeyset,\%keystore);
13570:         }
13571:         if (keys(%ltitoolschg) > 0) {
13572:             $resulttext .= $ltitoolsoutput;
13573:         }
13574:         my $cachetime = 24*60*60;
13575:         &Apache::lonnet::do_cache_new('ltitools',$dom,\%newltitools,$cachetime);
13576:         if (ref($lastactref) eq 'HASH') {
13577:             $lastactref->{'ltitools'} = 1;
13578:         }
13579:     } else {
13580:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
13581:     }
13582:     if ($errors) {
13583:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
13584:                        $errors.'</ul></p>';
13585:     }
13586:     return $resulttext;
13587: }
13588: 
13589: sub fetch_secrets {
13590:     my ($dom,$context,$domconfig,$currsec,$secchanges,$newsec,$newkeyset) = @_;
13591:     my %keyset;
13592:     %{$currsec} = ();
13593:     $newsec->{'private'}{'keys'} = [];
13594:     $newsec->{'encrypt'} = {};
13595:     $newsec->{'rules'} = {};
13596:     if ($context eq 'ltisec') {
13597:         $newsec->{'linkprot'} = {};
13598:     }
13599:     if (ref($domconfig->{$context}) eq 'HASH') {
13600:         %{$currsec} = %{$domconfig->{$context}};
13601:         if ($context eq 'ltisec') {
13602:             if (ref($currsec->{'linkprot'}) eq 'HASH') {
13603:                 foreach my $id (keys(%{$currsec->{'linkprot'}})) {
13604:                     unless ($id =~ /^\d+$/) {
13605:                         delete($currsec->{'linkprot'}{$id});
13606:                     }
13607:                 }
13608:             }
13609:         }
13610:         if (ref($currsec->{'private'}) eq 'HASH') {
13611:             if (ref($currsec->{'private'}{'keys'}) eq 'ARRAY') {
13612:                 $newsec->{'private'}{'keys'} = $currsec->{'private'}{'keys'};
13613:                 map { $keyset{$_} = 1; } @{$currsec->{'private'}{'keys'}};
13614:             }
13615:         }
13616:     }
13617:     my @items= ('crs','dom');
13618:     if ($context eq 'ltisec') {
13619:         push(@items,'consumers');
13620:     }
13621:     foreach my $item (@items) {
13622:         my $formelement;
13623:         if (($context eq 'toolsec') || ($item eq 'consumers')) {
13624:             $formelement = 'form.'.$context.'_'.$item;
13625:         } else {
13626:             $formelement = 'form.'.$context.'_'.$item.'linkprot';
13627:         }
13628:         if ($env{$formelement}) {
13629:             $newsec->{'encrypt'}{$item} = 1;
13630:             if (ref($currsec->{'encrypt'}) eq 'HASH') {
13631:                 unless ($currsec->{'encrypt'}{$item}) {
13632:                     $secchanges->{'encrypt'} = 1;
13633:                 }
13634:             } else {
13635:                 $secchanges->{'encrypt'} = 1;
13636:             }
13637:         } elsif (ref($currsec->{'encrypt'}) eq 'HASH') {
13638:             if ($currsec->{'encrypt'}{$item}) {
13639:                 $secchanges->{'encrypt'} = 1;
13640:             }
13641:         }
13642:     }
13643:     my $secrets;
13644:     if ($context eq 'ltisec') {
13645:         $secrets = 'ltisecrets';
13646:     } else {
13647:         $secrets = 'toolsecrets';
13648:     }
13649:     unless (exists($currsec->{'rules'})) {
13650:         $currsec->{'rules'} = {};
13651:     }
13652:     &password_rule_changes($secrets,$newsec->{'rules'},$currsec->{'rules'},$secchanges);
13653: 
13654:     my @ids=&Apache::lonnet::current_machine_ids();
13655:     my %servers = &Apache::lonnet::get_servers($dom,'library');
13656: 
13657:     foreach my $hostid (keys(%servers)) {
13658:         if (($hostid ne '') && (grep(/^\Q$hostid\E$/,@ids))) {
13659:             my $keyitem = 'form.'.$context.'_privkey_'.$hostid;
13660:             if (exists($env{$keyitem})) {
13661:                 $env{$keyitem} =~ s/(`)/'/g;
13662:                 if ($keyset{$hostid}) {
13663:                     if ($env{'form.'.$context.'_changeprivkey_'.$hostid}) {
13664:                         if ($env{$keyitem} ne '') {
13665:                             $secchanges->{'private'} = 1;
13666:                             $newkeyset->{$hostid} = $env{$keyitem};
13667:                         }
13668:                     }
13669:                 } elsif ($env{$keyitem} ne '') {
13670:                     unless (grep(/^\Q$hostid\E$/,@{$newsec->{'private'}{'keys'}})) {
13671:                         push(@{$newsec->{'private'}{'keys'}},$hostid);
13672:                     }
13673:                     $secchanges->{'private'} = 1;
13674:                     $newkeyset->{$hostid} = $env{$keyitem};
13675:                 }
13676:             }
13677:         }
13678:     }
13679: }
13680: 
13681: sub store_security {
13682:     my ($dom,$context,$secchanges,$newkeyset,$keystore) = @_;
13683:     return unless ((ref($secchanges) eq 'HASH') && (ref($newkeyset) eq 'HASH') &&
13684:                    (ref($keystore) eq 'HASH'));
13685:     if (keys(%{$secchanges})) {
13686:         if ($secchanges->{'private'}) {
13687:             my $who = &escape($env{'user.name'}.':'.$env{'user.domain'});
13688:             foreach my $hostid (keys(%{$newkeyset})) {
13689:                 my $storehash = {
13690:                                    key => $newkeyset->{$hostid},
13691:                                    who => $env{'user.name'}.':'.$env{'user.domain'},
13692:                                 };
13693:                 $keystore->{$hostid} = &Apache::lonnet::store_dom($storehash,$context,'private',
13694:                                                                   $dom,$hostid);
13695:             }
13696:         }
13697:     }
13698: }
13699: 
13700: sub lti_security_results {
13701:     my ($dom,$context,$secchanges,$newsec,$newkeyset,$keystore) = @_;
13702:     my $output;
13703:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
13704:     my $needs_update;
13705:     foreach my $item (keys(%{$secchanges})) {
13706:         if ($item eq 'encrypt') {
13707:             $needs_update = 1;
13708:             my %encrypted;
13709:             if ($context eq 'lti') {
13710:                 %encrypted = (
13711:                               crs  => {
13712:                                         on => &mt('Encryption of stored link protection secrets defined in courses enabled'),
13713:                                         off => &mt('Encryption of stored link protection secrets defined in courses disabled'),
13714:                                       },
13715:                               dom => {
13716:                                        on => &mt('Encryption of stored link protection secrets defined in domain enabled'),
13717:                                        off => &mt('Encryption of stored link protection secrets defined in domain disabled'),
13718:                                      },
13719:                               consumers => {
13720:                                              on => &mt('Encryption of stored consumer secrets defined in domain enabled'),
13721:                                              off => &mt('Encryption of stored consumer secrets defined in domain disabled'),
13722:                                            },
13723:                              );
13724:             } else {
13725:                 %encrypted = (
13726:                               crs  => {
13727:                                         on => &mt('Encryption of stored external tool secrets defined in courses enabled'),
13728:                                         off => &mt('Encryption of stored external tool secrets defined in courses disabled'),
13729:                                       },
13730:                               dom => {
13731:                                        on => &mt('Encryption of stored external tool secrets defined in domain enabled'),
13732:                                        off => &mt('Encryption of stored external tool secrets defined in domain disabled'),
13733:                                      },
13734:                              );
13735:             }
13736:             my @types= ('crs','dom');
13737:             if ($context eq 'lti') {
13738:                 foreach my $type (@types) {
13739:                     undef($domdefaults{'linkprotenc_'.$type});
13740:                 }
13741:                 push(@types,'consumers');
13742:                 undef($domdefaults{'ltienc_consumers'});
13743:             } elsif ($context eq 'ltitools') {
13744:                 foreach my $type (@types) {
13745:                     undef($domdefaults{'toolenc_'.$type});
13746:                 }
13747:             }
13748:             foreach my $type (@types) {
13749:                 my $shown = $encrypted{$type}{'off'};
13750:                 if (ref($newsec->{$item}) eq 'HASH') {
13751:                     if ($newsec->{$item}{$type}) {
13752:                         if ($context eq 'lti') {
13753:                             if ($type eq 'consumers') {
13754:                                 $domdefaults{'ltienc_consumers'} = 1;
13755:                             } else {
13756:                                 $domdefaults{'linkprotenc_'.$type} = 1;
13757:                             }
13758:                         } elsif ($context eq 'ltitools') {
13759:                             $domdefaults{'toolenc_'.$type} = 1;
13760:                         }
13761:                         $shown = $encrypted{$type}{'on'};
13762:                     }
13763:                 }
13764:                 $output .= '<li>'.$shown.'</li>';
13765:             }
13766:         } elsif ($item eq 'rules') {
13767:             my %titles = &Apache::lonlocal::texthash(
13768:                                       min   => 'Minimum password length',
13769:                                       max   => 'Maximum password length',
13770:                                       chars => 'Required characters',
13771:                          );
13772:             foreach my $rule ('min','max') {
13773:                 if ($newsec->{rules}{$rule} eq '') {
13774:                     if ($rule eq 'min') {
13775:                         $output .= '<li>'.&mt('[_1] not set.',$titles{$rule});
13776:                                    ' '.&mt('Default of [_1] will be used',
13777:                                            $Apache::lonnet::passwdmin).'</li>';
13778:                     } else {
13779:                         $output .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
13780:                     }
13781:                 } else {
13782:                     $output .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$newsec->{rules}{$rule}).'</li>';
13783:                 }
13784:             }
13785:             if (ref($newsec->{'rules'}{'chars'}) eq 'ARRAY') {
13786:                 if (@{$newsec->{'rules'}{'chars'}} > 0) {
13787:                     my %rulenames = &Apache::lonlocal::texthash(
13788:                                              uc => 'At least one upper case letter',
13789:                                              lc => 'At least one lower case letter',
13790:                                              num => 'At least one number',
13791:                                              spec => 'At least one non-alphanumeric',
13792:                                     );
13793:                     my $needed = '<ul><li>'.
13794:                                  join('</li><li>',map {$rulenames{$_} } @{$newsec->{'rules'}{'chars'}}).
13795:                                  '</li></ul>';
13796:                     $output .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
13797:                 } else {
13798:                     $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
13799:                 }
13800:             } else {
13801:                 $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
13802:             }
13803:         } elsif ($item eq 'private') {
13804:             $needs_update = 1;
13805:             if ($context eq 'lti') {
13806:                 undef($domdefaults{'ltiprivhosts'});
13807:             } elsif ($context eq 'ltitools') {
13808:                 undef($domdefaults{'toolprivhosts'});
13809:             }
13810:             if (keys(%{$newkeyset})) {
13811:                 my @privhosts;
13812:                 foreach my $hostid (sort(keys(%{$newkeyset}))) {
13813:                     if ($keystore->{$hostid} eq 'ok') {
13814:                         $output .= '<li>'.&mt('Encryption key for storage of shared secrets saved for [_1]',$hostid).'</li>';
13815:                         unless (grep(/^\Q$hostid\E$/,@privhosts)) {
13816:                             push(@privhosts,$hostid);
13817:                         }
13818:                     }
13819:                 }
13820:                 if (@privhosts) {
13821:                     if ($context eq 'lti') {
13822:                         $domdefaults{'ltiprivhosts'} = \@privhosts;
13823:                     } elsif ($context eq 'ltitools') {
13824:                         $domdefaults{'toolprivhosts'} = \@privhosts;
13825:                     }
13826:                 }
13827:             }
13828:         } elsif ($item eq 'linkprot') {
13829:             next;
13830:         } elsif ($item eq 'suggested') {
13831:             if ((ref($secchanges->{'suggested'}) eq 'HASH') &&
13832:                 (ref($newsec->{'suggested'}) eq 'HASH')) {
13833:                 my $suggestions;
13834:                 foreach my $id (sort { $a <=> $b } keys(%{$secchanges->{'suggested'}})) {
13835:                     if (ref($newsec->{'suggested'}->{$id}) eq 'HASH') {
13836:                         my $name = $newsec->{'suggested'}->{$id}->{'name'};
13837:                         my $info = $newsec->{'suggested'}->{$id}->{'info'};
13838:                         $suggestions .= '<li>'.&mt('Launcher: [_1]',$name).'<br />'.
13839:                                                &mt('Recommend: [_1]','<pre>'.$info.'</pre>').
13840:                                         '</li>';
13841:                     } else {
13842:                         $suggestions .= '<li>'.&mt('Recommendations deleted for Launcher: [_1]',
13843:                                                    $newsec->{'suggested'}->{$id}).'</li>';
13844:                     }
13845:                 }
13846:                 if ($suggestions) {
13847:                     $output .= '<li>'.&mt('Hints in Courses for Link Protector Configuration').
13848:                                '<ul>'.$suggestions.'</ul>'.
13849:                                '</li>';
13850:                 }
13851:             }
13852:         }
13853:     }
13854:     if ($needs_update) {
13855:         my $cachetime = 24*60*60;
13856:         &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13857:     }
13858:     return $output;
13859: }
13860: 
13861: sub modify_lti {
13862:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13863:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13864:     my ($newid,@allpos,%changes,%confhash,%ltienc,$errors,$resulttext);
13865:     my (%posslti,%posslticrs,%posscrstype);
13866:     my @courseroles = ('cc','in','ta','ep','st');
13867:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
13868:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
13869:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
13870:     my %coursetypetitles = &Apache::lonlocal::texthash (
13871:                                official   => 'Official',
13872:                                unofficial => 'Unofficial',
13873:                                community  => 'Community',
13874:                                textbook   => 'Textbook',
13875:                                placement  => 'Placement Test',
13876:                                lti        => 'LTI Provider',
13877:     );
13878:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13879:     my %lt = &lti_names();
13880:     map { $posslti{$_} = 1; } @ltiroles;
13881:     map { $posslticrs{$_} = 1; } @lticourseroles;
13882:     map { $posscrstype{$_} = 1; } @coursetypes;
13883: 
13884:     my %menutitles = &ltimenu_titles();
13885:     my (%currltisec,%secchanges,%newltisec,%newltienc,%newkeyset);
13886: 
13887:     &fetch_secrets($dom,'ltisec',\%domconfig,\%currltisec,\%secchanges,\%newltisec,\%newkeyset);
13888: 
13889:     my (%linkprotchg,$linkprotoutput,$is_home);
13890:     my $proterror = &Apache::courseprefs::process_linkprot($dom,'',$currltisec{'linkprot'},
13891:                                                            \%linkprotchg,'domain');
13892:     my $home = &Apache::lonnet::domain($dom,'primary');
13893:     unless (($home eq 'no_host') || ($home eq '')) {
13894:         my @ids=&Apache::lonnet::current_machine_ids();
13895:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; } }
13896:     }
13897: 
13898:     if (keys(%linkprotchg)) {
13899:         $secchanges{'linkprot'} = 1;
13900:         my %oldlinkprot;
13901:         if (ref($currltisec{'linkprot'}) eq 'HASH') {
13902:             %oldlinkprot = %{$currltisec{'linkprot'}};
13903:         }
13904:         foreach my $id (keys(%linkprotchg)) {
13905:             if (ref($linkprotchg{$id}) eq 'HASH') {
13906:                 foreach my $inner (keys(%{$linkprotchg{$id}})) {
13907:                     if (($inner eq 'secret') || ($inner eq 'key')) {
13908:                         if ($is_home) {
13909:                             $newltienc{$id}{$inner} = $linkprotchg{$id}{$inner};
13910:                         }
13911:                     }
13912:                 }
13913:             } else {
13914:                 $newltisec{'linkprot'}{$id} = $linkprotchg{$id};
13915:             }
13916:         }
13917:         $linkprotoutput = &Apache::courseprefs::store_linkprot($dom,'','domain',\%linkprotchg,\%oldlinkprot);
13918:         if (keys(%linkprotchg)) {
13919:             %{$newltisec{'linkprot'}} = %linkprotchg;
13920:         }
13921:     }
13922:     if (ref($currltisec{'linkprot'}) eq 'HASH') {
13923:         foreach my $id (keys(%{$currltisec{'linkprot'}})) {
13924:             next if ($id !~ /^\d+$/);
13925:             unless (exists($linkprotchg{$id})) {
13926:                 if (ref($currltisec{'linkprot'}{$id}) eq 'HASH') {
13927:                     foreach my $inner (keys(%{$currltisec{'linkprot'}{$id}})) {
13928:                         if (($inner eq 'secret') || ($inner eq 'key')) {
13929:                             if ($is_home) {
13930:                                 $newltienc{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
13931:                             }
13932:                         } else {
13933:                             $newltisec{'linkprot'}{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
13934:                         }
13935:                     }
13936:                 } else {
13937:                     $newltisec{'linkprot'}{$id} = $currltisec{'linkprot'}{$id};
13938:                 }
13939:             }
13940:         }
13941:     }
13942:     if ($proterror) {
13943:         $errors .= '<li>'.$proterror.'</li>';
13944:     }
13945: 
13946:     my (%delsuggested,%suggids,@suggested);;
13947:     if (ref($currltisec{'suggested'}) eq 'HASH') {
13948:         my $maxnum = $env{'form.linkprot_suggested_maxnum'};
13949:         my @todelete = &Apache::loncommon::get_env_multiple('form.linkprot_suggested_del');
13950:         for (my $i=0; $i<$maxnum; $i++) {
13951:             my $itemid = $env{'form.linkprot_suggested_id_'.$i};
13952:             $itemid =~ s/\D+//g;
13953:             if ($itemid) {
13954:                 if (ref($currltisec{'suggested'}->{$itemid}) eq 'HASH') {
13955:                     push(@suggested,$i);
13956:                     $suggids{$i} = $itemid;
13957:                     if ((@todelete > 0) && (grep(/^$i$/,@todelete))) {
13958:                         if (ref($currltisec{'suggested'}{$itemid}) eq 'HASH') {
13959:                             $delsuggested{$itemid} = $currltisec{'suggested'}{$itemid}{'name'};
13960:                         }
13961:                     } else {
13962:                         if ($env{'form.linkprot_suggested_name_'.$i} eq '') {
13963:                             $delsuggested{$itemid} = $currltisec{'suggested'}{$itemid}{'name'};
13964:                         } else {
13965:                             $env{'form.linkprot_suggested_name_'.$i} =~ s/(`)/'/g;
13966:                             $env{'form.linkprot_suggested_info_'.$i} =~ s/(`)/'/g;
13967:                             $newltisec{'suggested'}{$itemid}{'name'} = $env{'form.linkprot_suggested_name_'.$i};
13968:                             $newltisec{'suggested'}{$itemid}{'info'} = $env{'form.linkprot_suggested_info_'.$i};
13969:                             if (($currltisec{'suggested'}{$itemid}{'name'} ne $newltisec{'suggested'}{$itemid}{'name'}) ||
13970:                                 ($currltisec{'suggested'}{$itemid}{'info'} ne $newltisec{'suggested'}{$itemid}{'info'})) {
13971:                                 $secchanges{'suggested'}{$itemid} = 1;
13972:                             }
13973:                         }
13974:                     }
13975:                 }
13976:             }
13977:         }
13978:     }
13979:     foreach my $key (keys(%delsuggested)) {
13980:         $newltisec{'suggested'}{$key} = $delsuggested{$key};
13981:         $secchanges{'suggested'}{$key} = 1;
13982:     }
13983:     if (($env{'form.linkprot_suggested_add'}) &&
13984:         ($env{'form.linkprot_suggested_name_add'} ne '')) {
13985:         $env{'form.linkprot_suggested_name_add'} =~ s/(`)/'/g;
13986:         $env{'form.linkprot_suggested_info_add'} =~ s/(`)/'/g;
13987:         my ($newsuggid,$errormsg) = &get_lti_id($dom,$env{'form.linkprot_suggested_name_add'},'suggested');
13988:         if ($newsuggid) {
13989:             $newltisec{'suggested'}{$newsuggid}{'name'} = $env{'form.linkprot_suggested_name_add'};
13990:             $newltisec{'suggested'}{$newsuggid}{'info'} = $env{'form.linkprot_suggested_info_add'};
13991:             $secchanges{'suggested'}{$newsuggid} = 1;
13992:         } else {
13993:             my $error = &mt('Failed to acquire unique ID for new Link Protectors in Courses Suggestion');
13994:             if ($errormsg) {
13995:                 $error .= ' ('.$errormsg.')';
13996:             }
13997:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13998:         }
13999:     }
14000:     my (@items,%deletions,%itemids);
14001:     if ($env{'form.lti_add'}) {
14002:         my $consumer = $env{'form.lti_consumer_add'};
14003:         $consumer =~ s/(`)/'/g;
14004:         ($newid,my $errormsg) = &get_lti_id($dom,$consumer,'lti');
14005:         if ($newid) {
14006:             $itemids{'add'} = $newid;
14007:             push(@items,'add');
14008:             $changes{$newid} = 1;
14009:         } else {
14010:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
14011:             if ($errormsg) {
14012:                 $error .= ' ('.$errormsg.')';
14013:             }
14014:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14015:         }
14016:     }
14017:     if (ref($domconfig{$action}) eq 'HASH') {
14018:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
14019:         if (@todelete) {
14020:             map { $deletions{$_} = 1; } @todelete;
14021:         }
14022:         my $maxnum = $env{'form.lti_maxnum'};
14023:         for (my $i=0; $i<$maxnum; $i++) {
14024:             my $itemid = $env{'form.lti_id_'.$i};
14025:             $itemid =~ s/\D+//g;
14026:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
14027:                 if ($deletions{$itemid}) {
14028:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
14029:                 } else {
14030:                     push(@items,$i);
14031:                     $itemids{$i} = $itemid;
14032:                 }
14033:             }
14034:         }
14035:     }
14036:     my (%keystore,$secstored);
14037:     if ($is_home) {
14038:         &store_security($dom,'lti',\%secchanges,\%newkeyset,\%keystore);
14039:     }
14040: 
14041:     my ($cipher,$privnum);
14042:     if ((@items > 0) && ($is_home)) {
14043:         ($cipher,$privnum) = &get_priv_creds($dom,$home,$secchanges{'encrypt'},
14044:                                              $newltisec{'encrypt'},$keystore{$home});
14045:     }
14046:     foreach my $idx (@items) {
14047:         my $itemid = $itemids{$idx};
14048:         next unless ($itemid);
14049:         my %currlti;
14050:         unless ($idx eq 'add') {
14051:             if (ref($domconfig{$action}) eq 'HASH') {
14052:                 if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
14053:                     %currlti = %{$domconfig{$action}{$itemid}};
14054:                 }
14055:             }
14056:         }
14057:         my $position = $env{'form.lti_pos_'.$itemid};
14058:         $position =~ s/\D+//g;
14059:         if ($position ne '') {
14060:             $allpos[$position] = $itemid;
14061:         }
14062:         foreach my $item ('consumer','lifetime','requser','crsinc') {
14063:             my $formitem = 'form.lti_'.$item.'_'.$idx;
14064:             $env{$formitem} =~ s/(`)/'/g;
14065:             if ($item eq 'lifetime') {
14066:                 $env{$formitem} =~ s/[^\d.]//g;
14067:             }
14068:             if ($env{$formitem} ne '') {
14069:                 $confhash{$itemid}{$item} = $env{$formitem};
14070:                 unless (($idx eq 'add') || ($changes{$itemid})) {
14071:                     if ($currlti{$item} ne $confhash{$itemid}{$item}) {
14072:                         $changes{$itemid} = 1;
14073:                     }
14074:                 }
14075:             }
14076:         }
14077:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
14078:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
14079:         }
14080:         if ($confhash{$itemid}{'requser'}) {
14081:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
14082:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid';
14083:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
14084:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
14085:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
14086:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
14087:                 $mapuser =~ s/(`)/'/g;
14088:                 $mapuser =~ s/^\s+|\s+$//g;
14089:                 $confhash{$itemid}{'mapuser'} = $mapuser;
14090:             }
14091:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
14092:             my @makeuser;
14093:             foreach my $ltirole (sort(@possmakeuser)) {
14094:                 if ($posslti{$ltirole}) {
14095:                     push(@makeuser,$ltirole);
14096:                 }
14097:             }
14098:             $confhash{$itemid}{'makeuser'} = \@makeuser;
14099:             if (@makeuser) {
14100:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
14101:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
14102:                     $confhash{$itemid}{'lcauth'} = $lcauth;
14103:                     if ($lcauth ne 'internal') {
14104:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
14105:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
14106:                         $lcauthparm =~ s/`//g;
14107:                         if ($lcauthparm ne '') {
14108:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
14109:                         }
14110:                     }
14111:                 } else {
14112:                     $confhash{$itemid}{'lcauth'} = 'lti';
14113:                 }
14114:             }
14115:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
14116:             if (@possinstdata) {
14117:                 foreach my $field (@possinstdata) {
14118:                     if (exists($fieldtitles{$field})) {
14119:                         push(@{$confhash{$itemid}{'instdata'}});
14120:                     }
14121:                 }
14122:             }
14123:             if ($env{'form.lti_callback_'.$idx}) {
14124:                 if ($env{'form.lti_callbackparam_'.$idx}) {
14125:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
14126:                     $callback =~ s/^\s+|\s+$//g;
14127:                     $confhash{$itemid}{'callback'} = $callback;
14128:                 }
14129:             }
14130:             foreach my $field ('topmenu','inlinemenu') {
14131:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
14132:                     $confhash{$itemid}{$field} = 1;
14133:                 }
14134:             }
14135:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
14136:                 $confhash{$itemid}{lcmenu} = [];
14137:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
14138:                 foreach my $field (@possmenu) {
14139:                     if (exists($menutitles{$field})) {
14140:                         if ($field eq 'grades') {
14141:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
14142:                         }
14143:                         push(@{$confhash{$itemid}{lcmenu}},$field);
14144:                     }
14145:                 }
14146:             }
14147:             if ($confhash{$itemid}{'crsinc'}) {
14148:                 if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
14149:                     ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
14150:                     $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
14151:                 } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
14152:                     my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx};
14153:                     $mapcrs =~ s/(`)/'/g;
14154:                     $mapcrs =~ s/^\s+|\s+$//g;
14155:                     $confhash{$itemid}{'mapcrs'} = $mapcrs;
14156:                 }
14157:                 my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
14158:                 my @crstypes;
14159:                 foreach my $type (sort(@posstypes)) {
14160:                     if ($posscrstype{$type}) {
14161:                         push(@crstypes,$type);
14162:                     }
14163:                 }
14164:                 $confhash{$itemid}{'mapcrstype'} = \@crstypes;
14165:                 if ($env{'form.lti_storecrs_'.$idx}) {
14166:                     $confhash{$itemid}{'storecrs'} = 1;
14167:                 }
14168:                 if ($env{'form.lti_makecrs_'.$idx}) {
14169:                     $confhash{$itemid}{'makecrs'} = 1;
14170:                 }
14171:                 foreach my $ltirole (@lticourseroles) {
14172:                     my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
14173:                     if (grep(/^\Q$possrole\E$/,@courseroles)) {
14174:                         $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
14175:                     }
14176:                 }
14177:                 my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
14178:                 my @selfenroll;
14179:                 foreach my $type (sort(@possenroll)) {
14180:                     if ($posslticrs{$type}) {
14181:                         push(@selfenroll,$type);
14182:                     }
14183:                 }
14184:                 $confhash{$itemid}{'selfenroll'} = \@selfenroll;
14185:                 if ($env{'form.lti_crssec_'.$idx}) {
14186:                     if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
14187:                         $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
14188:                     } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
14189:                         my $section = $env{'form.lti_customsection_'.$idx};
14190:                         $section =~ s/(`)/'/g;
14191:                         $section =~ s/^\s+|\s+$//g;
14192:                         if ($section ne '') {
14193:                             $confhash{$itemid}{'section'} = $section;
14194:                         }
14195:                     }
14196:                 }
14197:                 foreach my $field ('passback','roster') {
14198:                     if ($env{'form.lti_'.$field.'_'.$idx}) {
14199:                         $confhash{$itemid}{$field} = 1;
14200:                     }
14201:                 }
14202:                 if ($env{'form.lti_passback_'.$idx}) {
14203:                     if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
14204:                         $confhash{$itemid}{'passbackformat'} = '1.0';
14205:                     } else {
14206:                         $confhash{$itemid}{'passbackformat'} = '1.1';
14207:                     }
14208:                 }
14209:             }
14210:             unless (($idx eq 'add') || ($changes{$itemid})) {
14211:                 if ($confhash{$itemid}{'crsinc'}) {
14212:                     foreach my $field ('mapcrs','storecrs','makecrs','section','passback','roster') {
14213:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
14214:                             $changes{$itemid} = 1;
14215:                         }
14216:                     }
14217:                     unless ($changes{$itemid}) {
14218:                         if ($currlti{'passback'} eq $confhash{$itemid}{'passback'}) {
14219:                             if ($currlti{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
14220:                                 $changes{$itemid} = 1;
14221:                             }
14222:                         }
14223:                     }
14224:                     foreach my $field ('mapcrstype','selfenroll') {
14225:                         unless ($changes{$itemid}) {
14226:                             if (ref($currlti{$field}) eq 'ARRAY') {
14227:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
14228:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
14229:                                                                                    $confhash{$itemid}{$field});
14230:                                     if (@diffs) {
14231:                                         $changes{$itemid} = 1;
14232:                                     }
14233:                                 } elsif (@{$currlti{$field}} > 0) {
14234:                                     $changes{$itemid} = 1;
14235:                                 }
14236:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
14237:                                 if (@{$confhash{$itemid}{$field}} > 0) {
14238:                                     $changes{$itemid} = 1;
14239:                                 }
14240:                             }
14241:                         }
14242:                     }
14243:                     unless ($changes{$itemid}) {
14244:                         if (ref($currlti{'maproles'}) eq 'HASH') {
14245:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
14246:                                 foreach my $ltirole (keys(%{$currlti{'maproles'}})) {
14247:                                     if ($currlti{'maproles'}{$ltirole} ne
14248:                                         $confhash{$itemid}{'maproles'}{$ltirole}) {
14249:                                         $changes{$itemid} = 1;
14250:                                         last;
14251:                                     }
14252:                                 }
14253:                                 unless ($changes{$itemid}) {
14254:                                     foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
14255:                                         if ($confhash{$itemid}{'maproles'}{$ltirole} ne
14256:                                             $currlti{'maproles'}{$ltirole}) {
14257:                                             $changes{$itemid} = 1;
14258:                                             last;
14259:                                         }
14260:                                     }
14261:                                 }
14262:                             } elsif (keys(%{$currlti{'maproles'}}) > 0) {
14263:                                 $changes{$itemid} = 1;
14264:                             }
14265:                         } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
14266:                             unless ($changes{$itemid}) {
14267:                                 if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
14268:                                     $changes{$itemid} = 1;
14269:                                 }
14270:                             }
14271:                         }
14272:                     }
14273:                 }
14274:                 unless ($changes{$itemid}) {
14275:                     foreach my $field ('mapuser','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
14276:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
14277:                             $changes{$itemid} = 1;
14278:                         }
14279:                     }
14280:                     unless ($changes{$itemid}) {
14281:                         foreach my $field ('makeuser','lcmenu') {
14282:                             if (ref($currlti{$field}) eq 'ARRAY') {
14283:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
14284:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
14285:                                                                                    $confhash{$itemid}{$field});
14286:                                     if (@diffs) {
14287:                                         $changes{$itemid} = 1;
14288:                                     }
14289:                                 } elsif (@{$currlti{$field}} > 0) {
14290:                                     $changes{$itemid} = 1;
14291:                                 }
14292:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
14293:                                 if (@{$confhash{$itemid}{$field}} > 0) {
14294:                                     $changes{$itemid} = 1;
14295:                                 }
14296:                             }
14297:                         }
14298:                     }
14299:                 }
14300:             }
14301:         }
14302:         if ($is_home) {
14303:             my $keyitem = 'form.lti_key_'.$idx;
14304:             $env{$keyitem} =~ s/(`)/'/g;
14305:             if ($env{$keyitem} ne '') {
14306:                 $ltienc{$itemid}{'key'} = $env{$keyitem};
14307:                 unless ($changes{$itemid}) {
14308:                     if ($currlti{'key'} ne $env{$keyitem}) {
14309:                         $changes{$itemid} = 1;
14310:                     }
14311:                 }
14312:             }
14313:             my $secretitem = 'form.lti_secret_'.$idx;
14314:             $env{$secretitem} =~ s/(`)/'/g;
14315:             if ($currlti{'usable'}) {
14316:                 if ($env{'form.lti_changesecret_'.$idx}) {
14317:                     if ($env{$secretitem} ne '') {
14318:                         if ($privnum && $cipher) {
14319:                             $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
14320:                             $confhash{$itemid}{'cipher'} = $privnum;
14321:                         } else {
14322:                             $ltienc{$itemid}{'secret'} = $env{$secretitem};
14323:                         }
14324:                         $changes{$itemid} = 1;
14325:                     }
14326:                 } else {
14327:                     $ltienc{$itemid}{'secret'} = $currlti{'secret'};
14328:                     $confhash{$itemid}{'cipher'} = $currlti{'cipher'};
14329:                 }
14330:                 if (ref($ltienc{$itemid}) eq 'HASH') {
14331:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'secret'} ne '')) {
14332:                         $confhash{$itemid}{'usable'} = 1;
14333:                     }
14334:                 }
14335:             } elsif ($env{$secretitem} ne '') {
14336:                 if ($privnum && $cipher) {
14337:                     $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
14338:                     $confhash{$itemid}{'cipher'} = $privnum;
14339:                 } else {
14340:                     $ltienc{$itemid}{'secret'} = $env{$secretitem};
14341:                 }
14342:                 if (ref($ltienc{$itemid}) eq 'HASH') {
14343:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'key'} ne '')) {
14344:                         $confhash{$itemid}{'usable'} = 1;
14345:                     }
14346:                 }
14347:                 $changes{$itemid} = 1;
14348:             }
14349:         }
14350:         unless ($changes{$itemid}) {
14351:             foreach my $key (keys(%currlti)) {
14352:                 if (ref($currlti{$key}) eq 'HASH') {
14353:                     if (ref($confhash{$itemid}{$key}) eq 'HASH') {
14354:                         foreach my $innerkey (keys(%{$currlti{$key}})) {
14355:                             unless (exists($confhash{$itemid}{$key}{$innerkey})) {
14356:                                 $changes{$itemid} = 1;
14357:                                 last;
14358:                             }
14359:                         }
14360:                     } elsif (keys(%{$currlti{$key}}) > 0) {
14361:                         $changes{$itemid} = 1;
14362:                     }
14363:                 }
14364:                 last if ($changes{$itemid});
14365:             }
14366:         }
14367:     }
14368:     if (@allpos > 0) {
14369:         my $idx = 0;
14370:         foreach my $itemid (@allpos) {
14371:             if ($itemid ne '') {
14372:                 $confhash{$itemid}{'order'} = $idx;
14373:                 if (ref($domconfig{$action}) eq 'HASH') {
14374:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
14375:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
14376:                             $changes{$itemid} = 1;
14377:                         }
14378:                     }
14379:                 }
14380:                 $idx ++;
14381:             }
14382:         }
14383:     }
14384: 
14385:     if ((keys(%changes) == 0) && (keys(%secchanges) == 0)) {
14386:         return &mt('No changes made.');
14387:     }
14388: 
14389:     my %ltihash = (
14390:                       $action => { %confhash }
14391:                   );
14392:     my %ltienchash;
14393: 
14394:     if ($is_home) {
14395:         %ltienchash = (
14396:                          $action => { %ltienc }
14397:                       );
14398:     }
14399:     if (keys(%secchanges)) {
14400:         $ltihash{'ltisec'} = \%newltisec;
14401:         if ($secchanges{'linkprot'}) {
14402:             if ($is_home) {
14403:                 $ltienchash{'linkprot'} = \%newltienc;
14404:             }
14405:         }
14406:     }
14407:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,$dom);
14408:     if ($putresult eq 'ok') {
14409:         if (keys(%ltienchash)) {
14410:             &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
14411:         }
14412:         $resulttext = &mt('Changes made:').'<ul>';
14413:         if (keys(%secchanges) > 0) {
14414:             $resulttext .= &lti_security_results($dom,'lti',\%secchanges,\%newltisec,\%newkeyset,\%keystore);
14415:             if (exists($secchanges{'linkprot'})) {
14416:                 $resulttext .= $linkprotoutput;
14417:             }
14418:         }
14419:         if (keys(%changes) > 0) {
14420:             my $cachetime = 24*60*60;
14421:             &Apache::lonnet::do_cache_new('lti',$dom,\%confhash,$cachetime);
14422:             if (ref($lastactref) eq 'HASH') {
14423:                 $lastactref->{'lti'} = 1;
14424:             }
14425:             my %bynum;
14426:             foreach my $itemid (sort(keys(%changes))) {
14427:                 if (ref($confhash{$itemid}) eq 'HASH') {
14428:                     my $position = $confhash{$itemid}{'order'};
14429:                     $bynum{$position} = $itemid;
14430:                 }
14431:             }
14432:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
14433:                 my $itemid = $bynum{$pos};
14434:                 if (ref($confhash{$itemid}) eq 'HASH') {
14435:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b><ul>';
14436:                     my $position = $pos + 1;
14437:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
14438:                     foreach my $item ('version','lifetime') {
14439:                         if ($confhash{$itemid}{$item} ne '') {
14440:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
14441:                         }
14442:                     }
14443:                     if ($ltienc{$itemid}{'key'} ne '') {
14444:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$ltienc{$itemid}{'key'}.'</li>';
14445:                     }
14446:                     if ($ltienc{$itemid}{'secret'} ne '') {
14447:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;['.&mt('not shown').']</li>';
14448:                     }
14449:                     if ($confhash{$itemid}{'requser'}) {
14450:                         if ($confhash{$itemid}{'callback'}) {
14451:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
14452:                         } else {
14453:                             $resulttext .= '<li>'.&mt('Callback to logout LON-CAPA on log out from Consumer').'</li>';
14454:                         }
14455:                         if ($confhash{$itemid}{'mapuser'}) {
14456:                             my $shownmapuser;
14457:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
14458:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
14459:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
14460:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
14461:                             } else {
14462:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
14463:                             }
14464:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
14465:                         }
14466:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
14467:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) {
14468:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
14469:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
14470:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
14471:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
14472:                                 } else {
14473:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
14474:                                                        $confhash{$itemid}{'lcauth'});
14475:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
14476:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
14477:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
14478:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
14479:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
14480:                                         }
14481:                                     } else {
14482:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
14483:                                     }
14484:                                 }
14485:                                 $resulttext .= '</li>';
14486:                             } else {
14487:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
14488:                             }
14489:                         }
14490:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
14491:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
14492:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
14493:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
14494:                             } else {
14495:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
14496:                             }
14497:                         }
14498:                         foreach my $item ('topmenu','inlinemenu') {
14499:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
14500:                             if ($confhash{$itemid}{$item}) {
14501:                                 $resulttext .= &mt('Yes');
14502:                             } else {
14503:                                 $resulttext .= &mt('No');
14504:                             }
14505:                             $resulttext .= '</li>';
14506:                         }
14507:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
14508:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
14509:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
14510:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
14511:                             } else {
14512:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
14513:                             }
14514:                         }
14515:                         if ($confhash{$itemid}{'crsinc'}) {
14516:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
14517:                                 my $rolemaps;
14518:                                 foreach my $role (@ltiroles) {
14519:                                     if ($confhash{$itemid}{'maproles'}{$role}) {
14520:                                         $rolemaps .= ('&nbsp;'x2).$role.'='.
14521:                                                      &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
14522:                                                                                 'Course').',';
14523:                                     }
14524:                                 }
14525:                                 if ($rolemaps) {
14526:                                     $rolemaps =~ s/,$//;
14527:                                     $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
14528:                                 }
14529:                             }
14530:                             if ($confhash{$itemid}{'mapcrs'}) {
14531:                                 $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
14532:                             }
14533:                             if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
14534:                                 if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
14535:                                     $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
14536:                                                    join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
14537:                                                    '</li>';
14538:                                 } else {
14539:                                     $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
14540:                                 }
14541:                             }
14542:                             if ($confhash{$itemid}{'storecrs'}) {
14543:                                 $resulttext .= '<li>'.&mt('Store mapping of course identifier to LON-CAPA CourseID').': '.$confhash{$itemid}{'storecrs'}.'</li>';
14544:                             }
14545:                             if ($confhash{$itemid}{'makecrs'}) {
14546:                                 $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
14547:                             } else {
14548:                                 $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
14549:                             }
14550:                             if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
14551:                                 if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
14552:                                     $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
14553:                                                               join(', ',@{$confhash{$itemid}{'selfenroll'}})).
14554:                                                    '</li>';
14555:                                 } else {
14556:                                     $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
14557:                                 }
14558:                             }
14559:                             if ($confhash{$itemid}{'section'}) {
14560:                                 if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
14561:                                     $resulttext .= '<li>'.&mt('User section from standard field:').
14562:                                                          ' (course_section_sourcedid)'.'</li>';
14563:                                 } else {
14564:                                     $resulttext .= '<li>'.&mt('User section from:').' '.
14565:                                                           $confhash{$itemid}{'section'}.'</li>';
14566:                                 }
14567:                             } else {
14568:                                 $resulttext .= '<li>'.&mt('No section assignment').'</li>';
14569:                             }
14570:                             foreach my $item ('passback','roster','topmenu','inlinemenu') {
14571:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
14572:                                 if ($confhash{$itemid}{$item}) {
14573:                                     $resulttext .= &mt('Yes');
14574:                                     if ($item eq 'passback') {
14575:                                         if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
14576:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
14577:                                         } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
14578:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
14579:                                         }
14580:                                     }
14581:                                 } else {
14582:                                     $resulttext .= &mt('No');
14583:                                 }
14584:                                 $resulttext .= '</li>';
14585:                             }
14586:                             if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
14587:                                 if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
14588:                                     $resulttext .= '<li>'.&mt('Menu items:').' '.
14589:                                                    join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
14590:                                 } else {
14591:                                     $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
14592:                                 }
14593:                             }
14594:                         }
14595:                     }
14596:                     $resulttext .= '</ul></li>';
14597:                 }
14598:             }
14599:             if (keys(%deletions)) {
14600:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
14601:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
14602:                 }
14603:             }
14604:         }
14605:         $resulttext .= '</ul>';
14606:         if (ref($lastactref) eq 'HASH') {
14607:             if (($secchanges{'encrypt'}) || ($secchanges{'private'}) || (exists($secchanges{'suggested'}))) {
14608:                 &Apache::lonnet::get_domain_defaults($dom,1);
14609:                 $lastactref->{'domdefaults'} = 1;
14610:             }
14611:         }
14612:     } else {
14613:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14614:     }
14615:     if ($errors) {
14616:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14617:                        $errors.'</ul>';
14618:     }
14619:     return $resulttext;
14620: }
14621: 
14622: sub get_priv_creds {
14623:     my ($dom,$home,$encchg,$encrypt,$storedsec) = @_;
14624:     my ($needenc,$cipher,$privnum);
14625:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
14626:     if (($encchg) && (ref($encrypt) eq 'HASH')) {
14627:         $needenc = $encrypt->{'consumers'}
14628:     } else {
14629:         $needenc = $domdefs{'ltienc_consumers'};
14630:     }
14631:     if ($needenc) {
14632:         if (($storedsec eq 'ok') || ((ref($domdefs{'ltiprivhosts'}) eq 'ARRAY') &&
14633:                                      (grep(/^\Q$home\E$/,@{$domdefs{'ltiprivhosts'}})))) {
14634:                         my %privhash  = &Apache::lonnet::restore_dom('lti','private',$dom,$home,1);
14635:             my $privkey = $privhash{'key'};
14636:             $privnum = $privhash{'version'};
14637:             if (($privnum) && ($privkey ne '')) {
14638:                 $cipher = Crypt::CBC->new({'key'     => $privkey,
14639:                                           'cipher'  => 'DES'});
14640:             }
14641:         }
14642:     }
14643:     return ($cipher,$privnum);
14644: }
14645: 
14646: sub get_lti_id {
14647:     my ($domain,$consumer,$dbname) = @_;
14648:     unless (($dbname eq 'lti') || ($dbname eq 'suggested')) {
14649:         return ('','invalid db');
14650:     }
14651:     # get lock on db
14652:     my $lockhash = {
14653:                       lock => $env{'user.name'}.
14654:                               ':'.$env{'user.domain'},
14655:                    };
14656:     my $tries = 0;
14657:     my $gotlock = &Apache::lonnet::newput_dom($dbname,$lockhash,$domain);
14658:     my ($id,$error);
14659: 
14660:     while (($gotlock ne 'ok') && ($tries<10)) {
14661:         $tries ++;
14662:         sleep (0.1);
14663:         $gotlock = &Apache::lonnet::newput_dom($dbname,$lockhash,$domain);
14664:     }
14665:     if ($gotlock eq 'ok') {
14666:         my %currids = &Apache::lonnet::dump_dom($dbname,$domain);
14667:         if ($currids{'lock'}) {
14668:             delete($currids{'lock'});
14669:             if (keys(%currids)) {
14670:                 my @curr = sort { $a <=> $b } keys(%currids);
14671:                 if ($curr[-1] =~ /^\d+$/) {
14672:                     $id = 1 + $curr[-1];
14673:                 }
14674:             } else {
14675:                 $id = 1;
14676:             }
14677:             if ($id) {
14678:                 unless (&Apache::lonnet::newput_dom($dbname,{ $id => $consumer },$domain) eq 'ok') {
14679:                     $error = 'nostore';
14680:                 }
14681:             } else {
14682:                 $error = 'nonumber';
14683:             }
14684:         }
14685:         my $dellockoutcome = &Apache::lonnet::del_dom($dbname,['lock'],$domain);
14686:     } else {
14687:         $error = 'nolock';
14688:     }
14689:     return ($id,$error);
14690: }
14691: 
14692: sub modify_autoenroll {
14693:     my ($dom,$lastactref,%domconfig) = @_;
14694:     my ($resulttext,%changes);
14695:     my %currautoenroll;
14696:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
14697:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
14698:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
14699:         }
14700:     }
14701:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
14702:     my %title = ( run => 'Auto-enrollment active',
14703:                   sender => 'Sender for notification messages',
14704:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
14705:                   autofailsafe => 'Failsafe for no drops if institutional data missing for a section');
14706:     my @offon = ('off','on');
14707:     my $sender_uname = $env{'form.sender_uname'};
14708:     my $sender_domain = $env{'form.sender_domain'};
14709:     if ($sender_domain eq '') {
14710:         $sender_uname = '';
14711:     } elsif ($sender_uname eq '') {
14712:         $sender_domain = '';
14713:     }
14714:     my $coowners = $env{'form.autoassign_coowners'};
14715:     my $autofailsafe = $env{'form.autoenroll_autofailsafe'};
14716:     $autofailsafe =~ s{^\s+|\s+$}{}g;
14717:     if ($autofailsafe =~ /\D/) {
14718:         undef($autofailsafe);
14719:     }
14720:     my $failsafe = $env{'form.autoenroll_failsafe'};
14721:     unless (($failsafe eq 'zero') || ($failsafe eq 'any')) {
14722:         $failsafe = 'off';
14723:         undef($autofailsafe);
14724:     }
14725:     my %autoenrollhash =  (
14726:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
14727:                                        'sender_uname' => $sender_uname,
14728:                                        'sender_domain' => $sender_domain,
14729:                                        'co-owners' => $coowners,
14730:                                        'autofailsafe' => $autofailsafe,
14731:                                        'failsafe' => $failsafe,
14732:                                 }
14733:                      );
14734:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
14735:                                              $dom);
14736:     if ($putresult eq 'ok') {
14737:         if (exists($currautoenroll{'run'})) {
14738:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
14739:                  $changes{'run'} = 1;
14740:              }
14741:         } elsif ($autorun) {
14742:             if ($env{'form.autoenroll_run'} ne '1') {
14743:                  $changes{'run'} = 1;
14744:             }
14745:         }
14746:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
14747:             $changes{'sender'} = 1;
14748:         }
14749:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
14750:             $changes{'sender'} = 1;
14751:         }
14752:         if ($currautoenroll{'co-owners'} ne '') {
14753:             if ($currautoenroll{'co-owners'} ne $coowners) {
14754:                 $changes{'coowners'} = 1;
14755:             }
14756:         } elsif ($coowners) {
14757:             $changes{'coowners'} = 1;
14758:         }
14759:         if ($currautoenroll{'autofailsafe'} ne $autofailsafe) {
14760:             $changes{'autofailsafe'} = 1;
14761:         }
14762:         if ($currautoenroll{'failsafe'} ne $failsafe) {
14763:             $changes{'failsafe'} = 1;
14764:         }
14765:         if (keys(%changes) > 0) {
14766:             $resulttext = &mt('Changes made:').'<ul>';
14767:             if ($changes{'run'}) {
14768:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
14769:             }
14770:             if ($changes{'sender'}) {
14771:                 if ($sender_uname eq '' || $sender_domain eq '') {
14772:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
14773:                 } else {
14774:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
14775:                 }
14776:             }
14777:             if ($changes{'coowners'}) {
14778:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
14779:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
14780:                 if (ref($lastactref) eq 'HASH') {
14781:                     $lastactref->{'domainconfig'} = 1;
14782:                 }
14783:             }
14784:             if ($changes{'autofailsafe'}) {
14785:                 if ($autofailsafe ne '') {
14786:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$autofailsafe).'</li>';
14787:                 } else {
14788:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
14789:                 }
14790:             }
14791:             if ($changes{'failsafe'}) {
14792:                 if ($failsafe eq 'off') {
14793:                     unless ($changes{'autofailsafe'}) {
14794:                         $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
14795:                     }
14796:                 } elsif ($failsafe eq 'zero') {
14797:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero').'</li>';
14798:                 } else {
14799:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero or greater').'</li>';
14800:                 }
14801:             }
14802:             if (($changes{'autofailsafe'}) || ($changes{'failsafe'})) {
14803:                 &Apache::lonnet::get_domain_defaults($dom,1);
14804:                 if (ref($lastactref) eq 'HASH') {
14805:                     $lastactref->{'domdefaults'} = 1;
14806:                 }
14807:             }
14808:             $resulttext .= '</ul>';
14809:         } else {
14810:             $resulttext = &mt('No changes made to auto-enrollment settings');
14811:         }
14812:     } else {
14813:         $resulttext = '<span class="LC_error">'.
14814: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14815:     }
14816:     return $resulttext;
14817: }
14818: 
14819: sub modify_autoupdate {
14820:     my ($dom,%domconfig) = @_;
14821:     my ($resulttext,%currautoupdate,%fields,%changes);
14822:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
14823:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
14824:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
14825:         }
14826:     }
14827:     my @offon = ('off','on');
14828:     my %title = &Apache::lonlocal::texthash (
14829:                     run        => 'Auto-update:',
14830:                     classlists => 'Updates to user information in classlists?',
14831:                     unexpired  => 'Skip updates for users without active or future roles?',
14832:                     lastactive => 'Skip updates for inactive users?',
14833:                 );
14834:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14835:     my %fieldtitles = &Apache::lonlocal::texthash (
14836:                         id => 'Student/Employee ID',
14837:                         permanentemail => 'E-mail address',
14838:                         lastname => 'Last Name',
14839:                         firstname => 'First Name',
14840:                         middlename => 'Middle Name',
14841:                         generation => 'Generation',
14842:                       );
14843:     $othertitle = &mt('All users');
14844:     if (keys(%{$usertypes}) >  0) {
14845:         $othertitle = &mt('Other users');
14846:     }
14847:     foreach my $key (keys(%env)) {
14848:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
14849:             my ($usertype,$item) = ($1,$2);
14850:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
14851:                 if ($usertype eq 'default') {   
14852:                     push(@{$fields{$1}},$2);
14853:                 } elsif (ref($types) eq 'ARRAY') {
14854:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
14855:                         push(@{$fields{$1}},$2);
14856:                     }
14857:                 }
14858:             }
14859:         }
14860:     }
14861:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
14862:     @lockablenames = sort(@lockablenames);
14863:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
14864:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
14865:         if (@changed) {
14866:             $changes{'lockablenames'} = 1;
14867:         }
14868:     } else {
14869:         if (@lockablenames) {
14870:             $changes{'lockablenames'} = 1;
14871:         }
14872:     }
14873:     my %updatehash = (
14874:                       autoupdate => { run => $env{'form.autoupdate_run'},
14875:                                       classlists => $env{'form.classlists'},
14876:                                       unexpired  => $env{'form.unexpired'},
14877:                                       fields => {%fields},
14878:                                       lockablenames => \@lockablenames,
14879:                                     }
14880:                      );
14881:     my $lastactivedays;
14882:     if ($env{'form.lastactive'}) {
14883:         $lastactivedays = $env{'form.lastactivedays'};
14884:         $lastactivedays =~ s/^\s+|\s+$//g;
14885:         unless ($lastactivedays =~ /^\d+$/) {
14886:             undef($lastactivedays);
14887:             $env{'form.lastactive'} = 0;
14888:         }
14889:     }
14890:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
14891:     foreach my $key (keys(%currautoupdate)) {
14892:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
14893:             if (exists($updatehash{autoupdate}{$key})) {
14894:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
14895:                     $changes{$key} = 1;
14896:                 }
14897:             }
14898:         } elsif ($key eq 'fields') {
14899:             if (ref($currautoupdate{$key}) eq 'HASH') {
14900:                 foreach my $item (@{$types},'default') {
14901:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
14902:                         my $change = 0;
14903:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
14904:                             if (!exists($fields{$item})) {
14905:                                 $change = 1;
14906:                                 last;
14907:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
14908:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
14909:                                     $change = 1;
14910:                                     last;
14911:                                 }
14912:                             }
14913:                         }
14914:                         if ($change) {
14915:                             push(@{$changes{$key}},$item);
14916:                         }
14917:                     } 
14918:                 }
14919:             }
14920:         } elsif ($key eq 'lockablenames') {
14921:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
14922:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
14923:                 if (@changed) {
14924:                     $changes{'lockablenames'} = 1;
14925:                 }
14926:             } else {
14927:                 if (@lockablenames) {
14928:                     $changes{'lockablenames'} = 1;
14929:                 }
14930:             }
14931:         }
14932:     }
14933:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
14934:         if (@lockablenames) {
14935:             $changes{'lockablenames'} = 1;
14936:         }
14937:     }
14938:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
14939:         if ($updatehash{'autoupdate'}{'unexpired'}) {
14940:             $changes{'unexpired'} = 1;
14941:         }
14942:     }
14943:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
14944:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
14945:             $changes{'lastactive'} = 1;
14946:         }
14947:     }
14948:     foreach my $item (@{$types},'default') {
14949:         if (defined($fields{$item})) {
14950:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
14951:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
14952:                     my $change = 0;
14953:                     if (ref($fields{$item}) eq 'ARRAY') {
14954:                         foreach my $type (@{$fields{$item}}) {
14955:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
14956:                                 $change = 1;
14957:                                 last;
14958:                             }
14959:                         }
14960:                     }
14961:                     if ($change) {
14962:                         push(@{$changes{'fields'}},$item);
14963:                     }
14964:                 } else {
14965:                     push(@{$changes{'fields'}},$item);
14966:                 }
14967:             } else {
14968:                 push(@{$changes{'fields'}},$item);
14969:             }
14970:         }
14971:     }
14972:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
14973:                                              $dom);
14974:     if ($putresult eq 'ok') {
14975:         if (keys(%changes) > 0) {
14976:             $resulttext = &mt('Changes made:').'<ul>';
14977:             foreach my $key (sort(keys(%changes))) {
14978:                 if ($key eq 'lockablenames') {
14979:                     $resulttext .= '<li>';
14980:                     if (@lockablenames) {
14981:                         $usertypes->{'default'} = $othertitle;
14982:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
14983:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
14984:                     } else {
14985:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
14986:                     }
14987:                     $resulttext .= '</li>';
14988:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
14989:                     foreach my $item (@{$changes{$key}}) {
14990:                         my @newvalues;
14991:                         foreach my $type (@{$fields{$item}}) {
14992:                             push(@newvalues,$fieldtitles{$type});
14993:                         }
14994:                         my $newvaluestr;
14995:                         if (@newvalues > 0) {
14996:                             $newvaluestr = join(', ',@newvalues);
14997:                         } else {
14998:                             $newvaluestr = &mt('none');
14999:                         }
15000:                         if ($item eq 'default') {
15001:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
15002:                         } else {
15003:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
15004:                         }
15005:                     }
15006:                 } else {
15007:                     my $newvalue;
15008:                     if ($key eq 'run') {
15009:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
15010:                     } elsif ($key eq 'lastactive') {
15011:                         $newvalue = $offon[$env{'form.lastactive'}];
15012:                         unless ($lastactivedays eq '') {
15013:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
15014:                         }
15015:                     } else {
15016:                         $newvalue = $offon[$env{'form.'.$key}];
15017:                     }
15018:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
15019:                 }
15020:             }
15021:             $resulttext .= '</ul>';
15022:         } else {
15023:             $resulttext = &mt('No changes made to autoupdates');
15024:         }
15025:     } else {
15026:         $resulttext = '<span class="LC_error">'.
15027: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
15028:     }
15029:     return $resulttext;
15030: }
15031: 
15032: sub modify_autocreate {
15033:     my ($dom,%domconfig) = @_;
15034:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
15035:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
15036:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
15037:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
15038:         }
15039:     }
15040:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
15041:                  req => 'Auto-creation of validated requests for official courses',
15042:                  xmldc => 'Identity of course creator of courses from XML files',
15043:                );
15044:     my @types = ('xml','req');
15045:     foreach my $item (@types) {
15046:         $newvals{$item} = $env{'form.autocreate_'.$item};
15047:         $newvals{$item} =~ s/\D//g;
15048:         $newvals{$item} = 0 if ($newvals{$item} eq '');
15049:     }
15050:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
15051:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
15052:     unless (exists($domcoords{$newvals{'xmldc'}})) {
15053:         $newvals{'xmldc'} = '';
15054:     } 
15055:     %autocreatehash =  (
15056:                         autocreate => { xml => $newvals{'xml'},
15057:                                         req => $newvals{'req'},
15058:                                       }
15059:                        );
15060:     if ($newvals{'xmldc'} ne '') {
15061:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
15062:     }
15063:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
15064:                                              $dom);
15065:     if ($putresult eq 'ok') {
15066:         my @items = @types;
15067:         if ($newvals{'xml'}) {
15068:             push(@items,'xmldc');
15069:         }
15070:         foreach my $item (@items) {
15071:             if (exists($currautocreate{$item})) {
15072:                 if ($currautocreate{$item} ne $newvals{$item}) {
15073:                     $changes{$item} = 1;
15074:                 }
15075:             } elsif ($newvals{$item}) {
15076:                 $changes{$item} = 1;
15077:             }
15078:         }
15079:         if (keys(%changes) > 0) {
15080:             my @offon = ('off','on'); 
15081:             $resulttext = &mt('Changes made:').'<ul>';
15082:             foreach my $item (@types) {
15083:                 if ($changes{$item}) {
15084:                     my $newtxt = $offon[$newvals{$item}];
15085:                     $resulttext .= '<li>'.
15086:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
15087:                                        '<b>','</b>').
15088:                                    '</li>';
15089:                 }
15090:             }
15091:             if ($changes{'xmldc'}) {
15092:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
15093:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
15094:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
15095:             }
15096:             $resulttext .= '</ul>';
15097:         } else {
15098:             $resulttext = &mt('No changes made to auto-creation settings');
15099:         }
15100:     } else {
15101:         $resulttext = '<span class="LC_error">'.
15102:             &mt('An error occurred: [_1]',$putresult).'</span>';
15103:     }
15104:     return $resulttext;
15105: }
15106: 
15107: sub modify_directorysrch {
15108:     my ($dom,$lastactref,%domconfig) = @_;
15109:     my ($resulttext,%changes);
15110:     my %currdirsrch;
15111:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
15112:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
15113:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
15114:         }
15115:     }
15116:     my %title = ( available => 'Institutional directory search available',
15117:                   localonly => 'Other domains can search institution',
15118:                   lcavailable => 'LON-CAPA directory search available',
15119:                   lclocalonly => 'Other domains can search LON-CAPA domain',
15120:                   searchby => 'Search types',
15121:                   searchtypes => 'Search latitude');
15122:     my @offon = ('off','on');
15123:     my @otherdoms = ('Yes','No');
15124: 
15125:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
15126:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
15127:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
15128: 
15129:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15130:     if (keys(%{$usertypes}) == 0) {
15131:         @cansearch = ('default');
15132:     } else {
15133:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
15134:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
15135:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
15136:                     push(@{$changes{'cansearch'}},$type);
15137:                 }
15138:             }
15139:             foreach my $type (@cansearch) {
15140:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
15141:                     push(@{$changes{'cansearch'}},$type);
15142:                 }
15143:             }
15144:         } else {
15145:             push(@{$changes{'cansearch'}},@cansearch);
15146:         }
15147:     }
15148: 
15149:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
15150:         foreach my $by (@{$currdirsrch{'searchby'}}) {
15151:             if (!grep(/^\Q$by\E$/,@searchby)) {
15152:                 push(@{$changes{'searchby'}},$by);
15153:             }
15154:         }
15155:         foreach my $by (@searchby) {
15156:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
15157:                 push(@{$changes{'searchby'}},$by);
15158:             }
15159:         }
15160:     } else {
15161:         push(@{$changes{'searchby'}},@searchby);
15162:     }
15163: 
15164:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
15165:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
15166:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
15167:                 push(@{$changes{'searchtypes'}},$type);
15168:             }
15169:         }
15170:         foreach my $type (@searchtypes) {
15171:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
15172:                 push(@{$changes{'searchtypes'}},$type);
15173:             }
15174:         }
15175:     } else {
15176:         if (exists($currdirsrch{'searchtypes'})) {
15177:             foreach my $type (@searchtypes) {  
15178:                 if ($type ne $currdirsrch{'searchtypes'}) { 
15179:                     push(@{$changes{'searchtypes'}},$type);
15180:                 }
15181:             }
15182:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
15183:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
15184:             }   
15185:         } else {
15186:             push(@{$changes{'searchtypes'}},@searchtypes); 
15187:         }
15188:     }
15189: 
15190:     my %dirsrch_hash =  (
15191:             directorysrch => { available => $env{'form.dirsrch_available'},
15192:                                cansearch => \@cansearch,
15193:                                localonly => $env{'form.dirsrch_instlocalonly'},
15194:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
15195:                                lcavailable => $env{'form.dirsrch_domavailable'},
15196:                                searchby => \@searchby,
15197:                                searchtypes => \@searchtypes,
15198:                              }
15199:             );
15200:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
15201:                                              $dom);
15202:     if ($putresult eq 'ok') {
15203:         if (exists($currdirsrch{'available'})) {
15204:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
15205:                  $changes{'available'} = 1;
15206:              }
15207:         } else {
15208:             if ($env{'form.dirsrch_available'} eq '1') {
15209:                 $changes{'available'} = 1;
15210:             }
15211:         }
15212:         if (exists($currdirsrch{'lcavailable'})) {
15213:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
15214:                 $changes{'lcavailable'} = 1;
15215:             }
15216:         } else {
15217:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
15218:                 $changes{'lcavailable'} = 1;
15219:             }
15220:         }
15221:         if (exists($currdirsrch{'localonly'})) {
15222:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
15223:                 $changes{'localonly'} = 1;
15224:             }
15225:         } else {
15226:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
15227:                 $changes{'localonly'} = 1;
15228:             }
15229:         }
15230:         if (exists($currdirsrch{'lclocalonly'})) {
15231:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
15232:                 $changes{'lclocalonly'} = 1;
15233:             }
15234:         } else {
15235:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
15236:                 $changes{'lclocalonly'} = 1;
15237:             }
15238:         }
15239:         if (keys(%changes) > 0) {
15240:             $resulttext = &mt('Changes made:').'<ul>';
15241:             if ($changes{'available'}) {
15242:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
15243:             }
15244:             if ($changes{'lcavailable'}) {
15245:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
15246:             }
15247:             if ($changes{'localonly'}) {
15248:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
15249:             }
15250:             if ($changes{'lclocalonly'}) {
15251:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
15252:             }
15253:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
15254:                 my $chgtext;
15255:                 if (ref($usertypes) eq 'HASH') {
15256:                     if (keys(%{$usertypes}) > 0) {
15257:                         foreach my $type (@{$types}) {
15258:                             if (grep(/^\Q$type\E$/,@cansearch)) {
15259:                                 $chgtext .= $usertypes->{$type}.'; ';
15260:                             }
15261:                         }
15262:                         if (grep(/^default$/,@cansearch)) {
15263:                             $chgtext .= $othertitle;
15264:                         } else {
15265:                             $chgtext =~ s/\; $//;
15266:                         }
15267:                         $resulttext .=
15268:                             '<li>'.
15269:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
15270:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
15271:                             '</li>';
15272:                     }
15273:                 }
15274:             }
15275:             if (ref($changes{'searchby'}) eq 'ARRAY') {
15276:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
15277:                 my $chgtext;
15278:                 foreach my $type (@{$titleorder}) {
15279:                     if (grep(/^\Q$type\E$/,@searchby)) {
15280:                         if (defined($searchtitles->{$type})) {
15281:                             $chgtext .= $searchtitles->{$type}.'; ';
15282:                         }
15283:                     }
15284:                 }
15285:                 $chgtext =~ s/\; $//;
15286:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
15287:             }
15288:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
15289:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
15290:                 my $chgtext;
15291:                 foreach my $type (@{$srchtypeorder}) {
15292:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
15293:                         if (defined($srchtypes_desc->{$type})) {
15294:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
15295:                         }
15296:                     }
15297:                 }
15298:                 $chgtext =~ s/\; $//;
15299:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
15300:             }
15301:             $resulttext .= '</ul>';
15302:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
15303:             if (ref($lastactref) eq 'HASH') {
15304:                 $lastactref->{'directorysrch'} = 1;
15305:             }
15306:         } else {
15307:             $resulttext = &mt('No changes made to directory search settings');
15308:         }
15309:     } else {
15310:         $resulttext = '<span class="LC_error">'.
15311:                       &mt('An error occurred: [_1]',$putresult).'</span>';
15312:     }
15313:     return $resulttext;
15314: }
15315: 
15316: sub modify_contacts {
15317:     my ($dom,$lastactref,%domconfig) = @_;
15318:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
15319:     if (ref($domconfig{'contacts'}) eq 'HASH') {
15320:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
15321:             $currsetting{$key} = $domconfig{'contacts'}{$key};
15322:         }
15323:     }
15324:     my (%others,%to,%bcc,%includestr,%includeloc);
15325:     my @contacts = ('supportemail','adminemail');
15326:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
15327:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
15328:     my @toggles = ('reporterrors','reportupdates','reportstatus');
15329:     my @lonstatus = ('threshold','sysmail','weights','excluded');
15330:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
15331:     foreach my $type (@mailings) {
15332:         @{$newsetting{$type}} = 
15333:             &Apache::loncommon::get_env_multiple('form.'.$type);
15334:         foreach my $item (@contacts) {
15335:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
15336:                 $contacts_hash{contacts}{$type}{$item} = 1;
15337:             } else {
15338:                 $contacts_hash{contacts}{$type}{$item} = 0;
15339:             }
15340:         }
15341:         $others{$type} = $env{'form.'.$type.'_others'};
15342:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
15343:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15344:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
15345:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
15346:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
15347:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
15348:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
15349:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
15350:             }
15351:         }
15352:     }
15353:     foreach my $item (@contacts) {
15354:         $to{$item} = $env{'form.'.$item};
15355:         $contacts_hash{'contacts'}{$item} = $to{$item};
15356:     }
15357:     foreach my $item (@toggles) {
15358:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
15359:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
15360:         }
15361:     }
15362:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
15363:     foreach my $item (@lonstatus) {
15364:         if ($item eq 'excluded') {
15365:             my (%serverhomes,@excluded);
15366:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
15367:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
15368:             if (@possexcluded) {
15369:                 foreach my $id (sort(@possexcluded)) {
15370:                     if ($serverhomes{$id}) {
15371:                         push(@excluded,$id);
15372:                     }
15373:                 }
15374:             }
15375:             if (@excluded) {
15376:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
15377:             }
15378:         } elsif ($item eq 'weights') {
15379:             foreach my $type ('E','W','N','U') {
15380:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
15381:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
15382:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
15383:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
15384:                             $env{'form.error'.$item.'_'.$type};
15385:                     }
15386:                 }
15387:             }
15388:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
15389:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
15390:             if ($env{'form.error'.$item} =~ /^\d+$/) {
15391:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
15392:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
15393:                 }
15394:             }
15395:         }
15396:     }
15397:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
15398:         foreach my $field (@{$fields}) {
15399:             if (ref($possoptions->{$field}) eq 'ARRAY') {
15400:                 my $value = $env{'form.helpform_'.$field};
15401:                 $value =~ s/^\s+|\s+$//g;
15402:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
15403:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
15404:                     if ($field eq 'screenshot') {
15405:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
15406:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
15407:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
15408:                         }
15409:                     }
15410:                 }
15411:             }
15412:         }
15413:     }
15414:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15415:     my (@statuses,%usertypeshash,@overrides);
15416:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
15417:         @statuses = @{$types};
15418:         if (ref($usertypes) eq 'HASH') {
15419:             %usertypeshash = %{$usertypes};
15420:         }
15421:     }
15422:     if (@statuses) {
15423:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
15424:         foreach my $type (@possoverrides) {
15425:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
15426:                 push(@overrides,$type);
15427:             }
15428:         }
15429:         if (@overrides) {
15430:             foreach my $type (@overrides) {
15431:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
15432:                 foreach my $item (@contacts) {
15433:                     if (grep(/^\Q$item\E$/,@standard)) {
15434:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
15435:                         $newsetting{'override_'.$type}{$item} = 1;
15436:                     } else {
15437:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
15438:                         $newsetting{'override_'.$type}{$item} = 0;
15439:                     }
15440:                 }
15441:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
15442:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
15443:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
15444:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
15445:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
15446:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
15447:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
15448:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
15449:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
15450:                 }
15451:             }
15452:         }
15453:     }
15454:     if (keys(%currsetting) > 0) {
15455:         foreach my $item (@contacts) {
15456:             if ($to{$item} ne $currsetting{$item}) {
15457:                 $changes{$item} = 1;
15458:             }
15459:         }
15460:         foreach my $type (@mailings) {
15461:             foreach my $item (@contacts) {
15462:                 if (ref($currsetting{$type}) eq 'HASH') {
15463:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
15464:                         push(@{$changes{$type}},$item);
15465:                     }
15466:                 } else {
15467:                     push(@{$changes{$type}},@{$newsetting{$type}});
15468:                 }
15469:             }
15470:             if ($others{$type} ne $currsetting{$type}{'others'}) {
15471:                 push(@{$changes{$type}},'others');
15472:             }
15473:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15474:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
15475:                     push(@{$changes{$type}},'bcc'); 
15476:                 }
15477:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
15478:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
15479:                     push(@{$changes{$type}},'include');
15480:                 }
15481:             }
15482:         }
15483:         if (ref($fields) eq 'ARRAY') {
15484:             if (ref($currsetting{'helpform'}) eq 'HASH') {
15485:                 foreach my $field (@{$fields}) {
15486:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
15487:                         push(@{$changes{'helpform'}},$field);
15488:                     }
15489:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15490:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
15491:                             push(@{$changes{'helpform'}},'maxsize');
15492:                         }
15493:                     }
15494:                 }
15495:             } else {
15496:                 foreach my $field (@{$fields}) {
15497:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
15498:                         push(@{$changes{'helpform'}},$field);
15499:                     }
15500:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15501:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
15502:                             push(@{$changes{'helpform'}},'maxsize');
15503:                         }
15504:                     }
15505:                 }
15506:             }
15507:         }
15508:         if (@statuses) {
15509:             if (ref($currsetting{'overrides'}) eq 'HASH') {
15510:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
15511:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
15512:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
15513:                             foreach my $item (@contacts,'bcc','others','include') {
15514:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
15515:                                     push(@{$changes{'overrides'}},$key);
15516:                                     last;
15517:                                 }
15518:                             }
15519:                         } else {
15520:                             push(@{$changes{'overrides'}},$key);
15521:                         }
15522:                     }
15523:                 }
15524:                 foreach my $key (@overrides) {
15525:                     unless (exists($currsetting{'overrides'}{$key})) {
15526:                         push(@{$changes{'overrides'}},$key);
15527:                     }
15528:                 }
15529:             } else {
15530:                 foreach my $key (@overrides) {
15531:                     push(@{$changes{'overrides'}},$key);
15532:                 }
15533:             }
15534:         }
15535:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
15536:             foreach my $key ('excluded','weights','threshold','sysmail') {
15537:                 if ($key eq 'excluded') {
15538:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
15539:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
15540:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
15541:                             (@{$currsetting{'lonstatus'}{$key}})) {
15542:                             my @diffs =
15543:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
15544:                                                                    $currsetting{'lonstatus'}{$key});
15545:                             if (@diffs) {
15546:                                 push(@{$changes{'lonstatus'}},$key);
15547:                             }
15548:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
15549:                             push(@{$changes{'lonstatus'}},$key);
15550:                         }
15551:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
15552:                              (@{$currsetting{'lonstatus'}{$key}})) {
15553:                         push(@{$changes{'lonstatus'}},$key);
15554:                     }
15555:                 } elsif ($key eq 'weights') {
15556:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
15557:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
15558:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
15559:                             foreach my $type ('E','W','N','U') {
15560:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
15561:                                         $currsetting{'lonstatus'}{$key}{$type}) {
15562:                                     push(@{$changes{'lonstatus'}},$key);
15563:                                     last;
15564:                                 }
15565:                             }
15566:                         } else {
15567:                             foreach my $type ('E','W','N','U') {
15568:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
15569:                                     push(@{$changes{'lonstatus'}},$key);
15570:                                     last;
15571:                                 }
15572:                             }
15573:                         }
15574:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
15575:                         foreach my $type ('E','W','N','U') {
15576:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
15577:                                 push(@{$changes{'lonstatus'}},$key);
15578:                                 last;
15579:                             }
15580:                         }
15581:                     }
15582:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
15583:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15584:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
15585:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
15586:                                 push(@{$changes{'lonstatus'}},$key);
15587:                             }
15588:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
15589:                             push(@{$changes{'lonstatus'}},$key);
15590:                         }
15591:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
15592:                         push(@{$changes{'lonstatus'}},$key);
15593:                     }
15594:                 }
15595:             }
15596:         } else {
15597:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15598:                 foreach my $key ('excluded','weights','threshold','sysmail') {
15599:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
15600:                         push(@{$changes{'lonstatus'}},$key);
15601:                     }
15602:                 }
15603:             }
15604:         }
15605:     } else {
15606:         my %default;
15607:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
15608:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
15609:         $default{'errormail'} = 'adminemail';
15610:         $default{'packagesmail'} = 'adminemail';
15611:         $default{'helpdeskmail'} = 'supportemail';
15612:         $default{'otherdomsmail'} = 'supportemail';
15613:         $default{'lonstatusmail'} = 'adminemail';
15614:         $default{'requestsmail'} = 'adminemail';
15615:         $default{'updatesmail'} = 'adminemail';
15616:         $default{'hostipmail'} = 'adminemail';
15617:         foreach my $item (@contacts) {
15618:            if ($to{$item} ne $default{$item}) {
15619:                $changes{$item} = 1;
15620:            }
15621:         }
15622:         foreach my $type (@mailings) {
15623:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
15624:                 push(@{$changes{$type}},@{$newsetting{$type}});
15625:             }
15626:             if ($others{$type} ne '') {
15627:                 push(@{$changes{$type}},'others');
15628:             }
15629:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15630:                 if ($bcc{$type} ne '') {
15631:                     push(@{$changes{$type}},'bcc');
15632:                 }
15633:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
15634:                     push(@{$changes{$type}},'include');
15635:                 }
15636:             }
15637:         }
15638:         if (ref($fields) eq 'ARRAY') {
15639:             foreach my $field (@{$fields}) {
15640:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
15641:                     push(@{$changes{'helpform'}},$field);
15642:                 }
15643:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15644:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
15645:                         push(@{$changes{'helpform'}},'maxsize');
15646:                     }
15647:                 }
15648:             }
15649:         }
15650:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15651:             foreach my $key ('excluded','weights','threshold','sysmail') {
15652:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
15653:                     push(@{$changes{'lonstatus'}},$key);
15654:                 }
15655:             }
15656:         }
15657:     }
15658:     foreach my $item (@toggles) {
15659:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
15660:             $changes{$item} = 1;
15661:         } elsif ((!$env{'form.'.$item}) &&
15662:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
15663:             $changes{$item} = 1;
15664:         }
15665:     }
15666:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
15667:                                              $dom);
15668:     if ($putresult eq 'ok') {
15669:         if (keys(%changes) > 0) {
15670:             &Apache::loncommon::devalidate_domconfig_cache($dom);
15671:             if (ref($lastactref) eq 'HASH') {
15672:                 $lastactref->{'domainconfig'} = 1;
15673:             }
15674:             my ($titles,$short_titles)  = &contact_titles();
15675:             $resulttext = &mt('Changes made:').'<ul>';
15676:             foreach my $item (@contacts) {
15677:                 if ($changes{$item}) {
15678:                     $resulttext .= '<li>'.$titles->{$item}.
15679:                                     &mt(' set to: ').
15680:                                     '<span class="LC_cusr_emph">'.
15681:                                     $to{$item}.'</span></li>';
15682:                 }
15683:             }
15684:             foreach my $type (@mailings) {
15685:                 if (ref($changes{$type}) eq 'ARRAY') {
15686:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15687:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
15688:                     } else {
15689:                         $resulttext .= '<li>'.$titles->{$type}.': ';
15690:                     }
15691:                     my @text;
15692:                     foreach my $item (@{$newsetting{$type}}) {
15693:                         push(@text,$short_titles->{$item});
15694:                     }
15695:                     if ($others{$type} ne '') {
15696:                         push(@text,$others{$type});
15697:                     }
15698:                     if (@text) {
15699:                         $resulttext .= '<span class="LC_cusr_emph">'.
15700:                                        join(', ',@text).'</span>';
15701:                     }
15702:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15703:                         if ($bcc{$type} ne '') {
15704:                             my $bcctext;
15705:                             if (@text) {
15706:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
15707:                             } else {
15708:                                 $bcctext = '(Bcc)';
15709:                             }
15710:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
15711:                         } elsif (!@text) {
15712:                             $resulttext .= &mt('No one');
15713:                         }
15714:                         if ($includestr{$type} ne '') {
15715:                             if ($includeloc{$type} eq 'b') {
15716:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
15717:                             } elsif ($includeloc{$type} eq 's') {
15718:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
15719:                             }
15720:                         }
15721:                     } elsif (!@text) {
15722:                         $resulttext .= &mt('No recipients');
15723:                     }
15724:                     $resulttext .= '</li>';
15725:                 }
15726:             }
15727:             if (ref($changes{'overrides'}) eq 'ARRAY') {
15728:                 my @deletions;
15729:                 foreach my $type (@{$changes{'overrides'}}) {
15730:                     if ($usertypeshash{$type}) {
15731:                         if (grep(/^\Q$type\E/,@overrides)) {
15732:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
15733:                                                       $usertypeshash{$type}).'<ul><li>';
15734:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
15735:                                 my @text;
15736:                                 foreach my $item (@contacts) {
15737:                                     if ($newsetting{'override_'.$type}{$item}) {
15738:                                         push(@text,$short_titles->{$item});
15739:                                     }
15740:                                 }
15741:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
15742:                                     push(@text,$newsetting{'override_'.$type}{'others'});
15743:                                 }
15744: 
15745:                                 if (@text) {
15746:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
15747:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
15748:                                 }
15749:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
15750:                                     my $bcctext;
15751:                                     if (@text) {
15752:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
15753:                                     } else {
15754:                                         $bcctext = '(Bcc)';
15755:                                     }
15756:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
15757:                                 } elsif (!@text) {
15758:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
15759:                                 }
15760:                                 $resulttext .= '</li>';
15761:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
15762:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
15763:                                     if ($loc eq 'b') {
15764:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
15765:                                     } elsif ($loc eq 's') {
15766:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
15767:                                     }
15768:                                 }
15769:                             }
15770:                             $resulttext .= '</li></ul></li>';
15771:                         } else {
15772:                             push(@deletions,$usertypeshash{$type});
15773:                         }
15774:                     }
15775:                 }
15776:                 if (@deletions) {
15777:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
15778:                                               join(', ',@deletions)).'</li>';
15779:                 }
15780:             }
15781:             my @offon = ('off','on');
15782:             my $corelink = &core_link_msu();
15783:             if ($changes{'reporterrors'}) {
15784:                 $resulttext .= '<li>'.
15785:                                &mt('E-mail error reports to [_1] set to "'.
15786:                                    $offon[$env{'form.reporterrors'}].'".',
15787:                                    $corelink).
15788:                                '</li>';
15789:             }
15790:             if ($changes{'reportupdates'}) {
15791:                 $resulttext .= '<li>'.
15792:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
15793:                                     $offon[$env{'form.reportupdates'}].'".',
15794:                                     $corelink).
15795:                                 '</li>';
15796:             }
15797:             if ($changes{'reportstatus'}) {
15798:                 $resulttext .= '<li>'.
15799:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
15800:                                     $offon[$env{'form.reportstatus'}].'".',
15801:                                     $corelink).
15802:                                 '</li>';
15803:             }
15804:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
15805:                 $resulttext .= '<li>'.
15806:                                &mt('Nightly status check e-mail settings').':<ul>';
15807:                 my (%defval,%use_def,%shown);
15808:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
15809:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
15810:                 $defval{'weights'} =
15811:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
15812:                 $defval{'excluded'} = &mt('None');
15813:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
15814:                     foreach my $item ('threshold','sysmail','weights','excluded') {
15815:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
15816:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
15817:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
15818:                             } elsif ($item eq 'weights') {
15819:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
15820:                                     foreach my $type ('E','W','N','U') {
15821:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
15822:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
15823:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
15824:                                         } else {
15825:                                             $shown{$item} .= $lonstatus_defs->{$type};
15826:                                         }
15827:                                         $shown{$item} .= ', ';
15828:                                     }
15829:                                     $shown{$item} =~ s/, $//;
15830:                                 } else {
15831:                                     $shown{$item} = $defval{$item};
15832:                                 }
15833:                             } elsif ($item eq 'excluded') {
15834:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
15835:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
15836:                                 } else {
15837:                                     $shown{$item} = $defval{$item};
15838:                                 }
15839:                             }
15840:                         } else {
15841:                             $shown{$item} = $defval{$item};
15842:                         }
15843:                     }
15844:                 } else {
15845:                     foreach my $item ('threshold','weights','excluded','sysmail') {
15846:                         $shown{$item} = $defval{$item};
15847:                     }
15848:                 }
15849:                 foreach my $item ('threshold','weights','excluded','sysmail') {
15850:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
15851:                                           $shown{$item}).'</li>';
15852:                 }
15853:                 $resulttext .= '</ul></li>';
15854:             }
15855:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
15856:                 my (@optional,@required,@unused,$maxsizechg);
15857:                 foreach my $field (@{$changes{'helpform'}}) {
15858:                     if ($field eq 'maxsize') {
15859:                         $maxsizechg = 1;
15860:                         next;
15861:                     }
15862:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
15863:                         push(@optional,$field);
15864:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
15865:                         push(@unused,$field);
15866:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
15867:                         push(@required,$field);
15868:                     }
15869:                 }
15870:                 if (@optional) {
15871:                     $resulttext .= '<li>'.
15872:                                    &mt('Help form fields changed to "Optional": [_1].',
15873:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
15874:                                    '</li>';
15875:                 }
15876:                 if (@required) {
15877:                     $resulttext .= '<li>'.
15878:                                    &mt('Help form fields changed to "Required": [_1].',
15879:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
15880:                                    '</li>';
15881:                 }
15882:                 if (@unused) {
15883:                     $resulttext .= '<li>'.
15884:                                    &mt('Help form fields changed to "Not shown": [_1].',
15885:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
15886:                                    '</li>';
15887:                 }
15888:                 if ($maxsizechg) {
15889:                     $resulttext .= '<li>'.
15890:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
15891:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
15892:                                    '</li>';
15893:                 }
15894:             }
15895:             $resulttext .= '</ul>';
15896:         } else {
15897:             $resulttext = &mt('No changes made to contacts and form settings');
15898:         }
15899:     } else {
15900:         $resulttext = '<span class="LC_error">'.
15901:             &mt('An error occurred: [_1]',$putresult).'</span>';
15902:     }
15903:     return $resulttext;
15904: }
15905: 
15906: sub modify_passwords {
15907:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
15908:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
15909:         $updatedefaults,$updateconf);
15910:     my $customfn = 'resetpw.html';
15911:     if (ref($domconfig{'passwords'}) eq 'HASH') {
15912:         %current = %{$domconfig{'passwords'}};
15913:     }
15914:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15915:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15916:     if (ref($types) eq 'ARRAY') {
15917:         @oktypes = @{$types};
15918:     }
15919:     push(@oktypes,'default');
15920: 
15921:     my %titles = &Apache::lonlocal::texthash (
15922:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
15923:         intauth_check  => 'Check bcrypt cost if authenticated',
15924:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
15925:         permanent      => 'Permanent e-mail address',
15926:         critical       => 'Critical notification address',
15927:         notify         => 'Notification address',
15928:         min            => 'Minimum password length',
15929:         max            => 'Maximum password length',
15930:         chars          => 'Required characters',
15931:         numsaved       => 'Number of previous passwords to save',
15932:         reset          => 'Resetting Forgotten Password',
15933:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
15934:         rules          => 'Rules for LON-CAPA Passwords',
15935:         crsownerchg    => 'Course Owner Changing Student Passwords',
15936:         username       => 'Username',
15937:         email          => 'E-mail address',
15938:     );
15939: 
15940: #
15941: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
15942: #
15943:     my (%curr_defaults,%save_defaults);
15944:     if (ref($domconfig{'defaults'}) eq 'HASH') {
15945:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
15946:             if ($key =~ /^intauth_(cost|check|switch)$/) {
15947:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
15948:             } else {
15949:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
15950:             }
15951:         }
15952:     }
15953:     my %staticdefaults = (
15954:         'resetlink'      => 2,
15955:         'resetcase'      => \@oktypes,
15956:         'resetprelink'   => 'both',
15957:         'resetemail'     => ['critical','notify','permanent'],
15958:         'intauth_cost'   => 10,
15959:         'intauth_check'  => 0,
15960:         'intauth_switch' => 0,
15961:     );
15962:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
15963:     foreach my $type (@oktypes) {
15964:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
15965:     }
15966:     my $linklife = $env{'form.passwords_link'};
15967:     $linklife =~ s/^\s+|\s+$//g;
15968:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
15969:         $newvalues{'resetlink'} = $linklife;
15970:         if ($current{'resetlink'}) {
15971:             if ($current{'resetlink'} ne $linklife) {
15972:                 $changes{'reset'} = 1;
15973:             }
15974:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
15975:             if ($staticdefaults{'resetlink'} ne $linklife) {
15976:                 $changes{'reset'} = 1;
15977:             }
15978:         }
15979:     } elsif ($current{'resetlink'}) {
15980:         $changes{'reset'} = 1;
15981:     }
15982:     my @casesens;
15983:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
15984:     foreach my $case (sort(@posscase)) {
15985:         if (grep(/^\Q$case\E$/,@oktypes)) {
15986:             push(@casesens,$case);
15987:         }
15988:     }
15989:     $newvalues{'resetcase'} = \@casesens;
15990:     if (ref($current{'resetcase'}) eq 'ARRAY') {
15991:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
15992:         if (@diffs > 0) {
15993:             $changes{'reset'} = 1;
15994:         }
15995:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
15996:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
15997:         if (@diffs > 0) {
15998:             $changes{'reset'} = 1;
15999:         }
16000:     }
16001:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
16002:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
16003:         if (exists($current{'resetprelink'})) {
16004:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
16005:                 $changes{'reset'} = 1;
16006:             }
16007:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16008:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
16009:                 $changes{'reset'} = 1;
16010:             }
16011:         }
16012:     } elsif ($current{'resetprelink'}) {
16013:         $changes{'reset'} = 1;
16014:     }
16015:     foreach my $type (@oktypes) {
16016:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
16017:         my @postlink;
16018:         foreach my $item (sort(@possplink)) {
16019:             if ($item =~ /^(email|username)$/) {
16020:                 push(@postlink,$item);
16021:             }
16022:         }
16023:         $newvalues{'resetpostlink'}{$type} = \@postlink;
16024:         unless ($changes{'reset'}) {
16025:             if (ref($current{'resetpostlink'}) eq 'HASH') {
16026:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
16027:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
16028:                     if (@diffs > 0) {
16029:                         $changes{'reset'} = 1;
16030:                     }
16031:                 } else {
16032:                     $changes{'reset'} = 1;
16033:                 }
16034:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16035:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
16036:                 if (@diffs > 0) {
16037:                     $changes{'reset'} = 1;
16038:                 }
16039:             }
16040:         }
16041:     }
16042:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
16043:     my @resetemail;
16044:     foreach my $item (sort(@possemailsrc)) {
16045:         if ($item =~ /^(permanent|critical|notify)$/) {
16046:             push(@resetemail,$item);
16047:         }
16048:     }
16049:     $newvalues{'resetemail'} = \@resetemail;
16050:     unless ($changes{'reset'}) {
16051:         if (ref($current{'resetemail'}) eq 'ARRAY') {
16052:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
16053:             if (@diffs > 0) {
16054:                 $changes{'reset'} = 1;
16055:             }
16056:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16057:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
16058:             if (@diffs > 0) {
16059:                 $changes{'reset'} = 1;
16060:             }
16061:         }
16062:     }
16063:     if ($env{'form.passwords_stdtext'} == 0) {
16064:         $newvalues{'resetremove'} = 1;
16065:         unless ($current{'resetremove'}) {
16066:             $changes{'reset'} = 1;
16067:         }
16068:     } elsif ($current{'resetremove'}) {
16069:         $changes{'reset'} = 1;
16070:     }
16071:     if ($env{'form.passwords_customfile.filename'} ne '') {
16072:         my $servadm = $r->dir_config('lonAdmEMail');
16073:         my ($configuserok,$author_ok,$switchserver) =
16074:             &config_check($dom,$confname,$servadm);
16075:         my $error;
16076:         if ($configuserok eq 'ok') {
16077:             if ($switchserver) {
16078:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
16079:             } else {
16080:                 if ($author_ok eq 'ok') {
16081:                     my $modified = [];
16082:                     my ($result,$customurl) =
16083:                         &Apache::lonconfigsettings::publishlogo($r,'upload','passwords_customfile',$dom,
16084:                                                                 $confname,'customtext/resetpw','','',$customfn,
16085:                                                                 $modified);
16086:                     if ($result eq 'ok') {
16087:                         $newvalues{'resetcustom'} = $customurl;
16088:                         $changes{'reset'} = 1;
16089:                         &update_modify_urls($r,$modified);
16090:                     } else {
16091:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
16092:                     }
16093:                 } else {
16094:                     $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);
16095:                 }
16096:             }
16097:         } else {
16098:             $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);
16099:         }
16100:         if ($error) {
16101:             &Apache::lonnet::logthis($error);
16102:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
16103:         }
16104:     } elsif ($current{'resetcustom'}) {
16105:         if ($env{'form.passwords_custom_del'}) {
16106:             $changes{'reset'} = 1;
16107:         } else {
16108:             $newvalues{'resetcustom'} = $current{'resetcustom'};
16109:         }
16110:     }
16111:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
16112:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
16113:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
16114:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
16115:             $changes{'intauth'} = 1;
16116:         }
16117:     } else {
16118:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
16119:     }
16120:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
16121:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
16122:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
16123:             $changes{'intauth'} = 1;
16124:         }
16125:     } else {
16126:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
16127:     }
16128:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
16129:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
16130:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
16131:             $changes{'intauth'} = 1;
16132:         }
16133:     } else {
16134:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
16135:     }
16136:     foreach my $item ('cost','check','switch') {
16137:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
16138:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
16139:             $updatedefaults = 1;
16140:         }
16141:     }
16142:     &password_rule_changes('passwords',\%newvalues,\%current,\%changes);
16143:     my %crsownerchg = (
16144:                         by => [],
16145:                         for => [],
16146:                       );
16147:     foreach my $item ('by','for') {
16148:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
16149:         foreach my $type (sort(@posstypes)) {
16150:             if (grep(/^\Q$type\E$/,@oktypes)) {
16151:                 push(@{$crsownerchg{$item}},$type);
16152:             }
16153:         }
16154:     }
16155:     $newvalues{'crsownerchg'} = \%crsownerchg;
16156:     if (ref($current{'crsownerchg'}) eq 'HASH') {
16157:         foreach my $item ('by','for') {
16158:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
16159:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
16160:                 if (@diffs > 0) {
16161:                     $changes{'crsownerchg'} = 1;
16162:                     last;
16163:                 }
16164:             }
16165:         }
16166:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
16167:         foreach my $item ('by','for') {
16168:             if (@{$crsownerchg{$item}} > 0) {
16169:                 $changes{'crsownerchg'} = 1;
16170:                 last;
16171:             }
16172:         }
16173:     }
16174: 
16175:     my %confighash = (
16176:                         defaults  => \%save_defaults,
16177:                         passwords => \%newvalues,
16178:                      );
16179:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
16180: 
16181:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
16182:     if ($putresult eq 'ok') {
16183:         if (keys(%changes) > 0) {
16184:             $resulttext = &mt('Changes made: ').'<ul>';
16185:             foreach my $key ('reset','intauth','rules','crsownerchg') {
16186:                 if ($changes{$key}) {
16187:                     unless ($key eq 'intauth') {
16188:                         $updateconf = 1;
16189:                     }
16190:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
16191:                     if ($key eq 'reset') {
16192:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
16193:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
16194:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
16195:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
16196:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
16197:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
16198:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
16199:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
16200:                             }
16201:                         } else {
16202:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
16203:                         }
16204:                         if ($confighash{'passwords'}{'resetlink'}) {
16205:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
16206:                         } else {
16207:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
16208:                                                   &mt('Will default to 2 hours').'</li>';
16209:                         }
16210:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
16211:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
16212:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
16213:                             } else {
16214:                                 my $casesens;
16215:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
16216:                                     if ($type eq 'default') {
16217:                                         $casesens .= $othertitle.', ';
16218:                                     } elsif ($usertypes->{$type} ne '') {
16219:                                         $casesens .= $usertypes->{$type}.', ';
16220:                                     }
16221:                                 }
16222:                                 $casesens =~ s/\Q, \E$//;
16223:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
16224:                             }
16225:                         } else {
16226:                             $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>';
16227:                         }
16228:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
16229:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
16230:                         } else {
16231:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
16232:                         }
16233:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
16234:                             my $output;
16235:                             if (ref($types) eq 'ARRAY') {
16236:                                 foreach my $type (@{$types}) {
16237:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
16238:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
16239:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
16240:                                         } else {
16241:                                             $output .= $usertypes->{$type}.' -- '.
16242:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
16243:                                         }
16244:                                     }
16245:                                 }
16246:                             }
16247:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
16248:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
16249:                                     $output .= $othertitle.' -- '.&mt('none');
16250:                                 } else {
16251:                                     $output .= $othertitle.' -- '.
16252:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
16253:                                 }
16254:                             }
16255:                             if ($output) {
16256:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
16257:                             } else {
16258:                                 $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>';
16259:                             }
16260:                         } else {
16261:                             $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>';
16262:                         }
16263:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
16264:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
16265:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
16266:                             } else {
16267:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
16268:                             }
16269:                         } else {
16270:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
16271:                         }
16272:                         if ($confighash{'passwords'}{'resetremove'}) {
16273:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
16274:                         } else {
16275:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
16276:                         }
16277:                         if ($confighash{'passwords'}{'resetcustom'}) {
16278:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
16279:                                                                             &mt('custom text'),600,500,undef,undef,
16280:                                                                             undef,undef,'background-color:#ffffff');
16281:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
16282:                         } else {
16283:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
16284:                         }
16285:                     } elsif ($key eq 'intauth') {
16286:                         foreach my $item ('cost','switch','check') {
16287:                             my $value = $save_defaults{$key.'_'.$item};
16288:                             if ($item eq 'switch') {
16289:                                 my %optiondesc = &Apache::lonlocal::texthash (
16290:                                                      0 => 'No',
16291:                                                      1 => 'Yes',
16292:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
16293:                                                  );
16294:                                 if ($value =~ /^(0|1|2)$/) {
16295:                                     $value = $optiondesc{$value};
16296:                                 } else {
16297:                                     $value = &mt('none -- defaults to No');
16298:                                 }
16299:                             } elsif ($item eq 'check') {
16300:                                 my %optiondesc = &Apache::lonlocal::texthash (
16301:                                                      0 => 'No',
16302:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
16303:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
16304:                                                  );
16305:                                 if ($value =~ /^(0|1|2)$/) {
16306:                                     $value = $optiondesc{$value};
16307:                                 } else {
16308:                                     $value = &mt('none -- defaults to No');
16309:                                 }
16310:                             }
16311:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
16312:                         }
16313:                     } elsif ($key eq 'rules') {
16314:                         foreach my $rule ('min','max','numsaved') {
16315:                             if ($confighash{'passwords'}{$rule} eq '') {
16316:                                 if ($rule eq 'min') {
16317:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
16318:                                                    ' '.&mt('Default of [_1] will be used',
16319:                                                            $Apache::lonnet::passwdmin).'</li>';
16320:                                 } else {
16321:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
16322:                                 }
16323:                             } else {
16324:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
16325:                             }
16326:                         }
16327:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
16328:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
16329:                                 my %rulenames = &Apache::lonlocal::texthash(
16330:                                                      uc => 'At least one upper case letter',
16331:                                                      lc => 'At least one lower case letter',
16332:                                                      num => 'At least one number',
16333:                                                      spec => 'At least one non-alphanumeric',
16334:                                                    );
16335:                                 my $needed = '<ul><li>'.
16336:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
16337:                                              '</li></ul>';
16338:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
16339:                             } else {
16340:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
16341:                             }
16342:                         } else {
16343:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
16344:                         }
16345:                     } elsif ($key eq 'crsownerchg') {
16346:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
16347:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
16348:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
16349:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
16350:                             } else {
16351:                                 my %crsownerstr;
16352:                                 foreach my $item ('by','for') {
16353:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
16354:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
16355:                                             if ($type eq 'default') {
16356:                                                 $crsownerstr{$item} .= $othertitle.', ';
16357:                                             } elsif ($usertypes->{$type} ne '') {
16358:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
16359:                                             }
16360:                                         }
16361:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
16362:                                     }
16363:                                 }
16364:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
16365:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
16366:                             }
16367:                         } else {
16368:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
16369:                         }
16370:                     }
16371:                     $resulttext .= '</ul></li>';
16372:                 }
16373:             }
16374:             $resulttext .= '</ul>';
16375:         } else {
16376:             $resulttext = &mt('No changes made to password settings');
16377:         }
16378:         my $cachetime = 24*60*60;
16379:         if ($updatedefaults) {
16380:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16381:             if (ref($lastactref) eq 'HASH') {
16382:                 $lastactref->{'domdefaults'} = 1;
16383:             }
16384:         }
16385:         if ($updateconf) {
16386:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
16387:             if (ref($lastactref) eq 'HASH') {
16388:                 $lastactref->{'passwdconf'} = 1;
16389:             }
16390:         }
16391:     } else {
16392:         $resulttext = '<span class="LC_error">'.
16393:             &mt('An error occurred: [_1]',$putresult).'</span>';
16394:     }
16395:     if ($errors) {
16396:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
16397:                        $errors.'</ul></p>';
16398:     }
16399:     return $resulttext;
16400: }
16401: 
16402: sub password_rule_changes {
16403:     my ($prefix,$newvalues,$current,$changes) = @_;
16404:     return unless ((ref($newvalues) eq 'HASH') &&
16405:                    (ref($current) eq 'HASH') &&
16406:                    (ref($changes) eq 'HASH'));
16407:     my (@rules,%staticdefaults);
16408:     if ($prefix eq 'passwords') {
16409:         @rules = ('min','max','numsaved');
16410:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
16411:         @rules = ('min','max');
16412:     }
16413:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
16414:     foreach my $rule (@rules) {
16415:         $env{'form.'.$prefix.'_'.$rule} =~ s/^\s+|\s+$//g;
16416:         my $ruleok;
16417:         if ($rule eq 'min') {
16418:             if ($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) {
16419:                 if ($env{'form.'.$prefix.'_'.$rule} >= $staticdefaults{$rule}) {
16420:                     $ruleok = 1;
16421:                 }
16422:             }
16423:         } elsif (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) &&
16424:                  ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
16425:             $ruleok = 1;
16426:         }
16427:         if ($ruleok) {
16428:             $newvalues->{$rule} = $env{'form.'.$prefix.'_'.$rule};
16429:             if (exists($current->{$rule})) {
16430:                 if ($newvalues->{$rule} ne $current->{$rule}) {
16431:                     $changes->{'rules'} = 1;
16432:                 }
16433:             } elsif ($rule eq 'min') {
16434:                 if ($staticdefaults{$rule} ne $newvalues->{$rule}) {
16435:                     $changes->{'rules'} = 1;
16436:                 }
16437:             } else {
16438:                 $changes->{'rules'} = 1;
16439:             }
16440:         } elsif (exists($current->{$rule})) {
16441:             $changes->{'rules'} = 1;
16442:         }
16443:     }
16444:     my @posschars = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_chars');
16445:     my @chars;
16446:     foreach my $item (sort(@posschars)) {
16447:         if ($item =~ /^(uc|lc|num|spec)$/) {
16448:             push(@chars,$item);
16449:         }
16450:     }
16451:     $newvalues->{'chars'} = \@chars;
16452:     unless ($changes->{'rules'}) {
16453:         if (ref($current->{'chars'}) eq 'ARRAY') {
16454:             my @diffs = &Apache::loncommon::compare_arrays($current->{'chars'},\@chars);
16455:             if (@diffs > 0) {
16456:                 $changes->{'rules'} = 1;
16457:             }
16458:         } else {
16459:             if (@chars > 0) {
16460:                 $changes->{'rules'} = 1;
16461:             }
16462:         }
16463:     }
16464:     return;
16465: }
16466: 
16467: sub modify_usercreation {
16468:     my ($dom,%domconfig) = @_;
16469:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
16470:     my $warningmsg;
16471:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
16472:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
16473:             if ($key eq 'cancreate') {
16474:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
16475:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
16476:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
16477:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16478:                         } else {
16479:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16480:                         }
16481:                     }
16482:                 }
16483:             } elsif ($key eq 'email_rule') {
16484:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
16485:             } else {
16486:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
16487:             }
16488:         }
16489:     }
16490:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
16491:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
16492:     my @contexts = ('author','course','requestcrs');
16493:     foreach my $item(@contexts) {
16494:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
16495:     }
16496:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
16497:         foreach my $item (@contexts) {
16498:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
16499:                 push(@{$changes{'cancreate'}},$item);
16500:             }
16501:         }
16502:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
16503:         foreach my $item (@contexts) {
16504:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
16505:                 if ($cancreate{$item} ne 'any') {
16506:                     push(@{$changes{'cancreate'}},$item);
16507:                 }
16508:             } else {
16509:                 if ($cancreate{$item} ne 'none') {
16510:                     push(@{$changes{'cancreate'}},$item);
16511:                 }
16512:             }
16513:         }
16514:     } else {
16515:         foreach my $item (@contexts)  {
16516:             push(@{$changes{'cancreate'}},$item);
16517:         }
16518:     }
16519: 
16520:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
16521:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
16522:             if (!grep(/^\Q$type\E$/,@username_rule)) {
16523:                 push(@{$changes{'username_rule'}},$type);
16524:             }
16525:         }
16526:         foreach my $type (@username_rule) {
16527:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
16528:                 push(@{$changes{'username_rule'}},$type);
16529:             }
16530:         }
16531:     } else {
16532:         push(@{$changes{'username_rule'}},@username_rule);
16533:     }
16534: 
16535:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
16536:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
16537:             if (!grep(/^\Q$type\E$/,@id_rule)) {
16538:                 push(@{$changes{'id_rule'}},$type);
16539:             }
16540:         }
16541:         foreach my $type (@id_rule) {
16542:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
16543:                 push(@{$changes{'id_rule'}},$type);
16544:             }
16545:         }
16546:     } else {
16547:         push(@{$changes{'id_rule'}},@id_rule);
16548:     }
16549: 
16550:     my @authen_contexts = ('author','course','domain');
16551:     my @authtypes = ('int','krb4','krb5','loc','lti');
16552:     my %authhash;
16553:     foreach my $item (@authen_contexts) {
16554:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
16555:         foreach my $auth (@authtypes) {
16556:             if (grep(/^\Q$auth\E$/,@authallowed)) {
16557:                 $authhash{$item}{$auth} = 1;
16558:             } else {
16559:                 $authhash{$item}{$auth} = 0;
16560:             }
16561:         }
16562:     }
16563:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
16564:         foreach my $item (@authen_contexts) {
16565:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
16566:                 foreach my $auth (@authtypes) {
16567:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
16568:                         push(@{$changes{'authtypes'}},$item);
16569:                         last;
16570:                     }
16571:                 }
16572:             }
16573:         }
16574:     } else {
16575:         foreach my $item (@authen_contexts) {
16576:             push(@{$changes{'authtypes'}},$item);
16577:         }
16578:     }
16579: 
16580:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
16581:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
16582:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
16583:     $save_usercreate{'id_rule'} = \@id_rule;
16584:     $save_usercreate{'username_rule'} = \@username_rule,
16585:     $save_usercreate{'authtypes'} = \%authhash;
16586: 
16587:     my %usercreation_hash =  (
16588:         usercreation     => \%save_usercreate,
16589:     );
16590: 
16591:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
16592:                                              $dom);
16593: 
16594:     if ($putresult eq 'ok') {
16595:         if (keys(%changes) > 0) {
16596:             $resulttext = &mt('Changes made:').'<ul>';
16597:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
16598:                 my %lt = &usercreation_types();
16599:                 foreach my $type (@{$changes{'cancreate'}}) {
16600:                     my $chgtext = $lt{$type}.', ';
16601:                     if ($cancreate{$type} eq 'none') {
16602:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
16603:                     } elsif ($cancreate{$type} eq 'any') {
16604:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
16605:                     } elsif ($cancreate{$type} eq 'official') {
16606:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
16607:                     } elsif ($cancreate{$type} eq 'unofficial') {
16608:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
16609:                     }
16610:                     $resulttext .= '<li>'.$chgtext.'</li>';
16611:                 }
16612:             }
16613:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
16614:                 my ($rules,$ruleorder) = 
16615:                     &Apache::lonnet::inst_userrules($dom,'username');
16616:                 my $chgtext = '<ul>';
16617:                 foreach my $type (@username_rule) {
16618:                     if (ref($rules->{$type}) eq 'HASH') {
16619:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
16620:                     }
16621:                 }
16622:                 $chgtext .= '</ul>';
16623:                 if (@username_rule > 0) {
16624:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
16625:                 } else {
16626:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
16627:                 }
16628:             }
16629:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
16630:                 my ($idrules,$idruleorder) = 
16631:                     &Apache::lonnet::inst_userrules($dom,'id');
16632:                 my $chgtext = '<ul>';
16633:                 foreach my $type (@id_rule) {
16634:                     if (ref($idrules->{$type}) eq 'HASH') {
16635:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
16636:                     }
16637:                 }
16638:                 $chgtext .= '</ul>';
16639:                 if (@id_rule > 0) {
16640:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
16641:                 } else {
16642:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
16643:                 }
16644:             }
16645:             my %authname = &authtype_names();
16646:             my %context_title = &context_names();
16647:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
16648:                 my $chgtext = '<ul>';
16649:                 foreach my $type (@{$changes{'authtypes'}}) {
16650:                     my @allowed;
16651:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
16652:                     foreach my $auth (@authtypes) {
16653:                         if ($authhash{$type}{$auth}) {
16654:                             push(@allowed,$authname{$auth});
16655:                         }
16656:                     }
16657:                     if (@allowed > 0) {
16658:                         $chgtext .= join(', ',@allowed).'</li>';
16659:                     } else {
16660:                         $chgtext .= &mt('none').'</li>';
16661:                     }
16662:                 }
16663:                 $chgtext .= '</ul>';
16664:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
16665:                 $resulttext .= '</li>';
16666:             }
16667:             $resulttext .= '</ul>';
16668:         } else {
16669:             $resulttext = &mt('No changes made to user creation settings');
16670:         }
16671:     } else {
16672:         $resulttext = '<span class="LC_error">'.
16673:             &mt('An error occurred: [_1]',$putresult).'</span>';
16674:     }
16675:     if ($warningmsg ne '') {
16676:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
16677:     }
16678:     return $resulttext;
16679: }
16680: 
16681: sub modify_selfcreation {
16682:     my ($dom,$lastactref,%domconfig) = @_;
16683:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
16684:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
16685:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16686:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
16687:     if (ref($typesref) eq 'ARRAY') {
16688:         @types = @{$typesref};
16689:     }
16690:     if (ref($usertypesref) eq 'HASH') {
16691:         %usertypes = %{$usertypesref};
16692:     }
16693:     $usertypes{'default'} = $othertitle;
16694: #
16695: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
16696: #
16697:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
16698:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
16699:             if ($key eq 'cancreate') {
16700:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
16701:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
16702:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
16703:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
16704:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
16705:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
16706:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
16707:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
16708:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16709:                         } else {
16710:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16711:                         }
16712:                     }
16713:                 }
16714:             } elsif ($key eq 'email_rule') {
16715:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
16716:             } else {
16717:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
16718:             }
16719:         }
16720:     }
16721: #
16722: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
16723: #
16724:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
16725:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
16726:             if ($key eq 'selfcreate') {
16727:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
16728:             } else {
16729:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
16730:             }
16731:         }
16732:     }
16733: #
16734: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
16735: #
16736:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
16737:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
16738:             if ($key eq 'inststatusguest') {
16739:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
16740:             } else {
16741:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
16742:             }
16743:         }
16744:     }
16745: 
16746:     my @contexts = ('selfcreate');
16747:     @{$cancreate{'selfcreate'}} = ();
16748:     %{$cancreate{'emailusername'}} = ();
16749:     if (@types) {
16750:         @{$cancreate{'statustocreate'}} = ();
16751:     }
16752:     %{$cancreate{'selfcreateprocessing'}} = ();
16753:     %{$cancreate{'shibenv'}} = ();
16754:     %{$cancreate{'emailverified'}} = ();
16755:     %{$cancreate{'emailoptions'}} = ();
16756:     %{$cancreate{'emaildomain'}} = ();
16757:     my %selfcreatetypes = (
16758:                              sso   => 'users authenticated by institutional single sign on',
16759:                              login => 'users authenticated by institutional log-in',
16760:                              email => 'users verified by e-mail',
16761:                           );
16762: #
16763: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
16764: # is permitted.
16765: #
16766:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
16767: 
16768:     my (@statuses,%email_rule);
16769:     foreach my $item ('login','sso','email') {
16770:         if ($item eq 'email') {
16771:             if ($env{'form.cancreate_email'}) {
16772:                 if (@types) {
16773:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
16774:                     foreach my $status (@poss_statuses) {
16775:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
16776:                             push(@statuses,$status);
16777:                         }
16778:                     }
16779:                     $save_inststatus{'inststatusguest'} = \@statuses;
16780:                 } else {
16781:                     push(@statuses,'default');
16782:                 }
16783:                 if (@statuses) {
16784:                     my %curr_rule;
16785:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
16786:                         foreach my $type (@statuses) {
16787:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
16788:                         }
16789:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
16790:                         foreach my $type (@statuses) {
16791:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
16792:                         }
16793:                     }
16794:                     push(@{$cancreate{'selfcreate'}},'email');
16795:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
16796:                     my %curremaildom;
16797:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
16798:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
16799:                     }
16800:                     foreach my $type (@statuses) {
16801:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
16802:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
16803:                         }
16804:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
16805:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
16806:                         }
16807:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
16808: #
16809: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
16810: #
16811:                             my $chosen = $1;
16812:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
16813:                                 my $emaildom;
16814:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
16815:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
16816:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
16817:                                     if (ref($curremaildom{$type}) eq 'HASH') {
16818:                                         if (exists($curremaildom{$type}{$chosen})) {
16819:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
16820:                                                 push(@{$changes{'cancreate'}},'emaildomain');
16821:                                             }
16822:                                         } elsif ($emaildom ne '') {
16823:                                             push(@{$changes{'cancreate'}},'emaildomain');
16824:                                         }
16825:                                     } elsif ($emaildom ne '') {
16826:                                         push(@{$changes{'cancreate'}},'emaildomain');
16827:                                     }
16828:                                 }
16829:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
16830:                             } elsif ($chosen eq 'custom') {
16831:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
16832:                                 $email_rule{$type} = [];
16833:                                 if (ref($emailrules) eq 'HASH') {
16834:                                     foreach my $rule (@possemail_rules) {
16835:                                         if (exists($emailrules->{$rule})) {
16836:                                             push(@{$email_rule{$type}},$rule);
16837:                                         }
16838:                                     }
16839:                                 }
16840:                                 if (@{$email_rule{$type}}) {
16841:                                     $cancreate{'emailoptions'}{$type} = 'custom';
16842:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
16843:                                         if (@{$curr_rule{$type}} > 0) {
16844:                                             foreach my $rule (@{$curr_rule{$type}}) {
16845:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
16846:                                                     push(@{$changes{'email_rule'}},$type);
16847:                                                 }
16848:                                             }
16849:                                         }
16850:                                         foreach my $type (@{$email_rule{$type}}) {
16851:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
16852:                                                 push(@{$changes{'email_rule'}},$type);
16853:                                             }
16854:                                         }
16855:                                     } else {
16856:                                         push(@{$changes{'email_rule'}},$type);
16857:                                     }
16858:                                 }
16859:                             } else {
16860:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
16861:                             }
16862:                         }
16863:                     }
16864:                     if (@types) {
16865:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
16866:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
16867:                             if (@changed) {
16868:                                 push(@{$changes{'inststatus'}},'inststatusguest');
16869:                             }
16870:                         } else {
16871:                             push(@{$changes{'inststatus'}},'inststatusguest');
16872:                         }
16873:                     }
16874:                 } else {
16875:                     delete($env{'form.cancreate_email'});
16876:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
16877:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
16878:                             push(@{$changes{'inststatus'}},'inststatusguest');
16879:                         }
16880:                     }
16881:                 }
16882:             } else {
16883:                 $save_inststatus{'inststatusguest'} = [];
16884:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
16885:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
16886:                         push(@{$changes{'inststatus'}},'inststatusguest');
16887:                     }
16888:                 }
16889:             }
16890:         } else {
16891:             if ($env{'form.cancreate_'.$item}) {
16892:                 push(@{$cancreate{'selfcreate'}},$item);
16893:             }
16894:         }
16895:     }
16896:     my (%userinfo,%savecaptcha);
16897:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
16898: #
16899: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
16900: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
16901: #
16902: 
16903:     if ($env{'form.cancreate_email'}) {
16904:         push(@contexts,'emailusername');
16905:         if (@statuses) {
16906:             foreach my $type (@statuses) {
16907:                 if (ref($infofields) eq 'ARRAY') {
16908:                     foreach my $field (@{$infofields}) {
16909:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
16910:                             $cancreate{'emailusername'}{$type}{$field} = $1;
16911:                         }
16912:                     }
16913:                 }
16914:             }
16915:         }
16916: #
16917: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
16918: # queued requests for self-creation of account verified by e-mail.
16919: #
16920: 
16921:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
16922:         @approvalnotify = sort(@approvalnotify);
16923:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
16924:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
16925:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
16926:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
16927:                     push(@{$changes{'cancreate'}},'notify');
16928:                 }
16929:             } else {
16930:                 if ($cancreate{'notify'}{'approval'}) {
16931:                     push(@{$changes{'cancreate'}},'notify');
16932:                 }
16933:             }
16934:         } elsif ($cancreate{'notify'}{'approval'}) {
16935:             push(@{$changes{'cancreate'}},'notify');
16936:         }
16937: 
16938:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
16939:     }
16940: #  
16941: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
16942: # institutional log-in.
16943: #
16944:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
16945:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
16946:                ($domdefaults{'auth_def'} eq 'localauth'))) {
16947:             $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.').' '.
16948:                           &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.');
16949:         }
16950:     }
16951:     my @fields = ('lastname','firstname','middlename','generation',
16952:                   'permanentemail','id');
16953:     my @shibfields = (@fields,'inststatus');
16954:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
16955: #
16956: # Where usernames may created for institutional log-in and/or institutional single sign on:
16957: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
16958: # may self-create accounts 
16959: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
16960: # which the user may supply, if institutional data is unavailable.
16961: #
16962:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
16963:         if (@types) {
16964:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
16965:             push(@contexts,'statustocreate');
16966:             foreach my $type (@types) {
16967:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
16968:                 foreach my $field (@fields) {
16969:                     if (grep(/^\Q$field\E$/,@modifiable)) {
16970:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
16971:                     } else {
16972:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
16973:                     }
16974:                 }
16975:             }
16976:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
16977:                 foreach my $type (@types) {
16978:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
16979:                         foreach my $field (@fields) {
16980:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
16981:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
16982:                                 push(@{$changes{'selfcreate'}},$type);
16983:                                 last;
16984:                             }
16985:                         }
16986:                     }
16987:                 }
16988:             } else {
16989:                 foreach my $type (@types) {
16990:                     push(@{$changes{'selfcreate'}},$type);
16991:                 }
16992:             }
16993:         }
16994:         foreach my $field (@shibfields) {
16995:             if ($env{'form.shibenv_'.$field} ne '') {
16996:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
16997:             }
16998:         }
16999:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
17000:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
17001:                 foreach my $field (@shibfields) {
17002:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
17003:                         push(@{$changes{'cancreate'}},'shibenv');
17004:                     }
17005:                 }
17006:             } else {
17007:                 foreach my $field (@shibfields) {
17008:                     if ($env{'form.shibenv_'.$field}) {
17009:                         push(@{$changes{'cancreate'}},'shibenv');
17010:                         last;
17011:                     }
17012:                 }
17013:             }
17014:         }
17015:     }
17016:     foreach my $item (@contexts) {
17017:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
17018:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
17019:                 if (ref($cancreate{$item}) eq 'ARRAY') {
17020:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
17021:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17022:                             push(@{$changes{'cancreate'}},$item);
17023:                         }
17024:                     }
17025:                 }
17026:             }
17027:             if (ref($cancreate{$item}) eq 'ARRAY') {
17028:                 foreach my $type (@{$cancreate{$item}}) {
17029:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
17030:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17031:                             push(@{$changes{'cancreate'}},$item);
17032:                         }
17033:                     }
17034:                 }
17035:             }
17036:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
17037:             if (ref($cancreate{$item}) eq 'HASH') {
17038:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
17039:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
17040:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
17041:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
17042:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17043:                                     push(@{$changes{'cancreate'}},$item);
17044:                                 }
17045:                             }
17046:                         }
17047:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
17048:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
17049:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17050:                                 push(@{$changes{'cancreate'}},$item);
17051:                             }
17052:                         }
17053:                     }
17054:                 }
17055:                 foreach my $type (keys(%{$cancreate{$item}})) {
17056:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
17057:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
17058:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
17059:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
17060:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17061:                                         push(@{$changes{'cancreate'}},$item);
17062:                                     }
17063:                                 }
17064:                             } else {
17065:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17066:                                     push(@{$changes{'cancreate'}},$item);
17067:                                 }
17068:                             }
17069:                         }
17070:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
17071:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
17072:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17073:                                 push(@{$changes{'cancreate'}},$item);
17074:                             }
17075:                         }
17076:                     }
17077:                 }
17078:             }
17079:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
17080:             if (ref($cancreate{$item}) eq 'ARRAY') {
17081:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
17082:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17083:                         push(@{$changes{'cancreate'}},$item);
17084:                     }
17085:                 }
17086:             }
17087:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
17088:             if (ref($cancreate{$item}) eq 'HASH') {
17089:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17090:                     push(@{$changes{'cancreate'}},$item);
17091:                 }
17092:             }
17093:         } elsif ($item eq 'emailusername') {
17094:             if (ref($cancreate{$item}) eq 'HASH') {
17095:                 foreach my $type (keys(%{$cancreate{$item}})) {
17096:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
17097:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
17098:                             if ($cancreate{$item}{$type}{$field}) {
17099:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17100:                                     push(@{$changes{'cancreate'}},$item);
17101:                                 }
17102:                                 last;
17103:                             }
17104:                         }
17105:                     }
17106:                 }
17107:             }
17108:         }
17109:     }
17110: #
17111: # Populate %save_usercreate hash with updates to self-creation configuration.
17112: #
17113:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
17114:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
17115:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
17116:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
17117:     if (ref($cancreate{'notify'}) eq 'HASH') {
17118:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
17119:     }
17120:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
17121:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
17122:     }
17123:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
17124:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
17125:     }
17126:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
17127:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
17128:     }
17129:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
17130:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
17131:     }
17132:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
17133:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
17134:     }
17135:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
17136:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
17137:     }
17138:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
17139:     $save_usercreate{'email_rule'} = \%email_rule;
17140: 
17141:     my %userconfig_hash = (
17142:             usercreation     => \%save_usercreate,
17143:             usermodification => \%save_usermodify,
17144:             inststatus       => \%save_inststatus,
17145:     );
17146: 
17147:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
17148:                                              $dom);
17149: #
17150: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
17151: #
17152:     if ($putresult eq 'ok') {
17153:         if (keys(%changes) > 0) {
17154:             $resulttext = &mt('Changes made:').'<ul>';
17155:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
17156:                 my %lt = &selfcreation_types();
17157:                 foreach my $type (@{$changes{'cancreate'}}) {
17158:                     my $chgtext = '';
17159:                     if ($type eq 'selfcreate') {
17160:                         if (@{$cancreate{$type}} == 0) {
17161:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
17162:                         } else {
17163:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
17164:                                         '<ul>';
17165:                             foreach my $case (@{$cancreate{$type}}) {
17166:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
17167:                             }
17168:                             $chgtext .= '</ul>';
17169:                             if (ref($cancreate{$type}) eq 'ARRAY') {
17170:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
17171:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
17172:                                         if (@{$cancreate{'statustocreate'}} == 0) {
17173:                                             $chgtext .= '<span class="LC_warning">'.
17174:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
17175:                                                         '</span><br />';
17176:                                         }
17177:                                     }
17178:                                 }
17179:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
17180:                                     if (!@statuses) {
17181:                                         $chgtext .= '<span class="LC_warning">'.
17182:                                                     &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.").
17183:                                                     '</span><br />';
17184: 
17185:                                     }
17186:                                 }
17187:                             }
17188:                         }
17189:                     } elsif ($type eq 'shibenv') {
17190:                         if (keys(%{$cancreate{$type}}) == 0) {
17191:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
17192:                         } else {
17193:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
17194:                                         '<ul>';
17195:                             foreach my $field (@shibfields) {
17196:                                 next if ($cancreate{$type}{$field} eq '');
17197:                                 if ($field eq 'inststatus') {
17198:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
17199:                                 } else {
17200:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
17201:                                 }
17202:                             }
17203:                             $chgtext .= '</ul>';
17204:                         }
17205:                     } elsif ($type eq 'statustocreate') {
17206:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
17207:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
17208:                             if (@{$cancreate{'selfcreate'}} > 0) {
17209:                                 if (@{$cancreate{'statustocreate'}} == 0) {
17210:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
17211:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
17212:                                         $chgtext .= '<br />'.
17213:                                                     '<span class="LC_warning">'.
17214:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
17215:                                                     '</span>';
17216:                                     }
17217:                                 } elsif (keys(%usertypes) > 0) {
17218:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
17219:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
17220:                                     } else {
17221:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
17222:                                     }
17223:                                     $chgtext .= '<ul>';
17224:                                     foreach my $case (@{$cancreate{$type}}) {
17225:                                         if ($case eq 'default') {
17226:                                             $chgtext .= '<li>'.$othertitle.'</li>';
17227:                                         } else {
17228:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
17229:                                         }
17230:                                     }
17231:                                     $chgtext .= '</ul>';
17232:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
17233:                                         $chgtext .= '<span class="LC_warning">'.
17234:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
17235:                                                     '</span>';
17236:                                     }
17237:                                 }
17238:                             } else {
17239:                                 if (@{$cancreate{$type}} == 0) {
17240:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
17241:                                 } else {
17242:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
17243:                                 }
17244:                             }
17245:                             $chgtext .= '<br />';
17246:                         }
17247:                     } elsif ($type eq 'selfcreateprocessing') {
17248:                         my %choices = &Apache::lonlocal::texthash (
17249:                                                                     automatic => 'Automatic approval',
17250:                                                                     approval  => 'Queued for approval',
17251:                                                                   );
17252:                         if (@types) {
17253:                             if (@statuses) {
17254:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
17255:                                             '<ul>';
17256:                                 foreach my $status (@statuses) {
17257:                                     if ($status eq 'default') {
17258:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
17259:                                     } else {
17260:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
17261:                                     }
17262:                                 }
17263:                                 $chgtext .= '</ul>';
17264:                             }
17265:                         } else {
17266:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
17267:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
17268:                         }
17269:                     } elsif ($type eq 'emailverified') {
17270:                         my %options = &Apache::lonlocal::texthash (
17271:                                                                     all   => 'Same as e-mail',
17272:                                                                     first => 'Omit @domain',
17273:                                                                     free  => 'Free to choose',
17274:                                                                   );
17275:                         if (@types) {
17276:                             if (@statuses) {
17277:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
17278:                                             '<ul>';
17279:                                 foreach my $status (@statuses) {
17280:                                     if ($status eq 'default') {
17281:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
17282:                                     } else {
17283:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
17284:                                     }
17285:                                 }
17286:                                 $chgtext .= '</ul>';
17287:                             }
17288:                         } else {
17289:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
17290:                                             $options{$cancreate{'emailverified'}{'default'}});
17291:                         }
17292:                     } elsif ($type eq 'emailoptions') {
17293:                         my %options = &Apache::lonlocal::texthash (
17294:                                                                     any     => 'Any e-mail',
17295:                                                                     inst    => 'Institutional only',
17296:                                                                     noninst => 'Non-institutional only',
17297:                                                                     custom  => 'Custom restrictions',
17298:                                                                   );
17299:                         if (@types) {
17300:                             if (@statuses) {
17301:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
17302:                                             '<ul>';
17303:                                 foreach my $status (@statuses) {
17304:                                     if ($type eq 'default') {
17305:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
17306:                                     } else {
17307:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
17308:                                     }
17309:                                 }
17310:                                 $chgtext .= '</ul>';
17311:                             }
17312:                         } else {
17313:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
17314:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
17315:                             } else {
17316:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
17317:                                                 $options{$cancreate{'emailoptions'}{'default'}});
17318:                             }
17319:                         }
17320:                     } elsif ($type eq 'emaildomain') {
17321:                         my $output;
17322:                         if (@statuses) {
17323:                             foreach my $type (@statuses) {
17324:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
17325:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
17326:                                         if ($type eq 'default') {
17327:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17328:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
17329:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
17330:                                             } else {
17331:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
17332:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
17333:                                             }
17334:                                         } else {
17335:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17336:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
17337:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
17338:                                             } else {
17339:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
17340:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
17341:                                             }
17342:                                         }
17343:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
17344:                                         if ($type eq 'default') {
17345:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17346:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
17347:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
17348:                                             } else {
17349:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
17350:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
17351:                                             }
17352:                                         } else {
17353:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17354:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
17355:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
17356:                                             } else {
17357:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
17358:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
17359:                                             }
17360:                                         }
17361:                                     }
17362:                                 }
17363:                             }
17364:                         }
17365:                         if ($output ne '') {
17366:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
17367:                                         '<ul>'.$output.'</ul>';
17368:                         }
17369:                     } elsif ($type eq 'captcha') {
17370:                         if ($savecaptcha{$type} eq 'notused') {
17371:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
17372:                         } else {
17373:                             my %captchas = &captcha_phrases();
17374:                             if ($captchas{$savecaptcha{$type}}) {
17375:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
17376:                             } else {
17377:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
17378:                             }
17379:                         }
17380:                     } elsif ($type eq 'recaptchakeys') {
17381:                         my ($privkey,$pubkey);
17382:                         if (ref($savecaptcha{$type}) eq 'HASH') {
17383:                             $pubkey = $savecaptcha{$type}{'public'};
17384:                             $privkey = $savecaptcha{$type}{'private'};
17385:                         }
17386:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
17387:                         if (!$pubkey) {
17388:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
17389:                         } else {
17390:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
17391:                         }
17392:                         if (!$privkey) {
17393:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
17394:                         } else {
17395:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
17396:                         }
17397:                         $chgtext .= '</ul>';
17398:                     } elsif ($type eq 'recaptchaversion') {
17399:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
17400:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
17401:                         }
17402:                     } elsif ($type eq 'emailusername') {
17403:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
17404:                             if (@statuses) {
17405:                                 foreach my $type (@statuses) {
17406:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
17407:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
17408:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
17409:                                                     '<ul>';
17410:                                             foreach my $field (@{$infofields}) {
17411:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
17412:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
17413:                                                 }
17414:                                             }
17415:                                             $chgtext .= '</ul>';
17416:                                         } else {
17417:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
17418:                                         }
17419:                                     } else {
17420:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
17421:                                     }
17422:                                 }
17423:                             }
17424:                         }
17425:                     } elsif ($type eq 'notify') {
17426:                         my $numapprove = 0;
17427:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
17428:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
17429:                                 if ($cancreate{'notify'}{'approval'}) {
17430:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
17431:                                     $numapprove ++;
17432:                                 }
17433:                             }
17434:                         }
17435:                         unless ($numapprove) {
17436:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
17437:                         }
17438:                     }
17439:                     if ($chgtext) {
17440:                         $resulttext .= '<li>'.$chgtext.'</li>';
17441:                     }
17442:                 }
17443:             }
17444:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
17445:                 my ($emailrules,$emailruleorder) =
17446:                     &Apache::lonnet::inst_userrules($dom,'email');
17447:                 foreach my $type (@{$changes{'email_rule'}}) {
17448:                     if (ref($email_rule{$type}) eq 'ARRAY') {
17449:                         my $chgtext = '<ul>';
17450:                         foreach my $rule (@{$email_rule{$type}}) {
17451:                             if (ref($emailrules->{$rule}) eq 'HASH') {
17452:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
17453:                             }
17454:                         }
17455:                         $chgtext .= '</ul>';
17456:                         my $typename;
17457:                         if (@types) {
17458:                             if ($type eq 'default') {
17459:                                 $typename = $othertitle;
17460:                             } else {
17461:                                 $typename = $usertypes{$type};
17462:                             }
17463:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
17464:                         }
17465:                         if (@{$email_rule{$type}} > 0) {
17466:                             $resulttext .= '<li>'.
17467:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
17468:                                                $usertypes{$type}).
17469:                                            $chgtext.
17470:                                            '</li>';
17471:                         } else {
17472:                             $resulttext .= '<li>'.
17473:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
17474:                                            '</li>'.
17475:                                            &mt('(Affiliation: [_1])',$typename);
17476:                         }
17477:                     }
17478:                 }
17479:             }
17480:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
17481:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
17482:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
17483:                         my $chgtext = '<ul>';
17484:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
17485:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
17486:                         }
17487:                         $chgtext .= '</ul>';
17488:                         $resulttext .= '<li>'.
17489:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
17490:                                           $chgtext.
17491:                                        '</li>';
17492:                     } else {
17493:                         $resulttext .= '<li>'.
17494:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
17495:                                        '</li>';
17496:                     }
17497:                 }
17498:             }
17499:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
17500:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
17501:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
17502:                 foreach my $type (@{$changes{'selfcreate'}}) {
17503:                     my $typename = $type;
17504:                     if (keys(%usertypes) > 0) {
17505:                         if ($usertypes{$type} ne '') {
17506:                             $typename = $usertypes{$type};
17507:                         }
17508:                     }
17509:                     my @modifiable;
17510:                     $resulttext .= '<li>'.
17511:                                     &mt('Self-creation of account by users with status: [_1]',
17512:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
17513:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
17514:                     foreach my $field (@fields) {
17515:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
17516:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
17517:                         }
17518:                     }
17519:                     if (@modifiable > 0) {
17520:                         $resulttext .= join(', ',@modifiable);
17521:                     } else {
17522:                         $resulttext .= &mt('none');
17523:                     }
17524:                     $resulttext .= '</li>';
17525:                 }
17526:                 $resulttext .= '</ul></li>';
17527:             }
17528:             $resulttext .= '</ul>';
17529:             my $cachetime = 24*60*60;
17530:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
17531:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17532:             if (ref($lastactref) eq 'HASH') {
17533:                 $lastactref->{'domdefaults'} = 1;
17534:             }
17535:         } else {
17536:             $resulttext = &mt('No changes made to self-creation settings');
17537:         }
17538:     } else {
17539:         $resulttext = '<span class="LC_error">'.
17540:             &mt('An error occurred: [_1]',$putresult).'</span>';
17541:     }
17542:     if ($warningmsg ne '') {
17543:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
17544:     }
17545:     return $resulttext;
17546: }
17547: 
17548: sub process_captcha {
17549:     my ($container,$changes,$newsettings,$currsettings) = @_;
17550:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
17551:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
17552:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
17553:         $newsettings->{'captcha'} = 'original';
17554:     }
17555:     my %current;
17556:     if (ref($currsettings) eq 'HASH') {
17557:         %current = %{$currsettings};
17558:     }
17559:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
17560:         if ($container eq 'cancreate') {
17561:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17562:                 push(@{$changes->{'cancreate'}},'captcha');
17563:             } elsif (!defined($changes->{'cancreate'})) {
17564:                 $changes->{'cancreate'} = ['captcha'];
17565:             }
17566:         } elsif ($container eq 'passwords') {
17567:             $changes->{'reset'} = 1;
17568:         } else {
17569:             $changes->{'captcha'} = 1;
17570:         }
17571:     }
17572:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
17573:     if ($newsettings->{'captcha'} eq 'recaptcha') {
17574:         $newpub = $env{'form.'.$container.'_recaptchapub'};
17575:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
17576:         $newpub =~ s/[^\w\-]//g;
17577:         $newpriv =~ s/[^\w\-]//g;
17578:         $newsettings->{'recaptchakeys'} = {
17579:                                              public  => $newpub,
17580:                                              private => $newpriv,
17581:                                           };
17582:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
17583:         $newversion =~ s/\D//g;
17584:         if ($newversion ne '2') {
17585:             $newversion = 1;
17586:         }
17587:         $newsettings->{'recaptchaversion'} = $newversion;
17588:     }
17589:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
17590:         $currpub = $current{'recaptchakeys'}{'public'};
17591:         $currpriv = $current{'recaptchakeys'}{'private'};
17592:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
17593:             $newsettings->{'recaptchakeys'} = {
17594:                                                  public  => '',
17595:                                                  private => '',
17596:                                               }
17597:         }
17598:     }
17599:     if ($current{'captcha'} eq 'recaptcha') {
17600:         $currversion = $current{'recaptchaversion'};
17601:         if ($currversion ne '2') {
17602:             $currversion = 1;
17603:         }
17604:     }
17605:     if ($currversion ne $newversion) {
17606:         if ($container eq 'cancreate') {
17607:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17608:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
17609:             } elsif (!defined($changes->{'cancreate'})) {
17610:                 $changes->{'cancreate'} = ['recaptchaversion'];
17611:             }
17612:         } elsif ($container eq 'passwords') {
17613:             $changes->{'reset'} = 1;
17614:         } else {
17615:             $changes->{'recaptchaversion'} = 1;
17616:         }
17617:     }
17618:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
17619:         if ($container eq 'cancreate') {
17620:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17621:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
17622:             } elsif (!defined($changes->{'cancreate'})) {
17623:                 $changes->{'cancreate'} = ['recaptchakeys'];
17624:             }
17625:         } elsif ($container eq 'passwords') {
17626:             $changes->{'reset'} = 1;
17627:         } else {
17628:             $changes->{'recaptchakeys'} = 1;
17629:         }
17630:     }
17631:     return;
17632: }
17633: 
17634: sub modify_usermodification {
17635:     my ($dom,%domconfig) = @_;
17636:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
17637:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
17638:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
17639:             if ($key eq 'selfcreate') {
17640:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
17641:             } else {  
17642:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
17643:             }
17644:         }
17645:     }
17646:     my @contexts = ('author','course');
17647:     my %context_title = (
17648:                            author => 'In author context',
17649:                            course => 'In course context',
17650:                         );
17651:     my @fields = ('lastname','firstname','middlename','generation',
17652:                   'permanentemail','id');
17653:     my %roles = (
17654:                   author => ['ca','aa'],
17655:                   course => ['st','ep','ta','in','cr'],
17656:                 );
17657:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
17658:     foreach my $context (@contexts) {
17659:         foreach my $role (@{$roles{$context}}) {
17660:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
17661:             foreach my $item (@fields) {
17662:                 if (grep(/^\Q$item\E$/,@modifiable)) {
17663:                     $modifyhash{$context}{$role}{$item} = 1;
17664:                 } else {
17665:                     $modifyhash{$context}{$role}{$item} = 0;
17666:                 }
17667:             }
17668:         }
17669:         if (ref($curr_usermodification{$context}) eq 'HASH') {
17670:             foreach my $role (@{$roles{$context}}) {
17671:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
17672:                     foreach my $field (@fields) {
17673:                         if ($modifyhash{$context}{$role}{$field} ne 
17674:                                 $curr_usermodification{$context}{$role}{$field}) {
17675:                             push(@{$changes{$context}},$role);
17676:                             last;
17677:                         }
17678:                     }
17679:                 }
17680:             }
17681:         } else {
17682:             foreach my $context (@contexts) {
17683:                 foreach my $role (@{$roles{$context}}) {
17684:                     push(@{$changes{$context}},$role);
17685:                 }
17686:             }
17687:         }
17688:     }
17689:     my %usermodification_hash =  (
17690:                                    usermodification => \%modifyhash,
17691:                                  );
17692:     my $putresult = &Apache::lonnet::put_dom('configuration',
17693:                                              \%usermodification_hash,$dom);
17694:     if ($putresult eq 'ok') {
17695:         if (keys(%changes) > 0) {
17696:             $resulttext = &mt('Changes made: ').'<ul>';
17697:             foreach my $context (@contexts) {
17698:                 if (ref($changes{$context}) eq 'ARRAY') {
17699:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
17700:                     if (ref($changes{$context}) eq 'ARRAY') {
17701:                         foreach my $role (@{$changes{$context}}) {
17702:                             my $rolename;
17703:                             if ($role eq 'cr') {
17704:                                 $rolename = &mt('Custom');
17705:                             } else {
17706:                                 $rolename = &Apache::lonnet::plaintext($role);
17707:                             }
17708:                             my @modifiable;
17709:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
17710:                             foreach my $field (@fields) {
17711:                                 if ($modifyhash{$context}{$role}{$field}) {
17712:                                     push(@modifiable,$fieldtitles{$field});
17713:                                 }
17714:                             }
17715:                             if (@modifiable > 0) {
17716:                                 $resulttext .= join(', ',@modifiable);
17717:                             } else {
17718:                                 $resulttext .= &mt('none'); 
17719:                             }
17720:                             $resulttext .= '</li>';
17721:                         }
17722:                         $resulttext .= '</ul></li>';
17723:                     }
17724:                 }
17725:             }
17726:             $resulttext .= '</ul>';
17727:         } else {
17728:             $resulttext = &mt('No changes made to user modification settings');
17729:         }
17730:     } else {
17731:         $resulttext = '<span class="LC_error">'.
17732:             &mt('An error occurred: [_1]',$putresult).'</span>';
17733:     }
17734:     return $resulttext;
17735: }
17736: 
17737: sub modify_defaults {
17738:     my ($dom,$lastactref,%domconfig) = @_;
17739:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
17740:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17741:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
17742:                  'portal_def');
17743:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
17744:     foreach my $item (@items) {
17745:         $newvalues{$item} = $env{'form.'.$item};
17746:         if ($item eq 'auth_def') {
17747:             if ($newvalues{$item} ne '') {
17748:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
17749:                     push(@errors,$item);
17750:                 }
17751:             }
17752:         } elsif ($item eq 'lang_def') {
17753:             if ($newvalues{$item} ne '') {
17754:                 if ($newvalues{$item} =~ /^(\w+)/) {
17755:                     my $langcode = $1;
17756:                     if ($langcode ne 'x_chef') {
17757:                         if (code2language($langcode) eq '') {
17758:                             push(@errors,$item);
17759:                         }
17760:                     }
17761:                 } else {
17762:                     push(@errors,$item);
17763:                 }
17764:             }
17765:         } elsif ($item eq 'timezone_def') {
17766:             if ($newvalues{$item} ne '') {
17767:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
17768:                     push(@errors,$item);   
17769:                 }
17770:             }
17771:         } elsif ($item eq 'datelocale_def') {
17772:             if ($newvalues{$item} ne '') {
17773:                 my @datelocale_ids = DateTime::Locale->ids();
17774:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
17775:                     push(@errors,$item);
17776:                 }
17777:             }
17778:         } elsif ($item eq 'portal_def') {
17779:             if ($newvalues{$item} ne '') {
17780:                 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])\/?$/) {
17781:                     foreach my $field ('email','web') {
17782:                         if ($env{'form.'.$item.'_'.$field}) {
17783:                             $newvalues{$item.'_'.$field} = $env{'form.'.$item.'_'.$field};
17784:                         }
17785:                     }
17786:                 } else {
17787:                     push(@errors,$item);
17788:                 }
17789:             }
17790:         }
17791:         if (grep(/^\Q$item\E$/,@errors)) {
17792:             $newvalues{$item} = $domdefaults{$item};
17793:             if ($item eq 'portal_def') {
17794:                 if ($domdefaults{$item}) {
17795:                     foreach my $field ('email','web') {
17796:                         if (exists($domdefaults{$item.'_'.$field})) {
17797:                             $newvalues{$item.'_'.$field} = $domdefaults{$item.'_'.$field};
17798:                         }
17799:                     }
17800:                 }
17801:             }
17802:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
17803:             $changes{$item} = 1;
17804:         }
17805:         if ($item eq 'portal_def') {
17806:             unless (grep(/^\Q$item\E$/,@errors)) {
17807:                 if ($newvalues{$item} eq '') {
17808:                     foreach my $field ('email','web') {
17809:                         if (exists($domdefaults{$item.'_'.$field})) {
17810:                             delete($domdefaults{$item.'_'.$field});
17811:                         }
17812:                     }
17813:                 } else {
17814:                     unless ($changes{$item}) {
17815:                         foreach my $field ('email','web') {
17816:                             if ($domdefaults{$item.'_'.$field} ne $newvalues{$item.'_'.$field}) {
17817:                                 $changes{$item} = 1;
17818:                                 last;
17819:                             }
17820:                         }
17821:                     }
17822:                     foreach my $field ('email','web') {
17823:                         if ($newvalues{$item.'_'.$field}) {
17824:                             $domdefaults{$item.'_'.$field} = $newvalues{$item.'_'.$field};
17825:                         } elsif (exists($domdefaults{$item.'_'.$field})) {
17826:                             delete($domdefaults{$item.'_'.$field});
17827:                         }
17828:                     }
17829:                 }
17830:             }
17831:         }
17832:         $domdefaults{$item} = $newvalues{$item};
17833:     }
17834:     my %staticdefaults = (
17835:                            'intauth_cost'   => 10,
17836:                            'intauth_check'  => 0,
17837:                            'intauth_switch' => 0,
17838:                          );
17839:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
17840:         if (exists($domdefaults{$item})) {
17841:             $newvalues{$item} = $domdefaults{$item};
17842:         } else {
17843:             $newvalues{$item} = $staticdefaults{$item};
17844:         }
17845:     }
17846:     my ($unamemaprules,$ruleorder);
17847:     my @possunamemaprules = &Apache::loncommon::get_env_multiple('form.unamemap_rule');
17848:     if (@possunamemaprules) {
17849:         ($unamemaprules,$ruleorder) =
17850:             &Apache::lonnet::inst_userrules($dom,'unamemap');
17851:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
17852:             if (@{$ruleorder} > 0) {
17853:                 my %possrules;
17854:                 map { $possrules{$_} = 1; } @possunamemaprules;
17855:                 foreach my $rule (@{$ruleorder}) {
17856:                     if ($possrules{$rule}) {
17857:                         push(@{$newvalues{'unamemap_rule'}},$rule);
17858:                     }
17859:                 }
17860:             }
17861:         }
17862:     }
17863:     if (ref($domdefaults{'unamemap_rule'}) eq 'ARRAY') {
17864:         if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
17865:             my @rulediffs = &Apache::loncommon::compare_arrays($domdefaults{'unamemap_rule'},
17866:                                                                $newvalues{'unamemap_rule'});
17867:             if (@rulediffs) {
17868:                 $changes{'unamemap_rule'} = 1;
17869:                 $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
17870:             }
17871:         } elsif (@{$domdefaults{'unamemap_rule'}} > 0) {
17872:             $changes{'unamemap_rule'} = 1;
17873:             delete($domdefaults{'unamemap_rule'});
17874:         }
17875:     } elsif (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
17876:         if (@{$newvalues{'unamemap_rule'}} > 0) {
17877:             $changes{'unamemap_rule'} = 1;
17878:             $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
17879:         }
17880:     }
17881:     my %defaults_hash = (
17882:                          defaults => \%newvalues,
17883:                         );
17884:     my $title = &defaults_titles();
17885: 
17886:     my $currinststatus;
17887:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
17888:         $currinststatus = $domconfig{'inststatus'};
17889:     } else {
17890:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17891:         $currinststatus = {
17892:                              inststatustypes => $usertypes,
17893:                              inststatusorder => $types,
17894:                              inststatusguest => [],
17895:                           };
17896:     }
17897:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
17898:     my @allpos;
17899:     my %alltypes;
17900:     my @inststatusguest;
17901:     if (ref($currinststatus) eq 'HASH') {
17902:         if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
17903:             foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
17904:                 unless (grep(/^\Q$type\E$/,@todelete)) {
17905:                     push(@inststatusguest,$type);
17906:                 }
17907:             }
17908:         }
17909:     }
17910:     my ($currtitles,$currorder);
17911:     if (ref($currinststatus) eq 'HASH') {
17912:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
17913:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
17914:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
17915:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
17916:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
17917:                     }
17918:                 }
17919:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
17920:                     my $position = $env{'form.inststatus_pos_'.$type};
17921:                     $position =~ s/\D+//g;
17922:                     $allpos[$position] = $type;
17923:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
17924:                     $alltypes{$type} =~ s/`//g;
17925:                 }
17926:             }
17927:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
17928:             $currtitles =~ s/,$//;
17929:         }
17930:     }
17931:     if ($env{'form.addinststatus'}) {
17932:         my $newtype = $env{'form.addinststatus'};
17933:         $newtype =~ s/\W//g;
17934:         unless (exists($alltypes{$newtype})) {
17935:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
17936:             $alltypes{$newtype} =~ s/`//g; 
17937:             my $position = $env{'form.addinststatus_pos'};
17938:             $position =~ s/\D+//g;
17939:             if ($position ne '') {
17940:                 $allpos[$position] = $newtype;
17941:             }
17942:         }
17943:     }
17944:     my @orderedstatus;
17945:     foreach my $type (@allpos) {
17946:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
17947:             push(@orderedstatus,$type);
17948:         }
17949:     }
17950:     foreach my $type (keys(%alltypes)) {
17951:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
17952:             delete($alltypes{$type});
17953:         }
17954:     }
17955:     $defaults_hash{'inststatus'} = {
17956:                                      inststatustypes => \%alltypes,
17957:                                      inststatusorder => \@orderedstatus,
17958:                                      inststatusguest => \@inststatusguest,
17959:                                    };
17960:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
17961:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
17962:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
17963:         }
17964:     }
17965:     if ($currorder ne join(',',@orderedstatus)) {
17966:         $changes{'inststatus'}{'inststatusorder'} = 1;
17967:     }
17968:     my $newtitles;
17969:     foreach my $item (@orderedstatus) {
17970:         $newtitles .= $alltypes{$item}.',';
17971:     }
17972:     $newtitles =~ s/,$//;
17973:     if ($currtitles ne $newtitles) {
17974:         $changes{'inststatus'}{'inststatustypes'} = 1;
17975:     }
17976:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
17977:                                              $dom);
17978:     if ($putresult eq 'ok') {
17979:         if (keys(%changes) > 0) {
17980:             $resulttext = &mt('Changes made:').'<ul>';
17981:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
17982:             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";
17983:             foreach my $item (sort(keys(%changes))) {
17984:                 if ($item eq 'inststatus') {
17985:                     if (ref($changes{'inststatus'}) eq 'HASH') {
17986:                         if (@orderedstatus) {
17987:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
17988:                             foreach my $type (@orderedstatus) { 
17989:                                 $resulttext .= $alltypes{$type}.', ';
17990:                             }
17991:                             $resulttext =~ s/, $//;
17992:                             $resulttext .= '</li>';
17993:                         } else {
17994:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
17995:                         }
17996:                     }
17997:                 } elsif ($item eq 'unamemap_rule') {
17998:                     if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
17999:                         my @rulenames;
18000:                         if (ref($unamemaprules) eq 'HASH') {
18001:                             foreach my $rule (@{$newvalues{'unamemap_rule'}}) {
18002:                                 if (ref($unamemaprules->{$rule}) eq 'HASH') {
18003:                                     push(@rulenames,$unamemaprules->{$rule}->{'name'});
18004:                                 }
18005:                             }
18006:                         }
18007:                         if (@rulenames) {
18008:                             $resulttext .= '<li>'.&mt('Mapping for missing usernames includes: [_1]',
18009:                                                      '<ul><li>'.join('</li><li>',@rulenames).'</li></ul>').
18010:                                            '</li>';
18011:                         } else {
18012:                             $resulttext .= '<li>'.&mt('No mapping for missing usernames via standard log-in').'</li>';
18013:                         }
18014:                     } else {
18015:                         $resulttext .= '<li>'.&mt('Mapping for missing usernames via standard log-in deleted').'</li>';
18016:                     }
18017:                 } else {
18018:                     my $value = $env{'form.'.$item};
18019:                     if ($value eq '') {
18020:                         $value = &mt('none');
18021:                     } elsif ($item eq 'auth_def') {
18022:                         my %authnames = &authtype_names();
18023:                         my %shortauth = (
18024:                                           internal   => 'int',
18025:                                           krb4       => 'krb4',
18026:                                           krb5       => 'krb5',
18027:                                           localauth  => 'loc',
18028:                                           lti        => 'lti',
18029:                         );
18030:                         $value = $authnames{$shortauth{$value}};
18031:                     }
18032:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
18033:                     $mailmsgtext .= "$title->{$item} set to $value\n";
18034:                     if ($item eq 'portal_def') {
18035:                         if ($env{'form.'.$item} ne '') {
18036:                             foreach my $field ('email','web') {
18037:                                 $value = $env{'form.'.$item.'_'.$field};
18038:                                 if ($value) {
18039:                                     $value = &mt('Yes');
18040:                                 } else {
18041:                                     $value = &mt('No');
18042:                                 }
18043:                                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$field},$value).'</li>';
18044:                             }
18045:                         }
18046:                     }
18047:                 }
18048:             }
18049:             $resulttext .= '</ul>';
18050:             $mailmsgtext .= "\n";
18051:             my $cachetime = 24*60*60;
18052:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18053:             if (ref($lastactref) eq 'HASH') {
18054:                 $lastactref->{'domdefaults'} = 1;
18055:             }
18056:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
18057:                 my $notify = 1;
18058:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
18059:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
18060:                         $notify = 0;
18061:                     }
18062:                 }
18063:                 if ($notify) {
18064:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
18065:                                                "LON-CAPA Domain Settings Change - $dom",
18066:                                                $mailmsgtext);
18067:                 }
18068:             }
18069:         } else {
18070:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
18071:         }
18072:     } else {
18073:         $resulttext = '<span class="LC_error">'.
18074:             &mt('An error occurred: [_1]',$putresult).'</span>';
18075:     }
18076:     if (@errors > 0) {
18077:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
18078:         foreach my $item (@errors) {
18079:             $resulttext .= ' "'.$title->{$item}.'",';
18080:         }
18081:         $resulttext =~ s/,$//;
18082:     }
18083:     return $resulttext;
18084: }
18085: 
18086: sub modify_scantron {
18087:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
18088:     my ($resulttext,%confhash,%changes,$errors);
18089:     my $custom = 'custom.tab';
18090:     my $default = 'default.tab';
18091:     my $servadm = $r->dir_config('lonAdmEMail');
18092:     my ($configuserok,$author_ok,$switchserver) =
18093:         &config_check($dom,$confname,$servadm);
18094:     if ($env{'form.scantronformat.filename'} ne '') {
18095:         my $error;
18096:         if ($configuserok eq 'ok') {
18097:             if ($switchserver) {
18098:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
18099:             } else {
18100:                 if ($author_ok eq 'ok') {
18101:                     my $modified = [];
18102:                     my ($result,$scantronurl) =
18103:                         &Apache::lonconfigsettings::publishlogo($r,'upload','scantronformat',$dom,
18104:                                                                 $confname,'scantron','','',$custom,
18105:                                                                 $modified);
18106:                     if ($result eq 'ok') {
18107:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
18108:                         $changes{'scantronformat'} = 1;
18109:                         &update_modify_urls($r,$modified);
18110:                     } else {
18111:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
18112:                     }
18113:                 } else {
18114:                     $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);
18115:                 }
18116:             }
18117:         } else {
18118:             $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);
18119:         }
18120:         if ($error) {
18121:             &Apache::lonnet::logthis($error);
18122:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
18123:         }
18124:     }
18125:     if (ref($domconfig{'scantron'}) eq 'HASH') {
18126:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
18127:             if ($env{'form.scantronformat_del'}) {
18128:                 $confhash{'scantron'}{'scantronformat'} = '';
18129:                 $changes{'scantronformat'} = 1;
18130:             } else {
18131:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
18132:             }
18133:         }
18134:     }
18135:     my @options = ('hdr','pad','rem');
18136:     my @fields = &scantroncsv_fields();
18137:     my %titles = &scantronconfig_titles();
18138:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
18139:     my ($newdat,$currdat,%newcol,%currcol);
18140:     if (grep(/^dat$/,@formats)) {
18141:         $confhash{'scantron'}{config}{dat} = 1;
18142:         $newdat = 1;
18143:     } else {
18144:         $newdat = 0;
18145:     }
18146:     if (grep(/^csv$/,@formats)) {
18147:         my %bynum;
18148:         foreach my $field (@fields) {
18149:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
18150:                 my $posscol = $1;
18151:                 if (($posscol < 20) && (!$bynum{$posscol})) {
18152:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
18153:                     $bynum{$posscol} = $field;
18154:                     $newcol{$field} = $posscol;
18155:                 }
18156:             }
18157:         }
18158:         if (keys(%newcol)) {
18159:             foreach my $option (@options) {
18160:                 if ($env{'form.scantroncsv_'.$option}) {
18161:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
18162:                 }
18163:             }
18164:         }
18165:     }
18166:     $currdat = 1;
18167:     if (ref($domconfig{'scantron'}) eq 'HASH') {
18168:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
18169:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
18170:                 $currdat = 0;
18171:             }
18172:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
18173:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
18174:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
18175:                 }
18176:             }
18177:         }
18178:     }
18179:     if ($currdat != $newdat) {
18180:         $changes{'config'} = 1;
18181:     } else {
18182:         foreach my $field (@fields) {
18183:             if ($currcol{$field} ne '') {
18184:                 if ($currcol{$field} ne $newcol{$field}) {
18185:                     $changes{'config'} = 1;
18186:                     last;
18187:                 }
18188:             } elsif ($newcol{$field} ne '') {
18189:                 $changes{'config'} = 1;
18190:                 last;
18191:             }
18192:         }
18193:     }
18194:     if (keys(%confhash) > 0) {
18195:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
18196:                                                  $dom);
18197:         if ($putresult eq 'ok') {
18198:             if (keys(%changes) > 0) {
18199:                 if (ref($confhash{'scantron'}) eq 'HASH') {
18200:                     $resulttext = &mt('Changes made:').'<ul>';
18201:                     if ($changes{'scantronformat'}) {
18202:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
18203:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
18204:                         } else {
18205:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
18206:                         }
18207:                     }
18208:                     if ($changes{'config'}) {
18209:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
18210:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
18211:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
18212:                             }
18213:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
18214:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
18215:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
18216:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
18217:                                         foreach my $field (@fields) {
18218:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
18219:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
18220:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
18221:                                             }
18222:                                         }
18223:                                         $resulttext .= '</ul></li>';
18224:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
18225:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
18226:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
18227:                                                 foreach my $option (@options) {
18228:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
18229:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
18230:                                                     }
18231:                                                 }
18232:                                                 $resulttext .= '</ul></li>';
18233:                                             }
18234:                                         }
18235:                                     }
18236:                                 }
18237:                             }
18238:                         } else {
18239:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
18240:                         }
18241:                     }
18242:                     $resulttext .= '</ul>';
18243:                 } else {
18244:                     $resulttext = &mt('Changes made to bubblesheet format file.');
18245:                 }
18246:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
18247:                 if (ref($lastactref) eq 'HASH') {
18248:                     $lastactref->{'domainconfig'} = 1;
18249:                 }
18250:             } else {
18251:                 $resulttext = &mt('No changes made to bubblesheet format settings');
18252:             }
18253:         } else {
18254:             $resulttext = '<span class="LC_error">'.
18255:                 &mt('An error occurred: [_1]',$putresult).'</span>';
18256:         }
18257:     } else {
18258:         $resulttext = &mt('No changes made to bubblesheet format settings');
18259:     }
18260:     if ($errors) {
18261:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
18262:                        $errors.'</ul></p>';
18263:     }
18264:     return $resulttext;
18265: }
18266: 
18267: sub modify_coursecategories {
18268:     my ($dom,$lastactref,%domconfig) = @_;
18269:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
18270:         $cathash);
18271:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
18272:     my @catitems = ('unauth','auth');
18273:     my @cattypes = ('std','domonly','codesrch','none');
18274:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
18275:         $cathash = $domconfig{'coursecategories'}{'cats'};
18276:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
18277:             $changes{'togglecats'} = 1;
18278:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
18279:         }
18280:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
18281:             $changes{'categorize'} = 1;
18282:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
18283:         }
18284:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
18285:             $changes{'togglecatscomm'} = 1;
18286:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
18287:         }
18288:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
18289:             $changes{'categorizecomm'} = 1;
18290:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
18291:         }
18292:         foreach my $item (@catitems) {
18293:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
18294:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
18295:                     $changes{$item} = 1;
18296:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
18297:                 }
18298:             }
18299:         }
18300:     } else {
18301:         $changes{'togglecats'} = 1;
18302:         $changes{'categorize'} = 1;
18303:         $changes{'togglecatscomm'} = 1;
18304:         $changes{'categorizecomm'} = 1;
18305:         $domconfig{'coursecategories'} = {
18306:                                              togglecats => $env{'form.togglecats'},
18307:                                              categorize => $env{'form.categorize'},
18308:                                              togglecatscomm => $env{'form.togglecatscomm'},
18309:                                              categorizecomm => $env{'form.categorizecomm'},
18310:                                          };
18311:         foreach my $item (@catitems) {
18312:             if ($env{'form.coursecat_'.$item} ne 'std') {
18313:                 $changes{$item} = 1;
18314:             }
18315:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
18316:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
18317:             }
18318:         }
18319:     }
18320:     if (ref($cathash) eq 'HASH') {
18321:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
18322:             push (@deletecategory,'instcode::0');
18323:         }
18324:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
18325:             push(@deletecategory,'communities::0');
18326:         }
18327:     }
18328:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
18329:     if (ref($cathash) eq 'HASH') {
18330:         if (@deletecategory > 0) {
18331:             #FIXME Need to remove category from all courses using a deleted category 
18332:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
18333:             foreach my $item (@deletecategory) {
18334:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
18335:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
18336:                     $deletions{$item} = 1;
18337:                     &recurse_cat_deletes($item,$cathash,\%deletions);
18338:                 }
18339:             }
18340:         }
18341:         foreach my $item (keys(%{$cathash})) {
18342:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
18343:             if ($cathash->{$item} ne $env{'form.'.$item}) {
18344:                 $reorderings{$item} = 1;
18345:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
18346:             }
18347:             if ($env{'form.addcategory_name_'.$item} ne '') {
18348:                 my $newcat = $env{'form.addcategory_name_'.$item};
18349:                 my $newdepth = $depth+1;
18350:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
18351:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
18352:                 $adds{$newitem} = 1; 
18353:             }
18354:             if ($env{'form.subcat_'.$item} ne '') {
18355:                 my $newcat = $env{'form.subcat_'.$item};
18356:                 my $newdepth = $depth+1;
18357:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
18358:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
18359:                 $adds{$newitem} = 1;
18360:             }
18361:         }
18362:     }
18363:     if ($env{'form.instcode'} eq '1') {
18364:         if (ref($cathash) eq 'HASH') {
18365:             my $newitem = 'instcode::0';
18366:             if ($cathash->{$newitem} eq '') {  
18367:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
18368:                 $adds{$newitem} = 1;
18369:             }
18370:         } else {
18371:             my $newitem = 'instcode::0';
18372:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
18373:             $adds{$newitem} = 1;
18374:         }
18375:     }
18376:     if ($env{'form.communities'} eq '1') {
18377:         if (ref($cathash) eq 'HASH') {
18378:             my $newitem = 'communities::0';
18379:             if ($cathash->{$newitem} eq '') {
18380:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
18381:                 $adds{$newitem} = 1;
18382:             }
18383:         } else {
18384:             my $newitem = 'communities::0';
18385:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
18386:             $adds{$newitem} = 1;
18387:         }
18388:     }
18389:     if ($env{'form.addcategory_name'} ne '') {
18390:         if (($env{'form.addcategory_name'} ne 'instcode') &&
18391:             ($env{'form.addcategory_name'} ne 'communities')) {
18392:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
18393:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
18394:             $adds{$newitem} = 1;
18395:         }
18396:     }
18397:     my $putresult;
18398:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
18399:         if (keys(%deletions) > 0) {
18400:             foreach my $key (keys(%deletions)) {
18401:                 if ($predelallitems{$key} ne '') {
18402:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
18403:                 }
18404:             }
18405:         }
18406:         my (@chkcats,@chktrails,%chkallitems);
18407:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
18408:         if (ref($chkcats[0]) eq 'ARRAY') {
18409:             my $depth = 0;
18410:             my $chg = 0;
18411:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
18412:                 my $name = $chkcats[0][$i];
18413:                 my $item;
18414:                 if ($name eq '') {
18415:                     $chg ++;
18416:                 } else {
18417:                     $item = &escape($name).'::0';
18418:                     if ($chg) {
18419:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
18420:                     }
18421:                     $depth ++; 
18422:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
18423:                     $depth --;
18424:                 }
18425:             }
18426:         }
18427:     }
18428:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
18429:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
18430:         if ($putresult eq 'ok') {
18431:             my %title = (
18432:                          togglecats     => 'Show/Hide a course in catalog',
18433:                          categorize     => 'Assign a category to a course',
18434:                          togglecatscomm => 'Show/Hide a community in catalog',
18435:                          categorizecomm => 'Assign a category to a community',
18436:                         );
18437:             my %level = (
18438:                          dom  => 'set in Domain ("Modify Course/Community")',
18439:                          crs  => 'set in Course ("Course Configuration")',
18440:                          comm => 'set in Community ("Community Configuration")',
18441:                          none     => 'No catalog',
18442:                          std      => 'Standard catalog',
18443:                          domonly  => 'Domain-only catalog',
18444:                          codesrch => 'Code search form',
18445:                         );
18446:             $resulttext = &mt('Changes made:').'<ul>';
18447:             if ($changes{'togglecats'}) {
18448:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
18449:             }
18450:             if ($changes{'categorize'}) {
18451:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
18452:             }
18453:             if ($changes{'togglecatscomm'}) {
18454:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
18455:             }
18456:             if ($changes{'categorizecomm'}) {
18457:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
18458:             }
18459:             if ($changes{'unauth'}) {
18460:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
18461:             }
18462:             if ($changes{'auth'}) {
18463:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
18464:             }
18465:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
18466:                 my $cathash;
18467:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
18468:                     $cathash = $domconfig{'coursecategories'}{'cats'};
18469:                 } else {
18470:                     $cathash = {};
18471:                 } 
18472:                 my (@cats,@trails,%allitems);
18473:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
18474:                 if (keys(%deletions) > 0) {
18475:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
18476:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
18477:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
18478:                     }
18479:                     $resulttext .= '</ul></li>';
18480:                 }
18481:                 if (keys(%reorderings) > 0) {
18482:                     my %sort_by_trail;
18483:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
18484:                     foreach my $key (keys(%reorderings)) {
18485:                         if ($allitems{$key} ne '') {
18486:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
18487:                         }
18488:                     }
18489:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
18490:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
18491:                     }
18492:                     $resulttext .= '</ul></li>';
18493:                 }
18494:                 if (keys(%adds) > 0) {
18495:                     my %sort_by_trail;
18496:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
18497:                     foreach my $key (keys(%adds)) {
18498:                         if ($allitems{$key} ne '') {
18499:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
18500:                         }
18501:                     }
18502:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
18503:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
18504:                     }
18505:                     $resulttext .= '</ul></li>';
18506:                 }
18507:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
18508:                 if (ref($lastactref) eq 'HASH') {
18509:                     $lastactref->{'cats'} = 1;
18510:                 }
18511:             }
18512:             $resulttext .= '</ul>';
18513:             if ($changes{'unauth'} || $changes{'auth'}) {
18514:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
18515:                 if ($changes{'auth'}) {
18516:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
18517:                 }
18518:                 if ($changes{'unauth'}) {
18519:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
18520:                 }
18521:                 my $cachetime = 24*60*60;
18522:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18523:                 if (ref($lastactref) eq 'HASH') {
18524:                     $lastactref->{'domdefaults'} = 1;
18525:                 }
18526:             }
18527:         } else {
18528:             $resulttext = '<span class="LC_error">'.
18529:                           &mt('An error occurred: [_1]',$putresult).'</span>';
18530:         }
18531:     } else {
18532:         $resulttext = &mt('No changes made to course and community categories');
18533:     }
18534:     return $resulttext;
18535: }
18536: 
18537: sub modify_serverstatuses {
18538:     my ($dom,%domconfig) = @_;
18539:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
18540:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
18541:         %currserverstatus = %{$domconfig{'serverstatuses'}};
18542:     }
18543:     my @pages = &serverstatus_pages();
18544:     foreach my $type (@pages) {
18545:         $newserverstatus{$type}{'namedusers'} = '';
18546:         $newserverstatus{$type}{'machines'} = '';
18547:         if (defined($env{'form.'.$type.'_namedusers'})) {
18548:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
18549:             my @okusers;
18550:             foreach my $user (@users) {
18551:                 my ($uname,$udom) = split(/:/,$user);
18552:                 if (($udom =~ /^$match_domain$/) &&   
18553:                     (&Apache::lonnet::domain($udom)) &&
18554:                     ($uname =~ /^$match_username$/)) {
18555:                     if (!grep(/^\Q$user\E/,@okusers)) {
18556:                         push(@okusers,$user);
18557:                     }
18558:                 }
18559:             }
18560:             if (@okusers > 0) {
18561:                  @okusers = sort(@okusers);
18562:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
18563:             }
18564:         }
18565:         if (defined($env{'form.'.$type.'_machines'})) {
18566:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
18567:             my @okmachines;
18568:             foreach my $ip (@machines) {
18569:                 my @parts = split(/\./,$ip);
18570:                 next if (@parts < 4);
18571:                 my $badip = 0;
18572:                 for (my $i=0; $i<4; $i++) {
18573:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
18574:                         $badip = 1;
18575:                         last;
18576:                     }
18577:                 }
18578:                 if (!$badip) {
18579:                     push(@okmachines,$ip);     
18580:                 }
18581:             }
18582:             @okmachines = sort(@okmachines);
18583:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
18584:         }
18585:     }
18586:     my %serverstatushash =  (
18587:                                 serverstatuses => \%newserverstatus,
18588:                             );
18589:     foreach my $type (@pages) {
18590:         foreach my $setting ('namedusers','machines') {
18591:             my (@current,@new);
18592:             if (ref($currserverstatus{$type}) eq 'HASH') {
18593:                 if ($currserverstatus{$type}{$setting} ne '') { 
18594:                     @current = split(/,/,$currserverstatus{$type}{$setting});
18595:                 }
18596:             }
18597:             if ($newserverstatus{$type}{$setting} ne '') {
18598:                 @new = split(/,/,$newserverstatus{$type}{$setting});
18599:             }
18600:             if (@current > 0) {
18601:                 if (@new > 0) {
18602:                     foreach my $item (@current) {
18603:                         if (!grep(/^\Q$item\E$/,@new)) {
18604:                             $changes{$type}{$setting} = 1;
18605:                             last;
18606:                         }
18607:                     }
18608:                     foreach my $item (@new) {
18609:                         if (!grep(/^\Q$item\E$/,@current)) {
18610:                             $changes{$type}{$setting} = 1;
18611:                             last;
18612:                         }
18613:                     }
18614:                 } else {
18615:                     $changes{$type}{$setting} = 1;
18616:                 }
18617:             } elsif (@new > 0) {
18618:                 $changes{$type}{$setting} = 1;
18619:             }
18620:         }
18621:     }
18622:     if (keys(%changes) > 0) {
18623:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
18624:         my $putresult = &Apache::lonnet::put_dom('configuration',
18625:                                                  \%serverstatushash,$dom);
18626:         if ($putresult eq 'ok') {
18627:             $resulttext .= &mt('Changes made:').'<ul>';
18628:             foreach my $type (@pages) {
18629:                 if (ref($changes{$type}) eq 'HASH') {
18630:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
18631:                     if ($changes{$type}{'namedusers'}) {
18632:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
18633:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
18634:                         } else {
18635:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
18636:                         }
18637:                     }
18638:                     if ($changes{$type}{'machines'}) {
18639:                         if ($newserverstatus{$type}{'machines'} eq '') {
18640:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
18641:                         } else {
18642:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
18643:                         }
18644: 
18645:                     }
18646:                     $resulttext .= '</ul></li>';
18647:                 }
18648:             }
18649:             $resulttext .= '</ul>';
18650:         } else {
18651:             $resulttext = '<span class="LC_error">'.
18652:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
18653: 
18654:         }
18655:     } else {
18656:         $resulttext = &mt('No changes made to access to server status pages');
18657:     }
18658:     return $resulttext;
18659: }
18660: 
18661: sub modify_helpsettings {
18662:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
18663:     my ($resulttext,$errors,%changes,%helphash);
18664:     my %defaultchecked = ('submitbugs' => 'on');
18665:     my @offon = ('off','on');
18666:     my @toggles = ('submitbugs');
18667:     my %current = ('submitbugs' => '',
18668:                    'adhoc'      => {},
18669:                   );
18670:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
18671:         %current = %{$domconfig{'helpsettings'}};
18672:     }
18673:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18674:     foreach my $item (@toggles) {
18675:         if ($defaultchecked{$item} eq 'on') { 
18676:             if ($current{$item} eq '') {
18677:                 if ($env{'form.'.$item} eq '0') {
18678:                     $changes{$item} = 1;
18679:                 }
18680:             } elsif ($current{$item} ne $env{'form.'.$item}) {
18681:                 $changes{$item} = 1;
18682:             }
18683:         } elsif ($defaultchecked{$item} eq 'off') {
18684:             if ($current{$item} eq '') {
18685:                 if ($env{'form.'.$item} eq '1') {
18686:                     $changes{$item} = 1;
18687:                 }
18688:             } elsif ($current{$item} ne $env{'form.'.$item}) {
18689:                 $changes{$item} = 1;
18690:             }
18691:         }
18692:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
18693:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
18694:         }
18695:     }
18696:     my $maxnum = $env{'form.helproles_maxnum'};
18697:     my $confname = $dom.'-domainconfig';
18698:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
18699:     my (@allpos,%newsettings,%changedprivs,$newrole);
18700:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
18701:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
18702:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
18703:     my %lt = &Apache::lonlocal::texthash(
18704:                     s      => 'system',
18705:                     d      => 'domain',
18706:                     order  => 'Display order',
18707:                     access => 'Role usage',
18708:                     all    => 'All with domain helpdesk or helpdesk assistant role',
18709:                     dh     => 'All with domain helpdesk role',
18710:                     da     => 'All with domain helpdesk assistant role',
18711:                     none   => 'None',
18712:                     status => 'Determined based on institutional status',
18713:                     inc    => 'Include all, but exclude specific personnel',
18714:                     exc    => 'Exclude all, but include specific personnel',
18715:     );
18716:     for (my $num=0; $num<=$maxnum; $num++) {
18717:         my ($prefix,$identifier,$rolename,%curr);
18718:         if ($num == $maxnum) {
18719:             next unless ($env{'form.newcusthelp'} == $maxnum);
18720:             $identifier = 'custhelp'.$num;
18721:             $prefix = 'helproles_'.$num;
18722:             $rolename = $env{'form.custhelpname'.$num};
18723:             $rolename=~s/[^A-Za-z0-9]//gs;
18724:             next if ($rolename eq '');
18725:             next if (exists($existing{'rolesdef_'.$rolename}));
18726:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
18727:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
18728:                                                      $newprivs{'c'},$confname,$dom);
18729:             if ($result ne 'ok') {
18730:                 $errors .= '<li><span class="LC_error">'.
18731:                            &mt('An error occurred storing the new custom role: [_1]',
18732:                            $result).'</span></li>';
18733:                 next;
18734:             } else {
18735:                 $changedprivs{$rolename} = \%newprivs;
18736:                 $newrole = $rolename;
18737:             }
18738:         } else {
18739:             $prefix = 'helproles_'.$num;
18740:             $rolename = $env{'form.'.$prefix};
18741:             next if ($rolename eq '');
18742:             next unless (exists($existing{'rolesdef_'.$rolename}));
18743:             $identifier = 'custhelp'.$num;
18744:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
18745:             my %currprivs;
18746:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
18747:                 split(/\_/,$existing{'rolesdef_'.$rolename});
18748:             foreach my $level ('c','d','s') {
18749:                 if ($newprivs{$level} ne $currprivs{$level}) {
18750:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
18751:                                                              $newprivs{'c'},$confname,$dom);
18752:                     if ($result ne 'ok') {
18753:                         $errors .= '<li><span class="LC_error">'.
18754:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
18755:                                        $rolename,$result).'</span></li>';
18756:                     } else {
18757:                         $changedprivs{$rolename} = \%newprivs;
18758:                     }
18759:                     last;
18760:                 }
18761:             }
18762:             if (ref($current{'adhoc'}) eq 'HASH') {
18763:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
18764:                     %curr = %{$current{'adhoc'}{$rolename}};
18765:                 }
18766:             }
18767:         }
18768:         my $newpos = $env{'form.'.$prefix.'_pos'};
18769:         $newpos =~ s/\D+//g;
18770:         $allpos[$newpos] = $rolename;
18771:         my $newdesc = $env{'form.'.$prefix.'_desc'};
18772:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
18773:         if ($curr{'desc'}) {
18774:             if ($curr{'desc'} ne $newdesc) {
18775:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
18776:                 $newsettings{$rolename}{'desc'} = $newdesc;
18777:             }
18778:         } elsif ($newdesc ne '') {
18779:             $changes{'customrole'}{$rolename}{'desc'} = 1;
18780:             $newsettings{$rolename}{'desc'} = $newdesc;
18781:         }
18782:         my $access = $env{'form.'.$prefix.'_access'};
18783:         if (grep(/^\Q$access\E$/,@accesstypes)) {
18784:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
18785:             if ($access eq 'status') {
18786:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
18787:                 if (scalar(@statuses) == 0) {
18788:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
18789:                 } else {
18790:                     my (@shownstatus,$numtypes);
18791:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
18792:                     if (ref($types) eq 'ARRAY') {
18793:                         $numtypes = scalar(@{$types});
18794:                         foreach my $type (sort(@statuses)) {
18795:                             if ($type eq 'default') {
18796:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
18797:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
18798:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
18799:                                 push(@shownstatus,$usertypes->{$type});
18800:                             }
18801:                         }
18802:                     }
18803:                     if (grep(/^default$/,@statuses)) {
18804:                         push(@shownstatus,$othertitle);
18805:                     }
18806:                     if (scalar(@shownstatus) == 1+$numtypes) {
18807:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
18808:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
18809:                     } else {
18810:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
18811:                         if (ref($curr{'status'}) eq 'ARRAY') {
18812:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
18813:                             if (@diffs) {
18814:                                 $changes{'customrole'}{$rolename}{$access} = 1;
18815:                             }
18816:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
18817:                             $changes{'customrole'}{$rolename}{$access} = 1;
18818:                         }
18819:                     }
18820:                 }
18821:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
18822:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
18823:                 my @newspecstaff;
18824:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
18825:                 foreach my $person (sort(@personnel)) {
18826:                     if ($domhelpdesk{$person}) {
18827:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
18828:                     }
18829:                 }
18830:                 if (ref($curr{$access}) eq 'ARRAY') {
18831:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
18832:                     if (@diffs) {
18833:                         $changes{'customrole'}{$rolename}{$access} = 1;
18834:                     }
18835:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
18836:                     $changes{'customrole'}{$rolename}{$access} = 1;
18837:                 }
18838:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
18839:                     my ($uname,$udom) = split(/:/,$person);
18840:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
18841:                 }
18842:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
18843:             }
18844:         } else {
18845:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
18846:         }
18847:         unless ($curr{'access'} eq $access) {
18848:             $changes{'customrole'}{$rolename}{'access'} = 1;
18849:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
18850:         }
18851:     }
18852:     if (@allpos > 0) {
18853:         my $idx = 0;
18854:         foreach my $rolename (@allpos) {
18855:             if ($rolename ne '') {
18856:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
18857:                 if (ref($current{'adhoc'}) eq 'HASH') {
18858:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
18859:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
18860:                             $changes{'customrole'}{$rolename}{'order'} = 1;
18861:                             $newsettings{$rolename}{'order'} = $idx+1;
18862:                         }
18863:                     }
18864:                 }
18865:                 $idx ++;
18866:             }
18867:         }
18868:     }
18869:     my $putresult;
18870:     if (keys(%changes) > 0) {
18871:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
18872:         if ($putresult eq 'ok') {
18873:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
18874:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
18875:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
18876:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
18877:                 }
18878:             }
18879:             my $cachetime = 24*60*60;
18880:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18881:             if (ref($lastactref) eq 'HASH') {
18882:                 $lastactref->{'domdefaults'} = 1;
18883:             }
18884:         } else {
18885:             $errors .= '<li><span class="LC_error">'.
18886:                        &mt('An error occurred storing the settings: [_1]',
18887:                            $putresult).'</span></li>';
18888:         }
18889:     }
18890:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
18891:         $resulttext = &mt('Changes made:').'<ul>';
18892:         my (%shownprivs,@levelorder);
18893:         @levelorder = ('c','d','s');
18894:         if ((keys(%changes)) && ($putresult eq 'ok')) {
18895:             foreach my $item (sort(keys(%changes))) {
18896:                 if ($item eq 'submitbugs') {
18897:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
18898:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
18899:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
18900:                 } elsif ($item eq 'customrole') {
18901:                     if (ref($changes{'customrole'}) eq 'HASH') {
18902:                         my @keyorder = ('order','desc','access','status','exc','inc');
18903:                         my %keytext = &Apache::lonlocal::texthash(
18904:                                                                    order  => 'Order',
18905:                                                                    desc   => 'Role description',
18906:                                                                    access => 'Role usage',
18907:                                                                    status => 'Allowed institutional types',
18908:                                                                    exc    => 'Allowed personnel',
18909:                                                                    inc    => 'Disallowed personnel',
18910:                         );
18911:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
18912:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
18913:                                 if ($role eq $newrole) {
18914:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
18915:                                                               $role).'<ul>';
18916:                                 } else {
18917:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
18918:                                                               $role).'<ul>';
18919:                                 }
18920:                                 foreach my $key (@keyorder) {
18921:                                     if ($changes{'customrole'}{$role}{$key}) {
18922:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
18923:                                                                   $keytext{$key},$newsettings{$role}{$key}).
18924:                                                        '</li>';
18925:                                     }
18926:                                 }
18927:                                 if (ref($changedprivs{$role}) eq 'HASH') {
18928:                                     $shownprivs{$role} = 1;
18929:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
18930:                                     foreach my $level (@levelorder) {
18931:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
18932:                                             next if ($item eq '');
18933:                                             my ($priv) = split(/\&/,$item,2);
18934:                                             if (&Apache::lonnet::plaintext($priv)) {
18935:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
18936:                                                 unless ($level eq 'c') {
18937:                                                     $resulttext .= ' ('.$lt{$level}.')';
18938:                                                 }
18939:                                                 $resulttext .= '</li>';
18940:                                             }
18941:                                         }
18942:                                     }
18943:                                     $resulttext .= '</ul>';
18944:                                 }
18945:                                 $resulttext .= '</ul></li>';
18946:                             }
18947:                         }
18948:                     }
18949:                 }
18950:             }
18951:         }
18952:         if (keys(%changedprivs)) {
18953:             foreach my $role (sort(keys(%changedprivs))) {
18954:                 unless ($shownprivs{$role}) {
18955:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
18956:                                               $role).'<ul>'.
18957:                                    '<li>'.&mt('Privileges set to :').'<ul>';
18958:                     foreach my $level (@levelorder) {
18959:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
18960:                             next if ($item eq '');
18961:                             my ($priv) = split(/\&/,$item,2);
18962:                             if (&Apache::lonnet::plaintext($priv)) {
18963:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
18964:                                 unless ($level eq 'c') {
18965:                                     $resulttext .= ' ('.$lt{$level}.')';
18966:                                 }
18967:                                 $resulttext .= '</li>';
18968:                             }
18969:                         }
18970:                     }
18971:                     $resulttext .= '</ul></li></ul></li>';
18972:                 }
18973:             }
18974:         }
18975:         $resulttext .= '</ul>';
18976:     } else {
18977:         $resulttext = &mt('No changes made to help settings');
18978:     }
18979:     if ($errors) {
18980:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
18981:                                     $errors.'</ul>';
18982:     }
18983:     return $resulttext;
18984: }
18985: 
18986: sub modify_coursedefaults {
18987:     my ($dom,$lastactref,%domconfig) = @_;
18988:     my ($resulttext,$errors,%changes,%defaultshash);
18989:     my %defaultchecked = (
18990:                            'uselcmath'       => 'on',
18991:                            'usejsme'         => 'on',
18992:                            'inline_chem'     => 'on',
18993:                            'ltiauth'         => 'off',
18994:                          );
18995:     my @toggles = ('uselcmath','usejsme','inline_chem','ltiauth');
18996:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
18997:                    'uploadquota_community','uploadquota_textbook','coursequota_official',
18998:                    'coursequota_unofficial','coursequota_community','coursequota_textbook',
18999:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
19000:                    'mysqltables_textbook');
19001:     my @types = ('official','unofficial','community','textbook');
19002:     my %staticdefaults = (
19003:                            anonsurvey_threshold => 10,
19004:                            uploadquota          => 500,
19005:                            coursequota          => 20,
19006:                            postsubmit           => 60,
19007:                            mysqltables          => 172800,
19008:                            domexttool           => 1,
19009:                          );
19010:     my %texoptions = (
19011:                         MathJax  => 'MathJax',
19012:                         mimetex  => &mt('Convert to Images'),
19013:                         tth      => &mt('TeX to HTML'),
19014:                      );
19015:     $defaultshash{'coursedefaults'} = {};
19016: 
19017:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
19018:         if ($domconfig{'coursedefaults'} eq '') {
19019:             $domconfig{'coursedefaults'} = {};
19020:         }
19021:     }
19022: 
19023:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
19024:         foreach my $item (@toggles) {
19025:             if ($defaultchecked{$item} eq 'on') {
19026:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
19027:                     ($env{'form.'.$item} eq '0')) {
19028:                     $changes{$item} = 1;
19029:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
19030:                     $changes{$item} = 1;
19031:                 }
19032:             } elsif ($defaultchecked{$item} eq 'off') {
19033:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
19034:                     ($env{'form.'.$item} eq '1')) {
19035:                     $changes{$item} = 1;
19036:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
19037:                     $changes{$item} = 1;
19038:                 }
19039:             }
19040:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
19041:         }
19042:         foreach my $item (@numbers) {
19043:             my ($currdef,$newdef);
19044:             $newdef = $env{'form.'.$item};
19045:             if ($item eq 'anonsurvey_threshold') {
19046:                 $currdef = $domconfig{'coursedefaults'}{$item};
19047:                 $newdef =~ s/\D//g;
19048:                 if ($newdef eq '' || $newdef < 1) {
19049:                     $newdef = 1;
19050:                 }
19051:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
19052:             } else {
19053:                 my ($setting,$type) = ($item =~ /^(uploadquota|coursequota|mysqltables)_(\w+)$/);
19054:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
19055:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
19056:                 }
19057:                 $newdef =~ s/[^\w.\-]//g;
19058:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
19059:             }
19060:             if ($currdef ne $newdef) {
19061:                 if ($item eq 'anonsurvey_threshold') {
19062:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
19063:                         $changes{$item} = 1;
19064:                     }
19065:                 } elsif ($item =~ /^(uploadquota|coursequota|mysqltables)_/) {
19066:                     my $setting = $1;
19067:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
19068:                         $changes{$setting} = 1;
19069:                     }
19070:                 }
19071:             }
19072:         }
19073:         my $texengine;
19074:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
19075:             $texengine = $env{'form.texengine'};
19076:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
19077:             if ($currdef eq '') {
19078:                 unless ($texengine eq $Apache::lonnet::deftex) {
19079:                     $changes{'texengine'} = 1;
19080:                 }
19081:             } elsif ($currdef ne $texengine) {
19082:                 $changes{'texengine'} = 1;
19083:             }
19084:         }
19085:         if ($texengine ne '') {
19086:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
19087:         }
19088:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
19089:         my @currclonecode;
19090:         if (ref($currclone) eq 'HASH') {
19091:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
19092:                 @currclonecode = @{$currclone->{'instcode'}};
19093:             }
19094:         }
19095:         my $newclone;
19096:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
19097:             $newclone = $env{'form.canclone'};
19098:         }
19099:         if ($newclone eq 'instcode') {
19100:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
19101:             my (%codedefaults,@code_order,@clonecode);
19102:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
19103:                                                     \@code_order);
19104:             foreach my $item (@code_order) {
19105:                 if (grep(/^\Q$item\E$/,@newcodes)) {
19106:                     push(@clonecode,$item);
19107:                 }
19108:             }
19109:             if (@clonecode) {
19110:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
19111:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
19112:                 if (@diffs) {
19113:                     $changes{'canclone'} = 1;
19114:                 }
19115:             } else {
19116:                 $newclone eq '';
19117:             }
19118:         } elsif ($newclone ne '') {
19119:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
19120:         }
19121:         if ($newclone ne $currclone) {
19122:             $changes{'canclone'} = 1;
19123:         }
19124:         my %credits;
19125:         foreach my $type (@types) {
19126:             unless ($type eq 'community') {
19127:                 $credits{$type} = $env{'form.'.$type.'_credits'};
19128:                 $credits{$type} =~ s/[^\d.]+//g;
19129:             }
19130:         }
19131:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
19132:             ($env{'form.coursecredits'} eq '1')) {
19133:             $changes{'coursecredits'} = 1;
19134:             foreach my $type (keys(%credits)) {
19135:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
19136:             }
19137:         } else {
19138:             if ($env{'form.coursecredits'} eq '1') {
19139:                 foreach my $type (@types) {
19140:                     unless ($type eq 'community') {
19141:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
19142:                             $changes{'coursecredits'} = 1;
19143:                         }
19144:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
19145:                     }
19146:                 }
19147:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
19148:                 foreach my $type (@types) {
19149:                     unless ($type eq 'community') {
19150:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
19151:                             $changes{'coursecredits'} = 1;
19152:                             last;
19153:                         }
19154:                     }
19155:                 }
19156:             }
19157:         }
19158:         if ($env{'form.postsubmit'} eq '1') {
19159:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
19160:             my %currtimeout;
19161:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19162:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
19163:                     $changes{'postsubmit'} = 1;
19164:                 }
19165:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
19166:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
19167:                 }
19168:             } else {
19169:                 $changes{'postsubmit'} = 1;
19170:             }
19171:             foreach my $type (@types) {
19172:                 my $timeout = $env{'form.'.$type.'_timeout'};
19173:                 $timeout =~ s/\D//g;
19174:                 if ($timeout == $staticdefaults{'postsubmit'}) {
19175:                     $timeout = '';
19176:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
19177:                     $timeout = '0';
19178:                 }
19179:                 unless ($timeout eq '') {
19180:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
19181:                 }
19182:                 if (exists($currtimeout{$type})) {
19183:                     if ($timeout ne $currtimeout{$type}) {
19184:                         $changes{'postsubmit'} = 1;
19185:                     }
19186:                 } elsif ($timeout ne '') {
19187:                     $changes{'postsubmit'} = 1;
19188:                 }
19189:             }
19190:         } else {
19191:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
19192:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19193:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
19194:                     $changes{'postsubmit'} = 1;
19195:                 }
19196:             } else {
19197:                 $changes{'postsubmit'} = 1;
19198:             }
19199:         }
19200:         my (%newdomexttool,%newexttool,%olddomexttool,%oldexttool);
19201:         map { $newdomexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.domexttool');
19202:         map { $newexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.exttool');
19203:         if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
19204:             %olddomexttool = %{$domconfig{'coursedefaults'}{'domexttool'}};
19205:         } else {
19206:            foreach my $type (@types) {
19207:                if ($staticdefaults{'domexttool'}) {
19208:                    $olddomexttool{$type} = 1;
19209:                } else {
19210:                    $olddomexttool{$type} = 0;
19211:                }
19212:             }
19213:         }
19214:         if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
19215:             %oldexttool = %{$domconfig{'coursedefaults'}{'exttool'}};
19216:         } else {
19217:             foreach my $type (@types) {
19218:                if ($staticdefaults{'exttool'}) {
19219:                    $oldexttool{$type} = 1;
19220:                } else {
19221:                    $oldexttool{$type} = 0;
19222:                }
19223:             }
19224:         }
19225:         foreach my $type (@types) {
19226:             unless ($newdomexttool{$type}) {
19227:                 $newdomexttool{$type} = 0;
19228:             }
19229:             unless ($newexttool{$type}) {
19230:                 $newexttool{$type} = 0;
19231:             }
19232:             if ($newdomexttool{$type} != $olddomexttool{$type}) {
19233:                 $changes{'domexttool'} = 1;
19234:             }
19235:             if ($newexttool{$type} != $oldexttool{$type}) {
19236:                 $changes{'exttool'} = 1;
19237:             }
19238:         }
19239:         $defaultshash{'coursedefaults'}{'domexttool'} = \%newdomexttool;
19240:         $defaultshash{'coursedefaults'}{'exttool'} = \%newexttool;
19241:     }
19242:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
19243:                                              $dom);
19244:     if ($putresult eq 'ok') {
19245:         if (keys(%changes) > 0) {
19246:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19247:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
19248:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
19249:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'}) ||
19250:                 ($changes{'inline_chem'}) || ($changes{'ltiauth'}) || ($changes{'domexttool'}) ||
19251:                 ($changes{'exttool'}) || ($changes{'coursequota'})) {
19252:                 foreach my $item ('uselcmath','usejsme','inline_chem','texengine','ltiauth') {
19253:                     if ($changes{$item}) {
19254:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
19255:                     }
19256:                 }
19257:                 if ($changes{'coursecredits'}) {
19258:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
19259:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
19260:                             $domdefaults{$type.'credits'} =
19261:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
19262:                         }
19263:                     }
19264:                 }
19265:                 if ($changes{'postsubmit'}) {
19266:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19267:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
19268:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
19269:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
19270:                                 $domdefaults{$type.'postsubtimeout'} =
19271:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
19272:                             }
19273:                         }
19274:                     }
19275:                 }
19276:                 if ($changes{'uploadquota'}) {
19277:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
19278:                         foreach my $type (@types) {
19279:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
19280:                         }
19281:                     }
19282:                 }
19283:                 if ($changes{'coursequota'}) {
19284:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
19285:                         foreach my $type (@types) {
19286:                             $domdefaults{$type.'coursequota'}=$defaultshash{'coursedefaults'}{'coursequota'}{$type};
19287:                         }
19288:                     }
19289:                 }
19290:                 if ($changes{'canclone'}) {
19291:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
19292:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
19293:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
19294:                             if (@clonecodes) {
19295:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
19296:                             }
19297:                         }
19298:                     } else {
19299:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
19300:                     }
19301:                 }
19302:                 if ($changes{'domexttool'}) {
19303:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
19304:                         foreach my $type (@types) {
19305:                             $domdefaults{$type.'domexttool'}=$defaultshash{'coursedefaults'}{'domexttool'}{$type};
19306:                         }
19307:                     }
19308:                 }
19309:                 if ($changes{'exttool'}) {
19310:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
19311:                         foreach my $type (@types) {
19312:                             $domdefaults{$type.'exttool'}=$defaultshash{'coursedefaults'}{'exttool'}{$type};
19313:                         }
19314:                     }
19315:                 }
19316:                 my $cachetime = 24*60*60;
19317:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19318:                 if (ref($lastactref) eq 'HASH') {
19319:                     $lastactref->{'domdefaults'} = 1;
19320:                 }
19321:             }
19322:             $resulttext = &mt('Changes made:').'<ul>';
19323:             foreach my $item (sort(keys(%changes))) {
19324:                 if ($item eq 'uselcmath') {
19325:                     if ($env{'form.'.$item} eq '1') {
19326:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
19327:                     } else {
19328:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
19329:                     }
19330:                 } elsif ($item eq 'usejsme') {
19331:                     if ($env{'form.'.$item} eq '1') {
19332:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
19333:                     } else {
19334:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
19335:                     }
19336:                 } elsif ($item eq 'inline_chem') {
19337:                     if ($env{'form.'.$item} eq '1') {
19338:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses inline previewer').'</li>';
19339:                     } else {
19340:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses pop-up previewer').'</li>';
19341:                     }
19342:                 } elsif ($item eq 'texengine') {
19343:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
19344:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
19345:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
19346:                     }
19347:                 } elsif ($item eq 'anonsurvey_threshold') {
19348:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
19349:                 } elsif ($item eq 'uploadquota') {
19350:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
19351:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
19352:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
19353:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
19354:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
19355:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
19356:                                        '</ul>'.
19357:                                        '</li>';
19358:                     } else {
19359:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
19360:                     }
19361:                 } elsif ($item eq 'coursequota') {
19362:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
19363:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course set as follows:').'<ul>'.
19364:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'official'}.'</b>').'</li>'.
19365:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'unofficial'}.'</b>').'</li>'.
19366:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'textbook'}.'</b>').'</li>'.
19367:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'community'}.'</b>').'</li>'.
19368:                                        '</ul>'.
19369:                                        '</li>';
19370:                     } else {
19371:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course remains default: [_1] MB',$staticdefaults{'coursequota'}).'</li>';
19372:                     }
19373:                 } elsif ($item eq 'mysqltables') {
19374:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
19375:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
19376:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
19377:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
19378:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
19379:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
19380:                                        '</ul>'.
19381:                                        '</li>';
19382:                     } else {
19383:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
19384:                     }
19385:                 } elsif ($item eq 'postsubmit') {
19386:                     if ($domdefaults{'postsubmit'} eq 'off') {
19387:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
19388:                     } else {
19389:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
19390:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19391:                             $resulttext .= &mt('durations:').'<ul>';
19392:                             foreach my $type (@types) {
19393:                                 $resulttext .= '<li>';
19394:                                 my $timeout;
19395:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
19396:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
19397:                                 }
19398:                                 my $display;
19399:                                 if ($timeout eq '0') {
19400:                                     $display = &mt('unlimited');
19401:                                 } elsif ($timeout eq '') {
19402:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
19403:                                 } else {
19404:                                     $display = &mt('[quant,_1,second]',$timeout);
19405:                                 }
19406:                                 if ($type eq 'community') {
19407:                                     $resulttext .= &mt('Communities');
19408:                                 } elsif ($type eq 'official') {
19409:                                     $resulttext .= &mt('Official courses');
19410:                                 } elsif ($type eq 'unofficial') {
19411:                                     $resulttext .= &mt('Unofficial courses');
19412:                                 } elsif ($type eq 'textbook') {
19413:                                     $resulttext .= &mt('Textbook courses');
19414:                                 }
19415:                                 $resulttext .= ' -- '.$display.'</li>';
19416:                             }
19417:                             $resulttext .= '</ul>';
19418:                         }
19419:                         $resulttext .= '</li>';
19420:                     }
19421:                 } elsif ($item eq 'coursecredits') {
19422:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
19423:                         if (($domdefaults{'officialcredits'} eq '') &&
19424:                             ($domdefaults{'unofficialcredits'} eq '') &&
19425:                             ($domdefaults{'textbookcredits'} eq '')) {
19426:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
19427:                         } else {
19428:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
19429:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
19430:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
19431:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
19432:                                            '</ul>'.
19433:                                            '</li>';
19434:                         }
19435:                     } else {
19436:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
19437:                     }
19438:                 } elsif ($item eq 'canclone') {
19439:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
19440:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
19441:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
19442:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
19443:                         }
19444:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
19445:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
19446:                     } else {
19447:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
19448:                     }
19449:                 } elsif ($item eq 'ltiauth') {
19450:                     if ($env{'form.'.$item} eq '1') {
19451:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL need not require re-authentication').'</li>';
19452:                     } else {
19453:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL will require re-authentication').'</li>';
19454:                     }
19455:                 } elsif ($item eq 'domexttool') {
19456:                     my @noyes = (&mt('no'),&mt('yes'));
19457:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
19458:                         $resulttext .= '<li>'.&mt('External Tools defined in the domain may be used as follows:').'<ul>'.
19459:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'official'}].'</b>').'</li>'.
19460:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'unofficial'}].'</b>').'</li>'.
19461:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'textbook'}].'</b>').'</li>'.
19462:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'placement'}].'</b>').'</li>'.
19463:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'community'}].'</b>').'</li>'.
19464:                                        '</ul>'.
19465:                                        '</li>';
19466:                     } else {
19467:                         $resulttext .= '<li>'.&mt('External Tools defined in the domain may be used in all course types, by default').'</li>';
19468:                     }
19469:                 } elsif ($item eq 'exttool') {
19470:                     my @noyes = (&mt('no'),&mt('yes'));
19471:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
19472:                         $resulttext .= '<li>'.&mt('External Tools can be defined and configured in course containers as follows:').'<ul>'.
19473:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'official'}].'</b>').'</li>'.
19474:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'unofficial'}].'</b>').'</li>'.
19475:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'textbook'}].'</b>').'</li>'.
19476:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'placement'}].'</b>').'</li>'.
19477:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'community'}].'</b>').'</li>'.
19478:                                        '</ul>'.
19479:                                        '</li>';
19480:                     } else {
19481:                         $resulttext .= '<li>'.&mt('External Tools can not be defined in any course types, by default').'</li>';
19482:                     }
19483:                 }
19484:             }
19485:             $resulttext .= '</ul>';
19486:         } else {
19487:             $resulttext = &mt('No changes made to course defaults');
19488:         }
19489:     } else {
19490:         $resulttext = '<span class="LC_error">'.
19491:             &mt('An error occurred: [_1]',$putresult).'</span>';
19492:     }
19493:     return $resulttext;
19494: }
19495: 
19496: sub modify_selfenrollment {
19497:     my ($dom,$lastactref,%domconfig) = @_;
19498:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
19499:     my @types = ('official','unofficial','community','textbook');
19500:     my %titles = &tool_titles();
19501:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
19502:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
19503:     $ordered{'default'} = ['types','registered','approval','limit'];
19504: 
19505:     my (%roles,%shown,%toplevel);
19506:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
19507: 
19508:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
19509:         if ($domconfig{'selfenrollment'} eq '') {
19510:             $domconfig{'selfenrollment'} = {};
19511:         }
19512:     }
19513:     %toplevel = (
19514:                   admin      => 'Configuration Rights',
19515:                   default    => 'Default settings',
19516:                   validation => 'Validation of self-enrollment requests',
19517:                 );
19518:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
19519: 
19520:     if (ref($ordered{'admin'}) eq 'ARRAY') {
19521:         foreach my $item (@{$ordered{'admin'}}) {
19522:             foreach my $type (@types) {
19523:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
19524:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
19525:                 } else {
19526:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
19527:                 }
19528:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
19529:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
19530:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
19531:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
19532:                             push(@{$changes{'admin'}{$type}},$item);
19533:                         }
19534:                     } else {
19535:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
19536:                             push(@{$changes{'admin'}{$type}},$item);
19537:                         }
19538:                     }
19539:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
19540:                     push(@{$changes{'admin'}{$type}},$item);
19541:                 }
19542:             }
19543:         }
19544:     }
19545: 
19546:     foreach my $item (@{$ordered{'default'}}) {
19547:         foreach my $type (@types) {
19548:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
19549:             if ($item eq 'types') {
19550:                 unless (($value eq 'all') || ($value eq 'dom')) {
19551:                     $value = '';
19552:                 }
19553:             } elsif ($item eq 'registered') {
19554:                 unless ($value eq '1') {
19555:                     $value = 0;
19556:                 }
19557:             } elsif ($item eq 'approval') {
19558:                 unless ($value =~ /^[012]$/) {
19559:                     $value = 0;
19560:                 }
19561:             } else {
19562:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
19563:                     $value = 'none';
19564:                 }
19565:             }
19566:             $selfenrollhash{'default'}{$type}{$item} = $value;
19567:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
19568:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
19569:                     if ($selfenrollhash{'default'}{$type}{$item} ne
19570:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
19571:                          push(@{$changes{'default'}{$type}},$item);
19572:                     }
19573:                 } else {
19574:                     push(@{$changes{'default'}{$type}},$item);
19575:                 }
19576:             } else {
19577:                 push(@{$changes{'default'}{$type}},$item);
19578:             }
19579:             if ($item eq 'limit') {
19580:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
19581:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
19582:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
19583:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
19584:                     }
19585:                 } else {
19586:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
19587:                 }
19588:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
19589:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
19590:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
19591:                          push(@{$changes{'default'}{$type}},'cap');
19592:                     }
19593:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
19594:                     push(@{$changes{'default'}{$type}},'cap');
19595:                 }
19596:             }
19597:         }
19598:     }
19599: 
19600:     foreach my $item (@{$itemsref}) {
19601:         if ($item eq 'fields') {
19602:             my @changed;
19603:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
19604:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
19605:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
19606:             }
19607:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
19608:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
19609:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
19610:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
19611:                 } else {
19612:                     @changed = @{$selfenrollhash{'validation'}{$item}};
19613:                 }
19614:             } else {
19615:                 @changed = @{$selfenrollhash{'validation'}{$item}};
19616:             }
19617:             if (@changed) {
19618:                 if ($selfenrollhash{'validation'}{$item}) { 
19619:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
19620:                 } else {
19621:                     $changes{'validation'}{$item} = &mt('None');
19622:                 }
19623:             }
19624:         } else {
19625:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
19626:             if ($item eq 'markup') {
19627:                if ($env{'form.selfenroll_validation_'.$item}) {
19628:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
19629:                }
19630:             }
19631:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
19632:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
19633:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
19634:                 }
19635:             }
19636:         }
19637:     }
19638: 
19639:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
19640:                                              $dom);
19641:     if ($putresult eq 'ok') {
19642:         if (keys(%changes) > 0) {
19643:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19644:             $resulttext = &mt('Changes made:').'<ul>';
19645:             foreach my $key ('admin','default','validation') {
19646:                 if (ref($changes{$key}) eq 'HASH') {
19647:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
19648:                     if ($key eq 'validation') {
19649:                         foreach my $item (@{$itemsref}) {
19650:                             if (exists($changes{$key}{$item})) {
19651:                                 if ($item eq 'markup') {
19652:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
19653:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
19654:                                 } else {  
19655:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
19656:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
19657:                                 }
19658:                             }
19659:                         }
19660:                     } else {
19661:                         foreach my $type (@types) {
19662:                             if ($type eq 'community') {
19663:                                 $roles{'1'} = &mt('Community personnel');
19664:                             } else {
19665:                                 $roles{'1'} = &mt('Course personnel');
19666:                             }
19667:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
19668:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
19669:                                     if ($key eq 'admin') {
19670:                                         my @mgrdc = ();
19671:                                         if (ref($ordered{$key}) eq 'ARRAY') {
19672:                                             foreach my $item (@{$ordered{'admin'}}) {
19673:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
19674:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
19675:                                                         push(@mgrdc,$item);
19676:                                                     }
19677:                                                 }
19678:                                             }
19679:                                             if (@mgrdc) {
19680:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
19681:                                             } else {
19682:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
19683:                                             }
19684:                                         }
19685:                                     } else {
19686:                                         if (ref($ordered{$key}) eq 'ARRAY') {
19687:                                             foreach my $item (@{$ordered{$key}}) {
19688:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
19689:                                                     $domdefaults{$type.'selfenroll'.$item} =
19690:                                                         $selfenrollhash{$key}{$type}{$item};
19691:                                                 }
19692:                                             }
19693:                                         }
19694:                                     }
19695:                                 }
19696:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
19697:                                 foreach my $item (@{$ordered{$key}}) {
19698:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
19699:                                         $resulttext .= '<li>';
19700:                                         if ($key eq 'admin') {
19701:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
19702:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
19703:                                         } else {
19704:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
19705:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
19706:                                         }
19707:                                         $resulttext .= '</li>';
19708:                                     }
19709:                                 }
19710:                                 $resulttext .= '</ul></li>';
19711:                             }
19712:                         }
19713:                         $resulttext .= '</ul></li>'; 
19714:                     }
19715:                 }
19716:             }
19717:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
19718:                 my $cachetime = 24*60*60;
19719:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19720:                 if (ref($lastactref) eq 'HASH') {
19721:                     $lastactref->{'domdefaults'} = 1;
19722:                 }
19723:             }
19724:             $resulttext .= '</ul>';
19725:         } else {
19726:             $resulttext = &mt('No changes made to self-enrollment settings');
19727:         }
19728:     } else {
19729:         $resulttext = '<span class="LC_error">'.
19730:             &mt('An error occurred: [_1]',$putresult).'</span>';
19731:     }
19732:     return $resulttext;
19733: }
19734: 
19735: sub modify_wafproxy {
19736:     my ($dom,$action,$lastactref,%domconfig) = @_;
19737:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
19738:     my (%othercontrol,%canset,%values,%curralias,%currsaml,%currvalue,@warnings,
19739:         %wafproxy,%changes,%expirecache,%expiresaml);
19740:     foreach my $server (sort(keys(%servers))) {
19741:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
19742:         if ($serverhome eq $server) {
19743:             my $serverdom = &Apache::lonnet::host_domain($server);
19744:             if ($serverdom eq $dom) {
19745:                 $canset{$server} = 1;
19746:             }
19747:         }
19748:     }
19749:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
19750:         %{$values{$dom}} = ();
19751:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
19752:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
19753:         }
19754:         if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
19755:             %currsaml = %{$domconfig{'wafproxy'}{'saml'}};
19756:         }
19757:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
19758:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
19759:         }
19760:     }
19761:     my $output;
19762:     if (keys(%canset)) {
19763:         %{$wafproxy{'alias'}} = ();
19764:         %{$wafproxy{'saml'}} = ();
19765:         foreach my $key (sort(keys(%canset))) {
19766:             if ($env{'form.wafproxy_'.$dom}) {
19767:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
19768:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
19769:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
19770:                     $changes{'alias'} = 1;
19771:                 }
19772:                 if ($env{'form.wafproxy_alias_saml_'.$key}) {
19773:                     $wafproxy{'saml'}{$key} = 1;
19774:                 }
19775:                 if ($wafproxy{'saml'}{$key} ne $currsaml{$key}) {
19776:                     $changes{'saml'} = 1;
19777:                 }
19778:             } else {
19779:                 $wafproxy{'alias'}{$key} = '';
19780:                 $wafproxy{'saml'}{$key} = '';
19781:                 if ($curralias{$key}) {
19782:                     $changes{'alias'} = 1;
19783:                 }
19784:                 if ($currsaml{$key}) {
19785:                     $changes{'saml'} = 1;
19786:                 }
19787:             }
19788:             if ($wafproxy{'alias'}{$key} eq '') {
19789:                 if ($curralias{$key}) {
19790:                     $expirecache{$key} = 1;
19791:                 }
19792:                 delete($wafproxy{'alias'}{$key});
19793:             }
19794:             if ($wafproxy{'saml'}{$key} eq '') {
19795:                 if ($currsaml{$key}) {
19796:                     $expiresaml{$key} = 1;
19797:                 }
19798:                 delete($wafproxy{'saml'}{$key});
19799:             }
19800:         }
19801:         unless (keys(%{$wafproxy{'alias'}})) {
19802:             delete($wafproxy{'alias'});
19803:         }
19804:         unless (keys(%{$wafproxy{'saml'}})) {
19805:             delete($wafproxy{'saml'});
19806:         }
19807:         # Localization for values in %warn occurs in &mt() calls separately.
19808:         my %warn = (
19809:                      trusted => 'trusted IP range(s)',
19810:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
19811:                      vpnext => 'IP range(s) for backend WAF connections',
19812:                    );
19813:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
19814:             my $possible = $env{'form.wafproxy_'.$item};
19815:             $possible =~ s/^\s+|\s+$//g;
19816:             if ($possible ne '') {
19817:                 if ($item eq 'remoteip') {
19818:                     if ($possible =~ /^[mhn]$/) {
19819:                         $wafproxy{$item} = $possible;
19820:                     }
19821:                 } elsif ($item eq 'ipheader') {
19822:                     if ($wafproxy{'remoteip'} eq 'h') {
19823:                         $wafproxy{$item} = $possible;
19824:                     }
19825:                 } elsif ($item eq 'sslopt') {
19826:                     if ($possible =~ /^0|1$/) {
19827:                         $wafproxy{$item} = $possible;
19828:                     }
19829:                 } else {
19830:                     my (@ok,$count);
19831:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
19832:                         unless ($env{'form.wafproxy_vpnaccess'}) {
19833:                             $possible = '';
19834:                         }
19835:                     } elsif ($item eq 'trusted') {
19836:                         unless ($wafproxy{'remoteip'} eq 'h') {
19837:                             $possible = '';
19838:                         }
19839:                     }
19840:                     unless ($possible eq '') {
19841:                         $possible =~ s/[\r\n]+/\s/g;
19842:                         $possible =~ s/\s*-\s*/-/g;
19843:                         $possible =~ s/\s+/,/g;
19844:                         $possible =~ s/,+/,/g;
19845:                     }
19846:                     $count = 0;
19847:                     if ($possible ne '') {
19848:                         foreach my $poss (split(/\,/,$possible)) {
19849:                             $count ++;
19850:                             $poss = &validate_ip_pattern($poss);
19851:                             if ($poss ne '') {
19852:                                 push(@ok,$poss);
19853:                             }
19854:                         }
19855:                         my $diff = $count - scalar(@ok);
19856:                         if ($diff) {
19857:                             push(@warnings,'<li>'.
19858:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
19859:                                      $diff,$warn{$item}).
19860:                                  '</li>');
19861:                         }
19862:                         if (@ok) {
19863:                             my @cidr_list;
19864:                             foreach my $item (@ok) {
19865:                                 @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
19866:                             }
19867:                             $wafproxy{$item} = join(',',@cidr_list);
19868:                         }
19869:                     }
19870:                 }
19871:                 if ($wafproxy{$item} ne $currvalue{$item}) {
19872:                     $changes{$item} = 1;
19873:                 }
19874:             } elsif ($currvalue{$item}) {
19875:                 $changes{$item} = 1;
19876:             }
19877:         }
19878:     } else {
19879:         if (keys(%curralias)) {
19880:             $changes{'alias'} = 1;
19881:         }
19882:         if (keys(%currsaml)) {
19883:             $changes{'saml'} = 1;
19884:         }
19885:         if (keys(%currvalue)) {
19886:             foreach my $key (keys(%currvalue)) {
19887:                 $changes{$key} = 1;
19888:             }
19889:         }
19890:     }
19891:     if (keys(%changes)) {
19892:         my %defaultshash = (
19893:                               wafproxy => \%wafproxy,
19894:                            );
19895:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
19896:                                                  $dom);
19897:         if ($putresult eq 'ok') {
19898:             my $cachetime = 24*60*60;
19899:             my (%domdefaults,$updatedomdefs);
19900:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
19901:                 if ($changes{$item}) {
19902:                     unless ($updatedomdefs) {
19903:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
19904:                         $updatedomdefs = 1;
19905:                     }
19906:                     if ($wafproxy{$item}) {
19907:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
19908:                     } elsif (exists($domdefaults{'waf_'.$item})) {
19909:                         delete($domdefaults{'waf_'.$item});
19910:                     }
19911:                 }
19912:             }
19913:             if ($updatedomdefs) {
19914:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19915:                 if (ref($lastactref) eq 'HASH') {
19916:                     $lastactref->{'domdefaults'} = 1;
19917:                 }
19918:             }
19919:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
19920:                 my %updates = %expirecache;
19921:                 foreach my $key (keys(%expirecache)) {
19922:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
19923:                 }
19924:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
19925:                     my $cachetime = 24*60*60;
19926:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
19927:                         $updates{$key} = 1;
19928:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
19929:                                                       $cachetime);
19930:                     }
19931:                 }
19932:                 if (ref($lastactref) eq 'HASH') {
19933:                     $lastactref->{'proxyalias'} = \%updates;
19934:                 }
19935:             }
19936:             if ((exists($wafproxy{'saml'})) || (keys(%expiresaml))) {
19937:                 my %samlupdates = %expiresaml;
19938:                 foreach my $key (keys(%expiresaml)) {
19939:                     &Apache::lonnet::devalidate_cache_new('proxysaml',$key);
19940:                 }
19941:                 if (ref($wafproxy{'saml'}) eq 'HASH') {
19942:                     my $cachetime = 24*60*60;
19943:                     foreach my $key (keys(%{$wafproxy{'saml'}})) {
19944:                         $samlupdates{$key} = 1;
19945:                         &Apache::lonnet::do_cache_new('proxysaml',$key,$wafproxy{'saml'}{$key},
19946:                                                       $cachetime);
19947:                     }
19948:                 }
19949:                 if (ref($lastactref) eq 'HASH') {
19950:                     $lastactref->{'proxysaml'} = \%samlupdates;
19951:                 }
19952:             }
19953:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
19954:             foreach my $item ('alias','saml','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
19955:                 if ($changes{$item}) {
19956:                     if ($item eq 'alias') {
19957:                         my $numaliased = 0;
19958:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
19959:                             my $shown;
19960:                             if (keys(%{$wafproxy{'alias'}})) {
19961:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
19962:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
19963:                                                          &Apache::lonnet::hostname($server),
19964:                                                          $wafproxy{'alias'}{$server}).'</li>';
19965:                                     $numaliased ++;
19966:                                 }
19967:                                 if ($numaliased) {
19968:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
19969:                                                           '<ul>'.$shown.'</ul>').'</li>';
19970:                                 }
19971:                             }
19972:                         }
19973:                         unless ($numaliased) {
19974:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
19975:                         }
19976:                     } elsif ($item eq 'saml') {
19977:                         my $shown;
19978:                         if (ref($wafproxy{'saml'}) eq 'HASH') {
19979:                             if (keys(%{$wafproxy{'saml'}})) {
19980:                                 $shown = join(', ',sort(keys(%{$wafproxy{'saml'}})));
19981:                             }
19982:                         }
19983:                         if ($shown) {
19984:                             $output .= '<li>'.&mt('Alias used by SSO Auth for: [_1]',
19985:                                                   $shown).'</li>';
19986:                         } else {
19987:                             $output .= '<li>'.&mt('No alias used for SSO Auth').'</li>';
19988:                         }
19989:                     } else {
19990:                         if ($item eq 'remoteip') {
19991:                             my %ip_methods = &remoteip_methods();
19992:                             if ($wafproxy{$item} =~ /^[mh]$/) {
19993:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
19994:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
19995:                             } else {
19996:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
19997:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
19998:                                                '</li>';
19999:                                 } else {
20000:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
20001:                                 }
20002:                             }
20003:                         } elsif ($item eq 'ipheader') {
20004:                             if ($wafproxy{$item}) {
20005:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
20006:                                                       $wafproxy{$item}).'</li>';
20007:                             } else {
20008:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
20009:                             }
20010:                         } elsif ($item eq 'trusted') {
20011:                             if ($wafproxy{$item}) {
20012:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
20013:                                                       $wafproxy{$item}).'</li>';
20014:                             } else {
20015:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
20016:                             }
20017:                         } elsif ($item eq 'vpnint') {
20018:                             if ($wafproxy{$item}) {
20019:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
20020:                                                        $wafproxy{$item}).'</li>';
20021:                             } else {
20022:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
20023:                             }
20024:                         } elsif ($item eq 'vpnext') {
20025:                             if ($wafproxy{$item}) {
20026:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
20027:                                                        $wafproxy{$item}).'</li>';
20028:                             } else {
20029:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
20030:                             }
20031:                         } elsif ($item eq 'sslopt') {
20032:                             if ($wafproxy{$item}) {
20033:                                 $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>';
20034:                             } else {
20035:                                 $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>';
20036:                             }
20037:                         }
20038:                     }
20039:                 }
20040:             }
20041:             $output .= '</ul>';
20042:         } else {
20043:             $output = '<span class="LC_error">'.
20044:                       &mt('An error occurred: [_1]',$putresult).'</span>';
20045:         }
20046:     } elsif (keys(%canset)) {
20047:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
20048:     }
20049:     if (@warnings) {
20050:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
20051:                        join("\n",@warnings).'</ul>';
20052:     }
20053:     return $output;
20054: }
20055: 
20056: sub validate_ip_pattern {
20057:     my ($pattern) = @_;
20058:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
20059:         my ($start,$end) = ($1,$2);
20060:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
20061:             if (($start !~ m{/}) && ($end !~ m{/})) {
20062:                 return $start.'-'.$end;
20063:             }
20064:         }
20065:     } elsif ($pattern ne '') {
20066:         $pattern = &Net::CIDR::cidrvalidate($pattern);
20067:         if ($pattern ne '') {
20068:             return $pattern;
20069:         }
20070:     }
20071:     return;
20072: }
20073: 
20074: sub modify_usersessions {
20075:     my ($dom,$lastactref,%domconfig) = @_;
20076:     my @hostingtypes = ('version','excludedomain','includedomain');
20077:     my @offloadtypes = ('primary','default');
20078:     my %types = (
20079:                   remote => \@hostingtypes,
20080:                   hosted => \@hostingtypes,
20081:                   spares => \@offloadtypes,
20082:                 );
20083:     my @prefixes = ('remote','hosted','spares');
20084:     my @lcversions = &Apache::lonnet::all_loncaparevs();
20085:     my (%by_ip,%by_location,@intdoms);
20086:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
20087:     my @locations = sort(keys(%by_location));
20088:     my (%defaultshash,%changes);
20089:     foreach my $prefix (@prefixes) {
20090:         $defaultshash{'usersessions'}{$prefix} = {};
20091:     }
20092:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20093:     my $resulttext;
20094:     my %iphost = &Apache::lonnet::get_iphost();
20095:     foreach my $prefix (@prefixes) {
20096:         next if ($prefix eq 'spares');
20097:         foreach my $type (@{$types{$prefix}}) {
20098:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
20099:             if ($type eq 'version') {
20100:                 my $value = $env{'form.'.$prefix.'_'.$type};
20101:                 my $okvalue;
20102:                 if ($value ne '') {
20103:                     if (grep(/^\Q$value\E$/,@lcversions)) {
20104:                         $okvalue = $value;
20105:                     }
20106:                 }
20107:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
20108:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
20109:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
20110:                             if ($inuse == 0) {
20111:                                 $changes{$prefix}{$type} = 1;
20112:                             } else {
20113:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
20114:                                     $changes{$prefix}{$type} = 1;
20115:                                 }
20116:                                 if ($okvalue ne '') {
20117:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
20118:                                 } 
20119:                             }
20120:                         } else {
20121:                             if (($inuse == 1) && ($okvalue ne '')) {
20122:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
20123:                                 $changes{$prefix}{$type} = 1;
20124:                             }
20125:                         }
20126:                     } else {
20127:                         if (($inuse == 1) && ($okvalue ne '')) {
20128:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
20129:                             $changes{$prefix}{$type} = 1;
20130:                         }
20131:                     }
20132:                 } else {
20133:                     if (($inuse == 1) && ($okvalue ne '')) {
20134:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
20135:                         $changes{$prefix}{$type} = 1;
20136:                     }
20137:                 }
20138:             } else {
20139:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
20140:                 my @okvals;
20141:                 foreach my $val (@vals) {
20142:                     if ($val =~ /:/) {
20143:                         my @items = split(/:/,$val);
20144:                         foreach my $item (@items) {
20145:                             if (ref($by_location{$item}) eq 'ARRAY') {
20146:                                 push(@okvals,$item);
20147:                             }
20148:                         }
20149:                     } else {
20150:                         if (ref($by_location{$val}) eq 'ARRAY') {
20151:                             push(@okvals,$val);
20152:                         }
20153:                     }
20154:                 }
20155:                 @okvals = sort(@okvals);
20156:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
20157:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
20158:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
20159:                             if ($inuse == 0) {
20160:                                 $changes{$prefix}{$type} = 1; 
20161:                             } else {
20162:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
20163:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
20164:                                 if (@changed > 0) {
20165:                                     $changes{$prefix}{$type} = 1;
20166:                                 }
20167:                             }
20168:                         } else {
20169:                             if ($inuse == 1) {
20170:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
20171:                                 $changes{$prefix}{$type} = 1;
20172:                             }
20173:                         } 
20174:                     } else {
20175:                         if ($inuse == 1) {
20176:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
20177:                             $changes{$prefix}{$type} = 1;
20178:                         }
20179:                     }
20180:                 } else {
20181:                     if ($inuse == 1) {
20182:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
20183:                         $changes{$prefix}{$type} = 1;
20184:                     }
20185:                 }
20186:             }
20187:         }
20188:     }
20189: 
20190:     my @alldoms = &Apache::lonnet::all_domains();
20191:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20192:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
20193:     my $savespares;
20194: 
20195:     foreach my $lonhost (sort(keys(%servers))) {
20196:         my $serverhomeID =
20197:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
20198:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
20199:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
20200:         my %spareschg;
20201:         foreach my $type (@{$types{'spares'}}) {
20202:             my @okspares;
20203:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
20204:             foreach my $server (@checked) {
20205:                 if (&Apache::lonnet::hostname($server) ne '') {
20206:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
20207:                         unless (grep(/^\Q$server\E$/,@okspares)) {
20208:                             push(@okspares,$server);
20209:                         }
20210:                     }
20211:                 }
20212:             }
20213:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
20214:             my $newspare;
20215:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
20216:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
20217:                     $newspare = $new;
20218:                 }
20219:             }
20220:             my @spares;
20221:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
20222:                 @spares = sort(@okspares,$newspare);
20223:             } else {
20224:                 @spares = sort(@okspares);
20225:             }
20226:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
20227:             if (ref($spareid{$lonhost}) eq 'HASH') {
20228:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
20229:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
20230:                     if (@diffs > 0) {
20231:                         $spareschg{$type} = 1;
20232:                     }
20233:                 }
20234:             }
20235:         }
20236:         if (keys(%spareschg) > 0) {
20237:             $changes{'spares'}{$lonhost} = \%spareschg;
20238:         }
20239:     }
20240:     $defaultshash{'usersessions'}{'offloadnow'} = {};
20241:     $defaultshash{'usersessions'}{'offloadoth'} = {};
20242:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
20243:     my @okoffload;
20244:     if (@offloadnow) {
20245:         foreach my $server (@offloadnow) {
20246:             if (&Apache::lonnet::hostname($server) ne '') {
20247:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
20248:                     push(@okoffload,$server);
20249:                 }
20250:             }
20251:         }
20252:         if (@okoffload) {
20253:             foreach my $lonhost (@okoffload) {
20254:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
20255:             }
20256:         }
20257:     }
20258:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
20259:     my @okoffloadoth;
20260:     if (@offloadoth) {
20261:         foreach my $server (@offloadoth) {
20262:             if (&Apache::lonnet::hostname($server) ne '') {
20263:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
20264:                     push(@okoffloadoth,$server);
20265:                 }
20266:             }
20267:         }
20268:         if (@okoffloadoth) {
20269:             foreach my $lonhost (@okoffloadoth) {
20270:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
20271:             }
20272:         }
20273:     }
20274:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
20275:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
20276:             if (ref($changes{'spares'}) eq 'HASH') {
20277:                 if (keys(%{$changes{'spares'}}) > 0) {
20278:                     $savespares = 1;
20279:                 }
20280:             }
20281:         } else {
20282:             $savespares = 1;
20283:         }
20284:         foreach my $offload ('offloadnow','offloadoth') {
20285:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
20286:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
20287:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
20288:                         $changes{$offload} = 1;
20289:                         last;
20290:                     }
20291:                 }
20292:                 unless ($changes{$offload}) {
20293:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
20294:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
20295:                             $changes{$offload} = 1;
20296:                             last;
20297:                         }
20298:                     }
20299:                 }
20300:             } else {
20301:                 if (($offload eq 'offloadnow') && (@okoffload)) {
20302:                      $changes{'offloadnow'} = 1;
20303:                 }
20304:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
20305:                     $changes{'offloadoth'} = 1;
20306:                 }
20307:             }
20308:         }
20309:     } else {
20310:         if (@okoffload) {
20311:             $changes{'offloadnow'} = 1;
20312:         }
20313:         if (@okoffloadoth) {
20314:             $changes{'offloadoth'} = 1;
20315:         }
20316:     }
20317:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
20318:     if ((keys(%changes) > 0) || ($savespares)) {
20319:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20320:                                                  $dom);
20321:         if ($putresult eq 'ok') {
20322:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
20323:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
20324:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
20325:                 }
20326:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
20327:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
20328:                 }
20329:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
20330:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
20331:                 }
20332:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
20333:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
20334:                 }
20335:             }
20336:             my $cachetime = 24*60*60;
20337:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20338:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
20339:             if (ref($lastactref) eq 'HASH') {
20340:                 $lastactref->{'domdefaults'} = 1;
20341:                 $lastactref->{'usersessions'} = 1;
20342:             }
20343:             if (keys(%changes) > 0) {
20344:                 my %lt = &usersession_titles();
20345:                 $resulttext = &mt('Changes made:').'<ul>';
20346:                 foreach my $prefix (@prefixes) {
20347:                     if (ref($changes{$prefix}) eq 'HASH') {
20348:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
20349:                         if ($prefix eq 'spares') {
20350:                             if (ref($changes{$prefix}) eq 'HASH') {
20351:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
20352:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
20353:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
20354:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
20355:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
20356:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
20357:                                         foreach my $type (@{$types{$prefix}}) {
20358:                                             if ($changes{$prefix}{$lonhost}{$type}) {
20359:                                                 my $offloadto = &mt('None');
20360:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
20361:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
20362:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
20363:                                                     }
20364:                                                 }
20365:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
20366:                                             }
20367:                                         }
20368:                                     }
20369:                                     $resulttext .= '</li>';
20370:                                 }
20371:                             }
20372:                         } else {
20373:                             foreach my $type (@{$types{$prefix}}) {
20374:                                 if (defined($changes{$prefix}{$type})) {
20375:                                     my $newvalue;
20376:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
20377:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
20378:                                             if ($type eq 'version') {
20379:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
20380:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
20381:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
20382:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
20383:                                                 }
20384:                                             }
20385:                                         }
20386:                                     }
20387:                                     if ($newvalue eq '') {
20388:                                         if ($type eq 'version') {
20389:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
20390:                                         } else {
20391:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
20392:                                         }
20393:                                     } else {
20394:                                         if ($type eq 'version') {
20395:                                             $newvalue .= ' '.&mt('(or later)');
20396:                                         }
20397:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
20398:                                     }
20399:                                 }
20400:                             }
20401:                         }
20402:                         $resulttext .= '</ul>';
20403:                     }
20404:                 }
20405:                 if ($changes{'offloadnow'}) {
20406:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
20407:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
20408:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
20409:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
20410:                                 $resulttext .= '<li>'.$lonhost.'</li>';
20411:                             }
20412:                             $resulttext .= '</ul>';
20413:                         } else {
20414:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
20415:                         }
20416:                     } else {
20417:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
20418:                     }
20419:                 }
20420:                 if ($changes{'offloadoth'}) {
20421:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
20422:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
20423:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
20424:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
20425:                                 $resulttext .= '<li>'.$lonhost.'</li>';
20426:                             }
20427:                             $resulttext .= '</ul>';
20428:                         } else {
20429:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
20430:                         }
20431:                     } else {
20432:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
20433:                     }
20434:                 }
20435:                 $resulttext .= '</ul>';
20436:             } else {
20437:                 $resulttext = $nochgmsg;
20438:             }
20439:         } else {
20440:             $resulttext = '<span class="LC_error">'.
20441:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20442:         }
20443:     } else {
20444:         $resulttext = $nochgmsg;
20445:     }
20446:     return $resulttext;
20447: }
20448: 
20449: sub modify_loadbalancing {
20450:     my ($dom,%domconfig) = @_;
20451:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
20452:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
20453:     my ($othertitle,$usertypes,$types) =
20454:         &Apache::loncommon::sorted_inst_types($dom);
20455:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20456:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
20457:     my @sparestypes = ('primary','default');
20458:     my %typetitles = &sparestype_titles();
20459:     my $resulttext;
20460:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
20461:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
20462:         %existing = %{$domconfig{'loadbalancing'}};
20463:     }
20464:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
20465:                               \%currtargets,\%currrules,\%currcookies);
20466:     my ($saveloadbalancing,%defaultshash,%changes);
20467:     my ($alltypes,$othertypes,$titles) =
20468:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
20469:     my %ruletitles = &offloadtype_text();
20470:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
20471:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
20472:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
20473:         if ($balancer eq '') {
20474:             next;
20475:         }
20476:         if (!exists($servers{$balancer})) {
20477:             if (exists($currbalancer{$balancer})) {
20478:                 push(@{$changes{'delete'}},$balancer);
20479:             }
20480:             next;
20481:         }
20482:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
20483:             push(@{$changes{'delete'}},$balancer);
20484:             next;
20485:         }
20486:         if (!exists($currbalancer{$balancer})) {
20487:             push(@{$changes{'add'}},$balancer);
20488:         }
20489:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
20490:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
20491:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
20492:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
20493:             $saveloadbalancing = 1;
20494:         }
20495:         foreach my $sparetype (@sparestypes) {
20496:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
20497:             my @offloadto;
20498:             foreach my $target (@targets) {
20499:                 if (($servers{$target}) && ($target ne $balancer)) {
20500:                     if ($sparetype eq 'default') {
20501:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
20502:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
20503:                         }
20504:                     }
20505:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
20506:                         push(@offloadto,$target);
20507:                     }
20508:                 }
20509:             }
20510:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
20511:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
20512:                     push(@offloadto,$balancer);
20513:                 }
20514:             }
20515:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
20516:         }
20517:         if ($env{'form.loadbalancing_cookie_'.$i}) {
20518:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
20519:             if (exists($currbalancer{$balancer})) {
20520:                 unless ($currcookies{$balancer}) {
20521:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
20522:                 }
20523:             }
20524:         } elsif (exists($currbalancer{$balancer})) {
20525:             if ($currcookies{$balancer}) {
20526:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
20527:             }
20528:         }
20529:         if (ref($currtargets{$balancer}) eq 'HASH') {
20530:             foreach my $sparetype (@sparestypes) {
20531:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
20532:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
20533:                     if (@targetdiffs > 0) {
20534:                         $changes{'curr'}{$balancer}{'targets'} = 1;
20535:                     }
20536:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20537:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20538:                         $changes{'curr'}{$balancer}{'targets'} = 1;
20539:                     }
20540:                 }
20541:             }
20542:         } else {
20543:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
20544:                 foreach my $sparetype (@sparestypes) {
20545:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20546:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20547:                             $changes{'curr'}{$balancer}{'targets'} = 1;
20548:                         }
20549:                     }
20550:                 }
20551:             }
20552:         }
20553:         my $ishomedom;
20554:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
20555:             $ishomedom = 1;
20556:         }
20557:         if (ref($alltypes) eq 'ARRAY') {
20558:             foreach my $type (@{$alltypes}) {
20559:                 my $rule;
20560:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
20561:                          (!$ishomedom)) {
20562:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
20563:                 }
20564:                 if ($rule eq 'specific') {
20565:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
20566:                     if (exists($servers{$specifiedhost})) {
20567:                         $rule = $specifiedhost;
20568:                     }
20569:                 }
20570:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
20571:                 if (ref($currrules{$balancer}) eq 'HASH') {
20572:                     if ($rule ne $currrules{$balancer}{$type}) {
20573:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
20574:                     }
20575:                 } elsif ($rule ne '') {
20576:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
20577:                 }
20578:             }
20579:         }
20580:     }
20581:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
20582:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
20583:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
20584:             $defaultshash{'loadbalancing'} = {};
20585:         }
20586:         my $putresult = &Apache::lonnet::put_dom('configuration',
20587:                                                  \%defaultshash,$dom);
20588:         if ($putresult eq 'ok') {
20589:             if (keys(%changes) > 0) {
20590:                 my %toupdate;
20591:                 if (ref($changes{'delete'}) eq 'ARRAY') {
20592:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
20593:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
20594:                         $toupdate{$balancer} = 1;
20595:                     }
20596:                 }
20597:                 if (ref($changes{'add'}) eq 'ARRAY') {
20598:                     foreach my $balancer (sort(@{$changes{'add'}})) {
20599:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
20600:                         $toupdate{$balancer} = 1;
20601:                     }
20602:                 }
20603:                 if (ref($changes{'curr'}) eq 'HASH') {
20604:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
20605:                         $toupdate{$balancer} = 1;
20606:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
20607:                             if ($changes{'curr'}{$balancer}{'targets'}) {
20608:                                 my %offloadstr;
20609:                                 foreach my $sparetype (@sparestypes) {
20610:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20611:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20612:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
20613:                                         }
20614:                                     }
20615:                                 }
20616:                                 if (keys(%offloadstr) == 0) {
20617:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
20618:                                 } else {
20619:                                     my $showoffload;
20620:                                     foreach my $sparetype (@sparestypes) {
20621:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
20622:                                         if (defined($offloadstr{$sparetype})) {
20623:                                             $showoffload .= $offloadstr{$sparetype};
20624:                                         } else {
20625:                                             $showoffload .= &mt('None');
20626:                                         }
20627:                                         $showoffload .= ('&nbsp;'x3);
20628:                                     }
20629:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
20630:                                 }
20631:                             }
20632:                         }
20633:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
20634:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
20635:                                 foreach my $type (@{$alltypes}) {
20636:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
20637:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
20638:                                         my $balancetext;
20639:                                         if ($rule eq '') {
20640:                                             $balancetext =  $ruletitles{'default'};
20641:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
20642:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
20643:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
20644:                                                 foreach my $sparetype (@sparestypes) {
20645:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20646:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
20647:                                                     }
20648:                                                 }
20649:                                                 foreach my $item (@{$alltypes}) {
20650:                                                     next if ($item =~  /^_LC_ipchange/);
20651:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
20652:                                                     if ($hasrule eq 'homeserver') {
20653:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
20654:                                                     } else {
20655:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
20656:                                                             if ($servers{$hasrule}) {
20657:                                                                 $toupdate{$hasrule} = 1;
20658:                                                             }
20659:                                                         }
20660:                                                     }
20661:                                                 }
20662:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
20663:                                                     $balancetext =  $ruletitles{$rule};
20664:                                                 } else {
20665:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
20666:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
20667:                                                     if ($receiver) {
20668:                                                         $toupdate{$receiver};
20669:                                                     }
20670:                                                 }
20671:                                             } else {
20672:                                                 $balancetext =  $ruletitles{$rule};
20673:                                             }
20674:                                         } else {
20675:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
20676:                                         }
20677:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
20678:                                     }
20679:                                 }
20680:                             }
20681:                         }
20682:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
20683:                             if ($currcookies{$balancer}) {
20684:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use disabled',
20685:                                                           $balancer).'</li>';
20686:                             } else {
20687:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
20688:                                                           $balancer).'</li>';
20689:                             }
20690:                         }
20691:                     }
20692:                 }
20693:                 if (keys(%toupdate)) {
20694:                     my %thismachine;
20695:                     my $updatedhere;
20696:                     my $cachetime = 60*60*24;
20697:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
20698:                     foreach my $lonhost (keys(%toupdate)) {
20699:                         if ($thismachine{$lonhost}) {
20700:                             unless ($updatedhere) {
20701:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
20702:                                                               $defaultshash{'loadbalancing'},
20703:                                                               $cachetime);
20704:                                 $updatedhere = 1;
20705:                             }
20706:                         } else {
20707:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
20708:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
20709:                         }
20710:                     }
20711:                 }
20712:                 if ($resulttext ne '') {
20713:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
20714:                 } else {
20715:                     $resulttext = $nochgmsg;
20716:                 }
20717:             } else {
20718:                 $resulttext = $nochgmsg;
20719:             }
20720:         } else {
20721:             $resulttext = '<span class="LC_error">'.
20722:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20723:         }
20724:     } else {
20725:         $resulttext = $nochgmsg;
20726:     }
20727:     return $resulttext;
20728: }
20729: 
20730: sub recurse_check {
20731:     my ($chkcats,$categories,$depth,$name) = @_;
20732:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
20733:         my $chg = 0;
20734:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
20735:             my $category = $chkcats->[$depth]{$name}[$j];
20736:             my $item;
20737:             if ($category eq '') {
20738:                 $chg ++;
20739:             } else {
20740:                 my $deeper = $depth + 1;
20741:                 $item = &escape($category).':'.&escape($name).':'.$depth;
20742:                 if ($chg) {
20743:                     $categories->{$item} -= $chg;
20744:                 }
20745:                 &recurse_check($chkcats,$categories,$deeper,$category);
20746:                 $deeper --;
20747:             }
20748:         }
20749:     }
20750:     return;
20751: }
20752: 
20753: sub recurse_cat_deletes {
20754:     my ($item,$coursecategories,$deletions) = @_;
20755:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
20756:     my $subdepth = $depth + 1;
20757:     if (ref($coursecategories) eq 'HASH') {
20758:         foreach my $subitem (keys(%{$coursecategories})) {
20759:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
20760:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
20761:                 delete($coursecategories->{$subitem});
20762:                 $deletions->{$subitem} = 1;
20763:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
20764:             }
20765:         }
20766:     }
20767:     return;
20768: }
20769: 
20770: sub active_dc_picker {
20771:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
20772:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
20773:     my @domcoord = keys(%domcoords);
20774:     if (keys(%currhash)) {
20775:         foreach my $dc (keys(%currhash)) {
20776:             unless (exists($domcoords{$dc})) {
20777:                 push(@domcoord,$dc);
20778:             }
20779:         }
20780:     }
20781:     @domcoord = sort(@domcoord);
20782:     my $numdcs = scalar(@domcoord);
20783:     my $rows = 0;
20784:     my $table;
20785:     if ($numdcs > 1) {
20786:         $table = '<table>';
20787:         for (my $i=0; $i<@domcoord; $i++) {
20788:             my $rem = $i%($numinrow);
20789:             if ($rem == 0) {
20790:                 if ($i > 0) {
20791:                     $table .= '</tr>';
20792:                 }
20793:                 $table .= '<tr>';
20794:                 $rows ++;
20795:             }
20796:             my $check = '';
20797:             if ($inputtype eq 'radio') {
20798:                 if (keys(%currhash) == 0) {
20799:                     if (!$i) {
20800:                         $check = ' checked="checked"';
20801:                     }
20802:                 } elsif (exists($currhash{$domcoord[$i]})) {
20803:                     $check = ' checked="checked"';
20804:                 }
20805:             } else {
20806:                 if (exists($currhash{$domcoord[$i]})) {
20807:                     $check = ' checked="checked"';
20808:                 }
20809:             }
20810:             if ($i == @domcoord - 1) {
20811:                 my $colsleft = $numinrow - $rem;
20812:                 if ($colsleft > 1) {
20813:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
20814:                 } else {
20815:                     $table .= '<td class="LC_left_item">';
20816:                 }
20817:             } else {
20818:                 $table .= '<td class="LC_left_item">';
20819:             }
20820:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
20821:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
20822:             $table .= '<span class="LC_nobreak"><label>'.
20823:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
20824:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
20825:             if ($user ne $dcname.':'.$dcdom) {
20826:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
20827:             }
20828:             $table .= '</label></span></td>';
20829:         }
20830:         $table .= '</tr></table>';
20831:     } elsif ($numdcs == 1) {
20832:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
20833:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
20834:         if ($inputtype eq 'radio') {
20835:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
20836:             if ($user ne $dcname.':'.$dcdom) {
20837:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
20838:             }
20839:         } else {
20840:             my $check;
20841:             if (exists($currhash{$domcoord[0]})) {
20842:                 $check = ' checked="checked"';
20843:             }
20844:             $table = '<span class="LC_nobreak"><label>'.
20845:                      '<input type="checkbox" name="'.$name.'" '.
20846:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
20847:             if ($user ne $dcname.':'.$dcdom) {
20848:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
20849:             }
20850:             $table .= '</label></span>';
20851:             $rows ++;
20852:         }
20853:     }
20854:     return ($numdcs,$table,$rows);
20855: }
20856: 
20857: sub usersession_titles {
20858:     return &Apache::lonlocal::texthash(
20859:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
20860:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
20861:                spares => 'Servers offloaded to, when busy',
20862:                version => 'LON-CAPA version requirement',
20863:                excludedomain => 'Allow all, but exclude specific domains',
20864:                includedomain => 'Deny all, but include specific domains',
20865:                primary => 'Primary (checked first)',
20866:                default => 'Default',
20867:            );
20868: }
20869: 
20870: sub id_for_thisdom {
20871:     my (%servers) = @_;
20872:     my %altids;
20873:     foreach my $server (keys(%servers)) {
20874:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
20875:         if ($serverhome ne $server) {
20876:             $altids{$serverhome} = $server;
20877:         }
20878:     }
20879:     return %altids;
20880: }
20881: 
20882: sub count_servers {
20883:     my ($currbalancer,%servers) = @_;
20884:     my (@spares,$numspares);
20885:     foreach my $lonhost (sort(keys(%servers))) {
20886:         next if ($currbalancer eq $lonhost);
20887:         push(@spares,$lonhost);
20888:     }
20889:     if ($currbalancer) {
20890:         $numspares = scalar(@spares);
20891:     } else {
20892:         $numspares = scalar(@spares) - 1;
20893:     }
20894:     return ($numspares,@spares);
20895: }
20896: 
20897: sub lonbalance_targets_js {
20898:     my ($dom,$types,$servers,$settings) = @_;
20899:     my $select = &mt('Select');
20900:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
20901:     if (ref($servers) eq 'HASH') {
20902:         $alltargets = join("','",sort(keys(%{$servers})));
20903:         my @homedoms;
20904:         foreach my $server (sort(keys(%{$servers}))) {
20905:             if (&Apache::lonnet::host_domain($server) eq $dom) {
20906:                 push(@homedoms,'1');
20907:             } else {
20908:                 push(@homedoms,'0');
20909:             }
20910:         }
20911:         $allishome = join("','",@homedoms);
20912:     }
20913:     if (ref($types) eq 'ARRAY') {
20914:         if (@{$types} > 0) {
20915:             @alltypes = @{$types};
20916:         }
20917:     }
20918:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
20919:     $allinsttypes = join("','",@alltypes);
20920:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
20921:     if (ref($settings) eq 'HASH') {
20922:         %existing = %{$settings};
20923:     }
20924:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
20925:                               \%currtargets,\%currrules,\%currcookies);
20926:     my $balancers = join("','",sort(keys(%currbalancer)));
20927:     return <<"END";
20928: 
20929: <script type="text/javascript">
20930: // <![CDATA[
20931: 
20932: currBalancers = new Array('$balancers');
20933: 
20934: function toggleTargets(balnum) {
20935:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
20936:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
20937:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
20938:     var prevbalancer = prevhostitem.value;
20939:     var baltotal = document.getElementById('loadbalancing_total').value;
20940:     prevhostitem.value = balancer;
20941:     if (prevbalancer != '') {
20942:         var prevIdx = currBalancers.indexOf(prevbalancer);
20943:         if (prevIdx != -1) {
20944:             currBalancers.splice(prevIdx,1);
20945:         }
20946:     }
20947:     if (balancer == '') {
20948:         hideSpares(balnum);
20949:     } else {
20950:         var currIdx = currBalancers.indexOf(balancer);
20951:         if (currIdx == -1) {
20952:             currBalancers.push(balancer);
20953:         }
20954:         var homedoms = new Array('$allishome');
20955:         var ishomedom = homedoms[lonhostitem.selectedIndex];
20956:         showSpares(balancer,ishomedom,balnum);
20957:     }
20958:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
20959:     return;
20960: }
20961: 
20962: function showSpares(balancer,ishomedom,balnum) {
20963:     var alltargets = new Array('$alltargets');
20964:     var insttypes = new Array('$allinsttypes');
20965:     var offloadtypes = new Array('primary','default');
20966: 
20967:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
20968:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
20969:  
20970:     for (var i=0; i<offloadtypes.length; i++) {
20971:         var count = 0;
20972:         for (var j=0; j<alltargets.length; j++) {
20973:             if (alltargets[j] != balancer) {
20974:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
20975:                 item.value = alltargets[j];
20976:                 item.style.textAlign='left';
20977:                 item.style.textFace='normal';
20978:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
20979:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
20980:                     item.disabled = '';
20981:                 } else {
20982:                     item.disabled = 'disabled';
20983:                     item.checked = false;
20984:                 }
20985:                 count ++;
20986:             }
20987:         }
20988:     }
20989:     for (var k=0; k<insttypes.length; k++) {
20990:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
20991:             if (ishomedom == 1) {
20992:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
20993:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
20994:             } else {
20995:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
20996:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
20997:             }
20998:         } else {
20999:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
21000:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
21001:         }
21002:         if ((insttypes[k] != '_LC_external') && 
21003:             ((insttypes[k] != '_LC_internetdom') ||
21004:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
21005:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
21006:             item.options.length = 0;
21007:             item.options[0] = new Option("","",true,true);
21008:             var idx = 0;
21009:             for (var m=0; m<alltargets.length; m++) {
21010:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
21011:                     idx ++;
21012:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
21013:                 }
21014:             }
21015:         }
21016:     }
21017:     return;
21018: }
21019: 
21020: function hideSpares(balnum) {
21021:     var alltargets = new Array('$alltargets');
21022:     var insttypes = new Array('$allinsttypes');
21023:     var offloadtypes = new Array('primary','default');
21024: 
21025:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
21026:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
21027: 
21028:     var total = alltargets.length - 1;
21029:     for (var i=0; i<offloadtypes; i++) {
21030:         for (var j=0; j<total; j++) {
21031:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
21032:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
21033:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
21034:         }
21035:     }
21036:     for (var k=0; k<insttypes.length; k++) {
21037:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
21038:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
21039:         if (insttypes[k] != '_LC_external') {
21040:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
21041:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
21042:         }
21043:     }
21044:     return;
21045: }
21046: 
21047: function checkOffloads(item,balnum,type) {
21048:     var alltargets = new Array('$alltargets');
21049:     var offloadtypes = new Array('primary','default');
21050:     if (item.checked) {
21051:         var total = alltargets.length - 1;
21052:         var other;
21053:         if (type == offloadtypes[0]) {
21054:             other = offloadtypes[1];
21055:         } else {
21056:             other = offloadtypes[0];
21057:         }
21058:         for (var i=0; i<total; i++) {
21059:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
21060:             if (server == item.value) {
21061:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
21062:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
21063:                 }
21064:             }
21065:         }
21066:     }
21067:     return;
21068: }
21069: 
21070: function singleServerToggle(balnum,type) {
21071:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
21072:     if (offloadtoSelIdx == 0) {
21073:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
21074:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
21075: 
21076:     } else {
21077:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
21078:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
21079:     }
21080:     return;
21081: }
21082: 
21083: function balanceruleChange(formname,balnum,type) {
21084:     if (type == '_LC_external') {
21085:         return;
21086:     }
21087:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
21088:     for (var i=0; i<typesRules.length; i++) {
21089:         if (formname.elements[typesRules[i]].checked) {
21090:             if (formname.elements[typesRules[i]].value != 'specific') {
21091:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
21092:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
21093:             } else {
21094:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
21095:             }
21096:         }
21097:     }
21098:     return;
21099: }
21100: 
21101: function balancerDeleteChange(balnum) {
21102:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
21103:     var baltotal = document.getElementById('loadbalancing_total').value;
21104:     var addtarget;
21105:     var removetarget;
21106:     var action = 'delete';
21107:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
21108:         var lonhost = hostitem.value;
21109:         var currIdx = currBalancers.indexOf(lonhost);
21110:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
21111:             if (currIdx != -1) {
21112:                 currBalancers.splice(currIdx,1);
21113:             }
21114:             addtarget = lonhost;
21115:         } else {
21116:             if (currIdx == -1) {
21117:                 currBalancers.push(lonhost);
21118:             }
21119:             removetarget = lonhost;
21120:             action = 'undelete';
21121:         }
21122:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
21123:     }
21124:     return;
21125: }
21126: 
21127: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
21128:     if (baltotal > 1) {
21129:         var offloadtypes = new Array('primary','default');
21130:         var alltargets = new Array('$alltargets');
21131:         var insttypes = new Array('$allinsttypes');
21132:         for (var i=0; i<baltotal; i++) {
21133:             if (i != balnum) {
21134:                 for (var j=0; j<offloadtypes.length; j++) {
21135:                     var total = alltargets.length - 1;
21136:                     for (var k=0; k<total; k++) {
21137:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
21138:                         var server = serveritem.value;
21139:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
21140:                             if (server == addtarget) {
21141:                                 serveritem.disabled = '';
21142:                             }
21143:                         }
21144:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
21145:                             if (server == removetarget) {
21146:                                 serveritem.disabled = 'disabled';
21147:                                 serveritem.checked = false;
21148:                             }
21149:                         }
21150:                     }
21151:                 }
21152:                 for (var j=0; j<insttypes.length; j++) {
21153:                     if (insttypes[j] != '_LC_external') {
21154:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
21155:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
21156:                             var currSel = singleserver.selectedIndex;
21157:                             var currVal = singleserver.options[currSel].value;
21158:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
21159:                                 var numoptions = singleserver.options.length;
21160:                                 var needsnew = 1;
21161:                                 for (var k=0; k<numoptions; k++) {
21162:                                     if (singleserver.options[k] == addtarget) {
21163:                                         needsnew = 0;
21164:                                         break;
21165:                                     }
21166:                                 }
21167:                                 if (needsnew == 1) {
21168:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
21169:                                 }
21170:                             }
21171:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
21172:                                 singleserver.options.length = 0;
21173:                                 if ((currVal) && (currVal != removetarget)) {
21174:                                     singleserver.options[0] = new Option("","",false,false);
21175:                                 } else {
21176:                                     singleserver.options[0] = new Option("","",true,true);
21177:                                 }
21178:                                 var idx = 0;
21179:                                 for (var m=0; m<alltargets.length; m++) {
21180:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
21181:                                         idx ++;
21182:                                         if (currVal == alltargets[m]) {
21183:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
21184:                                         } else {
21185:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
21186:                                         }
21187:                                     }
21188:                                 }
21189:                             }
21190:                         }
21191:                     }
21192:                 }
21193:             }
21194:         }
21195:     }
21196:     return;
21197: }
21198: 
21199: // ]]>
21200: </script>
21201: 
21202: END
21203: }
21204: 
21205: sub new_spares_js {
21206:     my @sparestypes = ('primary','default');
21207:     my $types = join("','",@sparestypes);
21208:     my $select = &mt('Select');
21209:     return <<"END";
21210: 
21211: <script type="text/javascript">
21212: // <![CDATA[
21213: 
21214: function updateNewSpares(formname,lonhost) {
21215:     var types = new Array('$types');
21216:     var include = new Array();
21217:     var exclude = new Array();
21218:     for (var i=0; i<types.length; i++) {
21219:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
21220:         for (var j=0; j<spareboxes.length; j++) {
21221:             if (formname.elements[spareboxes[j]].checked) {
21222:                 exclude.push(formname.elements[spareboxes[j]].value);
21223:             } else {
21224:                 include.push(formname.elements[spareboxes[j]].value);
21225:             }
21226:         }
21227:     }
21228:     for (var i=0; i<types.length; i++) {
21229:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
21230:         var selIdx = newSpare.selectedIndex;
21231:         var currnew = newSpare.options[selIdx].value;
21232:         var okSpares = new Array();
21233:         for (var j=0; j<newSpare.options.length; j++) {
21234:             var possible = newSpare.options[j].value;
21235:             if (possible != '') {
21236:                 if (exclude.indexOf(possible) == -1) {
21237:                     okSpares.push(possible);
21238:                 } else {
21239:                     if (currnew == possible) {
21240:                         selIdx = 0;
21241:                     }
21242:                 }
21243:             }
21244:         }
21245:         for (var k=0; k<include.length; k++) {
21246:             if (okSpares.indexOf(include[k]) == -1) {
21247:                 okSpares.push(include[k]);
21248:             }
21249:         }
21250:         okSpares.sort();
21251:         newSpare.options.length = 0;
21252:         if (selIdx == 0) {
21253:             newSpare.options[0] = new Option("$select","",true,true);
21254:         } else {
21255:             newSpare.options[0] = new Option("$select","",false,false);
21256:         }
21257:         for (var m=0; m<okSpares.length; m++) {
21258:             var idx = m+1;
21259:             var selThis = 0;
21260:             if (selIdx != 0) {
21261:                 if (okSpares[m] == currnew) {
21262:                     selThis = 1;
21263:                 }
21264:             }
21265:             if (selThis == 1) {
21266:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
21267:             } else {
21268:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
21269:             }
21270:         }
21271:     }
21272:     return;
21273: }
21274: 
21275: function checkNewSpares(lonhost,type) {
21276:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
21277:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
21278:     if (chosen != '') {
21279:         var othertype;
21280:         var othernewSpare;
21281:         if (type == 'primary') {
21282:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
21283:         }
21284:         if (type == 'default') {
21285:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
21286:         }
21287:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
21288:             othernewSpare.selectedIndex = 0;
21289:         }
21290:     }
21291:     return;
21292: }
21293: 
21294: // ]]>
21295: </script>
21296: 
21297: END
21298: 
21299: }
21300: 
21301: sub common_domprefs_js {
21302:     return <<"END";
21303: 
21304: <script type="text/javascript">
21305: // <![CDATA[
21306: 
21307: function getIndicesByName(formname,item) {
21308:     var group = new Array();
21309:     for (var i=0;i<formname.elements.length;i++) {
21310:         if (formname.elements[i].name == item) {
21311:             group.push(formname.elements[i].id);
21312:         }
21313:     }
21314:     return group;
21315: }
21316: 
21317: // ]]>
21318: </script>
21319: 
21320: END
21321: 
21322: }
21323: 
21324: sub recaptcha_js {
21325:     my %lt = &captcha_phrases();
21326:     return <<"END";
21327: 
21328: <script type="text/javascript">
21329: // <![CDATA[
21330: 
21331: function updateCaptcha(caller,context) {
21332:     var privitem;
21333:     var pubitem;
21334:     var privtext;
21335:     var pubtext;
21336:     var versionitem;
21337:     var versiontext;
21338:     if (document.getElementById(context+'_recaptchapub')) {
21339:         pubitem = document.getElementById(context+'_recaptchapub');
21340:     } else {
21341:         return;
21342:     }
21343:     if (document.getElementById(context+'_recaptchapriv')) {
21344:         privitem = document.getElementById(context+'_recaptchapriv');
21345:     } else {
21346:         return;
21347:     }
21348:     if (document.getElementById(context+'_recaptchapubtxt')) {
21349:         pubtext = document.getElementById(context+'_recaptchapubtxt');
21350:     } else {
21351:         return;
21352:     }
21353:     if (document.getElementById(context+'_recaptchaprivtxt')) {
21354:         privtext = document.getElementById(context+'_recaptchaprivtxt');
21355:     } else {
21356:         return;
21357:     }
21358:     if (document.getElementById(context+'_recaptchaversion')) {
21359:         versionitem = document.getElementById(context+'_recaptchaversion');
21360:     } else {
21361:         return;
21362:     }
21363:     if (document.getElementById(context+'_recaptchavertxt')) {
21364:         versiontext = document.getElementById(context+'_recaptchavertxt');
21365:     } else {
21366:         return;
21367:     }
21368:     if (caller.checked) {
21369:         if (caller.value == 'recaptcha') {
21370:             pubitem.type = 'text';
21371:             privitem.type = 'text';
21372:             pubitem.size = '40';
21373:             privitem.size = '40';
21374:             pubtext.innerHTML = "$lt{'pub'}";
21375:             privtext.innerHTML = "$lt{'priv'}";
21376:             versionitem.type = 'text';
21377:             versionitem.size = '3';
21378:             versiontext.innerHTML = "$lt{'ver'}";
21379:         } else {
21380:             pubitem.type = 'hidden';
21381:             privitem.type = 'hidden';
21382:             versionitem.type = 'hidden';
21383:             pubtext.innerHTML = '';
21384:             privtext.innerHTML = '';
21385:             versiontext.innerHTML = '';
21386:         }
21387:     }
21388:     return;
21389: }
21390: 
21391: // ]]>
21392: </script>
21393: 
21394: END
21395: 
21396: }
21397: 
21398: sub toggle_display_js {
21399:     return <<"END";
21400: 
21401: <script type="text/javascript">
21402: // <![CDATA[
21403: 
21404: function toggleDisplay(domForm,caller) {
21405:     if (document.getElementById(caller)) {
21406:         var divitem = document.getElementById(caller);
21407:         var optionsElement = domForm.coursecredits;
21408:         var checkval = 1;
21409:         var dispval = 'block';
21410:         var selfcreateRegExp = /^cancreate_emailverified/;
21411:         if (caller == 'emailoptions') {
21412:             optionsElement = domForm.cancreate_email;
21413:         }
21414:         if (caller == 'studentsubmission') {
21415:             optionsElement = domForm.postsubmit;
21416:         }
21417:         if (caller == 'cloneinstcode') {
21418:             optionsElement = domForm.canclone;
21419:             checkval = 'instcode';
21420:         }
21421:         if (selfcreateRegExp.test(caller)) {
21422:             optionsElement = domForm.elements[caller];
21423:             checkval = 'other';
21424:             dispval = 'inline'
21425:         }
21426:         if (optionsElement.length) {
21427:             var currval;
21428:             for (var i=0; i<optionsElement.length; i++) {
21429:                 if (optionsElement[i].checked) {
21430:                    currval = optionsElement[i].value;
21431:                 }
21432:             }
21433:             if (currval == checkval) {
21434:                 divitem.style.display = dispval;
21435:             } else {
21436:                 divitem.style.display = 'none';
21437:             }
21438:         }
21439:     }
21440:     return;
21441: }
21442: 
21443: // ]]>
21444: </script>
21445: 
21446: END
21447: 
21448: }
21449: 
21450: sub captcha_phrases {
21451:     return &Apache::lonlocal::texthash (
21452:                  priv => 'Private key',
21453:                  pub  => 'Public key',
21454:                  original  => 'original (CAPTCHA)',
21455:                  recaptcha => 'successor (ReCAPTCHA)',
21456:                  notused   => 'unused',
21457:                  ver => 'ReCAPTCHA version (1 or 2)',
21458:     );
21459: }
21460: 
21461: sub devalidate_remote_domconfs {
21462:     my ($dom,$cachekeys) = @_;
21463:     return unless (ref($cachekeys) eq 'HASH');
21464:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21465:     my %thismachine;
21466:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
21467:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
21468:                       'directorysrch','passwdconf','cats','proxyalias','proxysaml',
21469:                       'ipaccess');
21470:     my %cache_by_lonhost;
21471:     if (exists($cachekeys->{'samllanding'})) {
21472:         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
21473:             my %landing = %{$cachekeys->{'samllanding'}};
21474:             my %domservers = &Apache::lonnet::get_servers($dom);
21475:             if (keys(%domservers)) {
21476:                 foreach my $server (keys(%domservers)) {
21477:                     my @cached;
21478:                     next if ($thismachine{$server});
21479:                     if ($landing{$server}) {
21480:                         push(@cached,&escape('samllanding').':'.&escape($server));
21481:                     }
21482:                     if (@cached) {
21483:                         $cache_by_lonhost{$server} = \@cached;
21484:                     }
21485:                 }
21486:             }
21487:         }
21488:     }
21489:     if (keys(%servers)) {
21490:         foreach my $server (keys(%servers)) {
21491:             next if ($thismachine{$server});
21492:             my @cached;
21493:             foreach my $name (@posscached) {
21494:                 if ($cachekeys->{$name}) {
21495:                     if (($name eq 'proxyalias') || ($name eq 'proxysaml')) {
21496:                         if (ref($cachekeys->{$name}) eq 'HASH') {
21497:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
21498:                                 push(@cached,&escape($name).':'.&escape($key));
21499:                             }
21500:                         }
21501:                     } else {
21502:                         push(@cached,&escape($name).':'.&escape($dom));
21503:                     }
21504:                 }
21505:             }
21506:             if ((exists($cache_by_lonhost{$server})) &&
21507:                 (ref($cache_by_lonhost{$server}) eq 'ARRAY')) {
21508:                 push(@cached,@{$cache_by_lonhost{$server}});
21509:             }
21510:             if (@cached) {
21511:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
21512:             }
21513:         }
21514:     }
21515:     return;
21516: }
21517: 
21518: 1;

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